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