2
0

machine-smp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. #include "qemu/osdep.h"
  20. #include "hw/boards.h"
  21. #include "qapi/error.h"
  22. #include "qemu/error-report.h"
  23. /*
  24. * Report information of a machine's supported CPU topology hierarchy.
  25. * Topology members will be ordered from the largest to the smallest
  26. * in the string.
  27. */
  28. static char *cpu_hierarchy_to_string(MachineState *ms)
  29. {
  30. MachineClass *mc = MACHINE_GET_CLASS(ms);
  31. GString *s = g_string_new(NULL);
  32. if (mc->smp_props.drawers_supported) {
  33. g_string_append_printf(s, "drawers (%u) * ", ms->smp.drawers);
  34. }
  35. if (mc->smp_props.books_supported) {
  36. g_string_append_printf(s, "books (%u) * ", ms->smp.books);
  37. }
  38. g_string_append_printf(s, "sockets (%u)", ms->smp.sockets);
  39. if (mc->smp_props.dies_supported) {
  40. g_string_append_printf(s, " * dies (%u)", ms->smp.dies);
  41. }
  42. if (mc->smp_props.clusters_supported) {
  43. g_string_append_printf(s, " * clusters (%u)", ms->smp.clusters);
  44. }
  45. if (mc->smp_props.modules_supported) {
  46. g_string_append_printf(s, " * modules (%u)", ms->smp.modules);
  47. }
  48. g_string_append_printf(s, " * cores (%u)", ms->smp.cores);
  49. g_string_append_printf(s, " * threads (%u)", ms->smp.threads);
  50. return g_string_free(s, false);
  51. }
  52. /*
  53. * machine_parse_smp_config: Generic function used to parse the given
  54. * SMP configuration
  55. *
  56. * Any missing parameter in "cpus/maxcpus/sockets/cores/threads" will be
  57. * automatically computed based on the provided ones.
  58. *
  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.
  62. *
  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.
  68. *
  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).
  72. */
  73. void machine_parse_smp_config(MachineState *ms,
  74. const SMPConfiguration *config, Error **errp)
  75. {
  76. MachineClass *mc = MACHINE_GET_CLASS(ms);
  77. unsigned cpus = config->has_cpus ? config->cpus : 0;
  78. unsigned drawers = config->has_drawers ? config->drawers : 0;
  79. unsigned books = config->has_books ? config->books : 0;
  80. unsigned sockets = config->has_sockets ? config->sockets : 0;
  81. unsigned dies = config->has_dies ? config->dies : 0;
  82. unsigned clusters = config->has_clusters ? config->clusters : 0;
  83. unsigned modules = config->has_modules ? config->modules : 0;
  84. unsigned cores = config->has_cores ? config->cores : 0;
  85. unsigned threads = config->has_threads ? config->threads : 0;
  86. unsigned maxcpus = config->has_maxcpus ? config->maxcpus : 0;
  87. unsigned total_cpus;
  88. /*
  89. * Specified CPU topology parameters must be greater than zero,
  90. * explicit configuration like "cpus=0" is not allowed.
  91. */
  92. if ((config->has_cpus && config->cpus == 0) ||
  93. (config->has_drawers && config->drawers == 0) ||
  94. (config->has_books && config->books == 0) ||
  95. (config->has_sockets && config->sockets == 0) ||
  96. (config->has_dies && config->dies == 0) ||
  97. (config->has_clusters && config->clusters == 0) ||
  98. (config->has_modules && config->modules == 0) ||
  99. (config->has_cores && config->cores == 0) ||
  100. (config->has_threads && config->threads == 0) ||
  101. (config->has_maxcpus && config->maxcpus == 0)) {
  102. error_setg(errp, "Invalid CPU topology: "
  103. "CPU topology parameters must be greater than zero");
  104. return;
  105. }
  106. /*
  107. * If not supported by the machine, a topology parameter must
  108. * not be set to a value greater than 1.
  109. */
  110. if (!mc->smp_props.modules_supported &&
  111. config->has_modules && config->modules > 1) {
  112. error_setg(errp,
  113. "modules > 1 not supported by this machine's CPU topology");
  114. return;
  115. }
  116. modules = modules > 0 ? modules : 1;
  117. if (!mc->smp_props.clusters_supported &&
  118. config->has_clusters && config->clusters > 1) {
  119. error_setg(errp,
  120. "clusters > 1 not supported by this machine's CPU topology");
  121. return;
  122. }
  123. clusters = clusters > 0 ? clusters : 1;
  124. if (!mc->smp_props.dies_supported &&
  125. config->has_dies && config->dies > 1) {
  126. error_setg(errp,
  127. "dies > 1 not supported by this machine's CPU topology");
  128. return;
  129. }
  130. dies = dies > 0 ? dies : 1;
  131. if (!mc->smp_props.books_supported &&
  132. config->has_books && config->books > 1) {
  133. error_setg(errp,
  134. "books > 1 not supported by this machine's CPU topology");
  135. return;
  136. }
  137. books = books > 0 ? books : 1;
  138. if (!mc->smp_props.drawers_supported &&
  139. config->has_drawers && config->drawers > 1) {
  140. error_setg(errp,
  141. "drawers > 1 not supported by this machine's CPU topology");
  142. return;
  143. }
  144. drawers = drawers > 0 ? drawers : 1;
  145. /* compute missing values based on the provided ones */
  146. if (cpus == 0 && maxcpus == 0) {
  147. sockets = sockets > 0 ? sockets : 1;
  148. cores = cores > 0 ? cores : 1;
  149. threads = threads > 0 ? threads : 1;
  150. } else {
  151. maxcpus = maxcpus > 0 ? maxcpus : cpus;
  152. if (mc->smp_props.prefer_sockets) {
  153. /* prefer sockets over cores before 6.2 */
  154. if (sockets == 0) {
  155. cores = cores > 0 ? cores : 1;
  156. threads = threads > 0 ? threads : 1;
  157. sockets = maxcpus /
  158. (drawers * books * dies * clusters *
  159. modules * cores * threads);
  160. } else if (cores == 0) {
  161. threads = threads > 0 ? threads : 1;
  162. cores = maxcpus /
  163. (drawers * books * sockets * dies *
  164. clusters * modules * threads);
  165. }
  166. } else {
  167. /* prefer cores over sockets since 6.2 */
  168. if (cores == 0) {
  169. sockets = sockets > 0 ? sockets : 1;
  170. threads = threads > 0 ? threads : 1;
  171. cores = maxcpus /
  172. (drawers * books * sockets * dies *
  173. clusters * modules * threads);
  174. } else if (sockets == 0) {
  175. threads = threads > 0 ? threads : 1;
  176. sockets = maxcpus /
  177. (drawers * books * dies * clusters *
  178. modules * cores * threads);
  179. }
  180. }
  181. /* try to calculate omitted threads at last */
  182. if (threads == 0) {
  183. threads = maxcpus /
  184. (drawers * books * sockets * dies *
  185. clusters * modules * cores);
  186. }
  187. }
  188. total_cpus = drawers * books * sockets * dies *
  189. clusters * modules * cores * threads;
  190. maxcpus = maxcpus > 0 ? maxcpus : total_cpus;
  191. cpus = cpus > 0 ? cpus : maxcpus;
  192. ms->smp.cpus = cpus;
  193. ms->smp.drawers = drawers;
  194. ms->smp.books = books;
  195. ms->smp.sockets = sockets;
  196. ms->smp.dies = dies;
  197. ms->smp.clusters = clusters;
  198. ms->smp.modules = modules;
  199. ms->smp.cores = cores;
  200. ms->smp.threads = threads;
  201. ms->smp.max_cpus = maxcpus;
  202. mc->smp_props.has_clusters = config->has_clusters;
  203. /* sanity-check of the computed topology */
  204. if (total_cpus != maxcpus) {
  205. g_autofree char *topo_msg = cpu_hierarchy_to_string(ms);
  206. error_setg(errp, "Invalid CPU topology: "
  207. "product of the hierarchy must match maxcpus: "
  208. "%s != maxcpus (%u)",
  209. topo_msg, maxcpus);
  210. return;
  211. }
  212. if (maxcpus < cpus) {
  213. g_autofree char *topo_msg = cpu_hierarchy_to_string(ms);
  214. error_setg(errp, "Invalid CPU topology: "
  215. "maxcpus must be equal to or greater than smp: "
  216. "%s == maxcpus (%u) < smp_cpus (%u)",
  217. topo_msg, maxcpus, cpus);
  218. return;
  219. }
  220. if (ms->smp.cpus < mc->min_cpus) {
  221. error_setg(errp, "Invalid SMP CPUs %d. The min CPUs "
  222. "supported by machine '%s' is %d",
  223. ms->smp.cpus,
  224. mc->name, mc->min_cpus);
  225. return;
  226. }
  227. if (ms->smp.max_cpus > mc->max_cpus) {
  228. error_setg(errp, "Invalid SMP CPUs %d. The max CPUs "
  229. "supported by machine '%s' is %d",
  230. ms->smp.max_cpus,
  231. mc->name, mc->max_cpus);
  232. return;
  233. }
  234. }
  235. bool machine_parse_smp_cache(MachineState *ms,
  236. const SmpCachePropertiesList *caches,
  237. Error **errp)
  238. {
  239. const SmpCachePropertiesList *node;
  240. DECLARE_BITMAP(caches_bitmap, CACHE_LEVEL_AND_TYPE__MAX);
  241. for (node = caches; node; node = node->next) {
  242. /* Prohibit users from repeating settings. */
  243. if (test_bit(node->value->cache, caches_bitmap)) {
  244. error_setg(errp,
  245. "Invalid cache properties: %s. "
  246. "The cache properties are duplicated",
  247. CacheLevelAndType_str(node->value->cache));
  248. return false;
  249. }
  250. machine_set_cache_topo_level(ms, node->value->cache,
  251. node->value->topology);
  252. set_bit(node->value->cache, caches_bitmap);
  253. }
  254. return true;
  255. }
  256. unsigned int machine_topo_get_cores_per_socket(const MachineState *ms)
  257. {
  258. return ms->smp.cores * ms->smp.modules * ms->smp.clusters * ms->smp.dies;
  259. }
  260. unsigned int machine_topo_get_threads_per_socket(const MachineState *ms)
  261. {
  262. return ms->smp.threads * machine_topo_get_cores_per_socket(ms);
  263. }
  264. CpuTopologyLevel machine_get_cache_topo_level(const MachineState *ms,
  265. CacheLevelAndType cache)
  266. {
  267. return ms->smp_cache.props[cache].topology;
  268. }
  269. void machine_set_cache_topo_level(MachineState *ms, CacheLevelAndType cache,
  270. CpuTopologyLevel level)
  271. {
  272. ms->smp_cache.props[cache].topology = level;
  273. }