Path: blob/master/test/hotspot/jtreg/containers/docker/TestCPUSets.java
40942 views
/*1* Copyright (c) 2017, 2021, 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 Test JVM's awareness of cpu sets (cpus and mems)28* @requires docker.support29* @requires (os.arch != "s390x")30* @library /test/lib31* @modules java.base/jdk.internal.misc32* java.management33* jdk.jartool/sun.tools.jar34* @build AttemptOOM sun.hotspot.WhiteBox PrintContainerInfo35* @run driver jdk.test.lib.helpers.ClassFileInstaller -jar whitebox.jar sun.hotspot.WhiteBox36* @run driver TestCPUSets37*/38import java.util.List;39import jdk.test.lib.containers.docker.Common;40import jdk.test.lib.containers.docker.DockerRunOptions;41import jdk.test.lib.containers.docker.DockerTestUtils;42import jdk.test.lib.containers.cgroup.CPUSetsReader;43import jdk.test.lib.Asserts;44import jdk.test.lib.Platform;45import jdk.test.lib.Utils;46import jdk.test.lib.process.OutputAnalyzer;47import jtreg.SkippedException;4849public class TestCPUSets {50private static final String imageName = Common.imageName("cpusets");5152public static void main(String[] args) throws Exception {53if (!DockerTestUtils.canTestDocker()) {54return;55}565758Common.prepareWhiteBox();59DockerTestUtils.buildJdkDockerImage(imageName, "Dockerfile-BasicTest", "jdk-docker");6061try {62// Sanity test the cpu sets reader and parser63CPUSetsReader.test();64testTheSet("Cpus_allowed_list");65testTheSet("Mems_allowed_list");66} finally {67DockerTestUtils.removeDockerImage(imageName);68}69}707172private static void testTheSet(String setType) throws Exception {73String cpuSetStr = CPUSetsReader.readFromProcStatus(setType);7475if (cpuSetStr == null) {76String msg = String.format("The %s test is skipped: cpuSetStr is null %n", setType);77throw new SkippedException(msg);78}7980List<Integer> cpuSet = CPUSetsReader.parseCpuSet(cpuSetStr);81int availableProcessors = Runtime.getRuntime().availableProcessors();8283// print diagnostic info84printSet(setType, cpuSet);85log("getNumCpus(): " + CPUSetsReader.getNumCpus());86log("Runtime.getRuntime().availableProcessors(): " + availableProcessors);8788int maxSetSize = Math.min(cpuSet.size(), availableProcessors);89log("maxSetSize = " + maxSetSize);9091// Test subset of one, full set, and half of the set92testCpuSet(CPUSetsReader.listToString(cpuSet, 1));93if (maxSetSize >= 2) {94String cpuSetParam = CPUSetsReader.listToString(cpuSet, maxSetSize);95log("Testing with cpuSetParam = " + cpuSetParam);96testCpuSet(cpuSetParam);97}98if (maxSetSize >= 4) {99String cpuSetParam = CPUSetsReader.listToString(cpuSet, maxSetSize/2);100log("Testing with cpuSetParam = " + cpuSetParam);101testCpuSet(cpuSetParam);102}103}104105106private static void printSet(String setType, List<Integer> set) {107System.out.print("printSet(): " + setType + ": ");108set.forEach( i -> System.out.print(i + ", "));109System.out.println("");110}111112113private static void log(String msg) {114System.out.println(msg);115}116117118private static DockerRunOptions commonOpts() {119DockerRunOptions opts = new DockerRunOptions(imageName, "/jdk/bin/java",120"PrintContainerInfo");121opts.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/");122opts.addJavaOpts("-Xlog:os+container=trace", "-cp", "/test-classes/");123Common.addWhiteBoxOpts(opts);124return opts;125}126127128private static void checkResult(List<String> lines, String lineMarker, String value) {129boolean lineMarkerFound = false;130131for (String line : lines) {132if (line.contains(lineMarker)) {133lineMarkerFound = true;134String[] parts = line.split(":");135System.out.println("DEBUG: line = " + line);136System.out.println("DEBUG: parts.length = " + parts.length);137138Asserts.assertEquals(parts.length, 2);139String set = parts[1].replaceAll("\\s","");140String actual = CPUSetsReader.listToString(CPUSetsReader.parseCpuSet(set));141Asserts.assertEquals(actual, value);142break;143}144}145Asserts.assertTrue(lineMarkerFound);146}147148149private static void testCpuSet(String value) throws Exception {150Common.logNewTestCase("cpusets.cpus, value = " + value);151152DockerRunOptions opts = commonOpts();153opts.addDockerOpts("--cpuset-cpus=" + value);154155List<String> lines = Common.run(opts).asLines();156checkResult(lines, "cpuset.cpus is:", value);157}158159private static void testMemSet(String value) throws Exception {160Common.logNewTestCase("cpusets.mems, value = " + value);161162DockerRunOptions opts = commonOpts();163opts.addDockerOpts("--cpuset-mems=" + value);164165List<String> lines = Common.run(opts).asLines();166checkResult(lines, "cpuset.mems is:", value);167}168169}170171172