C++ 獲取進程CPU占用率
核心代碼
// 時間轉(zhuǎn)換 static __int64 file_time_2_utc(const FILETIME* ftime) { LARGE_INTEGER li; li.LowPart = ftime->dwLowDateTime; li.HighPart = ftime->dwHighDateTime; return li.QuadPart; } // 獲得CPU的核數(shù) static int get_processor_number() { SYSTEM_INFO info; GetSystemInfo(&info); return (int)info.dwNumberOfProcessors; } // 獲取進程CPU占用 int get_cpu_usage(int pid) { //cpu數(shù)量 static int processor_count_ = -1; //上一次的時間 static __int64 last_time_ = 0; static __int64 last_system_time_ = 0; FILETIME now; FILETIME creation_time; FILETIME exit_time; FILETIME kernel_time; FILETIME user_time; __int64 system_time; __int64 time; __int64 system_time_delta; __int64 time_delta; int cpu = -1; if(processor_count_ == -1) { processor_count_ = get_processor_number(); } GetSystemTimeAsFileTime(&now); HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, pid); if (!GetProcessTimes(hProcess, &creation_time, &exit_time, &kernel_time, &user_time)) { 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)) { last_system_time_ = system_time; last_time_ = time; return -1; } system_time_delta = system_time - last_system_time_; time_delta = time - last_time_; if (time_delta == 0) return -1; cpu = (int)((system_time_delta * 100 + time_delta / 2) / time_delta); last_system_time_ = system_time; last_time_ = time; return cpu; }
以下是其它網(wǎng)友的補充
C++ 獲取進程內(nèi)存占用和CPU利用率等信息
1.獲取內(nèi)存占用信息
獲取步驟:
(1)獲取當(dāng)前進程句柄 使用GetCurrentProcess(),返回一個當(dāng)前進程的句柄
(2)定義一個保存內(nèi)存信息的結(jié)構(gòu)體 PROCESS_MEMORY_COUNTERS pmc;
結(jié)構(gòu)體定義如下:
typedef struct _PROCESS_MEMORY_COUNTERS { DWORD cb; Size of the structure, in bytes.//結(jié)構(gòu)體大小 DWORD PageFaultCount; Number of page faults. // 缺頁中斷次數(shù) SIZE_T PeakWorkingSetSize; Peak working set size, in bytes. // 使用內(nèi)存高峰 SIZE_T WorkingSetSize; Current working set size, in bytes. // 當(dāng)前使用的內(nèi)存 SIZE_T QuotaPeakPagedPoolUsage; Peak paged pool usage, in bytes. // 使用頁面緩存池高峰 SIZE_T QuotaPagedPoolUsage; Current paged pool usage, in bytes.// 使用頁面緩存池 SIZE_T QuotaPeakNonPagedPoolUsage; Peak nonpaged pool usage, in bytes.// 使用非分頁緩存池高峰 SIZE_T QuotaNonPagedPoolUsage; Current nonpaged pool usage, in bytes.// 使用非分頁緩存池 SIZE_T PagefileUsage; Current space allocated for the pagefile, in bytes.Those pages may or may not be in memory.// 使用分頁文件 SIZE_T PeakPagefileUsage; Peak space allocated for the pagefile, in bytes.// 使用分頁文件高峰 } PROCESS_MEMORY_COUNTERS, *PPROCESS_MEMORY_COUNTERS;
(3)獲取當(dāng)前進程的內(nèi)存信息,保存到結(jié)構(gòu)體pmc中(第二個參數(shù)) 使用GetProcessMemoryInfo()
API定義如下:
GetProcessMemoryInfo(
HANDLE Process, 獲取內(nèi)存使用情況的進程句柄。
PPROCESS_MEMORY_COUNTERS ppsmemCounters, 返回內(nèi)存使用情況的結(jié)構(gòu)
DWORD cb 結(jié)構(gòu)的大小
);DE
2.獲取CPU利用率
獲取步驟:
(1)獲取當(dāng)前進程句柄 通過OpenProcess(),返回一個進程句柄
函數(shù)原型如下:
HANDLE OpenProcess( DWORD dwDesiredAccess, //渴望得到的訪問權(quán)限(標(biāo)志) BOOL bInheritHandle, // 是否繼承句柄 DWORD dwProcessId// 進程標(biāo)示符,可以通過getpid()獲取當(dāng)前進程ID );
(2)獲取CPU使用時間 通過調(diào)用GetProcessTimes()
函數(shù)原型如下:
BOOL WINAPI GetProcessTimes( __in HANDLE hProcess, 需要獲取相關(guān)時間的進程句柄 __out LPFILETIME lpCreationTime, 進程的創(chuàng)建時間 __out LPFILETIME lpExitTime, 進程的退出時間 __out LPFILETIME lpKernelTime, 進程在內(nèi)核模式下的所有時間 __out LPFILETIME lpUserTime 進程在用戶模式下的所有時間 );
CPU使用時間=(lpKernelTime+lpUserTime)/GetProcessNumber()(內(nèi)核數(shù))
內(nèi)核數(shù)獲取方法如下:
int GetProcessNumber() { SYSTEM_INFO info; GetSystemInfo(&info); return (int)info.dwNumberOfProcessors; }
(3)計算CPU利用率
CPU利用率=(現(xiàn)在的CPU占用時間-過去的CPU占用時間)/系統(tǒng)時間差
注:系統(tǒng)時間差可以通過GetSystemTimeAsFileTime()獲取,然后在轉(zhuǎn)換為int64類型即可,自定義轉(zhuǎn)換方法如下:
__int64 FileTimeToInt64(const FILETIME& time) { ULARGE_INTEGER tt; //64位無符號整型值 tt.LowPart = time.dwLowDateTime; tt.HighPart = time.dwHighDateTime; return(tt.QuadPart); //返回整型值 }
這篇文章就介紹到這了,需要的朋友可以參考一下。
欄 目:C語言
下一篇:基于C++11的threadpool線程池(簡潔且可以帶任意多的參數(shù))
本文標(biāo)題:C++ 獲取進程CPU占用率
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/348.html
您可能感興趣的文章


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery
- 01-10C#中split用法實例總結(jié)
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-11ajax實現(xiàn)頁面的局部加載