Skip to contentSkip to conversation

TPEBS counting mode support

RFC PATCH Superseded [email protected] 3 participants 24 messages Mar 29, 2024 – May 15, 2024 perf events

v6 of 18 Superseded Latest is v18 →

lore
Share & tools
lore
#0 lore raw

v6 cover TPEBS counting mode support

From: Weilin Wang <[email protected]>

Changes in v5:
- Update code and add comments for better code quality [Namhyung]
- Remove the added fd var and directly pass the opened fd to data.file.fd [Namhyung]
- Add kill() to stop perf record when perf stat exists early [Namhyung]
- Add command opt check to ensure only start perf record when -a/-C given [Namhyung]
- Squash commits [Namhyung]

v5: https://lore.kernel.org/lkml/CO6PR11MB56353F87C19F5D1DD913F94FEE3A2@CO6PR11MB5635.namprd11.prod.outlook.com/

Weilin Wang (5):
perf stat: Parse and find tpebs events when parsing metrics to prepare
for perf record sampling
perf stat: Fork and launch perf record when perf stat needs to get
retire latency value for a metric.
perf stat: Add retire latency values into the expr_parse_ctx to
prepare for final metric calculation
perf stat: Add retire latency print functions to print out at the very
end of print out
perf vendor events intel: Add MTL metric json files

 tools/perf/builtin-stat.c                     |  225 +-
 .../arch/x86/meteorlake/metricgroups.json     |  127 +
 .../arch/x86/meteorlake/mtl-metrics.json      | 2551 +++++++++++++++++
 tools/perf/util/data.c                        |    6 +-
 tools/perf/util/metricgroup.c                 |   88 +-
 tools/perf/util/metricgroup.h                 |   22 +-
 tools/perf/util/stat-display.c                |   65 +
 tools/perf/util/stat-shadow.c                 |   19 +
 tools/perf/util/stat.h                        |    4 +
 9 files changed, 3087 insertions(+), 20 deletions(-)
 create mode 100644 tools/perf/pmu-events/arch/x86/meteorlake/metricgroups.json
 create mode 100644 tools/perf/pmu-events/arch/x86/meteorlake/mtl-metrics.json

--
2.43.0

#1 lore raw

RFC PATCH v6 1/5 perf stat: Parse and find tpebs events when parsing metrics to prepare for perf record sampling

From: Weilin Wang <[email protected]>

Metrics that use tpebs values would use the R as retire_latency modifier in formulas. We put all these events into a list and pass the list to perf record to collect their retire latency value.

---
 tools/perf/builtin-stat.c     | 38 +++++++++++++--
 tools/perf/util/metricgroup.c | 88 +++++++++++++++++++++++++++++------
 tools/perf/util/metricgroup.h | 10 +++-
 tools/perf/util/stat.h        |  2 +
 4 files changed, 119 insertions(+), 19 deletions(-)
tools/perf/builtin-stat.c +33 -5
index 6bba1a89d030..6291e1e24535 100644
@@ -162,6 +162,7 @@ static struct perf_stat_config stat_config = {
 	.ctl_fd			= -1,
 	.ctl_fd_ack		= -1,
 	.iostat_run		= false,
+	.tpebs_events		= LIST_HEAD_INIT(stat_config.tpebs_events),
 };
 
 static bool cpus_map_matched(struct evsel *a, struct evsel *b)
@@ -686,6 +687,12 @@ static enum counter_recovery stat_handle_error(struct evsel *counter)
 	return COUNTER_FATAL;
 }
 
+static int __run_perf_record(void)
+{
+	pr_debug("Prepare perf record for retire_latency\n");
+	return 0;
+}
+
 static int __run_perf_stat(int argc, const char **argv, int run_idx)
 {
 	int interval = stat_config.interval;
@@ -703,6 +710,16 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
 	int err;
 	bool second_pass = false;
 
+	/* Prepare perf record for sampling event retire_latency before fork and
+	 * prepare workload */
+	if (stat_config.tpebs_event_size > 0) {
+		int ret;
+
+		ret = __run_perf_record();
+		if (ret)
+			return ret;
+	}
+
 	if (forks) {
 		if (evlist__prepare_workload(evsel_list, &target, argv, is_pipe, workload_exec_failed_signal) < 0) {
 			perror("failed to prepare workload");
@@ -2106,7 +2123,9 @@ static int add_default_attributes(void)
 						stat_config.metric_no_threshold,
 						stat_config.user_requested_cpu_list,
 						stat_config.system_wide,
-						&stat_config.metric_events);
+						&stat_config.metric_events,
+						&stat_config.tpebs_events,
+						&stat_config.tpebs_event_size);
 	}
 
 	if (smi_cost) {
@@ -2139,7 +2158,9 @@ static int add_default_attributes(void)
 						stat_config.metric_no_threshold,
 						stat_config.user_requested_cpu_list,
 						stat_config.system_wide,
-						&stat_config.metric_events);
+						&stat_config.metric_events,
+						&stat_config.tpebs_events,
+						&stat_config.tpebs_event_size);
 	}
 
 	if (topdown_run) {
@@ -2173,7 +2194,9 @@ static int add_default_attributes(void)
 						/*metric_no_threshold=*/true,
 						stat_config.user_requested_cpu_list,
 						stat_config.system_wide,
-						&stat_config.metric_events) < 0)
+						&stat_config.metric_events,
+						&stat_config.tpebs_events,
+						&stat_config.tpebs_event_size) < 0)
 			return -1;
 	}
 
@@ -2214,7 +2237,9 @@ static int add_default_attributes(void)
 							/*metric_no_threshold=*/true,
 							stat_config.user_requested_cpu_list,
 							stat_config.system_wide,
-							&stat_config.metric_events) < 0)
+							&stat_config.metric_events,
+							/*&stat_config.tpebs_events=*/NULL,
+							/*stat_config.tpebs_event_size=*/0) < 0)
 				return -1;
 
 			evlist__for_each_entry(metric_evlist, metric_evsel) {
@@ -2736,6 +2761,7 @@ int cmd_stat(int argc, const char **argv)
 		}
 	}
 
+
 	/*
 	 * Metric parsing needs to be delayed as metrics may optimize events
 	 * knowing the target is system-wide.
@@ -2748,7 +2774,9 @@ int cmd_stat(int argc, const char **argv)
 						stat_config.metric_no_threshold,
 						stat_config.user_requested_cpu_list,
 						stat_config.system_wide,
-						&stat_config.metric_events);
+						&stat_config.metric_events,
+						&stat_config.tpebs_events,
+						&stat_config.tpebs_event_size);
 
 		zfree(&metrics);
 		if (ret) {
tools/perf/util/metricgroup.c +75 -13

173 lines not on this page · raw message

tools/perf/util/metricgroup.h +9 -1
index d5325c6ec8e1..7c24ed768ff3 100644
@@ -66,6 +66,12 @@ struct metric_expr {
 	int runtime;
 };
 
+struct tpebs_event {
+	struct list_head nd;
+	const char *name;
+	const char *tpebs_name;
+};
+
 struct metric_event *metricgroup__lookup(struct rblist *metric_events,
 					 struct evsel *evsel,
 					 bool create);
@@ -77,7 +83,9 @@ int metricgroup__parse_groups(struct evlist *perf_evlist,
 			      bool metric_no_threshold,
 			      const char *user_requested_cpu_list,
 			      bool system_wide,
-			      struct rblist *metric_events);
+			      struct rblist *metric_events,
+			      struct list_head *tpebs_events,
+			      size_t *tpebs_event_size);
 int metricgroup__parse_groups_test(struct evlist *evlist,
 				   const struct pmu_metrics_table *table,
 				   const char *str,
tools/perf/util/stat.h +2 -0
index d6e5c8787ba2..b987960df3c5 100644
@@ -109,6 +109,8 @@ struct perf_stat_config {
 	struct cpu_aggr_map	*cpus_aggr_map;
 	u64			*walltime_run;
 	struct rblist		 metric_events;
+	struct list_head	 tpebs_events;
+	size_t			 tpebs_event_size;
 	int			 ctl_fd;
 	int			 ctl_fd_ack;
 	bool			 ctl_fd_close;
Signature · 1 line
2.43.0
maintainer
#2 lore raw

Re: [RFC PATCH v6 1/5] perf stat: Parse and find tpebs events when parsing metrics to prepare for perf record sampling

Hello Weilin,

Quoted 59 lines · On Fri, Mar 29, 2024 at 12:12 PM <[email protected]> wrote:

From: Weilin Wang <[email protected]>

Metrics that use tpebs values would use the R as retire_latency modifier in formulas. We put all these events into a list and pass the list to perf record to collect their retire latency value.

---
 tools/perf/builtin-stat.c     | 38 +++++++++++++--
 tools/perf/util/metricgroup.c | 88 +++++++++++++++++++++++++++++------
 tools/perf/util/metricgroup.h | 10 +++-
 tools/perf/util/stat.h        |  2 +
 4 files changed, 119 insertions(+), 19 deletions(-)
tools/perf/builtin-stat.c +17 -0
index 6bba1a89d030..6291e1e24535 100644
@@ -162,6 +162,7 @@ static struct perf_stat_config stat_config = {
        .ctl_fd                 = -1,
        .ctl_fd_ack             = -1,
        .iostat_run             = false,
+       .tpebs_events           = LIST_HEAD_INIT(stat_config.tpebs_events),
 };
 
 static bool cpus_map_matched(struct evsel *a, struct evsel *b)
@@ -686,6 +687,12 @@ static enum counter_recovery stat_handle_error(struct evsel *counter)
        return COUNTER_FATAL;
 }
 
+static int __run_perf_record(void)
+{
+       pr_debug("Prepare perf record for retire_latency\n");
+       return 0;
+}
+
 static int __run_perf_stat(int argc, const char **argv, int run_idx)
 {
        int interval = stat_config.interval;
@@ -703,6 +710,16 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
        int err;
        bool second_pass = false;
 
+       /* Prepare perf record for sampling event retire_latency before fork and
+        * prepare workload */
+       if (stat_config.tpebs_event_size > 0) {
+               int ret;
+
+               ret = __run_perf_record();
+               if (ret)
+                       return ret;
+       }
+
        if (forks) {
                if (evlist__prepare_workload(evsel_list, &target, argv, is_pipe, workload_exec_failed_signal) < 0) {
                        perror("failed to prepare workload");

tools/perf/builtin-stat.c static int add_default_attributes(void) · line 2128

Quoted 7 lines
tools/perf/builtin-stat.c +3 -1
@@ -2106,7 +2123,9 @@ static int add_default_attributes(void)
                                                stat_config.metric_no_threshold,
                                                stat_config.user_requested_cpu_list,
                                                stat_config.system_wide,
-                                               &stat_config.metric_events);
+                                               &stat_config.metric_events,
+                                               &stat_config.tpebs_events,
+                                               &stat_config.tpebs_event_size);

Maybe it'd be better to pass the stat_config, but it can be done later.

Quoted 98 lines
tools/perf/builtin-stat.c +13 -4
        }
 
        if (smi_cost) {
@@ -2139,7 +2158,9 @@ static int add_default_attributes(void)
                                                stat_config.metric_no_threshold,
                                                stat_config.user_requested_cpu_list,
                                                stat_config.system_wide,
-                                               &stat_config.metric_events);
+                                               &stat_config.metric_events,
+                                               &stat_config.tpebs_events,
+                                               &stat_config.tpebs_event_size);
        }
 
        if (topdown_run) {
@@ -2173,7 +2194,9 @@ static int add_default_attributes(void)
                                                /*metric_no_threshold=*/true,
                                                stat_config.user_requested_cpu_list,
                                                stat_config.system_wide,
-                                               &stat_config.metric_events) < 0)
+                                               &stat_config.metric_events,
+                                               &stat_config.tpebs_events,
+                                               &stat_config.tpebs_event_size) < 0)
                        return -1;
        }
 
@@ -2214,7 +2237,9 @@ static int add_default_attributes(void)
                                                        /*metric_no_threshold=*/true,
                                                        stat_config.user_requested_cpu_list,
                                                        stat_config.system_wide,
-                                                       &stat_config.metric_events) < 0)
+                                                       &stat_config.metric_events,
+                                                       /*&stat_config.tpebs_events=*/NULL,
+                                                       /*stat_config.tpebs_event_size=*/0) < 0)
                                return -1;
 
                        evlist__for_each_entry(metric_evlist, metric_evsel) {
@@ -2736,6 +2761,7 @@ int cmd_stat(int argc, const char **argv)
                }
        }
 
+
        /*
         * Metric parsing needs to be delayed as metrics may optimize events
         * knowing the target is system-wide.
@@ -2748,7 +2774,9 @@ int cmd_stat(int argc, const char **argv)
                                                stat_config.metric_no_threshold,
                                                stat_config.user_requested_cpu_list,
                                                stat_config.system_wide,
-                                               &stat_config.metric_events);
+                                               &stat_config.metric_events,
+                                               &stat_config.tpebs_events,
+                                               &stat_config.tpebs_event_size);
 
                zfree(&metrics);
                if (ret) {
tools/perf/util/metricgroup.c +7 -3
index 79ef6095ab28..8e007d60af91 100644
@@ -277,7 +277,8 @@ static bool contains_metric_id(struct evsel **metric_events, int num_events,
  */
 static int setup_metric_events(const char *pmu, struct hashmap *ids,
                               struct evlist *metric_evlist,
-                              struct evsel ***out_metric_events)
+                              struct evsel ***out_metric_events,
+                              size_t tpebs_event_size)
 {
        struct evsel **metric_events;
        const char *metric_id;
@@ -286,7 +287,7 @@ static int setup_metric_events(const char *pmu, struct hashmap *ids,
        bool all_pmus = !strcmp(pmu, "all") || perf_pmus__num_core_pmus() == 1 || !is_pmu_core(pmu);
 
        *out_metric_events = NULL;
-       ids_size = hashmap__size(ids);
+       ids_size = hashmap__size(ids) - tpebs_event_size;
 
        metric_events = calloc(ids_size + 1, sizeof(void *));
        if (!metric_events)
@@ -323,6 +324,7 @@ static int setup_metric_events(const char *pmu, struct hashmap *ids,
                }
        }
        if (matched_events < ids_size) {
+               pr_debug("Error: matched_events = %lu, ids_size = %lu\n", matched_events, ids_size);
                free(metric_events);
                return -EINVAL;
        }
@@ -668,7 +670,9 @@ static int decode_all_metric_ids(struct evlist *perf_evlist, const char *modifie
 static int metricgroup__build_event_string(struct strbuf *events,
                                           const struct expr_parse_ctx *ctx,
                                           const char *modifier,
-                                          bool group_events)
+                                          bool group_events,
+                                          struct list_head *tpebs_events __maybe_unused,
+                                          size_t *tpebs_event_size)
 {
        struct hashmap_entry *cur;
        size_t bkt;

tools/perf/util/metricgroup.c static int metricgroup__build_event_string(struct strbuf *events, · line 694

Quoted 10 lines
tools/perf/util/metricgroup.c +7 -0
@@ -681,8 +685,56 @@ static int metricgroup__build_event_string(struct strbuf *events,
        hashmap__for_each_entry(ctx->ids, cur, bkt) {
                const char *sep, *rsep, *id = cur->pkey;
                enum perf_tool_event ev;
+               /*
+                * Parse and search for event name with retire_latency modifier R.
+                * If found, put event name into the tpebs_events list. This list
+                * of events will be passed to perf record for sampling to get
+                * their reitre_latency value.
+                * Search for ":R" in event name without "@". Search for the
+                * last "@R" in event name with "@".

Hmm.. it seems you look for an 'R' modifier and then change it to 'p', right? Why not use strrchr to check ':' or '@' and if it's followed by 'R'?

Is the 'R' modifier only used in the metric expressions? Also please mention why some events have "@" in the name and others don't.

Quoted 12 lines
tools/perf/util/metricgroup.c +12 -0
+                */
+               char *p = strstr(id, ":R");
+               char *p1 = strstr(id, "@R");
+
+               if (p == NULL && p1) {
+                       p = strstr(p1+1, "@R");
+                       if (p == NULL)
+                               p = p1;
+                       p = p+1;
+               }
+
+               if (p) {

tools/perf/util/metricgroup.c static int metricgroup__build_event_string(struct strbuf *events, · line 715

Quoted 10 lines
tools/perf/util/metricgroup.c +8 -1
+                       char *name;
+                       char *at;
+                       struct tpebs_event *new_event = malloc(sizeof(struct tpebs_event));
 
-               pr_debug("found event %s\n", id);
+                       if (!new_event)
+                               return -ENOMEM;
+
+                       new_event->tpebs_name = strdup(id);
+                       *p = '\0';

I think 'p' points to the 'id' string ("cur->pkey"). Is it ok to change it here?
I guess you may want to do it on the tpebs_name.

Thanks,
Namhyung

Quoted 23 lines
tools/perf/util/metricgroup.c +22 -0
+                       name = malloc(strlen(id) + 2);
+                       if (!name)
+                               return -ENOMEM;
+
+                       at = strchr(id, '@');
+                       if (at != NULL) {
+                               *at = '/';
+                               at = strchr(id, '@');
+                               *at = '/';
+                               strcpy(name, id);
+                               strcat(name, "p");
+                       } else {
+                               strcpy(name, id);
+                               strcat(name, ":p");
+                       }
+                       new_event->name = name;
+                       *tpebs_event_size += 1;
+                       pr_debug("retire_latency required, tpebs_event_size=%lu, new_event=%s\n",
+                               *tpebs_event_size, new_event->name);
+                       list_add_tail(&new_event->nd, tpebs_events);
+                       continue;
+               }
 
#3 lore raw

RE: [RFC PATCH v6 1/5] perf stat: Parse and find tpebs events when parsing metrics to prepare for perf record sampling

Quoted 210 lines

-----Original Message-----
From: Namhyung Kim <[email protected]>
Sent: Monday, April 1, 2024 1:35 PM
To: Wang, Weilin <[email protected]>
Cc: Ian Rogers <[email protected]>; Arnaldo Carvalho de Melo <[email protected]>; Peter Zijlstra <[email protected]>; Ingo Molnar <[email protected]>; Alexander Shishkin <[email protected]>; Jiri Olsa <[email protected]>; Hunter, Adrian <[email protected]>; Kan Liang <[email protected]>; [email protected]; [email protected]; Taylor, Perry <[email protected]>; Alt, Samantha <[email protected]>; Biggers, Caleb <[email protected]>
Subject: Re: [RFC PATCH v6 1/5] perf stat: Parse and find tpebs events when parsing metrics to prepare for perf record sampling

Hello Weilin,

Quoted 7 lines · On Fri, Mar 29, 2024 at 12:12 PM <[email protected]> wrote:

From: Weilin Wang <[email protected]>

Metrics that use tpebs values would use the R as retire_latency modifier in formulas. We put all these events into a list and pass the list to perf record to collect their retire latency value.

Quoted 5 lines
---
 tools/perf/builtin-stat.c     | 38 +++++++++++++--
 tools/perf/util/metricgroup.c | 88 +++++++++++++++++++++++++++++---

---

tools/perf/builtin-stat.c static enum counter_recovery

Quoted 17 lines
 tools/perf/util/metricgroup.h | 10 +++-
 tools/perf/util/stat.h        |  2 +
 4 files changed, 119 insertions(+), 19 deletions(-)
tools/perf/builtin-stat.c +1 -0
index 6bba1a89d030..6291e1e24535 100644
@@ -162,6 +162,7 @@ static struct perf_stat_config stat_config = {
        .ctl_fd                 = -1,
        .ctl_fd_ack             = -1,
        .iostat_run             = false,
+       .tpebs_events           = LIST_HEAD_INIT(stat_config.tpebs_events),
 };
 
 static bool cpus_map_matched(struct evsel *a, struct evsel *b)
@@ -686,6 +687,12 @@ static enum counter_recovery

stat_handle_error(struct evsel *counter)

tools/perf/builtin-stat.c static int __run_perf_stat(int argc, const char

Quoted 13 lines
tools/perf/builtin-stat.c +6 -0
        return COUNTER_FATAL;
 }
 
+static int __run_perf_record(void)
+{
+       pr_debug("Prepare perf record for retire_latency\n");
+       return 0;
+}
+
 static int __run_perf_stat(int argc, const char **argv, int run_idx)
 {
        int interval = stat_config.interval;
@@ -703,6 +710,16 @@ static int __run_perf_stat(int argc, const char

**argv, int run_idx)

Quoted 5 lines
tools/perf/builtin-stat.c +2 -0
        int err;
        bool second_pass = false;
 
+       /* Prepare perf record for sampling event retire_latency before fork and
+        * prepare workload */

tools/perf/builtin-stat.c static int __run_perf_stat(int argc, const char **argv, int run_idx)

Quoted 10 lines
tools/perf/builtin-stat.c +8 -0
+       if (stat_config.tpebs_event_size > 0) {
+               int ret;
+
+               ret = __run_perf_record();
+               if (ret)
+                       return ret;
+       }
+
        if (forks) {
                if (evlist__prepare_workload(evsel_list, &target, argv, is_pipe,

workload_exec_failed_signal) < 0) {

tools/perf/builtin-stat.c static int add_default_attributes(void) · line 2128

Quoted 9 lines
tools/perf/builtin-stat.c +3 -1
                        perror("failed to prepare workload");
@@ -2106,7 +2123,9 @@ static int add_default_attributes(void)
                                                stat_config.metric_no_threshold,
                                                stat_config.user_requested_cpu_list,
                                                stat_config.system_wide,
-                                               &stat_config.metric_events);
+                                               &stat_config.metric_events,
+                                               &stat_config.tpebs_events,
+                                               &stat_config.tpebs_event_size);

Maybe it'd be better to pass the stat_config, but it can be done later.

tools/perf/util/metricgroup.c static bool contains_metric_id(struct evsel

Quoted 60 lines
tools/perf/builtin-stat.c +13 -4
        }
 
        if (smi_cost) {
@@ -2139,7 +2158,9 @@ static int add_default_attributes(void)
                                                stat_config.metric_no_threshold,
                                                stat_config.user_requested_cpu_list,
                                                stat_config.system_wide,
-                                               &stat_config.metric_events);
+                                               &stat_config.metric_events,
+                                               &stat_config.tpebs_events,
+                                               &stat_config.tpebs_event_size);
        }
 
        if (topdown_run) {
@@ -2173,7 +2194,9 @@ static int add_default_attributes(void)
                                                /*metric_no_threshold=*/true,
                                                stat_config.user_requested_cpu_list,
                                                stat_config.system_wide,
-                                               &stat_config.metric_events) < 0)
+                                               &stat_config.metric_events,
+                                               &stat_config.tpebs_events,
+                                               &stat_config.tpebs_event_size) < 0)
                        return -1;
        }
 
@@ -2214,7 +2237,9 @@ static int add_default_attributes(void)
                                                        /*metric_no_threshold=*/true,
                                                        stat_config.user_requested_cpu_list,
                                                        stat_config.system_wide,
-                                                       &stat_config.metric_events) < 0)
+                                                       &stat_config.metric_events,
+                                                       /*&stat_config.tpebs_events=*/NULL,
+                                                       /*stat_config.tpebs_event_size=*/0) < 0)
                                return -1;
 
                        evlist__for_each_entry(metric_evlist, metric_evsel) {
@@ -2736,6 +2761,7 @@ int cmd_stat(int argc, const char **argv)
                }
        }
 
+
        /*
         * Metric parsing needs to be delayed as metrics may optimize events
         * knowing the target is system-wide.
@@ -2748,7 +2774,9 @@ int cmd_stat(int argc, const char **argv)
                                                stat_config.metric_no_threshold,
                                                stat_config.user_requested_cpu_list,
                                                stat_config.system_wide,
-                                               &stat_config.metric_events);
+                                               &stat_config.metric_events,
+                                               &stat_config.tpebs_events,
+                                               &stat_config.tpebs_event_size);
 
                zfree(&metrics);
                if (ret) {
tools/perf/util/metricgroup.c +0 -0
index 79ef6095ab28..8e007d60af91 100644
@@ -277,7 +277,8 @@ static bool contains_metric_id(struct evsel

**metric_events, int num_events,

tools/perf/util/metricgroup.c static int setup_metric_events(const char *pmu,

Quoted 10 lines
tools/perf/util/metricgroup.c +2 -1
  */
 static int setup_metric_events(const char *pmu, struct hashmap *ids,
                               struct evlist *metric_evlist,
-                              struct evsel ***out_metric_events)
+                              struct evsel ***out_metric_events,
+                              size_t tpebs_event_size)
 {
        struct evsel **metric_events;
        const char *metric_id;
@@ -286,7 +287,7 @@ static int setup_metric_events(const char *pmu,

struct hashmap *ids,

tools/perf/util/metricgroup.c static int setup_metric_events(const char *pmu, · line 287

Quoted 1 line
tools/perf/util/metricgroup.c +0 -0
        bool all_pmus = !strcmp(pmu, "all") || perf_pmus__num_core_pmus()

== 1 || !is_pmu_core(pmu);

tools/perf/util/metricgroup.c static int setup_metric_events(const char *pmu,

Quoted 8 lines
tools/perf/util/metricgroup.c +1 -1
        *out_metric_events = NULL;
-       ids_size = hashmap__size(ids);
+       ids_size = hashmap__size(ids) - tpebs_event_size;
 
        metric_events = calloc(ids_size + 1, sizeof(void *));
        if (!metric_events)
@@ -323,6 +324,7 @@ static int setup_metric_events(const char *pmu,

struct hashmap *ids,

tools/perf/util/metricgroup.c static int setup_metric_events(const char *pmu, struct hashmap *ids,

Quoted 4 lines
tools/perf/util/metricgroup.c +1 -0
                }
        }
        if (matched_events < ids_size) {
+               pr_debug("Error: matched_events = %lu, ids_size = %lu\n",

matched_events, ids_size);

tools/perf/util/metricgroup.c static int decode_all_metric_ids(struct evlist

Quoted 4 lines
tools/perf/util/metricgroup.c +0 -0
                free(metric_events);
                return -EINVAL;
        }
@@ -668,7 +670,9 @@ static int decode_all_metric_ids(struct evlist

*perf_evlist, const char *modifie

tools/perf/util/metricgroup.c static int metricgroup__build_event_string(struct

Quoted 11 lines
tools/perf/util/metricgroup.c +3 -1
 static int metricgroup__build_event_string(struct strbuf *events,
                                           const struct expr_parse_ctx *ctx,
                                           const char *modifier,
-                                          bool group_events)
+                                          bool group_events,
+                                          struct list_head *tpebs_events __maybe_unused,
+                                          size_t *tpebs_event_size)
 {
        struct hashmap_entry *cur;
        size_t bkt;
@@ -681,8 +685,56 @@ static int metricgroup__build_event_string(struct

strbuf *events,

tools/perf/util/metricgroup.c static int metricgroup__build_event_string(struct strbuf *events, · line 694

Quoted 10 lines
tools/perf/util/metricgroup.c +7 -0
        hashmap__for_each_entry(ctx->ids, cur, bkt) {
                const char *sep, *rsep, *id = cur->pkey;
                enum perf_tool_event ev;
+               /*
+                * Parse and search for event name with retire_latency modifier R.
+                * If found, put event name into the tpebs_events list. This list
+                * of events will be passed to perf record for sampling to get
+                * their reitre_latency value.
+                * Search for ":R" in event name without "@". Search for the
+                * last "@R" in event name with "@".
Quoted 2 lines

Hmm.. it seems you look for an 'R' modifier and then change it to 'p', right? Why not use strrchr to check ':' or '@' and if it's followed by 'R'?

Yes, this is looking for the 'R' modifier and add 'p' for sampling. We might want to explore 'P' or 'ppp' later.

I will try strrchr out and update the code if that makes the code simpler!

Quoted 29 lines

Is the 'R' modifier only used in the metric expressions? Also please mention why some events have "@" in the name and others don't.

Quoted 5 lines
tools/perf/util/metricgroup.c +5 -0
+                */
+               char *p = strstr(id, ":R");
+               char *p1 = strstr(id, "@R");
+
+               if (p == NULL && p1) {

tools/perf/util/metricgroup.c static int metricgroup__build_event_string(struct strbuf *events,

Quoted 10 lines
tools/perf/util/metricgroup.c +10 -0
+                       p = strstr(p1+1, "@R");
+                       if (p == NULL)
+                               p = p1;
+                       p = p+1;
+               }
+
+               if (p) {
+                       char *name;
+                       char *at;
+                       struct tpebs_event *new_event = malloc(sizeof(struct

tpebs_event));

tools/perf/util/metricgroup.c static int metricgroup__build_event_string(struct strbuf *events, · line 715

Quoted 7 lines
tools/perf/util/metricgroup.c +5 -1
-               pr_debug("found event %s\n", id);
+                       if (!new_event)
+                               return -ENOMEM;
+
+                       new_event->tpebs_name = strdup(id);
+                       *p = '\0';
Quoted 3 lines

I think 'p' points to the 'id' string ("cur->pkey"). Is it ok to change it here?
I guess you may want to do it on the tpebs_name.

If I understand your question correctly:

The tpebs_name wants to keep the full name with the modifier. But for the rest places, we don't need to keep this modifier in the name. So we could change 'p' like this.

Thanks,
Weilin

Quoted 29 lines

Thanks,
Namhyung

Quoted 8 lines
tools/perf/util/metricgroup.c +8 -0
+                       name = malloc(strlen(id) + 2);
+                       if (!name)
+                               return -ENOMEM;
+
+                       at = strchr(id, '@');
+                       if (at != NULL) {
+                               *at = '/';
+                               at = strchr(id, '@');

tools/perf/util/metricgroup.c static int metricgroup__build_event_string(struct strbuf *events,

Quoted 10 lines
tools/perf/util/metricgroup.c +10 -0
+                               *at = '/';
+                               strcpy(name, id);
+                               strcat(name, "p");
+                       } else {
+                               strcpy(name, id);
+                               strcat(name, ":p");
+                       }
+                       new_event->name = name;
+                       *tpebs_event_size += 1;
+                       pr_debug("retire_latency required, tpebs_event_size=%lu,

new_event=%s\n",

Quoted 5 lines
tools/perf/util/metricgroup.c +4 -0
+                               *tpebs_event_size, new_event->name);
+                       list_add_tail(&new_event->nd, tpebs_events);
+                       continue;
+               }
 
#4 lore raw

RE: [RFC PATCH v6 1/5] perf stat: Parse and find tpebs events when parsing metrics to prepare for perf record sampling

Quoted 230 lines

-----Original Message-----
From: Wang, Weilin
Sent: Monday, April 1, 2024 2:55 PM
To: Namhyung Kim <[email protected]>
Cc: Ian Rogers <[email protected]>; Arnaldo Carvalho de Melo <[email protected]>; Peter Zijlstra <[email protected]>; Ingo Molnar <[email protected]>; Alexander Shishkin <[email protected]>; Jiri Olsa <[email protected]>; Hunter, Adrian <[email protected]>; Kan Liang <[email protected]>; [email protected]; [email protected]; Taylor, Perry <[email protected]>; Alt, Samantha <[email protected]>; Biggers, Caleb <[email protected]>
Subject: RE: [RFC PATCH v6 1/5] perf stat: Parse and find tpebs events when parsing metrics to prepare for perf record sampling

Quoted 11 lines

-----Original Message-----
From: Namhyung Kim <[email protected]>
Sent: Monday, April 1, 2024 1:35 PM
To: Wang, Weilin <[email protected]>
Cc: Ian Rogers <[email protected]>; Arnaldo Carvalho de Melo <[email protected]>; Peter Zijlstra <[email protected]>; Ingo Molnar <[email protected]>; Alexander Shishkin <[email protected]>; Jiri Olsa <[email protected]>; Hunter, Adrian <[email protected]>; Kan Liang <[email protected]>; [email protected]; [email protected]; Taylor, Perry <[email protected]>; Alt, Samantha <[email protected]>;

Biggers,

Quoted 4 lines

Caleb <[email protected]>
Subject: Re: [RFC PATCH v6 1/5] perf stat: Parse and find tpebs events when parsing metrics to prepare for perf record sampling

Quoted 15 lines

Hello Weilin,

Quoted 12 lines · On Fri, Mar 29, 2024 at 12:12 PM <[email protected]> wrote:

From: Weilin Wang <[email protected]>

Metrics that use tpebs values would use the R as retire_latency modifier in formulas. We put all these events into a list and pass the list to perf record to collect their retire latency value.

---
 tools/perf/builtin-stat.c     | 38 +++++++++++++--
 tools/perf/util/metricgroup.c | 88 +++++++++++++++++++++++++++++-

--

Quoted 22 lines

---

tools/perf/builtin-stat.c static enum counter_recovery

Quoted 17 lines
 tools/perf/util/metricgroup.h | 10 +++-
 tools/perf/util/stat.h        |  2 +
 4 files changed, 119 insertions(+), 19 deletions(-)
tools/perf/builtin-stat.c +1 -0
index 6bba1a89d030..6291e1e24535 100644
@@ -162,6 +162,7 @@ static struct perf_stat_config stat_config = {
        .ctl_fd                 = -1,
        .ctl_fd_ack             = -1,
        .iostat_run             = false,
+       .tpebs_events           = LIST_HEAD_INIT(stat_config.tpebs_events),
 };
 
 static bool cpus_map_matched(struct evsel *a, struct evsel *b)
@@ -686,6 +687,12 @@ static enum counter_recovery

stat_handle_error(struct evsel *counter)

Quoted 3 lines
tools/perf/builtin-stat.c +0 -0
        return COUNTER_FATAL;
 }
 
Quoted 15 lines

tools/perf/builtin-stat.c static int __run_perf_stat(int argc, const char

Quoted 10 lines
tools/perf/builtin-stat.c +6 -0
+static int __run_perf_record(void)
+{
+       pr_debug("Prepare perf record for retire_latency\n");
+       return 0;
+}
+
 static int __run_perf_stat(int argc, const char **argv, int run_idx)
 {
        int interval = stat_config.interval;
@@ -703,6 +710,16 @@ static int __run_perf_stat(int argc, const char

**argv, int run_idx)

Quoted 4 lines
tools/perf/builtin-stat.c +1 -0
        int err;
        bool second_pass = false;
 
+       /* Prepare perf record for sampling event retire_latency before fork

and

Quoted 141 lines

tools/perf/builtin-stat.c static int __run_perf_stat(int argc, const char **argv, int run_idx)

Quoted 11 lines
tools/perf/builtin-stat.c +9 -0
+        * prepare workload */
+       if (stat_config.tpebs_event_size > 0) {
+               int ret;
+
+               ret = __run_perf_record();
+               if (ret)
+                       return ret;
+       }
+
        if (forks) {
                if (evlist__prepare_workload(evsel_list, &target, argv, is_pipe,

workload_exec_failed_signal) < 0) {

tools/perf/builtin-stat.c static int add_default_attributes(void) · line 2128

Quoted 9 lines
tools/perf/builtin-stat.c +3 -1
                        perror("failed to prepare workload");
@@ -2106,7 +2123,9 @@ static int add_default_attributes(void)
                                                stat_config.metric_no_threshold,
                                                stat_config.user_requested_cpu_list,
                                                stat_config.system_wide,
-                                               &stat_config.metric_events);
+                                               &stat_config.metric_events,
+                                               &stat_config.tpebs_events,
+                                               &stat_config.tpebs_event_size);

Maybe it'd be better to pass the stat_config, but it can be done later.

tools/perf/util/metricgroup.c static bool contains_metric_id(struct evsel

Quoted 60 lines
tools/perf/builtin-stat.c +13 -4
        }
 
        if (smi_cost) {
@@ -2139,7 +2158,9 @@ static int add_default_attributes(void)
                                                stat_config.metric_no_threshold,
                                                stat_config.user_requested_cpu_list,
                                                stat_config.system_wide,
-                                               &stat_config.metric_events);
+                                               &stat_config.metric_events,
+                                               &stat_config.tpebs_events,
+                                               &stat_config.tpebs_event_size);
        }
 
        if (topdown_run) {
@@ -2173,7 +2194,9 @@ static int add_default_attributes(void)
                                                /*metric_no_threshold=*/true,
                                                stat_config.user_requested_cpu_list,
                                                stat_config.system_wide,
-                                               &stat_config.metric_events) < 0)
+                                               &stat_config.metric_events,
+                                               &stat_config.tpebs_events,
+                                               &stat_config.tpebs_event_size) < 0)
                        return -1;
        }
 
@@ -2214,7 +2237,9 @@ static int add_default_attributes(void)
                                                        /*metric_no_threshold=*/true,
                                                        stat_config.user_requested_cpu_list,
                                                        stat_config.system_wide,
-                                                       &stat_config.metric_events) < 0)
+                                                       &stat_config.metric_events,
+                                                       /*&stat_config.tpebs_events=*/NULL,
+                                                       /*stat_config.tpebs_event_size=*/0) < 0)
                                return -1;
 
                        evlist__for_each_entry(metric_evlist, metric_evsel) {
@@ -2736,6 +2761,7 @@ int cmd_stat(int argc, const char **argv)
                }
        }
 
+
        /*
         * Metric parsing needs to be delayed as metrics may optimize events
         * knowing the target is system-wide.
@@ -2748,7 +2774,9 @@ int cmd_stat(int argc, const char **argv)
                                                stat_config.metric_no_threshold,
                                                stat_config.user_requested_cpu_list,
                                                stat_config.system_wide,
-                                               &stat_config.metric_events);
+                                               &stat_config.metric_events,
+                                               &stat_config.tpebs_events,
+                                               &stat_config.tpebs_event_size);
 
                zfree(&metrics);
                if (ret) {
tools/perf/util/metricgroup.c +0 -0
index 79ef6095ab28..8e007d60af91 100644
@@ -277,7 +277,8 @@ static bool contains_metric_id(struct evsel

**metric_events, int num_events,

tools/perf/util/metricgroup.c static int setup_metric_events(const char *pmu,

Quoted 10 lines
tools/perf/util/metricgroup.c +2 -1
  */
 static int setup_metric_events(const char *pmu, struct hashmap *ids,
                               struct evlist *metric_evlist,
-                              struct evsel ***out_metric_events)
+                              struct evsel ***out_metric_events,
+                              size_t tpebs_event_size)
 {
        struct evsel **metric_events;
        const char *metric_id;
@@ -286,7 +287,7 @@ static int setup_metric_events(const char *pmu,

struct hashmap *ids,

tools/perf/util/metricgroup.c static int setup_metric_events(const char *pmu, · line 287

Quoted 1 line
tools/perf/util/metricgroup.c +0 -0
        bool all_pmus = !strcmp(pmu, "all") || perf_pmus__num_core_pmus()

== 1 || !is_pmu_core(pmu);

tools/perf/util/metricgroup.c static int setup_metric_events(const char *pmu,

Quoted 8 lines
tools/perf/util/metricgroup.c +1 -1
        *out_metric_events = NULL;
-       ids_size = hashmap__size(ids);
+       ids_size = hashmap__size(ids) - tpebs_event_size;
 
        metric_events = calloc(ids_size + 1, sizeof(void *));
        if (!metric_events)
@@ -323,6 +324,7 @@ static int setup_metric_events(const char *pmu,

struct hashmap *ids,

tools/perf/util/metricgroup.c static int setup_metric_events(const char *pmu, struct hashmap *ids,

Quoted 4 lines
tools/perf/util/metricgroup.c +1 -0
                }
        }
        if (matched_events < ids_size) {
+               pr_debug("Error: matched_events = %lu, ids_size = %lu\n",

matched_events, ids_size);

tools/perf/util/metricgroup.c static int decode_all_metric_ids(struct evlist

Quoted 4 lines
tools/perf/util/metricgroup.c +0 -0
                free(metric_events);
                return -EINVAL;
        }
@@ -668,7 +670,9 @@ static int decode_all_metric_ids(struct evlist

*perf_evlist, const char *modifie

tools/perf/util/metricgroup.c static int metricgroup__build_event_string(struct

Quoted 11 lines
tools/perf/util/metricgroup.c +3 -1
 static int metricgroup__build_event_string(struct strbuf *events,
                                           const struct expr_parse_ctx *ctx,
                                           const char *modifier,
-                                          bool group_events)
+                                          bool group_events,
+                                          struct list_head *tpebs_events __maybe_unused,
+                                          size_t *tpebs_event_size)
 {
        struct hashmap_entry *cur;
        size_t bkt;
@@ -681,8 +685,56 @@ static int metricgroup__build_event_string(struct

strbuf *events,

Quoted 10 lines
tools/perf/util/metricgroup.c +7 -0
        hashmap__for_each_entry(ctx->ids, cur, bkt) {
                const char *sep, *rsep, *id = cur->pkey;
                enum perf_tool_event ev;
+               /*
+                * Parse and search for event name with retire_latency modifier R.
+                * If found, put event name into the tpebs_events list. This list
+                * of events will be passed to perf record for sampling to get
+                * their reitre_latency value.
+                * Search for ":R" in event name without "@". Search for the
+                * last "@R" in event name with "@".
Quoted 11 lines
Quoted 2 lines

Hmm.. it seems you look for an 'R' modifier and then change it to 'p', right? Why not use strrchr to check ':' or '@' and if it's followed by 'R'?

Yes, this is looking for the 'R' modifier and add 'p' for sampling. We might want to explore 'P' or 'ppp' later.

I will try strrchr out and update the code if that makes the code simpler!

Quoted 3 lines

Is the 'R' modifier only used in the metric expressions? Also please mention why some events have "@" in the name and others don't.

Sorry, forgot to answer this one in last email. Yes, 'R' is only used in metric expression currently. But I remember there were some discussions on using this for perf record. This modifier might be used consistently in both record and stat.

I will update the comment about '@'.

Thanks,
Weilin

Quoted 68 lines
Quoted 26 lines
Quoted 7 lines
tools/perf/util/metricgroup.c +5 -0
+                */
+               char *p = strstr(id, ":R");
+               char *p1 = strstr(id, "@R");
+
+               if (p == NULL && p1) {

tools/perf/util/metricgroup.c static int metricgroup__build_event_string(struct strbuf *events,

Quoted 10 lines
tools/perf/util/metricgroup.c +10 -0
+                       p = strstr(p1+1, "@R");
+                       if (p == NULL)
+                               p = p1;
+                       p = p+1;
+               }
+
+               if (p) {
+                       char *name;
+                       char *at;
+                       struct tpebs_event *new_event = malloc(sizeof(struct

tpebs_event));

tools/perf/util/metricgroup.c static int metricgroup__build_event_string(struct strbuf *events, · line 715

Quoted 7 lines
tools/perf/util/metricgroup.c +5 -1
-               pr_debug("found event %s\n", id);
+                       if (!new_event)
+                               return -ENOMEM;
+
+                       new_event->tpebs_name = strdup(id);
+                       *p = '\0';
Quoted 3 lines

I think 'p' points to the 'id' string ("cur->pkey"). Is it ok to change it here?
I guess you may want to do it on the tpebs_name.

If I understand your question correctly:

The tpebs_name wants to keep the full name with the modifier. But for the rest places, we don't need to keep this modifier in the name. So we could change 'p' like this.

Thanks,
Weilin

Quoted 29 lines

Thanks,
Namhyung

Quoted 8 lines
tools/perf/util/metricgroup.c +8 -0
+                       name = malloc(strlen(id) + 2);
+                       if (!name)
+                               return -ENOMEM;
+
+                       at = strchr(id, '@');
+                       if (at != NULL) {
+                               *at = '/';
+                               at = strchr(id, '@');

tools/perf/util/metricgroup.c static int metricgroup__build_event_string(struct strbuf *events,

Quoted 10 lines
tools/perf/util/metricgroup.c +10 -0
+                               *at = '/';
+                               strcpy(name, id);
+                               strcat(name, "p");
+                       } else {
+                               strcpy(name, id);
+                               strcat(name, ":p");
+                       }
+                       new_event->name = name;
+                       *tpebs_event_size += 1;
+                       pr_debug("retire_latency required, tpebs_event_size=%lu,

new_event=%s\n",

Quoted 5 lines
tools/perf/util/metricgroup.c +4 -0
+                               *tpebs_event_size, new_event->name);
+                       list_add_tail(&new_event->nd, tpebs_events);
+                       continue;
+               }
 
#5 lore raw

RFC PATCH v6 2/5 perf stat: Fork and launch perf record when perf stat needs to get retire latency value for a metric.

In reply to [email protected]

From: Weilin Wang <[email protected]>

When retire_latency value is used in a metric formula, perf stat would fork a perf record process with "-e" and "-W" options. Perf record will collect required retire_latency values in parallel while perf stat is collecting counting values.

At the point of time that perf stat stops counting, it would send sigterm signal to perf record process and receiving sampling data back from perf record from a pipe. Perf stat will then process the received data to get retire latency data and calculate metric result.

Another thread is required to synchronize between perf stat and perf record when we pass data through pipe.

---
 tools/perf/builtin-stat.c     | 190 +++++++++++++++++++++++++++++++++-
 tools/perf/util/data.c        |   6 +-
 tools/perf/util/metricgroup.h |   8 ++
 tools/perf/util/stat.h        |   2 +
 4 files changed, 203 insertions(+), 3 deletions(-)
tools/perf/builtin-stat.c +188 -2
index 6291e1e24535..7fbe47b0c44c 100644
@@ -94,8 +94,13 @@
 #include <perf/evlist.h>
 #include <internal/threadmap.h>
 
+#include "util/sample.h"
+#include <sys/param.h>
+#include <subcmd/run-command.h>
+
 #define DEFAULT_SEPARATOR	" "
 #define FREEZE_ON_SMI_PATH	"devices/cpu/freeze_on_smi"
+#define PERF_DATA		"-"
 
 static void print_counters(struct timespec *ts, int argc, const char **argv);
 
@@ -163,6 +168,8 @@ static struct perf_stat_config stat_config = {
 	.ctl_fd_ack		= -1,
 	.iostat_run		= false,
 	.tpebs_events		= LIST_HEAD_INIT(stat_config.tpebs_events),
+	.tpebs_results		= LIST_HEAD_INIT(stat_config.tpebs_results),
+	.tpebs_pid              = -1,
 };
 
 static bool cpus_map_matched(struct evsel *a, struct evsel *b)
@@ -684,15 +691,155 @@ static enum counter_recovery stat_handle_error(struct evsel *counter)
 
 	if (child_pid != -1)
 		kill(child_pid, SIGTERM);
+	if (stat_config.tpebs_pid != -1)
+		kill(stat_config.tpebs_pid, SIGTERM);
 	return COUNTER_FATAL;
 }
 
-static int __run_perf_record(void)
+static int __run_perf_record(const char **record_argv)
 {
+	int i = 0;
+	struct tpebs_event *e;
+
 	pr_debug("Prepare perf record for retire_latency\n");
+
+	record_argv[i++] = "perf";
+	record_argv[i++] = "record";
+	record_argv[i++] = "-W";
+	record_argv[i++] = "--synth=no";
+
+	if (stat_config.user_requested_cpu_list) {
+		record_argv[i++] = "-C";
+		record_argv[i++] = stat_config.user_requested_cpu_list;
+	}
+
+	if (stat_config.system_wide)
+		record_argv[i++] = "-a";
+
+	if (!stat_config.system_wide && !stat_config.user_requested_cpu_list) {
+		pr_err("Require -a or -C option to run sampling.\n");
+		return -ECANCELED;
+	}
+
+	list_for_each_entry(e, &stat_config.tpebs_events, nd) {
+		record_argv[i++] = "-e";
+		record_argv[i++] = e->name;
+	}
+
+	record_argv[i++] = "-o";
+	record_argv[i++] = PERF_DATA;
+
 	return 0;
 }
 
+static void prepare_run_command(struct child_process *cmd,
+			       const char **argv)
+{
+	memset(cmd, 0, sizeof(*cmd));
+	cmd->argv = argv;
+	cmd->out = -1;
+}
+
+static int prepare_perf_record(struct child_process *cmd)
+{
+	const char **record_argv;
+	int ret;
+
+	record_argv = calloc(10 + 2 * stat_config.tpebs_event_size, sizeof(char *));
+	if (!record_argv)
+		return -1;
+
+	ret = __run_perf_record(record_argv);
+	if (ret)
+		return ret;
+
+	prepare_run_command(cmd, record_argv);
+	return start_command(cmd);
+}
+
+struct perf_script {
+	struct perf_tool	tool;
+	struct perf_session	*session;
+};
+
+static void tpebs_data__delete(void)
+{
+	struct tpebs_retire_lat *r, *rtmp;
+	struct tpebs_event *e, *etmp;
+
+	list_for_each_entry_safe(r, rtmp, &stat_config.tpebs_results, event.nd) {
+		list_del_init(&r->event.nd);
+		free(r);
+	}
+	list_for_each_entry_safe(e, etmp, &stat_config.tpebs_events, nd) {
+		list_del_init(&e->nd);
+		free(e);
+	}
+}
+
+static int process_sample_event(struct perf_tool *tool __maybe_unused,
+				union perf_event *event __maybe_unused,
+				struct perf_sample *sample,
+				struct evsel *evsel,
+				struct machine *machine __maybe_unused)
+{
+	int ret = 0;
+	const char *evname;
+	struct tpebs_retire_lat *t;
+
+	evname = evsel__name(evsel);
+
+	/*
+	 * Need to handle per core results? We are assuming average retire
+	 * latency value will be used. Save the number of samples and the sum of
+	 * retire latency value for each event.
+	 */
+	list_for_each_entry(t, &stat_config.tpebs_results, event.nd) {
+		if (!strcmp(evname, t->event.name)) {
+			t->count += 1;
+			t->sum += sample->retire_lat;
+			break;
+		}
+	}
+
+	return ret;
+}
+
+static int process_feature_event(struct perf_session *session,
+				 union perf_event *event)
+{
+	if (event->feat.feat_id < HEADER_LAST_FEATURE)
+		return perf_event__process_feature(session, event);
+	return 0;
+}
+
+static void *__cmd_script(void *arg __maybe_unused)
+{
+	struct child_process *cmd = arg;
+	struct perf_session *session;
+	struct perf_data data = {
+		.mode = PERF_DATA_MODE_READ,
+		.path = PERF_DATA,
+		.file.fd = cmd->out,
+	};
+	struct perf_script script = {
+		.tool = {
+		.sample		 = process_sample_event,
+		.feature	 = process_feature_event,
+		.attr		 = perf_event__process_attr,
+		},
+	};
+
+	session = perf_session__new(&data, &script.tool);
+	if (IS_ERR(session))
+		return NULL;
+	script.session = session;
+	perf_session__process_events(session);
+	perf_session__delete(session);
+
+	return NULL;
+}
+
 static int __run_perf_stat(int argc, const char **argv, int run_idx)
 {
 	int interval = stat_config.interval;
@@ -709,15 +856,38 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
 	struct affinity saved_affinity, *affinity = NULL;
 	int err;
 	bool second_pass = false;
+	struct child_process cmd;
+	pthread_t thread_script;
 
 	/* Prepare perf record for sampling event retire_latency before fork and
 	 * prepare workload */
 	if (stat_config.tpebs_event_size > 0) {
 		int ret;
+		struct tpebs_event *e;
 
-		ret = __run_perf_record();
+		pr_debug("perf stat pid = %d\n", getpid());
+		list_for_each_entry(e, &stat_config.tpebs_events, nd) {
+			struct tpebs_retire_lat *new = malloc(sizeof(struct tpebs_retire_lat));
+
+			if (!new)
+				return -1;
+			new->event.name = strdup(e->name);
+			new->event.tpebs_name = strdup(e->tpebs_name);
+			new->count = 0;
+			new->sum = 0;
+			list_add_tail(&new->event.nd, &stat_config.tpebs_results);
+		}
+		ret = prepare_perf_record(&cmd);
 		if (ret)
 			return ret;
+		if (pthread_create(&thread_script, NULL, __cmd_script, &cmd)) {
+			kill(cmd.pid, SIGTERM);
+			close(cmd.out);
+			pr_err("Could not create thread to process sample data.\n");
+			return -1;
+		}
+		/* Wait for perf record initialization a little bit.*/
+		sleep(2);
 	}
 
 	if (forks) {
@@ -925,6 +1095,17 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
 
 	t1 = rdclock();
 
+	if (stat_config.tpebs_event_size > 0) {
+		int ret;
+
+		kill(cmd.pid, SIGTERM);
+		pthread_join(thread_script, NULL);
+		close(cmd.out);
+		ret = finish_command(&cmd);
+		if (ret != -ERR_RUN_COMMAND_WAITPID_SIGNAL)
+			return ret;
+	}
+
 	if (stat_config.walltime_run_table)
 		stat_config.walltime_run[run_idx] = t1 - t0;
 
@@ -1032,6 +1213,9 @@ static void sig_atexit(void)
 	if (child_pid != -1)
 		kill(child_pid, SIGTERM);
 
+	if (stat_config.tpebs_pid != -1)
+		kill(stat_config.tpebs_pid, SIGTERM);
+
 	sigprocmask(SIG_SETMASK, &oset, NULL);
 
 	if (signr == -1)
@@ -2972,5 +3156,7 @@ int cmd_stat(int argc, const char **argv)
 	metricgroup__rblist_exit(&stat_config.metric_events);
 	evlist__close_control(stat_config.ctl_fd, stat_config.ctl_fd_ack, &stat_config.ctl_fd_close);
 
+	tpebs_data__delete();
+
 	return status;
 }
tools/perf/util/data.c +5 -1
index 08c4bfbd817f..98e3014c0aef 100644
@@ -204,7 +204,11 @@ static bool check_pipe(struct perf_data *data)
 				data->file.fd = fd;
 				data->use_stdio = false;
 			}
-		} else {
+		/*
+		 * When is_pipe and data->file.fd is given, use given fd
+		 * instead of STDIN_FILENO or STDOUT_FILENO
+		 */
+		} else if (data->file.fd <= 0) {
 			data->file.fd = fd;
 		}
 	}
tools/perf/util/metricgroup.h +8 -0
index 7c24ed768ff3..ae788edef30f 100644
@@ -68,10 +68,18 @@ struct metric_expr {
 
 struct tpebs_event {
 	struct list_head nd;
+	/* Event name */
 	const char *name;
+	/* Event name with the TPEBS modifier R */
 	const char *tpebs_name;
 };
 
+struct tpebs_retire_lat {
+	struct tpebs_event event;
+	size_t count;
+	int sum;
+};
+
 struct metric_event *metricgroup__lookup(struct rblist *metric_events,
 					 struct evsel *evsel,
 					 bool create);
tools/perf/util/stat.h +2 -0
index b987960df3c5..0726bdc06681 100644
@@ -111,6 +111,8 @@ struct perf_stat_config {
 	struct rblist		 metric_events;
 	struct list_head	 tpebs_events;
 	size_t			 tpebs_event_size;
+	struct list_head	 tpebs_results;
+	pid_t			 tpebs_pid;
 	int			 ctl_fd;
 	int			 ctl_fd_ack;
 	bool			 ctl_fd_close;
Signature · 1 line
2.43.0
maintainer
#6 lore raw

Re: [RFC PATCH v6 2/5] perf stat: Fork and launch perf record when perf stat needs to get retire latency value for a metric.

Quoted 87 lines · On Fri, Mar 29, 2024 at 12:12 PM <[email protected]> wrote:

From: Weilin Wang <[email protected]>

When retire_latency value is used in a metric formula, perf stat would fork a perf record process with "-e" and "-W" options. Perf record will collect required retire_latency values in parallel while perf stat is collecting counting values.

At the point of time that perf stat stops counting, it would send sigterm signal to perf record process and receiving sampling data back from perf record from a pipe. Perf stat will then process the received data to get retire latency data and calculate metric result.

Another thread is required to synchronize between perf stat and perf record when we pass data through pipe.

---
 tools/perf/builtin-stat.c     | 190 +++++++++++++++++++++++++++++++++-
 tools/perf/util/data.c        |   6 +-
 tools/perf/util/metricgroup.h |   8 ++
 tools/perf/util/stat.h        |   2 +
 4 files changed, 203 insertions(+), 3 deletions(-)
tools/perf/builtin-stat.c +32 -1
index 6291e1e24535..7fbe47b0c44c 100644
@@ -94,8 +94,13 @@
 #include <perf/evlist.h>
 #include <internal/threadmap.h>
 
+#include "util/sample.h"
+#include <sys/param.h>
+#include <subcmd/run-command.h>
+
 #define DEFAULT_SEPARATOR      " "
 #define FREEZE_ON_SMI_PATH     "devices/cpu/freeze_on_smi"
+#define PERF_DATA              "-"
 
 static void print_counters(struct timespec *ts, int argc, const char **argv);
 
@@ -163,6 +168,8 @@ static struct perf_stat_config stat_config = {
        .ctl_fd_ack             = -1,
        .iostat_run             = false,
        .tpebs_events           = LIST_HEAD_INIT(stat_config.tpebs_events),
+       .tpebs_results          = LIST_HEAD_INIT(stat_config.tpebs_results),
+       .tpebs_pid              = -1,
 };
 
 static bool cpus_map_matched(struct evsel *a, struct evsel *b)
@@ -684,15 +691,155 @@ static enum counter_recovery stat_handle_error(struct evsel *counter)
 
        if (child_pid != -1)
                kill(child_pid, SIGTERM);
+       if (stat_config.tpebs_pid != -1)
+               kill(stat_config.tpebs_pid, SIGTERM);
        return COUNTER_FATAL;
 }
 
-static int __run_perf_record(void)
+static int __run_perf_record(const char **record_argv)
 {
+       int i = 0;
+       struct tpebs_event *e;
+
        pr_debug("Prepare perf record for retire_latency\n");
+
+       record_argv[i++] = "perf";
+       record_argv[i++] = "record";
+       record_argv[i++] = "-W";
+       record_argv[i++] = "--synth=no";
+
+       if (stat_config.user_requested_cpu_list) {
+               record_argv[i++] = "-C";
+               record_argv[i++] = stat_config.user_requested_cpu_list;
+       }
+
+       if (stat_config.system_wide)
+               record_argv[i++] = "-a";
+
+       if (!stat_config.system_wide && !stat_config.user_requested_cpu_list) {
+               pr_err("Require -a or -C option to run sampling.\n");
+               return -ECANCELED;
+       }
+

tools/perf/builtin-stat.c static enum counter_recovery stat_handle_error(struct evsel *counter) · line 733

Quoted 10 lines
tools/perf/builtin-stat.c +8 -0
+       list_for_each_entry(e, &stat_config.tpebs_events, nd) {
+               record_argv[i++] = "-e";
+               record_argv[i++] = e->name;
+       }
+
+       record_argv[i++] = "-o";
+       record_argv[i++] = PERF_DATA;
+
        return 0;
 }

Still I think it's weird it has 'perf record' in perf stat (despite the 'perf stat record'). If it's only Intel thing and we don't have a plan to do the same on other arches, we can move it to the arch directory and keep the perf stat code simple.

Quoted 33 lines
tools/perf/builtin-stat.c +32 -0
+static void prepare_run_command(struct child_process *cmd,
+                              const char **argv)
+{
+       memset(cmd, 0, sizeof(*cmd));
+       cmd->argv = argv;
+       cmd->out = -1;
+}
+
+static int prepare_perf_record(struct child_process *cmd)
+{
+       const char **record_argv;
+       int ret;
+
+       record_argv = calloc(10 + 2 * stat_config.tpebs_event_size, sizeof(char *));
+       if (!record_argv)
+               return -1;
+
+       ret = __run_perf_record(record_argv);
+       if (ret)
+               return ret;
+
+       prepare_run_command(cmd, record_argv);
+       return start_command(cmd);
+}
+
+struct perf_script {
+       struct perf_tool        tool;
+       struct perf_session     *session;
+};
+
+static void tpebs_data__delete(void)
+{

tools/perf/builtin-stat.c static enum counter_recovery stat_handle_error(struct evsel *counter) · line 776

Quoted 10 lines
tools/perf/builtin-stat.c +10 -0
+       struct tpebs_retire_lat *r, *rtmp;
+       struct tpebs_event *e, *etmp;
+
+       list_for_each_entry_safe(r, rtmp, &stat_config.tpebs_results, event.nd) {
+               list_del_init(&r->event.nd);
+               free(r);
+       }
+       list_for_each_entry_safe(e, etmp, &stat_config.tpebs_events, nd) {
+               list_del_init(&e->nd);
+               free(e);

Shouldn't it free the names?

Quoted 30 lines
tools/perf/builtin-stat.c +30 -0
+       }
+}
+
+static int process_sample_event(struct perf_tool *tool __maybe_unused,
+                               union perf_event *event __maybe_unused,
+                               struct perf_sample *sample,
+                               struct evsel *evsel,
+                               struct machine *machine __maybe_unused)
+{
+       int ret = 0;
+       const char *evname;
+       struct tpebs_retire_lat *t;
+
+       evname = evsel__name(evsel);
+
+       /*
+        * Need to handle per core results? We are assuming average retire
+        * latency value will be used. Save the number of samples and the sum of
+        * retire latency value for each event.
+        */
+       list_for_each_entry(t, &stat_config.tpebs_results, event.nd) {
+               if (!strcmp(evname, t->event.name)) {
+                       t->count += 1;
+                       t->sum += sample->retire_lat;
+                       break;
+               }
+       }
+
+       return ret;
+}

tools/perf/builtin-stat.c static enum counter_recovery stat_handle_error(struct evsel *counter) · line 816

Quoted 10 lines
tools/perf/builtin-stat.c +10 -0
+
+static int process_feature_event(struct perf_session *session,
+                                union perf_event *event)
+{
+       if (event->feat.feat_id < HEADER_LAST_FEATURE)
+               return perf_event__process_feature(session, event);
+       return 0;
+}
+
+static void *__cmd_script(void *arg __maybe_unused)

The arg is used.

Also I don't like the name 'script' as it has nothing to do with scripting. Maybe 'sample_reader', 'tpebs_reader' or 'reader_thread'?

tools/perf/builtin-stat.c static enum counter_recovery stat_handle_error(struct evsel *counter) · line 829

Quoted 13 lines
tools/perf/builtin-stat.c +13 -0
+{
+       struct child_process *cmd = arg;
+       struct perf_session *session;
+       struct perf_data data = {
+               .mode = PERF_DATA_MODE_READ,
+               .path = PERF_DATA,
+               .file.fd = cmd->out,
+       };
+       struct perf_script script = {
+               .tool = {
+               .sample          = process_sample_event,
+               .feature         = process_feature_event,
+               .attr            = perf_event__process_attr,

Broken indentation. And if you just use the tool, you can pass it directly.

Quoted 28 lines
tools/perf/builtin-stat.c +16 -0
+               },
+       };
+
+       session = perf_session__new(&data, &script.tool);
+       if (IS_ERR(session))
+               return NULL;
+       script.session = session;
+       perf_session__process_events(session);
+       perf_session__delete(session);
+
+       return NULL;
+}
+
 static int __run_perf_stat(int argc, const char **argv, int run_idx)
 {
        int interval = stat_config.interval;
@@ -709,15 +856,38 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
        struct affinity saved_affinity, *affinity = NULL;
        int err;
        bool second_pass = false;
+       struct child_process cmd;
+       pthread_t thread_script;
 
        /* Prepare perf record for sampling event retire_latency before fork and
         * prepare workload */
        if (stat_config.tpebs_event_size > 0) {
                int ret;
+               struct tpebs_event *e;

tools/perf/builtin-stat.c static int __run_perf_stat(int argc, const char **argv, int run_idx) · line 875

Quoted 10 lines
tools/perf/builtin-stat.c +8 -1
 
-               ret = __run_perf_record();
+               pr_debug("perf stat pid = %d\n", getpid());
+               list_for_each_entry(e, &stat_config.tpebs_events, nd) {
+                       struct tpebs_retire_lat *new = malloc(sizeof(struct tpebs_retire_lat));
+
+                       if (!new)
+                               return -1;
+                       new->event.name = strdup(e->name);
+                       new->event.tpebs_name = strdup(e->tpebs_name);

These can fail too.

Quoted 5 lines
tools/perf/builtin-stat.c +5 -0
+                       new->count = 0;
+                       new->sum = 0;
+                       list_add_tail(&new->event.nd, &stat_config.tpebs_results);
+               }
+               ret = prepare_perf_record(&cmd);

tools/perf/builtin-stat.c static int __run_perf_stat(int argc, const char **argv, int run_idx) · line 890

Quoted 10 lines
tools/perf/builtin-stat.c +8 -0
                if (ret)
                        return ret;
+               if (pthread_create(&thread_script, NULL, __cmd_script, &cmd)) {
+                       kill(cmd.pid, SIGTERM);
+                       close(cmd.out);
+                       pr_err("Could not create thread to process sample data.\n");
+                       return -1;
+               }
+               /* Wait for perf record initialization a little bit.*/
+               sleep(2);

This won't guarantee anything. If you want to make sure the 'thread_script' to run before the 'perf record' process, you can use a pipe to signal that like in evlist__prepare_workload() and evlist__start_workload().

Quoted 46 lines
tools/perf/builtin-stat.c +16 -0
        }
 
        if (forks) {
@@ -925,6 +1095,17 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
 
        t1 = rdclock();
 
+       if (stat_config.tpebs_event_size > 0) {
+               int ret;
+
+               kill(cmd.pid, SIGTERM);
+               pthread_join(thread_script, NULL);
+               close(cmd.out);
+               ret = finish_command(&cmd);
+               if (ret != -ERR_RUN_COMMAND_WAITPID_SIGNAL)
+                       return ret;
+       }
+
        if (stat_config.walltime_run_table)
                stat_config.walltime_run[run_idx] = t1 - t0;
 
@@ -1032,6 +1213,9 @@ static void sig_atexit(void)
        if (child_pid != -1)
                kill(child_pid, SIGTERM);
 
+       if (stat_config.tpebs_pid != -1)
+               kill(stat_config.tpebs_pid, SIGTERM);
+
        sigprocmask(SIG_SETMASK, &oset, NULL);
 
        if (signr == -1)
@@ -2972,5 +3156,7 @@ int cmd_stat(int argc, const char **argv)
        metricgroup__rblist_exit(&stat_config.metric_events);
        evlist__close_control(stat_config.ctl_fd, stat_config.ctl_fd_ack, &stat_config.ctl_fd_close);
 
+       tpebs_data__delete();
+
        return status;
 }
tools/perf/util/data.c +0 -0
index 08c4bfbd817f..98e3014c0aef 100644
@@ -204,7 +204,11 @@ static bool check_pipe(struct perf_data *data)
                                data->file.fd = fd;
                                data->use_stdio = false;

tools/perf/util/data.c static bool check_pipe(struct perf_data *data) · line 214

Quoted 10 lines
tools/perf/util/data.c +5 -1
                        }
-               } else {
+               /*
+                * When is_pipe and data->file.fd is given, use given fd
+                * instead of STDIN_FILENO or STDOUT_FILENO
+                */
+               } else if (data->file.fd <= 0) {
                        data->file.fd = fd;
                }
        }

I think this can be in a separate commit.

Quoted 9 lines
tools/perf/util/metricgroup.h +1 -0
index 7c24ed768ff3..ae788edef30f 100644
@@ -68,10 +68,18 @@ struct metric_expr {
 
 struct tpebs_event {
        struct list_head nd;
+       /* Event name */

tools/perf/util/metricgroup.h struct metric_expr { · line 81

Quoted 10 lines
tools/perf/util/metricgroup.h +6 -0
        const char *name;
+       /* Event name with the TPEBS modifier R */
        const char *tpebs_name;
 };
 
+struct tpebs_retire_lat {
+       struct tpebs_event event;
+       size_t count;
+       int sum;
+};

Actually I don't know why you need this separate structure. Can we just use tpebs_event?

Thanks,
Namhyung

Quoted 20 lines
tools/perf/util/metricgroup.h +1 -0
+
 struct metric_event *metricgroup__lookup(struct rblist *metric_events,
                                         struct evsel *evsel,
                                         bool create);
tools/perf/util/stat.h +2 -1
index b987960df3c5..0726bdc06681 100644
@@ -111,6 +111,8 @@ struct perf_stat_config {
        struct rblist            metric_events;
        struct list_head         tpebs_events;
        size_t                   tpebs_event_size;
+       struct list_head         tpebs_results;
+       pid_t                    tpebs_pid;
        int                      ctl_fd;
        int                      ctl_fd_ack;
        bool                     ctl_fd_close;
--

2.43.0

#7 lore raw

RE: [RFC PATCH v6 2/5] perf stat: Fork and launch perf record when perf stat needs to get retire latency value for a metric.

Quoted 117 lines

-----Original Message-----
From: Namhyung Kim <[email protected]>
Sent: Monday, April 1, 2024 1:58 PM
To: Wang, Weilin <[email protected]>
Cc: Ian Rogers <[email protected]>; Arnaldo Carvalho de Melo <[email protected]>; Peter Zijlstra <[email protected]>; Ingo Molnar <[email protected]>; Alexander Shishkin <[email protected]>; Jiri Olsa <[email protected]>; Hunter, Adrian <[email protected]>; Kan Liang <[email protected]>; [email protected]; [email protected]; Taylor, Perry <[email protected]>; Alt, Samantha <[email protected]>; Biggers, Caleb <[email protected]>
Subject: Re: [RFC PATCH v6 2/5] perf stat: Fork and launch perf record when perf stat needs to get retire latency value for a metric.

Quoted 9 lines · On Fri, Mar 29, 2024 at 12:12 PM <[email protected]> wrote:

From: Weilin Wang <[email protected]>

When retire_latency value is used in a metric formula, perf stat would fork a perf record process with "-e" and "-W" options. Perf record will collect required retire_latency values in parallel while perf stat is collecting counting values.

At the point of time that perf stat stops counting, it would send sigterm

signal

Quoted 1 line

to perf record process and receiving sampling data back from perf record

from a

Quoted 10 lines

pipe. Perf stat will then process the received data to get retire latency data and calculate metric result.

Another thread is required to synchronize between perf stat and perf record when we pass data through pipe.

---
 tools/perf/builtin-stat.c     | 190

+++++++++++++++++++++++++++++++++-

tools/perf/builtin-stat.c static enum counter_recovery

Quoted 33 lines
 tools/perf/util/data.c        |   6 +-
 tools/perf/util/metricgroup.h |   8 ++
 tools/perf/util/stat.h        |   2 +
 4 files changed, 203 insertions(+), 3 deletions(-)
tools/perf/builtin-stat.c +7 -0
index 6291e1e24535..7fbe47b0c44c 100644
@@ -94,8 +94,13 @@
 #include <perf/evlist.h>
 #include <internal/threadmap.h>
 
+#include "util/sample.h"
+#include <sys/param.h>
+#include <subcmd/run-command.h>
+
 #define DEFAULT_SEPARATOR      " "
 #define FREEZE_ON_SMI_PATH     "devices/cpu/freeze_on_smi"
+#define PERF_DATA              "-"
 
 static void print_counters(struct timespec *ts, int argc, const char **argv);
 
@@ -163,6 +168,8 @@ static struct perf_stat_config stat_config = {
        .ctl_fd_ack             = -1,
        .iostat_run             = false,
        .tpebs_events           = LIST_HEAD_INIT(stat_config.tpebs_events),
+       .tpebs_results          = LIST_HEAD_INIT(stat_config.tpebs_results),
+       .tpebs_pid              = -1,
 };
 
 static bool cpus_map_matched(struct evsel *a, struct evsel *b)
@@ -684,15 +691,155 @@ static enum counter_recovery

stat_handle_error(struct evsel *counter)

Quoted 20 lines
tools/perf/builtin-stat.c +11 -1
        if (child_pid != -1)
                kill(child_pid, SIGTERM);
+       if (stat_config.tpebs_pid != -1)
+               kill(stat_config.tpebs_pid, SIGTERM);
        return COUNTER_FATAL;
 }
 
-static int __run_perf_record(void)
+static int __run_perf_record(const char **record_argv)
 {
+       int i = 0;
+       struct tpebs_event *e;
+
        pr_debug("Prepare perf record for retire_latency\n");
+
+       record_argv[i++] = "perf";
+       record_argv[i++] = "record";
+       record_argv[i++] = "-W";
+       record_argv[i++] = "--synth=no";

tools/perf/builtin-stat.c static enum counter_recovery stat_handle_error(struct evsel *counter)

Quoted 10 lines
tools/perf/builtin-stat.c +10 -0
+
+       if (stat_config.user_requested_cpu_list) {
+               record_argv[i++] = "-C";
+               record_argv[i++] = stat_config.user_requested_cpu_list;
+       }
+
+       if (stat_config.system_wide)
+               record_argv[i++] = "-a";
+
+       if (!stat_config.system_wide && !stat_config.user_requested_cpu_list)

{

Quoted 11 lines
tools/perf/builtin-stat.c +11 -0
+               pr_err("Require -a or -C option to run sampling.\n");
+               return -ECANCELED;
+       }
+
+       list_for_each_entry(e, &stat_config.tpebs_events, nd) {
+               record_argv[i++] = "-e";
+               record_argv[i++] = e->name;
+       }
+
+       record_argv[i++] = "-o";
+       record_argv[i++] = PERF_DATA;
Quoted 8 lines

tools/perf/builtin-stat.c static enum counter_recovery stat_handle_error(struct evsel *counter) · line 733

Quoted 3 lines
tools/perf/builtin-stat.c +1 -0
+
        return 0;
 }

Still I think it's weird it has 'perf record' in perf stat (despite the 'perf stat record'). If it's only Intel thing and we don't have a plan to do the same on other arches, we can move it to the arch directory and keep the perf stat code simple.

I'm not sure what is the proper way to solve this. And Ian mentioned that put code in arch directory could potentially cause other bugs. So I'm wondering if we could keep this code here for now. I could work on it later if we found it's better to be in arch directory.

Quoted 178 lines
Quoted 7 lines
tools/perf/builtin-stat.c +4 -0
+static void prepare_run_command(struct child_process *cmd,
+                              const char **argv)
+{
+       memset(cmd, 0, sizeof(*cmd));

tools/perf/builtin-stat.c static enum counter_recovery stat_handle_error(struct evsel *counter)

Quoted 10 lines
tools/perf/builtin-stat.c +10 -0
+       cmd->argv = argv;
+       cmd->out = -1;
+}
+
+static int prepare_perf_record(struct child_process *cmd)
+{
+       const char **record_argv;
+       int ret;
+
+       record_argv = calloc(10 + 2 * stat_config.tpebs_event_size, sizeof(char

*));

Quoted 12 lines
tools/perf/builtin-stat.c +12 -0
+       if (!record_argv)
+               return -1;
+
+       ret = __run_perf_record(record_argv);
+       if (ret)
+               return ret;
+
+       prepare_run_command(cmd, record_argv);
+       return start_command(cmd);
+}
+
+struct perf_script {

tools/perf/builtin-stat.c static enum counter_recovery stat_handle_error(struct evsel *counter)

Quoted 10 lines
tools/perf/builtin-stat.c +10 -0
+       struct perf_tool        tool;
+       struct perf_session     *session;
+};
+
+static void tpebs_data__delete(void)
+{
+       struct tpebs_retire_lat *r, *rtmp;
+       struct tpebs_event *e, *etmp;
+
+       list_for_each_entry_safe(r, rtmp, &stat_config.tpebs_results, event.nd)

{

tools/perf/builtin-stat.c static enum counter_recovery stat_handle_error(struct evsel *counter) · line 776

Quoted 6 lines
tools/perf/builtin-stat.c +6 -0
+               list_del_init(&r->event.nd);
+               free(r);
+       }
+       list_for_each_entry_safe(e, etmp, &stat_config.tpebs_events, nd) {
+               list_del_init(&e->nd);
+               free(e);

Shouldn't it free the names?

Quoted 8 lines
tools/perf/builtin-stat.c +8 -0
+       }
+}
+
+static int process_sample_event(struct perf_tool *tool __maybe_unused,
+                               union perf_event *event __maybe_unused,
+                               struct perf_sample *sample,
+                               struct evsel *evsel,
+                               struct machine *machine __maybe_unused)

tools/perf/builtin-stat.c static enum counter_recovery stat_handle_error(struct evsel *counter)

Quoted 10 lines
tools/perf/builtin-stat.c +10 -0
+{
+       int ret = 0;
+       const char *evname;
+       struct tpebs_retire_lat *t;
+
+       evname = evsel__name(evsel);
+
+       /*
+        * Need to handle per core results? We are assuming average retire
+        * latency value will be used. Save the number of samples and the sum

of

Quoted 12 lines
tools/perf/builtin-stat.c +12 -0
+        * retire latency value for each event.
+        */
+       list_for_each_entry(t, &stat_config.tpebs_results, event.nd) {
+               if (!strcmp(evname, t->event.name)) {
+                       t->count += 1;
+                       t->sum += sample->retire_lat;
+                       break;
+               }
+       }
+
+       return ret;
+}

tools/perf/builtin-stat.c static enum counter_recovery stat_handle_error(struct evsel *counter) · line 816

Quoted 10 lines
tools/perf/builtin-stat.c +10 -0
+
+static int process_feature_event(struct perf_session *session,
+                                union perf_event *event)
+{
+       if (event->feat.feat_id < HEADER_LAST_FEATURE)
+               return perf_event__process_feature(session, event);
+       return 0;
+}
+
+static void *__cmd_script(void *arg __maybe_unused)

The arg is used.

Also I don't like the name 'script' as it has nothing to do with scripting. Maybe 'sample_reader', 'tpebs_reader' or 'reader_thread'?

tools/perf/builtin-stat.c static enum counter_recovery stat_handle_error(struct evsel *counter) · line 829

Quoted 13 lines
tools/perf/builtin-stat.c +13 -0
+{
+       struct child_process *cmd = arg;
+       struct perf_session *session;
+       struct perf_data data = {
+               .mode = PERF_DATA_MODE_READ,
+               .path = PERF_DATA,
+               .file.fd = cmd->out,
+       };
+       struct perf_script script = {
+               .tool = {
+               .sample          = process_sample_event,
+               .feature         = process_feature_event,
+               .attr            = perf_event__process_attr,

Broken indentation. And if you just use the tool, you can pass it directly.

tools/perf/builtin-stat.c static int __run_perf_stat(int argc, const char

Quoted 17 lines
tools/perf/builtin-stat.c +13 -0
+               },
+       };
+
+       session = perf_session__new(&data, &script.tool);
+       if (IS_ERR(session))
+               return NULL;
+       script.session = session;
+       perf_session__process_events(session);
+       perf_session__delete(session);
+
+       return NULL;
+}
+
 static int __run_perf_stat(int argc, const char **argv, int run_idx)
 {
        int interval = stat_config.interval;
@@ -709,15 +856,38 @@ static int __run_perf_stat(int argc, const char

**argv, int run_idx)

Quoted 6 lines
tools/perf/builtin-stat.c +2 -0
        struct affinity saved_affinity, *affinity = NULL;
        int err;
        bool second_pass = false;
+       struct child_process cmd;
+       pthread_t thread_script;
 

tools/perf/builtin-stat.c static int __run_perf_stat(int argc, const char **argv, int run_idx)

Quoted 10 lines
tools/perf/builtin-stat.c +4 -1
        /* Prepare perf record for sampling event retire_latency before fork and
         * prepare workload */
        if (stat_config.tpebs_event_size > 0) {
                int ret;
+               struct tpebs_event *e;
 
-               ret = __run_perf_record();
+               pr_debug("perf stat pid = %d\n", getpid());
+               list_for_each_entry(e, &stat_config.tpebs_events, nd) {
+                       struct tpebs_retire_lat *new = malloc(sizeof(struct

tpebs_retire_lat));

tools/perf/builtin-stat.c static int __run_perf_stat(int argc, const char **argv, int run_idx) · line 875

Quoted 5 lines
tools/perf/builtin-stat.c +5 -0
+
+                       if (!new)
+                               return -1;
+                       new->event.name = strdup(e->name);
+                       new->event.tpebs_name = strdup(e->tpebs_name);

These can fail too.

Quoted 5 lines
tools/perf/builtin-stat.c +5 -0
+                       new->count = 0;
+                       new->sum = 0;
+                       list_add_tail(&new->event.nd, &stat_config.tpebs_results);
+               }
+               ret = prepare_perf_record(&cmd);

tools/perf/builtin-stat.c static int __run_perf_stat(int argc, const char **argv, int run_idx) · line 890

Quoted 10 lines
tools/perf/builtin-stat.c +8 -0
                if (ret)
                        return ret;
+               if (pthread_create(&thread_script, NULL, __cmd_script, &cmd)) {
+                       kill(cmd.pid, SIGTERM);
+                       close(cmd.out);
+                       pr_err("Could not create thread to process sample data.\n");
+                       return -1;
+               }
+               /* Wait for perf record initialization a little bit.*/
+               sleep(2);
Quoted 4 lines

This won't guarantee anything. If you want to make sure the 'thread_script' to run before the 'perf record' process, you can use a pipe to signal that like in evlist__prepare_workload() and evlist__start_workload().

This sleep is added to make perf stat wait for record initialization because in the case that the workload runs very small a mount of time, we'd like to ensure perf record has enough time to launch and start collecting sample data.

Because the code uses the common API in run-command.h to do the fork, I think it cannot use PIPE like in evlist__prepare_workload to sync and start perf record and perf stat data collection together. Please correct me if I'm wrong here.

Quoted 78 lines

tools/perf/builtin-stat.c static int __run_perf_stat(int argc, const char

Quoted 6 lines
tools/perf/builtin-stat.c +0 -0
        }
 
        if (forks) {
@@ -925,6 +1095,17 @@ static int __run_perf_stat(int argc, const char

**argv, int run_idx)

Quoted 28 lines
tools/perf/builtin-stat.c +14 -0
        t1 = rdclock();
 
+       if (stat_config.tpebs_event_size > 0) {
+               int ret;
+
+               kill(cmd.pid, SIGTERM);
+               pthread_join(thread_script, NULL);
+               close(cmd.out);
+               ret = finish_command(&cmd);
+               if (ret != -ERR_RUN_COMMAND_WAITPID_SIGNAL)
+                       return ret;
+       }
+
        if (stat_config.walltime_run_table)
                stat_config.walltime_run[run_idx] = t1 - t0;
 
@@ -1032,6 +1213,9 @@ static void sig_atexit(void)
        if (child_pid != -1)
                kill(child_pid, SIGTERM);
 
+       if (stat_config.tpebs_pid != -1)
+               kill(stat_config.tpebs_pid, SIGTERM);
+
        sigprocmask(SIG_SETMASK, &oset, NULL);
 
        if (signr == -1)

tools/perf/builtin-stat.c int cmd_stat(int argc, const char **argv) · line 3157

Quoted 2 lines
tools/perf/builtin-stat.c +0 -0
@@ -2972,5 +3156,7 @@ int cmd_stat(int argc, const char **argv)
        metricgroup__rblist_exit(&stat_config.metric_events);
        evlist__close_control(stat_config.ctl_fd, stat_config.ctl_fd_ack,

&stat_config.ctl_fd_close);

Quoted 12 lines
tools/perf/builtin-stat.c +2 -0
+       tpebs_data__delete();
+
        return status;
 }
tools/perf/util/data.c +0 -0
index 08c4bfbd817f..98e3014c0aef 100644
@@ -204,7 +204,11 @@ static bool check_pipe(struct perf_data *data)
                                data->file.fd = fd;
                                data->use_stdio = false;

tools/perf/util/data.c static bool check_pipe(struct perf_data *data) · line 214

Quoted 10 lines
tools/perf/util/data.c +5 -1
                        }
-               } else {
+               /*
+                * When is_pipe and data->file.fd is given, use given fd
+                * instead of STDIN_FILENO or STDOUT_FILENO
+                */
+               } else if (data->file.fd <= 0) {
                        data->file.fd = fd;
                }
        }

I think this can be in a separate commit.

Quoted 14 lines
tools/perf/util/metricgroup.h +2 -0
index 7c24ed768ff3..ae788edef30f 100644
@@ -68,10 +68,18 @@ struct metric_expr {
 
 struct tpebs_event {
        struct list_head nd;
+       /* Event name */
        const char *name;
+       /* Event name with the TPEBS modifier R */
        const char *tpebs_name;
 };
 
Quoted 8 lines

tools/perf/util/metricgroup.h struct metric_expr { · line 81

Quoted 5 lines
tools/perf/util/metricgroup.h +5 -0
+struct tpebs_retire_lat {
+       struct tpebs_event event;
+       size_t count;
+       int sum;
+};

Actually I don't know why you need this separate structure. Can we just use tpebs_event?

Currently, we use average value as the retire latency value in metrics. But we might update it to use other value, for example the minimum or maximum. So, I thought it would be better to have a dedicated data structure to handle this data. I could update the code to use tpebs_event if you still feel that's better.

Thanks,
Weilin

Quoted 25 lines

Thanks,
Namhyung

Quoted 20 lines
tools/perf/util/metricgroup.h +1 -0
+
 struct metric_event *metricgroup__lookup(struct rblist *metric_events,
                                         struct evsel *evsel,
                                         bool create);
tools/perf/util/stat.h +2 -1
index b987960df3c5..0726bdc06681 100644
@@ -111,6 +111,8 @@ struct perf_stat_config {
        struct rblist            metric_events;
        struct list_head         tpebs_events;
        size_t                   tpebs_event_size;
+       struct list_head         tpebs_results;
+       pid_t                    tpebs_pid;
        int                      ctl_fd;
        int                      ctl_fd_ack;
        bool                     ctl_fd_close;
--

2.43.0

maintainer
#8 lore raw

Re: [RFC PATCH v6 2/5] perf stat: Fork and launch perf record when perf stat needs to get retire latency value for a metric.

Quoted 127 lines · Weilin
Quoted 118 lines

-----Original Message-----
From: Namhyung Kim <[email protected]>
Sent: Monday, April 1, 2024 1:58 PM
To: Wang, Weilin <[email protected]>
Cc: Ian Rogers <[email protected]>; Arnaldo Carvalho de Melo <[email protected]>; Peter Zijlstra <[email protected]>; Ingo Molnar <[email protected]>; Alexander Shishkin <[email protected]>; Jiri Olsa <[email protected]>; Hunter, Adrian <[email protected]>; Kan Liang <[email protected]>; [email protected]; [email protected]; Taylor, Perry <[email protected]>; Alt, Samantha <[email protected]>; Biggers, Caleb <[email protected]>
Subject: Re: [RFC PATCH v6 2/5] perf stat: Fork and launch perf record when perf stat needs to get retire latency value for a metric.

Quoted 9 lines · On Fri, Mar 29, 2024 at 12:12 PM <[email protected]> wrote:

From: Weilin Wang <[email protected]>

When retire_latency value is used in a metric formula, perf stat would fork a perf record process with "-e" and "-W" options. Perf record will collect required retire_latency values in parallel while perf stat is collecting counting values.

At the point of time that perf stat stops counting, it would send sigterm

signal

Quoted 1 line

to perf record process and receiving sampling data back from perf record

from a

Quoted 10 lines

pipe. Perf stat will then process the received data to get retire latency data and calculate metric result.

Another thread is required to synchronize between perf stat and perf record when we pass data through pipe.

---
 tools/perf/builtin-stat.c     | 190

+++++++++++++++++++++++++++++++++-

tools/perf/builtin-stat.c static enum counter_recovery

Quoted 33 lines
 tools/perf/util/data.c        |   6 +-
 tools/perf/util/metricgroup.h |   8 ++
 tools/perf/util/stat.h        |   2 +
 4 files changed, 203 insertions(+), 3 deletions(-)
tools/perf/builtin-stat.c +7 -0
index 6291e1e24535..7fbe47b0c44c 100644
@@ -94,8 +94,13 @@
 #include <perf/evlist.h>
 #include <internal/threadmap.h>
 
+#include "util/sample.h"
+#include <sys/param.h>
+#include <subcmd/run-command.h>
+
 #define DEFAULT_SEPARATOR      " "
 #define FREEZE_ON_SMI_PATH     "devices/cpu/freeze_on_smi"
+#define PERF_DATA              "-"
 
 static void print_counters(struct timespec *ts, int argc, const char **argv);
 
@@ -163,6 +168,8 @@ static struct perf_stat_config stat_config = {
        .ctl_fd_ack             = -1,
        .iostat_run             = false,
        .tpebs_events           = LIST_HEAD_INIT(stat_config.tpebs_events),
+       .tpebs_results          = LIST_HEAD_INIT(stat_config.tpebs_results),
+       .tpebs_pid              = -1,
 };
 
 static bool cpus_map_matched(struct evsel *a, struct evsel *b)
@@ -684,15 +691,155 @@ static enum counter_recovery

stat_handle_error(struct evsel *counter)

Quoted 20 lines
tools/perf/builtin-stat.c +11 -1
        if (child_pid != -1)
                kill(child_pid, SIGTERM);
+       if (stat_config.tpebs_pid != -1)
+               kill(stat_config.tpebs_pid, SIGTERM);
        return COUNTER_FATAL;
 }
 
-static int __run_perf_record(void)
+static int __run_perf_record(const char **record_argv)
 {
+       int i = 0;
+       struct tpebs_event *e;
+
        pr_debug("Prepare perf record for retire_latency\n");
+
+       record_argv[i++] = "perf";
+       record_argv[i++] = "record";
+       record_argv[i++] = "-W";
+       record_argv[i++] = "--synth=no";

tools/perf/builtin-stat.c static enum counter_recovery stat_handle_error(struct evsel *counter)

Quoted 10 lines
tools/perf/builtin-stat.c +10 -0
+
+       if (stat_config.user_requested_cpu_list) {
+               record_argv[i++] = "-C";
+               record_argv[i++] = stat_config.user_requested_cpu_list;
+       }
+
+       if (stat_config.system_wide)
+               record_argv[i++] = "-a";
+
+       if (!stat_config.system_wide && !stat_config.user_requested_cpu_list)

{

Quoted 11 lines
tools/perf/builtin-stat.c +11 -0
+               pr_err("Require -a or -C option to run sampling.\n");
+               return -ECANCELED;
+       }
+
+       list_for_each_entry(e, &stat_config.tpebs_events, nd) {
+               record_argv[i++] = "-e";
+               record_argv[i++] = e->name;
+       }
+
+       record_argv[i++] = "-o";
+       record_argv[i++] = PERF_DATA;
Quoted 8 lines

tools/perf/builtin-stat.c static enum counter_recovery stat_handle_error(struct evsel *counter) · line 733

Quoted 3 lines
tools/perf/builtin-stat.c +1 -0
+
        return 0;
 }

Still I think it's weird it has 'perf record' in perf stat (despite the 'perf stat record'). If it's only Intel thing and we don't have a plan to do the same on other arches, we can move it to the arch directory and keep the perf stat code simple.

Quoted 4 lines

I'm not sure what is the proper way to solve this. And Ian mentioned that put code in arch directory could potentially cause other bugs. So I'm wondering if we could keep this code here for now. I could work on it later if we found it's better to be in arch directory.

Maybe somewhere in the util/ and keep the main code minimal. IIUC it's only for very recent (or upcoming?) Intel CPUs and we don't have tests (hopefully can run on other arch/CPUs).

So I don't think having it here would help fixing potential bugs.

Quoted 176 lines
Quoted 176 lines
Quoted 5 lines
tools/perf/builtin-stat.c +4 -0
+static void prepare_run_command(struct child_process *cmd,
+                              const char **argv)
+{
+       memset(cmd, 0, sizeof(*cmd));

tools/perf/builtin-stat.c static enum counter_recovery stat_handle_error(struct evsel *counter)

Quoted 10 lines
tools/perf/builtin-stat.c +10 -0
+       cmd->argv = argv;
+       cmd->out = -1;
+}
+
+static int prepare_perf_record(struct child_process *cmd)
+{
+       const char **record_argv;
+       int ret;
+
+       record_argv = calloc(10 + 2 * stat_config.tpebs_event_size, sizeof(char

*));

Quoted 12 lines
tools/perf/builtin-stat.c +12 -0
+       if (!record_argv)
+               return -1;
+
+       ret = __run_perf_record(record_argv);
+       if (ret)
+               return ret;
+
+       prepare_run_command(cmd, record_argv);
+       return start_command(cmd);
+}
+
+struct perf_script {

tools/perf/builtin-stat.c static enum counter_recovery stat_handle_error(struct evsel *counter)

Quoted 10 lines
tools/perf/builtin-stat.c +10 -0
+       struct perf_tool        tool;
+       struct perf_session     *session;
+};
+
+static void tpebs_data__delete(void)
+{
+       struct tpebs_retire_lat *r, *rtmp;
+       struct tpebs_event *e, *etmp;
+
+       list_for_each_entry_safe(r, rtmp, &stat_config.tpebs_results, event.nd)

{

tools/perf/builtin-stat.c static enum counter_recovery stat_handle_error(struct evsel *counter) · line 776

Quoted 6 lines
tools/perf/builtin-stat.c +6 -0
+               list_del_init(&r->event.nd);
+               free(r);
+       }
+       list_for_each_entry_safe(e, etmp, &stat_config.tpebs_events, nd) {
+               list_del_init(&e->nd);
+               free(e);

Shouldn't it free the names?

Quoted 8 lines
tools/perf/builtin-stat.c +8 -0
+       }
+}
+
+static int process_sample_event(struct perf_tool *tool __maybe_unused,
+                               union perf_event *event __maybe_unused,
+                               struct perf_sample *sample,
+                               struct evsel *evsel,
+                               struct machine *machine __maybe_unused)

tools/perf/builtin-stat.c static enum counter_recovery stat_handle_error(struct evsel *counter)

Quoted 10 lines
tools/perf/builtin-stat.c +10 -0
+{
+       int ret = 0;
+       const char *evname;
+       struct tpebs_retire_lat *t;
+
+       evname = evsel__name(evsel);
+
+       /*
+        * Need to handle per core results? We are assuming average retire
+        * latency value will be used. Save the number of samples and the sum

of

Quoted 12 lines
tools/perf/builtin-stat.c +12 -0
+        * retire latency value for each event.
+        */
+       list_for_each_entry(t, &stat_config.tpebs_results, event.nd) {
+               if (!strcmp(evname, t->event.name)) {
+                       t->count += 1;
+                       t->sum += sample->retire_lat;
+                       break;
+               }
+       }
+
+       return ret;
+}

tools/perf/builtin-stat.c static enum counter_recovery stat_handle_error(struct evsel *counter) · line 816

Quoted 10 lines
tools/perf/builtin-stat.c +10 -0
+
+static int process_feature_event(struct perf_session *session,
+                                union perf_event *event)
+{
+       if (event->feat.feat_id < HEADER_LAST_FEATURE)
+               return perf_event__process_feature(session, event);
+       return 0;
+}
+
+static void *__cmd_script(void *arg __maybe_unused)

The arg is used.

Also I don't like the name 'script' as it has nothing to do with scripting. Maybe 'sample_reader', 'tpebs_reader' or 'reader_thread'?

tools/perf/builtin-stat.c static enum counter_recovery stat_handle_error(struct evsel *counter) · line 829

Quoted 13 lines
tools/perf/builtin-stat.c +13 -0
+{
+       struct child_process *cmd = arg;
+       struct perf_session *session;
+       struct perf_data data = {
+               .mode = PERF_DATA_MODE_READ,
+               .path = PERF_DATA,
+               .file.fd = cmd->out,
+       };
+       struct perf_script script = {
+               .tool = {
+               .sample          = process_sample_event,
+               .feature         = process_feature_event,
+               .attr            = perf_event__process_attr,

Broken indentation. And if you just use the tool, you can pass it directly.

tools/perf/builtin-stat.c static int __run_perf_stat(int argc, const char

Quoted 17 lines
tools/perf/builtin-stat.c +13 -0
+               },
+       };
+
+       session = perf_session__new(&data, &script.tool);
+       if (IS_ERR(session))
+               return NULL;
+       script.session = session;
+       perf_session__process_events(session);
+       perf_session__delete(session);
+
+       return NULL;
+}
+
 static int __run_perf_stat(int argc, const char **argv, int run_idx)
 {
        int interval = stat_config.interval;
@@ -709,15 +856,38 @@ static int __run_perf_stat(int argc, const char

**argv, int run_idx)

Quoted 6 lines
tools/perf/builtin-stat.c +2 -0
        struct affinity saved_affinity, *affinity = NULL;
        int err;
        bool second_pass = false;
+       struct child_process cmd;
+       pthread_t thread_script;
 

tools/perf/builtin-stat.c static int __run_perf_stat(int argc, const char **argv, int run_idx)

Quoted 10 lines
tools/perf/builtin-stat.c +4 -1
        /* Prepare perf record for sampling event retire_latency before fork and
         * prepare workload */
        if (stat_config.tpebs_event_size > 0) {
                int ret;
+               struct tpebs_event *e;
 
-               ret = __run_perf_record();
+               pr_debug("perf stat pid = %d\n", getpid());
+               list_for_each_entry(e, &stat_config.tpebs_events, nd) {
+                       struct tpebs_retire_lat *new = malloc(sizeof(struct

tpebs_retire_lat));

tools/perf/builtin-stat.c static int __run_perf_stat(int argc, const char **argv, int run_idx) · line 875

Quoted 5 lines
tools/perf/builtin-stat.c +5 -0
+
+                       if (!new)
+                               return -1;
+                       new->event.name = strdup(e->name);
+                       new->event.tpebs_name = strdup(e->tpebs_name);

These can fail too.

Quoted 15 lines
tools/perf/builtin-stat.c +13 -0
+                       new->count = 0;
+                       new->sum = 0;
+                       list_add_tail(&new->event.nd, &stat_config.tpebs_results);
+               }
+               ret = prepare_perf_record(&cmd);
                if (ret)
                        return ret;
+               if (pthread_create(&thread_script, NULL, __cmd_script, &cmd)) {
+                       kill(cmd.pid, SIGTERM);
+                       close(cmd.out);
+                       pr_err("Could not create thread to process sample data.\n");
+                       return -1;
+               }
+               /* Wait for perf record initialization a little bit.*/
+               sleep(2);
Quoted 8 lines
Quoted 4 lines

This won't guarantee anything. If you want to make sure the 'thread_script' to run before the 'perf record' process, you can use a pipe to signal that like in evlist__prepare_workload() and evlist__start_workload().

This sleep is added to make perf stat wait for record initialization because in the case that the workload runs very small a mount of time, we'd like to ensure perf record has enough time to launch and start collecting sample data.

But waiting for 2 seconds won't solve the problem.

Quoted 4 lines

Because the code uses the common API in run-command.h to do the fork, I think it cannot use PIPE like in evlist__prepare_workload to sync and start perf record and perf stat data collection together. Please correct me if I'm wrong here.

Ok, it'd be hard to sync both perf record and perf stat with a single workload. I think you can try --control option in perf record to enable/disable with timing
you want. Also --synth=no should reduce a lot of overhead during initialization.
Maybe you can also add --synth=no-kernel to completely skip the synthesis.

Thanks,
Namhyung

Quoted 118 lines
Quoted 76 lines

tools/perf/builtin-stat.c static int __run_perf_stat(int argc, const char

Quoted 4 lines
tools/perf/builtin-stat.c +0 -0
        }
 
        if (forks) {
@@ -925,6 +1095,17 @@ static int __run_perf_stat(int argc, const char

**argv, int run_idx)

Quoted 28 lines
tools/perf/builtin-stat.c +14 -0
        t1 = rdclock();
 
+       if (stat_config.tpebs_event_size > 0) {
+               int ret;
+
+               kill(cmd.pid, SIGTERM);
+               pthread_join(thread_script, NULL);
+               close(cmd.out);
+               ret = finish_command(&cmd);
+               if (ret != -ERR_RUN_COMMAND_WAITPID_SIGNAL)
+                       return ret;
+       }
+
        if (stat_config.walltime_run_table)
                stat_config.walltime_run[run_idx] = t1 - t0;
 
@@ -1032,6 +1213,9 @@ static void sig_atexit(void)
        if (child_pid != -1)
                kill(child_pid, SIGTERM);
 
+       if (stat_config.tpebs_pid != -1)
+               kill(stat_config.tpebs_pid, SIGTERM);
+
        sigprocmask(SIG_SETMASK, &oset, NULL);
 
        if (signr == -1)

tools/perf/builtin-stat.c int cmd_stat(int argc, const char **argv) · line 3157

Quoted 2 lines
tools/perf/builtin-stat.c +0 -0
@@ -2972,5 +3156,7 @@ int cmd_stat(int argc, const char **argv)
        metricgroup__rblist_exit(&stat_config.metric_events);
        evlist__close_control(stat_config.ctl_fd, stat_config.ctl_fd_ack,

&stat_config.ctl_fd_close);

Quoted 12 lines
tools/perf/builtin-stat.c +2 -0
+       tpebs_data__delete();
+
        return status;
 }
tools/perf/util/data.c +0 -0
index 08c4bfbd817f..98e3014c0aef 100644
@@ -204,7 +204,11 @@ static bool check_pipe(struct perf_data *data)
                                data->file.fd = fd;
                                data->use_stdio = false;

tools/perf/util/data.c static bool check_pipe(struct perf_data *data) · line 214

Quoted 10 lines
tools/perf/util/data.c +5 -1
                        }
-               } else {
+               /*
+                * When is_pipe and data->file.fd is given, use given fd
+                * instead of STDIN_FILENO or STDOUT_FILENO
+                */
+               } else if (data->file.fd <= 0) {
                        data->file.fd = fd;
                }
        }

I think this can be in a separate commit.

Quoted 14 lines
tools/perf/util/metricgroup.h +2 -0
index 7c24ed768ff3..ae788edef30f 100644
@@ -68,10 +68,18 @@ struct metric_expr {
 
 struct tpebs_event {
        struct list_head nd;
+       /* Event name */
        const char *name;
+       /* Event name with the TPEBS modifier R */
        const char *tpebs_name;
 };
 
Quoted 8 lines

tools/perf/util/metricgroup.h struct metric_expr { · line 81

Quoted 5 lines
tools/perf/util/metricgroup.h +5 -0
+struct tpebs_retire_lat {
+       struct tpebs_event event;
+       size_t count;
+       int sum;
+};

Actually I don't know why you need this separate structure. Can we just use tpebs_event?

Currently, we use average value as the retire latency value in metrics. But we might update it to use other value, for example the minimum or maximum. So, I thought it would be better to have a dedicated data structure to handle this data. I could update the code to use tpebs_event if you still feel that's better.

Thanks,
Weilin

Quoted 25 lines

Thanks,
Namhyung

Quoted 20 lines
tools/perf/util/metricgroup.h +1 -0
+
 struct metric_event *metricgroup__lookup(struct rblist *metric_events,
                                         struct evsel *evsel,
                                         bool create);
tools/perf/util/stat.h +2 -1
index b987960df3c5..0726bdc06681 100644
@@ -111,6 +111,8 @@ struct perf_stat_config {
        struct rblist            metric_events;
        struct list_head         tpebs_events;
        size_t                   tpebs_event_size;
+       struct list_head         tpebs_results;
+       pid_t                    tpebs_pid;
        int                      ctl_fd;
        int                      ctl_fd_ack;
        bool                     ctl_fd_close;
--

2.43.0

#9 lore raw

RE: [RFC PATCH v6 2/5] perf stat: Fork and launch perf record when perf stat needs to get retire latency value for a metric.

Quoted 159 lines

-----Original Message-----
From: Namhyung Kim <[email protected]>
Sent: Tuesday, April 23, 2024 1:59 PM
To: Wang, Weilin <[email protected]>
Cc: Ian Rogers <[email protected]>; Arnaldo Carvalho de Melo <[email protected]>; Peter Zijlstra <[email protected]>; Ingo Molnar <[email protected]>; Alexander Shishkin <[email protected]>; Jiri Olsa <[email protected]>; Hunter, Adrian <[email protected]>; Kan Liang <[email protected]>; [email protected]; [email protected]; Taylor, Perry <[email protected]>; Alt, Samantha <[email protected]>; Biggers, Caleb <[email protected]>
Subject: Re: [RFC PATCH v6 2/5] perf stat: Fork and launch perf record when perf stat needs to get retire latency value for a metric.

Quoted 13 lines · Weilin
Quoted 13 lines

-----Original Message-----
From: Namhyung Kim <[email protected]>
Sent: Monday, April 1, 2024 1:58 PM
To: Wang, Weilin <[email protected]>
Cc: Ian Rogers <[email protected]>; Arnaldo Carvalho de Melo <[email protected]>; Peter Zijlstra <[email protected]>; Ingo Molnar <[email protected]>; Alexander Shishkin <[email protected]>; Jiri Olsa <[email protected]>; Hunter, Adrian <[email protected]>; Kan Liang <[email protected]>; [email protected]; [email protected]; Taylor,

Perry

Quoted 1 line
Quoted 1 line

<[email protected]>; Alt, Samantha <[email protected]>;

Biggers,

Quoted 2 lines
Quoted 2 lines

Caleb <[email protected]>
Subject: Re: [RFC PATCH v6 2/5] perf stat: Fork and launch perf record

when

Quoted 7 lines
Quoted 7 lines

perf stat needs to get retire latency value for a metric.

Quoted 4 lines · On Fri, Mar 29, 2024 at 12:12 PM <[email protected]> wrote:

From: Weilin Wang <[email protected]>

When retire_latency value is used in a metric formula, perf stat would

fork a

Quoted 9 lines
Quoted 9 lines
Quoted 5 lines

perf record process with "-e" and "-W" options. Perf record will collect required retire_latency values in parallel while perf stat is collecting counting values.

At the point of time that perf stat stops counting, it would send sigterm

signal

Quoted 1 line

to perf record process and receiving sampling data back from perf record

from a

Quoted 1 line

pipe. Perf stat will then process the received data to get retire latency

data

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

and calculate metric result.

Another thread is required to synchronize between perf stat and perf

record

Quoted 28 lines
Quoted 28 lines
Quoted 6 lines

when we pass data through pipe.

---
 tools/perf/builtin-stat.c     | 190

+++++++++++++++++++++++++++++++++-

Quoted 21 lines
 tools/perf/util/data.c        |   6 +-
 tools/perf/util/metricgroup.h |   8 ++
 tools/perf/util/stat.h        |   2 +
 4 files changed, 203 insertions(+), 3 deletions(-)
tools/perf/builtin-stat.c +5 -0
index 6291e1e24535..7fbe47b0c44c 100644
@@ -94,8 +94,13 @@
 #include <perf/evlist.h>
 #include <internal/threadmap.h>
 
+#include "util/sample.h"
+#include <sys/param.h>
+#include <subcmd/run-command.h>
+
 #define DEFAULT_SEPARATOR      " "
 #define FREEZE_ON_SMI_PATH     "devices/cpu/freeze_on_smi"
+#define PERF_DATA              "-"
 
Quoted 1 line
Quoted 1 line

tools/perf/builtin-stat.c line 105

Quoted 1 line
tools/perf/builtin-stat.c +0 -0
 static void print_counters(struct timespec *ts, int argc, const char

**argv);

Quoted 34 lines
Quoted 34 lines

tools/perf/builtin-stat.c static enum counter_recovery

Quoted 11 lines
tools/perf/builtin-stat.c +2 -0
@@ -163,6 +168,8 @@ static struct perf_stat_config stat_config = {
        .ctl_fd_ack             = -1,
        .iostat_run             = false,
        .tpebs_events           = LIST_HEAD_INIT(stat_config.tpebs_events),
+       .tpebs_results          = LIST_HEAD_INIT(stat_config.tpebs_results),
+       .tpebs_pid              = -1,
 };
 
 static bool cpus_map_matched(struct evsel *a, struct evsel *b)
@@ -684,15 +691,155 @@ static enum counter_recovery

stat_handle_error(struct evsel *counter)

Quoted 22 lines
tools/perf/builtin-stat.c +13 -1
        if (child_pid != -1)
                kill(child_pid, SIGTERM);
+       if (stat_config.tpebs_pid != -1)
+               kill(stat_config.tpebs_pid, SIGTERM);
        return COUNTER_FATAL;
 }
 
-static int __run_perf_record(void)
+static int __run_perf_record(const char **record_argv)
 {
+       int i = 0;
+       struct tpebs_event *e;
+
        pr_debug("Prepare perf record for retire_latency\n");
+
+       record_argv[i++] = "perf";
+       record_argv[i++] = "record";
+       record_argv[i++] = "-W";
+       record_argv[i++] = "--synth=no";
+
+       if (stat_config.user_requested_cpu_list) {
Quoted 8 lines
Quoted 8 lines

tools/perf/builtin-stat.c static enum counter_recovery stat_handle_error(struct evsel *counter)

Quoted 8 lines
tools/perf/builtin-stat.c +8 -0
+               record_argv[i++] = "-C";
+               record_argv[i++] = stat_config.user_requested_cpu_list;
+       }
+
+       if (stat_config.system_wide)
+               record_argv[i++] = "-a";
+
+       if (!stat_config.system_wide

&& !stat_config.user_requested_cpu_list)

Quoted 21 lines
Quoted 12 lines

{

Quoted 11 lines
tools/perf/builtin-stat.c +11 -0
+               pr_err("Require -a or -C option to run sampling.\n");
+               return -ECANCELED;
+       }
+
+       list_for_each_entry(e, &stat_config.tpebs_events, nd) {
+               record_argv[i++] = "-e";
+               record_argv[i++] = e->name;
+       }
+
+       record_argv[i++] = "-o";
+       record_argv[i++] = PERF_DATA;
Quoted 8 lines

tools/perf/builtin-stat.c static enum counter_recovery stat_handle_error(struct evsel *counter) · line 733

Quoted 3 lines
tools/perf/builtin-stat.c +1 -0
+
        return 0;
 }

Still I think it's weird it has 'perf record' in perf stat (despite the 'perf stat record'). If it's only Intel thing and we don't have a plan to do the same on other arches, we can move it to the arch directory and keep the perf stat code simple.

Quoted 4 lines

I'm not sure what is the proper way to solve this. And Ian mentioned that put code in arch directory could potentially cause other bugs. So I'm wondering if we could keep this code here for now. I could work on it later if we found it's better to be in arch directory.

Quoted 5 lines

Maybe somewhere in the util/ and keep the main code minimal. IIUC it's only for very recent (or upcoming?) Intel CPUs and we don't have tests (hopefully can run on other arch/CPUs).

So I don't think having it here would help fixing potential bugs.

Do you mean by creating a new file in util/ to hold this code?

Yes, this feature is for very recent Intel CPUs. It should only be triggered if a metric uses event(s) that has the R modifier in the formula.

Thanks,
Weilin

Quoted 334 lines
Quoted 16 lines
Quoted 16 lines
Quoted 6 lines
tools/perf/builtin-stat.c +4 -0
+static void prepare_run_command(struct child_process *cmd,
+                              const char **argv)
+{
+       memset(cmd, 0, sizeof(*cmd));

tools/perf/builtin-stat.c static enum counter_recovery stat_handle_error(struct evsel *counter)

Quoted 10 lines
tools/perf/builtin-stat.c +10 -0
+       cmd->argv = argv;
+       cmd->out = -1;
+}
+
+static int prepare_perf_record(struct child_process *cmd)
+{
+       const char **record_argv;
+       int ret;
+
+       record_argv = calloc(10 + 2 * stat_config.tpebs_event_size,

sizeof(char

Quoted 15 lines
Quoted 15 lines

*));

Quoted 14 lines
tools/perf/builtin-stat.c +14 -0
+       if (!record_argv)
+               return -1;
+
+       ret = __run_perf_record(record_argv);
+       if (ret)
+               return ret;
+
+       prepare_run_command(cmd, record_argv);
+       return start_command(cmd);
+}
+
+struct perf_script {
+       struct perf_tool        tool;
+       struct perf_session     *session;
Quoted 8 lines
Quoted 8 lines

tools/perf/builtin-stat.c static enum counter_recovery stat_handle_error(struct evsel *counter)

Quoted 8 lines
tools/perf/builtin-stat.c +8 -0
+};
+
+static void tpebs_data__delete(void)
+{
+       struct tpebs_retire_lat *r, *rtmp;
+       struct tpebs_event *e, *etmp;
+
+       list_for_each_entry_safe(r, rtmp, &stat_config.tpebs_results,

event.nd)

Quoted 8 lines
Quoted 8 lines

{

Quoted 6 lines
tools/perf/builtin-stat.c +6 -0
+               list_del_init(&r->event.nd);
+               free(r);
+       }
+       list_for_each_entry_safe(e, etmp, &stat_config.tpebs_events, nd) {
+               list_del_init(&e->nd);
+               free(e);
Quoted 7 lines
Quoted 7 lines

Shouldn't it free the names?

tools/perf/builtin-stat.c static enum counter_recovery stat_handle_error(struct evsel *counter)

Quoted 4 lines
tools/perf/builtin-stat.c +4 -0
+       }
+}
+
+static int process_sample_event(struct perf_tool *tool

__maybe_unused,

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
tools/perf/builtin-stat.c +6 -0
+                               union perf_event *event __maybe_unused,
+                               struct perf_sample *sample,
+                               struct evsel *evsel,
+                               struct machine *machine __maybe_unused)
+{
+       int ret = 0;
Quoted 8 lines
Quoted 8 lines

tools/perf/builtin-stat.c static enum counter_recovery stat_handle_error(struct evsel *counter)

Quoted 8 lines
tools/perf/builtin-stat.c +8 -0
+       const char *evname;
+       struct tpebs_retire_lat *t;
+
+       evname = evsel__name(evsel);
+
+       /*
+        * Need to handle per core results? We are assuming average retire
+        * latency value will be used. Save the number of samples and the

sum

Quoted 67 lines
Quoted 67 lines

of

Quoted 12 lines
tools/perf/builtin-stat.c +12 -0
+        * retire latency value for each event.
+        */
+       list_for_each_entry(t, &stat_config.tpebs_results, event.nd) {
+               if (!strcmp(evname, t->event.name)) {
+                       t->count += 1;
+                       t->sum += sample->retire_lat;
+                       break;
+               }
+       }
+
+       return ret;
+}

tools/perf/builtin-stat.c static enum counter_recovery stat_handle_error(struct evsel *counter) · line 816

Quoted 10 lines
tools/perf/builtin-stat.c +10 -0
+
+static int process_feature_event(struct perf_session *session,
+                                union perf_event *event)
+{
+       if (event->feat.feat_id < HEADER_LAST_FEATURE)
+               return perf_event__process_feature(session, event);
+       return 0;
+}
+
+static void *__cmd_script(void *arg __maybe_unused)

The arg is used.

Also I don't like the name 'script' as it has nothing to do with scripting. Maybe 'sample_reader', 'tpebs_reader' or 'reader_thread'?

tools/perf/builtin-stat.c static enum counter_recovery stat_handle_error(struct evsel *counter) · line 829

Quoted 13 lines
tools/perf/builtin-stat.c +13 -0
+{
+       struct child_process *cmd = arg;
+       struct perf_session *session;
+       struct perf_data data = {
+               .mode = PERF_DATA_MODE_READ,
+               .path = PERF_DATA,
+               .file.fd = cmd->out,
+       };
+       struct perf_script script = {
+               .tool = {
+               .sample          = process_sample_event,
+               .feature         = process_feature_event,
+               .attr            = perf_event__process_attr,

Broken indentation. And if you just use the tool, you can pass it directly.

tools/perf/builtin-stat.c static int __run_perf_stat(int argc, const char

Quoted 17 lines
tools/perf/builtin-stat.c +13 -0
+               },
+       };
+
+       session = perf_session__new(&data, &script.tool);
+       if (IS_ERR(session))
+               return NULL;
+       script.session = session;
+       perf_session__process_events(session);
+       perf_session__delete(session);
+
+       return NULL;
+}
+
 static int __run_perf_stat(int argc, const char **argv, int run_idx)
 {
        int interval = stat_config.interval;
@@ -709,15 +856,38 @@ static int __run_perf_stat(int argc, const char

**argv, int run_idx)

Quoted 7 lines
Quoted 7 lines

tools/perf/builtin-stat.c static int __run_perf_stat(int argc, const char **argv, int run_idx)

Quoted 7 lines
tools/perf/builtin-stat.c +2 -0
        struct affinity saved_affinity, *affinity = NULL;
        int err;
        bool second_pass = false;
+       struct child_process cmd;
+       pthread_t thread_script;
 
        /* Prepare perf record for sampling event retire_latency before fork

and

Quoted 19 lines
Quoted 19 lines

tools/perf/builtin-stat.c static int __run_perf_stat(int argc, const char **argv, int run_idx)

Quoted 9 lines
tools/perf/builtin-stat.c +4 -1
         * prepare workload */
        if (stat_config.tpebs_event_size > 0) {
                int ret;
+               struct tpebs_event *e;
 
-               ret = __run_perf_record();
+               pr_debug("perf stat pid = %d\n", getpid());
+               list_for_each_entry(e, &stat_config.tpebs_events, nd) {
+                       struct tpebs_retire_lat *new = malloc(sizeof(struct

tpebs_retire_lat));

tools/perf/builtin-stat.c static int __run_perf_stat(int argc, const char **argv, int run_idx) · line 875

Quoted 5 lines
tools/perf/builtin-stat.c +5 -0
+
+                       if (!new)
+                               return -1;
+                       new->event.name = strdup(e->name);
+                       new->event.tpebs_name = strdup(e->tpebs_name);

These can fail too.

Quoted 8 lines
Quoted 8 lines

tools/perf/builtin-stat.c static int __run_perf_stat(int argc, const char **argv, int run_idx)

Quoted 8 lines
tools/perf/builtin-stat.c +6 -0
+                       new->count = 0;
+                       new->sum = 0;
+                       list_add_tail(&new->event.nd, &stat_config.tpebs_results);
+               }
+               ret = prepare_perf_record(&cmd);
                if (ret)
                        return ret;
+               if (pthread_create(&thread_script, NULL, __cmd_script, &cmd))

{

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
tools/perf/builtin-stat.c +7 -0
+                       kill(cmd.pid, SIGTERM);
+                       close(cmd.out);
+                       pr_err("Could not create thread to process sample data.\n");
+                       return -1;
+               }
+               /* Wait for perf record initialization a little bit.*/
+               sleep(2);
Quoted 6 lines
Quoted 4 lines

This won't guarantee anything. If you want to make sure the 'thread_script' to run before the 'perf record' process, you can use a pipe to signal that like in evlist__prepare_workload() and evlist__start_workload().

This sleep is added to make perf stat wait for record initialization because in

the

Quoted 1 line

case that the workload runs very small a mount of time, we'd like to ensure

perf

Quoted 1 line

record has enough time to launch and start collecting sample data.

But waiting for 2 seconds won't solve the problem.

Quoted 2 lines

Because the code uses the common API in run-command.h to do the fork, I

think

Quoted 1 line

it cannot use PIPE like in evlist__prepare_workload to sync and start perf

record

Quoted 1 line

and perf stat data collection together. Please correct me if I'm wrong here.

Ok, it'd be hard to sync both perf record and perf stat with a single workload. I think you can try --control option in perf record to enable/disable with timing
you want. Also --synth=no should reduce a lot of overhead during initialization.
Maybe you can also add --synth=no-kernel to completely skip the synthesis.

Thanks,
Namhyung

Quoted 85 lines
Quoted 76 lines

tools/perf/builtin-stat.c static int __run_perf_stat(int argc, const char

Quoted 4 lines
tools/perf/builtin-stat.c +0 -0
        }
 
        if (forks) {
@@ -925,6 +1095,17 @@ static int __run_perf_stat(int argc, const char

**argv, int run_idx)

Quoted 28 lines
tools/perf/builtin-stat.c +14 -0
        t1 = rdclock();
 
+       if (stat_config.tpebs_event_size > 0) {
+               int ret;
+
+               kill(cmd.pid, SIGTERM);
+               pthread_join(thread_script, NULL);
+               close(cmd.out);
+               ret = finish_command(&cmd);
+               if (ret != -ERR_RUN_COMMAND_WAITPID_SIGNAL)
+                       return ret;
+       }
+
        if (stat_config.walltime_run_table)
                stat_config.walltime_run[run_idx] = t1 - t0;
 
@@ -1032,6 +1213,9 @@ static void sig_atexit(void)
        if (child_pid != -1)
                kill(child_pid, SIGTERM);
 
+       if (stat_config.tpebs_pid != -1)
+               kill(stat_config.tpebs_pid, SIGTERM);
+
        sigprocmask(SIG_SETMASK, &oset, NULL);
 
        if (signr == -1)

tools/perf/builtin-stat.c int cmd_stat(int argc, const char **argv) · line 3157

Quoted 2 lines
tools/perf/builtin-stat.c +0 -0
@@ -2972,5 +3156,7 @@ int cmd_stat(int argc, const char **argv)
        metricgroup__rblist_exit(&stat_config.metric_events);
        evlist__close_control(stat_config.ctl_fd, stat_config.ctl_fd_ack,

&stat_config.ctl_fd_close);

Quoted 12 lines
tools/perf/builtin-stat.c +2 -0
+       tpebs_data__delete();
+
        return status;
 }
tools/perf/util/data.c +0 -0
index 08c4bfbd817f..98e3014c0aef 100644
@@ -204,7 +204,11 @@ static bool check_pipe(struct perf_data *data)
                                data->file.fd = fd;
                                data->use_stdio = false;

tools/perf/util/data.c static bool check_pipe(struct perf_data *data) · line 214

Quoted 10 lines
tools/perf/util/data.c +5 -1
                        }
-               } else {
+               /*
+                * When is_pipe and data->file.fd is given, use given fd
+                * instead of STDIN_FILENO or STDOUT_FILENO
+                */
+               } else if (data->file.fd <= 0) {
                        data->file.fd = fd;
                }
        }

I think this can be in a separate commit.

Quoted 14 lines
tools/perf/util/metricgroup.h +2 -0
index 7c24ed768ff3..ae788edef30f 100644
@@ -68,10 +68,18 @@ struct metric_expr {
 
 struct tpebs_event {
        struct list_head nd;
+       /* Event name */
        const char *name;
+       /* Event name with the TPEBS modifier R */
        const char *tpebs_name;
 };
 
Quoted 8 lines

tools/perf/util/metricgroup.h struct metric_expr { · line 81

Quoted 5 lines
tools/perf/util/metricgroup.h +5 -0
+struct tpebs_retire_lat {
+       struct tpebs_event event;
+       size_t count;
+       int sum;
+};

Actually I don't know why you need this separate structure. Can we just use tpebs_event?

Quoted 2 lines

Currently, we use average value as the retire latency value in metrics. But we might update it to use other value, for example the minimum or maximum.

So, I thought

Quoted 1 line

it would be better to have a dedicated data structure to handle this data. I

could update

Quoted 30 lines

the code to use tpebs_event if you still feel that's better.

Thanks,
Weilin

Quoted 25 lines

Thanks,
Namhyung

Quoted 20 lines
tools/perf/util/metricgroup.h +1 -0
+
 struct metric_event *metricgroup__lookup(struct rblist *metric_events,
                                         struct evsel *evsel,
                                         bool create);
tools/perf/util/stat.h +2 -1
index b987960df3c5..0726bdc06681 100644
@@ -111,6 +111,8 @@ struct perf_stat_config {
        struct rblist            metric_events;
        struct list_head         tpebs_events;
        size_t                   tpebs_event_size;
+       struct list_head         tpebs_results;
+       pid_t                    tpebs_pid;
        int                      ctl_fd;
        int                      ctl_fd_ack;
        bool                     ctl_fd_close;
--

2.43.0

maintainer
#10 lore raw

Re: [RFC PATCH v6 2/5] perf stat: Fork and launch perf record when perf stat needs to get retire latency value for a metric.

Quoted 49 lines · Weilin
Quoted 49 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
tools/perf/builtin-stat.c +11 -1
-static int __run_perf_record(void)
+static int __run_perf_record(const char **record_argv)
 {
+       int i = 0;
+       struct tpebs_event *e;
+
        pr_debug("Prepare perf record for retire_latency\n");
+
+       record_argv[i++] = "perf";
+       record_argv[i++] = "record";
+       record_argv[i++] = "-W";
+       record_argv[i++] = "--synth=no";
+
+       if (stat_config.user_requested_cpu_list) {
Quoted 8 lines
Quoted 8 lines

tools/perf/builtin-stat.c static enum counter_recovery stat_handle_error(struct evsel *counter)

Quoted 8 lines
tools/perf/builtin-stat.c +8 -0
+               record_argv[i++] = "-C";
+               record_argv[i++] = stat_config.user_requested_cpu_list;
+       }
+
+       if (stat_config.system_wide)
+               record_argv[i++] = "-a";
+
+       if (!stat_config.system_wide

&& !stat_config.user_requested_cpu_list)

Quoted 25 lines
Quoted 12 lines

{

Quoted 11 lines
tools/perf/builtin-stat.c +11 -0
+               pr_err("Require -a or -C option to run sampling.\n");
+               return -ECANCELED;
+       }
+
+       list_for_each_entry(e, &stat_config.tpebs_events, nd) {
+               record_argv[i++] = "-e";
+               record_argv[i++] = e->name;
+       }
+
+       record_argv[i++] = "-o";
+       record_argv[i++] = PERF_DATA;
Quoted 8 lines
Quoted 3 lines

+

        return 0;

}

Still I think it's weird it has 'perf record' in perf stat (despite the 'perf stat record'). If it's only Intel thing and we don't have a plan to do the same on other arches, we can move it to the arch directory and keep the perf stat code simple.

I'm not sure what is the proper way to solve this. And Ian mentioned that put code in arch directory could potentially cause other bugs. So I'm wondering if we could keep this code here for now. I could work on it later if we found it's better to be in arch directory.

Quoted 7 lines
Quoted 5 lines

Maybe somewhere in the util/ and keep the main code minimal. IIUC it's only for very recent (or upcoming?) Intel CPUs and we don't have tests (hopefully can run on other arch/CPUs).

So I don't think having it here would help fixing potential bugs.

Do you mean by creating a new file in util/ to hold this code?

Yeah, maybe util/intel-tpebs.c (if it's better than arch/x86/...) ?

Quoted 3 lines

Yes, this feature is for very recent Intel CPUs. It should only be triggered if a metric uses event(s) that has the R modifier in the formula.

Can we have a test with a fake metric so that we can test the code on non-(or old-)Intel machines?

Thanks,
Namhyung

#11 lore raw

RE: [RFC PATCH v6 2/5] perf stat: Fork and launch perf record when perf stat needs to get retire latency value for a metric.

Quoted 76 lines

-----Original Message-----
From: Namhyung Kim <[email protected]>
Sent: Tuesday, April 23, 2024 4:06 PM
To: Wang, Weilin <[email protected]>
Cc: Ian Rogers <[email protected]>; Arnaldo Carvalho de Melo <[email protected]>; Peter Zijlstra <[email protected]>; Ingo Molnar <[email protected]>; Alexander Shishkin <[email protected]>; Jiri Olsa <[email protected]>; Hunter, Adrian <[email protected]>; Kan Liang <[email protected]>; [email protected]; [email protected]; Taylor, Perry <[email protected]>; Alt, Samantha <[email protected]>; Biggers, Caleb <[email protected]>
Subject: Re: [RFC PATCH v6 2/5] perf stat: Fork and launch perf record when perf stat needs to get retire latency value for a metric.

Quoted 49 lines · Weilin
Quoted 49 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
tools/perf/builtin-stat.c +11 -1
-static int __run_perf_record(void)
+static int __run_perf_record(const char **record_argv)
 {
+       int i = 0;
+       struct tpebs_event *e;
+
        pr_debug("Prepare perf record for retire_latency\n");
+
+       record_argv[i++] = "perf";
+       record_argv[i++] = "record";
+       record_argv[i++] = "-W";
+       record_argv[i++] = "--synth=no";
+
+       if (stat_config.user_requested_cpu_list) {
Quoted 8 lines
Quoted 8 lines

tools/perf/builtin-stat.c static enum counter_recovery stat_handle_error(struct evsel *counter)

Quoted 8 lines
tools/perf/builtin-stat.c +8 -0
+               record_argv[i++] = "-C";
+               record_argv[i++] = stat_config.user_requested_cpu_list;
+       }
+
+       if (stat_config.system_wide)
+               record_argv[i++] = "-a";
+
+       if (!stat_config.system_wide

&& !stat_config.user_requested_cpu_list)

Quoted 25 lines
Quoted 12 lines

{

Quoted 11 lines
tools/perf/builtin-stat.c +11 -0
+               pr_err("Require -a or -C option to run sampling.\n");
+               return -ECANCELED;
+       }
+
+       list_for_each_entry(e, &stat_config.tpebs_events, nd) {
+               record_argv[i++] = "-e";
+               record_argv[i++] = e->name;
+       }
+
+       record_argv[i++] = "-o";
+       record_argv[i++] = PERF_DATA;
Quoted 8 lines
Quoted 3 lines

+

        return 0;

}

Still I think it's weird it has 'perf record' in perf stat (despite the 'perf stat record'). If it's only Intel thing and we don't have a plan to do the same on other arches, we can move it to the arch directory and keep the perf stat code simple.

I'm not sure what is the proper way to solve this. And Ian mentioned that put code in arch directory could potentially cause other bugs. So I'm wondering if we could keep this code here for now. I could work on it later if we found it's better to be in arch directory.

Quoted 7 lines
Quoted 5 lines

Maybe somewhere in the util/ and keep the main code minimal. IIUC it's only for very recent (or upcoming?) Intel CPUs and we don't have tests (hopefully can run on other arch/CPUs).

So I don't think having it here would help fixing potential bugs.

Do you mean by creating a new file in util/ to hold this code?

Quoted 8 lines

Yeah, maybe util/intel-tpebs.c (if it's better than arch/x86/...) ?

Quoted 3 lines

Yes, this feature is for very recent Intel CPUs. It should only be triggered if a metric uses event(s) that has the R modifier in the formula.

Can we have a test with a fake metric so that we can test the code on non-(or old-)Intel machines?

All the existing metrics in non-(or old-)Intel CPUs should work as usual. So I think existing metric tests should work for it.

If we want to add a fake metric uses the :R modifier for those platforms, the metric should either fail (if the fake metric uses an event not exist on the test platform) or return all 0 retire latency data.

So, I'm not quite sure what we want the fake metric to test for. Maybe we could continue using existing metric tests to ensure all the defined metrics work correctly in each machine under test?

Thanks,
Weilin

Quoted 3 lines

Thanks,
Namhyung

maintainer
#12 lore raw

Re: [RFC PATCH v6 2/5] perf stat: Fork and launch perf record when perf stat needs to get retire latency value for a metric.

Quoted 89 lines · Weilin
Quoted 77 lines

-----Original Message-----
From: Namhyung Kim <[email protected]>
Sent: Tuesday, April 23, 2024 4:06 PM
To: Wang, Weilin <[email protected]>
Cc: Ian Rogers <[email protected]>; Arnaldo Carvalho de Melo <[email protected]>; Peter Zijlstra <[email protected]>; Ingo Molnar <[email protected]>; Alexander Shishkin <[email protected]>; Jiri Olsa <[email protected]>; Hunter, Adrian <[email protected]>; Kan Liang <[email protected]>; [email protected]; [email protected]; Taylor, Perry <[email protected]>; Alt, Samantha <[email protected]>; Biggers, Caleb <[email protected]>
Subject: Re: [RFC PATCH v6 2/5] perf stat: Fork and launch perf record when perf stat needs to get retire latency value for a metric.

Quoted 49 lines · Weilin
Quoted 49 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
(quoted diff) +11 -1
-static int __run_perf_record(void)
+static int __run_perf_record(const char **record_argv)
 {
+       int i = 0;
+       struct tpebs_event *e;
+
        pr_debug("Prepare perf record for retire_latency\n");
+
+       record_argv[i++] = "perf";
+       record_argv[i++] = "record";
+       record_argv[i++] = "-W";
+       record_argv[i++] = "--synth=no";
+
+       if (stat_config.user_requested_cpu_list) {
Quoted 8 lines
Quoted 8 lines

Review

Quoted 8 lines
(quoted diff) +8 -0
+               record_argv[i++] = "-C";
+               record_argv[i++] = stat_config.user_requested_cpu_list;
+       }
+
+       if (stat_config.system_wide)
+               record_argv[i++] = "-a";
+
+       if (!stat_config.system_wide

&& !stat_config.user_requested_cpu_list)

Quoted 25 lines
Quoted 12 lines

{

Quoted 11 lines
(quoted diff) +11 -0
+               pr_err("Require -a or -C option to run sampling.\n");
+               return -ECANCELED;
+       }
+
+       list_for_each_entry(e, &stat_config.tpebs_events, nd) {
+               record_argv[i++] = "-e";
+               record_argv[i++] = e->name;
+       }
+
+       record_argv[i++] = "-o";
+       record_argv[i++] = PERF_DATA;
Quoted 8 lines
Quoted 3 lines

+

        return 0;

}

Still I think it's weird it has 'perf record' in perf stat (despite the 'perf stat record'). If it's only Intel thing and we don't have a plan to do the same on other arches, we can move it to the arch directory and keep the perf stat code simple.

I'm not sure what is the proper way to solve this. And Ian mentioned that put code in arch directory could potentially cause other bugs. So I'm wondering if we could keep this code here for now. I could work on it later if we found it's better to be in arch directory.

Quoted 7 lines
Quoted 5 lines

Maybe somewhere in the util/ and keep the main code minimal. IIUC it's only for very recent (or upcoming?) Intel CPUs and we don't have tests (hopefully can run on other arch/CPUs).

So I don't think having it here would help fixing potential bugs.

Do you mean by creating a new file in util/ to hold this code?

Quoted 8 lines

Yeah, maybe util/intel-tpebs.c (if it's better than arch/x86/...) ?

Quoted 3 lines

Yes, this feature is for very recent Intel CPUs. It should only be triggered if a metric uses event(s) that has the R modifier in the formula.

Can we have a test with a fake metric so that we can test the code on non-(or old-)Intel machines?

All the existing metrics in non-(or old-)Intel CPUs should work as usual. So I think existing metric tests should work for it.

Quoted 7 lines

If we want to add a fake metric uses the :R modifier for those platforms, the metric should either fail (if the fake metric uses an event not exist on the test platform) or return all 0 retire latency data.

So, I'm not quite sure what we want the fake metric to test for. Maybe we could continue using existing metric tests to ensure all the defined metrics work correctly in each machine under test?

I think it's ok to return all 0 retire latency for fake tPEBS metrics. It's just to verify the background record + report logic works ok.

Thanks,
Namhyung

#13 lore raw

RE: [RFC PATCH v6 2/5] perf stat: Fork and launch perf record when perf stat needs to get retire latency value for a metric.

Quoted 119 lines

-----Original Message-----
From: Namhyung Kim <[email protected]>
Sent: Wednesday, April 24, 2024 11:50 AM
To: Wang, Weilin <[email protected]>
Cc: Ian Rogers <[email protected]>; Arnaldo Carvalho de Melo <[email protected]>; Peter Zijlstra <[email protected]>; Ingo Molnar <[email protected]>; Alexander Shishkin <[email protected]>; Jiri Olsa <[email protected]>; Hunter, Adrian <[email protected]>; Kan Liang <[email protected]>; [email protected]; [email protected]; Taylor, Perry <[email protected]>; Alt, Samantha <[email protected]>; Biggers, Caleb <[email protected]>
Subject: Re: [RFC PATCH v6 2/5] perf stat: Fork and launch perf record when perf stat needs to get retire latency value for a metric.

Quoted 13 lines · Weilin
Quoted 13 lines

-----Original Message-----
From: Namhyung Kim <[email protected]>
Sent: Tuesday, April 23, 2024 4:06 PM
To: Wang, Weilin <[email protected]>
Cc: Ian Rogers <[email protected]>; Arnaldo Carvalho de Melo <[email protected]>; Peter Zijlstra <[email protected]>; Ingo Molnar <[email protected]>; Alexander Shishkin <[email protected]>; Jiri Olsa <[email protected]>; Hunter, Adrian <[email protected]>; Kan Liang <[email protected]>; [email protected]; [email protected]; Taylor,

Perry

Quoted 1 line
Quoted 1 line

<[email protected]>; Alt, Samantha <[email protected]>;

Biggers,

Quoted 2 lines
Quoted 2 lines

Caleb <[email protected]>
Subject: Re: [RFC PATCH v6 2/5] perf stat: Fork and launch perf record

when

Quoted 48 lines
Quoted 48 lines

perf stat needs to get retire latency value for a metric.

Quoted 44 lines · Weilin
Quoted 44 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
(quoted diff) +11 -1
-static int __run_perf_record(void)
+static int __run_perf_record(const char **record_argv)
 {
+       int i = 0;
+       struct tpebs_event *e;
+
        pr_debug("Prepare perf record for retire_latency\n");
+
+       record_argv[i++] = "perf";
+       record_argv[i++] = "record";
+       record_argv[i++] = "-W";
+       record_argv[i++] = "--synth=no";
+
+       if (stat_config.user_requested_cpu_list) {
Quoted 8 lines
Quoted 8 lines

Review

Quoted 8 lines
(quoted diff) +8 -0
+               record_argv[i++] = "-C";
+               record_argv[i++] = stat_config.user_requested_cpu_list;
+       }
+
+       if (stat_config.system_wide)
+               record_argv[i++] = "-a";
+
+       if (!stat_config.system_wide

&& !stat_config.user_requested_cpu_list)

Quoted 21 lines
Quoted 20 lines

{

Quoted 4 lines
(quoted diff) +4 -0
+               pr_err("Require -a or -C option to run sampling.\n");
+               return -ECANCELED;
+       }
+

Review

Quoted 10 lines
(quoted diff) +8 -0
+       list_for_each_entry(e, &stat_config.tpebs_events, nd) {
+               record_argv[i++] = "-e";
+               record_argv[i++] = e->name;
+       }
+
+       record_argv[i++] = "-o";
+       record_argv[i++] = PERF_DATA;
+
        return 0;
 }

Still I think it's weird it has 'perf record' in perf stat (despite the 'perf stat record'). If it's only Intel thing and we don't have a plan to do the same on other arches, we can move it to the arch directory and keep the perf stat code simple.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

I'm not sure what is the proper way to solve this. And Ian mentioned that put code in arch directory could potentially cause other bugs. So I'm wondering if we could keep this code here for now. I could

work

Quoted 13 lines
Quoted 13 lines
Quoted 9 lines
Quoted 7 lines
Quoted 1 line

on it later if we found it's better to be in arch directory.

Maybe somewhere in the util/ and keep the main code minimal. IIUC it's only for very recent (or upcoming?) Intel CPUs and we don't have tests (hopefully can run on other arch/CPUs).

So I don't think having it here would help fixing potential bugs.

Do you mean by creating a new file in util/ to hold this code?

Yeah, maybe util/intel-tpebs.c (if it's better than arch/x86/...) ?

Quoted 1 line
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Yes, this feature is for very recent Intel CPUs. It should only be triggered if a metric uses event(s) that has the R modifier in the formula.

Can we have a test with a fake metric so that we can test the code on non-(or old-)Intel machines?

All the existing metrics in non-(or old-)Intel CPUs should work as usual. So I

think

Quoted 3 lines

existing metric tests should work for it.

If we want to add a fake metric uses the :R modifier for those platforms, the

metric

Quoted 1 line

should either fail (if the fake metric uses an event not exist on the test

platform) or

Quoted 2 lines

return all 0 retire latency data.

Quoted 8 lines
Quoted 1 line

So, I'm not quite sure what we want the fake metric to test for. Maybe we

could

Quoted 1 line

continue using existing metric tests to ensure all the defined metrics work

correctly

Quoted 1 line

in each machine under test?

I think it's ok to return all 0 retire latency for fake tPEBS metrics. It's just to verify the background record + report logic works ok.

Ok, I will work on this. We need a fake metric that uses events exist on all the platforms.

Thanks,
Weilin

Quoted 3 lines

Thanks,
Namhyung

#14 lore raw

RE: [RFC PATCH v6 2/5] perf stat: Fork and launch perf record when perf stat needs to get retire latency value for a metric.

In reply to Namhyung Kim
Quoted 119 lines

-----Original Message-----
From: Namhyung Kim <[email protected]>
Sent: Wednesday, April 24, 2024 11:50 AM
To: Wang, Weilin <[email protected]>
Cc: Ian Rogers <[email protected]>; Arnaldo Carvalho de Melo <[email protected]>; Peter Zijlstra <[email protected]>; Ingo Molnar <[email protected]>; Alexander Shishkin <[email protected]>; Jiri Olsa <[email protected]>; Hunter, Adrian <[email protected]>; Kan Liang <[email protected]>; [email protected]; [email protected]; Taylor, Perry <[email protected]>; Alt, Samantha <[email protected]>; Biggers, Caleb <[email protected]>
Subject: Re: [RFC PATCH v6 2/5] perf stat: Fork and launch perf record when perf stat needs to get retire latency value for a metric.

Quoted 13 lines · Weilin
Quoted 13 lines

-----Original Message-----
From: Namhyung Kim <[email protected]>
Sent: Tuesday, April 23, 2024 4:06 PM
To: Wang, Weilin <[email protected]>
Cc: Ian Rogers <[email protected]>; Arnaldo Carvalho de Melo <[email protected]>; Peter Zijlstra <[email protected]>; Ingo Molnar <[email protected]>; Alexander Shishkin <[email protected]>; Jiri Olsa <[email protected]>; Hunter, Adrian <[email protected]>; Kan Liang <[email protected]>; [email protected]; [email protected]; Taylor,

Perry

Quoted 1 line
Quoted 1 line

<[email protected]>; Alt, Samantha <[email protected]>;

Biggers,

Quoted 2 lines
Quoted 2 lines

Caleb <[email protected]>
Subject: Re: [RFC PATCH v6 2/5] perf stat: Fork and launch perf record

when

Quoted 48 lines
Quoted 48 lines

perf stat needs to get retire latency value for a metric.

Quoted 44 lines · Weilin
Quoted 44 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
(quoted diff) +11 -1
-static int __run_perf_record(void)
+static int __run_perf_record(const char **record_argv)
 {
+       int i = 0;
+       struct tpebs_event *e;
+
        pr_debug("Prepare perf record for retire_latency\n");
+
+       record_argv[i++] = "perf";
+       record_argv[i++] = "record";
+       record_argv[i++] = "-W";
+       record_argv[i++] = "--synth=no";
+
+       if (stat_config.user_requested_cpu_list) {
Quoted 8 lines
Quoted 8 lines

Review

Quoted 8 lines
(quoted diff) +8 -0
+               record_argv[i++] = "-C";
+               record_argv[i++] = stat_config.user_requested_cpu_list;
+       }
+
+       if (stat_config.system_wide)
+               record_argv[i++] = "-a";
+
+       if (!stat_config.system_wide

&& !stat_config.user_requested_cpu_list)

Quoted 21 lines
Quoted 20 lines

{

Quoted 4 lines
(quoted diff) +4 -0
+               pr_err("Require -a or -C option to run sampling.\n");
+               return -ECANCELED;
+       }
+

Review

Quoted 10 lines
(quoted diff) +8 -0
+       list_for_each_entry(e, &stat_config.tpebs_events, nd) {
+               record_argv[i++] = "-e";
+               record_argv[i++] = e->name;
+       }
+
+       record_argv[i++] = "-o";
+       record_argv[i++] = PERF_DATA;
+
        return 0;
 }

Still I think it's weird it has 'perf record' in perf stat (despite the 'perf stat record'). If it's only Intel thing and we don't have a plan to do the same on other arches, we can move it to the arch directory and keep the perf stat code simple.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

I'm not sure what is the proper way to solve this. And Ian mentioned that put code in arch directory could potentially cause other bugs. So I'm wondering if we could keep this code here for now. I could

work

Quoted 13 lines
Quoted 13 lines
Quoted 9 lines
Quoted 7 lines
Quoted 1 line

on it later if we found it's better to be in arch directory.

Maybe somewhere in the util/ and keep the main code minimal. IIUC it's only for very recent (or upcoming?) Intel CPUs and we don't have tests (hopefully can run on other arch/CPUs).

So I don't think having it here would help fixing potential bugs.

Do you mean by creating a new file in util/ to hold this code?

Yeah, maybe util/intel-tpebs.c (if it's better than arch/x86/...) ?

Quoted 1 line
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Yes, this feature is for very recent Intel CPUs. It should only be triggered if a metric uses event(s) that has the R modifier in the formula.

Can we have a test with a fake metric so that we can test the code on non-(or old-)Intel machines?

All the existing metrics in non-(or old-)Intel CPUs should work as usual. So I

think

Quoted 3 lines

existing metric tests should work for it.

If we want to add a fake metric uses the :R modifier for those platforms, the

metric

Quoted 1 line

should either fail (if the fake metric uses an event not exist on the test

platform) or

Quoted 2 lines

return all 0 retire latency data.

Quoted 8 lines
Quoted 1 line

So, I'm not quite sure what we want the fake metric to test for. Maybe we

could

Quoted 1 line

continue using existing metric tests to ensure all the defined metrics work

correctly

Quoted 1 line

in each machine under test?

I think it's ok to return all 0 retire latency for fake tPEBS metrics. It's just to verify the background record + report logic works ok.

Hi Namhyung,

After think more about how TPEBS and metrics work, I feel should discuss more about defining a fake TPEBS metric in unsupported platforms.
If we'd like a add fake metrics, where should we define and store these metrics? Should we add this type of metrics for every platform? All the official metrics we publish are defined by architect and stored in JSON files under separate directories for each platform. I think it is not a good idea to place fake metrics together with real metrics. Could you please let me know if there is any other method to define fake metrics for testing?

Thanks,
Weilin

Quoted 3 lines

Thanks,
Namhyung

reviewer
#15 lore raw

Re: [RFC PATCH v6 2/5] perf stat: Fork and launch perf record when perf stat needs to get retire latency value for a metric.

Quoted 131 lines · Weilin
Quoted 120 lines

-----Original Message-----
From: Namhyung Kim <[email protected]>
Sent: Wednesday, April 24, 2024 11:50 AM
To: Wang, Weilin <[email protected]>
Cc: Ian Rogers <[email protected]>; Arnaldo Carvalho de Melo <[email protected]>; Peter Zijlstra <[email protected]>; Ingo Molnar <[email protected]>; Alexander Shishkin <[email protected]>; Jiri Olsa <[email protected]>; Hunter, Adrian <[email protected]>; Kan Liang <[email protected]>; [email protected]; [email protected]; Taylor, Perry <[email protected]>; Alt, Samantha <[email protected]>; Biggers, Caleb <[email protected]>
Subject: Re: [RFC PATCH v6 2/5] perf stat: Fork and launch perf record when perf stat needs to get retire latency value for a metric.

Quoted 13 lines · Weilin
Quoted 13 lines

-----Original Message-----
From: Namhyung Kim <[email protected]>
Sent: Tuesday, April 23, 2024 4:06 PM
To: Wang, Weilin <[email protected]>
Cc: Ian Rogers <[email protected]>; Arnaldo Carvalho de Melo <[email protected]>; Peter Zijlstra <[email protected]>; Ingo Molnar <[email protected]>; Alexander Shishkin <[email protected]>; Jiri Olsa <[email protected]>; Hunter, Adrian <[email protected]>; Kan Liang <[email protected]>; [email protected]; [email protected]; Taylor,

Perry

Quoted 1 line
Quoted 1 line

<[email protected]>; Alt, Samantha <[email protected]>;

Biggers,

Quoted 2 lines
Quoted 2 lines

Caleb <[email protected]>
Subject: Re: [RFC PATCH v6 2/5] perf stat: Fork and launch perf record

when

Quoted 48 lines
Quoted 48 lines

perf stat needs to get retire latency value for a metric.

Quoted 44 lines · Weilin
Quoted 44 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
(quoted diff) +11 -1
-static int __run_perf_record(void)
+static int __run_perf_record(const char **record_argv)
 {
+       int i = 0;
+       struct tpebs_event *e;
+
        pr_debug("Prepare perf record for retire_latency\n");
+
+       record_argv[i++] = "perf";
+       record_argv[i++] = "record";
+       record_argv[i++] = "-W";
+       record_argv[i++] = "--synth=no";
+
+       if (stat_config.user_requested_cpu_list) {
Quoted 8 lines
Quoted 8 lines

Review

Quoted 8 lines
(quoted diff) +8 -0
+               record_argv[i++] = "-C";
+               record_argv[i++] = stat_config.user_requested_cpu_list;
+       }
+
+       if (stat_config.system_wide)
+               record_argv[i++] = "-a";
+
+       if (!stat_config.system_wide

&& !stat_config.user_requested_cpu_list)

Quoted 21 lines
Quoted 20 lines

{

Quoted 4 lines
(quoted diff) +4 -0
+               pr_err("Require -a or -C option to run sampling.\n");
+               return -ECANCELED;
+       }
+

Review

Quoted 10 lines
(quoted diff) +8 -0
+       list_for_each_entry(e, &stat_config.tpebs_events, nd) {
+               record_argv[i++] = "-e";
+               record_argv[i++] = e->name;
+       }
+
+       record_argv[i++] = "-o";
+       record_argv[i++] = PERF_DATA;
+
        return 0;
 }

Still I think it's weird it has 'perf record' in perf stat (despite the 'perf stat record'). If it's only Intel thing and we don't have a plan to do the same on other arches, we can move it to the arch directory and keep the perf stat code simple.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

I'm not sure what is the proper way to solve this. And Ian mentioned that put code in arch directory could potentially cause other bugs. So I'm wondering if we could keep this code here for now. I could

work

Quoted 13 lines
Quoted 13 lines
Quoted 9 lines
Quoted 7 lines
Quoted 1 line

on it later if we found it's better to be in arch directory.

Maybe somewhere in the util/ and keep the main code minimal. IIUC it's only for very recent (or upcoming?) Intel CPUs and we don't have tests (hopefully can run on other arch/CPUs).

So I don't think having it here would help fixing potential bugs.

Do you mean by creating a new file in util/ to hold this code?

Yeah, maybe util/intel-tpebs.c (if it's better than arch/x86/...) ?

Quoted 1 line
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Yes, this feature is for very recent Intel CPUs. It should only be triggered if a metric uses event(s) that has the R modifier in the formula.

Can we have a test with a fake metric so that we can test the code on non-(or old-)Intel machines?

All the existing metrics in non-(or old-)Intel CPUs should work as usual. So I

think

Quoted 3 lines

existing metric tests should work for it.

If we want to add a fake metric uses the :R modifier for those platforms, the

metric

Quoted 1 line

should either fail (if the fake metric uses an event not exist on the test

platform) or

Quoted 2 lines

return all 0 retire latency data.

Quoted 8 lines
Quoted 1 line

So, I'm not quite sure what we want the fake metric to test for. Maybe we

could

Quoted 1 line

continue using existing metric tests to ensure all the defined metrics work

correctly

Quoted 1 line

in each machine under test?

I think it's ok to return all 0 retire latency for fake tPEBS metrics. It's just to verify the background record + report logic works ok.

Hi Namhyung,

Quoted 8 lines

After think more about how TPEBS and metrics work, I feel should discuss more about defining a fake TPEBS metric in unsupported platforms.
If we'd like a add fake metrics, where should we define and store these metrics? Should we add this type of metrics for every platform? All the official metrics we publish are defined by architect and stored in JSON files under separate directories for each platform. I think it is not a good idea to place fake metrics together with real metrics. Could you please let me know if there is any other method to define fake metrics for testing?

We do put fake events/metrics in the "test" arch:
https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/pmu-events/arch/test/test_soc/cpu/metrics.json

It is something of a pain bringing things here over to the NO_JEVENTS=1 (ie no python) empty-pmu-events.c file. I agree on not wanting to pollute real metrics with test metrics, we currently just use the test metrics to fake up some expression parsing tests:
https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/tests/pmu-events.c#n811

Thanks,
Ian

Quoted 6 lines

Thanks,
Weilin

Quoted 3 lines

Thanks,
Namhyung

#16 lore raw

RE: [RFC PATCH v6 2/5] perf stat: Fork and launch perf record when perf stat needs to get retire latency value for a metric.

Quoted 178 lines

-----Original Message-----
From: Ian Rogers <[email protected]>
Sent: Tuesday, May 14, 2024 11:07 PM
To: Wang, Weilin <[email protected]>
Cc: Namhyung Kim <[email protected]>; Arnaldo Carvalho de Melo <[email protected]>; Peter Zijlstra <[email protected]>; Ingo Molnar <[email protected]>; Alexander Shishkin <[email protected]>; Jiri Olsa <[email protected]>; Hunter, Adrian <[email protected]>; Kan Liang <[email protected]>; [email protected]; [email protected]; Taylor, Perry <[email protected]>; Alt, Samantha <[email protected]>; Biggers, Caleb <[email protected]>
Subject: Re: [RFC PATCH v6 2/5] perf stat: Fork and launch perf record when perf stat needs to get retire latency value for a metric.

Quoted 13 lines · Weilin
Quoted 13 lines

-----Original Message-----
From: Namhyung Kim <[email protected]>
Sent: Wednesday, April 24, 2024 11:50 AM
To: Wang, Weilin <[email protected]>
Cc: Ian Rogers <[email protected]>; Arnaldo Carvalho de Melo <[email protected]>; Peter Zijlstra <[email protected]>; Ingo Molnar <[email protected]>; Alexander Shishkin <[email protected]>; Jiri Olsa <[email protected]>; Hunter, Adrian <[email protected]>; Kan Liang <[email protected]>; [email protected]; [email protected]; Taylor,

Perry

Quoted 1 line
Quoted 1 line

<[email protected]>; Alt, Samantha <[email protected]>;

Biggers,

Quoted 2 lines
Quoted 2 lines

Caleb <[email protected]>
Subject: Re: [RFC PATCH v6 2/5] perf stat: Fork and launch perf record

when

Quoted 3 lines
Quoted 3 lines

perf stat needs to get retire latency value for a metric.

On Wed, Apr 24, 2024 at 10:08 AM Wang, Weilin

<[email protected]>

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines · wrote:
Quoted 9 lines

-----Original Message-----
From: Namhyung Kim <[email protected]>
Sent: Tuesday, April 23, 2024 4:06 PM
To: Wang, Weilin <[email protected]>
Cc: Ian Rogers <[email protected]>; Arnaldo Carvalho de Melo <[email protected]>; Peter Zijlstra <[email protected]>; Ingo

Molnar

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

<[email protected]>; Alexander Shishkin <[email protected]>; Jiri Olsa <[email protected]>;

Hunter,

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

Adrian <[email protected]>; Kan Liang

<[email protected]>;

Quoted 10 lines
Quoted 10 lines
Quoted 1 line
Quoted 1 line

[email protected]; [email protected]; Taylor,

Perry

Quoted 1 line
Quoted 1 line

<[email protected]>; Alt, Samantha <[email protected]>;

Biggers,

Quoted 2 lines
Quoted 2 lines

Caleb <[email protected]>
Subject: Re: [RFC PATCH v6 2/5] perf stat: Fork and launch perf record

when

Quoted 3 lines
Quoted 3 lines

perf stat needs to get retire latency value for a metric.

On Tue, Apr 23, 2024 at 3:16 PM Wang, Weilin

<[email protected]>

Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 8 lines · wrote:
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
(quoted diff) +5 -1
-static int __run_perf_record(void)
+static int __run_perf_record(const char **record_argv)
 {
+       int i = 0;
+       struct tpebs_event *e;
+
        pr_debug("Prepare perf record for retire_latency\n");
+
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Review

Quoted 8 lines
(quoted diff) +8 -0
+       record_argv[i++] = "perf";
+       record_argv[i++] = "record";
+       record_argv[i++] = "-W";
+       record_argv[i++] = "--synth=no";
+
+       if (stat_config.user_requested_cpu_list) {
+               record_argv[i++] = "-C";
+               record_argv[i++] =

stat_config.user_requested_cpu_list;

Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Quoted 6 lines
Quoted 6 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+       }
+
+       if (stat_config.system_wide)
+               record_argv[i++] = "-a";
+
+       if (!stat_config.system_wide

&& !stat_config.user_requested_cpu_list)

Quoted 21 lines
Quoted 20 lines

{

Quoted 4 lines
(quoted diff) +4 -0
+               pr_err("Require -a or -C option to run sampling.\n");
+               return -ECANCELED;
+       }
+

Review

Quoted 10 lines
(quoted diff) +8 -0
+       list_for_each_entry(e, &stat_config.tpebs_events, nd) {
+               record_argv[i++] = "-e";
+               record_argv[i++] = e->name;
+       }
+
+       record_argv[i++] = "-o";
+       record_argv[i++] = PERF_DATA;
+
        return 0;
 }

Still I think it's weird it has 'perf record' in perf stat (despite the 'perf stat record'). If it's only Intel thing and we don't have a plan to do the same on other arches, we can move it to the arch directory and keep the perf stat code simple.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

I'm not sure what is the proper way to solve this. And Ian

mentioned

Quoted 9 lines
Quoted 9 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

that put code in arch directory could potentially cause other bugs. So I'm wondering if we could keep this code here for now. I could

work

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 1 line

on it later if we found it's better to be in arch directory.

Maybe somewhere in the util/ and keep the main code minimal. IIUC it's only for very recent (or upcoming?) Intel CPUs and we don't have tests (hopefully can run on other arch/CPUs).

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines
Quoted 1 line

So I don't think having it here would help fixing potential bugs.

Do you mean by creating a new file in util/ to hold this code?

Yeah, maybe util/intel-tpebs.c (if it's better than arch/x86/...) ?

Quoted 2 lines

Yes, this feature is for very recent Intel CPUs. It should only be

triggered if

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

a metric uses event(s) that has the R modifier in the formula.

Can we have a test with a fake metric so that we can test the code on non-(or old-)Intel machines?

All the existing metrics in non-(or old-)Intel CPUs should work as usual.

So I

Quoted 4 lines
Quoted 4 lines

think

Quoted 3 lines

existing metric tests should work for it.

If we want to add a fake metric uses the :R modifier for those platforms,

the

Quoted 9 lines
Quoted 9 lines

metric

Quoted 1 line

should either fail (if the fake metric uses an event not exist on the test

platform) or

Quoted 3 lines

return all 0 retire latency data.

So, I'm not quite sure what we want the fake metric to test for. Maybe we

could

Quoted 1 line

continue using existing metric tests to ensure all the defined metrics work

correctly

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

in each machine under test?

I think it's ok to return all 0 retire latency for fake tPEBS metrics. It's just to verify the background record + report logic works ok.

Hi Namhyung,

After think more about how TPEBS and metrics work, I feel should discuss

more

Quoted 2 lines

about defining a fake TPEBS metric in unsupported platforms. If we'd like a add fake metrics, where should we define and store these

metrics?

Quoted 5 lines

Should we add this type of metrics for every platform? All the official metrics we publish are defined by architect and stored in JSON files under separate directories for each platform. I think it is not a good idea to place fake metrics together with real metrics. Could you please let me know if there is any other method to define fake metrics for testing?

We do put fake events/metrics in the "test" arch:
https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-
next.git/tree/tools/perf/pmu-events/arch/test/test_soc/cpu/metrics.json

Quoted 7 lines

It is something of a pain bringing things here over to the NO_JEVENTS=1 (ie no python) empty-pmu-events.c file. I agree on not wanting to pollute real metrics with test metrics, we currently just use the test metrics to fake up some expression parsing tests:
https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-
next.git/tree/tools/perf/tests/pmu-events.c#n811

Thanks Ian! I will work on this.

Best,
Weilin

Quoted 9 lines

Thanks,
Ian

Quoted 6 lines

Thanks,
Weilin

Quoted 3 lines

Thanks,
Namhyung

#17 lore raw

RFC PATCH v6 3/5 perf stat: Add retire latency values into the expr_parse_ctx to prepare for final metric calculation

In reply to [email protected]

From: Weilin Wang <[email protected]>

Retire latency values of events are used in metric formulas. This update adds code to process data from perf record for required retire latency values.

---
 tools/perf/builtin-stat.c     |  1 +
 tools/perf/util/metricgroup.h |  4 ++++
 tools/perf/util/stat-shadow.c | 19 +++++++++++++++++++
 3 files changed, 24 insertions(+)
tools/perf/builtin-stat.c +1 -0
index 7fbe47b0c44c..4954ff84c407 100644
@@ -798,6 +798,7 @@ static int process_sample_event(struct perf_tool *tool __maybe_unused,
 		if (!strcmp(evname, t->event.name)) {
 			t->count += 1;
 			t->sum += sample->retire_lat;
+			t->val = (double) t->sum / t->count;
 			break;
 		}
 	}
tools/perf/util/metricgroup.h +4 -0
index ae788edef30f..d63209b29bf1 100644
@@ -76,8 +76,12 @@ struct tpebs_event {
 
 struct tpebs_retire_lat {
 	struct tpebs_event event;
+	/* Count of retire_latency values found in sample data */
 	size_t count;
+	/* Sum of all the retire_latency values in sample data */
 	int sum;
+	/* Average of retire_latency, val = sum / count */
+	double val;
 };
 
 struct metric_event *metricgroup__lookup(struct rblist *metric_events,
tools/perf/util/stat-shadow.c +19 -0
index 3466aa952442..bc77e9e02892 100644
@@ -355,6 +355,20 @@ static void print_nsecs(struct perf_stat_config *config,
 		print_metric(config, ctxp, NULL, NULL, "CPUs utilized", 0);
 }
 
+static int prepare_retire_lat(struct expr_parse_ctx *pctx,
+			     struct list_head *retire_lats)
+{
+	int ret = 0;
+	struct tpebs_retire_lat *t;
+
+	list_for_each_entry(t, retire_lats, event.nd) {
+		ret = expr__add_id_val(pctx, strdup(t->event.tpebs_name), t->val);
+		if (ret < 0)
+			return ret;
+	}
+	return ret;
+}
+
 static int prepare_metric(const struct metric_expr *mexp,
 			  const struct evsel *evsel,
 			  struct expr_parse_ctx *pctx,
@@ -486,6 +500,11 @@ static void generic_metric(struct perf_stat_config *config,
 		pctx->sctx.user_requested_cpu_list = strdup(config->user_requested_cpu_list);
 	pctx->sctx.runtime = runtime;
 	pctx->sctx.system_wide = config->system_wide;
+	i = prepare_retire_lat(pctx, &config->tpebs_results);
+	if (i < 0) {
+		expr__ctx_free(pctx);
+		return;
+	}
 	i = prepare_metric(mexp, evsel, pctx, aggr_idx);
 	if (i < 0) {
 		expr__ctx_free(pctx);
Signature · 1 line
2.43.0
#18 lore raw

RFC PATCH v6 4/5 perf stat: Add retire latency print functions to print out at the very end of print out

In reply to [email protected]

From: Weilin Wang <[email protected]>

Add print out functions so that users could read retire latency values.

Example output:
In this simple example, there is no MEM_INST_RETIRED.STLB_HIT_STORES sample. Therefore, the MEM_INST_RETIRED.STLB_HIT_STORES:p retire_latency value, count and sum are all 0.

Performance counter stats for 'system wide':

       181,047,168      cpu_core/TOPDOWN.SLOTS/          #      0.6 %  tma_dtlb_store
         3,195,608      cpu_core/topdown-retiring/
        40,156,649      cpu_core/topdown-mem-bound/
         3,550,925      cpu_core/topdown-bad-spec/
       117,571,818      cpu_core/topdown-fe-bound/
        57,118,087      cpu_core/topdown-be-bound/
            69,179      cpu_core/EXE_ACTIVITY.BOUND_ON_STORES/
             4,582      cpu_core/MEM_INST_RETIRED.STLB_HIT_STORES/
        30,183,104      cpu_core/CPU_CLK_UNHALTED.DISTRIBUTED/
        30,556,790      cpu_core/CPU_CLK_UNHALTED.THREAD/
           168,486      cpu_core/DTLB_STORE_MISSES.WALK_ACTIVE/

0.00 MEM_INST_RETIRED.STLB_HIT_STORES:p 0 0

1.003105924 seconds time elapsed

---
 tools/perf/util/stat-display.c | 65 ++++++++++++++++++++++++++++++++++
 1 file changed, 65 insertions(+)
tools/perf/util/stat-display.c +65 -0
index bfc1d705f437..6c043d9c9f81 100644
@@ -21,6 +21,7 @@
 #include "iostat.h"
 #include "pmu.h"
 #include "pmus.h"
+#include "metricgroup.h"
 
 #define CNTR_NOT_SUPPORTED	"<not supported>"
 #define CNTR_NOT_COUNTED	"<not counted>"
@@ -34,6 +35,7 @@
 #define COMM_LEN     16
 #define PID_LEN       7
 #define CPUS_LEN      4
+#define RETIRE_LEN    8
 
 static int aggr_header_lens[] = {
 	[AGGR_CORE] 	= 18,
@@ -426,6 +428,67 @@ static void print_metric_std(struct perf_stat_config *config,
 	fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
 }
 
+static void print_retire_lat_std(struct perf_stat_config *config,
+				 struct outstate *os)
+{
+	FILE *out = os->fh;
+	bool newline = os->newline;
+	struct tpebs_retire_lat *t;
+	struct list_head *retire_lats = &config->tpebs_results;
+
+	list_for_each_entry(t, retire_lats, event.nd) {
+		if (newline)
+			do_new_line_std(config, os);
+		fprintf(out, "%'*.2f %-*s", COUNTS_LEN, t->val, EVNAME_LEN, t->event.name);
+		fprintf(out, "%*ld %*d\n", RETIRE_LEN, t->count,
+			 RETIRE_LEN, t->sum);
+	}
+}
+
+static void print_retire_lat_csv(struct perf_stat_config *config,
+				 struct outstate *os)
+{
+	FILE *out = os->fh;
+	struct tpebs_retire_lat *t;
+	struct list_head *retire_lats = &config->tpebs_results;
+	const char *sep = config->csv_sep;
+
+	list_for_each_entry(t, retire_lats, event.nd) {
+		fprintf(out, "%f%s%s%s%s%ld%s%d\n", t->val, sep, sep, t->event.name, sep,
+			t->count, sep, t->sum);
+	}
+}
+
+static void print_retire_lat_json(struct perf_stat_config *config,
+				  struct outstate *os)
+{
+	FILE *out = os->fh;
+	struct tpebs_retire_lat *t;
+	struct list_head *retire_lats = &config->tpebs_results;
+
+	fprintf(out, "{");
+	list_for_each_entry(t, retire_lats, event.nd) {
+		fprintf(out, "\"retire_latency-value\" : \"%f\", ", t->val);
+		fprintf(out, "\"event-name\" : \"%s\"", t->event.name);
+		fprintf(out, "\"sample-counts\" : \"%ld\"", t->count);
+		fprintf(out, "\"retire_latency-sum\" : \"%d\"", t->sum);
+	}
+	fprintf(out, "}");
+}
+
+static void print_retire_lat(struct perf_stat_config *config,
+			     struct outstate *os)
+{
+	if (!&config->tpebs_results)
+		return;
+	if (config->json_output)
+		print_retire_lat_json(config, os);
+	else if (config->csv_output)
+		print_retire_lat_csv(config, os);
+	else
+		print_retire_lat_std(config, os);
+}
+
 static void new_line_csv(struct perf_stat_config *config, void *ctx)
 {
 	struct outstate *os = ctx;
@@ -1609,6 +1672,8 @@ void evlist__print_counters(struct evlist *evlist, struct perf_stat_config *conf
 		break;
 	}
 
+	print_retire_lat(config, &os);
+
 	print_footer(config);
 
 	fflush(config->output);
Signature · 1 line
2.43.0
maintainer
#19 lore raw

Re: [RFC PATCH v6 4/5] perf stat: Add retire latency print functions to print out at the very end of print out

Quoted 23 lines · On Fri, Mar 29, 2024 at 12:12 PM <[email protected]> wrote:

From: Weilin Wang <[email protected]>

Add print out functions so that users could read retire latency values.

Example output:
In this simple example, there is no MEM_INST_RETIRED.STLB_HIT_STORES sample. Therefore, the MEM_INST_RETIRED.STLB_HIT_STORES:p retire_latency value, count and sum are all 0.

Performance counter stats for 'system wide':

       181,047,168      cpu_core/TOPDOWN.SLOTS/          #      0.6 %  tma_dtlb_store
         3,195,608      cpu_core/topdown-retiring/
        40,156,649      cpu_core/topdown-mem-bound/
         3,550,925      cpu_core/topdown-bad-spec/
       117,571,818      cpu_core/topdown-fe-bound/
        57,118,087      cpu_core/topdown-be-bound/
            69,179      cpu_core/EXE_ACTIVITY.BOUND_ON_STORES/
             4,582      cpu_core/MEM_INST_RETIRED.STLB_HIT_STORES/
        30,183,104      cpu_core/CPU_CLK_UNHALTED.DISTRIBUTED/
        30,556,790      cpu_core/CPU_CLK_UNHALTED.THREAD/
           168,486      cpu_core/DTLB_STORE_MISSES.WALK_ACTIVE/
Quoted 1 line

0.00 MEM_INST_RETIRED.STLB_HIT_STORES:p 0 0

The output is not aligned and I think it's hard to read. I think it should print the result like this:

<sum> <event-name> # <val> average retired latency

Thanks,
Namhyung

Quoted 109 lines
       1.003105924 seconds time elapsed
---
 tools/perf/util/stat-display.c | 65 ++++++++++++++++++++++++++++++++++
 1 file changed, 65 insertions(+)
tools/perf/util/stat-display.c +65 -1
index bfc1d705f437..6c043d9c9f81 100644
@@ -21,6 +21,7 @@
 #include "iostat.h"
 #include "pmu.h"
 #include "pmus.h"
+#include "metricgroup.h"
 
 #define CNTR_NOT_SUPPORTED     "<not supported>"
 #define CNTR_NOT_COUNTED       "<not counted>"
@@ -34,6 +35,7 @@
 #define COMM_LEN     16
 #define PID_LEN       7
 #define CPUS_LEN      4
+#define RETIRE_LEN    8
 
 static int aggr_header_lens[] = {
        [AGGR_CORE]     = 18,
@@ -426,6 +428,67 @@ static void print_metric_std(struct perf_stat_config *config,
        fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
 }
 
+static void print_retire_lat_std(struct perf_stat_config *config,
+                                struct outstate *os)
+{
+       FILE *out = os->fh;
+       bool newline = os->newline;
+       struct tpebs_retire_lat *t;
+       struct list_head *retire_lats = &config->tpebs_results;
+
+       list_for_each_entry(t, retire_lats, event.nd) {
+               if (newline)
+                       do_new_line_std(config, os);
+               fprintf(out, "%'*.2f %-*s", COUNTS_LEN, t->val, EVNAME_LEN, t->event.name);
+               fprintf(out, "%*ld %*d\n", RETIRE_LEN, t->count,
+                        RETIRE_LEN, t->sum);
+       }
+}
+
+static void print_retire_lat_csv(struct perf_stat_config *config,
+                                struct outstate *os)
+{
+       FILE *out = os->fh;
+       struct tpebs_retire_lat *t;
+       struct list_head *retire_lats = &config->tpebs_results;
+       const char *sep = config->csv_sep;
+
+       list_for_each_entry(t, retire_lats, event.nd) {
+               fprintf(out, "%f%s%s%s%s%ld%s%d\n", t->val, sep, sep, t->event.name, sep,
+                       t->count, sep, t->sum);
+       }
+}
+
+static void print_retire_lat_json(struct perf_stat_config *config,
+                                 struct outstate *os)
+{
+       FILE *out = os->fh;
+       struct tpebs_retire_lat *t;
+       struct list_head *retire_lats = &config->tpebs_results;
+
+       fprintf(out, "{");
+       list_for_each_entry(t, retire_lats, event.nd) {
+               fprintf(out, "\"retire_latency-value\" : \"%f\", ", t->val);
+               fprintf(out, "\"event-name\" : \"%s\"", t->event.name);
+               fprintf(out, "\"sample-counts\" : \"%ld\"", t->count);
+               fprintf(out, "\"retire_latency-sum\" : \"%d\"", t->sum);
+       }
+       fprintf(out, "}");
+}
+
+static void print_retire_lat(struct perf_stat_config *config,
+                            struct outstate *os)
+{
+       if (!&config->tpebs_results)
+               return;
+       if (config->json_output)
+               print_retire_lat_json(config, os);
+       else if (config->csv_output)
+               print_retire_lat_csv(config, os);
+       else
+               print_retire_lat_std(config, os);
+}
+
 static void new_line_csv(struct perf_stat_config *config, void *ctx)
 {
        struct outstate *os = ctx;
@@ -1609,6 +1672,8 @@ void evlist__print_counters(struct evlist *evlist, struct perf_stat_config *conf
                break;
        }
 
+       print_retire_lat(config, &os);
+
        print_footer(config);
 
        fflush(config->output);
--

2.43.0

#20 lore raw

RE: [RFC PATCH v6 4/5] perf stat: Add retire latency print functions to print out at the very end of print out

Quoted 46 lines

-----Original Message-----
From: Namhyung Kim <[email protected]>
Sent: Monday, April 1, 2024 2:04 PM
To: Wang, Weilin <[email protected]>
Cc: Ian Rogers <[email protected]>; Arnaldo Carvalho de Melo <[email protected]>; Peter Zijlstra <[email protected]>; Ingo Molnar <[email protected]>; Alexander Shishkin <[email protected]>; Jiri Olsa <[email protected]>; Hunter, Adrian <[email protected]>; Kan Liang <[email protected]>; [email protected]; [email protected]; Taylor, Perry <[email protected]>; Alt, Samantha <[email protected]>; Biggers, Caleb <[email protected]>
Subject: Re: [RFC PATCH v6 4/5] perf stat: Add retire latency print functions to print out at the very end of print out

Quoted 7 lines · On Fri, Mar 29, 2024 at 12:12 PM <[email protected]> wrote:

From: Weilin Wang <[email protected]>

Add print out functions so that users could read retire latency values.

Example output:
In this simple example, there is no MEM_INST_RETIRED.STLB_HIT_STORES

sample.

Quoted 1 line

Therefore, the MEM_INST_RETIRED.STLB_HIT_STORES:p retire_latency

value, count

Quoted 5 lines

and sum are all 0.

Performance counter stats for 'system wide':

       181,047,168      cpu_core/TOPDOWN.SLOTS/          #      0.6 %

tma_dtlb_store

Quoted 11 lines
         3,195,608      cpu_core/topdown-retiring/
        40,156,649      cpu_core/topdown-mem-bound/
         3,550,925      cpu_core/topdown-bad-spec/
       117,571,818      cpu_core/topdown-fe-bound/
        57,118,087      cpu_core/topdown-be-bound/
            69,179      cpu_core/EXE_ACTIVITY.BOUND_ON_STORES/
             4,582      cpu_core/MEM_INST_RETIRED.STLB_HIT_STORES/
        30,183,104      cpu_core/CPU_CLK_UNHALTED.DISTRIBUTED/
        30,556,790      cpu_core/CPU_CLK_UNHALTED.THREAD/
           168,486      cpu_core/DTLB_STORE_MISSES.WALK_ACTIVE/

0.00 MEM_INST_RETIRED.STLB_HIT_STORES:p 0 0

Quoted 4 lines

The output is not aligned and I think it's hard to read. I think it should print the result like this:

<sum> <event-name> # <val> average retired latency

Since we would like to use the average retire latency, I would think put average at the beginning would be more consistent. So in format like:
<val> <event-name> <sum> <count> or <val> <event-name> <count> <sum> ?

I will work on the alignment.

Thanks,
Weilin

Quoted 119 lines

Thanks,
Namhyung

Quoted 7 lines
       1.003105924 seconds time elapsed
---
 tools/perf/util/stat-display.c | 65

++++++++++++++++++++++++++++++++++

tools/perf/util/stat-display.c static void print_metric_std(struct

Quoted 23 lines
 1 file changed, 65 insertions(+)
tools/perf/util/stat-display.c +2 -0
index bfc1d705f437..6c043d9c9f81 100644
@@ -21,6 +21,7 @@
 #include "iostat.h"
 #include "pmu.h"
 #include "pmus.h"
+#include "metricgroup.h"
 
 #define CNTR_NOT_SUPPORTED     "<not supported>"
 #define CNTR_NOT_COUNTED       "<not counted>"
@@ -34,6 +35,7 @@
 #define COMM_LEN     16
 #define PID_LEN       7
 #define CPUS_LEN      4
+#define RETIRE_LEN    8
 
 static int aggr_header_lens[] = {
        [AGGR_CORE]     = 18,
@@ -426,6 +428,67 @@ static void print_metric_std(struct

perf_stat_config *config,

tools/perf/util/stat-display.c void evlist__print_counters(struct evlist *evlist,

Quoted 70 lines
tools/perf/util/stat-display.c +61 -0
        fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
 }
 
+static void print_retire_lat_std(struct perf_stat_config *config,
+                                struct outstate *os)
+{
+       FILE *out = os->fh;
+       bool newline = os->newline;
+       struct tpebs_retire_lat *t;
+       struct list_head *retire_lats = &config->tpebs_results;
+
+       list_for_each_entry(t, retire_lats, event.nd) {
+               if (newline)
+                       do_new_line_std(config, os);
+               fprintf(out, "%'*.2f %-*s", COUNTS_LEN, t->val, EVNAME_LEN, t-
 event.name);
+               fprintf(out, "%*ld %*d\n", RETIRE_LEN, t->count,
+                        RETIRE_LEN, t->sum);
+       }
+}
+
+static void print_retire_lat_csv(struct perf_stat_config *config,
+                                struct outstate *os)
+{
+       FILE *out = os->fh;
+       struct tpebs_retire_lat *t;
+       struct list_head *retire_lats = &config->tpebs_results;
+       const char *sep = config->csv_sep;
+
+       list_for_each_entry(t, retire_lats, event.nd) {
+               fprintf(out, "%f%s%s%s%s%ld%s%d\n", t->val, sep, sep, t-
 event.name, sep,
+                       t->count, sep, t->sum);
+       }
+}
+
+static void print_retire_lat_json(struct perf_stat_config *config,
+                                 struct outstate *os)
+{
+       FILE *out = os->fh;
+       struct tpebs_retire_lat *t;
+       struct list_head *retire_lats = &config->tpebs_results;
+
+       fprintf(out, "{");
+       list_for_each_entry(t, retire_lats, event.nd) {
+               fprintf(out, "\"retire_latency-value\" : \"%f\", ", t->val);
+               fprintf(out, "\"event-name\" : \"%s\"", t->event.name);
+               fprintf(out, "\"sample-counts\" : \"%ld\"", t->count);
+               fprintf(out, "\"retire_latency-sum\" : \"%d\"", t->sum);
+       }
+       fprintf(out, "}");
+}
+
+static void print_retire_lat(struct perf_stat_config *config,
+                            struct outstate *os)
+{
+       if (!&config->tpebs_results)
+               return;
+       if (config->json_output)
+               print_retire_lat_json(config, os);
+       else if (config->csv_output)
+               print_retire_lat_csv(config, os);
+       else
+               print_retire_lat_std(config, os);
+}
+
 static void new_line_csv(struct perf_stat_config *config, void *ctx)
 {
        struct outstate *os = ctx;
tools/perf/util/stat-display.c +0 -0
@@ -1609,6 +1672,8 @@ void evlist__print_counters(struct evlist *evlist,

struct perf_stat_config *conf

Quoted 11 lines
tools/perf/util/stat-display.c +2 -1
                break;
        }
 
+       print_retire_lat(config, &os);
+
        print_footer(config);
 
        fflush(config->output);
--

2.43.0

maintainer
#21 lore raw

Re: [RFC PATCH v6 4/5] perf stat: Add retire latency print functions to print out at the very end of print out

Quoted 47 lines · Weilin
Quoted 47 lines

-----Original Message-----
From: Namhyung Kim <[email protected]>
Sent: Monday, April 1, 2024 2:04 PM
To: Wang, Weilin <[email protected]>
Cc: Ian Rogers <[email protected]>; Arnaldo Carvalho de Melo <[email protected]>; Peter Zijlstra <[email protected]>; Ingo Molnar <[email protected]>; Alexander Shishkin <[email protected]>; Jiri Olsa <[email protected]>; Hunter, Adrian <[email protected]>; Kan Liang <[email protected]>; [email protected]; [email protected]; Taylor, Perry <[email protected]>; Alt, Samantha <[email protected]>; Biggers, Caleb <[email protected]>
Subject: Re: [RFC PATCH v6 4/5] perf stat: Add retire latency print functions to print out at the very end of print out

Quoted 7 lines · On Fri, Mar 29, 2024 at 12:12 PM <[email protected]> wrote:

From: Weilin Wang <[email protected]>

Add print out functions so that users could read retire latency values.

Example output:
In this simple example, there is no MEM_INST_RETIRED.STLB_HIT_STORES

sample.

Quoted 1 line

Therefore, the MEM_INST_RETIRED.STLB_HIT_STORES:p retire_latency

value, count

Quoted 5 lines

and sum are all 0.

Performance counter stats for 'system wide':

       181,047,168      cpu_core/TOPDOWN.SLOTS/          #      0.6 %

tma_dtlb_store

Quoted 11 lines
         3,195,608      cpu_core/topdown-retiring/
        40,156,649      cpu_core/topdown-mem-bound/
         3,550,925      cpu_core/topdown-bad-spec/
       117,571,818      cpu_core/topdown-fe-bound/
        57,118,087      cpu_core/topdown-be-bound/
            69,179      cpu_core/EXE_ACTIVITY.BOUND_ON_STORES/
             4,582      cpu_core/MEM_INST_RETIRED.STLB_HIT_STORES/
        30,183,104      cpu_core/CPU_CLK_UNHALTED.DISTRIBUTED/
        30,556,790      cpu_core/CPU_CLK_UNHALTED.THREAD/
           168,486      cpu_core/DTLB_STORE_MISSES.WALK_ACTIVE/

0.00 MEM_INST_RETIRED.STLB_HIT_STORES:p 0 0

Quoted 8 lines
Quoted 4 lines

The output is not aligned and I think it's hard to read. I think it should print the result like this:

<sum> <event-name> # <val> average retired latency

Since we would like to use the average retire latency, I would think put average at the beginning would be more consistent. So in format like:
<val> <event-name> <sum> <count> or <val> <event-name> <count> <sum> ?

But it's not consistent with others. When I see the perf stat output, I'd expect it shows the total count. And the average latency is a derived value so I think it can be treated as a metric.

Thanks,
Namhyung

#22 lore raw

RE: [RFC PATCH v6 4/5] perf stat: Add retire latency print functions to print out at the very end of print out

Quoted 76 lines

-----Original Message-----
From: Namhyung Kim <[email protected]>
Sent: Monday, April 1, 2024 2:15 PM
To: Wang, Weilin <[email protected]>
Cc: Ian Rogers <[email protected]>; Arnaldo Carvalho de Melo <[email protected]>; Peter Zijlstra <[email protected]>; Ingo Molnar <[email protected]>; Alexander Shishkin <[email protected]>; Jiri Olsa <[email protected]>; Hunter, Adrian <[email protected]>; Kan Liang <[email protected]>; [email protected]; [email protected]; Taylor, Perry <[email protected]>; Alt, Samantha <[email protected]>; Biggers, Caleb <[email protected]>
Subject: Re: [RFC PATCH v6 4/5] perf stat: Add retire latency print functions to print out at the very end of print out

Quoted 13 lines · Weilin
Quoted 13 lines

-----Original Message-----
From: Namhyung Kim <[email protected]>
Sent: Monday, April 1, 2024 2:04 PM
To: Wang, Weilin <[email protected]>
Cc: Ian Rogers <[email protected]>; Arnaldo Carvalho de Melo <[email protected]>; Peter Zijlstra <[email protected]>; Ingo Molnar <[email protected]>; Alexander Shishkin <[email protected]>; Jiri Olsa <[email protected]>; Hunter, Adrian <[email protected]>; Kan Liang <[email protected]>; [email protected]; [email protected]; Taylor,

Perry

Quoted 1 line
Quoted 1 line

<[email protected]>; Alt, Samantha <[email protected]>;

Biggers,

Quoted 2 lines
Quoted 2 lines

Caleb <[email protected]>
Subject: Re: [RFC PATCH v6 4/5] perf stat: Add retire latency print

functions to

Quoted 10 lines
Quoted 10 lines

print out at the very end of print out

Quoted 7 lines · On Fri, Mar 29, 2024 at 12:12 PM <[email protected]> wrote:

From: Weilin Wang <[email protected]>

Add print out functions so that users could read retire latency values.

Example output:
In this simple example, there is no

MEM_INST_RETIRED.STLB_HIT_STORES

Quoted 21 lines
Quoted 21 lines

sample.

Quoted 1 line

Therefore, the MEM_INST_RETIRED.STLB_HIT_STORES:p retire_latency

value, count

Quoted 5 lines

and sum are all 0.

Performance counter stats for 'system wide':

       181,047,168      cpu_core/TOPDOWN.SLOTS/          #      0.6 %

tma_dtlb_store

Quoted 11 lines
         3,195,608      cpu_core/topdown-retiring/
        40,156,649      cpu_core/topdown-mem-bound/
         3,550,925      cpu_core/topdown-bad-spec/
       117,571,818      cpu_core/topdown-fe-bound/
        57,118,087      cpu_core/topdown-be-bound/
            69,179      cpu_core/EXE_ACTIVITY.BOUND_ON_STORES/
             4,582      cpu_core/MEM_INST_RETIRED.STLB_HIT_STORES/
        30,183,104      cpu_core/CPU_CLK_UNHALTED.DISTRIBUTED/
        30,556,790      cpu_core/CPU_CLK_UNHALTED.THREAD/
           168,486      cpu_core/DTLB_STORE_MISSES.WALK_ACTIVE/

0.00 MEM_INST_RETIRED.STLB_HIT_STORES:p 0 0

Quoted 6 lines
Quoted 4 lines

The output is not aligned and I think it's hard to read. I think it should print the result like this:

<sum> <event-name> # <val> average retired latency

Since we would like to use the average retire latency, I would think put

Quoted 8 lines

average

Quoted 2 lines

at the beginning would be more consistent. So in format like:
<val> <event-name> <sum> <count> or <val> <event-name> <count>

<sum> ?

But it's not consistent with others. When I see the perf stat output, I'd expect it shows the total count. And the average latency is a derived value so I think it can be treated as a metric.

I think whether it is consistent or not depends on how we read this data. If there is multiplexing happening, would the total count value of events be the scaled counts or raw counts? If these are scaled counts, then these are derived value as well. But we do expect the first column shows the value we care most from the row.

Thanks,
Weilin

Quoted 3 lines

Thanks,
Namhyung

#23 lore raw

RFC PATCH v6 5/5 perf vendor events intel: Add MTL metric json files

In reply to [email protected]

From: Weilin Wang <[email protected]>

Add MTL metric json file at TMA4.7 [1]. Some of the metrics' formulas use TPEBS retire_latency in MTL.

[1] https://lore.kernel.org/all/[email protected]/

---
 .../arch/x86/meteorlake/metricgroups.json     |  127 +
 .../arch/x86/meteorlake/mtl-metrics.json      | 2551 +++++++++++++++++
 2 files changed, 2678 insertions(+)
 create mode 100644 tools/perf/pmu-events/arch/x86/meteorlake/metricgroups.json
 create mode 100644 tools/perf/pmu-events/arch/x86/meteorlake/mtl-metrics.json
tools/perf/pmu-events/arch/x86/meteorlake/metricgroups.json new file +127 -0
new file mode 100644
index 000000000000..7a03835f262c
@@ -0,0 +1,127 @@
+{
+    "Backend": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "Bad": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "BadSpec": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "BigFootprint": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "BrMispredicts": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "Branches": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "C0Wait": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "CacheHits": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "CodeGen": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "Compute": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "Cor": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "DSB": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "DSBmiss": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "DataSharing": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "Fed": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "FetchBW": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "FetchLat": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "Flops": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "FpScalar": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "FpVector": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "Frontend": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "HPC": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "IcMiss": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "InsType": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "IntVector": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "L2Evicts": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "LSD": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "MachineClears": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "Machine_Clears": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "Mem": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "MemOffcore": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "MemoryBW": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "MemoryBound": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "MemoryLat": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "MemoryTLB": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "Memory_BW": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "Memory_Lat": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "MicroSeq": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "OS": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "Offcore": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "PGO": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "Pipeline": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "PortsUtil": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "Power": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "Prefetches": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "Ret": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "Retire": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "SMT": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "Server": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "Snoop": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "SoC": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "Summary": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "TmaL1": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "TmaL2": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "TmaL3mem": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "TopdownL1": "Metrics for top-down breakdown at level 1",
+    "TopdownL2": "Metrics for top-down breakdown at level 2",
+    "TopdownL3": "Metrics for top-down breakdown at level 3",
+    "TopdownL4": "Metrics for top-down breakdown at level 4",
+    "TopdownL5": "Metrics for top-down breakdown at level 5",
+    "TopdownL6": "Metrics for top-down breakdown at level 6",
+    "tma_L1_group": "Metrics for top-down breakdown at level 1",
+    "tma_L2_group": "Metrics for top-down breakdown at level 2",
+    "tma_L3_group": "Metrics for top-down breakdown at level 3",
+    "tma_L4_group": "Metrics for top-down breakdown at level 4",
+    "tma_L5_group": "Metrics for top-down breakdown at level 5",
+    "tma_L6_group": "Metrics for top-down breakdown at level 6",
+    "tma_alu_op_utilization_group": "Metrics contributing to tma_alu_op_utilization category",
+    "tma_assists_group": "Metrics contributing to tma_assists category",
+    "tma_backend_bound_aux_group": "Metrics contributing to tma_backend_bound_aux category",
+    "tma_backend_bound_group": "Metrics contributing to tma_backend_bound category",
+    "tma_bad_speculation_group": "Metrics contributing to tma_bad_speculation category",
+    "tma_base_group": "Metrics contributing to tma_base category",
+    "tma_branch_mispredicts_group": "Metrics contributing to tma_branch_mispredicts category",
+    "tma_branch_resteers_group": "Metrics contributing to tma_branch_resteers category",
+    "tma_core_bound_group": "Metrics contributing to tma_core_bound category",
+    "tma_dram_bound_group": "Metrics contributing to tma_dram_bound category",
+    "tma_dtlb_load_group": "Metrics contributing to tma_dtlb_load category",
+    "tma_dtlb_store_group": "Metrics contributing to tma_dtlb_store category",
+    "tma_fetch_bandwidth_group": "Metrics contributing to tma_fetch_bandwidth category",
+    "tma_fetch_latency_group": "Metrics contributing to tma_fetch_latency category",
+    "tma_fp_arith_group": "Metrics contributing to tma_fp_arith category",
+    "tma_fp_vector_group": "Metrics contributing to tma_fp_vector category",
+    "tma_frontend_bound_group": "Metrics contributing to tma_frontend_bound category",
+    "tma_heavy_operations_group": "Metrics contributing to tma_heavy_operations category",
+    "tma_int_operations_group": "Metrics contributing to tma_int_operations category",
+    "tma_issue2P": "Metrics related by the issue $issue2P",
+    "tma_issueBM": "Metrics related by the issue $issueBM",
+    "tma_issueBW": "Metrics related by the issue $issueBW",
+    "tma_issueComp": "Metrics related by the issue $issueComp",
+    "tma_issueD0": "Metrics related by the issue $issueD0",
+    "tma_issueFB": "Metrics related by the issue $issueFB",
+    "tma_issueFL": "Metrics related by the issue $issueFL",
+    "tma_issueL1": "Metrics related by the issue $issueL1",
+    "tma_issueLat": "Metrics related by the issue $issueLat",
+    "tma_issueMC": "Metrics related by the issue $issueMC",
+    "tma_issueMS": "Metrics related by the issue $issueMS",
+    "tma_issueMV": "Metrics related by the issue $issueMV",
+    "tma_issueRFO": "Metrics related by the issue $issueRFO",
+    "tma_issueSL": "Metrics related by the issue $issueSL",
+    "tma_issueSO": "Metrics related by the issue $issueSO",
+    "tma_issueSmSt": "Metrics related by the issue $issueSmSt",
+    "tma_issueSpSt": "Metrics related by the issue $issueSpSt",
+    "tma_issueSyncxn": "Metrics related by the issue $issueSyncxn",
+    "tma_issueTLB": "Metrics related by the issue $issueTLB",
+    "tma_l1_bound_group": "Metrics contributing to tma_l1_bound category",
+    "tma_l3_bound_group": "Metrics contributing to tma_l3_bound category",
+    "tma_light_operations_group": "Metrics contributing to tma_light_operations category",
+    "tma_load_op_utilization_group": "Metrics contributing to tma_load_op_utilization category",
+    "tma_machine_clears_group": "Metrics contributing to tma_machine_clears category",
+    "tma_mem_latency_group": "Metrics contributing to tma_mem_latency category",
+    "tma_mem_scheduler_group": "Metrics contributing to tma_mem_scheduler category",
+    "tma_memory_bound_group": "Metrics contributing to tma_memory_bound category",
+    "tma_microcode_sequencer_group": "Metrics contributing to tma_microcode_sequencer category",
+    "tma_mite_group": "Metrics contributing to tma_mite category",
+    "tma_nuke_group": "Metrics contributing to tma_nuke category",
+    "tma_other_light_ops_group": "Metrics contributing to tma_other_light_ops category",
+    "tma_ports_utilization_group": "Metrics contributing to tma_ports_utilization category",
+    "tma_ports_utilized_0_group": "Metrics contributing to tma_ports_utilized_0 category",
+    "tma_ports_utilized_3m_group": "Metrics contributing to tma_ports_utilized_3m category",
+    "tma_resource_bound_group": "Metrics contributing to tma_resource_bound category",
+    "tma_retiring_group": "Metrics contributing to tma_retiring category",
+    "tma_serializing_operation_group": "Metrics contributing to tma_serializing_operation category",
+    "tma_store_bound_group": "Metrics contributing to tma_store_bound category",
+    "tma_store_op_utilization_group": "Metrics contributing to tma_store_op_utilization category"
+}
tools/perf/pmu-events/arch/x86/meteorlake/mtl-metrics.json new file +2551 -0

2,551 lines not on this page · raw message

Signature · 1 line
2.43.0