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 jdk.test.lib.process.ProcessTools; 25 import jdk.test.lib.process.OutputAnalyzer; 26 import java.util.ArrayList; 27 28 /* 29 * @test 30 * @bug 6515172 8148766 31 * @summary Check that availableProcessors reports the correct value when running in a cpuset on linux 32 * @requires os.family == "linux" 33 * @modules java.base/jdk.internal.misc 34 * @library /test/lib 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 Exception { 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 "-Xlog:os=trace", 71 "AvailableProcessors"); 72 73 int[] expected = new int[] { 1, available/2, available-1, available }; 74 75 for (int i : expected) { 76 System.out.println("Testing for " + i + " processors ..."); 77 int max = i - 1; 78 ArrayList<String> cmdline = new ArrayList<>(master.command()); 79 // prepend taskset command 80 cmdline.add(0, "0-" + max); 81 cmdline.add(0, "-c"); 82 cmdline.add(0, taskset); 83 // append expected processor count 84 cmdline.add(String.valueOf(i)); 85 ProcessBuilder pb = new ProcessBuilder(cmdline); 86 System.out.println("Final command line: " + 87 ProcessTools.getCommandLine(pb)); 88 OutputAnalyzer output = ProcessTools.executeProcess(pb); 89 output.shouldContain(SUCCESS_STRING); 90 } 91 } 92 } 93 94 static void checkProcessors(int expected) { 95 int available = Runtime.getRuntime().availableProcessors(); 96 // available can dynamically drop below expected due to aggressive power management 97 // but we should never have more than expected, else taskset is broken 98 if (available <= 0 || available > expected) 99 throw new Error("Expected " + expected + " processors, but found " 100 + available); 101 else 102 System.out.println(SUCCESS_STRING + available); 103 } 104 }