can get instance name now.

This commit is contained in:
Zhen Tang 2013-07-06 23:15:17 +08:00
parent 8f246d5bdb
commit 0ec099bb47
22 changed files with 233 additions and 79 deletions

View File

@ -0,0 +1,113 @@
#include "stdafx.h"
double GetCounterValue(const wchar_t * fullCounterPath)
{
HQUERY hquery;
PDH_STATUS status;
status=PdhOpenQuery(NULL,NULL,&hquery);
if(status!=ERROR_SUCCESS)
{
return -1;
}
HCOUNTER counter=NULL;
status=PdhAddCounter(hquery, fullCounterPath, NULL, &counter);
if(status!=ERROR_SUCCESS)
{
return -1;
}
status=PdhCollectQueryData(hquery);
if(status!=ERROR_SUCCESS)
{
return -1;
}
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;
}
double GetCounterValueWithSleep(const wchar_t * fullCounterPath,int idleTime)
{
HQUERY hquery;
PDH_STATUS status;
status=PdhOpenQuery(NULL,NULL,&hquery);
if(status!=ERROR_SUCCESS)
{
return -1;
}
HCOUNTER counter=NULL;
status=PdhAddCounter(hquery, fullCounterPath, NULL, &counter);
if(status!=ERROR_SUCCESS)
{
return -1;
}
status=PdhCollectQueryData(hquery);
if(status!=ERROR_SUCCESS)
{
return -1;
}
Sleep(idleTime);
status=PdhCollectQueryData(hquery);
if(status!=ERROR_SUCCESS)
{
return -1;
}
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;
}
list<wstring> GetInstanceName(const wchar_t * objectName)
{
list<wstring> ret;
wchar_t * counterListBuffer = NULL;
wchar_t * instanceListBuffer = NULL;
DWORD dwCounterListSize = 0;
DWORD dwInstanceListSize = 0;
BOOL pass =FALSE;
PDH_STATUS status = PdhEnumObjectItems(NULL,NULL,objectName,counterListBuffer,&dwCounterListSize,instanceListBuffer,&dwInstanceListSize,PERF_DETAIL_WIZARD, 0);
if(status == PDH_MORE_DATA)
{
counterListBuffer=(wchar_t *)malloc((dwCounterListSize*sizeof(wchar_t)));
instanceListBuffer=(wchar_t *)malloc((dwInstanceListSize*sizeof(wchar_t)));
status= PdhEnumObjectItems(NULL,NULL,objectName,counterListBuffer,&dwCounterListSize,instanceListBuffer,&dwInstanceListSize,PERF_DETAIL_WIZARD,0);
if(status == ERROR_SUCCESS)
{
wchar_t * instance = instanceListBuffer;
for(; *instance != 0; instance += lstrlen(instance) + 1)
{
ret.push_back(wstring(instance));
}
}
}
if(counterListBuffer != NULL)
{
free(counterListBuffer);
}
if(instanceListBuffer != NULL)
{
free(instanceListBuffer);
}
return ret;
}

View File

@ -0,0 +1,5 @@
#pragma once
double GetCounterValue(const wchar_t * fullCounterPath);
double GetCounterValueWithSleep(const wchar_t * fullCounterPath,int sleepTime);
list<wstring> GetInstanceName(const wchar_t * objectName);

View File

@ -1,46 +0,0 @@
#include "stdafx.h"
#include "Monitor.h"
PDH_FMT_COUNTERVALUE GetCounterValue(wchar_t * fullCounterPath,unsigned long format)
{
HQUERY hquery;
PdhOpenQuery(NULL,NULL,&hquery);
HCOUNTER counter=NULL;
PdhAddCounter(hquery, fullCounterPath, NULL, &counter);
PdhCollectQueryData(hquery);
PDH_FMT_COUNTERVALUE counterValue;
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;
}

View File

@ -3,6 +3,3 @@
#else
#define MONITOR_API __declspec(dllimport)
#endif
MONITOR_API double GetCpuUsage(int sleepTime);
MONITOR_API double GetFileReadBytesPerSecond(int sleepTime);

View File

@ -411,10 +411,14 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="Common.h" />
<ClInclude Include="Monitor.h" />
<ClInclude Include="MonitorApi.h" />
<ClInclude Include="ProcessorInformation.h" />
<ClInclude Include="stdafx.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="Common.cpp" />
<ClCompile Include="dllmain.cpp">
<CompileAsManaged Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</CompileAsManaged>
<CompileAsManaged Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</CompileAsManaged>
@ -453,7 +457,7 @@
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release-x86|x64'">
</PrecompiledHeader>
</ClCompile>
<ClCompile Include="Monitor.cpp" />
<ClCompile Include="ProcessorInformation.cpp" />
<ClCompile Include="stdafx.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>

View File

@ -21,15 +21,27 @@
<ClInclude Include="Monitor.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="Common.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="ProcessorInformation.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="MonitorApi.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="stdafx.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="Monitor.cpp">
<ClCompile Include="dllmain.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="dllmain.cpp">
<ClCompile Include="Common.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="ProcessorInformation.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>

View File

@ -0,0 +1,14 @@
#pragma once
#ifdef MONITOR_EXPORTS
#define MONITOR_API __declspec(dllexport)
#else
#define MONITOR_API __declspec(dllimport)
#endif
#include <list>
#include <string>
using namespace std;
MONITOR_API list<wstring> GetProcessorInformationInstances();
MONITOR_API double GetProcessorTimePercent(wchar_t * instanceName, int sleepTime);

View File

@ -0,0 +1,18 @@
#include "stdafx.h"
#include "Monitor.h"
#include "Common.h"
MONITOR_API list<wstring> GetProcessorInformationInstances()
{
return GetInstanceName(L"Processor Information");
}
MONITOR_API double GetProcessorTimePercent(wchar_t * instanceName, int sleepTime)
{
wstring fullCounterPath(L"");
fullCounterPath+=L"\\Processor Information(";
fullCounterPath+=instanceName;
fullCounterPath+=L")\\% Processor Time";
double ret=GetCounterValueWithSleep(fullCounterPath.c_str(),sleepTime);
return ret;
}

View File

@ -0,0 +1,8 @@
#pragma once
#include "Monitor.h"
#include <list>
#include <string>
using namespace std;
MONITOR_API list<wstring> GetProcessorInformationInstances();
MONITOR_API double GetProcessorTimePercent(wchar_t * instanceName, int sleepTime);

View File

@ -2,5 +2,14 @@
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <Pdh.h>
#include <PdhMsg.h>
#include <memory>
#include <string>
#include <list>
using namespace std;
#pragma comment(lib, "Pdh.lib")

View File

@ -1,8 +0,0 @@
#ifdef MONITOR_EXPORTS
#define MONITOR_API __declspec(dllexport)
#else
#define MONITOR_API __declspec(dllimport)
#endif
MONITOR_API double GetCpuUsage(int sleepTime);
MONITOR_API double GetFileReadBytesPerSecond(int sleepTime);

View File

@ -0,0 +1,14 @@
#pragma once
#ifdef MONITOR_EXPORTS
#define MONITOR_API __declspec(dllexport)
#else
#define MONITOR_API __declspec(dllimport)
#endif
#include <list>
#include <string>
using namespace std;
MONITOR_API list<wstring> GetProcessorInformationInstances();
MONITOR_API double GetProcessorTimePercent(wchar_t * instanceName, int sleepTime);

View File

@ -1,9 +1,8 @@
#include "stdafx.h"
#include "Native.h"
#include "Monitor.h"
JNIEXPORT jdouble JNICALL Java_org_bench4q_monitor_probe_windows_WindowsCpuProbe_getCpuUsage
(JNIEnv * environment, jobject object)
{
return GetCpuUsage(100);
return GetProcessorTimePercent(L"_Total",1500);
}

View File

@ -419,7 +419,7 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="WindowsCpuProbe.h" />
<ClInclude Include="Monitor.h" />
<ClInclude Include="MonitorApi.h" />
<ClInclude Include="Native.h" />
<ClInclude Include="stdafx.h" />
</ItemGroup>

View File

@ -21,10 +21,10 @@
<ClInclude Include="Native.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="Monitor.h">
<ClInclude Include="WindowsCpuProbe.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="WindowsCpuProbe.h">
<ClInclude Include="MonitorApi.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>

View File

@ -6,4 +6,6 @@
#include <windows.h>
#include <jni.h>
#include "WindowsCpuProbe.h"
#include "WindowsCpuProbe.h"
#include "MonitorApi.h"

View File

@ -1,8 +0,0 @@
#ifdef MONITOR_EXPORTS
#define MONITOR_API __declspec(dllexport)
#else
#define MONITOR_API __declspec(dllimport)
#endif
MONITOR_API double GetCpuUsage(int sleepTime);
MONITOR_API double GetFileReadBytesPerSecond(int sleepTime);

View File

@ -0,0 +1,14 @@
#pragma once
#ifdef MONITOR_EXPORTS
#define MONITOR_API __declspec(dllexport)
#else
#define MONITOR_API __declspec(dllimport)
#endif
#include <list>
#include <string>
using namespace std;
MONITOR_API list<wstring> GetProcessorInformationInstances();
MONITOR_API double GetProcessorTimePercent(wchar_t * instanceName, int sleepTime);

View File

@ -1,11 +1,17 @@
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
list<wstring> instances=GetProcessorInformationInstances();
list<wstring>::iterator iter;
for(iter=instances.begin();iter!=instances.end();iter++)
{
wprintf(L"%s\n",*iter);
}
while(1)
{
printf("%lf\n",GetFileReadBytesPerSecond(500));
printf("%lf\n",GetProcessorTimePercent(L"_Total",1500));
}
return 0;
}

View File

@ -139,7 +139,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="Monitor.h" />
<ClInclude Include="MonitorApi.h" />
<ClInclude Include="stdafx.h" />
</ItemGroup>
<ItemGroup>

View File

@ -18,7 +18,7 @@
<ClInclude Include="stdafx.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="Monitor.h">
<ClInclude Include="MonitorApi.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>

View File

@ -4,6 +4,7 @@
#include <tchar.h>
#include <iostream>
#include "Monitor.h"
using namespace std;
#include "MonitorApi.h"