]> git.proxmox.com Git - mirror_qemu.git/blame - numa.c
numa: Keep track of NUMA nodes present on the command-line
[mirror_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"
0042109a
WG
38
39QemuOptsList qemu_numa_opts = {
40 .name = "numa",
41 .implied_opt_name = "type",
42 .head = QTAILQ_HEAD_INITIALIZER(qemu_numa_opts.head),
43 .desc = { { 0 } } /* validated with OptsVisitor */
44};
45
7febe36f
PB
46static int have_memdevs = -1;
47
0042109a 48static void numa_node_parse(NumaNodeOptions *node, QemuOpts *opts, Error **errp)
96d0e26c 49{
0042109a
WG
50 uint16_t nodenr;
51 uint16List *cpus = NULL;
96d0e26c 52
0042109a
WG
53 if (node->has_nodeid) {
54 nodenr = node->nodeid;
96d0e26c 55 } else {
0042109a 56 nodenr = nb_numa_nodes;
96d0e26c
WG
57 }
58
0042109a
WG
59 if (nodenr >= MAX_NODES) {
60 error_setg(errp, "Max number of NUMA nodes reached: %"
61 PRIu16 "\n", nodenr);
62 return;
96d0e26c
WG
63 }
64
0042109a
WG
65 for (cpus = node->cpus; cpus; cpus = cpus->next) {
66 if (cpus->value > MAX_CPUMASK_BITS) {
67 error_setg(errp, "CPU number %" PRIu16 " is bigger than %d",
68 cpus->value, MAX_CPUMASK_BITS);
69 return;
70 }
71 bitmap_set(numa_info[nodenr].node_cpu, cpus->value, 1);
96d0e26c
WG
72 }
73
7febe36f
PB
74 if (node->has_mem && node->has_memdev) {
75 error_setg(errp, "qemu: cannot specify both mem= and memdev=\n");
76 return;
77 }
78
79 if (have_memdevs == -1) {
80 have_memdevs = node->has_memdev;
81 }
82 if (node->has_memdev != have_memdevs) {
83 error_setg(errp, "qemu: memdev option must be specified for either "
84 "all or no nodes\n");
85 return;
86 }
87
0042109a
WG
88 if (node->has_mem) {
89 uint64_t mem_size = node->mem;
90 const char *mem_str = qemu_opt_get(opts, "mem");
91 /* Fix up legacy suffix-less format */
92 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
93 mem_size <<= 20;
94 }
95 numa_info[nodenr].node_mem = mem_size;
96 }
7febe36f
PB
97 if (node->has_memdev) {
98 Object *o;
99 o = object_resolve_path_type(node->memdev, TYPE_MEMORY_BACKEND, NULL);
100 if (!o) {
101 error_setg(errp, "memdev=%s is ambiguous", node->memdev);
102 return;
103 }
104
105 object_ref(o);
106 numa_info[nodenr].node_mem = object_property_get_int(o, "size", NULL);
107 numa_info[nodenr].node_memdev = MEMORY_BACKEND(o);
108 }
1af878e0
EH
109 numa_info[nodenr].present = true;
110 max_numa_nodeid = MAX(max_numa_nodeid, nodenr + 1);
96d0e26c
WG
111}
112
0042109a 113int numa_init_func(QemuOpts *opts, void *opaque)
96d0e26c 114{
0042109a
WG
115 NumaOptions *object = NULL;
116 Error *err = NULL;
96d0e26c 117
0042109a
WG
118 {
119 OptsVisitor *ov = opts_visitor_new(opts);
120 visit_type_NumaOptions(opts_get_visitor(ov), &object, NULL, &err);
121 opts_visitor_cleanup(ov);
96d0e26c 122 }
96d0e26c 123
0042109a
WG
124 if (err) {
125 goto error;
126 }
96d0e26c 127
0042109a
WG
128 switch (object->kind) {
129 case NUMA_OPTIONS_KIND_NODE:
130 numa_node_parse(object->node, opts, &err);
131 if (err) {
132 goto error;
96d0e26c 133 }
0042109a
WG
134 nb_numa_nodes++;
135 break;
136 default:
137 abort();
138 }
96d0e26c 139
0042109a 140 return 0;
96d0e26c 141
0042109a
WG
142error:
143 qerror_report_err(err);
144 error_free(err);
145
146 if (object) {
147 QapiDeallocVisitor *dv = qapi_dealloc_visitor_new();
148 visit_type_NumaOptions(qapi_dealloc_get_visitor(dv),
149 &object, NULL, NULL);
150 qapi_dealloc_visitor_cleanup(dv);
96d0e26c 151 }
0042109a
WG
152
153 return -1;
96d0e26c
WG
154}
155
156void set_numa_nodes(void)
157{
158 if (nb_numa_nodes > 0) {
2b631ec2 159 uint64_t numa_total;
96d0e26c
WG
160 int i;
161
162 if (nb_numa_nodes > MAX_NODES) {
163 nb_numa_nodes = MAX_NODES;
164 }
165
9851d0fe 166 /* If no memory size is given for any node, assume the default case
96d0e26c
WG
167 * and distribute the available memory equally across all nodes
168 */
169 for (i = 0; i < nb_numa_nodes; i++) {
8c85901e 170 if (numa_info[i].node_mem != 0) {
96d0e26c
WG
171 break;
172 }
173 }
174 if (i == nb_numa_nodes) {
175 uint64_t usedmem = 0;
176
d75e2f68 177 /* On Linux, each node's border has to be 8MB aligned,
96d0e26c
WG
178 * the final node gets the rest.
179 */
180 for (i = 0; i < nb_numa_nodes - 1; i++) {
8c85901e
WG
181 numa_info[i].node_mem = (ram_size / nb_numa_nodes) &
182 ~((1 << 23UL) - 1);
183 usedmem += numa_info[i].node_mem;
96d0e26c 184 }
8c85901e 185 numa_info[i].node_mem = ram_size - usedmem;
96d0e26c
WG
186 }
187
2b631ec2
WG
188 numa_total = 0;
189 for (i = 0; i < nb_numa_nodes; i++) {
8c85901e 190 numa_total += numa_info[i].node_mem;
2b631ec2
WG
191 }
192 if (numa_total != ram_size) {
193 error_report("total memory for NUMA nodes (%" PRIu64 ")"
194 " should equal RAM size (" RAM_ADDR_FMT ")",
195 numa_total, ram_size);
196 exit(1);
197 }
198
96d0e26c 199 for (i = 0; i < nb_numa_nodes; i++) {
8c85901e 200 if (!bitmap_empty(numa_info[i].node_cpu, MAX_CPUMASK_BITS)) {
96d0e26c
WG
201 break;
202 }
203 }
204 /* assigning the VCPUs round-robin is easier to implement, guest OSes
205 * must cope with this anyway, because there are BIOSes out there in
206 * real machines which also use this scheme.
207 */
208 if (i == nb_numa_nodes) {
209 for (i = 0; i < max_cpus; i++) {
8c85901e 210 set_bit(i, numa_info[i % nb_numa_nodes].node_cpu);
96d0e26c
WG
211 }
212 }
213 }
214}
215
216void set_numa_modes(void)
217{
218 CPUState *cpu;
219 int i;
220
221 CPU_FOREACH(cpu) {
222 for (i = 0; i < nb_numa_nodes; i++) {
8c85901e 223 if (test_bit(cpu->cpu_index, numa_info[i].node_cpu)) {
96d0e26c
WG
224 cpu->numa_node = i;
225 }
226 }
227 }
228}
dfabb8b9 229
7febe36f
PB
230static void allocate_system_memory_nonnuma(MemoryRegion *mr, Object *owner,
231 const char *name,
232 uint64_t ram_size)
233{
0b183fc8
PB
234 if (mem_path) {
235#ifdef __linux__
7f56e740 236 Error *err = NULL;
dbcb8981 237 memory_region_init_ram_from_file(mr, owner, name, ram_size, false,
7f56e740
PB
238 mem_path, &err);
239
240 /* Legacy behavior: if allocation failed, fall back to
241 * regular RAM allocation.
242 */
c3ba3095 243 if (err) {
7f56e740
PB
244 qerror_report_err(err);
245 error_free(err);
246 memory_region_init_ram(mr, owner, name, ram_size);
247 }
0b183fc8
PB
248#else
249 fprintf(stderr, "-mem-path not supported on this host\n");
250 exit(1);
251#endif
252 } else {
253 memory_region_init_ram(mr, owner, name, ram_size);
254 }
7febe36f
PB
255 vmstate_register_ram_global(mr);
256}
257
dfabb8b9
PB
258void memory_region_allocate_system_memory(MemoryRegion *mr, Object *owner,
259 const char *name,
260 uint64_t ram_size)
261{
7febe36f
PB
262 uint64_t addr = 0;
263 int i;
264
265 if (nb_numa_nodes == 0 || !have_memdevs) {
266 allocate_system_memory_nonnuma(mr, owner, name, ram_size);
267 return;
268 }
269
270 memory_region_init(mr, owner, name, ram_size);
271 for (i = 0; i < MAX_NODES; i++) {
272 Error *local_err = NULL;
273 uint64_t size = numa_info[i].node_mem;
274 HostMemoryBackend *backend = numa_info[i].node_memdev;
275 if (!backend) {
276 continue;
277 }
278 MemoryRegion *seg = host_memory_backend_get_memory(backend, &local_err);
279 if (local_err) {
280 qerror_report_err(local_err);
281 exit(1);
282 }
283
284 memory_region_add_subregion(mr, addr, seg);
285 vmstate_register_ram_global(seg);
286 addr += size;
287 }
dfabb8b9 288}
76b5d850
HT
289
290static int query_memdev(Object *obj, void *opaque)
291{
292 MemdevList **list = opaque;
293 Error *err = NULL;
294
295 if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
296 MemdevList *m = g_malloc0(sizeof(*m));
297
298 m->value = g_malloc0(sizeof(*m->value));
299
300 m->value->size = object_property_get_int(obj, "size",
301 &err);
302 if (err) {
303 goto error;
304 }
305
306 m->value->merge = object_property_get_bool(obj, "merge",
307 &err);
308 if (err) {
309 goto error;
310 }
311
312 m->value->dump = object_property_get_bool(obj, "dump",
313 &err);
314 if (err) {
315 goto error;
316 }
317
318 m->value->prealloc = object_property_get_bool(obj,
319 "prealloc", &err);
320 if (err) {
321 goto error;
322 }
323
324 m->value->policy = object_property_get_enum(obj,
325 "policy",
326 HostMemPolicy_lookup,
327 &err);
328 if (err) {
329 goto error;
330 }
331
332 object_property_get_uint16List(obj, "host-nodes",
333 &m->value->host_nodes, &err);
334 if (err) {
335 goto error;
336 }
337
338 m->next = *list;
339 *list = m;
340 }
341
342 return 0;
343error:
344 return -1;
345}
346
347MemdevList *qmp_query_memdev(Error **errp)
348{
349 Object *obj;
350 MemdevList *list = NULL, *m;
351
352 obj = object_resolve_path("/objects", NULL);
353 if (obj == NULL) {
354 return NULL;
355 }
356
357 if (object_child_foreach(obj, query_memdev, &list) != 0) {
358 goto error;
359 }
360
361 return list;
362
363error:
364 while (list) {
365 m = list;
366 list = list->next;
367 g_free(m->value);
368 g_free(m);
369 }
370 return NULL;
371}