Path: blob/master/test/hotspot/jtreg/containers/docker/JfrNetwork.java
40948 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24import java.io.InputStream;25import java.io.OutputStream;26import java.net.ServerSocket;27import java.net.InetAddress;28import java.net.Socket;29import java.net.SocketAddress;30import java.nio.file.Paths;31import java.util.ArrayList;32import java.util.List;33import jdk.jfr.Recording;34import jdk.jfr.consumer.RecordedEvent;35import jdk.jfr.consumer.RecordingFile;36import jdk.test.lib.process.OutputAnalyzer;373839// This class is intended to run inside a container40public class JfrNetwork {41// use a unique hostname for container42public static final String HOST_NAME = "container-unique-8221711";43public static final String JFR_REPORTED_CONTAINER_HOSTNAME_TAG = "jfr_reported_container_hostname=";4445public static void main(String[] args) throws Exception {46String event = args[0];47try (ServerSocket ss = new ServerSocket()) {48testNetworkInfo(ss, event);49}50}5152private static void assertTrue(boolean expr, String msg) {53if (!expr) {54throw new RuntimeException(msg);55}56}5758private static void testNetworkInfo(ServerSocket ss, String event) throws Exception {59ServerSocketListener server = new ServerSocketListener(ss);60server.start();61SocketWriter writer = new SocketWriter(ss.getLocalSocketAddress());6263// setup and start the recording64String recordingPath = event + ".jfr";65log("========= Recording event: " + event);66Recording r = new Recording();67r.enable(event);68r.setDestination(Paths.get("/", "tmp", recordingPath));69r.start();7071// start the socker writer thread, write some data into the socket72writer.start();7374// wait for writer thread to terminate, then for server thread, then stop recording75writer.joinAndThrow();76server.joinAndThrow();77r.stop();7879// analyze the recording80List<RecordedEvent> events = RecordingFile.readAllEvents(r.getDestination());81events.forEach(e -> log ("event = " + e));82assertTrue(!events.isEmpty(), "No recorded network events");83RecordedEvent e = events.get(0);84log(JFR_REPORTED_CONTAINER_HOSTNAME_TAG + e.getString("host"));8586// compare IP addresses87boolean matchFound = false;88InetAddress reportedByJfr = InetAddress.getByName(e.getString("address"));89for (InetAddress ip : getLocalIp()) {90if (ip.equals(reportedByJfr)) {91matchFound = true;92break;93}94}95assertTrue(matchFound, "IP address match not found");96}9798private static List<InetAddress> getLocalIp() throws Exception {99List<InetAddress> addrs = new ArrayList<>();100InetAddress localHost = InetAddress.getLocalHost();101if (!localHost.isLoopbackAddress()) {102addrs.add(localHost);103}104105log("getLocalIp() returning:");106for (InetAddress addr : addrs) {107log(addr.getHostName());108log(addr.getHostAddress());109}110111return addrs;112}113114private static void log(String msg) {115System.out.println(msg);116}117118119private static class ServerSocketListener extends Thread {120Exception exception;121ServerSocket ss;122123ServerSocketListener(ServerSocket socket) throws Exception {124ss = socket;125ss.setReuseAddress(true);126ss.bind(null);127log("ServerSocker Local Address: " + ss.getLocalSocketAddress());128}129130public void joinAndThrow() throws Exception {131join();132if (exception != null) {133throw exception;134}135}136137public void run() {138try {139try (Socket s = ss.accept(); InputStream is = s.getInputStream()) {140System.out.println("ServerSocketListener: accepted socket connection: s = " + s);141is.read();142is.read();143is.read();144}145} catch (Exception e) {146exception = e;147}148}149}150151152private static class SocketWriter extends Thread {153Exception exception;154private SocketAddress ssAddr;155156public SocketWriter(SocketAddress sa) {157this.ssAddr = sa;158System.out.println("SocketWriter(): sa = " + sa);159}160161public void joinAndThrow() throws Exception {162join();163if (exception != null) {164throw exception;165}166}167168public void run() {169try (Socket s = new Socket()) {170s.connect(ssAddr);171try (OutputStream os = s.getOutputStream()) {172os.write('A');173os.write('B');174os.write('C');175}176} catch (Exception e) {177exception = e;178}179}180}181182}183184185