多核CPU如何获取某个进程在各个核的使用率 本帖最后由 gordian693 于 2012-01-19 15:42:37 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用API测进程在双核PC的CPU使用率我先用WINXP自带的perfmon测一个进程的CPU使用率,这个进程的使用率有时会超过100%,因为是在双核电脑中,计数器% Processor Time是以单核的为标准。 然后我用PdhGetFormattedCounterValue还是测这个进程的CPU使用率,还是用的计数器% Processor Time,问题来了,在perfmon中显示CPU使用率超过100%的时间,我用PdhGetFormattedCounterValue测出的值却只是100%,怎么解决这个问题?不知道我把意思说清楚没有。下面是代码。 #include "stdafx.h" #include <Pdh.h> #include <stdlib.h> #include <conio.h> #include <windows.h> #include <iostream> #include <iomanip> using namespace std; const int SampleTime = 1000; PDH_STATUS status; PDH_HQUERY hQuery; HCOUNTER *pCounterHandle; wchar_t szPathBuffer = L"\\Processor(1)\\% Processor Time"; SYSTEMTIME szSampleTime; DWORD ctrType; PDH_FMT_COUNTERVALUE Value; int _tmain(int argc, _TCHAR* argv) { int duration = 0; double LastValue = 0.0; double sum = 0.0, average = 0.0, max = 0.0; status = PdhOpenQuery(0, 0, &hQuery); if(status != ERROR_SUCCESS) exit(1); pCounterHandle = (HCOUNTER *)GlobalAlloc(GPTR, sizeof(HCOUNTER)); status = PdhAddCounter(hQuery, szPathBuffer, 0, pCounterHandle); while (!_kbhit()) { GetLocalTime(&szSampleTime); status = PdhCollectQueryData(hQuery); cout << setw(2) << setfill('0') << szSampleTime.wHour << ": " << setw(2) << setfill('0') << szSampleTime.wMinute << ": " << setw(2) << setfill('0') << szSampleTime.wSecond << " "; status = PdhGetFormattedCounterValue (*pCounterHandle, PDH_FMT_DOUBLE, &ctrType, &Value); if(LastValue > 0 && Value.doubleValue == 0) { printf("CPU Usage: %-7.3f Time: %d", Value.doubleValue, duration); break; } printf("CPU Usage: %7.3f ", Value.doubleValue); Sleep(SampleTime); if(Value.doubleValue > 0) { duration++; if(max < Value.doubleValue) max = Value.doubleValue; sum = sum + Value.doubleValue; average = sum / duration; cout << "Time: " << duration; } cout << endl; LastValue = Value.doubleValue; } cout << endl << "Over:" << endl; cout << "Duration: " << duration << " " << endl; cout << "Average: " << average << " Max: " << max << endl; status = PdhCloseQuery(hQuery); return 0; } 这段代码是想测一个进程在一段时间内的平均CPU使用率,所以当实际的CPU使用率超过100%,而代码中得到的值仅为100%时会导致最后结果的不准确。 性能计数器: private PerformanceCounter cpuPerformance = new PerformanceCounter(); cpuPerformance.CategoryName = "Processor"; cpuPerformance.CounterName = "% Processor Time"; cpuPerformance.InstanceName = "_Total"; double y = cpuPerformance.NextValue(); //CPU的使用率 这个不知道了 估计windows的dll里有这个功能 请问如何对XML或数据进行自由排序并保存 c# 多线程的问题 急需一个web services ? 高分!高手们进来看一眼,程序中引用了图片,结果出现了很多的怪问题! 程序打包问题,急需解决送分送分了,谢谢大家~! 求一取值的正则表达式和一个打包的小问题 DataTable删除数据的问题 虚方法的使用? 求助▃◣一个简单问题 高分简单问题:FORM1如何控制FORM2 两个小问题,请求解决|两个小问题,请求解决 『C#』改变应用程序鼠标光标
然后我用PdhGetFormattedCounterValue还是测这个进程的CPU使用率,还是用的计数器% Processor Time,问题来了,在perfmon中显示CPU使用率超过100%的时间,我用PdhGetFormattedCounterValue测出的值却只是100%,怎么解决这个问题?不知道我把意思说清楚没有。下面是代码。 #include "stdafx.h"
#include <Pdh.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <iostream>
#include <iomanip>
using namespace std; const int SampleTime = 1000;
PDH_STATUS status;
PDH_HQUERY hQuery;
HCOUNTER *pCounterHandle;
wchar_t szPathBuffer = L"\\Processor(1)\\% Processor Time";
SYSTEMTIME szSampleTime;
DWORD ctrType;
PDH_FMT_COUNTERVALUE Value; int _tmain(int argc, _TCHAR* argv)
{
int duration = 0;
double LastValue = 0.0;
double sum = 0.0, average = 0.0, max = 0.0;
status = PdhOpenQuery(0, 0, &hQuery);
if(status != ERROR_SUCCESS)
exit(1);
pCounterHandle = (HCOUNTER *)GlobalAlloc(GPTR, sizeof(HCOUNTER));
status = PdhAddCounter(hQuery, szPathBuffer, 0, pCounterHandle);
while (!_kbhit())
{
GetLocalTime(&szSampleTime);
status = PdhCollectQueryData(hQuery);
cout << setw(2) << setfill('0') << szSampleTime.wHour << ": "
<< setw(2) << setfill('0') << szSampleTime.wMinute << ": "
<< setw(2) << setfill('0') << szSampleTime.wSecond << " ";
status = PdhGetFormattedCounterValue (*pCounterHandle, PDH_FMT_DOUBLE, &ctrType, &Value);
if(LastValue > 0 && Value.doubleValue == 0)
{
printf("CPU Usage: %-7.3f Time: %d", Value.doubleValue, duration);
break;
}
printf("CPU Usage: %7.3f ", Value.doubleValue);
Sleep(SampleTime);
if(Value.doubleValue > 0)
{
duration++;
if(max < Value.doubleValue)
max = Value.doubleValue;
sum = sum + Value.doubleValue;
average = sum / duration;
cout << "Time: " << duration;
}
cout << endl;
LastValue = Value.doubleValue;
}
cout << endl << "Over:" << endl;
cout << "Duration: " << duration << " " << endl;
cout << "Average: " << average << " Max: " << max << endl;
status = PdhCloseQuery(hQuery);
return 0;
}
这段代码是想测一个进程在一段时间内的平均CPU使用率,所以当实际的CPU使用率超过100%,而代码中得到的值仅为100%时会导致最后结果的不准确。
cpuPerformance.CounterName = "% Processor Time";
cpuPerformance.InstanceName = "_Total"; double y = cpuPerformance.NextValue(); //CPU的使用率