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 g_string_append_printf(s
, "sockets (%u)", ms
->smp
.sockets
);
38 if (mc
->smp_props
.dies_supported
) {
39 g_string_append_printf(s
, " * dies (%u)", ms
->smp
.dies
);
42 if (mc
->smp_props
.clusters_supported
) {
43 g_string_append_printf(s
, " * clusters (%u)", ms
->smp
.clusters
);
46 g_string_append_printf(s
, " * cores (%u)", ms
->smp
.cores
);
47 g_string_append_printf(s
, " * threads (%u)", ms
->smp
.threads
);
49 return g_string_free(s
, false);
53 * machine_parse_smp_config: Generic function used to parse the given
56 * Any missing parameter in "cpus/maxcpus/sockets/cores/threads" will be
57 * automatically computed based on the provided ones.
59 * In the calculation of omitted sockets/cores/threads: we prefer sockets
60 * over cores over threads before 6.2, while preferring cores over sockets
61 * over threads since 6.2.
63 * In the calculation of cpus/maxcpus: When both maxcpus and cpus are omitted,
64 * maxcpus will be computed from the given parameters and cpus will be set
65 * equal to maxcpus. When only one of maxcpus and cpus is given then the
66 * omitted one will be set to its given counterpart's value. Both maxcpus and
67 * cpus may be specified, but maxcpus must be equal to or greater than cpus.
69 * For compatibility, apart from the parameters that will be computed, newly
70 * introduced topology members which are likely to be target specific should
71 * be directly set as 1 if they are omitted (e.g. dies for PC since 4.1).
73 void machine_parse_smp_config(MachineState
*ms
,
74 const SMPConfiguration
*config
, Error
**errp
)
76 MachineClass
*mc
= MACHINE_GET_CLASS(ms
);
77 unsigned cpus
= config
->has_cpus
? config
->cpus
: 0;
78 unsigned sockets
= config
->has_sockets
? config
->sockets
: 0;
79 unsigned dies
= config
->has_dies
? config
->dies
: 0;
80 unsigned clusters
= config
->has_clusters
? config
->clusters
: 0;
81 unsigned cores
= config
->has_cores
? config
->cores
: 0;
82 unsigned threads
= config
->has_threads
? config
->threads
: 0;
83 unsigned maxcpus
= config
->has_maxcpus
? config
->maxcpus
: 0;
86 * Specified CPU topology parameters must be greater than zero,
87 * explicit configuration like "cpus=0" is not allowed.
89 if ((config
->has_cpus
&& config
->cpus
== 0) ||
90 (config
->has_sockets
&& config
->sockets
== 0) ||
91 (config
->has_dies
&& config
->dies
== 0) ||
92 (config
->has_clusters
&& config
->clusters
== 0) ||
93 (config
->has_cores
&& config
->cores
== 0) ||
94 (config
->has_threads
&& config
->threads
== 0) ||
95 (config
->has_maxcpus
&& config
->maxcpus
== 0)) {
96 warn_report("Deprecated CPU topology (considered invalid): "
97 "CPU topology parameters must be greater than zero");
101 * If not supported by the machine, a topology parameter must be
102 * omitted or specified equal to 1.
104 if (!mc
->smp_props
.dies_supported
&& dies
> 1) {
105 error_setg(errp
, "dies not supported by this machine's CPU topology");
108 if (!mc
->smp_props
.clusters_supported
&& clusters
> 1) {
109 error_setg(errp
, "clusters not supported by this machine's CPU topology");
113 dies
= dies
> 0 ? dies
: 1;
114 clusters
= clusters
> 0 ? clusters
: 1;
116 /* compute missing values based on the provided ones */
117 if (cpus
== 0 && maxcpus
== 0) {
118 sockets
= sockets
> 0 ? sockets
: 1;
119 cores
= cores
> 0 ? cores
: 1;
120 threads
= threads
> 0 ? threads
: 1;
122 maxcpus
= maxcpus
> 0 ? maxcpus
: cpus
;
124 if (mc
->smp_props
.prefer_sockets
) {
125 /* prefer sockets over cores before 6.2 */
127 cores
= cores
> 0 ? cores
: 1;
128 threads
= threads
> 0 ? threads
: 1;
129 sockets
= maxcpus
/ (dies
* clusters
* cores
* threads
);
130 } else if (cores
== 0) {
131 threads
= threads
> 0 ? threads
: 1;
132 cores
= maxcpus
/ (sockets
* dies
* clusters
* threads
);
135 /* prefer cores over sockets since 6.2 */
137 sockets
= sockets
> 0 ? sockets
: 1;
138 threads
= threads
> 0 ? threads
: 1;
139 cores
= maxcpus
/ (sockets
* dies
* clusters
* threads
);
140 } else if (sockets
== 0) {
141 threads
= threads
> 0 ? threads
: 1;
142 sockets
= maxcpus
/ (dies
* clusters
* cores
* threads
);
146 /* try to calculate omitted threads at last */
148 threads
= maxcpus
/ (sockets
* dies
* clusters
* cores
);
152 maxcpus
= maxcpus
> 0 ? maxcpus
: sockets
* dies
* clusters
* cores
* threads
;
153 cpus
= cpus
> 0 ? cpus
: maxcpus
;
156 ms
->smp
.sockets
= sockets
;
158 ms
->smp
.clusters
= clusters
;
159 ms
->smp
.cores
= cores
;
160 ms
->smp
.threads
= threads
;
161 ms
->smp
.max_cpus
= maxcpus
;
163 mc
->smp_props
.has_clusters
= config
->has_clusters
;
165 /* sanity-check of the computed topology */
166 if (sockets
* dies
* clusters
* cores
* threads
!= maxcpus
) {
167 g_autofree
char *topo_msg
= cpu_hierarchy_to_string(ms
);
168 error_setg(errp
, "Invalid CPU topology: "
169 "product of the hierarchy must match maxcpus: "
170 "%s != maxcpus (%u)",
175 if (maxcpus
< cpus
) {
176 g_autofree
char *topo_msg
= cpu_hierarchy_to_string(ms
);
177 error_setg(errp
, "Invalid CPU topology: "
178 "maxcpus must be equal to or greater than smp: "
179 "%s == maxcpus (%u) < smp_cpus (%u)",
180 topo_msg
, maxcpus
, cpus
);
184 if (ms
->smp
.cpus
< mc
->min_cpus
) {
185 error_setg(errp
, "Invalid SMP CPUs %d. The min CPUs "
186 "supported by machine '%s' is %d",
188 mc
->name
, mc
->min_cpus
);
192 if (ms
->smp
.max_cpus
> mc
->max_cpus
) {
193 error_setg(errp
, "Invalid SMP CPUs %d. The max CPUs "
194 "supported by machine '%s' is %d",
196 mc
->name
, mc
->max_cpus
);