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"
35 #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 int core_id
; /* Core id. */
74 bool available
; /* If the core can be pinned. */
75 bool pinned
; /* If a thread has been pinned to the core. */
78 /* Contains all 'struct numa_node's. */
79 static struct hmap all_numa_nodes
= HMAP_INITIALIZER(&all_numa_nodes
);
80 /* Contains all 'struct cpu_core's. */
81 static struct hmap all_cpu_cores
= HMAP_INITIALIZER(&all_cpu_cores
);
82 /* True if numa node and core info are correctly extracted. */
83 static bool found_numa_and_core
;
85 /* Returns true if 'str' contains all digits. Returns false otherwise. */
87 contain_all_digits(const char *str
)
89 return str
[strspn(str
, "0123456789")] == '\0';
92 /* Discovers all numa nodes and the corresponding cpu cores.
93 * Constructs the 'struct numa_node' and 'struct cpu_core'. */
95 discover_numa_and_core(void)
100 for (i
= 0; i
< MAX_NUMA_NODES
; i
++) {
104 /* Constructs the path to node /sys/devices/system/nodeX. */
105 path
= xasprintf("/sys/devices/system/node/node%d", i
);
108 /* Creates 'struct numa_node' if the 'dir' is non-null. */
110 struct numa_node
*n
= xzalloc(sizeof *n
);
111 struct dirent
*subdir
;
113 hmap_insert(&all_numa_nodes
, &n
->hmap_node
, hash_int(i
, 0));
114 list_init(&n
->cores
);
117 while ((subdir
= readdir(dir
)) != NULL
) {
118 if (!strncmp(subdir
->d_name
, "cpu", 3)
119 && contain_all_digits(subdir
->d_name
+ 3)){
120 struct cpu_core
*c
= xzalloc(sizeof *c
);
123 core_id
= strtoul(subdir
->d_name
+ 3, NULL
, 10);
124 hmap_insert(&all_cpu_cores
, &c
->hmap_node
,
125 hash_int(core_id
, 0));
126 list_insert(&n
->cores
, &c
->list_node
);
127 c
->core_id
= core_id
;
133 VLOG_INFO("Discovered %"PRIuSIZE
" CPU cores on NUMA node %d",
134 list_size(&n
->cores
), n
->numa_id
);
138 if (errno
!= ENOENT
) {
139 VLOG_WARN("opendir(%s) failed (%s)", path
,
140 ovs_strerror(errno
));
147 VLOG_INFO("Discovered %"PRIuSIZE
" NUMA nodes and %d CPU cores",
148 hmap_count(&all_numa_nodes
), n_cpus
);
149 if (hmap_count(&all_numa_nodes
) && hmap_count(&all_cpu_cores
)) {
150 found_numa_and_core
= true;
154 /* Gets 'struct cpu_core' by 'core_id'. */
155 static struct cpu_core
*
156 get_core_by_core_id(int core_id
)
158 struct cpu_core
*core
= NULL
;
160 if (ovs_numa_core_id_is_valid(core_id
)) {
161 core
= CONTAINER_OF(hmap_first_with_hash(&all_cpu_cores
,
162 hash_int(core_id
, 0)),
163 struct cpu_core
, hmap_node
);
169 /* Gets 'struct numa_node' by 'numa_id'. */
170 static struct numa_node
*
171 get_numa_by_numa_id(int numa_id
)
173 struct numa_node
*numa
= NULL
;
175 if (ovs_numa_numa_id_is_valid(numa_id
)) {
176 numa
= CONTAINER_OF(hmap_first_with_hash(&all_numa_nodes
,
177 hash_int(numa_id
, 0)),
178 struct numa_node
, hmap_node
);
185 /* Extracts the numa node and core info from the 'sysfs'. */
189 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
191 if (ovsthread_once_start(&once
)) {
192 discover_numa_and_core();
193 ovsthread_once_done(&once
);
198 ovs_numa_numa_id_is_valid(int numa_id
)
200 return found_numa_and_core
&& numa_id
< ovs_numa_get_n_numas();
204 ovs_numa_core_id_is_valid(int core_id
)
206 return found_numa_and_core
&& core_id
< ovs_numa_get_n_cores();
210 ovs_numa_core_is_pinned(int core_id
)
212 struct cpu_core
*core
= get_core_by_core_id(core_id
);
221 /* Returns the number of numa nodes. */
223 ovs_numa_get_n_numas(void)
225 return found_numa_and_core
? hmap_count(&all_numa_nodes
)
229 /* Returns the number of cpu cores. */
231 ovs_numa_get_n_cores(void)
233 return found_numa_and_core
? hmap_count(&all_cpu_cores
)
237 /* Given 'core_id', returns the corresponding numa node id. Returns
238 * OVS_NUMA_UNSPEC if 'core_id' is invalid. */
240 ovs_numa_get_numa_id(int core_id
)
242 struct cpu_core
*core
= get_core_by_core_id(core_id
);
245 return core
->numa
->numa_id
;
248 return OVS_NUMA_UNSPEC
;
251 /* Returns the number of cpu cores on numa node. Returns OVS_CORE_UNSPEC
252 * if 'numa_id' is invalid. */
254 ovs_numa_get_n_cores_on_numa(int numa_id
)
256 struct numa_node
*numa
= get_numa_by_numa_id(numa_id
);
259 return list_size(&numa
->cores
);
262 return OVS_CORE_UNSPEC
;
265 /* Returns the number of cpu cores that are available and unpinned
266 * on numa node. Returns OVS_CORE_UNSPEC if 'numa_id' is invalid. */
268 ovs_numa_get_n_unpinned_cores_on_numa(int numa_id
)
270 struct numa_node
*numa
= get_numa_by_numa_id(numa_id
);
273 struct cpu_core
*core
;
276 LIST_FOR_EACH(core
, list_node
, &numa
->cores
) {
277 if (core
->available
&& !core
->pinned
) {
284 return OVS_CORE_UNSPEC
;
287 /* Given 'core_id', tries to pin that core. Returns true, if succeeds.
288 * False, if the core has already been pinned, or if it is invalid or
291 ovs_numa_try_pin_core_specific(int core_id
)
293 struct cpu_core
*core
= get_core_by_core_id(core_id
);
296 if (core
->available
&& !core
->pinned
) {
305 /* Searches through all cores for an unpinned and available core. Returns
306 * the 'core_id' if found and sets the 'core->pinned' to true. Otherwise,
307 * returns OVS_CORE_UNSPEC. */
309 ovs_numa_get_unpinned_core_any(void)
311 struct cpu_core
*core
;
313 HMAP_FOR_EACH(core
, hmap_node
, &all_cpu_cores
) {
314 if (core
->available
&& !core
->pinned
) {
316 return core
->core_id
;
320 return OVS_CORE_UNSPEC
;
323 /* Searches through all cores on numa node with 'numa_id' for an
324 * unpinned and available core. Returns the core_id if found and
325 * sets the 'core->pinned' to true. Otherwise, returns OVS_CORE_UNSPEC. */
327 ovs_numa_get_unpinned_core_on_numa(int numa_id
)
329 struct numa_node
*numa
= get_numa_by_numa_id(numa_id
);
332 struct cpu_core
*core
;
334 LIST_FOR_EACH(core
, list_node
, &numa
->cores
) {
335 if (core
->available
&& !core
->pinned
) {
337 return core
->core_id
;
342 return OVS_CORE_UNSPEC
;
345 /* Unpins the core with 'core_id'. */
347 ovs_numa_unpin_core(int core_id
)
349 struct cpu_core
*core
= get_core_by_core_id(core_id
);
352 core
->pinned
= false;
356 /* Given the 'numa_id', returns dump of all cores on the numa node. */
357 struct ovs_numa_dump
*
358 ovs_numa_dump_cores_on_numa(int numa_id
)
360 struct ovs_numa_dump
*dump
= NULL
;
361 struct numa_node
*numa
= get_numa_by_numa_id(numa_id
);
364 struct cpu_core
*core
;
366 dump
= xmalloc(sizeof *dump
);
367 list_init(&dump
->dump
);
368 LIST_FOR_EACH(core
, list_node
, &numa
->cores
) {
369 struct ovs_numa_info
*info
= xmalloc(sizeof *info
);
371 info
->numa_id
= numa
->numa_id
;
372 info
->core_id
= core
->core_id
;
373 list_insert(&dump
->dump
, &info
->list_node
);
381 ovs_numa_dump_destroy(struct ovs_numa_dump
*dump
)
383 struct ovs_numa_info
*iter
, *next
;
385 LIST_FOR_EACH_SAFE (iter
, next
, list_node
, &dump
->dump
) {
386 list_remove(&iter
->list_node
);
393 /* Reads the cpu mask configuration from 'cmask' and sets the
394 * 'available' of corresponding cores. For unspecified cores,
395 * sets 'available' to false. */
397 ovs_numa_set_cpu_mask(const char *cmask
)
402 if (!found_numa_and_core
) {
406 /* If no mask specified, resets the 'available' to true for all cores. */
408 struct cpu_core
*core
;
410 HMAP_FOR_EACH(core
, hmap_node
, &all_cpu_cores
) {
411 core
->available
= true;
417 for (i
= strlen(cmask
) - 1; i
>= 0; i
--) {
418 char hex
= toupper(cmask
[i
]);
421 if (hex
>= '0' && hex
<= '9') {
423 } else if (hex
>= 'A' && hex
<= 'F') {
424 bin
= hex
- 'A' + 10;
427 VLOG_WARN("Invalid cpu mask: %c", cmask
[i
]);
430 for (j
= 0; j
< 4; j
++) {
431 struct cpu_core
*core
;
433 core
= CONTAINER_OF(hmap_first_with_hash(&all_cpu_cores
,
434 hash_int(core_id
++, 0)),
435 struct cpu_core
, hmap_node
);
436 core
->available
= (bin
>> j
) & 0x1;
438 if (core_id
>= hmap_count(&all_cpu_cores
)) {
444 /* For unspecified cores, sets 'available' to false. */
445 while (core_id
< hmap_count(&all_cpu_cores
)) {
446 struct cpu_core
*core
;
448 core
= CONTAINER_OF(hmap_first_with_hash(&all_cpu_cores
,
449 hash_int(core_id
++, 0)),
450 struct cpu_core
, hmap_node
);
451 core
->available
= false;
455 #endif /* __linux__ */