2022年测量Java应用程序的CPU和内存占用率 .pdf
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《2022年测量Java应用程序的CPU和内存占用率 .pdf》由会员分享,可在线阅读,更多相关《2022年测量Java应用程序的CPU和内存占用率 .pdf(16页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、JNI里有两个特殊的函数JNI_OnLoad和 JNI_OnUnload,它们分别在加载和卸载库的时候被调用。 JNI_OnLoad 在调用其他任何方法之前被执行,而且能够很方便地用于初始化在这一进程的生命周期中没有发生变化的变量,并用于协调JNI规范的版本。在默认情况下,库会测量它自己的进程的参数,但是通过调用systemInformation.setPid ()方法它可以从Java应用程序被重载。s_PID C变量用来保存PID,而 s_currentProcess 用来保存进程句柄(用于Windows 的是 HANDLE类型,而用于Solaris 的是 int 类型) 。为了读取的一些参
2、数,应该首先打开进程句柄,我们需要在库关闭使用的时候停止同一个进程句柄(通常它在JVM 因为相同的原因而关闭的时候发生)。这就是JNI_OnUnload()函数起作用的地方。但是,JVM的一些实现事实上没有调用JNI_OnUnload() ,还有发生句柄会永远打开的危险。为了降低这种可能性,我们应该在Java应用程序里加入一个明确调用detachProcess()C 函数的关闭挂钩。下面就是我们加入关闭挂钩的方法:if (pid!=-1) int result = SystemInformation.setPid(pid); if (result!=0) return; hasPid = tr
3、ue; / Create shutdown hook for proper process detach Runtime.getRuntime().addShutdownHook(new Thread() public void run() SystemInformation.detachProcess(); ); 通过调用WinAPI 里的 GetSystemInfo() ,我们还可以获得关于中央处理器的一些信息。只要它是CPU 的占用率根据这个值来调节,测量进程最重要的值就是处理器的个数(s_numberOfProcessors) 。SystemInformation.getSysInfo
4、 ()的Win32 实现相当麻烦,因为在各个版本的Windows 里,关于操作系统的版本、补丁、服务包以及相关硬件等信息被以不同的方式保存。 所以需要读者来分析相关的源代码和代码中的注释。下面就是 Windows XP上代码输出的示例:System.out.println(SysInfo: ” +SystemInformation.getSysInfo():SysInfo: WinXP Professional Service Pack 1 (Build 2600), on DBMOSWS2132 (Intel(R) Xeon(TM) CPU 1.70GHz) And the same cod
5、e on Solaris will give: SysInfo: SunOS 5.8 sxav-dev Generic_108528-29 sun4u sparc SUNW,Ultra-Enterprise Sun_Microsystems 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 16 页 - - - - - - - - - 为了获得CPU 上进程所占用的总时间,我们使用了WinAPI 的函数GetProcessTimes.其他的函数实现都非常简单,直接调用Win
6、API,所以没有什么必要讨论它们。列表D 里是用于Windows 版本的 GCC的 make.bat 文件,用来帮助读者创建相关的。dll 库。列表 D gcc -D_JNI_IMPLEMENTATION_ -Wl , kill-at -IC : /jdk1.3.1_12/include -IC :/jdk1.3.1_12/include/win32 -shared C:/cpu_profile/src/native/com_vladium_utils_SystemInformation.c -o C: /cpu_profile/dll/silib.dll C:/MinGW/lib/libps
7、api.a 这个库的Solaris 实现见列表E和列表 F.这两个都是C 语言文件, 应该被编译到一个共享库( .so)里。用于编译共享库的帮助器make.sh 见列表 G.所有基于Solaris系统的调用被移到列表F里,这使得列表E就是一个JNI的简单包装程序。Solaris 实现要比Win32 实现更加复杂,要求更多的临时数据结构、内核和进程表。列出的代码里有更多的注释。列表 E /* - */ /* * An implementation of JNI methods in com.vladium.utils.SystemInformation * class. * This is a
8、ported version from Win32 to Solaris. * * For simplicity, this implementaion assumes JNI 1.2+ and omits error handling. * * Port from Win32 by Peter V. Mikhalenko (C) 2004, Deutsche Bank petermikhalenko.ru * Original source (C) 2002, Vladimir Roubtsov */ /* - */ #include #include com_vladium_utils_S
9、ystemInformation.h / Helper Solaris8-dependent external routines extern int sol_getCPUs(); extern int sol_getFreeMem(); extern int sol_getMaxMem(); extern long int sol_getProcessCPUTime(int pid,int nproc); extern double sol_getProcessCPUPercentage(int pid); extern long sol_getMemoryUsage(int pid); e
10、xtern long sol_getMemoryResident(int pid); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 16 页 - - - - - - - - - extern char* sol_getSysInfo(); extern void initKVM(); static jint s_PID; static int s_numberOfProcessors; static int externalCPUmon; static int alre
11、adyDetached; /* - */ /* . */ /* * This method was added in JNI 1.2. It is executed once before any other * methods are called and is ostensibly for negotiating JNI spec versions, but * can also be conveniently used for initializing variables that will not * change throughout the lifetime of this pro
12、cess. */ JNIEXPORT jint JNICALL JNI_OnLoad (JavaVM * vm, void * reserved) s_PID = _getpid (); externalCPUmon = 0; alreadyDetached = 0; /* use kstat to update all processor information */ s_numberOfProcessors = sol_getCPUs(); initKVM(); return JNI_VERSION_1_2; /* . */ /* * Class: com_vladium_utils_Sy
13、stemInformation * Method: getCPUs * Signature: ()I */ JNIEXPORT jint JNICALL Java_com_vladium_utils_SystemInformation_getCPUs (JNIEnv * env, jclass cls) return (jint)s_numberOfProcessors; /* . */ 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 16 页 - - - - - - -
14、 - - /* * Class: com_vladium_utils_SystemInformation * Method: getSysInfo * Signature: ()S */ JNIEXPORT jstring JNICALL Java_com_vladium_utils_SystemInformation_getSysInfo (JNIEnv * env, jclass cls) char * buf = sol_getSysInfo(); jstring retval = (*env)-NewStringUTF(env,buf); free(buf); return retva
15、l; /* . */ /* * Class: com_vladium_utils_SystemInformation * Method: getProcessID * Signature: ()I */ JNIEXPORT jint JNICALL Java_com_vladium_utils_SystemInformation_getProcessID (JNIEnv * env, jclass cls) return s_PID; /* . */ /* * Class: com_vladium_utils_SystemInformation * Method: setPid * Signa
16、ture: ()I */ JNIEXPORT jint JNICALL Java_com_vladium_utils_SystemInformation_setPid (JNIEnv * env, jclass cls, jint pid) s_PID = pid; externalCPUmon = 1; printf(CPUmon Attached to process. ); fflush(stdout); return 0; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4
17、页,共 16 页 - - - - - - - - - /* . */ /* * Class: com_vladium_utils_SystemInformation * Method: detachProcess * Signature: ()I */ JNIEXPORT jint JNICALL Java_com_vladium_utils_SystemInformation_detachProcess (JNIEnv * env, jclass cls) if (externalCPUmon & !alreadyDetached) alreadyDetached = 1; printf(C
18、PUmon Detached from process. ); fflush(stdout); return 0; /* . */ /* * Class: com_vladium_utils_SystemInformation * Method: getProcessCPUTime * Signature: ()J */ JNIEXPORT jlong JNICALL Java_com_vladium_utils_SystemInformation_getProcessCPUTime (JNIEnv * env, jclass cls) return (jlong)sol_getProcess
19、CPUTime(int)s_PID,s_numberOfProcessors); /* . */ /* * Class: com_vladium_utils_SystemInformation * Method: getMaxMem * Signature: ()J */ JNIEXPORT jlong JNICALL Java_com_vladium_utils_SystemInformation_getMaxMem (JNIEnv * env, jclass cls) return (jlong)sol_getMaxMem(); /* . */ 名师资料总结 - - -精品资料欢迎下载 -
20、 - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 16 页 - - - - - - - - - /* * Class: com_vladium_utils_SystemInformation * Method: getFreeMem * Signature: ()J */ JNIEXPORT jlong JNICALL Java_com_vladium_utils_SystemInformation_getFreeMem (JNIEnv * env, jclass cls) return (jlong)sol_get
21、FreeMem(); /* . */ /* define min elapsed time (in units of 10E-7 sec): */ #define MIN_ELAPSED_TIME (10000) /* * Class: com_vladium_utils_SystemInformation * Method: getProcessCPUUsage * Signature: ()D */ JNIEXPORT jdouble JNICALL Java_com_vladium_utils_SystemInformation_getProcessCPUUsage (JNIEnv *
22、env, jclass cls) return 0.0; /* . */ /* * Class: com_vladium_utils_SystemInformation * Method: getProcessCPUPercentage * Signature: ()D */ JNIEXPORT jdouble JNICALL Java_com_vladium_utils_SystemInformation_getProcessCPUPercentage (JNIEnv * env, jclass cls) return (jdouble)sol_getProcessCPUPercentage
23、(int)s_PID); /* . */ /* * Class: com_vladium_utils_SystemInformation * Method: getMemoryUsage 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 16 页 - - - - - - - - - * Signature: ()J */ JNIEXPORT jlong JNICALL Java_com_vladium_utils_SystemInformation_getMemoryUsa
24、ge (JNIEnv * env, jclass cls) return (jlong)sol_getMemoryUsage(int)s_PID); /* . */ /* * Class: com_vladium_utils_SystemInformation * Method: getMemoryResident * Signature: ()J */ JNIEXPORT jlong JNICALL Java_com_vladium_utils_SystemInformation_getMemoryResident (JNIEnv * env, jclass cls) return (jlo
25、ng)sol_getMemoryResident(int)s_PID); #undef MIN_ELAPSED_TIME /* - */ /* end of file */ 列表 F/* - */ /* * Solaris-dependent system routines and kernel calls * Used for getting memory and CPU consumption statistics * * Author: Peter V. Mikhalenko (C) 2004, Deutsche Bank petermikhalenko.ru */ /* - */ #i
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年测量Java应用程序的CPU和内存占用率 2022 测量 Java 应用程序 CPU 内存 占用率
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内