hotspot/test/runtime/os/AvailableProcessors.java
author ctornqvi
Fri, 19 Aug 2016 10:06:30 -0400
changeset 40631 ed82623d7831
parent 38152 80e5da81fb2c
permissions -rw-r--r--
8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder Reviewed-by: coleenp, gtriantafill, mseledtsov, iignatyev, dholmes, dsamersoff
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
35884
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
     1
/*
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
     4
 *
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
     8
 *
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    13
 * accompanied this code).
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    14
 *
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    18
 *
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    21
 * questions.
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    22
 */
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    23
import java.io.File;
40631
ed82623d7831 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 38152
diff changeset
    24
import jdk.test.lib.process.ProcessTools;
ed82623d7831 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 38152
diff changeset
    25
import jdk.test.lib.process.OutputAnalyzer;
35884
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    26
import java.util.ArrayList;
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    27
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    28
/*
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    29
 * @test
35906
1eed370d39f9 8148766: Test AvailableProcessors.java got wrong number of processors
dholmes
parents: 35884
diff changeset
    30
 * @bug 6515172 8148766
35884
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    31
 * @summary Check that availableProcessors reports the correct value when running in a cpuset on linux
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    32
 * @requires os.family == "linux"
38152
80e5da81fb2c 8154258: [TESTBUG] Various serviceability tests fail compilation
dsamersoff
parents: 35906
diff changeset
    33
 * @modules java.base/jdk.internal.misc
40631
ed82623d7831 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 38152
diff changeset
    34
 * @library /test/lib
35884
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    35
 * @run driver AvailableProcessors
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    36
 */
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    37
public class AvailableProcessors {
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    38
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    39
    static final String SUCCESS_STRING = "Found expected processors: ";
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    40
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    41
    public static void main(String[] args) throws Exception {
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    42
        if (args.length > 0)
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    43
            checkProcessors(Integer.parseInt(args[0]));
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    44
        else {
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    45
            // run ourselves under different cpu configurations
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    46
            // using the taskset command
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    47
            String taskset;
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    48
            final String taskset1 = "/bin/taskset";
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    49
            final String taskset2 = "/usr/bin/taskset";
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    50
            if (new File(taskset1).exists())
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    51
                taskset = taskset1;
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    52
            else if (new File(taskset2).exists())
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    53
                taskset = taskset2;
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    54
            else {
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    55
                System.out.println("Skipping test: could not find taskset command");
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    56
                return;
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    57
            }
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    58
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    59
            int available = Runtime.getRuntime().availableProcessors();
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    60
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    61
            if (available == 1) {
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    62
                System.out.println("Skipping test: only one processor available");
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    63
                return;
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    64
            }
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    65
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    66
            // Get the java command we want to execute
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    67
            // Enable logging for easier failure diagnosis
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    68
            ProcessBuilder master =
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    69
                    ProcessTools.createJavaProcessBuilder(false,
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    70
                                                          "-Xlog:os=trace",
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    71
                                                          "AvailableProcessors");
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    72
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    73
            int[] expected = new int[] { 1, available/2, available-1, available };
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    74
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    75
            for (int i : expected) {
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    76
                System.out.println("Testing for " + i + " processors ...");
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    77
                int max = i - 1;
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    78
                ArrayList<String> cmdline = new ArrayList<>(master.command());
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    79
                // prepend taskset command
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    80
                cmdline.add(0, "0-" + max);
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    81
                cmdline.add(0, "-c");
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    82
                cmdline.add(0, taskset);
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    83
                // append expected processor count
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    84
                cmdline.add(String.valueOf(i));
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    85
                ProcessBuilder pb = new ProcessBuilder(cmdline);
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    86
                System.out.println("Final command line: " +
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    87
                                   ProcessTools.getCommandLine(pb));
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    88
                OutputAnalyzer output = ProcessTools.executeProcess(pb);
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    89
                output.shouldContain(SUCCESS_STRING);
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    90
            }
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    91
        }
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    92
    }
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    93
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    94
    static void checkProcessors(int expected) {
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    95
        int available = Runtime.getRuntime().availableProcessors();
35906
1eed370d39f9 8148766: Test AvailableProcessors.java got wrong number of processors
dholmes
parents: 35884
diff changeset
    96
        // available can dynamically drop below expected due to aggressive power management
1eed370d39f9 8148766: Test AvailableProcessors.java got wrong number of processors
dholmes
parents: 35884
diff changeset
    97
        // but we should never have more than expected, else taskset is broken
1eed370d39f9 8148766: Test AvailableProcessors.java got wrong number of processors
dholmes
parents: 35884
diff changeset
    98
        if (available <= 0 || available > expected)
35884
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
    99
            throw new Error("Expected " + expected + " processors, but found "
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
   100
                            + available);
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
   101
        else
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
   102
            System.out.println(SUCCESS_STRING + available);
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
   103
    }
ec439aaca107 6515172: Runtime.availableProcessors() ignores Linux taskset command
dholmes
parents:
diff changeset
   104
}