Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp1586798rwl; Fri, 31 Mar 2023 13:31:10 -0700 (PDT) X-Google-Smtp-Source: AKy350ZmJ9gg2wlAyVK1QteaY83KklZDCic4LgLpq+BAZy5OotlJMByOlKWq2/QPsOb3nMcceAN3 X-Received: by 2002:a05:6a20:72a3:b0:e4:173c:cdec with SMTP id o35-20020a056a2072a300b000e4173ccdecmr6683358pzk.0.1680294670116; Fri, 31 Mar 2023 13:31:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680294670; cv=none; d=google.com; s=arc-20160816; b=c6xaj3+SOBTsFVOLmE4L+KnD0AD5Yen5/0x360F7FXRavqNWXQhEtXpqxnlxlJPIAU YqWxP/78MwnDVx+bSzXYN/FNPwX0x86Q7SsTmnYNucSNAUWWViO9JNgcR8+5DXsFLSoW aRQ+YZ15KfP80Wy/hqXJrt9DjTPf92hnGoSHkpVlfEz29gp8WpVCuVKziWe88L4htasq S4l9/wLCLMHcn1wOmVuUEfMhCCMl8U4i20ff0/nc8AWDPCx53KrDC2BOmGR9VY3f0h9h 7ja8j2FI1gRdDQic4eikPl84M83tzCWDYs0AGEdJljoTzioA8A4aLPaJ+lottwWZ56Sl m1TQ== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h18-20020aa79f52000000b005a8b856ad47si3285960pfr.7.2023.03.31.13.30.58; Fri, 31 Mar 2023 13:31:10 -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 S233230AbjCaUaK (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 Content-Transfer-Encoding: 8bit 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 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 } }; -- 2.40.0.348.gf938b09366-goog