2022-10-07 19:02:27 +08:00
|
|
|
#include "fastfetch.h"
|
2023-03-17 17:56:31 +08:00
|
|
|
#include "detection/cpuusage/cpuusage.h"
|
2022-10-07 19:02:27 +08:00
|
|
|
#include "common/time.h"
|
|
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
2023-10-10 16:00:23 +08:00
|
|
|
static FFlist cpuTimes1;
|
2022-10-07 19:02:27 +08:00
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
void ffPrepareCPUUsage(void)
|
2022-10-07 19:02:27 +08:00
|
|
|
{
|
2023-10-10 16:00:23 +08:00
|
|
|
assert(cpuTimes1.elementSize == 0);
|
|
|
|
|
ffListInit(&cpuTimes1, sizeof(FFCpuUsageInfo));
|
|
|
|
|
ffGetCpuUsageInfo(&cpuTimes1);
|
2022-10-07 19:02:27 +08:00
|
|
|
}
|
|
|
|
|
|
2023-10-10 16:00:23 +08:00
|
|
|
const char* ffGetCpuUsageResult(FFlist* result)
|
2022-10-07 19:02:27 +08:00
|
|
|
{
|
|
|
|
|
const char* error = NULL;
|
2023-10-10 16:00:23 +08:00
|
|
|
if(cpuTimes1.elementSize == 0)
|
2022-10-07 19:02:27 +08:00
|
|
|
{
|
2023-10-10 16:00:23 +08:00
|
|
|
ffListInit(&cpuTimes1, sizeof(FFCpuUsageInfo));
|
|
|
|
|
error = ffGetCpuUsageInfo(&cpuTimes1);
|
|
|
|
|
if(error) return error;
|
2022-11-25 23:14:41 +08:00
|
|
|
ffTimeSleep(200);
|
2022-10-07 19:02:27 +08:00
|
|
|
}
|
|
|
|
|
|
2023-10-10 16:00:23 +08:00
|
|
|
if(cpuTimes1.length == 0) return "No CPU cores found";
|
|
|
|
|
|
|
|
|
|
FF_LIST_AUTO_DESTROY cpuTimes2 = ffListCreate(sizeof(FFCpuUsageInfo));
|
2022-10-07 19:02:27 +08:00
|
|
|
|
2023-10-10 16:00:23 +08:00
|
|
|
retry:
|
|
|
|
|
error = ffGetCpuUsageInfo(&cpuTimes2);
|
|
|
|
|
if(error) return error;
|
|
|
|
|
if(cpuTimes1.length != cpuTimes2.length) return "Unexpected CPU usage result";
|
|
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < cpuTimes1.length; ++i)
|
|
|
|
|
{
|
|
|
|
|
FFCpuUsageInfo* cpuTime1 = ffListGet(&cpuTimes1, i);
|
|
|
|
|
FFCpuUsageInfo* cpuTime2 = ffListGet(&cpuTimes2, i);
|
|
|
|
|
if (cpuTime2->totalAll <= cpuTime1->totalAll)
|
2022-11-15 17:48:57 +08:00
|
|
|
{
|
2023-10-10 16:00:23 +08:00
|
|
|
ffListClear(&cpuTimes2);
|
2022-11-25 23:14:41 +08:00
|
|
|
ffTimeSleep(200);
|
2023-10-10 16:00:23 +08:00
|
|
|
goto retry;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < cpuTimes1.length; ++i)
|
|
|
|
|
{
|
|
|
|
|
FFCpuUsageInfo* cpuTime1 = ffListGet(&cpuTimes1, i);
|
|
|
|
|
FFCpuUsageInfo* cpuTime2 = ffListGet(&cpuTimes2, i);
|
|
|
|
|
*(double*) ffListAdd(result) = (double)(cpuTime2->inUseAll - cpuTime1->inUseAll) / (double)(cpuTime2->totalAll - cpuTime1->totalAll) * 100;
|
|
|
|
|
cpuTime1->inUseAll = cpuTime2->inUseAll;
|
|
|
|
|
cpuTime1->totalAll = cpuTime2->totalAll;
|
2022-11-15 17:48:57 +08:00
|
|
|
}
|
2023-10-10 16:00:23 +08:00
|
|
|
return NULL;
|
2022-10-07 19:02:27 +08:00
|
|
|
}
|