本文配套程式為:http://download.csdn.net/detail/morewindows/5160810
轉載請標明出處,原文地址:http://blog.csdn.net/morewindows/article/details/8678359
歡迎關注微博:http://weibo.com/MoreWindows
上一篇《Windows系統CPU記憶體網路效能統計第一篇記憶體》(http://blog.csdn.net/morewindows/article/details/8459219)介紹了在Windows系統下如何查看系統記憶體總量,使用率等資訊。接下在將用三篇部落格來介紹如何擷取CPU整體使用率及多核CPU各核的使用率。
Windows系統CPU記憶體網路效能統計部落格目錄:
1.《Windows系統CPU記憶體網路效能統計第一篇記憶體》
http://blog.csdn.net/morewindows/article/details/8459219
2.《Windows系統CPU記憶體網路效能統計第二篇 CPU CPU整體使用率》
http://blog.csdn.net/morewindows/article/details/8678359
3.《Windows系統CPU記憶體網路效能統計第三篇 CPU 多核CPU各核使用率 C#》
http://blog.csdn.net/morewindows/article/details/8678382
4.《Windows系統CPU記憶體網路效能統計第四篇 CPU多核CPU各核使用率 C++》
http://blog.csdn.net/morewindows/article/details/8678396
如何擷取CPU整體使用率及多核CPU各核的使用率在網上已經有很多文章了,在各大論壇上也有很多人在提問。本人在網上搜尋了下,之前很多文章都是介紹使用ntdll.dll中一個未公開的API函數——NtQuerySystemInformation。基本代碼結構如下:
// 獲得系統CPU使用率 By MoreWindows( http://blog.csdn.net/MoreWindows )
typedef LONG (WINAPI *PROCNTQSI)(UINT, PVOID, ULONG, PULONG);
pFunNtQuerySystemInf = (PROCNTQSI)GetProcAddress(GetModuleHandle("ntdll"), "NtQuerySystemInformation");
這種方法肯定不太好,這個API函數既然未公開,那麼說明微軟公司不支援開發應用程式時直接使用這個函數,並且在以後的系統升級中ntdll.dll很可能去掉這個API函數。經過本人MoreWindows(http://weibo.com/MoreWindows)的實地測試,這種使用NtQuerySystemInformation的程式在Win7下都無法擷取CPU使用率。
為了讓我們的代碼能在WinXP及Win7多系統中正常運行。本人實現了一個CCPUUseRate類。下面是這個CCPUUseRate類的實現代碼:
// 獲得系統CPU使用率// http://blog.csdn.net/morewindows/article/details/8678359// By MoreWindows( http://blog.csdn.net/MoreWindows )// 先調用Initialize(),然後while(true){Sleep(1000);GetCPUUseRate();}就能獲得CPU使用率。// 經過測試,可以在WinXP及Win7下使用。class CCPUUseRate{public:BOOL Initialize() {FILETIME ftIdle, ftKernel, ftUser;BOOL flag = FALSE;if (flag = GetSystemTimes(&ftIdle, &ftKernel, &ftUser)){m_fOldCPUIdleTime = FileTimeToDouble(ftIdle);m_fOldCPUKernelTime = FileTimeToDouble(ftKernel);m_fOldCPUUserTime = FileTimeToDouble(ftUser);}return flag;}//調用Initialize後要等待1秒再調用此函數int GetCPUUseRate(){int nCPUUseRate = -1;FILETIME ftIdle, ftKernel, ftUser;if (GetSystemTimes(&ftIdle, &ftKernel, &ftUser)){double fCPUIdleTime = FileTimeToDouble(ftIdle);double fCPUKernelTime = FileTimeToDouble(ftKernel);double fCPUUserTime = FileTimeToDouble(ftUser);nCPUUseRate= (int)(100.0 - (fCPUIdleTime - m_fOldCPUIdleTime) / (fCPUKernelTime - m_fOldCPUKernelTime + fCPUUserTime - m_fOldCPUUserTime) *100.0);m_fOldCPUIdleTime = fCPUIdleTime;m_fOldCPUKernelTime = fCPUKernelTime;m_fOldCPUUserTime = fCPUUserTime;}return nCPUUseRate;}private:double FileTimeToDouble(FILETIME &filetime){return (double)(filetime.dwHighDateTime * 4.294967296E9) + (double)filetime.dwLowDateTime;}private:double m_fOldCPUIdleTime;double m_fOldCPUKernelTime;double m_fOldCPUUserTime;};
這個類的使用非常簡單。先調用Initialize(),然後使用類似於如下代碼
while(true)
{
Sleep(1000);
GetCPUUseRate();
}就能獲得CPU使用率。
下面是使用範例,程式中printf的\r可以參考《C/C++ 在控制台下顯示進度》(http://blog.csdn.net/morewindows/article/details/6742078):
// Windows系統CPU記憶體網路效能統計第二篇 CPU CPU整體使用率// 經過測試,可以在WinXP及Win7下使用//http://blog.csdn.net/morewindows/article/details/8678359#include <windows.h>#include <stdio.h>#include <conio.h>int main(){printf(" Windows系統CPU記憶體網路效能統計第二篇 CPU CPU整體使用率\n"); printf(" - http://blog.csdn.net/morewindows/article/details/8678359 -\n");printf(" -- By MoreWindows( http://blog.csdn.net/MoreWindows ) --\n\n"); CCPUUseRate cpuUseRate;if (!cpuUseRate.Initialize()){printf("Error! %d\n", GetLastError());getch();return -1;}else{while (true){Sleep(1000);printf("\r當前CPU使用率為:%4d%%", cpuUseRate.GetCPUUseRate());}}return 0;}
在WinXP系統運行結果如下(本文配套程式為:http://download.csdn.net/detail/morewindows/5160810):
在Win7系統運行結果如下(本文配套程式為:http://download.csdn.net/detail/morewindows/5160810):
本文《Windows系統CPU記憶體網路效能統計第二篇 CPU CPU整體使用率》(http://blog.csdn.net/morewindows/article/details/8678359)介紹的是如何擷取CPU的整體使用率,現大多為多核的CPU,因此如何擷取多核CPU各核的使用率了。請看下面二篇——
3.Windows系統CPU記憶體網路效能統計第三篇 CPU 多核CPU各核使用率 C#
http://blog.csdn.net/morewindows/article/details/8678382
4.Windows系統CPU記憶體網路效能統計第四篇 CPU多核CPU各核使用率 C++
http://blog.csdn.net/morewindows/article/details/8678396
本文配套程式為:http://download.csdn.net/detail/morewindows/5160810
轉載請標明出處,原文地址:http://blog.csdn.net/morewindows/article/details/8678359
歡迎關注微博:http://weibo.com/MoreWindows