Skip to content

Commit

Permalink
Cleanups.
Browse files Browse the repository at this point in the history
(cherry picked from commit fa67285)
  • Loading branch information
christianhaeubl authored and zakkak committed Mar 28, 2023
1 parent 049801c commit c641ce2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
*/
public class JfrRecorderThread extends Thread {
private static final int BUFFER_FULL_ENOUGH_PERCENTAGE = 50;
private static final boolean CHUNK_ROTATION_MONITOR_PRESENT = (new Target_jdk_jfr_internal_JVM.JvmChunkRotationMonitorAvailable()).getAsBoolean();

private final JfrGlobalMemory globalMemory;
private final JfrUnlockedChunkWriter unlockedChunkWriter;
Expand Down Expand Up @@ -102,9 +101,7 @@ private void persistBuffers(JfrChunkWriter chunkWriter) {
if (isFullEnough(buffer)) {
boolean shouldNotify = persistBuffer(chunkWriter, buffer);
if (shouldNotify) {
Object chunkRotationMonitor = CHUNK_ROTATION_MONITOR_PRESENT
? Target_jdk_jfr_internal_JVM.CHUNK_ROTATION_MONITOR
: Target_jdk_jfr_internal_JVM.FILE_DELTA_CHANGE;
Object chunkRotationMonitor = getChunkRotationMonitor();
synchronized (chunkRotationMonitor) {
chunkRotationMonitor.notifyAll();
}
Expand All @@ -113,6 +110,14 @@ private void persistBuffers(JfrChunkWriter chunkWriter) {
}
}

private static Object getChunkRotationMonitor() {
if (HasChunkRotationMonitorField.get()) {
return Target_jdk_jfr_internal_JVM.CHUNK_ROTATION_MONITOR;
} else {
return Target_jdk_jfr_internal_JVM.FILE_DELTA_CHANGE;
}
}

@Uninterruptible(reason = "Epoch must not change while in this method.")
private static boolean persistBuffer(JfrChunkWriter chunkWriter, JfrBuffer buffer) {
if (JfrBufferAccess.acquire(buffer)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import java.util.List;
import java.util.function.BooleanSupplier;

import org.graalvm.compiler.api.replacements.Fold;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.nativeimage.ProcessProperties;

import com.oracle.svm.core.Containers;
Expand All @@ -51,43 +54,21 @@
@TargetClass(value = jdk.jfr.internal.JVM.class, onlyWith = HasJfrSupport.class)
public final class Target_jdk_jfr_internal_JVM {
// Checkstyle: stop
@Alias @TargetElement(onlyWith = JvmChunkRotationMonitorAvailable.class) //
@Alias //
@TargetElement(onlyWith = HasChunkRotationMonitorField.class) //
static Object CHUNK_ROTATION_MONITOR;

@Alias @TargetElement(onlyWith = JvmFileDeltaChangeAvailable.class) //
@Alias //
@TargetElement(onlyWith = HasFileDeltaChangeField.class) //
static Object FILE_DELTA_CHANGE;
// Checkstyle: resume

static class JvmChunkRotationMonitorAvailable extends JvmFieldAvailable {
protected JvmChunkRotationMonitorAvailable() {
super("CHUNK_ROTATION_MONITOR");
}
}

private static class JvmFileDeltaChangeAvailable extends JvmFieldAvailable {
protected JvmFileDeltaChangeAvailable() {
super("FILE_DELTA_CHANGE");
}
}

private abstract static class JvmFieldAvailable implements BooleanSupplier {

private final String fieldName;

protected JvmFieldAvailable(String fieldName) {
this.fieldName = fieldName;
}

@Override
public boolean getAsBoolean() {
return ReflectionUtil.lookupField(true, JVM.class, fieldName) != null;
}
}

@Alias @RecomputeFieldValue(kind = RecomputeFieldValue.Kind.Reset) //
@Alias //
@RecomputeFieldValue(kind = RecomputeFieldValue.Kind.Reset) //
private volatile boolean nativeOK;

@Alias @RecomputeFieldValue(kind = RecomputeFieldValue.Kind.Reset) //
@Alias //
@RecomputeFieldValue(kind = RecomputeFieldValue.Kind.Reset) //
@TargetElement(onlyWith = JDK11OrEarlier.class) //
private volatile boolean recording;

Expand Down Expand Up @@ -498,3 +479,27 @@ public void markChunkFinal() {
// Temporarily do nothing. This is used for JFR streaming.
}
}

class HasChunkRotationMonitorField implements BooleanSupplier {
private static final boolean HAS_FIELD = ReflectionUtil.lookupField(true, JVM.class, "CHUNK_ROTATION_MONITOR") != null;

@Override
public boolean getAsBoolean() {
return HAS_FIELD;
}

@Fold
public static boolean get() {
return HAS_FIELD;
}
}

@Platforms(Platform.HOSTED_ONLY.class)
class HasFileDeltaChangeField implements BooleanSupplier {
private static final boolean HAS_FIELD = ReflectionUtil.lookupField(true, JVM.class, "FILE_DELTA_CHANGE") != null;

@Override
public boolean getAsBoolean() {
return HAS_FIELD;
}
}

0 comments on commit c641ce2

Please sign in to comment.