Skip to content

Commit

Permalink
Add a flag to return 0 if only one data point has been recorded
Browse files Browse the repository at this point in the history
Currently, getProcessCpuLoad() and getSystemCpuLoad() return -1 if only
one data point has been recorded. A compatibility flag
-XX:[+/-]CpuLoadCompatibility is added to match the behaviour of the RI,
which is to return 0.

Fixes: #13389
Related: eclipse-omr/omr#7189

Signed-off-by: Gengchen Tuo <[email protected]>
  • Loading branch information
thallium committed Dec 15, 2023
1 parent 0c87adb commit 0a12382
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,12 @@ public final String getHardwareModel() throws UnsupportedOperationException {
/* Returns the number of CPU's online at this very moment. */
private native int getOnlineProcessorsImpl();

/**
* Check if the CpuLoadCompatibility flag is set.
* @return if the CpuLoadCompatibility flag is set
*/
private static native boolean hasCpuLoadCompatibilityFlag();

/**
* {@inheritDoc}
*/
Expand All @@ -263,16 +269,21 @@ public final synchronized double getProcessCpuLoad() {
}
latestCpuTime = cpuTime;

/* First call to this method should -1, since we don't have any previous
* CPU times (or timestamp) to compute CPU load against.
/* First call to this method returns 0 to match the behaviour of the RI.
* If the CpuLoadCompatibility flag is set, the bahaviour is reverted to
* return -1 for compatibility.
*/
if (-1 == oldTime) {
/* Save current counters; next invocation onwards, we use these to
* compute CPU loads.
*/
oldTime = interimTime = latestTime;
oldCpuTime = interimCpuTime = latestCpuTime;
return CpuLoadCalculationConstants.ERROR_VALUE;
if (hasCpuLoadCompatibilityFlag()) {
return 0;
} else {
return CpuLoadCalculationConstants.ERROR_VALUE;
}
}

/* If a sufficiently long interval has elapsed since last sampling, calculate using
Expand Down
14 changes: 14 additions & 0 deletions runtime/jcl/common/extendedosmbean.c
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,20 @@ Java_com_ibm_lang_management_internal_ExtendedOperatingSystemMXBeanImpl_getHardw
return (NULL == str) ? NULL : (*env)->NewStringUTF(env, str);
}

/**
* Check if the CpuLoadCompatibility flag is set.
*
* @param env instance of JNIEnv
* @param unusedClass
*
* @return if the CpuLoadCompatibility flag is set
*/
jboolean JNICALL
Java_com_ibm_lang_management_internal_ExtendedOperatingSystemMXBeanImpl_hasCpuLoadCompatibilityFlag(JNIEnv *env, jclass unusedClass) {
J9JavaVM *vm = ((J9VMThread *)env)->javaVM;
return J9_ARE_ALL_BITS_SET(vm->extendedRuntimeFlags2, J9_EXTENDED_RUNTIME2_CPU_LOAD_COMPATIBILITY);
}

/**
* Returns the maximum number of file descriptors that can be opened in a process.
*
Expand Down
8 changes: 8 additions & 0 deletions runtime/jcl/common/mgmtosext.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ jdouble JNICALL
Java_com_ibm_lang_management_internal_ExtendedOperatingSystemMXBeanImpl_getSystemCpuLoadImpl(JNIEnv *env, jobject instance) {
PORT_ACCESS_FROM_ENV(env);
OMRPORT_ACCESS_FROM_J9PORT(PORTLIB);

J9JavaVM *vm = ((J9VMThread *)env)->javaVM;
double cpuLoad = 0.0;

intptr_t portLibraryStatus = omrsysinfo_get_CPU_load(&cpuLoad);
Expand All @@ -95,6 +97,12 @@ Java_com_ibm_lang_management_internal_ExtendedOperatingSystemMXBeanImpl_getSyste
case OMRPORT_ERROR_SYSINFO_NOT_SUPPORTED:
portLibraryStatus = -3;
break;
case OMRPORT_ERROR_INSUFFICIENT_DATA:
portLibraryStatus =
J9_ARE_ALL_BITS_SET(vm->extendedRuntimeFlags2, J9_EXTENDED_RUNTIME2_CPU_LOAD_COMPATIBILITY)
? 0
: -1;
break;
default:
portLibraryStatus = OMRPORT_ERROR_OPFAILED;
break;
Expand Down
1 change: 1 addition & 0 deletions runtime/jcl/exports.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ omr_add_exports(jclse
Java_com_ibm_lang_management_internal_ExtendedOperatingSystemMXBeanImpl_getSystemCpuLoadImpl
Java_com_ibm_lang_management_internal_ExtendedOperatingSystemMXBeanImpl_getTotalPhysicalMemoryImpl
Java_com_ibm_lang_management_internal_ExtendedOperatingSystemMXBeanImpl_getTotalProcessorUsageImpl
Java_com_ibm_lang_management_internal_ExtendedOperatingSystemMXBeanImpl_hasCpuLoadCompatibilityFlag
Java_com_ibm_lang_management_internal_ExtendedOperatingSystemMXBeanImpl_isDLPAREnabled
Java_com_ibm_lang_management_internal_ExtendedRuntimeMXBeanImpl_getVMIdleStateImpl
Java_com_ibm_lang_management_internal_JvmCpuMonitor_getThreadCategoryImpl
Expand Down
1 change: 1 addition & 0 deletions runtime/jcl/uma/se7_exports.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<export name="Java_com_ibm_lang_management_internal_ExtendedOperatingSystemMXBeanImpl_getMemoryUsageImpl" />
<export name="Java_com_ibm_lang_management_internal_ExtendedOperatingSystemMXBeanImpl_getOnlineProcessorsImpl" />
<export name="Java_com_ibm_lang_management_internal_ExtendedOperatingSystemMXBeanImpl_getHardwareModelImpl" />
<export name="Java_com_ibm_lang_management_internal_ExtendedOperatingSystemMXBeanImpl_hasCpuLoadCompatibilityFlag" />
<export name="Java_com_ibm_lang_management_internal_JvmCpuMonitor_getThreadsCpuUsageImpl" />
<export name="Java_com_ibm_lang_management_internal_JvmCpuMonitor_setThreadCategoryImpl" />
<export name="Java_com_ibm_lang_management_internal_JvmCpuMonitor_getThreadCategoryImpl" />
Expand Down
2 changes: 1 addition & 1 deletion runtime/oti/j9consts.h
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ extern "C" {
#define J9_EXTENDED_RUNTIME2_USE_CONTAINER_SUPPORT 0x200000
#define J9_EXTENDED_RUNTIME2_SHOW_CARRIER_FRAMES 0x400000
#define J9_EXTENDED_RUNTIME2_CRIU_SINGLE_THROW_BLOCKING_EXCEPTIONS 0x800000
#define J9_EXTENDED_RUNTIME2_UNUSED_0x1000000 0x1000000
#define J9_EXTENDED_RUNTIME2_CPU_LOAD_COMPATIBILITY 0x1000000

#define J9_OBJECT_HEADER_AGE_DEFAULT 0xA /* OBJECT_HEADER_AGE_DEFAULT */
#define J9_OBJECT_HEADER_SHAPE_MASK 0xE /* OBJECT_HEADER_SHAPE_MASK */
Expand Down
2 changes: 2 additions & 0 deletions runtime/oti/jclprots.h
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,8 @@ Java_com_ibm_lang_management_internal_ExtendedOperatingSystemMXBeanImpl_getOnlin
jstring JNICALL
Java_com_ibm_lang_management_internal_ExtendedOperatingSystemMXBeanImpl_getHardwareModelImpl(JNIEnv *env, jobject obj);

jboolean JNICALL
Java_com_ibm_lang_management_internal_ExtendedOperatingSystemMXBeanImpl_hasCpuLoadCompatibilityFlag(JNIEnv *env, jclass unusedClass);
/**
* Returns the maximum number of file descriptors that can be opened in a process.
*
Expand Down
2 changes: 2 additions & 0 deletions runtime/oti/jvminit.h
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,8 @@ enum INIT_STAGE {
#define VMOPT_XXDISABLEENSUREHASHED "-XX:-EnsureHashed:"
#define VMOPT_XXOPENJ9COMMANDLINEENV "-XX:+OpenJ9CommandLineEnv"
#define VMOPT_XXNOOPENJ9COMMANDLINEENV "-XX:-OpenJ9CommandLineEnv"
#define VMOPT_XXCPULOADCOMPATIBILITY "-XX:+CpuLoadCompatibility"
#define VMOPT_XXNOCPULOADCOMPATIBILITY "-XX:-CpuLoadCompatibility"

#if defined(J9VM_ZOS_3164_INTEROPERABILITY)
#define VMOPT_XXENABLE3164INTEROPERABILITY "-XX:+Enable3164Interoperability"
Expand Down
8 changes: 8 additions & 0 deletions runtime/vm/jvminit.c
Original file line number Diff line number Diff line change
Expand Up @@ -4123,6 +4123,14 @@ processVMArgsFromFirstToLast(J9JavaVM * vm)
}
}

{
IDATA cpuLoadCompatibility = FIND_AND_CONSUME_VMARG(EXACT_MATCH, VMOPT_XXCPULOADCOMPATIBILITY , NULL);
IDATA noCpuLoadCompatibility = FIND_AND_CONSUME_VMARG(EXACT_MATCH, VMOPT_XXNOCPULOADCOMPATIBILITY , NULL);
if (cpuLoadCompatibility > noCpuLoadCompatibility) {
vm->extendedRuntimeFlags2 |= J9_EXTENDED_RUNTIME2_CPU_LOAD_COMPATIBILITY;
}
}

return JNI_OK;
}

Expand Down

0 comments on commit 0a12382

Please sign in to comment.