From 6aa837eee62c2243689142915abcd85db85d0eed Mon Sep 17 00:00:00 2001 From: Erik Gahlin Date: Tue, 17 Oct 2023 13:11:52 +0000 Subject: [PATCH] 8316927: JFR: Move shouldCommit check earlier for socket events Reviewed-by: alanb, dfuchs, mgronlun --- .../share/classes/java/net/Socket.java | 10 ++++++++-- .../jdk/internal/event/SocketReadEvent.java | 20 +++++++++---------- .../jdk/internal/event/SocketWriteEvent.java | 18 ++++++++--------- .../classes/sun/nio/ch/SocketChannelImpl.java | 20 +++++++++++++++---- .../bench/java/net/SocketEventOverhead.java | 10 ++++++++-- 5 files changed, 49 insertions(+), 29 deletions(-) diff --git a/src/java.base/share/classes/java/net/Socket.java b/src/java.base/share/classes/java/net/Socket.java index ada2073c408..9477f5deb3c 100644 --- a/src/java.base/share/classes/java/net/Socket.java +++ b/src/java.base/share/classes/java/net/Socket.java @@ -1096,7 +1096,10 @@ public class Socket implements java.io.Closeable { } long start = SocketReadEvent.timestamp(); int nbytes = implRead(b, off, len); - SocketReadEvent.offer(start, nbytes, parent.getRemoteSocketAddress(), getSoTimeout()); + long duration = SocketReadEvent.timestamp() - start; + if (SocketReadEvent.shouldCommit(duration)) { + SocketReadEvent.emit(start, duration, nbytes, parent.getRemoteSocketAddress(), getSoTimeout()); + } return nbytes; } @@ -1209,7 +1212,10 @@ public class Socket implements java.io.Closeable { } long start = SocketWriteEvent.timestamp(); implWrite(b, off, len); - SocketWriteEvent.offer(start, len, parent.getRemoteSocketAddress()); + long duration = SocketWriteEvent.timestamp() - start; + if (SocketWriteEvent.shouldCommit(duration)) { + SocketWriteEvent.emit(start, duration, len, parent.getRemoteSocketAddress()); + } } private void implWrite(byte[] b, int off, int len) throws IOException { diff --git a/src/java.base/share/classes/jdk/internal/event/SocketReadEvent.java b/src/java.base/share/classes/jdk/internal/event/SocketReadEvent.java index 8f9b6ac5710..d7e3bef0eff 100644 --- a/src/java.base/share/classes/jdk/internal/event/SocketReadEvent.java +++ b/src/java.base/share/classes/jdk/internal/event/SocketReadEvent.java @@ -113,21 +113,19 @@ public class SocketReadEvent extends Event { * {@link #commit(long, long, String, String, int, long, long, boolean)}. * * @param start the start time + * @param duration the duration * @param nbytes how many bytes were transferred * @param remote the address of the remote socket * @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)) { - boolean eof = nbytes < 0 ? true : false; - nbytes = nbytes < 0 ? 0 : nbytes; - if (remote instanceof InetSocketAddress isa) { - commit(start, duration, isa.getHostString(), isa.getAddress().getHostAddress(), isa.getPort(), timeout, nbytes, eof); - } else if (remote instanceof UnixDomainSocketAddress udsa) { - String path = "[" + udsa.getPath().toString() + "]"; - commit(start, duration, "Unix domain socket", path, 0, timeout, nbytes, eof); - } + public static void emit(long start, long duration, long nbytes, SocketAddress remote, long timeout) { + boolean eof = nbytes < 0 ? true : false; + nbytes = nbytes < 0 ? 0 : nbytes; + if (remote instanceof InetSocketAddress isa) { + commit(start, duration, isa.getHostString(), isa.getAddress().getHostAddress(), isa.getPort(), timeout, nbytes, eof); + } else if (remote instanceof UnixDomainSocketAddress udsa) { + String path = "[" + udsa.getPath().toString() + "]"; + commit(start, duration, "Unix domain socket", path, 0, timeout, nbytes, eof); } } diff --git a/src/java.base/share/classes/jdk/internal/event/SocketWriteEvent.java b/src/java.base/share/classes/jdk/internal/event/SocketWriteEvent.java index 4fba69d3efe..a60ef69f7d0 100644 --- a/src/java.base/share/classes/jdk/internal/event/SocketWriteEvent.java +++ b/src/java.base/share/classes/jdk/internal/event/SocketWriteEvent.java @@ -108,19 +108,17 @@ public class SocketWriteEvent extends Event { * {@link #commit(long, long, String, String, int, long)}. * * @param start the start time + * @param duration the duration * @param bytesWritten how many bytes were sent * @param remote the address of the remote socket being written to */ - public static void offer(long start, long bytesWritten, SocketAddress remote) { - long duration = timestamp() - start; - if (shouldCommit(duration)) { - long bytes = bytesWritten < 0 ? 0 : bytesWritten; - if (remote instanceof InetSocketAddress isa) { - commit(start, duration, isa.getHostString(), isa.getAddress().getHostAddress(), isa.getPort(), bytes); - } else if (remote instanceof UnixDomainSocketAddress udsa) { - String path = "[" + udsa.getPath().toString() + "]"; - commit(start, duration, "Unix domain socket", path, 0, bytes); - } + public static void emit(long start, long duration, long bytesWritten, SocketAddress remote) { + long bytes = bytesWritten < 0 ? 0 : bytesWritten; + if (remote instanceof InetSocketAddress isa) { + commit(start, duration, isa.getHostString(), isa.getAddress().getHostAddress(), isa.getPort(), bytes); + } else if (remote instanceof UnixDomainSocketAddress udsa) { + String path = "[" + udsa.getPath().toString() + "]"; + commit(start, duration, "Unix domain socket", path, 0, bytes); } } } diff --git a/src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java b/src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java index 6c65a964a65..af40407b85d 100644 --- a/src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java +++ b/src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java @@ -494,7 +494,10 @@ class SocketChannelImpl } long start = SocketReadEvent.timestamp(); int nbytes = implRead(buf); - SocketReadEvent.offer(start, nbytes, remoteAddress(), 0); + long duration = SocketReadEvent.timestamp() - start; + if (SocketReadEvent.shouldCommit(duration)) { + SocketReadEvent.emit(start, duration, nbytes, remoteAddress(), 0); + } return nbytes; } @@ -508,7 +511,10 @@ class SocketChannelImpl } long start = SocketReadEvent.timestamp(); long nbytes = implRead(dsts, offset, length); - SocketReadEvent.offer(start, nbytes, remoteAddress(), 0); + long duration = SocketReadEvent.timestamp() - start; + if (SocketReadEvent.shouldCommit(duration)) { + SocketReadEvent.emit(start, duration, nbytes, remoteAddress(), 0); + } return nbytes; } @@ -619,7 +625,10 @@ class SocketChannelImpl } long start = SocketWriteEvent.timestamp(); int nbytes = implWrite(buf); - SocketWriteEvent.offer(start, nbytes, remoteAddress()); + long duration = SocketWriteEvent.timestamp() - start; + if (SocketWriteEvent.shouldCommit(duration)) { + SocketWriteEvent.emit(start, duration, nbytes, remoteAddress()); + } return nbytes; } @@ -632,7 +641,10 @@ class SocketChannelImpl } long start = SocketWriteEvent.timestamp(); long nbytes = implWrite(srcs, offset, length); - SocketWriteEvent.offer(start, nbytes, remoteAddress()); + long duration = SocketWriteEvent.timestamp() - start; + if (SocketWriteEvent.shouldCommit(duration)) { + SocketWriteEvent.emit(start, duration, nbytes, remoteAddress()); + } return nbytes; } diff --git a/test/micro/org/openjdk/bench/java/net/SocketEventOverhead.java b/test/micro/org/openjdk/bench/java/net/SocketEventOverhead.java index e54b42cd6fe..a898aff5283 100644 --- a/test/micro/org/openjdk/bench/java/net/SocketEventOverhead.java +++ b/test/micro/org/openjdk/bench/java/net/SocketEventOverhead.java @@ -137,7 +137,10 @@ public class SocketEventOverhead { try { nbytes = write0(); } finally { - SocketWriteEvent.offer(start, nbytes, getRemoteAddress()); + long duration = start - SocketWriteEvent.timestamp(); + if (SocketWriteEvent.shouldCommit(duration)) { + SocketWriteEvent.emit(start, duration, nbytes, getRemoteAddress()); + } } return nbytes; } @@ -155,7 +158,10 @@ public class SocketEventOverhead { try { nbytes = read0(); } finally { - SocketReadEvent.offer(start, nbytes, getRemoteAddress(), 0); + long duration = start - SocketReadEvent.timestamp(); + if (SocketReadEvent.shouldCommit(duration)) { + SocketReadEvent.emit(start, duration, nbytes, getRemoteAddress(), 0); + } } return nbytes; }