CPUUsage: support display value per core

This commit is contained in:
李通洲
2023-10-10 16:00:23 +08:00
parent 6b008ed93b
commit 42cf88be9d
9 changed files with 179 additions and 61 deletions
+8 -4
View File
@@ -6,13 +6,13 @@
#include <sys/resource.h>
#include <stdlib.h>
const char* ffGetCpuUsageInfo(uint64_t* inUseAll, uint64_t* totalAll)
const char* ffGetCpuUsageInfo(FFlist* cpuTimes)
{
size_t neededLength = 0;
if(sysctlbyname("kern.cp_times", NULL, &neededLength, NULL, 0) != 0)
return "sysctlbyname(kern.cp_times, NULL) failed";
uint32_t coreCount = neededLength / (CPUSTATES * sizeof(uint64_t));
uint32_t coreCount = neededLength / (CPUSTATES * (uint32_t) sizeof(uint64_t));
assert(coreCount > 0);
FF_AUTO_FREE uint64_t (*cpTimes)[CPUSTATES] = malloc(neededLength);
@@ -24,8 +24,12 @@ const char* ffGetCpuUsageInfo(uint64_t* inUseAll, uint64_t* totalAll)
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;
FFCpuUsageInfo* info = (FFCpuUsageInfo*) ffListAdd(cpuTimes);
*info = (FFCpuUsageInfo) {
.inUseAll = (uint64_t)inUse,
.totalAll = (uint64_t)total,
};
}
return NULL;