Skip to contentSkip to conversation

perf intel-tpebs: Filter non-workload samples

PATCH Applied Ian Rogers 3 participants 3 messages Apr 30, 2025 – May 16, 2025 perf events

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

PATCH 1/1 perf intel-tpebs: Filter non-workload samples

If perf is running with a benchmark then we want the retirement latency samples associated with the benchmark rather than from the system as a whole. Use the workload's PID to filter out samples that aren't from the workload or its children.

---
 tools/perf/util/intel-tpebs.c | 59 ++++++++++++++++++++++++++++++++++-
 1 file changed, 58 insertions(+), 1 deletion(-)
tools/perf/util/intel-tpebs.c +58 -1
index 7fd6cae1063e..4ad4bc118ea5 100644
@@ -3,7 +3,7 @@
  * intel_tpebs.c: Intel TPEBS support
  */
 
-
+#include <api/fs/fs.h>
 #include <sys/param.h>
 #include <subcmd/run-command.h>
 #include <thread.h>
@@ -121,6 +121,59 @@ static int evsel__tpebs_start_perf_record(struct evsel *evsel)
 	return ret;
 }
 
+static bool is_child_pid(pid_t parent, pid_t child)
+{
+	if (parent < 0 || child < 0)
+		return false;
+
+	while (true) {
+		char path[PATH_MAX];
+		char line[256];
+		FILE *fp;
+
+new_child:
+		if (parent == child)
+			return true;
+
+		if (child <= 0)
+			return false;
+
+		scnprintf(path, sizeof(path), "%s/%d/status", procfs__mountpoint(), child);
+		fp = fopen(path, "r");
+		if (!fp) {
+			/* Presumably the process went away. Assume not a child. */
+			return false;
+		}
+		while (fgets(line, sizeof(line), fp) != NULL) {
+			if (strncmp(line, "PPid:", 5) == 0) {
+				fclose(fp);
+				if (sscanf(line + 5, "%d", &child) != 1) {
+					/* Unexpected error parsing. */
+					return false;
+				}
+				goto new_child;
+			}
+		}
+		/* Unexpected EOF. */
+		fclose(fp);
+		return false;
+	}
+}
+
+static bool should_ignore_sample(const struct perf_sample *sample, const struct tpebs_retire_lat *t)
+{
+	pid_t workload_pid = t->evsel->evlist->workload.pid;
+	pid_t sample_pid = sample->pid;
+
+	if (workload_pid < 0 || workload_pid == sample_pid)
+		return false;
+
+	if (!t->evsel->core.attr.inherit)
+		return true;
+
+	return !is_child_pid(workload_pid, sample_pid);
+}
+
 static int process_sample_event(const struct perf_tool *tool __maybe_unused,
 				union perf_event *event __maybe_unused,
 				struct perf_sample *sample,
@@ -140,6 +193,10 @@ static int process_sample_event(const struct perf_tool *tool __maybe_unused,
 		mutex_unlock(tpebs_mtx_get());
 		return -EINVAL;
 	}
+	if (should_ignore_sample(sample, t)) {
+		mutex_unlock(tpebs_mtx_get());
+		return 0;
+	}
 	/*
 	 * Need to handle per core results? We are assuming average retire
 	 * latency value will be used. Save the number of samples and the sum of
Signature · 1 line
2.49.0.906.g1f30a19c02-goog
Tested-by on v1 1/1
#1 lore raw
Quoted 15 lines

-----Original Message-----
From: Ian Rogers <[email protected]>
Sent: Wednesday, April 30, 2025 1:01 PM
To: Peter Zijlstra <[email protected]>; Ingo Molnar <[email protected]>; Arnaldo Carvalho de Melo <[email protected]>; Namhyung Kim <[email protected]>; Mark Rutland <[email protected]>; Alexander Shishkin <[email protected]>; Jiri Olsa <[email protected]>; Ian Rogers <[email protected]>; Hunter, Adrian <[email protected]>; Kan Liang <[email protected]>; Wang, Weilin <[email protected]>; James Clark <[email protected]>; linux- [email protected]; [email protected]
Subject: [PATCH v1] perf intel-tpebs: Filter non-workload samples

Quoted 6 lines

If perf is running with a benchmark then we want the retirement latency samples associated with the benchmark rather than from the system as a whole. Use the workload's PID to filter out samples that aren't from the workload or its children.

Quoted 98 lines
---
 tools/perf/util/intel-tpebs.c | 59
++++++++++++++++++++++++++++++++++-
 1 file changed, 58 insertions(+), 1 deletion(-)
tools/perf/util/intel-tpebs.c +22 -1
index 7fd6cae1063e..4ad4bc118ea5 100644
@@ -3,7 +3,7 @@
  * intel_tpebs.c: Intel TPEBS support
  */
 
-
+#include <api/fs/fs.h>
 #include <sys/param.h>
 #include <subcmd/run-command.h>
 #include <thread.h>
@@ -121,6 +121,59 @@ static int evsel__tpebs_start_perf_record(struct
 evsel *evsel)
 	return ret;
 }
 
+static bool is_child_pid(pid_t parent, pid_t child)
+{
+	if (parent < 0 || child < 0)
+		return false;
+
+	while (true) {
+		char path[PATH_MAX];
+		char line[256];
+		FILE *fp;
+
+new_child:
+		if (parent == child)
+			return true;
+
+		if (child <= 0)
+			return false;
+
+		scnprintf(path, sizeof(path), "%s/%d/status",
 procfs__mountpoint(), child);
+		fp = fopen(path, "r");
+		if (!fp) {
+			/* Presumably the process went away. Assume not a

child. */

tools/perf/util/intel-tpebs.c +19 -0
+			return false;
+		}
+		while (fgets(line, sizeof(line), fp) != NULL) {
+			if (strncmp(line, "PPid:", 5) == 0) {
+				fclose(fp);
+				if (sscanf(line + 5, "%d", &child) != 1) {
+					/* Unexpected error parsing. */
+					return false;
+				}
+				goto new_child;
+			}
+		}
+		/* Unexpected EOF. */
+		fclose(fp);
+		return false;
+	}
+}
+
+static bool should_ignore_sample(const struct perf_sample *sample, const

struct tpebs_retire_lat *t)

tools/perf/util/intel-tpebs.c +13 -0
+{
+	pid_t workload_pid = t->evsel->evlist->workload.pid;
+	pid_t sample_pid = sample->pid;
+
+	if (workload_pid < 0 || workload_pid == sample_pid)
+		return false;
+
+	if (!t->evsel->core.attr.inherit)
+		return true;
+
+	return !is_child_pid(workload_pid, sample_pid);
+}
+
 static int process_sample_event(const struct perf_tool *tool

__maybe_unused,

tools/perf/util/intel-tpebs.c +4 -0
 				union perf_event *event __maybe_unused,
 				struct perf_sample *sample,
@@ -140,6 +193,10 @@ static int process_sample_event(const struct
 perf_tool *tool __maybe_unused,
 		mutex_unlock(tpebs_mtx_get());
 		return -EINVAL;
 	}
+	if (should_ignore_sample(sample, t)) {
+		mutex_unlock(tpebs_mtx_get());
+		return 0;
+	}
 	/*
 	 * Need to handle per core results? We are assuming average retire

* latency value will be used. Save the number of samples and the sum
of
--
2.49.0.906.g1f30a19c02-goog

maintainer Applied
#2 lore raw
Quoted 13 lines · Weilin
Quoted 13 lines

-----Original Message-----
From: Ian Rogers <[email protected]>
Sent: Wednesday, April 30, 2025 1:01 PM
To: Peter Zijlstra <[email protected]>; Ingo Molnar <[email protected]>; Arnaldo Carvalho de Melo <[email protected]>; Namhyung Kim <[email protected]>; Mark Rutland <[email protected]>; Alexander Shishkin <[email protected]>; Jiri Olsa <[email protected]>; Ian Rogers <[email protected]>; Hunter, Adrian <[email protected]>; Kan Liang <[email protected]>; Wang, Weilin <[email protected]>; James Clark <[email protected]>; linux- [email protected]; [email protected]
Subject: [PATCH v1] perf intel-tpebs: Filter non-workload samples

Quoted 8 lines
Quoted 6 lines

If perf is running with a benchmark then we want the retirement latency samples associated with the benchmark rather than from the system as a whole. Use the workload's PID to filter out samples that aren't from the workload or its children.

Thanks, applied to perf-tools-next,

- Arnaldo

Quoted 99 lines
Quoted 98 lines
---
 tools/perf/util/intel-tpebs.c | 59
++++++++++++++++++++++++++++++++++-
 1 file changed, 58 insertions(+), 1 deletion(-)
tools/perf/util/intel-tpebs.c +22 -1
index 7fd6cae1063e..4ad4bc118ea5 100644
@@ -3,7 +3,7 @@
  * intel_tpebs.c: Intel TPEBS support
  */
 
-
+#include <api/fs/fs.h>
 #include <sys/param.h>
 #include <subcmd/run-command.h>
 #include <thread.h>
@@ -121,6 +121,59 @@ static int evsel__tpebs_start_perf_record(struct
 evsel *evsel)
 	return ret;
 }
 
+static bool is_child_pid(pid_t parent, pid_t child)
+{
+	if (parent < 0 || child < 0)
+		return false;
+
+	while (true) {
+		char path[PATH_MAX];
+		char line[256];
+		FILE *fp;
+
+new_child:
+		if (parent == child)
+			return true;
+
+		if (child <= 0)
+			return false;
+
+		scnprintf(path, sizeof(path), "%s/%d/status",
 procfs__mountpoint(), child);
+		fp = fopen(path, "r");
+		if (!fp) {
+			/* Presumably the process went away. Assume not a

child. */

tools/perf/util/intel-tpebs.c +19 -0
+			return false;
+		}
+		while (fgets(line, sizeof(line), fp) != NULL) {
+			if (strncmp(line, "PPid:", 5) == 0) {
+				fclose(fp);
+				if (sscanf(line + 5, "%d", &child) != 1) {
+					/* Unexpected error parsing. */
+					return false;
+				}
+				goto new_child;
+			}
+		}
+		/* Unexpected EOF. */
+		fclose(fp);
+		return false;
+	}
+}
+
+static bool should_ignore_sample(const struct perf_sample *sample, const

struct tpebs_retire_lat *t)

tools/perf/util/intel-tpebs.c +13 -0
+{
+	pid_t workload_pid = t->evsel->evlist->workload.pid;
+	pid_t sample_pid = sample->pid;
+
+	if (workload_pid < 0 || workload_pid == sample_pid)
+		return false;
+
+	if (!t->evsel->core.attr.inherit)
+		return true;
+
+	return !is_child_pid(workload_pid, sample_pid);
+}
+
 static int process_sample_event(const struct perf_tool *tool

__maybe_unused,

tools/perf/util/intel-tpebs.c +4 -0
 				union perf_event *event __maybe_unused,
 				struct perf_sample *sample,
@@ -140,6 +193,10 @@ static int process_sample_event(const struct
 perf_tool *tool __maybe_unused,
 		mutex_unlock(tpebs_mtx_get());
 		return -EINVAL;
 	}
+	if (should_ignore_sample(sample, t)) {
+		mutex_unlock(tpebs_mtx_get());
+		return 0;
+	}
 	/*
 	 * Need to handle per core results? We are assuming average retire

* latency value will be used. Save the number of samples and the sum
of
--
2.49.0.906.g1f30a19c02-goog