1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/runtime/os/AvailableProcessors.java Thu Sep 22 02:04:40 2016 -0700 1.3 @@ -0,0 +1,103 @@ 1.4 +/* 1.5 + * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 +import java.io.File; 1.27 +import com.oracle.java.testlibrary.ProcessTools; 1.28 +import com.oracle.java.testlibrary.OutputAnalyzer; 1.29 +import java.util.ArrayList; 1.30 + 1.31 +/* 1.32 + * @test 1.33 + * @bug 6515172 1.34 + * @summary Check that availableProcessors reports the correct value when running in a cpuset on linux 1.35 + * @requires os.family == "linux" 1.36 + * @library /testlibrary 1.37 + * @build com.oracle.java.testlibrary.* 1.38 + * @run driver AvailableProcessors 1.39 + */ 1.40 +public class AvailableProcessors { 1.41 + 1.42 + static final String SUCCESS_STRING = "Found expected processors: "; 1.43 + 1.44 + public static void main(String[] args) throws Throwable { 1.45 + if (args.length > 0) 1.46 + checkProcessors(Integer.parseInt(args[0])); 1.47 + else { 1.48 + // run ourselves under different cpu configurations 1.49 + // using the taskset command 1.50 + String taskset; 1.51 + final String taskset1 = "/bin/taskset"; 1.52 + final String taskset2 = "/usr/bin/taskset"; 1.53 + if (new File(taskset1).exists()) 1.54 + taskset = taskset1; 1.55 + else if (new File(taskset2).exists()) 1.56 + taskset = taskset2; 1.57 + else { 1.58 + System.out.println("Skipping test: could not find taskset command"); 1.59 + return; 1.60 + } 1.61 + 1.62 + int available = Runtime.getRuntime().availableProcessors(); 1.63 + 1.64 + if (available == 1) { 1.65 + System.out.println("Skipping test: only one processor available"); 1.66 + return; 1.67 + } 1.68 + 1.69 + // Get the java command we want to execute 1.70 + // Enable logging for easier failure diagnosis 1.71 + ProcessBuilder master = 1.72 + ProcessTools.createJavaProcessBuilder(false, 1.73 + "-XX:+UnlockDiagnosticVMOptions", 1.74 + "-XX:+PrintActiveCpus", 1.75 + "AvailableProcessors"); 1.76 + 1.77 + int[] expected = new int[] { 1, available/2, available-1, available }; 1.78 + 1.79 + for (int i : expected) { 1.80 + System.out.println("Testing for " + i + " processors ..."); 1.81 + int max = i - 1; 1.82 + ArrayList<String> cmdline = new ArrayList<>(master.command()); 1.83 + // prepend taskset command 1.84 + cmdline.add(0, "0-" + max); 1.85 + cmdline.add(0, "-c"); 1.86 + cmdline.add(0, taskset); 1.87 + // append expected processor count 1.88 + cmdline.add(String.valueOf(i)); 1.89 + ProcessBuilder pb = new ProcessBuilder(cmdline); 1.90 + System.out.println("Final command line: " + 1.91 + ProcessTools.getCommandLine(pb)); 1.92 + OutputAnalyzer output = ProcessTools.executeProcess(pb); 1.93 + output.shouldContain(SUCCESS_STRING); 1.94 + } 1.95 + } 1.96 + } 1.97 + 1.98 + static void checkProcessors(int expected) { 1.99 + int available = Runtime.getRuntime().availableProcessors(); 1.100 + if (available != expected) 1.101 + throw new Error("Expected " + expected + " processors, but found " 1.102 + + available); 1.103 + else 1.104 + System.out.println(SUCCESS_STRING + available); 1.105 + } 1.106 +}