]> Git Repo - qemu.git/blame - numa.c
include: import virtio headers from linux 4.0
[qemu.git] / numa.c
CommitLineData
96d0e26c
WG
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/sysemu.h"
26#include "exec/cpu-common.h"
27#include "qemu/bitmap.h"
28#include "qom/cpu.h"
2b631ec2
WG
29#include "qemu/error-report.h"
30#include "include/exec/cpu-common.h" /* for RAM_ADDR_FMT */
0042109a
WG
31#include "qapi-visit.h"
32#include "qapi/opts-visitor.h"
33#include "qapi/dealloc-visitor.h"
34#include "qapi/qmp/qerror.h"
dfabb8b9 35#include "hw/boards.h"
7febe36f 36#include "sysemu/hostmem.h"
76b5d850 37#include "qmp-commands.h"
5b009e40 38#include "hw/mem/pc-dimm.h"
0042109a
WG
39
40QemuOptsList qemu_numa_opts = {
41 .name = "numa",
42 .implied_opt_name = "type",
43 .head = QTAILQ_HEAD_INITIALIZER(qemu_numa_opts.head),
44 .desc = { { 0 } } /* validated with OptsVisitor */
45};
46
7febe36f
PB
47static int have_memdevs = -1;
48
0042109a 49static void numa_node_parse(NumaNodeOptions *node, QemuOpts *opts, Error **errp)
96d0e26c 50{
0042109a
WG
51 uint16_t nodenr;
52 uint16List *cpus = NULL;
96d0e26c 53
0042109a
WG
54 if (node->has_nodeid) {
55 nodenr = node->nodeid;
96d0e26c 56 } else {
0042109a 57 nodenr = nb_numa_nodes;
96d0e26c
WG
58 }
59
0042109a
WG
60 if (nodenr >= MAX_NODES) {
61 error_setg(errp, "Max number of NUMA nodes reached: %"
62 PRIu16 "\n", nodenr);
63 return;
96d0e26c
WG
64 }
65
1945b9d8
EH
66 if (numa_info[nodenr].present) {
67 error_setg(errp, "Duplicate NUMA nodeid: %" PRIu16, nodenr);
68 return;
69 }
70
0042109a
WG
71 for (cpus = node->cpus; cpus; cpus = cpus->next) {
72 if (cpus->value > MAX_CPUMASK_BITS) {
73 error_setg(errp, "CPU number %" PRIu16 " is bigger than %d",
74 cpus->value, MAX_CPUMASK_BITS);
75 return;
76 }
77 bitmap_set(numa_info[nodenr].node_cpu, cpus->value, 1);
96d0e26c
WG
78 }
79
7febe36f
PB
80 if (node->has_mem && node->has_memdev) {
81 error_setg(errp, "qemu: cannot specify both mem= and memdev=\n");
82 return;
83 }
84
85 if (have_memdevs == -1) {
86 have_memdevs = node->has_memdev;
87 }
88 if (node->has_memdev != have_memdevs) {
89 error_setg(errp, "qemu: memdev option must be specified for either "
90 "all or no nodes\n");
91 return;
92 }
93
0042109a
WG
94 if (node->has_mem) {
95 uint64_t mem_size = node->mem;
96 const char *mem_str = qemu_opt_get(opts, "mem");
97 /* Fix up legacy suffix-less format */
98 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
99 mem_size <<= 20;
100 }
101 numa_info[nodenr].node_mem = mem_size;
102 }
7febe36f
PB
103 if (node->has_memdev) {
104 Object *o;
105 o = object_resolve_path_type(node->memdev, TYPE_MEMORY_BACKEND, NULL);
106 if (!o) {
107 error_setg(errp, "memdev=%s is ambiguous", node->memdev);
108 return;
109 }
110
111 object_ref(o);
112 numa_info[nodenr].node_mem = object_property_get_int(o, "size", NULL);
113 numa_info[nodenr].node_memdev = MEMORY_BACKEND(o);
114 }
1af878e0
EH
115 numa_info[nodenr].present = true;
116 max_numa_nodeid = MAX(max_numa_nodeid, nodenr + 1);
96d0e26c
WG
117}
118
0042109a 119int numa_init_func(QemuOpts *opts, void *opaque)
96d0e26c 120{
0042109a
WG
121 NumaOptions *object = NULL;
122 Error *err = NULL;
96d0e26c 123
0042109a
WG
124 {
125 OptsVisitor *ov = opts_visitor_new(opts);
126 visit_type_NumaOptions(opts_get_visitor(ov), &object, NULL, &err);
127 opts_visitor_cleanup(ov);
96d0e26c 128 }
96d0e26c 129
0042109a
WG
130 if (err) {
131 goto error;
132 }
96d0e26c 133
0042109a
WG
134 switch (object->kind) {
135 case NUMA_OPTIONS_KIND_NODE:
136 numa_node_parse(object->node, opts, &err);
137 if (err) {
138 goto error;
96d0e26c 139 }
0042109a
WG
140 nb_numa_nodes++;
141 break;
142 default:
143 abort();
144 }
96d0e26c 145
0042109a 146 return 0;
96d0e26c 147
0042109a 148error:
29b762f5 149 error_report_err(err);
0042109a
WG
150
151 if (object) {
152 QapiDeallocVisitor *dv = qapi_dealloc_visitor_new();
153 visit_type_NumaOptions(qapi_dealloc_get_visitor(dv),
154 &object, NULL, NULL);
155 qapi_dealloc_visitor_cleanup(dv);
96d0e26c 156 }
0042109a
WG
157
158 return -1;
96d0e26c
WG
159}
160
161void set_numa_nodes(void)
162{
12d6e464
EH
163 int i;
164
165 assert(max_numa_nodeid <= MAX_NODES);
166
167 /* No support for sparse NUMA node IDs yet: */
168 for (i = max_numa_nodeid - 1; i >= 0; i--) {
169 /* Report large node IDs first, to make mistakes easier to spot */
170 if (!numa_info[i].present) {
171 error_report("numa: Node ID missing: %d", i);
172 exit(1);
173 }
174 }
175
176 /* This must be always true if all nodes are present: */
177 assert(nb_numa_nodes == max_numa_nodeid);
178
96d0e26c 179 if (nb_numa_nodes > 0) {
2b631ec2 180 uint64_t numa_total;
96d0e26c
WG
181
182 if (nb_numa_nodes > MAX_NODES) {
183 nb_numa_nodes = MAX_NODES;
184 }
185
9851d0fe 186 /* If no memory size is given for any node, assume the default case
96d0e26c
WG
187 * and distribute the available memory equally across all nodes
188 */
189 for (i = 0; i < nb_numa_nodes; i++) {
8c85901e 190 if (numa_info[i].node_mem != 0) {
96d0e26c
WG
191 break;
192 }
193 }
194 if (i == nb_numa_nodes) {
195 uint64_t usedmem = 0;
196
d75e2f68 197 /* On Linux, each node's border has to be 8MB aligned,
96d0e26c
WG
198 * the final node gets the rest.
199 */
200 for (i = 0; i < nb_numa_nodes - 1; i++) {
8c85901e
WG
201 numa_info[i].node_mem = (ram_size / nb_numa_nodes) &
202 ~((1 << 23UL) - 1);
203 usedmem += numa_info[i].node_mem;
96d0e26c 204 }
8c85901e 205 numa_info[i].node_mem = ram_size - usedmem;
96d0e26c
WG
206 }
207
2b631ec2
WG
208 numa_total = 0;
209 for (i = 0; i < nb_numa_nodes; i++) {
8c85901e 210 numa_total += numa_info[i].node_mem;
2b631ec2
WG
211 }
212 if (numa_total != ram_size) {
c68233ae
HT
213 error_report("total memory for NUMA nodes (0x%" PRIx64 ")"
214 " should equal RAM size (0x" RAM_ADDR_FMT ")",
2b631ec2
WG
215 numa_total, ram_size);
216 exit(1);
217 }
218
96d0e26c 219 for (i = 0; i < nb_numa_nodes; i++) {
8c85901e 220 if (!bitmap_empty(numa_info[i].node_cpu, MAX_CPUMASK_BITS)) {
96d0e26c
WG
221 break;
222 }
223 }
224 /* assigning the VCPUs round-robin is easier to implement, guest OSes
225 * must cope with this anyway, because there are BIOSes out there in
226 * real machines which also use this scheme.
227 */
228 if (i == nb_numa_nodes) {
229 for (i = 0; i < max_cpus; i++) {
8c85901e 230 set_bit(i, numa_info[i % nb_numa_nodes].node_cpu);
96d0e26c
WG
231 }
232 }
233 }
234}
235
236void set_numa_modes(void)
237{
238 CPUState *cpu;
239 int i;
240
241 CPU_FOREACH(cpu) {
242 for (i = 0; i < nb_numa_nodes; i++) {
8c85901e 243 if (test_bit(cpu->cpu_index, numa_info[i].node_cpu)) {
96d0e26c
WG
244 cpu->numa_node = i;
245 }
246 }
247 }
248}
dfabb8b9 249
7febe36f
PB
250static void allocate_system_memory_nonnuma(MemoryRegion *mr, Object *owner,
251 const char *name,
252 uint64_t ram_size)
253{
0b183fc8
PB
254 if (mem_path) {
255#ifdef __linux__
7f56e740 256 Error *err = NULL;
dbcb8981 257 memory_region_init_ram_from_file(mr, owner, name, ram_size, false,
7f56e740
PB
258 mem_path, &err);
259
260 /* Legacy behavior: if allocation failed, fall back to
261 * regular RAM allocation.
262 */
c3ba3095 263 if (err) {
29b762f5 264 error_report_err(err);
49946538 265 memory_region_init_ram(mr, owner, name, ram_size, &error_abort);
7f56e740 266 }
0b183fc8
PB
267#else
268 fprintf(stderr, "-mem-path not supported on this host\n");
269 exit(1);
270#endif
271 } else {
49946538 272 memory_region_init_ram(mr, owner, name, ram_size, &error_abort);
0b183fc8 273 }
7febe36f
PB
274 vmstate_register_ram_global(mr);
275}
276
dfabb8b9
PB
277void memory_region_allocate_system_memory(MemoryRegion *mr, Object *owner,
278 const char *name,
279 uint64_t ram_size)
280{
7febe36f
PB
281 uint64_t addr = 0;
282 int i;
283
284 if (nb_numa_nodes == 0 || !have_memdevs) {
285 allocate_system_memory_nonnuma(mr, owner, name, ram_size);
286 return;
287 }
288
289 memory_region_init(mr, owner, name, ram_size);
290 for (i = 0; i < MAX_NODES; i++) {
291 Error *local_err = NULL;
292 uint64_t size = numa_info[i].node_mem;
293 HostMemoryBackend *backend = numa_info[i].node_memdev;
294 if (!backend) {
295 continue;
296 }
297 MemoryRegion *seg = host_memory_backend_get_memory(backend, &local_err);
298 if (local_err) {
29b762f5 299 error_report_err(local_err);
7febe36f
PB
300 exit(1);
301 }
302
0462faee
HT
303 if (memory_region_is_mapped(seg)) {
304 char *path = object_get_canonical_path_component(OBJECT(backend));
305 error_report("memory backend %s is used multiple times. Each "
306 "-numa option must use a different memdev value.",
307 path);
308 exit(1);
309 }
310
7febe36f
PB
311 memory_region_add_subregion(mr, addr, seg);
312 vmstate_register_ram_global(seg);
313 addr += size;
314 }
dfabb8b9 315}
76b5d850 316
5b009e40
HZ
317static void numa_stat_memory_devices(uint64_t node_mem[])
318{
319 MemoryDeviceInfoList *info_list = NULL;
320 MemoryDeviceInfoList **prev = &info_list;
321 MemoryDeviceInfoList *info;
322
323 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
324 for (info = info_list; info; info = info->next) {
325 MemoryDeviceInfo *value = info->value;
326
327 if (value) {
328 switch (value->kind) {
329 case MEMORY_DEVICE_INFO_KIND_DIMM:
330 node_mem[value->dimm->node] += value->dimm->size;
331 break;
332 default:
333 break;
334 }
335 }
336 }
337 qapi_free_MemoryDeviceInfoList(info_list);
338}
339
340void query_numa_node_mem(uint64_t node_mem[])
341{
342 int i;
343
344 if (nb_numa_nodes <= 0) {
345 return;
346 }
347
348 numa_stat_memory_devices(node_mem);
349 for (i = 0; i < nb_numa_nodes; i++) {
350 node_mem[i] += numa_info[i].node_mem;
351 }
352}
353
76b5d850
HT
354static int query_memdev(Object *obj, void *opaque)
355{
356 MemdevList **list = opaque;
b0e90181 357 MemdevList *m = NULL;
76b5d850
HT
358 Error *err = NULL;
359
360 if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
b0e90181 361 m = g_malloc0(sizeof(*m));
76b5d850
HT
362
363 m->value = g_malloc0(sizeof(*m->value));
364
365 m->value->size = object_property_get_int(obj, "size",
366 &err);
367 if (err) {
368 goto error;
369 }
370
371 m->value->merge = object_property_get_bool(obj, "merge",
372 &err);
373 if (err) {
374 goto error;
375 }
376
377 m->value->dump = object_property_get_bool(obj, "dump",
378 &err);
379 if (err) {
380 goto error;
381 }
382
383 m->value->prealloc = object_property_get_bool(obj,
384 "prealloc", &err);
385 if (err) {
386 goto error;
387 }
388
389 m->value->policy = object_property_get_enum(obj,
390 "policy",
391 HostMemPolicy_lookup,
392 &err);
393 if (err) {
394 goto error;
395 }
396
397 object_property_get_uint16List(obj, "host-nodes",
398 &m->value->host_nodes, &err);
399 if (err) {
400 goto error;
401 }
402
403 m->next = *list;
404 *list = m;
405 }
406
407 return 0;
408error:
b0e90181
CF
409 g_free(m->value);
410 g_free(m);
411
76b5d850
HT
412 return -1;
413}
414
415MemdevList *qmp_query_memdev(Error **errp)
416{
417 Object *obj;
ecaf54a0 418 MemdevList *list = NULL;
76b5d850
HT
419
420 obj = object_resolve_path("/objects", NULL);
421 if (obj == NULL) {
422 return NULL;
423 }
424
425 if (object_child_foreach(obj, query_memdev, &list) != 0) {
426 goto error;
427 }
428
429 return list;
430
431error:
ecaf54a0 432 qapi_free_MemdevList(list);
76b5d850
HT
433 return NULL;
434}
This page took 0.13813 seconds and 4 git commands to generate.