项目背景:在评估软件资源利用率时,有必要计算CPU和内存的最大利用率。因此,有必要在软件操作项目中监视CPU和内存利用率的变化并记录最大值
1.内存统计信息将相对简单,只需要查询计算机中的最大内存,以及实时内存使用情况
1 #include2 #include 3 #include 4 #include 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使用率: "< " 当前内存: " << MemoryUseRate << 92 " 最大内存:"<< maxMemoryUsePercentage << std::endl; 93 94 } 95 } 96 return 0; 97 }
查看代码
2. CPU使用情况更为复杂
CPU:内核和超线程超线程(超线程)
超线程是Intel提出的第一项技术,该技术于2002年首次出现在Pentium4上。单个超线程CPU就像操作系统的两个逻辑CPU。因此,P4处理器需要添加一个逻辑CPU指针(逻辑处理单元)。
尽管超线程技术可以同时执行两个线程,但它不像两个真正的CPU。每个CPU都有独立的资源。当两个线程同时需要某个资源时,应暂时停止其中一个线程,并释放这些资源,直到这些资源空闲为止。因此,超线程的性能不等于两个CPU的性能。
多核(多核)
一开始,CPU只有一个内核。为了提高性能,引入了双核CPU,四核CPU等。双核CPU可以同时执行两个线程。与超线程不同,双核CPU在一个CPU芯片上具有两个中央处理器。
上图显示主板上有1个插槽,此插槽已插入CPU,该CPU有4个核心,每个核心均采用超线程技术,因此该机器共有8个逻辑核心。
实现CPU使用率统计程序
参考:
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/shoujiruanjian/article-357812-1.html
ear见面会#杨洋微微一笑很倾城##杨洋肖奈#这一年确实提升了很多
到现在潮水般的旅日
自由航行是指商船而不是军舰