machine-smp.c 7.7 KB

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