blob: 6fb0888d607f6071fb82582486f79fbee4473986 [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
25#include "sysemu/sysemu.h"
26#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"
34#include "qapi/qmp/qerror.h"
Wanlong Gao96d0e262014-05-14 17:43:05 +080035
Wanlong Gao00421092014-05-14 17:43:08 +080036QemuOptsList 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)
Wanlong Gao96d0e262014-05-14 17:43:05 +080044{
Wanlong Gao00421092014-05-14 17:43:08 +080045 uint16_t nodenr;
46 uint16List *cpus = NULL;
Wanlong Gao96d0e262014-05-14 17:43:05 +080047
Wanlong Gao00421092014-05-14 17:43:08 +080048 if (node->has_nodeid) {
49 nodenr = node->nodeid;
50 } else {
51 nodenr = nb_numa_nodes;
52 }
53
54 if (nodenr >= MAX_NODES) {
55 error_setg(errp, "Max number of NUMA nodes reached: %"
56 PRIu16 "\n", nodenr);
Wanlong Gao96d0e262014-05-14 17:43:05 +080057 return;
58 }
59
Wanlong Gao00421092014-05-14 17:43:08 +080060 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;
Wanlong Gao96d0e262014-05-14 17:43:05 +080065 }
Wanlong Gao00421092014-05-14 17:43:08 +080066 bitmap_set(numa_info[nodenr].node_cpu, cpus->value, 1);
Wanlong Gao96d0e262014-05-14 17:43:05 +080067 }
68
Wanlong Gao00421092014-05-14 17:43:08 +080069 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;
Wanlong Gao96d0e262014-05-14 17:43:05 +080077 }
Wanlong Gao96d0e262014-05-14 17:43:05 +080078}
79
Wanlong Gao00421092014-05-14 17:43:08 +080080int numa_init_func(QemuOpts *opts, void *opaque)
Wanlong Gao96d0e262014-05-14 17:43:05 +080081{
Wanlong Gao00421092014-05-14 17:43:08 +080082 NumaOptions *object = NULL;
83 Error *err = NULL;
Wanlong Gao96d0e262014-05-14 17:43:05 +080084
Wanlong Gao00421092014-05-14 17:43:08 +080085 {
86 OptsVisitor *ov = opts_visitor_new(opts);
87 visit_type_NumaOptions(opts_get_visitor(ov), &object, NULL, &err);
88 opts_visitor_cleanup(ov);
Wanlong Gao96d0e262014-05-14 17:43:05 +080089 }
Wanlong Gao96d0e262014-05-14 17:43:05 +080090
Wanlong Gao00421092014-05-14 17:43:08 +080091 if (err) {
92 goto error;
93 }
Wanlong Gao96d0e262014-05-14 17:43:05 +080094
Wanlong Gao00421092014-05-14 17:43:08 +080095 switch (object->kind) {
96 case NUMA_OPTIONS_KIND_NODE:
97 numa_node_parse(object->node, opts, &err);
98 if (err) {
99 goto error;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800100 }
101 nb_numa_nodes++;
Wanlong Gao00421092014-05-14 17:43:08 +0800102 break;
103 default:
104 abort();
Wanlong Gao96d0e262014-05-14 17:43:05 +0800105 }
Wanlong Gao00421092014-05-14 17:43:08 +0800106
107 return 0;
108
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);
118 }
119
120 return -1;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800121}
122
123void set_numa_nodes(void)
124{
125 if (nb_numa_nodes > 0) {
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800126 uint64_t numa_total;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800127 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++) {
Wanlong Gao8c859012014-05-14 17:43:07 +0800137 if (numa_info[i].node_mem != 0) {
Wanlong Gao96d0e262014-05-14 17:43:05 +0800138 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++) {
Wanlong Gao8c859012014-05-14 17:43:07 +0800148 numa_info[i].node_mem = (ram_size / nb_numa_nodes) &
149 ~((1 << 23UL) - 1);
150 usedmem += numa_info[i].node_mem;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800151 }
Wanlong Gao8c859012014-05-14 17:43:07 +0800152 numa_info[i].node_mem = ram_size - usedmem;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800153 }
154
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800155 numa_total = 0;
156 for (i = 0; i < nb_numa_nodes; i++) {
Wanlong Gao8c859012014-05-14 17:43:07 +0800157 numa_total += numa_info[i].node_mem;
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800158 }
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
Wanlong Gao96d0e262014-05-14 17:43:05 +0800166 for (i = 0; i < nb_numa_nodes; i++) {
Wanlong Gao8c859012014-05-14 17:43:07 +0800167 if (!bitmap_empty(numa_info[i].node_cpu, MAX_CPUMASK_BITS)) {
Wanlong Gao96d0e262014-05-14 17:43:05 +0800168 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++) {
Wanlong Gao8c859012014-05-14 17:43:07 +0800177 set_bit(i, numa_info[i % nb_numa_nodes].node_cpu);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800178 }
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++) {
Wanlong Gao8c859012014-05-14 17:43:07 +0800190 if (test_bit(cpu->cpu_index, numa_info[i].node_cpu)) {
Wanlong Gao96d0e262014-05-14 17:43:05 +0800191 cpu->numa_node = i;
192 }
193 }
194 }
195}