程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 用C語言獲取進程cpu使用率,內存使用,IO情況

用C語言獲取進程cpu使用率,內存使用,IO情況

編輯:關於C語言

/** @file
* @brief 進程統計信息函數的聲明
* @author 張亞霏
* @date 2009/05/03
* @version 0.1
*
*/
#ifndef PROCESS_STAT_H
#define PROCESS_STAT_H


#ifdef __cplusplus
extern "C" {
#endif

 typedef long long           int64_t;
 typedef unsigned long long uint64_t;


 /// 獲取當前進程的cpu使用率,返回-1失敗
 int get_cpu_usage();


 /// 獲取當前進程內存和虛擬內存使用量,返回-1失敗,0成功
 int get_memory_usage(uint64_t* mem, uint64_t* vmem);


 /// 獲取當前進程總共讀和寫的IO字節數,返回-1失敗,0成功
 int get_io_bytes(uint64_t* read_bytes, uint64_t* write_bytes);


#ifdef  __cplusplus
}
#endif

#endif/*PROCESS_STAT_H*/

/** @file
* @brief 進程統計信息函數的實現
* @author 張亞霏
* @date 2009/05/03
* @version 0.1
*
* 部分代碼來自MSDN的例子
* 部分代碼來自google chromium項目
*
* 需要連接到psapi.lib
*/


#include <windows.h>
#include <psapi.h>
#include <assert.h>
#include "process_stat.h"

/// 時間轉換
static uint64_t file_time_2_utc(const FILETIME* ftime)
{
 LARGE_INTEGER li;

 assert(ftime);
 li.LowPart = ftime->dwLowDateTime;
 li.HighPart = ftime->dwHighDateTime;
 return li.QuadPart;
}


/// 獲得CPU的核數
static int get_processor_number()
{
 SYSTEM_INFO info;
 GetSystemInfo(&info);
 return (int)info.dwNumberOfProcessors;
}


int get_cpu_usage()
{
 //cpu數量
 static int processor_count_ = -1;
 //上一次的時間
 static int64_t last_time_ = 0;
 static int64_t last_system_time_ = 0;


 FILETIME now;
 FILETIME creation_time;
 FILETIME exit_time;
 FILETIME kernel_time;
 FILETIME user_time;
 int64_t system_time;
 int64_t time;
 int64_t system_time_delta;
 int64_t time_delta;

 int cpu = -1;


 if(processor_count_ == -1)
 {
  processor_count_ = get_processor_number();
 }

 GetSystemTimeAsFileTime(&now);

 if (!GetProcessTimes(GetCurrentProcess(), &creation_time, &exit_time,
  &kernel_time, &user_time))
 {
  // We dont assert here because in some cases (such as in the Task

Manager)
  // we may call this function on a process that has just exited but

we have
  // not yet received the notification.
  return -1;
 }
 system_time = (file_time_2_utc(&kernel_time) + file_time_2_utc(&user_time))

/
  processor_count_;
 time = file_time_2_utc(&now);

 if ((last_system_time_ == 0) || (last_time_ == 0))
 {
  // First call, just set the last values.
  last_system_time_ = system_time;
  last_time_ = time;
  return -1;
 }

 system_time_delta = system_time - last_system_time_;
 time_delta = time - last_time_;

 assert(time_delta != 0);

 if (time_delta == 0)
  return -1;

 // We add time_delta / 2 so the result is rounded.
 cpu = (int)((system_time_delta * 100 + time_delta / 2) / time_delta);
 last_system_time_ = system_time;
 last_time_ = time;
 return cpu;
}

int get_memory_usage(uint64_t* mem, uint64_t* vmem)
{
 PROCESS_MEMORY_COUNTERS pmc;
 if(GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc)))
 {
  if(mem) *mem = pmc.WorkingSetSize;
  if(vmem) *vmem = pmc.PagefileUsage;
  return 0;
 }
 return -1;
}

int get_io_bytes(uint64_t* read_bytes, uint64_t* write_bytes)
{
 IO_COUNTERS io_counter;
 if(GetProcessIoCounters(GetCurrentProcess(), &io_counter))
 {
  if(read_bytes) *read_bytes = io_counter.ReadTransferCount;
  if(write_bytes) *write_bytes = io_counter.WriteTransferCount;
  return 0;
 }
 return -1;
}

/** @file
* @brief 進程統計信息函數的測試
* @author 張亞霏
* @date 2009/05/03
* @version 0.1
*
*/

#include "process_stat.h"
#include <stdio.h>
#include <Windows.h>

int main()
{
 while(1)
 {
  int cpu;
  uint64_t mem, vmem, r, w;


  cpu = get_cpu_usage();
  get_memory_usage(&mem, &vmem);
  get_io_bytes(&r, &w);

  printf("CPU使用率: %u ",cpu);
  printf("內存使用: %u 字節 ", mem);
  printf("虛擬內存使用: %u 字節 ", vmem);
  printf("總共讀: %u 字節 ", r);
  printf("總共寫: %u 字節 ", w);

  Sleep(1000);
 }
 return 0;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved