// processor_info.c
#include <stdio.h>
#include <stdlib.h>
#include <mach/mach.h>
void
print_basic_info(processor_basic_info_t info)
{
printf("CPU: slot %d%s %s, type %d, subtype %d\n", info->slot_num,
(info->is_master) ? " (master)," : ",",
(info->running) ? "running" : "not running",
info->cpu_type, info->cpu_subtype);
}
void
print_cpu_load_info(processor_cpu_load_info_t info)
{
unsigned long ticks;
// Total ticks do not amount to the uptime if the machine has slept
ticks = info->cpu_ticks[CPU_STATE_USER] +
info->cpu_ticks[CPU_STATE_SYSTEM] +
info->cpu_ticks[CPU_STATE_IDLE] +
info->cpu_ticks[CPU_STATE_NICE];
printf(" %ld ticks "
"(user %ld, system %ld, idle %ld, nice %ld)\n", ticks,
info->cpu_ticks[CPU_STATE_USER],
info->cpu_ticks[CPU_STATE_SYSTEM],
info->cpu_ticks[CPU_STATE_IDLE],
info->cpu_ticks[CPU_STATE_NICE]);
printf(" cpu uptime %ld h %ld m %ld s\n",
(ticks / 100) / 3600, // hours
((ticks / 100) % 3600) / 60, // minutes
(ticks / 100) % 60); // seconds
}
int
main(void)
{
int i;
kern_return_t kr;
host_name_port_t myhost;
host_priv_t host_priv;
processor_port_array_t processor_list;
natural_t processor_count;
processor_basic_info_data_t basic_info;
processor_cpu_load_info_data_t cpu_load_info;
natural_t info_count;
myhost = mach_host_self();
kr = host_get_host_priv_port(myhost, &host_priv);
if (kr != KERN_SUCCESS) {
mach_error("host_get_host_priv_port:", kr);
exit(1);
}
kr = host_processors(host_priv, &processor_list, &processor_count);
if (kr != KERN_SUCCESS) {
mach_error("host_processors:", kr);
exit(1);
}
printf("%d processors total.\n", processor_count);
for (i = 0; i < processor_count; i++) {
info_count = PROCESSOR_BASIC_INFO_COUNT;
kr = processor_info(processor_list[i],
PROCESSOR_BASIC_INFO,
&myhost,
(processor_info_t)&basic_info,
&info_count);
if (kr == KERN_SUCCESS)
print_basic_info((processor_basic_info_t)&basic_info);
info_count = PROCESSOR_CPU_LOAD_INFO_COUNT;
kr = processor_info(processor_list[i],
PROCESSOR_CPU_LOAD_INFO,
&myhost,
(processor_info_t)&cpu_load_info,
&info_count);
if (kr == KERN_SUCCESS)
print_cpu_load_info((processor_cpu_load_info_t)&cpu_load_info);
}
// Other processor information flavors (may be unsupported)
//
// PROCESSOR_PM_REGS_INFO, // performance monitor register information
// PROCESSOR_TEMPERATURE, // core temperature
// This will deallocate while rounding up to page size
(void)vm_deallocate(mach_task_self(), (vm_address_t)processor_list,
processor_count * sizeof(processor_t *));
exit(0);
}
haidragondeMacBook-Pro:7-6 haidragon$ ls
processor_info processor_info.c
haidragondeMacBook-Pro:7-6 haidragon$ ./processor_info
host_get_host_priv_port: (os/kern) invalid argument
haidragondeMacBook-Pro:7-6 haidragon$ sudo ./processor_info
Password:
16 processors total.
CPU: slot 0 (master), running, type 7, subtype 8
1060338 ticks (user 124790, system 71872, idle 863676, nice 0)
cpu uptime 2 h 56 m 43 s
CPU: slot 1, running, type 7, subtype 8
1060233 ticks (user 953, system 1164, idle 1058116, nice 0)
cpu uptime 2 h 56 m 42 s
CPU: slot 2, running, type 7, subtype 8
1060248 ticks (user 112908, system 42124, idle 905216, nice 0)
cpu uptime 2 h 56 m 42 s
CPU: slot 3, running, type 7, subtype 8
1060232 ticks (user 924, system 923, idle 1058385, nice 0)
cpu uptime 2 h 56 m 42 s
CPU: slot 4, running, type 7, subtype 8
1060246 ticks (user 74848, system 29175, idle 956223, nice 0)
cpu uptime 2 h 56 m 42 s
CPU: slot 5, running, type 7, subtype 8
1060231 ticks (user 922, system 805, idle 1058504, nice 0)
cpu uptime 2 h 56 m 42 s
CPU: slot 6, running, type 7, subtype 8
1060244 ticks (user 52182, system 19622, idle 988440, nice 0)
cpu uptime 2 h 56 m 42 s
CPU: slot 7, running, type 7, subtype 8
1060230 ticks (user 903, system 700, idle 1058627, nice 0)
cpu uptime 2 h 56 m 42 s
CPU: slot 8, running, type 7, subtype 8
1060241 ticks (user 33667, system 12779, idle 1013795, nice 0)
cpu uptime 2 h 56 m 42 s
CPU: slot 9, running, type 7, subtype 8
1060228 ticks (user 886, system 607, idle 1058735, nice 0)
cpu uptime 2 h 56 m 42 s
CPU: slot 10, running, type 7, subtype 8
1060240 ticks (user 23411, system 8853, idle 1027976, nice 0)
cpu uptime 2 h 56 m 42 s
CPU: slot 11, running, type 7, subtype 8
1060227 ticks (user 837, system 541, idle 1058849, nice 0)
cpu uptime 2 h 56 m 42 s
CPU: slot 12, running, type 7, subtype 8
1060236 ticks (user 13741, system 5382, idle 1041113, nice 0)
cpu uptime 2 h 56 m 42 s
CPU: slot 13, running, type 7, subtype 8
1060226 ticks (user 809, system 491, idle 1058926, nice 0)
cpu uptime 2 h 56 m 42 s
CPU: slot 14, running, type 7, subtype 8
1060234 ticks (user 11096, system 4239, idle 1044899, nice 0)
cpu uptime 2 h 56 m 42 s
CPU: slot 15, running, type 7, subtype 8
1060224 ticks (user 799, system 454, idle 1058971, nice 0)
cpu uptime 2 h 56 m 42 s
haidragondeMacBook-Pro:7-6 haidragon$