1f6a7bbbfSNamhyung Kim // SPDX-License-Identifier: GPL-2.0 2f6a7bbbfSNamhyung Kim /* 3f6a7bbbfSNamhyung Kim * Benchmark scanning sysfs files for PMU information. 4f6a7bbbfSNamhyung Kim * 5f6a7bbbfSNamhyung Kim * Copyright 2023 Google LLC. 6f6a7bbbfSNamhyung Kim */ 7f6a7bbbfSNamhyung Kim #include <stdio.h> 8f6a7bbbfSNamhyung Kim #include "bench.h" 9f6a7bbbfSNamhyung Kim #include "util/debug.h" 10f6a7bbbfSNamhyung Kim #include "util/pmu.h" 11f6a7bbbfSNamhyung Kim #include "util/pmus.h" 12f6a7bbbfSNamhyung Kim #include "util/stat.h" 13f6a7bbbfSNamhyung Kim #include <linux/atomic.h> 14f6a7bbbfSNamhyung Kim #include <linux/err.h> 15f6a7bbbfSNamhyung Kim #include <linux/time64.h> 16f6a7bbbfSNamhyung Kim #include <subcmd/parse-options.h> 17f6a7bbbfSNamhyung Kim 18f6a7bbbfSNamhyung Kim static unsigned int iterations = 100; 19f6a7bbbfSNamhyung Kim 20f6a7bbbfSNamhyung Kim struct pmu_scan_result { 21f6a7bbbfSNamhyung Kim char *name; 22f6a7bbbfSNamhyung Kim int nr_aliases; 23f6a7bbbfSNamhyung Kim int nr_formats; 24f6a7bbbfSNamhyung Kim int nr_caps; 25*9d6a1df9SIan Rogers bool is_core; 26f6a7bbbfSNamhyung Kim }; 27f6a7bbbfSNamhyung Kim 28f6a7bbbfSNamhyung Kim static const struct option options[] = { 29f6a7bbbfSNamhyung Kim OPT_UINTEGER('i', "iterations", &iterations, 30f6a7bbbfSNamhyung Kim "Number of iterations used to compute average"), 31f6a7bbbfSNamhyung Kim OPT_END() 32f6a7bbbfSNamhyung Kim }; 33f6a7bbbfSNamhyung Kim 34f6a7bbbfSNamhyung Kim static const char *const bench_usage[] = { 35f6a7bbbfSNamhyung Kim "perf bench internals pmu-scan <options>", 36f6a7bbbfSNamhyung Kim NULL 37f6a7bbbfSNamhyung Kim }; 38f6a7bbbfSNamhyung Kim 39f6a7bbbfSNamhyung Kim static int nr_pmus; 40f6a7bbbfSNamhyung Kim static struct pmu_scan_result *results; 41f6a7bbbfSNamhyung Kim 42f6a7bbbfSNamhyung Kim static int save_result(void) 43f6a7bbbfSNamhyung Kim { 44f24ebe80SIan Rogers struct perf_pmu *pmu = NULL; 45f6a7bbbfSNamhyung Kim struct list_head *list; 46f6a7bbbfSNamhyung Kim struct pmu_scan_result *r; 47f6a7bbbfSNamhyung Kim 481eaf496eSIan Rogers while ((pmu = perf_pmus__scan(pmu)) != NULL) { 49f6a7bbbfSNamhyung Kim r = realloc(results, (nr_pmus + 1) * sizeof(*r)); 50f6a7bbbfSNamhyung Kim if (r == NULL) 51f6a7bbbfSNamhyung Kim return -ENOMEM; 52f6a7bbbfSNamhyung Kim 53f6a7bbbfSNamhyung Kim results = r; 54f6a7bbbfSNamhyung Kim r = results + nr_pmus; 55f6a7bbbfSNamhyung Kim 56f6a7bbbfSNamhyung Kim r->name = strdup(pmu->name); 57*9d6a1df9SIan Rogers r->is_core = pmu->is_core; 58f6a7bbbfSNamhyung Kim r->nr_caps = pmu->nr_caps; 59f6a7bbbfSNamhyung Kim 60f6a7bbbfSNamhyung Kim r->nr_aliases = 0; 61f6a7bbbfSNamhyung Kim list_for_each(list, &pmu->aliases) 62f6a7bbbfSNamhyung Kim r->nr_aliases++; 63f6a7bbbfSNamhyung Kim 64f6a7bbbfSNamhyung Kim r->nr_formats = 0; 65f6a7bbbfSNamhyung Kim list_for_each(list, &pmu->format) 66f6a7bbbfSNamhyung Kim r->nr_formats++; 67f6a7bbbfSNamhyung Kim 68f6a7bbbfSNamhyung Kim pr_debug("pmu[%d] name=%s, nr_caps=%d, nr_aliases=%d, nr_formats=%d\n", 69f6a7bbbfSNamhyung Kim nr_pmus, r->name, r->nr_caps, r->nr_aliases, r->nr_formats); 70f6a7bbbfSNamhyung Kim nr_pmus++; 71f6a7bbbfSNamhyung Kim } 72f6a7bbbfSNamhyung Kim 731eaf496eSIan Rogers perf_pmus__destroy(); 74f6a7bbbfSNamhyung Kim return 0; 75f6a7bbbfSNamhyung Kim } 76f6a7bbbfSNamhyung Kim 77*9d6a1df9SIan Rogers static int check_result(bool core_only) 78f6a7bbbfSNamhyung Kim { 79f6a7bbbfSNamhyung Kim struct pmu_scan_result *r; 80f6a7bbbfSNamhyung Kim struct perf_pmu *pmu; 81f6a7bbbfSNamhyung Kim struct list_head *list; 82f6a7bbbfSNamhyung Kim int nr; 83f6a7bbbfSNamhyung Kim 84f6a7bbbfSNamhyung Kim for (int i = 0; i < nr_pmus; i++) { 85f6a7bbbfSNamhyung Kim r = &results[i]; 86*9d6a1df9SIan Rogers if (core_only && !r->is_core) 87*9d6a1df9SIan Rogers continue; 88*9d6a1df9SIan Rogers 891eaf496eSIan Rogers pmu = perf_pmus__find(r->name); 90f6a7bbbfSNamhyung Kim if (pmu == NULL) { 91f6a7bbbfSNamhyung Kim pr_err("Cannot find PMU %s\n", r->name); 92f6a7bbbfSNamhyung Kim return -1; 93f6a7bbbfSNamhyung Kim } 94f6a7bbbfSNamhyung Kim 95f6a7bbbfSNamhyung Kim if (pmu->nr_caps != (u32)r->nr_caps) { 96f6a7bbbfSNamhyung Kim pr_err("Unmatched number of event caps in %s: expect %d vs got %d\n", 97f6a7bbbfSNamhyung Kim pmu->name, r->nr_caps, pmu->nr_caps); 98f6a7bbbfSNamhyung Kim return -1; 99f6a7bbbfSNamhyung Kim } 100f6a7bbbfSNamhyung Kim 101f6a7bbbfSNamhyung Kim nr = 0; 102f6a7bbbfSNamhyung Kim list_for_each(list, &pmu->aliases) 103f6a7bbbfSNamhyung Kim nr++; 104f6a7bbbfSNamhyung Kim if (nr != r->nr_aliases) { 105f6a7bbbfSNamhyung Kim pr_err("Unmatched number of event aliases in %s: expect %d vs got %d\n", 106f6a7bbbfSNamhyung Kim pmu->name, r->nr_aliases, nr); 107f6a7bbbfSNamhyung Kim return -1; 108f6a7bbbfSNamhyung Kim } 109f6a7bbbfSNamhyung Kim 110f6a7bbbfSNamhyung Kim nr = 0; 111f6a7bbbfSNamhyung Kim list_for_each(list, &pmu->format) 112f6a7bbbfSNamhyung Kim nr++; 113f6a7bbbfSNamhyung Kim if (nr != r->nr_formats) { 114f6a7bbbfSNamhyung Kim pr_err("Unmatched number of event formats in %s: expect %d vs got %d\n", 115f6a7bbbfSNamhyung Kim pmu->name, r->nr_formats, nr); 116f6a7bbbfSNamhyung Kim return -1; 117f6a7bbbfSNamhyung Kim } 118f6a7bbbfSNamhyung Kim } 119f6a7bbbfSNamhyung Kim return 0; 120f6a7bbbfSNamhyung Kim } 121f6a7bbbfSNamhyung Kim 122f6a7bbbfSNamhyung Kim static void delete_result(void) 123f6a7bbbfSNamhyung Kim { 124f6a7bbbfSNamhyung Kim for (int i = 0; i < nr_pmus; i++) 125f6a7bbbfSNamhyung Kim free(results[i].name); 126f6a7bbbfSNamhyung Kim free(results); 127f6a7bbbfSNamhyung Kim 128f6a7bbbfSNamhyung Kim results = NULL; 129f6a7bbbfSNamhyung Kim nr_pmus = 0; 130f6a7bbbfSNamhyung Kim } 131f6a7bbbfSNamhyung Kim 132f6a7bbbfSNamhyung Kim static int run_pmu_scan(void) 133f6a7bbbfSNamhyung Kim { 134f6a7bbbfSNamhyung Kim struct stats stats; 135f6a7bbbfSNamhyung Kim struct timeval start, end, diff; 136f6a7bbbfSNamhyung Kim double time_average, time_stddev; 137f6a7bbbfSNamhyung Kim u64 runtime_us; 138f6a7bbbfSNamhyung Kim int ret; 139f6a7bbbfSNamhyung Kim 140f6a7bbbfSNamhyung Kim init_stats(&stats); 141f6a7bbbfSNamhyung Kim pr_info("Computing performance of sysfs PMU event scan for %u times\n", 142f6a7bbbfSNamhyung Kim iterations); 143f6a7bbbfSNamhyung Kim 144f6a7bbbfSNamhyung Kim if (save_result() < 0) { 145f6a7bbbfSNamhyung Kim pr_err("Failed to initialize PMU scan result\n"); 146f6a7bbbfSNamhyung Kim return -1; 147f6a7bbbfSNamhyung Kim } 148f6a7bbbfSNamhyung Kim 149*9d6a1df9SIan Rogers for (int j = 0; j < 2; j++) { 150*9d6a1df9SIan Rogers bool core_only = (j == 0); 151*9d6a1df9SIan Rogers 152*9d6a1df9SIan Rogers for (unsigned int i = 0; i < iterations; i++) { 153f6a7bbbfSNamhyung Kim gettimeofday(&start, NULL); 154*9d6a1df9SIan Rogers if (core_only) 155*9d6a1df9SIan Rogers perf_pmus__scan_core(NULL); 156*9d6a1df9SIan Rogers else 1571eaf496eSIan Rogers perf_pmus__scan(NULL); 158f6a7bbbfSNamhyung Kim gettimeofday(&end, NULL); 159f6a7bbbfSNamhyung Kim timersub(&end, &start, &diff); 160f6a7bbbfSNamhyung Kim runtime_us = diff.tv_sec * USEC_PER_SEC + diff.tv_usec; 161f6a7bbbfSNamhyung Kim update_stats(&stats, runtime_us); 162f6a7bbbfSNamhyung Kim 163*9d6a1df9SIan Rogers ret = check_result(core_only); 1641eaf496eSIan Rogers perf_pmus__destroy(); 165f6a7bbbfSNamhyung Kim if (ret < 0) 166f6a7bbbfSNamhyung Kim break; 167f6a7bbbfSNamhyung Kim } 168f6a7bbbfSNamhyung Kim time_average = avg_stats(&stats); 169f6a7bbbfSNamhyung Kim time_stddev = stddev_stats(&stats); 170*9d6a1df9SIan Rogers pr_info(" Average%s PMU scanning took: %.3f usec (+- %.3f usec)\n", 171*9d6a1df9SIan Rogers core_only ? " core" : "", time_average, time_stddev); 172*9d6a1df9SIan Rogers } 173f6a7bbbfSNamhyung Kim delete_result(); 174f6a7bbbfSNamhyung Kim return 0; 175f6a7bbbfSNamhyung Kim } 176f6a7bbbfSNamhyung Kim 177f6a7bbbfSNamhyung Kim int bench_pmu_scan(int argc, const char **argv) 178f6a7bbbfSNamhyung Kim { 179f6a7bbbfSNamhyung Kim int err = 0; 180f6a7bbbfSNamhyung Kim 181f6a7bbbfSNamhyung Kim argc = parse_options(argc, argv, options, bench_usage, 0); 182f6a7bbbfSNamhyung Kim if (argc) { 183f6a7bbbfSNamhyung Kim usage_with_options(bench_usage, options); 184f6a7bbbfSNamhyung Kim exit(EXIT_FAILURE); 185f6a7bbbfSNamhyung Kim } 186f6a7bbbfSNamhyung Kim 187f6a7bbbfSNamhyung Kim err = run_pmu_scan(); 188f6a7bbbfSNamhyung Kim 189f6a7bbbfSNamhyung Kim return err; 190f6a7bbbfSNamhyung Kim } 191