|
6 | 6 |
|
7 | 7 | const char* ffGetCpuUsageInfo(uint64_t* inUseAll, uint64_t* totalAll) |
8 | 8 | { |
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; |
12 | 12 | *inUseAll = *totalAll = 0; |
13 | 13 |
|
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"; |
21 | 18 |
|
| 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 | + } |
22 | 27 | return NULL; |
23 | 28 | } |
0 commit comments