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 available
; /* If the core can be pinned. */
73 bool pinned
; /* If a thread has been pinned to the core. */
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
;
83 /* Returns true if 'str' contains all digits. Returns false otherwise. */
85 contain_all_digits(const char *str
)
87 return str
[strspn(str
, "0123456789")] == '\0';
90 /* Discovers all numa nodes and the corresponding cpu cores.
91 * Constructs the 'struct numa_node' and 'struct cpu_core'. */
93 discover_numa_and_core(void)
98 for (i
= 0; i
< MAX_NUMA_NODES
; i
++) {
102 /* Constructs the path to node /sys/devices/system/nodeX. */
103 path
= xasprintf("/sys/devices/system/node/node%d", i
);
106 /* Creates 'struct numa_node' if the 'dir' is non-null. */
108 struct numa_node
*n
= xzalloc(sizeof *n
);
109 struct dirent
*subdir
;
111 hmap_insert(&all_numa_nodes
, &n
->hmap_node
, hash_int(i
, 0));
112 list_init(&n
->cores
);
115 while ((subdir
= readdir(dir
)) != NULL
) {
116 if (!strncmp(subdir
->d_name
, "cpu", 3)
117 && contain_all_digits(subdir
->d_name
+ 3)){
118 struct cpu_core
*c
= xzalloc(sizeof *c
);
121 core_id
= strtoul(subdir
->d_name
+ 3, NULL
, 10);
122 hmap_insert(&all_cpu_cores
, &c
->hmap_node
,
123 hash_int(core_id
, 0));
124 list_insert(&n
->cores
, &c
->list_node
);
125 c
->core_id
= core_id
;
131 VLOG_INFO("Discovered %"PRIuSIZE
" CPU cores on NUMA node %d",
132 list_size(&n
->cores
), n
->numa_id
);
136 if (errno
!= ENOENT
) {
137 VLOG_WARN("opendir(%s) failed (%s)", path
,
138 ovs_strerror(errno
));
145 VLOG_INFO("Discovered %"PRIuSIZE
" NUMA nodes and %d CPU cores",
146 hmap_count(&all_numa_nodes
), n_cpus
);
147 if (hmap_count(&all_numa_nodes
) && hmap_count(&all_cpu_cores
)) {
148 found_numa_and_core
= true;
152 /* Extracts the numa node and core info from the 'sysfs'. */
156 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
158 if (ovsthread_once_start(&once
)) {
159 discover_numa_and_core();
160 ovsthread_once_done(&once
);
165 ovs_numa_numa_id_is_valid(int numa_id
)
167 return found_numa_and_core
&& numa_id
< ovs_numa_get_n_numas();
171 ovs_numa_core_id_is_valid(int core_id
)
173 return found_numa_and_core
&& core_id
< ovs_numa_get_n_cores();
176 /* Returns the number of numa nodes. */
178 ovs_numa_get_n_numas(void)
180 return found_numa_and_core
? hmap_count(&all_numa_nodes
)
184 /* Returns the number of cpu cores. */
186 ovs_numa_get_n_cores(void)
188 return found_numa_and_core
? hmap_count(&all_cpu_cores
)
192 /* Given 'core_id', returns the corresponding numa node id. Returns
193 * OVS_NUMA_UNSPEC if 'core_id' is invalid. */
195 ovs_numa_get_numa_id(int core_id
)
197 if (ovs_numa_core_id_is_valid(core_id
)) {
198 struct cpu_core
*core
;
200 core
= CONTAINER_OF(hmap_first_with_hash(&all_cpu_cores
,
201 hash_int(core_id
, 0)),
202 struct cpu_core
, hmap_node
);
204 return core
->numa
->numa_id
;
206 return OVS_NUMA_UNSPEC
;
209 /* Returns the number of cpu cores on numa node. Returns OVS_CORE_UNSPEC
210 * if 'numa_id' is invalid. */
212 ovs_numa_get_n_cores_on_numa(int numa_id
)
214 if (ovs_numa_numa_id_is_valid(numa_id
)) {
215 struct numa_node
*numa
;
217 numa
= CONTAINER_OF(hmap_first_with_hash(&all_numa_nodes
,
218 hash_int(numa_id
, 0)),
219 struct numa_node
, hmap_node
);
221 return list_size(&numa
->cores
);
224 return OVS_CORE_UNSPEC
;
227 /* Returns the number of cpu cores that are available and unpinned
228 * on numa node. Returns OVS_CORE_UNSPEC if 'numa_id' is invalid. */
230 ovs_numa_get_n_unpinned_cores_on_numa(int numa_id
)
232 if (ovs_numa_numa_id_is_valid(numa_id
)) {
233 struct numa_node
*numa
;
234 struct cpu_core
*core
;
237 numa
= CONTAINER_OF(hmap_first_with_hash(&all_numa_nodes
,
238 hash_int(numa_id
, 0)),
239 struct numa_node
, hmap_node
);
240 LIST_FOR_EACH(core
, list_node
, &numa
->cores
) {
241 if (core
->available
&& !core
->pinned
) {
249 return OVS_CORE_UNSPEC
;
252 /* Given 'core_id', tries to pin that core. Returns true, if succeeds.
253 * False, if the core has already been pinned, or if it is invalid or
256 ovs_numa_try_pin_core_specific(int core_id
)
258 if (ovs_numa_core_id_is_valid(core_id
)) {
259 struct cpu_core
*core
;
261 core
= CONTAINER_OF(hmap_first_with_hash(&all_cpu_cores
,
262 hash_int(core_id
, 0)),
263 struct cpu_core
, hmap_node
);
264 if (core
->available
&& !core
->pinned
) {
273 /* Searches through all cores for an unpinned and available core. Returns
274 * the 'core_id' if found and sets the 'core->pinned' to true. Otherwise,
275 * returns OVS_CORE_UNSPEC. */
277 ovs_numa_get_unpinned_core_any(void)
279 struct cpu_core
*core
;
281 HMAP_FOR_EACH(core
, hmap_node
, &all_cpu_cores
) {
282 if (core
->available
&& !core
->pinned
) {
284 return core
->core_id
;
288 return OVS_CORE_UNSPEC
;
291 /* Searches through all cores on numa node with 'numa_id' for an
292 * unpinned and available core. Returns the core_id if found and
293 * sets the 'core->pinned' to true. Otherwise, returns OVS_CORE_UNSPEC. */
295 ovs_numa_get_unpinned_core_on_numa(int numa_id
)
297 if (ovs_numa_numa_id_is_valid(numa_id
)) {
298 struct numa_node
*numa
;
299 struct cpu_core
*core
;
301 numa
= CONTAINER_OF(hmap_first_with_hash(&all_numa_nodes
,
302 hash_int(numa_id
, 0)),
303 struct numa_node
, hmap_node
);
304 LIST_FOR_EACH(core
, list_node
, &numa
->cores
) {
305 if (core
->available
&& !core
->pinned
) {
307 return core
->core_id
;
312 return OVS_CORE_UNSPEC
;
315 /* Unpins the core with 'core_id'. */
317 ovs_numa_unpin_core(int core_id
)
319 if (ovs_numa_core_id_is_valid(core_id
)) {
320 struct cpu_core
*core
;
322 core
= CONTAINER_OF(hmap_first_with_hash(&all_cpu_cores
,
323 hash_int(core_id
, 0)),
324 struct cpu_core
, hmap_node
);
325 core
->pinned
= false;
329 /* Reads the cpu mask configuration from 'cmask' and sets the
330 * 'available' of corresponding cores. For unspecified cores,
331 * sets 'available' to false. */
333 ovs_numa_set_cpu_mask(const char *cmask
)
338 if (!found_numa_and_core
) {
342 /* If no mask specified, resets the 'available' to true for all cores. */
344 struct cpu_core
*core
;
346 HMAP_FOR_EACH(core
, hmap_node
, &all_cpu_cores
) {
347 core
->available
= true;
353 for (i
= strlen(cmask
) - 1; i
>= 0; i
--) {
354 char hex
= toupper(cmask
[i
]);
357 if (hex
>= '0' && hex
<= '9') {
359 } else if (hex
>= 'A' && hex
<= 'F') {
360 bin
= hex
- 'A' + 10;
363 VLOG_WARN("Invalid cpu mask: %c", cmask
[i
]);
366 for (j
= 0; j
< 4; j
++) {
367 struct cpu_core
*core
;
369 core
= CONTAINER_OF(hmap_first_with_hash(&all_cpu_cores
,
370 hash_int(core_id
++, 0)),
371 struct cpu_core
, hmap_node
);
372 core
->available
= (bin
>> j
) & 0x1;
374 if (core_id
>= hmap_count(&all_cpu_cores
)) {
380 /* For unspecified cores, sets 'available' to false. */
381 while (core_id
< hmap_count(&all_cpu_cores
)) {
382 struct cpu_core
*core
;
384 core
= CONTAINER_OF(hmap_first_with_hash(&all_cpu_cores
,
385 hash_int(core_id
++, 0)),
386 struct cpu_core
, hmap_node
);
387 core
->available
= false;
391 #endif /* __linux__ */