Skip to content

Commit 2feb531

Browse files
committed
Revert "CpuUsage (apple): simplify implementation"
This reverts commit cabd36d.
1 parent e7df944 commit 2feb531

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

src/detection/cpuusage/cpuusage_apple.c

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

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

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];
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";
2118

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+
}
2227
return NULL;
2328
}

0 commit comments

Comments
 (0)