1 /* 2 * Copyright (c) 2017, 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 JVM's awareness of cpu sets (cpus and mems) 28 * @requires docker.support 29 * @requires (os.arch != "s390x") 30 * @library /test/lib 31 * @modules java.base/jdk.internal.misc 32 * java.management 33 * jdk.jartool/sun.tools.jar 34 * @build AttemptOOM sun.hotspot.WhiteBox PrintContainerInfo 35 * @run driver ClassFileInstaller -jar whitebox.jar sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission 36 * @run driver TestCPUSets 37 */ 38 import java.util.List; 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.containers.cgroup.CPUSetsReader; 43 import jdk.test.lib.Asserts; 44 import jdk.test.lib.Platform; 45 import jdk.test.lib.Utils; 46 import jdk.test.lib.process.OutputAnalyzer; 47 48 49 public class TestCPUSets { 50 private static final String imageName = Common.imageName("cpusets"); 51 52 public static void main(String[] args) throws Exception { 53 if (!DockerTestUtils.canTestDocker()) { 54 return; 55 } 56 57 Common.prepareWhiteBox(); 58 DockerTestUtils.buildJdkDockerImage(imageName, "Dockerfile-BasicTest", "jdk-docker"); 59 60 try { 61 // Sanity test the cpu sets reader and parser 62 CPUSetsReader.test(); 63 testTheSet("Cpus_allowed_list"); 64 testTheSet("Mems_allowed_list"); 65 } finally { 66 DockerTestUtils.removeDockerImage(imageName); 67 } 68 } 69 70 71 private static void testTheSet(String setType) throws Exception { 72 String cpuSetStr = CPUSetsReader.readFromProcStatus(setType); 73 74 if (cpuSetStr == null) { 75 System.out.printf("The %s test is skipped %n", setType); 76 } else { 77 List<Integer> cpuSet = CPUSetsReader.parseCpuSet(cpuSetStr); 78 79 // Test subset of one, full subset, and half of the subset 80 testCpuSet(CPUSetsReader.listToString(cpuSet, 1)); 81 if (cpuSet.size() > 1) { 82 testCpuSet(CPUSetsReader.listToString(cpuSet)); 83 } 84 if (cpuSet.size() > 2) { 85 testCpuSet(CPUSetsReader.listToString(cpuSet, cpuSet.size()/2 )); 86 } 87 } 88 } 89 90 91 private static DockerRunOptions commonOpts() { 92 DockerRunOptions opts = new DockerRunOptions(imageName, "/jdk/bin/java", 93 "PrintContainerInfo"); 94 opts.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/"); 95 opts.addJavaOpts("-Xlog:os+container=trace", "-cp", "/test-classes/"); 96 Common.addWhiteBoxOpts(opts); 97 return opts; 98 } 99 100 101 private static void checkResult(List<String> lines, String lineMarker, String value) { 102 boolean lineMarkerFound = false; 103 104 for (String line : lines) { 105 if (line.contains(lineMarker)) { 106 lineMarkerFound = true; 107 String[] parts = line.split(":"); 108 System.out.println("DEBUG: line = " + line); 109 System.out.println("DEBUG: parts.length = " + parts.length); 110 111 Asserts.assertEquals(parts.length, 2); 112 String set = parts[1].replaceAll("\\s",""); 113 String actual = CPUSetsReader.listToString(CPUSetsReader.parseCpuSet(set)); 114 Asserts.assertEquals(actual, value); 115 break; 116 } 117 } 118 Asserts.assertTrue(lineMarkerFound); 119 } 120 121 122 private static void testCpuSet(String value) throws Exception { 123 Common.logNewTestCase("cpusets.cpus, value = " + value); 124 125 DockerRunOptions opts = commonOpts(); 126 opts.addDockerOpts("--cpuset-cpus=" + value); 127 128 List<String> lines = Common.run(opts).asLines(); 129 checkResult(lines, "cpuset.cpus is:", value); 130 } 131 132 private static void testMemSet(String value) throws Exception { 133 Common.logNewTestCase("cpusets.mems, value = " + value); 134 135 DockerRunOptions opts = commonOpts(); 136 opts.addDockerOpts("--cpuset-mems=" + value); 137 138 List<String> lines = Common.run(opts).asLines(); 139 checkResult(lines, "cpuset.mems is:", value); 140 } 141 142 }