Skip to content

Commit 9353899

Browse files
committed
8298175: JFR: Common timestamp for periodic events
Reviewed-by: dholmes, mgronlun
1 parent 94575d1 commit 9353899

File tree

6 files changed

+45
-23
lines changed

6 files changed

+45
-23
lines changed

make/src/classes/build/tools/jfr/GenerateJfrFiles.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -573,9 +573,13 @@ private static void printJfrPeriodicHpp(Metadata metadata, File outputFile) thro
573573
out.write("#include \"jfrfiles/jfrEventIds.hpp\"");
574574
out.write("#include \"memory/allocation.hpp\"");
575575
out.write("");
576+
out.write("enum PeriodicType {BEGIN_CHUNK, INTERVAL, END_CHUNK};");
577+
out.write("");
576578
out.write("class JfrPeriodicEventSet : public AllStatic {");
577579
out.write(" public:");
578-
out.write(" static void requestEvent(JfrEventId id) {");
580+
out.write(" static void requestEvent(JfrEventId id, jlong timestamp, PeriodicType periodicType) {");
581+
out.write(" _timestamp = Ticks(timestamp);");
582+
out.write(" _type = periodicType;");
579583
out.write(" switch(id) {");
580584
out.write(" ");
581585
for (TypeElement e : metadata.getPeriodicEvents()) {
@@ -595,6 +599,10 @@ private static void printJfrPeriodicHpp(Metadata metadata, File outputFile) thro
595599
out.write(" static void request" + e.name + "(void);");
596600
out.write("");
597601
}
602+
out.write(" static Ticks timestamp(void);");
603+
out.write(" static Ticks _timestamp;");
604+
out.write(" static PeriodicType type(void);");
605+
out.write(" static PeriodicType _type;");
598606
out.write("};");
599607
out.write("");
600608
out.write("#endif // INCLUDE_JFR");

src/hotspot/share/jfr/jni/jfrJniMethod.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,8 @@ JVM_ENTRY_NO_ENV(void, jfr_mark_chunk_final(JNIEnv * env, jobject jvm))
238238
JfrRepository::mark_chunk_final();
239239
JVM_END
240240

241-
JVM_ENTRY_NO_ENV(jboolean, jfr_emit_event(JNIEnv* env, jobject jvm, jlong eventTypeId, jlong timeStamp, jlong when))
242-
JfrPeriodicEventSet::requestEvent((JfrEventId)eventTypeId);
241+
JVM_ENTRY_NO_ENV(jboolean, jfr_emit_event(JNIEnv* env, jobject jvm, jlong event_type_id, jlong timestamp, jlong periodic_type))
242+
JfrPeriodicEventSet::requestEvent((JfrEventId)event_type_id, timestamp, static_cast<PeriodicType>(periodic_type));
243243
return thread->has_pending_exception() ? JNI_FALSE : JNI_TRUE;
244244
JVM_END
245245

src/hotspot/share/jfr/periodic/jfrPeriodic.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@
8181
*/
8282
#define TRACE_REQUEST_FUNC(id) void JfrPeriodicEventSet::request##id(void)
8383

84+
// Timestamp to correlate events in the same batch/generation
85+
Ticks JfrPeriodicEventSet::_timestamp;
86+
PeriodicType JfrPeriodicEventSet::_type;
87+
88+
Ticks JfrPeriodicEventSet::timestamp(void) {
89+
return _timestamp;
90+
}
91+
92+
PeriodicType JfrPeriodicEventSet::type(void) {
93+
return _type;
94+
}
95+
8496
TRACE_REQUEST_FUNC(JVMInformation) {
8597
ResourceMark rm;
8698
EventJVMInformation event;

src/hotspot/share/utilities/ticks.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ class TimeInstant : public Rep<TimeSource> {
231231
friend class GranularTimer;
232232
friend class ObjectSample;
233233
friend class EventEmitter;
234+
friend class JfrPeriodicEventSet;
234235
// GC unit tests
235236
friend class TimePartitionsTest;
236237
friend class GCTimerTest;

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,11 @@ private JVM() {
118118
* @param eventTypeId type id
119119
*
120120
* @param timestamp commit time for event
121-
* @param when when it is being done {@link Periodic.When}
121+
* @param periodicType when it is being done {@link PeriodicType.When}
122122
*
123123
* @return true if the event was committed
124124
*/
125-
public native boolean emitEvent(long eventTypeId, long timestamp, long when);
125+
public native boolean emitEvent(long eventTypeId, long timestamp, long periodicType);
126126

127127
/**
128128
* Return a list of all classes deriving from {@link jdk.internal.event.Event}

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

+19-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
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.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -39,6 +39,9 @@
3939
import jdk.jfr.EventType;
4040

4141
public final class RequestEngine {
42+
enum PeriodicType {
43+
BEGIN_CHUNK, INTERVAL, END_CHUNK
44+
}
4245

4346
private static final JVM jvm = JVM.getJVM();
4447
private static final ReentrantLock lock = new ReentrantLock();
@@ -62,13 +65,13 @@ private RequestHook(@SuppressWarnings("removal") AccessControlContext acc, Platf
6265
this(null, eventType, null);
6366
}
6467

65-
private void execute() {
68+
private void execute(long timestamp, PeriodicType periodicType) {
6669
try {
6770
if (accessControllerContext == null) { // native
6871
if (type.isJDK()) {
6972
hook.run();
7073
} else {
71-
emitJVMEvent(type);
74+
emitJVMEvent(type, timestamp, periodicType);
7275
}
7376
if (Logger.shouldLog(LogTag.JFR_SYSTEM, LogLevel.DEBUG)) {
7477
Logger.log(LogTag.JFR_SYSTEM, LogLevel.DEBUG, "Executed periodic hook for " + type.getLogName());
@@ -82,13 +85,13 @@ private void execute() {
8285
}
8386
}
8487

85-
private void emitJVMEvent(PlatformEventType type) {
88+
private void emitJVMEvent(PlatformEventType type, long timestamp, PeriodicType periodicType) {
8689
try {
8790
// There should only be one thread in native at a time.
8891
// ReentrantLock is used to avoid JavaMonitorBlocked event
8992
// from synchronized block.
9093
lock.lock();
91-
jvm.emitEvent(type.getId(), JVM.counterTime(), 0);
94+
jvm.emitEvent(type.getId(), timestamp, periodicType.ordinal());
9295
} finally {
9396
lock.unlock();
9497
}
@@ -183,35 +186,33 @@ static void addHooks(List<RequestHook> newEntries) {
183186
}
184187

185188
static void doChunkEnd() {
186-
doChunk(x -> x.isEndChunk());
189+
doChunk(x -> x.isEndChunk(), PeriodicType.END_CHUNK);
187190
}
188191

189192
static void doChunkBegin() {
190-
doChunk(x -> x.isBeginChunk());
193+
doChunk(x -> x.isBeginChunk(), PeriodicType.BEGIN_CHUNK);
191194
}
192195

193-
private static void doChunk(Predicate<PlatformEventType> predicate) {
196+
private static void doChunk(Predicate<PlatformEventType> predicate, PeriodicType type) {
197+
long timestamp = JVM.counterTime();
194198
for (RequestHook requestHook : entries) {
195199
PlatformEventType s = requestHook.type;
196200
if (s.isEnabled() && predicate.test(s)) {
197-
requestHook.execute();
201+
requestHook.execute(timestamp, type);
198202
}
199203
}
200204
}
201205

202206
static long doPeriodic() {
203-
return run_requests(entries);
207+
return run_requests(entries, JVM.counterTime());
204208
}
205209

206210
// 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) {
208212
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.
215216
long now = System.currentTimeMillis();
216217
long min = 0;
217218
long delta = 0;
@@ -249,7 +250,7 @@ private static long run_requests(Collection<RequestHook> entries) {
249250
// Bug 9000556 - don't try to compensate
250251
// for wait > period
251252
r_delta = 0;
252-
he.execute();
253+
he.execute(eventTimestamp, PeriodicType.INTERVAL);
253254
}
254255

255256
// calculate time left

0 commit comments

Comments
 (0)