




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領
文檔簡介
1、JNI 里有兩個特殊的函數(shù) JNI_OnLoad和 JNI_OnUnload,它們分別在加載和卸載庫的時候 被調(diào)用。 JNI_OnLoad在調(diào)用其他任何方法之前被執(zhí)行,而且能夠很方便地用于初始化在這 一進程的生命周期中沒有發(fā)生變化的變量,并用于協(xié)調(diào) JNI 規(guī)范的版本。在默認情況下,庫 會測量它自己的進程的參數(shù),但是通過調(diào)用 systemInformation.setPid (方法它可以從 Java 應用程序被重載。 s_PID C 變量用來保存 PID ,而 s_currentProcess用來保存進程句柄(用于 Windows 的是 HANDLE 類型,而用于 Solaris 的是 int
2、 類型 。為了讀取的一些參數(shù),應該首 先打開進程句柄,我們需要在庫關(guān)閉使用的時候停止同一個進程句柄(通常它在 JVM 因為 相同的原因而關(guān)閉的時候發(fā)生 。這就是 JNI_OnUnload(函數(shù)起作用的地方。但是, JVM 的一些實現(xiàn)事實上沒有調(diào)用 JNI_OnUnload( ,還有發(fā)生句柄會永遠打開的危險。為了降低 這種可能性,我們應該在 Java 應用程序里加入一個明確調(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(;通過調(diào)用 WinAPI 里的 GetSystemInfo ( ,我們還可以獲得關(guān)于中央處理器的一些信息。只 要它是 CPU 的占用率根據(jù)這個值來調(diào)節(jié),測量進程最重要的值就是處理器的個數(shù) (s_numberOfProcessors 。 SystemInformati
4、on.getSysInfo (的 Win32實現(xiàn)相當麻煩,因為 在各個版本的 Windows 里,關(guān)于操作系統(tǒng)的版本、補丁、服務包以及相關(guān)硬件等信息被以 不同的方式保存。 所以需要讀者來分析相關(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 上進程所占用的總時間,我們使用了 WinAPI 的函數(shù) GetProcessTimes. 其他的 函數(shù)實現(xiàn)都非常簡單,直接調(diào)用 WinAPI ,所以沒有什么必要討論它們。列表 D 里是用于 Windows 版本的 GCC 的 make.bat 文件,用來幫助讀者創(chuàng)建相關(guān)的。 dll 庫。列表 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 這個庫的 Solaris 實現(xiàn)見列表 E 和列表 F. 這兩個都是 C 語言文件, 應該 被編譯到一個共享庫 (.so 里。 用于編譯共享庫的幫助器 make.
7、sh 見列表 G. 所有基于 Solaris 系統(tǒng)的調(diào)用被移到列表 F 里,這使得列表 E 就是一個 JNI 的簡單包裝程序。 Solaris 實現(xiàn)要比 Win32實現(xiàn)更加復雜, 要求更多的臨時數(shù)據(jù)結(jié)構(gòu)、 內(nèi)核和進程表。列出的代碼里有更多的注 釋。列表 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. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 電子商務運營基礎指南
- 投資融資居間合同模板
- 廣告公司廣告策劃與創(chuàng)意預案
- 物聯(lián)網(wǎng)技術(shù)發(fā)展趨勢研究
- 汽車維修項目可行性研究報告
- 康養(yǎng)項目可研究性報告
- 區(qū)塊鏈技術(shù)在公共服務中的應用預案
- 休閑娛樂場所拆舊協(xié)議
- 交通建設項目可行性研究報告編制辦法
- 農(nóng)業(yè)行業(yè)農(nóng)產(chǎn)品溯源與農(nóng)技服務系統(tǒng)方案
- 新教材統(tǒng)編版高中語文古代詩歌閱讀講與練 22 從七大常見題材入手把握詩歌內(nèi)容情感
- 2024年蘇州經(jīng)貿(mào)職業(yè)技術(shù)學院高職單招(英語/數(shù)學/語文)筆試歷年參考題庫含答案解析
- 風險分級管控74411ppt課件(PPT 146頁)
- 《道德與法治》五年級下冊全冊教案
- 三八女神節(jié)活動策劃PPT課件
- Q∕GDW 12131-2021 干擾源用戶接入電網(wǎng)電能質(zhì)量評估技術(shù)規(guī)范
- 鋼軌鋁熱焊技術(shù)知識題庫(含答案)
- 第2章全站儀使用
- 電子商務B2B模式-ppt課件
- 日本_多自然河川_治理及其對我國河道整治的啟示
- 《甲方認質(zhì)認價確認單》
評論
0/150
提交評論