这是衡量特定Java应用程序的CPU时间的正确方法吗?

时间:2011-05-14 20:33:11

标签: java c++ windows java-native-interface cpu-time

我想测量我的Java应用程序的CPU时间,而不是操作系统CPU使用率,而不是整个JVM的CPU使用/时间,从而测量系统上运行的所有Java应用程序,而只测量这一个Java应用程序。 为此,我选择使用JNI。此C ++代码是否测量加载库或整个JVM的Java应用程序的CPU时间?

#include "JNIPipeline.h"
#include <windows.h>
#include <stdio.h>
#include <iostream>

unsigned __int64 fileTimeToUint64(FILETIME*);

static HANDLE s_currentProcess;
static int s_numberOfProcessors;

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) {
    SYSTEM_INFO systemInfo;

    GetSystemInfo(&systemInfo);
    s_currentProcess = GetCurrentProcess();
    s_numberOfProcessors = systemInfo.dwNumberOfProcessors;

    return JNI_VERSION_1_6;
}

JNIEXPORT jlong JNICALL Java_org_server_util_JNIPipeline_getCPUTime(JNIEnv*, jobject) {
    FILETIME creationTime, userTime, exitTime, kernelTime;

    GetProcessTimes(s_currentProcess, &creationTime, &exitTime, &kernelTime, &userTime);

    return (jlong)(fileTimeToUint64(&kernelTime) + fileTimeToUint64(&userTime)) / (s_numberOfProcessors * 10000);
}

unsigned __int64 fileTimeToUint64(FILETIME* ft) {
    return (((unsigned __int64)ft->dwHighDateTime << 32) | ft->dwLowDateTime);
}

这是以毫秒为单位测量CPU时间的可接受方式吗?请注意,我将使用测量值绘制“曲线图”。

0 个答案:

没有答案