Skip to contentSkip to conversation

TPEBS counting mode support

RFC PATCH Superseded [email protected] 4 participants 25 messages May 29, 2024 – Jun 4, 2024 perf events

v10 of 18 Superseded Latest is v18 →

lore
Share & tools
lore
#0 lore raw

v10 cover TPEBS counting mode support

From: Weilin Wang <[email protected]>

I have tried not to count retire_latency events but did not succeed. In particular, I tried the following methods:
- Convert retire_latency event to dummy event in event parser.
- Early bail out in evsel__open_cpu() and store_evsel_ids().

The first method fails and causes non-retire_latency events with the same event name return 0 count.

The second method fails and causes all the events in the same group returning "<not counted>" results.

Because of above, the retire_latency event will still run in counting mode.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Performance counter stats for 'system wide':

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

0.00 MEM_INST_RETIRED.STLB_HIT_STORES:p 0 0

1.003105924 seconds time elapsed

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

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

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

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

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

 tools/perf/Documentation/perf-list.txt        |    1 +
 tools/perf/Documentation/topdown.txt          |   30 +
 tools/perf/arch/x86/util/evlist.c             |    6 +
 tools/perf/builtin-stat.c                     |    8 +
 .../arch/x86/meteorlake/metricgroups.json     |  140 +
 .../arch/x86/meteorlake/mtl-metrics.json      | 2595 +++++++++++++++++
 .../perf/tests/shell/test_stat_intel_tpebs.sh |   19 +
 tools/perf/util/Build                         |    1 +
 tools/perf/util/data.c                        |    7 +-
 tools/perf/util/evsel.c                       |   26 +
 tools/perf/util/evsel.h                       |    6 +
 tools/perf/util/intel-tpebs.c                 |  397 +++
 tools/perf/util/intel-tpebs.h                 |   48 +
 tools/perf/util/parse-events.c                |    2 +
 tools/perf/util/parse-events.h                |    1 +
 tools/perf/util/parse-events.l                |    3 +-
 16 files changed, 3288 insertions(+), 2 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 v10 1/8 perf parse-events: Add a retirement latency modifier

From: Ian Rogers <[email protected]>

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

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

RFC PATCH v10 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 v10 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.

---
 tools/perf/builtin-stat.c     |   6 +
 tools/perf/util/Build         |   1 +
 tools/perf/util/evsel.c       |  11 +
 tools/perf/util/intel-tpebs.c | 397 ++++++++++++++++++++++++++++++++++
 tools/perf/util/intel-tpebs.h |  48 ++++
 5 files changed, 463 insertions(+)
 create mode 100644 tools/perf/util/intel-tpebs.c
 create mode 100644 tools/perf/util/intel-tpebs.h
tools/perf/builtin-stat.c +6 -0
index 428e9721b908..b09cb2c6e9c2 100644
@@ -70,6 +70,7 @@
 #include "util/bpf_counter.h"
 #include "util/iostat.h"
 #include "util/util.h"
+#include "util/intel-tpebs.h"
 #include "asm/bug.h"
 
 #include <linux/time64.h>
@@ -653,6 +654,9 @@ static enum counter_recovery stat_handle_error(struct evsel *counter)
 
 	if (child_pid != -1)
 		kill(child_pid, SIGTERM);
+
+	tpebs_stop_delete();
+
 	return COUNTER_FATAL;
 }
 
@@ -985,6 +989,8 @@ static void sig_atexit(void)
 	if (child_pid != -1)
 		kill(child_pid, SIGTERM);
 
+	tpebs_stop();
+
 	sigprocmask(SIG_SETMASK, &oset, NULL);
 
 	if (signr == -1)
tools/perf/util/Build +1 -0
index 292170a99ab6..79adf39e0d8f 100644
@@ -153,6 +153,7 @@ perf-y += clockid.o
 perf-y += list_sort.o
 perf-y += mutex.o
 perf-y += sharded_mutex.o
+perf-$(CONFIG_X86_64) += intel-tpebs.o
 
 perf-$(CONFIG_LIBBPF) += bpf_map.o
 perf-$(CONFIG_PERF_BPF_SKEL) += bpf_counter.o
tools/perf/util/evsel.c +11 -0
index a0a8aee7d6b9..4157db30c3e7 100644
@@ -2186,6 +2186,12 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 		return 0;
 	}
 
+	if (evsel__is_retire_lat(evsel)) {
+		err = tpebs_start(evsel->evlist, cpus);
+		if (err)
+			return err;
+	}
+
 	err = __evsel__prepare_open(evsel, cpus, threads);
 	if (err)
 		return err;
@@ -2376,6 +2382,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);
 }
@@ -3341,6 +3349,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 +397 -0

397 lines not on this page · raw message

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

48 lines not on this page · raw message

Signature · 1 line
2.43.0
maintainer
#4 lore raw

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

Quoted 16 lines · On Tue, May 28, 2024 at 11:43 PM <[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.

---

[SNIP]

Quoted 34 lines
tools/perf/util/intel-tpebs.c new file +34 -0
+int tpebs_set_evsel(struct evsel *evsel, int cpu_map_idx, int thread)
+{
+       struct perf_counts_values *count;
+       struct tpebs_retire_lat *t;
+       bool found = false;
+       __u64 val;
+       int ret;
+
+       /* Non reitre_latency evsel should never enter this function. */
+       if (!evsel__is_retire_lat(evsel))
+               return -1;
+
+       ret = tpebs_stop();
+       if (ret)
+               return ret;
+
+       count = perf_counts(evsel->counts, cpu_map_idx, thread);
+
+       list_for_each_entry(t, &tpebs_results, nd) {
+               if (!strcmp(t->tpebs_name, evsel->name) || !strcmp(t->tpebs_name, evsel->metric_id)) {
+                       found = true;
+                       break;
+               }
+       }
+
+       /* Set ena and run to non-zero */
+       count->ena = count->run = 1;
+       count->lost = 0;
+
+       if (!found) {
+               /*
+                * Set default value or 0 when retire_latency for this event is
+                * not found from sampling data (enable_tpebs_recording not set
+                * or 0 sample recorded).

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

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+                */
+               val = 0;
+               return 0;
+       }
+
+       /*
+        * Only set retire_latency value to the first CPU and thread.
+        */
+       if (cpu_map_idx == 0 && thread == 0) {
+       /* Lost precision when casting from double to __u64. Any improvement? */

As I said before I think you can set t->val * 1000 and then set the evsel->scale to 1e3 or 1e-3.

Thanks,
Namhyung

Quoted 8 lines
tools/perf/util/intel-tpebs.c new file +8 -0
+               val = t->val;
+       } else
+               val = 0;
+
+       count->val = val;
+       return 0;
+}
+
#5 lore raw

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

Quoted 84 lines

-----Original Message-----
From: Namhyung Kim <[email protected]>
Sent: Thursday, May 30, 2024 11:41 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 v10 3/8] perf stat: Fork and launch perf record when perf stat needs to get retire latency value for a metric.

Quoted 8 lines · On Tue, May 28, 2024 at 11:43 PM <[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 7 lines

to get retire latency value.

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

---

[SNIP]

Quoted 35 lines
tools/perf/util/intel-tpebs.c new file +20 -0
+int tpebs_set_evsel(struct evsel *evsel, int cpu_map_idx, int thread)
+{
+       struct perf_counts_values *count;
+       struct tpebs_retire_lat *t;
+       bool found = false;
+       __u64 val;
+       int ret;
+
+       /* Non reitre_latency evsel should never enter this function. */
+       if (!evsel__is_retire_lat(evsel))
+               return -1;
+
+       ret = tpebs_stop();
+       if (ret)
+               return ret;
+
+       count = perf_counts(evsel->counts, cpu_map_idx, thread);
+
+       list_for_each_entry(t, &tpebs_results, nd) {
+               if (!strcmp(t->tpebs_name, evsel->name) || !strcmp(t-

tpebs_name, evsel->metric_id)) {

tools/perf/util/intel-tpebs.c new file +14 -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 (enable_tpebs_recording not set
+                * or 0 sample recorded).

tools/perf/util/intel-tpebs.c

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+                */
+               val = 0;
+               return 0;
+       }
+
+       /*
+        * Only set retire_latency value to the first CPU and thread.
+        */
+       if (cpu_map_idx == 0 && thread == 0) {
+       /* Lost precision when casting from double to __u64. Any

improvement? */

Quoted 2 lines

As I said before I think you can set t->val * 1000 and then set the evsel->scale to 1e3 or 1e-3.

Hi Namhyung,

Sorry if this is a repeated message. I thought I replied to your suggestion on this last time. I'm thinking we should keep it like this for now and make this change unless we find the precision loss is critical. Because I thought we don't want to add special code to handle the calculation and final print to keep code simple.

I kept this comment here so that we don't forget about it. Please let me know if you'd like me to remove it.

Thanks,
Weilin

Quoted 13 lines

Thanks,
Namhyung

Quoted 8 lines
tools/perf/util/intel-tpebs.c new file +8 -0
+               val = t->val;
+       } else
+               val = 0;
+
+       count->val = val;
+       return 0;
+}
+
maintainer
#6 lore raw

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

Quoted 90 lines · Weilin
Quoted 85 lines

-----Original Message-----
From: Namhyung Kim <[email protected]>
Sent: Thursday, May 30, 2024 11:41 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 v10 3/8] perf stat: Fork and launch perf record when perf stat needs to get retire latency value for a metric.

Quoted 8 lines · On Tue, May 28, 2024 at 11:43 PM <[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 7 lines

to get retire latency value.

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

---

[SNIP]

Quoted 35 lines
tools/perf/util/intel-tpebs.c new file +20 -0
+int tpebs_set_evsel(struct evsel *evsel, int cpu_map_idx, int thread)
+{
+       struct perf_counts_values *count;
+       struct tpebs_retire_lat *t;
+       bool found = false;
+       __u64 val;
+       int ret;
+
+       /* Non reitre_latency evsel should never enter this function. */
+       if (!evsel__is_retire_lat(evsel))
+               return -1;
+
+       ret = tpebs_stop();
+       if (ret)
+               return ret;
+
+       count = perf_counts(evsel->counts, cpu_map_idx, thread);
+
+       list_for_each_entry(t, &tpebs_results, nd) {
+               if (!strcmp(t->tpebs_name, evsel->name) || !strcmp(t-

tpebs_name, evsel->metric_id)) {

tools/perf/util/intel-tpebs.c new file +14 -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 (enable_tpebs_recording not set
+                * or 0 sample recorded).

tools/perf/util/intel-tpebs.c

Quoted 10 lines
tools/perf/util/intel-tpebs.c new file +10 -0
+                */
+               val = 0;
+               return 0;
+       }
+
+       /*
+        * Only set retire_latency value to the first CPU and thread.
+        */
+       if (cpu_map_idx == 0 && thread == 0) {
+       /* Lost precision when casting from double to __u64. Any

improvement? */

Quoted 2 lines

As I said before I think you can set t->val * 1000 and then set the evsel->scale to 1e3 or 1e-3.

Hi Namhyung,

Quoted 8 lines

Sorry if this is a repeated message. I thought I replied to your suggestion on this last time. I'm thinking we should keep it like this for now and make this change unless we find the precision loss is critical. Because I thought we don't want to add special code to handle the calculation and final print to keep code simple.

I kept this comment here so that we don't forget about it. Please let me know if you'd like me to remove it.

Please see print_counter_aggrdata(). It's the generic code to print the event value and it'll display the value multiplied by the scale (default to 1.0). So you can keep precision as long as you set the scale properly (1e-3).

Thanks,
Namhyung

#7 lore raw

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

Quoted 123 lines

-----Original Message-----
From: Namhyung Kim <[email protected]>
Sent: Friday, May 31, 2024 2:40 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 v10 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: Namhyung Kim <[email protected]>
Sent: Thursday, May 30, 2024 11:41 PM
To: Wang, Weilin <[email protected]>
Cc: Ian Rogers <[email protected]>; Arnaldo Carvalho de Melo <[email protected]>; Peter Zijlstra <[email protected]>; Ingo Molnar <[email protected]>; Alexander Shishkin <[email protected]>; Jiri Olsa <[email protected]>; Hunter, Adrian <[email protected]>; Kan Liang <[email protected]>; [email protected]; [email protected]; Taylor,

Perry

Quoted 1 line
Quoted 1 line

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

Biggers,

Quoted 2 lines
Quoted 2 lines

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

when

Quoted 7 lines
Quoted 7 lines

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

Quoted 4 lines · On Tue, May 28, 2024 at 11:43 PM <[email protected]> wrote:

From: Weilin Wang <[email protected]>

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

perf

Quoted 1 line
Quoted 1 line
Quoted 1 line

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

required

Quoted 1 line
Quoted 1 line
Quoted 1 line

retire_latency values in parallel while perf stat is collecting counting

values.

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

to get retire latency value.

Another thread is required to synchronize between perf stat and perf

record

Quoted 31 lines
Quoted 31 lines
Quoted 4 lines

when we pass data through pipe.

---

[SNIP]

Quoted 26 lines
tools/perf/util/intel-tpebs.c new file +20 -0
+int tpebs_set_evsel(struct evsel *evsel, int cpu_map_idx, int thread)
+{
+       struct perf_counts_values *count;
+       struct tpebs_retire_lat *t;
+       bool found = false;
+       __u64 val;
+       int ret;
+
+       /* Non reitre_latency evsel should never enter this function. */
+       if (!evsel__is_retire_lat(evsel))
+               return -1;
+
+       ret = tpebs_stop();
+       if (ret)
+               return ret;
+
+       count = perf_counts(evsel->counts, cpu_map_idx, thread);
+
+       list_for_each_entry(t, &tpebs_results, nd) {
+               if (!strcmp(t->tpebs_name, evsel->name) || !strcmp(t-

tpebs_name, evsel->metric_id)) {

tools/perf/util/intel-tpebs.c new file +5 -0
+                       found = true;
+                       break;
+               }
+       }
+
Quoted 8 lines
Quoted 8 lines

tools/perf/util/intel-tpebs.c

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

set

Quoted 24 lines
Quoted 11 lines

tools/perf/util/intel-tpebs.c

Quoted 11 lines
tools/perf/util/intel-tpebs.c new file +11 -0
+                * or 0 sample recorded).
+                */
+               val = 0;
+               return 0;
+       }
+
+       /*
+        * Only set retire_latency value to the first CPU and thread.
+        */
+       if (cpu_map_idx == 0 && thread == 0) {
+       /* Lost precision when casting from double to __u64. Any
Quoted 4 lines

improvement? */

As I said before I think you can set t->val * 1000 and then set the evsel->scale to 1e3 or 1e-3.

Hi Namhyung,

Sorry if this is a repeated message. I thought I replied to your suggestion on this last time. I'm thinking we should keep it like this for now and make this change unless we find the precision loss is critical. Because I thought we don't want to add special code to handle the calculation and final print to keep code simple.

Quoted 7 lines
Quoted 2 lines

I kept this comment here so that we don't forget about it. Please let me know if you'd like me to remove it.

Please see print_counter_aggrdata(). It's the generic code to print the event value and it'll display the value multiplied by the scale (default to 1.0). So you can keep precision as long as you set the scale properly (1e-3).

Ah, okay. I will do this.

Thanks,
Weilin

Quoted 3 lines

Thanks,
Namhyung

#8 lore raw

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

In reply to Namhyung Kim
Quoted 123 lines

-----Original Message-----
From: Namhyung Kim <[email protected]>
Sent: Friday, May 31, 2024 2:40 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 v10 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: Namhyung Kim <[email protected]>
Sent: Thursday, May 30, 2024 11:41 PM
To: Wang, Weilin <[email protected]>
Cc: Ian Rogers <[email protected]>; Arnaldo Carvalho de Melo <[email protected]>; Peter Zijlstra <[email protected]>; Ingo Molnar <[email protected]>; Alexander Shishkin <[email protected]>; Jiri Olsa <[email protected]>; Hunter, Adrian <[email protected]>; Kan Liang <[email protected]>; [email protected]; [email protected]; Taylor,

Perry

Quoted 1 line
Quoted 1 line

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

Biggers,

Quoted 2 lines
Quoted 2 lines

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

when

Quoted 7 lines
Quoted 7 lines

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

Quoted 4 lines · On Tue, May 28, 2024 at 11:43 PM <[email protected]> wrote:

From: Weilin Wang <[email protected]>

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

perf

Quoted 1 line
Quoted 1 line
Quoted 1 line

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

required

Quoted 1 line
Quoted 1 line
Quoted 1 line

retire_latency values in parallel while perf stat is collecting counting

values.

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

to get retire latency value.

Another thread is required to synchronize between perf stat and perf

record

Quoted 31 lines
Quoted 31 lines
Quoted 4 lines

when we pass data through pipe.

---

[SNIP]

Quoted 26 lines
tools/perf/util/intel-tpebs.c new file +20 -0
+int tpebs_set_evsel(struct evsel *evsel, int cpu_map_idx, int thread)
+{
+       struct perf_counts_values *count;
+       struct tpebs_retire_lat *t;
+       bool found = false;
+       __u64 val;
+       int ret;
+
+       /* Non reitre_latency evsel should never enter this function. */
+       if (!evsel__is_retire_lat(evsel))
+               return -1;
+
+       ret = tpebs_stop();
+       if (ret)
+               return ret;
+
+       count = perf_counts(evsel->counts, cpu_map_idx, thread);
+
+       list_for_each_entry(t, &tpebs_results, nd) {
+               if (!strcmp(t->tpebs_name, evsel->name) || !strcmp(t-

tpebs_name, evsel->metric_id)) {

tools/perf/util/intel-tpebs.c new file +5 -0
+                       found = true;
+                       break;
+               }
+       }
+
Quoted 8 lines
Quoted 8 lines

tools/perf/util/intel-tpebs.c

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

set

Quoted 24 lines
Quoted 11 lines

tools/perf/util/intel-tpebs.c

Quoted 11 lines
tools/perf/util/intel-tpebs.c new file +11 -0
+                * or 0 sample recorded).
+                */
+               val = 0;
+               return 0;
+       }
+
+       /*
+        * Only set retire_latency value to the first CPU and thread.
+        */
+       if (cpu_map_idx == 0 && thread == 0) {
+       /* Lost precision when casting from double to __u64. Any
Quoted 4 lines

improvement? */

As I said before I think you can set t->val * 1000 and then set the evsel->scale to 1e3 or 1e-3.

Hi Namhyung,

Sorry if this is a repeated message. I thought I replied to your suggestion on this last time. I'm thinking we should keep it like this for now and make this change unless we find the precision loss is critical. Because I thought we don't want to add special code to handle the calculation and final print to keep code simple.

Quoted 7 lines
Quoted 2 lines

I kept this comment here so that we don't forget about it. Please let me know if you'd like me to remove it.

Please see print_counter_aggrdata(). It's the generic code to print the event value and it'll display the value multiplied by the scale (default to 1.0). So you can keep precision as long as you set the scale properly (1e-3).

I could see the retire_latency is printed correctly after set the evsel->scale to 1e-3 and multiply the t->val * 1000. However, this scale is not used in metric calculations. We need to add code in metric calculation or display part to scale it as well. Is that acceptable or do you have other suggestions?

Thanks,
Weilin

Quoted 3 lines

Thanks,
Namhyung

maintainer
#9 lore raw

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

Quoted 27 lines · Weilin
Quoted 19 lines

On Thu, May 30, 2024 at 11:46 PM Wang, Weilin <[email protected]>

Quoted 18 lines
Quoted 9 lines

tools/perf/util/intel-tpebs.c

Quoted 5 lines
tools/perf/util/intel-tpebs.c new file +5 -0
+       /*
+        * Only set retire_latency value to the first CPU and thread.
+        */
+       if (cpu_map_idx == 0 && thread == 0) {
+       /* Lost precision when casting from double to __u64. Any

improvement? */

As I said before I think you can set t->val * 1000 and then set the evsel->scale to 1e3 or 1e-3.

Hi Namhyung,

Sorry if this is a repeated message. I thought I replied to your suggestion on this last time. I'm thinking we should keep it like this for now and make this change unless we find the precision loss is critical. Because I thought we don't want to add special code to handle the calculation and final print to keep code simple.

Quoted 7 lines
Quoted 2 lines

I kept this comment here so that we don't forget about it. Please let me know if you'd like me to remove it.

Please see print_counter_aggrdata(). It's the generic code to print the event value and it'll display the value multiplied by the scale (default to 1.0). So you can keep precision as long as you set the scale properly (1e-3).

Quoted 4 lines

I could see the retire_latency is printed correctly after set the evsel->scale to 1e-3 and multiply the t->val * 1000. However, this scale is not used in metric calculations. We need to add code in metric calculation or display part to scale it as well. Is that acceptable or do you have other suggestions?

Hmm.. I don't know if other metric already dealt with the scale like with RAPL events.. If not, I think it's reasonable to add that to the metric calculation.

Ian, what do you think?

Thanks,
Namhyung

reviewer
#10 lore raw

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

Quoted 35 lines · Namhyung Kim
Quoted 27 lines · Weilin
Quoted 19 lines

On Thu, May 30, 2024 at 11:46 PM Wang, Weilin <[email protected]>

Quoted 18 lines
Quoted 9 lines

tools/perf/util/intel-tpebs.c

Quoted 5 lines
tools/perf/util/intel-tpebs.c new file +5 -0
+       /*
+        * Only set retire_latency value to the first CPU and thread.
+        */
+       if (cpu_map_idx == 0 && thread == 0) {
+       /* Lost precision when casting from double to __u64. Any

improvement? */

As I said before I think you can set t->val * 1000 and then set the evsel->scale to 1e3 or 1e-3.

Hi Namhyung,

Sorry if this is a repeated message. I thought I replied to your suggestion on this last time. I'm thinking we should keep it like this for now and make this change unless we find the precision loss is critical. Because I thought we don't want to add special code to handle the calculation and final print to keep code simple.

Quoted 7 lines
Quoted 2 lines

I kept this comment here so that we don't forget about it. Please let me know if you'd like me to remove it.

Please see print_counter_aggrdata(). It's the generic code to print the event value and it'll display the value multiplied by the scale (default to 1.0). So you can keep precision as long as you set the scale properly (1e-3).

Quoted 4 lines

I could see the retire_latency is printed correctly after set the evsel->scale to 1e-3 and multiply the t->val * 1000. However, this scale is not used in metric calculations. We need to add code in metric calculation or display part to scale it as well. Is that acceptable or do you have other suggestions?

Quoted 5 lines

Hmm.. I don't know if other metric already dealt with the scale like with RAPL events.. If not, I think it's reasonable to add that to the metric calculation.

Ian, what do you think?

Tbh, I don't understand the conversation and it looks like we're in the weeds. In metrics the scale/unit from the event aren't used - that is all events in a metric are the unscaled quantities unless something is broken.

Thanks,
Ian

Quoted 3 lines

Thanks,
Namhyung

reviewer
#11 lore raw

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

Quoted 6 lines · Ian Rogers
Quoted 4 lines · Namhyung Kim

Hmm.. I don't know if other metric already dealt with the scale like with RAPL events.. If not, I think it's reasonable to add that to the metric calculation.

Quoted 6 lines
Quoted 1 line

Ian, what do you think?

Tbh, I don't understand the conversation and it looks like we're in the weeds. In metrics the scale/unit from the event aren't used - that is all events in a metric are the unscaled quantities unless something is broken.

As I understand the problem is that the retirement latencies are an average and so fractional, assigning these to a u64 count loses the fractional part. In the future we hope that the retirement latencies will be directly read from an evsel. Should we scale up and then scale down the retirement latency to avoid losing say 3 decimal places of precision by multiplying and dividing by 1000? As the metrics read unscaled values we'd need to change the formula. I'd prefer we didn't do that and generally avoid making the retirement latency evsels further special. So I think for now that means losing precision. We can at least avoid some of the worst rounding by using "rint" when we read the latency.

Thanks,
Ian

#12 lore raw

RFC PATCH v10 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 4157db30c3e7..e892d69e7839 100644
@@ -58,6 +58,7 @@
 #include <internal/xyarray.h>
 #include <internal/lib.h>
 #include <internal/threadmap.h>
+#include "util/intel-tpebs.h"
 
 #include <linux/ctype.h>
 
@@ -1523,6 +1524,11 @@ static int evsel__read_one(struct evsel *evsel, int cpu_map_idx, int thread)
 	return perf_evsel__read(&evsel->core, cpu_map_idx, thread, count);
 }
 
+static int evsel__read_retire_lat(struct evsel *evsel, int cpu_map_idx, int thread)
+{
+	return tpebs_set_evsel(evsel, cpu_map_idx, thread);
+}
+
 static void evsel__set_count(struct evsel *counter, int cpu_map_idx, int thread,
 			     u64 val, u64 ena, u64 run, u64 lost)
 {
@@ -1530,6 +1536,12 @@ static void evsel__set_count(struct evsel *counter, int cpu_map_idx, int thread,
 
 	count = perf_counts(counter->counts, cpu_map_idx, thread);
 
+	if (counter->retire_lat) {
+		evsel__read_retire_lat(counter, cpu_map_idx, thread);
+		perf_counts__set_loaded(counter->counts, cpu_map_idx, thread, true);
+		return;
+	}
+
 	count->val    = val;
 	count->ena    = ena;
 	count->run    = run;
@@ -1778,6 +1790,9 @@ int evsel__read_counter(struct evsel *evsel, int cpu_map_idx, int thread)
 	if (evsel__is_tool(evsel))
 		return evsel__read_tool(evsel, cpu_map_idx, thread);
 
+	if (evsel__is_retire_lat(evsel))
+		return evsel__read_retire_lat(evsel, cpu_map_idx, thread);
+
 	if (evsel->core.attr.read_format & PERF_FORMAT_GROUP)
 		return evsel__read_group(evsel, cpu_map_idx, thread);
 
tools/perf/util/evsel.h +5 -0
index bd8e84954e34..aaf572317e92 100644
@@ -303,6 +303,11 @@ static inline bool evsel__is_tool(const struct evsel *evsel)
 	return evsel->tool_event != PERF_TOOL_NONE;
 }
 
+static inline bool evsel__is_retire_lat(const struct evsel *evsel)
+{
+	return evsel->retire_lat;
+}
+
 const char *evsel__group_name(struct evsel *evsel);
 int evsel__group_desc(struct evsel *evsel, char *buf, size_t size);
 
Signature · 1 line
2.43.0
#13 lore raw

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

In reply to [email protected]

From: Weilin Wang <[email protected]>

Add MTL metric json file at TMA4.8. Some of the metrics' formulas use TPEBS retire_latency in MTL.

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

2,595 lines not on this page · raw message

Signature · 1 line
2.43.0
#14 lore raw

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

In reply to [email protected]

From: Weilin Wang <[email protected]>

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

Exampe with --enable-tpebs-recording:

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

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

Performance counter stats for 'system wide':

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

Exampe without --enable-tpebs-recording:

perf stat -M tma_dtlb_store -a sleep 1

Performance counter stats for 'system wide':

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

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

In reply to [email protected]

From: Weilin Wang <[email protected]>

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

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

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

In reply to [email protected]

From: Weilin Wang <[email protected]>

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

---
 .../perf/tests/shell/test_stat_intel_tpebs.sh | 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..43f75055fee4
@@ -0,0 +1,19 @@
+#!/bin/bash
+# test Intel TPEBS counting mode
+# SPDX-License-Identifier: GPL-2.0
+
+set e
+err=0
+
+# Use this event for testing because it should exist in all platforms
+e=cache-misses:R
+
+# Without this cmd option, default value or zero is returned
+echo "Testing without --enable-tpebs-recording"
+result=$(perf stat -e "$e" true 2>&1)
+[[ "$result" =~ "$e" ]] || exit 1
+
+# In platforms that do not support TPEBS, it should execute without error.
+echo "Testing with --enable-tpebs-recording"
+result=$(perf stat -e "$e" --enable-tpebs-recording -a sleep 0.01 2>&1)
+[[ "$result" =~ "perf record" && "$result" =~ "$e" ]] || exit 1
Signature · 1 line
2.43.0
#17 lore raw

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

From: [email protected]

Quoted 27 lines · On Wed, 29 May 2024 02:43:24 -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.

---
 .../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 +9 -0
new file mode 100755
index 000000000000..43f75055fee4
@@ -0,0 +1,19 @@
+#!/bin/bash
+# test Intel TPEBS counting mode
+# SPDX-License-Identifier: GPL-2.0
+
+set e
+err=0
+
+# Use this event for testing because it should exist in all platforms
+e=cache-misses:R

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

Quoted 10 lines
tools/perf/tests/shell/test_stat_intel_tpebs.sh new file +10 -0
+
+# Without this cmd option, default value or zero is returned
+echo "Testing without --enable-tpebs-recording"
+result=$(perf stat -e "$e" true 2>&1)
+[[ "$result" =~ "$e" ]] || exit 1
+
+# In platforms that do not support TPEBS, it should execute without error.
+echo "Testing with --enable-tpebs-recording"
+result=$(perf stat -e "$e" --enable-tpebs-recording -a sleep 0.01 2>&1)
+[[ "$result" =~ "perf record" && "$result" =~ "$e" ]] || exit 1

I got these build errors:

Thanks,
Namhyung

---

TEST tests/shell/test_stat_intel_tpebs.sh.shellcheck_log

In tests/shell/test_stat_intel_tpebs.sh line 6:
err=0
^-^ SC2034 (warning): err appears unused. Verify use (or export if used externally).

In tests/shell/test_stat_intel_tpebs.sh line 14:
[[ "$result" =~ "$e" ]] || exit 1
^--^ SC2076 (warning): Remove quotes from right-hand side of =~ to match as a regex rather than literally.

In tests/shell/test_stat_intel_tpebs.sh line 19:
[[ "$result" =~ "perf record" && "$result" =~ "$e" ]] || exit 1
^--^ SC2076 (warning): Remove quotes from right-hand side of =~ to match as a regex rather than literally.

For more information:
https://www.shellcheck.net/wiki/SC2034 -- err appears unused. Verify use (o...
https://www.shellcheck.net/wiki/SC2076 -- Remove quotes from right-hand sid...
make[4]: *** [tests/Build:91: tests/shell/test_stat_intel_tpebs.sh.shellcheck_log] Error 1

#18 lore raw

RE: [RFC PATCH v10 8/8] perf test: Add test for Intel TPEBS counting mode

Quoted 55 lines

-----Original Message-----
From: Namhyung Kim <[email protected]>
Sent: Sunday, June 2, 2024 4:21 PM
To: Wang, Weilin <[email protected]>
Cc: [email protected]; Namhyung Kim <[email protected]>; 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 v10 8/8] perf test: Add test for Intel TPEBS counting mode

From: [email protected]

Quoted 3 lines · On Wed, 29 May 2024 02:43:24 -0400 [email protected] wrote:

From: Weilin Wang <[email protected]>

Intel TPEBS sampling mode is supported through perf record. The counting

mode

Review

Quoted 10 lines

code uses perf record to capture retire_latency value and use it in metric calculation. This test checks the counting mode code.

---
 .../perf/tests/shell/test_stat_intel_tpebs.sh | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
 create mode 100755 tools/perf/tests/shell/test_stat_intel_tpebs.sh
(quoted diff) +0 -0

b/tools/perf/tests/shell/test_stat_intel_tpebs.sh

Quoted 18 lines

new file mode 100755
index 000000000000..43f75055fee4

tools/perf/tests/shell/test_stat_intel_tpebs.sh new file +13 -0
@@ -0,0 +1,19 @@
+#!/bin/bash
+# test Intel TPEBS counting mode
+# SPDX-License-Identifier: GPL-2.0
+
+set e
+err=0
+
+# Use this event for testing because it should exist in all platforms
+e=cache-misses:R
+
+# Without this cmd option, default value or zero is returned
+echo "Testing without --enable-tpebs-recording"
+result=$(perf stat -e "$e" true 2>&1)
Quoted 9 lines

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

Quoted 6 lines
tools/perf/tests/shell/test_stat_intel_tpebs.sh new file +6 -0
+[[ "$result" =~ "$e" ]] || exit 1
+
+# In platforms that do not support TPEBS, it should execute without error.
+echo "Testing with --enable-tpebs-recording"
+result=$(perf stat -e "$e" --enable-tpebs-recording -a sleep 0.01 2>&1)
+[[ "$result" =~ "perf record" && "$result" =~ "$e" ]] || exit 1

I got these build errors:

Sorry about the error! I will fix it.

Thanks,
Weilin

Quoted 32 lines

Thanks,
Namhyung

---

TEST tests/shell/test_stat_intel_tpebs.sh.shellcheck_log

In tests/shell/test_stat_intel_tpebs.sh line 6:
err=0
^-^ SC2034 (warning): err appears unused. Verify use (or export if used externally).

In tests/shell/test_stat_intel_tpebs.sh line 14:
[[ "$result" =~ "$e" ]] || exit 1
^--^ SC2076 (warning): Remove quotes from right-hand side of =~ to
match as a regex rather than literally.

In tests/shell/test_stat_intel_tpebs.sh line 19:
[[ "$result" =~ "perf record" && "$result" =~ "$e" ]] || exit 1
^--^ SC2076 (warning): Remove quotes from right-
hand side of =~ to match as a regex rather than literally.

For more information:
https://www.shellcheck.net/wiki/SC2034 -- err appears unused. Verify use
(o...
https://www.shellcheck.net/wiki/SC2076 -- Remove quotes from right-hand
sid...
make[4]: *** [tests/Build:91:
tests/shell/test_stat_intel_tpebs.sh.shellcheck_log] Error 1

maintainer
#19 lore raw
In reply to [email protected]

Hello,

Quoted 8 lines · On Tue, May 28, 2024 at 11:43 PM <[email protected]> wrote:

From: Weilin Wang <[email protected]>

I have tried not to count retire_latency events but did not succeed. In particular, I tried the following methods:
- Convert retire_latency event to dummy event in event parser.
- Early bail out in evsel__open_cpu() and store_evsel_ids().

Quoted 5 lines

The first method fails and causes non-retire_latency events with the same event name return 0 count.

The second method fails and causes all the events in the same group returning "<not counted>" results.

Can you please describe where it fails? Is it failing on other events because the tpebs event is a leader of the group? I think you wanted to avoid having it in the leader position. If we can skip any actual operations (open/close/enable/disable/read) for the tpebs events, then it could be fine..

Thanks,
Namhyung

Quoted 154 lines

Because of above, the retire_latency event will still run in counting mode.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Performance counter stats for 'system wide':

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

0.00 MEM_INST_RETIRED.STLB_HIT_STORES:p 0 0

1.003105924 seconds time elapsed

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

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

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

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

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

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

#20 lore raw
Quoted 32 lines

-----Original Message-----
From: Namhyung Kim <[email protected]>
Sent: Thursday, May 30, 2024 11:37 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 v10 0/8] TPEBS counting mode support

Hello,

Quoted 9 lines · On Tue, May 28, 2024 at 11:43 PM <[email protected]> wrote:

From: Weilin Wang <[email protected]>

I have tried not to count retire_latency events but did not succeed. In particular, I tried the following methods:
- Convert retire_latency event to dummy event in event parser.
- Early bail out in evsel__open_cpu() and store_evsel_ids().

The first method fails and causes non-retire_latency events with the same

event

Quoted 3 lines

name return 0 count.

The second method fails and causes all the events in the same group

Quoted 8 lines

returning

Quoted 1 line

"<not counted>" results.

Can you please describe where it fails? Is it failing on other events because the tpebs event is a leader of the group? I think you wanted to avoid having it in the leader position. If we can skip any actual operations (open/close/enable/disable/read) for the tpebs events, then it could be fine..

It does not fail with the code in this patch set. But if I make it return directly from tpebs_start() in evsel__open_cpu(), it will cause segfault. The segfault is caused by store_evsel_id(). I could add another early return from store_evsel_id() if the evsel->retire_lat is true.

After this change, it will eventually run and give me <not counted> results like below:

<not counted> event1
<not counted> event2
xx event1:R

In a different case, it may seem to work (xxxxxx stands for some valid value):

xxxxxxx event1
xxxxxxx event2
xxxxxxx event3
xx event1:R

In the first case, the event1, event2 and event1:R are scheduled in the same group. On the other hand, in the second case, event1, event2 and event3 are in one group, while event1:R is in a different group.

Based on these two different type of results, I believe the failure happens in the group that include a :R event. I've added the change to arch_evlist__cmp() so that a :R event would not be a leader of the group.

I think I've made evsel__open_cpu() return before it create fd and make store_evsel_id() not to read and store fd. I'm not sure where I'm missing. Please let me know if you have any suggestions.

Thanks,
Weilin

Quoted 179 lines

Thanks,
Namhyung

Quoted 8 lines

Because of above, the retire_latency event will still run in counting mode.

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

Quoted 6 lines

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/20240521173952.3397644-1-

[email protected]/

Quoted 3 lines

Changes in v9:
- Update the retire_latency result print and metric calculation method.

Plugin

Quoted 8 lines

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/20240428053616.1125891-3-

[email protected]/

Quoted 7 lines

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,

Quoted 1 line

it would already require to count the event. As a result, we will endup count

the

Quoted 1 line

same event twice. This should be able to be handled properly when we

finalize our

Quoted 5 lines

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]

Quoted 11 lines

- 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]

Quoted 2 lines

- 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]

Quoted 9 lines

- 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

Quoted 11 lines

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.

Quoted 2 lines

In this simple example, there is no MEM_INST_RETIRED.STLB_HIT_STORES

sample.

Quoted 1 line

Therefore, the MEM_INST_RETIRED.STLB_HIT_STORES:p count and

retire_latency value

Quoted 10 lines

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

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

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

Quoted 4 lines

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

Quoted 7 lines

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

Quoted 25 lines

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

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

 tools/perf/Documentation/perf-list.txt        |    1 +
 tools/perf/Documentation/topdown.txt          |   30 +
 tools/perf/arch/x86/util/evlist.c             |    6 +
 tools/perf/builtin-stat.c                     |    8 +
 .../arch/x86/meteorlake/metricgroups.json     |  140 +
 .../arch/x86/meteorlake/mtl-metrics.json      | 2595 +++++++++++++++++
 .../perf/tests/shell/test_stat_intel_tpebs.sh |   19 +
 tools/perf/util/Build                         |    1 +
 tools/perf/util/data.c                        |    7 +-
 tools/perf/util/evsel.c                       |   26 +
Quoted 8 lines
 tools/perf/util/evsel.h                       |    6 +
 tools/perf/util/intel-tpebs.c                 |  397 +++
 tools/perf/util/intel-tpebs.h                 |   48 +
 tools/perf/util/parse-events.c                |    2 +
 tools/perf/util/parse-events.h                |    1 +
 tools/perf/util/parse-events.l                |    3 +-
 16 files changed, 3288 insertions(+), 2 deletions(-)
 create mode 100644 tools/perf/pmu-

events/arch/x86/meteorlake/metricgroups.json

Quoted 1 line
 create mode 100644 tools/perf/pmu-events/arch/x86/meteorlake/mtl-

metrics.json

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

maintainer
#21 lore raw
Quoted 42 lines · Weilin
Quoted 33 lines

-----Original Message-----
From: Namhyung Kim <[email protected]>
Sent: Thursday, May 30, 2024 11:37 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 v10 0/8] TPEBS counting mode support

Hello,

Quoted 9 lines · On Tue, May 28, 2024 at 11:43 PM <[email protected]> wrote:

From: Weilin Wang <[email protected]>

I have tried not to count retire_latency events but did not succeed. In particular, I tried the following methods:
- Convert retire_latency event to dummy event in event parser.
- Early bail out in evsel__open_cpu() and store_evsel_ids().

The first method fails and causes non-retire_latency events with the same

event

Quoted 3 lines

name return 0 count.

The second method fails and causes all the events in the same group

Quoted 8 lines

returning

Quoted 1 line

"<not counted>" results.

Can you please describe where it fails? Is it failing on other events because the tpebs event is a leader of the group? I think you wanted to avoid having it in the leader position. If we can skip any actual operations (open/close/enable/disable/read) for the tpebs events, then it could be fine..

Quoted 4 lines

It does not fail with the code in this patch set. But if I make it return directly from tpebs_start() in evsel__open_cpu(), it will cause segfault. The segfault is caused by store_evsel_id(). I could add another early return from store_evsel_id() if the evsel->retire_lat is true.

Yeah, I think event:R should not go to kernel from perf stat and you need to handle that in the tools.

Quoted 10 lines

After this change, it will eventually run and give me <not counted> results like below:

<not counted> event1
<not counted> event2
xx event1:R

In a different case, it may seem to work (xxxxxx stands for some valid value):

Quoted 8 lines

xxxxxxx event1
xxxxxxx event2
xxxxxxx event3
xx event1:R

In the first case, the event1, event2 and event1:R are scheduled in the same group. On the other hand, in the second case, event1, event2 and event3 are in one group, while event1:R is in a different group.

If you don't open event1:R then the kernel only sees event1 and event2.

Quoted 8 lines

Based on these two different type of results, I believe the failure happens in the group that include a :R event. I've added the change to arch_evlist__cmp() so that a :R event would not be a leader of the group.

I think I've made evsel__open_cpu() return before it create fd and make store_evsel_id() not to read and store fd. I'm not sure where I'm missing. Please let me know if you have any suggestions.

As I said, please don't open event1:R (for perf stat) and let tpebs_stop() set the value using the data from perf record in background.

Thanks,
Namhyung

#22 lore raw
Quoted 99 lines

-----Original Message-----
From: Namhyung Kim <[email protected]>
Sent: Friday, May 31, 2024 2:30 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 v10 0/8] TPEBS counting mode support

Quoted 13 lines · Weilin
Quoted 13 lines

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

Perry

Quoted 1 line
Quoted 1 line

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

Biggers,

Quoted 28 lines
Quoted 19 lines

Caleb <[email protected]>
Subject: Re: [RFC PATCH v10 0/8] TPEBS counting mode support

Hello,

Quoted 9 lines · On Tue, May 28, 2024 at 11:43 PM <[email protected]> wrote:

From: Weilin Wang <[email protected]>

I have tried not to count retire_latency events but did not succeed. In particular, I tried the following methods:
- Convert retire_latency event to dummy event in event parser.
- Early bail out in evsel__open_cpu() and store_evsel_ids().

The first method fails and causes non-retire_latency events with the same

event

Quoted 3 lines

name return 0 count.

The second method fails and causes all the events in the same group

Quoted 8 lines

returning

Quoted 1 line

"<not counted>" results.

Can you please describe where it fails? Is it failing on other events because the tpebs event is a leader of the group? I think you wanted to avoid having it in the leader position. If we can skip any actual operations (open/close/enable/disable/read) for the tpebs events, then it could be fine..

Quoted 3 lines

It does not fail with the code in this patch set. But if I make it return directly from tpebs_start() in evsel__open_cpu(), it will cause segfault. The segfault is caused by store_evsel_id(). I could add another early return from

store_evsel_id()

Quoted 1 line

if the evsel->retire_lat is true.

Yeah, I think event:R should not go to kernel from perf stat and you need to handle that in the tools.

Quoted 10 lines

After this change, it will eventually run and give me <not counted> results like below:

<not counted> event1
<not counted> event2
xx event1:R

In a different case, it may seem to work (xxxxxx stands for some valid value):

Quoted 7 lines

xxxxxxx event1
xxxxxxx event2
xxxxxxx event3
xx event1:R

In the first case, the event1, event2 and event1:R are scheduled in the same group. On the other hand, in the second case, event1, event2 and event3

are

Quoted 1 line

in one group, while event1:R is in a different group.

If you don't open event1:R then the kernel only sees event1 and event2.

Quoted 3 lines

Based on these two different type of results, I believe the failure happens in the group that include a :R event. I've added the change to

arch_evlist__cmp()

Quoted 2 lines

so that a :R event would not be a leader of the group.

Quoted 7 lines
Quoted 2 lines

I think I've made evsel__open_cpu() return before it create fd and make store_evsel_id() not to read and store fd. I'm not sure where I'm missing.

Please

Quoted 1 line

let me know if you have any suggestions.

As I said, please don't open event1:R (for perf stat) and let tpebs_stop() set the value using the data from perf record in background.

I think this is exactly what I'm trying to achieve, not open event1:R for perf stat. The problem is that I'm not sure how to do it properly in the code. Please give me some suggestion here.

Thanks,
Weilin

Quoted 3 lines

Thanks,
Namhyung

maintainer
#23 lore raw
Quoted 9 lines · Weilin
Quoted 5 lines

-----Original Message-----
From: Namhyung Kim <[email protected]>
As I said, please don't open event1:R (for perf stat) and let tpebs_stop() set the value using the data from perf record in background.

I think this is exactly what I'm trying to achieve, not open event1:R for perf stat. The problem is that I'm not sure how to do it properly in the code. Please give me some suggestion here.

Ok, I think the problem is in the read code. It requires the number of entries and the data size to match with what it calculates from the member events. It should not count TPEBS events as we don't want to open them.

Something like below might work (on top of your series). Probably libperf should be aware of this..

Thanks,
Namhyung

---8<---

tools/perf/util/evsel.c +57 -4
index ac7a98ff6b19..7913db4a99e0 100644
@@ -1559,6 +1559,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;
@@ -1567,7 +1621,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)
@@ -1597,7 +1651,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))
@@ -2210,8 +2264,7 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 
 	if (evsel__is_retire_lat(evsel)) {
 		err = tpebs_start(evsel->evlist, cpus);
-		if (err)
-			return err;
+		return err;
 	}
 
 	err = __evsel__prepare_open(evsel, cpus, threads);
tools/perf/util/intel-tpebs.c +2 -1
index d099fc8080e1..a3857e88af96 100644
@@ -354,10 +354,11 @@ int tpebs_set_evsel(struct evsel *evsel, int cpu_map_idx, int thread)
 	 */
 	if (cpu_map_idx == 0 && thread == 0) {
 	/* Lost precision when casting from double to __u64. Any improvement? */
-		val = t->val;
+		val = t->val * 1000;
 	} else
 		val = 0;
 
+	evsel->scale = 1e-3;
 	count->val = val;
 	return 0;
 }
#24 lore raw
Quoted 29 lines

-----Original Message-----
From: Namhyung Kim <[email protected]>
Sent: Sunday, June 2, 2024 2:18 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 v10 0/8] TPEBS counting mode support

Quoted 7 lines · Weilin
Quoted 5 lines

-----Original Message-----
From: Namhyung Kim <[email protected]>
As I said, please don't open event1:R (for perf stat) and let tpebs_stop() set the value using the data from perf record in background.

I think this is exactly what I'm trying to achieve, not open event1:R for perf

stat.

Quoted 1 line

The problem is that I'm not sure how to do it properly in the code. Please

give

Quoted 1 line

me some suggestion here.

Quoted 8 lines

Ok, I think the problem is in the read code. It requires the number of entries and the data size to match with what it calculates from the member events. It should not count TPEBS events as we don't want to open them.

Something like below might work (on top of your series). Probably libperf should be aware of this..

Thanks Namhyung! I will integrate this patch and test it out.

Thanks,
Weilin

Quoted 123 lines

Thanks,
Namhyung

---8<---

tools/perf/util/evsel.c +54 -0
index ac7a98ff6b19..7913db4a99e0 100644
@@ -1559,6 +1559,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)
{

tools/perf/util/evsel.c +1 -1
 	u64 read_format = leader->core.attr.read_format;
@@ -1567,7 +1621,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;
 
tools/perf/util/evsel.c +1 -1
 	if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
@@ -1597,7 +1651,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;
 
tools/perf/util/evsel.c +1 -2
 	if (!(read_format & PERF_FORMAT_ID))
@@ -2210,8 +2264,7 @@ static int evsel__open_cpu(struct evsel *evsel,
 struct perf_cpu_map *cpus,
 
 	if (evsel__is_retire_lat(evsel)) {
 		err = tpebs_start(evsel->evlist, cpus);
-		if (err)
-			return err;
+		return err;
 	}
 
tools/perf/util/evsel.c +0 -0
 	err = __evsel__prepare_open(evsel, cpus, threads);
tools/perf/util/intel-tpebs.c +0 -0
index d099fc8080e1..a3857e88af96 100644
@@ -354,10 +354,11 @@ int tpebs_set_evsel(struct evsel *evsel, int
 cpu_map_idx, int thread)
 	 */
 	if (cpu_map_idx == 0 && thread == 0) {
 	/* Lost precision when casting from double to __u64. Any

improvement? */

tools/perf/util/intel-tpebs.c +2 -1
-		val = t->val;
+		val = t->val * 1000;
 	} else
 		val = 0;
 
+	evsel->scale = 1e-3;
 	count->val = val;
 	return 0;
 }