Merge remote-tracking branch 'acme/perf-tools' into perf-tools-next
[platform/kernel/linux-rpi.git] / tools / perf / builtin-stat.c
index fa7c409..cfc7551 100644 (file)
 
 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;
 
@@ -489,7 +424,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 +433,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)
@@ -970,7 +903,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 +1183,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 +1650,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 +1770,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 +1804,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;
+                       stat_config.topdown_level = 1;
 
-               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;
-               }
-
-               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 +1875,19 @@ 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.
+                */
+               if (metricgroup__has_metric("TopdownL1") &&
+                   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 +2124,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;
 
@@ -2514,12 +2397,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;