Skip to content

Commit 261174a

Browse files
committed
svm: further adopt "JDK-8351594: JFR: Rate-limited sampling of Java events"
1 parent a1c87a1 commit 261174a

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jfr/Target_jdk_jfr_internal_settings_Throttler.java

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,38 +27,50 @@
2727
import java.util.Random;
2828

2929
import com.oracle.svm.core.annotate.Alias;
30+
import com.oracle.svm.core.annotate.Inject;
3031
import com.oracle.svm.core.annotate.InjectAccessors;
32+
import com.oracle.svm.core.annotate.RecomputeFieldValue;
3133
import com.oracle.svm.core.annotate.TargetClass;
3234

35+
import jdk.graal.compiler.nodes.extended.MembarNode;
36+
3337
@TargetClass(className = "jdk.jfr.internal.settings.Throttler")
3438
public final class Target_jdk_jfr_internal_settings_Throttler {
3539
@Alias //
3640
@InjectAccessors(ThrottlerRandomAccessor.class) //
3741
private Random randomGenerator;
42+
43+
@Inject //
44+
@RecomputeFieldValue(kind = RecomputeFieldValue.Kind.Reset) //
45+
Random injectedRandomGenerator;
3846
}
3947

4048
final class ThrottlerRandomAccessor {
41-
private static Random cachedRandom;
42-
4349
@SuppressWarnings("unused")
4450
static Random get(Target_jdk_jfr_internal_settings_Throttler receiver) {
45-
if (cachedRandom != null) {
46-
return cachedRandom;
51+
Random value = receiver.injectedRandomGenerator;
52+
if (value != null) {
53+
return value;
4754
}
48-
return initializeRandom();
55+
return initializeRandom(receiver);
4956
}
5057

51-
private static synchronized Random initializeRandom() {
52-
if (cachedRandom != null) {
53-
return cachedRandom;
58+
private static synchronized Random initializeRandom(Target_jdk_jfr_internal_settings_Throttler receiver) {
59+
Random value = receiver.injectedRandomGenerator;
60+
if (value != null) {
61+
return value;
5462
}
5563

56-
cachedRandom = new Random();
57-
return cachedRandom;
64+
value = new Random();
65+
/* Ensure that other threads see a fully initialized Random object once published below. */
66+
MembarNode.memoryBarrier(MembarNode.FenceKind.STORE_STORE);
67+
68+
receiver.injectedRandomGenerator = value;
69+
return value;
5870
}
5971

6072
@SuppressWarnings("unused")
6173
static synchronized void set(Target_jdk_jfr_internal_settings_Throttler receiver, Random value) {
62-
throw new RuntimeException("The field jdk.jfr.internal.settings.Throttler.randomGenerator cannot be set");
74+
receiver.injectedRandomGenerator = value;
6375
}
6476
}

0 commit comments

Comments
 (0)