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