2 * Copyright (c) 2014 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /* On non-Linux, these functions are defined inline in ovs-numa.h. */
28 #include <sys/types.h>
34 #include "ovs-thread.h"
37 VLOG_DEFINE_THIS_MODULE(ovs_numa);
42 * This module stores the affinity information of numa nodes and cpu cores.
43 * It also provides functions to bookkeep the pin of threads on cpu cores.
45 * It is assumed that the numa node ids and cpu core ids all start from 0 and
46 * range continuously. So, for example, if 'ovs_numa_get_n_cores()' returns N,
47 * user can assume core ids from 0 to N-1 are all valid and there is a
48 * 'struct cpu_core' for each id.
50 * NOTE, the assumption above will fail when cpu hotplug is used. In that
51 * case ovs-numa will not function correctly. For now, add a TODO entry
52 * for addressing it in the future.
54 * TODO: Fix ovs-numa when cpu hotplug is used.
57 #define MAX_NUMA_NODES 128
61 struct hmap_node hmap_node; /* In the 'all_numa_nodes'. */
62 struct list cores; /* List of cpu cores on the numa node. */
63 int numa_id; /* numa node id. */
66 /* Cpu core on a numa node. */
68 struct hmap_node hmap_node;/* In the 'all_cpu_cores'. */
69 struct list list_node; /* In 'numa_node->cores' list. */
70 struct numa_node *numa; /* numa node containing the core. */
71 int core_id; /* Core id. */
72 bool pinned; /* If a thread has been pinned to the core. */
75 /* Contains all 'struct numa_node's. */
76 static struct hmap all_numa_nodes = HMAP_INITIALIZER(&all_numa_nodes);
77 /* Contains all 'struct cpu_core's. */
78 static struct hmap all_cpu_cores = HMAP_INITIALIZER(&all_cpu_cores);
79 /* True if numa node and core info are correctly extracted. */
80 static bool found_numa_and_core;
82 /* Returns true if 'str' contains all digits. Returns false otherwise. */
84 contain_all_digits(const char *str)
86 return str[strspn(str, "0123456789")] == '\0';
89 /* Discovers all numa nodes and the corresponding cpu cores.
90 * Constructs the 'struct numa_node' and 'struct cpu_core'. */
92 discover_numa_and_core(void)
97 for (i = 0; i < MAX_NUMA_NODES; i++) {
101 /* Constructs the path to node /sys/devices/system/nodeX. */
102 path = xasprintf("/sys/devices/system/node/node%d", i);
105 /* Creates 'struct numa_node' if the 'dir' is non-null. */
107 struct numa_node *n = xzalloc(sizeof *n);
108 struct dirent *subdir;
110 hmap_insert(&all_numa_nodes, &n->hmap_node, hash_int(i, 0));
111 list_init(&n->cores);
114 while ((subdir = readdir(dir)) != NULL) {
115 if (!strncmp(subdir->d_name, "cpu", 3)
116 && contain_all_digits(subdir->d_name + 3)){
117 struct cpu_core *c = xzalloc(sizeof *c);
120 core_id = strtoul(subdir->d_name + 3, NULL, 10);
121 hmap_insert(&all_cpu_cores, &c->hmap_node,
122 hash_int(core_id, 0));
123 list_insert(&n->cores, &c->list_node);
124 c->core_id = core_id;
129 VLOG_INFO("Discovered %"PRIuSIZE" CPU cores on NUMA node %d",
130 list_size(&n->cores), n->numa_id);
134 if (errno != ENOENT) {
135 VLOG_WARN("opendir(%s) failed (%s)", path,
136 ovs_strerror(errno));
143 VLOG_INFO("Discovered %"PRIuSIZE" NUMA nodes and %d CPU cores",
144 hmap_count(&all_numa_nodes), n_cpus);
145 if (hmap_count(&all_numa_nodes) && hmap_count(&all_cpu_cores)) {
146 found_numa_and_core = true;
150 /* Extracts the numa node and core info from the 'sysfs'. */
154 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
156 if (ovsthread_once_start(&once)) {
157 discover_numa_and_core();
158 ovsthread_once_done(&once);
163 ovs_numa_numa_id_is_valid(int numa_id)
165 return found_numa_and_core && numa_id < ovs_numa_get_n_numas();
169 ovs_numa_core_id_is_valid(int core_id)
171 return found_numa_and_core && core_id < ovs_numa_get_n_cores();
174 /* Returns the number of numa nodes. */
176 ovs_numa_get_n_numas(void)
178 return found_numa_and_core ? hmap_count(&all_numa_nodes)
182 /* Returns the number of cpu cores. */
184 ovs_numa_get_n_cores(void)
186 return found_numa_and_core ? hmap_count(&all_cpu_cores)
190 /* Given 'core_id', returns the corresponding numa node id. Returns
191 * OVS_NUMA_UNSPEC if 'core_id' is invalid. */
193 ovs_numa_get_numa_id(int core_id)
195 if (ovs_numa_core_id_is_valid(core_id)) {
196 struct cpu_core *core;
198 core = CONTAINER_OF(hmap_first_with_hash(&all_cpu_cores,
199 hash_int(core_id, 0)),
200 struct cpu_core, hmap_node);
202 return core->numa->numa_id;
204 return OVS_NUMA_UNSPEC;
207 /* Returns the number of cpu cores on numa node. Returns OVS_CORE_UNSPEC
208 * if 'numa_id' is invalid. */
210 ovs_numa_get_n_cores_on_numa(int numa_id)
212 if (ovs_numa_numa_id_is_valid(numa_id)) {
213 struct numa_node *numa;
215 numa = CONTAINER_OF(hmap_first_with_hash(&all_numa_nodes,
216 hash_int(numa_id, 0)),
217 struct numa_node, hmap_node);
219 return list_size(&numa->cores);
222 return OVS_CORE_UNSPEC;
225 /* Returns the number of unpinned cpu cores on numa node. Returns
226 * OVS_CORE_UNSPEC if 'numa_id' is invalid. */
228 ovs_numa_get_n_unpinned_cores_on_numa(int numa_id)
230 if (ovs_numa_numa_id_is_valid(numa_id)) {
231 struct numa_node *numa;
232 struct cpu_core *core;
235 numa = CONTAINER_OF(hmap_first_with_hash(&all_numa_nodes,
236 hash_int(numa_id, 0)),
237 struct numa_node, hmap_node);
238 LIST_FOR_EACH(core, list_node, &numa->cores) {
247 return OVS_CORE_UNSPEC;
250 /* Given 'core_id', tries to pin that core. Returns true, if succeeds.
251 * False, if the core has already been pinned or if 'core_id' is invalid. */
253 ovs_numa_try_pin_core_specific(int core_id)
255 if (ovs_numa_core_id_is_valid(core_id)) {
256 struct cpu_core *core;
258 core = CONTAINER_OF(hmap_first_with_hash(&all_cpu_cores,
259 hash_int(core_id, 0)),
260 struct cpu_core, hmap_node);
270 /* Searches through all cores for an unpinned core. Returns the core_id
271 * if found and set the 'core->pinned' to true. Otherwise, returns
272 * OVS_CORE_UNSPEC. */
274 ovs_numa_get_unpinned_core_any(void)
276 struct cpu_core *core;
278 HMAP_FOR_EACH(core, hmap_node, &all_cpu_cores) {
281 return core->core_id;
285 return OVS_CORE_UNSPEC;
288 /* Searches through all cores on numa node with 'numa_id' for an unpinned
289 * core. Returns the core_id if found and sets the 'core->pinned' to true.
290 * Otherwise, returns OVS_CORE_UNSPEC. */
292 ovs_numa_get_unpinned_core_on_numa(int numa_id)
294 if (ovs_numa_numa_id_is_valid(numa_id)) {
295 struct numa_node *numa;
296 struct cpu_core *core;
298 numa = CONTAINER_OF(hmap_first_with_hash(&all_numa_nodes,
299 hash_int(numa_id, 0)),
300 struct numa_node, hmap_node);
301 LIST_FOR_EACH(core, list_node, &numa->cores) {
304 return core->core_id;
309 return OVS_CORE_UNSPEC;
312 /* Resets the 'core->pinned' for the core with 'core_id'. */
314 ovs_numa_unpin_core(int core_id)
316 if (ovs_numa_core_id_is_valid(core_id)) {
317 struct cpu_core *core;
319 core = CONTAINER_OF(hmap_first_with_hash(&all_cpu_cores,
320 hash_int(core_id, 0)),
321 struct cpu_core, hmap_node);
322 core->pinned = false;
326 #endif /* __linux__ */