mac os x使用Mach获取调试和虚拟内存统计信息

// 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$
``

原文地址:https://blog.51cto.com/haidragon/2416423

时间: 2024-08-29 23:27:34

mac os x使用Mach获取调试和虚拟内存统计信息的相关文章

mac os x使用Mach调用获取基本的主机信息

// host_basic_info.c #include <stdio.h> #include <stdlib.h> #include <mach/mach.h> #define EXIT_ON_MACH_ERROR(msg, retval) if (kr != KERN_SUCCESS) { mach_error(msg ":" , kr); exit((retval)); } int main() { kern_return_t kr; //

mac os x在Mach中获取时钟基本属性和时间值

// host_clock.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);

mac os x使用Mach调用设置时间警报(回调)

// 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);

Mac OS X 配置 Apache+Mysql+PHP 详细教程

网上的教程已经有很多,这里简洁的记录一下.以 Mac OS X Mavericks 10.9.X 为例. 先附上如何进入指定目录文件夹,按键盘 Command + Shift + G ,然后输入指定目录名即可. 工具/原料 以 Mac OS X Mavericks 10.9.X 为例. 一.启动 Apache 1 Mac OS X 系统已经集成了 Apache+PHP 环境,依次进入“系统偏好设置 -> 共享” ,开启“Web 共享”,就可以打开 Apache. 但新版的 Mac OS X 中,

MAC OS开发之从入门到崩溃(一)

本文目标:通过xcode,创建一个Mac app程序.点击按钮,弹出Hello World窗口,其效果如下. 我们可以通过4个步骤来实现如上效果. 1.创建一个MAC app工程项目. 2.布局"按钮"控件 3.建立相应"连接" 4.编写弹窗代码. 一.创建Mac App工程项目 1.打开xcode,选择Create a new Xcode project. 2.找到mac os选项,选择Cocoa App 3.填写相关信息. 4.选择文件夹,点击create. 至

mac os 用真机调试android应用

首先,要安装filetransfer.dmg程序,才能把android机连接到mac os上面 1,设置adb的环境变量 查找android tools所在的目录,在android tools下面有adb工具,目录如下“/Users/fenghewei/Android/Android_tools/android-sdk-macosx/platform-tools”,把这路径加入到~/.bash_profile文件中 #adb ANDROID_ADB=/Users/fenghewei/Androi

Mac OS X 10.10, Eclipse+ADT真机调试代码时,Device Chooser中不显示真机的解决方案

Mac OS X 10.10的环境下,Eclipse+ADT,进行真机调试时,会出现一个问题. Device Chooser对话框里不显示真机设备,只有重新插拔数据线才可以.经过测试,有两个临时解决方案 第一个方案 保持Deivce Chooser 对话框打开,然后在手机上,把debug调试的复选框,取消,再勾选一次,就能出现真机设备. 第二个方案更好一些,可以进入该项目的,Run Configurations设置界面,选择target,选择Launch on all compatible de

解决魅族USB调试无法被电脑识别的问题(含Mac OS X、Win7)

每次打开豌豆荚或者360手机助手之类手机助手后Eclipse才会检测到mx4(实际上是豌豆荚关闭eclipse的adb使用自己的驱动连接的).解决方法就是在"adb_usb.ini"文件中手动加上厂商ID就可以了.首先保证你的ADB是最新版本:http://stackoverflow.com/questio ... adb-version-1-0-29. ?    MAC的设置方法:1.把Android手机开启调试模式,然后连接在我们的Mac OS上.2.选择Mac的 关于本机->

笔记:MAC OS X下配置PHP开发、调试环境

操作系统:MAC OS X 工具:MAMP.PhpStorm.xdebug.chrome 1.下载MAMP 2.安装比较简单,安装完成后,应用程序中会增加如下4个应用 MacGDBp是PHP调试器,使用的是xdebug 若要使用此调试器,需要在php.ini中配置: [xdebug] zend_extension="/Applications/MAMP/bin/php/php5.5.10/lib/php/extensions/no-debug-non-zts-20121212/xdebug.so