< prev index next >

test/hotspot/jtreg/containers/docker/TestJcmdWithSideCar.java

Print this page




  51 import jdk.test.lib.process.OutputAnalyzer;
  52 
  53 
  54 public class TestJcmdWithSideCar {
  55     private static final String IMAGE_NAME = Common.imageName("jfr-jcmd");
  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         } finally {
  88             DockerTestUtils.removeDockerImage(IMAGE_NAME);
  89         }
  90     }
  91 
  92 
  93     // Run "jcmd -l" in a sidecar container and find a process that runs EventGeneratorLoop
  94     private static OutputAnalyzer testCase01() throws Exception {
  95         return runSideCar(MAIN_CONTAINER_NAME, "/jdk/bin/jcmd", "-l")


 114             .shouldContain("VM.version");
 115         runSideCar(MAIN_CONTAINER_NAME, "/jdk/bin/jcmd", "" + pid, "JFR.start")
 116             .shouldHaveExitValue(0)
 117             .shouldContain("Started recording");
 118     }
 119 
 120     private static DockerThread startMainContainer() throws Exception {
 121         // start "main" container (the observee)
 122         DockerRunOptions opts = commonDockerOpts("EventGeneratorLoop");
 123         opts.addDockerOpts("--cap-add=SYS_PTRACE")
 124             .addDockerOpts("--name", MAIN_CONTAINER_NAME)
 125             .addDockerOpts("-v", "/tmp")
 126             .addJavaOpts("-XX:+UsePerfData")
 127             .addClassOptions("" + TIME_TO_RUN_MAIN_PROCESS);
 128         DockerThread t = new DockerThread(opts);
 129         t.start();
 130 
 131         return t;
 132     }
 133 
 134     private static void waitForMainContainerToStart(int delayMillis, int count) throws Exception {
 135         boolean started = false;
 136         for(int i=0; i < count; i++) {
 137             try {
 138                 Thread.sleep(delayMillis);
 139             } catch (InterruptedException e) {}
 140             if (isMainContainerRunning()) {
 141                 started = true;
 142                 break;
 143             }
 144         }
 145         if (!started) {
 146             throw new RuntimeException("Main container did not start");
 147         }
 148     }
 149 
 150     private static boolean isMainContainerRunning() throws Exception {
 151         OutputAnalyzer out =
 152             DockerTestUtils.execute(Container.ENGINE_COMMAND,
 153                                     "ps", "--no-trunc",
 154                                     "--filter", "name=" + MAIN_CONTAINER_NAME);
 155         return out.getStdout().contains(MAIN_CONTAINER_NAME);
 156     }
 157 
 158     // JCMD relies on the attach mechanism (com.sun.tools.attach),
 159     // which in turn relies on JVMSTAT mechanism, which puts its mapped
 160     // buffers in /tmp directory (hsperfdata_<user>). Thus, in sidecar
 161     // we mount /tmp via --volumes-from from the main container.
 162     private static OutputAnalyzer runSideCar(String MAIN_CONTAINER_NAME, String whatToRun,
 163                                              String... args) throws Exception {
 164         List<String> cmd = new ArrayList<>();
 165         String[] command = new String[] {
 166             Container.ENGINE_COMMAND, "run",
 167             "--tty=true", "--rm",
 168             "--cap-add=SYS_PTRACE", "--sig-proxy=true",
 169             "--pid=container:" + MAIN_CONTAINER_NAME,
 170             "--volumes-from", MAIN_CONTAINER_NAME,
 171             IMAGE_NAME, whatToRun
 172         };
 173 
 174         cmd.addAll(Arrays.asList(command));
 175         cmd.addAll(Arrays.asList(args));
 176         return DockerTestUtils.execute(cmd);
 177     }
 178 
 179     private static long findProcess(OutputAnalyzer out, String name) throws Exception {
 180         List<String> l = out.asLines()
 181             .stream()
 182             .filter(s -> s.contains(name))
 183             .collect(Collectors.toList());
 184         if (l.isEmpty()) {
 185             throw new RuntimeException("Could not find matching process");
 186         }
 187         String psInfo = l.get(0);
 188         System.out.println("findProcess(): psInfo: " + psInfo);
 189         String pid = psInfo.substring(0, psInfo.indexOf(' '));
 190         System.out.println("findProcess(): pid: " + pid);
 191         return Long.parseLong(pid);
 192     }
 193 
 194     private static DockerRunOptions commonDockerOpts(String className) {
 195         return new DockerRunOptions(IMAGE_NAME, "/jdk/bin/java", className)
 196             .addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/")
 197             .addJavaOpts("-cp", "/test-classes/");
 198     }
 199 
 200 
 201     static class DockerThread extends Thread {
 202         DockerRunOptions runOpts;
 203         Exception exception;
 204 
 205         DockerThread(DockerRunOptions opts) {
 206             runOpts = opts;
 207         }
 208 
 209         public void run() {
 210             try {
 211                 DockerTestUtils.dockerRunJava(runOpts);
 212             } catch (Exception e) {


  51 import jdk.test.lib.process.OutputAnalyzer;
  52 
  53 
  54 public class TestJcmdWithSideCar {
  55     private static final String IMAGE_NAME = Common.imageName("jfr-jcmd");
  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             DockerTestUtils.waitForContainerToStart(MAIN_CONTAINER_NAME, 2000, 500, 10);
  72             t.checkForErrors();
  73 
  74             OutputAnalyzer jcmdOut = testCase01();
  75             long mainProcPid = Common.findPidFromJcmdOutput(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         } finally {
  88             DockerTestUtils.removeDockerImage(IMAGE_NAME);
  89         }
  90     }
  91 
  92 
  93     // Run "jcmd -l" in a sidecar container and find a process that runs EventGeneratorLoop
  94     private static OutputAnalyzer testCase01() throws Exception {
  95         return runSideCar(MAIN_CONTAINER_NAME, "/jdk/bin/jcmd", "-l")


 114             .shouldContain("VM.version");
 115         runSideCar(MAIN_CONTAINER_NAME, "/jdk/bin/jcmd", "" + pid, "JFR.start")
 116             .shouldHaveExitValue(0)
 117             .shouldContain("Started recording");
 118     }
 119 
 120     private static DockerThread startMainContainer() throws Exception {
 121         // start "main" container (the observee)
 122         DockerRunOptions opts = commonDockerOpts("EventGeneratorLoop");
 123         opts.addDockerOpts("--cap-add=SYS_PTRACE")
 124             .addDockerOpts("--name", MAIN_CONTAINER_NAME)
 125             .addDockerOpts("-v", "/tmp")
 126             .addJavaOpts("-XX:+UsePerfData")
 127             .addClassOptions("" + TIME_TO_RUN_MAIN_PROCESS);
 128         DockerThread t = new DockerThread(opts);
 129         t.start();
 130 
 131         return t;
 132     }
 133 























 134 
 135     // JCMD relies on the attach mechanism (com.sun.tools.attach),
 136     // which in turn relies on JVMSTAT mechanism, which puts its mapped
 137     // buffers in /tmp directory (hsperfdata_<user>). Thus, in sidecar
 138     // we mount /tmp via --volumes-from from the main container.
 139     private static OutputAnalyzer runSideCar(String MAIN_CONTAINER_NAME, String whatToRun,
 140                                              String... args) throws Exception {
 141         List<String> cmd = new ArrayList<>();
 142         String[] command = new String[] {
 143             Container.ENGINE_COMMAND, "run",
 144             "--tty=true", "--rm",
 145             "--cap-add=SYS_PTRACE", "--sig-proxy=true",
 146             "--pid=container:" + MAIN_CONTAINER_NAME,
 147             "--volumes-from", MAIN_CONTAINER_NAME,
 148             IMAGE_NAME, whatToRun
 149         };
 150 
 151         cmd.addAll(Arrays.asList(command));
 152         cmd.addAll(Arrays.asList(args));
 153         return DockerTestUtils.execute(cmd);
 154     }
 155 














 156 
 157     private static DockerRunOptions commonDockerOpts(String className) {
 158         return new DockerRunOptions(IMAGE_NAME, "/jdk/bin/java", className)
 159             .addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/")
 160             .addJavaOpts("-cp", "/test-classes/");
 161     }
 162 
 163 
 164     static class DockerThread extends Thread {
 165         DockerRunOptions runOpts;
 166         Exception exception;
 167 
 168         DockerThread(DockerRunOptions opts) {
 169             runOpts = opts;
 170         }
 171 
 172         public void run() {
 173             try {
 174                 DockerTestUtils.dockerRunJava(runOpts);
 175             } catch (Exception e) {
< prev index next >