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