Blame SOURCES/8175813-pr3394-rh1448880.patch

0ad01d
# HG changeset patch
0ad01d
# User gromero
0ad01d
# Date 1495057954 14400
0ad01d
#      Wed May 17 17:52:34 2017 -0400
0ad01d
# Node ID 74c81011375b5f432df155dcd7b3c9a668b45740
0ad01d
# Parent  4d9931ebf8617b1b06adbc1beee6ed1b58661a8b
0ad01d
8175813: PPC64: "mbind: Invalid argument" when -XX:+UseNUMA is used
0ad01d
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
@@ -2736,8 +2736,9 @@
0ad01d
 bool os::numa_topology_changed()   { return false; }
0ad01d
 
0ad01d
 size_t os::numa_get_groups_num() {
0ad01d
-  int max_node = Linux::numa_max_node();
0ad01d
-  return max_node > 0 ? max_node + 1 : 1;
0ad01d
+  // Return just the number of nodes in which it's possible to allocate memory
0ad01d
+  // (in numa terminology, configured nodes).
0ad01d
+  return Linux::numa_num_configured_nodes();
0ad01d
 }
0ad01d
 
0ad01d
 int os::numa_get_group_id() {
0ad01d
@@ -2751,11 +2752,33 @@
0ad01d
   return 0;
0ad01d
 }
0ad01d
 
0ad01d
+int os::Linux::get_existing_num_nodes() {
0ad01d
+  size_t node;
0ad01d
+  size_t highest_node_number = Linux::numa_max_node();
0ad01d
+  int num_nodes = 0;
0ad01d
+
0ad01d
+  // Get the total number of nodes in the system including nodes without memory.
0ad01d
+  for (node = 0; node <= highest_node_number; node++) {
0ad01d
+    if (isnode_in_existing_nodes(node)) {
0ad01d
+      num_nodes++;
0ad01d
+    }
0ad01d
+  }
0ad01d
+  return num_nodes;
0ad01d
+}
0ad01d
+
0ad01d
 size_t os::numa_get_leaf_groups(int *ids, size_t size) {
0ad01d
-  for (size_t i = 0; i < size; i++) {
0ad01d
-    ids[i] = i;
0ad01d
-  }
0ad01d
-  return size;
0ad01d
+  size_t highest_node_number = Linux::numa_max_node();
0ad01d
+  size_t i = 0;
0ad01d
+
0ad01d
+  // Map all node ids in which is possible to allocate memory. Also nodes are
0ad01d
+  // not always consecutively available, i.e. available from 0 to the highest
0ad01d
+  // node number.
0ad01d
+  for (size_t node = 0; node <= highest_node_number; node++) {
0ad01d
+    if (Linux::isnode_in_configured_nodes(node)) {
0ad01d
+      ids[i++] = node;
0ad01d
+    }
0ad01d
+  }
0ad01d
+  return i;
0ad01d
 }
0ad01d
 
0ad01d
 bool os::get_page_info(char *start, page_info* info) {
0ad01d
@@ -2825,18 +2848,28 @@
0ad01d
                                            libnuma_dlsym(handle, "numa_node_to_cpus")));
0ad01d
       set_numa_max_node(CAST_TO_FN_PTR(numa_max_node_func_t,
0ad01d
                                        libnuma_dlsym(handle, "numa_max_node")));
0ad01d
+      set_numa_num_configured_nodes(CAST_TO_FN_PTR(numa_num_configured_nodes_func_t,
0ad01d
+                                                   libnuma_dlsym(handle, "numa_num_configured_nodes")));
0ad01d
       set_numa_available(CAST_TO_FN_PTR(numa_available_func_t,
0ad01d
                                         libnuma_dlsym(handle, "numa_available")));
0ad01d
       set_numa_tonode_memory(CAST_TO_FN_PTR(numa_tonode_memory_func_t,
0ad01d
                                             libnuma_dlsym(handle, "numa_tonode_memory")));
0ad01d
       set_numa_interleave_memory(CAST_TO_FN_PTR(numa_interleave_memory_func_t,
0ad01d
-                                            libnuma_dlsym(handle, "numa_interleave_memory")));
0ad01d
+                                                libnuma_dlsym(handle, "numa_interleave_memory")));
0ad01d
       set_numa_set_bind_policy(CAST_TO_FN_PTR(numa_set_bind_policy_func_t,
0ad01d
-                                            libnuma_dlsym(handle, "numa_set_bind_policy")));
0ad01d
-
0ad01d
+                                              libnuma_dlsym(handle, "numa_set_bind_policy")));
0ad01d
+      set_numa_bitmask_isbitset(CAST_TO_FN_PTR(numa_bitmask_isbitset_func_t,
0ad01d
+                                               libnuma_dlsym(handle, "numa_bitmask_isbitset")));
0ad01d
+      set_numa_distance(CAST_TO_FN_PTR(numa_distance_func_t,
0ad01d
+                                       libnuma_dlsym(handle, "numa_distance")));
0ad01d
 
0ad01d
       if (numa_available() != -1) {
0ad01d
         set_numa_all_nodes((unsigned long*)libnuma_dlsym(handle, "numa_all_nodes"));
0ad01d
+        set_numa_all_nodes_ptr((struct bitmask **)libnuma_dlsym(handle, "numa_all_nodes_ptr"));
0ad01d
+        set_numa_nodes_ptr((struct bitmask **)libnuma_dlsym(handle, "numa_nodes_ptr"));
0ad01d
+        // Create an index -> node mapping, since nodes are not always consecutive
0ad01d
+        _nindex_to_node = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<int>(0, true);
0ad01d
+        rebuild_nindex_to_node_map();
0ad01d
         // Create a cpu -> node mapping
0ad01d
         _cpu_to_node = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<int>(0, true);
0ad01d
         rebuild_cpu_to_node_map();
0ad01d
@@ -2847,6 +2880,17 @@
0ad01d
   return false;
0ad01d
 }
0ad01d
 
0ad01d
+void os::Linux::rebuild_nindex_to_node_map() {
0ad01d
+  int highest_node_number = Linux::numa_max_node();
0ad01d
+
0ad01d
+  nindex_to_node()->clear();
0ad01d
+  for (int node = 0; node <= highest_node_number; node++) {
0ad01d
+    if (Linux::isnode_in_existing_nodes(node)) {
0ad01d
+      nindex_to_node()->append(node);
0ad01d
+    }
0ad01d
+  }
0ad01d
+}
0ad01d
+
0ad01d
 // rebuild_cpu_to_node_map() constructs a table mapping cpud id to node id.
0ad01d
 // The table is later used in get_node_by_cpu().
0ad01d
 void os::Linux::rebuild_cpu_to_node_map() {
0ad01d
@@ -2866,16 +2910,46 @@
0ad01d
 
0ad01d
   cpu_to_node()->clear();
0ad01d
   cpu_to_node()->at_grow(cpu_num - 1);
0ad01d
-  size_t node_num = numa_get_groups_num();
0ad01d
-
0ad01d
+
0ad01d
+  size_t node_num = get_existing_num_nodes();
0ad01d
+
0ad01d
+  int distance = 0;
0ad01d
+  int closest_distance = INT_MAX;
0ad01d
+  int closest_node = 0;
0ad01d
   unsigned long *cpu_map = NEW_C_HEAP_ARRAY(unsigned long, cpu_map_size, mtInternal);
0ad01d
   for (size_t i = 0; i < node_num; i++) {
0ad01d
-    if (numa_node_to_cpus(i, cpu_map, cpu_map_size * sizeof(unsigned long)) != -1) {
0ad01d
+    // Check if node is configured (not a memory-less node). If it is not, find
0ad01d
+    // the closest configured node.
0ad01d
+    if (!isnode_in_configured_nodes(nindex_to_node()->at(i))) {
0ad01d
+      closest_distance = INT_MAX;
0ad01d
+      // Check distance from all remaining nodes in the system. Ignore distance
0ad01d
+      // from itself and from another non-configured node.
0ad01d
+      for (size_t m = 0; m < node_num; m++) {
0ad01d
+        if (m != i && isnode_in_configured_nodes(nindex_to_node()->at(m))) {
0ad01d
+          distance = numa_distance(nindex_to_node()->at(i), nindex_to_node()->at(m));
0ad01d
+          // If a closest node is found, update. There is always at least one
0ad01d
+          // configured node in the system so there is always at least one node
0ad01d
+          // close.
0ad01d
+          if (distance != 0 && distance < closest_distance) {
0ad01d
+            closest_distance = distance;
0ad01d
+            closest_node = nindex_to_node()->at(m);
0ad01d
+          }
0ad01d
+        }
0ad01d
+      }
0ad01d
+     } else {
0ad01d
+       // Current node is already a configured node.
0ad01d
+       closest_node = nindex_to_node()->at(i);
0ad01d
+     }
0ad01d
+
0ad01d
+    // Get cpus from the original node and map them to the closest node. If node
0ad01d
+    // is a configured node (not a memory-less node), then original node and
0ad01d
+    // closest node are the same.
0ad01d
+    if (numa_node_to_cpus(nindex_to_node()->at(i), cpu_map, cpu_map_size * sizeof(unsigned long)) != -1) {
0ad01d
       for (size_t j = 0; j < cpu_map_valid_size; j++) {
0ad01d
         if (cpu_map[j] != 0) {
0ad01d
           for (size_t k = 0; k < BitsPerCLong; k++) {
0ad01d
             if (cpu_map[j] & (1UL << k)) {
0ad01d
-              cpu_to_node()->at_put(j * BitsPerCLong + k, i);
0ad01d
+              cpu_to_node()->at_put(j * BitsPerCLong + k, closest_node);
0ad01d
             }
0ad01d
           }
0ad01d
         }
0ad01d
@@ -2893,14 +2967,20 @@
0ad01d
 }
0ad01d
 
0ad01d
 GrowableArray<int>* os::Linux::_cpu_to_node;
0ad01d
+GrowableArray<int>* os::Linux::_nindex_to_node;
0ad01d
 os::Linux::sched_getcpu_func_t os::Linux::_sched_getcpu;
0ad01d
 os::Linux::numa_node_to_cpus_func_t os::Linux::_numa_node_to_cpus;
0ad01d
 os::Linux::numa_max_node_func_t os::Linux::_numa_max_node;
0ad01d
+os::Linux::numa_num_configured_nodes_func_t os::Linux::_numa_num_configured_nodes;
0ad01d
 os::Linux::numa_available_func_t os::Linux::_numa_available;
0ad01d
 os::Linux::numa_tonode_memory_func_t os::Linux::_numa_tonode_memory;
0ad01d
 os::Linux::numa_interleave_memory_func_t os::Linux::_numa_interleave_memory;
0ad01d
 os::Linux::numa_set_bind_policy_func_t os::Linux::_numa_set_bind_policy;
0ad01d
+os::Linux::numa_bitmask_isbitset_func_t os::Linux::_numa_bitmask_isbitset;
0ad01d
+os::Linux::numa_distance_func_t os::Linux::_numa_distance;
0ad01d
 unsigned long* os::Linux::_numa_all_nodes;
0ad01d
+struct bitmask* os::Linux::_numa_all_nodes_ptr;
0ad01d
+struct bitmask* os::Linux::_numa_nodes_ptr;
0ad01d
 
0ad01d
 bool os::pd_uncommit_memory(char* addr, size_t size) {
0ad01d
   uintptr_t res = (uintptr_t) ::mmap(addr, size, PROT_NONE,
0ad01d
diff --git a/src/os/linux/vm/os_linux.hpp b/src/os/linux/vm/os_linux.hpp
0ad01d
--- openjdk/hotspot/src/os/linux/vm/os_linux.hpp
0ad01d
+++ openjdk/hotspot/src/os/linux/vm/os_linux.hpp
0ad01d
@@ -67,6 +67,7 @@
0ad01d
   static bool _supports_fast_thread_cpu_time;
0ad01d
 
0ad01d
   static GrowableArray<int>* _cpu_to_node;
0ad01d
+  static GrowableArray<int>* _nindex_to_node;
0ad01d
 
0ad01d
  protected:
0ad01d
 
0ad01d
@@ -94,7 +95,9 @@
0ad01d
   static void set_is_floating_stack()         { _is_floating_stack = true; }
0ad01d
 
0ad01d
   static void rebuild_cpu_to_node_map();
0ad01d
+  static void rebuild_nindex_to_node_map();
0ad01d
   static GrowableArray<int>* cpu_to_node()    { return _cpu_to_node; }
0ad01d
+  static GrowableArray<int>* nindex_to_node()  { return _nindex_to_node; }
0ad01d
 
0ad01d
   static size_t find_large_page_size();
0ad01d
   static size_t setup_large_page_size();
0ad01d
@@ -243,28 +246,41 @@
0ad01d
   typedef int (*sched_getcpu_func_t)(void);
0ad01d
   typedef int (*numa_node_to_cpus_func_t)(int node, unsigned long *buffer, int bufferlen);
0ad01d
   typedef int (*numa_max_node_func_t)(void);
0ad01d
+  typedef int (*numa_num_configured_nodes_func_t)(void);
0ad01d
   typedef int (*numa_available_func_t)(void);
0ad01d
   typedef int (*numa_tonode_memory_func_t)(void *start, size_t size, int node);
0ad01d
   typedef void (*numa_interleave_memory_func_t)(void *start, size_t size, unsigned long *nodemask);
0ad01d
   typedef void (*numa_set_bind_policy_func_t)(int policy);
0ad01d
+  typedef int (*numa_bitmask_isbitset_func_t)(struct bitmask *bmp, unsigned int n);
0ad01d
+  typedef int (*numa_distance_func_t)(int node1, int node2);
0ad01d
 
0ad01d
   static sched_getcpu_func_t _sched_getcpu;
0ad01d
   static numa_node_to_cpus_func_t _numa_node_to_cpus;
0ad01d
   static numa_max_node_func_t _numa_max_node;
0ad01d
+  static numa_num_configured_nodes_func_t _numa_num_configured_nodes;
0ad01d
   static numa_available_func_t _numa_available;
0ad01d
   static numa_tonode_memory_func_t _numa_tonode_memory;
0ad01d
   static numa_interleave_memory_func_t _numa_interleave_memory;
0ad01d
   static numa_set_bind_policy_func_t _numa_set_bind_policy;
0ad01d
+  static numa_bitmask_isbitset_func_t _numa_bitmask_isbitset;
0ad01d
+  static numa_distance_func_t _numa_distance;
0ad01d
   static unsigned long* _numa_all_nodes;
0ad01d
+  static struct bitmask* _numa_all_nodes_ptr;
0ad01d
+  static struct bitmask* _numa_nodes_ptr;
0ad01d
 
0ad01d
   static void set_sched_getcpu(sched_getcpu_func_t func) { _sched_getcpu = func; }
0ad01d
   static void set_numa_node_to_cpus(numa_node_to_cpus_func_t func) { _numa_node_to_cpus = func; }
0ad01d
   static void set_numa_max_node(numa_max_node_func_t func) { _numa_max_node = func; }
0ad01d
+  static void set_numa_num_configured_nodes(numa_num_configured_nodes_func_t func) { _numa_num_configured_nodes = func; }
0ad01d
   static void set_numa_available(numa_available_func_t func) { _numa_available = func; }
0ad01d
   static void set_numa_tonode_memory(numa_tonode_memory_func_t func) { _numa_tonode_memory = func; }
0ad01d
   static void set_numa_interleave_memory(numa_interleave_memory_func_t func) { _numa_interleave_memory = func; }
0ad01d
   static void set_numa_set_bind_policy(numa_set_bind_policy_func_t func) { _numa_set_bind_policy = func; }
0ad01d
+  static void set_numa_bitmask_isbitset(numa_bitmask_isbitset_func_t func) { _numa_bitmask_isbitset = func; }
0ad01d
+  static void set_numa_distance(numa_distance_func_t func) { _numa_distance = func; }
0ad01d
   static void set_numa_all_nodes(unsigned long* ptr) { _numa_all_nodes = ptr; }
0ad01d
+  static void set_numa_all_nodes_ptr(struct bitmask **ptr) { _numa_all_nodes_ptr = *ptr; }
0ad01d
+  static void set_numa_nodes_ptr(struct bitmask **ptr) { _numa_nodes_ptr = *ptr; }
0ad01d
   static int sched_getcpu_syscall(void);
0ad01d
 public:
0ad01d
   static int sched_getcpu()  { return _sched_getcpu != NULL ? _sched_getcpu() : -1; }
0ad01d
@@ -272,6 +288,9 @@
0ad01d
     return _numa_node_to_cpus != NULL ? _numa_node_to_cpus(node, buffer, bufferlen) : -1;
0ad01d
   }
0ad01d
   static int numa_max_node() { return _numa_max_node != NULL ? _numa_max_node() : -1; }
0ad01d
+  static int numa_num_configured_nodes() {
0ad01d
+    return _numa_num_configured_nodes != NULL ? _numa_num_configured_nodes() : -1;
0ad01d
+  }
0ad01d
   static int numa_available() { return _numa_available != NULL ? _numa_available() : -1; }
0ad01d
   static int numa_tonode_memory(void *start, size_t size, int node) {
0ad01d
     return _numa_tonode_memory != NULL ? _numa_tonode_memory(start, size, node) : -1;
0ad01d
@@ -286,7 +305,25 @@
0ad01d
       _numa_set_bind_policy(policy);
0ad01d
     }
0ad01d
   }
0ad01d
+  static int numa_distance(int node1, int node2) {
0ad01d
+    return _numa_distance != NULL ? _numa_distance(node1, node2) : -1;
0ad01d
+  }
0ad01d
   static int get_node_by_cpu(int cpu_id);
0ad01d
+  static int get_existing_num_nodes();
0ad01d
+  // Check if numa node is configured (non-zero memory node).
0ad01d
+  static bool isnode_in_configured_nodes(unsigned int n) {
0ad01d
+    if (_numa_bitmask_isbitset != NULL && _numa_all_nodes_ptr != NULL) {
0ad01d
+      return _numa_bitmask_isbitset(_numa_all_nodes_ptr, n);
0ad01d
+    } else
0ad01d
+      return 0;
0ad01d
+  }
0ad01d
+  // Check if numa node exists in the system (including zero memory nodes).
0ad01d
+  static bool isnode_in_existing_nodes(unsigned int n) {
0ad01d
+    if (_numa_bitmask_isbitset != NULL && _numa_nodes_ptr != NULL) {
0ad01d
+      return _numa_bitmask_isbitset(_numa_nodes_ptr, n);
0ad01d
+    } else
0ad01d
+      return 0;
0ad01d
+  }
0ad01d
 };
0ad01d
 
0ad01d