Original link
/* ------------------------------------------------------------------------- *//** An implementation of JNI methods in Com.vladium.utils.SystemInformation * class. The author compiled it using Microsoft Visual C + + But the code * should is easy-to-use with any compiler for Win32 Platfor M. * * For simplicity, this implementaion assumes JNI 1.2+ and omits error handling. * * (C) 2002, Vladimir Roubtsov [[email protected]*//* ------------------------------------------------------------------------- */#include<windows.h>#include<process.h>#include<winbase.h>#include"Com_vladium_utils_systeminformation.h"StaticJint s_pid;StaticHANDLE s_currentprocess;Static ints_numberofprocessors;/* ------------------------------------------------------------------------- *//** A helper function for converting FILETIME to A Longlong [safe from memory * Alignment point of view].*/StaticLONGLONGfileTimeToInt64 (ConstFILETIME *Time ) {Ularge_integer _time; _time. LowPart= time->Dwlowdatetime; _time. Highpart= time->Dwhighdatetime; return_time. QuadPart;}/* ......................................................................... *//** This method is added in JNI 1.2. It is executed once before all other * methods be called and is ostensibly for negotiating JNI spec versions, but * Can a LSO be conveniently used for initializing variables that would not * change throughout the lifetime of this process. */jniexport jint jnicalljni_onload (JAVAVM* VMS,void*reserved) {System_info systemInfo; S_pid=_getpid (); S_currentprocess=getcurrentprocess (); GetSystemInfo (&systemInfo); S_numberofprocessors=systeminfo.dwnumberofprocessors; returnjni_version_1_2;}/* ......................................................................... *//** class:com_vladium_utils_systeminformation * method:getprocessid * Signature: () I*/jniexport jint jnicalljava_com_vladium_utils_systeminformation_getprocessid (jnienv*env, Jclass CLS) { returns_pid;}/* ......................................................................... *//** class:com_vladium_utils_systeminformation * method:getprocesscputime * Signature: () J*/jniexport jlong jnicalljava_com_vladium_utils_systeminformation_getprocesscputime (JNIEnv*env, Jclass CLS) {FILETIME creationtime, exittime, Kerneltime, Usertime; Getprocesstimes (s_currentprocess,& CreationTime, & Exittime, & Kerneltime, &usertime); return(Jlong) (FileTimeToInt64 (& kerneltime) + FileTimeToInt64 (& Usertime))/(S_numberofprocessors*10000));}/* ......................................................................... *//*Define min Elapsed time (in units of 10E-7 sec):*/#defineMin_elapsed_time (10000)/** class:com_vladium_utils_systeminformation * method:getprocesscpuusage * Signature: () D*/jniexport jdouble jnicalljava_com_vladium_utils_systeminformation_getprocesscpuusage (JNIEnv*env, Jclass CLS) {FILETIME creationtime, exittime, Kerneltime, Usertime, Nowtime; Longlong ElapsedTime; Getprocesstimes (s_currentprocess,& CreationTime, & Exittime, & Kerneltime, &usertime); Getsystemtimeasfiletime (&nowtime); /*Note:win32 system time isn't very precise [~10ms resolution], use sufficiently long sampling interval s If you do use the of this method. */ElapsedTime= FileTimeToInt64 (& nowtime)-FileTimeToInt64 (&creationtime); if(ElapsedTime <min_elapsed_time)return 0.0; Else return((jdouble) (FileTimeToInt64 (& kerneltime) + FileTimeToInt64 (& Usertime))/(S_numberofprocessors*elapsedtime);}#undefMin_elapsed_time
Java Read process CPU usage