`
haierboos
  • 浏览: 438324 次
文章分类
社区版块
存档分类
最新评论

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

 
阅读更多

转载请标明出处,原文地址: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"

//Windows系统CPU内存网络性能统计第四篇 CPU多核CPU各核使用率 C++
//http://blog.csdn.net/morewindows/article/details/8678396
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace CShapeCPUUseRateDLL
{
    public class CShapeCPUUseRate
    {
        public int Initialize()
        {
            try
            {
                m_nCPUCoreNumber = System.Environment.ProcessorCount;
                m_pfCounters = new PerformanceCounter[m_nCPUCoreNumber]; 
                for(int i = 0; i < m_nCPUCoreNumber; i++)        
                {            
                    m_pfCounters[i] = new PerformanceCounter("Processor", "% Processor Time", i.ToString()); 
                }  
            }
            catch (System.Exception e)
            {
            	return 0;
            }
            return 1;
        }
        public int GetCPUCoreNumber()
        {
            return m_nCPUCoreNumber;
        }
        public string GetCPUEveryCoreUseRate()
        {
            StringBuilder strBuild = new StringBuilder();
            float fRate = m_pfCounters[0].NextValue();
            int nRate = Convert.ToInt32(fRate);
            strBuild.Append(nRate.ToString());
            for(int i = 1; i < m_nCPUCoreNumber; i++)       
            {
                fRate = m_pfCounters[i].NextValue();
                nRate = Convert.ToInt32(fRate);
                strBuild.Append("," + nRate.ToString());
            }
            return strBuild.ToString();
        }
        private PerformanceCounter[]   m_pfCounters;
        private int                    m_nCPUCoreNumber;
    }
}

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

//Windows系统CPU内存网络性能统计第四篇CPU多核CPU各核使用率C++
//http://blog.csdn.net/morewindows/article/details/8678396
//#using "CShapeCPUUseRateDLL\\CShapeCPUUseRateDLL\\bin\\Debug\\CShapeCPUUseRateDLL.dll"
#using "CShapeCPUUseRateDLL\\CShapeCPUUseRateDLL\\bin\\Release\\CShapeCPUUseRateDLL.dll"
#include <Windows.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
using namespace CShapeCPUUseRateDLL;

int main()  
{  
	printf("    Windows系统CPU内存网络性能统计第四篇CPU多核CPU各核使用率C++\n");  
	printf(" - http://blog.csdn.net/morewindows/article/details/8678396 -\n");
	printf(" -- By MoreWindows( http://blog.csdn.net/MoreWindows ) --\n\n"); 

	CShapeCPUUseRate ^ cpuUseRate = gcnew CShapeCPUUseRate;
	if (!cpuUseRate->Initialize())
	{
		printf("Error!\n");
		getch();
		return -1;
	}
	else
	{
		printf("系统中CPU为%d核CPU\n",cpuUseRate->GetCPUCoreNumber());
		while (true)
		{	
			Sleep(1000);
			printf("\r当前CPU各核使用率分别为:%s     ", cpuUseRate->GetCPUEveryCoreUseRate());
		}
	}
	return 0;
}

程序运行结果如下:

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

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

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



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics