23
23
24
24
package jdk .jfr .api .metadata .annotations ;
25
25
26
- import java .lang .annotation .Annotation ;
27
- import java .lang .annotation .ElementType ;
28
- import java .lang .annotation .Retention ;
29
- import java .lang .annotation .RetentionPolicy ;
30
- import java .lang .annotation .Target ;
26
+ import java .io .IOException ;
31
27
import java .lang .reflect .Constructor ;
28
+ import java .nio .file .Files ;
29
+ import java .nio .file .Path ;
32
30
import java .time .Duration ;
33
- import java .util .HashSet ;
34
- import java .util .List ;
35
- import java .util .Map ;
31
+ import java .time .Instant ;
36
32
import java .util .Set ;
37
33
import java .util .concurrent .atomic .AtomicInteger ;
38
- import java .util .concurrent .atomic .AtomicLong ;
39
- import java .util .concurrent .atomic .AtomicReference ;
40
34
41
- import jdk .jfr .AnnotationElement ;
42
- import jdk .jfr .Event ;
43
- import jdk .jfr .EventType ;
44
- import jdk .jfr .MetadataDefinition ;
45
- import jdk .jfr .Name ;
46
- import jdk .jfr .Threshold ;
47
35
import jdk .jfr .Enabled ;
36
+ import jdk .jfr .Event ;
48
37
import jdk .jfr .Recording ;
38
+ import jdk .jfr .SettingControl ;
49
39
import jdk .jfr .SettingDefinition ;
50
- import jdk .jfr .SettingDescriptor ;
40
+ import jdk .jfr .Threshold ;
51
41
import jdk .jfr .Throttle ;
52
- import jdk .jfr .ValueDescriptor ;
53
42
import jdk .jfr .consumer .RecordedEvent ;
43
+ import jdk .jfr .consumer .RecordingFile ;
54
44
import jdk .jfr .consumer .RecordingStream ;
55
- import jdk .test .lib .Asserts ;
56
- import jdk .test .lib .jfr .Events ;
57
- import jdk .jfr .SettingControl ;
58
45
59
46
/**
60
47
* @test
65
52
*/
66
53
public class TestThrottle {
67
54
55
+ public static class UnthrottledEvent extends Event {
56
+ }
57
+
68
58
@ Throttle ("off" )
69
59
@ Enabled (false )
70
60
public static class ThrottledDisabledEvent extends Event {
@@ -130,7 +120,11 @@ public static class ThrottledReuseEvent extends Event {
130
120
public int index ;
131
121
}
132
122
123
+ private static Instant startTime ;
124
+
133
125
public static void main (String [] args ) throws Exception {
126
+ startTime = determineMinimumTime ();
127
+ testUnthrottled (); // To ensure problem is specific to throttled events
134
128
testThrottleDisabled ();
135
129
testThrottledOff ();
136
130
testThottleZeroRate ();
@@ -140,6 +134,20 @@ public static void main(String[] args) throws Exception {
140
134
testThrottleUserdefined ();
141
135
}
142
136
137
+ private static void testUnthrottled () throws Exception {
138
+ testEvent (UnthrottledEvent .class , true );
139
+ }
140
+
141
+ private static Instant determineMinimumTime () throws IOException {
142
+ try (Recording r = new Recording ()) {
143
+ r .enable ("jdk.JVMInformation" );
144
+ r .start ();
145
+ Path p = Path .of ("start.jfr" );
146
+ r .dump (p );
147
+ return RecordingFile .readAllEvents (p ).get (0 ).getStartTime ();
148
+ }
149
+ }
150
+
143
151
private static void testThrottleDisabled () throws Exception {
144
152
testEvent (ThrottledDisabledEvent .class , false );
145
153
}
@@ -220,8 +228,6 @@ private static void testThrottleUserdefined(String test, String throttle, boolea
220
228
if (e5 .shouldCommit ()) {
221
229
e5 .commit ();
222
230
}
223
-
224
- r .stop ();
225
231
assertEvents (r , eventName , emit ? 5 : 0 );
226
232
}
227
233
}
@@ -272,14 +278,31 @@ private static void testEvent(Class<? extends Event> eventClass, boolean shouldC
272
278
273
279
private static void assertEvents (Recording r , String name , int expected ) throws Exception {
274
280
int count = 0 ;
275
- for (RecordedEvent event : Events .fromRecording (r )) {
281
+ r .stop ();
282
+ Duration d = Duration .between (r .getStartTime (), r .getStopTime ());
283
+ Path file = Path .of ("dump.jfr" );
284
+ r .dump (file );
285
+ for (RecordedEvent event : RecordingFile .readAllEvents (file )) {
276
286
if (event .getEventType ().getName ().equals (name )) {
277
287
count ++;
278
288
}
289
+ if (event .getDuration ().isNegative ()) {
290
+ System .out .println (event );
291
+ throw new Exception ("Unexpected negative duration" );
292
+ }
293
+ if (event .getStartTime ().isBefore (startTime )) {
294
+ System .out .println (event );
295
+ throw new Exception ("Unexpected early start time" );
296
+ }
297
+ if (event .getDuration ().toMillis () > 2 * d .toMillis ()) {
298
+ System .out .println (event );
299
+ throw new Exception ("Duration exceed twice the length of the recording" );
300
+ }
279
301
}
280
302
if (count != expected ) {
281
303
throw new Exception ("Expected " + expected + " " + name + " events, but found " + count );
282
304
}
305
+ Files .delete (file );
283
306
}
284
307
285
308
private static void assertShouldCommit (Event e , boolean expected ) throws Exception {
0 commit comments