Blame SOURCES/6515172-pr3346.patch

e24496
# HG changeset patch
e24496
# User shshahma
e24496
# Date 1474535080 25200
e24496
#      Thu Sep 22 02:04:40 2016 -0700
e24496
# Node ID baf64c88538f477d7f5a0cf90b670108ac312375
e24496
# Parent  62212568179b76b5ebe7b0129ddeed7b268b0bc0
e24496
6515172, PR3346: Runtime.availableProcessors() ignores Linux taskset command
e24496
Summary: extract processor count from sched_getaffinity mask
e24496
Reviewed-by: dholmes, gthornbr
e24496
e24496
diff --git a/src/os/linux/vm/globals_linux.hpp b/src/os/linux/vm/globals_linux.hpp
e24496
--- openjdk/hotspot/src/os/linux/vm/globals_linux.hpp
e24496
+++ openjdk/hotspot/src/os/linux/vm/globals_linux.hpp
e24496
@@ -1,5 +1,5 @@
e24496
 /*
e24496
- * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
e24496
+ * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
e24496
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e24496
  *
e24496
  * This code is free software; you can redistribute it and/or modify it
e24496
@@ -47,7 +47,10 @@
e24496
           "Load DLLs with executable-stack attribute in the VM Thread") \
e24496
                                                                         \
e24496
   product(bool, UseSHM, false,                                          \
e24496
-          "Use SYSV shared memory for large pages")
e24496
+          "Use SYSV shared memory for large pages")                     \
e24496
+                                                                        \
e24496
+  diagnostic(bool, PrintActiveCpus, false,                              \
e24496
+          "Print the number of CPUs detected in os::active_processor_count")
e24496
 
e24496
 //
e24496
 // Defines Linux-specific default values. The flags are available on all
e24496
diff --git a/src/os/linux/vm/os_linux.cpp b/src/os/linux/vm/os_linux.cpp
e24496
--- openjdk/hotspot/src/os/linux/vm/os_linux.cpp
e24496
+++ openjdk/hotspot/src/os/linux/vm/os_linux.cpp
e24496
@@ -1,5 +1,5 @@
e24496
 /*
e24496
- * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
e24496
+ * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
e24496
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e24496
  *
e24496
  * This code is free software; you can redistribute it and/or modify it
e24496
@@ -104,6 +104,14 @@
e24496
 
e24496
 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
e24496
 
e24496
+#ifndef _GNU_SOURCE
e24496
+  #define _GNU_SOURCE
e24496
+  #include <sched.h>
e24496
+  #undef _GNU_SOURCE
e24496
+#else
e24496
+  #include <sched.h>
e24496
+#endif
e24496
+
e24496
 // if RUSAGE_THREAD for getrusage() has not been defined, do it here. The code calling
e24496
 // getrusage() is prepared to handle the associated failure.
e24496
 #ifndef RUSAGE_THREAD
e24496
@@ -5027,12 +5035,42 @@
e24496
   }
e24496
 };
e24496
 
e24496
+static int os_cpu_count(const cpu_set_t* cpus) {
e24496
+  int count = 0;
e24496
+  // only look up to the number of configured processors
e24496
+  for (int i = 0; i < os::processor_count(); i++) {
e24496
+    if (CPU_ISSET(i, cpus)) {
e24496
+      count++;
e24496
+    }
e24496
+  }
e24496
+  return count;
e24496
+}
e24496
+
e24496
+// Get the current number of available processors for this process.
e24496
+// This value can change at any time during a process's lifetime.
e24496
+// sched_getaffinity gives an accurate answer as it accounts for cpusets.
e24496
+// If anything goes wrong we fallback to returning the number of online
e24496
+// processors - which can be greater than the number available to the process.
e24496
 int os::active_processor_count() {
e24496
-  // Linux doesn't yet have a (official) notion of processor sets,
e24496
-  // so just return the number of online processors.
e24496
-  int online_cpus = ::sysconf(_SC_NPROCESSORS_ONLN);
e24496
-  assert(online_cpus > 0 && online_cpus <= processor_count(), "sanity check");
e24496
-  return online_cpus;
e24496
+  cpu_set_t cpus;  // can represent at most 1024 (CPU_SETSIZE) processors
e24496
+  int cpus_size = sizeof(cpu_set_t);
e24496
+  int cpu_count = 0;
e24496
+
e24496
+  // pid 0 means the current thread - which we have to assume represents the process
e24496
+  if (sched_getaffinity(0, cpus_size, &cpus) == 0) {
e24496
+    cpu_count = os_cpu_count(&cpus);
e24496
+    if (PrintActiveCpus) {
e24496
+      tty->print_cr("active_processor_count: sched_getaffinity processor count: %d", cpu_count);
e24496
+    }
e24496
+  }
e24496
+  else {
e24496
+    cpu_count = ::sysconf(_SC_NPROCESSORS_ONLN);
e24496
+    warning("sched_getaffinity failed (%s)- using online processor count (%d) "
e24496
+            "which may exceed available processors", strerror(errno), cpu_count);
e24496
+  }
e24496
+
e24496
+  assert(cpu_count > 0 && cpu_count <= processor_count(), "sanity check");
e24496
+  return cpu_count;
e24496
 }
e24496
 
e24496
 void os::set_native_thread_name(const char *name) {
e24496
diff --git a/test/runtime/os/AvailableProcessors.java b/test/runtime/os/AvailableProcessors.java
e24496
new file mode 100644
e24496
--- /dev/null
e24496
+++ openjdk/hotspot/test/runtime/os/AvailableProcessors.java
e24496
@@ -0,0 +1,103 @@
e24496
+/*
e24496
+ * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
e24496
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e24496
+ *
e24496
+ * This code is free software; you can redistribute it and/or modify it
e24496
+ * under the terms of the GNU General Public License version 2 only, as
e24496
+ * published by the Free Software Foundation.
e24496
+ *
e24496
+ * This code is distributed in the hope that it will be useful, but WITHOUT
e24496
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e24496
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e24496
+ * version 2 for more details (a copy is included in the LICENSE file that
e24496
+ * accompanied this code).
e24496
+ *
e24496
+ * You should have received a copy of the GNU General Public License version
e24496
+ * 2 along with this work; if not, write to the Free Software Foundation,
e24496
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e24496
+ *
e24496
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e24496
+ * or visit www.oracle.com if you need additional information or have any
e24496
+ * questions.
e24496
+ */
e24496
+import java.io.File;
e24496
+import com.oracle.java.testlibrary.ProcessTools;
e24496
+import com.oracle.java.testlibrary.OutputAnalyzer;
e24496
+import java.util.ArrayList;
e24496
+
e24496
+/*
e24496
+ * @test
e24496
+ * @bug 6515172
e24496
+ * @summary Check that availableProcessors reports the correct value when running in a cpuset on linux
e24496
+ * @requires os.family == "linux"
e24496
+ * @library /testlibrary
e24496
+ * @build com.oracle.java.testlibrary.*
e24496
+ * @run driver AvailableProcessors
e24496
+ */
e24496
+public class AvailableProcessors {
e24496
+
e24496
+    static final String SUCCESS_STRING = "Found expected processors: ";
e24496
+
e24496
+    public static void main(String[] args) throws Throwable {
e24496
+        if (args.length > 0)
e24496
+            checkProcessors(Integer.parseInt(args[0]));
e24496
+        else {
e24496
+            // run ourselves under different cpu configurations
e24496
+            // using the taskset command
e24496
+            String taskset;
e24496
+            final String taskset1 = "/bin/taskset";
e24496
+            final String taskset2 = "/usr/bin/taskset";
e24496
+            if (new File(taskset1).exists())
e24496
+                taskset = taskset1;
e24496
+            else if (new File(taskset2).exists())
e24496
+                taskset = taskset2;
e24496
+            else {
e24496
+                System.out.println("Skipping test: could not find taskset command");
e24496
+                return;
e24496
+            }
e24496
+
e24496
+            int available = Runtime.getRuntime().availableProcessors();
e24496
+
e24496
+            if (available == 1) {
e24496
+                System.out.println("Skipping test: only one processor available");
e24496
+                return;
e24496
+            }
e24496
+
e24496
+            // Get the java command we want to execute
e24496
+            // Enable logging for easier failure diagnosis
e24496
+            ProcessBuilder master =
e24496
+                    ProcessTools.createJavaProcessBuilder(false,
e24496
+                                                          "-XX:+UnlockDiagnosticVMOptions",
e24496
+                                                          "-XX:+PrintActiveCpus",
e24496
+                                                          "AvailableProcessors");
e24496
+
e24496
+            int[] expected = new int[] { 1, available/2, available-1, available };
e24496
+
e24496
+            for (int i : expected) {
e24496
+                System.out.println("Testing for " + i + " processors ...");
e24496
+                int max = i - 1;
e24496
+                ArrayList<String> cmdline = new ArrayList<>(master.command());
e24496
+                // prepend taskset command
e24496
+                cmdline.add(0, "0-" + max);
e24496
+                cmdline.add(0, "-c");
e24496
+                cmdline.add(0, taskset);
e24496
+                // append expected processor count
e24496
+                cmdline.add(String.valueOf(i));
e24496
+                ProcessBuilder pb = new ProcessBuilder(cmdline);
e24496
+                System.out.println("Final command line: " +
e24496
+                                   ProcessTools.getCommandLine(pb));
e24496
+                OutputAnalyzer output = ProcessTools.executeProcess(pb);
e24496
+                output.shouldContain(SUCCESS_STRING);
e24496
+            }
e24496
+        }
e24496
+    }
e24496
+
e24496
+    static void checkProcessors(int expected) {
e24496
+        int available = Runtime.getRuntime().availableProcessors();
e24496
+        if (available != expected)
e24496
+            throw new Error("Expected " + expected + " processors, but found "
e24496
+                            + available);
e24496
+        else
e24496
+            System.out.println(SUCCESS_STRING + available);
e24496
+    }
e24496
+}