ovs-numa: Replace name 'cpu_socket' with 'numa_node'.
[cascardo/ovs.git] / lib / ovs-numa.c
index 44df40f..3583663 100644 (file)
 
 VLOG_DEFINE_THIS_MODULE(ovs_numa);
 
-#define MAX_CPU_SOCKETS 128
+#define MAX_NUMA_NODES 128
 
-/* Cpu socket. */
-struct cpu_socket {
-    struct hmap_node hmap_node;     /* In the 'all_cpu_sockets'. */
-    struct list cores;              /* List of cpu cores on the socket. */
-    int socket_id;                  /* Socket id. */
+/* numa node. */
+struct numa_node {
+    struct hmap_node hmap_node;     /* In the 'all_numa_nodes'. */
+    struct list cores;              /* List of cpu cores on the numa node. */
+    int numa_id;                    /* numa node id. */
 };
 
-/* Cpu core on a cpu socket. */
+/* Cpu core on a numa node. */
 struct cpu_core {
     struct hmap_node hmap_node;/* In the 'all_cpu_cores'. */
-    struct list list_node;     /* In 'cpu_socket->cores' list. */
-    struct cpu_socket *socket; /* Socket containing the core. */
+    struct list list_node;     /* In 'numa_node->cores' list. */
+    struct numa_node *numa;    /* numa node containing the core. */
     int core_id;               /* Core id. */
     bool pinned;               /* If a thread has been pinned to the core. */
 };
 
-/* Contains all 'struct cpu_socket's. */
-static struct hmap all_cpu_sockets = HMAP_INITIALIZER(&all_cpu_sockets);
+/* Contains all 'struct numa_node's. */
+static struct hmap all_numa_nodes = HMAP_INITIALIZER(&all_numa_nodes);
 /* Contains all 'struct cpu_core's. */
 static struct hmap all_cpu_cores = HMAP_INITIALIZER(&all_cpu_cores);
-/* True if socket and core info are correctly extracted. */
-static bool found_sockets_and_cores;
+/* True if numa node and core info are correctly extracted. */
+static bool found_numa_and_core;
 
 /* Returns true if 'str' contains all digits.  Returns false otherwise. */
 static bool
@@ -68,15 +68,15 @@ contain_all_digits(const char *str)
     return str[strspn(str, "0123456789")] == '\0';
 }
 
-/* Discovers all cpu sockets and the corresponding cpu cores for each socket.
- * Constructs the 'struct cpu_socket' and 'struct cpu_core'. */
+/* Discovers all numa nodes and the corresponding cpu cores.
+ * Constructs the 'struct numa_node' and 'struct cpu_core'. */
 static void
-discover_sockets_and_cores(void)
+discover_numa_and_core(void)
 {
     int n_cpus = 0;
     int i;
 
-    for (i = 0; i < MAX_CPU_SOCKETS; i++) {
+    for (i = 0; i < MAX_NUMA_NODES; i++) {
         DIR *dir;
         char* path;
 
@@ -84,14 +84,14 @@ discover_sockets_and_cores(void)
         path = xasprintf("/sys/devices/system/node/node%d", i);
         dir = opendir(path);
 
-        /* Creates 'struct cpu_socket' if the 'dir' is non-null. */
+        /* Creates 'struct numa_node' if the 'dir' is non-null. */
         if (dir) {
-            struct cpu_socket *s = xzalloc(sizeof *s);
+            struct numa_node *n = xzalloc(sizeof *n);
             struct dirent *subdir;
 
-            hmap_insert(&all_cpu_sockets, &s->hmap_node, hash_int(i, 0));
-            list_init(&s->cores);
-            s->socket_id = i;
+            hmap_insert(&all_numa_nodes, &n->hmap_node, hash_int(i, 0));
+            list_init(&n->cores);
+            n->numa_id = i;
 
             while ((subdir = readdir(dir)) != NULL) {
                 if (!strncmp(subdir->d_name, "cpu", 3)
@@ -102,13 +102,13 @@ discover_sockets_and_cores(void)
                     core_id = strtoul(subdir->d_name + 3, NULL, 10);
                     hmap_insert(&all_cpu_cores, &c->hmap_node,
                                 hash_int(core_id, 0));
-                    list_insert(&s->cores, &c->list_node);
+                    list_insert(&n->cores, &c->list_node);
                     c->core_id = core_id;
                     n_cpus++;
                 }
             }
-            VLOG_INFO("Discovered %"PRIuSIZE" CPU cores on CPU socket %d",
-                      list_size(&s->cores), s->socket_id);
+            VLOG_INFO("Discovered %"PRIuSIZE" CPU cores on NUMA node %d",
+                      list_size(&n->cores), n->numa_id);
             free(path);
             closedir(dir);
         } else {
@@ -121,10 +121,10 @@ discover_sockets_and_cores(void)
         }
     }
 
-    VLOG_INFO("Discovered %"PRIuSIZE" CPU Sockets and %d CPU cores",
-               hmap_count(&all_cpu_sockets), n_cpus);
-    if (hmap_count(&all_cpu_sockets) && hmap_count(&all_cpu_cores)) {
-        found_sockets_and_cores = true;
+    VLOG_INFO("Discovered %"PRIuSIZE" NUMA nodes and %d CPU cores",
+               hmap_count(&all_numa_nodes), n_cpus);
+    if (hmap_count(&all_numa_nodes) && hmap_count(&all_cpu_cores)) {
+        found_numa_and_core = true;
     }
 }
 
@@ -135,71 +135,71 @@ ovs_numa_init(void)
     static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
 
     if (ovsthread_once_start(&once)) {
-        discover_sockets_and_cores();
+        discover_numa_and_core();
         ovsthread_once_done(&once);
     }
 }
 
 bool
-ovs_numa_cpu_socket_id_is_valid(int sid)
+ovs_numa_numa_id_is_valid(int numa_id)
 {
-    return sid < ovs_numa_get_n_sockets();
+    return numa_id < ovs_numa_get_n_numas();
 }
 
 bool
-ovs_numa_cpu_core_id_is_valid(int cid)
+ovs_numa_core_id_is_valid(int core_id)
 {
-    return cid < ovs_numa_get_n_cores();
+    return core_id < ovs_numa_get_n_cores();
 }
 
-/* Returns the number of cpu sockets. */
+/* Returns the number of numa nodes. */
 int
-ovs_numa_get_n_sockets(void)
+ovs_numa_get_n_numas(void)
 {
-    return found_sockets_and_cores ? hmap_count(&all_cpu_sockets)
-                                   : OVS_SOCKET_UNSPEC;
+    return found_numa_and_core ? hmap_count(&all_numa_nodes)
+                               : OVS_NUMA_UNSPEC;
 }
 
 /* Returns the number of cpu cores. */
 int
 ovs_numa_get_n_cores(void)
 {
-    return found_sockets_and_cores ? hmap_count(&all_cpu_cores)
-                                   : OVS_CORE_UNSPEC;
+    return found_numa_and_core ? hmap_count(&all_cpu_cores)
+                               : OVS_CORE_UNSPEC;
 }
 
-/* Returns the number of cpu cores on socket. */
+/* Returns the number of cpu cores on numa node. */
 int
-ovs_numa_get_n_cores_on_socket(int socket_id)
+ovs_numa_get_n_cores_on_numa(int numa_id)
 {
-    if (found_sockets_and_cores) {
-        struct cpu_socket *socket;
+    if (found_numa_and_core) {
+        struct numa_node *numa;
 
-        ovs_assert(ovs_numa_cpu_socket_id_is_valid(socket_id));
-        socket = CONTAINER_OF(hmap_first_with_hash(&all_cpu_sockets,
-                                                   hash_int(socket_id, 0)),
-                              struct cpu_socket, hmap_node);
+        ovs_assert(ovs_numa_numa_id_is_valid(numa_id));
+        numa = CONTAINER_OF(hmap_first_with_hash(&all_numa_nodes,
+                                                 hash_int(numa_id, 0)),
+                            struct numa_node, hmap_node);
 
-        return list_size(&socket->cores);
+        return list_size(&numa->cores);
     }
 
     return OVS_CORE_UNSPEC;
 }
 
-/* Returns the number of unpinned cpu cores on socket. */
+/* Returns the number of unpinned cpu cores on numa node. */
 int
-ovs_numa_get_n_unpinned_cores_on_socket(int socket_id)
+ovs_numa_get_n_unpinned_cores_on_numa(int numa_id)
 {
-    if (found_sockets_and_cores) {
-        struct cpu_socket *socket;
+    if (found_numa_and_core) {
+        struct numa_node *numa;
         struct cpu_core *core;
         int count = 0;
 
-        ovs_assert(ovs_numa_cpu_socket_id_is_valid(socket_id));
-        socket = CONTAINER_OF(hmap_first_with_hash(&all_cpu_sockets,
-                                                   hash_int(socket_id, 0)),
-                              struct cpu_socket, hmap_node);
-        LIST_FOR_EACH(core, list_node, &socket->cores) {
+        ovs_assert(ovs_numa_numa_id_is_valid(numa_id));
+        numa = CONTAINER_OF(hmap_first_with_hash(&all_numa_nodes,
+                                                 hash_int(numa_id, 0)),
+                            struct numa_node, hmap_node);
+        LIST_FOR_EACH(core, list_node, &numa->cores) {
             if (!core->pinned) {
                 count++;
             }
@@ -218,7 +218,7 @@ ovs_numa_try_pin_core_specific(int core_id)
 {
     struct cpu_core *core;
 
-    ovs_assert(ovs_numa_cpu_core_id_is_valid(core_id));
+    ovs_assert(ovs_numa_core_id_is_valid(core_id));
 
     core = CONTAINER_OF(hmap_first_with_hash(&all_cpu_cores,
                                              hash_int(core_id, 0)),
@@ -248,21 +248,21 @@ ovs_numa_get_unpinned_core_any(void)
     return OVS_CORE_UNSPEC;
 }
 
-/* Searches through all cores on socket with 'socket_id' for an unpinned core.
- * Returns the core_id if found and sets the 'core->pinned' to true.
+/* Searches through all cores on numa node with 'numa_id' for an unpinned
+ * core.  Returns the core_id if found and sets the 'core->pinned' to true.
  * Otherwise, returns -1. */
 int
-ovs_numa_get_unpinned_core_on_socket(int socket_id)
+ovs_numa_get_unpinned_core_on_numa(int numa_id)
 {
-    struct cpu_socket *socket;
+    struct numa_node *numa;
     struct cpu_core *core;
 
-    ovs_assert(ovs_numa_cpu_socket_id_is_valid(socket_id));
+    ovs_assert(ovs_numa_numa_id_is_valid(numa_id));
 
-    socket = CONTAINER_OF(hmap_first_with_hash(&all_cpu_sockets,
-                                               hash_int(socket_id, 0)),
-                          struct cpu_socket, hmap_node);
-    LIST_FOR_EACH(core, list_node, &socket->cores) {
+    numa = CONTAINER_OF(hmap_first_with_hash(&all_numa_nodes,
+                                             hash_int(numa_id, 0)),
+                        struct numa_node, hmap_node);
+    LIST_FOR_EACH(core, list_node, &numa->cores) {
         if (!core->pinned) {
             core->pinned = true;
             return core->core_id;
@@ -278,7 +278,7 @@ ovs_numa_unpin_core(int core_id)
 {
     struct cpu_core *core;
 
-    ovs_assert(ovs_numa_cpu_core_id_is_valid(core_id));
+    ovs_assert(ovs_numa_core_id_is_valid(core_id));
 
     core = CONTAINER_OF(hmap_first_with_hash(&all_cpu_cores,
                                              hash_int(core_id, 0)),