Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A7415C05027 for ; Fri, 17 Feb 2023 07:42:06 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pSvNG-0006eC-L5; Fri, 17 Feb 2023 02:41:43 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pSvNE-0006dA-RK for qemu-devel@nongnu.org; Fri, 17 Feb 2023 02:41:40 -0500 Received: from szxga02-in.huawei.com ([45.249.212.188]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pSvNB-0003P6-OI for qemu-devel@nongnu.org; Fri, 17 Feb 2023 02:41:40 -0500 Received: from dggpemm500023.china.huawei.com (unknown [172.30.72.54]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4PJ3cG0wcDzGpks; Fri, 17 Feb 2023 15:39:42 +0800 (CST) Received: from [10.174.187.128] (10.174.187.128) by dggpemm500023.china.huawei.com (7.185.36.83) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.6; Fri, 17 Feb 2023 15:41:30 +0800 Message-ID: Date: Fri, 17 Feb 2023 15:41:30 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.6.1 Subject: Re: [RFC 08/52] machine: Add helpers to get cpu topology info from MachineState.topo To: Zhao Liu CC: , Zhenyu Wang , Dapeng Mi , Zhuocheng Ding , Robert Hoo , Sean Christopherson , Like Xu , Zhao Liu , Eduardo Habkost , Marcel Apfelbaum , =?UTF-8?Q?Philippe_Mathieu-Daud=c3=a9?= , "Michael S . Tsirkin" , Richard Henderson , Paolo Bonzini , Eric Blake , Markus Armbruster References: <20230213095035.158240-1-zhao1.liu@linux.intel.com> <20230213095035.158240-9-zhao1.liu@linux.intel.com> <6fb117f7-4d7e-326d-d0e4-0a77ef65e179@huawei.com> In-Reply-To: Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 8bit X-Originating-IP: [10.174.187.128] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To dggpemm500023.china.huawei.com (7.185.36.83) X-CFilter-Loop: Reflected Received-SPF: pass client-ip=45.249.212.188; envelope-from=wangyanan55@huawei.com; helo=szxga02-in.huawei.com X-Spam_score_int: -45 X-Spam_score: -4.6 X-Spam_bar: ---- X-Spam_report: (-4.6 / 5.0 requ) BAYES_00=-1.9, NICE_REPLY_A=-0.351, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: "wangyanan (Y)" From: "wangyanan (Y)" via Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org 在 2023/2/17 11:07, Zhao Liu 写道: > On Thu, Feb 16, 2023 at 04:38:38PM +0800, wangyanan (Y) wrote: >> Date: Thu, 16 Feb 2023 16:38:38 +0800 >> From: "wangyanan (Y)" >> Subject: Re: [RFC 08/52] machine: Add helpers to get cpu topology info from >> MachineState.topo >> >> Hi Zhao, >> >> 在 2023/2/13 17:49, Zhao Liu 写道: >>> From: Zhao Liu >>> >>> When MachineState.topo is introduced, the topology related structures >>> become complicated. In the general case (hybrid or smp topology), >>> accessing the topology information needs to determine whether it is >>> currently smp or hybrid topology, and then access the corresponding >>> MachineState.topo.smp or MachineState.topo.hybrid. >>> >>> The best way to do this is to wrap the access to the topology to >>> avoid having to check each time it is accessed. >>> >>> The following helpers are provided here: >>> >>> - General interfaces - no need to worry about whether the underlying >>> topology is smp or hybrid: >>> >>> * machine_topo_get_cpus() >>> * machine_topo_get_max_cpus() >>> * machine_topo_is_smp() >>> * machine_topo_get_sockets() >>> * machine_topo_get_dies() >>> * machine_topo_get_clusters() >>> * machine_topo_get_threads(); >>> * machine_topo_get_cores(); >>> * machine_topo_get_threads_by_idx() >>> * machine_topo_get_cores_by_idx() >>> * machine_topo_get_cores_per_socket() >>> * machine_topo_get_threads_per_socket() >>> >>> - SMP-specific interfaces - provided for the cases that are clearly >>> known to be smp topology: >>> >>> * machine_topo_get_smp_cores() >>> * machine_topo_get_smp_threads() >>> >>> Since for hybrid topology, each core may has different threads, if >>> someone wants "cpus per core", the cpu_index is need to target a >>> specific core (machine_topo_get_threads_by_idx()). But for smp, there is >>> no need to be so troublesome, so for this case, we provide smp-specific >>> interfaces. >>> >>> Signed-off-by: Zhao Liu >>> --- >>> hw/core/machine-topo.c | 142 +++++++++++++++++++++++++++++++++++++++++ >>> include/hw/boards.h | 35 ++++++++++ >>> 2 files changed, 177 insertions(+) >>> >>> diff --git a/hw/core/machine-topo.c b/hw/core/machine-topo.c >>> index 7223f73f99b0..b20160479629 100644 >>> --- a/hw/core/machine-topo.c >>> +++ b/hw/core/machine-topo.c >>> @@ -21,6 +21,148 @@ >>> #include "hw/boards.h" >>> #include "qapi/error.h" >>> +unsigned int machine_topo_get_sockets(const MachineState *ms) >>> +{ >>> + return machine_topo_is_smp(ms) ? ms->topo.smp.sockets : >>> + ms->topo.hybrid.sockets; >>> +} >>> + >>> +unsigned int machine_topo_get_dies(const MachineState *ms) >>> +{ >>> + return machine_topo_is_smp(ms) ? ms->topo.smp.dies : >>> + ms->topo.hybrid.dies; >>> +} >>> + >>> +unsigned int machine_topo_get_clusters(const MachineState *ms) >>> +{ >>> + return machine_topo_is_smp(ms) ? ms->topo.smp.clusters : >>> + ms->topo.hybrid.clusters; >>> +} >>> + >>> +unsigned int machine_topo_get_smp_cores(const MachineState *ms) >>> +{ >>> + g_assert(machine_topo_is_smp(ms)); >>> + return ms->topo.smp.cores; >>> +} >>> + >>> +unsigned int machine_topo_get_smp_threads(const MachineState *ms) >>> +{ >>> + g_assert(machine_topo_is_smp(ms)); >>> + return ms->topo.smp.threads; >>> +} >>> + >>> +unsigned int machine_topo_get_threads(const MachineState *ms, >>> + unsigned int cluster_id, >>> + unsigned int core_id) >>> +{ >>> + if (machine_topo_is_smp(ms)) { >>> + return ms->topo.smp.threads; >>> + } else { >>> + return ms->topo.hybrid.cluster_list[cluster_id] >>> + .core_list[core_id].threads; >>> + } >>> + >>> + return 0; >>> +} >>> + >>> +unsigned int machine_topo_get_cores(const MachineState *ms, >>> + unsigned int cluster_id) >>> +{ >>> + if (machine_topo_is_smp(ms)) { >>> + return ms->topo.smp.cores; >>> + } else { >>> + return ms->topo.hybrid.cluster_list[cluster_id].cores; >>> + } >>> +} >> Is it possible to use variadic function so that those two smp specific >> helpers can be avoided? It's a bit wired that we have the generic >> machine_topo_get_threads but also need machine_topo_get_smp_threads >> at the same time. > I am not sure about this, because variadic functions unify function > naming, but eliminate the "smp-specific" information from the name. > > Trying to get the cres/threads without considering the cpu index can > only be used in smp scenarios, and I think the caller needs to > understand that he knows it's smp. Ok, I get the point. When it comes to the naming, would it be more concise to remove the *_get_* in the fun name, such as machine_topo_get_cpus to machine_topo_cpus, machine_topo_get_clusters to machine_topo_clusters. And maybe rename machine_topo_get_cores(int cluster_id, int core_id) to machine_topo_cores_by_ids? Or machine_topo_get_cores() to machine_topo_cores_by_topo_ids() and machine_topo_get_cores_by_idx to machine_topo_cores_by_cpu_idx() >>> + >>> +unsigned int machine_topo_get_threads_by_idx(const MachineState *ms, >>> + unsigned int cpu_index) >>> +{ >>> + unsigned cpus_per_die; >>> + unsigned tmp_idx; >>> + HybridCluster *cluster; >>> + HybridCore *core; >>> + >>> + if (machine_topo_is_smp(ms)) { >>> + return ms->topo.smp.threads; >>> + } >>> + >>> + cpus_per_die = ms->topo.max_cpus / (ms->topo.hybrid.sockets * >>> + ms->topo.hybrid.dies); >>> + tmp_idx = cpu_index % cpus_per_die; >>> + >>> + for (int i = 0; i < ms->topo.hybrid.clusters; i++) { >>> + cluster = &ms->topo.hybrid.cluster_list[i]; >>> + >>> + for (int j = 0; j < cluster->cores; j++) { >>> + core = &cluster->core_list[j]; >>> + >>> + if (tmp_idx < core->threads) { >>> + return core->threads; >>> + } else { >>> + tmp_idx -= core->threads; >>> + } >>> + } >>> + } >>> + >>> + return 0; >>> +} >>> + >>> +unsigned int machine_topo_get_cores_by_idx(const MachineState *ms, >>> + unsigned int cpu_index) >>> +{ >>> + unsigned cpus_per_die; >>> + unsigned tmp_idx; >>> + HybridCluster *cluster; >>> + HybridCore *core; >>> + >>> + if (machine_topo_is_smp(ms)) { >>> + return ms->topo.smp.cores; >>> + } >>> + >>> + cpus_per_die = ms->topo.max_cpus / (ms->topo.hybrid.sockets * >>> + ms->topo.hybrid.dies); >>> + tmp_idx = cpu_index % cpus_per_die; >>> + >>> + for (int i = 0; i < ms->topo.hybrid.clusters; i++) { >>> + cluster = &ms->topo.hybrid.cluster_list[i]; >>> + >>> + for (int j = 0; j < cluster->cores; j++) { >>> + core = &cluster->core_list[j]; >>> + >>> + if (tmp_idx < core->threads) { >>> + return cluster->cores; >>> + } else { >>> + tmp_idx -= core->threads; >>> + } >>> + } >>> + } >>> + >>> + return 0; >>> +} >>> + >>> +unsigned int machine_topo_get_cores_per_socket(const MachineState *ms) >>> +{ >>> + unsigned int cores_per_die = 0; >>> + >>> + if (machine_topo_is_smp(ms)) { >>> + return ms->topo.smp.cores * ms->topo.smp.clusters * ms->topo.smp.dies; >>> + } >>> + >>> + for (int i = 0; i < ms->topo.hybrid.clusters; i++) { >>> + cores_per_die += ms->topo.hybrid.cluster_list[i].cores; >>> + } >>> + >>> + return cores_per_die * ms->topo.hybrid.dies; >>> +} >>> + >>> +unsigned int machine_topo_get_threads_per_socket(const MachineState *ms) >>> +{ >>> + unsigned int sockets = machine_topo_is_smp(ms) ? ms->topo.smp.sockets : >>> + ms->topo.hybrid.sockets; >>> + return ms->topo.max_cpus / sockets; >>> +} >>> + >>> /* >>> * Report information of a machine's supported CPU topology hierarchy. >>> * Topology members will be ordered from the largest to the smallest >>> diff --git a/include/hw/boards.h b/include/hw/boards.h >>> index 0a61855499e3..34b64b012022 100644 >>> --- a/include/hw/boards.h >>> +++ b/include/hw/boards.h >>> @@ -461,4 +461,39 @@ extern const size_t hw_compat_2_2_len; >>> extern GlobalProperty hw_compat_2_1[]; >>> extern const size_t hw_compat_2_1_len; >>> +static inline >>> +unsigned int machine_topo_get_cpus(const MachineState *ms) >>> +{ >>> + return ms->topo.cpus; >>> +} >>> + >>> +static inline >>> +unsigned int machine_topo_get_max_cpus(const MachineState *ms) >>> +{ >>> + return ms->topo.max_cpus; >>> +} >>> + >>> +static inline >>> +bool machine_topo_is_smp(const MachineState *ms) >>> +{ >>> + return ms->topo.topo_type == CPU_TOPO_TYPE_SMP; >>> +} >>> + >>> +unsigned int machine_topo_get_sockets(const MachineState *ms); >>> +unsigned int machine_topo_get_dies(const MachineState *ms); >>> +unsigned int machine_topo_get_clusters(const MachineState *ms); >>> +unsigned int machine_topo_get_smp_cores(const MachineState *ms); >>> +unsigned int machine_topo_get_smp_threads(const MachineState *ms); >>> +unsigned int machine_topo_get_threads(const MachineState *ms, >>> + unsigned int cluster_id, >>> + unsigned int core_id); >>> +unsigned int machine_topo_get_cores(const MachineState *ms, >>> + unsigned int cluster_id); >>> +unsigned int machine_topo_get_threads_by_idx(const MachineState *ms, >>> + unsigned int cpu_index); >>> +unsigned int machine_topo_get_cores_by_idx(const MachineState *ms, >>> + unsigned int cpu_index); >>> +unsigned int machine_topo_get_cores_per_socket(const MachineState *ms); >>> +unsigned int machine_topo_get_threads_per_socket(const MachineState *ms); >>> + >>> #endif >> I think it's necessary to document the ablity for each helper. >> For example, at a flance, I cant figure out what >> machine_topo_get_threads_idx >> does. Add some something like: >> /* >>  * Get number of threads within the CPU core where a processor locates, >>  * according to the processor index. >>  * >>  * @param: ... >>  */ >> will be friendly to future users. > Yeah, thanks! I will. > >> Thanks, >> Yanan