cpu usage monitor added.

This commit is contained in:
Zhen Tang 2013-07-06 17:04:15 +08:00
parent a798168c12
commit c32d2fe4d7
32 changed files with 341 additions and 501 deletions

View File

@ -1,12 +1,40 @@
// Monitor.cpp : 定义 DLL 应用程序的导出函数。
//
#include "stdafx.h"
#include "Monitor.h"
// 这是导出函数的一个示例。
MONITOR_API int Add(int a,int b)
MONITOR_API double GetCpuUsage(int sleepTime)
{
return a+b;
PDH_STATUS status;
HQUERY hquery;
status=PdhOpenQuery(NULL,NULL,&hquery);
if(status!=ERROR_SUCCESS)
{
return -1;
}
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;
}
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;
}

View File

@ -4,4 +4,4 @@
#define MONITOR_API __declspec(dllimport)
#endif
MONITOR_API int Add(int a,int b);
MONITOR_API double GetCpuUsage(int sleepTime);

View File

@ -413,7 +413,6 @@
<ItemGroup>
<ClInclude Include="Monitor.h" />
<ClInclude Include="stdafx.h" />
<ClInclude Include="targetver.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="dllmain.cpp">

View File

@ -18,9 +18,6 @@
<ClInclude Include="stdafx.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="targetver.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="Monitor.h">
<Filter>头文件</Filter>
</ClInclude>

View File

@ -1,4 +1,3 @@
// dllmain.cpp : 定义 DLL 应用程序的入口点。
#include "stdafx.h"
BOOL APIENTRY DllMain( HMODULE hModule,

View File

@ -1,8 +1 @@
// stdafx.cpp : 只包括标准包含文件的源文件
// Monitor.pch 将作为预编译头
// stdafx.obj 将包含预编译类型信息
#include "stdafx.h"
// TODO: 在 STDAFX.H 中
// 引用任何所需的附加头文件,而不是在此文件中引用

View File

@ -1,16 +1,6 @@
// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//
#pragma once
#include "targetver.h"
#define WIN32_LEAN_AND_MEAN // 从 Windows 头文件中排除极少使用的信息
// Windows 头文件:
#include <windows.h>
// TODO: 在此处引用程序需要的其他头文件
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <Pdh.h>
#pragma comment(lib, "Pdh.lib")

View File

@ -1,8 +0,0 @@
#pragma once
// 包括 SDKDDKVer.h 将定义可用的最高版本的 Windows 平台。
// 如果要为以前的 Windows 平台生成应用程序,请包括 WinSDKVer.h并将
// WIN32_WINNT 宏设置为要支持的平台,然后再包括 SDKDDKVer.h。
#include <SDKDDKVer.h>

View File

@ -1,21 +0,0 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class org_bench4q_monitor_JNITest */
#ifndef _Included_org_bench4q_monitor_JNITest
#define _Included_org_bench4q_monitor_JNITest
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: org_bench4q_monitor_JNITest
* Method: add
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_org_bench4q_monitor_JNITest_add
(JNIEnv *, jobject, jint, jint);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -4,4 +4,4 @@
#define MONITOR_API __declspec(dllimport)
#endif
MONITOR_API int Add(int a,int b);
MONITOR_API double GetCpuUsage(int sleepTime);

View File

@ -2,13 +2,8 @@
#include "Native.h"
#include "Monitor.h"
NATIVE_API int Test()
JNIEXPORT jdouble JNICALL Java_org_bench4q_monitor_probe_windows_WindowsCpuProbe_getCpuUsage
(JNIEnv * environment, jobject object)
{
return 42;
}
JNIEXPORT jint JNICALL Java_org_bench4q_monitor_JNITest_add
(JNIEnv * environment, jobject object, jint a, jint b)
{
return Add(a,b);
return GetCpuUsage(100);
}

View File

@ -1,15 +1,5 @@
// 下列 ifdef 块是创建使从 DLL 导出更简单的
// 宏的标准方法。此 DLL 中的所有文件都是用命令行上定义的 NATIVE_EXPORTS
// 符号编译的。在使用此 DLL 的
// 任何其他项目上不应定义此符号。这样,源文件中包含此文件的任何其他项目都会将
// NATIVE_API 函数视为是从 DLL 导入的,而此 DLL 则将用此宏定义的
// 符号视为是被导出的。
#ifdef NATIVE_EXPORTS
#define NATIVE_API __declspec(dllexport)
#else
#define NATIVE_API __declspec(dllimport)
#endif
#pragma comment(lib,"../x64/Debug/Monitor.lib")
NATIVE_API int Test(void);
#endif

View File

@ -305,6 +305,7 @@
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;NATIVE_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>$(JAVA_HOME)/include/win32;$(JAVA_HOME)/include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
@ -417,11 +418,10 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="JNITest.h" />
<ClInclude Include="WindowsCpuProbe.h" />
<ClInclude Include="Monitor.h" />
<ClInclude Include="Native.h" />
<ClInclude Include="stdafx.h" />
<ClInclude Include="targetver.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="dllmain.cpp">

View File

@ -18,16 +18,13 @@
<ClInclude Include="stdafx.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="targetver.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="Native.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="JNITest.h">
<ClInclude Include="Monitor.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="Monitor.h">
<ClInclude Include="WindowsCpuProbe.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>

View File

@ -0,0 +1,21 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class org_bench4q_monitor_probe_windows_WindowsCpuProbe */
#ifndef _Included_org_bench4q_monitor_probe_windows_WindowsCpuProbe
#define _Included_org_bench4q_monitor_probe_windows_WindowsCpuProbe
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: org_bench4q_monitor_probe_windows_WindowsCpuProbe
* Method: getCpuUsage
* Signature: ()D
*/
JNIEXPORT jdouble JNICALL Java_org_bench4q_monitor_probe_windows_WindowsCpuProbe_getCpuUsage
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -1,15 +1,9 @@
// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//
#pragma once
#include "targetver.h"
#pragma comment(lib,"../x64/Debug/Monitor.lib")
#define WIN32_LEAN_AND_MEAN // 从 Windows 头文件中排除极少使用的信息
// Windows 头文件:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <jni.h>
#include "JNITest.h"
#include "WindowsCpuProbe.h"

View File

@ -1,8 +0,0 @@
#pragma once
// 包括 SDKDDKVer.h 将定义可用的最高版本的 Windows 平台。
// 如果要为以前的 Windows 平台生成应用程序,请包括 WinSDKVer.h并将
// WIN32_WINNT 宏设置为要支持的平台,然后再包括 SDKDDKVer.h。
#include <SDKDDKVer.h>

View File

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

View File

@ -0,0 +1,10 @@
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
printf("%lf",GetCpuUsage(100));
system("pause");
return 0;
}

View File

@ -0,0 +1,157 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{50CE9AFE-3102-4568-8107-2E3D77F2FC22}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>Test</RootNamespace>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v110</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v110</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v110</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v110</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>Use</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>Use</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="Monitor.h" />
<ClInclude Include="stdafx.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="stdafx.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
</ClCompile>
<ClCompile Include="Test.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="源文件">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="头文件">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="资源文件">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="stdafx.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="Monitor.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="stdafx.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="Test.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -0,0 +1 @@
#include "stdafx.h"

View File

@ -0,0 +1,9 @@
#pragma once
#pragma comment(lib,"../x64/Debug/Monitor.lib")
#include <tchar.h>
#include <iostream>
#include "Monitor.h"
using namespace std;

View File

@ -4,31 +4,61 @@ Microsoft Visual Studio Solution File, Format Version 12.00
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Monitor", "Monitor\Monitor.vcxproj", "{200C95C6-D4F1-47CF-8178-11EF1C3B61B1}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Native", "Native\Native.vcxproj", "{286E6C20-9603-4870-BD34-FF54406AE1DC}"
ProjectSection(ProjectDependencies) = postProject
{200C95C6-D4F1-47CF-8178-11EF1C3B61B1} = {200C95C6-D4F1-47CF-8178-11EF1C3B61B1}
EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Test", "Test\Test.vcxproj", "{50CE9AFE-3102-4568-8107-2E3D77F2FC22}"
ProjectSection(ProjectDependencies) = postProject
{200C95C6-D4F1-47CF-8178-11EF1C3B61B1} = {200C95C6-D4F1-47CF-8178-11EF1C3B61B1}
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Mixed Platforms = Debug|Mixed Platforms
Debug|Win32 = Debug|Win32
Debug|x64 = Debug|x64
Release|Mixed Platforms = Release|Mixed Platforms
Release|Win32 = Release|Win32
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{200C95C6-D4F1-47CF-8178-11EF1C3B61B1}.Debug|Mixed Platforms.ActiveCfg = Debug|Win32
{200C95C6-D4F1-47CF-8178-11EF1C3B61B1}.Debug|Mixed Platforms.Build.0 = Debug|Win32
{200C95C6-D4F1-47CF-8178-11EF1C3B61B1}.Debug|Win32.ActiveCfg = Debug|Win32
{200C95C6-D4F1-47CF-8178-11EF1C3B61B1}.Debug|Win32.Build.0 = Debug|Win32
{200C95C6-D4F1-47CF-8178-11EF1C3B61B1}.Debug|x64.ActiveCfg = Debug|x64
{200C95C6-D4F1-47CF-8178-11EF1C3B61B1}.Debug|x64.Build.0 = Debug|x64
{200C95C6-D4F1-47CF-8178-11EF1C3B61B1}.Release|Mixed Platforms.ActiveCfg = Release|Win32
{200C95C6-D4F1-47CF-8178-11EF1C3B61B1}.Release|Mixed Platforms.Build.0 = Release|Win32
{200C95C6-D4F1-47CF-8178-11EF1C3B61B1}.Release|Win32.ActiveCfg = Release|Win32
{200C95C6-D4F1-47CF-8178-11EF1C3B61B1}.Release|Win32.Build.0 = Release|Win32
{200C95C6-D4F1-47CF-8178-11EF1C3B61B1}.Release|x64.ActiveCfg = Release|x64
{200C95C6-D4F1-47CF-8178-11EF1C3B61B1}.Release|x64.Build.0 = Release|x64
{286E6C20-9603-4870-BD34-FF54406AE1DC}.Debug|Mixed Platforms.ActiveCfg = Debug|Win32
{286E6C20-9603-4870-BD34-FF54406AE1DC}.Debug|Mixed Platforms.Build.0 = Debug|Win32
{286E6C20-9603-4870-BD34-FF54406AE1DC}.Debug|Win32.ActiveCfg = Debug|Win32
{286E6C20-9603-4870-BD34-FF54406AE1DC}.Debug|Win32.Build.0 = Debug|Win32
{286E6C20-9603-4870-BD34-FF54406AE1DC}.Debug|x64.ActiveCfg = Debug|x64
{286E6C20-9603-4870-BD34-FF54406AE1DC}.Debug|x64.Build.0 = Debug|x64
{286E6C20-9603-4870-BD34-FF54406AE1DC}.Release|Mixed Platforms.ActiveCfg = Release|Win32
{286E6C20-9603-4870-BD34-FF54406AE1DC}.Release|Mixed Platforms.Build.0 = Release|Win32
{286E6C20-9603-4870-BD34-FF54406AE1DC}.Release|Win32.ActiveCfg = Release|Win32
{286E6C20-9603-4870-BD34-FF54406AE1DC}.Release|Win32.Build.0 = Release|Win32
{286E6C20-9603-4870-BD34-FF54406AE1DC}.Release|x64.ActiveCfg = Release|x64
{286E6C20-9603-4870-BD34-FF54406AE1DC}.Release|x64.Build.0 = Release|x64
{50CE9AFE-3102-4568-8107-2E3D77F2FC22}.Debug|Mixed Platforms.ActiveCfg = Debug|Win32
{50CE9AFE-3102-4568-8107-2E3D77F2FC22}.Debug|Mixed Platforms.Build.0 = Debug|Win32
{50CE9AFE-3102-4568-8107-2E3D77F2FC22}.Debug|Win32.ActiveCfg = Debug|Win32
{50CE9AFE-3102-4568-8107-2E3D77F2FC22}.Debug|Win32.Build.0 = Debug|Win32
{50CE9AFE-3102-4568-8107-2E3D77F2FC22}.Debug|x64.ActiveCfg = Debug|x64
{50CE9AFE-3102-4568-8107-2E3D77F2FC22}.Debug|x64.Build.0 = Debug|x64
{50CE9AFE-3102-4568-8107-2E3D77F2FC22}.Release|Mixed Platforms.ActiveCfg = Release|Win32
{50CE9AFE-3102-4568-8107-2E3D77F2FC22}.Release|Mixed Platforms.Build.0 = Release|Win32
{50CE9AFE-3102-4568-8107-2E3D77F2FC22}.Release|Win32.ActiveCfg = Release|Win32
{50CE9AFE-3102-4568-8107-2E3D77F2FC22}.Release|Win32.Build.0 = Release|Win32
{50CE9AFE-3102-4568-8107-2E3D77F2FC22}.Release|x64.ActiveCfg = Release|x64
{50CE9AFE-3102-4568-8107-2E3D77F2FC22}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -3,9 +3,9 @@
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>dir</id>
<id>publish</id>
<formats>
<format>dir</format>
<format>tar.gz</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
@ -21,5 +21,13 @@
<source>target/bench4q-monitor.jar</source>
<outputDirectory>/</outputDirectory>
</file>
<file>
<source>WindowsMonitor/x64/Release/Monitor.dll</source>
<outputDirectory>/lib</outputDirectory>
</file>
<file>
<source>WindowsMonitor/x64/Release/Native.dll</source>
<outputDirectory>/lib</outputDirectory>
</file>
</files>
</assembly>

View File

@ -1,15 +0,0 @@
package org.bench4q.monitor;
public class JNITest {
static {
String path = Thread.currentThread().getContextClassLoader()
.getResource("Native.dll").getFile();
System.load(path);
}
public native int add(int a, int b);
public static void main(String[] args) {
System.out.println(new JNITest().add(1, 2));
}
}

View File

@ -1,10 +1,19 @@
package org.bench4q.monitor;
import org.bench4q.monitor.probe.windows.WindowsCpuProbe;
public class Main {
static {
System.load(System.getProperty("user.dir").replace("\\", "/")
+ "/lib/Monitor.dll");
System.load(System.getProperty("user.dir").replace("\\", "/")
+ "/lib/Native.dll");
}
public static void main(String[] args) {
MonitorServer monitorServer = new MonitorServer(5555);
monitorServer.start();
System.out.println(new WindowsCpuProbe().getCpuUsage());
}
}

View File

@ -1,9 +1,5 @@
package org.bench4q.monitor.api;
import org.bench4q.monitor.api.model.MonitorResultModel;
import org.bench4q.monitor.entity.MonitorInfo;
import org.bench4q.monitor.service.MonitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@ -12,36 +8,10 @@ import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/monitor")
public class MonitorController {
private MonitorService monitorService;
private MonitorService getMonitorService() {
return monitorService;
}
@Autowired
private void setMonitorService(MonitorService monitorService) {
this.monitorService = monitorService;
}
@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public MonitorResultModel index() {
MonitorInfo monitorInfo = this.getMonitorService().getMonitorInfo();
MonitorResultModel monitorResultModel = new MonitorResultModel();
monitorResultModel.setCpuRatio(monitorInfo.getCpuRatio());
monitorResultModel.setFreePhysicsMemory(monitorInfo
.getFreePhysicsMemory());
monitorResultModel.setFreeVirtualMachineMemory(monitorInfo
.getFreeVirtualMachineMemory());
monitorResultModel.setMaxVirtualMachineMemory(monitorInfo
.getMaxVirtualMachineMemory());
monitorResultModel.setOperationSystem(monitorInfo.getOperationSystem());
monitorResultModel.setTotalPhysicsMemory(monitorInfo
.getTotalPhysicsMemory());
monitorResultModel.setTotalVirtualMachineMemory(monitorInfo
.getTotalVirtualMachineMemory());
monitorResultModel.setUsedPhysicsMemory(monitorInfo
.getUsedPhysicsMemory());
return monitorResultModel;
public String index() {
return "It works!";
}
}

View File

@ -1,88 +0,0 @@
package org.bench4q.monitor.api.model;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "monitorResult")
public class MonitorResultModel {
private long totalVirtualMachineMemory;
private long freeVirtualMachineMemory;
private long maxVirtualMachineMemory;
private String operationSystem;
private long totalPhysicsMemory;
private long freePhysicsMemory;
private long usedPhysicsMemory;
private double cpuRatio;
@XmlElement
public long getTotalVirtualMachineMemory() {
return totalVirtualMachineMemory;
}
public void setTotalVirtualMachineMemory(long totalVirtualMachineMemory) {
this.totalVirtualMachineMemory = totalVirtualMachineMemory;
}
@XmlElement
public long getFreeVirtualMachineMemory() {
return freeVirtualMachineMemory;
}
public void setFreeVirtualMachineMemory(long freeVirtualMachineMemory) {
this.freeVirtualMachineMemory = freeVirtualMachineMemory;
}
@XmlElement
public long getMaxVirtualMachineMemory() {
return maxVirtualMachineMemory;
}
public void setMaxVirtualMachineMemory(long maxVirtualMachineMemory) {
this.maxVirtualMachineMemory = maxVirtualMachineMemory;
}
@XmlElement
public String getOperationSystem() {
return operationSystem;
}
public void setOperationSystem(String operationSystem) {
this.operationSystem = operationSystem;
}
@XmlElement
public long getTotalPhysicsMemory() {
return totalPhysicsMemory;
}
public void setTotalPhysicsMemory(long totalPhysicsMemory) {
this.totalPhysicsMemory = totalPhysicsMemory;
}
@XmlElement
public long getFreePhysicsMemory() {
return freePhysicsMemory;
}
public void setFreePhysicsMemory(long freePhysicsMemory) {
this.freePhysicsMemory = freePhysicsMemory;
}
@XmlElement
public long getUsedPhysicsMemory() {
return usedPhysicsMemory;
}
public void setUsedPhysicsMemory(long usedPhysicsMemory) {
this.usedPhysicsMemory = usedPhysicsMemory;
}
@XmlElement
public double getCpuRatio() {
return cpuRatio;
}
public void setCpuRatio(double cpuRatio) {
this.cpuRatio = cpuRatio;
}
}

View File

@ -1,77 +0,0 @@
package org.bench4q.monitor.entity;
public class MonitorInfo {
private long totalVirtualMachineMemory;
private long freeVirtualMachineMemory;
private long maxVirtualMachineMemory;
private String operationSystem;
private long totalPhysicsMemory;
private long freePhysicsMemory;
private long usedPhysicsMemory;
private double cpuRatio;
public long getTotalVirtualMachineMemory() {
return totalVirtualMachineMemory;
}
public void setTotalVirtualMachineMemory(long totalVirtualMachineMemory) {
this.totalVirtualMachineMemory = totalVirtualMachineMemory;
}
public long getFreeVirtualMachineMemory() {
return freeVirtualMachineMemory;
}
public void setFreeVirtualMachineMemory(long freeVirtualMachineMemory) {
this.freeVirtualMachineMemory = freeVirtualMachineMemory;
}
public long getMaxVirtualMachineMemory() {
return maxVirtualMachineMemory;
}
public void setMaxVirtualMachineMemory(long maxVirtualMachineMemory) {
this.maxVirtualMachineMemory = maxVirtualMachineMemory;
}
public String getOperationSystem() {
return operationSystem;
}
public void setOperationSystem(String operationSystem) {
this.operationSystem = operationSystem;
}
public long getTotalPhysicsMemory() {
return totalPhysicsMemory;
}
public void setTotalPhysicsMemory(long totalPhysicsMemory) {
this.totalPhysicsMemory = totalPhysicsMemory;
}
public long getFreePhysicsMemory() {
return freePhysicsMemory;
}
public void setFreePhysicsMemory(long freePhysicsMemory) {
this.freePhysicsMemory = freePhysicsMemory;
}
public long getUsedPhysicsMemory() {
return usedPhysicsMemory;
}
public void setUsedPhysicsMemory(long usedPhysicsMemory) {
this.usedPhysicsMemory = usedPhysicsMemory;
}
public double getCpuRatio() {
return cpuRatio;
}
public void setCpuRatio(double cpuRatio) {
this.cpuRatio = cpuRatio;
}
}

View File

@ -1,5 +1,5 @@
package org.bench4q.monitor.probe.windows;
public class WindowsCpuProbe {
// /proc/stat
public native double getCpuUsage();
}

View File

@ -1,180 +0,0 @@
package org.bench4q.monitor.service;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.lang.management.ManagementFactory;
import java.util.StringTokenizer;
import com.sun.management.OperatingSystemMXBean;
import org.bench4q.monitor.entity.MonitorInfo;
import org.springframework.stereotype.Component;
@Component
public class MonitorService {
private static final int CPUTIME = 30;
private static final int PERCENT = 100;
private static final int FAULTLENGTH = 10;
public MonitorInfo getMonitorInfo() {
MonitorInfo monitorInfo = new MonitorInfo();
monitorInfo.setTotalVirtualMachineMemory(Runtime.getRuntime()
.totalMemory());
monitorInfo.setFreeVirtualMachineMemory(Runtime.getRuntime()
.freeMemory());
monitorInfo
.setMaxVirtualMachineMemory(Runtime.getRuntime().maxMemory());
monitorInfo.setOperationSystem(System.getProperty("os.name"));
OperatingSystemMXBean operatingSystemMXBean = (OperatingSystemMXBean) ManagementFactory
.getOperatingSystemMXBean();
monitorInfo.setTotalPhysicsMemory(operatingSystemMXBean
.getTotalPhysicalMemorySize());
monitorInfo.setFreePhysicsMemory(operatingSystemMXBean
.getFreePhysicalMemorySize());
monitorInfo.setUsedPhysicsMemory(monitorInfo.getTotalPhysicsMemory()
- monitorInfo.getFreePhysicsMemory());
ThreadGroup parentThread;
for (parentThread = Thread.currentThread().getThreadGroup(); parentThread
.getParent() != null; parentThread = parentThread.getParent())
;
double cpuRatio = 0;
if (monitorInfo.getOperationSystem().toLowerCase()
.startsWith("windows")) {
cpuRatio = this.getCpuRatioForWindows();
} else {
cpuRatio = this.getCpuRatioForLinux();
}
monitorInfo.setCpuRatio(cpuRatio);
return monitorInfo;
}
private double getCpuRatioForLinux() {
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
StringTokenizer tokenStat = null;
try {
Process process = Runtime.getRuntime().exec("top -b -n 1");
is = process.getInputStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
br.readLine();
br.readLine();
tokenStat = new StringTokenizer(br.readLine());
tokenStat.nextToken();
tokenStat.nextToken();
tokenStat.nextToken();
tokenStat.nextToken();
tokenStat.nextToken();
tokenStat.nextToken();
tokenStat.nextToken();
String cpuUsage = tokenStat.nextToken();
Float usage = new Float(
cpuUsage.substring(0, cpuUsage.indexOf("%")));
return (1 - usage.floatValue() / 100);
} catch (Exception e) {
e.printStackTrace();
return -1;
} finally {
try {
if (is != null) {
is.close();
}
if (isr != null) {
isr.close();
}
if (br != null) {
br.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
private double getCpuRatioForWindows() {
try {
String procCmd = System.getenv("windir")
+ "\\system32\\wbem\\wmic.exe process get Caption,CommandLine,"
+ "KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount";
// È¡½ø³ÌÐÅÏ¢
long[] c0 = readCpu(Runtime.getRuntime().exec(procCmd));
Thread.sleep(CPUTIME);
long[] c1 = readCpu(Runtime.getRuntime().exec(procCmd));
if (c0 != null && c1 != null) {
long idletime = c1[0] - c0[0];
long busytime = c1[1] - c0[1];
return Double.valueOf(
PERCENT * (busytime) / (busytime + idletime))
.doubleValue();
} else {
return 0.0;
}
} catch (Exception ex) {
ex.printStackTrace();
return 0.0;
}
}
private long[] readCpu(final Process proc) {
long[] retn = new long[2];
try {
proc.getOutputStream().close();
InputStreamReader ir = new InputStreamReader(proc.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line = input.readLine();
if (line == null || line.length() < FAULTLENGTH) {
return null;
}
int capidx = line.indexOf("Caption");
int cmdidx = line.indexOf("CommandLine");
int rocidx = line.indexOf("ReadOperationCount");
int umtidx = line.indexOf("UserModeTime");
int kmtidx = line.indexOf("KernelModeTime");
int wocidx = line.indexOf("WriteOperationCount");
long idletime = 0;
long kneltime = 0;
long usertime = 0;
while ((line = input.readLine()) != null) {
if (line.length() < wocidx) {
continue;
}
String caption = line.substring(capidx, cmdidx - 1).trim();
String cmd = line.substring(cmdidx, kmtidx - 1).trim();
if (cmd.indexOf("wmic.exe") >= 0) {
continue;
}
if (caption.equals("System Idle Process")
|| caption.equals("System")) {
idletime += Long.valueOf(
line.substring(kmtidx, rocidx - 1).trim())
.longValue();
idletime += Long.valueOf(
line.substring(umtidx, wocidx - 1).trim())
.longValue();
continue;
}
kneltime += Long.valueOf(
line.substring(kmtidx, rocidx - 1).trim()).longValue();
usertime += Long.valueOf(
line.substring(umtidx, wocidx - 1).trim()).longValue();
}
retn[0] = idletime;
retn[1] = kneltime + usertime;
return retn;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
proc.getInputStream().close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}