




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
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
3、 return;hasPid = true;/ Create shutdown hook for proper process detachRuntime.getRuntime(.addShutdownHook(new Thread( public void run( SystemInformation.detachProcess(;通過調用 WinAPI 里的 GetSystemInfo ( ,我們還可以獲得關于中央處理器的一些信息。只 要它是 CPU 的占用率根據這個值來調節,測量進程最重要的值就是處理器的個數 (s_numberOfProcessors 。 SystemInformati
4、on.getSysInfo (的 Win32實現相當麻煩,因為 在各個版本的 Windows 里,關于操作系統的版本、補丁、服務包以及相關硬件等信息被以 不同的方式保存。 所以需要讀者來分析相關的源代碼和代碼中的注釋。 下面就是 Windows XP上代碼輸出的示例:System.out.println("SysInfo: ”+SystemInformation.getSysInfo(:SysInfo: WinXP Professional Service Pack 1 (Build 2600,And the same code on Solaris will give:SysInf
5、o: SunOS 5.8 sxav-dev Generic_108528-29 sun4u sparcSUNW,Ultra-Enterprise Sun_Microsystems為了獲得 CPU 上進程所占用的總時間,我們使用了 WinAPI 的函數 GetProcessTimes. 其他的 函數實現都非常簡單,直接調用 WinAPI ,所以沒有什么必要討論它們。列表 D 里是用于 Windows 版本的 GCC 的 make.bat 文件,用來幫助讀者創建相關的。 dll 庫。列表 Dgcc -D_JNI_IMPLEMENTATION_ -Wl , kill-at -IC :/jdk1.3.
6、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 實現見列表 E 和列表 F. 這兩個都是 C 語言文件, 應該 被編譯到一個共享庫 (.so 里。 用于編譯共享庫的幫助器 make.sh 見列表 G. 所有基于 Solaris 系統的調用被移到列表 F 里,這使得列表
7、 E 就是一個 JNI 的簡單包裝程序。 Solaris 實現要比 Win32實現更加復雜, 要求更多的臨時數據結構、 內核和進程表。列出的代碼里有更多的注 釋。列表 E/* - */* class.* This is a ported version from Win32 to Solaris.* For simplicity, this implementaion assumes JNI 1.2+ and omits error handling.*/* - */#include#include "com_vladium_utils_SystemInformation.h&quo
8、t;/ Helper Solaris8-dependent external routinesextern 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_getMemoryReside
9、nt(int pid;extern char* sol_getSysInfo(;extern void 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 negotia
10、ting JNI spec versions, but * can also be conveniently 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 pro
11、cessor information */s_numberOfProcessors = sol_getCPUs(;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_numberOfProcess
12、ors;/* . */* Class: com_vladium_utils_SystemInformation* 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;/* .
13、*/* Class: com_vladium_utils_SystemInformation* Method: 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 J
14、NICALLJava_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;/* . */* Class: com_vladium_utils_SystemInformation* Method: detachProcess* Signature: (I*/JNIEXPORT jint JNIC
15、ALLJava_com_vladium_utils_SystemInformation_detachProcess (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
16、* Signature: (J*/JNIEXPORT jlong JNICALLJava_com_vladium_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 JNICALL
17、Java_com_vladium_utils_SystemInformation_getMaxMem (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
18、(jlongsol_getFreeMem(;/* . */* 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 JNICALLJava_com_vladium_utils_SystemInformation_getProcessCPUUsage (JNIEnv * env
19、, jclass cls return 0.0;/* . */* Class: com_vladium_utils_SystemInformation* Method: getProcessCPUPercentage* Signature: (D*/JNIEXPORT jdouble JNICALLJava_com_vladium_utils_SystemInformation_getProcessCPUPercentage (JNIEnv * env, jclass cls return (jdoublesol_getProcessCPUPercentage(ints_PID;/* . */
20、* Class: com_vladium_utils_SystemInformation* Method: 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: getMemor
21、yResident* Signature: (J*/JNIEXPORT jlong JNICALLJava_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 ge
22、tting memory and CPU consumption statistics*/* - */#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#define ZOMBIE(p (p->pr_nlwp = 0#define SIZE_
23、K(p (p->pr_size#define RSS_K(p (p->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#de
24、fine X_NPROC 9#define X_NCPUS 10static struct 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_ANONIN
25、FO */, /* 5 */ /* available from kstat >= 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;stat
26、ic cpu_stat_t *cpu_stat;static int ncpus;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_pag
27、etok (;#define pagetok(size (*p_pagetok(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;voi
28、d initKVM( int i;/* perform the kvm_open - 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 102
29、4 is 10 (i.e.: 210 = 1024 */ pageshift -= 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;els
30、ep_pagetok = pagetok_right;pageshift = -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<=m
31、axl 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; curend+=res-1;max
32、l=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 &
33、;& res<=maxl strcat(retbuf,buf; curend+=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 =
34、ks->ks_nextif (strncmp(ks->ks_name, "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 t
35、hat ncpu could be less than ncpus, but that'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, "
36、;unix", 0, "system_pages"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&q
37、uot;if (knfreemem = kn->value.ul;return (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
38、 nproc struct prpsinfo currproc;int fd;char 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
39、_time.tv_sec * 1000 + currproc.pr_time.tv_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&q
40、uot;, 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 = (doublecurrproc.pr_pctcpu/0x8000*100;return retval;/ Returns current space allocated for the process, in bytes. Those pages may
41、or may not be in memory.long sol_getMemoryUsage(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(
42、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, pid;if (fd = open (buf, O_RDONLY < 0 return 0;if (rea
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 六一活動進小區活動方案
- 六一童裝營銷活動方案
- 六一自然活動方案
- 六一迎接端午活動方案
- 六一飛鏢游戲活動方案
- 六年級名家節活動方案
- 醫師文職考試試題及答案
- 醫師考試試題及答案解析
- 醫生院感考試試題及答案
- 抗體試題及答案
- 穿越機的基礎知識
- 畢業設計(論文)-基于PLC智能分類垃圾桶設計
- 油田安全生產管理培訓課件
- 2025年度文化創意產業園區委托代建與運營協議
- 山東省東營市2023-2024學年高二下學期7月期末考試 歷史 含解析
- 《慢性阻塞性肺疾病的健康宣教》課件
- 課題申報參考:新時代高校“行走的思政課”教學模式創新與保障機制研究
- 2023-2024學年北京市朝陽區八年級下學期期末英語試卷(含答案)
- 《瑞幸咖啡財務舞弊案例分析》11000字(論文)
- 學校消防報警系統安裝施工方案
- 品管圈PDCA獲獎案例呼吸科提高患者吸入劑規范使用達標率
評論
0/150
提交評論