* [PATCH v1] perf intel-tpebs: Filter non-workload samples
@ 2025-04-30 20:01 Ian Rogers
2025-05-15 20:46 ` Wang, Weilin
0 siblings, 1 reply; 3+ messages in thread
From: Ian Rogers @ 2025-04-30 20:01 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, Kan Liang, Weilin Wang, James Clark,
linux-perf-users, linux-kernel
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.
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/intel-tpebs.c | 59 ++++++++++++++++++++++++++++++++++-
1 file changed, 58 insertions(+), 1 deletion(-)
diff --git a/tools/perf/util/intel-tpebs.c b/tools/perf/util/intel-tpebs.c
index 7fd6cae1063e..4ad4bc118ea5 100644
--- a/tools/perf/util/intel-tpebs.c
+++ b/tools/perf/util/intel-tpebs.c
@@ -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
--
2.49.0.906.g1f30a19c02-goog
^ permalink raw reply related [flat|nested] 3+ messages in thread
* RE: [PATCH v1] perf intel-tpebs: Filter non-workload samples
2025-04-30 20:01 [PATCH v1] perf intel-tpebs: Filter non-workload samples Ian Rogers
@ 2025-05-15 20:46 ` Wang, Weilin
2025-05-16 14:35 ` Arnaldo Carvalho de Melo
0 siblings, 1 reply; 3+ messages in thread
From: Wang, Weilin @ 2025-05-15 20:46 UTC (permalink / raw)
To: Ian Rogers, Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Hunter, Adrian, Kan Liang, James Clark,
linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
> -----Original Message-----
> From: Ian Rogers <irogers@google.com>
> Sent: Wednesday, April 30, 2025 1:01 PM
> To: Peter Zijlstra <peterz@infradead.org>; Ingo Molnar <mingo@redhat.com>;
> Arnaldo Carvalho de Melo <acme@kernel.org>; Namhyung Kim
> <namhyung@kernel.org>; Mark Rutland <mark.rutland@arm.com>;
> Alexander Shishkin <alexander.shishkin@linux.intel.com>; Jiri Olsa
> <jolsa@kernel.org>; Ian Rogers <irogers@google.com>; Hunter, Adrian
> <adrian.hunter@intel.com>; Kan Liang <kan.liang@linux.intel.com>; Wang,
> Weilin <weilin.wang@intel.com>; James Clark <james.clark@linaro.org>; linux-
> perf-users@vger.kernel.org; linux-kernel@vger.kernel.org
> Subject: [PATCH v1] 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.
>
> Signed-off-by: Ian Rogers <irogers@google.com>
Tested-by: Weilin Wang <weilin.wang@intel.com>
> ---
> tools/perf/util/intel-tpebs.c | 59
> ++++++++++++++++++++++++++++++++++-
> 1 file changed, 58 insertions(+), 1 deletion(-)
>
> diff --git a/tools/perf/util/intel-tpebs.c b/tools/perf/util/intel-tpebs.c
> index 7fd6cae1063e..4ad4bc118ea5 100644
> --- a/tools/perf/util/intel-tpebs.c
> +++ b/tools/perf/util/intel-tpebs.c
> @@ -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
> --
> 2.49.0.906.g1f30a19c02-goog
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v1] perf intel-tpebs: Filter non-workload samples
2025-05-15 20:46 ` Wang, Weilin
@ 2025-05-16 14:35 ` Arnaldo Carvalho de Melo
0 siblings, 0 replies; 3+ messages in thread
From: Arnaldo Carvalho de Melo @ 2025-05-16 14:35 UTC (permalink / raw)
To: Wang, Weilin
Cc: Ian Rogers, Peter Zijlstra, Ingo Molnar, Namhyung Kim,
Mark Rutland, Alexander Shishkin, Jiri Olsa, Hunter, Adrian,
Kan Liang, James Clark, linux-perf-users@vger.kernel.org,
linux-kernel@vger.kernel.org
On Thu, May 15, 2025 at 08:46:22PM +0000, Wang, Weilin wrote:
> > -----Original Message-----
> > From: Ian Rogers <irogers@google.com>
> > Sent: Wednesday, April 30, 2025 1:01 PM
> > To: Peter Zijlstra <peterz@infradead.org>; Ingo Molnar <mingo@redhat.com>;
> > Arnaldo Carvalho de Melo <acme@kernel.org>; Namhyung Kim
> > <namhyung@kernel.org>; Mark Rutland <mark.rutland@arm.com>;
> > Alexander Shishkin <alexander.shishkin@linux.intel.com>; Jiri Olsa
> > <jolsa@kernel.org>; Ian Rogers <irogers@google.com>; Hunter, Adrian
> > <adrian.hunter@intel.com>; Kan Liang <kan.liang@linux.intel.com>; Wang,
> > Weilin <weilin.wang@intel.com>; James Clark <james.clark@linaro.org>; linux-
> > perf-users@vger.kernel.org; linux-kernel@vger.kernel.org
> > Subject: [PATCH v1] 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.
> >
> > Signed-off-by: Ian Rogers <irogers@google.com>
> Tested-by: Weilin Wang <weilin.wang@intel.com>
Thanks, applied to perf-tools-next,
- Arnaldo
> > ---
> > tools/perf/util/intel-tpebs.c | 59
> > ++++++++++++++++++++++++++++++++++-
> > 1 file changed, 58 insertions(+), 1 deletion(-)
> >
> > diff --git a/tools/perf/util/intel-tpebs.c b/tools/perf/util/intel-tpebs.c
> > index 7fd6cae1063e..4ad4bc118ea5 100644
> > --- a/tools/perf/util/intel-tpebs.c
> > +++ b/tools/perf/util/intel-tpebs.c
> > @@ -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
> > --
> > 2.49.0.906.g1f30a19c02-goog
>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-05-16 14:35 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-30 20:01 [PATCH v1] perf intel-tpebs: Filter non-workload samples Ian Rogers
2025-05-15 20:46 ` Wang, Weilin
2025-05-16 14:35 ` Arnaldo Carvalho de Melo
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).