]> Git Repo - qemu.git/blob - numa.c
Merge remote-tracking branch 'remotes/armbru/tags/pull-monitor-2015-06-22' into staging
[qemu.git] / numa.c
1 /*
2  * NUMA parameter parsing routines
3  *
4  * Copyright (c) 2014 Fujitsu Ltd.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24
25 #include "sysemu/numa.h"
26 #include "exec/cpu-common.h"
27 #include "qemu/bitmap.h"
28 #include "qom/cpu.h"
29 #include "qemu/error-report.h"
30 #include "include/exec/cpu-common.h" /* for RAM_ADDR_FMT */
31 #include "qapi-visit.h"
32 #include "qapi/opts-visitor.h"
33 #include "qapi/dealloc-visitor.h"
34 #include "hw/boards.h"
35 #include "sysemu/hostmem.h"
36 #include "qmp-commands.h"
37 #include "hw/mem/pc-dimm.h"
38 #include "qemu/option.h"
39 #include "qemu/config-file.h"
40
41 QemuOptsList qemu_numa_opts = {
42     .name = "numa",
43     .implied_opt_name = "type",
44     .head = QTAILQ_HEAD_INITIALIZER(qemu_numa_opts.head),
45     .desc = { { 0 } } /* validated with OptsVisitor */
46 };
47
48 static int have_memdevs = -1;
49 static int max_numa_nodeid; /* Highest specified NUMA node ID, plus one.
50                              * For all nodes, nodeid < max_numa_nodeid
51                              */
52 int nb_numa_nodes;
53 NodeInfo numa_info[MAX_NODES];
54
55 static void numa_node_parse(NumaNodeOptions *node, QemuOpts *opts, Error **errp)
56 {
57     uint16_t nodenr;
58     uint16List *cpus = NULL;
59
60     if (node->has_nodeid) {
61         nodenr = node->nodeid;
62     } else {
63         nodenr = nb_numa_nodes;
64     }
65
66     if (nodenr >= MAX_NODES) {
67         error_setg(errp, "Max number of NUMA nodes reached: %"
68                    PRIu16 "", nodenr);
69         return;
70     }
71
72     if (numa_info[nodenr].present) {
73         error_setg(errp, "Duplicate NUMA nodeid: %" PRIu16, nodenr);
74         return;
75     }
76
77     for (cpus = node->cpus; cpus; cpus = cpus->next) {
78         if (cpus->value >= max_cpus) {
79             error_setg(errp,
80                        "CPU index (%" PRIu16 ")"
81                        " should be smaller than maxcpus (%d)",
82                        cpus->value, max_cpus);
83             return;
84         }
85         bitmap_set(numa_info[nodenr].node_cpu, cpus->value, 1);
86     }
87
88     if (node->has_mem && node->has_memdev) {
89         error_setg(errp, "qemu: cannot specify both mem= and memdev=");
90         return;
91     }
92
93     if (have_memdevs == -1) {
94         have_memdevs = node->has_memdev;
95     }
96     if (node->has_memdev != have_memdevs) {
97         error_setg(errp, "qemu: memdev option must be specified for either "
98                    "all or no nodes");
99         return;
100     }
101
102     if (node->has_mem) {
103         uint64_t mem_size = node->mem;
104         const char *mem_str = qemu_opt_get(opts, "mem");
105         /* Fix up legacy suffix-less format */
106         if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
107             mem_size <<= 20;
108         }
109         numa_info[nodenr].node_mem = mem_size;
110     }
111     if (node->has_memdev) {
112         Object *o;
113         o = object_resolve_path_type(node->memdev, TYPE_MEMORY_BACKEND, NULL);
114         if (!o) {
115             error_setg(errp, "memdev=%s is ambiguous", node->memdev);
116             return;
117         }
118
119         object_ref(o);
120         numa_info[nodenr].node_mem = object_property_get_int(o, "size", NULL);
121         numa_info[nodenr].node_memdev = MEMORY_BACKEND(o);
122     }
123     numa_info[nodenr].present = true;
124     max_numa_nodeid = MAX(max_numa_nodeid, nodenr + 1);
125 }
126
127 static int parse_numa(void *opaque, QemuOpts *opts, Error **errp)
128 {
129     NumaOptions *object = NULL;
130     Error *err = NULL;
131
132     {
133         OptsVisitor *ov = opts_visitor_new(opts);
134         visit_type_NumaOptions(opts_get_visitor(ov), &object, NULL, &err);
135         opts_visitor_cleanup(ov);
136     }
137
138     if (err) {
139         goto error;
140     }
141
142     switch (object->kind) {
143     case NUMA_OPTIONS_KIND_NODE:
144         numa_node_parse(object->node, opts, &err);
145         if (err) {
146             goto error;
147         }
148         nb_numa_nodes++;
149         break;
150     default:
151         abort();
152     }
153
154     return 0;
155
156 error:
157     error_report_err(err);
158
159     if (object) {
160         QapiDeallocVisitor *dv = qapi_dealloc_visitor_new();
161         visit_type_NumaOptions(qapi_dealloc_get_visitor(dv),
162                                &object, NULL, NULL);
163         qapi_dealloc_visitor_cleanup(dv);
164     }
165
166     return -1;
167 }
168
169 static char *enumerate_cpus(unsigned long *cpus, int max_cpus)
170 {
171     int cpu;
172     bool first = true;
173     GString *s = g_string_new(NULL);
174
175     for (cpu = find_first_bit(cpus, max_cpus);
176         cpu < max_cpus;
177         cpu = find_next_bit(cpus, max_cpus, cpu + 1)) {
178         g_string_append_printf(s, "%s%d", first ? "" : " ", cpu);
179         first = false;
180     }
181     return g_string_free(s, FALSE);
182 }
183
184 static void validate_numa_cpus(void)
185 {
186     int i;
187     DECLARE_BITMAP(seen_cpus, MAX_CPUMASK_BITS);
188
189     bitmap_zero(seen_cpus, MAX_CPUMASK_BITS);
190     for (i = 0; i < nb_numa_nodes; i++) {
191         if (bitmap_intersects(seen_cpus, numa_info[i].node_cpu,
192                               MAX_CPUMASK_BITS)) {
193             bitmap_and(seen_cpus, seen_cpus,
194                        numa_info[i].node_cpu, MAX_CPUMASK_BITS);
195             error_report("CPU(s) present in multiple NUMA nodes: %s",
196                          enumerate_cpus(seen_cpus, max_cpus));;
197             exit(EXIT_FAILURE);
198         }
199         bitmap_or(seen_cpus, seen_cpus,
200                   numa_info[i].node_cpu, MAX_CPUMASK_BITS);
201     }
202
203     if (!bitmap_full(seen_cpus, max_cpus)) {
204         char *msg;
205         bitmap_complement(seen_cpus, seen_cpus, max_cpus);
206         msg = enumerate_cpus(seen_cpus, max_cpus);
207         error_report("warning: CPU(s) not present in any NUMA nodes: %s", msg);
208         error_report("warning: All CPU(s) up to maxcpus should be described "
209                      "in NUMA config");
210         g_free(msg);
211     }
212 }
213
214 void parse_numa_opts(MachineClass *mc)
215 {
216     int i;
217
218     if (qemu_opts_foreach(qemu_find_opts("numa"), parse_numa, NULL, NULL)) {
219         exit(1);
220     }
221
222     assert(max_numa_nodeid <= MAX_NODES);
223
224     /* No support for sparse NUMA node IDs yet: */
225     for (i = max_numa_nodeid - 1; i >= 0; i--) {
226         /* Report large node IDs first, to make mistakes easier to spot */
227         if (!numa_info[i].present) {
228             error_report("numa: Node ID missing: %d", i);
229             exit(1);
230         }
231     }
232
233     /* This must be always true if all nodes are present: */
234     assert(nb_numa_nodes == max_numa_nodeid);
235
236     if (nb_numa_nodes > 0) {
237         uint64_t numa_total;
238
239         if (nb_numa_nodes > MAX_NODES) {
240             nb_numa_nodes = MAX_NODES;
241         }
242
243         /* If no memory size is given for any node, assume the default case
244          * and distribute the available memory equally across all nodes
245          */
246         for (i = 0; i < nb_numa_nodes; i++) {
247             if (numa_info[i].node_mem != 0) {
248                 break;
249             }
250         }
251         if (i == nb_numa_nodes) {
252             uint64_t usedmem = 0;
253
254             /* On Linux, each node's border has to be 8MB aligned,
255              * the final node gets the rest.
256              */
257             for (i = 0; i < nb_numa_nodes - 1; i++) {
258                 numa_info[i].node_mem = (ram_size / nb_numa_nodes) &
259                                         ~((1 << 23UL) - 1);
260                 usedmem += numa_info[i].node_mem;
261             }
262             numa_info[i].node_mem = ram_size - usedmem;
263         }
264
265         numa_total = 0;
266         for (i = 0; i < nb_numa_nodes; i++) {
267             numa_total += numa_info[i].node_mem;
268         }
269         if (numa_total != ram_size) {
270             error_report("total memory for NUMA nodes (0x%" PRIx64 ")"
271                          " should equal RAM size (0x" RAM_ADDR_FMT ")",
272                          numa_total, ram_size);
273             exit(1);
274         }
275
276         for (i = 0; i < nb_numa_nodes; i++) {
277             if (!bitmap_empty(numa_info[i].node_cpu, MAX_CPUMASK_BITS)) {
278                 break;
279             }
280         }
281         /* Historically VCPUs were assigned in round-robin order to NUMA
282          * nodes. However it causes issues with guest not handling it nice
283          * in case where cores/threads from a multicore CPU appear on
284          * different nodes. So allow boards to override default distribution
285          * rule grouping VCPUs by socket so that VCPUs from the same socket
286          * would be on the same node.
287          */
288         if (i == nb_numa_nodes) {
289             for (i = 0; i < max_cpus; i++) {
290                 unsigned node_id = i % nb_numa_nodes;
291                 if (mc->cpu_index_to_socket_id) {
292                     node_id = mc->cpu_index_to_socket_id(i) % nb_numa_nodes;
293                 }
294
295                 set_bit(i, numa_info[node_id].node_cpu);
296             }
297         }
298
299         validate_numa_cpus();
300     }
301 }
302
303 void numa_post_machine_init(void)
304 {
305     CPUState *cpu;
306     int i;
307
308     CPU_FOREACH(cpu) {
309         for (i = 0; i < nb_numa_nodes; i++) {
310             if (test_bit(cpu->cpu_index, numa_info[i].node_cpu)) {
311                 cpu->numa_node = i;
312             }
313         }
314     }
315 }
316
317 static void allocate_system_memory_nonnuma(MemoryRegion *mr, Object *owner,
318                                            const char *name,
319                                            uint64_t ram_size)
320 {
321     if (mem_path) {
322 #ifdef __linux__
323         Error *err = NULL;
324         memory_region_init_ram_from_file(mr, owner, name, ram_size, false,
325                                          mem_path, &err);
326
327         /* Legacy behavior: if allocation failed, fall back to
328          * regular RAM allocation.
329          */
330         if (err) {
331             error_report_err(err);
332             memory_region_init_ram(mr, owner, name, ram_size, &error_abort);
333         }
334 #else
335         fprintf(stderr, "-mem-path not supported on this host\n");
336         exit(1);
337 #endif
338     } else {
339         memory_region_init_ram(mr, owner, name, ram_size, &error_abort);
340     }
341     vmstate_register_ram_global(mr);
342 }
343
344 void memory_region_allocate_system_memory(MemoryRegion *mr, Object *owner,
345                                           const char *name,
346                                           uint64_t ram_size)
347 {
348     uint64_t addr = 0;
349     int i;
350
351     if (nb_numa_nodes == 0 || !have_memdevs) {
352         allocate_system_memory_nonnuma(mr, owner, name, ram_size);
353         return;
354     }
355
356     memory_region_init(mr, owner, name, ram_size);
357     for (i = 0; i < MAX_NODES; i++) {
358         Error *local_err = NULL;
359         uint64_t size = numa_info[i].node_mem;
360         HostMemoryBackend *backend = numa_info[i].node_memdev;
361         if (!backend) {
362             continue;
363         }
364         MemoryRegion *seg = host_memory_backend_get_memory(backend, &local_err);
365         if (local_err) {
366             error_report_err(local_err);
367             exit(1);
368         }
369
370         if (memory_region_is_mapped(seg)) {
371             char *path = object_get_canonical_path_component(OBJECT(backend));
372             error_report("memory backend %s is used multiple times. Each "
373                          "-numa option must use a different memdev value.",
374                          path);
375             exit(1);
376         }
377
378         memory_region_add_subregion(mr, addr, seg);
379         vmstate_register_ram_global(seg);
380         addr += size;
381     }
382 }
383
384 static void numa_stat_memory_devices(uint64_t node_mem[])
385 {
386     MemoryDeviceInfoList *info_list = NULL;
387     MemoryDeviceInfoList **prev = &info_list;
388     MemoryDeviceInfoList *info;
389
390     qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
391     for (info = info_list; info; info = info->next) {
392         MemoryDeviceInfo *value = info->value;
393
394         if (value) {
395             switch (value->kind) {
396             case MEMORY_DEVICE_INFO_KIND_DIMM:
397                 node_mem[value->dimm->node] += value->dimm->size;
398                 break;
399             default:
400                 break;
401             }
402         }
403     }
404     qapi_free_MemoryDeviceInfoList(info_list);
405 }
406
407 void query_numa_node_mem(uint64_t node_mem[])
408 {
409     int i;
410
411     if (nb_numa_nodes <= 0) {
412         return;
413     }
414
415     numa_stat_memory_devices(node_mem);
416     for (i = 0; i < nb_numa_nodes; i++) {
417         node_mem[i] += numa_info[i].node_mem;
418     }
419 }
420
421 static int query_memdev(Object *obj, void *opaque)
422 {
423     MemdevList **list = opaque;
424     MemdevList *m = NULL;
425     Error *err = NULL;
426
427     if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
428         m = g_malloc0(sizeof(*m));
429
430         m->value = g_malloc0(sizeof(*m->value));
431
432         m->value->size = object_property_get_int(obj, "size",
433                                                  &err);
434         if (err) {
435             goto error;
436         }
437
438         m->value->merge = object_property_get_bool(obj, "merge",
439                                                    &err);
440         if (err) {
441             goto error;
442         }
443
444         m->value->dump = object_property_get_bool(obj, "dump",
445                                                   &err);
446         if (err) {
447             goto error;
448         }
449
450         m->value->prealloc = object_property_get_bool(obj,
451                                                       "prealloc", &err);
452         if (err) {
453             goto error;
454         }
455
456         m->value->policy = object_property_get_enum(obj,
457                                                     "policy",
458                                                     "HostMemPolicy",
459                                                     &err);
460         if (err) {
461             goto error;
462         }
463
464         object_property_get_uint16List(obj, "host-nodes",
465                                        &m->value->host_nodes, &err);
466         if (err) {
467             goto error;
468         }
469
470         m->next = *list;
471         *list = m;
472     }
473
474     return 0;
475 error:
476     g_free(m->value);
477     g_free(m);
478
479     return -1;
480 }
481
482 MemdevList *qmp_query_memdev(Error **errp)
483 {
484     Object *obj;
485     MemdevList *list = NULL;
486
487     obj = object_get_objects_root();
488     if (obj == NULL) {
489         return NULL;
490     }
491
492     if (object_child_foreach(obj, query_memdev, &list) != 0) {
493         goto error;
494     }
495
496     return list;
497
498 error:
499     qapi_free_MemdevList(list);
500     return NULL;
501 }
This page took 0.052783 seconds and 4 git commands to generate.