lib/odp-util: Fix mapping to Netlink frag mask.
[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 "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, 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.
53  *
54  * TODO: Fix ovs-numa when cpu hotplug is used.
55  */
56
57 #define MAX_NUMA_NODES 128
58
59 /* numa node. */
60 struct numa_node {
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. */
64 };
65
66 /* Cpu core on a numa node. */
67 struct cpu_core {
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 pinned;               /* If a thread has been pinned to the core. */
73 };
74
75 /* Contains all 'struct numa_node's. */
76 static struct hmap all_numa_nodes = HMAP_INITIALIZER(&all_numa_nodes);
77 /* Contains all 'struct cpu_core's. */
78 static struct hmap all_cpu_cores = HMAP_INITIALIZER(&all_cpu_cores);
79 /* True if numa node and core info are correctly extracted. */
80 static bool found_numa_and_core;
81
82 /* Returns true if 'str' contains all digits.  Returns false otherwise. */
83 static bool
84 contain_all_digits(const char *str)
85 {
86     return str[strspn(str, "0123456789")] == '\0';
87 }
88
89 /* Discovers all numa nodes and the corresponding cpu cores.
90  * Constructs the 'struct numa_node' and 'struct cpu_core'. */
91 static void
92 discover_numa_and_core(void)
93 {
94     int n_cpus = 0;
95     int i;
96
97     for (i = 0; i < MAX_NUMA_NODES; i++) {
98         DIR *dir;
99         char* path;
100
101         /* Constructs the path to node /sys/devices/system/nodeX. */
102         path = xasprintf("/sys/devices/system/node/node%d", i);
103         dir = opendir(path);
104
105         /* Creates 'struct numa_node' if the 'dir' is non-null. */
106         if (dir) {
107             struct numa_node *n = xzalloc(sizeof *n);
108             struct dirent *subdir;
109
110             hmap_insert(&all_numa_nodes, &n->hmap_node, hash_int(i, 0));
111             list_init(&n->cores);
112             n->numa_id = i;
113
114             while ((subdir = readdir(dir)) != NULL) {
115                 if (!strncmp(subdir->d_name, "cpu", 3)
116                     && contain_all_digits(subdir->d_name + 3)){
117                     struct cpu_core *c = xzalloc(sizeof *c);
118                     uint32_t core_id;
119
120                     core_id = strtoul(subdir->d_name + 3, NULL, 10);
121                     hmap_insert(&all_cpu_cores, &c->hmap_node,
122                                 hash_int(core_id, 0));
123                     list_insert(&n->cores, &c->list_node);
124                     c->core_id = core_id;
125                     c->numa = n;
126                     n_cpus++;
127                 }
128             }
129             VLOG_INFO("Discovered %"PRIuSIZE" CPU cores on NUMA node %d",
130                       list_size(&n->cores), n->numa_id);
131             free(path);
132             closedir(dir);
133         } else {
134             if (errno != ENOENT) {
135                 VLOG_WARN("opendir(%s) failed (%s)", path,
136                           ovs_strerror(errno));
137             }
138             free(path);
139             break;
140         }
141     }
142
143     VLOG_INFO("Discovered %"PRIuSIZE" NUMA nodes and %d CPU cores",
144                hmap_count(&all_numa_nodes), n_cpus);
145     if (hmap_count(&all_numa_nodes) && hmap_count(&all_cpu_cores)) {
146         found_numa_and_core = true;
147     }
148 }
149
150 /* Extracts the numa node and core info from the 'sysfs'. */
151 void
152 ovs_numa_init(void)
153 {
154     static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
155
156     if (ovsthread_once_start(&once)) {
157         discover_numa_and_core();
158         ovsthread_once_done(&once);
159     }
160 }
161
162 bool
163 ovs_numa_numa_id_is_valid(int numa_id)
164 {
165     return found_numa_and_core && numa_id < ovs_numa_get_n_numas();
166 }
167
168 bool
169 ovs_numa_core_id_is_valid(int core_id)
170 {
171     return found_numa_and_core && core_id < ovs_numa_get_n_cores();
172 }
173
174 /* Returns the number of numa nodes. */
175 int
176 ovs_numa_get_n_numas(void)
177 {
178     return found_numa_and_core ? hmap_count(&all_numa_nodes)
179                                : OVS_NUMA_UNSPEC;
180 }
181
182 /* Returns the number of cpu cores. */
183 int
184 ovs_numa_get_n_cores(void)
185 {
186     return found_numa_and_core ? hmap_count(&all_cpu_cores)
187                                : OVS_CORE_UNSPEC;
188 }
189
190 /* Given 'core_id', returns the corresponding numa node id.  Returns
191  * OVS_NUMA_UNSPEC if 'core_id' is invalid. */
192 int
193 ovs_numa_get_numa_id(int core_id)
194 {
195     if (ovs_numa_core_id_is_valid(core_id)) {
196         struct cpu_core *core;
197
198         core = CONTAINER_OF(hmap_first_with_hash(&all_cpu_cores,
199                                                  hash_int(core_id, 0)),
200                             struct cpu_core, hmap_node);
201
202         return core->numa->numa_id;
203     }
204     return OVS_NUMA_UNSPEC;
205 }
206
207 /* Returns the number of cpu cores on numa node.  Returns OVS_CORE_UNSPEC
208  * if 'numa_id' is invalid. */
209 int
210 ovs_numa_get_n_cores_on_numa(int numa_id)
211 {
212     if (ovs_numa_numa_id_is_valid(numa_id)) {
213         struct numa_node *numa;
214
215         numa = CONTAINER_OF(hmap_first_with_hash(&all_numa_nodes,
216                                                  hash_int(numa_id, 0)),
217                             struct numa_node, hmap_node);
218
219         return list_size(&numa->cores);
220     }
221
222     return OVS_CORE_UNSPEC;
223 }
224
225 /* Returns the number of unpinned cpu cores on numa node.  Returns
226  * OVS_CORE_UNSPEC if 'numa_id' is invalid. */
227 int
228 ovs_numa_get_n_unpinned_cores_on_numa(int numa_id)
229 {
230     if (ovs_numa_numa_id_is_valid(numa_id)) {
231         struct numa_node *numa;
232         struct cpu_core *core;
233         int count = 0;
234
235         numa = CONTAINER_OF(hmap_first_with_hash(&all_numa_nodes,
236                                                  hash_int(numa_id, 0)),
237                             struct numa_node, hmap_node);
238         LIST_FOR_EACH(core, list_node, &numa->cores) {
239             if (!core->pinned) {
240                 count++;
241             }
242         }
243
244         return count;
245     }
246
247     return OVS_CORE_UNSPEC;
248 }
249
250 /* Given 'core_id', tries to pin that core.  Returns true, if succeeds.
251  * False, if the core has already been pinned or if 'core_id' is invalid. */
252 bool
253 ovs_numa_try_pin_core_specific(int core_id)
254 {
255     if (ovs_numa_core_id_is_valid(core_id)) {
256         struct cpu_core *core;
257
258         core = CONTAINER_OF(hmap_first_with_hash(&all_cpu_cores,
259                                                  hash_int(core_id, 0)),
260                             struct cpu_core, hmap_node);
261         if (!core->pinned) {
262             core->pinned = true;
263             return true;
264         }
265     }
266
267     return false;
268 }
269
270 /* Searches through all cores for an unpinned core.  Returns the core_id
271  * if found and set the 'core->pinned' to true.  Otherwise, returns
272  * OVS_CORE_UNSPEC. */
273 int
274 ovs_numa_get_unpinned_core_any(void)
275 {
276     struct cpu_core *core;
277
278     HMAP_FOR_EACH(core, hmap_node, &all_cpu_cores) {
279         if (!core->pinned) {
280             core->pinned = true;
281             return core->core_id;
282         }
283     }
284
285     return OVS_CORE_UNSPEC;
286 }
287
288 /* Searches through all cores on numa node with 'numa_id' for an unpinned
289  * core.  Returns the core_id if found and sets the 'core->pinned' to true.
290  * Otherwise, returns OVS_CORE_UNSPEC. */
291 int
292 ovs_numa_get_unpinned_core_on_numa(int numa_id)
293 {
294     if (ovs_numa_numa_id_is_valid(numa_id)) {
295         struct numa_node *numa;
296         struct cpu_core *core;
297
298         numa = CONTAINER_OF(hmap_first_with_hash(&all_numa_nodes,
299                                                  hash_int(numa_id, 0)),
300                             struct numa_node, hmap_node);
301         LIST_FOR_EACH(core, list_node, &numa->cores) {
302             if (!core->pinned) {
303                 core->pinned = true;
304                 return core->core_id;
305             }
306         }
307     }
308
309     return OVS_CORE_UNSPEC;
310 }
311
312 /* Resets the 'core->pinned' for the core with 'core_id'. */
313 void
314 ovs_numa_unpin_core(int core_id)
315 {
316     if (ovs_numa_core_id_is_valid(core_id)) {
317         struct cpu_core *core;
318
319         core = CONTAINER_OF(hmap_first_with_hash(&all_cpu_cores,
320                                                  hash_int(core_id, 0)),
321                             struct cpu_core, hmap_node);
322         core->pinned = false;
323     }
324 }
325
326 #endif /* __linux__ */