Thu, 22 Sep 2016 02:04:40 -0700
6515172: Runtime.availableProcessors() ignores Linux taskset command
Summary: extract processor count from sched_getaffinity mask
Reviewed-by: dholmes, gthornbr
shshahma@8619 | 1 | /* |
shshahma@8619 | 2 | * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. |
shshahma@8619 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
shshahma@8619 | 4 | * |
shshahma@8619 | 5 | * This code is free software; you can redistribute it and/or modify it |
shshahma@8619 | 6 | * under the terms of the GNU General Public License version 2 only, as |
shshahma@8619 | 7 | * published by the Free Software Foundation. |
shshahma@8619 | 8 | * |
shshahma@8619 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
shshahma@8619 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
shshahma@8619 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
shshahma@8619 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
shshahma@8619 | 13 | * accompanied this code). |
shshahma@8619 | 14 | * |
shshahma@8619 | 15 | * You should have received a copy of the GNU General Public License version |
shshahma@8619 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
shshahma@8619 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
shshahma@8619 | 18 | * |
shshahma@8619 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
shshahma@8619 | 20 | * or visit www.oracle.com if you need additional information or have any |
shshahma@8619 | 21 | * questions. |
shshahma@8619 | 22 | */ |
shshahma@8619 | 23 | import java.io.File; |
shshahma@8619 | 24 | import com.oracle.java.testlibrary.ProcessTools; |
shshahma@8619 | 25 | import com.oracle.java.testlibrary.OutputAnalyzer; |
shshahma@8619 | 26 | import java.util.ArrayList; |
shshahma@8619 | 27 | |
shshahma@8619 | 28 | /* |
shshahma@8619 | 29 | * @test |
shshahma@8619 | 30 | * @bug 6515172 |
shshahma@8619 | 31 | * @summary Check that availableProcessors reports the correct value when running in a cpuset on linux |
shshahma@8619 | 32 | * @requires os.family == "linux" |
shshahma@8619 | 33 | * @library /testlibrary |
shshahma@8619 | 34 | * @build com.oracle.java.testlibrary.* |
shshahma@8619 | 35 | * @run driver AvailableProcessors |
shshahma@8619 | 36 | */ |
shshahma@8619 | 37 | public class AvailableProcessors { |
shshahma@8619 | 38 | |
shshahma@8619 | 39 | static final String SUCCESS_STRING = "Found expected processors: "; |
shshahma@8619 | 40 | |
shshahma@8619 | 41 | public static void main(String[] args) throws Throwable { |
shshahma@8619 | 42 | if (args.length > 0) |
shshahma@8619 | 43 | checkProcessors(Integer.parseInt(args[0])); |
shshahma@8619 | 44 | else { |
shshahma@8619 | 45 | // run ourselves under different cpu configurations |
shshahma@8619 | 46 | // using the taskset command |
shshahma@8619 | 47 | String taskset; |
shshahma@8619 | 48 | final String taskset1 = "/bin/taskset"; |
shshahma@8619 | 49 | final String taskset2 = "/usr/bin/taskset"; |
shshahma@8619 | 50 | if (new File(taskset1).exists()) |
shshahma@8619 | 51 | taskset = taskset1; |
shshahma@8619 | 52 | else if (new File(taskset2).exists()) |
shshahma@8619 | 53 | taskset = taskset2; |
shshahma@8619 | 54 | else { |
shshahma@8619 | 55 | System.out.println("Skipping test: could not find taskset command"); |
shshahma@8619 | 56 | return; |
shshahma@8619 | 57 | } |
shshahma@8619 | 58 | |
shshahma@8619 | 59 | int available = Runtime.getRuntime().availableProcessors(); |
shshahma@8619 | 60 | |
shshahma@8619 | 61 | if (available == 1) { |
shshahma@8619 | 62 | System.out.println("Skipping test: only one processor available"); |
shshahma@8619 | 63 | return; |
shshahma@8619 | 64 | } |
shshahma@8619 | 65 | |
shshahma@8619 | 66 | // Get the java command we want to execute |
shshahma@8619 | 67 | // Enable logging for easier failure diagnosis |
shshahma@8619 | 68 | ProcessBuilder master = |
shshahma@8619 | 69 | ProcessTools.createJavaProcessBuilder(false, |
shshahma@8619 | 70 | "-XX:+UnlockDiagnosticVMOptions", |
shshahma@8619 | 71 | "-XX:+PrintActiveCpus", |
shshahma@8619 | 72 | "AvailableProcessors"); |
shshahma@8619 | 73 | |
shshahma@8619 | 74 | int[] expected = new int[] { 1, available/2, available-1, available }; |
shshahma@8619 | 75 | |
shshahma@8619 | 76 | for (int i : expected) { |
shshahma@8619 | 77 | System.out.println("Testing for " + i + " processors ..."); |
shshahma@8619 | 78 | int max = i - 1; |
shshahma@8619 | 79 | ArrayList<String> cmdline = new ArrayList<>(master.command()); |
shshahma@8619 | 80 | // prepend taskset command |
shshahma@8619 | 81 | cmdline.add(0, "0-" + max); |
shshahma@8619 | 82 | cmdline.add(0, "-c"); |
shshahma@8619 | 83 | cmdline.add(0, taskset); |
shshahma@8619 | 84 | // append expected processor count |
shshahma@8619 | 85 | cmdline.add(String.valueOf(i)); |
shshahma@8619 | 86 | ProcessBuilder pb = new ProcessBuilder(cmdline); |
shshahma@8619 | 87 | System.out.println("Final command line: " + |
shshahma@8619 | 88 | ProcessTools.getCommandLine(pb)); |
shshahma@8619 | 89 | OutputAnalyzer output = ProcessTools.executeProcess(pb); |
shshahma@8619 | 90 | output.shouldContain(SUCCESS_STRING); |
shshahma@8619 | 91 | } |
shshahma@8619 | 92 | } |
shshahma@8619 | 93 | } |
shshahma@8619 | 94 | |
shshahma@8619 | 95 | static void checkProcessors(int expected) { |
shshahma@8619 | 96 | int available = Runtime.getRuntime().availableProcessors(); |
shshahma@8619 | 97 | if (available != expected) |
shshahma@8619 | 98 | throw new Error("Expected " + expected + " processors, but found " |
shshahma@8619 | 99 | + available); |
shshahma@8619 | 100 | else |
shshahma@8619 | 101 | System.out.println(SUCCESS_STRING + available); |
shshahma@8619 | 102 | } |
shshahma@8619 | 103 | } |