Skip to content

Commit 8a571ee

Browse files
committed
8364667: JFR: Throttle doesn't work with dynamic events
Reviewed-by: mgronlun
1 parent ba0ae4c commit 8a571ee

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

src/jdk.jfr/share/classes/jdk/jfr/internal/ClassInspector.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ boolean isEnabled() {
140140
return true;
141141
}
142142

143-
boolean isThrottled() {
143+
boolean isThrottled(MethodDesc staticThrottleMethod) {
144144
String result = annotationValue(ANNOTATION_THROTTLE, String.class, "off");
145145
if (result != null) {
146146
return true;
@@ -151,6 +151,9 @@ boolean isThrottled() {
151151
return true;
152152
}
153153
}
154+
if (isJDK()) {
155+
return hasStaticMethod(staticThrottleMethod);
156+
}
154157
return false;
155158
}
156159

src/jdk.jfr/share/classes/jdk/jfr/internal/EventInstrumentation.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,7 @@ public final class EventInstrumentation {
124124
this.eventClassDesc = inspector.getClassDesc();
125125
this.staticCommitMethod = inspector.findStaticCommitMethod();
126126
this.untypedEventConfiguration = hasUntypedConfiguration();
127-
if (inspector.isJDK()) {
128-
this.throttled = inspector.hasStaticMethod(METHOD_EVENT_SHOULD_THROTTLE_COMMIT_LONG_LONG);
129-
} else {
130-
this.throttled = inspector.isThrottled();
131-
}
127+
this.throttled = inspector.isThrottled(METHOD_EVENT_SHOULD_THROTTLE_COMMIT_LONG_LONG);
132128
}
133129

134130
byte[] buildInstrumented() {

test/jdk/jdk/jfr/api/metadata/annotations/TestThrottle.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,17 @@
2929
import java.nio.file.Path;
3030
import java.time.Duration;
3131
import java.time.Instant;
32+
import java.util.ArrayList;
33+
import java.util.List;
3234
import java.util.Set;
35+
import java.util.concurrent.CopyOnWriteArrayList;
3336
import java.util.concurrent.atomic.AtomicInteger;
3437

38+
import jdk.jfr.AnnotationElement;
3539
import jdk.jfr.Enabled;
3640
import jdk.jfr.Event;
41+
import jdk.jfr.EventFactory;
42+
import jdk.jfr.Name;
3743
import jdk.jfr.Recording;
3844
import jdk.jfr.SettingControl;
3945
import jdk.jfr.SettingDefinition;
@@ -132,6 +138,7 @@ public static void main(String[] args) throws Exception {
132138
testThrottleThresholded();
133139
testThrottleNormalRate();
134140
testThrottleUserdefined();
141+
testThrottleDynamic();
135142
}
136143

137144
private static void testUnthrottled() throws Exception {
@@ -232,6 +239,40 @@ private static void testThrottleUserdefined(String test, String throttle, boolea
232239
}
233240
}
234241

242+
private static void testThrottleDynamic() throws Exception {
243+
List<AnnotationElement> offAnnotations = new ArrayList<>();
244+
offAnnotations.add(new AnnotationElement(Name.class, "DynamicZero"));
245+
offAnnotations.add(new AnnotationElement(Throttle.class, "0/s"));
246+
EventFactory offFactory = EventFactory.create(offAnnotations, List.of());
247+
248+
List<AnnotationElement> highRateAnnotations = new ArrayList<>();
249+
highRateAnnotations.add(new AnnotationElement(Name.class, "DynamicHighRate"));
250+
highRateAnnotations.add(new AnnotationElement(Throttle.class, "1000/s"));
251+
EventFactory highRateFactory = EventFactory.create(highRateAnnotations, List.of());
252+
253+
List<RecordedEvent> events = new CopyOnWriteArrayList<>();
254+
try (RecordingStream r = new RecordingStream()) {
255+
r.enable("DynamicZero");
256+
r.enable("DynamicHighRate");
257+
r.onEvent(events::add);
258+
r.startAsync();
259+
Event offEvent = offFactory.newEvent();
260+
offEvent.commit();
261+
Event highRateEvent = highRateFactory.newEvent();
262+
highRateEvent.begin();
263+
highRateEvent.commit();
264+
r.stop();
265+
if (events.size() != 1) {
266+
System.out.println(events);
267+
throw new Exception("Expected one dynamic event");
268+
}
269+
if (!events.get(0).getEventType().getName().equals("DynamicHighRate")) {
270+
System.out.println(events);
271+
throw new Exception("Expected DynamicHighRate");
272+
}
273+
}
274+
}
275+
235276
@SuppressWarnings("unchecked")
236277
private static void testEvent(Class<? extends Event> eventClass, boolean shouldCommit) throws Exception {
237278
try (Recording r = new Recording()) {
@@ -272,6 +313,8 @@ private static void testEvent(Class<? extends Event> eventClass, boolean shouldC
272313
}
273314
if (shouldCommit) {
274315
assertEvents(r, eventClass.getName(), 17 + 50 + 11);
316+
} else {
317+
assertEvents(r, eventClass.getName(), 0);
275318
}
276319
}
277320
}

0 commit comments

Comments
 (0)