Windows系统CPU内存网络性能统计第四篇 CPU 多核CPU各核使用率C++

来源:互联网 发布:资深淘宝美工 编辑:程序博客网 时间:2024/05/20 17:42

转载请标明出处,原文地址:http://blog.csdn.net/morewindows/article/details/8678396

欢迎关注微博:http://weibo.com/MoreWindows

 

Windows系统CPU内存网络性能统计第四篇 CPU多核CPU各核使用率 C++

http://blog.csdn.net/morewindows/article/details/8678396

 

    本篇《Windows系统CPU内存网络性能统计第四篇 CPU多核CPU各核使用率C++(http://blog.csdn.net/morewindows/article/details/8678396)将介绍在VC++中引用C#代码来完成对多核CPU各核使用率的统计。

Windows系统CPU内存网络性能统计博客目录:

1Windows系统CPU内存网络性能统计第一篇内存

http://blog.csdn.net/morewindows/article/details/8459219

2Windows系统CPU内存网络性能统计第二篇 CPU CPU整体使用率

http://blog.csdn.net/morewindows/article/details/8678359

3Windows系统CPU内存网络性能统计第三篇 CPU 多核CPU各核使用率 C#

http://blog.csdn.net/morewindows/article/details/8678382

4Windows系统CPU内存网络性能统计第四篇 CPU多核CPU各核使用率 C++

http://blog.csdn.net/morewindows/article/details/8678396

 

首先是C#代码。注意这是一个“C#类库”的工程,在此工程中完成了一个CShapeCPUUseRate类,这个类的GetCPUEveryCoreUseRate函数将返回一个包含各CPU各核使用率的字符串,比如双核CPU一个核的使用率是3%,另一个的使用率是5%,那么将返回"3,5"

[csharp] view plaincopy
  1. //Windows系统CPU内存网络性能统计第四篇 CPU多核CPU各核使用率 C++  
  2. //http://blog.csdn.net/morewindows/article/details/8678396  
  3. using System;  
  4. using System.Collections.Generic;  
  5. using System.Linq;  
  6. using System.Text;  
  7. using System.Diagnostics;  
  8.   
  9. namespace CShapeCPUUseRateDLL  
  10. {  
  11.     public class CShapeCPUUseRate  
  12.     {  
  13.         public int Initialize()  
  14.         {  
  15.             try  
  16.             {  
  17.                 m_nCPUCoreNumber = System.Environment.ProcessorCount;  
  18.                 m_pfCounters = new PerformanceCounter[m_nCPUCoreNumber];   
  19.                 for(int i = 0; i < m_nCPUCoreNumber; i++)          
  20.                 {              
  21.                     m_pfCounters[i] = new PerformanceCounter("Processor""% Processor Time", i.ToString());   
  22.                 }    
  23.             }  
  24.             catch (System.Exception e)  
  25.             {  
  26.                 return 0;  
  27.             }  
  28.             return 1;  
  29.         }  
  30.         public int GetCPUCoreNumber()  
  31.         {  
  32.             return m_nCPUCoreNumber;  
  33.         }  
  34.         public string GetCPUEveryCoreUseRate()  
  35.         {  
  36.             StringBuilder strBuild = new StringBuilder();  
  37.             float fRate = m_pfCounters[0].NextValue();  
  38.             int nRate = Convert.ToInt32(fRate);  
  39.             strBuild.Append(nRate.ToString());  
  40.             for(int i = 1; i < m_nCPUCoreNumber; i++)         
  41.             {  
  42.                 fRate = m_pfCounters[i].NextValue();  
  43.                 nRate = Convert.ToInt32(fRate);  
  44.                 strBuild.Append("," + nRate.ToString());  
  45.             }  
  46.             return strBuild.ToString();  
  47.         }  
  48.         private PerformanceCounter[]   m_pfCounters;  
  49.         private int                    m_nCPUCoreNumber;  
  50.     }  
  51. }  

如何在C++调用C#代码可以参考《C++通过DLL调用C#代码》(http://blog.csdn.net/morewindows/article/details/8678431)

[cpp] view plaincopy
  1. //Windows系统CPU内存网络性能统计第四篇CPU多核CPU各核使用率C++  
  2. //http://blog.csdn.net/morewindows/article/details/8678396  
  3. //#using "CShapeCPUUseRateDLL\\CShapeCPUUseRateDLL\\bin\\Debug\\CShapeCPUUseRateDLL.dll"  
  4. #using "CShapeCPUUseRateDLL\\CShapeCPUUseRateDLL\\bin\\Release\\CShapeCPUUseRateDLL.dll"  
  5. #include <Windows.h>  
  6. #include <stdio.h>  
  7. #include <conio.h>  
  8. #include <string.h>  
  9. using namespace CShapeCPUUseRateDLL;  
  10.   
  11. int main()    
  12. {    
  13.     printf("    Windows系统CPU内存网络性能统计第四篇CPU多核CPU各核使用率C++\n");    
  14.     printf(" - http://blog.csdn.net/morewindows/article/details/8678396 -\n");  
  15.     printf(" -- By MoreWindows( http://blog.csdn.net/MoreWindows ) --\n\n");   
  16.   
  17.     CShapeCPUUseRate ^ cpuUseRate = gcnew CShapeCPUUseRate;  
  18.     if (!cpuUseRate->Initialize())  
  19.     {  
  20.         printf("Error!\n");  
  21.         getch();  
  22.         return -1;  
  23.     }  
  24.     else  
  25.     {  
  26.         printf("系统中CPU为%d核CPU\n",cpuUseRate->GetCPUCoreNumber());  
  27.         while (true)  
  28.         {     
  29.             Sleep(1000);  
  30.             printf("\r当前CPU各核使用率分别为:%s     ", cpuUseRate->GetCPUEveryCoreUseRate());  
  31.         }  
  32.     }  
  33.     return 0;  
  34. }  

程序运行结果如下:

 

这种通过C++调用C#代码来获取CPU各核使用率的方法不是太好,以后再找找资料看看在C++中如何直接获取CPU各核使用率,欢迎高手指点。

转载请标明出处,原文地址:http://blog.csdn.net/morewindows/article/details/8678396

欢迎关注微博:http://weibo.com/MoreWindows

0 0
原创粉丝点击