Skip to contentSkip to conversation

TPEBS counting mode support

RFC PATCH Superseded [email protected] 2 participants 16 messages Jun 5, 2024 – Jun 10, 2024 perf events

v11 of 18 Superseded Latest is v18 →

lore
Share & tools
lore
#0 lore raw

v11 cover TPEBS counting mode support

From: Weilin Wang <[email protected]>

Changes in v11:
- Make retire_latency evsels not opened for counting. This works correctly now with the code Namhyung suggested that adjusting group read size when retire_latency evsels included in the group.
- Update retire_latency value assignment using rint() to reduce precision loss while keeping code generic.
- Fix the build error caused by not used variable in the test.

Other changes in v10:
- Change perf record fork from perf stat to evsel. All the major operations like tpebs start, stop, read_evsel should directly work through evsel.
- Make intel-tpebs x86_64 only. This change is cross-compiled to arm64.
- Put tpebs code to intel-tepbs and simplify intel-tpebs APIs to minimum number
of functions and variables. Update funtion name and variable names to use consistent prefix. Also improve error handling.
- Integrate code patch from Ian for the :R parser.
- Update MTL metrics to TMA 4.8.

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

Changes in v9:
- Update the retire_latency result print and metric calculation method. Plugin
the value to evsel so that no special code is required.
- Update --control:fifo to use pipe instead of named pipe.
- Add test for TPEBS counting mode.
- Update Document with more details.

Changes in v8:
- In this revision, the code is updated to base on Ian's patch on R modifier
parser https://lore.kernel.org/lkml/[email protected]/
After this change, there is no special code required for R modifier in metricgroup.c and metricgroup.h files.

Caveat of this change:
Ideally, we will need to add special handling to skip counting events with R
modifier in evsel. Currently, this is not implemented so the event with :R will be both counted and sampled. Usually, in a metric formula that uses retire_latency, it would already require to count the event. As a result, we will endup count the same event twice. This should be able to be handled properly when we finalize our design on evsel R modifier support.

- Move TPEBS specific code out from main perf stat code to separate files in
util/intel-tpebs.c and util/intel-tpebs.h. [Namhyung]
- Use --control:fifo to ack perf stat from forked perf record instead of sleep(2) [Namhyung]
- Add introductions about TPEBS and R modifier in Documents. [Namhyung]

Changes in v7:
- Update code and comments for better code quality [Namhyung]
- Add a separate commit for perf data [Namhyung]
- Update retire latency print function to improve alignment [Namhyung]

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

Changes in v5:
- Update code and add comments for better code quality [Ian]

Changes in v4:
- Remove uncessary debug print and update code and comments for better
readability and quality [Namhyung]
- Update mtl metric json file with consistent TmaL1 and TopdownL1 metricgroup

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

Changes in v2:
- Add MTL metric file
- Add more descriptions and example to the patch [Arnaldo]

Here is an example of running perf stat to collect a metric that uses retire_latency value of event MEM_INST_RETIRED.STLB_HIT_STORES on a MTL system.

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

./perf stat -M tma_dtlb_store -a -- sleep 1

[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.000 MB - ]

Performance counter stats for 'system wide':

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

0.00 MEM_INST_RETIRED.STLB_HIT_STORES:p 0 0

1.003105924 seconds time elapsed

v1:
TPEBS is one of the features provided by the next generation of Intel PMU. Please refer to Section 8.4.1 of "Intel® Architecture Instruction Set Extensions Programming Reference" [1] for more details about this feature.

This set of patches supports TPEBS in counting mode. The code works in the following way: it forks a perf record process from perf stat when retire_latency of one or more events are used in a metric formula. Perf stat would send a SIGTERM signal to perf record before it needs the retire latency value for metric calculation. Perf stat will then process sample data to extract the retire latency data for metric calculations. Currently, the code uses the arithmetic average of retire latency values.

[1] https://www.intel.com/content/www/us/en/content-details/812218/intel-architecture-instruction-set-extensions-programming-reference.html?wapkw=future%20features

Ian Rogers (1):
perf parse-events: Add a retirement latency modifier

Weilin Wang (7):
perf data: Allow to use given fd in data->file.fd
perf stat: Fork and launch perf record when perf stat needs to get
retire latency value for a metric.
perf stat: Plugin retire_lat value from sampled data to evsel perf vendor events intel: Add MTL metric json files perf stat: Add command line option for enabling tpebs recording perf Document: Add TPEBS to Documents
perf test: Add test for Intel TPEBS counting mode

 tools/perf/Documentation/perf-list.txt        |    1 +
 tools/perf/Documentation/topdown.txt          |   30 +
 tools/perf/arch/x86/util/evlist.c             |    6 +
 tools/perf/builtin-stat.c                     |    8 +
 .../arch/x86/meteorlake/metricgroups.json     |  140 +
 .../arch/x86/meteorlake/mtl-metrics.json      | 2595 +++++++++++++++++
 .../perf/tests/shell/test_stat_intel_tpebs.sh |   18 +
 tools/perf/util/Build                         |    1 +
 tools/perf/util/data.c                        |    7 +-
 tools/perf/util/evsel.c                       |   81 +-
 tools/perf/util/evsel.h                       |    6 +
 tools/perf/util/intel-tpebs.c                 |  397 +++
 tools/perf/util/intel-tpebs.h                 |   48 +
 tools/perf/util/parse-events.c                |    2 +
 tools/perf/util/parse-events.h                |    1 +
 tools/perf/util/parse-events.l                |    3 +-
 16 files changed, 3340 insertions(+), 4 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
 create mode 100755 tools/perf/tests/shell/test_stat_intel_tpebs.sh
 create mode 100644 tools/perf/util/intel-tpebs.c
 create mode 100644 tools/perf/util/intel-tpebs.h

--
2.43.0

#1 lore raw

RFC PATCH v11 1/8 perf parse-events: Add a retirement latency modifier

From: Ian Rogers <[email protected]>

Retirement latency is a separate sampled count used on newer Intel CPUs.

---
 tools/perf/util/evsel.h        | 1 +
 tools/perf/util/parse-events.c | 2 ++
 tools/perf/util/parse-events.h | 1 +
 tools/perf/util/parse-events.l | 3 ++-
 4 files changed, 6 insertions(+), 1 deletion(-)
tools/perf/util/evsel.h +1 -0
index 43f6fd1dcb4d..bd8e84954e34 100644
@@ -98,6 +98,7 @@ struct evsel {
 		bool			bpf_counter;
 		bool			use_config_name;
 		bool			skippable;
+		bool			retire_lat;
 		int			bpf_fd;
 		struct bpf_object	*bpf_obj;
 		struct list_head	config_terms;
tools/perf/util/parse-events.c +2 -0
index 0f308b4db2b9..9c2a76ec8c99 100644
@@ -1818,6 +1818,8 @@ static int parse_events__modifier_list(struct parse_events_state *parse_state,
 			evsel->weak_group = true;
 		if (mod.bpf)
 			evsel->bpf_counter = true;
+		if (mod.retire_lat)
+			evsel->retire_lat = true;
 	}
 	return 0;
 }
tools/perf/util/parse-events.h +1 -0
index 5695308efab9..eb94d1247dae 100644
@@ -201,6 +201,7 @@ struct parse_events_modifier {
 	bool hypervisor : 1;	/* 'h' */
 	bool guest : 1;		/* 'G' */
 	bool host : 1;		/* 'H' */
+	bool retire_lat : 1;	/* 'R' */
 };
 
 int parse_events__modifier_event(struct parse_events_state *parse_state, void *loc,
tools/perf/util/parse-events.l +2 -1
index 08ea2d845dc3..85015f080240 100644
@@ -209,6 +209,7 @@ static int modifiers(struct parse_events_state *parse_state, yyscan_t scanner)
 		CASE('W', weak);
 		CASE('e', exclusive);
 		CASE('b', bpf);
+		CASE('R', retire_lat);
 		default:
 			return PE_ERROR;
 		}
@@ -250,7 +251,7 @@ drv_cfg_term	[a-zA-Z0-9_\.]+(=[a-zA-Z0-9_*?\.:]+)?
  * If you add a modifier you need to update check_modifier().
  * Also, the letters in modifier_event must not be in modifier_bp.
  */
-modifier_event	[ukhpPGHSDIWeb]{1,15}
+modifier_event	[ukhpPGHSDIWebR]{1,16}
 modifier_bp	[rwx]{1,3}
 lc_type 	(L1-dcache|l1-d|l1d|L1-data|L1-icache|l1-i|l1i|L1-instruction|LLC|L2|dTLB|d-tlb|Data-TLB|iTLB|i-tlb|Instruction-TLB|branch|branches|bpu|btb|bpc|node)
 lc_op_result	(load|loads|read|store|stores|write|prefetch|prefetches|speculative-read|speculative-load|refs|Reference|ops|access|misses|miss)
Signature · 1 line
2.43.0
#2 lore raw

RFC PATCH v11 2/8 perf data: Allow to use given fd in data->file.fd

In reply to [email protected]

From: Weilin Wang <[email protected]>

When in PIPE mode, allow to use fd dynamically opened and asigned to data->file.fd instead of STDIN_FILENO or STDOUT_FILENO.

---
 tools/perf/util/data.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
tools/perf/util/data.c +6 -1
index 08c4bfbd817f..98661ede2a73 100644
@@ -204,7 +204,12 @@ static bool check_pipe(struct perf_data *data)
 				data->file.fd = fd;
 				data->use_stdio = false;
 			}
-		} else {
+
+		/*
+		 * When is_pipe and data->file.fd is given, use given fd
+		 * instead of STDIN_FILENO or STDOUT_FILENO
+		 */
+		} else if (data->file.fd <= 0) {
 			data->file.fd = fd;
 		}
 	}
Signature · 1 line
2.43.0
#3 lore raw

RFC PATCH v11 3/8 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, evsel 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, evsel would stop perf record by sending sigterm signal to perf record process. Sampled data will be process to get retire latency value. Another thread is required to synchronize between perf stat and perf record when we pass data through pipe.

Retire_latency evsel is not opened for perf stat so that there is no counter wasted on it. This commit includes code suggested by Namhyung to adjust reading size for groups that include retire_latency evsels.

---
 tools/perf/builtin-stat.c     |   6 +
 tools/perf/util/Build         |   1 +
 tools/perf/util/evsel.c       |  66 +++++-
 tools/perf/util/intel-tpebs.c | 397 ++++++++++++++++++++++++++++++++++
 tools/perf/util/intel-tpebs.h |  48 ++++
 5 files changed, 516 insertions(+), 2 deletions(-)
 create mode 100644 tools/perf/util/intel-tpebs.c
 create mode 100644 tools/perf/util/intel-tpebs.h
tools/perf/builtin-stat.c +6 -0
index 428e9721b908..b09cb2c6e9c2 100644
@@ -70,6 +70,7 @@
 #include "util/bpf_counter.h"
 #include "util/iostat.h"
 #include "util/util.h"
+#include "util/intel-tpebs.h"
 #include "asm/bug.h"
 
 #include <linux/time64.h>
@@ -653,6 +654,9 @@ static enum counter_recovery stat_handle_error(struct evsel *counter)
 
 	if (child_pid != -1)
 		kill(child_pid, SIGTERM);
+
+	tpebs_stop_delete();
+
 	return COUNTER_FATAL;
 }
 
@@ -985,6 +989,8 @@ static void sig_atexit(void)
 	if (child_pid != -1)
 		kill(child_pid, SIGTERM);
 
+	tpebs_stop();
+
 	sigprocmask(SIG_SETMASK, &oset, NULL);
 
 	if (signr == -1)
tools/perf/util/Build +1 -0
index 292170a99ab6..79adf39e0d8f 100644
@@ -153,6 +153,7 @@ perf-y += clockid.o
 perf-y += list_sort.o
 perf-y += mutex.o
 perf-y += sharded_mutex.o
+perf-$(CONFIG_X86_64) += intel-tpebs.o
 
 perf-$(CONFIG_LIBBPF) += bpf_map.o
 perf-$(CONFIG_PERF_BPF_SKEL) += bpf_counter.o
tools/perf/util/evsel.c +64 -2

102 lines not on this page · raw message

tools/perf/util/intel-tpebs.c new file +397 -0

397 lines not on this page · raw message

tools/perf/util/intel-tpebs.h new file +48 -0

48 lines not on this page · raw message

Signature · 1 line
2.43.0
maintainer
#4 lore raw

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

Hello,

Quoted 153 lines · On Wed, Jun 05, 2024 at 01:21:44AM -0400, [email protected] wrote:

From: Weilin Wang <[email protected]>

When retire_latency value is used in a metric formula, evsel 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, evsel would stop perf record by sending sigterm signal to perf record process. Sampled data will be process to get retire latency value. Another thread is required to synchronize between perf stat and perf record when we pass data through pipe.

Retire_latency evsel is not opened for perf stat so that there is no counter wasted on it. This commit includes code suggested by Namhyung to adjust reading size for groups that include retire_latency evsels.

---
 tools/perf/builtin-stat.c     |   6 +
 tools/perf/util/Build         |   1 +
 tools/perf/util/evsel.c       |  66 +++++-
 tools/perf/util/intel-tpebs.c | 397 ++++++++++++++++++++++++++++++++++
 tools/perf/util/intel-tpebs.h |  48 ++++
 5 files changed, 516 insertions(+), 2 deletions(-)
 create mode 100644 tools/perf/util/intel-tpebs.c
 create mode 100644 tools/perf/util/intel-tpebs.h
tools/perf/builtin-stat.c +6 -0
index 428e9721b908..b09cb2c6e9c2 100644
@@ -70,6 +70,7 @@
 #include "util/bpf_counter.h"
 #include "util/iostat.h"
 #include "util/util.h"
+#include "util/intel-tpebs.h"
 #include "asm/bug.h"
 
 #include <linux/time64.h>
@@ -653,6 +654,9 @@ static enum counter_recovery stat_handle_error(struct evsel *counter)
 
 	if (child_pid != -1)
 		kill(child_pid, SIGTERM);
+
+	tpebs_stop_delete();
+
 	return COUNTER_FATAL;
 }
 
@@ -985,6 +989,8 @@ static void sig_atexit(void)
 	if (child_pid != -1)
 		kill(child_pid, SIGTERM);
 
+	tpebs_stop();
+
 	sigprocmask(SIG_SETMASK, &oset, NULL);
 
 	if (signr == -1)
tools/perf/util/Build +1 -0
index 292170a99ab6..79adf39e0d8f 100644
@@ -153,6 +153,7 @@ perf-y += clockid.o
 perf-y += list_sort.o
 perf-y += mutex.o
 perf-y += sharded_mutex.o
+perf-$(CONFIG_X86_64) += intel-tpebs.o
 
 perf-$(CONFIG_LIBBPF) += bpf_map.o
 perf-$(CONFIG_PERF_BPF_SKEL) += bpf_counter.o
tools/perf/util/evsel.c +56 -2
index a0a8aee7d6b9..bd3627819afe 100644
@@ -1538,6 +1538,60 @@ static void evsel__set_count(struct evsel *counter, int cpu_map_idx, int thread,
 	perf_counts__set_loaded(counter->counts, cpu_map_idx, thread, true);
 }
 
+static bool evsel__group_has_tpebs(struct evsel *leader)
+{
+	struct evsel *evsel;
+
+	for_each_group_evsel(evsel, leader) {
+		if (evsel__is_retire_lat(evsel))
+			return true;
+	}
+	return false;
+}
+
+static u64 evsel__group_read_nr_members(struct evsel *leader)
+{
+	u64 nr = leader->core.nr_members;
+	struct evsel *evsel;
+
+	for_each_group_evsel(evsel, leader) {
+		if (evsel__is_retire_lat(evsel))
+			nr--;
+	}
+	return nr;
+}
+
+static u64 evsel__group_read_size(struct evsel *leader)
+{
+	u64 read_format = leader->core.attr.read_format;
+	int entry = sizeof(u64); /* value */
+	int size = 0;
+	int nr = 1;
+
+	if (!evsel__group_has_tpebs(leader))
+		return perf_evsel__read_size(&leader->core);
+
+	if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
+		size += sizeof(u64);
+
+	if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
+		size += sizeof(u64);
+
+	if (read_format & PERF_FORMAT_ID)
+		entry += sizeof(u64);
+
+	if (read_format & PERF_FORMAT_LOST)
+		entry += sizeof(u64);
+
+	if (read_format & PERF_FORMAT_GROUP) {
+		nr = evsel__group_read_nr_members(leader);
+		size += sizeof(u64);
+	}
+
+	size += entry * nr;
+	return size;
+}
+
 static int evsel__process_group_data(struct evsel *leader, int cpu_map_idx, int thread, u64 *data)
 {
 	u64 read_format = leader->core.attr.read_format;
@@ -1546,7 +1600,7 @@ static int evsel__process_group_data(struct evsel *leader, int cpu_map_idx, int
 
 	nr = *data++;
 
-	if (nr != (u64) leader->core.nr_members)
+	if (nr != evsel__group_read_nr_members(leader))
 		return -EINVAL;
 
 	if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
@@ -1576,7 +1630,7 @@ static int evsel__read_group(struct evsel *leader, int cpu_map_idx, int thread)
 {
 	struct perf_stat_evsel *ps = leader->stats;
 	u64 read_format = leader->core.attr.read_format;
-	int size = perf_evsel__read_size(&leader->core);
+	int size = evsel__group_read_size(leader);
 	u64 *data = ps->group_data;
 
 	if (!(read_format & PERF_FORMAT_ID))

tools/perf/util/evsel.c static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus, · line 2244

Quoted 5 lines
tools/perf/util/evsel.c +2 -0
@@ -2186,6 +2240,9 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 		return 0;
 	}
 
+	if (evsel__is_retire_lat(evsel))
+		return tpebs_start(evsel->evlist, cpus);

As it works with evlist, I think it's better to put this code there. But it seems perf stat doesn't call the evlist API for open, then we can add this to somewhere in __run_perf_stat() directly.

tools/perf/util/evsel.c int evsel__open(struct evsel *evsel, struct perf_cpu_map *cpus, · line 2437

Quoted 10 lines
tools/perf/util/evsel.c +3 -0
+
 	err = __evsel__prepare_open(evsel, cpus, threads);
 	if (err)
 		return err;
@@ -2376,6 +2433,8 @@ int evsel__open(struct evsel *evsel, struct perf_cpu_map *cpus,
 
 void evsel__close(struct evsel *evsel)
 {
+	if (evsel__is_retire_lat(evsel))
+		tpebs_delete();

Ditto.

Quoted 43 lines
tools/perf/util/evsel.c +3 -0
 	perf_evsel__close(&evsel->core);
 	perf_evsel__free_id(&evsel->core);
 }
@@ -3341,6 +3400,9 @@ static int store_evsel_ids(struct evsel *evsel, struct evlist *evlist)
 {
 	int cpu_map_idx, thread;
 
+	if (evsel__is_retire_lat(evsel))
+		return 0;
+
 	for (cpu_map_idx = 0; cpu_map_idx < xyarray__max_x(evsel->core.fd); cpu_map_idx++) {
 		for (thread = 0; thread < xyarray__max_y(evsel->core.fd);
 		     thread++) {
tools/perf/util/intel-tpebs.c new file +24 -0
new file mode 100644
index 000000000000..37b7a4f92dd9
@@ -0,0 +1,397 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * intel_tpebs.c: Intel TPEBS support
+ */
+
+
+#include <sys/param.h>
+#include <subcmd/run-command.h>
+#include <thread.h>
+#include "intel-tpebs.h"
+#include <linux/list.h>
+#include <linux/zalloc.h>
+#include <linux/err.h>
+#include "sample.h"
+#include "debug.h"
+#include "evlist.h"
+#include "evsel.h"
+#include "session.h"
+#include "tool.h"
+#include "cpumap.h"
+#include "metricgroup.h"
+#include <sys/stat.h>
+#include <sys/file.h>
+#include <poll.h>

tools/perf/util/intel-tpebs.c line 34

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+#include <math.h>
+
+#define PERF_DATA		"-"
+
+bool tpebs_recording;
+static pid_t tpebs_pid = -1;
+static size_t tpebs_event_size;
+static pthread_t tpebs_reader_thread;
+static struct child_process *tpebs_cmd;
+static struct list_head tpebs_results = LIST_HEAD_INIT(tpebs_results);

It can be 'static LIST_HEAD(tpebs_results);'

Quoted 30 lines
tools/perf/util/intel-tpebs.c new file +30 -0
+
+struct tpebs_retire_lat {
+	struct list_head nd;
+	/* Event name */
+	const char *name;
+	/* Event name with the TPEBS modifier R */
+	const char *tpebs_name;
+	/* Count of retire_latency values found in sample data */
+	size_t count;
+	/* Sum of all the retire_latency values in sample data */
+	int sum;
+	/* Average of retire_latency, val = sum / count */
+	double val;
+};
+
+static int get_perf_record_args(const char **record_argv, char buf[],
+				const char *cpumap_buf)
+{
+	struct tpebs_retire_lat *e;
+	int i = 0;
+
+	pr_debug("Prepare perf record for retire_latency\n");
+
+	record_argv[i++] = "perf";
+	record_argv[i++] = "record";
+	record_argv[i++] = "-W";
+	record_argv[i++] = "--synth=no";
+	record_argv[i++] = buf;
+
+	if (cpumap_buf) {

tools/perf/util/intel-tpebs.c line 74

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+		record_argv[i++] = "-C";
+		record_argv[i++] = cpumap_buf;
+	}
+
+	record_argv[i++] = "-a";
+
+	if (!cpumap_buf) {
+		pr_err("Require cpumap list to run sampling.\n");
+		return -ECANCELED;
+	}

Hmm.. I thought you supported system wide collection, not sure if it has a cpumap. Anyway this check makes the earlier one meaningless - you need the cpumap always, right?

Quoted 114 lines
tools/perf/util/intel-tpebs.c new file +114 -0
+
+	list_for_each_entry(e, &tpebs_results, nd) {
+		record_argv[i++] = "-e";
+		record_argv[i++] = e->name;
+	}
+
+	record_argv[i++] = "-o";
+	record_argv[i++] = PERF_DATA;
+
+	return 0;
+}
+
+static int prepare_run_command(const char **argv)
+{
+	tpebs_cmd = zalloc(sizeof(struct child_process));
+	if (!tpebs_cmd)
+		return -ENOMEM;
+	tpebs_cmd->argv = argv;
+	tpebs_cmd->out = -1;
+	return 0;
+}
+
+static int start_perf_record(int control_fd[], int ack_fd[],
+				const char *cpumap_buf)
+{
+	const char **record_argv;
+	int ret;
+	char buf[32];
+
+	scnprintf(buf, sizeof(buf), "--control=fd:%d,%d", control_fd[0], ack_fd[1]);
+
+	record_argv = calloc(12 + 2 * tpebs_event_size, sizeof(char *));
+	if (!record_argv)
+		return -ENOMEM;
+
+	ret = get_perf_record_args(record_argv, buf, cpumap_buf);
+	if (ret)
+		goto out;
+
+	ret = prepare_run_command(record_argv);
+	if (ret)
+		goto out;
+	ret = start_command(tpebs_cmd);
+out:
+	free(record_argv);
+	return ret;
+}
+
+static int process_sample_event(struct perf_tool *tool __maybe_unused,
+				union perf_event *event __maybe_unused,
+				struct perf_sample *sample,
+				struct evsel *evsel,
+				struct machine *machine __maybe_unused)
+{
+	int ret = 0;
+	const char *evname;
+	struct tpebs_retire_lat *t;
+
+	evname = evsel__name(evsel);
+
+	/*
+	 * Need to handle per core results? We are assuming average retire
+	 * latency value will be used. Save the number of samples and the sum of
+	 * retire latency value for each event.
+	 */
+	list_for_each_entry(t, &tpebs_results, nd) {
+		if (!strcmp(evname, t->name)) {
+			t->count += 1;
+			t->sum += sample->retire_lat;
+			t->val = (double) t->sum / t->count;
+			break;
+		}
+	}
+
+	return ret;
+}
+
+static int process_feature_event(struct perf_session *session,
+				 union perf_event *event)
+{
+	if (event->feat.feat_id < HEADER_LAST_FEATURE)
+		return perf_event__process_feature(session, event);
+	return 0;
+}
+
+static void *__sample_reader(void *arg)
+{
+	struct child_process *child = arg;
+	struct perf_session *session;
+	struct perf_data data = {
+		.mode = PERF_DATA_MODE_READ,
+		.path = PERF_DATA,
+		.file.fd = child->out,
+	};
+	struct perf_tool tool = {
+		.sample = process_sample_event,
+		.feature = process_feature_event,
+		.attr = perf_event__process_attr,
+	};
+
+	session = perf_session__new(&data, &tool);
+	if (IS_ERR(session))
+		return NULL;
+	perf_session__process_events(session);
+	perf_session__delete(session);
+
+	return NULL;
+}
+
+int tpebs_start(struct evlist *evsel_list, struct perf_cpu_map *cpus)
+{
+	int ret = 0;
+	struct evsel *evsel;
+	char cpumap_buf[50];

tools/perf/util/intel-tpebs.c line 198

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+
+	/*
+	 * We should only run tpebs_start when tpebs_recording is enabled.
+	 * And we should only run it once with all the required events.
+	 */
+	if (tpebs_pid != -1 || !tpebs_recording)
+		return 0;
+
+	cpu_map__snprint(cpus, cpumap_buf, sizeof(cpumap_buf));
+	pr_debug("cpu map: %s\n", cpumap_buf);

Can you please remove unnecessary debug prints? If you really want it, then make it more meaningful like with more context.

tools/perf/util/intel-tpebs.c line 210

Quoted 12 lines
tools/perf/util/intel-tpebs.c new file +12 -0
+
+	/*
+	 * Prepare perf record for sampling event retire_latency before fork and
+	 * prepare workload
+	 */
+	evlist__for_each_entry(evsel_list, evsel) {
+		struct tpebs_retire_lat *new = zalloc(sizeof(*new));
+		char *name;
+		int i;
+
+		if (!evsel->retire_lat)
+			continue;

Please move the allocation after this.

tools/perf/util/intel-tpebs.c line 219

Quoted 9 lines
tools/perf/util/intel-tpebs.c new file +9 -0
+
+		pr_debug("perf stat retire latency of event %s required\n", evsel->name);
+		if (!new) {
+			ret = -1;
+			goto err;
+		}
+		for (i = strlen(evsel->name) - 1; i > 0; i--) {
+			if (evsel->name[i] == 'R')
+				break;

I think you also need to check ':' or '/'..

Quoted 7 lines
tools/perf/util/intel-tpebs.c new file +7 -0
+		}
+		if (i <= 0 || evsel->name[i] != 'R') {
+			ret = -1;
+			goto err;
+		}
+
+		name = strdup(evsel->name);

tools/perf/util/intel-tpebs.c line 236

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+		if (!name) {
+			ret = -ENOMEM;
+			goto err;
+		}
+		name[i] = 'p';
+		new->name = name;
+		new->tpebs_name = strdup(evsel->name);
+		if (!new->tpebs_name) {
+			ret = -ENOMEM;
+			goto err;

This error handling (including above) will leak 'new' because it's not linked to the list yet.

Quoted 80 lines
tools/perf/util/intel-tpebs.c new file +80 -0
+		}
+		list_add_tail(&new->nd, &tpebs_results);
+		tpebs_event_size += 1;
+	}
+
+	if (tpebs_event_size > 0) {
+		int control_fd[2], ack_fd[2], len;
+		char ack_buf[8];
+
+		/*Create control and ack fd for --control*/
+		if (pipe(control_fd) < 0) {
+			pr_err("Failed to create control fifo");
+			ret = -1;
+			goto out;
+		}
+		if (pipe(ack_fd) < 0) {
+			pr_err("Failed to create control fifo");
+			ret = -1;
+			goto out;
+		}
+
+		ret = start_perf_record(control_fd, ack_fd, cpumap_buf);
+		if (ret)
+			goto out;
+		tpebs_pid = tpebs_cmd->pid;
+		if (pthread_create(&tpebs_reader_thread, NULL, __sample_reader, tpebs_cmd)) {
+			kill(tpebs_cmd->pid, SIGTERM);
+			close(tpebs_cmd->out);
+			pr_err("Could not create thread to process sample data.\n");
+			ret = -1;
+			goto out;
+		}
+		/* Wait for perf record initialization.*/
+		len = strlen("enable");
+		ret = write(control_fd[1], "enable", len);
+		if (ret != len) {
+			pr_err("perf record control write control message failed\n");
+			goto out;
+		}
+
+		ret = read(ack_fd[0], ack_buf, sizeof(ack_buf));
+		if (ret > 0)
+			ret = strcmp(ack_buf, "ack\n");
+		else {
+			pr_err("perf record control ack failed\n");
+			goto out;
+		}
+		pr_debug("Received ack from perf record\n");
+out:
+		close(control_fd[0]);
+		close(control_fd[1]);
+		close(ack_fd[0]);
+		close(ack_fd[1]);
+	}
+err:
+	if (ret)
+		tpebs_delete();
+	return ret;
+}
+
+int tpebs_stop(void)
+{
+	int ret = 0;
+
+	/* Like tpebs_start, we should only run tpebs_end once. */
+	if (tpebs_pid != -1) {
+		kill(tpebs_cmd->pid, SIGTERM);
+		tpebs_pid = -1;
+		pthread_join(tpebs_reader_thread, NULL);
+		close(tpebs_cmd->out);
+		ret = finish_command(tpebs_cmd);
+		if (ret == -ERR_RUN_COMMAND_WAITPID_SIGNAL)
+			ret = 0;
+	}
+	return ret;
+}
+
+int tpebs_set_evsel(struct evsel *evsel, int cpu_map_idx, int thread)
+{
+	struct perf_counts_values *count;

tools/perf/util/intel-tpebs.c line 326

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+	struct tpebs_retire_lat *t;
+	bool found = false;
+	__u64 val;
+	int ret;
+
+	/* Non reitre_latency evsel should never enter this function. */
+	if (!evsel__is_retire_lat(evsel))
+		return -1;
+
+	ret = tpebs_stop();

I think it's better to call this in the upper layer.

Quoted 13 lines
tools/perf/util/intel-tpebs.c new file +13 -0
+	if (ret)
+		return ret;
+
+	count = perf_counts(evsel->counts, cpu_map_idx, thread);
+
+	list_for_each_entry(t, &tpebs_results, nd) {
+		if (!strcmp(t->tpebs_name, evsel->name) || !strcmp(t->tpebs_name, evsel->metric_id)) {
+			found = true;
+			break;
+		}
+	}
+
+	/* Set ena and run to non-zero */

tools/perf/util/intel-tpebs.c line 349

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+	count->ena = count->run = 1;
+	count->lost = 0;
+
+	if (!found) {
+		/*
+		 * Set default value or 0 when retire_latency for this event is
+		 * not found from sampling data (enable_tpebs_recording not set
+		 * or 0 sample recorded).
+		 */
+		val = 0;

Shouldn't it set count->val to 0?

Thanks,
Namhyung

Quoted 105 lines
tools/perf/util/intel-tpebs.c new file +48 -0
+		return 0;
+	}
+
+	/*
+	 * Only set retire_latency value to the first CPU and thread.
+	 */
+	if (cpu_map_idx == 0 && thread == 0)
+		val = rint(t->val);
+	else
+		val = 0;
+
+	count->val = val;
+	return 0;
+}
+
+static void tpebs_retire_lat__delete(struct tpebs_retire_lat *r)
+{
+	zfree(&r->name);
+	zfree(&r->tpebs_name);
+	free(r);
+}
+
+void tpebs_delete(void)
+{
+	struct tpebs_retire_lat *r, *rtmp;
+
+	list_for_each_entry_safe(r, rtmp, &tpebs_results, nd) {
+		list_del_init(&r->nd);
+		tpebs_retire_lat__delete(r);
+	}
+
+	if (tpebs_cmd) {
+		free(tpebs_cmd);
+		tpebs_cmd = NULL;
+	}
+}
+
+int tpebs_stop_delete(void)
+{
+	int ret;
+
+	if (tpebs_pid == -1)
+		return 0;
+
+	ret = tpebs_stop();
+	tpebs_delete();
+	return ret;
+}
tools/perf/util/intel-tpebs.h new file +48 -0
new file mode 100644
index 000000000000..73c1e5219522
@@ -0,0 +1,48 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * intel_tpebs.h: Intel TEPBS support
+ */
+#ifndef INCLUDE__PERF_INTEL_TPEBS_H__
+#define INCLUDE__PERF_INTEL_TPEBS_H__
+
+#include "stat.h"
+#include "evsel.h"
+
+#ifdef HAVE_ARCH_X86_64_SUPPORT
+
+extern bool tpebs_recording;
+int tpebs_start(struct evlist *evsel_list, struct perf_cpu_map *cpus);
+int tpebs_stop(void);
+void tpebs_delete(void);
+int tpebs_set_evsel(struct evsel *evsel, int cpu_map_idx, int thread);
+int tpebs_stop_delete(void);
+
+#else
+
+static inline int tpebs_start(struct evlist *evsel_list __maybe_unused,
+				struct perf_cpu_map *cpus  __maybe_unused)
+{
+	return 0;
+}
+
+static inline int tpebs_stop(void)
+{
+	return 0;
+}
+
+static inline void tpebs_delete(void) {};
+
+static inline int tpebs_set_evsel(struct evsel *evsel  __maybe_unused,
+				int cpu_map_idx  __maybe_unused,
+				int thread  __maybe_unused)
+{
+	return 0;
+}
+
+static inline int tpebs_stop_delete(void)
+{
+	return 0;
+}
+
+#endif
+#endif
Signature · 1 line
2.43.0
#5 lore raw

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

Quoted 202 lines

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

Hello,

Quoted 7 lines · On Wed, Jun 05, 2024 at 01:21:44AM -0400, [email protected] wrote:

From: Weilin Wang <[email protected]>

When retire_latency value is used in a metric formula, evsel 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, evsel would stop perf

record

Quoted 1 line

by sending sigterm signal to perf record process. Sampled data will be

process

Quoted 1 line

to get retire latency value. Another thread is required to synchronize

between

Quoted 4 lines

perf stat and perf record when we pass data through pipe.

Retire_latency evsel is not opened for perf stat so that there is no counter wasted on it. This commit includes code suggested by Namhyung to adjust

reading

Quoted 8 lines

size for groups that include retire_latency evsels.

---
 tools/perf/builtin-stat.c     |   6 +
 tools/perf/util/Build         |   1 +
 tools/perf/util/evsel.c       |  66 +++++-
 tools/perf/util/intel-tpebs.c | 397

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

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

Quoted 18 lines
 tools/perf/util/intel-tpebs.h |  48 ++++
 5 files changed, 516 insertions(+), 2 deletions(-)
 create mode 100644 tools/perf/util/intel-tpebs.c
 create mode 100644 tools/perf/util/intel-tpebs.h
tools/perf/builtin-stat.c +1 -0
index 428e9721b908..b09cb2c6e9c2 100644
@@ -70,6 +70,7 @@
 #include "util/bpf_counter.h"
 #include "util/iostat.h"
 #include "util/util.h"
+#include "util/intel-tpebs.h"
 #include "asm/bug.h"
 
 #include <linux/time64.h>
@@ -653,6 +654,9 @@ static enum counter_recovery

stat_handle_error(struct evsel *counter)

tools/perf/util/evsel.c static void evsel__set_count(struct evsel

Quoted 35 lines
tools/perf/builtin-stat.c +5 -0
 	if (child_pid != -1)
 		kill(child_pid, SIGTERM);
+
+	tpebs_stop_delete();
+
 	return COUNTER_FATAL;
 }
 
@@ -985,6 +989,8 @@ static void sig_atexit(void)
 	if (child_pid != -1)
 		kill(child_pid, SIGTERM);
 
+	tpebs_stop();
+
 	sigprocmask(SIG_SETMASK, &oset, NULL);
 
 	if (signr == -1)
tools/perf/util/Build +1 -0
index 292170a99ab6..79adf39e0d8f 100644
@@ -153,6 +153,7 @@ perf-y += clockid.o
 perf-y += list_sort.o
 perf-y += mutex.o
 perf-y += sharded_mutex.o
+perf-$(CONFIG_X86_64) += intel-tpebs.o
 
 perf-$(CONFIG_LIBBPF) += bpf_map.o
 perf-$(CONFIG_PERF_BPF_SKEL) += bpf_counter.o
tools/perf/util/evsel.c +0 -0
index a0a8aee7d6b9..bd3627819afe 100644
@@ -1538,6 +1538,60 @@ static void evsel__set_count(struct evsel

*counter, int cpu_map_idx, int thread,

tools/perf/util/evsel.c static void evsel__set_count(struct evsel · line 1538

Quoted 1 line
tools/perf/util/evsel.c +0 -0
 	perf_counts__set_loaded(counter->counts, cpu_map_idx, thread,

true);

Quoted 47 lines
tools/perf/util/evsel.c +45 -0
 }
 
+static bool evsel__group_has_tpebs(struct evsel *leader)
+{
+	struct evsel *evsel;
+
+	for_each_group_evsel(evsel, leader) {
+		if (evsel__is_retire_lat(evsel))
+			return true;
+	}
+	return false;
+}
+
+static u64 evsel__group_read_nr_members(struct evsel *leader)
+{
+	u64 nr = leader->core.nr_members;
+	struct evsel *evsel;
+
+	for_each_group_evsel(evsel, leader) {
+		if (evsel__is_retire_lat(evsel))
+			nr--;
+	}
+	return nr;
+}
+
+static u64 evsel__group_read_size(struct evsel *leader)
+{
+	u64 read_format = leader->core.attr.read_format;
+	int entry = sizeof(u64); /* value */
+	int size = 0;
+	int nr = 1;
+
+	if (!evsel__group_has_tpebs(leader))
+		return perf_evsel__read_size(&leader->core);
+
+	if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
+		size += sizeof(u64);
+
+	if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
+		size += sizeof(u64);
+
+	if (read_format & PERF_FORMAT_ID)
+		entry += sizeof(u64);
+
+	if (read_format & PERF_FORMAT_LOST)
+		entry += sizeof(u64);
+

tools/perf/util/evsel.c static void evsel__set_count(struct evsel *counter, int cpu_map_idx, int thread,

Quoted 10 lines
tools/perf/util/evsel.c +9 -0
+	if (read_format & PERF_FORMAT_GROUP) {
+		nr = evsel__group_read_nr_members(leader);
+		size += sizeof(u64);
+	}
+
+	size += entry * nr;
+	return size;
+}
+
 static int evsel__process_group_data(struct evsel *leader, int cpu_map_idx,

int thread, u64 *data)

tools/perf/util/evsel.c static int evsel__process_group_data(struct evsel

Quoted 3 lines
tools/perf/util/evsel.c +0 -0
 {
 	u64 read_format = leader->core.attr.read_format;
@@ -1546,7 +1600,7 @@ static int evsel__process_group_data(struct evsel

*leader, int cpu_map_idx, int

tools/perf/util/evsel.c static int evsel__read_group(struct evsel *leader,

Quoted 9 lines
tools/perf/util/evsel.c +1 -1
 	nr = *data++;
 
-	if (nr != (u64) leader->core.nr_members)
+	if (nr != evsel__group_read_nr_members(leader))
 		return -EINVAL;
 
 	if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
@@ -1576,7 +1630,7 @@ static int evsel__read_group(struct evsel *leader,

int cpu_map_idx, int thread)

tools/perf/util/evsel.c static int evsel__open_cpu(struct evsel *evsel,

Quoted 9 lines
tools/perf/util/evsel.c +1 -1
 {
 	struct perf_stat_evsel *ps = leader->stats;
 	u64 read_format = leader->core.attr.read_format;
-	int size = perf_evsel__read_size(&leader->core);
+	int size = evsel__group_read_size(leader);
 	u64 *data = ps->group_data;
 
 	if (!(read_format & PERF_FORMAT_ID))
@@ -2186,6 +2240,9 @@ static int evsel__open_cpu(struct evsel *evsel,

struct perf_cpu_map *cpus,

tools/perf/util/evsel.c static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus, · line 2244

Quoted 5 lines
tools/perf/util/evsel.c +2 -0
 		return 0;
 	}
 
+	if (evsel__is_retire_lat(evsel))
+		return tpebs_start(evsel->evlist, cpus);

As it works with evlist, I think it's better to put this code there. But it seems perf stat doesn't call the evlist API for open, then we can add this to somewhere in __run_perf_stat() directly.

tools/perf/util/evsel.c int evsel__open(struct evsel *evsel, struct

Quoted 5 lines
tools/perf/util/evsel.c +1 -0
+
 	err = __evsel__prepare_open(evsel, cpus, threads);
 	if (err)
 		return err;
@@ -2376,6 +2433,8 @@ int evsel__open(struct evsel *evsel, struct

perf_cpu_map *cpus,

Quoted 1 line
Quoted 6 lines

tools/perf/util/evsel.c int evsel__open(struct evsel *evsel, struct perf_cpu_map *cpus, · line 2437

Quoted 4 lines

void evsel__close(struct evsel *evsel)
{

tools/perf/util/evsel.c +2 -0
+	if (evsel__is_retire_lat(evsel))
+		tpebs_delete();

Ditto.

Hi Namhyung,

I hope both this and the one above on open could stay in evsel level because these are operations on retire_latency evsel. At the same time, a lot of the previous several versions of work was to move TPEBS code out from perf stat to evsel to make it more generic. I think move these back to __run_perf_stat() are opposite to that goal.

Quoted 95 lines

tools/perf/util/evsel.c static int store_evsel_ids(struct evsel *evsel,

Quoted 5 lines
tools/perf/util/evsel.c +0 -0
 	perf_evsel__close(&evsel->core);
 	perf_evsel__free_id(&evsel->core);
 }
@@ -3341,6 +3400,9 @@ static int store_evsel_ids(struct evsel *evsel,

struct evlist *evlist)

tools/perf/util/evsel.c static int store_evsel_ids(struct evsel *evsel, struct evlist *evlist)

Quoted 7 lines
tools/perf/util/evsel.c +3 -0
 {
 	int cpu_map_idx, thread;
 
+	if (evsel__is_retire_lat(evsel))
+		return 0;
+
 	for (cpu_map_idx = 0; cpu_map_idx < xyarray__max_x(evsel->core.fd);

cpu_map_idx++) {

Quoted 32 lines
tools/perf/util/evsel.c +0 -0
 		for (thread = 0; thread < xyarray__max_y(evsel->core.fd);
 		     thread++) {
tools/perf/util/intel-tpebs.c new file +24 -0
new file mode 100644
index 000000000000..37b7a4f92dd9
@@ -0,0 +1,397 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * intel_tpebs.c: Intel TPEBS support
+ */
+
+
+#include <sys/param.h>
+#include <subcmd/run-command.h>
+#include <thread.h>
+#include "intel-tpebs.h"
+#include <linux/list.h>
+#include <linux/zalloc.h>
+#include <linux/err.h>
+#include "sample.h"
+#include "debug.h"
+#include "evlist.h"
+#include "evsel.h"
+#include "session.h"
+#include "tool.h"
+#include "cpumap.h"
+#include "metricgroup.h"
+#include <sys/stat.h>
+#include <sys/file.h>
+#include <poll.h>

tools/perf/util/intel-tpebs.c line 34

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+#include <math.h>
+
+#define PERF_DATA		"-"
+
+bool tpebs_recording;
+static pid_t tpebs_pid = -1;
+static size_t tpebs_event_size;
+static pthread_t tpebs_reader_thread;
+static struct child_process *tpebs_cmd;
+static struct list_head tpebs_results = LIST_HEAD_INIT(tpebs_results);

It can be 'static LIST_HEAD(tpebs_results);'

Quoted 36 lines
tools/perf/util/intel-tpebs.c new file +36 -0
+
+struct tpebs_retire_lat {
+	struct list_head nd;
+	/* Event name */
+	const char *name;
+	/* Event name with the TPEBS modifier R */
+	const char *tpebs_name;
+	/* Count of retire_latency values found in sample data */
+	size_t count;
+	/* Sum of all the retire_latency values in sample data */
+	int sum;
+	/* Average of retire_latency, val = sum / count */
+	double val;
+};
+
+static int get_perf_record_args(const char **record_argv, char buf[],
+				const char *cpumap_buf)
+{
+	struct tpebs_retire_lat *e;
+	int i = 0;
+
+	pr_debug("Prepare perf record for retire_latency\n");
+
+	record_argv[i++] = "perf";
+	record_argv[i++] = "record";
+	record_argv[i++] = "-W";
+	record_argv[i++] = "--synth=no";
+	record_argv[i++] = buf;
+
+	if (cpumap_buf) {
+		record_argv[i++] = "-C";
+		record_argv[i++] = cpumap_buf;
+	}
+
+	record_argv[i++] = "-a";
+
Quoted 8 lines

tools/perf/util/intel-tpebs.c line 74

Quoted 4 lines
tools/perf/util/intel-tpebs.c new file +4 -0
+	if (!cpumap_buf) {
+		pr_err("Require cpumap list to run sampling.\n");
+		return -ECANCELED;
+	}

Hmm.. I thought you supported system wide collection, not sure if it has a cpumap. Anyway this check makes the earlier one meaningless - you need the cpumap always, right?

TPEBS should be work with "-a" or "-C". I'm not sure what the cpumap would be for "-a". Would it make sense to add "-a" only when cpumap_buf is NULL?

Quoted 150 lines
Quoted 21 lines
tools/perf/util/intel-tpebs.c new file +20 -0
+
+	list_for_each_entry(e, &tpebs_results, nd) {
+		record_argv[i++] = "-e";
+		record_argv[i++] = e->name;
+	}
+
+	record_argv[i++] = "-o";
+	record_argv[i++] = PERF_DATA;
+
+	return 0;
+}
+
+static int prepare_run_command(const char **argv)
+{
+	tpebs_cmd = zalloc(sizeof(struct child_process));
+	if (!tpebs_cmd)
+		return -ENOMEM;
+	tpebs_cmd->argv = argv;
+	tpebs_cmd->out = -1;
+	return 0;

tools/perf/util/intel-tpebs.c

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+}
+
+static int start_perf_record(int control_fd[], int ack_fd[],
+				const char *cpumap_buf)
+{
+	const char **record_argv;
+	int ret;
+	char buf[32];
+
+	scnprintf(buf, sizeof(buf), "--control=fd:%d,%d", control_fd[0],

ack_fd[1]);

Quoted 23 lines
tools/perf/util/intel-tpebs.c new file +23 -0
+
+	record_argv = calloc(12 + 2 * tpebs_event_size, sizeof(char *));
+	if (!record_argv)
+		return -ENOMEM;
+
+	ret = get_perf_record_args(record_argv, buf, cpumap_buf);
+	if (ret)
+		goto out;
+
+	ret = prepare_run_command(record_argv);
+	if (ret)
+		goto out;
+	ret = start_command(tpebs_cmd);
+out:
+	free(record_argv);
+	return ret;
+}
+
+static int process_sample_event(struct perf_tool *tool __maybe_unused,
+				union perf_event *event __maybe_unused,
+				struct perf_sample *sample,
+				struct evsel *evsel,
+				struct machine *machine __maybe_unused)

tools/perf/util/intel-tpebs.c

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

of

Quoted 51 lines
tools/perf/util/intel-tpebs.c new file +51 -0
+	 * retire latency value for each event.
+	 */
+	list_for_each_entry(t, &tpebs_results, nd) {
+		if (!strcmp(evname, t->name)) {
+			t->count += 1;
+			t->sum += sample->retire_lat;
+			t->val = (double) t->sum / t->count;
+			break;
+		}
+	}
+
+	return ret;
+}
+
+static int process_feature_event(struct perf_session *session,
+				 union perf_event *event)
+{
+	if (event->feat.feat_id < HEADER_LAST_FEATURE)
+		return perf_event__process_feature(session, event);
+	return 0;
+}
+
+static void *__sample_reader(void *arg)
+{
+	struct child_process *child = arg;
+	struct perf_session *session;
+	struct perf_data data = {
+		.mode = PERF_DATA_MODE_READ,
+		.path = PERF_DATA,
+		.file.fd = child->out,
+	};
+	struct perf_tool tool = {
+		.sample = process_sample_event,
+		.feature = process_feature_event,
+		.attr = perf_event__process_attr,
+	};
+
+	session = perf_session__new(&data, &tool);
+	if (IS_ERR(session))
+		return NULL;
+	perf_session__process_events(session);
+	perf_session__delete(session);
+
+	return NULL;
+}
+
+int tpebs_start(struct evlist *evsel_list, struct perf_cpu_map *cpus)
+{
+	int ret = 0;
+	struct evsel *evsel;
+	char cpumap_buf[50];

tools/perf/util/intel-tpebs.c line 198

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+
+	/*
+	 * We should only run tpebs_start when tpebs_recording is enabled.
+	 * And we should only run it once with all the required events.
+	 */
+	if (tpebs_pid != -1 || !tpebs_recording)
+		return 0;
+
+	cpu_map__snprint(cpus, cpumap_buf, sizeof(cpumap_buf));
+	pr_debug("cpu map: %s\n", cpumap_buf);

Can you please remove unnecessary debug prints? If you really want it, then make it more meaningful like with more context.

tools/perf/util/intel-tpebs.c

Quoted 3 lines
tools/perf/util/intel-tpebs.c new file +3 -0
+
+	/*
+	 * Prepare perf record for sampling event retire_latency before fork

and

tools/perf/util/intel-tpebs.c line 210

Quoted 9 lines
tools/perf/util/intel-tpebs.c new file +9 -0
+	 * prepare workload
+	 */
+	evlist__for_each_entry(evsel_list, evsel) {
+		struct tpebs_retire_lat *new = zalloc(sizeof(*new));
+		char *name;
+		int i;
+
+		if (!evsel->retire_lat)
+			continue;

Please move the allocation after this.

tools/perf/util/intel-tpebs.c line 212

Quoted 2 lines
tools/perf/util/intel-tpebs.c new file +2 -0
+
+		pr_debug("perf stat retire latency of event %s required\n",

evsel->name);

Quoted 9 lines

tools/perf/util/intel-tpebs.c line 219

Quoted 7 lines
tools/perf/util/intel-tpebs.c new file +7 -0
+		if (!new) {
+			ret = -1;
+			goto err;
+		}
+		for (i = strlen(evsel->name) - 1; i > 0; i--) {
+			if (evsel->name[i] == 'R')
+				break;

I think you also need to check ':' or '/'..

I think the ':' and '/' have already been checked by the evsel parser to make this evsel a retire_latency evsel. Do we need to check them again here?

Quoted 13 lines
Quoted 13 lines
tools/perf/util/intel-tpebs.c new file +12 -0
+		}
+		if (i <= 0 || evsel->name[i] != 'R') {
+			ret = -1;
+			goto err;
+		}
+
+		name = strdup(evsel->name);
+		if (!name) {
+			ret = -ENOMEM;
+			goto err;
+		}
+		name[i] = 'p';
Quoted 8 lines

tools/perf/util/intel-tpebs.c line 236

Quoted 5 lines
tools/perf/util/intel-tpebs.c new file +5 -0
+		new->name = name;
+		new->tpebs_name = strdup(evsel->name);
+		if (!new->tpebs_name) {
+			ret = -ENOMEM;
+			goto err;

This error handling (including above) will leak 'new' because it's not linked to the list yet.

Yes, you are right. I will fix this.

Quoted 88 lines
Quoted 17 lines
tools/perf/util/intel-tpebs.c new file +16 -0
+		}
+		list_add_tail(&new->nd, &tpebs_results);
+		tpebs_event_size += 1;
+	}
+
+	if (tpebs_event_size > 0) {
+		int control_fd[2], ack_fd[2], len;
+		char ack_buf[8];
+
+		/*Create control and ack fd for --control*/
+		if (pipe(control_fd) < 0) {
+			pr_err("Failed to create control fifo");
+			ret = -1;
+			goto out;
+		}
+		if (pipe(ack_fd) < 0) {

tools/perf/util/intel-tpebs.c

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+			pr_err("Failed to create control fifo");
+			ret = -1;
+			goto out;
+		}
+
+		ret = start_perf_record(control_fd, ack_fd, cpumap_buf);
+		if (ret)
+			goto out;
+		tpebs_pid = tpebs_cmd->pid;
+		if (pthread_create(&tpebs_reader_thread, NULL,

__sample_reader, tpebs_cmd)) {

tools/perf/util/intel-tpebs.c

Quoted 3 lines
tools/perf/util/intel-tpebs.c new file +3 -0
+			kill(tpebs_cmd->pid, SIGTERM);
+			close(tpebs_cmd->out);
+			pr_err("Could not create thread to process sample

data.\n");

tools/perf/util/intel-tpebs.c

Quoted 8 lines
tools/perf/util/intel-tpebs.c new file +8 -0
+			ret = -1;
+			goto out;
+		}
+		/* Wait for perf record initialization.*/
+		len = strlen("enable");
+		ret = write(control_fd[1], "enable", len);
+		if (ret != len) {
+			pr_err("perf record control write control message

failed\n");

Quoted 47 lines
tools/perf/util/intel-tpebs.c new file +47 -0
+			goto out;
+		}
+
+		ret = read(ack_fd[0], ack_buf, sizeof(ack_buf));
+		if (ret > 0)
+			ret = strcmp(ack_buf, "ack\n");
+		else {
+			pr_err("perf record control ack failed\n");
+			goto out;
+		}
+		pr_debug("Received ack from perf record\n");
+out:
+		close(control_fd[0]);
+		close(control_fd[1]);
+		close(ack_fd[0]);
+		close(ack_fd[1]);
+	}
+err:
+	if (ret)
+		tpebs_delete();
+	return ret;
+}
+
+int tpebs_stop(void)
+{
+	int ret = 0;
+
+	/* Like tpebs_start, we should only run tpebs_end once. */
+	if (tpebs_pid != -1) {
+		kill(tpebs_cmd->pid, SIGTERM);
+		tpebs_pid = -1;
+		pthread_join(tpebs_reader_thread, NULL);
+		close(tpebs_cmd->out);
+		ret = finish_command(tpebs_cmd);
+		if (ret == -ERR_RUN_COMMAND_WAITPID_SIGNAL)
+			ret = 0;
+	}
+	return ret;
+}
+
+int tpebs_set_evsel(struct evsel *evsel, int cpu_map_idx, int thread)
+{
+	struct perf_counts_values *count;
+	struct tpebs_retire_lat *t;
+	bool found = false;
+	__u64 val;
+	int ret;
Quoted 8 lines

tools/perf/util/intel-tpebs.c line 326

Quoted 6 lines
tools/perf/util/intel-tpebs.c new file +6 -0
+
+	/* Non reitre_latency evsel should never enter this function. */
+	if (!evsel__is_retire_lat(evsel))
+		return -1;
+
+	ret = tpebs_stop();

I think it's better to call this in the upper layer.

The tpebs_stop() is also called in sig_atexit() in perf stat. I want to make this function safe to be called multiple times and add one tpebs_stop() here in case tpebs_set_evsel() is invoked before tpebs_stop(). I hope this makes sense.

Quoted 24 lines
Quoted 13 lines
tools/perf/util/intel-tpebs.c new file +7 -0
+	if (ret)
+		return ret;
+
+	count = perf_counts(evsel->counts, cpu_map_idx, thread);
+
+	list_for_each_entry(t, &tpebs_results, nd) {
+		if (!strcmp(t->tpebs_name, evsel->name) || !strcmp(t-

tpebs_name, evsel->metric_id)) {

tools/perf/util/intel-tpebs.c new file +3 -0
+			found = true;
+			break;
+		}

tools/perf/util/intel-tpebs.c

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+	}
+
+	/* Set ena and run to non-zero */
+	count->ena = count->run = 1;
+	count->lost = 0;
+
+	if (!found) {
+		/*
+		 * Set default value or 0 when retire_latency for this event is
+		 * not found from sampling data (enable_tpebs_recording not

set

Quoted 5 lines

tools/perf/util/intel-tpebs.c line 349

Quoted 3 lines
tools/perf/util/intel-tpebs.c new file +3 -0
+		 * or 0 sample recorded).
+		 */
+		val = 0;

Shouldn't it set count->val to 0?

Yes, I will fix this one.

Thanks,
Weilin

Quoted 111 lines

Thanks,
Namhyung

Quoted 67 lines
tools/perf/util/intel-tpebs.c new file +48 -0
+		return 0;
+	}
+
+	/*
+	 * Only set retire_latency value to the first CPU and thread.
+	 */
+	if (cpu_map_idx == 0 && thread == 0)
+		val = rint(t->val);
+	else
+		val = 0;
+
+	count->val = val;
+	return 0;
+}
+
+static void tpebs_retire_lat__delete(struct tpebs_retire_lat *r)
+{
+	zfree(&r->name);
+	zfree(&r->tpebs_name);
+	free(r);
+}
+
+void tpebs_delete(void)
+{
+	struct tpebs_retire_lat *r, *rtmp;
+
+	list_for_each_entry_safe(r, rtmp, &tpebs_results, nd) {
+		list_del_init(&r->nd);
+		tpebs_retire_lat__delete(r);
+	}
+
+	if (tpebs_cmd) {
+		free(tpebs_cmd);
+		tpebs_cmd = NULL;
+	}
+}
+
+int tpebs_stop_delete(void)
+{
+	int ret;
+
+	if (tpebs_pid == -1)
+		return 0;
+
+	ret = tpebs_stop();
+	tpebs_delete();
+	return ret;
+}
tools/perf/util/intel-tpebs.h new file +13 -0
new file mode 100644
index 000000000000..73c1e5219522
@@ -0,0 +1,48 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * intel_tpebs.h: Intel TEPBS support
+ */
+#ifndef INCLUDE__PERF_INTEL_TPEBS_H__
+#define INCLUDE__PERF_INTEL_TPEBS_H__
+
+#include "stat.h"
+#include "evsel.h"
+
+#ifdef HAVE_ARCH_X86_64_SUPPORT
+
+extern bool tpebs_recording;

tools/perf/util/intel-tpebs.h line 23

Quoted 10 lines
tools/perf/util/intel-tpebs.h new file +10 -0
+int tpebs_start(struct evlist *evsel_list, struct perf_cpu_map *cpus);
+int tpebs_stop(void);
+void tpebs_delete(void);
+int tpebs_set_evsel(struct evsel *evsel, int cpu_map_idx, int thread);
+int tpebs_stop_delete(void);
+
+#else
+
+static inline int tpebs_start(struct evlist *evsel_list __maybe_unused,
+				struct perf_cpu_map *cpus

__maybe_unused)

Quoted 28 lines
tools/perf/util/intel-tpebs.h new file +25 -1
+{
+	return 0;
+}
+
+static inline int tpebs_stop(void)
+{
+	return 0;
+}
+
+static inline void tpebs_delete(void) {};
+
+static inline int tpebs_set_evsel(struct evsel *evsel  __maybe_unused,
+				int cpu_map_idx  __maybe_unused,
+				int thread  __maybe_unused)
+{
+	return 0;
+}
+
+static inline int tpebs_stop_delete(void)
+{
+	return 0;
+}
+
+#endif
+#endif
--

2.43.0

maintainer
#6 lore raw

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

Quoted 209 lines · Weilin
Quoted 202 lines

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

Hello,

Quoted 7 lines · On Wed, Jun 05, 2024 at 01:21:44AM -0400, [email protected] wrote:

From: Weilin Wang <[email protected]>

When retire_latency value is used in a metric formula, evsel 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, evsel would stop perf

record

Quoted 1 line

by sending sigterm signal to perf record process. Sampled data will be

process

Quoted 1 line

to get retire latency value. Another thread is required to synchronize

between

Quoted 4 lines

perf stat and perf record when we pass data through pipe.

Retire_latency evsel is not opened for perf stat so that there is no counter wasted on it. This commit includes code suggested by Namhyung to adjust

reading

Quoted 8 lines

size for groups that include retire_latency evsels.

---
 tools/perf/builtin-stat.c     |   6 +
 tools/perf/util/Build         |   1 +
 tools/perf/util/evsel.c       |  66 +++++-
 tools/perf/util/intel-tpebs.c | 397

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

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

Quoted 18 lines
 tools/perf/util/intel-tpebs.h |  48 ++++
 5 files changed, 516 insertions(+), 2 deletions(-)
 create mode 100644 tools/perf/util/intel-tpebs.c
 create mode 100644 tools/perf/util/intel-tpebs.h
tools/perf/builtin-stat.c +1 -0
index 428e9721b908..b09cb2c6e9c2 100644
@@ -70,6 +70,7 @@
 #include "util/bpf_counter.h"
 #include "util/iostat.h"
 #include "util/util.h"
+#include "util/intel-tpebs.h"
 #include "asm/bug.h"
 
 #include <linux/time64.h>
@@ -653,6 +654,9 @@ static enum counter_recovery

stat_handle_error(struct evsel *counter)

tools/perf/util/evsel.c static void evsel__set_count(struct evsel

Quoted 35 lines
tools/perf/builtin-stat.c +5 -0
 	if (child_pid != -1)
 		kill(child_pid, SIGTERM);
+
+	tpebs_stop_delete();
+
 	return COUNTER_FATAL;
 }
 
@@ -985,6 +989,8 @@ static void sig_atexit(void)
 	if (child_pid != -1)
 		kill(child_pid, SIGTERM);
 
+	tpebs_stop();
+
 	sigprocmask(SIG_SETMASK, &oset, NULL);
 
 	if (signr == -1)
tools/perf/util/Build +1 -0
index 292170a99ab6..79adf39e0d8f 100644
@@ -153,6 +153,7 @@ perf-y += clockid.o
 perf-y += list_sort.o
 perf-y += mutex.o
 perf-y += sharded_mutex.o
+perf-$(CONFIG_X86_64) += intel-tpebs.o
 
 perf-$(CONFIG_LIBBPF) += bpf_map.o
 perf-$(CONFIG_PERF_BPF_SKEL) += bpf_counter.o
tools/perf/util/evsel.c +0 -0
index a0a8aee7d6b9..bd3627819afe 100644
@@ -1538,6 +1538,60 @@ static void evsel__set_count(struct evsel

*counter, int cpu_map_idx, int thread,

tools/perf/util/evsel.c static void evsel__set_count(struct evsel · line 1538

Quoted 1 line
tools/perf/util/evsel.c +0 -0
 	perf_counts__set_loaded(counter->counts, cpu_map_idx, thread,

true);

Quoted 47 lines
tools/perf/util/evsel.c +45 -0
 }
 
+static bool evsel__group_has_tpebs(struct evsel *leader)
+{
+	struct evsel *evsel;
+
+	for_each_group_evsel(evsel, leader) {
+		if (evsel__is_retire_lat(evsel))
+			return true;
+	}
+	return false;
+}
+
+static u64 evsel__group_read_nr_members(struct evsel *leader)
+{
+	u64 nr = leader->core.nr_members;
+	struct evsel *evsel;
+
+	for_each_group_evsel(evsel, leader) {
+		if (evsel__is_retire_lat(evsel))
+			nr--;
+	}
+	return nr;
+}
+
+static u64 evsel__group_read_size(struct evsel *leader)
+{
+	u64 read_format = leader->core.attr.read_format;
+	int entry = sizeof(u64); /* value */
+	int size = 0;
+	int nr = 1;
+
+	if (!evsel__group_has_tpebs(leader))
+		return perf_evsel__read_size(&leader->core);
+
+	if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
+		size += sizeof(u64);
+
+	if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
+		size += sizeof(u64);
+
+	if (read_format & PERF_FORMAT_ID)
+		entry += sizeof(u64);
+
+	if (read_format & PERF_FORMAT_LOST)
+		entry += sizeof(u64);
+

tools/perf/util/evsel.c static void evsel__set_count(struct evsel *counter, int cpu_map_idx, int thread,

Quoted 10 lines
tools/perf/util/evsel.c +9 -0
+	if (read_format & PERF_FORMAT_GROUP) {
+		nr = evsel__group_read_nr_members(leader);
+		size += sizeof(u64);
+	}
+
+	size += entry * nr;
+	return size;
+}
+
 static int evsel__process_group_data(struct evsel *leader, int cpu_map_idx,

int thread, u64 *data)

tools/perf/util/evsel.c static int evsel__process_group_data(struct evsel

Quoted 3 lines
tools/perf/util/evsel.c +0 -0
 {
 	u64 read_format = leader->core.attr.read_format;
@@ -1546,7 +1600,7 @@ static int evsel__process_group_data(struct evsel

*leader, int cpu_map_idx, int

tools/perf/util/evsel.c static int evsel__read_group(struct evsel *leader,

Quoted 9 lines
tools/perf/util/evsel.c +1 -1
 	nr = *data++;
 
-	if (nr != (u64) leader->core.nr_members)
+	if (nr != evsel__group_read_nr_members(leader))
 		return -EINVAL;
 
 	if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
@@ -1576,7 +1630,7 @@ static int evsel__read_group(struct evsel *leader,

int cpu_map_idx, int thread)

tools/perf/util/evsel.c static int evsel__open_cpu(struct evsel *evsel,

Quoted 9 lines
tools/perf/util/evsel.c +1 -1
 {
 	struct perf_stat_evsel *ps = leader->stats;
 	u64 read_format = leader->core.attr.read_format;
-	int size = perf_evsel__read_size(&leader->core);
+	int size = evsel__group_read_size(leader);
 	u64 *data = ps->group_data;
 
 	if (!(read_format & PERF_FORMAT_ID))
@@ -2186,6 +2240,9 @@ static int evsel__open_cpu(struct evsel *evsel,

struct perf_cpu_map *cpus,

tools/perf/util/evsel.c static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus, · line 2244

Quoted 5 lines
tools/perf/util/evsel.c +2 -0
 		return 0;
 	}
 
+	if (evsel__is_retire_lat(evsel))
+		return tpebs_start(evsel->evlist, cpus);

As it works with evlist, I think it's better to put this code there. But it seems perf stat doesn't call the evlist API for open, then we can add this to somewhere in __run_perf_stat() directly.

tools/perf/util/evsel.c int evsel__open(struct evsel *evsel, struct

Quoted 5 lines
tools/perf/util/evsel.c +1 -0
+
 	err = __evsel__prepare_open(evsel, cpus, threads);
 	if (err)
 		return err;
@@ -2376,6 +2433,8 @@ int evsel__open(struct evsel *evsel, struct

perf_cpu_map *cpus,

Quoted 1 line
Quoted 6 lines

tools/perf/util/evsel.c int evsel__open(struct evsel *evsel, struct perf_cpu_map *cpus, · line 2437

Quoted 4 lines

void evsel__close(struct evsel *evsel)
{

tools/perf/util/evsel.c +2 -0
+	if (evsel__is_retire_lat(evsel))
+		tpebs_delete();

Ditto.

Quoted 4 lines

Hi Namhyung,

I hope both this and the one above on open could stay in evsel level because these are operations on retire_latency evsel.

Then I think you need to remove the specific evsel not the all tpebs events.

Quoted 4 lines

At the same time, a lot of the
previous several versions of work was to move TPEBS code out from perf stat to evsel to make it more generic. I think move these back to __run_perf_stat() are opposite to that goal.

Oh, I meant you can have the logic in utils/intel-tpebs.c but add a call to tpebs_delete() in __run_perf_stat(). I think it'd better to keep it in evlist__close() but we don't use evlist__open() for perf stat so it's not symmetric. :(

Anyway, all I want to say is that tpebs APIs work on evlist level. So I think it's natural that they are called for the whole list, not for an event/evsel.

Quoted 106 lines
Quoted 97 lines

tools/perf/util/evsel.c static int store_evsel_ids(struct evsel *evsel,

Quoted 7 lines
tools/perf/util/evsel.c +0 -0
 	perf_evsel__close(&evsel->core);
 	perf_evsel__free_id(&evsel->core);
 }
@@ -3341,6 +3400,9 @@ static int store_evsel_ids(struct evsel *evsel,

struct evlist *evlist)

tools/perf/util/evsel.c static int store_evsel_ids(struct evsel *evsel, struct evlist *evlist)

Quoted 7 lines
tools/perf/util/evsel.c +3 -0
 {
 	int cpu_map_idx, thread;
 
+	if (evsel__is_retire_lat(evsel))
+		return 0;
+
 	for (cpu_map_idx = 0; cpu_map_idx < xyarray__max_x(evsel->core.fd);

cpu_map_idx++) {

Quoted 32 lines
tools/perf/util/evsel.c +0 -0
 		for (thread = 0; thread < xyarray__max_y(evsel->core.fd);
 		     thread++) {
tools/perf/util/intel-tpebs.c new file +24 -0
new file mode 100644
index 000000000000..37b7a4f92dd9
@@ -0,0 +1,397 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * intel_tpebs.c: Intel TPEBS support
+ */
+
+
+#include <sys/param.h>
+#include <subcmd/run-command.h>
+#include <thread.h>
+#include "intel-tpebs.h"
+#include <linux/list.h>
+#include <linux/zalloc.h>
+#include <linux/err.h>
+#include "sample.h"
+#include "debug.h"
+#include "evlist.h"
+#include "evsel.h"
+#include "session.h"
+#include "tool.h"
+#include "cpumap.h"
+#include "metricgroup.h"
+#include <sys/stat.h>
+#include <sys/file.h>
+#include <poll.h>

tools/perf/util/intel-tpebs.c line 34

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+#include <math.h>
+
+#define PERF_DATA		"-"
+
+bool tpebs_recording;
+static pid_t tpebs_pid = -1;
+static size_t tpebs_event_size;
+static pthread_t tpebs_reader_thread;
+static struct child_process *tpebs_cmd;
+static struct list_head tpebs_results = LIST_HEAD_INIT(tpebs_results);

It can be 'static LIST_HEAD(tpebs_results);'

Quoted 36 lines
tools/perf/util/intel-tpebs.c new file +36 -0
+
+struct tpebs_retire_lat {
+	struct list_head nd;
+	/* Event name */
+	const char *name;
+	/* Event name with the TPEBS modifier R */
+	const char *tpebs_name;
+	/* Count of retire_latency values found in sample data */
+	size_t count;
+	/* Sum of all the retire_latency values in sample data */
+	int sum;
+	/* Average of retire_latency, val = sum / count */
+	double val;
+};
+
+static int get_perf_record_args(const char **record_argv, char buf[],
+				const char *cpumap_buf)
+{
+	struct tpebs_retire_lat *e;
+	int i = 0;
+
+	pr_debug("Prepare perf record for retire_latency\n");
+
+	record_argv[i++] = "perf";
+	record_argv[i++] = "record";
+	record_argv[i++] = "-W";
+	record_argv[i++] = "--synth=no";
+	record_argv[i++] = buf;
+
+	if (cpumap_buf) {
+		record_argv[i++] = "-C";
+		record_argv[i++] = cpumap_buf;
+	}
+
+	record_argv[i++] = "-a";
+
Quoted 8 lines

tools/perf/util/intel-tpebs.c line 74

Quoted 4 lines
tools/perf/util/intel-tpebs.c new file +4 -0
+	if (!cpumap_buf) {
+		pr_err("Require cpumap list to run sampling.\n");
+		return -ECANCELED;
+	}

Hmm.. I thought you supported system wide collection, not sure if it has a cpumap. Anyway this check makes the earlier one meaningless - you need the cpumap always, right?

Quoted 2 lines

TPEBS should be work with "-a" or "-C". I'm not sure what the cpumap would be for "-a". Would it make sense to add "-a" only when cpumap_buf is NULL?

I think the best way is to check target__has_cpu().

Quoted 161 lines
Quoted 151 lines
Quoted 22 lines
tools/perf/util/intel-tpebs.c new file +20 -0
+
+	list_for_each_entry(e, &tpebs_results, nd) {
+		record_argv[i++] = "-e";
+		record_argv[i++] = e->name;
+	}
+
+	record_argv[i++] = "-o";
+	record_argv[i++] = PERF_DATA;
+
+	return 0;
+}
+
+static int prepare_run_command(const char **argv)
+{
+	tpebs_cmd = zalloc(sizeof(struct child_process));
+	if (!tpebs_cmd)
+		return -ENOMEM;
+	tpebs_cmd->argv = argv;
+	tpebs_cmd->out = -1;
+	return 0;

tools/perf/util/intel-tpebs.c

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+}
+
+static int start_perf_record(int control_fd[], int ack_fd[],
+				const char *cpumap_buf)
+{
+	const char **record_argv;
+	int ret;
+	char buf[32];
+
+	scnprintf(buf, sizeof(buf), "--control=fd:%d,%d", control_fd[0],

ack_fd[1]);

Quoted 23 lines
tools/perf/util/intel-tpebs.c new file +23 -0
+
+	record_argv = calloc(12 + 2 * tpebs_event_size, sizeof(char *));
+	if (!record_argv)
+		return -ENOMEM;
+
+	ret = get_perf_record_args(record_argv, buf, cpumap_buf);
+	if (ret)
+		goto out;
+
+	ret = prepare_run_command(record_argv);
+	if (ret)
+		goto out;
+	ret = start_command(tpebs_cmd);
+out:
+	free(record_argv);
+	return ret;
+}
+
+static int process_sample_event(struct perf_tool *tool __maybe_unused,
+				union perf_event *event __maybe_unused,
+				struct perf_sample *sample,
+				struct evsel *evsel,
+				struct machine *machine __maybe_unused)

tools/perf/util/intel-tpebs.c

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

of

Quoted 51 lines
tools/perf/util/intel-tpebs.c new file +51 -0
+	 * retire latency value for each event.
+	 */
+	list_for_each_entry(t, &tpebs_results, nd) {
+		if (!strcmp(evname, t->name)) {
+			t->count += 1;
+			t->sum += sample->retire_lat;
+			t->val = (double) t->sum / t->count;
+			break;
+		}
+	}
+
+	return ret;
+}
+
+static int process_feature_event(struct perf_session *session,
+				 union perf_event *event)
+{
+	if (event->feat.feat_id < HEADER_LAST_FEATURE)
+		return perf_event__process_feature(session, event);
+	return 0;
+}
+
+static void *__sample_reader(void *arg)
+{
+	struct child_process *child = arg;
+	struct perf_session *session;
+	struct perf_data data = {
+		.mode = PERF_DATA_MODE_READ,
+		.path = PERF_DATA,
+		.file.fd = child->out,
+	};
+	struct perf_tool tool = {
+		.sample = process_sample_event,
+		.feature = process_feature_event,
+		.attr = perf_event__process_attr,
+	};
+
+	session = perf_session__new(&data, &tool);
+	if (IS_ERR(session))
+		return NULL;
+	perf_session__process_events(session);
+	perf_session__delete(session);
+
+	return NULL;
+}
+
+int tpebs_start(struct evlist *evsel_list, struct perf_cpu_map *cpus)
+{
+	int ret = 0;
+	struct evsel *evsel;
+	char cpumap_buf[50];

tools/perf/util/intel-tpebs.c line 198

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+
+	/*
+	 * We should only run tpebs_start when tpebs_recording is enabled.
+	 * And we should only run it once with all the required events.
+	 */
+	if (tpebs_pid != -1 || !tpebs_recording)
+		return 0;
+
+	cpu_map__snprint(cpus, cpumap_buf, sizeof(cpumap_buf));
+	pr_debug("cpu map: %s\n", cpumap_buf);

Can you please remove unnecessary debug prints? If you really want it, then make it more meaningful like with more context.

tools/perf/util/intel-tpebs.c

Quoted 3 lines
tools/perf/util/intel-tpebs.c new file +3 -0
+
+	/*
+	 * Prepare perf record for sampling event retire_latency before fork

and

tools/perf/util/intel-tpebs.c line 210

Quoted 9 lines
tools/perf/util/intel-tpebs.c new file +9 -0
+	 * prepare workload
+	 */
+	evlist__for_each_entry(evsel_list, evsel) {
+		struct tpebs_retire_lat *new = zalloc(sizeof(*new));
+		char *name;
+		int i;
+
+		if (!evsel->retire_lat)
+			continue;

Please move the allocation after this.

tools/perf/util/intel-tpebs.c line 212

Quoted 2 lines
tools/perf/util/intel-tpebs.c new file +2 -0
+
+		pr_debug("perf stat retire latency of event %s required\n",

evsel->name);

Quoted 9 lines

tools/perf/util/intel-tpebs.c line 219

Quoted 7 lines
tools/perf/util/intel-tpebs.c new file +7 -0
+		if (!new) {
+			ret = -1;
+			goto err;
+		}
+		for (i = strlen(evsel->name) - 1; i > 0; i--) {
+			if (evsel->name[i] == 'R')
+				break;

I think you also need to check ':' or '/'..

Quoted 2 lines

I think the ':' and '/' have already been checked by the evsel parser to make this evsel a retire_latency evsel. Do we need to check them again here?

Ok, it's fine if it's checked already. Then I thought you could just use strrchr() but it seems you want the index of 'R' to modify the copied string.

Quoted 16 lines
Quoted 16 lines
Quoted 6 lines
tools/perf/util/intel-tpebs.c new file +4 -0
+		}
+		if (i <= 0 || evsel->name[i] != 'R') {
+			ret = -1;
+			goto err;

tools/perf/util/intel-tpebs.c line 233

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+		}
+
+		name = strdup(evsel->name);
+		if (!name) {
+			ret = -ENOMEM;
+			goto err;
+		}
+		name[i] = 'p';
+		new->name = name;
+		new->tpebs_name = strdup(evsel->name);

Is this just to find the original evsel later? Then I think you can compare the pointer directly instead of name.

Quoted 106 lines
Quoted 6 lines

tools/perf/util/intel-tpebs.c line 236

Quoted 3 lines
tools/perf/util/intel-tpebs.c new file +3 -0
+		if (!new->tpebs_name) {
+			ret = -ENOMEM;
+			goto err;

This error handling (including above) will leak 'new' because it's not linked to the list yet.

Yes, you are right. I will fix this.

Quoted 88 lines
Quoted 17 lines
tools/perf/util/intel-tpebs.c new file +16 -0
+		}
+		list_add_tail(&new->nd, &tpebs_results);
+		tpebs_event_size += 1;
+	}
+
+	if (tpebs_event_size > 0) {
+		int control_fd[2], ack_fd[2], len;
+		char ack_buf[8];
+
+		/*Create control and ack fd for --control*/
+		if (pipe(control_fd) < 0) {
+			pr_err("Failed to create control fifo");
+			ret = -1;
+			goto out;
+		}
+		if (pipe(ack_fd) < 0) {

tools/perf/util/intel-tpebs.c

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+			pr_err("Failed to create control fifo");
+			ret = -1;
+			goto out;
+		}
+
+		ret = start_perf_record(control_fd, ack_fd, cpumap_buf);
+		if (ret)
+			goto out;
+		tpebs_pid = tpebs_cmd->pid;
+		if (pthread_create(&tpebs_reader_thread, NULL,

__sample_reader, tpebs_cmd)) {

tools/perf/util/intel-tpebs.c

Quoted 3 lines
tools/perf/util/intel-tpebs.c new file +3 -0
+			kill(tpebs_cmd->pid, SIGTERM);
+			close(tpebs_cmd->out);
+			pr_err("Could not create thread to process sample

data.\n");

tools/perf/util/intel-tpebs.c

Quoted 8 lines
tools/perf/util/intel-tpebs.c new file +8 -0
+			ret = -1;
+			goto out;
+		}
+		/* Wait for perf record initialization.*/
+		len = strlen("enable");
+		ret = write(control_fd[1], "enable", len);
+		if (ret != len) {
+			pr_err("perf record control write control message

failed\n");

Quoted 47 lines
tools/perf/util/intel-tpebs.c new file +47 -0
+			goto out;
+		}
+
+		ret = read(ack_fd[0], ack_buf, sizeof(ack_buf));
+		if (ret > 0)
+			ret = strcmp(ack_buf, "ack\n");
+		else {
+			pr_err("perf record control ack failed\n");
+			goto out;
+		}
+		pr_debug("Received ack from perf record\n");
+out:
+		close(control_fd[0]);
+		close(control_fd[1]);
+		close(ack_fd[0]);
+		close(ack_fd[1]);
+	}
+err:
+	if (ret)
+		tpebs_delete();
+	return ret;
+}
+
+int tpebs_stop(void)
+{
+	int ret = 0;
+
+	/* Like tpebs_start, we should only run tpebs_end once. */
+	if (tpebs_pid != -1) {
+		kill(tpebs_cmd->pid, SIGTERM);
+		tpebs_pid = -1;
+		pthread_join(tpebs_reader_thread, NULL);
+		close(tpebs_cmd->out);
+		ret = finish_command(tpebs_cmd);
+		if (ret == -ERR_RUN_COMMAND_WAITPID_SIGNAL)
+			ret = 0;
+	}
+	return ret;
+}
+
+int tpebs_set_evsel(struct evsel *evsel, int cpu_map_idx, int thread)
+{
+	struct perf_counts_values *count;
+	struct tpebs_retire_lat *t;
+	bool found = false;
+	__u64 val;
+	int ret;
Quoted 8 lines

tools/perf/util/intel-tpebs.c line 326

Quoted 6 lines
tools/perf/util/intel-tpebs.c new file +6 -0
+
+	/* Non reitre_latency evsel should never enter this function. */
+	if (!evsel__is_retire_lat(evsel))
+		return -1;
+
+	ret = tpebs_stop();

I think it's better to call this in the upper layer.

Quoted 3 lines

The tpebs_stop() is also called in sig_atexit() in perf stat. I want to make this function safe to be called multiple times and add one tpebs_stop() here in case tpebs_set_evsel() is invoked before tpebs_stop(). I hope this makes sense.

I guess it's ok to call tpebs_set_evsel() even if it's not stopped. It seems you use tpebs_retire_lat->val only and it's an average so it won't change much if you read either before or after concurrent changes.

It can be useful to the interval mode if you keep the tpebs background process after read.

Thanks,
Namhyung

Quoted 146 lines
Quoted 25 lines
Quoted 14 lines
tools/perf/util/intel-tpebs.c new file +7 -0
+	if (ret)
+		return ret;
+
+	count = perf_counts(evsel->counts, cpu_map_idx, thread);
+
+	list_for_each_entry(t, &tpebs_results, nd) {
+		if (!strcmp(t->tpebs_name, evsel->name) || !strcmp(t-

tpebs_name, evsel->metric_id)) {

tools/perf/util/intel-tpebs.c new file +3 -0
+			found = true;
+			break;
+		}

tools/perf/util/intel-tpebs.c

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+	}
+
+	/* Set ena and run to non-zero */
+	count->ena = count->run = 1;
+	count->lost = 0;
+
+	if (!found) {
+		/*
+		 * Set default value or 0 when retire_latency for this event is
+		 * not found from sampling data (enable_tpebs_recording not

set

Quoted 5 lines

tools/perf/util/intel-tpebs.c line 349

Quoted 3 lines
tools/perf/util/intel-tpebs.c new file +3 -0
+		 * or 0 sample recorded).
+		 */
+		val = 0;

Shouldn't it set count->val to 0?

Yes, I will fix this one.

Thanks,
Weilin

Quoted 111 lines

Thanks,
Namhyung

Quoted 67 lines
tools/perf/util/intel-tpebs.c new file +48 -0
+		return 0;
+	}
+
+	/*
+	 * Only set retire_latency value to the first CPU and thread.
+	 */
+	if (cpu_map_idx == 0 && thread == 0)
+		val = rint(t->val);
+	else
+		val = 0;
+
+	count->val = val;
+	return 0;
+}
+
+static void tpebs_retire_lat__delete(struct tpebs_retire_lat *r)
+{
+	zfree(&r->name);
+	zfree(&r->tpebs_name);
+	free(r);
+}
+
+void tpebs_delete(void)
+{
+	struct tpebs_retire_lat *r, *rtmp;
+
+	list_for_each_entry_safe(r, rtmp, &tpebs_results, nd) {
+		list_del_init(&r->nd);
+		tpebs_retire_lat__delete(r);
+	}
+
+	if (tpebs_cmd) {
+		free(tpebs_cmd);
+		tpebs_cmd = NULL;
+	}
+}
+
+int tpebs_stop_delete(void)
+{
+	int ret;
+
+	if (tpebs_pid == -1)
+		return 0;
+
+	ret = tpebs_stop();
+	tpebs_delete();
+	return ret;
+}
tools/perf/util/intel-tpebs.h new file +13 -0
new file mode 100644
index 000000000000..73c1e5219522
@@ -0,0 +1,48 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * intel_tpebs.h: Intel TEPBS support
+ */
+#ifndef INCLUDE__PERF_INTEL_TPEBS_H__
+#define INCLUDE__PERF_INTEL_TPEBS_H__
+
+#include "stat.h"
+#include "evsel.h"
+
+#ifdef HAVE_ARCH_X86_64_SUPPORT
+
+extern bool tpebs_recording;

tools/perf/util/intel-tpebs.h line 23

Quoted 10 lines
tools/perf/util/intel-tpebs.h new file +10 -0
+int tpebs_start(struct evlist *evsel_list, struct perf_cpu_map *cpus);
+int tpebs_stop(void);
+void tpebs_delete(void);
+int tpebs_set_evsel(struct evsel *evsel, int cpu_map_idx, int thread);
+int tpebs_stop_delete(void);
+
+#else
+
+static inline int tpebs_start(struct evlist *evsel_list __maybe_unused,
+				struct perf_cpu_map *cpus

__maybe_unused)

Quoted 28 lines
tools/perf/util/intel-tpebs.h new file +25 -1
+{
+	return 0;
+}
+
+static inline int tpebs_stop(void)
+{
+	return 0;
+}
+
+static inline void tpebs_delete(void) {};
+
+static inline int tpebs_set_evsel(struct evsel *evsel  __maybe_unused,
+				int cpu_map_idx  __maybe_unused,
+				int thread  __maybe_unused)
+{
+	return 0;
+}
+
+static inline int tpebs_stop_delete(void)
+{
+	return 0;
+}
+
+#endif
+#endif
--

2.43.0

#7 lore raw

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

Quoted 253 lines

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

Quoted 12 lines · Weilin
Quoted 12 lines

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

Perry

Quoted 1 line
Quoted 1 line

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

Biggers,

Quoted 2 lines
Quoted 2 lines

Caleb <[email protected]>
Subject: Re: [RFC PATCH v11 3/8] perf stat: Fork and launch perf record

when

Quoted 5 lines
Quoted 5 lines

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

Hello,

On Wed, Jun 05, 2024 at 01:21:44AM -0400, [email protected]

Quoted 3 lines · wrote:
Quoted 3 lines
Quoted 3 lines

From: Weilin Wang <[email protected]>

When retire_latency value is used in a metric formula, evsel would fork a

perf

Quoted 1 line
Quoted 1 line
Quoted 1 line

record process with "-e" and "-W" options. Perf record will collect

required

Quoted 1 line
Quoted 1 line
Quoted 1 line

retire_latency values in parallel while perf stat is collecting counting

values.

Quoted 11 lines
Quoted 11 lines
Quoted 2 lines

At the point of time that perf stat stops counting, evsel would stop perf

record

Quoted 1 line

by sending sigterm signal to perf record process. Sampled data will be

process

Quoted 1 line

to get retire latency value. Another thread is required to synchronize

between

Quoted 4 lines

perf stat and perf record when we pass data through pipe.

Retire_latency evsel is not opened for perf stat so that there is no counter wasted on it. This commit includes code suggested by Namhyung to

adjust

Quoted 116 lines
Quoted 116 lines

reading

Quoted 8 lines

size for groups that include retire_latency evsels.

---
 tools/perf/builtin-stat.c     |   6 +
 tools/perf/util/Build         |   1 +
 tools/perf/util/evsel.c       |  66 +++++-
 tools/perf/util/intel-tpebs.c | 397

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

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

Quoted 18 lines
 tools/perf/util/intel-tpebs.h |  48 ++++
 5 files changed, 516 insertions(+), 2 deletions(-)
 create mode 100644 tools/perf/util/intel-tpebs.c
 create mode 100644 tools/perf/util/intel-tpebs.h
tools/perf/builtin-stat.c +1 -0
index 428e9721b908..b09cb2c6e9c2 100644
@@ -70,6 +70,7 @@
 #include "util/bpf_counter.h"
 #include "util/iostat.h"
 #include "util/util.h"
+#include "util/intel-tpebs.h"
 #include "asm/bug.h"
 
 #include <linux/time64.h>
@@ -653,6 +654,9 @@ static enum counter_recovery

stat_handle_error(struct evsel *counter)

tools/perf/util/evsel.c static void evsel__set_count(struct evsel

Quoted 35 lines
tools/perf/builtin-stat.c +5 -0
 	if (child_pid != -1)
 		kill(child_pid, SIGTERM);
+
+	tpebs_stop_delete();
+
 	return COUNTER_FATAL;
 }
 
@@ -985,6 +989,8 @@ static void sig_atexit(void)
 	if (child_pid != -1)
 		kill(child_pid, SIGTERM);
 
+	tpebs_stop();
+
 	sigprocmask(SIG_SETMASK, &oset, NULL);
 
 	if (signr == -1)
tools/perf/util/Build +1 -0
index 292170a99ab6..79adf39e0d8f 100644
@@ -153,6 +153,7 @@ perf-y += clockid.o
 perf-y += list_sort.o
 perf-y += mutex.o
 perf-y += sharded_mutex.o
+perf-$(CONFIG_X86_64) += intel-tpebs.o
 
 perf-$(CONFIG_LIBBPF) += bpf_map.o
 perf-$(CONFIG_PERF_BPF_SKEL) += bpf_counter.o
tools/perf/util/evsel.c +0 -0
index a0a8aee7d6b9..bd3627819afe 100644
@@ -1538,6 +1538,60 @@ static void evsel__set_count(struct evsel

*counter, int cpu_map_idx, int thread,

tools/perf/util/evsel.c static void evsel__set_count(struct evsel · line 1538

Quoted 1 line
tools/perf/util/evsel.c +0 -0
 	perf_counts__set_loaded(counter->counts, cpu_map_idx, thread,

true);

Quoted 49 lines
tools/perf/util/evsel.c +47 -0
 }
 
+static bool evsel__group_has_tpebs(struct evsel *leader)
+{
+	struct evsel *evsel;
+
+	for_each_group_evsel(evsel, leader) {
+		if (evsel__is_retire_lat(evsel))
+			return true;
+	}
+	return false;
+}
+
+static u64 evsel__group_read_nr_members(struct evsel *leader)
+{
+	u64 nr = leader->core.nr_members;
+	struct evsel *evsel;
+
+	for_each_group_evsel(evsel, leader) {
+		if (evsel__is_retire_lat(evsel))
+			nr--;
+	}
+	return nr;
+}
+
+static u64 evsel__group_read_size(struct evsel *leader)
+{
+	u64 read_format = leader->core.attr.read_format;
+	int entry = sizeof(u64); /* value */
+	int size = 0;
+	int nr = 1;
+
+	if (!evsel__group_has_tpebs(leader))
+		return perf_evsel__read_size(&leader->core);
+
+	if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
+		size += sizeof(u64);
+
+	if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
+		size += sizeof(u64);
+
+	if (read_format & PERF_FORMAT_ID)
+		entry += sizeof(u64);
+
+	if (read_format & PERF_FORMAT_LOST)
+		entry += sizeof(u64);
+
+	if (read_format & PERF_FORMAT_GROUP) {
+		nr = evsel__group_read_nr_members(leader);
Quoted 8 lines
Quoted 8 lines

tools/perf/util/evsel.c static void evsel__set_count(struct evsel *counter, int cpu_map_idx, int thread,

Quoted 8 lines
tools/perf/util/evsel.c +7 -0
+		size += sizeof(u64);
+	}
+
+	size += entry * nr;
+	return size;
+}
+
 static int evsel__process_group_data(struct evsel *leader, int

cpu_map_idx,

Quoted 4 lines
Quoted 4 lines

int thread, u64 *data)

tools/perf/util/evsel.c static int evsel__process_group_data(struct

Quoted 3 lines
tools/perf/util/evsel.c +0 -0
 {
 	u64 read_format = leader->core.attr.read_format;
@@ -1546,7 +1600,7 @@ static int evsel__process_group_data(struct

evsel

Quoted 10 lines
Quoted 10 lines

*leader, int cpu_map_idx, int

tools/perf/util/evsel.c static int evsel__read_group(struct evsel

Quoted 9 lines
tools/perf/util/evsel.c +1 -1
 	nr = *data++;
 
-	if (nr != (u64) leader->core.nr_members)
+	if (nr != evsel__group_read_nr_members(leader))
 		return -EINVAL;
 
 	if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
@@ -1576,7 +1630,7 @@ static int evsel__read_group(struct evsel

*leader,

Quoted 35 lines
Quoted 28 lines

int cpu_map_idx, int thread)

tools/perf/util/evsel.c static int evsel__open_cpu(struct evsel *evsel,

Quoted 9 lines
tools/perf/util/evsel.c +1 -1
 {
 	struct perf_stat_evsel *ps = leader->stats;
 	u64 read_format = leader->core.attr.read_format;
-	int size = perf_evsel__read_size(&leader->core);
+	int size = evsel__group_read_size(leader);
 	u64 *data = ps->group_data;
 
 	if (!(read_format & PERF_FORMAT_ID))
@@ -2186,6 +2240,9 @@ static int evsel__open_cpu(struct evsel *evsel,

struct perf_cpu_map *cpus,

tools/perf/util/evsel.c static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus, · line 2244

Quoted 5 lines
tools/perf/util/evsel.c +2 -0
 		return 0;
 	}
 
+	if (evsel__is_retire_lat(evsel))
+		return tpebs_start(evsel->evlist, cpus);

As it works with evlist, I think it's better to put this code there. But it seems perf stat doesn't call the evlist API for open, then we can add this to somewhere in __run_perf_stat() directly.

tools/perf/util/evsel.c int evsel__open(struct evsel *evsel, struct

Quoted 5 lines
tools/perf/util/evsel.c +1 -0
+
 	err = __evsel__prepare_open(evsel, cpus, threads);
 	if (err)
 		return err;
@@ -2376,6 +2433,8 @@ int evsel__open(struct evsel *evsel, struct

perf_cpu_map *cpus,

Quoted 1 line
Quoted 6 lines

tools/perf/util/evsel.c int evsel__open(struct evsel *evsel, struct perf_cpu_map *cpus, · line 2437

Quoted 4 lines

void evsel__close(struct evsel *evsel)
{

tools/perf/util/evsel.c +2 -0
+	if (evsel__is_retire_lat(evsel))
+		tpebs_delete();

Ditto.

Quoted 4 lines

Hi Namhyung,

I hope both this and the one above on open could stay in evsel level because these are operations on retire_latency evsel.

Then I think you need to remove the specific evsel not the all tpebs events.

Quoted 2 lines

At the same time, a lot of the
previous several versions of work was to move TPEBS code out from perf

stat to

Quoted 1 line

evsel to make it more generic. I think move these back to __run_perf_stat()

are

Quoted 1 line

opposite to that goal.

Quoted 8 lines

Oh, I meant you can have the logic in utils/intel-tpebs.c but add a call to tpebs_delete() in __run_perf_stat(). I think it'd better to keep it in evlist__close() but we don't use evlist__open() for perf stat so it's not symmetric. :(

Anyway, all I want to say is that tpebs APIs work on evlist level. So I think it's natural that they are called for the whole list, not for an event/evsel.

I think we're trying to work at evsel level and open(remove) or close one retire_latency evsel at a time. In addition to that, we put all the required retire_latency together in one perf record launch in order to reduce overhead to fork multiple perf record. I hope this makes sense.

Quoted 107 lines
Quoted 107 lines
Quoted 106 lines

tools/perf/util/evsel.c static int store_evsel_ids(struct evsel *evsel,

Quoted 8 lines
tools/perf/util/evsel.c +0 -0
 	perf_evsel__close(&evsel->core);
 	perf_evsel__free_id(&evsel->core);
 }
@@ -3341,6 +3400,9 @@ static int store_evsel_ids(struct evsel *evsel,

struct evlist *evlist)

tools/perf/util/evsel.c static int store_evsel_ids(struct evsel *evsel, struct evlist *evlist)

Quoted 7 lines
tools/perf/util/evsel.c +3 -0
 {
 	int cpu_map_idx, thread;
 
+	if (evsel__is_retire_lat(evsel))
+		return 0;
+
 	for (cpu_map_idx = 0; cpu_map_idx < xyarray__max_x(evsel->core.fd);

cpu_map_idx++) {

Quoted 32 lines
tools/perf/util/evsel.c +0 -0
 		for (thread = 0; thread < xyarray__max_y(evsel->core.fd);
 		     thread++) {
tools/perf/util/intel-tpebs.c new file +24 -0
new file mode 100644
index 000000000000..37b7a4f92dd9
@@ -0,0 +1,397 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * intel_tpebs.c: Intel TPEBS support
+ */
+
+
+#include <sys/param.h>
+#include <subcmd/run-command.h>
+#include <thread.h>
+#include "intel-tpebs.h"
+#include <linux/list.h>
+#include <linux/zalloc.h>
+#include <linux/err.h>
+#include "sample.h"
+#include "debug.h"
+#include "evlist.h"
+#include "evsel.h"
+#include "session.h"
+#include "tool.h"
+#include "cpumap.h"
+#include "metricgroup.h"
+#include <sys/stat.h>
+#include <sys/file.h>
+#include <poll.h>

tools/perf/util/intel-tpebs.c line 34

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+#include <math.h>
+
+#define PERF_DATA		"-"
+
+bool tpebs_recording;
+static pid_t tpebs_pid = -1;
+static size_t tpebs_event_size;
+static pthread_t tpebs_reader_thread;
+static struct child_process *tpebs_cmd;
+static struct list_head tpebs_results = LIST_HEAD_INIT(tpebs_results);

It can be 'static LIST_HEAD(tpebs_results);'

Quoted 30 lines
tools/perf/util/intel-tpebs.c new file +30 -0
+
+struct tpebs_retire_lat {
+	struct list_head nd;
+	/* Event name */
+	const char *name;
+	/* Event name with the TPEBS modifier R */
+	const char *tpebs_name;
+	/* Count of retire_latency values found in sample data */
+	size_t count;
+	/* Sum of all the retire_latency values in sample data */
+	int sum;
+	/* Average of retire_latency, val = sum / count */
+	double val;
+};
+
+static int get_perf_record_args(const char **record_argv, char buf[],
+				const char *cpumap_buf)
+{
+	struct tpebs_retire_lat *e;
+	int i = 0;
+
+	pr_debug("Prepare perf record for retire_latency\n");
+
+	record_argv[i++] = "perf";
+	record_argv[i++] = "record";
+	record_argv[i++] = "-W";
+	record_argv[i++] = "--synth=no";
+	record_argv[i++] = buf;
+
+	if (cpumap_buf) {

tools/perf/util/intel-tpebs.c line 74

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+		record_argv[i++] = "-C";
+		record_argv[i++] = cpumap_buf;
+	}
+
+	record_argv[i++] = "-a";
+
+	if (!cpumap_buf) {
+		pr_err("Require cpumap list to run sampling.\n");
+		return -ECANCELED;
+	}

Hmm.. I thought you supported system wide collection, not sure if it has a cpumap. Anyway this check makes the earlier one meaningless - you need the cpumap always, right?

Quoted 5 lines
Quoted 1 line

TPEBS should be work with "-a" or "-C". I'm not sure what the cpumap

would be

Quoted 1 line

for "-a". Would it make sense to add "-a" only when cpumap_buf is NULL?

I think the best way is to check target__has_cpu().

Yes this is an ideal way to check. But since the tpebs_start() is called in evsel, I'm wondering do we still have target here?

Quoted 163 lines
Quoted 45 lines
Quoted 45 lines
Quoted 23 lines
tools/perf/util/intel-tpebs.c new file +20 -0
+
+	list_for_each_entry(e, &tpebs_results, nd) {
+		record_argv[i++] = "-e";
+		record_argv[i++] = e->name;
+	}
+
+	record_argv[i++] = "-o";
+	record_argv[i++] = PERF_DATA;
+
+	return 0;
+}
+
+static int prepare_run_command(const char **argv)
+{
+	tpebs_cmd = zalloc(sizeof(struct child_process));
+	if (!tpebs_cmd)
+		return -ENOMEM;
+	tpebs_cmd->argv = argv;
+	tpebs_cmd->out = -1;
+	return 0;

tools/perf/util/intel-tpebs.c

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+}
+
+static int start_perf_record(int control_fd[], int ack_fd[],
+				const char *cpumap_buf)
+{
+	const char **record_argv;
+	int ret;
+	char buf[32];
+
+	scnprintf(buf, sizeof(buf), "--control=fd:%d,%d", control_fd[0],

ack_fd[1]);

Quoted 11 lines
tools/perf/util/intel-tpebs.c new file +11 -0
+
+	record_argv = calloc(12 + 2 * tpebs_event_size, sizeof(char *));
+	if (!record_argv)
+		return -ENOMEM;
+
+	ret = get_perf_record_args(record_argv, buf, cpumap_buf);
+	if (ret)
+		goto out;
+
+	ret = prepare_run_command(record_argv);
+	if (ret)
Quoted 8 lines
Quoted 8 lines

tools/perf/util/intel-tpebs.c

Quoted 8 lines
tools/perf/util/intel-tpebs.c new file +8 -0
+		goto out;
+	ret = start_command(tpebs_cmd);
+out:
+	free(record_argv);
+	return ret;
+}
+
+static int process_sample_event(struct perf_tool *tool

__maybe_unused,

Quoted 109 lines
Quoted 108 lines
Quoted 4 lines
tools/perf/util/intel-tpebs.c new file +4 -0
+				union perf_event *event __maybe_unused,
+				struct perf_sample *sample,
+				struct evsel *evsel,
+				struct machine *machine __maybe_unused)

tools/perf/util/intel-tpebs.c

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

of

Quoted 51 lines
tools/perf/util/intel-tpebs.c new file +51 -0
+	 * retire latency value for each event.
+	 */
+	list_for_each_entry(t, &tpebs_results, nd) {
+		if (!strcmp(evname, t->name)) {
+			t->count += 1;
+			t->sum += sample->retire_lat;
+			t->val = (double) t->sum / t->count;
+			break;
+		}
+	}
+
+	return ret;
+}
+
+static int process_feature_event(struct perf_session *session,
+				 union perf_event *event)
+{
+	if (event->feat.feat_id < HEADER_LAST_FEATURE)
+		return perf_event__process_feature(session, event);
+	return 0;
+}
+
+static void *__sample_reader(void *arg)
+{
+	struct child_process *child = arg;
+	struct perf_session *session;
+	struct perf_data data = {
+		.mode = PERF_DATA_MODE_READ,
+		.path = PERF_DATA,
+		.file.fd = child->out,
+	};
+	struct perf_tool tool = {
+		.sample = process_sample_event,
+		.feature = process_feature_event,
+		.attr = perf_event__process_attr,
+	};
+
+	session = perf_session__new(&data, &tool);
+	if (IS_ERR(session))
+		return NULL;
+	perf_session__process_events(session);
+	perf_session__delete(session);
+
+	return NULL;
+}
+
+int tpebs_start(struct evlist *evsel_list, struct perf_cpu_map *cpus)
+{
+	int ret = 0;
+	struct evsel *evsel;
+	char cpumap_buf[50];

tools/perf/util/intel-tpebs.c line 198

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+
+	/*
+	 * We should only run tpebs_start when tpebs_recording is enabled.
+	 * And we should only run it once with all the required events.
+	 */
+	if (tpebs_pid != -1 || !tpebs_recording)
+		return 0;
+
+	cpu_map__snprint(cpus, cpumap_buf, sizeof(cpumap_buf));
+	pr_debug("cpu map: %s\n", cpumap_buf);

Can you please remove unnecessary debug prints? If you really want it, then make it more meaningful like with more context.

tools/perf/util/intel-tpebs.c

Quoted 3 lines
tools/perf/util/intel-tpebs.c new file +3 -0
+
+	/*
+	 * Prepare perf record for sampling event retire_latency before fork

and

tools/perf/util/intel-tpebs.c line 210

Quoted 9 lines
tools/perf/util/intel-tpebs.c new file +9 -0
+	 * prepare workload
+	 */
+	evlist__for_each_entry(evsel_list, evsel) {
+		struct tpebs_retire_lat *new = zalloc(sizeof(*new));
+		char *name;
+		int i;
+
+		if (!evsel->retire_lat)
+			continue;

Please move the allocation after this.

tools/perf/util/intel-tpebs.c line 212

Quoted 2 lines
tools/perf/util/intel-tpebs.c new file +2 -0
+
+		pr_debug("perf stat retire latency of event %s required\n",

evsel->name);

tools/perf/util/intel-tpebs.c line 219

Quoted 7 lines
tools/perf/util/intel-tpebs.c new file +7 -0
+		if (!new) {
+			ret = -1;
+			goto err;
+		}
+		for (i = strlen(evsel->name) - 1; i > 0; i--) {
+			if (evsel->name[i] == 'R')
+				break;

I think you also need to check ':' or '/'..

Quoted 6 lines
Quoted 2 lines

I think the ':' and '/' have already been checked by the evsel parser to make this evsel a retire_latency evsel. Do we need to check them again here?

Ok, it's fine if it's checked already. Then I thought you could just use strrchr() but it seems you want the index of 'R' to modify the copied string.

Yes, I'm trying to get the index and replace it with 'p' for sampling.

Quoted 132 lines
Quoted 17 lines
Quoted 17 lines
Quoted 7 lines
tools/perf/util/intel-tpebs.c new file +4 -0
+		}
+		if (i <= 0 || evsel->name[i] != 'R') {
+			ret = -1;
+			goto err;

tools/perf/util/intel-tpebs.c line 233

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+		}
+
+		name = strdup(evsel->name);
+		if (!name) {
+			ret = -ENOMEM;
+			goto err;
+		}
+		name[i] = 'p';
+		new->name = name;
+		new->tpebs_name = strdup(evsel->name);

Is this just to find the original evsel later? Then I think you can compare the pointer directly instead of name.

Quoted 106 lines
Quoted 6 lines

tools/perf/util/intel-tpebs.c line 236

Quoted 3 lines
tools/perf/util/intel-tpebs.c new file +3 -0
+		if (!new->tpebs_name) {
+			ret = -ENOMEM;
+			goto err;

This error handling (including above) will leak 'new' because it's not linked to the list yet.

Yes, you are right. I will fix this.

Quoted 88 lines
Quoted 17 lines
tools/perf/util/intel-tpebs.c new file +16 -0
+		}
+		list_add_tail(&new->nd, &tpebs_results);
+		tpebs_event_size += 1;
+	}
+
+	if (tpebs_event_size > 0) {
+		int control_fd[2], ack_fd[2], len;
+		char ack_buf[8];
+
+		/*Create control and ack fd for --control*/
+		if (pipe(control_fd) < 0) {
+			pr_err("Failed to create control fifo");
+			ret = -1;
+			goto out;
+		}
+		if (pipe(ack_fd) < 0) {

tools/perf/util/intel-tpebs.c

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+			pr_err("Failed to create control fifo");
+			ret = -1;
+			goto out;
+		}
+
+		ret = start_perf_record(control_fd, ack_fd, cpumap_buf);
+		if (ret)
+			goto out;
+		tpebs_pid = tpebs_cmd->pid;
+		if (pthread_create(&tpebs_reader_thread, NULL,

__sample_reader, tpebs_cmd)) {

tools/perf/util/intel-tpebs.c

Quoted 3 lines
tools/perf/util/intel-tpebs.c new file +3 -0
+			kill(tpebs_cmd->pid, SIGTERM);
+			close(tpebs_cmd->out);
+			pr_err("Could not create thread to process sample

data.\n");

tools/perf/util/intel-tpebs.c

Quoted 8 lines
tools/perf/util/intel-tpebs.c new file +8 -0
+			ret = -1;
+			goto out;
+		}
+		/* Wait for perf record initialization.*/
+		len = strlen("enable");
+		ret = write(control_fd[1], "enable", len);
+		if (ret != len) {
+			pr_err("perf record control write control message

failed\n");

Quoted 47 lines
tools/perf/util/intel-tpebs.c new file +47 -0
+			goto out;
+		}
+
+		ret = read(ack_fd[0], ack_buf, sizeof(ack_buf));
+		if (ret > 0)
+			ret = strcmp(ack_buf, "ack\n");
+		else {
+			pr_err("perf record control ack failed\n");
+			goto out;
+		}
+		pr_debug("Received ack from perf record\n");
+out:
+		close(control_fd[0]);
+		close(control_fd[1]);
+		close(ack_fd[0]);
+		close(ack_fd[1]);
+	}
+err:
+	if (ret)
+		tpebs_delete();
+	return ret;
+}
+
+int tpebs_stop(void)
+{
+	int ret = 0;
+
+	/* Like tpebs_start, we should only run tpebs_end once. */
+	if (tpebs_pid != -1) {
+		kill(tpebs_cmd->pid, SIGTERM);
+		tpebs_pid = -1;
+		pthread_join(tpebs_reader_thread, NULL);
+		close(tpebs_cmd->out);
+		ret = finish_command(tpebs_cmd);
+		if (ret == -ERR_RUN_COMMAND_WAITPID_SIGNAL)
+			ret = 0;
+	}
+	return ret;
+}
+
+int tpebs_set_evsel(struct evsel *evsel, int cpu_map_idx, int thread)
+{
+	struct perf_counts_values *count;
+	struct tpebs_retire_lat *t;
+	bool found = false;
+	__u64 val;
+	int ret;
Quoted 8 lines

tools/perf/util/intel-tpebs.c line 326

Quoted 6 lines
tools/perf/util/intel-tpebs.c new file +6 -0
+
+	/* Non reitre_latency evsel should never enter this function. */
+	if (!evsel__is_retire_lat(evsel))
+		return -1;
+
+	ret = tpebs_stop();

I think it's better to call this in the upper layer.

Quoted 1 line

The tpebs_stop() is also called in sig_atexit() in perf stat. I want to make this

function

Quoted 1 line

safe to be called multiple times and add one tpebs_stop() here in case

tpebs_set_evsel()

Quoted 9 lines
Quoted 1 line

is invoked before tpebs_stop(). I hope this makes sense.

I guess it's ok to call tpebs_set_evsel() even if it's not stopped. It seems you use tpebs_retire_lat->val only and it's an average so it won't change much if you read either before or after concurrent changes.

It can be useful to the interval mode if you keep the tpebs background process after read.

Ok, I will remove the tpebs_stop() here if that works fine.

Thanks,
Weilin

Quoted 149 lines

Thanks,
Namhyung

Quoted 146 lines
Quoted 25 lines
Quoted 14 lines
tools/perf/util/intel-tpebs.c new file +7 -0
+	if (ret)
+		return ret;
+
+	count = perf_counts(evsel->counts, cpu_map_idx, thread);
+
+	list_for_each_entry(t, &tpebs_results, nd) {
+		if (!strcmp(t->tpebs_name, evsel->name) || !strcmp(t-

tpebs_name, evsel->metric_id)) {

tools/perf/util/intel-tpebs.c new file +3 -0
+			found = true;
+			break;
+		}

tools/perf/util/intel-tpebs.c

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+	}
+
+	/* Set ena and run to non-zero */
+	count->ena = count->run = 1;
+	count->lost = 0;
+
+	if (!found) {
+		/*
+		 * Set default value or 0 when retire_latency for this event is
+		 * not found from sampling data (enable_tpebs_recording not

set

Quoted 5 lines

tools/perf/util/intel-tpebs.c line 349

Quoted 3 lines
tools/perf/util/intel-tpebs.c new file +3 -0
+		 * or 0 sample recorded).
+		 */
+		val = 0;

Shouldn't it set count->val to 0?

Yes, I will fix this one.

Thanks,
Weilin

Quoted 111 lines

Thanks,
Namhyung

Quoted 67 lines
tools/perf/util/intel-tpebs.c new file +48 -0
+		return 0;
+	}
+
+	/*
+	 * Only set retire_latency value to the first CPU and thread.
+	 */
+	if (cpu_map_idx == 0 && thread == 0)
+		val = rint(t->val);
+	else
+		val = 0;
+
+	count->val = val;
+	return 0;
+}
+
+static void tpebs_retire_lat__delete(struct tpebs_retire_lat *r)
+{
+	zfree(&r->name);
+	zfree(&r->tpebs_name);
+	free(r);
+}
+
+void tpebs_delete(void)
+{
+	struct tpebs_retire_lat *r, *rtmp;
+
+	list_for_each_entry_safe(r, rtmp, &tpebs_results, nd) {
+		list_del_init(&r->nd);
+		tpebs_retire_lat__delete(r);
+	}
+
+	if (tpebs_cmd) {
+		free(tpebs_cmd);
+		tpebs_cmd = NULL;
+	}
+}
+
+int tpebs_stop_delete(void)
+{
+	int ret;
+
+	if (tpebs_pid == -1)
+		return 0;
+
+	ret = tpebs_stop();
+	tpebs_delete();
+	return ret;
+}
tools/perf/util/intel-tpebs.h new file +13 -0
new file mode 100644
index 000000000000..73c1e5219522
@@ -0,0 +1,48 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * intel_tpebs.h: Intel TEPBS support
+ */
+#ifndef INCLUDE__PERF_INTEL_TPEBS_H__
+#define INCLUDE__PERF_INTEL_TPEBS_H__
+
+#include "stat.h"
+#include "evsel.h"
+
+#ifdef HAVE_ARCH_X86_64_SUPPORT
+
+extern bool tpebs_recording;

tools/perf/util/intel-tpebs.h line 23

Quoted 10 lines
tools/perf/util/intel-tpebs.h new file +10 -0
+int tpebs_start(struct evlist *evsel_list, struct perf_cpu_map *cpus);
+int tpebs_stop(void);
+void tpebs_delete(void);
+int tpebs_set_evsel(struct evsel *evsel, int cpu_map_idx, int thread);
+int tpebs_stop_delete(void);
+
+#else
+
+static inline int tpebs_start(struct evlist *evsel_list __maybe_unused,
+				struct perf_cpu_map *cpus

__maybe_unused)

Quoted 28 lines
tools/perf/util/intel-tpebs.h new file +25 -1
+{
+	return 0;
+}
+
+static inline int tpebs_stop(void)
+{
+	return 0;
+}
+
+static inline void tpebs_delete(void) {};
+
+static inline int tpebs_set_evsel(struct evsel *evsel  __maybe_unused,
+				int cpu_map_idx  __maybe_unused,
+				int thread  __maybe_unused)
+{
+	return 0;
+}
+
+static inline int tpebs_stop_delete(void)
+{
+	return 0;
+}
+
+#endif
+#endif
--

2.43.0

maintainer
#8 lore raw

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

Quoted 18 lines · Weilin
Quoted 18 lines

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

On Fri, Jun 07, 2024 at 01:07:12AM +0000, Wang, Weilin wrote:

[SNIP]

Quoted 46 lines
Quoted 46 lines
Quoted 26 lines
Quoted 19 lines

Review static int evsel__open_cpu(struct evsel *evsel,

Quoted 1 line
(quoted diff) +0 -0
@@ -2186,6 +2240,9 @@ static int evsel__open_cpu(struct evsel *evsel,

struct perf_cpu_map *cpus,

tools/perf/util/evsel.c static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus, · line 2244

Quoted 5 lines
tools/perf/util/evsel.c +2 -0
 		return 0;
 	}
 
+	if (evsel__is_retire_lat(evsel))
+		return tpebs_start(evsel->evlist, cpus);

As it works with evlist, I think it's better to put this code there. But it seems perf stat doesn't call the evlist API for open, then we can add this to somewhere in __run_perf_stat() directly.

tools/perf/util/evsel.c int evsel__open(struct evsel *evsel, struct

Quoted 5 lines
tools/perf/util/evsel.c +1 -0
+
 	err = __evsel__prepare_open(evsel, cpus, threads);
 	if (err)
 		return err;
@@ -2376,6 +2433,8 @@ int evsel__open(struct evsel *evsel, struct

perf_cpu_map *cpus,

Quoted 1 line
Quoted 6 lines

tools/perf/util/evsel.c int evsel__open(struct evsel *evsel, struct perf_cpu_map *cpus, · line 2437

Quoted 4 lines

void evsel__close(struct evsel *evsel)
{

tools/perf/util/evsel.c +2 -0
+	if (evsel__is_retire_lat(evsel))
+		tpebs_delete();

Ditto.

Quoted 4 lines

Hi Namhyung,

I hope both this and the one above on open could stay in evsel level because these are operations on retire_latency evsel.

Then I think you need to remove the specific evsel not the all tpebs events.

Quoted 2 lines

At the same time, a lot of the
previous several versions of work was to move TPEBS code out from perf

stat to

Quoted 1 line

evsel to make it more generic. I think move these back to __run_perf_stat()

are

Quoted 1 line

opposite to that goal.

Oh, I meant you can have the logic in utils/intel-tpebs.c but add a call to tpebs_delete() in __run_perf_stat(). I think it'd better to keep it in evlist__close() but we don't use evlist__open() for perf stat so it's not symmetric. :(

Quoted 8 lines
Quoted 3 lines

Anyway, all I want to say is that tpebs APIs work on evlist level. So I think it's natural that they are called for the whole list, not for an event/evsel.

I think we're trying to work at evsel level and open(remove) or close one retire_latency evsel at a time. In addition to that, we put all the required retire_latency together in one perf record launch in order to reduce overhead to fork multiple perf record. I hope this makes sense.

Well.. I think we can do something like this in the current code.

__run_perf_stat():
...

  tpebs__start(evlist, target);

  evlist__for_each_cpu(...) {
      if (create_perf_steat_counter() < 0) {
          ....

instead of doing it in the evsel__open(). What's the issue with this approach?

Quoted 108 lines
Quoted 108 lines
Quoted 108 lines
Quoted 107 lines

tools/perf/util/evsel.c static int store_evsel_ids(struct evsel *evsel,

Quoted 9 lines
tools/perf/util/evsel.c +0 -0
 	perf_evsel__close(&evsel->core);
 	perf_evsel__free_id(&evsel->core);
 }
@@ -3341,6 +3400,9 @@ static int store_evsel_ids(struct evsel *evsel,

struct evlist *evlist)

tools/perf/util/evsel.c static int store_evsel_ids(struct evsel *evsel, struct evlist *evlist)

Quoted 7 lines
tools/perf/util/evsel.c +3 -0
 {
 	int cpu_map_idx, thread;
 
+	if (evsel__is_retire_lat(evsel))
+		return 0;
+
 	for (cpu_map_idx = 0; cpu_map_idx < xyarray__max_x(evsel->core.fd);

cpu_map_idx++) {

Quoted 32 lines
tools/perf/util/evsel.c +0 -0
 		for (thread = 0; thread < xyarray__max_y(evsel->core.fd);
 		     thread++) {
tools/perf/util/intel-tpebs.c new file +24 -0
new file mode 100644
index 000000000000..37b7a4f92dd9
@@ -0,0 +1,397 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * intel_tpebs.c: Intel TPEBS support
+ */
+
+
+#include <sys/param.h>
+#include <subcmd/run-command.h>
+#include <thread.h>
+#include "intel-tpebs.h"
+#include <linux/list.h>
+#include <linux/zalloc.h>
+#include <linux/err.h>
+#include "sample.h"
+#include "debug.h"
+#include "evlist.h"
+#include "evsel.h"
+#include "session.h"
+#include "tool.h"
+#include "cpumap.h"
+#include "metricgroup.h"
+#include <sys/stat.h>
+#include <sys/file.h>
+#include <poll.h>

tools/perf/util/intel-tpebs.c line 34

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+#include <math.h>
+
+#define PERF_DATA		"-"
+
+bool tpebs_recording;
+static pid_t tpebs_pid = -1;
+static size_t tpebs_event_size;
+static pthread_t tpebs_reader_thread;
+static struct child_process *tpebs_cmd;
+static struct list_head tpebs_results = LIST_HEAD_INIT(tpebs_results);

It can be 'static LIST_HEAD(tpebs_results);'

Quoted 30 lines
tools/perf/util/intel-tpebs.c new file +30 -0
+
+struct tpebs_retire_lat {
+	struct list_head nd;
+	/* Event name */
+	const char *name;
+	/* Event name with the TPEBS modifier R */
+	const char *tpebs_name;
+	/* Count of retire_latency values found in sample data */
+	size_t count;
+	/* Sum of all the retire_latency values in sample data */
+	int sum;
+	/* Average of retire_latency, val = sum / count */
+	double val;
+};
+
+static int get_perf_record_args(const char **record_argv, char buf[],
+				const char *cpumap_buf)
+{
+	struct tpebs_retire_lat *e;
+	int i = 0;
+
+	pr_debug("Prepare perf record for retire_latency\n");
+
+	record_argv[i++] = "perf";
+	record_argv[i++] = "record";
+	record_argv[i++] = "-W";
+	record_argv[i++] = "--synth=no";
+	record_argv[i++] = buf;
+
+	if (cpumap_buf) {

tools/perf/util/intel-tpebs.c line 74

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+		record_argv[i++] = "-C";
+		record_argv[i++] = cpumap_buf;
+	}
+
+	record_argv[i++] = "-a";
+
+	if (!cpumap_buf) {
+		pr_err("Require cpumap list to run sampling.\n");
+		return -ECANCELED;
+	}

Hmm.. I thought you supported system wide collection, not sure if it has a cpumap. Anyway this check makes the earlier one meaningless - you need the cpumap always, right?

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

TPEBS should be work with "-a" or "-C". I'm not sure what the cpumap

would be

Quoted 1 line

for "-a". Would it make sense to add "-a" only when cpumap_buf is NULL?

I think the best way is to check target__has_cpu().

Yes this is an ideal way to check. But since the tpebs_start() is called in evsel, I'm wondering do we still have target here?

Please see above.

Thanks,
Namhyung

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
tools/perf/util/intel-tpebs.c new file +11 -0
+
+	list_for_each_entry(e, &tpebs_results, nd) {
+		record_argv[i++] = "-e";
+		record_argv[i++] = e->name;
+	}
+
+	record_argv[i++] = "-o";
+	record_argv[i++] = PERF_DATA;
+
+	return 0;
+}
#9 lore raw

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

Quoted 105 lines

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

Quoted 12 lines · Weilin
Quoted 12 lines

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

Perry

Quoted 1 line
Quoted 1 line

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

Biggers,

Quoted 2 lines
Quoted 2 lines

Caleb <[email protected]>
Subject: Re: [RFC PATCH v11 3/8] perf stat: Fork and launch perf record

when

Quoted 3 lines
Quoted 3 lines

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

On Fri, Jun 07, 2024 at 01:07:12AM +0000, Wang, Weilin wrote:

[SNIP]

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

Review static int evsel__open_cpu(struct evsel

Quoted 1 line
(quoted diff) +0 -0
@@ -2186,6 +2240,9 @@ static int evsel__open_cpu(struct evsel

*evsel,

Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 24 lines

struct perf_cpu_map *cpus,

tools/perf/util/evsel.c static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus, · line 2244

Quoted 5 lines
tools/perf/util/evsel.c +2 -0
 		return 0;
 	}
 
+	if (evsel__is_retire_lat(evsel))
+		return tpebs_start(evsel->evlist, cpus);

As it works with evlist, I think it's better to put this code there. But it seems perf stat doesn't call the evlist API for open, then we can add this to somewhere in __run_perf_stat() directly.

tools/perf/util/evsel.c int evsel__open(struct evsel *evsel, struct

Quoted 5 lines
tools/perf/util/evsel.c +1 -0
+
 	err = __evsel__prepare_open(evsel, cpus, threads);
 	if (err)
 		return err;
@@ -2376,6 +2433,8 @@ int evsel__open(struct evsel *evsel, struct

perf_cpu_map *cpus,

tools/perf/util/evsel.c int evsel__open(struct evsel *evsel, struct perf_cpu_map *cpus, · line 2437

Quoted 5 lines
tools/perf/util/evsel.c +2 -0
 void evsel__close(struct evsel *evsel)
 {
+	if (evsel__is_retire_lat(evsel))
+		tpebs_delete();

Ditto.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Hi Namhyung,

I hope both this and the one above on open could stay in evsel level

because

Quoted 9 lines
Quoted 9 lines
Quoted 1 line

these are operations on retire_latency evsel.

Then I think you need to remove the specific evsel not the all tpebs events.

Quoted 2 lines

At the same time, a lot of the
previous several versions of work was to move TPEBS code out from perf

stat to

Quoted 1 line

evsel to make it more generic. I think move these back to

__run_perf_stat()

Quoted 8 lines
Quoted 8 lines

are

Quoted 1 line

opposite to that goal.

Oh, I meant you can have the logic in utils/intel-tpebs.c but add a call to tpebs_delete() in __run_perf_stat(). I think it'd better to keep it in evlist__close() but we don't use evlist__open() for perf stat so it's not symmetric. :(

Quoted 6 lines
Quoted 3 lines

Anyway, all I want to say is that tpebs APIs work on evlist level. So I think it's natural that they are called for the whole list, not for an event/evsel.

I think we're trying to work at evsel level and open(remove) or close one retire_latency evsel at a time. In addition to that, we put all the required

retire_latency

Quoted 1 line

together in one perf record launch in order to reduce overhead to fork

multiple perf

Quoted 1 line

record. I hope this makes sense.

Well.. I think we can do something like this in the current code.

__run_perf_stat():
...

Quoted 8 lines
  tpebs__start(evlist, target);

  evlist__for_each_cpu(...) {
      if (create_perf_steat_counter() < 0) {
          ....

instead of doing it in the evsel__open(). What's the issue with this approach?

This is basically how tpebs__start() was invoked in v9 (https://lore.kernel.org/all/CAM9d7ci7tgjR8LVNx+ZrFKMGo+OZn=eFSksPL56MeP_Q84PkMw@mail.gmail.com/)

I changed it in v10 so that it works at evsel level.

Ian, could you please let me know what do you think about this?

Thanks,
Weilin

Quoted 144 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

tools/perf/util/evsel.c static int store_evsel_ids(struct evsel

Quoted 10 lines
tools/perf/util/evsel.c +0 -0
 	perf_evsel__close(&evsel->core);
 	perf_evsel__free_id(&evsel->core);
 }
@@ -3341,6 +3400,9 @@ static int store_evsel_ids(struct evsel

*evsel,

Quoted 45 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

struct evlist *evlist)

tools/perf/util/evsel.c static int store_evsel_ids(struct evsel *evsel, struct evlist *evlist)

Quoted 7 lines
tools/perf/util/evsel.c +3 -0
 {
 	int cpu_map_idx, thread;
 
+	if (evsel__is_retire_lat(evsel))
+		return 0;
+
 	for (cpu_map_idx = 0; cpu_map_idx < xyarray__max_x(evsel-

core.fd);

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

cpu_map_idx++) {

Quoted 1 line
(quoted diff) +0 -0
 		for (thread = 0; thread < xyarray__max_y(evsel-

core.fd);

Quoted 33 lines
Quoted 33 lines
Quoted 33 lines
Quoted 33 lines
tools/perf/util/evsel.c +0 -0
 		     thread++) {
tools/perf/util/intel-tpebs.c new file +26 -0
new file mode 100644
index 000000000000..37b7a4f92dd9
@@ -0,0 +1,397 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * intel_tpebs.c: Intel TPEBS support
+ */
+
+
+#include <sys/param.h>
+#include <subcmd/run-command.h>
+#include <thread.h>
+#include "intel-tpebs.h"
+#include <linux/list.h>
+#include <linux/zalloc.h>
+#include <linux/err.h>
+#include "sample.h"
+#include "debug.h"
+#include "evlist.h"
+#include "evsel.h"
+#include "session.h"
+#include "tool.h"
+#include "cpumap.h"
+#include "metricgroup.h"
+#include <sys/stat.h>
+#include <sys/file.h>
+#include <poll.h>
+#include <math.h>
+
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

tools/perf/util/intel-tpebs.c

Quoted 8 lines
tools/perf/util/intel-tpebs.c new file +8 -0
+#define PERF_DATA		"-"
+
+bool tpebs_recording;
+static pid_t tpebs_pid = -1;
+static size_t tpebs_event_size;
+static pthread_t tpebs_reader_thread;
+static struct child_process *tpebs_cmd;
+static struct list_head tpebs_results =

LIST_HEAD_INIT(tpebs_results);

Quoted 37 lines
Quoted 37 lines
Quoted 37 lines
Quoted 37 lines

It can be 'static LIST_HEAD(tpebs_results);'

Quoted 34 lines
tools/perf/util/intel-tpebs.c new file +34 -0
+
+struct tpebs_retire_lat {
+	struct list_head nd;
+	/* Event name */
+	const char *name;
+	/* Event name with the TPEBS modifier R */
+	const char *tpebs_name;
+	/* Count of retire_latency values found in sample data */
+	size_t count;
+	/* Sum of all the retire_latency values in sample data */
+	int sum;
+	/* Average of retire_latency, val = sum / count */
+	double val;
+};
+
+static int get_perf_record_args(const char **record_argv, char buf[],
+				const char *cpumap_buf)
+{
+	struct tpebs_retire_lat *e;
+	int i = 0;
+
+	pr_debug("Prepare perf record for retire_latency\n");
+
+	record_argv[i++] = "perf";
+	record_argv[i++] = "record";
+	record_argv[i++] = "-W";
+	record_argv[i++] = "--synth=no";
+	record_argv[i++] = buf;
+
+	if (cpumap_buf) {
+		record_argv[i++] = "-C";
+		record_argv[i++] = cpumap_buf;
+	}
+
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

tools/perf/util/intel-tpebs.c line 74

Quoted 6 lines
tools/perf/util/intel-tpebs.c new file +6 -0
+	record_argv[i++] = "-a";
+
+	if (!cpumap_buf) {
+		pr_err("Require cpumap list to run sampling.\n");
+		return -ECANCELED;
+	}

Hmm.. I thought you supported system wide collection, not sure if it

has

Quoted 6 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

a cpumap. Anyway this check makes the earlier one meaningless - you need the cpumap always, right?

TPEBS should be work with "-a" or "-C". I'm not sure what the cpumap

would be

Quoted 1 line

for "-a". Would it make sense to add "-a" only when cpumap_buf is

NULL?

Quoted 3 lines
Quoted 2 lines

I think the best way is to check target__has_cpu().

Yes this is an ideal way to check. But since the tpebs_start() is called in evsel,

I'm

Quoted 1 line

wondering do we still have target here?

Please see above.

Thanks,
Namhyung

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
tools/perf/util/intel-tpebs.c new file +11 -0
+
+	list_for_each_entry(e, &tpebs_results, nd) {
+		record_argv[i++] = "-e";
+		record_argv[i++] = e->name;
+	}
+
+	record_argv[i++] = "-o";
+	record_argv[i++] = PERF_DATA;
+
+	return 0;
+}
maintainer
#10 lore raw

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

Quoted 111 lines · Weilin
Quoted 111 lines

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

Quoted 12 lines · Weilin
Quoted 12 lines

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

Perry

Quoted 1 line
Quoted 1 line

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

Biggers,

Quoted 2 lines
Quoted 2 lines

Caleb <[email protected]>
Subject: Re: [RFC PATCH v11 3/8] perf stat: Fork and launch perf record

when

Quoted 3 lines
Quoted 3 lines

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

On Fri, Jun 07, 2024 at 01:07:12AM +0000, Wang, Weilin wrote:

[SNIP]

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

Review static int evsel__open_cpu(struct evsel

Quoted 1 line
(quoted diff) +0 -0
@@ -2186,6 +2240,9 @@ static int evsel__open_cpu(struct evsel

*evsel,

Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 24 lines

struct perf_cpu_map *cpus,

Review static int evsel__open_cpu(struct evsel · line 2244

Quoted 5 lines
(quoted diff) +2 -0
 		return 0;
 	}
 
+	if (evsel__is_retire_lat(evsel))
+		return tpebs_start(evsel->evlist, cpus);

As it works with evlist, I think it's better to put this code there. But it seems perf stat doesn't call the evlist API for open, then we can add this to somewhere in __run_perf_stat() directly.

Review int evsel__open(struct evsel *evsel, struct

Quoted 5 lines
(quoted diff) +1 -0
+
 	err = __evsel__prepare_open(evsel, cpus, threads);
 	if (err)
 		return err;
@@ -2376,6 +2433,8 @@ int evsel__open(struct evsel *evsel, struct

perf_cpu_map *cpus,

Review int evsel__open(struct evsel *evsel, struct · line 2436

Quoted 5 lines
(quoted diff) +2 -0
 void evsel__close(struct evsel *evsel)
 {
+	if (evsel__is_retire_lat(evsel))
+		tpebs_delete();

Ditto.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Hi Namhyung,

I hope both this and the one above on open could stay in evsel level

because

Quoted 9 lines
Quoted 9 lines
Quoted 1 line

these are operations on retire_latency evsel.

Then I think you need to remove the specific evsel not the all tpebs events.

Quoted 2 lines

At the same time, a lot of the
previous several versions of work was to move TPEBS code out from perf

stat to

Quoted 1 line

evsel to make it more generic. I think move these back to

__run_perf_stat()

Quoted 8 lines
Quoted 8 lines

are

Quoted 1 line

opposite to that goal.

Oh, I meant you can have the logic in utils/intel-tpebs.c but add a call to tpebs_delete() in __run_perf_stat(). I think it'd better to keep it in evlist__close() but we don't use evlist__open() for perf stat so it's not symmetric. :(

Quoted 6 lines
Quoted 3 lines

Anyway, all I want to say is that tpebs APIs work on evlist level. So I think it's natural that they are called for the whole list, not for an event/evsel.

I think we're trying to work at evsel level and open(remove) or close one retire_latency evsel at a time. In addition to that, we put all the required

retire_latency

Quoted 1 line

together in one perf record launch in order to reduce overhead to fork

multiple perf

Quoted 1 line

record. I hope this makes sense.

Well.. I think we can do something like this in the current code.

__run_perf_stat():
...

  tpebs__start(evlist, target);

  evlist__for_each_cpu(...) {
      if (create_perf_steat_counter() < 0) {
          ....
Quoted 8 lines
Quoted 2 lines

instead of doing it in the evsel__open(). What's the issue with this approach?

This is basically how tpebs__start() was invoked in v9 (https://lore.kernel.org/all/CAM9d7ci7tgjR8LVNx+ZrFKMGo+OZn=eFSksPL56MeP_Q84PkMw@mail.gmail.com/)

I changed it in v10 so that it works at evsel level.

Ian, could you please let me know what do you think about this?

Ok, we sync-ed offline and agreed to have it in evsel level. I still think it's better to handle it in evlist level (at least for TPEBS) but unfortunately we don't use evlist__open() consistently and there are places it's not called. Probably we need to convert the all call sites to open evsel to be from evlist__open() then move tpebs__start() there.

Thanks,
Namhyung

#11 lore raw

RFC PATCH v11 4/8 perf stat: Plugin retire_lat value from sampled data to evsel

In reply to [email protected]

From: Weilin Wang <[email protected]>

In current :R parsing implementation, the parser would recognize events with retire_latency modifier and insert them into the evlist like a normal event. Ideally, we need to avoid counting these events.

In this commit, at the time when a retire_latency evsel is read, set the retire latency value processed from the sampled data to count value. This sampled retire latency value will be used for metric calculation and final event count print out. No special metric calculation and event print out code required for retire_latency events.

---
 tools/perf/arch/x86/util/evlist.c |  6 ++++++
 tools/perf/util/evsel.c           | 15 +++++++++++++++
 tools/perf/util/evsel.h           |  5 +++++
 3 files changed, 26 insertions(+)
tools/perf/arch/x86/util/evlist.c +6 -0
index b1ce0c52d88d..cebdd483149e 100644
@@ -89,6 +89,12 @@ int arch_evlist__cmp(const struct evsel *lhs, const struct evsel *rhs)
 			return 1;
 	}
 
+	/* Retire latency event should not be group leader*/
+	if (lhs->retire_lat && !rhs->retire_lat)
+		return 1;
+	if (!lhs->retire_lat && rhs->retire_lat)
+		return -1;
+
 	/* Default ordering by insertion index. */
 	return lhs->core.idx - rhs->core.idx;
 }
tools/perf/util/evsel.c +15 -0
index bd3627819afe..a54806179555 100644
@@ -58,6 +58,7 @@
 #include <internal/xyarray.h>
 #include <internal/lib.h>
 #include <internal/threadmap.h>
+#include "util/intel-tpebs.h"
 
 #include <linux/ctype.h>
 
@@ -1523,6 +1524,11 @@ static int evsel__read_one(struct evsel *evsel, int cpu_map_idx, int thread)
 	return perf_evsel__read(&evsel->core, cpu_map_idx, thread, count);
 }
 
+static int evsel__read_retire_lat(struct evsel *evsel, int cpu_map_idx, int thread)
+{
+	return tpebs_set_evsel(evsel, cpu_map_idx, thread);
+}
+
 static void evsel__set_count(struct evsel *counter, int cpu_map_idx, int thread,
 			     u64 val, u64 ena, u64 run, u64 lost)
 {
@@ -1530,6 +1536,12 @@ static void evsel__set_count(struct evsel *counter, int cpu_map_idx, int thread,
 
 	count = perf_counts(counter->counts, cpu_map_idx, thread);
 
+	if (counter->retire_lat) {
+		evsel__read_retire_lat(counter, cpu_map_idx, thread);
+		perf_counts__set_loaded(counter->counts, cpu_map_idx, thread, true);
+		return;
+	}
+
 	count->val    = val;
 	count->ena    = ena;
 	count->run    = run;
@@ -1832,6 +1844,9 @@ int evsel__read_counter(struct evsel *evsel, int cpu_map_idx, int thread)
 	if (evsel__is_tool(evsel))
 		return evsel__read_tool(evsel, cpu_map_idx, thread);
 
+	if (evsel__is_retire_lat(evsel))
+		return evsel__read_retire_lat(evsel, cpu_map_idx, thread);
+
 	if (evsel->core.attr.read_format & PERF_FORMAT_GROUP)
 		return evsel__read_group(evsel, cpu_map_idx, thread);
 
tools/perf/util/evsel.h +5 -0
index bd8e84954e34..aaf572317e92 100644
@@ -303,6 +303,11 @@ static inline bool evsel__is_tool(const struct evsel *evsel)
 	return evsel->tool_event != PERF_TOOL_NONE;
 }
 
+static inline bool evsel__is_retire_lat(const struct evsel *evsel)
+{
+	return evsel->retire_lat;
+}
+
 const char *evsel__group_name(struct evsel *evsel);
 int evsel__group_desc(struct evsel *evsel, char *buf, size_t size);
 
Signature · 1 line
2.43.0
#12 lore raw

RFC PATCH v11 5/8 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.8. Some of the metrics' formulas use TPEBS retire_latency in MTL.

---
 .../arch/x86/meteorlake/metricgroups.json     |  140 +
 .../arch/x86/meteorlake/mtl-metrics.json      | 2595 +++++++++++++++++
 2 files changed, 2735 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 +140 -0
new file mode 100644
index 000000000000..05ce41fd07ae
@@ -0,0 +1,140 @@
+{
+    "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",
+    "BvBC": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "BvBO": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "BvCB": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "BvFB": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "BvIO": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "BvMB": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "BvML": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "BvMP": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "BvMS": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "BvMT": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "BvOB": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "BvUW": "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",
+    "CacheMisses": "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 +2595 -0

2,595 lines not on this page · raw message

Signature · 1 line
2.43.0
#13 lore raw

RFC PATCH v11 6/8 perf stat: Add command line option for enabling tpebs recording

In reply to [email protected]

From: Weilin Wang <[email protected]>

With this command line option, tpebs recording is turned off in perf stat on default. It will only be turned on when this option is given in perf stat command.

Exampe with --enable-tpebs-recording:

perf stat -M tma_dtlb_store -a --enable-tpebs-recording sleep 1

[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.000 MB - ]

Performance counter stats for 'system wide':

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

Exampe without --enable-tpebs-recording:

perf stat -M tma_dtlb_store -a sleep 1

Performance counter stats for 'system wide':

       181,047,168      cpu_core/TOPDOWN.SLOTS/          #      0.6 %  tma_dtlb_store
         3,195,608      cpu_core/topdown-retiring/
        40,156,649      cpu_core/topdown-mem-bound/
         3,550,925      cpu_core/topdown-bad-spec/
       117,571,818      cpu_core/topdown-fe-bound/
        57,118,087      cpu_core/topdown-be-bound/
            69,179      cpu_core/EXE_ACTIVITY.BOUND_ON_STORES/
             4,582      cpu_core/MEM_INST_RETIRED.STLB_HIT_STORES/
        30,183,104      cpu_core/CPU_CLK_UNHALTED.DISTRIBUTED/
        30,556,790      cpu_core/CPU_CLK_UNHALTED.THREAD/
           168,486      cpu_core/DTLB_STORE_MISSES.WALK_ACTIVE/
                 0      MEM_INST_RETIRED.STLB_HIT_STORES:R
       1.003105924 seconds time elapsed
---
 tools/perf/builtin-stat.c | 2 ++
 1 file changed, 2 insertions(+)
tools/perf/builtin-stat.c +2 -0
index b09cb2c6e9c2..4530e4caa417 100644
@@ -1237,6 +1237,8 @@ static struct option stat_options[] = {
 		       "disable adding events for the metric threshold calculation"),
 	OPT_BOOLEAN(0, "topdown", &topdown_run,
 			"measure top-down statistics"),
+	OPT_BOOLEAN(0, "enable-tpebs-recording", &tpebs_recording,
+			"enable recording for tpebs when retire_latency required"),
 	OPT_UINTEGER(0, "td-level", &stat_config.topdown_level,
 			"Set the metrics level for the top-down statistics (0: max level)"),
 	OPT_BOOLEAN(0, "smi-cost", &smi_cost,
Signature · 1 line
2.43.0
#14 lore raw

RFC PATCH v11 7/8 perf Document: Add TPEBS to Documents

In reply to [email protected]

From: Weilin Wang <[email protected]>

TPEBS is a new feature Intel PMU from Granite Rapids microarchitecture. It will be used in new TMA releases. Adding related introduction to documents while adding new code to support it in perf stat.

---
 tools/perf/Documentation/perf-list.txt |  1 +
 tools/perf/Documentation/topdown.txt   | 30 ++++++++++++++++++++++++++
 2 files changed, 31 insertions(+)
tools/perf/Documentation/perf-list.txt +1 -0
index 6bf2468f59d3..dea005410ec0 100644
@@ -72,6 +72,7 @@ counted. The following modifiers exist:
  W - group is weak and will fallback to non-group if not schedulable,
  e - group or event are exclusive and do not share the PMU
  b - use BPF aggregration (see perf stat --bpf-counters)
+ R - retire latency value of the event
 
 The 'p' modifier can be used for specifying how precise the instruction
 address should be. The 'p' modifier can be specified multiple times:
tools/perf/Documentation/topdown.txt +30 -0
index ae0aee86844f..98e5503552f5 100644
@@ -325,6 +325,36 @@ other four level 2 metrics by subtracting corresponding metrics as below.
     Fetch_Bandwidth = Frontend_Bound - Fetch_Latency
     Core_Bound = Backend_Bound - Memory_Bound
 
+TPEBS in TopDown
+================
+
+TPEBS (Timed PEBS) is one of the new Intel PMU features provided since Granite
+Rapids microarchitecture. The TPEBS feature adds a 16 bit retire_latency field
+in the Basic Info group of the PEBS record. It records the Core cycles since the
+retirement of the previous instruction to the retirement of current instruction.
+Please refer to Section 8.4.1 of "Intel® Architecture Instruction Set Extensions
+Programming Reference" for more details about this feature. Because this feature
+extends PEBS record, sampling with weight option is required to get the
+retire_latency value.
+
+	perf record -e event_name -W ...
+
+In the most recent release of TMA, the metrics begin to use event retire_latency
+values in some of the metrics’ formulas on processors that support TPEBS feature.
+For previous generations that do not support TPEBS, the values are static and
+predefined per processor family by the hardware architects. Due to the diversity
+of workloads in execution environments, retire_latency values measured at real
+time are more accurate. Therefore, new TMA metrics that use TPEBS will provide
+more accurate performance analysis results.
+
+To support TPEBS in TMA metrics, a new modifier :R on event is added. Perf would
+capture retire_latency value of required events(event with :R in metric formula)
+with perf record. The retire_latency value would be used in metric calculation.
+Currently, this feature is supported through perf stat
+
+	perf stat -M metric_name --enable-tpebs-recording ...
+
+
 
 [1] https://software.intel.com/en-us/top-down-microarchitecture-analysis-method-win
 [2] https://sites.google.com/site/analysismethods/yasin-pubs
Signature · 1 line
2.43.0
#15 lore raw

RFC PATCH v11 8/8 perf test: Add test for Intel TPEBS counting mode

In reply to [email protected]

From: Weilin Wang <[email protected]>

Intel TPEBS sampling mode is supported through perf record. The counting mode code uses perf record to capture retire_latency value and use it in metric calculation. This test checks the counting mode code.

---
 .../perf/tests/shell/test_stat_intel_tpebs.sh  | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
 create mode 100755 tools/perf/tests/shell/test_stat_intel_tpebs.sh
tools/perf/tests/shell/test_stat_intel_tpebs.sh new file +18 -0
new file mode 100755
index 000000000000..3c8763b39bd4
@@ -0,0 +1,18 @@
+#!/bin/bash
+# test Intel TPEBS counting mode
+# SPDX-License-Identifier: GPL-2.0
+
+set e
+
+# Use this event for testing because it should exist in all platforms
+e=cache-misses:R
+
+# Without this cmd option, default value or zero is returned
+echo "Testing without --enable-tpebs-recording"
+result=$(perf stat -e "$e" true 2>&1)
+[[ "$result" =~ "$e" ]] || exit 1
+
+# In platforms that do not support TPEBS, it should execute without error.
+echo "Testing with --enable-tpebs-recording"
+result=$(perf stat -e "$e" --enable-tpebs-recording -a sleep 0.01 2>&1)
+[[ "$result" =~ "perf record" && "$result" =~ "$e" ]] || exit 1
Signature · 1 line
2.43.0