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 95 /* 96 * Specified CPU topology parameters must be greater than zero, 97 * explicit configuration like "cpus=0" is not allowed. 98 */ 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 error_setg(errp, "Invalid CPU topology: " 109 "CPU topology parameters must be greater than zero"); 110 return; 111 } 112 113 /* 114 * If not supported by the machine, a topology parameter must be 115 * omitted. 116 */ 117 if (!mc->smp_props.clusters_supported && config->has_clusters) { 118 if (config->clusters > 1) { 119 error_setg(errp, "clusters not supported by this " 120 "machine's CPU topology"); 121 return; 122 } else { 123 /* Here clusters only equals 1 since we've checked zero case. */ 124 warn_report("Deprecated CPU topology (considered invalid): " 125 "Unsupported clusters parameter mustn't be " 126 "specified as 1"); 127 } 128 } 129 clusters = clusters > 0 ? clusters : 1; 130 131 if (!mc->smp_props.dies_supported && config->has_dies) { 132 if (config->dies > 1) { 133 error_setg(errp, "dies not supported by this " 134 "machine's CPU topology"); 135 return; 136 } else { 137 /* Here dies only equals 1 since we've checked zero case. */ 138 warn_report("Deprecated CPU topology (considered invalid): " 139 "Unsupported dies parameter mustn't be " 140 "specified as 1"); 141 } 142 } 143 dies = dies > 0 ? dies : 1; 144 145 if (!mc->smp_props.books_supported && config->has_books) { 146 if (config->books > 1) { 147 error_setg(errp, "books not supported by this " 148 "machine's CPU topology"); 149 return; 150 } else { 151 /* Here books only equals 1 since we've checked zero case. */ 152 warn_report("Deprecated CPU topology (considered invalid): " 153 "Unsupported books parameter mustn't be " 154 "specified as 1"); 155 } 156 } 157 books = books > 0 ? books : 1; 158 159 if (!mc->smp_props.drawers_supported && config->has_drawers) { 160 if (config->drawers > 1) { 161 error_setg(errp, "drawers not supported by this " 162 "machine's CPU topology"); 163 return; 164 } else { 165 /* Here drawers only equals 1 since we've checked zero case. */ 166 warn_report("Deprecated CPU topology (considered invalid): " 167 "Unsupported drawers parameter mustn't be " 168 "specified as 1"); 169 } 170 } 171 drawers = drawers > 0 ? drawers : 1; 172 173 /* compute missing values based on the provided ones */ 174 if (cpus == 0 && maxcpus == 0) { 175 sockets = sockets > 0 ? sockets : 1; 176 cores = cores > 0 ? cores : 1; 177 threads = threads > 0 ? threads : 1; 178 } else { 179 maxcpus = maxcpus > 0 ? maxcpus : cpus; 180 181 if (mc->smp_props.prefer_sockets) { 182 /* prefer sockets over cores before 6.2 */ 183 if (sockets == 0) { 184 cores = cores > 0 ? cores : 1; 185 threads = threads > 0 ? threads : 1; 186 sockets = maxcpus / 187 (drawers * books * dies * clusters * cores * threads); 188 } else if (cores == 0) { 189 threads = threads > 0 ? threads : 1; 190 cores = maxcpus / 191 (drawers * books * sockets * dies * clusters * threads); 192 } 193 } else { 194 /* prefer cores over sockets since 6.2 */ 195 if (cores == 0) { 196 sockets = sockets > 0 ? sockets : 1; 197 threads = threads > 0 ? threads : 1; 198 cores = maxcpus / 199 (drawers * books * sockets * dies * clusters * threads); 200 } else if (sockets == 0) { 201 threads = threads > 0 ? threads : 1; 202 sockets = maxcpus / 203 (drawers * books * dies * clusters * cores * threads); 204 } 205 } 206 207 /* try to calculate omitted threads at last */ 208 if (threads == 0) { 209 threads = maxcpus / 210 (drawers * books * sockets * dies * clusters * cores); 211 } 212 } 213 214 maxcpus = maxcpus > 0 ? maxcpus : drawers * books * sockets * dies * 215 clusters * cores * threads; 216 cpus = cpus > 0 ? cpus : maxcpus; 217 218 ms->smp.cpus = cpus; 219 ms->smp.drawers = drawers; 220 ms->smp.books = books; 221 ms->smp.sockets = sockets; 222 ms->smp.dies = dies; 223 ms->smp.clusters = clusters; 224 ms->smp.cores = cores; 225 ms->smp.threads = threads; 226 ms->smp.max_cpus = maxcpus; 227 228 mc->smp_props.has_clusters = config->has_clusters; 229 230 /* sanity-check of the computed topology */ 231 if (drawers * books * sockets * dies * clusters * cores * threads != 232 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