2 * QEMU Machine core (related to -smp parsing)
4 * Copyright (c) 2021 Huawei Technologies Co., Ltd
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License,
9 * or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
21 #include "hw/boards.h"
22 #include "qapi/error.h"
23 #include "qemu/error-report.h"
27 * Report information of a machine's supported CPU topology hierarchy.
28 * Topology members will be ordered from the largest to the smallest
31 static char *cpu_hierarchy_to_string(MachineState
*ms
)
33 MachineClass
*mc
= MACHINE_GET_CLASS(ms
);
34 GString
*s
= g_string_new(NULL
);
36 if (mc
->smp_props
.drawers_supported
) {
37 g_string_append_printf(s
, "drawers (%u) * ", ms
->smp
.drawers
);
40 if (mc
->smp_props
.books_supported
) {
41 g_string_append_printf(s
, "books (%u) * ", ms
->smp
.books
);
44 g_string_append_printf(s
, "sockets (%u)", ms
->smp
.sockets
);
46 if (mc
->smp_props
.dies_supported
) {
47 g_string_append_printf(s
, " * dies (%u)", ms
->smp
.dies
);
50 if (mc
->smp_props
.clusters_supported
) {
51 g_string_append_printf(s
, " * clusters (%u)", ms
->smp
.clusters
);
54 if (mc
->smp_props
.modules_supported
) {
55 g_string_append_printf(s
, " * modules (%u)", ms
->smp
.modules
);
58 g_string_append_printf(s
, " * cores (%u)", ms
->smp
.cores
);
59 g_string_append_printf(s
, " * threads (%u)", ms
->smp
.threads
);
61 return g_string_free(s
, false);
65 * machine_parse_smp_config: Generic function used to parse the given
68 * Any missing parameter in "cpus/maxcpus/sockets/cores/threads" will be
69 * automatically computed based on the provided ones.
71 * In the calculation of omitted sockets/cores/threads: we prefer sockets
72 * over cores over threads before 6.2, while preferring cores over sockets
73 * over threads since 6.2.
75 * In the calculation of cpus/maxcpus: When both maxcpus and cpus are omitted,
76 * maxcpus will be computed from the given parameters and cpus will be set
77 * equal to maxcpus. When only one of maxcpus and cpus is given then the
78 * omitted one will be set to its given counterpart's value. Both maxcpus and
79 * cpus may be specified, but maxcpus must be equal to or greater than cpus.
81 * For compatibility, apart from the parameters that will be computed, newly
82 * introduced topology members which are likely to be target specific should
83 * be directly set as 1 if they are omitted (e.g. dies for PC since 4.1).
85 void machine_parse_smp_config(MachineState
*ms
,
86 const SMPConfiguration
*config
, Error
**errp
)
88 MachineClass
*mc
= MACHINE_GET_CLASS(ms
);
89 unsigned cpus
= config
->has_cpus
? config
->cpus
: 0;
90 unsigned drawers
= config
->has_drawers
? config
->drawers
: 0;
91 unsigned books
= config
->has_books
? config
->books
: 0;
92 unsigned sockets
= config
->has_sockets
? config
->sockets
: 0;
93 unsigned dies
= config
->has_dies
? config
->dies
: 0;
94 unsigned clusters
= config
->has_clusters
? config
->clusters
: 0;
95 unsigned modules
= config
->has_modules
? config
->modules
: 0;
96 unsigned cores
= config
->has_cores
? config
->cores
: 0;
97 unsigned threads
= config
->has_threads
? config
->threads
: 0;
98 unsigned maxcpus
= config
->has_maxcpus
? config
->maxcpus
: 0;
102 * Specified CPU topology parameters must be greater than zero,
103 * explicit configuration like "cpus=0" is not allowed.
105 if ((config
->has_cpus
&& config
->cpus
== 0) ||
106 (config
->has_drawers
&& config
->drawers
== 0) ||
107 (config
->has_books
&& config
->books
== 0) ||
108 (config
->has_sockets
&& config
->sockets
== 0) ||
109 (config
->has_dies
&& config
->dies
== 0) ||
110 (config
->has_clusters
&& config
->clusters
== 0) ||
111 (config
->has_modules
&& config
->modules
== 0) ||
112 (config
->has_cores
&& config
->cores
== 0) ||
113 (config
->has_threads
&& config
->threads
== 0) ||
114 (config
->has_maxcpus
&& config
->maxcpus
== 0)) {
115 error_setg(errp
, "Invalid CPU topology: "
116 "CPU topology parameters must be greater than zero");
121 * If not supported by the machine, a topology parameter must be
124 if (!mc
->smp_props
.modules_supported
&& config
->has_modules
) {
125 if (config
->modules
> 1) {
126 error_setg(errp
, "modules not supported by this "
127 "machine's CPU topology");
130 /* Here modules only equals 1 since we've checked zero case. */
131 warn_report("Deprecated CPU topology (considered invalid): "
132 "Unsupported modules parameter mustn't be "
136 modules
= modules
> 0 ? modules
: 1;
138 if (!mc
->smp_props
.clusters_supported
&& config
->has_clusters
) {
139 if (config
->clusters
> 1) {
140 error_setg(errp
, "clusters not supported by this "
141 "machine's CPU topology");
144 /* Here clusters only equals 1 since we've checked zero case. */
145 warn_report("Deprecated CPU topology (considered invalid): "
146 "Unsupported clusters parameter mustn't be "
150 clusters
= clusters
> 0 ? clusters
: 1;
152 if (!mc
->smp_props
.dies_supported
&& config
->has_dies
) {
153 if (config
->dies
> 1) {
154 error_setg(errp
, "dies not supported by this "
155 "machine's CPU topology");
158 /* Here dies only equals 1 since we've checked zero case. */
159 warn_report("Deprecated CPU topology (considered invalid): "
160 "Unsupported dies parameter mustn't be "
164 dies
= dies
> 0 ? dies
: 1;
166 if (!mc
->smp_props
.books_supported
&& config
->has_books
) {
167 if (config
->books
> 1) {
168 error_setg(errp
, "books not supported by this "
169 "machine's CPU topology");
172 /* Here books only equals 1 since we've checked zero case. */
173 warn_report("Deprecated CPU topology (considered invalid): "
174 "Unsupported books parameter mustn't be "
178 books
= books
> 0 ? books
: 1;
180 if (!mc
->smp_props
.drawers_supported
&& config
->has_drawers
) {
181 if (config
->drawers
> 1) {
182 error_setg(errp
, "drawers not supported by this "
183 "machine's CPU topology");
186 /* Here drawers only equals 1 since we've checked zero case. */
187 warn_report("Deprecated CPU topology (considered invalid): "
188 "Unsupported drawers parameter mustn't be "
192 drawers
= drawers
> 0 ? drawers
: 1;
194 /* compute missing values based on the provided ones */
195 if (cpus
== 0 && maxcpus
== 0) {
196 sockets
= sockets
> 0 ? sockets
: 1;
197 cores
= cores
> 0 ? cores
: 1;
198 threads
= threads
> 0 ? threads
: 1;
200 maxcpus
= maxcpus
> 0 ? maxcpus
: cpus
;
202 if (mc
->smp_props
.prefer_sockets
) {
203 /* prefer sockets over cores before 6.2 */
205 cores
= cores
> 0 ? cores
: 1;
206 threads
= threads
> 0 ? threads
: 1;
208 (drawers
* books
* dies
* clusters
*
209 modules
* cores
* threads
);
210 } else if (cores
== 0) {
211 threads
= threads
> 0 ? threads
: 1;
213 (drawers
* books
* sockets
* dies
*
214 clusters
* modules
* threads
);
217 /* prefer cores over sockets since 6.2 */
219 sockets
= sockets
> 0 ? sockets
: 1;
220 threads
= threads
> 0 ? threads
: 1;
222 (drawers
* books
* sockets
* dies
*
223 clusters
* modules
* threads
);
224 } else if (sockets
== 0) {
225 threads
= threads
> 0 ? threads
: 1;
227 (drawers
* books
* dies
* clusters
*
228 modules
* cores
* threads
);
232 /* try to calculate omitted threads at last */
235 (drawers
* books
* sockets
* dies
*
236 clusters
* modules
* cores
);
240 total_cpus
= drawers
* books
* sockets
* dies
*
241 clusters
* modules
* cores
* threads
;
242 maxcpus
= maxcpus
> 0 ? maxcpus
: total_cpus
;
243 cpus
= cpus
> 0 ? cpus
: maxcpus
;
246 ms
->smp
.drawers
= drawers
;
247 ms
->smp
.books
= books
;
248 ms
->smp
.sockets
= sockets
;
250 ms
->smp
.clusters
= clusters
;
251 ms
->smp
.modules
= modules
;
252 ms
->smp
.cores
= cores
;
253 ms
->smp
.threads
= threads
;
254 ms
->smp
.max_cpus
= maxcpus
;
256 mc
->smp_props
.has_clusters
= config
->has_clusters
;
258 /* sanity-check of the computed topology */
259 if (total_cpus
!= maxcpus
) {
260 g_autofree
char *topo_msg
= cpu_hierarchy_to_string(ms
);
261 error_setg(errp
, "Invalid CPU topology: "
262 "product of the hierarchy must match maxcpus: "
263 "%s != maxcpus (%u)",
268 if (maxcpus
< cpus
) {
269 g_autofree
char *topo_msg
= cpu_hierarchy_to_string(ms
);
270 error_setg(errp
, "Invalid CPU topology: "
271 "maxcpus must be equal to or greater than smp: "
272 "%s == maxcpus (%u) < smp_cpus (%u)",
273 topo_msg
, maxcpus
, cpus
);
277 if (ms
->smp
.cpus
< mc
->min_cpus
) {
278 error_setg(errp
, "Invalid SMP CPUs %d. The min CPUs "
279 "supported by machine '%s' is %d",
281 mc
->name
, mc
->min_cpus
);
285 if (ms
->smp
.max_cpus
> mc
->max_cpus
) {
286 error_setg(errp
, "Invalid SMP CPUs %d. The max CPUs "
287 "supported by machine '%s' is %d",
289 mc
->name
, mc
->max_cpus
);
294 unsigned int machine_topo_get_cores_per_socket(const MachineState
*ms
)
296 return ms
->smp
.cores
* ms
->smp
.modules
* ms
->smp
.clusters
* ms
->smp
.dies
;
299 unsigned int machine_topo_get_threads_per_socket(const MachineState
*ms
)
301 return ms
->smp
.threads
* machine_topo_get_cores_per_socket(ms
);