主题:JNI 取得系统cpu使用率的问题!!!
public class CPUTest extends Thread
{
public void run(){
while(true)
{
try {
final SystemInformation.CPUUsageSnapshot m_prevSnapshot =
SystemInformation.makeCPUUsageSnapshot ();
Thread.sleep(1000);
final SystemInformation.CPUUsageSnapshot event =
SystemInformation.makeCPUUsageSnapshot ();
long freemem = SystemInformation.getFreeMem()/1024;
long maxmem = SystemInformation.getMaxMem()/1024;
double receivedCPUUsage = 100.0 * SystemInformation.getProcessCPUUsage (m_prevSnapshot, event);
System.out.println("Current CPU usage is "+receivedCPUUsage+"%");
}catch(Exception e)
{
e.printStackTrace();
}
}
}
/**
* @param args
*/
public static void main(String[] args)throws Exception {
new CPUTest().start();
}
}
C中的silib.c方法是:
JNIEXPORT jlong JNICALL
Java_com_vladium_utils_SystemInformation_getProcessCPUTime (JNIEnv * env, jclass cls)
{
FILETIME creationTime, exitTime, kernelTime, userTime;
DWORD errCode;
LPVOID lpMsgBuf;
BOOL resultSuccessful = GetProcessTimes (s_currentProcess, & creationTime, & exitTime, & kernelTime, & userTime);
if (!resultSuccessful) {
errCode = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
errCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
printf("[CPUmon] An error occured while trying to get CPU time.Error code: %ldError description: %s",errCode,lpMsgBuf);
fflush(stdout);
LocalFree(lpMsgBuf);
return -1;
}
return (jlong) ((fileTimeToInt64 (& kernelTime) + fileTimeToInt64 (& userTime)) /
(s_numberOfProcessors * 10000));
}
SystemInformation类中取得cpu的运行的两个差对象的方法是:
public static CPUUsageSnapshot makeCPUUsageSnapshot() throws SystemInformation.NegativeCPUTime
{
long prCPUTime = getProcessCPUTime ();
if (prCPUTime<0) throw new NegativeCPUTime();
return new CPUUsageSnapshot (System.currentTimeMillis (), getProcessCPUTime ());
}
java中的SystemInformation能正常编译成.h 的文件,silib.c也能编译成.dll文件。
问题是在CPUTest 类中构造CPUUsageSnapshot 对象时getProcessCPUTime 每次取的值是一样的。
打印出来的Current CPU usage is 0%。但是我CPUTest 类中生成m_prevSnapshot ,event 对象语句前设断点调试一直下一步getProcessCPUTime 就能得到不同的值。在生成m_prevSnapshot ,event 语句对象后设断点第一次getProcessCPUTime得到的值是一样的,第二次就能得到不同的值。为什么啊? 我实在是搞不明白是什么原因了。。上面的代码上我是参照网上找的写的。还请高手帮下忙!!!!!!!!
{
public void run(){
while(true)
{
try {
final SystemInformation.CPUUsageSnapshot m_prevSnapshot =
SystemInformation.makeCPUUsageSnapshot ();
Thread.sleep(1000);
final SystemInformation.CPUUsageSnapshot event =
SystemInformation.makeCPUUsageSnapshot ();
long freemem = SystemInformation.getFreeMem()/1024;
long maxmem = SystemInformation.getMaxMem()/1024;
double receivedCPUUsage = 100.0 * SystemInformation.getProcessCPUUsage (m_prevSnapshot, event);
System.out.println("Current CPU usage is "+receivedCPUUsage+"%");
}catch(Exception e)
{
e.printStackTrace();
}
}
}
/**
* @param args
*/
public static void main(String[] args)throws Exception {
new CPUTest().start();
}
}
C中的silib.c方法是:
JNIEXPORT jlong JNICALL
Java_com_vladium_utils_SystemInformation_getProcessCPUTime (JNIEnv * env, jclass cls)
{
FILETIME creationTime, exitTime, kernelTime, userTime;
DWORD errCode;
LPVOID lpMsgBuf;
BOOL resultSuccessful = GetProcessTimes (s_currentProcess, & creationTime, & exitTime, & kernelTime, & userTime);
if (!resultSuccessful) {
errCode = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
errCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
printf("[CPUmon] An error occured while trying to get CPU time.Error code: %ldError description: %s",errCode,lpMsgBuf);
fflush(stdout);
LocalFree(lpMsgBuf);
return -1;
}
return (jlong) ((fileTimeToInt64 (& kernelTime) + fileTimeToInt64 (& userTime)) /
(s_numberOfProcessors * 10000));
}
SystemInformation类中取得cpu的运行的两个差对象的方法是:
public static CPUUsageSnapshot makeCPUUsageSnapshot() throws SystemInformation.NegativeCPUTime
{
long prCPUTime = getProcessCPUTime ();
if (prCPUTime<0) throw new NegativeCPUTime();
return new CPUUsageSnapshot (System.currentTimeMillis (), getProcessCPUTime ());
}
java中的SystemInformation能正常编译成.h 的文件,silib.c也能编译成.dll文件。
问题是在CPUTest 类中构造CPUUsageSnapshot 对象时getProcessCPUTime 每次取的值是一样的。
打印出来的Current CPU usage is 0%。但是我CPUTest 类中生成m_prevSnapshot ,event 对象语句前设断点调试一直下一步getProcessCPUTime 就能得到不同的值。在生成m_prevSnapshot ,event 语句对象后设断点第一次getProcessCPUTime得到的值是一样的,第二次就能得到不同的值。为什么啊? 我实在是搞不明白是什么原因了。。上面的代码上我是参照网上找的写的。还请高手帮下忙!!!!!!!!