Skip to content

[GR-64839] Consolidate JfrJdkCompatibility #11282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import com.sun.management.internal.PlatformMBeanProviderImpl;

import jdk.jfr.Configuration;
import jdk.jfr.internal.JVMSupport;
import jdk.jfr.internal.jfc.JFC;

/**
Expand Down Expand Up @@ -161,7 +162,7 @@ public void afterRegistration(AfterRegistrationAccess access) {

// Initialize some parts of JFR/JFC at image build time.
List<Configuration> knownConfigurations = JFC.getConfigurations();
JfrJdkCompatibility.createNativeJFR();
JVMSupport.createJFR();

ImageSingletons.add(JfrManager.class, new JfrManager(HOSTED_ENABLED));
ImageSingletons.add(SubstrateJVM.class, new SubstrateJVM(knownConfigurations, true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,8 @@
*/
package com.oracle.svm.core.jfr;

import java.lang.reflect.Method;
import java.time.Duration;
import java.time.LocalDateTime;

import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.nativeimage.ProcessProperties;
import org.graalvm.nativeimage.hosted.FieldValueTransformer;

Expand All @@ -39,40 +35,8 @@
import com.oracle.svm.core.annotate.RecomputeFieldValue.Kind;
import com.oracle.svm.core.annotate.Substitute;
import com.oracle.svm.core.annotate.TargetClass;
import com.oracle.svm.core.util.VMError;
import com.oracle.svm.util.ReflectionUtil;

import jdk.jfr.Recording;
import jdk.jfr.internal.JVMSupport;

/**
* Compatibility class to handle incompatible changes between JDK 21 and JDK 22. Once support for
* JDKs prior to 22 is dropped, these the methods can be called directly and the substitutions can
* go away.
*/
@SuppressWarnings("unused")
public final class JfrJdkCompatibility {
private JfrJdkCompatibility() {
}

public static String makeFilename(Recording recording) {
return Target_jdk_jfr_internal_JVMSupport.makeFilename(recording);
}

public static String formatTimespan(Duration dValue, String separation) {
return Target_jdk_jfr_internal_util_ValueFormatter.formatTimespan(dValue, separation);
}

@Platforms(Platform.HOSTED_ONLY.class)
public static void createNativeJFR() {
try {
Method createJFR = ReflectionUtil.lookupMethod(JVMSupport.class, "createJFR");
createJFR.invoke(null);
} catch (ReflectiveOperationException | ClassCastException e) {
throw VMError.shouldNotReachHere(e);
}
}
}

@TargetClass(className = "jdk.jfr.internal.JVMSupport")
final class Target_jdk_jfr_internal_JVMSupport {
Expand All @@ -82,7 +46,12 @@ final class Target_jdk_jfr_internal_JVMSupport {

@Substitute
public static String makeFilename(Recording recording) {
return JfrFilenameUtil.makeFilename(recording);
long pid = ProcessProperties.getProcessID();
LocalDateTime now = LocalDateTime.now();
String date = Target_jdk_jfr_internal_util_ValueFormatter.formatDateTime(now);
String idText = recording == null ? "" : "-id-" + recording.getId();
String imageName = SubstrateOptions.Name.getValue();
return imageName + "-pid-" + pid + idText + "-" + date + ".jfr";
}
}

Expand All @@ -95,24 +64,6 @@ public Object transform(Object receiver, Object originalValue) {

@TargetClass(className = "jdk.jfr.internal.util.ValueFormatter", onlyWith = HasJfrSupport.class)
final class Target_jdk_jfr_internal_util_ValueFormatter {
@Alias
public static native String formatTimespan(Duration dValue, String separation);

@Alias
public static native String formatDateTime(LocalDateTime time);
}

final class JfrFilenameUtil {
public static String makeFilename(Recording recording) {
long pid = ProcessProperties.getProcessID();
String date = getFormatDateTime();
String idText = recording == null ? "" : "-id-" + recording.getId();
String imageName = SubstrateOptions.Name.getValue();
return imageName + "-pid-" + pid + idText + "-" + date + ".jfr";
}

private static String getFormatDateTime() {
LocalDateTime now = LocalDateTime.now();
return Target_jdk_jfr_internal_util_ValueFormatter.formatDateTime(now);
}
}