1 /* 2 * Copyright (c) 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 25 /* 26 * @test 27 * @summary Test JCMD with side car pattern. 28 * Sidecar is a common pattern used in the cloud environments for monitoring 29 * and other uses. In side car pattern the main application/service container 30 * is paired with a sidecar container by sharing certain aspects of container 31 * namespace such as PID namespace, specific sub-directories, IPC and more. 32 * @requires docker.support 33 * @library /test/lib 34 * @modules java.base/jdk.internal.misc 35 * java.management 36 * jdk.jartool/sun.tools.jar 37 * @build EventGeneratorLoop 38 * @run driver TestJcmdWithSideCar 39 */ 40 import java.nio.file.Path; 41 import java.nio.file.Paths; 42 import java.util.Arrays; 43 import java.util.ArrayList; 44 import java.util.List; 45 import java.util.stream.Collectors; 46 import jdk.test.lib.Utils; 47 import jdk.test.lib.containers.docker.Common; 48 import jdk.test.lib.containers.docker.DockerRunOptions; 49 import jdk.test.lib.containers.docker.DockerTestUtils; 50 import jdk.test.lib.process.OutputAnalyzer; 51 52 53 public class TestJcmdWithSideCar { 54 private static final String IMAGE_NAME = Common.imageName("jfr-jcmd"); 55 private static final Path WORK_DIR = Paths.get(".").toAbsolutePath(); 56 private static final int TIME_TO_RUN_MAIN_PROCESS = (int) (30 * Utils.TIMEOUT_FACTOR); // seconds 57 private static final String MAIN_CONTAINER_NAME = "test-container-main"; 58 59 public static void main(String[] args) throws Exception { 60 if (!DockerTestUtils.canTestDocker()) { 61 return; 62 } 63 64 DockerTestUtils.buildJdkDockerImage(IMAGE_NAME, "Dockerfile-BasicTest", "jdk-docker"); 65 66 try { 67 // Start the loop process in the "main" container, then run test cases 68 // using a sidecar container. 69 DockerThread t = startMainContainer(); 70 71 waitForMainContainerToStart(500, 10); 72 t.checkForErrors(); 73 74 OutputAnalyzer jcmdOut = testCase01(); 75 long mainProcPid = findProcess(jcmdOut, "EventGeneratorLoop"); 76 77 t.assertIsAlive(); 78 testCase02(mainProcPid); 79 80 // JCMD does not work in sidecar configuration, except for "jcmd -l". 81 // Including this test case to assist in reproduction of the problem. 82 // t.assertIsAlive(); 83 // testCase03(mainProcPid); 84 85 t.join(TIME_TO_RUN_MAIN_PROCESS * 1000); 86 t.checkForErrors(); 87 t.out.shouldHaveExitValue(0); 88 } finally { 89 DockerTestUtils.removeDockerImage(IMAGE_NAME); 90 } 91 } 92 93 94 // Run "jcmd -l" in a sidecar container and find a process that runs EventGeneratorLoop 95 private static OutputAnalyzer testCase01() throws Exception { 96 return runSideCar(MAIN_CONTAINER_NAME, "/jdk/bin/jcmd", "-l") 97 .shouldHaveExitValue(0) 98 .shouldContain("sun.tools.jcmd.JCmd") 99 .shouldContain("EventGeneratorLoop"); 100 } 101 102 // run jhsdb jinfo <PID> (jhsdb uses PTRACE) 103 private static void testCase02(long pid) throws Exception { 104 runSideCar(MAIN_CONTAINER_NAME, "/jdk/bin/jhsdb", "jinfo", "--pid", "" + pid) 105 .shouldHaveExitValue(0) 106 .shouldContain("Java System Properties") 107 .shouldContain("VM Flags"); 108 } 109 110 // test jcmd with some commands (help, start JFR recording) 111 // JCMD will use signal mechanism and Unix Socket 112 private static void testCase03(long pid) throws Exception { 113 runSideCar(MAIN_CONTAINER_NAME, "/jdk/bin/jcmd", "" + pid, "help") 114 .shouldHaveExitValue(0) 115 .shouldContain("VM.version"); 116 runSideCar(MAIN_CONTAINER_NAME, "/jdk/bin/jcmd", "" + pid, "JFR.start") 117 .shouldHaveExitValue(0) 118 .shouldContain("Started recording"); 119 } 120 121 private static DockerThread startMainContainer() throws Exception { 122 // start "main" container (the observee) 123 DockerRunOptions opts = commonDockerOpts("EventGeneratorLoop"); 124 opts.addDockerOpts("--cap-add=SYS_PTRACE") 125 .addDockerOpts("--name", MAIN_CONTAINER_NAME) 126 .addDockerOpts("-v", "/tmp") 127 .addJavaOpts("-XX:+UsePerfData") 128 .addClassOptions("" + TIME_TO_RUN_MAIN_PROCESS); 129 DockerThread t = new DockerThread(opts); 130 t.start(); 131 132 return t; 133 } 134 135 private static void waitForMainContainerToStart(int delayMillis, int count) throws Exception { 136 boolean started = false; 137 for(int i=0; i < count; i++) { 138 try { 139 Thread.sleep(delayMillis); 140 } catch (InterruptedException e) {} 141 if (isMainContainerRunning()) { 142 started = true; 143 break; 144 } 145 System.out.println("The main container has not started yet, count = " + i); 146 } 147 if (!started) { 148 throw new RuntimeException("Main container did not start"); 149 } 150 } 151 152 private static boolean isMainContainerRunning() throws Exception { 153 OutputAnalyzer out = 154 DockerTestUtils.execute(DockerTestUtils.DOCKER_COMMAND, 155 "ps", "--no-trunc", 156 "--filter", "name=" + MAIN_CONTAINER_NAME); 157 return out.getStdout().contains(MAIN_CONTAINER_NAME); 158 } 159 160 // The "sidecar" container shares "/tmp" directory with the "main" container for the 161 // JVM attach mechanism to work. JCMD relies on the attach mechanism (com.sun.tools.attach), 162 // which in turn relies on JVMSTAT mechanism, which uses Unix socket file hsperf_<pid> residing in 163 // the /tmp directory. 164 private static OutputAnalyzer runSideCar(String MAIN_CONTAINER_NAME, String whatToRun, 165 String... args) throws Exception { 166 List<String> cmd = new ArrayList<>(); 167 String[] command = new String[] { 168 DockerTestUtils.DOCKER_COMMAND, "run", 169 "--tty=true", "--rm", 170 "--cap-add=SYS_PTRACE", "--sig-proxy=true", 171 "--pid=container:" + MAIN_CONTAINER_NAME, 172 "--volumes-from", MAIN_CONTAINER_NAME, 173 IMAGE_NAME, whatToRun 174 }; 175 176 cmd.addAll(Arrays.asList(command)); 177 cmd.addAll(Arrays.asList(args)); 178 return DockerTestUtils.execute(cmd); 179 } 180 181 private static long findProcess(OutputAnalyzer out, String name) throws Exception { 182 List<String> l = out.asLines() 183 .stream() 184 .filter(s -> s.contains(name)) 185 .collect(Collectors.toList()); 186 if (l.isEmpty()) { 187 throw new RuntimeException("Could not find matching process"); 188 } 189 String psInfo = l.get(0); 190 System.out.println("findProcess(): psInfo: " + psInfo); 191 String pid = psInfo.substring(0, psInfo.indexOf(' ')); 192 System.out.println("findProcess(): pid: " + pid); 193 return Long.parseLong(pid); 194 } 195 196 private static DockerRunOptions commonDockerOpts(String className) { 197 return new DockerRunOptions(IMAGE_NAME, "/jdk/bin/java", className) 198 .addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/") 199 .addJavaOpts("-cp", "/test-classes/"); 200 } 201 202 203 static class DockerThread extends Thread { 204 DockerRunOptions runOpts; 205 OutputAnalyzer out; 206 Exception exception; 207 208 DockerThread(DockerRunOptions opts) { 209 runOpts = opts; 210 } 211 212 public void run() { 213 try { 214 out = DockerTestUtils.dockerRunJava(runOpts); 215 } catch (Exception e) { 216 exception = e; 217 } 218 } 219 220 public void assertIsAlive() throws Exception { 221 if (!isAlive()) { 222 throw new RuntimeException("DockerThread stopped unexpectedly"); 223 } 224 } 225 226 public void checkForErrors() throws Exception { 227 if (exception != null) { 228 throw new RuntimeException("DockerThread threw exception" 229 + exception.getMessage()); 230 } 231 } 232 } 233 234 }