測(cè)量Java應(yīng)用程序的CPU和內(nèi)存占用率 3_第1頁(yè)
測(cè)量Java應(yīng)用程序的CPU和內(nèi)存占用率 3_第2頁(yè)
測(cè)量Java應(yīng)用程序的CPU和內(nèi)存占用率 3_第3頁(yè)
測(cè)量Java應(yīng)用程序的CPU和內(nèi)存占用率 3_第4頁(yè)
測(cè)量Java應(yīng)用程序的CPU和內(nèi)存占用率 3_第5頁(yè)
已閱讀5頁(yè),還剩25頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、JNI 里有兩個(gè)特殊的函數(shù) JNI_OnLoad和 JNI_OnUnload,它們分別在加載和卸載庫(kù)的時(shí)候 被調(diào)用。 JNI_OnLoad在調(diào)用其他任何方法之前被執(zhí)行,而且能夠很方便地用于初始化在這 一進(jìn)程的生命周期中沒(méi)有發(fā)生變化的變量,并用于協(xié)調(diào) JNI 規(guī)范的版本。在默認(rèn)情況下,庫(kù) 會(huì)測(cè)量它自己的進(jìn)程的參數(shù),但是通過(guò)調(diào)用 systemInformation.setPid (方法它可以從 Java 應(yīng)用程序被重載。 s_PID C 變量用來(lái)保存 PID ,而 s_currentProcess用來(lái)保存進(jìn)程句柄(用于 Windows 的是 HANDLE 類(lèi)型,而用于 Solaris 的是 int

2、 類(lèi)型 。為了讀取的一些參數(shù),應(yīng)該首 先打開(kāi)進(jìn)程句柄,我們需要在庫(kù)關(guān)閉使用的時(shí)候停止同一個(gè)進(jìn)程句柄(通常它在 JVM 因?yàn)?相同的原因而關(guān)閉的時(shí)候發(fā)生 。這就是 JNI_OnUnload(函數(shù)起作用的地方。但是, JVM 的一些實(shí)現(xiàn)事實(shí)上沒(méi)有調(diào)用 JNI_OnUnload( ,還有發(fā)生句柄會(huì)永遠(yuǎn)打開(kāi)的危險(xiǎn)。為了降低 這種可能性,我們應(yīng)該在 Java 應(yīng)用程序里加入一個(gè)明確調(diào)用 detachProcess ( C 函數(shù)的關(guān) 閉掛鉤。下面就是我們加入關(guān)閉掛鉤的方法:if (pid!=-1 int result = SystemInformation.setPid(pid;if (result!=0

3、 return;hasPid = true;/ Create shutdown hook for proper process detachRuntime.getRuntime(.addShutdownHook(new Thread( public void run( SystemInformation.detachProcess(;通過(guò)調(diào)用 WinAPI 里的 GetSystemInfo ( ,我們還可以獲得關(guān)于中央處理器的一些信息。只 要它是 CPU 的占用率根據(jù)這個(gè)值來(lái)調(diào)節(jié),測(cè)量進(jìn)程最重要的值就是處理器的個(gè)數(shù) (s_numberOfProcessors 。 SystemInformati

4、on.getSysInfo (的 Win32實(shí)現(xiàn)相當(dāng)麻煩,因?yàn)?在各個(gè)版本的 Windows 里,關(guān)于操作系統(tǒng)的版本、補(bǔ)丁、服務(wù)包以及相關(guān)硬件等信息被以 不同的方式保存。 所以需要讀者來(lái)分析相關(guān)的源代碼和代碼中的注釋。 下面就是 Windows XP上代碼輸出的示例:System.out.println("SysInfo: ”+SystemInformation.getSysInfo(:SysInfo: WinXP Professional Service Pack 1 (Build 2600,on DBMOSWS2132 (Intel(R Xeon(TM CPU 1.70GHzAn

5、d the same code on Solaris will give:SysInfo: SunOS 5.8 sxav-dev Generic_108528-29 sun4u sparcSUNW,Ultra-Enterprise Sun_Microsystems為了獲得 CPU 上進(jìn)程所占用的總時(shí)間,我們使用了 WinAPI 的函數(shù) GetProcessTimes. 其他的 函數(shù)實(shí)現(xiàn)都非常簡(jiǎn)單,直接調(diào)用 WinAPI ,所以沒(méi)有什么必要討論它們。列表 D 里是用于 Windows 版本的 GCC 的 make.bat 文件,用來(lái)幫助讀者創(chuàng)建相關(guān)的。 dll 庫(kù)。列表 Dgcc -D_JNI_

6、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/libpsapi.a 這個(gè)庫(kù)的 Solaris 實(shí)現(xiàn)見(jiàn)列表 E 和列表 F. 這兩個(gè)都是 C 語(yǔ)言文件, 應(yīng)該 被編譯到一個(gè)共享庫(kù) (.so 里。 用于編譯共享庫(kù)的幫助器 make.

7、sh 見(jiàn)列表 G. 所有基于 Solaris 系統(tǒng)的調(diào)用被移到列表 F 里,這使得列表 E 就是一個(gè) JNI 的簡(jiǎn)單包裝程序。 Solaris 實(shí)現(xiàn)要比 Win32實(shí)現(xiàn)更加復(fù)雜, 要求更多的臨時(shí)數(shù)據(jù)結(jié)構(gòu)、 內(nèi)核和進(jìn)程表。列出的代碼里有更多的注 釋。列表 E/* - */* An implementation of JNI methods in com.vladium.utils.SystemInformation* class.* This is a ported version from Win32 to Solaris.* For simplicity, this implementaio

8、n 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 vlad*/* - */#include#include "com_vladium_utils_SystemInformation.h"/ Helper Solaris8-dependent external routinesexter

9、n 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;extern long sol_getMemoryResident(int pid;extern char* sol_getSysInfo(;extern void

10、initKVM(;static jint s_PID;static int s_numberOfProcessors;static int externalCPUmon;static int alreadyDetached;/* - */* . */* 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 convenient

11、ly used for initializing variables that will not* change throughout the lifetime of this process.*/JNIEXPORT jint JNICALLJNI_OnLoad (JavaVM * vm, void * reserveds_PID = _getpid (;externalCPUmon = 0;alreadyDetached = 0;/* use kstat to update all processor information */s_numberOfProcessors = sol_getC

12、PUs(;initKVM(;return JNI_VERSION_1_2;/* . */* Class: com_vladium_utils_SystemInformation* Method: getCPUs* Signature: (I*/JNIEXPORT jint JNICALLJava_com_vladium_utils_SystemInformation_getCPUs (JNIEnv * env, jclass cls return (jints_numberOfProcessors;/* . */* Class: com_vladium_utils_SystemInformat

13、ion* Method: getSysInfo* Signature: (S*/JNIEXPORT jstring JNICALLJava_com_vladium_utils_SystemInformation_getSysInfo (JNIEnv * env, jclass cls char * buf = sol_getSysInfo(;jstring retval = (*env->NewStringUTF(env,buf;free(buf;return retval;/* . */* Class: com_vladium_utils_SystemInformation* Meth

14、od: getProcessID* Signature: (I*/JNIEXPORT jint JNICALLJava_com_vladium_utils_SystemInformation_getProcessID (JNIEnv * env, jclass cls return s_PID;/* . */* Class: com_vladium_utils_SystemInformation* Method: setPid* Signature: (I*/JNIEXPORT jint JNICALLJava_com_vladium_utils_SystemInformation_setPi

15、d (JNIEnv * env, jclass cls, jint pid s_PID = pid;externalCPUmon = 1;printf("CPUmon Attached to process."fflush(stdout;return 0;/* . */* Class: com_vladium_utils_SystemInformation* Method: detachProcess* Signature: (I*/JNIEXPORT jint JNICALLJava_com_vladium_utils_SystemInformation_detachPr

16、ocess (JNIEnv * env, jclass cls if (externalCPUmon && !alreadyDetached alreadyDetached = 1;printf("CPUmon Detached from process."fflush(stdout;return 0;/* . */* Class: com_vladium_utils_SystemInformation* Method: getProcessCPUTime* Signature: (J*/JNIEXPORT jlong JNICALLJava_com_vla

17、dium_utils_SystemInformation_getProcessCPUTime (JNIEnv * env, jclass cls return (jlongsol_getProcessCPUTime(ints_PID,s_numberOfProcessors;/* . */* Class: com_vladium_utils_SystemInformation* Method: getMaxMem* Signature: (J*/JNIEXPORT jlong JNICALLJava_com_vladium_utils_SystemInformation_getMaxMem (

18、JNIEnv * env, jclass cls return (jlongsol_getMaxMem(;/* . */* Class: com_vladium_utils_SystemInformation* Method: getFreeMem* Signature: (J*/JNIEXPORT jlong JNICALLJava_com_vladium_utils_SystemInformation_getFreeMem (JNIEnv * env, jclass clsreturn (jlongsol_getFreeMem(;/* . */* define min elapsed ti

19、me (in units of 10E-7 sec: */#define MIN_ELAPSED_TIME (10000/* Class: com_vladium_utils_SystemInformation* Method: getProcessCPUUsage* Signature: (D*/JNIEXPORT jdouble JNICALLJava_com_vladium_utils_SystemInformation_getProcessCPUUsage (JNIEnv * env, jclass cls return 0.0;/* . */* Class: com_vladium_

20、utils_SystemInformation* Method: getProcessCPUPercentage* Signature: (D*/JNIEXPORT jdouble JNICALLJava_com_vladium_utils_SystemInformation_getProcessCPUPercentage (JNIEnv * env, jclass cls return (jdoublesol_getProcessCPUPercentage(ints_PID;/* . */* Class: com_vladium_utils_SystemInformation* Method

21、: getMemoryUsage* Signature: (J*/JNIEXPORT jlong JNICALLJava_com_vladium_utils_SystemInformation_getMemoryUsage (JNIEnv * env, jclass cls return (jlongsol_getMemoryUsage(ints_PID;/* . */* Class: com_vladium_utils_SystemInformation* Method: getMemoryResident* Signature: (J*/JNIEXPORT jlong JNICALLJav

22、a_com_vladium_utils_SystemInformation_getMemoryResident (JNIEnv * env, jclass cls return (jlongsol_getMemoryResident(ints_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:

23、 Peter V. Mikhalenko (C 2004, Deutsche Bank petermikhalenko.ru*/* - */#include# ifndef KSTAT_DATA_UINT32# define ui32 ul# endif#include#include#include#include#include#include#include#include#define _STRUCTURED_PROC 1#include#define prpsinfo psinfo#define pr_fill pr_nlwp/* These require an ANSI C co

24、mpiler "Reisser cpp" doesn't like this */ #define pr_state pr_lwp.pr_state#define pr_oldpri pr_lwp.pr_oldpri#define pr_nice pr_lwp.pr_nice#define pr_pri pr_lwp.pr_pri#define pr_onpro pr_lwp.pr_onpro#define ZOMBIE(p (p->pr_nlwp = 0#define SIZE_K(p (p->pr_size#define RSS_K(p (p->

25、;pr_rssize#define PROCFS "/proc"/* definitions for indices in the nlist array */#define X_V 0#define X_MPID 1#define X_ANONINFO 2#define X_MAXMEM 3#define X_SWAPFS_MINFREE 4#define X_FREEMEM 5#define X_AVAILRMEM 6#define X_AVENRUN 7#define X_CPU 8#define X_NPROC 9#define X_NCPUS 10static s

26、truct nlist nlst =, /* 0 */ /* replaced by dynamic allocation */, /* 1 */#if OSREV >= 56/* this structure really has some extra fields, but the first three match */ , /* 2 */#else, /* 2 */#endif, /* 3 */ /* use sysconf */, /* 4 */ /* used only w/ USE_ANONINFO */, /* 5 */ /* available from kstat &

27、gt;= 2.5 */, /* 6 */ /* available from kstat >= 2.5 */, /* 7 */ /* available from kstat */, /* 8 */ /* available from kstat */, /* 9 */ /* available from kstat */, /* 10 */ /* available from kstat */;static kstat_ctl_t *kc = NULL;static kstat_t *cpu_ks;static cpu_stat_t *cpu_stat;static int ncpus

28、;kvm_t *kd;static unsigned long freemem_offset;static unsigned long maxmem_offset;static unsigned long freemem = -1L;static unsigned long maxmem = -1L;/* pagetok function is really a pointer to an appropriate function */ static int pageshift;static int (*p_pagetok (;#define pagetok(size (*p_pagetok(

29、sizeint pagetok_none(int size return(size;int pagetok_left(int size return(size << pageshift;int pagetok_right(int size return(size >> pageshift;#define UPDKCID(nk,okif (nk = -1 perror("kstat_read "exit(1;if (nk != okgoto kcid_changed;void initKVM( int i;/* perform the kvm_open

30、 - suppress error here */kd = kvm_open (NULL, NULL, NULL, O_RDONLY, NULL;/* calculate pageshift value */i = sysconf(_SC_PAGESIZE;pageshift = 0;while (i >>= 1 > 0pageshift+;/* calculate an amount to shift to K values */* remember that log base 2 of 1024 is 10 (i.e.: 210 = 1024 */ pageshift -

31、= 10;/* now determine which pageshift function is appropriate for the result (have to because x << y is undefined for y < 0 */ if (pageshift > 0/* this is the most likely */p_pagetok = pagetok_left;else if (pageshift = 0p_pagetok = pagetok_none;elsep_pagetok = pagetok_right;pageshift = -

32、pageshift;#define SI_LEN 512#define BUFSIZE 256char * sol_getSysInfo( char * retbuf = (char*malloc(SI_LEN;int curend = 0;int maxl = SI_LEN;*retbuf=0;char * buf = (char*malloc(BUFSIZE; long res = sysinfo(SI_SYSNAME,buf,BUFSIZE; if (res>0 && res<=maxl strcat(retbuf,buf;curend=res-1;maxl=

33、SI_LEN-curend;if (curend0 && res<=maxl strcat(retbuf,buf;curend+=res-1;maxl=SI_LEN-curend;if (curend0 && res<=maxl strcat(retbuf,buf;curend+=res-1;maxl=SI_LEN-curend;if (curend0 && res<=maxl strcat(retbuf,buf; curend+=res-1;maxl=SI_LEN-curend; if (curend0 &&

34、res<=maxl strcat(retbuf,buf; curend+=res-1;maxl=SI_LEN-curend; if (curend0 && res<=maxl strcat(retbuf,buf; curend+=res-1;maxl=SI_LEN-curend; if (curend0 && res<=maxl strcat(retbuf,buf; curend+=res-1;maxl=SI_LEN-curend; if (curend0 && res<=maxl strcat(retbuf,buf; c

35、urend+=res-1;maxl=SI_LEN-curend; if (curendvalue.ui32 > ncpus ncpus = kn->value.ui32;cpu_ks = (kstat_t * realloc (cpu_ks, ncpus * sizeof (kstat_t *; cpu_stat = (cpu_stat_t * realloc (cpu_stat,ncpus * sizeof (cpu_stat_t;for (ks = kc->kc_chain; ks;ks = ks->ks_nextif (strncmp(ks->ks_name

36、, "cpu_stat", 8 = 0nkcid = kstat_read(kc, ks, NULL;/* if kcid changed, pointer might be invalid */UPDKCID(nkcid, kcid;cpu_ksncpu = ks;ncpu+;if (ncpu > ncpusfprintf(stderr, "kstat finds too many cpus: should be %d ",ncpus;exit(1;/* note that ncpu could be less than ncpus, but t

37、hat's okay */ changed = 0;/* return the number of cpus found */ncpus=ncpu;return ncpu;unsigned long sol_getMaxMem( maxmem = pagetok(sysconf(_SC_PHYS_PAGES;return maxmem;unsigned long sol_getFreeMem( kstat_t *ks;kstat_named_t *kn;ks = kstat_lookup(kc, "unix", 0, "system_pages"

38、if (kstat_read(kc, ks, 0 = -1 perror("kstat_read"exit(1;if (kd != NULL /* always get freemem from kvm if we can*/(void getkval (freemem_offset, (int * (&freemem, sizeof (freemem, "freemem" else kn = kstat_data_lookup(ks, "freemem"if (knfreemem = kn->value.ul;retu

39、rn (unsigned longpagetok(freemem;/ Returns the number of milliseconds (not nanoseconds and seconds elapsed on processor / since process start. The returned value is adjusted for the number of processors in the system. long int sol_getProcessCPUTime(int pid,int nproc struct prpsinfo currproc;int fd;c

40、har buf30;long int retval=0;snprintf(buf, sizeof(buf, "%s/%d/psinfo", PROCFS, pid;if (fd = open (buf, O_RDONLY < 0 return 0L;if (read(fd, &currproc, sizeof(psinfo_t != sizeof(psinfo_t (voidclose(fd;return 0L;(voidclose(fd;retval = (currproc.pr_time.tv_sec * 1000 + currproc.pr_time.t

41、v_nsec / 1000000 / nproc;return retval;/ Returns percentage CPU by pid/ In Solaris 8 it is contained in procfsdouble sol_getProcessCPUPercentage(int pid struct prpsinfo currproc;int fd;char buf30;double retval=0.0;snprintf(buf, sizeof(buf, "%s/%d/psinfo", PROCFS, pid;if (fd = open (buf, O_

42、RDONLY < 0 return 0;if (read(fd, &currproc, sizeof(psinfo_t != sizeof(psinfo_t (voidclose(fd;return 0;(voidclose(fd;retval = (doublecurrproc.pr_pctcpu/0x8000*100;return retval;/ Returns current space allocated for the process, in bytes. Those pages may or may not be in memory.long sol_getMemo

43、ryUsage(int pid struct prpsinfo currproc;int fd;char buf30;double retval=0.0;snprintf(buf, sizeof(buf, "%s/%d/psinfo", PROCFS, pid;if (fd = open (buf, O_RDONLY < 0 return 0;if (read(fd, &currproc, sizeof(psinfo_t != sizeof(psinfo_t (voidclose(fd;return 0;(voidclose(fd;retval = currproc.pr_size;return retval;/ Returns current process space being resident in memory.long sol_getMemoryResident(int pid struct prpsinfo currproc;int fd;char buf30;double retval=0.0;snprintf(buf, sizeof(buf, "%s/%d/psinfo", PROCFS,

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論