xref: /qemu/hw/core/machine-smp.c (revision 61dec060)
1 /*
2  * QEMU Machine core (related to -smp parsing)
3  *
4  * Copyright (c) 2021 Huawei Technologies Co., Ltd
5  *
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.
10  *
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.
15  *
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/>.
18  */
19 
20 #include "qemu/osdep.h"
21 #include "hw/boards.h"
22 #include "qapi/error.h"
23 #include "qemu/error-report.h"
24 
25 
26 /*
27  * Report information of a machine's supported CPU topology hierarchy.
28  * Topology members will be ordered from the largest to the smallest
29  * in the string.
30  */
31 static char *cpu_hierarchy_to_string(MachineState *ms)
32 {
33     MachineClass *mc = MACHINE_GET_CLASS(ms);
34     GString *s = g_string_new(NULL);
35 
36     if (mc->smp_props.drawers_supported) {
37         g_string_append_printf(s, "drawers (%u) * ", ms->smp.drawers);
38     }
39 
40     if (mc->smp_props.books_supported) {
41         g_string_append_printf(s, "books (%u) * ", ms->smp.books);
42     }
43 
44     g_string_append_printf(s, "sockets (%u)", ms->smp.sockets);
45 
46     if (mc->smp_props.dies_supported) {
47         g_string_append_printf(s, " * dies (%u)", ms->smp.dies);
48     }
49 
50     if (mc->smp_props.clusters_supported) {
51         g_string_append_printf(s, " * clusters (%u)", ms->smp.clusters);
52     }
53 
54     g_string_append_printf(s, " * cores (%u)", ms->smp.cores);
55     g_string_append_printf(s, " * threads (%u)", ms->smp.threads);
56 
57     return g_string_free(s, false);
58 }
59 
60 /*
61  * machine_parse_smp_config: Generic function used to parse the given
62  *                           SMP configuration
63  *
64  * Any missing parameter in "cpus/maxcpus/sockets/cores/threads" will be
65  * automatically computed based on the provided ones.
66  *
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.
70  *
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.
76  *
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).
80  */
81 void machine_parse_smp_config(MachineState *ms,
82                               const SMPConfiguration *config, Error **errp)
83 {
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;
94     unsigned total_cpus;
95 
96     /*
97      * Specified CPU topology parameters must be greater than zero,
98      * explicit configuration like "cpus=0" is not allowed.
99      */
100     if ((config->has_cpus && config->cpus == 0) ||
101         (config->has_drawers && config->drawers == 0) ||
102         (config->has_books && config->books == 0) ||
103         (config->has_sockets && config->sockets == 0) ||
104         (config->has_dies && config->dies == 0) ||
105         (config->has_clusters && config->clusters == 0) ||
106         (config->has_cores && config->cores == 0) ||
107         (config->has_threads && config->threads == 0) ||
108         (config->has_maxcpus && config->maxcpus == 0)) {
109         error_setg(errp, "Invalid CPU topology: "
110                    "CPU topology parameters must be greater than zero");
111         return;
112     }
113 
114     /*
115      * If not supported by the machine, a topology parameter must be
116      * omitted.
117      */
118     if (!mc->smp_props.clusters_supported && config->has_clusters) {
119         if (config->clusters > 1) {
120             error_setg(errp, "clusters not supported by this "
121                        "machine's CPU topology");
122             return;
123         } else {
124             /* Here clusters only equals 1 since we've checked zero case. */
125             warn_report("Deprecated CPU topology (considered invalid): "
126                         "Unsupported clusters parameter mustn't be "
127                         "specified as 1");
128         }
129     }
130     clusters = clusters > 0 ? clusters : 1;
131 
132     if (!mc->smp_props.dies_supported && config->has_dies) {
133         if (config->dies > 1) {
134             error_setg(errp, "dies not supported by this "
135                        "machine's CPU topology");
136             return;
137         } else {
138             /* Here dies only equals 1 since we've checked zero case. */
139             warn_report("Deprecated CPU topology (considered invalid): "
140                         "Unsupported dies parameter mustn't be "
141                         "specified as 1");
142         }
143     }
144     dies = dies > 0 ? dies : 1;
145 
146     if (!mc->smp_props.books_supported && config->has_books) {
147         if (config->books > 1) {
148             error_setg(errp, "books not supported by this "
149                        "machine's CPU topology");
150             return;
151         } else {
152             /* Here books only equals 1 since we've checked zero case. */
153             warn_report("Deprecated CPU topology (considered invalid): "
154                         "Unsupported books parameter mustn't be "
155                         "specified as 1");
156         }
157     }
158     books = books > 0 ? books : 1;
159 
160     if (!mc->smp_props.drawers_supported && config->has_drawers) {
161         if (config->drawers > 1) {
162             error_setg(errp, "drawers not supported by this "
163                        "machine's CPU topology");
164             return;
165         } else {
166             /* Here drawers only equals 1 since we've checked zero case. */
167             warn_report("Deprecated CPU topology (considered invalid): "
168                         "Unsupported drawers parameter mustn't be "
169                         "specified as 1");
170         }
171     }
172     drawers = drawers > 0 ? drawers : 1;
173 
174     /* compute missing values based on the provided ones */
175     if (cpus == 0 && maxcpus == 0) {
176         sockets = sockets > 0 ? sockets : 1;
177         cores = cores > 0 ? cores : 1;
178         threads = threads > 0 ? threads : 1;
179     } else {
180         maxcpus = maxcpus > 0 ? maxcpus : cpus;
181 
182         if (mc->smp_props.prefer_sockets) {
183             /* prefer sockets over cores before 6.2 */
184             if (sockets == 0) {
185                 cores = cores > 0 ? cores : 1;
186                 threads = threads > 0 ? threads : 1;
187                 sockets = maxcpus /
188                           (drawers * books * dies * clusters * cores * threads);
189             } else if (cores == 0) {
190                 threads = threads > 0 ? threads : 1;
191                 cores = maxcpus /
192                         (drawers * books * sockets * dies * clusters * threads);
193             }
194         } else {
195             /* prefer cores over sockets since 6.2 */
196             if (cores == 0) {
197                 sockets = sockets > 0 ? sockets : 1;
198                 threads = threads > 0 ? threads : 1;
199                 cores = maxcpus /
200                         (drawers * books * sockets * dies * clusters * threads);
201             } else if (sockets == 0) {
202                 threads = threads > 0 ? threads : 1;
203                 sockets = maxcpus /
204                           (drawers * books * dies * clusters * cores * threads);
205             }
206         }
207 
208         /* try to calculate omitted threads at last */
209         if (threads == 0) {
210             threads = maxcpus /
211                       (drawers * books * sockets * dies * clusters * cores);
212         }
213     }
214 
215     total_cpus = drawers * books * sockets * dies * clusters * cores * threads;
216     maxcpus = maxcpus > 0 ? maxcpus : total_cpus;
217     cpus = cpus > 0 ? cpus : maxcpus;
218 
219     ms->smp.cpus = cpus;
220     ms->smp.drawers = drawers;
221     ms->smp.books = books;
222     ms->smp.sockets = sockets;
223     ms->smp.dies = dies;
224     ms->smp.clusters = clusters;
225     ms->smp.cores = cores;
226     ms->smp.threads = threads;
227     ms->smp.max_cpus = maxcpus;
228 
229     mc->smp_props.has_clusters = config->has_clusters;
230 
231     /* sanity-check of the computed topology */
232     if (total_cpus != maxcpus) {
233         g_autofree char *topo_msg = cpu_hierarchy_to_string(ms);
234         error_setg(errp, "Invalid CPU topology: "
235                    "product of the hierarchy must match maxcpus: "
236                    "%s != maxcpus (%u)",
237                    topo_msg, maxcpus);
238         return;
239     }
240 
241     if (maxcpus < cpus) {
242         g_autofree char *topo_msg = cpu_hierarchy_to_string(ms);
243         error_setg(errp, "Invalid CPU topology: "
244                    "maxcpus must be equal to or greater than smp: "
245                    "%s == maxcpus (%u) < smp_cpus (%u)",
246                    topo_msg, maxcpus, cpus);
247         return;
248     }
249 
250     if (ms->smp.cpus < mc->min_cpus) {
251         error_setg(errp, "Invalid SMP CPUs %d. The min CPUs "
252                    "supported by machine '%s' is %d",
253                    ms->smp.cpus,
254                    mc->name, mc->min_cpus);
255         return;
256     }
257 
258     if (ms->smp.max_cpus > mc->max_cpus) {
259         error_setg(errp, "Invalid SMP CPUs %d. The max CPUs "
260                    "supported by machine '%s' is %d",
261                    ms->smp.max_cpus,
262                    mc->name, mc->max_cpus);
263         return;
264     }
265 }
266 
267 unsigned int machine_topo_get_cores_per_socket(const MachineState *ms)
268 {
269     return ms->smp.cores * ms->smp.clusters * ms->smp.dies;
270 }
271 
272 unsigned int machine_topo_get_threads_per_socket(const MachineState *ms)
273 {
274     return ms->smp.threads * machine_topo_get_cores_per_socket(ms);
275 }
276