Skip to content
25 changes: 6 additions & 19 deletions src/java.base/share/classes/java/io/FileInputStream.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1994, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1994, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -205,22 +205,17 @@ public int read() throws IOException {

private int traceRead0() throws IOException {
int result = 0;
boolean endOfFile = false;
long bytesRead = 0;
long start = 0;
long start = FileReadEvent.timestamp();
try {
start = FileReadEvent.timestamp();
result = read0();
if (result < 0) {
endOfFile = true;
bytesRead = -1;
} else {
bytesRead = 1;
}
} finally {
long duration = FileReadEvent.timestamp() - start;
if (FileReadEvent.shouldCommit(duration)) {
FileReadEvent.commit(start, duration, path, bytesRead, endOfFile);
}
FileReadEvent.offer(start, path, bytesRead);
}
return result;
}
Expand All @@ -236,19 +231,11 @@ private int traceRead0() throws IOException {

private int traceReadBytes(byte b[], int off, int len) throws IOException {
int bytesRead = 0;
long start = 0;
long start = FileReadEvent.timestamp();
try {
start = FileReadEvent.timestamp();
bytesRead = readBytes(b, off, len);
} finally {
long duration = FileReadEvent.timestamp() - start;
if (FileReadEvent.shouldCommit(duration)) {
if (bytesRead < 0) {
FileReadEvent.commit(start, duration, path, 0L, true);
} else {
FileReadEvent.commit(start, duration, path, bytesRead, false);
}
}
FileReadEvent.offer(start, path, bytesRead);
}
return bytesRead;
}
Expand Down
18 changes: 5 additions & 13 deletions src/java.base/share/classes/java/io/FileOutputStream.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1994, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1994, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -266,16 +266,12 @@ private void open(String name, boolean append) throws FileNotFoundException {

private void traceWrite(int b, boolean append) throws IOException {
long bytesWritten = 0;
long start = 0;
long start = FileWriteEvent.timestamp();
try {
start = FileWriteEvent.timestamp();
write(b, append);
bytesWritten = 1;
} finally {
long duration = FileWriteEvent.timestamp() - start;
if (FileWriteEvent.shouldCommit(duration)) {
FileWriteEvent.commit(start, duration, path, bytesWritten);
}
FileWriteEvent.offer(start, path, bytesWritten);
}
}

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

private void traceWriteBytes(byte b[], int off, int len, boolean append) throws IOException {
long bytesWritten = 0;
long start = 0;
long start = FileWriteEvent.timestamp();
try {
start = FileWriteEvent.timestamp();
writeBytes(b, off, len, append);
bytesWritten = len;
} finally {
long duration = FileWriteEvent.timestamp() - start;
if (FileWriteEvent.shouldCommit(duration)) {
FileWriteEvent.commit(start, duration, path, bytesWritten);
}
FileWriteEvent.offer(start, path, bytesWritten);
}
}

Expand Down
41 changes: 10 additions & 31 deletions src/java.base/share/classes/java/io/RandomAccessFile.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1994, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1994, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -367,21 +367,16 @@ public int read() throws IOException {
private int traceRead0() throws IOException {
int result = 0;
long bytesRead = 0;
boolean endOfFile = false;
long start = 0;
long start = FileReadEvent.timestamp();
try {
start = FileReadEvent.timestamp();
result = read0();
if (result < 0) {
endOfFile = true;
bytesRead = -1;
} else {
bytesRead = 1;
}
} finally {
long duration = FileReadEvent.timestamp() - start;
if (FileReadEvent.shouldCommit(duration)) {
FileReadEvent.commit(start, duration, path, bytesRead, endOfFile);
}
FileReadEvent.offer(start, path, bytesRead);
}
return result;
}
Expand All @@ -404,19 +399,11 @@ private int readBytes(byte[] b, int off, int len) throws IOException {

private int traceReadBytes0(byte b[], int off, int len) throws IOException {
int bytesRead = 0;
long start = 0;
long start = FileReadEvent.timestamp();
try {
start = FileReadEvent.timestamp();
bytesRead = readBytes0(b, off, len);
} finally {
long duration = FileReadEvent.timestamp() - start;
if (FileReadEvent.shouldCommit(duration)) {
if (bytesRead < 0) {
FileReadEvent.commit(start, duration, path, 0L, true);
} else {
FileReadEvent.commit(start, duration, path, bytesRead, false);
}
}
FileReadEvent.offer(start, path, bytesRead);
}
return bytesRead;
}
Expand Down Expand Up @@ -582,16 +569,12 @@ private void implWrite(int b) throws IOException {

private void traceImplWrite(int b) throws IOException {
long bytesWritten = 0;
long start = 0;
long start = FileWriteEvent.timestamp();
try {
start = FileWriteEvent.timestamp();
implWrite(b);
bytesWritten = 1;
} finally {
long duration = FileWriteEvent.timestamp() - start;
if (FileWriteEvent.shouldCommit(duration)) {
FileWriteEvent.commit(start, duration, path, bytesWritten);
}
FileWriteEvent.offer(start, path, bytesWritten);
}
}

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

private void traceImplWriteBytes(byte b[], int off, int len) throws IOException {
long bytesWritten = 0;
long start = 0;
long start = FileWriteEvent.timestamp();
try {
start = FileWriteEvent.timestamp();
implWriteBytes(b, off, len);
bytesWritten = len;
} finally {
long duration = FileWriteEvent.timestamp() - start;
if (FileWriteEvent.shouldCommit(duration)) {
FileWriteEvent.commit(start, duration, path, bytesWritten);
}
FileWriteEvent.offer(start, path, bytesWritten);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/java.base/share/classes/java/lang/Throwable.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1994, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1994, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -121,7 +121,7 @@ public class Throwable implements Serializable {
* Flag set by jdk.internal.event.JFRTracing to indicate if
* exceptions should be traced by JFR.
*/
static volatile boolean jfrTracing;
static boolean jfrTracing;

/**
* The JVM saves some indication of the stack backtrace in this slot.
Expand Down
10 changes: 2 additions & 8 deletions src/java.base/share/classes/java/net/Socket.java
Original file line number Diff line number Diff line change
Expand Up @@ -965,10 +965,7 @@ public int read(byte[] b, int off, int len) throws IOException {
}
long start = SocketReadEvent.timestamp();
int nbytes = implRead(b, off, len);
long duration = SocketReadEvent.timestamp() - start;
if (SocketReadEvent.shouldCommit(duration)) {
SocketReadEvent.emit(start, duration, nbytes, parent.getRemoteSocketAddress(), getSoTimeout());
}
SocketReadEvent.offer(start, nbytes, parent.getRemoteSocketAddress(), getSoTimeout());
return nbytes;
}

Expand Down Expand Up @@ -1081,10 +1078,7 @@ public void write(byte[] b, int off, int len) throws IOException {
}
long start = SocketWriteEvent.timestamp();
implWrite(b, off, len);
long duration = SocketWriteEvent.timestamp() - start;
if (SocketWriteEvent.shouldCommit(duration)) {
SocketWriteEvent.emit(start, duration, len, parent.getRemoteSocketAddress());
}
SocketWriteEvent.offer(start, len, parent.getRemoteSocketAddress());
}

private void implWrite(byte[] b, int off, int len) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -31,6 +31,11 @@ public final class ExceptionThrownEvent extends Event {
public String message;
public Class<?> thrownClass;

public static boolean shouldThrottleCommit(long timestamp) {
// Generated by JFR
return false;
}

public static void commit(long start, String message, Class<?> thrownClass) {
// Generated by JFR
}
Expand Down
26 changes: 24 additions & 2 deletions src/java.base/share/classes/jdk/internal/event/FileReadEvent.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -45,11 +45,33 @@ public static long timestamp() {
return 0L;
}

public static boolean shouldCommit(long duration) {
public static boolean shouldThrottleCommit(long duration, long end) {
// Generated by JFR
return false;
}

/**
* Helper method to offer the data needed to potentially commit an event.
* The duration of the operation is computed using the current
* timestamp and the given start time. If the duration meets
* or exceeds the configured value and is not throttled (determined by calling the
* generated method {@link #shouldThrottleCommit(long, long)}), an event will be
* emitted by calling {@link #commit(long, long, String, long, boolean)}
*
* @param start the start time
* @param path the path
* @param bytesRead the number of bytes that were read, or -1 if the end of the file was reached
*/
public static void offer(long start, String path, long bytesRead) {
long end = timestamp();
long duration = end - start;
if (shouldThrottleCommit(duration, end)) {
boolean endOfFile = bytesRead < 0;
long bytes = endOfFile ? 0 : bytesRead;
commit(start, duration, path, bytes, endOfFile);
}
}

public static void commit(long start, long duration, String path, long bytesRead, boolean endOfFile) {
// Generated by JFR
}
Expand Down
25 changes: 23 additions & 2 deletions src/java.base/share/classes/jdk/internal/event/FileWriteEvent.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -44,11 +44,32 @@ public static long timestamp() {
return 0L;
}

public static boolean shouldCommit(long duration) {
public static boolean shouldThrottleCommit(long duration, long end) {
// Generated by JFR
return false;
}

/**
* Helper method to offer the data needed to potentially commit an event.
* The duration of the operation is computed using the current
* timestamp and the given start time. If the duration meets
* or exceeds the configured value and is not throttled (determined by calling the
* generated method {@link #shouldThrottleCommit(long, long)}), an event will be
* emitted by calling {@link #commit(long, long, String, long)}
*
* @param start the start time
* @param path the path
* @param bytesRead the number of bytes that were written, or -1 if the end of the file was reached
*/
public static void offer(long start, String path, long bytesWritten) {
long end = timestamp();
long duration = end - start;
if (shouldThrottleCommit(duration, end)) {
long bytes = bytesWritten > 0 ? bytesWritten : 0;
commit(start, duration, path, bytes);
}
}

public static void commit(long start, long duration, String path, long bytesWritten) {
// Generated by JFR
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -74,9 +74,10 @@ public static void commit(long start, long duration, String host, String address
* of this method is generated automatically if jfr is enabled.
*
* @param duration time in nanoseconds to complete the operation
* @param end timestamp at the end of the operation
* @return true if the event should be commited
*/
public static boolean shouldCommit(long duration) {
public static boolean shouldThrottleCommit(long duration, long end) {
// Generated by JFR
return false;
}
Expand Down Expand Up @@ -118,8 +119,9 @@ public static long timestamp() {
* @param timeout maximum time to wait
*/
public static void offer(long start, long nbytes, SocketAddress remote, long timeout) {
long duration = timestamp() - start;
if (shouldCommit(duration)) {
long end = timestamp();
long duration = end - start;
if (shouldThrottleCommit(duration, end)) {
emit(start, duration, nbytes, remote, timeout);
}
}
Expand Down
Loading