Skip to content

Commit eb770a0

Browse files
committed
8351594: JFR: Rate-limited sampling of Java events
Reviewed-by: mgronlun, alanb
1 parent c5daf89 commit eb770a0

36 files changed

+1120
-238
lines changed

src/java.base/share/classes/java/io/FileInputStream.java

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1994, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1994, 2025, 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
@@ -205,22 +205,17 @@ public int read() throws IOException {
205205

206206
private int traceRead0() throws IOException {
207207
int result = 0;
208-
boolean endOfFile = false;
209208
long bytesRead = 0;
210-
long start = 0;
209+
long start = FileReadEvent.timestamp();
211210
try {
212-
start = FileReadEvent.timestamp();
213211
result = read0();
214212
if (result < 0) {
215-
endOfFile = true;
213+
bytesRead = -1;
216214
} else {
217215
bytesRead = 1;
218216
}
219217
} finally {
220-
long duration = FileReadEvent.timestamp() - start;
221-
if (FileReadEvent.shouldCommit(duration)) {
222-
FileReadEvent.commit(start, duration, path, bytesRead, endOfFile);
223-
}
218+
FileReadEvent.offer(start, path, bytesRead);
224219
}
225220
return result;
226221
}
@@ -236,19 +231,11 @@ private int traceRead0() throws IOException {
236231

237232
private int traceReadBytes(byte b[], int off, int len) throws IOException {
238233
int bytesRead = 0;
239-
long start = 0;
234+
long start = FileReadEvent.timestamp();
240235
try {
241-
start = FileReadEvent.timestamp();
242236
bytesRead = readBytes(b, off, len);
243237
} finally {
244-
long duration = FileReadEvent.timestamp() - start;
245-
if (FileReadEvent.shouldCommit(duration)) {
246-
if (bytesRead < 0) {
247-
FileReadEvent.commit(start, duration, path, 0L, true);
248-
} else {
249-
FileReadEvent.commit(start, duration, path, bytesRead, false);
250-
}
251-
}
238+
FileReadEvent.offer(start, path, bytesRead);
252239
}
253240
return bytesRead;
254241
}

src/java.base/share/classes/java/io/FileOutputStream.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1994, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1994, 2025, 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
@@ -266,16 +266,12 @@ private void open(String name, boolean append) throws FileNotFoundException {
266266

267267
private void traceWrite(int b, boolean append) throws IOException {
268268
long bytesWritten = 0;
269-
long start = 0;
269+
long start = FileWriteEvent.timestamp();
270270
try {
271-
start = FileWriteEvent.timestamp();
272271
write(b, append);
273272
bytesWritten = 1;
274273
} finally {
275-
long duration = FileWriteEvent.timestamp() - start;
276-
if (FileWriteEvent.shouldCommit(duration)) {
277-
FileWriteEvent.commit(start, duration, path, bytesWritten);
278-
}
274+
FileWriteEvent.offer(start, path, bytesWritten);
279275
}
280276
}
281277

@@ -310,16 +306,12 @@ private native void writeBytes(byte[] b, int off, int len, boolean append)
310306

311307
private void traceWriteBytes(byte b[], int off, int len, boolean append) throws IOException {
312308
long bytesWritten = 0;
313-
long start = 0;
309+
long start = FileWriteEvent.timestamp();
314310
try {
315-
start = FileWriteEvent.timestamp();
316311
writeBytes(b, off, len, append);
317312
bytesWritten = len;
318313
} finally {
319-
long duration = FileWriteEvent.timestamp() - start;
320-
if (FileWriteEvent.shouldCommit(duration)) {
321-
FileWriteEvent.commit(start, duration, path, bytesWritten);
322-
}
314+
FileWriteEvent.offer(start, path, bytesWritten);
323315
}
324316
}
325317

src/java.base/share/classes/java/io/RandomAccessFile.java

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1994, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1994, 2025, 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
@@ -367,21 +367,16 @@ public int read() throws IOException {
367367
private int traceRead0() throws IOException {
368368
int result = 0;
369369
long bytesRead = 0;
370-
boolean endOfFile = false;
371-
long start = 0;
370+
long start = FileReadEvent.timestamp();
372371
try {
373-
start = FileReadEvent.timestamp();
374372
result = read0();
375373
if (result < 0) {
376-
endOfFile = true;
374+
bytesRead = -1;
377375
} else {
378376
bytesRead = 1;
379377
}
380378
} finally {
381-
long duration = FileReadEvent.timestamp() - start;
382-
if (FileReadEvent.shouldCommit(duration)) {
383-
FileReadEvent.commit(start, duration, path, bytesRead, endOfFile);
384-
}
379+
FileReadEvent.offer(start, path, bytesRead);
385380
}
386381
return result;
387382
}
@@ -404,19 +399,11 @@ private int readBytes(byte[] b, int off, int len) throws IOException {
404399

405400
private int traceReadBytes0(byte b[], int off, int len) throws IOException {
406401
int bytesRead = 0;
407-
long start = 0;
402+
long start = FileReadEvent.timestamp();
408403
try {
409-
start = FileReadEvent.timestamp();
410404
bytesRead = readBytes0(b, off, len);
411405
} finally {
412-
long duration = FileReadEvent.timestamp() - start;
413-
if (FileReadEvent.shouldCommit(duration)) {
414-
if (bytesRead < 0) {
415-
FileReadEvent.commit(start, duration, path, 0L, true);
416-
} else {
417-
FileReadEvent.commit(start, duration, path, bytesRead, false);
418-
}
419-
}
406+
FileReadEvent.offer(start, path, bytesRead);
420407
}
421408
return bytesRead;
422409
}
@@ -582,16 +569,12 @@ private void implWrite(int b) throws IOException {
582569

583570
private void traceImplWrite(int b) throws IOException {
584571
long bytesWritten = 0;
585-
long start = 0;
572+
long start = FileWriteEvent.timestamp();
586573
try {
587-
start = FileWriteEvent.timestamp();
588574
implWrite(b);
589575
bytesWritten = 1;
590576
} finally {
591-
long duration = FileWriteEvent.timestamp() - start;
592-
if (FileWriteEvent.shouldCommit(duration)) {
593-
FileWriteEvent.commit(start, duration, path, bytesWritten);
594-
}
577+
FileWriteEvent.offer(start, path, bytesWritten);
595578
}
596579
}
597580

@@ -624,16 +607,12 @@ private void implWriteBytes(byte[] b, int off, int len) throws IOException {
624607

625608
private void traceImplWriteBytes(byte b[], int off, int len) throws IOException {
626609
long bytesWritten = 0;
627-
long start = 0;
610+
long start = FileWriteEvent.timestamp();
628611
try {
629-
start = FileWriteEvent.timestamp();
630612
implWriteBytes(b, off, len);
631613
bytesWritten = len;
632614
} finally {
633-
long duration = FileWriteEvent.timestamp() - start;
634-
if (FileWriteEvent.shouldCommit(duration)) {
635-
FileWriteEvent.commit(start, duration, path, bytesWritten);
636-
}
615+
FileWriteEvent.offer(start, path, bytesWritten);
637616
}
638617
}
639618

src/java.base/share/classes/java/lang/Throwable.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1994, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1994, 2025, 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
@@ -121,7 +121,7 @@ public class Throwable implements Serializable {
121121
* Flag set by jdk.internal.event.JFRTracing to indicate if
122122
* exceptions should be traced by JFR.
123123
*/
124-
static volatile boolean jfrTracing;
124+
static boolean jfrTracing;
125125

126126
/**
127127
* The JVM saves some indication of the stack backtrace in this slot.

src/java.base/share/classes/java/net/Socket.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -965,10 +965,7 @@ public int read(byte[] b, int off, int len) throws IOException {
965965
}
966966
long start = SocketReadEvent.timestamp();
967967
int nbytes = implRead(b, off, len);
968-
long duration = SocketReadEvent.timestamp() - start;
969-
if (SocketReadEvent.shouldCommit(duration)) {
970-
SocketReadEvent.emit(start, duration, nbytes, parent.getRemoteSocketAddress(), getSoTimeout());
971-
}
968+
SocketReadEvent.offer(start, nbytes, parent.getRemoteSocketAddress(), getSoTimeout());
972969
return nbytes;
973970
}
974971

@@ -1081,10 +1078,7 @@ public void write(byte[] b, int off, int len) throws IOException {
10811078
}
10821079
long start = SocketWriteEvent.timestamp();
10831080
implWrite(b, off, len);
1084-
long duration = SocketWriteEvent.timestamp() - start;
1085-
if (SocketWriteEvent.shouldCommit(duration)) {
1086-
SocketWriteEvent.emit(start, duration, len, parent.getRemoteSocketAddress());
1087-
}
1081+
SocketWriteEvent.offer(start, len, parent.getRemoteSocketAddress());
10881082
}
10891083

10901084
private void implWrite(byte[] b, int off, int len) throws IOException {

src/java.base/share/classes/jdk/internal/event/ExceptionThrownEvent.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2023, 2025, 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
@@ -31,6 +31,11 @@ public final class ExceptionThrownEvent extends Event {
3131
public String message;
3232
public Class<?> thrownClass;
3333

34+
public static boolean shouldThrottleCommit(long timestamp) {
35+
// Generated by JFR
36+
return false;
37+
}
38+
3439
public static void commit(long start, String message, Class<?> thrownClass) {
3540
// Generated by JFR
3641
}

src/java.base/share/classes/jdk/internal/event/FileReadEvent.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2024, 2025, 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
@@ -45,11 +45,33 @@ public static long timestamp() {
4545
return 0L;
4646
}
4747

48-
public static boolean shouldCommit(long duration) {
48+
public static boolean shouldThrottleCommit(long duration, long end) {
4949
// Generated by JFR
5050
return false;
5151
}
5252

53+
/**
54+
* Helper method to offer the data needed to potentially commit an event.
55+
* The duration of the operation is computed using the current
56+
* timestamp and the given start time. If the duration meets
57+
* or exceeds the configured value and is not throttled (determined by calling the
58+
* generated method {@link #shouldThrottleCommit(long, long)}), an event will be
59+
* emitted by calling {@link #commit(long, long, String, long, boolean)}
60+
*
61+
* @param start the start time
62+
* @param path the path
63+
* @param bytesRead the number of bytes that were read, or -1 if the end of the file was reached
64+
*/
65+
public static void offer(long start, String path, long bytesRead) {
66+
long end = timestamp();
67+
long duration = end - start;
68+
if (shouldThrottleCommit(duration, end)) {
69+
boolean endOfFile = bytesRead < 0;
70+
long bytes = endOfFile ? 0 : bytesRead;
71+
commit(start, duration, path, bytes, endOfFile);
72+
}
73+
}
74+
5375
public static void commit(long start, long duration, String path, long bytesRead, boolean endOfFile) {
5476
// Generated by JFR
5577
}

src/java.base/share/classes/jdk/internal/event/FileWriteEvent.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2023, 2025, 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
@@ -44,11 +44,32 @@ public static long timestamp() {
4444
return 0L;
4545
}
4646

47-
public static boolean shouldCommit(long duration) {
47+
public static boolean shouldThrottleCommit(long duration, long end) {
4848
// Generated by JFR
4949
return false;
5050
}
5151

52+
/**
53+
* Helper method to offer the data needed to potentially commit an event.
54+
* The duration of the operation is computed using the current
55+
* timestamp and the given start time. If the duration meets
56+
* or exceeds the configured value and is not throttled (determined by calling the
57+
* generated method {@link #shouldThrottleCommit(long, long)}), an event will be
58+
* emitted by calling {@link #commit(long, long, String, long)}
59+
*
60+
* @param start the start time
61+
* @param path the path
62+
* @param bytesRead the number of bytes that were written, or -1 if the end of the file was reached
63+
*/
64+
public static void offer(long start, String path, long bytesWritten) {
65+
long end = timestamp();
66+
long duration = end - start;
67+
if (shouldThrottleCommit(duration, end)) {
68+
long bytes = bytesWritten > 0 ? bytesWritten : 0;
69+
commit(start, duration, path, bytes);
70+
}
71+
}
72+
5273
public static void commit(long start, long duration, String path, long bytesWritten) {
5374
// Generated by JFR
5475
}

src/java.base/share/classes/jdk/internal/event/SocketReadEvent.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2023, 2025, 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
@@ -74,9 +74,10 @@ public static void commit(long start, long duration, String host, String address
7474
* of this method is generated automatically if jfr is enabled.
7575
*
7676
* @param duration time in nanoseconds to complete the operation
77+
* @param end timestamp at the end of the operation
7778
* @return true if the event should be commited
7879
*/
79-
public static boolean shouldCommit(long duration) {
80+
public static boolean shouldThrottleCommit(long duration, long end) {
8081
// Generated by JFR
8182
return false;
8283
}
@@ -118,8 +119,9 @@ public static long timestamp() {
118119
* @param timeout maximum time to wait
119120
*/
120121
public static void offer(long start, long nbytes, SocketAddress remote, long timeout) {
121-
long duration = timestamp() - start;
122-
if (shouldCommit(duration)) {
122+
long end = timestamp();
123+
long duration = end - start;
124+
if (shouldThrottleCommit(duration, end)) {
123125
emit(start, duration, nbytes, remote, timeout);
124126
}
125127
}

0 commit comments

Comments
 (0)