diff --git a/ddprof-lib/src/main/cpp/flightRecorder.cpp b/ddprof-lib/src/main/cpp/flightRecorder.cpp index 23448ba610..a83bd313cf 100644 --- a/ddprof-lib/src/main/cpp/flightRecorder.cpp +++ b/ddprof-lib/src/main/cpp/flightRecorder.cpp @@ -703,7 +703,7 @@ void Recording::writeRecordingInfo(Buffer *buf) { buf->putUtf8("java-profiler " PROFILER_VERSION); buf->putUtf8("java-profiler.jfr"); buf->putVar64(MAX_JLONG); - if (VM::java_version() >= 14) { + if (VM::hotspot_version() >= 14) { buf->put8(0); } buf->put8(0); diff --git a/ddprof-lib/src/main/cpp/jfrMetadata.cpp b/ddprof-lib/src/main/cpp/jfrMetadata.cpp index f155fd4758..82cb388dc3 100644 --- a/ddprof-lib/src/main/cpp/jfrMetadata.cpp +++ b/ddprof-lib/src/main/cpp/jfrMetadata.cpp @@ -214,7 +214,7 @@ void JfrMetadata::initialize( << field("destination", T_STRING, "Destination") << field("maxAge", T_LONG, "Max Age", F_DURATION_MILLIS) << field("flushInterval", T_LONG, "Flush Interval", - F_DURATION_MILLIS, VM::java_version() >= 14) + F_DURATION_MILLIS, VM::hotspot_version() >= 14) << field("maxSize", T_LONG, "Max Size", F_BYTES) << field("recordingStart", T_LONG, "Start Time", F_TIME_MILLIS) << field("recordingDuration", T_LONG, "Recording Duration", diff --git a/ddprof-lib/src/main/cpp/livenessTracker.cpp b/ddprof-lib/src/main/cpp/livenessTracker.cpp index 96dda0a129..b30af22b17 100644 --- a/ddprof-lib/src/main/cpp/livenessTracker.cpp +++ b/ddprof-lib/src/main/cpp/livenessTracker.cpp @@ -242,7 +242,7 @@ Error LivenessTracker::initialize(Arguments &args) { } _initialized = true; - if (VM::java_version() < 11) { + if (VM::hotspot_version() < 11) { Log::warn("Liveness tracking requires Java 11+"); // disable liveness tracking _table_max_cap = 0; diff --git a/ddprof-lib/src/main/cpp/profiler.cpp b/ddprof-lib/src/main/cpp/profiler.cpp index 0e712c2ba5..72157261c5 100644 --- a/ddprof-lib/src/main/cpp/profiler.cpp +++ b/ddprof-lib/src/main/cpp/profiler.cpp @@ -1194,7 +1194,7 @@ Error Profiler::start(Arguments &args, bool reset) { } _safe_mode = args._safe_mode; - if (VM::java_version() < 8 || VM::isZing()) { + if (VM::hotspot_version() < 8 || VM::isZing()) { _safe_mode |= GC_TRACES | LAST_JAVA_PC; } diff --git a/ddprof-lib/src/main/cpp/vmEntry.cpp b/ddprof-lib/src/main/cpp/vmEntry.cpp index 436bd18ddd..76882c6d68 100644 --- a/ddprof-lib/src/main/cpp/vmEntry.cpp +++ b/ddprof-lib/src/main/cpp/vmEntry.cpp @@ -41,6 +41,7 @@ jvmtiEnv *VM::_jvmti = NULL; int VM::_java_version = 0; int VM::_java_update_version = 0; +int VM::_hotspot_version = 0; bool VM::_openj9 = false; bool VM::_hotspot = false; bool VM::_zing = false; @@ -90,6 +91,58 @@ static void *resolveMethodId(void **mid) { static void resolveMethodIdEnd() {} +JavaFullVersion JavaVersionAccess::get_java_version(char* prop_value) { + JavaFullVersion version = {8, 362}; // initial value is 8u362; an arbitrary java version + if (strncmp(prop_value, "1.8.0", 5) == 0) { + version.major = 8; + version.update = atoi(prop_value + 6); + } else if (strncmp(prop_value, "8.0.", 4) == 0) { + version.major = 8; + version.update = atoi(prop_value + 4); + } else if (strncmp(prop_value, "JRE 1.8.0", 9) == 0) { + // IBM JDK 8 does not report the 'real' version in any property accessible + // from JVMTI The only piece of info we can use has the following format + // `JRE 1.8.0 20230313_47323 ` + // Considering that JDK 8.0.361 is the only release in 2023 we can use + // that part of the version string to pretend anything after year 2023 + // inclusive is 8.0.361. Not perfect, but this is the only thing we have. + JavaFullVersion version; + version.major = 8; + char *idx = strstr(prop_value, " 202"); + if (idx != NULL) { + int year = atol(idx + 1); + if (year >= 2023) { + version.update = 361; + } else { + version.update = 351; + } + } + } else { + version.major = atoi(prop_value); + if (version.major < 9) { + version.major = 9; + } + // format is 11.0.17+8 + // this shortcut for parsing the update version should hold till Java 99 + version.update = atoi(prop_value + 5); + } + return version; +} + +int JavaVersionAccess::get_hotspot_version(char* prop_value) { + int hs_version = 0; + if (strncmp(prop_value, "25.", 3) == 0 && prop_value[3] > '0') { + hs_version = 8; + } else if (strncmp(prop_value, "24.", 3) == 0 && prop_value[3] > '0') { + hs_version = 7; + } else if (strncmp(prop_value, "20.", 3) == 0 && prop_value[3] > '0') { + hs_version = 6; + } else if ((hs_version = atoi(prop_value)) < 9) { + hs_version = 9; + } + return hs_version; +} + bool VM::init(JavaVM *vm, bool attach) { if (_jvmti != NULL) return true; @@ -157,7 +210,7 @@ bool VM::init(JavaVM *vm, bool attach) { } } if (prop == NULL) { - if (_jvmti->GetSystemProperty("java.vm.version", &prop) == 0) { + if (_jvmti->GetSystemProperty("java.runtime.version", &prop) == 0) { // insert debug output here } else { if (prop != NULL) { @@ -167,41 +220,23 @@ bool VM::init(JavaVM *vm, bool attach) { } } if (prop != NULL) { - if (strncmp(prop, "1.8.0", 5) == 0) { - _java_version = 8; - _java_update_version = atoi(prop + 5); - } else if (strncmp(prop, "8.0.", 4) == 0) { - _java_version = 8; - _java_update_version = atoi(prop + 4); - } else if (strncmp(prop, "JRE 1.8.0", 9) == 0) { - // IBM JDK 8 does not report the 'real' version in any property accessible - // from JVMTI The only piece of info we can use has the following format - // `JRE 1.8.0 20230313_47323 ` - // Considering that JDK 8.0.361 is the only release in 2023 we can use - // that part of the version string to pretend anything after year 2023 - // inclusive is 8.0.361. Not perfect, but this is the only thing we have. - _java_version = 8; - char *idx = strstr(prop, " 202"); - if (idx != NULL) { - int year = atol(idx + 1); - if (year >= 2023) { - _java_update_version = 361; - } else { - _java_update_version = 351; - } - } - } else { - _java_version = atoi(prop); - if (_java_version < 9) { - _java_version = 9; - } - // format is 11.0.17+8 - // this shortcut for parsing the update version should hold till Java 99 - _java_update_version = atoi(prop + 5); - } + JavaFullVersion version = JavaVersionAccess::get_java_version(prop); + _java_version = version.major; + _java_update_version = version.update; + _jvmti->Deallocate((unsigned char *)prop); + prop = NULL; + } + if (_jvmti->GetSystemProperty("java.vm.version", &prop) == 0) { + _hotspot_version = JavaVersionAccess::get_hotspot_version(prop); + _jvmti->Deallocate((unsigned char *)prop); + prop = NULL; + } + + if (prop != NULL) { _jvmti->Deallocate((unsigned char *)prop); } - _can_sample_objects = !_hotspot || java_version() >= 11; + + _can_sample_objects = !_hotspot || hotspot_version() >= 11; CodeCache *lib = isOpenJ9() @@ -222,7 +257,7 @@ bool VM::init(JavaVM *vm, bool attach) { } } - if (!attach && java_version() == 8 && OS::isLinux()) { + if (!attach && hotspot_version() == 8 && OS::isLinux()) { // Workaround for JDK-8185348 char *func = (char *)lib->findSymbol( "_ZN6Method26checked_resolve_jmethod_idEP10_jmethodID"); @@ -241,7 +276,7 @@ bool VM::init(JavaVM *vm, bool attach) { _can_sample_objects = potential_capabilities.can_generate_sampled_object_alloc_events && - (!_hotspot || java_version() >= 11); + (!_hotspot || hotspot_version() >= 11); _can_intercept_binding = potential_capabilities.can_generate_native_method_bind_events && HeapUsage::needsNativeBindingInterception(); @@ -289,7 +324,7 @@ bool VM::init(JavaVM *vm, bool attach) { _jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_NATIVE_METHOD_BIND, NULL); - if (java_version() == 0 || !CodeHeap::available()) { + if (hotspot_version() == 0 || !CodeHeap::available()) { // Workaround for JDK-8173361: avoid CompiledMethodLoad events when possible _jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_COMPILED_METHOD_LOAD, NULL); @@ -309,7 +344,7 @@ bool VM::init(JavaVM *vm, bool attach) { // if the user sets -XX:+UseAdaptiveGCBoundary we will just disable the // profiler to avoid the risk of crashing flag was made obsolete (inert) in 15 // (see JDK-8228991) and removed in 16 (see JDK-8231560) - if (java_version() < 15) { + if (hotspot_version() < 15) { char *flag_addr = (char *)JVMFlag::find("UseAdaptiveGCBoundary"); _is_adaptive_gc_boundary_flag_set = flag_addr != NULL && *flag_addr == 1; } diff --git a/ddprof-lib/src/main/cpp/vmEntry.h b/ddprof-lib/src/main/cpp/vmEntry.h index ef7c4da7f2..a329db0e9f 100644 --- a/ddprof-lib/src/main/cpp/vmEntry.h +++ b/ddprof-lib/src/main/cpp/vmEntry.h @@ -87,6 +87,17 @@ typedef struct { jvmtiError(JNICALL *RetransformClasses)(jvmtiEnv *, jint, const jclass *); } JVMTIFunctions; +typedef struct { + int major; + int update; +} JavaFullVersion; + +class JavaVersionAccess { + public: + static JavaFullVersion get_java_version(char* prop_value); + static int get_hotspot_version(char* prop_value); +}; + class VM { private: static JavaVM *_vm; @@ -94,6 +105,7 @@ class VM { static int _java_version; static int _java_update_version; + static int _hotspot_version; static bool _openj9; static bool _hotspot; static bool _zing; @@ -144,7 +156,7 @@ class VM { static int java_version() { return _java_version; } - static int hotspot_version() { return isHotspot() ? _java_version : -1; } + static int hotspot_version() { return isHotspot() ? _hotspot_version : -1; } static int java_update_version() { return _java_update_version; } diff --git a/ddprof-lib/src/main/cpp/vmStructs.cpp b/ddprof-lib/src/main/cpp/vmStructs.cpp index e94f7864bc..42ad69cf12 100644 --- a/ddprof-lib/src/main/cpp/vmStructs.cpp +++ b/ddprof-lib/src/main/cpp/vmStructs.cpp @@ -493,7 +493,7 @@ const void *VMStructs::findHeapUsageFunc() { return _libjvm->findSymbol("_ZN15G1CollectedHeap12memory_usageEv"); } else if (isFlagTrue("UseShenandoahGC")) { return _libjvm->findSymbol("_ZN14ShenandoahHeap12memory_usageEv"); - } else if (isFlagTrue("UseZGC") && VM::java_version() < 21) { + } else if (isFlagTrue("UseZGC") && VM::hotspot_version() < 21) { // acessing this method in JDK 21 (generational ZGC) wil cause SIGSEGV return _libjvm->findSymbol("_ZN14ZCollectedHeap12memory_usageEv"); } @@ -506,7 +506,7 @@ void VMStructs::initJvmFunctions() { _get_stack_trace = (GetStackTraceFunc)_libjvm->findSymbolByPrefix( "_ZN8JvmtiEnv13GetStackTraceEP10JavaThreadiiP"); - if (VM::java_version() == 8) { + if (VM::hotspot_version() == 8) { _lock_func = (LockFunc)_libjvm->findSymbol( "_ZN7Monitor28lock_without_safepoint_checkEv"); _unlock_func = (LockFunc)_libjvm->findSymbol("_ZN7Monitor6unlockEv"); @@ -589,7 +589,7 @@ void VMStructs::initThreadBridge(JNIEnv *env) { void VMStructs::initLogging(JNIEnv *env) { // Workaround for JDK-8238460 // Can not be used for OpenJ9 because it will crash the VM - if (VM::java_version() >= 15 && !VM::isOpenJ9()) { + if (VM::java_version() >= 15 && VM::hotspot_version() >= 15 && !VM::isOpenJ9()) { VMManagement *management = VM::management(); if (management != NULL) { jstring vm_log_str = env->NewStringUTF("VM.log list"); diff --git a/ddprof-lib/src/test/cpp/ddprof_ut.cpp b/ddprof-lib/src/test/cpp/ddprof_ut.cpp index 96fecf7e3f..93cbbe40f5 100644 --- a/ddprof-lib/src/test/cpp/ddprof_ut.cpp +++ b/ddprof-lib/src/test/cpp/ddprof_ut.cpp @@ -9,6 +9,7 @@ #include "threadFilter.h" #include "threadInfo.h" #include "threadLocalData.h" + #include "vmEntry.h" #include ssize_t callback(char* ptr, int len) { @@ -193,6 +194,47 @@ EXPECT_FALSE(data.is_unwinding_Java()); } + TEST(JavaVersionAccess, testJavaVersionAccess_hs_8) { + char runtime_prop_value_1[] = "1.8.0_292"; + char vm_prop_value_1[] = "25.292-b10"; + char runtime_prop_value_2[] = "8.0.292"; + char vm_prop_value_2[] = "25.292-b10"; + + JavaFullVersion java_version1 = JavaVersionAccess::get_java_version(runtime_prop_value_1); + int hs_version1 = JavaVersionAccess::get_hotspot_version(vm_prop_value_1); + EXPECT_EQ(8, java_version1.major); + EXPECT_EQ(292, java_version1.update); + EXPECT_EQ(8, hs_version1); + + JavaFullVersion java_version2 = JavaVersionAccess::get_java_version(runtime_prop_value_2); + int hs_version2 = JavaVersionAccess::get_hotspot_version(vm_prop_value_2); + EXPECT_EQ(8, java_version2.major); + EXPECT_EQ(292, java_version2.update); + EXPECT_EQ(8, hs_version2); + } + + TEST(JavaVersionAccess, testJavaVersionAccess_hs_11) { + char runtime_prop_value_1[] = "11.0.25"; + char vm_prop_value_1[] = "11.0.25+10"; + + JavaFullVersion java_version1 = JavaVersionAccess::get_java_version(runtime_prop_value_1); + int hs_version1 = JavaVersionAccess::get_hotspot_version(vm_prop_value_1); + EXPECT_EQ(11, java_version1.major); + EXPECT_EQ(25, java_version1.update); + EXPECT_EQ(11, hs_version1); + } + + TEST(JavaVersionAccess, testJavaVersionAccess_hs_default) { + char runtime_prop_value_1[] = "3.11.25x_10"; + char vm_prop_value_1[] = "3.11.25x_10"; + + JavaFullVersion java_version1 = JavaVersionAccess::get_java_version(runtime_prop_value_1); + int hs_version1 = JavaVersionAccess::get_hotspot_version(vm_prop_value_1); + EXPECT_EQ(9, java_version1.major); + EXPECT_EQ(25, java_version1.update); + EXPECT_EQ(9, hs_version1); + } + int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS();