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