8331876: JFR: Move file read and write events to java.base

Reviewed-by: mgronlun, alanb
This commit is contained in:
Erik Gahlin 2024-05-30 13:32:57 +00:00
parent f608918df3
commit 4a20691e9b
20 changed files with 551 additions and 662 deletions

View file

@ -28,6 +28,7 @@ package java.io;
import java.nio.channels.FileChannel;
import jdk.internal.access.SharedSecrets;
import jdk.internal.access.JavaIOFileDescriptorAccess;
import jdk.internal.event.FileWriteEvent;
import sun.nio.ch.FileChannelImpl;
@ -68,6 +69,12 @@ public class FileOutputStream extends OutputStream
private static final JavaIOFileDescriptorAccess FD_ACCESS =
SharedSecrets.getJavaIOFileDescriptorAccess();
/**
* Flag set by jdk.internal.event.JFRTracing to indicate if
* file writes should be traced by JFR.
*/
private static boolean jfrTracing;
/**
* The system dependent file descriptor.
*/
@ -297,6 +304,21 @@ public class FileOutputStream extends OutputStream
*/
private native void write(int b, boolean append) throws IOException;
private void traceWrite(int b, boolean append) throws IOException {
long bytesWritten = 0;
long start = 0;
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);
}
}
}
/**
* Writes the specified byte to this file output stream. Implements
* the {@code write} method of {@code OutputStream}.
@ -307,6 +329,10 @@ public class FileOutputStream extends OutputStream
@Override
public void write(int b) throws IOException {
boolean append = FD_ACCESS.getAppend(fd);
if (jfrTracing && FileWriteEvent.enabled()) {
traceWrite(b, append);
return;
}
write(b, append);
}
@ -322,6 +348,21 @@ public class FileOutputStream extends OutputStream
private native void writeBytes(byte[] b, int off, int len, boolean append)
throws IOException;
private void traceWriteBytes(byte b[], int off, int len, boolean append) throws IOException {
long bytesWritten = 0;
long start = 0;
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);
}
}
}
/**
* Writes {@code b.length} bytes from the specified byte array
* to this file output stream.
@ -332,6 +373,10 @@ public class FileOutputStream extends OutputStream
@Override
public void write(byte[] b) throws IOException {
boolean append = FD_ACCESS.getAppend(fd);
if (jfrTracing && FileWriteEvent.enabled()) {
traceWriteBytes(b, 0, b.length, append);
return;
}
writeBytes(b, 0, b.length, append);
}
@ -348,6 +393,10 @@ public class FileOutputStream extends OutputStream
@Override
public void write(byte[] b, int off, int len) throws IOException {
boolean append = FD_ACCESS.getAppend(fd);
if (jfrTracing && FileWriteEvent.enabled()) {
traceWriteBytes(b, off, len, append);
return;
}
writeBytes(b, off, len, append);
}