织梦CMS - 轻松建站从此开始!

罗索

Windows下用C语言获取进程cpu使用率,内存使用,IO情况

落鹤生 发布于 2011-12-30 10:19 点击:次 
Windows下用C语言获取进程cpu使用率,内存使用,IO情况:部分代码来自MSDN的例子, 部分代码来自google chromium项目, 需要连接到psapi.lib
TAG:

一个项目需要,特地写了这些功能的函数。

process_stat.h的内容如下:

  1. /** @file 
  2. * @brief 进程统计信息函数的声明 
  3. * @author 张亚霏 
  4. * @date 2009/05/03 
  5. * @version 0.1 
  6. * 
  7. */ 
  8. #ifndef PROCESS_STAT_H 
  9. #define PROCESS_STAT_H 
  10. #ifdef __cplusplus 
  11. extern "C" { 
  12. #endif 
  13.     typedef long long           int64_t; 
  14.     typedef unsigned long long  uint64_t; 
  15.     /// 获取当前进程的cpu使用率,回-1失败 
  16.     int get_cpu_usage(); 
  17.     /// 获取当前进程内存和虚拟内存使用量,返回-1失败,0成功 
  18.     int get_memory_usage(uint64_t* mem, uint64_t* vmem); 
  19.     /// 获取当前进程总共读和写的IO字节数,返回-1失败,0成功 
  20.     int get_io_bytes(uint64_t* read_bytes, uint64_t* write_bytes); 
  21. #ifdef  __cplusplus 
  22. #endif 
  23.  
  24. #endif/*PROCESS_STAT_H*/ 

process_stat_win.c的内容如下:

  1. /** @file 
  2. * @brief 进程统计信息函数的实现 
  3. * @author 张亚霏 
  4. * @date 2009/05/03 
  5. * @version 0.1 
  6. * 
  7. * 部分代码来自MSDN的例子 
  8. * 部分代码来自google chromium项目 
  9. * 
  10. * 需要连接到psapi.lib 
  11. */ 
  12. #include <windows.h> 
  13. #include <psapi.h> 
  14. #include <assert.h> 
  15. #include "process_stat.h" 
  16.  
  17. /// 时间转换 
  18. static uint64_t file_time_2_utc(const FILETIME* ftime) 
  19.     LARGE_INTEGER li; 
  20.  
  21.     assert(ftime); 
  22.     li.LowPart = ftime->dwLowDateTime; 
  23.     li.HighPart = ftime->dwHighDateTime; 
  24.     return li.QuadPart; 
  25.  
  26. /// 获得CPU的核数 
  27. static int get_processor_number() 
  28.     SYSTEM_INFO info; 
  29.     GetSystemInfo(&info); 
  30.     return (int)info.dwNumberOfProcessors; 
  31. int get_cpu_usage() 
  32.     //cpu数量 
  33.     static int processor_count_ = -1; 
  34.     //上一次的时间 
  35.     static int64_t last_time_ = 0; 
  36.     static int64_t last_system_time_ = 0; 
  37.  
  38.     FILETIME now; 
  39.     FILETIME creation_time; 
  40.     FILETIME exit_time; 
  41.     FILETIME kernel_time; 
  42.     FILETIME user_time; 
  43.     int64_t system_time; 
  44.     int64_t time; 
  45.     int64_t system_time_delta; 
  46.     int64_t time_delta; 
  47.  
  48.     int cpu = -1; 
  49.     if(processor_count_ == -1) 
  50.     { 
  51.         processor_count_ = get_processor_number(); 
  52.     } 
  53.  
  54.     GetSystemTimeAsFileTime(&now); 
  55.  
  56.     if (!GetProcessTimes(GetCurrentProcess(), &creation_time, &exit_time, 
  57.         &kernel_time, &user_time)) 
  58.     { 
  59.         // We don't assert here because in some cases (such as in the Task  
  60.  
  61. Manager) 
  62.         // we may call this function on a process that has just exited but  
  63.  
  64. we have 
  65.         // not yet received the notification. 
  66.         return -1; 
  67.     } 
  68.     system_time = (file_time_2_utc(&kernel_time) + file_time_2_utc(&user_time))  
  69.  
  70.         processor_count_; 
  71.     time = file_time_2_utc(&now); 
  72.  
  73.     if ((last_system_time_ == 0) || (last_time_ == 0)) 
  74.     { 
  75.         // First call, just set the last values. 
  76.         last_system_time_ = system_time; 
  77.         last_time_ = time; 
  78.         return -1; 
  79.     } 
  80.  
  81.     system_time_delta = system_time - last_system_time_; 
  82.     time_delta = time - last_time_; 
  83.  
  84.     assert(time_delta != 0); 
  85.  
  86.     if (time_delta == 0) 
  87.         return -1; 
  88.  
  89.     // We add time_delta / 2 so the result is rounded. 
  90.     cpu = (int)((system_time_delta * 100 + time_delta / 2) / time_delta); 
  91.     last_system_time_ = system_time; 
  92.     last_time_ = time; 
  93.     return cpu; 
  94.  
  95. int get_memory_usage(uint64_t* mem, uint64_t* vmem) 
  96.     PROCESS_MEMORY_COUNTERS pmc; 
  97.     if(GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc))) 
  98.     { 
  99.         if(mem) *mem = pmc.WorkingSetSize; 
  100.         if(vmem) *vmem = pmc.PagefileUsage; 
  101.         return 0; 
  102.     } 
  103.     return -1; 
  104.  
  105. int get_io_bytes(uint64_t* read_bytes, uint64_t* write_bytes) 
  106.     IO_COUNTERS io_counter; 
  107.     if(GetProcessIoCounters(GetCurrentProcess(), &io_counter)) 
  108.     { 
  109.         if(read_bytes) *read_bytes = io_counter.ReadTransferCount; 
  110.         if(write_bytes) *write_bytes = io_counter.WriteTransferCount; 
  111.         return 0; 
  112.     } 
  113.     return -1; 

可以这样使用:

  1. /** @file 
  2. * @brief 进程统计信息函数的测试 
  3. * @author 张亚霏 
  4. * @date 2009/05/03 
  5. * @version 0.1 
  6. * 
  7. */ 
  8. #include "process_stat.h" 
  9. #include <stdio.h> 
  10. #include <Windows.h> 
  11.  
  12. int main()  
  13. {  
  14.     while(1)  
  15.     { 
  16.         int cpu; 
  17.         uint64_t mem, vmem, r, w; 
  18.  
  19.         cpu = get_cpu_usage(); 
  20.         get_memory_usage(&mem, &vmem); 
  21.         get_io_bytes(&r, &w); 
  22.  
  23.         printf("CPU使用率: %u\n",cpu); 
  24.         printf("内存使用: %u 字节\n", mem); 
  25.         printf("虚拟内存使用: %u 字节\n", vmem); 
  26.         printf("总共读: %u 字节\n", r); 
  27.         printf("总共写: %u 字节\n", w);  
  28.  
  29.         Sleep(1000);  
  30.     }  
  31.     return 0;  


 

(张亚霏)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201112/15557.html]
本文出处:zhangyafeikimi.iteye.com 作者:张亚霏 原文
顶一下
(2)
100%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容