項目背景:在評估軟件資源使用率的時候,需要統計CPU和內存最大使用率,因此需要監測軟件運行工程中的CPU和內存使用率的變化,並記錄最大值
1.內存統計會比較簡單,只需要查詢電腦中最大內存,以及實時內存使用情況

1 #include <windows.h> 2 #include <stdio.h> 3 #include <conio.h> 4 #include<iostream> 5 double FileTimeToDouble(FILETIME* pFiletime) 6 { 7 return (double)((*pFiletime).dwHighDateTime * 4.294967296E9) + (double)(*pFiletime).dwLowDateTime; 8 } 9 10 double m_fOldCPUIdleTime; 11 double m_fOldCPUKernelTime; 12 double m_fOldCPUUserTime; 13 14 15 16 BOOL Initialize() 17 { 18 FILETIME ftIdle, ftKernel, ftUser; 19 BOOL flag = FALSE; 20 if (flag = GetSystemTimes(&ftIdle, &ftKernel, &ftUser)) 21 { 22 m_fOldCPUIdleTime = FileTimeToDouble(&ftIdle); 23 m_fOldCPUKernelTime = FileTimeToDouble(&ftKernel); 24 m_fOldCPUUserTime = FileTimeToDouble(&ftUser); 25 26 } 27 return flag; 28 } 29 30 int GetCPUUseRate() 31 { 32 int nCPUUseRate = -1; 33 FILETIME ftIdle, ftKernel, ftUser; 34 if (GetSystemTimes(&ftIdle, &ftKernel, &ftUser)) 35 { 36 double fCPUIdleTime = FileTimeToDouble(&ftIdle); 37 double fCPUKernelTime = FileTimeToDouble(&ftKernel); 38 double fCPUUserTime = FileTimeToDouble(&ftUser); 39 nCPUUseRate = (int)(100.0 - (fCPUIdleTime - m_fOldCPUIdleTime) / (fCPUKernelTime - m_fOldCPUKernelTime + fCPUUserTime - m_fOldCPUUserTime)*100.0); 40 m_fOldCPUIdleTime = fCPUIdleTime; 41 m_fOldCPUKernelTime = fCPUKernelTime; 42 m_fOldCPUUserTime = fCPUUserTime; 43 } 44 return nCPUUseRate; 45 } 46 int GetMemoryUsePercentage() 47 { 48 MEMORYSTATUSEX mstx; 49 mstx.dwLength = sizeof(mstx); 50 GlobalMemoryStatusEx(&mstx); 51 int iMemeryUsePercentage = mstx.dwMemoryLoad; 52 int iTotalPhysMB = mstx.ullTotalPhys / 1024 / 1024; 53 int iAvailPhysMB = mstx.ullAvailPhys / 1024 / 1024; 54 int iTotalPageFileMB = mstx.ullTotalPageFile / 1024 / 1024; 55 int iAvailPageFileMB = mstx.ullAvailPageFile / 1024 / 1024; 56 char LogBuff[128]; 57 memset(LogBuff, 0, 128); 58 //sprintf(LogBuff, "MemAvailPct=%d%% Phys=%d/%d PageFile=%d/%d", iMemeryUsePercentage, iAvailPhysMB, iTotalPhysMB, iAvailPageFileMB, iTotalPageFileMB); 59 //printf("%s\n", LogBuff); 60 return iMemeryUsePercentage; 61 } 62 63 64 int main() 65 { 66 int maxCPUUsePercentage = 0; 67 int maxMemoryUsePercentage = 0; 68 if (!Initialize()) 69 { 70 system("pause"); 71 return -1; 72 } 73 else 74 { 75 while(true) 76 { 77 Sleep(1000); 78 int CpuUseRate = int(GetCPUUseRate( 79 int MemoryUseRate = GetMemoryUsePercentage();; 80 if (CpuUseRate> maxCPUUsePercentage) 81 { 82 maxCPUUsePercentage = CpuUseRate; 83 } 84 85 if (MemoryUseRate > maxMemoryUsePercentage) 86 { 87 maxMemoryUsePercentage = MemoryUseRate; 88 } 89 90 std::cout << "當前CPU使用率: " << CpuUseRate << 91 " 最大CPU使用率: "<<maxCPUUsePercentage<< " 當前內存: " << MemoryUseRate << 92 " 最大內存:"<< maxMemoryUsePercentage << std::endl; 93 94 } 95 } 96 return 0; 97 }
2.CPU使用率更復雜
CPU:Cores, and Hyper-Threading
超線程(Hyper-Threading )
超線程是Intel最早提出一項技術,最早出現在2002年的Pentium4上。單個采用超線程的CPU對於操作系統來說就像有兩個邏輯CPU,為此P4處理器需要多加入一個Logical CPU Pointer(邏輯處理單元)。
雖然采用超線程技術能同時執行兩個線程,但它並不像兩個真正的CPU那樣,每個CPU都具有獨立的資源。當兩個線程都同時需要某一個資源時,其中一個要暫時停止,並讓出資源,直到這些資源閑置后才能繼續。因此超線程的性能並不等於兩顆CPU的性能。
多核(multi-cores)
最開始CPU只有一個核(core),為了提高性能,引入了雙核CPU,四核CPU等,雙核CPU能同時執行兩個線程。和超線程不同的是,雙核CPU是實打實的有兩個central processing units在一個CPU chip。
上圖顯示主板上有1個插槽(socket),這個插槽插着一個CPU,這個CPU有4個核(core),每個核都使用超線程技術,所以這台機器總共有8個邏輯核。
實現CPU使用率統計程序
某進程cpu使用率 = 該進程cpu時間 / 總cpu時間。
參考:
https://www.cnblogs.com/gatsby123/p/11127158.html
https://blog.csdn.net/fyxichen/article/details/50577580
https://blog.csdn.net/sz76211822/article/details/58599219