Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2445168
  • 博文数量: 328
  • 博客积分: 4302
  • 博客等级: 上校
  • 技术积分: 5486
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-01 11:14
个人简介

悲剧,绝对的悲剧,悲剧中的悲剧。

文章分类

全部博文(328)

文章存档

2017年(6)

2016年(18)

2015年(28)

2014年(73)

2013年(62)

2012年(58)

2011年(55)

2010年(28)

分类: iOS平台

2017-07-12 22:13:08

To get the memory


  1. #import <mach/mach.h>

  2. // ...

  3. void report_memory(void) {
  4.   struct task_basic_info info;
  5.   mach_msg_type_number_t size = sizeof(info);
  6.   kern_return_t kerr = task_info(mach_task_self(),
  7.                                  TASK_BASIC_INFO,
  8.                                  (task_info_t)&info,
  9.                                  &size);
  10.   if( kerr == KERN_SUCCESS ) {
  11.     NSLog(@"Memory in use (in bytes): %lu", info.resident_size);
  12.     NSLog(@"Memory in use (in MB): %f", ((CGFloat)info.resident_size / 1000000));
  13.   } else {
  14.     NSLog(@"Error with task_info(): %s", mach_error_string(kerr));
  15.   }
  16. }

To get the CPU


  1. float cpu_usage()
  2. {
  3.     kern_return_t kr;
  4.     task_info_data_t tinfo;
  5.     mach_msg_type_number_t task_info_count;
  6.     
  7.     task_info_count = TASK_INFO_MAX;
  8.     kr = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)tinfo, &task_info_count);
  9.     if (kr != KERN_SUCCESS) {
  10.         return -1;
  11.     }
  12.     
  13.     task_basic_info_t basic_info;
  14.     thread_array_t thread_list;
  15.     mach_msg_type_number_t thread_count;
  16.     
  17.     thread_info_data_t thinfo;
  18.     mach_msg_type_number_t thread_info_count;
  19.     
  20.     thread_basic_info_t basic_info_th;
  21.     uint32_t stat_thread = 0;
  22.     
  23.     basic_info = (task_basic_info_t)tinfo;
  24.     
  25.     kr = task_threads(mach_task_self(), &thread_list, &thread_count);
  26.     if (kr != KERN_SUCCESS) {
  27.         return -1;
  28.     }
  29.     if (thread_count > 0)
  30.         stat_thread += thread_count;
  31.     
  32.     long tot_sec = 0;
  33.     long tot_usec = 0;
  34.     float tot_cpu = 0;
  35.     int j;
  36.     
  37.     for (j = 0; j < thread_count; j++)
  38.     {
  39.         thread_info_count = THREAD_INFO_MAX;
  40.         kr = thread_info(thread_list[j], THREAD_BASIC_INFO,
  41.                          (thread_info_t)thinfo, &thread_info_count);
  42.         if (kr != KERN_SUCCESS) {
  43.             return -1;
  44.         }
  45.         
  46.         basic_info_th = (thread_basic_info_t)thinfo;
  47.         
  48.         if (!(basic_info_th->flags & TH_FLAGS_IDLE)) {
  49.             tot_sec = tot_sec + basic_info_th->user_time.seconds + basic_info_th->system_time.seconds;
  50.             tot_usec = tot_usec + basic_info_th->system_time.microseconds + basic_info_th->system_time.microseconds;
  51.             tot_cpu = tot_cpu + basic_info_th->cpu_usage / (float)TH_USAGE_SCALE * 100.0;
  52.         }
  53.         
  54.     }
  55.     
  56.     kr = vm_deallocate(mach_task_self(), (vm_offset_t)thread_list, thread_count * sizeof(thread_t));
  57.     assert(kr == KERN_SUCCESS);
  58.     
  59.     return tot_cpu;
  60. }
阅读(1338) | 评论(0) | 转发(0) |
0

上一篇:Python Pytest

下一篇:没有了

给主人留下些什么吧!~~