blob: 3c8005913f2aa678bf2e05a2554e350afa4390c2 [file] [log] [blame]
Wanlong Gao96d0e262014-05-14 17:43:05 +08001/*
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
Eduardo Habkoste35704b2015-02-08 16:51:16 -020025#include "sysemu/numa.h"
Wanlong Gao96d0e262014-05-14 17:43:05 +080026#include "exec/cpu-common.h"
27#include "qemu/bitmap.h"
28#include "qom/cpu.h"
Wanlong Gao2b631ec2014-05-14 17:43:06 +080029#include "qemu/error-report.h"
30#include "include/exec/cpu-common.h" /* for RAM_ADDR_FMT */
Wanlong Gao00421092014-05-14 17:43:08 +080031#include "qapi-visit.h"
32#include "qapi/opts-visitor.h"
33#include "qapi/dealloc-visitor.h"
Paolo Bonzinidfabb8b2014-05-14 17:43:15 +080034#include "hw/boards.h"
Paolo Bonzini7febe362014-05-14 17:43:17 +080035#include "sysemu/hostmem.h"
Hu Tao76b5d852014-06-16 18:05:41 +080036#include "qmp-commands.h"
zhanghailiang5b009e42014-11-04 19:49:30 +080037#include "hw/mem/pc-dimm.h"
Eduardo Habkost7dcd1d72015-02-08 16:51:20 -020038#include "qemu/option.h"
39#include "qemu/config-file.h"
Wanlong Gao96d0e262014-05-14 17:43:05 +080040
Wanlong Gao00421092014-05-14 17:43:08 +080041QemuOptsList 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
Paolo Bonzini7febe362014-05-14 17:43:17 +080048static int have_memdevs = -1;
Eduardo Habkost25712ff2015-02-08 16:51:19 -020049static int max_numa_nodeid; /* Highest specified NUMA node ID, plus one.
50 * For all nodes, nodeid < max_numa_nodeid
51 */
Eduardo Habkostde1a7c82015-02-08 16:51:18 -020052int nb_numa_nodes;
Eduardo Habkostde1a7c82015-02-08 16:51:18 -020053NodeInfo numa_info[MAX_NODES];
Paolo Bonzini7febe362014-05-14 17:43:17 +080054
Bharata B Raofa9ea812015-06-29 13:50:25 +053055void numa_set_mem_node_id(ram_addr_t addr, uint64_t size, uint32_t node)
56{
57 struct numa_addr_range *range = g_malloc0(sizeof(*range));
58
Bharata B Raoabafabd2015-06-29 13:50:26 +053059 /*
60 * Memory-less nodes can come here with 0 size in which case,
61 * there is nothing to do.
62 */
63 if (!size) {
64 return;
65 }
66
Bharata B Raofa9ea812015-06-29 13:50:25 +053067 range->mem_start = addr;
68 range->mem_end = addr + size - 1;
69 QLIST_INSERT_HEAD(&numa_info[node].addr, range, entry);
70}
71
72void numa_unset_mem_node_id(ram_addr_t addr, uint64_t size, uint32_t node)
73{
74 struct numa_addr_range *range, *next;
75
76 QLIST_FOREACH_SAFE(range, &numa_info[node].addr, entry, next) {
77 if (addr == range->mem_start && (addr + size - 1) == range->mem_end) {
78 QLIST_REMOVE(range, entry);
79 g_free(range);
80 return;
81 }
82 }
83}
84
Bharata B Raoabafabd2015-06-29 13:50:26 +053085static void numa_set_mem_ranges(void)
86{
87 int i;
88 ram_addr_t mem_start = 0;
89
90 /*
91 * Deduce start address of each node and use it to store
92 * the address range info in numa_info address range list
93 */
94 for (i = 0; i < nb_numa_nodes; i++) {
95 numa_set_mem_node_id(mem_start, numa_info[i].node_mem, i);
96 mem_start += numa_info[i].node_mem;
97 }
98}
99
Bharata B Raoe75e2a12015-06-29 13:50:27 +0530100/*
101 * Check if @addr falls under NUMA @node.
102 */
103static bool numa_addr_belongs_to_node(ram_addr_t addr, uint32_t node)
104{
105 struct numa_addr_range *range;
106
107 QLIST_FOREACH(range, &numa_info[node].addr, entry) {
108 if (addr >= range->mem_start && addr <= range->mem_end) {
109 return true;
110 }
111 }
112 return false;
113}
114
115/*
116 * Given an address, return the index of the NUMA node to which the
117 * address belongs to.
118 */
119uint32_t numa_get_node(ram_addr_t addr, Error **errp)
120{
121 uint32_t i;
122
123 /* For non NUMA configurations, check if the addr falls under node 0 */
124 if (!nb_numa_nodes) {
125 if (numa_addr_belongs_to_node(addr, 0)) {
126 return 0;
127 }
128 }
129
130 for (i = 0; i < nb_numa_nodes; i++) {
131 if (numa_addr_belongs_to_node(addr, i)) {
132 return i;
133 }
134 }
135
136 error_setg(errp, "Address 0x" RAM_ADDR_FMT " doesn't belong to any "
137 "NUMA node", addr);
138 return -1;
139}
140
Wanlong Gao00421092014-05-14 17:43:08 +0800141static void numa_node_parse(NumaNodeOptions *node, QemuOpts *opts, Error **errp)
Wanlong Gao96d0e262014-05-14 17:43:05 +0800142{
Wanlong Gao00421092014-05-14 17:43:08 +0800143 uint16_t nodenr;
144 uint16List *cpus = NULL;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800145
Wanlong Gao00421092014-05-14 17:43:08 +0800146 if (node->has_nodeid) {
147 nodenr = node->nodeid;
148 } else {
149 nodenr = nb_numa_nodes;
150 }
151
152 if (nodenr >= MAX_NODES) {
153 error_setg(errp, "Max number of NUMA nodes reached: %"
Gonglei01bbbcf2015-02-25 12:22:30 +0800154 PRIu16 "", nodenr);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800155 return;
156 }
157
Eduardo Habkost1945b9d2014-06-26 18:33:19 -0300158 if (numa_info[nodenr].present) {
159 error_setg(errp, "Duplicate NUMA nodeid: %" PRIu16, nodenr);
160 return;
161 }
162
Wanlong Gao00421092014-05-14 17:43:08 +0800163 for (cpus = node->cpus; cpus; cpus = cpus->next) {
Eduardo Habkost8979c942015-02-09 17:28:52 -0200164 if (cpus->value >= max_cpus) {
165 error_setg(errp,
166 "CPU index (%" PRIu16 ")"
167 " should be smaller than maxcpus (%d)",
168 cpus->value, max_cpus);
Wanlong Gao00421092014-05-14 17:43:08 +0800169 return;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800170 }
Wanlong Gao00421092014-05-14 17:43:08 +0800171 bitmap_set(numa_info[nodenr].node_cpu, cpus->value, 1);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800172 }
173
Paolo Bonzini7febe362014-05-14 17:43:17 +0800174 if (node->has_mem && node->has_memdev) {
Gonglei01bbbcf2015-02-25 12:22:30 +0800175 error_setg(errp, "qemu: cannot specify both mem= and memdev=");
Paolo Bonzini7febe362014-05-14 17:43:17 +0800176 return;
177 }
178
179 if (have_memdevs == -1) {
180 have_memdevs = node->has_memdev;
181 }
182 if (node->has_memdev != have_memdevs) {
183 error_setg(errp, "qemu: memdev option must be specified for either "
Gonglei01bbbcf2015-02-25 12:22:30 +0800184 "all or no nodes");
Paolo Bonzini7febe362014-05-14 17:43:17 +0800185 return;
186 }
187
Wanlong Gao00421092014-05-14 17:43:08 +0800188 if (node->has_mem) {
189 uint64_t mem_size = node->mem;
190 const char *mem_str = qemu_opt_get(opts, "mem");
191 /* Fix up legacy suffix-less format */
192 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
193 mem_size <<= 20;
194 }
195 numa_info[nodenr].node_mem = mem_size;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800196 }
Paolo Bonzini7febe362014-05-14 17:43:17 +0800197 if (node->has_memdev) {
198 Object *o;
199 o = object_resolve_path_type(node->memdev, TYPE_MEMORY_BACKEND, NULL);
200 if (!o) {
201 error_setg(errp, "memdev=%s is ambiguous", node->memdev);
202 return;
203 }
204
205 object_ref(o);
206 numa_info[nodenr].node_mem = object_property_get_int(o, "size", NULL);
207 numa_info[nodenr].node_memdev = MEMORY_BACKEND(o);
208 }
Eduardo Habkost1af878e2014-06-26 18:33:18 -0300209 numa_info[nodenr].present = true;
210 max_numa_nodeid = MAX(max_numa_nodeid, nodenr + 1);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800211}
212
Markus Armbruster28d0de72015-03-13 13:35:14 +0100213static int parse_numa(void *opaque, QemuOpts *opts, Error **errp)
Wanlong Gao96d0e262014-05-14 17:43:05 +0800214{
Wanlong Gao00421092014-05-14 17:43:08 +0800215 NumaOptions *object = NULL;
216 Error *err = NULL;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800217
Wanlong Gao00421092014-05-14 17:43:08 +0800218 {
219 OptsVisitor *ov = opts_visitor_new(opts);
220 visit_type_NumaOptions(opts_get_visitor(ov), &object, NULL, &err);
221 opts_visitor_cleanup(ov);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800222 }
Wanlong Gao96d0e262014-05-14 17:43:05 +0800223
Wanlong Gao00421092014-05-14 17:43:08 +0800224 if (err) {
225 goto error;
226 }
Wanlong Gao96d0e262014-05-14 17:43:05 +0800227
Wanlong Gao00421092014-05-14 17:43:08 +0800228 switch (object->kind) {
229 case NUMA_OPTIONS_KIND_NODE:
230 numa_node_parse(object->node, opts, &err);
231 if (err) {
232 goto error;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800233 }
234 nb_numa_nodes++;
Wanlong Gao00421092014-05-14 17:43:08 +0800235 break;
236 default:
237 abort();
Wanlong Gao96d0e262014-05-14 17:43:05 +0800238 }
Wanlong Gao00421092014-05-14 17:43:08 +0800239
240 return 0;
241
242error:
Markus Armbruster29b762f2015-02-10 15:06:23 +0100243 error_report_err(err);
Wanlong Gao00421092014-05-14 17:43:08 +0800244
245 if (object) {
246 QapiDeallocVisitor *dv = qapi_dealloc_visitor_new();
247 visit_type_NumaOptions(qapi_dealloc_get_visitor(dv),
248 &object, NULL, NULL);
249 qapi_dealloc_visitor_cleanup(dv);
250 }
251
252 return -1;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800253}
254
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200255static char *enumerate_cpus(unsigned long *cpus, int max_cpus)
256{
257 int cpu;
258 bool first = true;
259 GString *s = g_string_new(NULL);
260
261 for (cpu = find_first_bit(cpus, max_cpus);
262 cpu < max_cpus;
263 cpu = find_next_bit(cpus, max_cpus, cpu + 1)) {
264 g_string_append_printf(s, "%s%d", first ? "" : " ", cpu);
265 first = false;
266 }
267 return g_string_free(s, FALSE);
268}
269
270static void validate_numa_cpus(void)
271{
272 int i;
273 DECLARE_BITMAP(seen_cpus, MAX_CPUMASK_BITS);
274
275 bitmap_zero(seen_cpus, MAX_CPUMASK_BITS);
276 for (i = 0; i < nb_numa_nodes; i++) {
277 if (bitmap_intersects(seen_cpus, numa_info[i].node_cpu,
278 MAX_CPUMASK_BITS)) {
279 bitmap_and(seen_cpus, seen_cpus,
280 numa_info[i].node_cpu, MAX_CPUMASK_BITS);
281 error_report("CPU(s) present in multiple NUMA nodes: %s",
282 enumerate_cpus(seen_cpus, max_cpus));;
283 exit(EXIT_FAILURE);
284 }
285 bitmap_or(seen_cpus, seen_cpus,
286 numa_info[i].node_cpu, MAX_CPUMASK_BITS);
287 }
Eduardo Habkost549fc542015-02-09 17:35:04 -0200288
289 if (!bitmap_full(seen_cpus, max_cpus)) {
290 char *msg;
291 bitmap_complement(seen_cpus, seen_cpus, max_cpus);
292 msg = enumerate_cpus(seen_cpus, max_cpus);
293 error_report("warning: CPU(s) not present in any NUMA nodes: %s", msg);
294 error_report("warning: All CPU(s) up to maxcpus should be described "
295 "in NUMA config");
296 g_free(msg);
297 }
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200298}
299
Igor Mammedov57924bc2015-03-19 17:09:21 +0000300void parse_numa_opts(MachineClass *mc)
Wanlong Gao96d0e262014-05-14 17:43:05 +0800301{
Eduardo Habkost12d6e462014-06-26 18:33:20 -0300302 int i;
303
Markus Armbruster28d0de72015-03-13 13:35:14 +0100304 if (qemu_opts_foreach(qemu_find_opts("numa"), parse_numa, NULL, NULL)) {
Eduardo Habkost7dcd1d72015-02-08 16:51:20 -0200305 exit(1);
306 }
307
Eduardo Habkost12d6e462014-06-26 18:33:20 -0300308 assert(max_numa_nodeid <= MAX_NODES);
309
310 /* No support for sparse NUMA node IDs yet: */
311 for (i = max_numa_nodeid - 1; i >= 0; i--) {
312 /* Report large node IDs first, to make mistakes easier to spot */
313 if (!numa_info[i].present) {
314 error_report("numa: Node ID missing: %d", i);
315 exit(1);
316 }
317 }
318
319 /* This must be always true if all nodes are present: */
320 assert(nb_numa_nodes == max_numa_nodeid);
321
Wanlong Gao96d0e262014-05-14 17:43:05 +0800322 if (nb_numa_nodes > 0) {
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800323 uint64_t numa_total;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800324
325 if (nb_numa_nodes > MAX_NODES) {
326 nb_numa_nodes = MAX_NODES;
327 }
328
Michael S. Tsirkin9851d0f2014-06-24 08:50:30 +0300329 /* If no memory size is given for any node, assume the default case
Wanlong Gao96d0e262014-05-14 17:43:05 +0800330 * and distribute the available memory equally across all nodes
331 */
332 for (i = 0; i < nb_numa_nodes; i++) {
Wanlong Gao8c859012014-05-14 17:43:07 +0800333 if (numa_info[i].node_mem != 0) {
Wanlong Gao96d0e262014-05-14 17:43:05 +0800334 break;
335 }
336 }
337 if (i == nb_numa_nodes) {
338 uint64_t usedmem = 0;
339
Michael S. Tsirkind75e2f62014-06-24 07:43:37 +0300340 /* On Linux, each node's border has to be 8MB aligned,
Wanlong Gao96d0e262014-05-14 17:43:05 +0800341 * the final node gets the rest.
342 */
343 for (i = 0; i < nb_numa_nodes - 1; i++) {
Wanlong Gao8c859012014-05-14 17:43:07 +0800344 numa_info[i].node_mem = (ram_size / nb_numa_nodes) &
345 ~((1 << 23UL) - 1);
346 usedmem += numa_info[i].node_mem;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800347 }
Wanlong Gao8c859012014-05-14 17:43:07 +0800348 numa_info[i].node_mem = ram_size - usedmem;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800349 }
350
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800351 numa_total = 0;
352 for (i = 0; i < nb_numa_nodes; i++) {
Wanlong Gao8c859012014-05-14 17:43:07 +0800353 numa_total += numa_info[i].node_mem;
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800354 }
355 if (numa_total != ram_size) {
Hu Taoc68233a2014-08-04 16:16:09 +0800356 error_report("total memory for NUMA nodes (0x%" PRIx64 ")"
357 " should equal RAM size (0x" RAM_ADDR_FMT ")",
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800358 numa_total, ram_size);
359 exit(1);
360 }
361
Wanlong Gao96d0e262014-05-14 17:43:05 +0800362 for (i = 0; i < nb_numa_nodes; i++) {
Bharata B Raofa9ea812015-06-29 13:50:25 +0530363 QLIST_INIT(&numa_info[i].addr);
364 }
365
Bharata B Raoabafabd2015-06-29 13:50:26 +0530366 numa_set_mem_ranges();
367
Bharata B Raofa9ea812015-06-29 13:50:25 +0530368 for (i = 0; i < nb_numa_nodes; i++) {
Wanlong Gao8c859012014-05-14 17:43:07 +0800369 if (!bitmap_empty(numa_info[i].node_cpu, MAX_CPUMASK_BITS)) {
Wanlong Gao96d0e262014-05-14 17:43:05 +0800370 break;
371 }
372 }
Igor Mammedov57924bc2015-03-19 17:09:21 +0000373 /* Historically VCPUs were assigned in round-robin order to NUMA
374 * nodes. However it causes issues with guest not handling it nice
375 * in case where cores/threads from a multicore CPU appear on
376 * different nodes. So allow boards to override default distribution
377 * rule grouping VCPUs by socket so that VCPUs from the same socket
378 * would be on the same node.
Wanlong Gao96d0e262014-05-14 17:43:05 +0800379 */
380 if (i == nb_numa_nodes) {
381 for (i = 0; i < max_cpus; i++) {
Igor Mammedov57924bc2015-03-19 17:09:21 +0000382 unsigned node_id = i % nb_numa_nodes;
383 if (mc->cpu_index_to_socket_id) {
384 node_id = mc->cpu_index_to_socket_id(i) % nb_numa_nodes;
385 }
386
387 set_bit(i, numa_info[node_id].node_cpu);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800388 }
389 }
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200390
391 validate_numa_cpus();
Bharata B Raoabafabd2015-06-29 13:50:26 +0530392 } else {
393 numa_set_mem_node_id(0, ram_size, 0);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800394 }
395}
396
Eduardo Habkostdde11112015-02-08 16:51:22 -0200397void numa_post_machine_init(void)
Wanlong Gao96d0e262014-05-14 17:43:05 +0800398{
399 CPUState *cpu;
400 int i;
401
402 CPU_FOREACH(cpu) {
403 for (i = 0; i < nb_numa_nodes; i++) {
Wanlong Gao8c859012014-05-14 17:43:07 +0800404 if (test_bit(cpu->cpu_index, numa_info[i].node_cpu)) {
Wanlong Gao96d0e262014-05-14 17:43:05 +0800405 cpu->numa_node = i;
406 }
407 }
408 }
409}
Paolo Bonzinidfabb8b2014-05-14 17:43:15 +0800410
Paolo Bonzini7febe362014-05-14 17:43:17 +0800411static void allocate_system_memory_nonnuma(MemoryRegion *mr, Object *owner,
412 const char *name,
413 uint64_t ram_size)
414{
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800415 if (mem_path) {
416#ifdef __linux__
Paolo Bonzini7f56e742014-05-14 17:43:20 +0800417 Error *err = NULL;
Paolo Bonzinidbcb8982014-06-10 19:15:24 +0800418 memory_region_init_ram_from_file(mr, owner, name, ram_size, false,
Paolo Bonzini7f56e742014-05-14 17:43:20 +0800419 mem_path, &err);
420
421 /* Legacy behavior: if allocation failed, fall back to
422 * regular RAM allocation.
423 */
Igor Mammedovc3ba3092014-06-17 12:17:05 +0200424 if (err) {
Markus Armbruster29b762f2015-02-10 15:06:23 +0100425 error_report_err(err);
Hu Tao49946532014-09-09 13:27:55 +0800426 memory_region_init_ram(mr, owner, name, ram_size, &error_abort);
Paolo Bonzini7f56e742014-05-14 17:43:20 +0800427 }
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800428#else
429 fprintf(stderr, "-mem-path not supported on this host\n");
430 exit(1);
431#endif
432 } else {
Hu Tao49946532014-09-09 13:27:55 +0800433 memory_region_init_ram(mr, owner, name, ram_size, &error_abort);
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800434 }
Paolo Bonzini7febe362014-05-14 17:43:17 +0800435 vmstate_register_ram_global(mr);
436}
437
Paolo Bonzinidfabb8b2014-05-14 17:43:15 +0800438void memory_region_allocate_system_memory(MemoryRegion *mr, Object *owner,
439 const char *name,
440 uint64_t ram_size)
441{
Paolo Bonzini7febe362014-05-14 17:43:17 +0800442 uint64_t addr = 0;
443 int i;
444
445 if (nb_numa_nodes == 0 || !have_memdevs) {
446 allocate_system_memory_nonnuma(mr, owner, name, ram_size);
447 return;
448 }
449
450 memory_region_init(mr, owner, name, ram_size);
451 for (i = 0; i < MAX_NODES; i++) {
452 Error *local_err = NULL;
453 uint64_t size = numa_info[i].node_mem;
454 HostMemoryBackend *backend = numa_info[i].node_memdev;
455 if (!backend) {
456 continue;
457 }
458 MemoryRegion *seg = host_memory_backend_get_memory(backend, &local_err);
459 if (local_err) {
Markus Armbruster29b762f2015-02-10 15:06:23 +0100460 error_report_err(local_err);
Paolo Bonzini7febe362014-05-14 17:43:17 +0800461 exit(1);
462 }
463
Hu Tao0462fae2014-06-30 18:28:15 +0800464 if (memory_region_is_mapped(seg)) {
465 char *path = object_get_canonical_path_component(OBJECT(backend));
466 error_report("memory backend %s is used multiple times. Each "
467 "-numa option must use a different memdev value.",
468 path);
469 exit(1);
470 }
471
Paolo Bonzini7febe362014-05-14 17:43:17 +0800472 memory_region_add_subregion(mr, addr, seg);
473 vmstate_register_ram_global(seg);
474 addr += size;
475 }
Paolo Bonzinidfabb8b2014-05-14 17:43:15 +0800476}
Hu Tao76b5d852014-06-16 18:05:41 +0800477
zhanghailiang5b009e42014-11-04 19:49:30 +0800478static void numa_stat_memory_devices(uint64_t node_mem[])
479{
480 MemoryDeviceInfoList *info_list = NULL;
481 MemoryDeviceInfoList **prev = &info_list;
482 MemoryDeviceInfoList *info;
483
484 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
485 for (info = info_list; info; info = info->next) {
486 MemoryDeviceInfo *value = info->value;
487
488 if (value) {
489 switch (value->kind) {
490 case MEMORY_DEVICE_INFO_KIND_DIMM:
491 node_mem[value->dimm->node] += value->dimm->size;
492 break;
493 default:
494 break;
495 }
496 }
497 }
498 qapi_free_MemoryDeviceInfoList(info_list);
499}
500
501void query_numa_node_mem(uint64_t node_mem[])
502{
503 int i;
504
505 if (nb_numa_nodes <= 0) {
506 return;
507 }
508
509 numa_stat_memory_devices(node_mem);
510 for (i = 0; i < nb_numa_nodes; i++) {
511 node_mem[i] += numa_info[i].node_mem;
512 }
513}
514
Hu Tao76b5d852014-06-16 18:05:41 +0800515static int query_memdev(Object *obj, void *opaque)
516{
517 MemdevList **list = opaque;
Chen Fanb0e90182014-08-18 14:46:33 +0800518 MemdevList *m = NULL;
Hu Tao76b5d852014-06-16 18:05:41 +0800519 Error *err = NULL;
520
521 if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
Chen Fanb0e90182014-08-18 14:46:33 +0800522 m = g_malloc0(sizeof(*m));
Hu Tao76b5d852014-06-16 18:05:41 +0800523
524 m->value = g_malloc0(sizeof(*m->value));
525
526 m->value->size = object_property_get_int(obj, "size",
527 &err);
528 if (err) {
529 goto error;
530 }
531
532 m->value->merge = object_property_get_bool(obj, "merge",
533 &err);
534 if (err) {
535 goto error;
536 }
537
538 m->value->dump = object_property_get_bool(obj, "dump",
539 &err);
540 if (err) {
541 goto error;
542 }
543
544 m->value->prealloc = object_property_get_bool(obj,
545 "prealloc", &err);
546 if (err) {
547 goto error;
548 }
549
550 m->value->policy = object_property_get_enum(obj,
551 "policy",
Daniel P. Berrangea3590da2015-05-27 16:07:56 +0100552 "HostMemPolicy",
Hu Tao76b5d852014-06-16 18:05:41 +0800553 &err);
554 if (err) {
555 goto error;
556 }
557
558 object_property_get_uint16List(obj, "host-nodes",
559 &m->value->host_nodes, &err);
560 if (err) {
561 goto error;
562 }
563
564 m->next = *list;
565 *list = m;
566 }
567
568 return 0;
569error:
Chen Fanb0e90182014-08-18 14:46:33 +0800570 g_free(m->value);
571 g_free(m);
572
Hu Tao76b5d852014-06-16 18:05:41 +0800573 return -1;
574}
575
576MemdevList *qmp_query_memdev(Error **errp)
577{
578 Object *obj;
Chen Fanecaf54a2014-08-18 14:46:35 +0800579 MemdevList *list = NULL;
Hu Tao76b5d852014-06-16 18:05:41 +0800580
Daniel P. Berrangebc2256c2015-05-13 17:14:05 +0100581 obj = object_get_objects_root();
Hu Tao76b5d852014-06-16 18:05:41 +0800582 if (obj == NULL) {
583 return NULL;
584 }
585
586 if (object_child_foreach(obj, query_memdev, &list) != 0) {
587 goto error;
588 }
589
590 return list;
591
592error:
Chen Fanecaf54a2014-08-18 14:46:35 +0800593 qapi_free_MemdevList(list);
Hu Tao76b5d852014-06-16 18:05:41 +0800594 return NULL;
595}