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>
|
|
|
|
|
|
|
|
|
|
// We need to use uint64_t because sizeof(long) == 4 on Windows
|
|
|
|
|
const char* ffGetCpuUsageInfo(uint64_t* inUseAll, uint64_t* totalAll);
|
|
|
|
|
|
2022-11-25 23:14:41 +08:00
|
|
|
static uint64_t inUseAll1, totalAll1;
|
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
|
|
|
{
|
|
|
|
|
ffGetCpuUsageInfo(&inUseAll1, &totalAll1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char* ffGetCpuUsageResult(double* result)
|
|
|
|
|
{
|
|
|
|
|
const char* error = NULL;
|
2022-11-25 23:14:41 +08:00
|
|
|
if(inUseAll1 == 0 && totalAll1 == 0)
|
2022-10-07 19:02:27 +08:00
|
|
|
{
|
|
|
|
|
error = ffGetCpuUsageInfo(&inUseAll1, &totalAll1);
|
|
|
|
|
if(error)
|
|
|
|
|
return error;
|
2022-11-25 23:14:41 +08:00
|
|
|
ffTimeSleep(200);
|
2022-10-07 19:02:27 +08:00
|
|
|
}
|
|
|
|
|
|
2022-11-15 17:48:57 +08:00
|
|
|
while(true)
|
|
|
|
|
{
|
|
|
|
|
uint64_t inUseAll2, totalAll2;
|
|
|
|
|
error = ffGetCpuUsageInfo(&inUseAll2, &totalAll2);
|
|
|
|
|
if(error)
|
|
|
|
|
return error;
|
2022-10-07 19:02:27 +08:00
|
|
|
|
2022-11-15 17:48:57 +08:00
|
|
|
if(inUseAll2 != inUseAll1)
|
|
|
|
|
{
|
|
|
|
|
*result = (double)(inUseAll2 - inUseAll1) / (double)(totalAll2 - totalAll1) * 100;
|
2023-08-18 13:28:07 +08:00
|
|
|
inUseAll1 = inUseAll2;
|
|
|
|
|
totalAll1 = totalAll2;
|
2022-11-15 17:48:57 +08:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
else
|
2022-11-25 23:14:41 +08:00
|
|
|
ffTimeSleep(200);
|
2022-11-15 17:48:57 +08:00
|
|
|
}
|
2022-10-07 19:02:27 +08:00
|
|
|
}
|