Visual c++ 使用pdh函数获取多个性能计数器
请查看以下代码(摘自):Visual c++ 使用pdh函数获取多个性能计数器,visual-c++,performancecounter,Visual C++,Performancecounter,请查看以下代码(摘自): 每个柜台等。这将是乏味的,因为我需要对整个代码进行相应的更改。你能建议其他的解决方法吗?你将PdhOpenQuery一次,然后PdhAddCounter根据需要为查询提供尽可能多的计数器。非常感谢。但是,如何动态获取计数器。例如,Process(chrome)\ThreadCount,Processs(explorer)\%用户时间。是的,我可以用它来枚举所有进程。那么,添加这些计数器的下一步是什么?你能详细说明一下吗。非常感谢。然后你打了那么多次电话给PdhAddCo
每个柜台等。这将是乏味的,因为我需要对整个代码进行相应的更改。你能建议其他的解决方法吗?你将
PdhOpenQuery
一次,然后PdhAddCounter
根据需要为查询提供尽可能多的计数器。非常感谢。但是,如何动态获取计数器。例如,Process(chrome)\ThreadCount,Processs(explorer)\%用户时间。是的,我可以用它来枚举所有进程。那么,添加这些计数器的下一步是什么?你能详细说明一下吗。非常感谢。然后你打了那么多次电话给PdhAddCounter
。我不确定我是否理解困难的性质。你能告诉我如何再添加一个计数器吗?比如CONST PWSTR counter\u PATH2=L“\\Processor(1)\\\%Processor Time”;对于我在问题中提到的基本代码?在PdhAddCounter
call之后,再次调用PdhAddCounter
,第二个参数为COUNTER_PATH2
。
#include <windows.h>
#include <stdio.h>
#include <pdh.h>
#include <pdhmsg.h>
#pragma comment(lib, "pdh.lib")
CONST PWSTR COUNTER_PATH = L"\\Processor(0)\\% Processor Time";
CONST ULONG SAMPLE_INTERVAL_MS = 1000;
void DisplayCommandLineHelp(void)
{
wprintf(L"The command line must include a valid log file name.\n");
}
void wmain(int argc, WCHAR **argv)
{
HQUERY hQuery = NULL;
HLOG hLog = NULL;
PDH_STATUS pdhStatus;
DWORD dwLogType = PDH_LOG_TYPE_CSV;
HCOUNTER hCounter;
DWORD dwCount;
if (argc != 2)
{
DisplayCommandLineHelp();
goto cleanup;
}
// Open a query object.
pdhStatus = PdhOpenQuery(NULL, 0, &hQuery);
if (pdhStatus != ERROR_SUCCESS)
{
wprintf(L"PdhOpenQuery failed with 0x%x\n", pdhStatus);
goto cleanup;
}
// Add one counter that will provide the data.
pdhStatus = PdhAddCounter(hQuery,
COUNTER_PATH,
0,
&hCounter);
if (pdhStatus != ERROR_SUCCESS)
{
wprintf(L"PdhAddCounter failed with 0x%x\n", pdhStatus);
goto cleanup;
}
// Open the log file for write access.
pdhStatus = PdhOpenLog(argv[1],
PDH_LOG_WRITE_ACCESS | PDH_LOG_CREATE_ALWAYS,
&dwLogType,
hQuery,
0,
NULL,
&hLog);
if (pdhStatus != ERROR_SUCCESS)
{
wprintf(L"PdhOpenLog failed with 0x%x\n", pdhStatus);
goto cleanup;
}
// Write 10 records to the log file.
for (dwCount = 0; dwCount < 10; dwCount++)
{
wprintf(L"Writing record %d\n", dwCount);
pdhStatus = PdhUpdateLog (hLog, NULL);
if (ERROR_SUCCESS != pdhStatus)
{
wprintf(L"PdhUpdateLog failed with 0x%x\n", pdhStatus);
goto cleanup;
}
// Wait one second between samples for a counter update.
Sleep(SAMPLE_INTERVAL_MS);
}
cleanup:
// Close the log file.
if (hLog)
PdhCloseLog (hLog, 0);
// Close the query object.
if (hQuery)
PdhCloseQuery (hQuery);
}
CONST PWSTR COUNTER_PATH = L"\\Processor(0)\\% Processor Time";
CONST PWSTR COUNTER_PATH1 = L"\\Processor\\% Processor Time";
HQUERY hQuery = NULL;
HQUERY hQuery1 = NULL;
pdhStatus = PdhOpenQuery(NULL, 0, &hQuery);
pdhStatus1 = PdhOpenQuery(NULL, 0, &hQuery1);