Skip to content

Commit cabd36d

Browse files
committed
CpuUsage (apple): simplify implementation
1 parent 64bacd2 commit cabd36d

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

src/detection/cpuUsage/cpuUsage_apple.c

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,18 @@
66

77
const char* ffGetCpuUsageInfo(uint64_t* inUseAll, uint64_t* totalAll)
88
{
9-
natural_t numCPUs = 0U;
10-
processor_info_array_t cpuInfo;
11-
mach_msg_type_number_t numCpuInfo;
9+
host_cpu_load_info_data_t cpustats;
10+
mach_msg_type_number_t count = HOST_CPU_LOAD_INFO_COUNT;
11+
1212
*inUseAll = *totalAll = 0;
1313

14-
if (host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &numCPUs, &cpuInfo, &numCpuInfo) != KERN_SUCCESS)
15-
return "host_processor_info() failed";
16-
if (numCPUs * CPU_STATE_MAX != numCpuInfo)
17-
return "Unexpected host_processor_info() result";
14+
if (host_statistics(mach_host_self(), HOST_CPU_LOAD_INFO, (host_info_t)(&cpustats), &count) != KERN_SUCCESS)
15+
return "host_statistics() failed";
16+
17+
*inUseAll = cpustats.cpu_ticks[CPU_STATE_USER]
18+
+ cpustats.cpu_ticks[CPU_STATE_SYSTEM]
19+
+ cpustats.cpu_ticks[CPU_STATE_NICE];
20+
*totalAll = *inUseAll + cpustats.cpu_ticks[CPU_STATE_IDLE];
1821

19-
for (natural_t i = 0U; i < numCPUs; ++i) {
20-
integer_t inUse = cpuInfo[CPU_STATE_MAX * i + CPU_STATE_USER]
21-
+ cpuInfo[CPU_STATE_MAX * i + CPU_STATE_SYSTEM]
22-
+ cpuInfo[CPU_STATE_MAX * i + CPU_STATE_NICE];
23-
integer_t total = inUse + cpuInfo[CPU_STATE_MAX * i + CPU_STATE_IDLE];
24-
*inUseAll += (uint64_t)inUse;
25-
*totalAll += (uint64_t)total;
26-
}
2722
return NULL;
2823
}

0 commit comments

Comments
 (0)