8203227: Introduce os::processor_id() for Linux and Solaris
authorpliden
Fri, 18 May 2018 15:21:23 +0200
changeset 50178 7b0291382aff
parent 50177 9806f23d5e5e
child 50179 d9bc8557ae16
8203227: Introduce os::processor_id() for Linux and Solaris Reviewed-by: dholmes, rehn
src/hotspot/os/linux/os_linux.cpp
src/hotspot/os/solaris/os_solaris.cpp
src/hotspot/share/runtime/os.hpp
--- a/src/hotspot/os/linux/os_linux.cpp	Fri May 18 08:47:42 2018 -0400
+++ b/src/hotspot/os/linux/os_linux.cpp	Fri May 18 15:21:23 2018 +0200
@@ -2872,6 +2872,10 @@
     set_sched_getcpu(CAST_TO_FN_PTR(sched_getcpu_func_t,
                                     (void*)&sched_getcpu_syscall));
   }
+
+  if (sched_getcpu() == -1) {
+    vm_exit_during_initialization("getcpu(2) system call not supported by kernel");
+  }
 }
 
 // Something to do with the numa-aware allocator needs these symbols
@@ -5229,6 +5233,12 @@
   return active_cpus;
 }
 
+uint os::processor_id() {
+  const int id = Linux::sched_getcpu();
+  assert(id >= 0 && id < _processor_count, "Invalid processor id");
+  return (uint)id;
+}
+
 void os::set_native_thread_name(const char *name) {
   if (Linux::_pthread_setname_np) {
     char buf [16]; // according to glibc manpage, 16 chars incl. '/0'
--- a/src/hotspot/os/solaris/os_solaris.cpp	Fri May 18 08:47:42 2018 -0400
+++ b/src/hotspot/os/solaris/os_solaris.cpp	Fri May 18 15:21:23 2018 +0200
@@ -291,6 +291,12 @@
                                      (julong)sysconf(_SC_PAGESIZE);
 }
 
+uint os::processor_id() {
+  const processorid_t id = ::getcpuid();
+  assert(id >= 0 && id < _processor_count, "Invalid processor id");
+  return (uint)id;
+}
+
 int os::active_processor_count() {
   // User has overridden the number of active processors
   if (ActiveProcessorCount > 0) {
--- a/src/hotspot/share/runtime/os.hpp	Fri May 18 08:47:42 2018 -0400
+++ b/src/hotspot/share/runtime/os.hpp	Fri May 18 15:21:23 2018 +0200
@@ -233,6 +233,10 @@
   static bool has_allocatable_memory_limit(julong* limit);
   static bool is_server_class_machine();
 
+  // Returns the id of the processor on which the calling thread is currently executing.
+  // The returned value is guaranteed to be between 0 and (os::processor_count() - 1).
+  static uint processor_id();
+
   // number of CPUs
   static int processor_count() {
     return _processor_count;