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.
26 #include <sys/types.h>
28 #endif /* __linux__ */
31 #include "openvswitch/hmap.h"
32 #include "openvswitch/list.h"
33 #include "ovs-thread.h"
34 #include "openvswitch/vlog.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, this module should only be used by the main thread.
52 * NOTE, the assumption above will fail when cpu hotplug is used. In that
53 * case ovs-numa will not function correctly. For now, add a TODO entry
54 * for addressing it in the future.
56 * TODO: Fix ovs-numa when cpu hotplug is used.
59 #define MAX_NUMA_NODES 128
63 struct hmap_node hmap_node
; /* In the 'all_numa_nodes'. */
64 struct ovs_list cores
; /* List of cpu cores on the numa node. */
65 int numa_id
; /* numa node id. */
68 /* Cpu core on a numa node. */
70 struct hmap_node hmap_node
;/* In the 'all_cpu_cores'. */
71 struct ovs_list list_node
; /* In 'numa_node->cores' list. */
72 struct numa_node
*numa
; /* numa node containing the core. */
73 unsigned core_id
; /* Core id. */
76 /* Contains all 'struct numa_node's. */
77 static struct hmap all_numa_nodes
= HMAP_INITIALIZER(&all_numa_nodes
);
78 /* Contains all 'struct cpu_core's. */
79 static struct hmap all_cpu_cores
= HMAP_INITIALIZER(&all_cpu_cores
);
80 /* True if numa node and core info are correctly extracted. */
81 static bool found_numa_and_core
;
82 /* True if the module was initialized with dummy options. In this case, the
83 * module must not interact with the actual cpus/nodes in the system. */
84 static bool dummy_numa
= false;
85 /* If 'dummy_numa' is true, contains a copy of the dummy numa configuration
87 static char *dummy_config
;
89 static struct numa_node
*get_numa_by_numa_id(int numa_id
);
92 /* Returns true if 'str' contains all digits. Returns false otherwise. */
94 contain_all_digits(const char *str
)
96 return str
[strspn(str
, "0123456789")] == '\0';
98 #endif /* __linux__ */
100 static struct numa_node
*
101 insert_new_numa_node(int numa_id
)
103 struct numa_node
*n
= xzalloc(sizeof *n
);
105 hmap_insert(&all_numa_nodes
, &n
->hmap_node
, hash_int(numa_id
, 0));
106 ovs_list_init(&n
->cores
);
107 n
->numa_id
= numa_id
;
112 static struct cpu_core
*
113 insert_new_cpu_core(struct numa_node
*n
, unsigned core_id
)
115 struct cpu_core
*c
= xzalloc(sizeof *c
);
117 hmap_insert(&all_cpu_cores
, &c
->hmap_node
, hash_int(core_id
, 0));
118 ovs_list_insert(&n
->cores
, &c
->list_node
);
119 c
->core_id
= core_id
;
125 /* Has the same effect as discover_numa_and_core(), but instead of reading
126 * sysfs entries, extracts the info from 'dummy_config'.
128 * 'dummy_config' lists the numa_ids of each CPU separated by a comma, e.g.
129 * - "0,0,0,0": four cores on numa socket 0.
130 * - "0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1": 16 cores on two numa sockets.
131 * - "0,0,0,0,1,1,1,1": 8 cores on two numa sockets.
133 * The different numa ids must be consecutives or the function will abort. */
135 discover_numa_and_core_dummy(const char *dummy_config
)
137 char *conf
= xstrdup(dummy_config
);
138 char *id
, *saveptr
= NULL
;
140 long max_numa_id
= 0;
142 for (id
= strtok_r(conf
, ",", &saveptr
); id
;
143 id
= strtok_r(NULL
, ",", &saveptr
)) {
144 struct hmap_node
*hnode
;
148 numa_id
= strtol(id
, NULL
, 10);
149 if (numa_id
< 0 || numa_id
>= MAX_NUMA_NODES
) {
150 VLOG_WARN("Invalid numa node %ld", numa_id
);
154 max_numa_id
= MAX(max_numa_id
, numa_id
);
156 hnode
= hmap_first_with_hash(&all_numa_nodes
, hash_int(numa_id
, 0));
159 n
= CONTAINER_OF(hnode
, struct numa_node
, hmap_node
);
161 n
= insert_new_numa_node(numa_id
);
164 insert_new_cpu_core(n
, i
);
171 if (max_numa_id
+ 1 != hmap_count(&all_numa_nodes
)) {
172 ovs_fatal(0, "dummy numa contains non consecutive numa ids");
176 /* Discovers all numa nodes and the corresponding cpu cores.
177 * Constructs the 'struct numa_node' and 'struct cpu_core'. */
179 discover_numa_and_core(void)
184 bool numa_supported
= true;
186 /* Check if NUMA supported on this system. */
187 dir
= opendir("/sys/devices/system/node");
189 if (!dir
&& errno
== ENOENT
) {
190 numa_supported
= false;
196 for (i
= 0; i
< MAX_NUMA_NODES
; i
++) {
199 if (numa_supported
) {
200 /* Constructs the path to node /sys/devices/system/nodeX. */
201 path
= xasprintf("/sys/devices/system/node/node%d", i
);
203 path
= xasprintf("/sys/devices/system/cpu/");
208 /* Creates 'struct numa_node' if the 'dir' is non-null. */
211 struct dirent
*subdir
;
213 n
= insert_new_numa_node(i
);
215 while ((subdir
= readdir(dir
)) != NULL
) {
216 if (!strncmp(subdir
->d_name
, "cpu", 3)
217 && contain_all_digits(subdir
->d_name
+ 3)) {
220 core_id
= strtoul(subdir
->d_name
+ 3, NULL
, 10);
221 insert_new_cpu_core(n
, core_id
);
225 } else if (errno
!= ENOENT
) {
226 VLOG_WARN("opendir(%s) failed (%s)", path
,
227 ovs_strerror(errno
));
231 if (!dir
|| !numa_supported
) {
235 #endif /* __linux__ */
238 /* Gets 'struct cpu_core' by 'core_id'. */
239 static struct cpu_core
*
240 get_core_by_core_id(unsigned core_id
)
242 struct cpu_core
*core
;
244 HMAP_FOR_EACH_WITH_HASH (core
, hmap_node
, hash_int(core_id
, 0),
246 if (core
->core_id
== core_id
) {
254 /* Gets 'struct numa_node' by 'numa_id'. */
255 static struct numa_node
*
256 get_numa_by_numa_id(int numa_id
)
258 struct numa_node
*numa
;
260 HMAP_FOR_EACH_WITH_HASH (numa
, hmap_node
, hash_int(numa_id
, 0),
262 if (numa
->numa_id
== numa_id
) {
273 ovs_numa_init__(const char *dummy_config
)
275 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
277 if (ovsthread_once_start(&once
)) {
278 const struct numa_node
*n
;
281 discover_numa_and_core();
283 discover_numa_and_core_dummy(dummy_config
);
286 HMAP_FOR_EACH(n
, hmap_node
, &all_numa_nodes
) {
287 VLOG_INFO("Discovered %"PRIuSIZE
" CPU cores on NUMA node %d",
288 ovs_list_size(&n
->cores
), n
->numa_id
);
291 VLOG_INFO("Discovered %"PRIuSIZE
" NUMA nodes and %"PRIuSIZE
" CPU cores",
292 hmap_count(&all_numa_nodes
), hmap_count(&all_cpu_cores
));
294 if (hmap_count(&all_numa_nodes
) && hmap_count(&all_cpu_cores
)) {
295 found_numa_and_core
= true;
298 ovsthread_once_done(&once
);
306 /* Extracts the numa node and core info from the 'config'. This is useful for
307 * testing purposes. The function must be called once, before ovs_numa_init().
309 * The format of 'config' is explained in the comment above
310 * discover_numa_and_core_dummy().*/
312 ovs_numa_set_dummy(const char *config
)
317 dummy_config
= xstrdup(config
);
320 /* Initializes the numa module. */
325 ovs_numa_init__(dummy_config
);
327 ovs_numa_init__(NULL
);
332 ovs_numa_numa_id_is_valid(int numa_id
)
334 return found_numa_and_core
&& numa_id
< ovs_numa_get_n_numas();
338 ovs_numa_core_id_is_valid(unsigned core_id
)
340 return found_numa_and_core
&& core_id
< ovs_numa_get_n_cores();
343 /* Returns the number of numa nodes. */
345 ovs_numa_get_n_numas(void)
347 return found_numa_and_core
? hmap_count(&all_numa_nodes
)
351 /* Returns the number of cpu cores. */
353 ovs_numa_get_n_cores(void)
355 return found_numa_and_core
? hmap_count(&all_cpu_cores
)
359 /* Given 'core_id', returns the corresponding numa node id. Returns
360 * OVS_NUMA_UNSPEC if 'core_id' is invalid. */
362 ovs_numa_get_numa_id(unsigned core_id
)
364 struct cpu_core
*core
= get_core_by_core_id(core_id
);
367 return core
->numa
->numa_id
;
370 return OVS_NUMA_UNSPEC
;
373 /* Returns the number of cpu cores on numa node. Returns OVS_CORE_UNSPEC
374 * if 'numa_id' is invalid. */
376 ovs_numa_get_n_cores_on_numa(int numa_id
)
378 struct numa_node
*numa
= get_numa_by_numa_id(numa_id
);
381 return ovs_list_size(&numa
->cores
);
384 return OVS_CORE_UNSPEC
;
387 static struct ovs_numa_dump
*
388 ovs_numa_dump_create(void)
390 struct ovs_numa_dump
*dump
= xmalloc(sizeof *dump
);
392 hmap_init(&dump
->cores
);
393 hmap_init(&dump
->numas
);
399 ovs_numa_dump_add(struct ovs_numa_dump
*dump
, int numa_id
, int core_id
)
401 struct ovs_numa_info_core
*c
= xzalloc(sizeof *c
);
402 struct ovs_numa_info_numa
*n
;
404 c
->numa_id
= numa_id
;
405 c
->core_id
= core_id
;
406 hmap_insert(&dump
->cores
, &c
->hmap_node
, hash_2words(numa_id
, core_id
));
408 HMAP_FOR_EACH_WITH_HASH (n
, hmap_node
, hash_int(numa_id
, 0),
410 if (n
->numa_id
== numa_id
) {
416 n
= xzalloc(sizeof *n
);
417 n
->numa_id
= numa_id
;
419 hmap_insert(&dump
->numas
, &n
->hmap_node
, hash_int(numa_id
, 0));
422 /* Given the 'numa_id', returns dump of all cores on the numa node. */
423 struct ovs_numa_dump
*
424 ovs_numa_dump_cores_on_numa(int numa_id
)
426 struct ovs_numa_dump
*dump
= ovs_numa_dump_create();
427 struct numa_node
*numa
= get_numa_by_numa_id(numa_id
);
430 struct cpu_core
*core
;
432 LIST_FOR_EACH (core
, list_node
, &numa
->cores
) {
433 ovs_numa_dump_add(dump
, numa
->numa_id
, core
->core_id
);
440 struct ovs_numa_dump
*
441 ovs_numa_dump_cores_with_cmask(const char *cmask
)
443 struct ovs_numa_dump
*dump
= ovs_numa_dump_create();
447 /* Ignore leading 0x. */
449 if (!strncmp(cmask
, "0x", 2) || !strncmp(cmask
, "0X", 2)) {
453 for (int i
= strlen(cmask
) - 1; i
>= end_idx
; i
--) {
457 bin
= hexit_value(hex
);
459 VLOG_WARN("Invalid cpu mask: %c", cmask
[i
]);
463 for (int j
= 0; j
< 4; j
++) {
464 if ((bin
>> j
) & 0x1) {
465 struct cpu_core
*core
= get_core_by_core_id(core_id
);
468 ovs_numa_dump_add(dump
,
481 struct ovs_numa_dump
*
482 ovs_numa_dump_n_cores_per_numa(int cores_per_numa
)
484 struct ovs_numa_dump
*dump
= ovs_numa_dump_create();
485 const struct numa_node
*n
;
487 HMAP_FOR_EACH (n
, hmap_node
, &all_numa_nodes
) {
488 const struct cpu_core
*core
;
491 LIST_FOR_EACH (core
, list_node
, &n
->cores
) {
492 if (i
++ >= cores_per_numa
) {
496 ovs_numa_dump_add(dump
, core
->numa
->numa_id
, core
->core_id
);
504 ovs_numa_dump_contains_core(const struct ovs_numa_dump
*dump
,
505 int numa_id
, unsigned core_id
)
507 struct ovs_numa_info_core
*core
;
509 HMAP_FOR_EACH_WITH_HASH (core
, hmap_node
, hash_2words(numa_id
, core_id
),
511 if (core
->core_id
== core_id
&& core
->numa_id
== numa_id
) {
520 ovs_numa_dump_count(const struct ovs_numa_dump
*dump
)
522 return hmap_count(&dump
->cores
);
526 ovs_numa_dump_destroy(struct ovs_numa_dump
*dump
)
528 struct ovs_numa_info_core
*c
;
529 struct ovs_numa_info_numa
*n
;
535 HMAP_FOR_EACH_POP (c
, hmap_node
, &dump
->cores
) {
539 HMAP_FOR_EACH_POP (n
, hmap_node
, &dump
->numas
) {
543 hmap_destroy(&dump
->cores
);
544 hmap_destroy(&dump
->numas
);
549 int ovs_numa_thread_setaffinity_core(unsigned core_id OVS_UNUSED
)
561 CPU_SET(core_id
, &cpuset
);
562 err
= pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t
), &cpuset
);
564 VLOG_ERR("Thread affinity error %d",err
);
569 #else /* !__linux__ */
571 #endif /* __linux__ */