]> git.proxmox.com Git - mirror_qemu.git/blame - numa.c
qmp: improve error reporting for -object and object-add
[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"
35
36QemuOptsList qemu_numa_opts = {
37 .name = "numa",
38 .implied_opt_name = "type",
39 .head = QTAILQ_HEAD_INITIALIZER(qemu_numa_opts.head),
40 .desc = { { 0 } } /* validated with OptsVisitor */
41};
42
43static void numa_node_parse(NumaNodeOptions *node, QemuOpts *opts, Error **errp)
96d0e26c 44{
0042109a
WG
45 uint16_t nodenr;
46 uint16List *cpus = NULL;
96d0e26c 47
0042109a
WG
48 if (node->has_nodeid) {
49 nodenr = node->nodeid;
96d0e26c 50 } else {
0042109a 51 nodenr = nb_numa_nodes;
96d0e26c
WG
52 }
53
0042109a
WG
54 if (nodenr >= MAX_NODES) {
55 error_setg(errp, "Max number of NUMA nodes reached: %"
56 PRIu16 "\n", nodenr);
57 return;
96d0e26c
WG
58 }
59
0042109a
WG
60 for (cpus = node->cpus; cpus; cpus = cpus->next) {
61 if (cpus->value > MAX_CPUMASK_BITS) {
62 error_setg(errp, "CPU number %" PRIu16 " is bigger than %d",
63 cpus->value, MAX_CPUMASK_BITS);
64 return;
65 }
66 bitmap_set(numa_info[nodenr].node_cpu, cpus->value, 1);
96d0e26c
WG
67 }
68
0042109a
WG
69 if (node->has_mem) {
70 uint64_t mem_size = node->mem;
71 const char *mem_str = qemu_opt_get(opts, "mem");
72 /* Fix up legacy suffix-less format */
73 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
74 mem_size <<= 20;
75 }
76 numa_info[nodenr].node_mem = mem_size;
77 }
96d0e26c
WG
78}
79
0042109a 80int numa_init_func(QemuOpts *opts, void *opaque)
96d0e26c 81{
0042109a
WG
82 NumaOptions *object = NULL;
83 Error *err = NULL;
96d0e26c 84
0042109a
WG
85 {
86 OptsVisitor *ov = opts_visitor_new(opts);
87 visit_type_NumaOptions(opts_get_visitor(ov), &object, NULL, &err);
88 opts_visitor_cleanup(ov);
96d0e26c 89 }
96d0e26c 90
0042109a
WG
91 if (err) {
92 goto error;
93 }
96d0e26c 94
0042109a
WG
95 switch (object->kind) {
96 case NUMA_OPTIONS_KIND_NODE:
97 numa_node_parse(object->node, opts, &err);
98 if (err) {
99 goto error;
96d0e26c 100 }
0042109a
WG
101 nb_numa_nodes++;
102 break;
103 default:
104 abort();
105 }
96d0e26c 106
0042109a 107 return 0;
96d0e26c 108
0042109a
WG
109error:
110 qerror_report_err(err);
111 error_free(err);
112
113 if (object) {
114 QapiDeallocVisitor *dv = qapi_dealloc_visitor_new();
115 visit_type_NumaOptions(qapi_dealloc_get_visitor(dv),
116 &object, NULL, NULL);
117 qapi_dealloc_visitor_cleanup(dv);
96d0e26c 118 }
0042109a
WG
119
120 return -1;
96d0e26c
WG
121}
122
123void set_numa_nodes(void)
124{
125 if (nb_numa_nodes > 0) {
2b631ec2 126 uint64_t numa_total;
96d0e26c
WG
127 int i;
128
129 if (nb_numa_nodes > MAX_NODES) {
130 nb_numa_nodes = MAX_NODES;
131 }
132
133 /* If no memory size if given for any node, assume the default case
134 * and distribute the available memory equally across all nodes
135 */
136 for (i = 0; i < nb_numa_nodes; i++) {
8c85901e 137 if (numa_info[i].node_mem != 0) {
96d0e26c
WG
138 break;
139 }
140 }
141 if (i == nb_numa_nodes) {
142 uint64_t usedmem = 0;
143
144 /* On Linux, the each node's border has to be 8MB aligned,
145 * the final node gets the rest.
146 */
147 for (i = 0; i < nb_numa_nodes - 1; i++) {
8c85901e
WG
148 numa_info[i].node_mem = (ram_size / nb_numa_nodes) &
149 ~((1 << 23UL) - 1);
150 usedmem += numa_info[i].node_mem;
96d0e26c 151 }
8c85901e 152 numa_info[i].node_mem = ram_size - usedmem;
96d0e26c
WG
153 }
154
2b631ec2
WG
155 numa_total = 0;
156 for (i = 0; i < nb_numa_nodes; i++) {
8c85901e 157 numa_total += numa_info[i].node_mem;
2b631ec2
WG
158 }
159 if (numa_total != ram_size) {
160 error_report("total memory for NUMA nodes (%" PRIu64 ")"
161 " should equal RAM size (" RAM_ADDR_FMT ")",
162 numa_total, ram_size);
163 exit(1);
164 }
165
96d0e26c 166 for (i = 0; i < nb_numa_nodes; i++) {
8c85901e 167 if (!bitmap_empty(numa_info[i].node_cpu, MAX_CPUMASK_BITS)) {
96d0e26c
WG
168 break;
169 }
170 }
171 /* assigning the VCPUs round-robin is easier to implement, guest OSes
172 * must cope with this anyway, because there are BIOSes out there in
173 * real machines which also use this scheme.
174 */
175 if (i == nb_numa_nodes) {
176 for (i = 0; i < max_cpus; i++) {
8c85901e 177 set_bit(i, numa_info[i % nb_numa_nodes].node_cpu);
96d0e26c
WG
178 }
179 }
180 }
181}
182
183void set_numa_modes(void)
184{
185 CPUState *cpu;
186 int i;
187
188 CPU_FOREACH(cpu) {
189 for (i = 0; i < nb_numa_nodes; i++) {
8c85901e 190 if (test_bit(cpu->cpu_index, numa_info[i].node_cpu)) {
96d0e26c
WG
191 cpu->numa_node = i;
192 }
193 }
194 }
195}