test/runtime/os/AvailableProcessors.java

changeset 8923
82f3ae5b4190
parent 8619
3a38e441474d
equal deleted inserted replaced
8922:864d59bedf3d 8923:82f3ae5b4190
1 /*
2 * Copyright (c) 2016, 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 import java.io.File;
24 import com.oracle.java.testlibrary.ProcessTools;
25 import com.oracle.java.testlibrary.OutputAnalyzer;
26 import java.util.ArrayList;
27
28 /*
29 * @test
30 * @bug 6515172
31 * @summary Check that availableProcessors reports the correct value when running in a cpuset on linux
32 * @requires os.family == "linux"
33 * @library /testlibrary
34 * @build com.oracle.java.testlibrary.*
35 * @run driver AvailableProcessors
36 */
37 public class AvailableProcessors {
38
39 static final String SUCCESS_STRING = "Found expected processors: ";
40
41 public static void main(String[] args) throws Throwable {
42 if (args.length > 0)
43 checkProcessors(Integer.parseInt(args[0]));
44 else {
45 // run ourselves under different cpu configurations
46 // using the taskset command
47 String taskset;
48 final String taskset1 = "/bin/taskset";
49 final String taskset2 = "/usr/bin/taskset";
50 if (new File(taskset1).exists())
51 taskset = taskset1;
52 else if (new File(taskset2).exists())
53 taskset = taskset2;
54 else {
55 System.out.println("Skipping test: could not find taskset command");
56 return;
57 }
58
59 int available = Runtime.getRuntime().availableProcessors();
60
61 if (available == 1) {
62 System.out.println("Skipping test: only one processor available");
63 return;
64 }
65
66 // Get the java command we want to execute
67 // Enable logging for easier failure diagnosis
68 ProcessBuilder master =
69 ProcessTools.createJavaProcessBuilder(false,
70 "-XX:+UnlockDiagnosticVMOptions",
71 "-XX:+PrintActiveCpus",
72 "AvailableProcessors");
73
74 int[] expected = new int[] { 1, available/2, available-1, available };
75
76 for (int i : expected) {
77 System.out.println("Testing for " + i + " processors ...");
78 int max = i - 1;
79 ArrayList<String> cmdline = new ArrayList<>(master.command());
80 // prepend taskset command
81 cmdline.add(0, "0-" + max);
82 cmdline.add(0, "-c");
83 cmdline.add(0, taskset);
84 // append expected processor count
85 cmdline.add(String.valueOf(i));
86 ProcessBuilder pb = new ProcessBuilder(cmdline);
87 System.out.println("Final command line: " +
88 ProcessTools.getCommandLine(pb));
89 OutputAnalyzer output = ProcessTools.executeProcess(pb);
90 output.shouldContain(SUCCESS_STRING);
91 }
92 }
93 }
94
95 static void checkProcessors(int expected) {
96 int available = Runtime.getRuntime().availableProcessors();
97 if (available != expected)
98 throw new Error("Expected " + expected + " processors, but found "
99 + available);
100 else
101 System.out.println(SUCCESS_STRING + available);
102 }
103 }

mercurial