scripts/qmp: Use Python 2.6 "except E as ..." syntax
[qemu/armbru.git] / numa.c
blobe1a05698a9f8c6b098b01fdeaa7e6cbc0190bafc
1 /*
2 * NUMA parameter parsing routines
4 * Copyright (c) 2014 Fujitsu Ltd.
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 #include "qemu/osdep.h"
26 #include "sysemu/numa.h"
27 #include "exec/cpu-common.h"
28 #include "qemu/bitmap.h"
29 #include "qom/cpu.h"
30 #include "qemu/error-report.h"
31 #include "include/exec/cpu-common.h" /* for RAM_ADDR_FMT */
32 #include "qapi-visit.h"
33 #include "qapi/opts-visitor.h"
34 #include "qapi/dealloc-visitor.h"
35 #include "hw/boards.h"
36 #include "sysemu/hostmem.h"
37 #include "qmp-commands.h"
38 #include "hw/mem/pc-dimm.h"
39 #include "qemu/option.h"
40 #include "qemu/config-file.h"
42 QemuOptsList qemu_numa_opts = {
43 .name = "numa",
44 .implied_opt_name = "type",
45 .head = QTAILQ_HEAD_INITIALIZER(qemu_numa_opts.head),
46 .desc = { { 0 } } /* validated with OptsVisitor */
49 static int have_memdevs = -1;
50 static int max_numa_nodeid; /* Highest specified NUMA node ID, plus one.
51 * For all nodes, nodeid < max_numa_nodeid
53 int nb_numa_nodes;
54 NodeInfo numa_info[MAX_NODES];
56 void numa_set_mem_node_id(ram_addr_t addr, uint64_t size, uint32_t node)
58 struct numa_addr_range *range;
61 * Memory-less nodes can come here with 0 size in which case,
62 * there is nothing to do.
64 if (!size) {
65 return;
68 range = g_malloc0(sizeof(*range));
69 range->mem_start = addr;
70 range->mem_end = addr + size - 1;
71 QLIST_INSERT_HEAD(&numa_info[node].addr, range, entry);
74 void numa_unset_mem_node_id(ram_addr_t addr, uint64_t size, uint32_t node)
76 struct numa_addr_range *range, *next;
78 QLIST_FOREACH_SAFE(range, &numa_info[node].addr, entry, next) {
79 if (addr == range->mem_start && (addr + size - 1) == range->mem_end) {
80 QLIST_REMOVE(range, entry);
81 g_free(range);
82 return;
87 static void numa_set_mem_ranges(void)
89 int i;
90 ram_addr_t mem_start = 0;
93 * Deduce start address of each node and use it to store
94 * the address range info in numa_info address range list
96 for (i = 0; i < nb_numa_nodes; i++) {
97 numa_set_mem_node_id(mem_start, numa_info[i].node_mem, i);
98 mem_start += numa_info[i].node_mem;
103 * Check if @addr falls under NUMA @node.
105 static bool numa_addr_belongs_to_node(ram_addr_t addr, uint32_t node)
107 struct numa_addr_range *range;
109 QLIST_FOREACH(range, &numa_info[node].addr, entry) {
110 if (addr >= range->mem_start && addr <= range->mem_end) {
111 return true;
114 return false;
118 * Given an address, return the index of the NUMA node to which the
119 * address belongs to.
121 uint32_t numa_get_node(ram_addr_t addr, Error **errp)
123 uint32_t i;
125 /* For non NUMA configurations, check if the addr falls under node 0 */
126 if (!nb_numa_nodes) {
127 if (numa_addr_belongs_to_node(addr, 0)) {
128 return 0;
132 for (i = 0; i < nb_numa_nodes; i++) {
133 if (numa_addr_belongs_to_node(addr, i)) {
134 return i;
138 error_setg(errp, "Address 0x" RAM_ADDR_FMT " doesn't belong to any "
139 "NUMA node", addr);
140 return -1;
143 static void numa_node_parse(NumaNodeOptions *node, QemuOpts *opts, Error **errp)
145 uint16_t nodenr;
146 uint16List *cpus = NULL;
148 if (node->has_nodeid) {
149 nodenr = node->nodeid;
150 } else {
151 nodenr = nb_numa_nodes;
154 if (nodenr >= MAX_NODES) {
155 error_setg(errp, "Max number of NUMA nodes reached: %"
156 PRIu16 "", nodenr);
157 return;
160 if (numa_info[nodenr].present) {
161 error_setg(errp, "Duplicate NUMA nodeid: %" PRIu16, nodenr);
162 return;
165 for (cpus = node->cpus; cpus; cpus = cpus->next) {
166 if (cpus->value >= max_cpus) {
167 error_setg(errp,
168 "CPU index (%" PRIu16 ")"
169 " should be smaller than maxcpus (%d)",
170 cpus->value, max_cpus);
171 return;
173 bitmap_set(numa_info[nodenr].node_cpu, cpus->value, 1);
176 if (node->has_mem && node->has_memdev) {
177 error_setg(errp, "qemu: cannot specify both mem= and memdev=");
178 return;
181 if (have_memdevs == -1) {
182 have_memdevs = node->has_memdev;
184 if (node->has_memdev != have_memdevs) {
185 error_setg(errp, "qemu: memdev option must be specified for either "
186 "all or no nodes");
187 return;
190 if (node->has_mem) {
191 uint64_t mem_size = node->mem;
192 const char *mem_str = qemu_opt_get(opts, "mem");
193 /* Fix up legacy suffix-less format */
194 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
195 mem_size <<= 20;
197 numa_info[nodenr].node_mem = mem_size;
199 if (node->has_memdev) {
200 Object *o;
201 o = object_resolve_path_type(node->memdev, TYPE_MEMORY_BACKEND, NULL);
202 if (!o) {
203 error_setg(errp, "memdev=%s is ambiguous", node->memdev);
204 return;
207 object_ref(o);
208 numa_info[nodenr].node_mem = object_property_get_int(o, "size", NULL);
209 numa_info[nodenr].node_memdev = MEMORY_BACKEND(o);
211 numa_info[nodenr].present = true;
212 max_numa_nodeid = MAX(max_numa_nodeid, nodenr + 1);
215 static int parse_numa(void *opaque, QemuOpts *opts, Error **errp)
217 NumaOptions *object = NULL;
218 Error *err = NULL;
221 OptsVisitor *ov = opts_visitor_new(opts);
222 visit_type_NumaOptions(opts_get_visitor(ov), &object, NULL, &err);
223 opts_visitor_cleanup(ov);
226 if (err) {
227 goto error;
230 switch (object->type) {
231 case NUMA_OPTIONS_KIND_NODE:
232 numa_node_parse(object->u.node, opts, &err);
233 if (err) {
234 goto error;
236 nb_numa_nodes++;
237 break;
238 default:
239 abort();
242 return 0;
244 error:
245 error_report_err(err);
247 if (object) {
248 QapiDeallocVisitor *dv = qapi_dealloc_visitor_new();
249 visit_type_NumaOptions(qapi_dealloc_get_visitor(dv),
250 &object, NULL, NULL);
251 qapi_dealloc_visitor_cleanup(dv);
254 return -1;
257 static char *enumerate_cpus(unsigned long *cpus, int max_cpus)
259 int cpu;
260 bool first = true;
261 GString *s = g_string_new(NULL);
263 for (cpu = find_first_bit(cpus, max_cpus);
264 cpu < max_cpus;
265 cpu = find_next_bit(cpus, max_cpus, cpu + 1)) {
266 g_string_append_printf(s, "%s%d", first ? "" : " ", cpu);
267 first = false;
269 return g_string_free(s, FALSE);
272 static void validate_numa_cpus(void)
274 int i;
275 DECLARE_BITMAP(seen_cpus, MAX_CPUMASK_BITS);
277 bitmap_zero(seen_cpus, MAX_CPUMASK_BITS);
278 for (i = 0; i < nb_numa_nodes; i++) {
279 if (bitmap_intersects(seen_cpus, numa_info[i].node_cpu,
280 MAX_CPUMASK_BITS)) {
281 bitmap_and(seen_cpus, seen_cpus,
282 numa_info[i].node_cpu, MAX_CPUMASK_BITS);
283 error_report("CPU(s) present in multiple NUMA nodes: %s",
284 enumerate_cpus(seen_cpus, max_cpus));
285 exit(EXIT_FAILURE);
287 bitmap_or(seen_cpus, seen_cpus,
288 numa_info[i].node_cpu, MAX_CPUMASK_BITS);
291 if (!bitmap_full(seen_cpus, max_cpus)) {
292 char *msg;
293 bitmap_complement(seen_cpus, seen_cpus, max_cpus);
294 msg = enumerate_cpus(seen_cpus, max_cpus);
295 error_report("warning: CPU(s) not present in any NUMA nodes: %s", msg);
296 error_report("warning: All CPU(s) up to maxcpus should be described "
297 "in NUMA config");
298 g_free(msg);
302 void parse_numa_opts(MachineClass *mc)
304 int i;
306 if (qemu_opts_foreach(qemu_find_opts("numa"), parse_numa, NULL, NULL)) {
307 exit(1);
310 assert(max_numa_nodeid <= MAX_NODES);
312 /* No support for sparse NUMA node IDs yet: */
313 for (i = max_numa_nodeid - 1; i >= 0; i--) {
314 /* Report large node IDs first, to make mistakes easier to spot */
315 if (!numa_info[i].present) {
316 error_report("numa: Node ID missing: %d", i);
317 exit(1);
321 /* This must be always true if all nodes are present: */
322 assert(nb_numa_nodes == max_numa_nodeid);
324 if (nb_numa_nodes > 0) {
325 uint64_t numa_total;
327 if (nb_numa_nodes > MAX_NODES) {
328 nb_numa_nodes = MAX_NODES;
331 /* If no memory size is given for any node, assume the default case
332 * and distribute the available memory equally across all nodes
334 for (i = 0; i < nb_numa_nodes; i++) {
335 if (numa_info[i].node_mem != 0) {
336 break;
339 if (i == nb_numa_nodes) {
340 uint64_t usedmem = 0;
342 /* On Linux, each node's border has to be 8MB aligned,
343 * the final node gets the rest.
345 for (i = 0; i < nb_numa_nodes - 1; i++) {
346 numa_info[i].node_mem = (ram_size / nb_numa_nodes) &
347 ~((1 << 23UL) - 1);
348 usedmem += numa_info[i].node_mem;
350 numa_info[i].node_mem = ram_size - usedmem;
353 numa_total = 0;
354 for (i = 0; i < nb_numa_nodes; i++) {
355 numa_total += numa_info[i].node_mem;
357 if (numa_total != ram_size) {
358 error_report("total memory for NUMA nodes (0x%" PRIx64 ")"
359 " should equal RAM size (0x" RAM_ADDR_FMT ")",
360 numa_total, ram_size);
361 exit(1);
364 for (i = 0; i < nb_numa_nodes; i++) {
365 QLIST_INIT(&numa_info[i].addr);
368 numa_set_mem_ranges();
370 for (i = 0; i < nb_numa_nodes; i++) {
371 if (!bitmap_empty(numa_info[i].node_cpu, MAX_CPUMASK_BITS)) {
372 break;
375 /* Historically VCPUs were assigned in round-robin order to NUMA
376 * nodes. However it causes issues with guest not handling it nice
377 * in case where cores/threads from a multicore CPU appear on
378 * different nodes. So allow boards to override default distribution
379 * rule grouping VCPUs by socket so that VCPUs from the same socket
380 * would be on the same node.
382 if (i == nb_numa_nodes) {
383 for (i = 0; i < max_cpus; i++) {
384 unsigned node_id = i % nb_numa_nodes;
385 if (mc->cpu_index_to_socket_id) {
386 node_id = mc->cpu_index_to_socket_id(i) % nb_numa_nodes;
389 set_bit(i, numa_info[node_id].node_cpu);
393 validate_numa_cpus();
394 } else {
395 numa_set_mem_node_id(0, ram_size, 0);
399 void numa_post_machine_init(void)
401 CPUState *cpu;
402 int i;
404 CPU_FOREACH(cpu) {
405 for (i = 0; i < nb_numa_nodes; i++) {
406 if (test_bit(cpu->cpu_index, numa_info[i].node_cpu)) {
407 cpu->numa_node = i;
413 static void allocate_system_memory_nonnuma(MemoryRegion *mr, Object *owner,
414 const char *name,
415 uint64_t ram_size)
417 if (mem_path) {
418 #ifdef __linux__
419 Error *err = NULL;
420 memory_region_init_ram_from_file(mr, owner, name, ram_size, false,
421 mem_path, &err);
422 if (err) {
423 error_report_err(err);
424 if (mem_prealloc) {
425 exit(1);
428 /* Legacy behavior: if allocation failed, fall back to
429 * regular RAM allocation.
431 memory_region_init_ram(mr, owner, name, ram_size, &error_fatal);
433 #else
434 fprintf(stderr, "-mem-path not supported on this host\n");
435 exit(1);
436 #endif
437 } else {
438 memory_region_init_ram(mr, owner, name, ram_size, &error_fatal);
440 vmstate_register_ram_global(mr);
443 void memory_region_allocate_system_memory(MemoryRegion *mr, Object *owner,
444 const char *name,
445 uint64_t ram_size)
447 uint64_t addr = 0;
448 int i;
450 if (nb_numa_nodes == 0 || !have_memdevs) {
451 allocate_system_memory_nonnuma(mr, owner, name, ram_size);
452 return;
455 memory_region_init(mr, owner, name, ram_size);
456 for (i = 0; i < MAX_NODES; i++) {
457 uint64_t size = numa_info[i].node_mem;
458 HostMemoryBackend *backend = numa_info[i].node_memdev;
459 if (!backend) {
460 continue;
462 MemoryRegion *seg = host_memory_backend_get_memory(backend,
463 &error_fatal);
465 if (memory_region_is_mapped(seg)) {
466 char *path = object_get_canonical_path_component(OBJECT(backend));
467 error_report("memory backend %s is used multiple times. Each "
468 "-numa option must use a different memdev value.",
469 path);
470 exit(1);
473 memory_region_add_subregion(mr, addr, seg);
474 vmstate_register_ram_global(seg);
475 addr += size;
479 static void numa_stat_memory_devices(uint64_t node_mem[])
481 MemoryDeviceInfoList *info_list = NULL;
482 MemoryDeviceInfoList **prev = &info_list;
483 MemoryDeviceInfoList *info;
485 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
486 for (info = info_list; info; info = info->next) {
487 MemoryDeviceInfo *value = info->value;
489 if (value) {
490 switch (value->type) {
491 case MEMORY_DEVICE_INFO_KIND_DIMM:
492 node_mem[value->u.dimm->node] += value->u.dimm->size;
493 break;
494 default:
495 break;
499 qapi_free_MemoryDeviceInfoList(info_list);
502 void query_numa_node_mem(uint64_t node_mem[])
504 int i;
506 if (nb_numa_nodes <= 0) {
507 return;
510 numa_stat_memory_devices(node_mem);
511 for (i = 0; i < nb_numa_nodes; i++) {
512 node_mem[i] += numa_info[i].node_mem;
516 static int query_memdev(Object *obj, void *opaque)
518 MemdevList **list = opaque;
519 MemdevList *m = NULL;
521 if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
522 m = g_malloc0(sizeof(*m));
524 m->value = g_malloc0(sizeof(*m->value));
526 m->value->size = object_property_get_int(obj, "size",
527 &error_abort);
528 m->value->merge = object_property_get_bool(obj, "merge",
529 &error_abort);
530 m->value->dump = object_property_get_bool(obj, "dump",
531 &error_abort);
532 m->value->prealloc = object_property_get_bool(obj,
533 "prealloc",
534 &error_abort);
535 m->value->policy = object_property_get_enum(obj,
536 "policy",
537 "HostMemPolicy",
538 &error_abort);
539 object_property_get_uint16List(obj, "host-nodes",
540 &m->value->host_nodes,
541 &error_abort);
543 m->next = *list;
544 *list = m;
547 return 0;
550 MemdevList *qmp_query_memdev(Error **errp)
552 Object *obj = object_get_objects_root();
553 MemdevList *list = NULL;
555 object_child_foreach(obj, query_memdev, &list);
556 return list;