mac os x使用Mach获取调度和虚拟内存统计信息
2019-07-02 22:30
791 查看
// host_alarm.c #include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include <mach/mach.h> #include <mach/clock.h> #define OUT_ON_MACH_ERROR(msg, retval) \ if (kr != KERN_SUCCESS) { mach_error(msg ":" , kr); goto out; } // Structure for the IPC message we will receive from the clock typedef struct msg_format_recv_s { mach_msg_header_t header; int data; mach_msg_trailer_t trailer; } msg_format_recv_t; int main() { kern_return_t kr; clock_serv_t clk_system; mach_timespec_t alarm_time; clock_reply_t alarm_port; struct timeval t1, t2; msg_format_recv_t message; mach_port_t mytask; // The C library optimized this call by returning the task port's value // that it caches in the mach_task_self_ variable mytask = mach_task_self(); kr = host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, (clock_serv_t *)&clk_system); OUT_ON_MACH_ERROR("host_get_clock_service", kr); // Let us set the alarm to ring after 2.5 seconds alarm_time.tv_sec = 2; alarm_time.tv_nsec = 500; // Allocate a port (specifically, get receive right for the new port) // We will use this port to receive the alarm message from the clock kr = mach_port_allocate( mytask, // the task acquiring the port right MACH_PORT_RIGHT_RECEIVE, // type of right &alarm_port); // task's name for the port right OUT_ON_MACH_ERROR("mach_port_allocate", kr); gettimeofday(&t1, NULL); // Set the alarm kr = clock_alarm(clk_system, // the clock to use TIME_RELATIVE, // how to interpret alarm time alarm_time, // the alarm time alarm_port); // this port will receive the alarm message OUT_ON_MACH_ERROR("clock_alarm", kr); printf("Current time %ld s + %d us\n" "Setting alarm to ring after %d s + %d ns\n", t1.tv_sec, t1.tv_usec, alarm_time.tv_sec, alarm_time.tv_nsec); // Wait to receive the alarm message (we will block here) kr = mach_msg(&(message.header), // the message buffer MACH_RCV_MSG, // message option bits 0, // send size (we are receiving, so 0) message.header.msgh_size,// receive limit alarm_port, // receive right MACH_MSG_TIMEOUT_NONE, // no timeout MACH_PORT_NULL); // no timeout notification port // We should have received an alarm message at this point gettimeofday(&t2, NULL); OUT_ON_MACH_ERROR("mach_msg", kr); if (t2.tv_usec < t1.tv_usec) { t1.tv_sec += 1; t1.tv_usec -= 1000000; } printf("\nCurrent time %ld s + %d us\n", t2.tv_sec, t2.tv_usec); printf("Alarm rang after %ld s + %d us\n", (t2.tv_sec - t1.tv_sec), (t2.tv_usec - t1.tv_usec)); out: mach_port_deallocate(mytask, clk_system); // Release user reference for the receive right we created mach_port_deallocate(mytask, alarm_port); exit(0); }
haidragondeMacBook-Air:6-4 haidragon$ ls host_statistics.c haidragondeMacBook-Air:6-4 haidragon$ gcc -Wall -o host_statistics host_statistics.c host_statistics.c:83:51: warning: format specifies type 'unsigned int' but the argument has type 'vm_size_t' (aka 'unsigned long') [-Wformat] printf(" page size = %u bytes\n", page_size); ~~ ^~~~~~~~~ %lu 1 warning generated. haidragondeMacBook-Air:6-4 haidragon$ ls host_statistics host_statistics.c haidragondeMacBook-Air:6-4 haidragon$ ./host_statistics Host statistics: Host load statistics time period (sec) 5 30 60 load average 3652 3840 3894 Mach factor 1432 1137 1035 Cumulative CPU load statistics User state ticks = 1982520 System state ticks = 1584434 Nice state ticks = 0 Idle state ticks = 4682452 Virtual memory statistics page size = 4096 bytes pages free = 37787 pages active = 743035 pages inactive = 741807 pages wired down = 394144 zero fill pages = 75376370 pages reactivated = 298997 pageins = 2638736 pageouts = 16703 translation faults = 604768506 copy-on-write faults = 92497463 object cache lookups = 0 object cache hits = 0 (hit rate nan %) haidragondeMacBook-Air:6-4 haidragon$ ``
相关文章推荐
- mac 系统使用 psutil 模块获取系统 cpu,内存等信息
- 如何 在 Mac OS X 里通过命令行获取 CPU 信息
- 有关安装和使用 Java 的 Mac OS X 信息
- 命令行获取 mac os x 系统信息
- ASP.NET获取客户端网卡使用的MAC地址信息
- [转]排查线程池使用情况,可定时任务统计.打印到指定日志. 获取系统扩容,线程池改造信息.
- os环境下,使用bsd实现echo server并如何获取本地网络信息
- node中使用os模块获取操作系统信息
- Delphi For Android 使用Posix函数获取网络信息(MAC, IP...)
- ASP.NET获取客户端网卡使用的MAC地址信息
- VC++获取系统信息/获取OS/获取MAC/获取本地IP/判断是否为网吧
- VC++获取系统信息/获取OS/获取MAC/获取本地IP/判断是否为网吧
- [Mac OS X Cocoa编程]如何获取其他应用程序的窗体标题以及相关信息?
- node-webkit 使用os模块获取用户MAC值和IP
- 使用 atmadm 来显示 ATM 适配器上传入和传出呼叫的统计信息
- 在vb中使用Iphlpapi.dll获取网络信息 第二章 第十一节 获取当前计算机的IP信息
- Javscript获取客户端常用信息,做访问统计比较有用...
- 在 Mac OS X 中启用和使用 root 用户
- 获取客户端的信息(版本,OS,IP)(NET2.0)
- 在vb中使用Iphlpapi.dll获取网络信息 第二章 第十五节 设置本机默认的生存时间(time-to-live:TTL)值