/* * Class: com_vladium_utils_SystemInformation * Method: getProcessCPUTime * Signature: ()J */ JNIEXPORT jlong JNICALL Java_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)); }
// 得到进程占用的CPU时间 int getTime(__int64& proc) { FILETIME create; FILETIME exit; FILETIME ker; // 内核占用时间 FILETIME user; // 用户占用时间 if(!GetProcessTimes(hd, &create, &exit, &ker, &user)){ return -1; } proc = (fileTimeToInt64(ker) + fileTimeToInt64(user)) / 10000; return 0; }
/* * Class: com_vladium_utils_SystemInformation * Method: getProcessCPUUsage * Signature: ()D */ JNIEXPORT jdouble JNICALL Java_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 is not very precise [~10ms resolution], use sufficiently long sampling intervals if you make use 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); }