693541fe7984ca51f57e057f21098c32fcba5980
[cascardo/ovs.git] / lib / ovs-numa.c
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
17 /* On non-Linux, these functions are defined inline in ovs-numa.h. */
18 #ifdef __linux__
19
20 #include <config.h>
21 #include "ovs-numa.h"
22
23 #include <ctype.h>
24 #include <dirent.h>
25 #include <errno.h>
26 #include <stddef.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #include <unistd.h>
30
31 #include "hash.h"
32 #include "hmap.h"
33 #include "list.h"
34 #include "ovs-thread.h"
35 #include "openvswitch/vlog.h"
36
37 VLOG_DEFINE_THIS_MODULE(ovs_numa);
38
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  *
50  * NOTE, this module should only be used by the main thread.
51  *
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
59 #define MAX_NUMA_NODES 128
60
61 /* numa node. */
62 struct numa_node {
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. */
66 };
67
68 /* Cpu core on a numa node. */
69 struct cpu_core {
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. */
74     bool available;            /* If the core can be pinned. */
75     bool pinned;               /* If a thread has been pinned to the core. */
76 };
77
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;
84
85 /* Returns true if 'str' contains all digits.  Returns false otherwise. */
86 static bool
87 contain_all_digits(const char *str)
88 {
89     return str[strspn(str, "0123456789")] == '\0';
90 }
91
92 /* Discovers all numa nodes and the corresponding cpu cores.
93  * Constructs the 'struct numa_node' and 'struct cpu_core'. */
94 static void
95 discover_numa_and_core(void)
96 {
97     int n_cpus = 0;
98     int i;
99
100     for (i = 0; i < MAX_NUMA_NODES; i++) {
101         DIR *dir;
102         char* path;
103
104         /* Constructs the path to node /sys/devices/system/nodeX. */
105         path = xasprintf("/sys/devices/system/node/node%d", i);
106         dir = opendir(path);
107
108         /* Creates 'struct numa_node' if the 'dir' is non-null. */
109         if (dir) {
110             struct numa_node *n = xzalloc(sizeof *n);
111             struct dirent *subdir;
112
113             hmap_insert(&all_numa_nodes, &n->hmap_node, hash_int(i, 0));
114             list_init(&n->cores);
115             n->numa_id = i;
116
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);
121                     unsigned core_id;
122
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;
128                     c->numa = n;
129                     c->available = true;
130                     n_cpus++;
131                 }
132             }
133             VLOG_INFO("Discovered %"PRIuSIZE" CPU cores on NUMA node %d",
134                       list_size(&n->cores), n->numa_id);
135             free(path);
136             closedir(dir);
137         } else {
138             if (errno != ENOENT) {
139                 VLOG_WARN("opendir(%s) failed (%s)", path,
140                           ovs_strerror(errno));
141             }
142             free(path);
143             break;
144         }
145     }
146
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;
151     }
152 }
153
154 /* Gets 'struct cpu_core' by 'core_id'. */
155 static struct cpu_core*
156 get_core_by_core_id(unsigned core_id)
157 {
158     struct cpu_core *core = NULL;
159
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);
164     }
165
166     return core;
167 }
168
169 /* Gets 'struct numa_node' by 'numa_id'. */
170 static struct numa_node*
171 get_numa_by_numa_id(int numa_id)
172 {
173     struct numa_node *numa = NULL;
174
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);
179     }
180
181     return numa;
182 }
183
184 \f
185 /* Extracts the numa node and core info from the 'sysfs'. */
186 void
187 ovs_numa_init(void)
188 {
189     static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
190
191     if (ovsthread_once_start(&once)) {
192         discover_numa_and_core();
193         ovsthread_once_done(&once);
194     }
195 }
196
197 bool
198 ovs_numa_numa_id_is_valid(int numa_id)
199 {
200     return found_numa_and_core && numa_id < ovs_numa_get_n_numas();
201 }
202
203 bool
204 ovs_numa_core_id_is_valid(unsigned core_id)
205 {
206     return found_numa_and_core && core_id < ovs_numa_get_n_cores();
207 }
208
209 bool
210 ovs_numa_core_is_pinned(unsigned core_id)
211 {
212     struct cpu_core *core = get_core_by_core_id(core_id);
213
214     if (core) {
215         return core->pinned;
216     }
217
218     return false;
219 }
220
221 /* Returns the number of numa nodes. */
222 int
223 ovs_numa_get_n_numas(void)
224 {
225     return found_numa_and_core ? hmap_count(&all_numa_nodes)
226                                : OVS_NUMA_UNSPEC;
227 }
228
229 /* Returns the number of cpu cores. */
230 int
231 ovs_numa_get_n_cores(void)
232 {
233     return found_numa_and_core ? hmap_count(&all_cpu_cores)
234                                : OVS_CORE_UNSPEC;
235 }
236
237 /* Given 'core_id', returns the corresponding numa node id.  Returns
238  * OVS_NUMA_UNSPEC if 'core_id' is invalid. */
239 int
240 ovs_numa_get_numa_id(unsigned core_id)
241 {
242     struct cpu_core *core = get_core_by_core_id(core_id);
243
244     if (core) {
245         return core->numa->numa_id;
246     }
247
248     return OVS_NUMA_UNSPEC;
249 }
250
251 /* Returns the number of cpu cores on numa node.  Returns OVS_CORE_UNSPEC
252  * if 'numa_id' is invalid. */
253 int
254 ovs_numa_get_n_cores_on_numa(int numa_id)
255 {
256     struct numa_node *numa = get_numa_by_numa_id(numa_id);
257
258     if (numa) {
259         return list_size(&numa->cores);
260     }
261
262     return OVS_CORE_UNSPEC;
263 }
264
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. */
267 int
268 ovs_numa_get_n_unpinned_cores_on_numa(int numa_id)
269 {
270     struct numa_node *numa = get_numa_by_numa_id(numa_id);
271
272     if (numa) {
273         struct cpu_core *core;
274         int count = 0;
275
276         LIST_FOR_EACH(core, list_node, &numa->cores) {
277             if (core->available && !core->pinned) {
278                 count++;
279             }
280         }
281         return count;
282     }
283
284     return OVS_CORE_UNSPEC;
285 }
286
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
289  * not available. */
290 bool
291 ovs_numa_try_pin_core_specific(unsigned core_id)
292 {
293     struct cpu_core *core = get_core_by_core_id(core_id);
294
295     if (core) {
296         if (core->available && !core->pinned) {
297             core->pinned = true;
298             return true;
299         }
300     }
301
302     return false;
303 }
304
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. */
308 unsigned
309 ovs_numa_get_unpinned_core_any(void)
310 {
311     struct cpu_core *core;
312
313     HMAP_FOR_EACH(core, hmap_node, &all_cpu_cores) {
314         if (core->available && !core->pinned) {
315             core->pinned = true;
316             return core->core_id;
317         }
318     }
319
320     return OVS_CORE_UNSPEC;
321 }
322
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. */
326 unsigned
327 ovs_numa_get_unpinned_core_on_numa(int numa_id)
328 {
329     struct numa_node *numa = get_numa_by_numa_id(numa_id);
330
331     if (numa) {
332         struct cpu_core *core;
333
334         LIST_FOR_EACH(core, list_node, &numa->cores) {
335             if (core->available && !core->pinned) {
336                 core->pinned = true;
337                 return core->core_id;
338             }
339         }
340     }
341
342     return OVS_CORE_UNSPEC;
343 }
344
345 /* Unpins the core with 'core_id'. */
346 void
347 ovs_numa_unpin_core(unsigned core_id)
348 {
349     struct cpu_core *core = get_core_by_core_id(core_id);
350
351     if (core) {
352         core->pinned = false;
353     }
354 }
355
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)
359 {
360     struct ovs_numa_dump *dump = NULL;
361     struct numa_node *numa = get_numa_by_numa_id(numa_id);
362
363     if (numa) {
364         struct cpu_core *core;
365
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);
370
371             info->numa_id = numa->numa_id;
372             info->core_id = core->core_id;
373             list_insert(&dump->dump, &info->list_node);
374         }
375     }
376
377     return dump;
378 }
379
380 void
381 ovs_numa_dump_destroy(struct ovs_numa_dump *dump)
382 {
383     struct ovs_numa_info *iter;
384
385     LIST_FOR_EACH_POP (iter, list_node, &dump->dump) {
386         free(iter);
387     }
388
389     free(dump);
390 }
391
392 /* Reads the cpu mask configuration from 'cmask' and sets the
393  * 'available' of corresponding cores.  For unspecified cores,
394  * sets 'available' to false. */
395 void
396 ovs_numa_set_cpu_mask(const char *cmask)
397 {
398     int core_id = 0;
399     int i;
400
401     if (!found_numa_and_core) {
402         return;
403     }
404
405     /* If no mask specified, resets the 'available' to true for all cores. */
406     if (!cmask) {
407         struct cpu_core *core;
408
409         HMAP_FOR_EACH(core, hmap_node, &all_cpu_cores) {
410             core->available = true;
411         }
412
413         return;
414     }
415
416     for (i = strlen(cmask) - 1; i >= 0; i--) {
417         char hex = toupper(cmask[i]);
418         int bin, j;
419
420         if (hex >= '0' && hex <= '9') {
421             bin = hex - '0';
422         } else if (hex >= 'A' && hex <= 'F') {
423             bin = hex - 'A' + 10;
424         } else {
425             bin = 0;
426             VLOG_WARN("Invalid cpu mask: %c", cmask[i]);
427         }
428
429         for (j = 0; j < 4; j++) {
430             struct cpu_core *core;
431
432             core = CONTAINER_OF(hmap_first_with_hash(&all_cpu_cores,
433                                                      hash_int(core_id++, 0)),
434                                 struct cpu_core, hmap_node);
435             core->available = (bin >> j) & 0x1;
436
437             if (core_id >= hmap_count(&all_cpu_cores)) {
438                 return;
439             }
440         }
441     }
442
443     /* For unspecified cores, sets 'available' to false.  */
444     while (core_id < hmap_count(&all_cpu_cores)) {
445         struct cpu_core *core;
446
447         core = CONTAINER_OF(hmap_first_with_hash(&all_cpu_cores,
448                                                  hash_int(core_id++, 0)),
449                             struct cpu_core, hmap_node);
450         core->available = false;
451     }
452 }
453
454 #endif /* __linux__ */