1 /* 2 * Copyright (c) 2018, 2019, 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.jfr.event.runtime; 27 28 import java.io.IOException; 29 import java.net.DatagramPacket; 30 import java.net.DatagramSocket; 31 import java.net.InetAddress; 32 import java.net.NetworkInterface; 33 import java.util.HashSet; 34 import java.util.List; 35 import java.util.Set; 36 import java.util.stream.Stream; 37 38 import jdk.jfr.Recording; 39 import jdk.jfr.consumer.RecordedEvent; 40 import jdk.test.lib.Asserts; 41 import jdk.test.lib.Platform; 42 import jdk.test.lib.jfr.EventNames; 43 import jdk.test.lib.jfr.Events; 44 45 /** 46 * @test 47 * @key jfr 48 * @requires vm.hasJFR 49 * @library /test/lib 50 * 51 * @run main/othervm -Xlog:jfr+event=trace jdk.jfr.event.runtime.TestNetworkUtilizationEvent 52 */ 53 public class TestNetworkUtilizationEvent { 54 55 private static final long packetSendCount = 100; 56 57 public static void main(String[] args) throws Throwable { 58 Set<String> networkInterfaces = new HashSet<>(); 59 60 Recording recording = new Recording(); 61 recording.enable(EventNames.NetworkUtilization).with("period", "endChunk"); 62 recording.start(); 63 64 DatagramSocket socket = new DatagramSocket(); 65 String msg = "hello!"; 66 byte[] buf = msg.getBytes(); 67 forceEndChunk(); 68 // Send a few packets both to the loopback address as well to an external 69 InetAddress iadr1 = InetAddress.getLoopbackAddress(); 70 System.out.println("InetAddress.getLoopbackAddress :" + iadr1 + " host address:" + iadr1.getHostAddress()); 71 72 DatagramPacket packet = new DatagramPacket(buf, buf.length, iadr1, 12345); 73 for (int i = 0; i < packetSendCount; ++i) { 74 socket.send(packet); 75 } 76 77 Stream<InetAddress> si = NetworkInterface.networkInterfaces().flatMap(NetworkInterface::inetAddresses); 78 si.forEach(inetAddr->{ 79 System.out.println("Sending to InetAddress:" + inetAddr); 80 try { 81 java.util.concurrent.TimeUnit.SECONDS.sleep(1); 82 } catch (InterruptedException e) { } 83 try { 84 final DatagramPacket packet2 = new DatagramPacket(buf, buf.length, inetAddr, 12345); 85 for (int i = 0; i < packetSendCount; ++i) { 86 socket.send(packet2); 87 } 88 } catch(IOException ioe) { 89 } 90 }); 91 92 forceEndChunk(); 93 socket.close(); 94 // Now there should have been traffic on at least two different interfaces 95 recording.stop(); 96 97 List<RecordedEvent> events = Events.fromRecording(recording); 98 Events.hasEvents(events); 99 for (RecordedEvent event : events) { 100 System.out.println(event); 101 Events.assertField(event, "writeRate").atLeast(0L).atMost(1000L * Integer.MAX_VALUE); 102 Events.assertField(event, "readRate").atLeast(0L).atMost(1000L * Integer.MAX_VALUE); 103 Events.assertField(event, "networkInterface").notNull(); 104 if (event.getLong("writeRate") > 0) { 105 networkInterfaces.add(event.getString("networkInterface")); 106 } 107 } 108 109 System.out.println("number of network interfaces with positive writeRate found is " + networkInterfaces.size()); 110 111 if (Platform.isWindows() || Platform.isSolaris()) { 112 // Windows and Solaris do not track statistics for the loopback interface 113 Asserts.assertGreaterThanOrEqual(networkInterfaces.size(), 1, "Not enough network interfaces found"); 114 } else { 115 Asserts.assertGreaterThanOrEqual(networkInterfaces.size(), 2, "Not enough network interfaces found"); 116 } 117 } 118 119 private static void forceEndChunk() { 120 try(Recording r = new Recording()) { 121 r.start(); 122 r.stop(); 123 } 124 } 125 }