From patchwork Fri Mar 31 20:29:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 77970 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp827143vqo; Fri, 31 Mar 2023 13:43:12 -0700 (PDT) X-Google-Smtp-Source: AKy350ZjUlBzBtNngmWaxZm59qnH5Z5RgvH9Stq/v9ybQ8JstRVo80l+TikOg4mxaJOIkzBg7P4F X-Received: by 2002:a62:17d5:0:b0:625:6d5b:c019 with SMTP id 204-20020a6217d5000000b006256d5bc019mr25577708pfx.11.1680295391794; Fri, 31 Mar 2023 13:43:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680295391; cv=none; d=google.com; s=arc-20160816; b=yAdEivSgIlSC1sAu/foX3Kz/ZJMkpdvGdvqA+8JabYz/c15j2CFHCdQ1mXthw2kW9o jkMXarLfdaY3VoUV882d6ltk6S9Esk62d74Z0Du0kIWbO8vjBJuoWCzsmRdp8BmbXH/h bttMqpoHbFoc5mzTXmxzlzVQ+tCin+FUsvnp2O7x6jcHT4kI5trOx4HzlbgZ0HpT5hyn CbTJAU+vhc6k6qkJdP4VXIA82xDK6EI8CU+BpNsJGluqzDvEB2xAdLKFv9P+JoUizeju 4Fean3+RLV9L5MG2qQeoRz1VFgYGxs7E7vP8q7FxmApp6qychnuJa5QPWf6EJAsmcm6Q w0jA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from:sender :dkim-signature; bh=fbGI83psWi01QhPjR2ttTrwM+cN287gT/1Q3Pzny4tc=; b=wRXzBmxW0Vgms1kvaoChq0RSqgOl1BRrZFTTw1weclz/7z7l1EeJNYEnCo+PBfIdvt C8qg+vIilklIGFX0+LozvsFlCkvqUMnpkaq4L3E7Sm90Lrov1nvFuV8MtQauzt8AiWuB 9wBqSEk7GRJ2TyhZujxhyApD9ziq0UYszXnllxQ/zHPmaM2EwEK+sXjX0DZxp8HaG/qK 7C2b1Iu/VnPoL8aNN3PQ2NtCJ2BdoYwzdCO6mv7IB0opXHfyEIjHT4rd7PjfkOJeoRYV JyyXdlVrNEcKysJw0cX/pJ9syFycIPjroWFESghp206WmuD+8Q3GHCNSc9zqubUEpIIM 0maw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=Ti80uQAx; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e187-20020a6369c4000000b0051352302c74si3259814pgc.7.2023.03.31.13.42.59; Fri, 31 Mar 2023 13:43:11 -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=@gmail.com header.s=20210112 header.b=Ti80uQAx; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233231AbjCaUaK (ORCPT + 99 others); Fri, 31 Mar 2023 16:30:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44640 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233169AbjCaU37 (ORCPT ); Fri, 31 Mar 2023 16:29:59 -0400 Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1497F2221E; Fri, 31 Mar 2023 13:29:58 -0700 (PDT) Received: by mail-pj1-x1030.google.com with SMTP id x15so21681033pjk.2; Fri, 31 Mar 2023 13:29:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680294597; x=1682886597; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=fbGI83psWi01QhPjR2ttTrwM+cN287gT/1Q3Pzny4tc=; b=Ti80uQAx6OpqUQQn6abxy1r6kIrgrYp5ucXtvk9dt06p/1yVv32tGEh6g9F98fwx3X 4ZU2Pbe//dxggcX700aMTo5Buj7wwNVrYVDcgKgq+YaP5Ww555i6KSIjOOsZMmw0nfji 6WGO4+XKePfeeZxFXzP2MHvqnUHH14iXbQpfVtwoVEjHNGC8R2xpNa8HAtPqQclMk8Nw G8E7nYF68HrNV8hJIV4hXAff9O8MXr2xpgkO8QgIqm/cTpizEOz1J/iAMY8TQjtrLmuw tutwRo5a0SCtbL9JChQFctQ5IvH9dgkmbqLh3mQneOkri/z2K3NRcmhbZluknNz05H3I z1sA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680294597; x=1682886597; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=fbGI83psWi01QhPjR2ttTrwM+cN287gT/1Q3Pzny4tc=; b=LYF6zqTIdeYjLySzn7RNX8CBhQG9VgPNlfh3R+3Wifzr8RkzefWRbzRs6eLrvIXFaP nOKavbjawHefckgyjsEEg+Ox88kCvvztRZvYLFhYmHauYp4Czmhj1T1qLbKcicTdSmGk j/XJVQTcoukNq4t/iuSYYYwprC9/f4r97BqfAFScQmqZYgvXGz/2yH4SLGlscxPGfR1B 7lNTeAcyj/btjUzAjFB7GSAMN49hrPc9za27lziPhdK9OhD6t5oXFIxz2S6beFmc9Ovv x9BnsXUyvzqkCKPdZiJEVcbpxH7odoNM4HMpA+AHQPshm+fQIzGhRxducumAx2CE0qsM ARDQ== X-Gm-Message-State: AAQBX9ejTEVEaaHe5zwck5uYlezk8ErBTTiro9TiiWWXneI2sgJBhDgs Rv9ghip7uC/f0BgEMFuXKt4= X-Received: by 2002:a17:902:ceca:b0:1a1:956e:5417 with SMTP id d10-20020a170902ceca00b001a1956e5417mr35644440plg.22.1680294597444; Fri, 31 Mar 2023 13:29:57 -0700 (PDT) Received: from bangji.hsd1.ca.comcast.net ([2601:647:6780:44b0:5cfb:ad2c:e6f5:d42f]) by smtp.gmail.com with ESMTPSA id t14-20020a1709028c8e00b001a1dc2be791sm1916803plo.259.2023.03.31.13.29.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 Mar 2023 13:29:57 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Ian Rogers , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Kan Liang , Leo Yan Subject: [PATCH 4/9] perf bench: Add pmu-scan benchmark Date: Fri, 31 Mar 2023 13:29:44 -0700 Message-Id: <20230331202949.810326-5-namhyung@kernel.org> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog In-Reply-To: <20230331202949.810326-1-namhyung@kernel.org> References: <20230331202949.810326-1-namhyung@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=0.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE, SPF_PASS autolearn=no 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761917421182760924?= X-GMAIL-MSGID: =?utf-8?q?1761917421182760924?= The pmu-scan benchmark will repeatedly scan the sysfs to get the available PMU information. $ ./perf bench internals pmu-scan # Running 'internals/pmu-scan' benchmark: Computing performance of sysfs PMU event scan for 100 times Average PMU scanning took: 6850.990 usec (+- 48.445 usec) Signed-off-by: Namhyung Kim --- tools/perf/bench/Build | 1 + tools/perf/bench/bench.h | 1 + tools/perf/bench/pmu-scan.c | 184 ++++++++++++++++++++++++++++++++++++ tools/perf/builtin-bench.c | 1 + 4 files changed, 187 insertions(+) create mode 100644 tools/perf/bench/pmu-scan.c diff --git a/tools/perf/bench/Build b/tools/perf/bench/Build index 6b6155a8ad09..0f158dc8139b 100644 --- a/tools/perf/bench/Build +++ b/tools/perf/bench/Build @@ -15,6 +15,7 @@ perf-y += find-bit-bench.o perf-y += inject-buildid.o perf-y += evlist-open-close.o perf-y += breakpoint.o +perf-y += pmu-scan.o perf-$(CONFIG_X86_64) += mem-memcpy-x86-64-asm.o perf-$(CONFIG_X86_64) += mem-memset-x86-64-asm.o diff --git a/tools/perf/bench/bench.h b/tools/perf/bench/bench.h index edfc25793cca..0d2b65976212 100644 --- a/tools/perf/bench/bench.h +++ b/tools/perf/bench/bench.h @@ -42,6 +42,7 @@ int bench_inject_build_id(int argc, const char **argv); int bench_evlist_open_close(int argc, const char **argv); int bench_breakpoint_thread(int argc, const char **argv); int bench_breakpoint_enable(int argc, const char **argv); +int bench_pmu_scan(int argc, const char **argv); #define BENCH_FORMAT_DEFAULT_STR "default" #define BENCH_FORMAT_DEFAULT 0 diff --git a/tools/perf/bench/pmu-scan.c b/tools/perf/bench/pmu-scan.c new file mode 100644 index 000000000000..f0f007843bb8 --- /dev/null +++ b/tools/perf/bench/pmu-scan.c @@ -0,0 +1,184 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Benchmark scanning sysfs files for PMU information. + * + * Copyright 2023 Google LLC. + */ +#include +#include "bench.h" +#include "util/debug.h" +#include "util/pmu.h" +#include "util/pmus.h" +#include "util/stat.h" +#include +#include +#include +#include + +static unsigned int iterations = 100; + +struct pmu_scan_result { + char *name; + int nr_aliases; + int nr_formats; + int nr_caps; +}; + +static const struct option options[] = { + OPT_UINTEGER('i', "iterations", &iterations, + "Number of iterations used to compute average"), + OPT_END() +}; + +static const char *const bench_usage[] = { + "perf bench internals pmu-scan ", + NULL +}; + +static int nr_pmus; +static struct pmu_scan_result *results; + +static int save_result(void) +{ + struct perf_pmu *pmu; + struct list_head *list; + struct pmu_scan_result *r; + + perf_pmu__scan(NULL); + + perf_pmus__for_each_pmu(pmu) { + r = realloc(results, (nr_pmus + 1) * sizeof(*r)); + if (r == NULL) + return -ENOMEM; + + results = r; + r = results + nr_pmus; + + r->name = strdup(pmu->name); + r->nr_caps = pmu->nr_caps; + + r->nr_aliases = 0; + list_for_each(list, &pmu->aliases) + r->nr_aliases++; + + r->nr_formats = 0; + list_for_each(list, &pmu->format) + r->nr_formats++; + + pr_debug("pmu[%d] name=%s, nr_caps=%d, nr_aliases=%d, nr_formats=%d\n", + nr_pmus, r->name, r->nr_caps, r->nr_aliases, r->nr_formats); + nr_pmus++; + } + + perf_pmu__destroy(); + return 0; +} + +static int check_result(void) +{ + struct pmu_scan_result *r; + struct perf_pmu *pmu; + struct list_head *list; + int nr; + + for (int i = 0; i < nr_pmus; i++) { + r = &results[i]; + pmu = perf_pmu__find(r->name); + if (pmu == NULL) { + pr_err("Cannot find PMU %s\n", r->name); + return -1; + } + + if (pmu->nr_caps != (u32)r->nr_caps) { + pr_err("Unmatched number of event caps in %s: expect %d vs got %d\n", + pmu->name, r->nr_caps, pmu->nr_caps); + return -1; + } + + nr = 0; + list_for_each(list, &pmu->aliases) + nr++; + if (nr != r->nr_aliases) { + pr_err("Unmatched number of event aliases in %s: expect %d vs got %d\n", + pmu->name, r->nr_aliases, nr); + return -1; + } + + nr = 0; + list_for_each(list, &pmu->format) + nr++; + if (nr != r->nr_formats) { + pr_err("Unmatched number of event formats in %s: expect %d vs got %d\n", + pmu->name, r->nr_formats, nr); + return -1; + } + } + return 0; +} + +static void delete_result(void) +{ + for (int i = 0; i < nr_pmus; i++) + free(results[i].name); + free(results); + + results = NULL; + nr_pmus = 0; +} + +static int run_pmu_scan(void) +{ + struct stats stats; + struct timeval start, end, diff; + double time_average, time_stddev; + u64 runtime_us; + unsigned int i; + int ret; + + init_stats(&stats); + pr_info("Computing performance of sysfs PMU event scan for %u times\n", + iterations); + + if (save_result() < 0) { + pr_err("Failed to initialize PMU scan result\n"); + return -1; + } + + for (i = 0; i < iterations; i++) { + gettimeofday(&start, NULL); + perf_pmu__scan(NULL); + gettimeofday(&end, NULL); + + timersub(&end, &start, &diff); + runtime_us = diff.tv_sec * USEC_PER_SEC + diff.tv_usec; + update_stats(&stats, runtime_us); + + ret = check_result(); + perf_pmu__destroy(); + if (ret < 0) + break; + } + + time_average = avg_stats(&stats); + time_stddev = stddev_stats(&stats); + pr_info(" Average PMU scanning took: %.3f usec (+- %.3f usec)\n", + time_average, time_stddev); + + delete_result(); + return 0; +} + +int bench_pmu_scan(int argc, const char **argv) +{ + int err = 0; + + argc = parse_options(argc, argv, options, bench_usage, 0); + if (argc) { + usage_with_options(bench_usage, options); + exit(EXIT_FAILURE); + } + + err = run_pmu_scan(); + + return err; +} diff --git a/tools/perf/builtin-bench.c b/tools/perf/builtin-bench.c index d0fcc3cdc185..58f1cfe1eb34 100644 --- a/tools/perf/builtin-bench.c +++ b/tools/perf/builtin-bench.c @@ -92,6 +92,7 @@ static struct bench internals_benchmarks[] = { { "kallsyms-parse", "Benchmark kallsyms parsing", bench_kallsyms_parse }, { "inject-build-id", "Benchmark build-id injection", bench_inject_build_id }, { "evlist-open-close", "Benchmark evlist open and close", bench_evlist_open_close }, + { "pmu-scan", "Benchmark sysfs PMU info scanning", bench_pmu_scan }, { NULL, NULL, NULL } };