Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp782410rwe; Wed, 31 Aug 2022 10:53:07 -0700 (PDT) X-Google-Smtp-Source: AA6agR6jF7rCWA/pG8blcJ7cjffrDq8nRa9e7+KrvK6LJdVdpl+nPpLUAezqbPE1VvhN7MpsUJL4 X-Received: by 2002:a63:154b:0:b0:42c:60ce:8bd3 with SMTP id 11-20020a63154b000000b0042c60ce8bd3mr10400233pgv.372.1661968387387; Wed, 31 Aug 2022 10:53:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661968387; cv=none; d=google.com; s=arc-20160816; b=dbXofPMqswx+ECLEa3aj/1RCvNMr/l7iJBt6gg9o8HK2D2ezFgz4sWvbIoaoEhFUS7 dvMcrZk6O4m0lv9Yv87b29Hox96B6Wtn/Tr4DHyRvjJSesxJnl8TaOXJJmfSCtVClJK8 UwqOdXx41CPp/usVxOPHJ09PrGRkfBRVYFSLD9chI+sFmm0hIs93y4/VLL6mZzyqn4yt 8C1By47e0hOK9jdW7GrW+p8IP0R7I/76/tj6hnAYE9/VnPmGUDI67wX5eeBEFrwXa9+A F+XPejh5gcenr69DWhM6HPVn6fIWaviMhYNz5kHzafs8aNVIzVehcZdxKrwBTEG9H+8/ 0I7Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=UUVPQFoHB4SWNXVjnYdqps9D8vjoKXb+eUEK4kYsPDY=; b=00Ho5QahRylPtLSOp0QdgwmX5SlwOvyq3xt3+A9ici8uJaFq6KISMterzKbcDgSWfA aBAeHLUmsJ9IPD+Hc5b68iYUyGMkDOw8C1glB5jWg6b/4x+XVxr4JyB6Xou0qzCltnKh jR7w4R5dWPs3HhMad+G9BSd4tF97QK1Gr1d/UKBE2+wMfYqcRViNgtMzfyWJCPY6j2N9 PsCB0RG3WivFa174FlnmZjy+XvrYYSWciJm5kDhfPYDSyh7laOFWE9Kc4/0P5MPCwl86 iH6i9I/uh5twzQ8joeZvMX7K+19NcbOvNV56TZgkDfxI3UXtFnuHX2UWN3Kvg1AcdbuD Lztg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=JrYRHr4a; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x8-20020a17090a164800b001fe300ae60asi1926018pje.54.2022.08.31.10.52.56; Wed, 31 Aug 2022 10:53:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=JrYRHr4a; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231921AbiHaRuc (ORCPT + 99 others); Wed, 31 Aug 2022 13:50:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36266 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231766AbiHaRuI (ORCPT ); Wed, 31 Aug 2022 13:50:08 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 74B9C13F6D for ; Wed, 31 Aug 2022 10:50:06 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id y81-20020a253254000000b0069339437485so2636433yby.5 for ; Wed, 31 Aug 2022 10:50:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc; bh=UUVPQFoHB4SWNXVjnYdqps9D8vjoKXb+eUEK4kYsPDY=; b=JrYRHr4aq2pvt/jT3CJxChuXVG1yWjglVjUr4IcK2lA1iP2BWrEYb6kfEFOQ0X8hK0 HjhkT6DdkBGkBE19qTOtOksK23eRhES60mmOI7bECIRPADVAZxE0N3RzVZjoXDfhslJa gX18RBo/FahJpDTaMAOR5fkJhXuUyBbxRw/rEdceSQ61OoLbG3sBJ79retLkBJmdl7vi eD1gYFizJUzbNaMhdqugTp/JxS0yf0ploA6kQm6obpEEA6nqYL1ObpVofVpvIAzRVBjK IM9iX7CLinOXzuh9n2hAC70G2CVuog1NXqY8JTFGuyZA8s6bdLEnLzOyGXDc4yfMd/lv VirA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc; bh=UUVPQFoHB4SWNXVjnYdqps9D8vjoKXb+eUEK4kYsPDY=; b=8GkN9/lYhiHioPBkEO/qCDw/LaoB+6QuZQI/RP9vms0CoxuCgjtCwNrfU4rgJ9atgn 9GEf7ti8uczPxX2jqDH1IEmeVLi+ZVEotYgT93PmRCpvndR9mHb2cj8Zq4CEKJ2RZqaw rf3/5HarT4brxRMdICCRSHJmF0L5zodDzZ6L7INwm7mw2GNhCr0r1zRzSN/9eno6eOWP XBd3CCgnrvQODGm5ZQT8d2Q/6SepkJm+jJt8e3KIoGfHJ3g65cz9pftR8yKO8082a6Gn qr8Ykt9r4zjUf4fz4DZwLFn79FNehu5WYk8H5VZtZa2tLlVUYaqpFE1hAA2TnEwRmN1x IIfQ== X-Gm-Message-State: ACgBeo0+01y/tYfXwrBphrOfXpuluWHjBQXTO9lxZt/wvLHGiBID5WN7 7TyziDbqw4uMEXzqfXscZEos7pchRgsI X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:c8e4:a3e5:9d09:d3d4]) (user=irogers job=sendgmr) by 2002:a81:6d16:0:b0:340:bbed:a2a9 with SMTP id i22-20020a816d16000000b00340bbeda2a9mr17159002ywc.367.1661968205425; Wed, 31 Aug 2022 10:50:05 -0700 (PDT) Date: Wed, 31 Aug 2022 10:49:22 -0700 In-Reply-To: <20220831174926.579643-1-irogers@google.com> Message-Id: <20220831174926.579643-4-irogers@google.com> Mime-Version: 1.0 References: <20220831174926.579643-1-irogers@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Subject: [PATCH v2 3/7] perf smt: Compute SMT from topology From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Andi Kleen , Kan Liang , Thomas Richter , James Clark , Miaoqian Lin , John Garry , Zhengjun Xing , Florian Fischer , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, perry.taylor@intel.com, caleb.biggers@intel.com, kshipra.bopardikar@intel.com, ahmad.yasin@intel.com Cc: Stephane Eranian , Ian Rogers Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The topology records sibling threads. Rather than computing SMT using siblings in sysfs, reuse the values in topology. This only applies when the file smt/active isn't available. Signed-off-by: Ian Rogers --- tools/perf/tests/expr.c | 24 ++++++---- tools/perf/util/cputopo.c | 15 +++++++ tools/perf/util/cputopo.h | 2 + tools/perf/util/expr.c | 9 ++-- tools/perf/util/smt.c | 95 ++++----------------------------------- tools/perf/util/smt.h | 5 ++- 6 files changed, 49 insertions(+), 101 deletions(-) diff --git a/tools/perf/tests/expr.c b/tools/perf/tests/expr.c index 7ca5e37de560..db736ed49556 100644 --- a/tools/perf/tests/expr.c +++ b/tools/perf/tests/expr.c @@ -1,4 +1,5 @@ // SPDX-License-Identifier: GPL-2.0 +#include "util/cputopo.h" #include "util/debug.h" #include "util/expr.h" #include "util/header.h" @@ -154,15 +155,20 @@ static int test__expr(struct test_suite *t __maybe_unused, int subtest __maybe_u (void **)&val_ptr)); /* Only EVENT1 or EVENT2 need be measured depending on the value of smt_on. */ - expr__ctx_clear(ctx); - TEST_ASSERT_VAL("find ids", - expr__find_ids("EVENT1 if #smt_on else EVENT2", - NULL, ctx) == 0); - TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1); - TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, - smt_on() ? "EVENT1" : "EVENT2", - (void **)&val_ptr)); - + { + struct cpu_topology *topology = cpu_topology__new(); + bool smton = smt_on(topology); + + cpu_topology__delete(topology); + expr__ctx_clear(ctx); + TEST_ASSERT_VAL("find ids", + expr__find_ids("EVENT1 if #smt_on else EVENT2", + NULL, ctx) == 0); + TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1); + TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, + smton ? "EVENT1" : "EVENT2", + (void **)&val_ptr)); + } /* The expression is a constant 1.0 without needing to evaluate EVENT1. */ expr__ctx_clear(ctx); TEST_ASSERT_VAL("find ids", diff --git a/tools/perf/util/cputopo.c b/tools/perf/util/cputopo.c index d275d843c155..511002e52714 100644 --- a/tools/perf/util/cputopo.c +++ b/tools/perf/util/cputopo.c @@ -157,6 +157,21 @@ void cpu_topology__delete(struct cpu_topology *tp) free(tp); } +bool cpu_topology__smt_on(const struct cpu_topology *topology) +{ + for (u32 i = 0; i < topology->core_cpus_lists; i++) { + const char *cpu_list = topology->core_cpus_list[i]; + + /* + * If there is a need to separate siblings in a core then SMT is + * enabled. + */ + if (strchr(cpu_list, ',') || strchr(cpu_list, '-')) + return true; + } + return false; +} + static bool has_die_topology(void) { char filename[MAXPATHLEN]; diff --git a/tools/perf/util/cputopo.h b/tools/perf/util/cputopo.h index 854e18f9041e..469db775a13c 100644 --- a/tools/perf/util/cputopo.h +++ b/tools/perf/util/cputopo.h @@ -58,6 +58,8 @@ struct hybrid_topology { struct cpu_topology *cpu_topology__new(void); void cpu_topology__delete(struct cpu_topology *tp); +/* Determine from the core list whether SMT was enabled. */ +bool cpu_topology__smt_on(const struct cpu_topology *topology); struct numa_topology *numa_topology__new(void); void numa_topology__delete(struct numa_topology *tp); diff --git a/tools/perf/util/expr.c b/tools/perf/util/expr.c index 00bde682e743..8aa7dafa18b3 100644 --- a/tools/perf/util/expr.c +++ b/tools/perf/util/expr.c @@ -412,11 +412,6 @@ double expr__get_literal(const char *literal) static struct cpu_topology *topology; double result = NAN; - if (!strcasecmp("#smt_on", literal)) { - result = smt_on() > 0 ? 1.0 : 0.0; - goto out; - } - if (!strcmp("#num_cpus", literal)) { result = cpu__max_present_cpu().cpu; goto out; @@ -440,6 +435,10 @@ double expr__get_literal(const char *literal) goto out; } } + if (!strcasecmp("#smt_on", literal)) { + result = smt_on(topology) ? 1.0 : 0.0; + goto out; + } if (!strcmp("#num_packages", literal)) { result = topology->package_cpus_lists; goto out; diff --git a/tools/perf/util/smt.c b/tools/perf/util/smt.c index 8fed03283c85..ce90c4ee4138 100644 --- a/tools/perf/util/smt.c +++ b/tools/perf/util/smt.c @@ -1,100 +1,23 @@ // SPDX-License-Identifier: GPL-2.0-only -#include -#include -#include -#include +#include #include "api/fs/fs.h" +#include "cputopo.h" #include "smt.h" -/** - * hweight_str - Returns the number of bits set in str. Stops at first non-hex - * or ',' character. - */ -static int hweight_str(char *str) -{ - int result = 0; - - while (*str) { - switch (*str++) { - case '0': - case ',': - break; - case '1': - case '2': - case '4': - case '8': - result++; - break; - case '3': - case '5': - case '6': - case '9': - case 'a': - case 'A': - case 'c': - case 'C': - result += 2; - break; - case '7': - case 'b': - case 'B': - case 'd': - case 'D': - case 'e': - case 'E': - result += 3; - break; - case 'f': - case 'F': - result += 4; - break; - default: - goto done; - } - } -done: - return result; -} - -int smt_on(void) +bool smt_on(const struct cpu_topology *topology) { static bool cached; - static int cached_result; - int cpu; - int ncpu; + static bool cached_result; + int fs_value; if (cached) return cached_result; - if (sysfs__read_int("devices/system/cpu/smt/active", &cached_result) >= 0) { - cached = true; - return cached_result; - } - - cached_result = 0; - ncpu = sysconf(_SC_NPROCESSORS_CONF); - for (cpu = 0; cpu < ncpu; cpu++) { - unsigned long long siblings; - char *str; - size_t strlen; - char fn[256]; + if (sysfs__read_int("devices/system/cpu/smt/active", &fs_value) >= 0) + cached_result = (fs_value == 1); + else + cached_result = cpu_topology__smt_on(topology); - snprintf(fn, sizeof fn, - "devices/system/cpu/cpu%d/topology/thread_siblings", cpu); - if (sysfs__read_str(fn, &str, &strlen) < 0) { - snprintf(fn, sizeof fn, - "devices/system/cpu/cpu%d/topology/core_cpus", cpu); - if (sysfs__read_str(fn, &str, &strlen) < 0) - continue; - } - /* Entry is hex, but does not have 0x, so need custom parser */ - siblings = hweight_str(str); - free(str); - if (siblings > 1) { - cached_result = 1; - break; - } - } cached = true; return cached_result; } diff --git a/tools/perf/util/smt.h b/tools/perf/util/smt.h index a98d65808f6a..e26999c6b8d4 100644 --- a/tools/perf/util/smt.h +++ b/tools/perf/util/smt.h @@ -2,6 +2,9 @@ #ifndef __SMT_H #define __SMT_H 1 -int smt_on(void); +struct cpu_topology; + +/* Returns true if SMT (aka hyperthreading) is enabled. */ +bool smt_on(const struct cpu_topology *topology); #endif /* __SMT_H */ -- 2.37.2.672.g94769d06f0-goog