numa: Rename option parsing functions
[qemu/cris-port.git] / numa.c
blobd5b95e1a0af16953775dd79f6f52a148a8492d81
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 "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 "qapi/qmp/qerror.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 static void numa_node_parse(NumaNodeOptions *node, QemuOpts *opts, Error **errp)
58 uint16_t nodenr;
59 uint16List *cpus = NULL;
61 if (node->has_nodeid) {
62 nodenr = node->nodeid;
63 } else {
64 nodenr = nb_numa_nodes;
67 if (nodenr >= MAX_NODES) {
68 error_setg(errp, "Max number of NUMA nodes reached: %"
69 PRIu16 "\n", nodenr);
70 return;
73 if (numa_info[nodenr].present) {
74 error_setg(errp, "Duplicate NUMA nodeid: %" PRIu16, nodenr);
75 return;
78 for (cpus = node->cpus; cpus; cpus = cpus->next) {
79 if (cpus->value > MAX_CPUMASK_BITS) {
80 error_setg(errp, "CPU number %" PRIu16 " is bigger than %d",
81 cpus->value, MAX_CPUMASK_BITS);
82 return;
84 bitmap_set(numa_info[nodenr].node_cpu, cpus->value, 1);
87 if (node->has_mem && node->has_memdev) {
88 error_setg(errp, "qemu: cannot specify both mem= and memdev=\n");
89 return;
92 if (have_memdevs == -1) {
93 have_memdevs = node->has_memdev;
95 if (node->has_memdev != have_memdevs) {
96 error_setg(errp, "qemu: memdev option must be specified for either "
97 "all or no nodes\n");
98 return;
101 if (node->has_mem) {
102 uint64_t mem_size = node->mem;
103 const char *mem_str = qemu_opt_get(opts, "mem");
104 /* Fix up legacy suffix-less format */
105 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
106 mem_size <<= 20;
108 numa_info[nodenr].node_mem = mem_size;
110 if (node->has_memdev) {
111 Object *o;
112 o = object_resolve_path_type(node->memdev, TYPE_MEMORY_BACKEND, NULL);
113 if (!o) {
114 error_setg(errp, "memdev=%s is ambiguous", node->memdev);
115 return;
118 object_ref(o);
119 numa_info[nodenr].node_mem = object_property_get_int(o, "size", NULL);
120 numa_info[nodenr].node_memdev = MEMORY_BACKEND(o);
122 numa_info[nodenr].present = true;
123 max_numa_nodeid = MAX(max_numa_nodeid, nodenr + 1);
126 static int parse_numa(QemuOpts *opts, void *opaque)
128 NumaOptions *object = NULL;
129 Error *err = NULL;
132 OptsVisitor *ov = opts_visitor_new(opts);
133 visit_type_NumaOptions(opts_get_visitor(ov), &object, NULL, &err);
134 opts_visitor_cleanup(ov);
137 if (err) {
138 goto error;
141 switch (object->kind) {
142 case NUMA_OPTIONS_KIND_NODE:
143 numa_node_parse(object->node, opts, &err);
144 if (err) {
145 goto error;
147 nb_numa_nodes++;
148 break;
149 default:
150 abort();
153 return 0;
155 error:
156 qerror_report_err(err);
157 error_free(err);
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);
166 return -1;
169 void parse_numa_opts(void)
171 int i;
173 if (qemu_opts_foreach(qemu_find_opts("numa"), parse_numa,
174 NULL, 1) != 0) {
175 exit(1);
178 assert(max_numa_nodeid <= MAX_NODES);
180 /* No support for sparse NUMA node IDs yet: */
181 for (i = max_numa_nodeid - 1; i >= 0; i--) {
182 /* Report large node IDs first, to make mistakes easier to spot */
183 if (!numa_info[i].present) {
184 error_report("numa: Node ID missing: %d", i);
185 exit(1);
189 /* This must be always true if all nodes are present: */
190 assert(nb_numa_nodes == max_numa_nodeid);
192 if (nb_numa_nodes > 0) {
193 uint64_t numa_total;
195 if (nb_numa_nodes > MAX_NODES) {
196 nb_numa_nodes = MAX_NODES;
199 /* If no memory size is given for any node, assume the default case
200 * and distribute the available memory equally across all nodes
202 for (i = 0; i < nb_numa_nodes; i++) {
203 if (numa_info[i].node_mem != 0) {
204 break;
207 if (i == nb_numa_nodes) {
208 uint64_t usedmem = 0;
210 /* On Linux, each node's border has to be 8MB aligned,
211 * the final node gets the rest.
213 for (i = 0; i < nb_numa_nodes - 1; i++) {
214 numa_info[i].node_mem = (ram_size / nb_numa_nodes) &
215 ~((1 << 23UL) - 1);
216 usedmem += numa_info[i].node_mem;
218 numa_info[i].node_mem = ram_size - usedmem;
221 numa_total = 0;
222 for (i = 0; i < nb_numa_nodes; i++) {
223 numa_total += numa_info[i].node_mem;
225 if (numa_total != ram_size) {
226 error_report("total memory for NUMA nodes (0x%" PRIx64 ")"
227 " should equal RAM size (0x" RAM_ADDR_FMT ")",
228 numa_total, ram_size);
229 exit(1);
232 for (i = 0; i < nb_numa_nodes; i++) {
233 if (!bitmap_empty(numa_info[i].node_cpu, MAX_CPUMASK_BITS)) {
234 break;
237 /* assigning the VCPUs round-robin is easier to implement, guest OSes
238 * must cope with this anyway, because there are BIOSes out there in
239 * real machines which also use this scheme.
241 if (i == nb_numa_nodes) {
242 for (i = 0; i < max_cpus; i++) {
243 set_bit(i, numa_info[i % nb_numa_nodes].node_cpu);
249 void set_numa_modes(void)
251 CPUState *cpu;
252 int i;
254 CPU_FOREACH(cpu) {
255 for (i = 0; i < nb_numa_nodes; i++) {
256 if (test_bit(cpu->cpu_index, numa_info[i].node_cpu)) {
257 cpu->numa_node = i;
263 static void allocate_system_memory_nonnuma(MemoryRegion *mr, Object *owner,
264 const char *name,
265 uint64_t ram_size)
267 if (mem_path) {
268 #ifdef __linux__
269 Error *err = NULL;
270 memory_region_init_ram_from_file(mr, owner, name, ram_size, false,
271 mem_path, &err);
273 /* Legacy behavior: if allocation failed, fall back to
274 * regular RAM allocation.
276 if (err) {
277 qerror_report_err(err);
278 error_free(err);
279 memory_region_init_ram(mr, owner, name, ram_size, &error_abort);
281 #else
282 fprintf(stderr, "-mem-path not supported on this host\n");
283 exit(1);
284 #endif
285 } else {
286 memory_region_init_ram(mr, owner, name, ram_size, &error_abort);
288 vmstate_register_ram_global(mr);
291 void memory_region_allocate_system_memory(MemoryRegion *mr, Object *owner,
292 const char *name,
293 uint64_t ram_size)
295 uint64_t addr = 0;
296 int i;
298 if (nb_numa_nodes == 0 || !have_memdevs) {
299 allocate_system_memory_nonnuma(mr, owner, name, ram_size);
300 return;
303 memory_region_init(mr, owner, name, ram_size);
304 for (i = 0; i < MAX_NODES; i++) {
305 Error *local_err = NULL;
306 uint64_t size = numa_info[i].node_mem;
307 HostMemoryBackend *backend = numa_info[i].node_memdev;
308 if (!backend) {
309 continue;
311 MemoryRegion *seg = host_memory_backend_get_memory(backend, &local_err);
312 if (local_err) {
313 qerror_report_err(local_err);
314 exit(1);
317 if (memory_region_is_mapped(seg)) {
318 char *path = object_get_canonical_path_component(OBJECT(backend));
319 error_report("memory backend %s is used multiple times. Each "
320 "-numa option must use a different memdev value.",
321 path);
322 exit(1);
325 memory_region_add_subregion(mr, addr, seg);
326 vmstate_register_ram_global(seg);
327 addr += size;
331 static void numa_stat_memory_devices(uint64_t node_mem[])
333 MemoryDeviceInfoList *info_list = NULL;
334 MemoryDeviceInfoList **prev = &info_list;
335 MemoryDeviceInfoList *info;
337 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
338 for (info = info_list; info; info = info->next) {
339 MemoryDeviceInfo *value = info->value;
341 if (value) {
342 switch (value->kind) {
343 case MEMORY_DEVICE_INFO_KIND_DIMM:
344 node_mem[value->dimm->node] += value->dimm->size;
345 break;
346 default:
347 break;
351 qapi_free_MemoryDeviceInfoList(info_list);
354 void query_numa_node_mem(uint64_t node_mem[])
356 int i;
358 if (nb_numa_nodes <= 0) {
359 return;
362 numa_stat_memory_devices(node_mem);
363 for (i = 0; i < nb_numa_nodes; i++) {
364 node_mem[i] += numa_info[i].node_mem;
368 static int query_memdev(Object *obj, void *opaque)
370 MemdevList **list = opaque;
371 MemdevList *m = NULL;
372 Error *err = NULL;
374 if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
375 m = g_malloc0(sizeof(*m));
377 m->value = g_malloc0(sizeof(*m->value));
379 m->value->size = object_property_get_int(obj, "size",
380 &err);
381 if (err) {
382 goto error;
385 m->value->merge = object_property_get_bool(obj, "merge",
386 &err);
387 if (err) {
388 goto error;
391 m->value->dump = object_property_get_bool(obj, "dump",
392 &err);
393 if (err) {
394 goto error;
397 m->value->prealloc = object_property_get_bool(obj,
398 "prealloc", &err);
399 if (err) {
400 goto error;
403 m->value->policy = object_property_get_enum(obj,
404 "policy",
405 HostMemPolicy_lookup,
406 &err);
407 if (err) {
408 goto error;
411 object_property_get_uint16List(obj, "host-nodes",
412 &m->value->host_nodes, &err);
413 if (err) {
414 goto error;
417 m->next = *list;
418 *list = m;
421 return 0;
422 error:
423 g_free(m->value);
424 g_free(m);
426 return -1;
429 MemdevList *qmp_query_memdev(Error **errp)
431 Object *obj;
432 MemdevList *list = NULL;
434 obj = object_resolve_path("/objects", NULL);
435 if (obj == NULL) {
436 return NULL;
439 if (object_child_foreach(obj, query_memdev, &list) != 0) {
440 goto error;
443 return list;
445 error:
446 qapi_free_MemdevList(list);
447 return NULL;