1
1
/*
2
- * Copyright (c) 2016, 2021 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2016, 2022 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
39
39
import jdk .jfr .EventType ;
40
40
41
41
public final class RequestEngine {
42
+ enum PeriodicType {
43
+ BEGIN_CHUNK , INTERVAL , END_CHUNK
44
+ }
42
45
43
46
private static final JVM jvm = JVM .getJVM ();
44
47
private static final ReentrantLock lock = new ReentrantLock ();
@@ -62,13 +65,13 @@ private RequestHook(@SuppressWarnings("removal") AccessControlContext acc, Platf
62
65
this (null , eventType , null );
63
66
}
64
67
65
- private void execute () {
68
+ private void execute (long timestamp , PeriodicType periodicType ) {
66
69
try {
67
70
if (accessControllerContext == null ) { // native
68
71
if (type .isJDK ()) {
69
72
hook .run ();
70
73
} else {
71
- emitJVMEvent (type );
74
+ emitJVMEvent (type , timestamp , periodicType );
72
75
}
73
76
if (Logger .shouldLog (LogTag .JFR_SYSTEM , LogLevel .DEBUG )) {
74
77
Logger .log (LogTag .JFR_SYSTEM , LogLevel .DEBUG , "Executed periodic hook for " + type .getLogName ());
@@ -82,13 +85,13 @@ private void execute() {
82
85
}
83
86
}
84
87
85
- private void emitJVMEvent (PlatformEventType type ) {
88
+ private void emitJVMEvent (PlatformEventType type , long timestamp , PeriodicType periodicType ) {
86
89
try {
87
90
// There should only be one thread in native at a time.
88
91
// ReentrantLock is used to avoid JavaMonitorBlocked event
89
92
// from synchronized block.
90
93
lock .lock ();
91
- jvm .emitEvent (type .getId (), JVM . counterTime (), 0 );
94
+ jvm .emitEvent (type .getId (), timestamp , periodicType . ordinal () );
92
95
} finally {
93
96
lock .unlock ();
94
97
}
@@ -183,35 +186,33 @@ static void addHooks(List<RequestHook> newEntries) {
183
186
}
184
187
185
188
static void doChunkEnd () {
186
- doChunk (x -> x .isEndChunk ());
189
+ doChunk (x -> x .isEndChunk (), PeriodicType . END_CHUNK );
187
190
}
188
191
189
192
static void doChunkBegin () {
190
- doChunk (x -> x .isBeginChunk ());
193
+ doChunk (x -> x .isBeginChunk (), PeriodicType . BEGIN_CHUNK );
191
194
}
192
195
193
- private static void doChunk (Predicate <PlatformEventType > predicate ) {
196
+ private static void doChunk (Predicate <PlatformEventType > predicate , PeriodicType type ) {
197
+ long timestamp = JVM .counterTime ();
194
198
for (RequestHook requestHook : entries ) {
195
199
PlatformEventType s = requestHook .type ;
196
200
if (s .isEnabled () && predicate .test (s )) {
197
- requestHook .execute ();
201
+ requestHook .execute (timestamp , type );
198
202
}
199
203
}
200
204
}
201
205
202
206
static long doPeriodic () {
203
- return run_requests (entries );
207
+ return run_requests (entries , JVM . counterTime () );
204
208
}
205
209
206
210
// code copied from native impl.
207
- private static long run_requests (Collection <RequestHook > entries ) {
211
+ private static long run_requests (Collection <RequestHook > entries , long eventTimestamp ) {
208
212
long last = lastTimeMillis ;
209
- // Bug 9000556 - current time millis has rather lame resolution
210
- // The use of os::elapsed_counter() is deliberate here, we don't
211
- // want it exchanged for os::ft_elapsed_counter().
212
- // Keeping direct call os::elapsed_counter() here for reliable
213
- // real time values in order to decide when registered requestable
214
- // events are due.
213
+ // The interval for periodic events is typically at least 1 s, so
214
+ // System.currentTimeMillis() is sufficient. JVM.counterTime() lacks
215
+ // unit and has in the past been more unreliable.
215
216
long now = System .currentTimeMillis ();
216
217
long min = 0 ;
217
218
long delta = 0 ;
@@ -249,7 +250,7 @@ private static long run_requests(Collection<RequestHook> entries) {
249
250
// Bug 9000556 - don't try to compensate
250
251
// for wait > period
251
252
r_delta = 0 ;
252
- he .execute ();
253
+ he .execute (eventTimestamp , PeriodicType . INTERVAL );
253
254
}
254
255
255
256
// calculate time left
0 commit comments