通过性能计数器(Process)来获取某个进程的CPU使用率、内存使用量、磁盘读写速率

本篇文章是在上次获取整个系统性能数据的延伸。

需求:获取某个进程(已知进程名)的CPU使用率、内存使用量、磁盘读写速率

实现:通过windows自带的性能计数器中的Process实现


与之前获取整个系统性能数据的流程是一样的,打开性能计数器——添加性能计数器——获取数据。

下面是具体的性能计数器查询语句,"tfb_manage_system"是进程名(tfb_manage_system.exe)。

获取CPU使用率:"\\Process(tfb_manage_system)\\% Processor Time"        ps:需要注意一点,这里拿到的数据是对于单核CPU的使用率,所以,对于整个系统的使用率,需要将本数据除以系统CPU核心数量。比如拿到的数据是80%,系统是四核,那该进程真实的CPU占用率是80 / 4 = 20%。

获取内存使用量:"\\Process(tfb_manage_system)\\Working Set - Private"        获取的专用内存使用量。单位是字节

获取磁盘读速率:"\\Process(tfb_manage_system)\\IO Read Bytes/sec"        单位是字节 / 秒

获取磁盘写速率:"\\Process(tfb_manage_system)\\IO Write Bytes/sec"        单位是字节 / 秒


下面是实现代码:

/*
	通过性能计数器获取某个进程的CPU使用率、内存使用量、磁盘读写速率
	使用性能计数器中的Process实现
*/


#pragma	comment(lib,"pdh")
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

int nCPU;

int GetData()
{
	HQUERY query;  
	double dbVal;
	long iVal;
	PDH_STATUS status = PdhOpenQuery(NULL, NULL, &query);  
	if (ERROR_SUCCESS != status)  
	{  
		MessageBox(NULL,TEXT("打开失败"),TEXT(""),MB_OK);  
		return -1;  

	}

	HCOUNTER cntProcessCPU, cntProcessMemory;
	HCOUNTER cntProcessDiskRead, cntProcessDiskWrite;
	status = PdhAddCounter(query, "\\Process(tfb_manage_system)\\% Processor Time", NULL, &cntProcessCPU);
	status = PdhAddCounter(query, "\\Process(tfb_manage_system)\\Working Set - Private", NULL, &cntProcessMemory);
	status = PdhAddCounter(query, "\\Process(tfb_manage_system)\\IO Read Bytes/sec", NULL, &cntProcessDiskRead);
	status = PdhAddCounter(query, "\\Process(tfb_manage_system)\\IO Write Bytes/sec", NULL, &cntProcessDiskWrite);
	if (ERROR_SUCCESS != status)  
	{  
		MessageBox(NULL,TEXT("添加失败"),TEXT(""),MB_OK);  
		return -1;  

	}

	status = PdhCollectQueryData(query);        
	Sleep(500);                 //这里要有延时不然结果相当不准确  
	status = PdhCollectQueryData(query);  
	if(ERROR_SUCCESS != status)  
	{  
		MessageBox(NULL,TEXT("数据请求失败"),TEXT(""),MB_OK);  
		return -1;  
	}  

	PDH_FMT_COUNTERVALUE pdhValue;       
	DWORD dwValue;  

	status = PdhGetFormattedCounterValue(cntProcessCPU, PDH_FMT_DOUBLE, &dwValue, &pdhValue);  
	if(ERROR_SUCCESS != status)  
	{  
		MessageBox(NULL,TEXT("得到数据失败"),TEXT(""),MB_OK);  
		return -1;  
	}
	else
	{
		dbVal = pdhValue.doubleValue;  
		printf("Process-CPU:          %3d%%          ", (int)(dbVal/nCPU+0.5));
	}

	status = PdhGetFormattedCounterValue(cntProcessMemory, PDH_FMT_DOUBLE, &dwValue, &pdhValue);  
	if(ERROR_SUCCESS != status)  
	{  
		MessageBox(NULL,TEXT("得到数据失败"),TEXT(""),MB_OK);  
		return -1;  
	}
	else
	{
		dbVal = pdhValue.doubleValue;  
		printf("Process-Memory:     %8dK        \n", (int)(dbVal/1024));
	}

	status = PdhGetFormattedCounterValue(cntProcessDiskRead, PDH_FMT_DOUBLE, &dwValue, &pdhValue);  
	if(ERROR_SUCCESS != status)  
	{  
		MessageBox(NULL,TEXT("得到数据失败"),TEXT(""),MB_OK);  
		return -1;  
	}
	else
	{
		dbVal = pdhValue.doubleValue;  
		printf("Process-DiskRead:%8dK/s        ", (int)(dbVal/1024));
	}

	status = PdhGetFormattedCounterValue(cntProcessDiskWrite, PDH_FMT_DOUBLE, &dwValue, &pdhValue);  
	if(ERROR_SUCCESS != status)  
	{  
		MessageBox(NULL,TEXT("得到数据失败"),TEXT(""),MB_OK);  
		return -1;  
	}
	else
	{
		dbVal = pdhValue.doubleValue;  
		printf("Process-DiskWrite:%8dK/s        \n*****************************************************************************\n", (int)(dbVal/1024));
	}

	PdhRemoveCounter(cntProcessCPU);
	PdhRemoveCounter(cntProcessMemory);
	PdhRemoveCounter(cntProcessDiskRead);
	PdhRemoveCounter(cntProcessDiskWrite);
	PdhCloseQuery(query);
}


int main() {
	setlocale(LC_ALL,"chs");

	SYSTEM_INFO si;
	GetSystemInfo(&si);
	nCPU = si.dwNumberOfProcessors;

	while(1)
	{
		Sleep(500);
		GetData();
	}
	system("pause");
	return 0;
}


你可能感兴趣的:(C/C++)