如何在mach内核中获得cpu使用?

时间:2015-05-17 09:40:51

标签: c mach xnu

我尝试在mach内核中获取cpu用法,但我总是得到相同的输出。我做错了什么?

static mach_port_t port;

获取cpu负载统计信息

int
load_get(host_info_t r_load) {
   mach_msg_type_number_t count;

   count = HOST_CPU_LOAD_INFO_COUNT;
   host_statistics(port, HOST_CPU_LOAD_INFO, r_load, &count);

   return 0;
}


int main(void) {

   port = mach_host_self();

   host_cpu_load_info_data_t cpu;

   load_get((host_info_t)&cpu);

   unsigned long long userticks, systicks, idleticks, totalticks;
   unsigned long long userwhole, userpart, syswhole, syspart, idlewhole, idlepart;

收集cpu ticks

   userticks = cpu.cpu_ticks[CPU_STATE_USER] + cpu.cpu_ticks[CPU_STATE_NICE];
   systicks = cpu.cpu_ticks[CPU_STATE_SYSTEM];
   idleticks = cpu.cpu_ticks[CPU_STATE_IDLE];

   totalticks = userticks + systicks + idleticks;

   return 0;
}

谢谢!

0 个答案:

没有答案