Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/java.base/share/classes/java/io/FileInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.nio.channels.FileChannel;
import java.util.Arrays;
import jdk.internal.util.ArraysSupport;
import jdk.internal.event.FileReadEvent;
import sun.nio.ch.FileChannelImpl;

/**
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -222,11 +229,36 @@ private void open(String name) throws FileNotFoundException {
*/
@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
Expand All @@ -236,6 +268,25 @@ public int read() throws IOException {
*/
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
Expand All @@ -249,6 +300,9 @@ public int read() throws IOException {
*/
@Override
public int read(byte[] b) throws IOException {
if (jfrTracing && FileReadEvent.enabled()) {
return traceReadBytes(b, 0, b.length);
}
return readBytes(b, 0, b.length);
}

Expand All @@ -268,6 +322,9 @@ public int read(byte[] b) throws IOException {
*/
@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);
}

Expand Down
49 changes: 49 additions & 0 deletions src/java.base/share/classes/java/io/FileOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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;


Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -297,6 +304,21 @@ private void open(String name, boolean append) throws FileNotFoundException {
*/
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}.
Expand All @@ -307,6 +329,10 @@ private void open(String name, boolean append) throws FileNotFoundException {
@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);
}

Expand All @@ -322,6 +348,21 @@ public void write(int b) throws IOException {
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.
Expand All @@ -332,6 +373,10 @@ private native void writeBytes(byte[] b, int off, int len, boolean append)
@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);
}

Expand All @@ -348,6 +393,10 @@ public void write(byte[] b) throws IOException {
@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);
}

Expand Down
101 changes: 101 additions & 0 deletions src/java.base/share/classes/java/io/RandomAccessFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import jdk.internal.access.SharedSecrets;
import jdk.internal.misc.Blocker;
import jdk.internal.util.ByteArray;
import jdk.internal.event.FileReadEvent;
import jdk.internal.event.FileWriteEvent;
import sun.nio.ch.FileChannelImpl;


Expand Down Expand Up @@ -68,6 +70,12 @@ public class RandomAccessFile implements DataOutput, DataInput, Closeable {
private static final int O_DSYNC = 8;
private static final int O_TEMPORARY = 16;

/**
* Flag set by jdk.internal.event.JFRTracing to indicate if
* file reads and writes should be traced by JFR.
*/
private static boolean jfrTracing;

private final FileDescriptor fd;

private final boolean rw;
Expand Down Expand Up @@ -376,11 +384,36 @@ private void open(String name, int mode) throws FileNotFoundException {
* end-of-file has been reached.
*/
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;
long bytesRead = 0;
boolean endOfFile = false;
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 sub array as a sequence of bytes.
* @param b the buffer into which the data is read.
Expand All @@ -389,11 +422,33 @@ public int read() throws IOException {
* @throws IOException If an I/O error has occurred.
*/
private int readBytes(byte[] b, int off, int len) throws IOException {
if (jfrTracing && FileReadEvent.enabled()) {
return traceReadBytes0(b, off, len);
}
return readBytes0(b, off, len);
}

private native int readBytes0(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;
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);
}
}
}
return bytesRead;
}

/**
* Reads up to {@code len} bytes of data from this file into an
* array of bytes. This method blocks until at least one byte of input
Expand Down Expand Up @@ -537,6 +592,14 @@ public int skipBytes(int n) throws IOException {
* @throws IOException if an I/O error occurs.
*/
public void write(int b) throws IOException {
if (jfrTracing && FileWriteEvent.enabled()) {
traceImplWrite(b);
return;
}
implWrite(b);
}

private void implWrite(int b) throws IOException {
boolean attempted = Blocker.begin(sync);
try {
write0(b);
Expand All @@ -545,6 +608,21 @@ public void write(int b) throws IOException {
}
}

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

private native void write0(int b) throws IOException;

/**
Expand All @@ -556,6 +634,14 @@ public void write(int b) throws IOException {
* @throws IOException If an I/O error has occurred.
*/
private void writeBytes(byte[] b, int off, int len) throws IOException {
if (jfrTracing && FileWriteEvent.enabled()) {
traceImplWriteBytes(b, off, len);
return;
}
implWriteBytes(b, off, len);
}

private void implWriteBytes(byte[] b, int off, int len) throws IOException {
boolean attempted = Blocker.begin(sync);
try {
writeBytes0(b, off, len);
Expand All @@ -564,6 +650,21 @@ private void writeBytes(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;
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);
}
}
}

private native void writeBytes0(byte[] b, int off, int len) throws IOException;

/**
Expand Down
3 changes: 2 additions & 1 deletion src/java.base/share/classes/java/lang/Throwable.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ public class Throwable implements Serializable {
private static final long serialVersionUID = -3042686055658047285L;

/**
* Flag that determines if exceptions should be traced by JFR
* Flag set by jdk.internal.event.JFRTracing to indicate if
* exceptions should be traced by JFR.
*/
static volatile boolean jfrTracing;

Expand Down
Loading