Windows 下获取多核CPU使用率

工作时候写的Dome, 可以参考:

MSDN
讲解如何利用NT Native API获取NT系统的CPU使用率(支持多核)
Win32 SDK 获得多核心处理器使用率。
阅读更多查看代码

代码一: 产生CPU曲线,源自《编程之美》第一章

#include 
#include 
#include 


const int SAMPLING_COUNT = 200;
const double PI = 3.1415926535;
const int TOTAL_AMPLITUDE = 600;


int main(int argc, char* argv[])
{
	DWORD busySpan[SAMPLING_COUNT];
	int amplitude = TOTAL_AMPLITUDE / 2;

	double radian = 0.0;

	double radianIncrement = 2.0 / (double) SAMPLING_COUNT;

	for (int i = 0; i< SAMPLING_COUNT; i++)
	{
		busySpan[i] = (DWORD) (amplitude + (sin(PI * radian) * amplitude));

		radian += radianIncrement;
	}

	DWORD startTime = 0;

	for (int j = 0;; j = (j+1) % SAMPLING_COUNT)
	{
		startTime = GetTickCount();
		while ((GetTickCount() - startTime ) <= busySpan[j])
			;
		Sleep(TOTAL_AMPLITUDE - busySpan[j]);
	}
	return 0;
}

代码二: 获取CPU使用率

#include  
#include 

#define SystemBasicInformation 0 
#define SystemPerformanceInformation 2 
#define SystemTimeInformation 3
#define SystemProcessorPerformanceInformation 8

#define Li2Double(x) ((double)((x).HighPart) * 4.294967296E9 + (double)((x).LowPart))

typedef struct 
{ 
 DWORD dwUnknown1; 
 ULONG uKeMaximumIncrement; 
 ULONG uPageSize; 
 ULONG uMmNumberOfPhysicalPages; 
 ULONG uMmLowestPhysicalPage; 
 ULONG uMmHighestPhysicalPage; 
 ULONG uAllocationGranularity; 
 PVOID pLowestUserAddress; 
 PVOID pMmHighestUserAddress; 
 ULONG uKeActiveProcessors; 
 BYTE bKeNumberProcessors; 
 BYTE bUnknown2; 
 WORD wUnknown3; 
} SYSTEM_BASIC_INFORMATION;

typedef struct 
{ 
 LARGE_INTEGER liIdleTime; 
 DWORD dwSpare[76]; 
} SYSTEM_PERFORMANCE_INFORMATION;

typedef struct 
{ 
 LARGE_INTEGER liKeBootTime; 
 LARGE_INTEGER liKeSystemTime; 
 LARGE_INTEGER liExpTimeZoneBias; 
 ULONG uCurrentTimeZoneId; 
 DWORD dwReserved; 
} SYSTEM_TIME_INFORMATION;

typedef struct
_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION {
    LARGE_INTEGER IdleTime;
    LARGE_INTEGER KernelTime;
    LARGE_INTEGER UserTime;
    LARGE_INTEGER DpcTime;
    LARGE_INTEGER InterruptTime;
    ULONG Reserved2;
} SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION;

typedef struct
_m_PROCESSORS_USE_TIME {
    double dbOldIdleTime;	// save old total time
	double dbOldCurrentTime;
	double dbIdleTime;		// save time after calc
	double dbCurrentTime;
	float fUse;
}m_PROCESSORS_USE_TIME;

m_PROCESSORS_USE_TIME * m_PUT;

SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION * m_pSPPI = NULL;

int m_iNumberProcessors;

typedef LONG (WINAPI *PROCNTQSI)(UINT,PVOID,ULONG,PULONG);

PROCNTQSI NtQuerySystemInformation;

static LARGE_INTEGER liOldIdleTime = {0,0}; 
static LARGE_INTEGER liOldSystemTime = {0,0};

double dbIdleTime = 0; 
double dbSystemTime = 0; 
double alldbIdleTime = 0;

void init()
{

}

int GetCpuUsage()
{ 
	SYSTEM_PERFORMANCE_INFORMATION SysPerfInfo; 
	SYSTEM_TIME_INFORMATION SysTimeInfo; 
	SYSTEM_BASIC_INFORMATION SysBaseInfo; 
	
	LONG status; 

	NtQuerySystemInformation = (PROCNTQSI)GetProcAddress(GetModuleHandle("ntdll"),"NtQuerySystemInformation");
	// get number of processors in the system 
	status = NtQuerySystemInformation(SystemBasicInformation,&SysBaseInfo,sizeof(SysBaseInfo),NULL); 
	if (status != NO_ERROR) 
		return -1;

	if (!NtQuerySystemInformation) 
		return -1;

	// get number of processors in the system 
	status = NtQuerySystemInformation(SystemBasicInformation,&SysBaseInfo,sizeof(SysBaseInfo),NULL); 
	if (status != NO_ERROR) 
		return -1;

	// get new system time 
	status = NtQuerySystemInformation(SystemTimeInformation,&SysTimeInfo,sizeof(SysTimeInfo),0); 
	if (status!=NO_ERROR) 
		return -1;

	// get new CPU's idle time 
	status =NtQuerySystemInformation(SystemPerformanceInformation,&SysPerfInfo,sizeof(SysPerfInfo),NULL); 
	if (status != NO_ERROR) 
		return -1;
	
	if ( m_iNumberProcessors != SysBaseInfo.bKeNumberProcessors)
	{
		//save
		m_iNumberProcessors = SysBaseInfo.bKeNumberProcessors;
		//if sppi not null clear
		if (m_pSPPI != NULL) delete []m_pSPPI;
		if (m_PUT != NULL) delete []m_PUT;
		//malloc and point
		m_pSPPI = new SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION[m_iNumberProcessors];
		m_PUT = new m_PROCESSORS_USE_TIME[m_iNumberProcessors];
	}

	// get ProcessorPer time 
	status =NtQuerySystemInformation(SystemProcessorPerformanceInformation, m_pSPPI, sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * m_iNumberProcessors, NULL); 
	if (status != NO_ERROR) 
		return -1;

	// if it's a first call - skip it 
	if (liOldIdleTime.QuadPart != 0) 
	{ 
		// CurrentValue = NewValue - OldValue 
		dbIdleTime = Li2Double(SysPerfInfo.liIdleTime) - Li2Double(liOldIdleTime); 

		dbSystemTime = Li2Double(SysTimeInfo.liKeSystemTime) - Li2Double(liOldSystemTime);

		// CurrentCpuIdle = IdleTime / SystemTime 
		dbIdleTime = dbIdleTime / dbSystemTime;

		// CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors 
		dbIdleTime = 100.0 - dbIdleTime * 100.0 / (double)SysBaseInfo.bKeNumberProcessors + 0.5;

		//calc Processors
		for (int i = 0; i < m_iNumberProcessors; i++)
		{
			m_PUT[i].dbCurrentTime = Li2Double(m_pSPPI[i].KernelTime) + Li2Double(m_pSPPI[i].UserTime) + 
										Li2Double(m_pSPPI[i].DpcTime) + Li2Double(m_pSPPI[i].InterruptTime) - m_PUT[i].dbOldCurrentTime;
			m_PUT[i].dbIdleTime = Li2Double(m_pSPPI[i].IdleTime) - m_PUT[i].dbOldIdleTime;

			// CurrentCpuIdle = IdleTime / SystemTime 
			m_PUT[i].dbIdleTime = m_PUT[i].dbIdleTime / m_PUT[i].dbCurrentTime;

			// CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors 
			m_PUT[i].dbIdleTime = 100.0 - m_PUT[i].dbIdleTime * 100.0 + 0.5;

		}
	}

	// store new CPU's idle and system time 
	liOldIdleTime = SysPerfInfo.liIdleTime; 
	liOldSystemTime = SysTimeInfo.liKeSystemTime;

	for (int i = 0; i < m_iNumberProcessors; i++)
	{
		m_PUT[i].dbOldCurrentTime = Li2Double(m_pSPPI[i].KernelTime) + Li2Double(m_pSPPI[i].UserTime) + 
										Li2Double(m_pSPPI[i].DpcTime) + Li2Double(m_pSPPI[i].InterruptTime);

		m_PUT[i].dbOldIdleTime = Li2Double(m_pSPPI[i].IdleTime);
	}

	return (int)dbIdleTime;
}

int main()
{
	init();


	for (;;)
	{
		printf("%3d%%", GetCpuUsage());
		printf("%d", m_iNumberProcessors);

		for (int i = 0; i < m_iNumberProcessors; i++)
		{
			printf(" %.0f%%", m_PUT[i].dbIdleTime);
		}
		printf("\n");
	/*	printf(" %.0f, %.0f |", m_PUT[0].dbIdleTime, m_PUT[0].dbCurrentTime);
		printf(" %.0f, %.0f |", m_PUT[1].dbIdleTime, m_PUT[1].dbCurrentTime);
		printf(" %.0f, %.0f |", m_PUT[2].dbIdleTime, m_PUT[2].dbCurrentTime);
		printf(" %.0f, %.0f |\n", m_PUT[3].dbIdleTime, m_PUT[3].dbCurrentTime);*/

		Sleep(1000);
	}
	return 0;
}

此条目发表在C, C++, Windows, 系统, 编程分类目录。将固定链接加入收藏夹。

Windows 下获取多核CPU使用率》有3条回应

  1. yinngyi说:

    我也在研究绘制CPU曲线,但是结果不令人满意。我在想,了解CPU被其他进程的占用比然后我再来填补剩下的应该可以让曲线更完美。不知道使用你下面的这段代码能否实现。
    另外,编程之美上说10ms的Windows调度时间片似乎在我的机器上也不能work,曲线停留在100%。

  2. 匿名说:

    你这在win7 64位上运行的话 会出错的

发表评论

邮箱地址不会被公开。 必填项已用*标注