|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | + |
| 26 | +package jdk.internal.event; |
| 27 | + |
| 28 | + |
| 29 | +import java.net.InetSocketAddress; |
| 30 | +import java.net.SocketAddress; |
| 31 | +import java.net.UnixDomainSocketAddress; |
| 32 | + |
| 33 | +/** |
| 34 | + * A JFR event for socket read operations. This event is mirrored in |
| 35 | + * {@code jdk.jfr.events.SocketReadEvent } where the metadata for the event is |
| 36 | + * provided with annotations. Some of the methods are replaced by generated |
| 37 | + * methods when jfr is enabled. Note that the order of the arguments of the |
| 38 | + * {@link #commit(long, long, String, String, int, long, long, boolean)} method |
| 39 | + * must be the same as the order of the fields. |
| 40 | + */ |
| 41 | +public class SocketReadEvent extends Event { |
| 42 | + |
| 43 | + // THE ORDER OF THE FOLLOWING FIELDS IS IMPORTANT! |
| 44 | + // The order must match the argument order of the generated commit method. |
| 45 | + public String host; |
| 46 | + public String address; |
| 47 | + public int port; |
| 48 | + public long timeout; |
| 49 | + public long bytesRead; |
| 50 | + public boolean endOfStream; |
| 51 | + |
| 52 | + /** |
| 53 | + * Actually commit a socket read event. The implementation |
| 54 | + * of this method is generated automatically if jfr is enabled. |
| 55 | + * The order of the fields must be the same as the parameters in this method. |
| 56 | + * {@code commit(..., String, String, int, long, long, boolean)} |
| 57 | + * |
| 58 | + * @param start timestamp of the start of the operation |
| 59 | + * @param duration time in nanoseconds to complete the operation |
| 60 | + * @param host remote host of the transfer |
| 61 | + * @param address remote address of the transfer |
| 62 | + * @param port remote port of the transfer |
| 63 | + * @param timeout timeout setting for the read |
| 64 | + * @param bytes number of bytes that were transferred |
| 65 | + * @param endOfStream has the end of the stream been reached |
| 66 | + */ |
| 67 | + public static void commit(long start, long duration, String host, String address, int port, long timeout, long bytes, boolean endOfStream) { |
| 68 | + // Generated by JFR |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Determine if an event should be emitted. The duration of the operation |
| 73 | + * must exceed some threshold in order to commit the event. The implementation |
| 74 | + * of this method is generated automatically if jfr is enabled. |
| 75 | + * |
| 76 | + * @param duration time in nanoseconds to complete the operation |
| 77 | + * @return true if the event should be commited |
| 78 | + */ |
| 79 | + public static boolean shouldCommit(long duration) { |
| 80 | + // Generated by JFR |
| 81 | + return false; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Determine if this kind of event is enabled. The implementation |
| 86 | + * of this method is generated automatically if jfr is enabled. |
| 87 | + * |
| 88 | + * @return true if socket read events are enabled, false otherwise |
| 89 | + */ |
| 90 | + public static boolean enabled() { |
| 91 | + // Generated by JFR |
| 92 | + return false; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Fetch the current timestamp in nanoseconds. This method is used |
| 97 | + * to determine the start and end of an operation. The implementation |
| 98 | + * of this method is generated automatically if jfr is enabled. |
| 99 | + * |
| 100 | + * @return the current timestamp value |
| 101 | + */ |
| 102 | + public static long timestamp() { |
| 103 | + // Generated by JFR |
| 104 | + return 0L; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Helper method to offer the data needed to potentially commit an event. |
| 109 | + * The duration of the operation is computed using the current |
| 110 | + * timestamp and the given start time. If the duration is meets |
| 111 | + * or exceeds the configured value (determined by calling the generated method |
| 112 | + * {@link #shouldCommit(long)}), an event will be emitted by calling |
| 113 | + * {@link #commit(long, long, String, String, int, long, long, boolean)}. |
| 114 | + * |
| 115 | + * @param start the start time |
| 116 | + * @param nbytes how many bytes were transferred |
| 117 | + * @param remote the address of the remote socket |
| 118 | + * @param timeout maximum time to wait |
| 119 | + */ |
| 120 | + public static void offer(long start, long nbytes, SocketAddress remote, long timeout) { |
| 121 | + long duration = timestamp() - start; |
| 122 | + if (shouldCommit(duration)) { |
| 123 | + boolean eof = nbytes < 0 ? true : false; |
| 124 | + nbytes = nbytes < 0 ? 0 : nbytes; |
| 125 | + if (remote instanceof InetSocketAddress isa) { |
| 126 | + commit(start, duration, isa.getHostString(), isa.getAddress().getHostAddress(), isa.getPort(), timeout, nbytes, eof); |
| 127 | + } else if (remote instanceof UnixDomainSocketAddress udsa) { |
| 128 | + String path = "[" + udsa.getPath().toString() + "]"; |
| 129 | + commit(start, duration, "Unix domain socket", path, 0, timeout, nbytes, eof); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | +} |
0 commit comments