Skip to content

Commit

Permalink
Make SocketWriteEvent and event mirror
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisHegarty committed Jun 2, 2021
1 parent 96cbba5 commit 030f3af
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 4 deletions.
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2021, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package jdk.internal.event;

public final class SocketWriteEvent extends Event {

public static final SocketWriteEvent EVENT = new SocketWriteEvent();

public String host;

public String address;

public int port;

public long bytesWritten;
}
37 changes: 36 additions & 1 deletion src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java
Expand Up @@ -37,6 +37,7 @@
import java.net.SocketOption;
import java.net.SocketTimeoutException;
import java.net.StandardSocketOptions;
import java.net.UnixDomainSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AlreadyBoundException;
import java.nio.channels.AlreadyConnectedException;
Expand All @@ -58,7 +59,7 @@
import static java.net.StandardProtocolFamily.INET;
import static java.net.StandardProtocolFamily.INET6;
import static java.net.StandardProtocolFamily.UNIX;

import jdk.internal.event.SocketWriteEvent;
import sun.net.ConnectionResetException;
import sun.net.NetHooks;
import sun.net.ext.ExtendedSocketOptions;
Expand Down Expand Up @@ -521,6 +522,40 @@ private void endWrite(boolean blocking, boolean completed)

@Override
public int write(ByteBuffer buf) throws IOException {
if (!SocketWriteEvent.EVENT.isEnabled()) {
return write0(buf);
}
int bytesWritten = 0;
SocketWriteEvent event = new SocketWriteEvent();
try {
event.begin();
bytesWritten = write0(buf);
} finally {
event.end();
if (event.shouldCommit()) {
long bytes = bytesWritten < 0 ? 0 : bytesWritten;
event.bytesWritten = bytes;
SocketAddress remoteAddress = getRemoteAddress();
if (remoteAddress instanceof InetSocketAddress isa) {
String hostString = isa.getAddress().toString();
int delimiterIndex = hostString.lastIndexOf('/');
event.host = hostString.substring(0, delimiterIndex);
event.address = hostString.substring(delimiterIndex + 1);
event.port = isa.getPort();
} else {
UnixDomainSocketAddress udsa = (UnixDomainSocketAddress) remoteAddress;
String path = "[" + udsa.getPath().toString() + "]";
event.host = "Unix domain socket";
event.address = path;
event.port = 0;
}
event.commit();
}
}
return bytesWritten;
}

private int write0(ByteBuffer buf) throws IOException {
Objects.requireNonNull(buf);
writeLock.lock();
try {
Expand Down
Expand Up @@ -30,12 +30,14 @@
import jdk.jfr.Label;
import jdk.jfr.DataAmount;
import jdk.jfr.Name;
import jdk.jfr.internal.MirrorEvent;
import jdk.jfr.internal.Type;

@Name(Type.EVENT_NAME_PREFIX + "SocketWrite")
@Label("Socket Write")
@Category("Java Application")
@Description("Writing data to a socket")
@MirrorEvent(className = "jdk.internal.event.SocketWriteEvent")
public final class SocketWriteEvent extends AbstractJDKEvent {

// The order of these fields must be the same as the parameters in
Expand Down
Expand Up @@ -67,6 +67,7 @@ public final class JDKEvents {
DeserializationEvent.class,
ProcessStartEvent.class,
SecurityPropertyModificationEvent.class,
SocketWriteEvent.class,
TLSHandshakeEvent.class,
X509CertificateEvent.class,
X509ValidationEvent.class
Expand All @@ -77,7 +78,6 @@ public final class JDKEvents {
FileReadEvent.class,
FileWriteEvent.class,
SocketReadEvent.class,
SocketWriteEvent.class,
ExceptionThrownEvent.class,
ExceptionStatisticsEvent.class,
ErrorThrownEvent.class,
Expand All @@ -86,6 +86,7 @@ public final class JDKEvents {
jdk.internal.event.DeserializationEvent.class,
jdk.internal.event.ProcessStartEvent.class,
jdk.internal.event.SecurityPropertyModificationEvent.class,
jdk.internal.event.SocketWriteEvent.class,
jdk.internal.event.TLSHandshakeEvent.class,
jdk.internal.event.X509CertificateEvent.class,
jdk.internal.event.X509ValidationEvent.class,
Expand All @@ -101,7 +102,7 @@ public final class JDKEvents {
FileChannelImplInstrumentor.class,
SocketInputStreamInstrumentor.class,
SocketOutputStreamInstrumentor.class,
SocketChannelImplInstrumentor.class
//SocketChannelImplInstrumentor.class
};

private static final Class<?>[] targetClasses = new Class<?>[instrumentationClasses.length];
Expand Down
Expand Up @@ -45,7 +45,7 @@ private SocketOutputStreamInstrumentor() {
@JIInstrumentationMethod
public void write(byte b[], int off, int len) throws IOException {
EventHandler handler = Handlers.SOCKET_WRITE;
if (!handler.isEnabled()) {
if (true) { //!handler.isEnabled()) { // temporarily disable
write(b, off, len);
return;
}
Expand Down

0 comments on commit 030f3af

Please sign in to comment.