shshahma@8619: /* shshahma@8619: * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. shshahma@8619: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. shshahma@8619: * shshahma@8619: * This code is free software; you can redistribute it and/or modify it shshahma@8619: * under the terms of the GNU General Public License version 2 only, as shshahma@8619: * published by the Free Software Foundation. shshahma@8619: * shshahma@8619: * This code is distributed in the hope that it will be useful, but WITHOUT shshahma@8619: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or shshahma@8619: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License shshahma@8619: * version 2 for more details (a copy is included in the LICENSE file that shshahma@8619: * accompanied this code). shshahma@8619: * shshahma@8619: * You should have received a copy of the GNU General Public License version shshahma@8619: * 2 along with this work; if not, write to the Free Software Foundation, shshahma@8619: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. shshahma@8619: * shshahma@8619: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA shshahma@8619: * or visit www.oracle.com if you need additional information or have any shshahma@8619: * questions. shshahma@8619: */ shshahma@8619: import java.io.File; shshahma@8619: import com.oracle.java.testlibrary.ProcessTools; shshahma@8619: import com.oracle.java.testlibrary.OutputAnalyzer; shshahma@8619: import java.util.ArrayList; shshahma@8619: shshahma@8619: /* shshahma@8619: * @test shshahma@8619: * @bug 6515172 shshahma@8619: * @summary Check that availableProcessors reports the correct value when running in a cpuset on linux shshahma@8619: * @requires os.family == "linux" shshahma@8619: * @library /testlibrary shshahma@8619: * @build com.oracle.java.testlibrary.* shshahma@8619: * @run driver AvailableProcessors shshahma@8619: */ shshahma@8619: public class AvailableProcessors { shshahma@8619: shshahma@8619: static final String SUCCESS_STRING = "Found expected processors: "; shshahma@8619: shshahma@8619: public static void main(String[] args) throws Throwable { shshahma@8619: if (args.length > 0) shshahma@8619: checkProcessors(Integer.parseInt(args[0])); shshahma@8619: else { shshahma@8619: // run ourselves under different cpu configurations shshahma@8619: // using the taskset command shshahma@8619: String taskset; shshahma@8619: final String taskset1 = "/bin/taskset"; shshahma@8619: final String taskset2 = "/usr/bin/taskset"; shshahma@8619: if (new File(taskset1).exists()) shshahma@8619: taskset = taskset1; shshahma@8619: else if (new File(taskset2).exists()) shshahma@8619: taskset = taskset2; shshahma@8619: else { shshahma@8619: System.out.println("Skipping test: could not find taskset command"); shshahma@8619: return; shshahma@8619: } shshahma@8619: shshahma@8619: int available = Runtime.getRuntime().availableProcessors(); shshahma@8619: shshahma@8619: if (available == 1) { shshahma@8619: System.out.println("Skipping test: only one processor available"); shshahma@8619: return; shshahma@8619: } shshahma@8619: shshahma@8619: // Get the java command we want to execute shshahma@8619: // Enable logging for easier failure diagnosis shshahma@8619: ProcessBuilder master = shshahma@8619: ProcessTools.createJavaProcessBuilder(false, shshahma@8619: "-XX:+UnlockDiagnosticVMOptions", shshahma@8619: "-XX:+PrintActiveCpus", shshahma@8619: "AvailableProcessors"); shshahma@8619: shshahma@8619: int[] expected = new int[] { 1, available/2, available-1, available }; shshahma@8619: shshahma@8619: for (int i : expected) { shshahma@8619: System.out.println("Testing for " + i + " processors ..."); shshahma@8619: int max = i - 1; shshahma@8619: ArrayList cmdline = new ArrayList<>(master.command()); shshahma@8619: // prepend taskset command shshahma@8619: cmdline.add(0, "0-" + max); shshahma@8619: cmdline.add(0, "-c"); shshahma@8619: cmdline.add(0, taskset); shshahma@8619: // append expected processor count shshahma@8619: cmdline.add(String.valueOf(i)); shshahma@8619: ProcessBuilder pb = new ProcessBuilder(cmdline); shshahma@8619: System.out.println("Final command line: " + shshahma@8619: ProcessTools.getCommandLine(pb)); shshahma@8619: OutputAnalyzer output = ProcessTools.executeProcess(pb); shshahma@8619: output.shouldContain(SUCCESS_STRING); shshahma@8619: } shshahma@8619: } shshahma@8619: } shshahma@8619: shshahma@8619: static void checkProcessors(int expected) { shshahma@8619: int available = Runtime.getRuntime().availableProcessors(); shshahma@8619: if (available != expected) shshahma@8619: throw new Error("Expected " + expected + " processors, but found " shshahma@8619: + available); shshahma@8619: else shshahma@8619: System.out.println(SUCCESS_STRING + available); shshahma@8619: } shshahma@8619: }