Files
fastfetch/src/detection/cpuusage/cpuusage.c
T

46 lines
1.1 KiB
C
Raw Normal View History

#include "fastfetch.h"
2023-03-17 17:56:31 +08:00
#include "detection/cpuusage/cpuusage.h"
#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;
void ffPrepareCPUUsage(void)
{
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)
{
error = ffGetCpuUsageInfo(&inUseAll1, &totalAll1);
if(error)
return error;
2022-11-25 23:14:41 +08:00
ffTimeSleep(200);
}
2022-11-15 17:48:57 +08:00
while(true)
{
uint64_t inUseAll2, totalAll2;
error = ffGetCpuUsageInfo(&inUseAll2, &totalAll2);
if(error)
return error;
2022-11-15 17:48:57 +08:00
if(inUseAll2 != inUseAll1)
{
*result = (double)(inUseAll2 - inUseAll1) / (double)(totalAll2 - totalAll1) * 100;
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
}
}