]>
git.proxmox.com Git - mirror_qemu.git/blob - numa.c
2 * NUMA parameter parsing routines
4 * Copyright (c) 2014 Fujitsu Ltd.
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "sysemu/sysemu.h"
26 #include "exec/cpu-common.h"
27 #include "qemu/bitmap.h"
29 #include "qemu/error-report.h"
30 #include "include/exec/cpu-common.h" /* for RAM_ADDR_FMT */
32 static void numa_node_parse_cpus(int nodenr
, const char *cpus
)
35 unsigned long long value
, endvalue
;
37 /* Empty CPU range strings will be considered valid, they will simply
38 * not set any bit in the CPU bitmap.
44 if (parse_uint(cpus
, &value
, &endptr
, 10) < 0) {
48 if (parse_uint_full(endptr
+ 1, &endvalue
, 10) < 0) {
51 } else if (*endptr
== '\0') {
57 if (endvalue
>= MAX_CPUMASK_BITS
) {
58 endvalue
= MAX_CPUMASK_BITS
- 1;
60 "qemu: NUMA: A max of %d VCPUs are supported\n",
64 if (endvalue
< value
) {
68 bitmap_set(node_cpumask
[nodenr
], value
, endvalue
-value
+1);
72 fprintf(stderr
, "qemu: Invalid NUMA CPU range: %s\n", cpus
);
76 void numa_add(const char *optarg
)
80 unsigned long long nodenr
;
82 optarg
= get_opt_name(option
, 128, optarg
, ',');
86 if (!strcmp(option
, "node")) {
88 if (nb_numa_nodes
>= MAX_NODES
) {
89 fprintf(stderr
, "qemu: too many NUMA nodes\n");
93 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
94 nodenr
= nb_numa_nodes
;
96 if (parse_uint_full(option
, &nodenr
, 10) < 0) {
97 fprintf(stderr
, "qemu: Invalid NUMA nodeid: %s\n", option
);
102 if (nodenr
>= MAX_NODES
) {
103 fprintf(stderr
, "qemu: invalid NUMA nodeid: %llu\n", nodenr
);
107 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
108 node_mem
[nodenr
] = 0;
111 sval
= strtosz(option
, &endptr
);
112 if (sval
< 0 || *endptr
) {
113 fprintf(stderr
, "qemu: invalid numa mem size: %s\n", optarg
);
116 node_mem
[nodenr
] = sval
;
118 if (get_param_value(option
, 128, "cpus", optarg
) != 0) {
119 numa_node_parse_cpus(nodenr
, option
);
123 fprintf(stderr
, "Invalid -numa option: %s\n", option
);
128 void set_numa_nodes(void)
130 if (nb_numa_nodes
> 0) {
134 if (nb_numa_nodes
> MAX_NODES
) {
135 nb_numa_nodes
= MAX_NODES
;
138 /* If no memory size if given for any node, assume the default case
139 * and distribute the available memory equally across all nodes
141 for (i
= 0; i
< nb_numa_nodes
; i
++) {
142 if (node_mem
[i
] != 0) {
146 if (i
== nb_numa_nodes
) {
147 uint64_t usedmem
= 0;
149 /* On Linux, the each node's border has to be 8MB aligned,
150 * the final node gets the rest.
152 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
153 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
154 usedmem
+= node_mem
[i
];
156 node_mem
[i
] = ram_size
- usedmem
;
160 for (i
= 0; i
< nb_numa_nodes
; i
++) {
161 numa_total
+= node_mem
[i
];
163 if (numa_total
!= ram_size
) {
164 error_report("total memory for NUMA nodes (%" PRIu64
")"
165 " should equal RAM size (" RAM_ADDR_FMT
")",
166 numa_total
, ram_size
);
170 for (i
= 0; i
< nb_numa_nodes
; i
++) {
171 if (!bitmap_empty(node_cpumask
[i
], MAX_CPUMASK_BITS
)) {
175 /* assigning the VCPUs round-robin is easier to implement, guest OSes
176 * must cope with this anyway, because there are BIOSes out there in
177 * real machines which also use this scheme.
179 if (i
== nb_numa_nodes
) {
180 for (i
= 0; i
< max_cpus
; i
++) {
181 set_bit(i
, node_cpumask
[i
% nb_numa_nodes
]);
187 void set_numa_modes(void)
193 for (i
= 0; i
< nb_numa_nodes
; i
++) {
194 if (test_bit(cpu
->cpu_index
, node_cpumask
[i
])) {