Skip to contentSkip to conversation

TPEBS counting mode support

RFC PATCH Superseded [email protected] 2 participants 17 messages Mar 2, 2024 – Mar 12, 2024 perf events

v3 of 18 Superseded Latest is v18 →

lore
Share & tools
lore
#0 lore raw

v3 cover TPEBS counting mode support

From: Weilin Wang <[email protected]>

Changes in v3:
- Remove ':' when event name has '@' [Ian]
- Use 'R' as the modifier instead of "retire_latency" [Ian]

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

Weilin Wang (6):
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: Create another thread for sample data processing 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                     |  222 +-
 .../arch/x86/meteorlake/metricgroups.json     |  127 +
 .../arch/x86/meteorlake/mtl-metrics.json      | 2547 +++++++++++++++++
 tools/perf/util/data.c                        |    4 +
 tools/perf/util/data.h                        |    1 +
 tools/perf/util/metricgroup.c                 |   81 +-
 tools/perf/util/metricgroup.h                 |   18 +-
 tools/perf/util/stat-display.c                |   65 +
 tools/perf/util/stat-shadow.c                 |   18 +
 tools/perf/util/stat.h                        |    5 +
 10 files changed, 3069 insertions(+), 19 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 v3 1/6 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 :retire_latency keyword 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 | 81 +++++++++++++++++++++++++++++------
 tools/perf/util/metricgroup.h | 10 ++++-
 tools/perf/util/stat.h        |  2 +
 4 files changed, 112 insertions(+), 19 deletions(-)
tools/perf/builtin-stat.c +33 -5
index 6bba1a89d030..5a3093541cff 100644
@@ -162,6 +162,7 @@ static struct perf_stat_config stat_config = {
 	.ctl_fd			= -1,
 	.ctl_fd_ack		= -1,
 	.iostat_run		= false,
+	.tpebs_event_size = 0,
 };
 
 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,15 @@ 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 +2122,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 +2157,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 +2193,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 +2236,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 +2760,8 @@ int cmd_stat(int argc, const char **argv)
 		}
 	}
 
+	INIT_LIST_HEAD(&stat_config.tpebs_events);
+
 	/*
 	 * 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 +68 -13

168 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 v3 1/6] perf stat: Parse and find tpebs events when parsing metrics to prepare for perf record sampling

Quoted 20 lines · On Fri, Mar 1, 2024 at 4:11 PM <[email protected]> wrote:

From: Weilin Wang <[email protected]>

Metrics that use tpebs values would use the :retire_latency keyword 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 | 81 +++++++++++++++++++++++++++++------
 tools/perf/util/metricgroup.h | 10 ++++-
 tools/perf/util/stat.h        |  2 +
 4 files changed, 112 insertions(+), 19 deletions(-)
tools/perf/builtin-stat.c +0 -0
index 6bba1a89d030..5a3093541cff 100644

tools/perf/builtin-stat.c static struct perf_stat_config stat_config = { · line 165

Quoted 4 lines
tools/perf/builtin-stat.c +1 -0
@@ -162,6 +162,7 @@ static struct perf_stat_config stat_config = {
        .ctl_fd                 = -1,
        .ctl_fd_ack             = -1,
        .iostat_run             = false,
+       .tpebs_event_size = 0,

Usually you don't need to add 0 initial value as it's the default.

Quoted 17 lines
tools/perf/builtin-stat.c +6 -0
 };
 
 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;

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

Quoted 4 lines
tools/perf/builtin-stat.c +1 -0
@@ -703,6 +710,15 @@ 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

We prefer C style comments /* ... */.

Quoted 12 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) {
                        perror("failed to prepare workload");

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

Quoted 7 lines
tools/perf/builtin-stat.c +3 -1
@@ -2106,7 +2122,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);

Hmm.. the argument list is too long. Maybe we need to think about just passing a pointer to the stat_config instead.

Quoted 37 lines
tools/perf/builtin-stat.c +9 -3
        }
 
        if (smi_cost) {
@@ -2139,7 +2157,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 +2193,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 +2236,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) {

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

Quoted 4 lines
tools/perf/builtin-stat.c +1 -0
@@ -2736,6 +2760,8 @@ int cmd_stat(int argc, const char **argv)
                }
        }
 
+       INIT_LIST_HEAD(&stat_config.tpebs_events);

You can move it to the initializer using LIST_HEAD_INIT().

Quoted 58 lines
tools/perf/builtin-stat.c +4 -1
+
        /*
         * 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 b24a1c177a80..5ee96d2f16c9 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;
        }
@@ -670,7 +672,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 691

Quoted 5 lines
tools/perf/util/metricgroup.c +2 -0
@@ -683,8 +687,47 @@ 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;
+               char *p = strstr(id, ":R");
+               char *p1 = strstr(id, "@R");

Can you please add a comment on what you are doing here?

Quoted 10 lines
tools/perf/util/metricgroup.c +10 -0
+
+               if (p == NULL && p1) {
+                       p = strstr(p1+1, "@R");
+                       if (p == NULL)
+                               p = p1;
+                       p = p+1;
+               }
+
+               if (p) {
+                       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 710

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

Note the previous two allocations can also fail. :)

Quoted 7 lines
tools/perf/util/metricgroup.c +7 -0
+
+                       at = strchr(id, '@');
+                       if (at != NULL) {
+                               pr_debug("tpebs name %s\n", id);
+                               *at = '/';
+                               at = strchr(id, '@');
+                               *at = '/';

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

Quoted 10 lines
tools/perf/util/metricgroup.c +10 -0
+                               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);

Broken indentation.

Quoted 67 lines
tools/perf/util/metricgroup.c +14 -6
+                       list_add_tail(&new_event->nd, tpebs_events);
+                       continue;
+               }
 
                /* Always move tool events outside of the group. */
                ev = perf_tool_event__from_str(id);
@@ -1449,7 +1492,8 @@ static int build_combined_expr_ctx(const struct list_head *metric_list,
 static int parse_ids(bool metric_no_merge, struct perf_pmu *fake_pmu,
                     struct expr_parse_ctx *ids, const char *modifier,
                     bool group_events, const bool tool_events[PERF_TOOL_MAX],
-                    struct evlist **out_evlist)
+                    struct evlist **out_evlist, struct list_head *tpebs_events,
+                    size_t *tpebs_event_size)
 {
        struct parse_events_error parse_error;
        struct evlist *parsed_evlist;
@@ -1492,7 +1536,7 @@ static int parse_ids(bool metric_no_merge, struct perf_pmu *fake_pmu,
                }
        }
        ret = metricgroup__build_event_string(&events, ids, modifier,
-                                             group_events);
+                                             group_events, tpebs_events, tpebs_event_size);
        if (ret)
                return ret;
 
@@ -1531,7 +1575,9 @@ static int parse_groups(struct evlist *perf_evlist,
                        bool system_wide,
                        struct perf_pmu *fake_pmu,
                        struct rblist *metric_events_list,
-                       const struct pmu_metrics_table *table)
+                       const struct pmu_metrics_table *table,
+                       struct list_head *tpebs_events,
+                       size_t *tpebs_event_size)
 {
        struct evlist *combined_evlist = NULL;
        LIST_HEAD(metric_list);
@@ -1563,7 +1609,8 @@ static int parse_groups(struct evlist *perf_evlist,
                                        /*modifier=*/NULL,
                                        /*group_events=*/false,
                                        tool_events,
-                                       &combined_evlist);
+                                       &combined_evlist,
+                                       tpebs_events, tpebs_event_size);
                }
                if (combined)
                        expr__ctx_free(combined);
@@ -1618,14 +1665,15 @@ static int parse_groups(struct evlist *perf_evlist,
                }
                if (!metric_evlist) {
                        ret = parse_ids(metric_no_merge, fake_pmu, m->pctx, m->modifier,
-                                       m->group_events, tool_events, &m->evlist);
+                                       m->group_events, tool_events, &m->evlist,
+                                       tpebs_events, tpebs_event_size);
                        if (ret)
                                goto out;
 
                        metric_evlist = m->evlist;
                }
                ret = setup_metric_events(fake_pmu ? "all" : m->pmu, m->pctx->ids,
-                                         metric_evlist, &metric_events);
+                                         metric_evlist, &metric_events, *tpebs_event_size);
                if (ret) {
                        pr_err("Cannot resolve IDs for %s: %s\n",
                                m->metric_name, m->metric_expr);
@@ -1692,16 +1740,21 @@ int metricgroup__parse_groups(struct evlist *perf_evlist,
                              bool metric_no_threshold,
                              const char *user_requested_cpu_list,

tools/perf/util/metricgroup.c int metricgroup__parse_groups(struct evlist *perf_evlist, · line 1750

Quoted 10 lines
tools/perf/util/metricgroup.c +5 -1
                              bool system_wide,
-                             struct rblist *metric_events)
+                             struct rblist *metric_events,
+                             struct list_head *tpebs_events,
+                             size_t *tpebs_event_size)
 {
        const struct pmu_metrics_table *table = pmu_metrics_table__find();
 
+       pr_debug("Test debugging\n");
+

Can you please remove these debug messages before submitting?

Thanks,
Namhyung

Quoted 66 lines
tools/perf/util/metricgroup.c +5 -2
        if (!table)
                return -EINVAL;
 
        return parse_groups(perf_evlist, pmu, str, metric_no_group, metric_no_merge,
                            metric_no_threshold, user_requested_cpu_list, system_wide,
-                           /*fake_pmu=*/NULL, metric_events, table);
+                           /*fake_pmu=*/NULL, metric_events, table, tpebs_events,
+                           tpebs_event_size);
 }
 
 int metricgroup__parse_groups_test(struct evlist *evlist,
@@ -1715,7 +1768,9 @@ int metricgroup__parse_groups_test(struct evlist *evlist,
                            /*metric_no_threshold=*/false,
                            /*user_requested_cpu_list=*/NULL,
                            /*system_wide=*/false,
-                           &perf_pmu__fake, metric_events, table);
+                           &perf_pmu__fake, metric_events, table,
+                           /*tpebs_events=*/NULL,
+                           /*tpebs_event_size=*/0);
 }
 
 struct metricgroup__has_metric_data {
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 -1
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;
--

2.43.0

#3 lore raw

RFC PATCH v3 2/6 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.

---
 tools/perf/builtin-stat.c     | 179 +++++++++++++++++++++++++++++++++-
 tools/perf/util/data.c        |   4 +
 tools/perf/util/data.h        |   1 +
 tools/perf/util/metricgroup.h |   7 ++
 tools/perf/util/stat.h        |   3 +
 5 files changed, 191 insertions(+), 3 deletions(-)
tools/perf/builtin-stat.c +176 -3
index 5a3093541cff..3890a579349e 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);
 
@@ -162,7 +167,8 @@ static struct perf_stat_config stat_config = {
 	.ctl_fd			= -1,
 	.ctl_fd_ack		= -1,
 	.iostat_run		= false,
-	.tpebs_event_size = 0,
+	.tpebs_event_size       = 0,
+	.tpebs_pid              = -1,
 };
 
 static bool cpus_map_matched(struct evsel *a, struct evsel *b)
@@ -687,12 +693,163 @@ static enum counter_recovery stat_handle_error(struct evsel *counter)
 	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";
+
+	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";
+
+	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;
+
+	record_argv = calloc(10 + 2 * stat_config.tpebs_event_size, sizeof(char *));
+	if (!record_argv)
+		return -1;
+	__run_perf_record(record_argv);
+
+	prepare_run_command(cmd, record_argv);
+	return start_command(cmd);
+}
+
+struct perf_script {
+	struct perf_tool	tool;
+	struct perf_session	*session;
+	struct evswitch		evswitch;
+	struct perf_cpu_map	*cpus;
+	struct perf_thread_map *threads;
+	int			name_width;
+};
+
+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, nd) {
+		list_del_init(&r->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,
+				union perf_event *event __maybe_unused,
+				struct perf_sample *sample,
+				struct evsel *evsel,
+				struct machine *machine __maybe_unused)
+{
+	struct perf_script *script = container_of(tool, struct perf_script, tool);
+	int ret = 0;
+	const char *evname;
+	struct tpebs_retire_lat *t;
+
+	pr_debug("entering function %s\n ", __func__);
+	evname = evsel__name(evsel);
+
+	pr_debug("[%03d] ", sample->cpu);
+	pr_debug("%*s: ", script->name_width, evname ?: "[unknown]");
+	pr_debug("%16" PRIu16, sample->retire_lat);
+	pr_debug("\n");
+
+	// 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, nd) {
+		if (!strcmp(evname, t->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 int __cmd_script(struct child_process *cmd __maybe_unused)
+{
+	int err = 0;
+	struct perf_session *session;
+	struct perf_data data = {
+		.mode = PERF_DATA_MODE_READ,
+		.path = PERF_DATA,
+		.fd   = cmd->out,
+	};
+	struct perf_script script = {
+		.tool = {
+		.sample		 = process_sample_event,
+		.ordered_events	 = true,
+		.ordering_requires_timestamps = true,
+		.feature	 = process_feature_event,
+		.attr		 = perf_event__process_attr,
+		},
+	};
+	struct tpebs_event *e;
+
+	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->name = strdup(e->name);
+		new->tpebs_name = strdup(e->tpebs_name);
+		new->count = 0;
+		new->sum = 0;
+		list_add_tail(&new->nd, &stat_config.tpebs_results);
+	}
+
+	kill(cmd->pid, SIGTERM);
+	session = perf_session__new(&data, &script.tool);
+	if (IS_ERR(session))
+		return PTR_ERR(session);
+	script.session = session;
+	err = perf_session__process_events(session);
+	perf_session__delete(session);
+
+	return err;
+}
+
 static int __run_perf_stat(int argc, const char **argv, int run_idx)
 {
 	int interval = stat_config.interval;
@@ -709,12 +866,14 @@ 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;
 
 	//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();
+		pr_debug("perf stat pid = %d\n", getpid());
+		ret = prepare_perf_record(&cmd);
 		if (ret)
 			return ret;
 	}
@@ -924,6 +1083,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;
+
+		pr_debug("pid = %d\n", getpid());
+		pr_debug("cmd.pid = %d\n", cmd.pid);
+
+		ret = __cmd_script(&cmd);
+		close(cmd.out);
+		pr_debug("%d\n", ret);
+	}
+
 	if (stat_config.walltime_run_table)
 		stat_config.walltime_run[run_idx] = t1 - t0;
 
@@ -2761,6 +2931,7 @@ int cmd_stat(int argc, const char **argv)
 	}
 
 	INIT_LIST_HEAD(&stat_config.tpebs_events);
+	INIT_LIST_HEAD(&stat_config.tpebs_results);
 
 	/*
 	 * Metric parsing needs to be delayed as metrics may optimize events
@@ -2972,5 +3143,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 +4 -0
index 08c4bfbd817f..2e2a20fc5c30 100644
@@ -185,6 +185,10 @@ static bool check_pipe(struct perf_data *data)
 	int fd = perf_data__is_read(data) ?
 		 STDIN_FILENO : STDOUT_FILENO;
 
+	if (data->fd > 0) {
+		fd = data->fd;
+	}
+
 	if (!data->path) {
 		if (!fstat(fd, &st) && S_ISFIFO(st.st_mode))
 			is_pipe = true;
tools/perf/util/data.h +1 -0
index 110f3ebde30f..720638116ca0 100644
@@ -28,6 +28,7 @@ struct perf_data_file {
 
 struct perf_data {
 	const char		*path;
+	int			 fd;
 	struct perf_data_file	 file;
 	bool			 is_pipe;
 	bool			 is_dir;
tools/perf/util/metricgroup.h +7 -0
index 7c24ed768ff3..1fa12cc3294e 100644
@@ -71,6 +71,13 @@ struct tpebs_event {
 	const char *name;
 	const char *tpebs_name;
 };
+struct tpebs_retire_lat {
+	struct list_head nd;
+	const char *name;
+	const char *tpebs_name;
+	size_t count;
+	int sum;
+};
 
 struct metric_event *metricgroup__lookup(struct rblist *metric_events,
 					 struct evsel *evsel,
tools/perf/util/stat.h +3 -0
index b987960df3c5..2387c6289f93 100644
@@ -111,6 +111,9 @@ 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			 tpebs_pipe;
 	int			 ctl_fd;
 	int			 ctl_fd_ack;
 	bool			 ctl_fd_close;
Signature · 1 line
2.43.0
maintainer
#4 lore raw

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

Hello Weilin,

Quoted 51 lines · On Fri, Mar 1, 2024 at 4:11 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.

---
 tools/perf/builtin-stat.c     | 179 +++++++++++++++++++++++++++++++++-
 tools/perf/util/data.c        |   4 +
 tools/perf/util/data.h        |   1 +
 tools/perf/util/metricgroup.h |   7 ++
 tools/perf/util/stat.h        |   3 +
 5 files changed, 191 insertions(+), 3 deletions(-)
tools/perf/builtin-stat.c +7 -1
index 5a3093541cff..3890a579349e 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);
 
@@ -162,7 +167,8 @@ static struct perf_stat_config stat_config = {
        .ctl_fd                 = -1,
        .ctl_fd_ack             = -1,
        .iostat_run             = false,
-       .tpebs_event_size = 0,
+       .tpebs_event_size       = 0,
+       .tpebs_pid              = -1,
 };
 
 static bool cpus_map_matched(struct evsel *a, struct evsel *b)

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

Quoted 8 lines
tools/perf/builtin-stat.c +3 -1
@@ -687,12 +693,163 @@ static enum counter_recovery stat_handle_error(struct evsel *counter)
        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;

Please put a blank line after the declaration.

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

Quoted 3 lines
tools/perf/builtin-stat.c +2 -0
        pr_debug("Prepare perf record for retire_latency\n");
+
+

A duplicate new line.

Quoted 9 lines
tools/perf/builtin-stat.c +9 -0
+       record_argv[i++] = "perf";
+       record_argv[i++] = "record";
+       record_argv[i++] = "-W";
+
+       if (stat_config.user_requested_cpu_list) {
+               record_argv[i++] = "-C";
+               record_argv[i++] = stat_config.user_requested_cpu_list;
+       }
+

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

Quoted 10 lines
tools/perf/builtin-stat.c +10 -0
+       if (stat_config.system_wide)
+               record_argv[i++] = "-a";
+
+       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;

I don't think you need side-band records and synthesizing for this. I'd like to disable all of them but it'd require changes in perf record. For now, you need to pass --synth=no at least.

Thanks,
Namhyung

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

RE: [RFC PATCH v3 2/6] 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, March 11, 2024 2:08 PM
To: Wang, Weilin <[email protected]>
Cc: Ian Rogers <[email protected]>; Peter Zijlstra <[email protected]>; Ingo Molnar <[email protected]>; Arnaldo Carvalho de Melo <[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 v3 2/6] perf stat: Fork and launch perf record when perf stat needs to get retire latency value for a metric.

Hello Weilin,

Quoted 9 lines · On Fri, Mar 1, 2024 at 4:11 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 6 lines

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

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

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

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

Quoted 35 lines
 tools/perf/util/data.c        |   4 +
 tools/perf/util/data.h        |   1 +
 tools/perf/util/metricgroup.h |   7 ++
 tools/perf/util/stat.h        |   3 +
 5 files changed, 191 insertions(+), 3 deletions(-)
tools/perf/builtin-stat.c +7 -1
index 5a3093541cff..3890a579349e 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);
 
@@ -162,7 +167,8 @@ static struct perf_stat_config stat_config = {
        .ctl_fd                 = -1,
        .ctl_fd_ack             = -1,
        .iostat_run             = false,
-       .tpebs_event_size = 0,
+       .tpebs_event_size       = 0,
+       .tpebs_pid              = -1,
 };
 
 static bool cpus_map_matched(struct evsel *a, struct evsel *b)
@@ -687,12 +693,163 @@ static enum counter_recovery

stat_handle_error(struct evsel *counter)

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

Quoted 8 lines
tools/perf/builtin-stat.c +3 -1
        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;

Please put a blank line after the declaration.

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

Quoted 3 lines
tools/perf/builtin-stat.c +2 -0
        pr_debug("Prepare perf record for retire_latency\n");
+
+

A duplicate new line.

Quoted 9 lines
tools/perf/builtin-stat.c +9 -0
+       record_argv[i++] = "perf";
+       record_argv[i++] = "record";
+       record_argv[i++] = "-W";
+
+       if (stat_config.user_requested_cpu_list) {
+               record_argv[i++] = "-C";
+               record_argv[i++] = stat_config.user_requested_cpu_list;
+       }
+

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

Quoted 10 lines
tools/perf/builtin-stat.c +10 -0
+       if (stat_config.system_wide)
+               record_argv[i++] = "-a";
+
+       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;

I don't think you need side-band records and synthesizing for this. I'd like to disable all of them but it'd require changes in perf record. For now, you need to pass --synth=no at least.

Quoted 3 lines

Thanks,
Namhyung

Thanks Namhyung! I will update the code and send a new version.

Quoted 4 lines
Quoted 4 lines
tools/perf/builtin-stat.c +1 -0
+
        return 0;
 }
maintainer
#6 lore raw

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

Quoted 118 lines · Weilin
Quoted 118 lines

-----Original Message-----
From: Namhyung Kim <[email protected]>
Sent: Monday, March 11, 2024 2:08 PM
To: Wang, Weilin <[email protected]>
Cc: Ian Rogers <[email protected]>; Peter Zijlstra <[email protected]>; Ingo Molnar <[email protected]>; Arnaldo Carvalho de Melo <[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 v3 2/6] perf stat: Fork and launch perf record when perf stat needs to get retire latency value for a metric.

Hello Weilin,

Quoted 9 lines · On Fri, Mar 1, 2024 at 4:11 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 6 lines

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

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

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

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

Quoted 35 lines
 tools/perf/util/data.c        |   4 +
 tools/perf/util/data.h        |   1 +
 tools/perf/util/metricgroup.h |   7 ++
 tools/perf/util/stat.h        |   3 +
 5 files changed, 191 insertions(+), 3 deletions(-)
tools/perf/builtin-stat.c +7 -1
index 5a3093541cff..3890a579349e 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);
 
@@ -162,7 +167,8 @@ static struct perf_stat_config stat_config = {
        .ctl_fd                 = -1,
        .ctl_fd_ack             = -1,
        .iostat_run             = false,
-       .tpebs_event_size = 0,
+       .tpebs_event_size       = 0,
+       .tpebs_pid              = -1,
 };
 
 static bool cpus_map_matched(struct evsel *a, struct evsel *b)
@@ -687,12 +693,163 @@ static enum counter_recovery

stat_handle_error(struct evsel *counter)

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

Quoted 8 lines
tools/perf/builtin-stat.c +3 -1
        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;

Please put a blank line after the declaration.

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

Quoted 3 lines
tools/perf/builtin-stat.c +2 -0
        pr_debug("Prepare perf record for retire_latency\n");
+
+

A duplicate new line.

Quoted 9 lines
tools/perf/builtin-stat.c +9 -0
+       record_argv[i++] = "perf";
+       record_argv[i++] = "record";
+       record_argv[i++] = "-W";
+
+       if (stat_config.user_requested_cpu_list) {
+               record_argv[i++] = "-C";
+               record_argv[i++] = stat_config.user_requested_cpu_list;
+       }
+

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

Quoted 10 lines
tools/perf/builtin-stat.c +10 -0
+       if (stat_config.system_wide)
+               record_argv[i++] = "-a";
+
+       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;

I don't think you need side-band records and synthesizing for this. I'd like to disable all of them but it'd require changes in perf record. For now, you need to pass --synth=no at least.

Quoted 5 lines
Quoted 3 lines

Thanks,
Namhyung

Thanks Namhyung! I will update the code and send a new version.

Sure, let me finish my review before then.

Thanks,
Namhyung

maintainer
#7 lore raw

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

In reply to [email protected]
Quoted 82 lines · On Fri, Mar 1, 2024 at 4:11 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.

---
 tools/perf/builtin-stat.c     | 179 +++++++++++++++++++++++++++++++++-
 tools/perf/util/data.c        |   4 +
 tools/perf/util/data.h        |   1 +
 tools/perf/util/metricgroup.h |   7 ++
 tools/perf/util/stat.h        |   3 +
 5 files changed, 191 insertions(+), 3 deletions(-)
tools/perf/builtin-stat.c +32 -2
index 5a3093541cff..3890a579349e 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);
 
@@ -162,7 +167,8 @@ static struct perf_stat_config stat_config = {
        .ctl_fd                 = -1,
        .ctl_fd_ack             = -1,
        .iostat_run             = false,
-       .tpebs_event_size = 0,
+       .tpebs_event_size       = 0,
+       .tpebs_pid              = -1,
 };
 
 static bool cpus_map_matched(struct evsel *a, struct evsel *b)
@@ -687,12 +693,163 @@ static enum counter_recovery stat_handle_error(struct evsel *counter)
        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";
+
+       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";
+
+       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;
+

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

Quoted 10 lines
tools/perf/builtin-stat.c +7 -0
        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;
+}

A new line please.

Quoted 11 lines
tools/perf/builtin-stat.c +11 -0
+static int prepare_perf_record(struct child_process *cmd)
+{
+       const char **record_argv;
+
+       record_argv = calloc(10 + 2 * stat_config.tpebs_event_size, sizeof(char *));
+       if (!record_argv)
+               return -1;
+       __run_perf_record(record_argv);
+
+       prepare_run_command(cmd, record_argv);
+       return start_command(cmd);

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

Quoted 10 lines
tools/perf/builtin-stat.c +10 -0
+}
+
+struct perf_script {
+       struct perf_tool        tool;
+       struct perf_session     *session;
+       struct evswitch         evswitch;
+       struct perf_cpu_map     *cpus;
+       struct perf_thread_map *threads;
+       int                     name_width;
+};

Do we really need all these?

Quoted 23 lines
tools/perf/builtin-stat.c +23 -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, nd) {
+               list_del_init(&r->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,
+                               union perf_event *event __maybe_unused,
+                               struct perf_sample *sample,
+                               struct evsel *evsel,
+                               struct machine *machine __maybe_unused)
+{
+       struct perf_script *script = container_of(tool, struct perf_script, tool);
+       int ret = 0;

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

Quoted 10 lines
tools/perf/builtin-stat.c +10 -0
+       const char *evname;
+       struct tpebs_retire_lat *t;
+
+       pr_debug("entering function %s\n ", __func__);
+       evname = evsel__name(evsel);
+
+       pr_debug("[%03d] ", sample->cpu);
+       pr_debug("%*s: ", script->name_width, evname ?: "[unknown]");
+       pr_debug("%16" PRIu16, sample->retire_lat);
+       pr_debug("\n");

These debug messages can go away.

Quoted 10 lines
tools/perf/builtin-stat.c +10 -0
+
+       // 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, nd) {
+               if (!strcmp(evname, t->name)) {
+                       t->count += 1;
+                       t->sum += sample->retire_lat;
+                       break;
+               }

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

Quoted 10 lines
tools/perf/builtin-stat.c +10 -0
+       }
+
+       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);

Just curious. Why is this necessary?

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

Quoted 4 lines
tools/perf/builtin-stat.c +4 -0
+       return 0;
+}
+
+static int __cmd_script(struct child_process *cmd __maybe_unused)

__cmd_script() is probably not a good name. Maybe just process_tpebs_samples()? Also 'cmd' is actually used.

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

Quoted 13 lines
tools/perf/builtin-stat.c +13 -0
+{
+       int err = 0;
+       struct perf_session *session;
+       struct perf_data data = {
+               .mode = PERF_DATA_MODE_READ,
+               .path = PERF_DATA,
+               .fd   = cmd->out,
+       };
+       struct perf_script script = {
+               .tool = {
+               .sample          = process_sample_event,
+               .ordered_events  = true,
+               .ordering_requires_timestamps = true,

I don't think you need an ordered events queue for this.

Quoted 5 lines
tools/perf/builtin-stat.c +5 -0
+               .feature         = process_feature_event,
+               .attr            = perf_event__process_attr,
+               },
+       };
+       struct tpebs_event *e;

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

Quoted 10 lines
tools/perf/builtin-stat.c +10 -0
+
+       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->name = strdup(e->name);
+               new->tpebs_name = strdup(e->tpebs_name);
+               new->count = 0;
+               new->sum = 0;

I think you can simply use zalloc() instead.

Quoted 36 lines
tools/perf/builtin-stat.c +17 -1
+               list_add_tail(&new->nd, &stat_config.tpebs_results);
+       }
+
+       kill(cmd->pid, SIGTERM);
+       session = perf_session__new(&data, &script.tool);
+       if (IS_ERR(session))
+               return PTR_ERR(session);
+       script.session = session;
+       err = perf_session__process_events(session);
+       perf_session__delete(session);
+
+       return err;
+}
+
 static int __run_perf_stat(int argc, const char **argv, int run_idx)
 {
        int interval = stat_config.interval;
@@ -709,12 +866,14 @@ 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;
 
        //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();
+               pr_debug("perf stat pid = %d\n", getpid());
+               ret = prepare_perf_record(&cmd);
                if (ret)
                        return ret;
        }
@@ -924,6 +1083,17 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
 
        t1 = rdclock();

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

Quoted 10 lines
tools/perf/builtin-stat.c +9 -0
 
+       if (stat_config.tpebs_event_size > 0) {
+               int ret;
+
+               pr_debug("pid = %d\n", getpid());
+               pr_debug("cmd.pid = %d\n", cmd.pid);
+
+               ret = __cmd_script(&cmd);
+               close(cmd.out);
+               pr_debug("%d\n", ret);

Please remove all the unnecessary debug messages.

And you don't need to wait for the target process to finish in order to process samples. Actually it'd increase the chance that the pipe is full during the execution.

Quoted 26 lines
tools/perf/builtin-stat.c +5 -0
+       }
+
        if (stat_config.walltime_run_table)
                stat_config.walltime_run[run_idx] = t1 - t0;
 
@@ -2761,6 +2931,7 @@ int cmd_stat(int argc, const char **argv)
        }
 
        INIT_LIST_HEAD(&stat_config.tpebs_events);
+       INIT_LIST_HEAD(&stat_config.tpebs_results);
 
        /*
         * Metric parsing needs to be delayed as metrics may optimize events
@@ -2972,5 +3143,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..2e2a20fc5c30 100644

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

Quoted 6 lines
tools/perf/util/data.c +3 -0
@@ -185,6 +185,10 @@ static bool check_pipe(struct perf_data *data)
        int fd = perf_data__is_read(data) ?
                 STDIN_FILENO : STDOUT_FILENO;
 
+       if (data->fd > 0) {
+               fd = data->fd;
+       }

I'm not sure if it's needed. The data->path is set and it'll be checked in the below.

tools/perf/util/data.h struct perf_data_file { · line 31

Quoted 13 lines
tools/perf/util/data.c +1 -0
+
        if (!data->path) {
                if (!fstat(fd, &st) && S_ISFIFO(st.st_mode))
                        is_pipe = true;
tools/perf/util/data.h +1 -0
index 110f3ebde30f..720638116ca0 100644
@@ -28,6 +28,7 @@ struct perf_data_file {
 
 struct perf_data {
        const char              *path;
+       int                      fd;

Can we remove this?

tools/perf/util/metricgroup.h struct tpebs_event { · line 73

Quoted 11 lines
tools/perf/util/data.h +0 -0
        struct perf_data_file    file;
        bool                     is_pipe;
        bool                     is_dir;
tools/perf/util/metricgroup.h +0 -0
index 7c24ed768ff3..1fa12cc3294e 100644
@@ -71,6 +71,13 @@ struct tpebs_event {
        const char *name;
        const char *tpebs_name;
 };

A new line please.

tools/perf/util/metricgroup.h struct tpebs_event { · line 77

Quoted 4 lines
tools/perf/util/metricgroup.h +4 -0
+struct tpebs_retire_lat {
+       struct list_head nd;
+       const char *name;
+       const char *tpebs_name;

What are the 'name' and 'tpebs_name'?

Quoted 11 lines
tools/perf/util/metricgroup.h +3 -0
+       size_t count;
+       int sum;
+};
 
 struct metric_event *metricgroup__lookup(struct rblist *metric_events,
                                         struct evsel *evsel,
tools/perf/util/stat.h +0 -0
index b987960df3c5..2387c6289f93 100644

tools/perf/util/stat.h struct perf_stat_config { · line 116

Quoted 6 lines
tools/perf/util/stat.h +3 -0
@@ -111,6 +111,9 @@ 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                      tpebs_pipe;

Did you use these?

Quoted 6 lines
tools/perf/util/stat.h +0 -1
        int                      ctl_fd;
        int                      ctl_fd_ack;
        bool                     ctl_fd_close;
--

2.43.0

#8 lore raw

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

Quoted 193 lines

-----Original Message-----
From: Namhyung Kim <[email protected]>
Sent: Monday, March 11, 2024 11:40 PM
To: Wang, Weilin <[email protected]>
Cc: Ian Rogers <[email protected]>; Peter Zijlstra <[email protected]>; Ingo Molnar <[email protected]>; Arnaldo Carvalho de Melo <[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 v3 2/6] 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 1, 2024 at 4:11 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 6 lines

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

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

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

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

Quoted 35 lines
 tools/perf/util/data.c        |   4 +
 tools/perf/util/data.h        |   1 +
 tools/perf/util/metricgroup.h |   7 ++
 tools/perf/util/stat.h        |   3 +
 5 files changed, 191 insertions(+), 3 deletions(-)
tools/perf/builtin-stat.c +7 -1
index 5a3093541cff..3890a579349e 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);
 
@@ -162,7 +167,8 @@ static struct perf_stat_config stat_config = {
        .ctl_fd                 = -1,
        .ctl_fd_ack             = -1,
        .iostat_run             = false,
-       .tpebs_event_size = 0,
+       .tpebs_event_size       = 0,
+       .tpebs_pid              = -1,
 };
 
 static bool cpus_map_matched(struct evsel *a, struct evsel *b)
@@ -687,12 +693,163 @@ static enum counter_recovery

stat_handle_error(struct evsel *counter)

Quoted 31 lines
tools/perf/builtin-stat.c +25 -1
        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";
+
+       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";
+
+       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;
+

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

Quoted 10 lines
tools/perf/builtin-stat.c +7 -0
        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;
+}

A new line please.

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

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

*));

Quoted 6 lines
tools/perf/builtin-stat.c +6 -0
+       if (!record_argv)
+               return -1;
+       __run_perf_record(record_argv);
+
+       prepare_run_command(cmd, record_argv);
+       return start_command(cmd);

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

Quoted 10 lines
tools/perf/builtin-stat.c +10 -0
+}
+
+struct perf_script {
+       struct perf_tool        tool;
+       struct perf_session     *session;
+       struct evswitch         evswitch;
+       struct perf_cpu_map     *cpus;
+       struct perf_thread_map *threads;
+       int                     name_width;
+};

Do we really need all these?

Quoted 23 lines
tools/perf/builtin-stat.c +23 -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, nd) {
+               list_del_init(&r->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,
+                               union perf_event *event __maybe_unused,
+                               struct perf_sample *sample,
+                               struct evsel *evsel,
+                               struct machine *machine __maybe_unused)
+{
+       struct perf_script *script = container_of(tool, struct perf_script, tool);
+       int ret = 0;

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

Quoted 10 lines
tools/perf/builtin-stat.c +10 -0
+       const char *evname;
+       struct tpebs_retire_lat *t;
+
+       pr_debug("entering function %s\n ", __func__);
+       evname = evsel__name(evsel);
+
+       pr_debug("[%03d] ", sample->cpu);
+       pr_debug("%*s: ", script->name_width, evname ?: "[unknown]");
+       pr_debug("%16" PRIu16, sample->retire_lat);
+       pr_debug("\n");

These debug messages can go away.

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

Quoted 3 lines
tools/perf/builtin-stat.c +3 -0
+
+       // Need to handle per core results?
+       // We are assuming average retire latency value will be used. Save the

number of

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

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

Quoted 6 lines
tools/perf/builtin-stat.c +6 -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);

Just curious. Why is this necessary?

Without this, we cannot successfully process retire_latency values when data is passed from a PIPE. I'm not sure the real reason though.

Quoted 99 lines

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

Quoted 6 lines
tools/perf/builtin-stat.c +4 -0
+       return 0;
+}
+
+static int __cmd_script(struct child_process *cmd __maybe_unused)

__cmd_script() is probably not a good name. Maybe just process_tpebs_samples()? Also 'cmd' is actually used.

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

Quoted 13 lines
tools/perf/builtin-stat.c +13 -0
+{
+       int err = 0;
+       struct perf_session *session;
+       struct perf_data data = {
+               .mode = PERF_DATA_MODE_READ,
+               .path = PERF_DATA,
+               .fd   = cmd->out,
+       };
+       struct perf_script script = {
+               .tool = {
+               .sample          = process_sample_event,
+               .ordered_events  = true,
+               .ordering_requires_timestamps = true,

I don't think you need an ordered events queue for this.

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
+               .feature         = process_feature_event,
+               .attr            = perf_event__process_attr,
+               },
+       };
+       struct tpebs_event *e;
+
+       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 enum counter_recovery stat_handle_error(struct evsel *counter) · line 838

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

I think you can simply use zalloc() instead.

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

Quoted 18 lines
tools/perf/builtin-stat.c +14 -0
+               list_add_tail(&new->nd, &stat_config.tpebs_results);
+       }
+
+       kill(cmd->pid, SIGTERM);
+       session = perf_session__new(&data, &script.tool);
+       if (IS_ERR(session))
+               return PTR_ERR(session);
+       script.session = session;
+       err = perf_session__process_events(session);
+       perf_session__delete(session);
+
+       return err;
+}
+
 static int __run_perf_stat(int argc, const char **argv, int run_idx)
 {
        int interval = stat_config.interval;
@@ -709,12 +866,14 @@ static int __run_perf_stat(int argc, const char

**argv, int run_idx)

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

Quoted 6 lines
tools/perf/builtin-stat.c +1 -0
        struct affinity saved_affinity, *affinity = NULL;
        int err;
        bool second_pass = false;
+       struct child_process cmd;
 
        //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

Quoted 10 lines
tools/perf/builtin-stat.c +2 -1
        if (stat_config.tpebs_event_size > 0) {
                int ret;
 
-               ret = __run_perf_record();
+               pr_debug("perf stat pid = %d\n", getpid());
+               ret = prepare_perf_record(&cmd);
                if (ret)
                        return ret;
        }
@@ -924,6 +1083,17 @@ static int __run_perf_stat(int argc, const char

**argv, int run_idx)

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

Quoted 12 lines
tools/perf/builtin-stat.c +9 -0
        t1 = rdclock();
 
+       if (stat_config.tpebs_event_size > 0) {
+               int ret;
+
+               pr_debug("pid = %d\n", getpid());
+               pr_debug("cmd.pid = %d\n", cmd.pid);
+
+               ret = __cmd_script(&cmd);
+               close(cmd.out);
+               pr_debug("%d\n", ret);

Please remove all the unnecessary debug messages.

Quoted 3 lines

And you don't need to wait for the target process to finish in order to process samples. Actually it'd increase the chance that the pipe is full during the execution.

This part is changed to use a separate thread in later commit.

Quoted 32 lines
Quoted 16 lines
tools/perf/builtin-stat.c +3 -0
+       }
+
        if (stat_config.walltime_run_table)
                stat_config.walltime_run[run_idx] = t1 - t0;
 
@@ -2761,6 +2931,7 @@ int cmd_stat(int argc, const char **argv)
        }
 
        INIT_LIST_HEAD(&stat_config.tpebs_events);
+       INIT_LIST_HEAD(&stat_config.tpebs_results);
 
        /*
         * Metric parsing needs to be delayed as metrics may optimize events

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

Quoted 2 lines
tools/perf/builtin-stat.c +0 -0
@@ -2972,5 +3143,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 13 lines
tools/perf/builtin-stat.c +2 -0
+       tpebs_data__delete();
+
        return status;
 }
tools/perf/util/data.c +0 -0
index 08c4bfbd817f..2e2a20fc5c30 100644
@@ -185,6 +185,10 @@ static bool check_pipe(struct perf_data *data)
        int fd = perf_data__is_read(data) ?
                 STDIN_FILENO : STDOUT_FILENO;
 
Quoted 6 lines

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

Quoted 3 lines
tools/perf/util/data.c +3 -0
+       if (data->fd > 0) {
+               fd = data->fd;
+       }

I'm not sure if it's needed. The data->path is set and it'll be checked in the below.

I think we need to set this fd when we use PIPE instead of a file. This fd is opened in start_command() in run-command.c. So we need to pass this value back at execution time.

Quoted 33 lines
Quoted 11 lines
tools/perf/util/data.c +1 -0
+
        if (!data->path) {
                if (!fstat(fd, &st) && S_ISFIFO(st.st_mode))
                        is_pipe = true;
tools/perf/util/data.h +0 -0
index 110f3ebde30f..720638116ca0 100644

tools/perf/util/data.h struct perf_data_file { · line 31

Quoted 4 lines
tools/perf/util/data.h +1 -0
@@ -28,6 +28,7 @@ struct perf_data_file {
 
 struct perf_data {
        const char              *path;
+       int                      fd;

Can we remove this?

tools/perf/util/metricgroup.h struct tpebs_event { · line 73

Quoted 11 lines
tools/perf/util/data.h +0 -0
        struct perf_data_file    file;
        bool                     is_pipe;
        bool                     is_dir;
tools/perf/util/metricgroup.h +0 -0
index 7c24ed768ff3..1fa12cc3294e 100644
@@ -71,6 +71,13 @@ struct tpebs_event {
        const char *name;
        const char *tpebs_name;
 };

A new line please.

Quoted 6 lines

tools/perf/util/metricgroup.h struct tpebs_event { · line 77

Quoted 4 lines
tools/perf/util/metricgroup.h +4 -0
+struct tpebs_retire_lat {
+       struct list_head nd;
+       const char *name;
+       const char *tpebs_name;

What are the 'name' and 'tpebs_name'?

Sorry about this bad naming! There is a name change from event_name:R to event_name:p at metric formula parsing time.
The "name" is meant for event_name (added :p for perf record purpose) and "tpebs_name" is event_name:R (event_name with TPEBS modifier).

I will add some comments here. Please let me know if you have any name suggestions.

Quoted 29 lines
Quoted 13 lines
tools/perf/util/metricgroup.h +3 -0
+       size_t count;
+       int sum;
+};
 
 struct metric_event *metricgroup__lookup(struct rblist *metric_events,
                                         struct evsel *evsel,
tools/perf/util/stat.h +0 -0
index b987960df3c5..2387c6289f93 100644

tools/perf/util/stat.h struct perf_stat_config { · line 116

Quoted 6 lines
tools/perf/util/stat.h +3 -0
@@ -111,6 +111,9 @@ 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                      tpebs_pipe;

Did you use these?

Quoted 6 lines
tools/perf/util/stat.h +0 -1
        int                      ctl_fd;
        int                      ctl_fd_ack;
        bool                     ctl_fd_close;
--

2.43.0

#9 lore raw

RFC PATCH v3 3/6 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 |  1 +
 tools/perf/util/stat-shadow.c | 18 ++++++++++++++++++
 3 files changed, 20 insertions(+)
tools/perf/builtin-stat.c +1 -0
index 3890a579349e..3e5865572266 100644
@@ -792,6 +792,7 @@ static int process_sample_event(struct perf_tool *tool,
 		if (!strcmp(evname, t->name)) {
 			t->count += 1;
 			t->sum += sample->retire_lat;
+			t->val = t->count > 0 ? t->sum/t->count : 0;
 			break;
 		}
 	}
tools/perf/util/metricgroup.h +1 -0
index 1fa12cc3294e..08af0f447550 100644
@@ -77,6 +77,7 @@ struct tpebs_retire_lat {
 	const char *tpebs_name;
 	size_t count;
 	int sum;
+	double val;
 };
 
 struct metric_event *metricgroup__lookup(struct rblist *metric_events,
tools/perf/util/stat-shadow.c +18 -0
index 3466aa952442..c63ba52004fc 100644
@@ -355,6 +355,19 @@ 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, nd) {
+		ret = expr__add_id_val(pctx, strdup(t->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 +499,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
maintainer
#10 lore raw

Re: [RFC PATCH v3 3/6] perf stat: Add retire latency values into the expr_parse_ctx to prepare for final metric calculation

Quoted 18 lines · On Fri, Mar 1, 2024 at 4:11 PM <[email protected]> wrote:

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 |  1 +
 tools/perf/util/stat-shadow.c | 18 ++++++++++++++++++
 3 files changed, 20 insertions(+)
tools/perf/builtin-stat.c +0 -0
index 3890a579349e..3e5865572266 100644

tools/perf/builtin-stat.c static int process_sample_event(struct perf_tool *tool, · line 795

Quoted 4 lines
tools/perf/builtin-stat.c +1 -0
@@ -792,6 +792,7 @@ static int process_sample_event(struct perf_tool *tool,
                if (!strcmp(evname, t->name)) {
                        t->count += 1;
                        t->sum += sample->retire_lat;
+                       t->val = t->count > 0 ? t->sum/t->count : 0;

You already increase the count, then it cannot be 0, right?

Quoted 20 lines
tools/perf/builtin-stat.c +0 -0
                        break;
                }
        }
tools/perf/util/metricgroup.h +1 -0
index 1fa12cc3294e..08af0f447550 100644
@@ -77,6 +77,7 @@ struct tpebs_retire_lat {
        const char *tpebs_name;
        size_t count;
        int sum;
+       double val;
 };
 
 struct metric_event *metricgroup__lookup(struct rblist *metric_events,
tools/perf/util/stat-shadow.c +0 -0
index 3466aa952442..c63ba52004fc 100644

tools/perf/util/stat-shadow.c static void print_nsecs(struct perf_stat_config *config, · line 362

Quoted 8 lines
tools/perf/util/stat-shadow.c +5 -0
@@ -355,6 +355,19 @@ 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;

A newline please.

Thanks,
Namhyung

Quoted 26 lines
tools/perf/util/stat-shadow.c +13 -1
+       list_for_each_entry(t, retire_lats, nd) {
+               ret = expr__add_id_val(pctx, strdup(t->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 +499,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);
--

2.43.0

#11 lore raw

RFC PATCH v3 4/6 perf stat: Create another thread for sample data processing

In reply to [email protected]

From: Weilin Wang <[email protected]>

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

---
 tools/perf/builtin-stat.c | 58 +++++++++++++++++++++++----------------
 1 file changed, 34 insertions(+), 24 deletions(-)
tools/perf/builtin-stat.c +34 -24
index 3e5865572266..2372175d3408 100644
@@ -777,7 +777,6 @@ static int process_sample_event(struct perf_tool *tool,
 	const char *evname;
 	struct tpebs_retire_lat *t;
 
-	pr_debug("entering function %s\n ", __func__);
 	evname = evsel__name(evsel);
 
 	pr_debug("[%03d] ", sample->cpu);
@@ -808,9 +807,9 @@ static int process_feature_event(struct perf_session *session,
 	return 0;
 }
 
-static int __cmd_script(struct child_process *cmd __maybe_unused)
+static void *__cmd_script(void *arg __maybe_unused)
 {
-	int err = 0;
+	struct child_process *cmd = arg;
 	struct perf_session *session;
 	struct perf_data data = {
 		.mode = PERF_DATA_MODE_READ,
@@ -826,29 +825,15 @@ static int __cmd_script(struct child_process *cmd __maybe_unused)
 		.attr		 = perf_event__process_attr,
 		},
 	};
-	struct tpebs_event *e;
-
-	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->name = strdup(e->name);
-		new->tpebs_name = strdup(e->tpebs_name);
-		new->count = 0;
-		new->sum = 0;
-		list_add_tail(&new->nd, &stat_config.tpebs_results);
-	}
 
-	kill(cmd->pid, SIGTERM);
 	session = perf_session__new(&data, &script.tool);
 	if (IS_ERR(session))
-		return PTR_ERR(session);
+		return NULL;
 	script.session = session;
-	err = perf_session__process_events(session);
+	perf_session__process_events(session);
 	perf_session__delete(session);
 
-	return err;
+	return NULL;
 }
 
 static int __run_perf_stat(int argc, const char **argv, int run_idx)
@@ -868,15 +853,37 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
 	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;
 		pr_debug("perf stat pid = %d\n", getpid());
 		ret = prepare_perf_record(&cmd);
 		if (ret)
 			return ret;
+
+		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->name = strdup(e->name);
+			new->tpebs_name = strdup(e->tpebs_name);
+			new->count = 0;
+			new->sum = 0;
+			list_add_tail(&new->nd, &stat_config.tpebs_results);
+		}
+
+		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;
+		}
+		sleep(2);
 	}
 
 	if (forks) {
@@ -1087,12 +1094,15 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
 	if (stat_config.tpebs_event_size > 0) {
 		int ret;
 
-		pr_debug("pid = %d\n", getpid());
-		pr_debug("cmd.pid = %d\n", cmd.pid);
+		pr_debug("Workload finished, finishing record\n");
+		pr_debug("Perf stat pid = %d, Perf record pid = %d\n", getpid(), cmd.pid);
 
-		ret = __cmd_script(&cmd);
+		kill(cmd.pid, SIGTERM);
+		pthread_join(thread_script, NULL);
 		close(cmd.out);
-		pr_debug("%d\n", ret);
+		ret = finish_command(&cmd);
+		if (ret != -ERR_RUN_COMMAND_WAITPID_SIGNAL)
+			return ret;
 	}
 
 	if (stat_config.walltime_run_table)
Signature · 1 line
2.43.0
maintainer
#12 lore raw

Re: [RFC PATCH v3 4/6] perf stat: Create another thread for sample data processing

Quoted 79 lines · On Fri, Mar 1, 2024 at 4:11 PM <[email protected]> wrote:

From: Weilin Wang <[email protected]>

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

---
 tools/perf/builtin-stat.c | 58 +++++++++++++++++++++++----------------
 1 file changed, 34 insertions(+), 24 deletions(-)
tools/perf/builtin-stat.c +7 -20
index 3e5865572266..2372175d3408 100644
@@ -777,7 +777,6 @@ static int process_sample_event(struct perf_tool *tool,
        const char *evname;
        struct tpebs_retire_lat *t;
 
-       pr_debug("entering function %s\n ", __func__);
        evname = evsel__name(evsel);
 
        pr_debug("[%03d] ", sample->cpu);
@@ -808,9 +807,9 @@ static int process_feature_event(struct perf_session *session,
        return 0;
 }
 
-static int __cmd_script(struct child_process *cmd __maybe_unused)
+static void *__cmd_script(void *arg __maybe_unused)
 {
-       int err = 0;
+       struct child_process *cmd = arg;
        struct perf_session *session;
        struct perf_data data = {
                .mode = PERF_DATA_MODE_READ,
@@ -826,29 +825,15 @@ static int __cmd_script(struct child_process *cmd __maybe_unused)
                .attr            = perf_event__process_attr,
                },
        };
-       struct tpebs_event *e;
-
-       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->name = strdup(e->name);
-               new->tpebs_name = strdup(e->tpebs_name);
-               new->count = 0;
-               new->sum = 0;
-               list_add_tail(&new->nd, &stat_config.tpebs_results);
-       }
 
-       kill(cmd->pid, SIGTERM);
        session = perf_session__new(&data, &script.tool);
        if (IS_ERR(session))
-               return PTR_ERR(session);
+               return NULL;
        script.session = session;
-       err = perf_session__process_events(session);
+       perf_session__process_events(session);
        perf_session__delete(session);
 
-       return err;
+       return NULL;
 }
 
 static int __run_perf_stat(int argc, const char **argv, int run_idx)
@@ -868,15 +853,37 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
        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 872

Quoted 10 lines
tools/perf/builtin-stat.c +6 -0
                pr_debug("perf stat pid = %d\n", getpid());
                ret = prepare_perf_record(&cmd);
                if (ret)
                        return ret;
+
+               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;

This would leave the record command. I think you'd better do this before prepare_perf_record().

Quoted 4 lines
tools/perf/builtin-stat.c +4 -0
+                       new->name = strdup(e->name);
+                       new->tpebs_name = strdup(e->tpebs_name);
+                       new->count = 0;
+                       new->sum = 0;

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

Quoted 10 lines
tools/perf/builtin-stat.c +10 -0
+                       list_add_tail(&new->nd, &stat_config.tpebs_results);
+               }
+
+               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;
+               }
+               sleep(2);

Why?

Thanks,
Namhyung

Quoted 26 lines
tools/perf/builtin-stat.c +7 -5
        }
 
        if (forks) {
@@ -1087,12 +1094,15 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
        if (stat_config.tpebs_event_size > 0) {
                int ret;
 
-               pr_debug("pid = %d\n", getpid());
-               pr_debug("cmd.pid = %d\n", cmd.pid);
+               pr_debug("Workload finished, finishing record\n");
+               pr_debug("Perf stat pid = %d, Perf record pid = %d\n", getpid(), cmd.pid);
 
-               ret = __cmd_script(&cmd);
+               kill(cmd.pid, SIGTERM);
+               pthread_join(thread_script, NULL);
                close(cmd.out);
-               pr_debug("%d\n", ret);
+               ret = finish_command(&cmd);
+               if (ret != -ERR_RUN_COMMAND_WAITPID_SIGNAL)
+                       return ret;
        }
 
        if (stat_config.walltime_run_table)
--

2.43.0

#13 lore raw

RE: [RFC PATCH v3 4/6] perf stat: Create another thread for sample data processing

Quoted 134 lines

-----Original Message-----
From: Namhyung Kim <[email protected]>
Sent: Monday, March 11, 2024 11:48 PM
To: Wang, Weilin <[email protected]>
Cc: Ian Rogers <[email protected]>; Peter Zijlstra <[email protected]>; Ingo Molnar <[email protected]>; Arnaldo Carvalho de Melo <[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 v3 4/6] perf stat: Create another thread for sample data processing

tools/perf/builtin-stat.c static int process_sample_event(struct perf_tool

Quoted 16 lines · On Fri, Mar 1, 2024 at 4:11 PM <[email protected]> wrote:

From: Weilin Wang <[email protected]>

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

---
 tools/perf/builtin-stat.c | 58 +++++++++++++++++++++++----------------
 1 file changed, 34 insertions(+), 24 deletions(-)
tools/perf/builtin-stat.c +0 -0
index 3e5865572266..2372175d3408 100644
@@ -777,7 +777,6 @@ static int process_sample_event(struct perf_tool

*tool,

tools/perf/builtin-stat.c static int process_feature_event(struct perf_session

Quoted 8 lines
tools/perf/builtin-stat.c +0 -1
        const char *evname;
        struct tpebs_retire_lat *t;
 
-       pr_debug("entering function %s\n ", __func__);
        evname = evsel__name(evsel);
 
        pr_debug("[%03d] ", sample->cpu);
@@ -808,9 +807,9 @@ static int process_feature_event(struct perf_session

*session,

tools/perf/builtin-stat.c static int __cmd_script(struct child_process *cmd

Quoted 12 lines
tools/perf/builtin-stat.c +2 -2
        return 0;
 }
 
-static int __cmd_script(struct child_process *cmd __maybe_unused)
+static void *__cmd_script(void *arg __maybe_unused)
 {
-       int err = 0;
+       struct child_process *cmd = arg;
        struct perf_session *session;
        struct perf_data data = {
                .mode = PERF_DATA_MODE_READ,
@@ -826,29 +825,15 @@ static int __cmd_script(struct child_process *cmd

__maybe_unused)

tools/perf/builtin-stat.c static int __cmd_script(struct child_process *cmd __maybe_unused)

Quoted 7 lines
tools/perf/builtin-stat.c +0 -4
                .attr            = perf_event__process_attr,
                },
        };
-       struct tpebs_event *e;
-
-       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

Quoted 26 lines
tools/perf/builtin-stat.c +3 -13
-
-               if (!new)
-                       return -1;
-               new->name = strdup(e->name);
-               new->tpebs_name = strdup(e->tpebs_name);
-               new->count = 0;
-               new->sum = 0;
-               list_add_tail(&new->nd, &stat_config.tpebs_results);
-       }
 
-       kill(cmd->pid, SIGTERM);
        session = perf_session__new(&data, &script.tool);
        if (IS_ERR(session))
-               return PTR_ERR(session);
+               return NULL;
        script.session = session;
-       err = perf_session__process_events(session);
+       perf_session__process_events(session);
        perf_session__delete(session);
 
-       return err;
+       return NULL;
 }
 
 static int __run_perf_stat(int argc, const char **argv, int run_idx)
@@ -868,15 +853,37 @@ static int __run_perf_stat(int argc, const char

**argv, int run_idx)

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

Quoted 6 lines
tools/perf/builtin-stat.c +1 -0
        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

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 +4 -0
        if (stat_config.tpebs_event_size > 0) {
                int ret;
 
+               struct tpebs_event *e;
                pr_debug("perf stat pid = %d\n", getpid());
                ret = prepare_perf_record(&cmd);
                if (ret)
                        return ret;
+
+               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 872

Quoted 3 lines
tools/perf/builtin-stat.c +3 -0
+
+                       if (!new)
+                               return -1;

This would leave the record command. I think you'd better do this before prepare_perf_record().

Quoted 4 lines
tools/perf/builtin-stat.c +4 -0
+                       new->name = strdup(e->name);
+                       new->tpebs_name = strdup(e->tpebs_name);
+                       new->count = 0;
+                       new->sum = 0;

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

Quoted 10 lines
tools/perf/builtin-stat.c +10 -0
+                       list_add_tail(&new->nd, &stat_config.tpebs_results);
+               }
+
+               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;
+               }
+               sleep(2);
Quoted 1 line

Why?

In my experiments, I found that perf record initialization time is usually longer than the time needed in perf stat, especially when we start a lot of initialization of perf stat before perf record already. Without a waiting time here, perf record may not have enough time to launch and collect data if the execution time of the parent process (perf stat) is very short (a few seconds). Therefore, I added a sleep here to wait for perf record as a quick but ad hoc solution here. The two second time is selected based on my experiment data. I mentioned this as a remaining issue in the cover letter of patch v1.

I will add a comment here. Please let me know if you have any suggestions on how To solve this issue. Thanks a lot!

Quoted 33 lines

Thanks,
Namhyung

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) {
@@ -1087,12 +1094,15 @@ static int __run_perf_stat(int argc, const char

**argv, int run_idx)

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 -2
        if (stat_config.tpebs_event_size > 0) {
                int ret;
 
-               pr_debug("pid = %d\n", getpid());
-               pr_debug("cmd.pid = %d\n", cmd.pid);
+               pr_debug("Workload finished, finishing record\n");
+               pr_debug("Perf stat pid = %d, Perf record pid = %d\n", getpid(),

cmd.pid);

Quoted 15 lines
tools/perf/builtin-stat.c +5 -3
-               ret = __cmd_script(&cmd);
+               kill(cmd.pid, SIGTERM);
+               pthread_join(thread_script, NULL);
                close(cmd.out);
-               pr_debug("%d\n", ret);
+               ret = finish_command(&cmd);
+               if (ret != -ERR_RUN_COMMAND_WAITPID_SIGNAL)
+                       return ret;
        }
 
        if (stat_config.walltime_run_table)
--

2.43.0

#14 lore raw

RFC PATCH v3 5/6 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.

---
 tools/perf/util/stat-display.c | 65 ++++++++++++++++++++++++++++++++++
 1 file changed, 65 insertions(+)
tools/perf/util/stat-display.c +65 -0
index bfc1d705f437..e2996ba67bfe 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, nd) {
+		if (newline)
+			do_new_line_std(config, os);
+		fprintf(out, "%'*.2f %-*s", COUNTS_LEN, t->val, EVNAME_LEN, t->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, nd) {
+		fprintf(out, "%f%s%s%s%s%ld%s%d\n", t->val, sep, sep, t->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, nd) {
+		fprintf(out, "\"retire_latency-value\" : \"%f\", ", t->val);
+		fprintf(out, "\"event-name\" : \"%s\"", t->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
#15 lore raw

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

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

From: Weilin Wang <[email protected]>

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

Can you please put some example output here?

Thanks,
Namhyung

Quoted 106 lines
---
 tools/perf/util/stat-display.c | 65 ++++++++++++++++++++++++++++++++++
 1 file changed, 65 insertions(+)
tools/perf/util/stat-display.c +65 -1
index bfc1d705f437..e2996ba67bfe 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, nd) {
+               if (newline)
+                       do_new_line_std(config, os);
+               fprintf(out, "%'*.2f %-*s", COUNTS_LEN, t->val, EVNAME_LEN, t->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, nd) {
+               fprintf(out, "%f%s%s%s%s%ld%s%d\n", t->val, sep, sep, t->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, nd) {
+               fprintf(out, "\"retire_latency-value\" : \"%f\", ", t->val);
+               fprintf(out, "\"event-name\" : \"%s\"", t->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

#16 lore raw

RFC PATCH v3 6/6 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      | 2547 +++++++++++++++++
 2 files changed, 2674 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 +2547 -0

2,547 lines not on this page · raw message

Signature · 1 line
2.43.0