PDH性能测试之四-----------获取每个对象的Counter和Instance

本文介绍了使用 PDH 函数获取实时源上的性能对象列表及其属性列表的方法,并详细阐述了如何遍历这些列表以获取所需数据。
#define _UNICODE /*主要对C标准库函数,将宏替换为宽字节形式*/
#define UNICODE /*主要对Windows API标准函数,将函数宏替换为宽字节形式*/
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
#include <malloc.h>
#include <stdio.h>
#include <pdh.h>
#include <pdhmsg.h> /*存放PDH函数返回的错误代码*/
#include <tchar.h> ///

#pragma comment(lib, "pdh.lib")
void getObjects(BOOL bCounter,BOOL bInstance);
void getObjcetItem(LPTSTR ptsObject,BOOL bCounter,BOOL bInstance);
void main(void) {
	getObjects(TRUE,FALSE);
	getObjects(FALSE,TRUE);
}
void getObjects(BOOL bCounter,BOOL bInstance) {

	PDH_STATUS status = ERROR_SUCCESS;
	LPTSTR ptsObjectsListBuffer = NULL;
	DWORD dwObjectsListSize = 0;
	LPTSTR pTemp = NULL;
	/*第一次调用,将返回实际的字符数*/
	status = PdhEnumObjects(
			NULL,				// real-time source
			NULL,				// local machine
			ptsObjectsListBuffer,         // LPTSTR mszObjectList
			&dwObjectsListSize,   //LPDWORD pcchBufferLength,
			PERF_DETAIL_EXPERT,     //DWORD dwDetailLevel
			FALSE     //BOOL bRefresh
			);
	/*读取性能对象列表*/
	if (status == PDH_MORE_DATA) {
		// Allocate the buffers and try the call again.
		ptsObjectsListBuffer = (LPTSTR) malloc(dwObjectsListSize * sizeof(TCHAR));
		if (NULL != ptsObjectsListBuffer) {
			status = PdhEnumObjects(
					NULL,     // real-time source
					NULL,     // local machine
					ptsObjectsListBuffer,         // LPTSTR mszObjectList
					&dwObjectsListSize,   //LPDWORD pcchBufferLength,
					PERF_DETAIL_EXPERT,     //DWORD dwDetailLevel
					FALSE     //BOOL bRefresh
					);
			if (status == ERROR_SUCCESS) {
				for (pTemp = ptsObjectsListBuffer; *pTemp != 0;pTemp += _tcslen(pTemp) + 1)
						/*从字符数组中读取字符串的方法*/
						{
					_tprintf(_T("%s\n"), pTemp);

					getObjcetItem(pTemp,bCounter,bInstance);
					//wprintf(L"%s\n", pTemp);
				}
			} else {
				_tprintf(_T("Second PdhEnumObjects failed with %0x%x.\n"),
						status);
			}
		} else {
			_tprintf(_T("Unable to allocate buffers.\n"));
			status = ERROR_OUTOFMEMORY;
		}
	} else {
		_tprintf(_T("\nPdhEnumObjects failed with 0x%x.\n"), status);
	}
	if (ptsObjectsListBuffer != NULL)
		free(ptsObjectsListBuffer);
}
void getObjcetItem(LPTSTR ptsObject,BOOL bCounter,BOOL bInstance) {
	PCTSTR strObject =ptsObject;
	PDH_STATUS status = ERROR_SUCCESS;
	LPTSTR ptsCounterListBuffer = NULL;     //类的属性列表
	DWORD dwCounterListSize = 0;
	LPTSTR ptsInstanceListBuffer = NULL;     //实例列表
	DWORD dwInstanceListSize = 0;
	LPTSTR pTemp = NULL;

	// Determine the required buffer size for the data.
	status = PdhEnumObjectItems(
			NULL,                   // real-time source
			NULL,                   // local machine
			strObject,         // object to enumerate
			ptsCounterListBuffer, &dwCounterListSize,//如果Buffer为NULL,Size为0
			ptsInstanceListBuffer, &dwInstanceListSize,
			PERF_DETAIL_WIZARD,     // counter detail level
			0);

	if (status == PDH_MORE_DATA) {
		// Allocate the buffers and try the call again.
		ptsCounterListBuffer = (LPTSTR) malloc(dwCounterListSize * sizeof(TCHAR));
		ptsInstanceListBuffer = (LPTSTR) malloc(dwInstanceListSize * sizeof(TCHAR));

		if (NULL != ptsCounterListBuffer && NULL != ptsInstanceListBuffer) {
			status = PdhEnumObjectItems(
					NULL,                   // real-time source
					NULL,                   // local machine
					strObject,         // object to enumerate
					ptsCounterListBuffer, &dwCounterListSize,
					ptsInstanceListBuffer, &dwInstanceListSize,
					PERF_DETAIL_WIZARD,     // counter detail level
					0);

			if (status == ERROR_SUCCESS) {
				_tprintf(_T("_____________________\n"));
				_tprintf(_T("Counters ConTain:\n\n"));
				//wprintf(L"Counters that the Process objects defines:\n\n");

				// Walk the counters list. The list can contain one
				// or more null-terminated strings. The list is terminated
				// using two null-terminator characters.
				if(bCounter){
				for (pTemp = ptsCounterListBuffer; *pTemp != 0;pTemp += _tcslen(pTemp) + 1) {
					_tprintf(_T("\t"));
					_tprintf(_T("%s\n"), pTemp);
					//wprintf(L"%s\n", pTemp);
				}
				}
				if(bInstance){
					_tprintf(_T("_____________________\n"));
					_tprintf(_T("\nInstances Contain:\n\n"));
				//wprintf(L"\nInstances of the Process object:\n\n");

				// Walk the instance list. The list can contain one
				// or more null-terminated strings. The list is terminated
				// using two null-terminator characters.
				for (pTemp = ptsInstanceListBuffer; *pTemp != 0;pTemp +=_tcslen(pTemp) + 1) {
					_tprintf(_T("\t"));
					_tprintf(_T("%s\n"), pTemp);
					//wprintf(L"%s\n", pTemp);
				}
				}
			} else {
				_tprintf(_T("Second PdhEnumObjectItems failed with %0x%x.\n"),
						status);
				//wprintf(L"Second PdhEnumObjectItems failed with %0x%x.\n",status);
			}
		} else {
			_tprintf(_T("Unable to allocate buffers.\n"));
			//wprintf(L"Unable to allocate buffers.\n");
			status = ERROR_OUTOFMEMORY;
		}
	} else {
		_tprintf(_T("\nPdhEnumObjectItems failed with 0x%x.\n"), status);
		//wprintf(L"\nPdhEnumObjectItems failed with 0x%x.\n", status);
	}

	if (ptsCounterListBuffer != NULL)
		free(ptsCounterListBuffer);

	if (ptsInstanceListBuffer != NULL)
		free(ptsInstanceListBuffer);
}

分析一下这个函数 bool GetCpuStats(std::vector<ProcessData>& topProcesses, double& totalUsage) { PDH_HQUERY hQuery = nullptr; PDH_STATUS status = PdhOpenQuery(nullptr, 0, &hQuery); if (status != ERROR_SUCCESS) return false; // 添加总CPU计数器 PDH_HCOUNTER hTotalCounter = nullptr; PdhAddCounter(hQuery, L"\\Processor(_Total)\\% Processor Time", 0, &hTotalCounter); // 枚举所有进程计数器 DWORD dwBufSize = 0; LPWSTR szInstanceList = nullptr; PdhEnumObjectItems(nullptr, nullptr, L"Process", nullptr, &dwBufSize, nullptr, 0, PERF_DETAIL_WIZARD,0); szInstanceList = new WCHAR[dwBufSize]; DWORD dwInstanceCount = 0; PdhEnumObjectItems(nullptr, nullptr, L"Process", szInstanceList, &dwBufSize, nullptr, &dwInstanceCount, PERF_DETAIL_WIZARD,0); // 准备进程计数器 std::vector<PDH_HCOUNTER> vCpuCounters, vPidCounters; LPWSTR pCurrent = szInstanceList; while (*pCurrent) { std::wstring instance = pCurrent; std::wstring cpuPath = L"\\Process(" + instance + L")\\% Processor Time"; std::wstring pidPath = L"\\Process(" + instance + L")\\ID Process"; PDH_HCOUNTER hCpuCounter = nullptr; PDH_HCOUNTER hPidCounter = nullptr; if (PdhAddCounter(hQuery, cpuPath.c_str(), 0, &hCpuCounter) == ERROR_SUCCESS && PdhAddCounter(hQuery, pidPath.c_str(), 0, &hPidCounter) == ERROR_SUCCESS) { vCpuCounters.push_back(hCpuCounter); vPidCounters.push_back(hPidCounter); } pCurrent += wcslen(pCurrent) + 1; } delete[] szInstanceList; // 数据采集(两次间隔) PdhCollectQueryData(hQuery); Sleep(1000); PdhCollectQueryData(hQuery); // 获取总CPU使用率 PDH_FMT_COUNTERVALUE totalValue; totalUsage = (PdhGetFormattedCounterValue(hTotalCounter, PDH_FMT_DOUBLE, nullptr, &totalValue) == ERROR_SUCCESS) ? totalValue.doubleValue : 0.0; // 处理进程数据 std::vector<ProcessData> processes; for (size_t i = 0; i < vCpuCounters.size(); ++i) { PDH_FMT_COUNTERVALUE pidValue, cpuValue; if (PdhGetFormattedCounterValue(vPidCounters[i], PDH_FMT_LONG, nullptr, &pidValue) != ERROR_SUCCESS) continue; if (PdhGetFormattedCounterValue(vCpuCounters[i], PDH_FMT_DOUBLE, nullptr, &cpuValue) != ERROR_SUCCESS) continue; DWORD pid = pidValue.longValue; double usage = cpuValue.doubleValue; std::wstring name = GetProcessName(pid); if (!name.empty() && name != L"Idle" && name != L"System Idle Process") { processes.push_back({ pid, name, usage }); } } // 排序并保留前5 std::sort(processes.begin(), processes.end(), [](const auto& a, const auto& b) { return a.cpuUsage > b.cpuUsage; }); topProcesses = processes.size() > 5 ? std::vector<ProcessData>(processes.begin(), processes.begin() + 5) : processes; // 清理资源 for (auto& counter : vCpuCounters) PdhRemoveCounter(counter); for (auto& counter : vPidCounters) PdhRemoveCounter(counter); PdhCloseQuery(hQuery); return true; }
05-13
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值