From 8f246d5bdbc0643636464777220aa9fd32b0bf33 Mon Sep 17 00:00:00 2001 From: Zhen Tang Date: Sat, 6 Jul 2013 18:00:51 +0800 Subject: [PATCH] more monitor added. --- WindowsMonitor/Monitor/Monitor.cpp | 66 ++++++++++++++++-------------- WindowsMonitor/Monitor/Monitor.h | 1 + WindowsMonitor/Native/Monitor.h | 3 +- WindowsMonitor/Native/stdafx.h | 2 +- WindowsMonitor/Test/Monitor.h | 3 +- WindowsMonitor/Test/Test.cpp | 6 ++- WindowsMonitor/Test/stdafx.h | 2 +- 7 files changed, 47 insertions(+), 36 deletions(-) diff --git a/WindowsMonitor/Monitor/Monitor.cpp b/WindowsMonitor/Monitor/Monitor.cpp index 5830b86f..f53dbc16 100644 --- a/WindowsMonitor/Monitor/Monitor.cpp +++ b/WindowsMonitor/Monitor/Monitor.cpp @@ -1,40 +1,46 @@ #include "stdafx.h" #include "Monitor.h" -MONITOR_API double GetCpuUsage(int sleepTime) +PDH_FMT_COUNTERVALUE GetCounterValue(wchar_t * fullCounterPath,unsigned long format) { - PDH_STATUS status; HQUERY hquery; - status=PdhOpenQuery(NULL,NULL,&hquery); - if(status!=ERROR_SUCCESS) - { - return -1; - } + PdhOpenQuery(NULL,NULL,&hquery); HCOUNTER counter=NULL; - wchar_t * fullCounterPath=L"\\Processor(*)\\% Processor Time"; PdhAddCounter(hquery, fullCounterPath, NULL, &counter); - status = PdhCollectQueryData(hquery); - if (status != ERROR_SUCCESS) - { - return -1; - } - Sleep(sleepTime); - status = PdhCollectQueryData(hquery); - if (status != ERROR_SUCCESS) - { - return -1; - } + PdhCollectQueryData(hquery); PDH_FMT_COUNTERVALUE counterValue; - status = PdhGetFormattedCounterValue(counter,PDH_FMT_DOUBLE,NULL,&counterValue); - if(status != ERROR_SUCCESS) - { - return -1; - } - status = PdhCloseQuery(hquery); - if (status != ERROR_SUCCESS) - { - return -1; - } - return counterValue.doubleValue; + PdhGetFormattedCounterValue(counter,format,NULL,&counterValue); + PdhCloseQuery(hquery); + return counterValue; } +PDH_FMT_COUNTERVALUE GetCounterValueWithSleep(wchar_t * fullCounterPath,unsigned long format,int sleepTime) +{ + HQUERY hquery; + PdhOpenQuery(NULL,NULL,&hquery); + HCOUNTER counter=NULL; + PdhAddCounter(hquery, fullCounterPath, NULL, &counter); + PdhCollectQueryData(hquery); + Sleep(sleepTime); + PdhCollectQueryData(hquery); + PDH_FMT_COUNTERVALUE counterValue; + PdhGetFormattedCounterValue(counter,format,NULL,&counterValue); + PdhCloseQuery(hquery); + return counterValue; +} + +MONITOR_API double GetCpuUsage(int sleepTime) +{ + wchar_t * fullCounterPath=L"\\Processor(*)\\% Processor Time"; + unsigned long format=PDH_FMT_DOUBLE; + double ret=GetCounterValueWithSleep(fullCounterPath,format,sleepTime).doubleValue; + return ret; +} + +MONITOR_API double GetFileReadBytesPerSecond(int sleepTime) +{ + wchar_t * fullCounterPath=L"\\System\\File Read Bytes/sec"; + unsigned long format=PDH_FMT_DOUBLE; + double ret=GetCounterValueWithSleep(fullCounterPath,format,sleepTime).doubleValue; + return ret; +} diff --git a/WindowsMonitor/Monitor/Monitor.h b/WindowsMonitor/Monitor/Monitor.h index f4d274a3..48bc9956 100644 --- a/WindowsMonitor/Monitor/Monitor.h +++ b/WindowsMonitor/Monitor/Monitor.h @@ -5,3 +5,4 @@ #endif MONITOR_API double GetCpuUsage(int sleepTime); +MONITOR_API double GetFileReadBytesPerSecond(int sleepTime); diff --git a/WindowsMonitor/Native/Monitor.h b/WindowsMonitor/Native/Monitor.h index 7a6e3e9a..5003ea51 100644 --- a/WindowsMonitor/Native/Monitor.h +++ b/WindowsMonitor/Native/Monitor.h @@ -4,4 +4,5 @@ #define MONITOR_API __declspec(dllimport) #endif -MONITOR_API double GetCpuUsage(int sleepTime); \ No newline at end of file +MONITOR_API double GetCpuUsage(int sleepTime); +MONITOR_API double GetFileReadBytesPerSecond(int sleepTime); \ No newline at end of file diff --git a/WindowsMonitor/Native/stdafx.h b/WindowsMonitor/Native/stdafx.h index 04b08455..91b5ed56 100644 --- a/WindowsMonitor/Native/stdafx.h +++ b/WindowsMonitor/Native/stdafx.h @@ -1,6 +1,6 @@ #pragma once -#pragma comment(lib,"../x64/Debug/Monitor.lib") +#pragma comment(lib,"../x64/Release/Monitor.lib") #define WIN32_LEAN_AND_MEAN #include diff --git a/WindowsMonitor/Test/Monitor.h b/WindowsMonitor/Test/Monitor.h index 7a6e3e9a..5003ea51 100644 --- a/WindowsMonitor/Test/Monitor.h +++ b/WindowsMonitor/Test/Monitor.h @@ -4,4 +4,5 @@ #define MONITOR_API __declspec(dllimport) #endif -MONITOR_API double GetCpuUsage(int sleepTime); \ No newline at end of file +MONITOR_API double GetCpuUsage(int sleepTime); +MONITOR_API double GetFileReadBytesPerSecond(int sleepTime); \ No newline at end of file diff --git a/WindowsMonitor/Test/Test.cpp b/WindowsMonitor/Test/Test.cpp index 7319a2f8..8d067e9d 100644 --- a/WindowsMonitor/Test/Test.cpp +++ b/WindowsMonitor/Test/Test.cpp @@ -3,8 +3,10 @@ int _tmain(int argc, _TCHAR* argv[]) { - printf("%lf",GetCpuUsage(100)); - system("pause"); + while(1) + { + printf("%lf\n",GetFileReadBytesPerSecond(500)); + } return 0; } diff --git a/WindowsMonitor/Test/stdafx.h b/WindowsMonitor/Test/stdafx.h index 1d43c802..945c1cc5 100644 --- a/WindowsMonitor/Test/stdafx.h +++ b/WindowsMonitor/Test/stdafx.h @@ -1,6 +1,6 @@ #pragma once -#pragma comment(lib,"../x64/Debug/Monitor.lib") +#pragma comment(lib,"../x64/Release/Monitor.lib") #include #include