Windows系統CPU記憶體網路效能統計第二篇 CPU CPU整體使用率

來源:互聯網
上載者:User

本文配套程式為: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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.