machine: Set the value of cpus to match maxcpus if it's omitted

Currently we directly calculate the omitted cpus based on the given
incomplete collection of parameters. This makes some cmdlines like:
  -smp maxcpus=16
  -smp sockets=2,maxcpus=16
  -smp sockets=2,dies=2,maxcpus=16
  -smp sockets=2,cores=4,maxcpus=16
not work. We should probably set the value of cpus to match maxcpus
if it's omitted, which will make above configs start to work.

So the calculation logic of cpus/maxcpus after this patch will be:
When both maxcpus and cpus are omitted, maxcpus will be calculated
from the given parameters and cpus will be set equal to maxcpus.
When only one of maxcpus and cpus is given then the omitted one
will be set to its counterpart's value. Both maxcpus and cpus may
be specified, but maxcpus must be equal to or greater than cpus.

Note: change in this patch won't affect any existing working cmdlines
but allows more incomplete configs to be valid.

Signed-off-by: Yanan Wang <wangyanan55@huawei.com>
Reviewed-by: Andrew Jones <drjones@redhat.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
Message-Id: <20210929025816.21076-6-wangyanan55@huawei.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
Yanan Wang 2021-09-29 10:58:05 +08:00 committed by Paolo Bonzini
parent 9a52b50806
commit 7d8c5a3962
3 changed files with 40 additions and 29 deletions

View file

@ -727,25 +727,28 @@ static void pc_smp_parse(MachineState *ms, SMPConfiguration *config, Error **err
dies = dies > 0 ? dies : 1;
/* compute missing values, prefer sockets over cores over threads */
maxcpus = maxcpus > 0 ? maxcpus : cpus;
if (cpus == 0) {
if (cpus == 0 && maxcpus == 0) {
sockets = sockets > 0 ? sockets : 1;
cores = cores > 0 ? cores : 1;
threads = threads > 0 ? threads : 1;
cpus = sockets * dies * cores * threads;
} else {
maxcpus = maxcpus > 0 ? maxcpus : cpus;
} else if (sockets == 0) {
cores = cores > 0 ? cores : 1;
threads = threads > 0 ? threads : 1;
sockets = maxcpus / (dies * cores * threads);
} else if (cores == 0) {
threads = threads > 0 ? threads : 1;
cores = maxcpus / (sockets * dies * threads);
} else if (threads == 0) {
threads = maxcpus / (sockets * dies * cores);
if (sockets == 0) {
cores = cores > 0 ? cores : 1;
threads = threads > 0 ? threads : 1;
sockets = maxcpus / (dies * cores * threads);
} else if (cores == 0) {
threads = threads > 0 ? threads : 1;
cores = maxcpus / (sockets * dies * threads);
} else if (threads == 0) {
threads = maxcpus / (sockets * dies * cores);
}
}
maxcpus = maxcpus > 0 ? maxcpus : sockets * dies * cores * threads;
cpus = cpus > 0 ? cpus : maxcpus;
if (sockets * dies * cores * threads < cpus) {
error_setg(errp, "cpu topology: "
"sockets (%u) * dies (%u) * cores (%u) * threads (%u) < "