From c32d2fe4d7369630626d5dca5c80e28967079b1c Mon Sep 17 00:00:00 2001 From: Zhen Tang Date: Sat, 6 Jul 2013 17:04:15 +0800 Subject: [PATCH] cpu usage monitor added. --- WindowsMonitor/Monitor/Monitor.cpp | 40 +++- WindowsMonitor/Monitor/Monitor.h | 2 +- WindowsMonitor/Monitor/Monitor.vcxproj | 1 - .../Monitor/Monitor.vcxproj.filters | 3 - WindowsMonitor/Monitor/dllmain.cpp | 1 - WindowsMonitor/Monitor/stdafx.cpp | 7 - WindowsMonitor/Monitor/stdafx.h | 18 +- WindowsMonitor/Monitor/targetver.h | 8 - WindowsMonitor/Native/JNITest.h | 21 -- WindowsMonitor/Native/Monitor.h | 2 +- WindowsMonitor/Native/Native.cpp | 11 +- WindowsMonitor/Native/Native.h | 12 +- WindowsMonitor/Native/Native.vcxproj | 4 +- WindowsMonitor/Native/Native.vcxproj.filters | 7 +- WindowsMonitor/Native/WindowsCpuProbe.h | 21 ++ WindowsMonitor/Native/stdafx.h | 12 +- WindowsMonitor/Native/targetver.h | 8 - WindowsMonitor/Test/Monitor.h | 7 + WindowsMonitor/Test/Test.cpp | 10 + WindowsMonitor/Test/Test.vcxproj | 157 +++++++++++++++ WindowsMonitor/Test/Test.vcxproj.filters | 33 ++++ WindowsMonitor/Test/stdafx.cpp | 1 + WindowsMonitor/Test/stdafx.h | 9 + WindowsMonitor/WindowsMonitor.sln | 30 +++ descriptor.xml | 12 +- .../java/org/bench4q/monitor/JNITest.java | 15 -- src/main/java/org/bench4q/monitor/Main.java | 9 + .../monitor/api/MonitorController.java | 34 +--- .../monitor/api/model/MonitorResultModel.java | 88 --------- .../bench4q/monitor/entity/MonitorInfo.java | 77 -------- .../probe/windows/WindowsCpuProbe.java | 2 +- .../monitor/service/MonitorService.java | 180 ------------------ 32 files changed, 341 insertions(+), 501 deletions(-) delete mode 100644 WindowsMonitor/Monitor/targetver.h delete mode 100644 WindowsMonitor/Native/JNITest.h create mode 100644 WindowsMonitor/Native/WindowsCpuProbe.h delete mode 100644 WindowsMonitor/Native/targetver.h create mode 100644 WindowsMonitor/Test/Monitor.h create mode 100644 WindowsMonitor/Test/Test.cpp create mode 100644 WindowsMonitor/Test/Test.vcxproj create mode 100644 WindowsMonitor/Test/Test.vcxproj.filters create mode 100644 WindowsMonitor/Test/stdafx.cpp create mode 100644 WindowsMonitor/Test/stdafx.h delete mode 100644 src/main/java/org/bench4q/monitor/JNITest.java delete mode 100644 src/main/java/org/bench4q/monitor/api/model/MonitorResultModel.java delete mode 100644 src/main/java/org/bench4q/monitor/entity/MonitorInfo.java delete mode 100644 src/main/java/org/bench4q/monitor/service/MonitorService.java diff --git a/WindowsMonitor/Monitor/Monitor.cpp b/WindowsMonitor/Monitor/Monitor.cpp index e8bfd027..5830b86f 100644 --- a/WindowsMonitor/Monitor/Monitor.cpp +++ b/WindowsMonitor/Monitor/Monitor.cpp @@ -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; } diff --git a/WindowsMonitor/Monitor/Monitor.h b/WindowsMonitor/Monitor/Monitor.h index e5e86c65..f4d274a3 100644 --- a/WindowsMonitor/Monitor/Monitor.h +++ b/WindowsMonitor/Monitor/Monitor.h @@ -4,4 +4,4 @@ #define MONITOR_API __declspec(dllimport) #endif -MONITOR_API int Add(int a,int b); +MONITOR_API double GetCpuUsage(int sleepTime); diff --git a/WindowsMonitor/Monitor/Monitor.vcxproj b/WindowsMonitor/Monitor/Monitor.vcxproj index 69ab2a92..97375a5c 100644 --- a/WindowsMonitor/Monitor/Monitor.vcxproj +++ b/WindowsMonitor/Monitor/Monitor.vcxproj @@ -413,7 +413,6 @@ - diff --git a/WindowsMonitor/Monitor/Monitor.vcxproj.filters b/WindowsMonitor/Monitor/Monitor.vcxproj.filters index 74c022ca..d811b3b5 100644 --- a/WindowsMonitor/Monitor/Monitor.vcxproj.filters +++ b/WindowsMonitor/Monitor/Monitor.vcxproj.filters @@ -18,9 +18,6 @@ 澶存枃浠 - - 澶存枃浠 - 澶存枃浠 diff --git a/WindowsMonitor/Monitor/dllmain.cpp b/WindowsMonitor/Monitor/dllmain.cpp index 260abc6d..41f3f931 100644 --- a/WindowsMonitor/Monitor/dllmain.cpp +++ b/WindowsMonitor/Monitor/dllmain.cpp @@ -1,4 +1,3 @@ -// dllmain.cpp : 定义 DLL 应用程序的入口点。 #include "stdafx.h" BOOL APIENTRY DllMain( HMODULE hModule, diff --git a/WindowsMonitor/Monitor/stdafx.cpp b/WindowsMonitor/Monitor/stdafx.cpp index 0ceb0f50..fd4f341c 100644 --- a/WindowsMonitor/Monitor/stdafx.cpp +++ b/WindowsMonitor/Monitor/stdafx.cpp @@ -1,8 +1 @@ -// stdafx.cpp : 只包括标准包含文件的源文件 -// Monitor.pch 将作为预编译头 -// stdafx.obj 将包含预编译类型信息 - #include "stdafx.h" - -// TODO: 在 STDAFX.H 中 -// 引用任何所需的附加头文件,而不是在此文件中引用 diff --git a/WindowsMonitor/Monitor/stdafx.h b/WindowsMonitor/Monitor/stdafx.h index dea91e7d..9b39e403 100644 --- a/WindowsMonitor/Monitor/stdafx.h +++ b/WindowsMonitor/Monitor/stdafx.h @@ -1,16 +1,6 @@ -// stdafx.h : 标准系统包含文件的包含文件, -// 或是经常使用但不常更改的 -// 特定于项目的包含文件 -// - #pragma once -#include "targetver.h" - -#define WIN32_LEAN_AND_MEAN // 从 Windows 头文件中排除极少使用的信息 -// Windows 头文件: -#include - - - -// TODO: 在此处引用程序需要的其他头文件 +#define WIN32_LEAN_AND_MEAN +#include +#include +#pragma comment(lib, "Pdh.lib") \ No newline at end of file diff --git a/WindowsMonitor/Monitor/targetver.h b/WindowsMonitor/Monitor/targetver.h deleted file mode 100644 index 7a7d2c83..00000000 --- a/WindowsMonitor/Monitor/targetver.h +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once - -// 包括 SDKDDKVer.h 将定义可用的最高版本的 Windows 平台。 - -// 如果要为以前的 Windows 平台生成应用程序,请包括 WinSDKVer.h,并将 -// WIN32_WINNT 宏设置为要支持的平台,然后再包括 SDKDDKVer.h。 - -#include diff --git a/WindowsMonitor/Native/JNITest.h b/WindowsMonitor/Native/JNITest.h deleted file mode 100644 index 0b575cb1..00000000 --- a/WindowsMonitor/Native/JNITest.h +++ /dev/null @@ -1,21 +0,0 @@ -/* DO NOT EDIT THIS FILE - it is machine generated */ -#include -/* 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 diff --git a/WindowsMonitor/Native/Monitor.h b/WindowsMonitor/Native/Monitor.h index e5e86c65..7a6e3e9a 100644 --- a/WindowsMonitor/Native/Monitor.h +++ b/WindowsMonitor/Native/Monitor.h @@ -4,4 +4,4 @@ #define MONITOR_API __declspec(dllimport) #endif -MONITOR_API int Add(int a,int b); +MONITOR_API double GetCpuUsage(int sleepTime); \ No newline at end of file diff --git a/WindowsMonitor/Native/Native.cpp b/WindowsMonitor/Native/Native.cpp index dd029e29..29242358 100644 --- a/WindowsMonitor/Native/Native.cpp +++ b/WindowsMonitor/Native/Native.cpp @@ -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); } \ No newline at end of file diff --git a/WindowsMonitor/Native/Native.h b/WindowsMonitor/Native/Native.h index e39f0e74..eca9cb17 100644 --- a/WindowsMonitor/Native/Native.h +++ b/WindowsMonitor/Native/Native.h @@ -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 \ No newline at end of file diff --git a/WindowsMonitor/Native/Native.vcxproj b/WindowsMonitor/Native/Native.vcxproj index 4465f718..3c7f3985 100644 --- a/WindowsMonitor/Native/Native.vcxproj +++ b/WindowsMonitor/Native/Native.vcxproj @@ -305,6 +305,7 @@ true WIN32;NDEBUG;_WINDOWS;_USRDLL;NATIVE_EXPORTS;%(PreprocessorDefinitions) true + $(JAVA_HOME)/include/win32;$(JAVA_HOME)/include;%(AdditionalIncludeDirectories) Windows @@ -417,11 +418,10 @@ - + - diff --git a/WindowsMonitor/Native/Native.vcxproj.filters b/WindowsMonitor/Native/Native.vcxproj.filters index 64a6738e..89e0b810 100644 --- a/WindowsMonitor/Native/Native.vcxproj.filters +++ b/WindowsMonitor/Native/Native.vcxproj.filters @@ -18,16 +18,13 @@ 澶存枃浠 - - 澶存枃浠 - 澶存枃浠 - + 澶存枃浠 - + 澶存枃浠 diff --git a/WindowsMonitor/Native/WindowsCpuProbe.h b/WindowsMonitor/Native/WindowsCpuProbe.h new file mode 100644 index 00000000..a2c8a0ff --- /dev/null +++ b/WindowsMonitor/Native/WindowsCpuProbe.h @@ -0,0 +1,21 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* 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 diff --git a/WindowsMonitor/Native/stdafx.h b/WindowsMonitor/Native/stdafx.h index 4d187e68..04b08455 100644 --- a/WindowsMonitor/Native/stdafx.h +++ b/WindowsMonitor/Native/stdafx.h @@ -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 #include -#include "JNITest.h" \ No newline at end of file +#include "WindowsCpuProbe.h" \ No newline at end of file diff --git a/WindowsMonitor/Native/targetver.h b/WindowsMonitor/Native/targetver.h deleted file mode 100644 index 7a7d2c83..00000000 --- a/WindowsMonitor/Native/targetver.h +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once - -// 包括 SDKDDKVer.h 将定义可用的最高版本的 Windows 平台。 - -// 如果要为以前的 Windows 平台生成应用程序,请包括 WinSDKVer.h,并将 -// WIN32_WINNT 宏设置为要支持的平台,然后再包括 SDKDDKVer.h。 - -#include diff --git a/WindowsMonitor/Test/Monitor.h b/WindowsMonitor/Test/Monitor.h new file mode 100644 index 00000000..7a6e3e9a --- /dev/null +++ b/WindowsMonitor/Test/Monitor.h @@ -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); \ No newline at end of file diff --git a/WindowsMonitor/Test/Test.cpp b/WindowsMonitor/Test/Test.cpp new file mode 100644 index 00000000..7319a2f8 --- /dev/null +++ b/WindowsMonitor/Test/Test.cpp @@ -0,0 +1,10 @@ +#include "stdafx.h" + + +int _tmain(int argc, _TCHAR* argv[]) +{ + printf("%lf",GetCpuUsage(100)); + system("pause"); + return 0; +} + diff --git a/WindowsMonitor/Test/Test.vcxproj b/WindowsMonitor/Test/Test.vcxproj new file mode 100644 index 00000000..8fb1b26a --- /dev/null +++ b/WindowsMonitor/Test/Test.vcxproj @@ -0,0 +1,157 @@ +锘 + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {50CE9AFE-3102-4568-8107-2E3D77F2FC22} + Win32Proj + Test + + + + Application + true + v110 + Unicode + + + Application + true + v110 + Unicode + + + Application + false + v110 + true + Unicode + + + Application + false + v110 + true + Unicode + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + Use + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Use + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + Use + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + Use + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + Create + Create + Create + Create + + + + + + + \ No newline at end of file diff --git a/WindowsMonitor/Test/Test.vcxproj.filters b/WindowsMonitor/Test/Test.vcxproj.filters new file mode 100644 index 00000000..54e14ec6 --- /dev/null +++ b/WindowsMonitor/Test/Test.vcxproj.filters @@ -0,0 +1,33 @@ +锘 + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 澶存枃浠 + + + 澶存枃浠 + + + + + 婧愭枃浠 + + + 婧愭枃浠 + + + \ No newline at end of file diff --git a/WindowsMonitor/Test/stdafx.cpp b/WindowsMonitor/Test/stdafx.cpp new file mode 100644 index 00000000..fd4f341c --- /dev/null +++ b/WindowsMonitor/Test/stdafx.cpp @@ -0,0 +1 @@ +#include "stdafx.h" diff --git a/WindowsMonitor/Test/stdafx.h b/WindowsMonitor/Test/stdafx.h new file mode 100644 index 00000000..1d43c802 --- /dev/null +++ b/WindowsMonitor/Test/stdafx.h @@ -0,0 +1,9 @@ +#pragma once + +#pragma comment(lib,"../x64/Debug/Monitor.lib") + +#include +#include +#include "Monitor.h" +using namespace std; + diff --git a/WindowsMonitor/WindowsMonitor.sln b/WindowsMonitor/WindowsMonitor.sln index ad89aa0c..753204c6 100644 --- a/WindowsMonitor/WindowsMonitor.sln +++ b/WindowsMonitor/WindowsMonitor.sln @@ -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 diff --git a/descriptor.xml b/descriptor.xml index e1d56b28..23c09a45 100644 --- a/descriptor.xml +++ b/descriptor.xml @@ -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"> - dir + publish - dir + tar.gz false @@ -21,5 +21,13 @@ target/bench4q-monitor.jar / + + WindowsMonitor/x64/Release/Monitor.dll + /lib + + + WindowsMonitor/x64/Release/Native.dll + /lib + \ No newline at end of file diff --git a/src/main/java/org/bench4q/monitor/JNITest.java b/src/main/java/org/bench4q/monitor/JNITest.java deleted file mode 100644 index 4e465d86..00000000 --- a/src/main/java/org/bench4q/monitor/JNITest.java +++ /dev/null @@ -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)); - } -} diff --git a/src/main/java/org/bench4q/monitor/Main.java b/src/main/java/org/bench4q/monitor/Main.java index 6904898f..1fe259bc 100644 --- a/src/main/java/org/bench4q/monitor/Main.java +++ b/src/main/java/org/bench4q/monitor/Main.java @@ -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()); } } diff --git a/src/main/java/org/bench4q/monitor/api/MonitorController.java b/src/main/java/org/bench4q/monitor/api/MonitorController.java index 3be18918..a7851c91 100644 --- a/src/main/java/org/bench4q/monitor/api/MonitorController.java +++ b/src/main/java/org/bench4q/monitor/api/MonitorController.java @@ -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!"; } } diff --git a/src/main/java/org/bench4q/monitor/api/model/MonitorResultModel.java b/src/main/java/org/bench4q/monitor/api/model/MonitorResultModel.java deleted file mode 100644 index d12e449b..00000000 --- a/src/main/java/org/bench4q/monitor/api/model/MonitorResultModel.java +++ /dev/null @@ -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; - } -} diff --git a/src/main/java/org/bench4q/monitor/entity/MonitorInfo.java b/src/main/java/org/bench4q/monitor/entity/MonitorInfo.java deleted file mode 100644 index 4e61a4f4..00000000 --- a/src/main/java/org/bench4q/monitor/entity/MonitorInfo.java +++ /dev/null @@ -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; - } - -} diff --git a/src/main/java/org/bench4q/monitor/probe/windows/WindowsCpuProbe.java b/src/main/java/org/bench4q/monitor/probe/windows/WindowsCpuProbe.java index f4072a9d..235576c2 100644 --- a/src/main/java/org/bench4q/monitor/probe/windows/WindowsCpuProbe.java +++ b/src/main/java/org/bench4q/monitor/probe/windows/WindowsCpuProbe.java @@ -1,5 +1,5 @@ package org.bench4q.monitor.probe.windows; public class WindowsCpuProbe { - // /proc/stat + public native double getCpuUsage(); } \ No newline at end of file diff --git a/src/main/java/org/bench4q/monitor/service/MonitorService.java b/src/main/java/org/bench4q/monitor/service/MonitorService.java deleted file mode 100644 index c84fafdb..00000000 --- a/src/main/java/org/bench4q/monitor/service/MonitorService.java +++ /dev/null @@ -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(); - } - } - } -}