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 g_string_append_printf(s
, " * cores (%u)", ms
->smp
.cores
);
55 g_string_append_printf(s
, " * threads (%u)", ms
->smp
.threads
);
57 return g_string_free(s
, false);
61 * machine_parse_smp_config: Generic function used to parse the given
64 * Any missing parameter in "cpus/maxcpus/sockets/cores/threads" will be
65 * automatically computed based on the provided ones.
67 * In the calculation of omitted sockets/cores/threads: we prefer sockets
68 * over cores over threads before 6.2, while preferring cores over sockets
69 * over threads since 6.2.
71 * In the calculation of cpus/maxcpus: When both maxcpus and cpus are omitted,
72 * maxcpus will be computed from the given parameters and cpus will be set
73 * equal to maxcpus. When only one of maxcpus and cpus is given then the
74 * omitted one will be set to its given counterpart's value. Both maxcpus and
75 * cpus may be specified, but maxcpus must be equal to or greater than cpus.
77 * For compatibility, apart from the parameters that will be computed, newly
78 * introduced topology members which are likely to be target specific should
79 * be directly set as 1 if they are omitted (e.g. dies for PC since 4.1).
81 void machine_parse_smp_config(MachineState
*ms
,
82 const SMPConfiguration
*config
, Error
**errp
)
84 MachineClass
*mc
= MACHINE_GET_CLASS(ms
);
85 unsigned cpus
= config
->has_cpus
? config
->cpus
: 0;
86 unsigned drawers
= config
->has_drawers
? config
->drawers
: 0;
87 unsigned books
= config
->has_books
? config
->books
: 0;
88 unsigned sockets
= config
->has_sockets
? config
->sockets
: 0;
89 unsigned dies
= config
->has_dies
? config
->dies
: 0;
90 unsigned clusters
= config
->has_clusters
? config
->clusters
: 0;
91 unsigned cores
= config
->has_cores
? config
->cores
: 0;
92 unsigned threads
= config
->has_threads
? config
->threads
: 0;
93 unsigned maxcpus
= config
->has_maxcpus
? config
->maxcpus
: 0;
96 * Specified CPU topology parameters must be greater than zero,
97 * explicit configuration like "cpus=0" is not allowed.
99 if ((config
->has_cpus
&& config
->cpus
== 0) ||
100 (config
->has_drawers
&& config
->drawers
== 0) ||
101 (config
->has_books
&& config
->books
== 0) ||
102 (config
->has_sockets
&& config
->sockets
== 0) ||
103 (config
->has_dies
&& config
->dies
== 0) ||
104 (config
->has_clusters
&& config
->clusters
== 0) ||
105 (config
->has_cores
&& config
->cores
== 0) ||
106 (config
->has_threads
&& config
->threads
== 0) ||
107 (config
->has_maxcpus
&& config
->maxcpus
== 0)) {
108 warn_report("Deprecated CPU topology (considered invalid): "
109 "CPU topology parameters must be greater than zero");
113 * If not supported by the machine, a topology parameter must be
114 * omitted or specified equal to 1.
116 if (!mc
->smp_props
.dies_supported
&& dies
> 1) {
117 error_setg(errp
, "dies not supported by this machine's CPU topology");
120 if (!mc
->smp_props
.clusters_supported
&& clusters
> 1) {
121 error_setg(errp
, "clusters not supported by this machine's CPU topology");
125 dies
= dies
> 0 ? dies
: 1;
126 clusters
= clusters
> 0 ? clusters
: 1;
128 if (!mc
->smp_props
.books_supported
&& books
> 1) {
129 error_setg(errp
, "books not supported by this machine's CPU topology");
132 books
= books
> 0 ? books
: 1;
134 if (!mc
->smp_props
.drawers_supported
&& drawers
> 1) {
136 "drawers not supported by this machine's CPU topology");
139 drawers
= drawers
> 0 ? drawers
: 1;
141 /* compute missing values based on the provided ones */
142 if (cpus
== 0 && maxcpus
== 0) {
143 sockets
= sockets
> 0 ? sockets
: 1;
144 cores
= cores
> 0 ? cores
: 1;
145 threads
= threads
> 0 ? threads
: 1;
147 maxcpus
= maxcpus
> 0 ? maxcpus
: cpus
;
149 if (mc
->smp_props
.prefer_sockets
) {
150 /* prefer sockets over cores before 6.2 */
152 cores
= cores
> 0 ? cores
: 1;
153 threads
= threads
> 0 ? threads
: 1;
155 (drawers
* books
* dies
* clusters
* cores
* threads
);
156 } else if (cores
== 0) {
157 threads
= threads
> 0 ? threads
: 1;
159 (drawers
* books
* sockets
* dies
* clusters
* threads
);
162 /* prefer cores over sockets since 6.2 */
164 sockets
= sockets
> 0 ? sockets
: 1;
165 threads
= threads
> 0 ? threads
: 1;
167 (drawers
* books
* sockets
* dies
* clusters
* threads
);
168 } else if (sockets
== 0) {
169 threads
= threads
> 0 ? threads
: 1;
171 (drawers
* books
* dies
* clusters
* cores
* threads
);
175 /* try to calculate omitted threads at last */
178 (drawers
* books
* sockets
* dies
* clusters
* cores
);
182 maxcpus
= maxcpus
> 0 ? maxcpus
: drawers
* books
* sockets
* dies
*
183 clusters
* cores
* threads
;
184 cpus
= cpus
> 0 ? cpus
: maxcpus
;
187 ms
->smp
.drawers
= drawers
;
188 ms
->smp
.books
= books
;
189 ms
->smp
.sockets
= sockets
;
191 ms
->smp
.clusters
= clusters
;
192 ms
->smp
.cores
= cores
;
193 ms
->smp
.threads
= threads
;
194 ms
->smp
.max_cpus
= maxcpus
;
196 mc
->smp_props
.has_clusters
= config
->has_clusters
;
198 /* sanity-check of the computed topology */
199 if (drawers
* books
* sockets
* dies
* clusters
* cores
* threads
!=
201 g_autofree
char *topo_msg
= cpu_hierarchy_to_string(ms
);
202 error_setg(errp
, "Invalid CPU topology: "
203 "product of the hierarchy must match maxcpus: "
204 "%s != maxcpus (%u)",
209 if (maxcpus
< cpus
) {
210 g_autofree
char *topo_msg
= cpu_hierarchy_to_string(ms
);
211 error_setg(errp
, "Invalid CPU topology: "
212 "maxcpus must be equal to or greater than smp: "
213 "%s == maxcpus (%u) < smp_cpus (%u)",
214 topo_msg
, maxcpus
, cpus
);
218 if (ms
->smp
.cpus
< mc
->min_cpus
) {
219 error_setg(errp
, "Invalid SMP CPUs %d. The min CPUs "
220 "supported by machine '%s' is %d",
222 mc
->name
, mc
->min_cpus
);
226 if (ms
->smp
.max_cpus
> mc
->max_cpus
) {
227 error_setg(errp
, "Invalid SMP CPUs %d. The max CPUs "
228 "supported by machine '%s' is %d",
230 mc
->name
, mc
->max_cpus
);
235 unsigned int machine_topo_get_cores_per_socket(const MachineState
*ms
)
237 return ms
->smp
.cores
* ms
->smp
.clusters
* ms
->smp
.dies
;
240 unsigned int machine_topo_get_threads_per_socket(const MachineState
*ms
)
242 return ms
->smp
.threads
* machine_topo_get_cores_per_socket(ms
);