]> Git Repo - J-linux.git/blobdiff - tools/perf/builtin-stat.c
Merge tag 'amd-drm-next-6.5-2023-06-09' of https://gitlab.freedesktop.org/agd5f/linux...
[J-linux.git] / tools / perf / builtin-stat.c
index fa7c40956d0fa957c95b8755db61d83e0ee1cc4b..cc9fa48d636f02039a31aec60a9be0dc0c97bfc0 100644 (file)
@@ -41,7 +41,6 @@
  */
 
 #include "builtin.h"
-#include "perf.h"
 #include "util/cgroup.h"
 #include <subcmd/parse-options.h>
 #include "util/parse-events.h"
@@ -71,6 +70,7 @@
 #include "util/bpf_counter.h"
 #include "util/iostat.h"
 #include "util/pmu-hybrid.h"
+#include "util/util.h"
 #include "asm/bug.h"
 
 #include <linux/time64.h>
 
 static void print_counters(struct timespec *ts, int argc, const char **argv);
 
-/* Default events used for perf stat -T */
-static const char *transaction_attrs = {
-       "task-clock,"
-       "{"
-       "instructions,"
-       "cycles,"
-       "cpu/cycles-t/,"
-       "cpu/tx-start/,"
-       "cpu/el-start/,"
-       "cpu/cycles-ct/"
-       "}"
-};
-
-/* More limited version when the CPU does not have all events. */
-static const char * transaction_limited_attrs = {
-       "task-clock,"
-       "{"
-       "instructions,"
-       "cycles,"
-       "cpu/cycles-t/,"
-       "cpu/tx-start/"
-       "}"
-};
-
-static const char * topdown_attrs[] = {
-       "topdown-total-slots",
-       "topdown-slots-retired",
-       "topdown-recovery-bubbles",
-       "topdown-fetch-bubbles",
-       "topdown-slots-issued",
-       NULL,
-};
-
-static const char *topdown_metric_attrs[] = {
-       "slots",
-       "topdown-retiring",
-       "topdown-bad-spec",
-       "topdown-fe-bound",
-       "topdown-be-bound",
-       NULL,
-};
-
-static const char *topdown_metric_L2_attrs[] = {
-       "slots",
-       "topdown-retiring",
-       "topdown-bad-spec",
-       "topdown-fe-bound",
-       "topdown-be-bound",
-       "topdown-heavy-ops",
-       "topdown-br-mispredict",
-       "topdown-fetch-lat",
-       "topdown-mem-bound",
-       NULL,
-};
-
-#define TOPDOWN_MAX_LEVEL                      2
-
-static const char *smi_cost_attrs = {
-       "{"
-       "msr/aperf/,"
-       "msr/smi/,"
-       "cycles"
-       "}"
-};
-
 static struct evlist   *evsel_list;
 static bool all_counters_use_bpf = true;
 
@@ -246,14 +181,13 @@ static bool cpus_map_matched(struct evsel *a, struct evsel *b)
 
 static void evlist__check_cpu_maps(struct evlist *evlist)
 {
-       struct evsel *evsel, *pos, *leader;
-       char buf[1024];
+       struct evsel *evsel, *warned_leader = NULL;
 
        if (evlist__has_hybrid(evlist))
                evlist__warn_hybrid_group(evlist);
 
        evlist__for_each_entry(evlist, evsel) {
-               leader = evsel__leader(evsel);
+               struct evsel *leader = evsel__leader(evsel);
 
                /* Check that leader matches cpus with each member. */
                if (leader == evsel)
@@ -262,19 +196,26 @@ static void evlist__check_cpu_maps(struct evlist *evlist)
                        continue;
 
                /* If there's mismatch disable the group and warn user. */
-               WARN_ONCE(1, "WARNING: grouped events cpus do not match, disabling group:\n");
-               evsel__group_desc(leader, buf, sizeof(buf));
-               pr_warning("  %s\n", buf);
-
+               if (warned_leader != leader) {
+                       char buf[200];
+
+                       pr_warning("WARNING: grouped events cpus do not match.\n"
+                               "Events with CPUs not matching the leader will "
+                               "be removed from the group.\n");
+                       evsel__group_desc(leader, buf, sizeof(buf));
+                       pr_warning("  %s\n", buf);
+                       warned_leader = leader;
+               }
                if (verbose > 0) {
+                       char buf[200];
+
                        cpu_map__snprint(leader->core.cpus, buf, sizeof(buf));
                        pr_warning("     %s: %s\n", leader->name, buf);
                        cpu_map__snprint(evsel->core.cpus, buf, sizeof(buf));
                        pr_warning("     %s: %s\n", evsel->name, buf);
                }
 
-               for_each_group_evsel(pos, leader)
-                       evsel__remove_from_group(pos, leader);
+               evsel__remove_from_group(evsel, leader);
        }
 }
 
@@ -489,7 +430,6 @@ static void process_counters(void)
 
        perf_stat_merge_counters(&stat_config, evsel_list);
        perf_stat_process_percore(&stat_config, evsel_list);
-       perf_stat_process_shadow_stats(&stat_config, evsel_list);
 }
 
 static void process_interval(void)
@@ -499,7 +439,6 @@ static void process_interval(void)
        clock_gettime(CLOCK_MONOTONIC, &ts);
        diff_timespec(&rs, &ts, &ref_time);
 
-       perf_stat__reset_shadow_per_stat(&rt_stat);
        evlist__reset_aggr_stats(evsel_list);
 
        if (read_counters(&rs) == 0)
@@ -610,7 +549,7 @@ static void process_evlist(struct evlist *evlist, unsigned int interval)
        if (evlist__ctlfd_process(evlist, &cmd) > 0) {
                switch (cmd) {
                case EVLIST_CTL_CMD_ENABLE:
-                       __fallthrough;
+                       fallthrough;
                case EVLIST_CTL_CMD_DISABLE:
                        if (interval)
                                process_interval();
@@ -773,7 +712,7 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
                counter->reset_group = false;
                if (bpf_counter__load(counter, &target))
                        return -1;
-               if (!evsel__is_bpf(counter))
+               if (!(evsel__is_bperf(counter)))
                        all_counters_use_bpf = false;
        }
 
@@ -789,7 +728,7 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
 
                if (counter->reset_group || counter->errored)
                        continue;
-               if (evsel__is_bpf(counter))
+               if (evsel__is_bperf(counter))
                        continue;
 try_again:
                if (create_perf_stat_counter(counter, &stat_config, &target,
@@ -970,7 +909,6 @@ try_again_reset:
                evlist__copy_prev_raw_counts(evsel_list);
                evlist__reset_prev_raw_counts(evsel_list);
                evlist__reset_aggr_stats(evsel_list);
-               perf_stat__reset_shadow_per_stat(&rt_stat);
        } else {
                update_stats(&walltime_nsecs_stats, t1 - t0);
                update_rusage_stats(&ru_stats, &stat_config.ru_data);
@@ -1251,6 +1189,8 @@ static struct option stat_options[] = {
                       "don't group metric events, impacts multiplexing"),
        OPT_BOOLEAN(0, "metric-no-merge", &stat_config.metric_no_merge,
                       "don't try to share events between metrics in a group"),
+       OPT_BOOLEAN(0, "metric-no-threshold", &stat_config.metric_no_threshold,
+                      "don't try to share events between metrics in a group  "),
        OPT_BOOLEAN(0, "topdown", &topdown_run,
                        "measure top-down statistics"),
        OPT_UINTEGER(0, "td-level", &stat_config.topdown_level,
@@ -1716,7 +1656,6 @@ static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
  */
 static int add_default_attributes(void)
 {
-       int err;
        struct perf_event_attr default_attrs0[] = {
 
   { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK             },
@@ -1837,44 +1776,29 @@ static int add_default_attributes(void)
                return 0;
 
        if (transaction_run) {
-               struct parse_events_error errinfo;
                /* Handle -T as -M transaction. Once platform specific metrics
                 * support has been added to the json files, all architectures
                 * will use this approach. To determine transaction support
                 * on an architecture test for such a metric name.
                 */
-               if (metricgroup__has_metric("transaction")) {
-                       return metricgroup__parse_groups(evsel_list, "transaction",
-                                                        stat_config.metric_no_group,
-                                                        stat_config.metric_no_merge,
-                                                        stat_config.user_requested_cpu_list,
-                                                        stat_config.system_wide,
-                                                        &stat_config.metric_events);
-               }
-
-               parse_events_error__init(&errinfo);
-               if (pmu_have_event("cpu", "cycles-ct") &&
-                   pmu_have_event("cpu", "el-start"))
-                       err = parse_events(evsel_list, transaction_attrs,
-                                          &errinfo);
-               else
-                       err = parse_events(evsel_list,
-                                          transaction_limited_attrs,
-                                          &errinfo);
-               if (err) {
-                       fprintf(stderr, "Cannot set up transaction events\n");
-                       parse_events_error__print(&errinfo, transaction_attrs);
+               if (!metricgroup__has_metric("transaction")) {
+                       pr_err("Missing transaction metrics");
+                       return -1;
                }
-               parse_events_error__exit(&errinfo);
-               return err ? -1 : 0;
+               return metricgroup__parse_groups(evsel_list, "transaction",
+                                               stat_config.metric_no_group,
+                                               stat_config.metric_no_merge,
+                                               stat_config.metric_no_threshold,
+                                               stat_config.user_requested_cpu_list,
+                                               stat_config.system_wide,
+                                               &stat_config.metric_events);
        }
 
        if (smi_cost) {
-               struct parse_events_error errinfo;
                int smi;
 
                if (sysfs__read_int(FREEZE_ON_SMI_PATH, &smi) < 0) {
-                       fprintf(stderr, "freeze_on_smi is not supported.\n");
+                       pr_err("freeze_on_smi is not supported.");
                        return -1;
                }
 
@@ -1886,108 +1810,62 @@ static int add_default_attributes(void)
                        smi_reset = true;
                }
 
-               if (!pmu_have_event("msr", "aperf") ||
-                   !pmu_have_event("msr", "smi")) {
-                       fprintf(stderr, "To measure SMI cost, it needs "
-                               "msr/aperf/, msr/smi/ and cpu/cycles/ support\n");
+               if (!metricgroup__has_metric("smi")) {
+                       pr_err("Missing smi metrics");
                        return -1;
                }
+
                if (!force_metric_only)
                        stat_config.metric_only = true;
 
-               parse_events_error__init(&errinfo);
-               err = parse_events(evsel_list, smi_cost_attrs, &errinfo);
-               if (err) {
-                       parse_events_error__print(&errinfo, smi_cost_attrs);
-                       fprintf(stderr, "Cannot set up SMI cost events\n");
-               }
-               parse_events_error__exit(&errinfo);
-               return err ? -1 : 0;
+               return metricgroup__parse_groups(evsel_list, "smi",
+                                               stat_config.metric_no_group,
+                                               stat_config.metric_no_merge,
+                                               stat_config.metric_no_threshold,
+                                               stat_config.user_requested_cpu_list,
+                                               stat_config.system_wide,
+                                               &stat_config.metric_events);
        }
 
        if (topdown_run) {
-               const char **metric_attrs = topdown_metric_attrs;
-               unsigned int max_level = 1;
-               char *str = NULL;
-               bool warn = false;
-               const char *pmu_name = arch_get_topdown_pmu_name(evsel_list, true);
+               unsigned int max_level = metricgroups__topdown_max_level();
+               char str[] = "TopdownL1";
 
                if (!force_metric_only)
                        stat_config.metric_only = true;
 
-               if (pmu_have_event(pmu_name, topdown_metric_L2_attrs[5])) {
-                       metric_attrs = topdown_metric_L2_attrs;
-                       max_level = 2;
+               if (!max_level) {
+                       pr_err("Topdown requested but the topdown metric groups aren't present.\n"
+                               "(See perf list the metric groups have names like TopdownL1)");
+                       return -1;
                }
-
                if (stat_config.topdown_level > max_level) {
                        pr_err("Invalid top-down metrics level. The max level is %u.\n", max_level);
                        return -1;
                } else if (!stat_config.topdown_level)
-                       stat_config.topdown_level = max_level;
-
-               if (topdown_filter_events(metric_attrs, &str, 1, pmu_name) < 0) {
-                       pr_err("Out of memory\n");
-                       return -1;
-               }
-
-               if (metric_attrs[0] && str) {
-                       if (!stat_config.interval && !stat_config.metric_only) {
-                               fprintf(stat_config.output,
-                                       "Topdown accuracy may decrease when measuring long periods.\n"
-                                       "Please print the result regularly, e.g. -I1000\n");
-                       }
-                       goto setup_metrics;
-               }
+                       stat_config.topdown_level = 1;
 
-               zfree(&str);
-
-               if (stat_config.aggr_mode != AGGR_GLOBAL &&
-                   stat_config.aggr_mode != AGGR_CORE) {
-                       pr_err("top down event configuration requires --per-core mode\n");
-                       return -1;
-               }
-               stat_config.aggr_mode = AGGR_CORE;
-               if (nr_cgroups || !target__has_cpu(&target)) {
-                       pr_err("top down event configuration requires system-wide mode (-a)\n");
-                       return -1;
-               }
-
-               if (topdown_filter_events(topdown_attrs, &str,
-                               arch_topdown_check_group(&warn),
-                               pmu_name) < 0) {
-                       pr_err("Out of memory\n");
-                       return -1;
+               if (!stat_config.interval && !stat_config.metric_only) {
+                       fprintf(stat_config.output,
+                               "Topdown accuracy may decrease when measuring long periods.\n"
+                               "Please print the result regularly, e.g. -I1000\n");
                }
-
-               if (topdown_attrs[0] && str) {
-                       struct parse_events_error errinfo;
-                       if (warn)
-                               arch_topdown_group_warn();
-setup_metrics:
-                       parse_events_error__init(&errinfo);
-                       err = parse_events(evsel_list, str, &errinfo);
-                       if (err) {
-                               fprintf(stderr,
-                                       "Cannot set up top down events %s: %d\n",
-                                       str, err);
-                               parse_events_error__print(&errinfo, str);
-                               parse_events_error__exit(&errinfo);
-                               free(str);
-                               return -1;
-                       }
-                       parse_events_error__exit(&errinfo);
-               } else {
-                       fprintf(stderr, "System does not support topdown\n");
+               str[8] = stat_config.topdown_level + '0';
+               if (metricgroup__parse_groups(evsel_list, str,
+                                               /*metric_no_group=*/false,
+                                               /*metric_no_merge=*/false,
+                                               /*metric_no_threshold=*/true,
+                                               stat_config.user_requested_cpu_list,
+                                               stat_config.system_wide,
+                                               &stat_config.metric_events) < 0)
                        return -1;
-               }
-               free(str);
        }
 
        if (!stat_config.topdown_level)
-               stat_config.topdown_level = TOPDOWN_MAX_LEVEL;
+               stat_config.topdown_level = 1;
 
        if (!evsel_list->core.nr_entries) {
+               /* No events so add defaults. */
                if (target__has_cpu(&target))
                        default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
 
@@ -2003,6 +1881,25 @@ setup_metrics:
                }
                if (evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
                        return -1;
+               /*
+                * Add TopdownL1 metrics if they exist. To minimize
+                * multiplexing, don't request threshold computation.
+                */
+               /*
+                * TODO: TopdownL1 is disabled on hybrid CPUs to avoid a crashes
+                * caused by exposing latent bugs. This is fixed properly in:
+                * https://lore.kernel.org/lkml/[email protected]/
+                */
+               if (metricgroup__has_metric("TopdownL1") && !perf_pmu__has_hybrid() &&
+                   metricgroup__parse_groups(evsel_list, "TopdownL1",
+                                           /*metric_no_group=*/false,
+                                           /*metric_no_merge=*/false,
+                                           /*metric_no_threshold=*/true,
+                                           stat_config.user_requested_cpu_list,
+                                           stat_config.system_wide,
+                                           &stat_config.metric_events) < 0)
+                       return -1;
+
                /* Platform specific attrs */
                if (evlist__add_default_attrs(evsel_list, default_null_attrs) < 0)
                        return -1;
@@ -2239,8 +2136,6 @@ static int __cmd_report(int argc, const char **argv)
                        input_name = "perf.data";
        }
 
-       perf_stat__init_shadow_stats();
-
        perf_stat.data.path = input_name;
        perf_stat.data.mode = PERF_DATA_MODE_READ;
 
@@ -2281,7 +2176,7 @@ static void setup_system_wide(int forks)
 
                evlist__for_each_entry(evsel_list, counter) {
                        if (!counter->core.requires_cpu &&
-                           strcmp(counter->name, "duration_time")) {
+                           !evsel__name_is(counter, "duration_time")) {
                                return;
                        }
                }
@@ -2383,8 +2278,10 @@ int cmd_stat(int argc, const char **argv)
                        perror("failed to create output file");
                        return -1;
                }
-               clock_gettime(CLOCK_REALTIME, &tm);
-               fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
+               if (!stat_config.json_output) {
+                       clock_gettime(CLOCK_REALTIME, &tm);
+                       fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
+               }
        } else if (output_fd > 0) {
                mode = append_file ? "a" : "w";
                output = fdopen(output_fd, mode);
@@ -2514,12 +2411,12 @@ int cmd_stat(int argc, const char **argv)
                metricgroup__parse_groups(evsel_list, metrics,
                                        stat_config.metric_no_group,
                                        stat_config.metric_no_merge,
+                                       stat_config.metric_no_threshold,
                                        stat_config.user_requested_cpu_list,
                                        stat_config.system_wide,
                                        &stat_config.metric_events);
                zfree(&metrics);
        }
-       perf_stat__init_shadow_stats();
 
        if (add_default_attributes())
                goto out;
This page took 0.0421 seconds and 4 git commands to generate.