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 java.util.Arrays;
import jdk.internal.util.ArraysSupport;
import jdk.internal.event.FileReadEvent;
import sun.nio.ch.FileChannelImpl;
/**
@ -59,6 +60,12 @@ public class FileInputStream extends InputStream
{
private static final int DEFAULT_BUFFER_SIZE = 8192;
/**
* Flag set by jdk.internal.event.JFRTracing to indicate if
* file reads should be traced by JFR.
*/
private static boolean jfrTracing;
/* File Descriptor - handle to the open file */
private final FileDescriptor fd;
@ -222,11 +229,36 @@ public class FileInputStream extends InputStream
*/
@Override
public int read() throws IOException {
if (jfrTracing && FileReadEvent.enabled()) {
return traceRead0();
}
return read0();
}
private native int read0() throws IOException;
private int traceRead0() throws IOException {
int result = 0;
boolean endOfFile = false;
long bytesRead = 0;
long start = 0;
try {
start = FileReadEvent.timestamp();
result = read0();
if (result < 0) {
endOfFile = true;
} else {
bytesRead = 1;
}
} finally {
long duration = FileReadEvent.timestamp() - start;
if (FileReadEvent.shouldCommit(duration)) {
FileReadEvent.commit(start, duration, path, bytesRead, endOfFile);
}
}
return result;
}
/**
* Reads a subarray as a sequence of bytes.
* @param b the data to be written
@ -236,6 +268,25 @@ public class FileInputStream extends InputStream
*/
private native int readBytes(byte[] b, int off, int len) throws IOException;
private int traceReadBytes(byte b[], int off, int len) throws IOException {
int bytesRead = 0;
long start = 0;
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);
}
}
}
return bytesRead;
}
/**
* Reads up to {@code b.length} bytes of data from this input
* stream into an array of bytes. This method blocks until some input
@ -249,6 +300,9 @@ public class FileInputStream extends InputStream
*/
@Override
public int read(byte[] b) throws IOException {
if (jfrTracing && FileReadEvent.enabled()) {
return traceReadBytes(b, 0, b.length);
}
return readBytes(b, 0, b.length);
}
@ -268,6 +322,9 @@ public class FileInputStream extends InputStream
*/
@Override
public int read(byte[] b, int off, int len) throws IOException {
if (jfrTracing && FileReadEvent.enabled()) {
return traceReadBytes(b, off, len);
}
return readBytes(b, off, len);
}