From nobody Thu May 16 02:45:20 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=philmd@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=philmd@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1635433774; cv=none; d=zohomail.com; s=zohoarc; b=G4hhkfux04w4zV4VReCDqjRyeerNth0LM2f9yr26iwkK9voPnDZ4kkpkPubvqw7vBy/AHk5pgjpSwJTMQ7xSwxUweuZK5+Fp0Doe43/z6hgj5LWcpxfQPZm8bySUh2etJ9IqU7nabM3MoQm4wp28/c9HfvovMskWaKHGz7BiclQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1635433774; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=FbOYartnyazvLTe6gb4OaRaZMz4D3pKWunSBNtEOXEI=; b=a2v032LRM9i0FRXXD7jRRiBRiPP2AraPsgCBC7Ee6VAqZxrR4S9ZLQe+zIS4e/K3CJwb+bgoy4BXUXKz1a6P70KeakDeDtqgPOj7S+p1/EKJYpdwKCxeBO290ITXmIJoZpCDiOV73Sg5tgYRZ/Q0lGwVkpkHa7/E17+Ubhzr+M4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=philmd@redhat.com; dmarc=pass header.from= (p=none dis=none) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1635433774141718.7446823155925; Thu, 28 Oct 2021 08:09:34 -0700 (PDT) Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-150-Id1Ym8SZPPmHGCgXI1PzpQ-1; Thu, 28 Oct 2021 11:09:22 -0400 Received: by mail-wm1-f71.google.com with SMTP id k5-20020a7bc3050000b02901e081f69d80so2498084wmj.8 for ; Thu, 28 Oct 2021 08:09:22 -0700 (PDT) Return-Path: Return-Path: Received: from x1w.redhat.com (62.red-83-57-168.dynamicip.rima-tde.net. [83.57.168.62]) by smtp.gmail.com with ESMTPSA id e18sm1823312wrs.48.2021.10.28.08.09.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Oct 2021 08:09:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1635433773; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=FbOYartnyazvLTe6gb4OaRaZMz4D3pKWunSBNtEOXEI=; b=TSCjYTTTb7xPGfNPHwlauv60DnTuxAkXuC4E4XzOWZf5RZcAjl4r/qGHpj9ict9Qt09/g0 psyUJaUNxdTveiRM1mSIAr+n9nw/iCeHuKWFplbDmQvOBfPOleW1wIIw0KTs3HKDqeT4X5 kordV/jgrkhfxRBtbWRK3Mm4t87NqSk= X-MC-Unique: Id1Ym8SZPPmHGCgXI1PzpQ-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=FbOYartnyazvLTe6gb4OaRaZMz4D3pKWunSBNtEOXEI=; b=ieXksE3yOlluWuPNvXp4amVsKpiTxOAq7v0XI1qRbDmDsyMYSUE4Jh5KroDez5r1la hPMvRUB+JNfHp8ZTcT9cg0aZnBVmUm1Rifw/E5lXjz1ZCFMwNST00h6lKS16Zzgfe1gY 74glaxHpX1qJsQ3lVIAGbBMzUsrHJxQSdyogfjgwn1ZptfUG3+V2ARyGHh+3bnkCtzov jAik4EJV5edbKusbzbeFjkC+BGKSjF9zjOM/fIEKmx5jVKT0VbU99TaguUV6lEhKgaoc 9oBvfsf3d5YdQcb4uP6J3KCDVCXPesH8psXgdRRf25+WqgEwZnEV3zEJed3liLL/mh1D LD2g== X-Gm-Message-State: AOAM532NVde/svQ7HCKlXlvyJ1YoJu74jlAtAZhwzUdujSKku9qmd5Ka 4QLWHR6RRdpmemjwkv67a9iDHYiE43c5JSjEKfck5mJkJBO8wSL3edzv5fIi5ytRU4oiMeOInJ2 YWbK8H9ZUGefyxQ== X-Received: by 2002:adf:d0cf:: with SMTP id z15mr6483741wrh.219.1635433760931; Thu, 28 Oct 2021 08:09:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxs7DCe4S+9byLTzNovwCMJBvkj/E+tT2At0BRdybwFe0nD3kWtyVgiSYhDmJPXAZHTfVyIeA== X-Received: by 2002:adf:d0cf:: with SMTP id z15mr6483681wrh.219.1635433760608; Thu, 28 Oct 2021 08:09:20 -0700 (PDT) From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Marcel Apfelbaum , Eduardo Habkost , Yanan Wang , Andrew Jones Subject: [PATCH v4 1/2] hw/core/machine: Split out the smp parsing code Date: Thu, 28 Oct 2021 17:09:12 +0200 Message-Id: <20211028150913.1975305-2-philmd@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20211028150913.1975305-1-philmd@redhat.com> References: <20211028150913.1975305-1-philmd@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=philmd@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1635433775456100001 From: Yanan Wang We are going to introduce an unit test for the parser smp_parse() in hw/core/machine.c, but now machine.c is only built in softmmu. In order to solve the build dependency on the smp parsing code and avoid building unrelated stuff for the unit tests, move the tested code from machine.c into a separate file, i.e., machine-smp.c and build it in common field. Signed-off-by: Yanan Wang Reviewed-by: Andrew Jones Reviewed-by: Philippe Mathieu-Daud=C3=A9 Tested-by: Philippe Mathieu-Daud=C3=A9 Message-Id: <20211026034659.22040-2-wangyanan55@huawei.com> Signed-off-by: Philippe Mathieu-Daud=C3=A9 Acked-by: Eduardo Habkost --- include/hw/boards.h | 1 + hw/core/machine-smp.c | 181 ++++++++++++++++++++++++++++++++++++++++++ hw/core/machine.c | 159 ------------------------------------- MAINTAINERS | 1 + hw/core/meson.build | 1 + 5 files changed, 184 insertions(+), 159 deletions(-) create mode 100644 hw/core/machine-smp.c diff --git a/include/hw/boards.h b/include/hw/boards.h index 5adbcbb99b1..e36fc7d8615 100644 --- a/include/hw/boards.h +++ b/include/hw/boards.h @@ -34,6 +34,7 @@ HotpluggableCPUList *machine_query_hotpluggable_cpus(Mach= ineState *machine); void machine_set_cpu_numa_node(MachineState *machine, const CpuInstanceProperties *props, Error **errp); +void smp_parse(MachineState *ms, SMPConfiguration *config, Error **errp); =20 /** * machine_class_allow_dynamic_sysbus_dev: Add type to list of valid devic= es diff --git a/hw/core/machine-smp.c b/hw/core/machine-smp.c new file mode 100644 index 00000000000..116a0cbbfab --- /dev/null +++ b/hw/core/machine-smp.c @@ -0,0 +1,181 @@ +/* + * QEMU Machine core (related to -smp parsing) + * + * Copyright (c) 2021 Huawei Technologies Co., Ltd + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, + * or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + +#include "qemu/osdep.h" +#include "hw/boards.h" +#include "qapi/error.h" + +/* + * Report information of a machine's supported CPU topology hierarchy. + * Topology members will be ordered from the largest to the smallest + * in the string. + */ +static char *cpu_hierarchy_to_string(MachineState *ms) +{ + MachineClass *mc =3D MACHINE_GET_CLASS(ms); + GString *s =3D g_string_new(NULL); + + g_string_append_printf(s, "sockets (%u)", ms->smp.sockets); + + if (mc->smp_props.dies_supported) { + g_string_append_printf(s, " * dies (%u)", ms->smp.dies); + } + + g_string_append_printf(s, " * cores (%u)", ms->smp.cores); + g_string_append_printf(s, " * threads (%u)", ms->smp.threads); + + return g_string_free(s, false); +} + +/* + * smp_parse - Generic function used to parse the given SMP configuration + * + * Any missing parameter in "cpus/maxcpus/sockets/cores/threads" will be + * automatically computed based on the provided ones. + * + * In the calculation of omitted sockets/cores/threads: we prefer sockets + * over cores over threads before 6.2, while preferring cores over sockets + * over threads since 6.2. + * + * In the calculation of cpus/maxcpus: When both maxcpus and cpus are omit= ted, + * maxcpus will be computed 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 given counterpart's value. Both maxcpus = and + * cpus may be specified, but maxcpus must be equal to or greater than cpu= s. + * + * For compatibility, apart from the parameters that will be computed, new= ly + * introduced topology members which are likely to be target specific shou= ld + * be directly set as 1 if they are omitted (e.g. dies for PC since 4.1). + */ +void smp_parse(MachineState *ms, SMPConfiguration *config, Error **errp) +{ + MachineClass *mc =3D MACHINE_GET_CLASS(ms); + unsigned cpus =3D config->has_cpus ? config->cpus : 0; + unsigned sockets =3D config->has_sockets ? config->sockets : 0; + unsigned dies =3D config->has_dies ? config->dies : 0; + unsigned cores =3D config->has_cores ? config->cores : 0; + unsigned threads =3D config->has_threads ? config->threads : 0; + unsigned maxcpus =3D config->has_maxcpus ? config->maxcpus : 0; + + /* + * Specified CPU topology parameters must be greater than zero, + * explicit configuration like "cpus=3D0" is not allowed. + */ + if ((config->has_cpus && config->cpus =3D=3D 0) || + (config->has_sockets && config->sockets =3D=3D 0) || + (config->has_dies && config->dies =3D=3D 0) || + (config->has_cores && config->cores =3D=3D 0) || + (config->has_threads && config->threads =3D=3D 0) || + (config->has_maxcpus && config->maxcpus =3D=3D 0)) { + warn_report("Deprecated CPU topology (considered invalid): " + "CPU topology parameters must be greater than zero"); + } + + /* + * If not supported by the machine, a topology parameter must be + * omitted or specified equal to 1. + */ + if (!mc->smp_props.dies_supported && dies > 1) { + error_setg(errp, "dies not supported by this machine's CPU topolog= y"); + return; + } + + dies =3D dies > 0 ? dies : 1; + + /* compute missing values based on the provided ones */ + if (cpus =3D=3D 0 && maxcpus =3D=3D 0) { + sockets =3D sockets > 0 ? sockets : 1; + cores =3D cores > 0 ? cores : 1; + threads =3D threads > 0 ? threads : 1; + } else { + maxcpus =3D maxcpus > 0 ? maxcpus : cpus; + + if (mc->smp_props.prefer_sockets) { + /* prefer sockets over cores before 6.2 */ + if (sockets =3D=3D 0) { + cores =3D cores > 0 ? cores : 1; + threads =3D threads > 0 ? threads : 1; + sockets =3D maxcpus / (dies * cores * threads); + } else if (cores =3D=3D 0) { + threads =3D threads > 0 ? threads : 1; + cores =3D maxcpus / (sockets * dies * threads); + } + } else { + /* prefer cores over sockets since 6.2 */ + if (cores =3D=3D 0) { + sockets =3D sockets > 0 ? sockets : 1; + threads =3D threads > 0 ? threads : 1; + cores =3D maxcpus / (sockets * dies * threads); + } else if (sockets =3D=3D 0) { + threads =3D threads > 0 ? threads : 1; + sockets =3D maxcpus / (dies * cores * threads); + } + } + + /* try to calculate omitted threads at last */ + if (threads =3D=3D 0) { + threads =3D maxcpus / (sockets * dies * cores); + } + } + + maxcpus =3D maxcpus > 0 ? maxcpus : sockets * dies * cores * threads; + cpus =3D cpus > 0 ? cpus : maxcpus; + + ms->smp.cpus =3D cpus; + ms->smp.sockets =3D sockets; + ms->smp.dies =3D dies; + ms->smp.cores =3D cores; + ms->smp.threads =3D threads; + ms->smp.max_cpus =3D maxcpus; + + /* sanity-check of the computed topology */ + if (sockets * dies * cores * threads !=3D maxcpus) { + g_autofree char *topo_msg =3D cpu_hierarchy_to_string(ms); + error_setg(errp, "Invalid CPU topology: " + "product of the hierarchy must match maxcpus: " + "%s !=3D maxcpus (%u)", + topo_msg, maxcpus); + return; + } + + if (maxcpus < cpus) { + g_autofree char *topo_msg =3D cpu_hierarchy_to_string(ms); + error_setg(errp, "Invalid CPU topology: " + "maxcpus must be equal to or greater than smp: " + "%s =3D=3D maxcpus (%u) < smp_cpus (%u)", + topo_msg, maxcpus, cpus); + return; + } + + if (ms->smp.cpus < mc->min_cpus) { + error_setg(errp, "Invalid SMP CPUs %d. The min CPUs " + "supported by machine '%s' is %d", + ms->smp.cpus, + mc->name, mc->min_cpus); + return; + } + + if (ms->smp.max_cpus > mc->max_cpus) { + error_setg(errp, "Invalid SMP CPUs %d. The max CPUs " + "supported by machine '%s' is %d", + ms->smp.max_cpus, + mc->name, mc->max_cpus); + return; + } +} diff --git a/hw/core/machine.c b/hw/core/machine.c index b8d95eec32d..dc15f5f9e5c 100644 --- a/hw/core/machine.c +++ b/hw/core/machine.c @@ -749,165 +749,6 @@ void machine_set_cpu_numa_node(MachineState *machine, } } =20 -/* - * Report information of a machine's supported CPU topology hierarchy. - * Topology members will be ordered from the largest to the smallest - * in the string. - */ -static char *cpu_hierarchy_to_string(MachineState *ms) -{ - MachineClass *mc =3D MACHINE_GET_CLASS(ms); - GString *s =3D g_string_new(NULL); - - g_string_append_printf(s, "sockets (%u)", ms->smp.sockets); - - if (mc->smp_props.dies_supported) { - g_string_append_printf(s, " * dies (%u)", ms->smp.dies); - } - - g_string_append_printf(s, " * cores (%u)", ms->smp.cores); - g_string_append_printf(s, " * threads (%u)", ms->smp.threads); - - return g_string_free(s, false); -} - -/* - * smp_parse - Generic function used to parse the given SMP configuration - * - * Any missing parameter in "cpus/maxcpus/sockets/cores/threads" will be - * automatically computed based on the provided ones. - * - * In the calculation of omitted sockets/cores/threads: we prefer sockets - * over cores over threads before 6.2, while preferring cores over sockets - * over threads since 6.2. - * - * In the calculation of cpus/maxcpus: When both maxcpus and cpus are omit= ted, - * maxcpus will be computed 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 given counterpart's value. Both maxcpus = and - * cpus may be specified, but maxcpus must be equal to or greater than cpu= s. - * - * For compatibility, apart from the parameters that will be computed, new= ly - * introduced topology members which are likely to be target specific shou= ld - * be directly set as 1 if they are omitted (e.g. dies for PC since 4.1). - */ -static void smp_parse(MachineState *ms, SMPConfiguration *config, Error **= errp) -{ - MachineClass *mc =3D MACHINE_GET_CLASS(ms); - unsigned cpus =3D config->has_cpus ? config->cpus : 0; - unsigned sockets =3D config->has_sockets ? config->sockets : 0; - unsigned dies =3D config->has_dies ? config->dies : 0; - unsigned cores =3D config->has_cores ? config->cores : 0; - unsigned threads =3D config->has_threads ? config->threads : 0; - unsigned maxcpus =3D config->has_maxcpus ? config->maxcpus : 0; - - /* - * Specified CPU topology parameters must be greater than zero, - * explicit configuration like "cpus=3D0" is not allowed. - */ - if ((config->has_cpus && config->cpus =3D=3D 0) || - (config->has_sockets && config->sockets =3D=3D 0) || - (config->has_dies && config->dies =3D=3D 0) || - (config->has_cores && config->cores =3D=3D 0) || - (config->has_threads && config->threads =3D=3D 0) || - (config->has_maxcpus && config->maxcpus =3D=3D 0)) { - warn_report("Deprecated CPU topology (considered invalid): " - "CPU topology parameters must be greater than zero"); - } - - /* - * If not supported by the machine, a topology parameter must be - * omitted or specified equal to 1. - */ - if (!mc->smp_props.dies_supported && dies > 1) { - error_setg(errp, "dies not supported by this machine's CPU topolog= y"); - return; - } - - dies =3D dies > 0 ? dies : 1; - - /* compute missing values based on the provided ones */ - if (cpus =3D=3D 0 && maxcpus =3D=3D 0) { - sockets =3D sockets > 0 ? sockets : 1; - cores =3D cores > 0 ? cores : 1; - threads =3D threads > 0 ? threads : 1; - } else { - maxcpus =3D maxcpus > 0 ? maxcpus : cpus; - - if (mc->smp_props.prefer_sockets) { - /* prefer sockets over cores before 6.2 */ - if (sockets =3D=3D 0) { - cores =3D cores > 0 ? cores : 1; - threads =3D threads > 0 ? threads : 1; - sockets =3D maxcpus / (dies * cores * threads); - } else if (cores =3D=3D 0) { - threads =3D threads > 0 ? threads : 1; - cores =3D maxcpus / (sockets * dies * threads); - } - } else { - /* prefer cores over sockets since 6.2 */ - if (cores =3D=3D 0) { - sockets =3D sockets > 0 ? sockets : 1; - threads =3D threads > 0 ? threads : 1; - cores =3D maxcpus / (sockets * dies * threads); - } else if (sockets =3D=3D 0) { - threads =3D threads > 0 ? threads : 1; - sockets =3D maxcpus / (dies * cores * threads); - } - } - - /* try to calculate omitted threads at last */ - if (threads =3D=3D 0) { - threads =3D maxcpus / (sockets * dies * cores); - } - } - - maxcpus =3D maxcpus > 0 ? maxcpus : sockets * dies * cores * threads; - cpus =3D cpus > 0 ? cpus : maxcpus; - - ms->smp.cpus =3D cpus; - ms->smp.sockets =3D sockets; - ms->smp.dies =3D dies; - ms->smp.cores =3D cores; - ms->smp.threads =3D threads; - ms->smp.max_cpus =3D maxcpus; - - /* sanity-check of the computed topology */ - if (sockets * dies * cores * threads !=3D maxcpus) { - g_autofree char *topo_msg =3D cpu_hierarchy_to_string(ms); - error_setg(errp, "Invalid CPU topology: " - "product of the hierarchy must match maxcpus: " - "%s !=3D maxcpus (%u)", - topo_msg, maxcpus); - return; - } - - if (maxcpus < cpus) { - g_autofree char *topo_msg =3D cpu_hierarchy_to_string(ms); - error_setg(errp, "Invalid CPU topology: " - "maxcpus must be equal to or greater than smp: " - "%s =3D=3D maxcpus (%u) < smp_cpus (%u)", - topo_msg, maxcpus, cpus); - return; - } - - if (ms->smp.cpus < mc->min_cpus) { - error_setg(errp, "Invalid SMP CPUs %d. The min CPUs " - "supported by machine '%s' is %d", - ms->smp.cpus, - mc->name, mc->min_cpus); - return; - } - - if (ms->smp.max_cpus > mc->max_cpus) { - error_setg(errp, "Invalid SMP CPUs %d. The max CPUs " - "supported by machine '%s' is %d", - ms->smp.max_cpus, - mc->name, mc->max_cpus); - return; - } -} - static void machine_get_smp(Object *obj, Visitor *v, const char *name, void *opaque, Error **errp) { diff --git a/MAINTAINERS b/MAINTAINERS index 894dc431052..80ec27d76ae 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -1623,6 +1623,7 @@ F: cpu.c F: hw/core/cpu.c F: hw/core/machine-qmp-cmds.c F: hw/core/machine.c +F: hw/core/machine-smp.c F: hw/core/null-machine.c F: hw/core/numa.c F: hw/cpu/cluster.c diff --git a/hw/core/meson.build b/hw/core/meson.build index afc5ed2c906..987bb118c3d 100644 --- a/hw/core/meson.build +++ b/hw/core/meson.build @@ -23,6 +23,7 @@ endif =20 common_ss.add(files('cpu-common.c')) +common_ss.add(files('machine-smp.c')) softmmu_ss.add(when: 'CONFIG_FITLOADER', if_true: files('loader-fit.c')) softmmu_ss.add(when: 'CONFIG_GENERIC_LOADER', if_true: files('generic-load= er.c')) softmmu_ss.add(when: ['CONFIG_GUEST_LOADER', fdt], if_true: files('guest-l= oader.c')) --=20 2.31.1 From nobody Thu May 16 02:45:20 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) client-ip=170.10.129.124; envelope-from=philmd@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=philmd@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1635433770; cv=none; d=zohomail.com; s=zohoarc; b=UUYtv4QxYpbE5vop5mAqF0Isxwk/CXVPW/8UnHZ5v1ABi1wbwfxV8KfSe3SaZO8NhE0qaSwGhkFQxSoDhiJysd2rHcHhEu+I0WBAuHoZ1Uy7IKOq+ALjxaHkFEWnRfnq/lvN/ksxL2f5pTizegAc+wOGVaQ6teZQ7Ch00ehZtQ4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1635433770; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=pOwIV9AKh3dkEprLXlRUpFWEOjomo1dJQhd4OWA0gNY=; b=Qaxw9x243JdR14NoPgao2oxMWqGL8SUlpHt/ciruco2qNfj3FPnPBLGjWSOj9pQRlOpG+PPCc5BKsiTXiuihBraOdVIElDmqeTXPkVO2aJzBC27iLmpUoF/5+A4F9anmY6t3tng+ZO+SLO0wjklmmW6B5yeeOdQGjTNNFwRjz7U= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=philmd@redhat.com; dmarc=pass header.from= (p=none dis=none) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mx.zohomail.com with SMTPS id 1635433770140126.39221282183644; Thu, 28 Oct 2021 08:09:30 -0700 (PDT) Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-19-HZGMRD8fMxOMZBUUAtRtMw-1; Thu, 28 Oct 2021 11:09:27 -0400 Received: by mail-wm1-f72.google.com with SMTP id b79-20020a1c1b52000000b0032328337393so2836434wmb.1 for ; Thu, 28 Oct 2021 08:09:27 -0700 (PDT) Return-Path: Return-Path: Received: from x1w.redhat.com (62.red-83-57-168.dynamicip.rima-tde.net. [83.57.168.62]) by smtp.gmail.com with ESMTPSA id q1sm2806674wmj.20.2021.10.28.08.09.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Oct 2021 08:09:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1635433769; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=pOwIV9AKh3dkEprLXlRUpFWEOjomo1dJQhd4OWA0gNY=; b=LdHdlcdCfBgDzVruzKpCeOs3nlfhqysX2fqny+lGFB46h6LE9aWZQyzlOTChJCvMBwpcG+ dHxBemndM/wTwMbe4RbJYk6EYwMpe+LeTkr1YL+H/8Zj0sf8QJzAry/cVYH+YYfkZgFVWQ +nV5M6btbkLdOdNFSQES4oM2pi29TT4= X-MC-Unique: HZGMRD8fMxOMZBUUAtRtMw-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=pOwIV9AKh3dkEprLXlRUpFWEOjomo1dJQhd4OWA0gNY=; b=ucj3ocbpPaMHkueRyKtVfXn76L7SE+8RHMiUmt0uw0LLgYmnu5++3rc/w/1V6xW0/D ONHOUwhZ5wq3NUtHvoCuPDC516Yc7nsBSNGe8jq8BZMP9ROM4TW7y8wH1ee31wG/uyNL Fqu9WqaNjIxP2j8AJuDXFnjKygH6JGUkxrEIWOojQcXbohQE0EbqBBNdZP4KJQLvsaf6 2SV9jAnOBgsBWT9T6KGRehlSu9h6n+sKtOdi3u3c6qXGe6QC55gSSsmQzFnCDuImKPrk XvhpGD4KL4W9f6RZd/pY2i5+91DmoWVV3wlfDTw53yBIod9x8hnYk7lFjyF2rUvcTEQ2 DfTg== X-Gm-Message-State: AOAM532N7PV5OlMJnv728fmrjrmXggx21s6/G9Bq0DWOl9Z8VXajQtqA 3jGoo8OoEBJiVwPKdcJP3EIiBlZBu9CsGH32ZfF1sdVxpKyqmhv2eOUBAyMF5+75arfbvvnR3Dp PD6CgTCv3ChQEBQ== X-Received: by 2002:a7b:cd16:: with SMTP id f22mr13095994wmj.42.1635433765642; Thu, 28 Oct 2021 08:09:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyNClTkXfG2u27KYWTLCfuEaGaUkiwp9bXktrCNhMQFW8T4jju7jHX4tZvatW2qXzK4mL3XRQ== X-Received: by 2002:a7b:cd16:: with SMTP id f22mr13095943wmj.42.1635433765301; Thu, 28 Oct 2021 08:09:25 -0700 (PDT) From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Marcel Apfelbaum , Eduardo Habkost , Yanan Wang , Andrew Jones Subject: [PATCH v4 2/2] tests/unit: Add an unit test for smp parsing Date: Thu, 28 Oct 2021 17:09:13 +0200 Message-Id: <20211028150913.1975305-3-philmd@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20211028150913.1975305-1-philmd@redhat.com> References: <20211028150913.1975305-1-philmd@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=philmd@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1635433771286100001 From: Yanan Wang Now that we have a generic parser smp_parse(), let's add an unit test for the code. All possible valid/invalid SMP configurations that the user can specify are covered. Signed-off-by: Yanan Wang Reviewed-by: Andrew Jones Tested-by: Philippe Mathieu-Daud=C3=A9 Message-Id: <20211026034659.22040-3-wangyanan55@huawei.com> Signed-off-by: Philippe Mathieu-Daud=C3=A9 Acked-by: Eduardo Habkost --- tests/unit/test-smp-parse.c | 594 ++++++++++++++++++++++++++++++++++++ MAINTAINERS | 1 + tests/unit/meson.build | 1 + 3 files changed, 596 insertions(+) create mode 100644 tests/unit/test-smp-parse.c diff --git a/tests/unit/test-smp-parse.c b/tests/unit/test-smp-parse.c new file mode 100644 index 00000000000..e96abe9ba46 --- /dev/null +++ b/tests/unit/test-smp-parse.c @@ -0,0 +1,594 @@ +/* + * SMP parsing unit-tests + * + * Copyright (c) 2021 Huawei Technologies Co., Ltd + * + * Authors: + * Yanan Wang + * + * This work is licensed under the terms of the GNU LGPL, version 2.1 or l= ater. + * See the COPYING.LIB file in the top-level directory. + */ + +#include "qemu/osdep.h" +#include "qom/object.h" +#include "qemu/module.h" +#include "qapi/error.h" + +#include "hw/boards.h" + +#define T true +#define F false + +#define MIN_CPUS 1 /* set the min CPUs supported by the machine as 1 */ +#define MAX_CPUS 512 /* set the max CPUs supported by the machine as 512 */ + +/* + * Used to define the generic 3-level CPU topology hierarchy + * -sockets/cores/threads + */ +#define SMP_CONFIG_GENERIC(ha, a, hb, b, hc, c, hd, d, he, e) \ + { \ + .has_cpus =3D ha, .cpus =3D a, \ + .has_sockets =3D hb, .sockets =3D b, \ + .has_cores =3D hc, .cores =3D c, \ + .has_threads =3D hd, .threads =3D d, \ + .has_maxcpus =3D he, .maxcpus =3D e, \ + } + +#define CPU_TOPOLOGY_GENERIC(a, b, c, d, e) \ + { \ + .cpus =3D a, \ + .sockets =3D b, \ + .cores =3D c, \ + .threads =3D d, \ + .max_cpus =3D e, \ + } + +/* + * Currently a 4-level topology hierarchy is supported on PC machines + * -sockets/dies/cores/threads + */ +#define SMP_CONFIG_WITH_DIES(ha, a, hb, b, hc, c, hd, d, he, e, hf, f) \ + { \ + .has_cpus =3D ha, .cpus =3D a, \ + .has_sockets =3D hb, .sockets =3D b, \ + .has_dies =3D hc, .dies =3D c, \ + .has_cores =3D hd, .cores =3D d, \ + .has_threads =3D he, .threads =3D e, \ + .has_maxcpus =3D hf, .maxcpus =3D f, \ + } + +/** + * @config - the given SMP configuration + * @expect_prefer_sockets - the expected parsing result for the + * valid configuration, when sockets are preferred over cores + * @expect_prefer_cores - the expected parsing result for the + * valid configuration, when cores are preferred over sockets + * @expect_error - the expected error report when the given + * configuration is invalid + */ +typedef struct SMPTestData { + SMPConfiguration config; + CpuTopology expect_prefer_sockets; + CpuTopology expect_prefer_cores; + const char *expect_error; +} SMPTestData; + +/* Type info of the tested machine */ +static const TypeInfo smp_machine_info =3D { + .name =3D TYPE_MACHINE, + .parent =3D TYPE_OBJECT, + .class_size =3D sizeof(MachineClass), + .instance_size =3D sizeof(MachineState), +}; + +/* + * List all the possible valid sub-collections of the generic 5 + * topology parameters (i.e. cpus/maxcpus/sockets/cores/threads), + * then test the automatic calculation algorithm of the missing + * values in the parser. + */ +static struct SMPTestData data_generic_valid[] =3D { + { + /* config: no configuration provided + * expect: cpus=3D1,sockets=3D1,cores=3D1,threads=3D1,maxcpus=3D1 = */ + .config =3D SMP_CONFIG_GENERIC(F, 0, F, 0, F, 0, F, 0, F, 0), + .expect_prefer_sockets =3D CPU_TOPOLOGY_GENERIC(1, 1, 1, 1, 1), + .expect_prefer_cores =3D CPU_TOPOLOGY_GENERIC(1, 1, 1, 1, 1), + }, { + /* config: -smp 8 + * prefer_sockets: cpus=3D8,sockets=3D8,cores=3D1,threads=3D1,maxc= pus=3D8 + * prefer_cores: cpus=3D8,sockets=3D1,cores=3D8,threads=3D1,maxcpu= s=3D8 */ + .config =3D SMP_CONFIG_GENERIC(T, 8, F, 0, F, 0, F, 0, F, 0), + .expect_prefer_sockets =3D CPU_TOPOLOGY_GENERIC(8, 8, 1, 1, 8), + .expect_prefer_cores =3D CPU_TOPOLOGY_GENERIC(8, 1, 8, 1, 8), + }, { + /* config: -smp sockets=3D2 + * expect: cpus=3D2,sockets=3D2,cores=3D1,threads=3D1,maxcpus=3D2 = */ + .config =3D SMP_CONFIG_GENERIC(F, 0, T, 2, F, 0, F, 0, F, 0), + .expect_prefer_sockets =3D CPU_TOPOLOGY_GENERIC(2, 2, 1, 1, 2), + .expect_prefer_cores =3D CPU_TOPOLOGY_GENERIC(2, 2, 1, 1, 2), + }, { + /* config: -smp cores=3D4 + * expect: cpus=3D4,sockets=3D1,cores=3D4,threads=3D1,maxcpus=3D4 = */ + .config =3D SMP_CONFIG_GENERIC(F, 0, F, 0, T, 4, F, 0, F, 0), + .expect_prefer_sockets =3D CPU_TOPOLOGY_GENERIC(4, 1, 4, 1, 4), + .expect_prefer_cores =3D CPU_TOPOLOGY_GENERIC(4, 1, 4, 1, 4), + }, { + /* config: -smp threads=3D2 + * expect: cpus=3D2,sockets=3D1,cores=3D1,threads=3D2,maxcpus=3D2 = */ + .config =3D SMP_CONFIG_GENERIC(F, 0, F, 0, F, 0, T, 2, F, 0), + .expect_prefer_sockets =3D CPU_TOPOLOGY_GENERIC(2, 1, 1, 2, 2), + .expect_prefer_cores =3D CPU_TOPOLOGY_GENERIC(2, 1, 1, 2, 2), + }, { + /* config: -smp maxcpus=3D16 + * prefer_sockets: cpus=3D16,sockets=3D16,cores=3D1,threads=3D1,ma= xcpus=3D16 + * prefer_cores: cpus=3D16,sockets=3D1,cores=3D16,threads=3D1,maxc= pus=3D16 */ + .config =3D SMP_CONFIG_GENERIC(F, 0, F, 0, F, 0, F, 0, T, 16), + .expect_prefer_sockets =3D CPU_TOPOLOGY_GENERIC(16, 16, 1, 1, 16), + .expect_prefer_cores =3D CPU_TOPOLOGY_GENERIC(16, 1, 16, 1, 16), + }, { + /* config: -smp 8,sockets=3D2 + * expect: cpus=3D8,sockets=3D2,cores=3D4,threads=3D1,maxcpus=3D8 = */ + .config =3D SMP_CONFIG_GENERIC(T, 8, T, 2, F, 0, F, 0, F, 0), + .expect_prefer_sockets =3D CPU_TOPOLOGY_GENERIC(8, 2, 4, 1, 8), + .expect_prefer_cores =3D CPU_TOPOLOGY_GENERIC(8, 2, 4, 1, 8), + }, { + /* config: -smp 8,cores=3D4 + * expect: cpus=3D8,sockets=3D2,cores=3D4,threads=3D1,maxcpus=3D8 = */ + .config =3D SMP_CONFIG_GENERIC(T, 8, F, 0, T, 4, F, 0, F, 0), + .expect_prefer_sockets =3D CPU_TOPOLOGY_GENERIC(8, 2, 4, 1, 8), + .expect_prefer_cores =3D CPU_TOPOLOGY_GENERIC(8, 2, 4, 1, 8), + }, { + /* config: -smp 8,threads=3D2 + * prefer_sockets: cpus=3D8,sockets=3D4,cores=3D1,threads=3D2,maxc= pus=3D8 + * prefer_cores: cpus=3D8,sockets=3D1,cores=3D4,threads=3D2,maxcpu= s=3D8 */ + .config =3D SMP_CONFIG_GENERIC(T, 8, F, 0, F, 0, T, 2, F, 0), + .expect_prefer_sockets =3D CPU_TOPOLOGY_GENERIC(8, 4, 1, 2, 8), + .expect_prefer_cores =3D CPU_TOPOLOGY_GENERIC(8, 1, 4, 2, 8), + }, { + /* config: -smp 8,maxcpus=3D16 + * prefer_sockets: cpus=3D8,sockets=3D16,cores=3D1,threads=3D1,max= cpus=3D16 + * prefer_cores: cpus=3D8,sockets=3D1,cores=3D16,threads=3D1,maxcp= us=3D16 */ + .config =3D SMP_CONFIG_GENERIC(T, 8, F, 0, F, 0, F, 0, T, 16), + .expect_prefer_sockets =3D CPU_TOPOLOGY_GENERIC(8, 16, 1, 1, 16), + .expect_prefer_cores =3D CPU_TOPOLOGY_GENERIC(8, 1, 16, 1, 16), + }, { + /* config: -smp sockets=3D2,cores=3D4 + * expect: cpus=3D8,sockets=3D2,cores=3D4,threads=3D1,maxcpus=3D8 = */ + .config =3D SMP_CONFIG_GENERIC(F, 0, T, 2, T, 4, F, 0, F, 0), + .expect_prefer_sockets =3D CPU_TOPOLOGY_GENERIC(8, 2, 4, 1, 8), + .expect_prefer_cores =3D CPU_TOPOLOGY_GENERIC(8, 2, 4, 1, 8), + }, { + /* config: -smp sockets=3D2,threads=3D2 + * expect: cpus=3D4,sockets=3D2,cores=3D1,threads=3D2,maxcpus=3D4 = */ + .config =3D SMP_CONFIG_GENERIC(F, 0, T, 2, F, 0, T, 2, F, 0), + .expect_prefer_sockets =3D CPU_TOPOLOGY_GENERIC(4, 2, 1, 2, 4), + .expect_prefer_cores =3D CPU_TOPOLOGY_GENERIC(4, 2, 1, 2, 4), + }, { + /* config: -smp sockets=3D2,maxcpus=3D16 + * expect: cpus=3D16,sockets=3D2,cores=3D8,threads=3D1,maxcpus=3D1= 6 */ + .config =3D SMP_CONFIG_GENERIC(F, 0, T, 2, F, 0, F, 0, T, 16), + .expect_prefer_sockets =3D CPU_TOPOLOGY_GENERIC(16, 2, 8, 1, 16), + .expect_prefer_cores =3D CPU_TOPOLOGY_GENERIC(16, 2, 8, 1, 16), + }, { + /* config: -smp cores=3D4,threads=3D2 + * expect: cpus=3D8,sockets=3D1,cores=3D4,threads=3D2,maxcpus=3D8 = */ + .config =3D SMP_CONFIG_GENERIC(F, 0, F, 0, T, 4, T, 2, F, 0), + .expect_prefer_sockets =3D CPU_TOPOLOGY_GENERIC(8, 1, 4, 2, 8), + .expect_prefer_cores =3D CPU_TOPOLOGY_GENERIC(8, 1, 4, 2, 8), + }, { + /* config: -smp cores=3D4,maxcpus=3D16 + * expect: cpus=3D16,sockets=3D4,cores=3D4,threads=3D1,maxcpus=3D1= 6 */ + .config =3D SMP_CONFIG_GENERIC(F, 0, F, 0, T, 4, F, 0, T, 16), + .expect_prefer_sockets =3D CPU_TOPOLOGY_GENERIC(16, 4, 4, 1, 16), + .expect_prefer_cores =3D CPU_TOPOLOGY_GENERIC(16, 4, 4, 1, 16), + }, { + /* config: -smp threads=3D2,maxcpus=3D16 + * prefer_sockets: cpus=3D16,sockets=3D8,cores=3D1,threads=3D2,max= cpus=3D16 + * prefer_cores: cpus=3D16,sockets=3D1,cores=3D8,threads=3D2,maxcp= us=3D16 */ + .config =3D SMP_CONFIG_GENERIC(F, 0, F, 0, F, 0, T, 2, T, 16), + .expect_prefer_sockets =3D CPU_TOPOLOGY_GENERIC(16, 8, 1, 2, 16), + .expect_prefer_cores =3D CPU_TOPOLOGY_GENERIC(16, 1, 8, 2, 16), + }, { + /* config: -smp 8,sockets=3D2,cores=3D4 + * expect: cpus=3D8,sockets=3D2,cores=3D4,threads=3D1,maxcpus=3D8 = */ + .config =3D SMP_CONFIG_GENERIC(T, 8, T, 2, T, 4, F, 0, F, 0), + .expect_prefer_sockets =3D CPU_TOPOLOGY_GENERIC(8, 2, 4, 1, 8), + .expect_prefer_cores =3D CPU_TOPOLOGY_GENERIC(8, 2, 4, 1, 8), + }, { + /* config: -smp 8,sockets=3D2,threads=3D2 + * expect: cpus=3D8,sockets=3D2,cores=3D2,threads=3D2,maxcpus=3D8 = */ + .config =3D SMP_CONFIG_GENERIC(T, 8, T, 2, F, 0, T, 2, F, 0), + .expect_prefer_sockets =3D CPU_TOPOLOGY_GENERIC(8, 2, 2, 2, 8), + .expect_prefer_cores =3D CPU_TOPOLOGY_GENERIC(8, 2, 2, 2, 8), + }, { + /* config: -smp 8,sockets=3D2,maxcpus=3D16 + * expect: cpus=3D8,sockets=3D2,cores=3D8,threads=3D1,maxcpus=3D16= */ + .config =3D SMP_CONFIG_GENERIC(T, 8, T, 2, F, 0, F, 0, T, 16), + .expect_prefer_sockets =3D CPU_TOPOLOGY_GENERIC(8, 2, 8, 1, 16), + .expect_prefer_cores =3D CPU_TOPOLOGY_GENERIC(8, 2, 8, 1, 16), + }, { + /* config: -smp 8,cores=3D4,threads=3D2 + * expect: cpus=3D8,sockets=3D1,cores=3D4,threads=3D2,maxcpus=3D8 = */ + .config =3D SMP_CONFIG_GENERIC(T, 8, F, 0, T, 4, T, 2, F, 0), + .expect_prefer_sockets =3D CPU_TOPOLOGY_GENERIC(8, 1, 4, 2, 8), + .expect_prefer_cores =3D CPU_TOPOLOGY_GENERIC(8, 1, 4, 2, 8), + }, { + /* config: -smp 8,cores=3D4,maxcpus=3D16 + * expect: cpus=3D8,sockets=3D4,cores=3D4,threads=3D1,maxcpus=3D16= */ + .config =3D SMP_CONFIG_GENERIC(T, 8, F, 0, T, 4, F, 0, T, 16), + .expect_prefer_sockets =3D CPU_TOPOLOGY_GENERIC(8, 4, 4, 1, 16), + .expect_prefer_cores =3D CPU_TOPOLOGY_GENERIC(8, 4, 4, 1, 16), + }, { + /* config: -smp 8,threads=3D2,maxcpus=3D16 + * prefer_sockets: cpus=3D8,sockets=3D8,cores=3D1,threads=3D2,maxc= pus=3D16 + * prefer_cores: cpus=3D8,sockets=3D1,cores=3D8,threads=3D2,maxcpu= s=3D16 */ + .config =3D SMP_CONFIG_GENERIC(T, 8, F, 0, F, 0, T, 2, T, 16), + .expect_prefer_sockets =3D CPU_TOPOLOGY_GENERIC(8, 8, 1, 2, 16), + .expect_prefer_cores =3D CPU_TOPOLOGY_GENERIC(8, 1, 8, 2, 16), + }, { + /* config: -smp sockets=3D2,cores=3D4,threads=3D2 + * expect: cpus=3D16,sockets=3D2,cores=3D4,threads=3D2,maxcpus=3D1= 6 */ + .config =3D SMP_CONFIG_GENERIC(F, 0, T, 2, T, 4, T, 2, F, 0), + .expect_prefer_sockets =3D CPU_TOPOLOGY_GENERIC(16, 2, 4, 2, 16), + .expect_prefer_cores =3D CPU_TOPOLOGY_GENERIC(16, 2, 4, 2, 16), + }, { + /* config: -smp sockets=3D2,cores=3D4,maxcpus=3D16 + * expect: cpus=3D16,sockets=3D2,cores=3D4,threads=3D2,maxcpus=3D1= 6 */ + .config =3D SMP_CONFIG_GENERIC(F, 0, T, 2, T, 4, F, 0, T, 16), + .expect_prefer_sockets =3D CPU_TOPOLOGY_GENERIC(16, 2, 4, 2, 16), + .expect_prefer_cores =3D CPU_TOPOLOGY_GENERIC(16, 2, 4, 2, 16), + }, { + /* config: -smp sockets=3D2,threads=3D2,maxcpus=3D16 + * expect: cpus=3D16,sockets=3D2,cores=3D4,threads=3D2,maxcpus=3D1= 6 */ + .config =3D SMP_CONFIG_GENERIC(F, 0, T, 2, F, 0, T, 2, T, 16), + .expect_prefer_sockets =3D CPU_TOPOLOGY_GENERIC(16, 2, 4, 2, 16), + .expect_prefer_cores =3D CPU_TOPOLOGY_GENERIC(16, 2, 4, 2, 16), + }, { + /* config: -smp cores=3D4,threads=3D2,maxcpus=3D16 + * expect: cpus=3D16,sockets=3D2,cores=3D4,threads=3D2,maxcpus=3D1= 6 */ + .config =3D SMP_CONFIG_GENERIC(F, 0, F, 0, T, 4, T, 2, T, 16), + .expect_prefer_sockets =3D CPU_TOPOLOGY_GENERIC(16, 2, 4, 2, 16), + .expect_prefer_cores =3D CPU_TOPOLOGY_GENERIC(16, 2, 4, 2, 16), + }, { + /* config: -smp 8,sockets=3D2,cores=3D4,threads=3D1 + * expect: cpus=3D8,sockets=3D2,cores=3D4,threads=3D1,maxcpus=3D8 = */ + .config =3D SMP_CONFIG_GENERIC(T, 8, T, 2, T, 4, T, 1, F, 0), + .expect_prefer_sockets =3D CPU_TOPOLOGY_GENERIC(8, 2, 4, 1, 8), + .expect_prefer_cores =3D CPU_TOPOLOGY_GENERIC(8, 2, 4, 1, 8), + }, { + /* config: -smp 8,sockets=3D2,cores=3D4,maxcpus=3D16 + * expect: cpus=3D8,sockets=3D2,cores=3D4,threads=3D2,maxcpus=3D16= */ + .config =3D SMP_CONFIG_GENERIC(T, 8, T, 2, T, 4, F, 0, T, 16), + .expect_prefer_sockets =3D CPU_TOPOLOGY_GENERIC(8, 2, 4, 2, 16), + .expect_prefer_cores =3D CPU_TOPOLOGY_GENERIC(8, 2, 4, 2, 16), + }, { + /* config: -smp 8,sockets=3D2,threads=3D2,maxcpus=3D16 + * expect: cpus=3D8,sockets=3D2,cores=3D4,threads=3D2,maxcpus=3D16= */ + .config =3D SMP_CONFIG_GENERIC(T, 8, T, 2, F, 0, T, 2, T, 16), + .expect_prefer_sockets =3D CPU_TOPOLOGY_GENERIC(8, 2, 4, 2, 16), + .expect_prefer_cores =3D CPU_TOPOLOGY_GENERIC(8, 2, 4, 2, 16), + }, { + /* config: -smp 8,cores=3D4,threads=3D2,maxcpus=3D16 + * expect: cpus=3D8,sockets=3D2,cores=3D4,threads=3D2,maxcpus=3D16= */ + .config =3D SMP_CONFIG_GENERIC(T, 8, F, 0, T, 4, T, 2, T, 16), + .expect_prefer_sockets =3D CPU_TOPOLOGY_GENERIC(8, 2, 4, 2, 16), + .expect_prefer_cores =3D CPU_TOPOLOGY_GENERIC(8, 2, 4, 2, 16), + }, { + /* config: -smp sockets=3D2,cores=3D4,threads=3D2,maxcpus=3D16 + * expect: cpus=3D16,sockets=3D2,cores=3D4,threads=3D2,maxcpus=3D1= 6 */ + .config =3D SMP_CONFIG_GENERIC(F, 0, T, 2, T, 4, T, 2, T, 16), + .expect_prefer_sockets =3D CPU_TOPOLOGY_GENERIC(16, 2, 4, 2, 16), + .expect_prefer_cores =3D CPU_TOPOLOGY_GENERIC(16, 2, 4, 2, 16), + }, { + /* config: -smp 8,sockets=3D2,cores=3D4,threads=3D2,maxcpus=3D16 + * expect: cpus=3D8,sockets=3D2,cores=3D4,threads=3D2,maxcpus=3D16= */ + .config =3D SMP_CONFIG_GENERIC(T, 8, T, 2, T, 4, T, 2, T, 16), + .expect_prefer_sockets =3D CPU_TOPOLOGY_GENERIC(8, 2, 4, 2, 16), + .expect_prefer_cores =3D CPU_TOPOLOGY_GENERIC(8, 2, 4, 2, 16), + }, +}; + +static struct SMPTestData data_generic_invalid[] =3D { + { + /* config: -smp 2,dies=3D2 */ + .config =3D SMP_CONFIG_WITH_DIES(T, 2, F, 0, T, 2, F, 0, F, 0, F, = 0), + .expect_error =3D "dies not supported by this machine's CPU topolo= gy", + }, { + /* config: -smp 8,sockets=3D2,cores=3D4,threads=3D2,maxcpus=3D8 */ + .config =3D SMP_CONFIG_GENERIC(T, 8, T, 2, T, 4, T, 2, T, 8), + .expect_error =3D "Invalid CPU topology: " + "product of the hierarchy must match maxcpus: " + "sockets (2) * cores (4) * threads (2) " + "!=3D maxcpus (8)", + }, { + /* config: -smp 18,sockets=3D2,cores=3D4,threads=3D2,maxcpus=3D16 = */ + .config =3D SMP_CONFIG_GENERIC(T, 18, T, 2, T, 4, T, 2, T, 16), + .expect_error =3D "Invalid CPU topology: " + "maxcpus must be equal to or greater than smp: " + "sockets (2) * cores (4) * threads (2) " + "=3D=3D maxcpus (16) < smp_cpus (18)", + }, { + /* config: -smp 1 + * should tweak the supported min CPUs to 2 for testing */ + .config =3D SMP_CONFIG_GENERIC(T, 1, F, 0, F, 0, F, 0, F, 0), + .expect_error =3D "Invalid SMP CPUs 1. The min CPUs supported " + "by machine '(null)' is 2", + }, { + /* config: -smp 512 + * should tweak the supported max CPUs to 511 for testing */ + .config =3D SMP_CONFIG_GENERIC(T, 512, F, 0, F, 0, F, 0, F, 0), + .expect_error =3D "Invalid SMP CPUs 512. The max CPUs supported " + "by machine '(null)' is 511", + }, +}; + +static struct SMPTestData data_with_dies_invalid[] =3D { + { + /* config: -smp 16,sockets=3D2,dies=3D2,cores=3D4,threads=3D2,maxc= pus=3D16 */ + .config =3D SMP_CONFIG_WITH_DIES(T, 16, T, 2, T, 2, T, 4, T, 2, T,= 16), + .expect_error =3D "Invalid CPU topology: " + "product of the hierarchy must match maxcpus: " + "sockets (2) * dies (2) * cores (4) * threads (2) " + "!=3D maxcpus (16)", + }, { + /* config: -smp 34,sockets=3D2,dies=3D2,cores=3D4,threads=3D2,maxc= pus=3D32 */ + .config =3D SMP_CONFIG_WITH_DIES(T, 34, T, 2, T, 2, T, 4, T, 2, T,= 32), + .expect_error =3D "Invalid CPU topology: " + "maxcpus must be equal to or greater than smp: " + "sockets (2) * dies (2) * cores (4) * threads (2) " + "=3D=3D maxcpus (32) < smp_cpus (34)", + }, +}; + +static char *smp_config_to_string(SMPConfiguration *config) +{ + return g_strdup_printf( + "(SMPConfiguration) {\n" + " .has_cpus =3D %5s, cpus =3D %ld,\n" + " .has_sockets =3D %5s, sockets =3D %ld,\n" + " .has_dies =3D %5s, dies =3D %ld,\n" + " .has_cores =3D %5s, cores =3D %ld,\n" + " .has_threads =3D %5s, threads =3D %ld,\n" + " .has_maxcpus =3D %5s, maxcpus =3D %ld,\n" + "}", + config->has_cpus ? "true" : "false", config->cpus, + config->has_sockets ? "true" : "false", config->sockets, + config->has_dies ? "true" : "false", config->dies, + config->has_cores ? "true" : "false", config->cores, + config->has_threads ? "true" : "false", config->threads, + config->has_maxcpus ? "true" : "false", config->maxcpus); +} + +static char *cpu_topology_to_string(CpuTopology *topo) +{ + return g_strdup_printf( + "(CpuTopology) {\n" + " .cpus =3D %u,\n" + " .sockets =3D %u,\n" + " .dies =3D %u,\n" + " .cores =3D %u,\n" + " .threads =3D %u,\n" + " .max_cpus =3D %u,\n" + "}", + topo->cpus, topo->sockets, topo->dies, + topo->cores, topo->threads, topo->max_cpus); +} + +static void check_parse(MachineState *ms, SMPConfiguration *config, + CpuTopology *expect_topo, const char *expect_err, + bool is_valid) +{ + g_autofree char *config_str =3D smp_config_to_string(config); + g_autofree char *expect_topo_str =3D cpu_topology_to_string(expect_top= o); + g_autofree char *output_topo_str =3D NULL; + Error *err =3D NULL; + + /* call the generic parser smp_parse() */ + smp_parse(ms, config, &err); + + output_topo_str =3D cpu_topology_to_string(&ms->smp); + + /* when the configuration is supposed to be valid */ + if (is_valid) { + if ((err =3D=3D NULL) && + (ms->smp.cpus =3D=3D expect_topo->cpus) && + (ms->smp.sockets =3D=3D expect_topo->sockets) && + (ms->smp.dies =3D=3D expect_topo->dies) && + (ms->smp.cores =3D=3D expect_topo->cores) && + (ms->smp.threads =3D=3D expect_topo->threads) && + (ms->smp.max_cpus =3D=3D expect_topo->max_cpus)) { + return; + } + + if (err !=3D NULL) { + g_printerr("Test smp_parse failed!\n" + "Input configuration: %s\n" + "Should be valid: yes\n" + "Expected topology: %s\n\n" + "Result is valid: no\n" + "Output error report: %s\n", + config_str, expect_topo_str, error_get_pretty(err)); + goto end; + } + + g_printerr("Test smp_parse failed!\n" + "Input configuration: %s\n" + "Should be valid: yes\n" + "Expected topology: %s\n\n" + "Result is valid: yes\n" + "Output topology: %s\n", + config_str, expect_topo_str, output_topo_str); + goto end; + } + + /* when the configuration is supposed to be invalid */ + if (err !=3D NULL) { + if (expect_err =3D=3D NULL || + g_str_equal(expect_err, error_get_pretty(err))) { + error_free(err); + return; + } + + g_printerr("Test smp_parse failed!\n" + "Input configuration: %s\n" + "Should be valid: no\n" + "Expected error report: %s\n\n" + "Result is valid: no\n" + "Output error report: %s\n", + config_str, expect_err, error_get_pretty(err)); + goto end; + } + + g_printerr("Test smp_parse failed!\n" + "Input configuration: %s\n" + "Should be valid: no\n" + "Expected error report: %s\n\n" + "Result is valid: yes\n" + "Output topology: %s\n", + config_str, expect_err, output_topo_str); + +end: + if (err !=3D NULL) { + error_free(err); + } + + abort(); +} + +static void smp_parse_test(MachineState *ms, SMPTestData *data, bool is_va= lid) +{ + MachineClass *mc =3D MACHINE_GET_CLASS(ms); + + mc->smp_props.prefer_sockets =3D true; + check_parse(ms, &data->config, &data->expect_prefer_sockets, + data->expect_error, is_valid); + + mc->smp_props.prefer_sockets =3D false; + check_parse(ms, &data->config, &data->expect_prefer_cores, + data->expect_error, is_valid); +} + +/* The parsed results of the unsupported parameters should be 1 */ +static void unsupported_params_init(MachineClass *mc, SMPTestData *data) +{ + if (!mc->smp_props.dies_supported) { + data->expect_prefer_sockets.dies =3D 1; + data->expect_prefer_cores.dies =3D 1; + } +} + +/* Reset the related machine properties before each sub-test */ +static void smp_machine_class_init(MachineClass *mc) +{ + mc->min_cpus =3D MIN_CPUS; + mc->max_cpus =3D MAX_CPUS; + + mc->smp_props.prefer_sockets =3D true; + mc->smp_props.dies_supported =3D false; +} + +static void test_generic(void) +{ + Object *obj =3D object_new(TYPE_MACHINE); + MachineState *ms =3D MACHINE(obj); + MachineClass *mc =3D MACHINE_GET_CLASS(obj); + SMPTestData *data =3D &(SMPTestData){0}; + int i; + + smp_machine_class_init(mc); + + for (i =3D 0; i < ARRAY_SIZE(data_generic_valid); i++) { + *data =3D data_generic_valid[i]; + unsupported_params_init(mc, data); + + smp_parse_test(ms, data, true); + + /* Unsupported parameters can be provided with their values as 1 */ + data->config.has_dies =3D true; + data->config.dies =3D 1; + smp_parse_test(ms, data, true); + } + + /* Reset the supported min CPUs and max CPUs */ + mc->min_cpus =3D 2; + mc->max_cpus =3D 511; + + for (i =3D 0; i < ARRAY_SIZE(data_generic_invalid); i++) { + *data =3D data_generic_invalid[i]; + unsupported_params_init(mc, data); + + smp_parse_test(ms, data, false); + } + + object_unref(obj); +} + +static void test_with_dies(void) +{ + Object *obj =3D object_new(TYPE_MACHINE); + MachineState *ms =3D MACHINE(obj); + MachineClass *mc =3D MACHINE_GET_CLASS(obj); + SMPTestData *data =3D &(SMPTestData){0}; + unsigned int num_dies =3D 2; + int i; + + smp_machine_class_init(mc); + mc->smp_props.dies_supported =3D true; + + for (i =3D 0; i < ARRAY_SIZE(data_generic_valid); i++) { + *data =3D data_generic_valid[i]; + unsupported_params_init(mc, data); + + /* when dies parameter is omitted, it will be set as 1 */ + data->expect_prefer_sockets.dies =3D 1; + data->expect_prefer_cores.dies =3D 1; + + smp_parse_test(ms, data, true); + + /* when dies parameter is specified */ + data->config.has_dies =3D true; + data->config.dies =3D num_dies; + if (data->config.has_cpus) { + data->config.cpus *=3D num_dies; + } + if (data->config.has_maxcpus) { + data->config.maxcpus *=3D num_dies; + } + + data->expect_prefer_sockets.dies =3D num_dies; + data->expect_prefer_sockets.cpus *=3D num_dies; + data->expect_prefer_sockets.max_cpus *=3D num_dies; + data->expect_prefer_cores.dies =3D num_dies; + data->expect_prefer_cores.cpus *=3D num_dies; + data->expect_prefer_cores.max_cpus *=3D num_dies; + + smp_parse_test(ms, data, true); + } + + for (i =3D 0; i < ARRAY_SIZE(data_with_dies_invalid); i++) { + *data =3D data_with_dies_invalid[i]; + unsupported_params_init(mc, data); + + smp_parse_test(ms, data, false); + } + + object_unref(obj); +} + +int main(int argc, char *argv[]) +{ + g_test_init(&argc, &argv, NULL); + + module_call_init(MODULE_INIT_QOM); + type_register_static(&smp_machine_info); + + g_test_add_func("/test-smp-parse/generic", test_generic); + g_test_add_func("/test-smp-parse/with_dies", test_with_dies); + + g_test_run(); + + return 0; +} diff --git a/MAINTAINERS b/MAINTAINERS index 80ec27d76ae..310a9512ea1 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -1633,6 +1633,7 @@ F: include/hw/boards.h F: include/hw/core/cpu.h F: include/hw/cpu/cluster.h F: include/sysemu/numa.h +F: tests/unit/test-smp-parse.c T: git https://gitlab.com/ehabkost/qemu.git machine-next =20 Xtensa Machines diff --git a/tests/unit/meson.build b/tests/unit/meson.build index 7c297d7e5c0..5e81525bef1 100644 --- a/tests/unit/meson.build +++ b/tests/unit/meson.build @@ -45,6 +45,7 @@ 'test-uuid': [], 'ptimer-test': ['ptimer-test-stubs.c', meson.project_source_root() / 'hw= /core/ptimer.c'], 'test-qapi-util': [], + 'test-smp-parse': [qom, meson.project_source_root() / 'hw/core/machine-s= mp.c'], } =20 if have_system or have_tools --=20 2.31.1