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.ProcessTools;
  25 import jdk.test.lib.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 /testlibrary
  35  * @build jdk.test.lib.*
  36  * @run driver AvailableProcessors
  37  */
  38 public class AvailableProcessors {
  39 
  40     static final String SUCCESS_STRING = "Found expected processors: ";
  41 
  42     public static void main(String[] args) throws Exception {
  43         if (args.length > 0)
  44             checkProcessors(Integer.parseInt(args[0]));
  45         else {
  46             // run ourselves under different cpu configurations
  47             // using the taskset command
  48             String taskset;
  49             final String taskset1 = "/bin/taskset";
  50             final String taskset2 = "/usr/bin/taskset";
  51             if (new File(taskset1).exists())
  52                 taskset = taskset1;
  53             else if (new File(taskset2).exists())
  54                 taskset = taskset2;
  55             else {
  56                 System.out.println("Skipping test: could not find taskset command");
  57                 return;
  58             }
  59 
  60             int available = Runtime.getRuntime().availableProcessors();
  61 
  62             if (available == 1) {
  63                 System.out.println("Skipping test: only one processor available");
  64                 return;
  65             }
  66 
  67             // Get the java command we want to execute
  68             // Enable logging for easier failure diagnosis
  69             ProcessBuilder master =
  70                     ProcessTools.createJavaProcessBuilder(false,
  71                                                           "-Xlog:os=trace",
  72                                                           "AvailableProcessors");
  73 
  74             int[] expected = new int[] { 1, available/2, available-1, available };
  75 
  76             for (int i : expected) {
  77                 System.out.println("Testing for " + i + " processors ...");
  78                 int max = i - 1;
  79                 ArrayList<String> cmdline = new ArrayList<>(master.command());
  80                 // prepend taskset command
  81                 cmdline.add(0, "0-" + max);
  82                 cmdline.add(0, "-c");
  83                 cmdline.add(0, taskset);
  84                 // append expected processor count
  85                 cmdline.add(String.valueOf(i));
  86                 ProcessBuilder pb = new ProcessBuilder(cmdline);
  87                 System.out.println("Final command line: " +
  88                                    ProcessTools.getCommandLine(pb));
  89                 OutputAnalyzer output = ProcessTools.executeProcess(pb);
  90                 output.shouldContain(SUCCESS_STRING);
  91             }
  92         }
  93     }
  94 
  95     static void checkProcessors(int expected) {
  96         int available = Runtime.getRuntime().availableProcessors();
  97         // available can dynamically drop below expected due to aggressive power management
  98         // but we should never have more than expected, else taskset is broken
  99         if (available <= 0 || available > expected)
 100             throw new Error("Expected " + expected + " processors, but found "
 101                             + available);
 102         else
 103             System.out.println(SUCCESS_STRING + available);
 104     }
 105 }