Path: blob/master/test/hotspot/jtreg/containers/docker/TestJFREvents.java
40942 views
/*1* Copyright (c) 2019, 2020, 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.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/222324/*25* @test26* @key cgroups27* @summary Ensure that certain JFR events return correct results for resource values28* 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/lib33* @modules java.base/jdk.internal.misc34* java.management35* jdk.jartool/sun.tools.jar36* @build JfrReporter37* @run driver TestJFREvents38*/39import java.util.List;40import jdk.test.lib.containers.docker.Common;41import jdk.test.lib.containers.docker.DockerRunOptions;42import jdk.test.lib.containers.docker.DockerTestUtils;43import jdk.test.lib.Asserts;44import jdk.test.lib.process.OutputAnalyzer;45import jdk.test.lib.Utils;464748public class TestJFREvents {49private static final String imageName = Common.imageName("jfr-events");50private static final String TEST_ENV_VARIABLE = "UNIQUE_VARIABLE_ABC592903XYZ";51private static final String TEST_ENV_VALUE = "unique_value_abc592903xyz";52private static final int availableCPUs = Runtime.getRuntime().availableProcessors();5354public static void main(String[] args) throws Exception {55System.out.println("Test Environment: detected availableCPUs = " + availableCPUs);56if (!DockerTestUtils.canTestDocker()) {57return;58}5960DockerTestUtils.buildJdkDockerImage(imageName, "Dockerfile-BasicTest", "jdk-docker");6162try {6364long MB = 1024*1024;65testMemory("200m", "" + 200*MB);66testMemory("500m", "" + 500*MB);67testMemory("1g", "" + 1024*MB);6869testProcessInfo();7071testEnvironmentVariables();7273containerInfoTestCase();74testCpuUsage();75testCpuThrottling();76testMemoryUsage();77testIOUsage();78} finally {79DockerTestUtils.removeDockerImage(imageName);80}81}8283private static void containerInfoTestCase() throws Exception {84// leave one CPU for system and tools, otherwise this test may be unstable85int maxNrOfAvailableCpus = availableCPUs - 1;86for (int i=1; i < maxNrOfAvailableCpus; i = i * 2) {87for (int j=64; j <= 256; j *= 2) {88testContainerInfo(i, j);89}90}91}9293private static void testContainerInfo(int expectedCPUs, int expectedMemoryMB) throws Exception {94Common.logNewTestCase("ContainerInfo: --cpus = " + expectedCPUs + " --memory=" + expectedMemoryMB + "m");95String eventName = "jdk.ContainerConfiguration";96long expectedSlicePeriod = 100000; // default slice period97long expectedMemoryLimit = expectedMemoryMB * 1024 * 1024;9899String cpuCountFld = "effectiveCpuCount";100String cpuQuotaFld = "cpuQuota";101String cpuSlicePeriodFld = "cpuSlicePeriod";102String memoryLimitFld = "memoryLimit";103104DockerTestUtils.dockerRunJava(105commonDockerOpts()106.addDockerOpts("--cpus=" + expectedCPUs)107.addDockerOpts("--memory=" + expectedMemoryMB + "m")108.addClassOptions(eventName))109.shouldHaveExitValue(0)110.shouldContain(cpuCountFld + " = " + expectedCPUs)111.shouldContain(cpuSlicePeriodFld + " = " + expectedSlicePeriod)112.shouldContain(cpuQuotaFld + " = " + expectedCPUs * expectedSlicePeriod)113.shouldContain(memoryLimitFld + " = " + expectedMemoryLimit);114}115116private static void testCpuUsage() throws Exception {117Common.logNewTestCase("CPU Usage");118String eventName = "jdk.ContainerCPUUsage";119120String cpuTimeFld = "cpuTime";121String cpuUserTimeFld = "cpuUserTime";122String cpuSystemTimeFld = "cpuSystemTime";123124DockerTestUtils.dockerRunJava(125commonDockerOpts()126.addClassOptions(eventName, "period=endChunk"))127.shouldHaveExitValue(0)128.shouldNotContain(cpuTimeFld + " = " + 0)129.shouldNotContain(cpuUserTimeFld + " = " + 0)130.shouldNotContain(cpuSystemTimeFld + " = " + 0);131}132133private static void testMemoryUsage() throws Exception {134Common.logNewTestCase("Memory Usage");135String eventName = "jdk.ContainerMemoryUsage";136137String memoryFailCountFld = "memoryFailCount";138String memoryUsageFld = "memoryUsage";139String swapMemoryUsageFld = "swapMemoryUsage";140141DockerTestUtils.dockerRunJava(142commonDockerOpts()143.addClassOptions(eventName, "period=endChunk"))144.shouldHaveExitValue(0)145.shouldContain(memoryFailCountFld)146.shouldContain(memoryUsageFld)147.shouldContain(swapMemoryUsageFld);148}149150private static void testIOUsage() throws Exception {151Common.logNewTestCase("I/O Usage");152String eventName = "jdk.ContainerIOUsage";153154String serviceRequestsFld = "serviceRequests";155String dataTransferredFld = "dataTransferred";156157DockerTestUtils.dockerRunJava(158commonDockerOpts()159.addClassOptions(eventName, "period=endChunk"))160.shouldHaveExitValue(0)161.shouldContain(serviceRequestsFld)162.shouldContain(dataTransferredFld);163}164165private static void testCpuThrottling() throws Exception {166Common.logNewTestCase("CPU Throttling");167String eventName = "jdk.ContainerCPUThrottling";168169String cpuElapsedSlicesFld = "cpuElapsedSlices";170String cpuThrottledSlicesFld = "cpuThrottledSlices";171String cpuThrottledTimeFld = "cpuThrottledTime";172173DockerTestUtils.dockerRunJava(174commonDockerOpts()175.addClassOptions(eventName, "period=endChunk"))176.shouldHaveExitValue(0)177.shouldContain(cpuElapsedSlicesFld)178.shouldContain(cpuThrottledSlicesFld)179.shouldContain(cpuThrottledTimeFld);180}181182183private static void testMemory(String valueToSet, String expectedValue) throws Exception {184Common.logNewTestCase("Memory: --memory = " + valueToSet);185DockerTestUtils.dockerRunJava(186commonDockerOpts()187.addDockerOpts("--memory=" + valueToSet)188.addClassOptions("jdk.PhysicalMemory"))189.shouldHaveExitValue(0)190.shouldContain("totalSize = " + expectedValue);191}192193194private static void testProcessInfo() throws Exception {195Common.logNewTestCase("ProcessInfo");196DockerTestUtils.dockerRunJava(197commonDockerOpts()198.addClassOptions("jdk.SystemProcess"))199.shouldHaveExitValue(0)200.shouldContain("pid = 1");201}202203private static DockerRunOptions commonDockerOpts() {204return new DockerRunOptions(imageName, "/jdk/bin/java", "JfrReporter")205.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/")206.addJavaOpts("-cp", "/test-classes/");207}208209210private static void testEnvironmentVariables() throws Exception {211Common.logNewTestCase("EnvironmentVariables");212213List<String> cmd = DockerTestUtils.buildJavaCommand(214commonDockerOpts()215.addClassOptions("jdk.InitialEnvironmentVariable"));216217ProcessBuilder pb = new ProcessBuilder(cmd);218// Container has JAVA_HOME defined via the Dockerfile; make sure219// it is reported by JFR event.220// Environment variable set in host system should not be visible inside a container,221// and should not be reported by JFR.222pb.environment().put(TEST_ENV_VARIABLE, TEST_ENV_VALUE);223224System.out.println("[COMMAND]\n" + Utils.getCommandLine(pb));225OutputAnalyzer out = new OutputAnalyzer(pb.start());226System.out.println("[STDERR]\n" + out.getStderr());227System.out.println("[STDOUT]\n" + out.getStdout());228229out.shouldHaveExitValue(0)230.shouldContain("key = JAVA_HOME")231.shouldContain("value = /jdk")232.shouldNotContain(TEST_ENV_VARIABLE)233.shouldNotContain(TEST_ENV_VALUE);234}235}236237238