Skip to contentSkip to conversation

TPEBS counting mode support

RFC PATCH Superseded [email protected] 2 participants 15 messages Jul 12, 2024 – Jul 20, 2024 perf events

v17 of 18 Superseded Latest is v18 →

lore
Share & tools
lore
#0 lore raw

v17 cover TPEBS counting mode support

From: Weilin Wang <[email protected]>

Changes in v17:
- Add a poll on control fifo ack_fd to ensure program returns successfully when perf record failed for any reason.
- Add a check in the tpebs test to only run on Intel platforms.

Changes in v16:
- Update tpebs bash test code and variable name.
- Add a check to ensure only use "-C" option when cpumap is not "-1" when forking "perf record".

Changes in v15:
- Revert changes added for python import test failure in v14 because the code works correctly with the new python build code.
- Update the command line option to --record-tpebs.

Changes in v14:
- Fix the python import test failure. We cannot support PYTHON_PERF because it will trigger a chain of dependency issues if we add intel-tpebs.c to it. So, only enable tpebs functions in evsel and evlist when PYTHON_PERF is not defined.
- Fix shellcheck warning for the tpebs test.

Changes in v13:
- Add document for the command line option and fix build error in non-x86_64.
- Update example with non-zero retire_latency value when tpebs recording is enabled.
- Add tpebs_stop() back to tpebs_set_evsel() because in hybrid platform, when the forked perf record is not killed, the reader thread does not get any sampled data from the PIPE. As a result, tpebs_set_evesel() will always return zero on retire_latency values. This does not happen on my test GNR machine. Since -I is not supported yet, I think we should add tpebs_stop() to ensure correctness for now. More investigation is required here when we work on supporting -I mode.
- Rebase code on top of perf-tools-next.

Changes in v12:
- Update MTL metric JSON file to include E-Core TMA3.6 changes.
- Update comments and code for better code quality. Keep tpebs_start() and tpebs_delete() at evsel level for now and add comments on these functions with more details about potential future changes. Remove tpebs_stop() call from tpebs_set_evsel(). Simplify the tpebs_start() and tpebs_stop()/tpebs_delete() interfaces. Also fixed the bugs on not freed "new" pointer and the incorrect variable value assignment to val instead of counter->val.

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/perf-stat.txt        |    8 +
 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     |  142 +
 .../arch/x86/meteorlake/mtl-metrics.json      | 2535 +++++++++++++++++
 .../perf/tests/shell/test_stat_intel_tpebs.sh |   19 +
 tools/perf/util/Build                         |    1 +
 tools/perf/util/data.c                        |    7 +-
 tools/perf/util/evlist.c                      |    2 +
 tools/perf/util/evsel.c                       |   81 +-
 tools/perf/util/evsel.h                       |    6 +
 tools/perf/util/intel-tpebs.c                 |  430 +++
 tools/perf/util/intel-tpebs.h                 |   35 +
 tools/perf/util/parse-events.c                |    2 +
 tools/perf/util/parse-events.h                |    1 +
 tools/perf/util/parse-events.l                |    3 +-
 18 files changed, 3313 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 v17 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 80b5f6dd868e..14f777b9e03e 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 321586fb5556..fab01ba54e34 100644
@@ -1811,6 +1811,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 e13de2c8b706..b735cd9e0acf 100644
@@ -203,6 +203,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 16045c383ada..5a0bcd7f166a 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 v17 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 v17 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     |   4 +
 tools/perf/util/Build         |   1 +
 tools/perf/util/evlist.c      |   2 +
 tools/perf/util/evsel.c       |  66 +++++-
 tools/perf/util/intel-tpebs.c | 430 ++++++++++++++++++++++++++++++++++
 tools/perf/util/intel-tpebs.h |  35 +++
 6 files changed, 536 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 +4 -0
index 661832756a24..68125bd75b37 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>
@@ -683,6 +684,9 @@ static enum counter_recovery stat_handle_error(struct evsel *counter)
 
 	if (child_pid != -1)
 		kill(child_pid, SIGTERM);
+
+	tpebs_delete();
+
 	return COUNTER_FATAL;
 }
 
tools/perf/util/Build +1 -0
index 0f18fe81ef0b..25cae5c613ad 100644
@@ -154,6 +154,7 @@ perf-util-y += clockid.o
 perf-util-y += list_sort.o
 perf-util-y += mutex.o
 perf-util-y += sharded_mutex.o
+perf-util-$(CONFIG_X86_64) += intel-tpebs.o
 
 perf-util-$(CONFIG_LIBBPF) += bpf_map.o
 perf-util-$(CONFIG_PERF_BPF_SKEL) += bpf_counter.o
tools/perf/util/evlist.c +2 -0
index 3a719edafc7a..78ce80f227aa 100644
@@ -33,6 +33,7 @@
 #include "util/bpf-filter.h"
 #include "util/stat.h"
 #include "util/util.h"
+#include "util/intel-tpebs.h"
 #include <signal.h>
 #include <unistd.h>
 #include <sched.h>
@@ -179,6 +180,7 @@ void evlist__delete(struct evlist *evlist)
 	if (evlist == NULL)
 		return;
 
+	tpebs_delete();
 	evlist__free_stats(evlist);
 	evlist__munmap(evlist);
 	evlist__close(evlist);
tools/perf/util/evsel.c +64 -2

102 lines not on this page · raw message

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

430 lines not on this page · raw message

tools/perf/util/intel-tpebs.h new file +35 -0
new file mode 100644
index 000000000000..766b3fbd79f1
@@ -0,0 +1,35 @@
+/* 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);
+void tpebs_delete(void);
+int tpebs_set_evsel(struct evsel *evsel, int cpu_map_idx, int thread);
+
+#else
+
+static inline int tpebs_start(struct evlist *evsel_list __maybe_unused)
+{
+	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;
+}
+
+#endif
+#endif
Signature · 1 line
2.43.0
maintainer
#4 lore raw

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

Quoted 484 lines · On Fri, Jul 12, 2024 at 03:09:25PM -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     |   4 +
 tools/perf/util/Build         |   1 +
 tools/perf/util/evlist.c      |   2 +
 tools/perf/util/evsel.c       |  66 +++++-
 tools/perf/util/intel-tpebs.c | 430 ++++++++++++++++++++++++++++++++++
 tools/perf/util/intel-tpebs.h |  35 +++
 6 files changed, 536 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 +4 -0
index 661832756a24..68125bd75b37 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>
@@ -683,6 +684,9 @@ static enum counter_recovery stat_handle_error(struct evsel *counter)
 
 	if (child_pid != -1)
 		kill(child_pid, SIGTERM);
+
+	tpebs_delete();
+
 	return COUNTER_FATAL;
 }
 
tools/perf/util/Build +1 -0
index 0f18fe81ef0b..25cae5c613ad 100644
@@ -154,6 +154,7 @@ perf-util-y += clockid.o
 perf-util-y += list_sort.o
 perf-util-y += mutex.o
 perf-util-y += sharded_mutex.o
+perf-util-$(CONFIG_X86_64) += intel-tpebs.o
 
 perf-util-$(CONFIG_LIBBPF) += bpf_map.o
 perf-util-$(CONFIG_PERF_BPF_SKEL) += bpf_counter.o
tools/perf/util/evlist.c +2 -0
index 3a719edafc7a..78ce80f227aa 100644
@@ -33,6 +33,7 @@
 #include "util/bpf-filter.h"
 #include "util/stat.h"
 #include "util/util.h"
+#include "util/intel-tpebs.h"
 #include <signal.h>
 #include <unistd.h>
 #include <sched.h>
@@ -179,6 +180,7 @@ void evlist__delete(struct evlist *evlist)
 	if (evlist == NULL)
 		return;
 
+	tpebs_delete();
 	evlist__free_stats(evlist);
 	evlist__munmap(evlist);
 	evlist__close(evlist);
tools/perf/util/evsel.c +64 -2
index bc603193c477..1d000fa2c6cf 100644
@@ -1554,6 +1554,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;
@@ -1562,7 +1616,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)
@@ -1592,7 +1646,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))
@@ -2200,6 +2254,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);
+
 	err = __evsel__prepare_open(evsel, cpus, threads);
 	if (err)
 		return err;
@@ -2392,6 +2449,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();
 	perf_evsel__close(&evsel->core);
 	perf_evsel__free_id(&evsel->core);
 }
@@ -3357,6 +3416,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 +285 -0
new file mode 100644
index 000000000000..6680ef887961
@@ -0,0 +1,430 @@
+// 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>
+
+#define PERF_DATA		"-"
+
+bool tpebs_recording;
+static pid_t tpebs_pid = -1;
+static size_t tpebs_event_size;
+static LIST_HEAD(tpebs_results);
+static pthread_t tpebs_reader_thread;
+static struct child_process *tpebs_cmd;
+
+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("tpebs: 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) {
+		pr_err("tpebs: Require cpumap list to run sampling\n");
+		return -ECANCELED;
+	}
+	/* Use -C when cpumap_buf is not "-1" */
+	if (strcmp(cpumap_buf, "-1")) {
+		record_argv[i++] = "-C";
+		record_argv[i++] = cpumap_buf;
+	}
+
+	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;
+}
+
+/*
+ * tpebs_stop - stop the sample data read thread and the perf record process.
+ */
+static 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;
+}
+
+/*
+ * tpebs_start - start tpebs execution.
+ * @evsel_list: retire_latency evsels in this list will be selected and sampled
+ * to get the average retire_latency value.
+ *
+ * This function will be called from evlist level later when evlist__open() is
+ * called consistently.
+ */
+int tpebs_start(struct evlist *evsel_list)
+{
+	int ret = 0;
+	struct evsel *evsel;
+	char cpumap_buf[50];
+
+	/*
+	 * 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(evsel_list->core.user_requested_cpus, cpumap_buf, sizeof(cpumap_buf));
+	/*
+	 * Prepare perf record for sampling event retire_latency before fork and
+	 * prepare workload
+	 */
+	evlist__for_each_entry(evsel_list, evsel) {
+		int i;
+		char *name;
+		struct tpebs_retire_lat *new;
+
+		if (!evsel->retire_lat)
+			continue;
+
+		pr_debug("tpebs: Retire_latency of event %s is required\n", evsel->name);
+		for (i = strlen(evsel->name) - 1; i > 0; i--) {
+			if (evsel->name[i] == 'R')
+				break;
+		}
+		if (i <= 0 || evsel->name[i] != 'R') {
+			ret = -1;
+			goto err;
+		}
+
+		name = strdup(evsel->name);
+		if (!name) {
+			ret = -ENOMEM;
+			goto err;
+		}
+		name[i] = 'p';
+
+		new = zalloc(sizeof(*new));
+		if (!new) {
+			ret = -1;
+			zfree(name);
+			goto err;
+		}
+		new->name = name;
+		new->tpebs_name = evsel->name;
+		list_add_tail(&new->nd, &tpebs_results);
+		tpebs_event_size += 1;
+	}
+
+	if (tpebs_event_size > 0) {
+		struct pollfd pollfd = { .events = POLLIN, };
+		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("tpebs: Failed to create control fifo");
+			ret = -1;
+			goto out;
+		}
+		if (pipe(ack_fd) < 0) {
+			pr_err("tpebs: 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;

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

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

Can we use EVLIST_CTL_CMD_ENABLE_TAG instead?

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

Quoted 13 lines
tools/perf/util/intel-tpebs.c new file +13 -0
+		if (ret != len) {
+			pr_err("perf record control write control message failed\n");
+			goto out;
+		}
+
+		/* wait for an ack */
+		pollfd.fd = ack_fd[0];
+
+		/*
+		 * We need this poll to ensure the ack_fd PIPE will not hang
+		 * when perf record failed for any reason. The timeout value
+		 * 3000ms is an empirical selection.
+		 */

Oh, you changed it to 3 sec. But I think it's ok as we don't wait for that long for the normal cases.

Quoted 5 lines
tools/perf/util/intel-tpebs.c new file +5 -0
+		if (!poll(&pollfd, 1, 3000)) {
+			pr_err("tpebs failed: perf record ack timeout\n");
+			ret = -1;
+			goto out;
+		}

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

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+
+		if (!(pollfd.revents & POLLIN)) {
+			pr_err("tpebs failed: did not received an ack\n");
+			ret = -1;
+			goto out;
+		}
+
+		ret = read(ack_fd[0], ack_buf, sizeof(ack_buf));
+		if (ret > 0)
+			ret = strcmp(ack_buf, "ack\n");

Same for EVLIST_CTL_CMD_ACK_TAG.

Quoted 27 lines
tools/perf/util/intel-tpebs.c new file +27 -0
+		else {
+			pr_err("tpebs: perf record control ack failed\n");
+			goto out;
+		}
+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_set_evsel(struct evsel *evsel, int cpu_map_idx, int thread)
+{
+	__u64 val;
+	bool found = false;
+	struct tpebs_retire_lat *t;
+	struct perf_counts_values *count;
+
+	/* Non reitre_latency evsel should never enter this function. */
+	if (!evsel__is_retire_lat(evsel))
+		return -1;

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

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+
+	/*
+	 * Need to stop the forked record to ensure get sampled data from the
+	 * PIPE to process and get non-zero retire_lat value for hybrid.
+	 */
+	tpebs_stop();
+	count = perf_counts(evsel->counts, cpu_map_idx, thread);
+
+	list_for_each_entry(t, &tpebs_results, nd) {
+		if (t->tpebs_name == evsel->name || (evsel->metric_id && !strcmp(t->tpebs_name, evsel->metric_id))) {

This line is too long, please break.

Thanks,
Namhyung

Quoted 114 lines
tools/perf/util/intel-tpebs.c new file +70 -0
+			found = true;
+			break;
+		}
+	}
+
+	/* 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 (record_tpebs not set or 0
+		 * sample recorded).
+		 */
+		count->val = 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);
+	free(r);
+}
+
+
+/*
+ * tpebs_delete - delete tpebs related data and stop the created thread and
+ * process by calling tpebs_stop().
+ *
+ * This function is called from evlist_delete() and also from builtin-stat
+ * stat_handle_error(). If tpebs_start() is called from places other then perf
+ * stat, need to ensure tpebs_delete() is also called to safely free mem and
+ * close the data read thread and the forked perf record process.
+ *
+ * This function is also called in evsel__close() to be symmetric with
+ * tpebs_start() being called in evsel__open(). We will update this call site
+ * when move tpebs_start() to evlist level.
+ */
+void tpebs_delete(void)
+{
+	struct tpebs_retire_lat *r, *rtmp;
+
+	if (tpebs_pid == -1)
+		return;
+
+	tpebs_stop();
+
+	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;
+	}
+}
tools/perf/util/intel-tpebs.h new file +35 -0
new file mode 100644
index 000000000000..766b3fbd79f1
@@ -0,0 +1,35 @@
+/* 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);
+void tpebs_delete(void);
+int tpebs_set_evsel(struct evsel *evsel, int cpu_map_idx, int thread);
+
+#else
+
+static inline int tpebs_start(struct evlist *evsel_list __maybe_unused)
+{
+	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;
+}
+
+#endif
+#endif
Signature · 1 line
2.43.0
#5 lore raw

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

Quoted 542 lines

-----Original Message-----
From: Namhyung Kim <[email protected]>
Sent: Wednesday, July 17, 2024 10:56 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 v17 3/8] perf stat: Fork and launch perf record when perf stat needs to get retire latency value for a metric.

Quoted 7 lines · On Fri, Jul 12, 2024 at 03:09:25PM -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 9 lines

size for groups that include retire_latency evsels.

---
 tools/perf/builtin-stat.c     |   4 +
 tools/perf/util/Build         |   1 +
 tools/perf/util/evlist.c      |   2 +
 tools/perf/util/evsel.c       |  66 +++++-
 tools/perf/util/intel-tpebs.c | 430

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

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

Quoted 18 lines
 tools/perf/util/intel-tpebs.h |  35 +++
 6 files changed, 536 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 661832756a24..68125bd75b37 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>
@@ -683,6 +684,9 @@ static enum counter_recovery

stat_handle_error(struct evsel *counter)

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

Quoted 46 lines
tools/perf/builtin-stat.c +3 -0
 	if (child_pid != -1)
 		kill(child_pid, SIGTERM);
+
+	tpebs_delete();
+
 	return COUNTER_FATAL;
 }
 
tools/perf/util/Build +1 -0
index 0f18fe81ef0b..25cae5c613ad 100644
@@ -154,6 +154,7 @@ perf-util-y += clockid.o
 perf-util-y += list_sort.o
 perf-util-y += mutex.o
 perf-util-y += sharded_mutex.o
+perf-util-$(CONFIG_X86_64) += intel-tpebs.o
 
 perf-util-$(CONFIG_LIBBPF) += bpf_map.o
 perf-util-$(CONFIG_PERF_BPF_SKEL) += bpf_counter.o
tools/perf/util/evlist.c +2 -0
index 3a719edafc7a..78ce80f227aa 100644
@@ -33,6 +33,7 @@
 #include "util/bpf-filter.h"
 #include "util/stat.h"
 #include "util/util.h"
+#include "util/intel-tpebs.h"
 #include <signal.h>
 #include <unistd.h>
 #include <sched.h>
@@ -179,6 +180,7 @@ void evlist__delete(struct evlist *evlist)
 	if (evlist == NULL)
 		return;
 
+	tpebs_delete();
 	evlist__free_stats(evlist);
 	evlist__munmap(evlist);
 	evlist__close(evlist);
tools/perf/util/evsel.c +0 -0
index bc603193c477..1d000fa2c6cf 100644
@@ -1554,6 +1554,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 1554

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;
@@ -1562,7 +1616,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)
@@ -1592,7 +1646,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))
@@ -2200,6 +2254,9 @@ static int evsel__open_cpu(struct evsel *evsel,

struct perf_cpu_map *cpus,

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

Quoted 10 lines
tools/perf/util/evsel.c +3 -0
 		return 0;
 	}
 
+	if (evsel__is_retire_lat(evsel))
+		return tpebs_start(evsel->evlist);
+
 	err = __evsel__prepare_open(evsel, cpus, threads);
 	if (err)
 		return err;
@@ -2392,6 +2449,8 @@ int evsel__open(struct evsel *evsel, struct

perf_cpu_map *cpus,

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

Quoted 9 lines
tools/perf/util/evsel.c +2 -0
 void evsel__close(struct evsel *evsel)
 {
+	if (evsel__is_retire_lat(evsel))
+		tpebs_delete();
 	perf_evsel__close(&evsel->core);
 	perf_evsel__free_id(&evsel->core);
 }
@@ -3357,6 +3416,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 100 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 +92 -0
new file mode 100644
index 000000000000..6680ef887961
@@ -0,0 +1,430 @@
+// 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>
+
+#define PERF_DATA		"-"
+
+bool tpebs_recording;
+static pid_t tpebs_pid = -1;
+static size_t tpebs_event_size;
+static LIST_HEAD(tpebs_results);
+static pthread_t tpebs_reader_thread;
+static struct child_process *tpebs_cmd;
+
+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("tpebs: 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) {
+		pr_err("tpebs: Require cpumap list to run sampling\n");
+		return -ECANCELED;
+	}
+	/* Use -C when cpumap_buf is not "-1" */
+	if (strcmp(cpumap_buf, "-1")) {
+		record_argv[i++] = "-C";
+		record_argv[i++] = cpumap_buf;
+	}
+
+	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 line 102

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 38 lines
tools/perf/util/intel-tpebs.c new file +38 -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);

tools/perf/util/intel-tpebs.c

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+	if (IS_ERR(session))
+		return NULL;
+	perf_session__process_events(session);
+	perf_session__delete(session);
+
+	return NULL;
+}
+
+/*
+ * tpebs_stop - stop the sample data read thread and the perf record

process.

Quoted 30 lines
tools/perf/util/intel-tpebs.c new file +30 -0
+ */
+static 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;
+}
+
+/*
+ * tpebs_start - start tpebs execution.
+ * @evsel_list: retire_latency evsels in this list will be selected and sampled
+ * to get the average retire_latency value.
+ *
+ * This function will be called from evlist level later when evlist__open() is
+ * called consistently.
+ */
+int tpebs_start(struct evlist *evsel_list)
+{
+	int ret = 0;
+	struct evsel *evsel;

tools/perf/util/intel-tpebs.c

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+	char cpumap_buf[50];
+
+	/*
+	 * 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(evsel_list->core.user_requested_cpus,

cpumap_buf, sizeof(cpumap_buf));

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

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

and

tools/perf/util/intel-tpebs.c

Quoted 11 lines
tools/perf/util/intel-tpebs.c new file +11 -0
+	 * prepare workload
+	 */
+	evlist__for_each_entry(evsel_list, evsel) {
+		int i;
+		char *name;
+		struct tpebs_retire_lat *new;
+
+		if (!evsel->retire_lat)
+			continue;
+
+		pr_debug("tpebs: Retire_latency of event %s is required\n",

evsel->name);

Quoted 40 lines
tools/perf/util/intel-tpebs.c new file +40 -0
+		for (i = strlen(evsel->name) - 1; i > 0; i--) {
+			if (evsel->name[i] == 'R')
+				break;
+		}
+		if (i <= 0 || evsel->name[i] != 'R') {
+			ret = -1;
+			goto err;
+		}
+
+		name = strdup(evsel->name);
+		if (!name) {
+			ret = -ENOMEM;
+			goto err;
+		}
+		name[i] = 'p';
+
+		new = zalloc(sizeof(*new));
+		if (!new) {
+			ret = -1;
+			zfree(name);
+			goto err;
+		}
+		new->name = name;
+		new->tpebs_name = evsel->name;
+		list_add_tail(&new->nd, &tpebs_results);
+		tpebs_event_size += 1;
+	}
+
+	if (tpebs_event_size > 0) {
+		struct pollfd pollfd = { .events = POLLIN, };
+		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("tpebs: 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("tpebs: 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 line 295

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

Can we use EVLIST_CTL_CMD_ENABLE_TAG instead?

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

Quoted 2 lines
tools/perf/util/intel-tpebs.c new file +2 -0
+		if (ret != len) {
+			pr_err("perf record control write control message

failed\n");

Quoted 14 lines

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

Quoted 11 lines
tools/perf/util/intel-tpebs.c new file +11 -0
+			goto out;
+		}
+
+		/* wait for an ack */
+		pollfd.fd = ack_fd[0];
+
+		/*
+		 * We need this poll to ensure the ack_fd PIPE will not hang
+		 * when perf record failed for any reason. The timeout value
+		 * 3000ms is an empirical selection.
+		 */

Oh, you changed it to 3 sec. But I think it's ok as we don't wait for that long for the normal cases.

Hi Namhyung,

I found it's more reliable to use 3 secs because in some of my test cases 2 secs are not enough for perf record reach the point of sending ACK back.

Thanks,
Weilin

Quoted 180 lines
Quoted 7 lines
tools/perf/util/intel-tpebs.c new file +5 -0
+		if (!poll(&pollfd, 1, 3000)) {
+			pr_err("tpebs failed: perf record ack timeout\n");
+			ret = -1;
+			goto out;
+		}

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

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+
+		if (!(pollfd.revents & POLLIN)) {
+			pr_err("tpebs failed: did not received an ack\n");
+			ret = -1;
+			goto out;
+		}
+
+		ret = read(ack_fd[0], ack_buf, sizeof(ack_buf));
+		if (ret > 0)
+			ret = strcmp(ack_buf, "ack\n");

Same for EVLIST_CTL_CMD_ACK_TAG.

Quoted 27 lines
tools/perf/util/intel-tpebs.c new file +27 -0
+		else {
+			pr_err("tpebs: perf record control ack failed\n");
+			goto out;
+		}
+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_set_evsel(struct evsel *evsel, int cpu_map_idx, int thread)
+{
+	__u64 val;
+	bool found = false;
+	struct tpebs_retire_lat *t;
+	struct perf_counts_values *count;
+
+	/* Non reitre_latency evsel should never enter this function. */
+	if (!evsel__is_retire_lat(evsel))
+		return -1;

tools/perf/util/intel-tpebs.c

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+
+	/*
+	 * Need to stop the forked record to ensure get sampled data from the
+	 * PIPE to process and get non-zero retire_lat value for hybrid.
+	 */
+	tpebs_stop();
+	count = perf_counts(evsel->counts, cpu_map_idx, thread);
+
+	list_for_each_entry(t, &tpebs_results, nd) {
+		if (t->tpebs_name == evsel->name || (evsel->metric_id

&& !strcmp(t->tpebs_name, evsel->metric_id))) {

This line is too long, please break.

Thanks,
Namhyung

Quoted 114 lines
tools/perf/util/intel-tpebs.c new file +70 -0
+			found = true;
+			break;
+		}
+	}
+
+	/* 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 (record_tpebs not set or 0
+		 * sample recorded).
+		 */
+		count->val = 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);
+	free(r);
+}
+
+
+/*
+ * tpebs_delete - delete tpebs related data and stop the created thread and
+ * process by calling tpebs_stop().
+ *
+ * This function is called from evlist_delete() and also from builtin-stat
+ * stat_handle_error(). If tpebs_start() is called from places other then perf
+ * stat, need to ensure tpebs_delete() is also called to safely free mem and
+ * close the data read thread and the forked perf record process.
+ *
+ * This function is also called in evsel__close() to be symmetric with
+ * tpebs_start() being called in evsel__open(). We will update this call site
+ * when move tpebs_start() to evlist level.
+ */
+void tpebs_delete(void)
+{
+	struct tpebs_retire_lat *r, *rtmp;
+
+	if (tpebs_pid == -1)
+		return;
+
+	tpebs_stop();
+
+	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;
+	}
+}
tools/perf/util/intel-tpebs.h new file +35 -1
new file mode 100644
index 000000000000..766b3fbd79f1
@@ -0,0 +1,35 @@
+/* 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);
+void tpebs_delete(void);
+int tpebs_set_evsel(struct evsel *evsel, int cpu_map_idx, int thread);
+
+#else
+
+static inline int tpebs_start(struct evlist *evsel_list __maybe_unused)
+{
+	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;
+}
+
+#endif
+#endif
--

2.43.0

#6 lore raw

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

Quoted 580 lines

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

Quoted 11 lines

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

Biggers,

Quoted 2 lines

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

when

Quoted 6 lines

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

Quoted 3 lines · On Fri, Jul 12, 2024 at 03:09:25PM -0400, [email protected] wrote:

From: Weilin Wang <[email protected]>

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

perf

Quoted 141 lines
Quoted 4 lines

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 9 lines

size for groups that include retire_latency evsels.

---
 tools/perf/builtin-stat.c     |   4 +
 tools/perf/util/Build         |   1 +
 tools/perf/util/evlist.c      |   2 +
 tools/perf/util/evsel.c       |  66 +++++-
 tools/perf/util/intel-tpebs.c | 430

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

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

Quoted 18 lines
 tools/perf/util/intel-tpebs.h |  35 +++
 6 files changed, 536 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 661832756a24..68125bd75b37 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>
@@ -683,6 +684,9 @@ static enum counter_recovery

stat_handle_error(struct evsel *counter)

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

Quoted 46 lines
tools/perf/builtin-stat.c +3 -0
 	if (child_pid != -1)
 		kill(child_pid, SIGTERM);
+
+	tpebs_delete();
+
 	return COUNTER_FATAL;
 }
 
tools/perf/util/Build +1 -0
index 0f18fe81ef0b..25cae5c613ad 100644
@@ -154,6 +154,7 @@ perf-util-y += clockid.o
 perf-util-y += list_sort.o
 perf-util-y += mutex.o
 perf-util-y += sharded_mutex.o
+perf-util-$(CONFIG_X86_64) += intel-tpebs.o
 
 perf-util-$(CONFIG_LIBBPF) += bpf_map.o
 perf-util-$(CONFIG_PERF_BPF_SKEL) += bpf_counter.o
tools/perf/util/evlist.c +2 -0
index 3a719edafc7a..78ce80f227aa 100644
@@ -33,6 +33,7 @@
 #include "util/bpf-filter.h"
 #include "util/stat.h"
 #include "util/util.h"
+#include "util/intel-tpebs.h"
 #include <signal.h>
 #include <unistd.h>
 #include <sched.h>
@@ -179,6 +180,7 @@ void evlist__delete(struct evlist *evlist)
 	if (evlist == NULL)
 		return;
 
+	tpebs_delete();
 	evlist__free_stats(evlist);
 	evlist__munmap(evlist);
 	evlist__close(evlist);
tools/perf/util/evsel.c +0 -0
index bc603193c477..1d000fa2c6cf 100644
@@ -1554,6 +1554,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 1554

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

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

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;
@@ -1562,7 +1616,7 @@ static int evsel__process_group_data(struct

evsel

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)
@@ -1592,7 +1646,7 @@ static int evsel__read_group(struct evsel

*leader,

Quoted 358 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))
@@ -2200,6 +2254,9 @@ static int evsel__open_cpu(struct evsel *evsel,

struct perf_cpu_map *cpus,

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

Quoted 10 lines
tools/perf/util/evsel.c +3 -0
 		return 0;
 	}
 
+	if (evsel__is_retire_lat(evsel))
+		return tpebs_start(evsel->evlist);
+
 	err = __evsel__prepare_open(evsel, cpus, threads);
 	if (err)
 		return err;
@@ -2392,6 +2449,8 @@ int evsel__open(struct evsel *evsel, struct

perf_cpu_map *cpus,

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

Quoted 9 lines
tools/perf/util/evsel.c +2 -0
 void evsel__close(struct evsel *evsel)
 {
+	if (evsel__is_retire_lat(evsel))
+		tpebs_delete();
 	perf_evsel__close(&evsel->core);
 	perf_evsel__free_id(&evsel->core);
 }
@@ -3357,6 +3416,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 100 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 +92 -0
new file mode 100644
index 000000000000..6680ef887961
@@ -0,0 +1,430 @@
+// 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>
+
+#define PERF_DATA		"-"
+
+bool tpebs_recording;
+static pid_t tpebs_pid = -1;
+static size_t tpebs_event_size;
+static LIST_HEAD(tpebs_results);
+static pthread_t tpebs_reader_thread;
+static struct child_process *tpebs_cmd;
+
+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("tpebs: 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) {
+		pr_err("tpebs: Require cpumap list to run sampling\n");
+		return -ECANCELED;
+	}
+	/* Use -C when cpumap_buf is not "-1" */
+	if (strcmp(cpumap_buf, "-1")) {
+		record_argv[i++] = "-C";
+		record_argv[i++] = cpumap_buf;
+	}
+
+	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 line 102

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 38 lines
tools/perf/util/intel-tpebs.c new file +38 -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);

tools/perf/util/intel-tpebs.c

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+	if (IS_ERR(session))
+		return NULL;
+	perf_session__process_events(session);
+	perf_session__delete(session);
+
+	return NULL;
+}
+
+/*
+ * tpebs_stop - stop the sample data read thread and the perf record

process.

Quoted 30 lines
tools/perf/util/intel-tpebs.c new file +30 -0
+ */
+static 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;
+}
+
+/*
+ * tpebs_start - start tpebs execution.
+ * @evsel_list: retire_latency evsels in this list will be selected and sampled
+ * to get the average retire_latency value.
+ *
+ * This function will be called from evlist level later when evlist__open() is
+ * called consistently.
+ */
+int tpebs_start(struct evlist *evsel_list)
+{
+	int ret = 0;
+	struct evsel *evsel;

tools/perf/util/intel-tpebs.c

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+	char cpumap_buf[50];
+
+	/*
+	 * 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(evsel_list->core.user_requested_cpus,

cpumap_buf, sizeof(cpumap_buf));

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

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

and

tools/perf/util/intel-tpebs.c

Quoted 11 lines
tools/perf/util/intel-tpebs.c new file +11 -0
+	 * prepare workload
+	 */
+	evlist__for_each_entry(evsel_list, evsel) {
+		int i;
+		char *name;
+		struct tpebs_retire_lat *new;
+
+		if (!evsel->retire_lat)
+			continue;
+
+		pr_debug("tpebs: Retire_latency of event %s is required\n",

evsel->name);

Quoted 40 lines
tools/perf/util/intel-tpebs.c new file +40 -0
+		for (i = strlen(evsel->name) - 1; i > 0; i--) {
+			if (evsel->name[i] == 'R')
+				break;
+		}
+		if (i <= 0 || evsel->name[i] != 'R') {
+			ret = -1;
+			goto err;
+		}
+
+		name = strdup(evsel->name);
+		if (!name) {
+			ret = -ENOMEM;
+			goto err;
+		}
+		name[i] = 'p';
+
+		new = zalloc(sizeof(*new));
+		if (!new) {
+			ret = -1;
+			zfree(name);
+			goto err;
+		}
+		new->name = name;
+		new->tpebs_name = evsel->name;
+		list_add_tail(&new->nd, &tpebs_results);
+		tpebs_event_size += 1;
+	}
+
+	if (tpebs_event_size > 0) {
+		struct pollfd pollfd = { .events = POLLIN, };
+		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("tpebs: 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("tpebs: 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 line 295

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

Can we use EVLIST_CTL_CMD_ENABLE_TAG instead?

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

Quoted 2 lines
tools/perf/util/intel-tpebs.c new file +2 -0
+		if (ret != len) {
+			pr_err("perf record control write control message

failed\n");

Quoted 14 lines

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

Quoted 11 lines
tools/perf/util/intel-tpebs.c new file +11 -0
+			goto out;
+		}
+
+		/* wait for an ack */
+		pollfd.fd = ack_fd[0];
+
+		/*
+		 * We need this poll to ensure the ack_fd PIPE will not hang
+		 * when perf record failed for any reason. The timeout value
+		 * 3000ms is an empirical selection.
+		 */

Oh, you changed it to 3 sec. But I think it's ok as we don't wait for that long for the normal cases.

Quoted 4 lines

Hi Namhyung,

I found it's more reliable to use 3 secs because in some of my test cases 2 secs are not enough for perf record reach the point of sending ACK back.

Does this 3sec wait looks good to you? Please let me know if you have other suggestions.

Thanks,
Weilin

Quoted 186 lines

Thanks,
Weilin

Quoted 98 lines
Quoted 7 lines
tools/perf/util/intel-tpebs.c new file +5 -0
+		if (!poll(&pollfd, 1, 3000)) {
+			pr_err("tpebs failed: perf record ack timeout\n");
+			ret = -1;
+			goto out;
+		}

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

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+
+		if (!(pollfd.revents & POLLIN)) {
+			pr_err("tpebs failed: did not received an ack\n");
+			ret = -1;
+			goto out;
+		}
+
+		ret = read(ack_fd[0], ack_buf, sizeof(ack_buf));
+		if (ret > 0)
+			ret = strcmp(ack_buf, "ack\n");

Same for EVLIST_CTL_CMD_ACK_TAG.

Quoted 27 lines
tools/perf/util/intel-tpebs.c new file +27 -0
+		else {
+			pr_err("tpebs: perf record control ack failed\n");
+			goto out;
+		}
+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_set_evsel(struct evsel *evsel, int cpu_map_idx, int thread)
+{
+	__u64 val;
+	bool found = false;
+	struct tpebs_retire_lat *t;
+	struct perf_counts_values *count;
+
+	/* Non reitre_latency evsel should never enter this function. */
+	if (!evsel__is_retire_lat(evsel))
+		return -1;

tools/perf/util/intel-tpebs.c

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+
+	/*
+	 * Need to stop the forked record to ensure get sampled data from the
+	 * PIPE to process and get non-zero retire_lat value for hybrid.
+	 */
+	tpebs_stop();
+	count = perf_counts(evsel->counts, cpu_map_idx, thread);
+
+	list_for_each_entry(t, &tpebs_results, nd) {
+		if (t->tpebs_name == evsel->name || (evsel->metric_id

&& !strcmp(t->tpebs_name, evsel->metric_id))) {

This line is too long, please break.

Thanks,
Namhyung

Quoted 32 lines
tools/perf/util/intel-tpebs.c new file +32 -0
+			found = true;
+			break;
+		}
+	}
+
+	/* 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 (record_tpebs not set or 0
+		 * sample recorded).
+		 */
+		count->val = 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)
Quoted 8 lines

tools/perf/util/intel-tpebs.c

Quoted 8 lines
tools/perf/util/intel-tpebs.c new file +8 -0
+{
+	zfree(&r->name);
+	free(r);
+}
+
+
+/*
+ * tpebs_delete - delete tpebs related data and stop the created thread

and

Quoted 4 lines

tools/perf/util/intel-tpebs.c

Quoted 4 lines
tools/perf/util/intel-tpebs.c new file +4 -0
+ * process by calling tpebs_stop().
+ *
+ * This function is called from evlist_delete() and also from builtin-stat
+ * stat_handle_error(). If tpebs_start() is called from places other then

perf

Quoted 70 lines
Quoted 70 lines
tools/perf/util/intel-tpebs.c new file +26 -0
+ * stat, need to ensure tpebs_delete() is also called to safely free mem and
+ * close the data read thread and the forked perf record process.
+ *
+ * This function is also called in evsel__close() to be symmetric with
+ * tpebs_start() being called in evsel__open(). We will update this call site
+ * when move tpebs_start() to evlist level.
+ */
+void tpebs_delete(void)
+{
+	struct tpebs_retire_lat *r, *rtmp;
+
+	if (tpebs_pid == -1)
+		return;
+
+	tpebs_stop();
+
+	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;
+	}
+}
tools/perf/util/intel-tpebs.h new file +35 -1
new file mode 100644
index 000000000000..766b3fbd79f1
@@ -0,0 +1,35 @@
+/* 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);
+void tpebs_delete(void);
+int tpebs_set_evsel(struct evsel *evsel, int cpu_map_idx, int thread);
+
+#else
+
+static inline int tpebs_start(struct evlist *evsel_list __maybe_unused)
+{
+	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;
+}
+
+#endif
+#endif
--

2.43.0

maintainer
#7 lore raw

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

Quoted 59 lines · Weilin
Quoted 59 lines

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

Quoted 11 lines

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

Biggers,

Quoted 2 lines

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

when

Quoted 6 lines

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

Quoted 3 lines · On Fri, Jul 12, 2024 at 03:09:25PM -0400, [email protected] wrote:

From: Weilin Wang <[email protected]>

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

perf

Quoted 17 lines
Quoted 4 lines

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 3 lines

size for groups that include retire_latency evsels.

[SNIP]

Quoted 98 lines
Quoted 98 lines
Quoted 97 lines
Quoted 2 lines

+ /*
+ * Prepare perf record for sampling event retire_latency before fork

and

tools/perf/util/intel-tpebs.c

Quoted 11 lines
tools/perf/util/intel-tpebs.c new file +11 -0
+  * prepare workload
+  */
+ evlist__for_each_entry(evsel_list, evsel) {
+         int i;
+         char *name;
+         struct tpebs_retire_lat *new;
+
+         if (!evsel->retire_lat)
+                 continue;
+
+         pr_debug("tpebs: Retire_latency of event %s is required\n",

evsel->name);

Quoted 40 lines
tools/perf/util/intel-tpebs.c new file +40 -0
+         for (i = strlen(evsel->name) - 1; i > 0; i--) {
+                 if (evsel->name[i] == 'R')
+                         break;
+         }
+         if (i <= 0 || evsel->name[i] != 'R') {
+                 ret = -1;
+                 goto err;
+         }
+
+         name = strdup(evsel->name);
+         if (!name) {
+                 ret = -ENOMEM;
+                 goto err;
+         }
+         name[i] = 'p';
+
+         new = zalloc(sizeof(*new));
+         if (!new) {
+                 ret = -1;
+                 zfree(name);
+                 goto err;
+         }
+         new->name = name;
+         new->tpebs_name = evsel->name;
+         list_add_tail(&new->nd, &tpebs_results);
+         tpebs_event_size += 1;
+ }
+
+ if (tpebs_event_size > 0) {
+         struct pollfd pollfd = { .events = POLLIN, };
+         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("tpebs: 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("tpebs: 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 line 295

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

Can we use EVLIST_CTL_CMD_ENABLE_TAG instead?

Review

Quoted 2 lines
(quoted diff) +2 -0
+         if (ret != len) {
+                 pr_err("perf record control write control message

failed\n");

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

Quoted 11 lines
tools/perf/util/intel-tpebs.c new file +11 -0
+                 goto out;
+         }
+
+         /* wait for an ack */
+         pollfd.fd = ack_fd[0];
+
+         /*
+          * We need this poll to ensure the ack_fd PIPE will not hang
+          * when perf record failed for any reason. The timeout value
+          * 3000ms is an empirical selection.
+          */

Oh, you changed it to 3 sec. But I think it's ok as we don't wait for that long for the normal cases.

Quoted 6 lines
Quoted 4 lines

Hi Namhyung,

I found it's more reliable to use 3 secs because in some of my test cases 2 secs are not enough for perf record reach the point of sending ACK back.

Does this 3sec wait looks good to you? Please let me know if you have other suggestions.

I have no idea or preference. It'd be ok if it works for you.. But I'm just curious in what situation 2 seconds was not enough.

Thanks,
Namhyung

Quoted 181 lines
Quoted 181 lines
Quoted 97 lines
Quoted 6 lines
tools/perf/util/intel-tpebs.c new file +5 -0
+         if (!poll(&pollfd, 1, 3000)) {
+                 pr_err("tpebs failed: perf record ack timeout\n");
+                 ret = -1;
+                 goto out;
+         }

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

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+
+         if (!(pollfd.revents & POLLIN)) {
+                 pr_err("tpebs failed: did not received an ack\n");
+                 ret = -1;
+                 goto out;
+         }
+
+         ret = read(ack_fd[0], ack_buf, sizeof(ack_buf));
+         if (ret > 0)
+                 ret = strcmp(ack_buf, "ack\n");

Same for EVLIST_CTL_CMD_ACK_TAG.

Quoted 27 lines
tools/perf/util/intel-tpebs.c new file +27 -0
+         else {
+                 pr_err("tpebs: perf record control ack failed\n");
+                 goto out;
+         }
+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_set_evsel(struct evsel *evsel, int cpu_map_idx, int thread)
+{
+ __u64 val;
+ bool found = false;
+ struct tpebs_retire_lat *t;
+ struct perf_counts_values *count;
+
+ /* Non reitre_latency evsel should never enter this function. */
+ if (!evsel__is_retire_lat(evsel))
+         return -1;

tools/perf/util/intel-tpebs.c

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+
+ /*
+  * Need to stop the forked record to ensure get sampled data from the
+  * PIPE to process and get non-zero retire_lat value for hybrid.
+  */
+ tpebs_stop();
+ count = perf_counts(evsel->counts, cpu_map_idx, thread);
+
+ list_for_each_entry(t, &tpebs_results, nd) {
+         if (t->tpebs_name == evsel->name || (evsel->metric_id

&& !strcmp(t->tpebs_name, evsel->metric_id))) {

This line is too long, please break.

Thanks,
Namhyung

Quoted 32 lines
tools/perf/util/intel-tpebs.c new file +32 -0
+                 found = true;
+                 break;
+         }
+ }
+
+ /* 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 (record_tpebs not set or 0
+          * sample recorded).
+          */
+         count->val = 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)
Quoted 8 lines

tools/perf/util/intel-tpebs.c

Quoted 8 lines
tools/perf/util/intel-tpebs.c new file +8 -0
+{
+ zfree(&r->name);
+ free(r);
+}
+
+
+/*
+ * tpebs_delete - delete tpebs related data and stop the created thread

and

Quoted 4 lines
Quoted 4 lines

+ * process by calling tpebs_stop().
+ *
+ * This function is called from evlist_delete() and also from builtin-stat
+ * stat_handle_error(). If tpebs_start() is called from places other then

perf

Quoted 70 lines
Quoted 70 lines

+ * stat, need to ensure tpebs_delete() is also called to safely free mem and
+ * close the data read thread and the forked perf record process.
+ *
+ * This function is also called in evsel__close() to be symmetric with
+ * tpebs_start() being called in evsel__open(). We will update this call site
+ * when move tpebs_start() to evlist level.
+ */

tools/perf/util/intel-tpebs.c new file +19 -0
+void tpebs_delete(void)
+{
+ struct tpebs_retire_lat *r, *rtmp;
+
+ if (tpebs_pid == -1)
+         return;
+
+ tpebs_stop();
+
+ 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;
+ }
+}
tools/perf/util/intel-tpebs.h new file +35 -1
new file mode 100644
index 000000000000..766b3fbd79f1
@@ -0,0 +1,35 @@
+/* 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);
+void tpebs_delete(void);
+int tpebs_set_evsel(struct evsel *evsel, int cpu_map_idx, int thread);
+
+#else
+
+static inline int tpebs_start(struct evlist *evsel_list __maybe_unused)
+{
+ 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;
+}
+
+#endif
+#endif
--

2.43.0

#8 lore raw

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

Quoted 191 lines

-----Original Message-----
From: Namhyung Kim <[email protected]>
Sent: Friday, July 19, 2024 5:27 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 v17 3/8] perf stat: Fork and launch perf record when perf stat needs to get retire latency value for a metric.

Quoted 13 lines · Weilin
Quoted 13 lines

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

Perry

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 v17 3/8] perf stat: Fork and launch perf record

when

Quoted 4 lines
Quoted 4 lines

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

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

-----Original Message-----
From: Namhyung Kim <[email protected]>
Sent: Wednesday, July 17, 2024 10:56 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,

Quoted 1 line
Quoted 1 line
Quoted 1 line

Adrian <[email protected]>; Kan Liang

<[email protected]>;

Quoted 1 line
Quoted 1 line
Quoted 1 line

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

Perry

Quoted 8 lines
Quoted 8 lines
Quoted 1 line

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

Biggers,

Quoted 2 lines

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

when

Quoted 3 lines

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

On Fri, Jul 12, 2024 at 03:09:25PM -0400, [email protected]

Quoted 5 lines · wrote:
Quoted 5 lines
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

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

required

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

retire_latency values in parallel while perf stat is collecting counting

values.

Quoted 10 lines
Quoted 10 lines
Quoted 10 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 3 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

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

wasted on it. This commit includes code suggested by Namhyung to

adjust

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

reading

Quoted 3 lines

size for groups that include retire_latency evsels.

[SNIP]

Quoted 98 lines
Quoted 98 lines
Quoted 97 lines
Quoted 2 lines

+ /*
+ * Prepare perf record for sampling event retire_latency before fork

and

tools/perf/util/intel-tpebs.c

Quoted 11 lines
tools/perf/util/intel-tpebs.c new file +11 -0
+  * prepare workload
+  */
+ evlist__for_each_entry(evsel_list, evsel) {
+         int i;
+         char *name;
+         struct tpebs_retire_lat *new;
+
+         if (!evsel->retire_lat)
+                 continue;
+
+         pr_debug("tpebs: Retire_latency of event %s is required\n",

evsel->name);

Quoted 40 lines
tools/perf/util/intel-tpebs.c new file +40 -0
+         for (i = strlen(evsel->name) - 1; i > 0; i--) {
+                 if (evsel->name[i] == 'R')
+                         break;
+         }
+         if (i <= 0 || evsel->name[i] != 'R') {
+                 ret = -1;
+                 goto err;
+         }
+
+         name = strdup(evsel->name);
+         if (!name) {
+                 ret = -ENOMEM;
+                 goto err;
+         }
+         name[i] = 'p';
+
+         new = zalloc(sizeof(*new));
+         if (!new) {
+                 ret = -1;
+                 zfree(name);
+                 goto err;
+         }
+         new->name = name;
+         new->tpebs_name = evsel->name;
+         list_add_tail(&new->nd, &tpebs_results);
+         tpebs_event_size += 1;
+ }
+
+ if (tpebs_event_size > 0) {
+         struct pollfd pollfd = { .events = POLLIN, };
+         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("tpebs: 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("tpebs: 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 line 295

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

Can we use EVLIST_CTL_CMD_ENABLE_TAG instead?

Review

Quoted 2 lines
(quoted diff) +2 -0
+         if (ret != len) {
+                 pr_err("perf record control write control message

failed\n");

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

Quoted 11 lines
tools/perf/util/intel-tpebs.c new file +11 -0
+                 goto out;
+         }
+
+         /* wait for an ack */
+         pollfd.fd = ack_fd[0];
+
+         /*
+          * We need this poll to ensure the ack_fd PIPE will not hang
+          * when perf record failed for any reason. The timeout value
+          * 3000ms is an empirical selection.
+          */

Oh, you changed it to 3 sec. But I think it's ok as we don't wait for that long for the normal cases.

Quoted 3 lines
Quoted 3 lines

Hi Namhyung,

I found it's more reliable to use 3 secs because in some of my test cases 2

Quoted 9 lines

secs

Quoted 3 lines
Quoted 1 line

are not enough for perf record reach the point of sending ACK back.

Does this 3sec wait looks good to you? Please let me know if you have other

suggestions.

I have no idea or preference. It'd be ok if it works for you.. But I'm just curious in what situation 2 seconds was not enough.

Ok, I will keep using 3secs then.

I tested different fake metrics (-M) that use :R events and also several :R events (-e) without requiring metrics. Sometimes 2 sec could be too short. I don't see obvious pattern which type of metrics would need more than 2 secs. But I do think 2 sec is always not enough for :R event (-e) without metrics.

Thanks,
Weilin

Quoted 186 lines

Thanks,
Namhyung

Quoted 106 lines
Quoted 106 lines
Quoted 97 lines
Quoted 6 lines
tools/perf/util/intel-tpebs.c new file +5 -0
+         if (!poll(&pollfd, 1, 3000)) {
+                 pr_err("tpebs failed: perf record ack timeout\n");
+                 ret = -1;
+                 goto out;
+         }

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

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+
+         if (!(pollfd.revents & POLLIN)) {
+                 pr_err("tpebs failed: did not received an ack\n");
+                 ret = -1;
+                 goto out;
+         }
+
+         ret = read(ack_fd[0], ack_buf, sizeof(ack_buf));
+         if (ret > 0)
+                 ret = strcmp(ack_buf, "ack\n");

Same for EVLIST_CTL_CMD_ACK_TAG.

Quoted 27 lines
tools/perf/util/intel-tpebs.c new file +27 -0
+         else {
+                 pr_err("tpebs: perf record control ack failed\n");
+                 goto out;
+         }
+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_set_evsel(struct evsel *evsel, int cpu_map_idx, int thread)
+{
+ __u64 val;
+ bool found = false;
+ struct tpebs_retire_lat *t;
+ struct perf_counts_values *count;
+
+ /* Non reitre_latency evsel should never enter this function. */
+ if (!evsel__is_retire_lat(evsel))
+         return -1;

tools/perf/util/intel-tpebs.c

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+
+ /*
+  * Need to stop the forked record to ensure get sampled data from the
+  * PIPE to process and get non-zero retire_lat value for hybrid.
+  */
+ tpebs_stop();
+ count = perf_counts(evsel->counts, cpu_map_idx, thread);
+
+ list_for_each_entry(t, &tpebs_results, nd) {
+         if (t->tpebs_name == evsel->name || (evsel->metric_id

&& !strcmp(t->tpebs_name, evsel->metric_id))) {

This line is too long, please break.

Thanks,
Namhyung

Quoted 32 lines
tools/perf/util/intel-tpebs.c new file +32 -0
+                 found = true;
+                 break;
+         }
+ }
+
+ /* 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 (record_tpebs not set or 0
+          * sample recorded).
+          */
+         count->val = 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)
Quoted 8 lines

tools/perf/util/intel-tpebs.c

Quoted 8 lines
tools/perf/util/intel-tpebs.c new file +8 -0
+{
+ zfree(&r->name);
+ free(r);
+}
+
+
+/*
+ * tpebs_delete - delete tpebs related data and stop the created thread

and

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

+ * process by calling tpebs_stop().
+ *
+ * This function is called from evlist_delete() and also from builtin-stat
+ * stat_handle_error(). If tpebs_start() is called from places other then

perf

Quoted 1 line
Quoted 1 line

+ * stat, need to ensure tpebs_delete() is also called to safely free mem

and

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

+ * close the data read thread and the forked perf record process.
+ *
+ * This function is also called in evsel__close() to be symmetric with
+ * tpebs_start() being called in evsel__open(). We will update this call

site

Quoted 65 lines
Quoted 65 lines
Quoted 65 lines
Quoted 65 lines

+ * when move tpebs_start() to evlist level.
+ */

tools/perf/util/intel-tpebs.c new file +19 -0
+void tpebs_delete(void)
+{
+ struct tpebs_retire_lat *r, *rtmp;
+
+ if (tpebs_pid == -1)
+         return;
+
+ tpebs_stop();
+
+ 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;
+ }
+}
tools/perf/util/intel-tpebs.h new file +35 -1
new file mode 100644
index 000000000000..766b3fbd79f1
@@ -0,0 +1,35 @@
+/* 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);
+void tpebs_delete(void);
+int tpebs_set_evsel(struct evsel *evsel, int cpu_map_idx, int thread);
+
+#else
+
+static inline int tpebs_start(struct evlist *evsel_list __maybe_unused)
+{
+ 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;
+}
+
+#endif
+#endif
--

2.43.0

#9 lore raw

RFC PATCH v17 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 1d000fa2c6cf..d607056b73c9 100644
@@ -59,6 +59,7 @@
 #include <internal/xyarray.h>
 #include <internal/lib.h>
 #include <internal/threadmap.h>
+#include "util/intel-tpebs.h"
 
 #include <linux/ctype.h>
 
@@ -1539,6 +1540,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)
 {
@@ -1546,6 +1552,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;
@@ -1838,6 +1850,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 14f777b9e03e..a5da4b03bb1c 100644
@@ -311,6 +311,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
#10 lore raw

RFC PATCH v17 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 for TMA4.8. Some of the metrics' formulas use TPEBS retire_latency in MTL.

This also includes lated E-Core TMA3.6 changes.

---
 .../arch/x86/meteorlake/metricgroups.json     |  142 +
 .../arch/x86/meteorlake/mtl-metrics.json      | 2535 +++++++++++++++++
 2 files changed, 2677 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 +142 -0
new file mode 100644
index 000000000000..b54a5fc0861f
@@ -0,0 +1,142 @@
+{
+    "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",
+    "Ifetch": "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",
+    "Load_Store_Miss": "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",
+    "Mem_Exec": "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",
+    "load_store_bound": "Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet",
+    "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_group": "Metrics contributing to tma_backend_bound category",
+    "tma_bad_speculation_group": "Metrics contributing to tma_bad_speculation 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_ifetch_bandwidth_group": "Metrics contributing to tma_ifetch_bandwidth category",
+    "tma_ifetch_latency_group": "Metrics contributing to tma_ifetch_latency 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_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_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 +2535 -0

2,535 lines not on this page · raw message

Signature · 1 line
2.43.0
#11 lore raw

RFC PATCH v17 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 --record-tpebs:

perf stat -M tma_split_loads -C1-4 --record-tpebs sleep 1

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

Performance counter stats for 'CPU(s) 1-4':

    53,259,156,071      cpu_core/TOPDOWN.SLOTS/          #      1.6 %  tma_split_loads          (50.00%)
    15,867,565,250      cpu_core/topdown-retiring/                                              (50.00%)
    15,655,580,731      cpu_core/topdown-mem-bound/                                             (50.00%)
    11,738,022,218      cpu_core/topdown-bad-spec/                                              (50.00%)
     6,151,265,424      cpu_core/topdown-fe-bound/                                              (50.00%)
    20,445,917,581      cpu_core/topdown-be-bound/                                              (50.00%)
     6,925,098,013      cpu_core/L1D_PEND_MISS.PENDING/                                         (50.00%)
     3,838,653,421      cpu_core/MEMORY_ACTIVITY.STALLS_L1D_MISS/                                        (50.00%)
     4,797,059,783      cpu_core/EXE_ACTIVITY.BOUND_ON_LOADS/                                        (50.00%)
    11,931,916,714      cpu_core/CPU_CLK_UNHALTED.THREAD/                                        (50.00%)
       102,576,164      cpu_core/MEM_LOAD_COMPLETED.L1_MISS_ANY/                                        (50.00%)
        64,071,854      cpu_core/MEM_INST_RETIRED.SPLIT_LOADS/                                        (50.00%)
                 3      cpu_core/MEM_INST_RETIRED.SPLIT_LOADS/R
       1.003049679 seconds time elapsed

Exampe without --record-tpebs:

perf stat -M tma_contested_accesses -C1 sleep 1

Performance counter stats for 'CPU(s) 1':

        50,203,891      cpu_core/TOPDOWN.SLOTS/          #      0.0 %  tma_contested_accesses   (63.60%)
        10,040,777      cpu_core/topdown-retiring/                                              (63.60%)
         6,890,729      cpu_core/topdown-mem-bound/                                             (63.60%)
         2,756,463      cpu_core/topdown-bad-spec/                                              (63.60%)
        10,828,288      cpu_core/topdown-fe-bound/                                              (63.60%)
        28,350,432      cpu_core/topdown-be-bound/                                              (63.60%)
                98      cpu_core/OCR.DEMAND_DATA_RD.L3_HIT.SNOOP_HITM/                                        (63.70%)
           577,520      cpu_core/MEMORY_ACTIVITY.STALLS_L2_MISS/                                        (54.62%)
           313,339      cpu_core/MEMORY_ACTIVITY.STALLS_L3_MISS/                                        (54.62%)
            14,155      cpu_core/MEM_LOAD_RETIRED.L1_MISS/                                        (45.54%)
                 0      cpu_core/OCR.DEMAND_DATA_RD.L3_HIT.SNOOP_HIT_WITH_FWD/                                        (36.30%)
         8,468,077      cpu_core/CPU_CLK_UNHALTED.THREAD/                                        (45.38%)
               198      cpu_core/MEM_LOAD_L3_HIT_RETIRED.XSNP_MISS/                                        (45.38%)
             8,324      cpu_core/MEM_LOAD_RETIRED.FB_HIT/                                        (45.38%)
     3,388,031,520      TSC
        23,226,785      cpu_core/CPU_CLK_UNHALTED.REF_TSC/                                        (54.46%)
                80      cpu_core/MEM_LOAD_L3_HIT_RETIRED.XSNP_FWD/                                        (54.46%)
                 0      cpu_core/MEM_LOAD_L3_HIT_RETIRED.XSNP_FWD/R
                 0      cpu_core/MEM_LOAD_L3_HIT_RETIRED.XSNP_MISS/R
     1,006,816,667 ns   duration_time
       1.002537737 seconds time elapsed
---
 tools/perf/Documentation/perf-stat.txt | 8 ++++++++
 tools/perf/builtin-stat.c              | 4 ++++
 2 files changed, 12 insertions(+)
tools/perf/Documentation/perf-stat.txt +8 -0
index 29756a87ab6f..2bc063672486 100644
@@ -498,6 +498,14 @@ To interpret the results it is usually needed to know on which
 CPUs the workload runs on. If needed the CPUs can be forced using
 taskset.
 
+--record-tpebs::
+Enable automatic sampling on Intel TPEBS retire_latency events (event with :R
+modifier). Without this option, perf would not capture dynamic retire_latency
+at runtime. Currently, a zero value is assigned to the retire_latency event when
+this option is not set. The TPEBS hardware feature starts from Intel Granite
+Rapids microarchitecture. This option only exists in X86_64 and is meaningful on
+Intel platforms with TPEBS feature.
+
 --td-level::
 Print the top-down statistics that equal the input level. It allows
 users to print the interested top-down metrics level instead of the
tools/perf/builtin-stat.c +4 -0
index 68125bd75b37..935386a02e78 100644
@@ -2475,6 +2475,10 @@ int cmd_stat(int argc, const char **argv)
 			"disable adding events for the metric threshold calculation"),
 		OPT_BOOLEAN(0, "topdown", &topdown_run,
 			"measure top-down statistics"),
+#ifdef HAVE_ARCH_X86_64_SUPPORT
+		OPT_BOOLEAN(0, "record-tpebs", &tpebs_recording,
+			"enable recording for tpebs when retire_latency required"),
+#endif
 		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
#12 lore raw

RFC PATCH v17 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..5c17fff694ee 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 --record-tpebs ...
+
+
 
 [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
#13 lore raw

RFC PATCH v17 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 on Intel platforms.

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

Re: [RFC PATCH v17 8/8] perf test: Add test for Intel TPEBS counting mode

Hello,

Quoted 18 lines · On Fri, Jul 12, 2024 at 03:09:30PM -0400, [email protected] wrote:

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 on Intel platforms.

---
 .../perf/tests/shell/test_stat_intel_tpebs.sh | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
 create mode 100755 tools/perf/tests/shell/test_stat_intel_tpebs.sh
tools/perf/tests/shell/test_stat_intel_tpebs.sh new file +0 -0
new file mode 100755
index 000000000000..773fcc5c35d6

tools/perf/tests/shell/test_stat_intel_tpebs.sh line 6

Quoted 6 lines
tools/perf/tests/shell/test_stat_intel_tpebs.sh new file +6 -0
@@ -0,0 +1,19 @@
+#!/bin/bash
+# test Intel TPEBS counting mode
+# SPDX-License-Identifier: GPL-2.0
+
+set -e
+grep -q GenuineIntel /proc/cpuinfo || { echo Skipping non-Intel; exit 0; }

You should exit with 2 when skipping the test.

Thanks,
Namhyung

Quoted 16 lines
tools/perf/tests/shell/test_stat_intel_tpebs.sh new file +13 -0
+
+# Use this event for testing because it should exist in all platforms
+event=cache-misses:R
+
+# Without this cmd option, default value or zero is returned
+echo "Testing without --record-tpebs"
+result=$(perf stat -e "$event" true 2>&1)
+[[ "$result" =~ $event ]] || exit 1
+
+# In platforms that do not support TPEBS, it should execute without error.
+echo "Testing with --record-tpebs"
+result=$(perf stat -e "$event" --record-tpebs -a sleep 0.01 2>&1)
+[[ "$result" =~ "perf record" && "$result" =~ $event ]] || exit 1
Signature · 1 line
2.43.0