]> git.proxmox.com Git - mirror_qemu.git/blob - numa.c
NUMA: move numa related code to new file numa.c
[mirror_qemu.git] / numa.c
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"
29
30 static void numa_node_parse_cpus(int nodenr, const char *cpus)
31 {
32 char *endptr;
33 unsigned long long value, endvalue;
34
35 /* Empty CPU range strings will be considered valid, they will simply
36 * not set any bit in the CPU bitmap.
37 */
38 if (!*cpus) {
39 return;
40 }
41
42 if (parse_uint(cpus, &value, &endptr, 10) < 0) {
43 goto error;
44 }
45 if (*endptr == '-') {
46 if (parse_uint_full(endptr + 1, &endvalue, 10) < 0) {
47 goto error;
48 }
49 } else if (*endptr == '\0') {
50 endvalue = value;
51 } else {
52 goto error;
53 }
54
55 if (endvalue >= MAX_CPUMASK_BITS) {
56 endvalue = MAX_CPUMASK_BITS - 1;
57 fprintf(stderr,
58 "qemu: NUMA: A max of %d VCPUs are supported\n",
59 MAX_CPUMASK_BITS);
60 }
61
62 if (endvalue < value) {
63 goto error;
64 }
65
66 bitmap_set(node_cpumask[nodenr], value, endvalue-value+1);
67 return;
68
69 error:
70 fprintf(stderr, "qemu: Invalid NUMA CPU range: %s\n", cpus);
71 exit(1);
72 }
73
74 void numa_add(const char *optarg)
75 {
76 char option[128];
77 char *endptr;
78 unsigned long long nodenr;
79
80 optarg = get_opt_name(option, 128, optarg, ',');
81 if (*optarg == ',') {
82 optarg++;
83 }
84 if (!strcmp(option, "node")) {
85
86 if (nb_numa_nodes >= MAX_NODES) {
87 fprintf(stderr, "qemu: too many NUMA nodes\n");
88 exit(1);
89 }
90
91 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
92 nodenr = nb_numa_nodes;
93 } else {
94 if (parse_uint_full(option, &nodenr, 10) < 0) {
95 fprintf(stderr, "qemu: Invalid NUMA nodeid: %s\n", option);
96 exit(1);
97 }
98 }
99
100 if (nodenr >= MAX_NODES) {
101 fprintf(stderr, "qemu: invalid NUMA nodeid: %llu\n", nodenr);
102 exit(1);
103 }
104
105 if (get_param_value(option, 128, "mem", optarg) == 0) {
106 node_mem[nodenr] = 0;
107 } else {
108 int64_t sval;
109 sval = strtosz(option, &endptr);
110 if (sval < 0 || *endptr) {
111 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
112 exit(1);
113 }
114 node_mem[nodenr] = sval;
115 }
116 if (get_param_value(option, 128, "cpus", optarg) != 0) {
117 numa_node_parse_cpus(nodenr, option);
118 }
119 nb_numa_nodes++;
120 } else {
121 fprintf(stderr, "Invalid -numa option: %s\n", option);
122 exit(1);
123 }
124 }
125
126 void set_numa_nodes(void)
127 {
128 if (nb_numa_nodes > 0) {
129 int i;
130
131 if (nb_numa_nodes > MAX_NODES) {
132 nb_numa_nodes = MAX_NODES;
133 }
134
135 /* If no memory size if given for any node, assume the default case
136 * and distribute the available memory equally across all nodes
137 */
138 for (i = 0; i < nb_numa_nodes; i++) {
139 if (node_mem[i] != 0) {
140 break;
141 }
142 }
143 if (i == nb_numa_nodes) {
144 uint64_t usedmem = 0;
145
146 /* On Linux, the each node's border has to be 8MB aligned,
147 * the final node gets the rest.
148 */
149 for (i = 0; i < nb_numa_nodes - 1; i++) {
150 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
151 usedmem += node_mem[i];
152 }
153 node_mem[i] = ram_size - usedmem;
154 }
155
156 for (i = 0; i < nb_numa_nodes; i++) {
157 if (!bitmap_empty(node_cpumask[i], MAX_CPUMASK_BITS)) {
158 break;
159 }
160 }
161 /* assigning the VCPUs round-robin is easier to implement, guest OSes
162 * must cope with this anyway, because there are BIOSes out there in
163 * real machines which also use this scheme.
164 */
165 if (i == nb_numa_nodes) {
166 for (i = 0; i < max_cpus; i++) {
167 set_bit(i, node_cpumask[i % nb_numa_nodes]);
168 }
169 }
170 }
171 }
172
173 void set_numa_modes(void)
174 {
175 CPUState *cpu;
176 int i;
177
178 CPU_FOREACH(cpu) {
179 for (i = 0; i < nb_numa_nodes; i++) {
180 if (test_bit(cpu->cpu_index, node_cpumask[i])) {
181 cpu->numa_node = i;
182 }
183 }
184 }
185 }