获取进程CPU占用率
生活随笔
收集整理的這篇文章主要介紹了
获取进程CPU占用率
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
獲取進程CPU占用率
?
// 時間轉換 static __int64 file_time_2_utc(const FILETIME* ftime) {LARGE_INTEGER li;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; } // 獲取進程CPU占用 int get_cpu_usage(int pid) { //cpu數量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; }?
轉載于:https://www.cnblogs.com/lzpong/p/3955891.html
總結
以上是生活随笔為你收集整理的获取进程CPU占用率的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 修改Eclipse自动换行长度
- 下一篇: 频率分辨率、采样频率、采样率、采样点数与