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 Ensure that certain JFR events return correct results for resource values
  28  *          when run inside Docker container, such as available CPU and memory.
  29  *          Also make sure that PIDs are based on value provided by container,
  30  *          not by the host system.
  31  * @requires (docker.support & os.maxMemory >= 2g)
  32  * @library /test/lib
  33  * @modules java.base/jdk.internal.misc
  34  *          java.management
  35  *          jdk.jartool/sun.tools.jar
  36  * @build JfrReporter
  37  * @run driver TestJFREvents
  38  */
  39 import jdk.test.lib.containers.docker.Common;
  40 import jdk.test.lib.containers.docker.DockerRunOptions;
  41 import jdk.test.lib.containers.docker.DockerTestUtils;
  42 import jdk.test.lib.Utils;
  43 
  44 
  45 public class TestJFREvents {
  46     private static final String imageName = Common.imageName("jfr-events");
  47     private static final int availableCPUs = Runtime.getRuntime().availableProcessors();
  48 
  49     public static void main(String[] args) throws Exception {
  50         System.out.println("Test Environment: detected availableCPUs = " + availableCPUs);
  51         if (!DockerTestUtils.canTestDocker()) {
  52             return;
  53         }
  54 
  55         DockerTestUtils.buildJdkDockerImage(imageName, "Dockerfile-BasicTest", "jdk-docker");
  56 
  57         try {
  58             // leave one CPU for system and tools, otherwise this test may be unstable
  59             int maxNrOfAvailableCpus =  availableCPUs - 1;
  60             for (int i=1; i < maxNrOfAvailableCpus; i = i * 2) {
  61                 testCPUInfo(i, i);
  62             }
  63 
  64             long MB = 1024*1024;
  65             testMemory("200m", "" + 200*MB);
  66             testMemory("500m", "" + 500*MB);
  67             testMemory("1g", "" + 1024*MB);
  68 
  69             testProcessInfo();
  70 
  71         } finally {
  72             DockerTestUtils.removeDockerImage(imageName);
  73         }
  74     }
  75 
  76 
  77     private static void testCPUInfo(int valueToSet, int expectedValue) throws Exception {
  78         Common.logNewTestCase("CPUInfo: --cpus = " + valueToSet);
  79         DockerTestUtils.dockerRunJava(
  80                                       commonDockerOpts()
  81                                       .addDockerOpts("--cpus=" + valueToSet)
  82                                       .addClassOptions(JfrReporter.TESTCASE_CPU))
  83             .shouldHaveExitValue(0)
  84             .shouldContain(JfrReporter.TEST_REPORTED_CORES);
  85 
  86         // The following assertion is currently disabled due to JFR reporting incorrect values.
  87         // JFR reports values for the host system as opposed to values for the container.
  88         // @ignore 8219999
  89         // .shouldContain(JfrReporter.TEST_REPORTED_CORES + "=" + expectedValue);
  90     }
  91 
  92 
  93     private static void testMemory(String valueToSet, String expectedValue) throws Exception {
  94         Common.logNewTestCase("Memory: --memory = " + valueToSet);
  95         DockerTestUtils.dockerRunJava(
  96                                       commonDockerOpts()
  97                                       .addDockerOpts("--memory=" + valueToSet)
  98                                       .addClassOptions(JfrReporter.TESTCASE_MEMORY))
  99             .shouldHaveExitValue(0)
 100             .shouldContain(JfrReporter.TEST_REPORTED_MEMORY + "=" + expectedValue);
 101     }
 102 
 103 
 104     private static void testProcessInfo() throws Exception {
 105         Common.logNewTestCase("ProcessInfo");
 106         DockerTestUtils.dockerRunJava(
 107                                       commonDockerOpts()
 108                                       .addClassOptions(JfrReporter.TESTCASE_PROCESS))
 109             .shouldHaveExitValue(0)
 110             .shouldContain(JfrReporter.TEST_REPORTED_PID + "=1");
 111 
 112     }
 113 
 114 
 115     private static DockerRunOptions commonDockerOpts() {
 116         return new DockerRunOptions(imageName, "/jdk/bin/java", "JfrReporter")
 117             .addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/")
 118             .addJavaOpts("-cp", "/test-classes/");
 119     }
 120 }