]> git.proxmox.com Git - mirror_ovs.git/blame - lib/ovs-numa.c
ovs-numa: Add per numa and global counts in dump.
[mirror_ovs.git] / lib / ovs-numa.c
CommitLineData
7c5a3bbf
AW
1/*
2 * Copyright (c) 2014 Nicira, Inc.
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
7c5a3bbf
AW
17#include <config.h>
18#include "ovs-numa.h"
19
20#include <ctype.h>
7c5a3bbf 21#include <errno.h>
93ce5762
DDP
22#ifdef __linux__
23#include <dirent.h>
7c5a3bbf
AW
24#include <stddef.h>
25#include <string.h>
26#include <sys/types.h>
27#include <unistd.h>
93ce5762 28#endif /* __linux__ */
7c5a3bbf
AW
29
30#include "hash.h"
ee89ea7b 31#include "openvswitch/hmap.h"
b19bab5b 32#include "openvswitch/list.h"
7c5a3bbf 33#include "ovs-thread.h"
e6211adc 34#include "openvswitch/vlog.h"
dbedeb9d 35#include "util.h"
7c5a3bbf
AW
36
37VLOG_DEFINE_THIS_MODULE(ovs_numa);
38
34185750
AW
39/* ovs-numa module
40 * ===============
41 *
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.
44 *
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.
49 *
9da2564e
AW
50 * NOTE, this module should only be used by the main thread.
51 *
34185750
AW
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.
55 *
56 * TODO: Fix ovs-numa when cpu hotplug is used.
57 */
58
012c0a04 59#define MAX_NUMA_NODES 128
7c5a3bbf 60
012c0a04
AW
61/* numa node. */
62struct numa_node {
63 struct hmap_node hmap_node; /* In the 'all_numa_nodes'. */
ca6ba700 64 struct ovs_list cores; /* List of cpu cores on the numa node. */
012c0a04 65 int numa_id; /* numa node id. */
7c5a3bbf
AW
66};
67
012c0a04 68/* Cpu core on a numa node. */
7c5a3bbf
AW
69struct cpu_core {
70 struct hmap_node hmap_node;/* In the 'all_cpu_cores'. */
ca6ba700 71 struct ovs_list list_node; /* In 'numa_node->cores' list. */
012c0a04 72 struct numa_node *numa; /* numa node containing the core. */
bd5131ba 73 unsigned core_id; /* Core id. */
8db2f898 74 bool available; /* If the core can be pinned. */
7c5a3bbf
AW
75 bool pinned; /* If a thread has been pinned to the core. */
76};
77
012c0a04
AW
78/* Contains all 'struct numa_node's. */
79static struct hmap all_numa_nodes = HMAP_INITIALIZER(&all_numa_nodes);
7c5a3bbf
AW
80/* Contains all 'struct cpu_core's. */
81static struct hmap all_cpu_cores = HMAP_INITIALIZER(&all_cpu_cores);
012c0a04
AW
82/* True if numa node and core info are correctly extracted. */
83static bool found_numa_and_core;
b4e28b7f
DDP
84/* True if the module was initialized with dummy options. In this case, the
85 * module must not interact with the actual cpus/nodes in the system. */
86static bool dummy_numa = false;
87/* If 'dummy_numa' is true, contains a copy of the dummy numa configuration
88 * parameter */
89static char *dummy_config;
90
91static struct numa_node *get_numa_by_numa_id(int numa_id);
7c5a3bbf 92
93ce5762 93#ifdef __linux__
7c5a3bbf
AW
94/* Returns true if 'str' contains all digits. Returns false otherwise. */
95static bool
96contain_all_digits(const char *str)
97{
98 return str[strspn(str, "0123456789")] == '\0';
99}
93ce5762 100#endif /* __linux__ */
7c5a3bbf 101
b4e28b7f
DDP
102static struct numa_node *
103insert_new_numa_node(int numa_id)
104{
105 struct numa_node *n = xzalloc(sizeof *n);
106
107 hmap_insert(&all_numa_nodes, &n->hmap_node, hash_int(numa_id, 0));
108 ovs_list_init(&n->cores);
109 n->numa_id = numa_id;
110
111 return n;
112}
113
114static struct cpu_core *
115insert_new_cpu_core(struct numa_node *n, unsigned core_id)
116{
117 struct cpu_core *c = xzalloc(sizeof *c);
118
119 hmap_insert(&all_cpu_cores, &c->hmap_node, hash_int(core_id, 0));
120 ovs_list_insert(&n->cores, &c->list_node);
121 c->core_id = core_id;
122 c->numa = n;
123 c->available = true;
124
125 return c;
126}
127
128/* Has the same effect as discover_numa_and_core(), but instead of reading
129 * sysfs entries, extracts the info from 'dummy_config'.
130 *
131 * 'dummy_config' lists the numa_ids of each CPU separated by a comma, e.g.
132 * - "0,0,0,0": four cores on numa socket 0.
133 * - "0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1": 16 cores on two numa sockets.
134 * - "0,0,0,0,1,1,1,1": 8 cores on two numa sockets.
135 *
136 * The different numa ids must be consecutives or the function will abort. */
137static void
138discover_numa_and_core_dummy(const char *dummy_config)
139{
140 char *conf = xstrdup(dummy_config);
141 char *id, *saveptr = NULL;
142 unsigned i = 0;
143 long max_numa_id = 0;
144
145 for (id = strtok_r(conf, ",", &saveptr); id;
146 id = strtok_r(NULL, ",", &saveptr)) {
147 struct hmap_node *hnode;
148 struct numa_node *n;
149 long numa_id;
150
151 numa_id = strtol(id, NULL, 10);
152 if (numa_id < 0 || numa_id >= MAX_NUMA_NODES) {
153 VLOG_WARN("Invalid numa node %ld", numa_id);
154 continue;
155 }
156
157 max_numa_id = MAX(max_numa_id, numa_id);
158
159 hnode = hmap_first_with_hash(&all_numa_nodes, hash_int(numa_id, 0));
160
161 if (hnode) {
162 n = CONTAINER_OF(hnode, struct numa_node, hmap_node);
163 } else {
164 n = insert_new_numa_node(numa_id);
165 }
166
167 insert_new_cpu_core(n, i);
168
169 i++;
170 }
171
172 free(conf);
173
174 if (max_numa_id + 1 != hmap_count(&all_numa_nodes)) {
175 ovs_fatal(0, "dummy numa contains non consecutive numa ids");
176 }
177}
178
012c0a04
AW
179/* Discovers all numa nodes and the corresponding cpu cores.
180 * Constructs the 'struct numa_node' and 'struct cpu_core'. */
7c5a3bbf 181static void
012c0a04 182discover_numa_and_core(void)
7c5a3bbf 183{
93ce5762 184#ifdef __linux__
7c5a3bbf 185 int i;
8ae587b9
IM
186 DIR *dir;
187 bool numa_supported = true;
188
189 /* Check if NUMA supported on this system. */
190 dir = opendir("/sys/devices/system/node");
191
192 if (!dir && errno == ENOENT) {
193 numa_supported = false;
194 }
195 if (dir) {
196 closedir(dir);
197 }
7c5a3bbf 198
012c0a04 199 for (i = 0; i < MAX_NUMA_NODES; i++) {
7c5a3bbf
AW
200 char* path;
201
8ae587b9
IM
202 if (numa_supported) {
203 /* Constructs the path to node /sys/devices/system/nodeX. */
204 path = xasprintf("/sys/devices/system/node/node%d", i);
205 } else {
206 path = xasprintf("/sys/devices/system/cpu/");
207 }
208
7c5a3bbf
AW
209 dir = opendir(path);
210
012c0a04 211 /* Creates 'struct numa_node' if the 'dir' is non-null. */
7c5a3bbf 212 if (dir) {
b4e28b7f 213 struct numa_node *n;
7c5a3bbf
AW
214 struct dirent *subdir;
215
b4e28b7f 216 n = insert_new_numa_node(i);
7c5a3bbf
AW
217
218 while ((subdir = readdir(dir)) != NULL) {
219 if (!strncmp(subdir->d_name, "cpu", 3)
b4e28b7f 220 && contain_all_digits(subdir->d_name + 3)) {
bd5131ba 221 unsigned core_id;
7c5a3bbf
AW
222
223 core_id = strtoul(subdir->d_name + 3, NULL, 10);
b4e28b7f 224 insert_new_cpu_core(n, core_id);
7c5a3bbf
AW
225 }
226 }
7c5a3bbf 227 closedir(dir);
8ae587b9
IM
228 } else if (errno != ENOENT) {
229 VLOG_WARN("opendir(%s) failed (%s)", path,
230 ovs_strerror(errno));
231 }
232
233 free(path);
234 if (!dir || !numa_supported) {
7c5a3bbf
AW
235 break;
236 }
237 }
93ce5762 238#endif /* __linux__ */
7c5a3bbf
AW
239}
240
9da2564e
AW
241/* Gets 'struct cpu_core' by 'core_id'. */
242static struct cpu_core*
bd5131ba 243get_core_by_core_id(unsigned core_id)
9da2564e 244{
0900ca8e 245 struct cpu_core *core;
9da2564e 246
0900ca8e
DDP
247 HMAP_FOR_EACH_WITH_HASH (core, hmap_node, hash_int(core_id, 0),
248 &all_cpu_cores) {
249 if (core->core_id == core_id) {
250 return core;
251 }
9da2564e
AW
252 }
253
0900ca8e 254 return NULL;
9da2564e
AW
255}
256
257/* Gets 'struct numa_node' by 'numa_id'. */
258static struct numa_node*
259get_numa_by_numa_id(int numa_id)
260{
0900ca8e 261 struct numa_node *numa;
9da2564e 262
0900ca8e
DDP
263 HMAP_FOR_EACH_WITH_HASH (numa, hmap_node, hash_int(numa_id, 0),
264 &all_numa_nodes) {
265 if (numa->numa_id == numa_id) {
266 return numa;
267 }
9da2564e
AW
268 }
269
0900ca8e 270 return NULL;
9da2564e
AW
271}
272
273\f
b4e28b7f
DDP
274
275static bool
276ovs_numa_init__(const char *dummy_config)
7c5a3bbf
AW
277{
278 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
279
280 if (ovsthread_once_start(&once)) {
b4e28b7f
DDP
281 const struct numa_node *n;
282
283 if (!dummy_config) {
284 discover_numa_and_core();
285 } else {
286 discover_numa_and_core_dummy(dummy_config);
287 }
288
289 HMAP_FOR_EACH(n, hmap_node, &all_numa_nodes) {
290 VLOG_INFO("Discovered %"PRIuSIZE" CPU cores on NUMA node %d",
291 ovs_list_size(&n->cores), n->numa_id);
292 }
293
294 VLOG_INFO("Discovered %"PRIuSIZE" NUMA nodes and %"PRIuSIZE" CPU cores",
295 hmap_count(&all_numa_nodes), hmap_count(&all_cpu_cores));
296
297 if (hmap_count(&all_numa_nodes) && hmap_count(&all_cpu_cores)) {
298 found_numa_and_core = true;
299 }
300
7c5a3bbf 301 ovsthread_once_done(&once);
b4e28b7f
DDP
302
303 return true;
304 } else {
305 return false;
306 }
307}
308
309/* Extracts the numa node and core info from the 'config'. This is useful for
310 * testing purposes. The function must be called once, before ovs_numa_init().
311 *
312 * The format of 'config' is explained in the comment above
313 * discover_numa_and_core_dummy().*/
314void
315ovs_numa_set_dummy(const char *config)
316{
317 dummy_numa = true;
318 ovs_assert(config);
319 free(dummy_config);
320 dummy_config = xstrdup(config);
321}
322
323/* Initializes the numa module. */
324void
325ovs_numa_init(void)
326{
327 if (dummy_numa) {
328 ovs_numa_init__(dummy_config);
329 } else {
330 ovs_numa_init__(NULL);
7c5a3bbf
AW
331 }
332}
333
334bool
012c0a04 335ovs_numa_numa_id_is_valid(int numa_id)
7c5a3bbf 336{
421aa227 337 return found_numa_and_core && numa_id < ovs_numa_get_n_numas();
7c5a3bbf
AW
338}
339
340bool
bd5131ba 341ovs_numa_core_id_is_valid(unsigned core_id)
7c5a3bbf 342{
421aa227 343 return found_numa_and_core && core_id < ovs_numa_get_n_cores();
7c5a3bbf
AW
344}
345
9da2564e 346bool
bd5131ba 347ovs_numa_core_is_pinned(unsigned core_id)
9da2564e
AW
348{
349 struct cpu_core *core = get_core_by_core_id(core_id);
350
351 if (core) {
352 return core->pinned;
353 }
354
355 return false;
356}
357
012c0a04 358/* Returns the number of numa nodes. */
7c5a3bbf 359int
012c0a04 360ovs_numa_get_n_numas(void)
7c5a3bbf 361{
012c0a04
AW
362 return found_numa_and_core ? hmap_count(&all_numa_nodes)
363 : OVS_NUMA_UNSPEC;
7c5a3bbf
AW
364}
365
366/* Returns the number of cpu cores. */
367int
368ovs_numa_get_n_cores(void)
369{
012c0a04
AW
370 return found_numa_and_core ? hmap_count(&all_cpu_cores)
371 : OVS_CORE_UNSPEC;
7c5a3bbf
AW
372}
373
6b1105fb
AW
374/* Given 'core_id', returns the corresponding numa node id. Returns
375 * OVS_NUMA_UNSPEC if 'core_id' is invalid. */
376int
bd5131ba 377ovs_numa_get_numa_id(unsigned core_id)
6b1105fb 378{
9da2564e 379 struct cpu_core *core = get_core_by_core_id(core_id);
6b1105fb 380
9da2564e 381 if (core) {
6b1105fb
AW
382 return core->numa->numa_id;
383 }
9da2564e 384
6b1105fb
AW
385 return OVS_NUMA_UNSPEC;
386}
387
421aa227
AW
388/* Returns the number of cpu cores on numa node. Returns OVS_CORE_UNSPEC
389 * if 'numa_id' is invalid. */
7c5a3bbf 390int
012c0a04 391ovs_numa_get_n_cores_on_numa(int numa_id)
7c5a3bbf 392{
9da2564e 393 struct numa_node *numa = get_numa_by_numa_id(numa_id);
7c5a3bbf 394
9da2564e 395 if (numa) {
417e7e66 396 return ovs_list_size(&numa->cores);
7c5a3bbf
AW
397 }
398
399 return OVS_CORE_UNSPEC;
400}
401
8db2f898
AW
402/* Returns the number of cpu cores that are available and unpinned
403 * on numa node. Returns OVS_CORE_UNSPEC if 'numa_id' is invalid. */
7c5a3bbf 404int
012c0a04 405ovs_numa_get_n_unpinned_cores_on_numa(int numa_id)
7c5a3bbf 406{
9da2564e
AW
407 struct numa_node *numa = get_numa_by_numa_id(numa_id);
408
409 if (numa) {
7c5a3bbf
AW
410 struct cpu_core *core;
411 int count = 0;
412
012c0a04 413 LIST_FOR_EACH(core, list_node, &numa->cores) {
8db2f898 414 if (core->available && !core->pinned) {
7c5a3bbf
AW
415 count++;
416 }
417 }
7c5a3bbf
AW
418 return count;
419 }
420
421 return OVS_CORE_UNSPEC;
422}
423
424/* Given 'core_id', tries to pin that core. Returns true, if succeeds.
8db2f898
AW
425 * False, if the core has already been pinned, or if it is invalid or
426 * not available. */
7c5a3bbf 427bool
bd5131ba 428ovs_numa_try_pin_core_specific(unsigned core_id)
7c5a3bbf 429{
9da2564e 430 struct cpu_core *core = get_core_by_core_id(core_id);
7c5a3bbf 431
9da2564e 432 if (core) {
8db2f898 433 if (core->available && !core->pinned) {
421aa227
AW
434 core->pinned = true;
435 return true;
436 }
7c5a3bbf
AW
437 }
438
439 return false;
440}
441
8db2f898
AW
442/* Searches through all cores for an unpinned and available core. Returns
443 * the 'core_id' if found and sets the 'core->pinned' to true. Otherwise,
444 * returns OVS_CORE_UNSPEC. */
bd5131ba 445unsigned
7c5a3bbf
AW
446ovs_numa_get_unpinned_core_any(void)
447{
448 struct cpu_core *core;
449
450 HMAP_FOR_EACH(core, hmap_node, &all_cpu_cores) {
8db2f898 451 if (core->available && !core->pinned) {
7c5a3bbf
AW
452 core->pinned = true;
453 return core->core_id;
454 }
455 }
456
457 return OVS_CORE_UNSPEC;
458}
459
8db2f898
AW
460/* Searches through all cores on numa node with 'numa_id' for an
461 * unpinned and available core. Returns the core_id if found and
462 * sets the 'core->pinned' to true. Otherwise, returns OVS_CORE_UNSPEC. */
bd5131ba 463unsigned
012c0a04 464ovs_numa_get_unpinned_core_on_numa(int numa_id)
7c5a3bbf 465{
9da2564e
AW
466 struct numa_node *numa = get_numa_by_numa_id(numa_id);
467
468 if (numa) {
421aa227 469 struct cpu_core *core;
7c5a3bbf 470
421aa227 471 LIST_FOR_EACH(core, list_node, &numa->cores) {
8db2f898 472 if (core->available && !core->pinned) {
421aa227
AW
473 core->pinned = true;
474 return core->core_id;
475 }
7c5a3bbf
AW
476 }
477 }
478
479 return OVS_CORE_UNSPEC;
480}
481
8db2f898 482/* Unpins the core with 'core_id'. */
7c5a3bbf 483void
bd5131ba 484ovs_numa_unpin_core(unsigned core_id)
7c5a3bbf 485{
9da2564e 486 struct cpu_core *core = get_core_by_core_id(core_id);
7c5a3bbf 487
9da2564e 488 if (core) {
421aa227
AW
489 core->pinned = false;
490 }
7c5a3bbf
AW
491}
492
90f9f839
DDP
493static struct ovs_numa_dump *
494ovs_numa_dump_create(void)
495{
496 struct ovs_numa_dump *dump = xmalloc(sizeof *dump);
497
498 hmap_init(&dump->cores);
499 hmap_init(&dump->numas);
500
501 return dump;
502}
503
504static void
505ovs_numa_dump_add(struct ovs_numa_dump *dump, int numa_id, int core_id)
506{
507 struct ovs_numa_info_core *c = xzalloc(sizeof *c);
508 struct ovs_numa_info_numa *n;
509
510 c->numa_id = numa_id;
511 c->core_id = core_id;
512 hmap_insert(&dump->cores, &c->hmap_node, hash_2words(numa_id, core_id));
513
514 HMAP_FOR_EACH_WITH_HASH (n, hmap_node, hash_int(numa_id, 0),
515 &dump->numas) {
516 if (n->numa_id == numa_id) {
517 n->n_cores++;
518 return;
519 }
520 }
521
522 n = xzalloc(sizeof *n);
523 n->numa_id = numa_id;
524 n->n_cores = 1;
525 hmap_insert(&dump->numas, &n->hmap_node, hash_int(numa_id, 0));
526}
527
9da2564e
AW
528/* Given the 'numa_id', returns dump of all cores on the numa node. */
529struct ovs_numa_dump *
530ovs_numa_dump_cores_on_numa(int numa_id)
531{
90f9f839 532 struct ovs_numa_dump *dump = ovs_numa_dump_create();
9da2564e
AW
533 struct numa_node *numa = get_numa_by_numa_id(numa_id);
534
535 if (numa) {
536 struct cpu_core *core;
537
90f9f839
DDP
538 LIST_FOR_EACH (core, list_node, &numa->cores) {
539 ovs_numa_dump_add(dump, numa->numa_id, core->core_id);
9da2564e
AW
540 }
541 }
542
543 return dump;
544}
545
dbedeb9d
DDP
546struct ovs_numa_dump *
547ovs_numa_dump_cores_with_cmask(const char *cmask)
548{
90f9f839 549 struct ovs_numa_dump *dump = ovs_numa_dump_create();
dbedeb9d
DDP
550 int core_id = 0;
551 int end_idx;
552
dbedeb9d
DDP
553 /* Ignore leading 0x. */
554 end_idx = 0;
555 if (!strncmp(cmask, "0x", 2) || !strncmp(cmask, "0X", 2)) {
556 end_idx = 2;
557 }
558
559 for (int i = strlen(cmask) - 1; i >= end_idx; i--) {
560 char hex = cmask[i];
561 int bin;
562
563 bin = hexit_value(hex);
564 if (bin == -1) {
565 VLOG_WARN("Invalid cpu mask: %c", cmask[i]);
566 bin = 0;
567 }
568
569 for (int j = 0; j < 4; j++) {
570 if ((bin >> j) & 0x1) {
571 struct cpu_core *core = get_core_by_core_id(core_id);
572
573 if (core) {
90f9f839
DDP
574 ovs_numa_dump_add(dump,
575 core->numa->numa_id,
576 core->core_id);
dbedeb9d
DDP
577 }
578 }
579
580 core_id++;
581 }
582 }
583
584 return dump;
585}
586
587struct ovs_numa_dump *
588ovs_numa_dump_n_cores_per_numa(int cores_per_numa)
589{
90f9f839 590 struct ovs_numa_dump *dump = ovs_numa_dump_create();
dbedeb9d
DDP
591 const struct numa_node *n;
592
dbedeb9d
DDP
593 HMAP_FOR_EACH (n, hmap_node, &all_numa_nodes) {
594 const struct cpu_core *core;
595 int i = 0;
596
597 LIST_FOR_EACH (core, list_node, &n->cores) {
598 if (i++ >= cores_per_numa) {
599 break;
600 }
601
90f9f839 602 ovs_numa_dump_add(dump, core->numa->numa_id, core->core_id);
dbedeb9d
DDP
603 }
604 }
605
606 return dump;
607}
608
b2ce05ed
DDP
609bool
610ovs_numa_dump_contains_core(const struct ovs_numa_dump *dump,
611 int numa_id, unsigned core_id)
612{
90f9f839 613 struct ovs_numa_info_core *core;
b2ce05ed
DDP
614
615 HMAP_FOR_EACH_WITH_HASH (core, hmap_node, hash_2words(numa_id, core_id),
90f9f839 616 &dump->cores) {
b2ce05ed
DDP
617 if (core->core_id == core_id && core->numa_id == numa_id) {
618 return true;
619 }
620 }
621
622 return false;
623}
624
90f9f839
DDP
625size_t
626ovs_numa_dump_count(const struct ovs_numa_dump *dump)
627{
628 return hmap_count(&dump->cores);
629}
630
9da2564e
AW
631void
632ovs_numa_dump_destroy(struct ovs_numa_dump *dump)
633{
90f9f839
DDP
634 struct ovs_numa_info_core *c;
635 struct ovs_numa_info_numa *n;
9da2564e 636
93ce5762
DDP
637 if (!dump) {
638 return;
639 }
640
90f9f839
DDP
641 HMAP_FOR_EACH_POP (c, hmap_node, &dump->cores) {
642 free(c);
643 }
644
645 HMAP_FOR_EACH_POP (n, hmap_node, &dump->numas) {
646 free(n);
9da2564e
AW
647 }
648
90f9f839
DDP
649 hmap_destroy(&dump->cores);
650 hmap_destroy(&dump->numas);
b2ce05ed 651
9da2564e
AW
652 free(dump);
653}
654
8db2f898
AW
655/* Reads the cpu mask configuration from 'cmask' and sets the
656 * 'available' of corresponding cores. For unspecified cores,
657 * sets 'available' to false. */
658void
659ovs_numa_set_cpu_mask(const char *cmask)
660{
661 int core_id = 0;
662 int i;
3fa215b1 663 int end_idx;
8db2f898
AW
664
665 if (!found_numa_and_core) {
666 return;
667 }
668
669 /* If no mask specified, resets the 'available' to true for all cores. */
670 if (!cmask) {
671 struct cpu_core *core;
672
673 HMAP_FOR_EACH(core, hmap_node, &all_cpu_cores) {
674 core->available = true;
675 }
676
677 return;
678 }
679
3fa215b1 680 /* Ignore leading 0x. */
681 end_idx = 0;
682 if (!strncmp(cmask, "0x", 2) || !strncmp(cmask, "0X", 2)) {
683 end_idx = 2;
684 }
685
686 for (i = strlen(cmask) - 1; i >= end_idx; i--) {
fb45b680 687 char hex = toupper((unsigned char)cmask[i]);
8db2f898
AW
688 int bin, j;
689
690 if (hex >= '0' && hex <= '9') {
691 bin = hex - '0';
692 } else if (hex >= 'A' && hex <= 'F') {
693 bin = hex - 'A' + 10;
694 } else {
695 bin = 0;
696 VLOG_WARN("Invalid cpu mask: %c", cmask[i]);
697 }
698
699 for (j = 0; j < 4; j++) {
700 struct cpu_core *core;
701
702 core = CONTAINER_OF(hmap_first_with_hash(&all_cpu_cores,
703 hash_int(core_id++, 0)),
704 struct cpu_core, hmap_node);
705 core->available = (bin >> j) & 0x1;
706
707 if (core_id >= hmap_count(&all_cpu_cores)) {
708 return;
709 }
3fa215b1 710 }
8db2f898
AW
711 }
712
713 /* For unspecified cores, sets 'available' to false. */
714 while (core_id < hmap_count(&all_cpu_cores)) {
715 struct cpu_core *core;
716
717 core = CONTAINER_OF(hmap_first_with_hash(&all_cpu_cores,
718 hash_int(core_id++, 0)),
719 struct cpu_core, hmap_node);
720 core->available = false;
721 }
722}
6930c7e0 723
b4e28b7f 724int ovs_numa_thread_setaffinity_core(unsigned core_id OVS_UNUSED)
6930c7e0 725{
b4e28b7f
DDP
726 if (dummy_numa) {
727 /* Nothing to do */
728 return 0;
729 }
730
6930c7e0
DDP
731#ifdef __linux__
732 cpu_set_t cpuset;
733 int err;
734
735 CPU_ZERO(&cpuset);
736 CPU_SET(core_id, &cpuset);
737 err = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
738 if (err) {
739 VLOG_ERR("Thread affinity error %d",err);
740 return err;
741 }
742
743 return 0;
744#else /* !__linux__ */
745 return EOPNOTSUPP;
746#endif /* __linux__ */
747}