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

33 lines
1020 B
C
Raw Normal View History

2023-03-17 17:56:31 +08:00
#include "detection/cpuusage/cpuusage.h"
#include "util/mallocHelper.h"
2022-10-27 10:08:22 +08:00
#include <sys/types.h>
#include <sys/sysctl.h>
#include <sys/resource.h>
#include <stdlib.h>
2022-10-27 10:08:22 +08:00
const char* ffGetCpuUsageInfo(uint64_t* inUseAll, uint64_t* totalAll)
{
size_t neededLength = 0;
if(sysctlbyname("kern.cp_times", NULL, &neededLength, NULL, 0) != 0)
return "sysctlbyname(kern.cp_times, NULL) failed";
2022-10-27 10:08:22 +08:00
uint32_t coreCount = neededLength / (CPUSTATES * sizeof(uint64_t));
assert(coreCount > 0);
FF_AUTO_FREE uint64_t (*cpTimes)[CPUSTATES] = malloc(neededLength);
if(sysctlbyname("kern.cp_times", cpTimes, &neededLength, NULL, 0) != 0)
return "sysctlbyname(kern.cp_times, cpTime) failed";
for (uint32_t i = 0; i < coreCount; ++i)
{
uint64_t* cpTime = cpTimes[i];
uint64_t inUse = cpTime[CP_USER] + cpTime[CP_NICE] + cpTime[CP_SYS];
uint64_t total = cpTime[CP_INTR] + cpTime[CP_IDLE];
*inUseAll += inUse;
*totalAll += total;
}
2022-10-27 10:08:22 +08:00
return NULL;
}