Skip to contentSkip to conversation

perf, tools: Add PERF_PID

PATCH In review Andi Kleen 2 participants 2 messages Aug 13, 2014 – Aug 19, 2014 perf events

lore
Share & tools
lore

Andi Kleen

author
#0 lore raw

PATCH 1/1 perf, tools: Add PERF_PID

From: Andi Kleen <[email protected]>

It's currently difficult to filter out perf itself using a filter. This can give cascading effects during IO tracing when the IO perf does itself causes more trace output.

The best way to filter is to use the pid. But it's difficult to get the pid of perf without using hacks.

Add a PERF_PID meta variable to the perf filter that contains the current pid.

With this patch the following works

% perf record -e syscalls:sys_enter_write -a --filter 'common_pid != PERF_PID' ...

This won't work for more complex perf pipe lines with multiple processes, but at least solves the problem nicely for a single perf.

---
 tools/perf/Documentation/perf-record.txt | 2 +-
 tools/perf/util/parse-events.c           | 9 +++++++++
 2 files changed, 10 insertions(+), 1 deletion(-)
tools/perf/Documentation/perf-record.txt +1 -1
index d460049..b6c5e51 100644
@@ -41,7 +41,7 @@ OPTIONS
           'mem:0x1000:rw'.
 
 --filter=<filter>::
-        Event filter.
+        Event filter. PERF_PID represents the perf pid.
 
 -a::
 --all-cpus::
tools/perf/util/parse-events.c +9 -0
index 1e15df1..90ed63f 100644
@@ -967,6 +967,7 @@ int parse_filter(const struct option *opt, const char *str,
 {
 	struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
 	struct perf_evsel *last = NULL;
+	char *pid;
 
 	if (evlist->nr_entries > 0)
 		last = perf_evlist__last(evlist);
@@ -983,6 +984,14 @@ int parse_filter(const struct option *opt, const char *str,
 		return -1;
 	}
 
+	/* Assume a pid has not more than 8 characters */
+	pid = strstr(last->filter, "PERF_PID");
+	if (pid) {
+		char buf[9];
+		snprintf(buf, 9, "%08d", getpid());
+		memcpy(pid, buf, 8);
+	}
+	fprintf(stderr, "filter |%s|\n", last->filter);
 	return 0;
 }
 
Signature · 1 line
1.9.3
maintainer Acked-by on v1 1/1
#1 lore raw

Hi Andi,

Quoted 11 lines · Andi Kleen

From: Andi Kleen <[email protected]>

It's currently difficult to filter out perf itself using a filter. This can give cascading effects during IO tracing when the IO perf does itself causes more trace output.

The best way to filter is to use the pid. But it's difficult to get the pid of perf without using hacks.

Add a PERF_PID meta variable to the perf filter that contains the current pid.

Quoted 8 lines

With this patch the following works

% perf record -e syscalls:sys_enter_write -a --filter 'common_pid != PERF_PID' ...

This won't work for more complex perf pipe lines with multiple processes, but at least solves the problem nicely for a single perf.

I like the idea, so with comments below fixed:

Thanks,
Namhyung

[SNIP]

tools/perf/util/parse-events.c int parse_filter(const struct option *opt, const char *str, · line 991

Quoted 5 lines
tools/perf/util/parse-events.c +5 -0
+	/* Assume a pid has not more than 8 characters */
+	pid = strstr(last->filter, "PERF_PID");
+	if (pid) {
+		char buf[9];
+		snprintf(buf, 9, "%08d", getpid());

Why do you add zero-paddings? I guess it'd confuse the parser as if it's an octal digits. What about just using "%8d"? It seems the parser ignores whitespaces between tokens..

tools/perf/util/parse-events.c int parse_filter(const struct option *opt, const char *str, · line 994

Quoted 3 lines
tools/perf/util/parse-events.c +3 -0
+		memcpy(pid, buf, 8);
+	}
+	fprintf(stderr, "filter |%s|\n", last->filter);

Isn't it a debug message?

Thanks,
Namhyung

Quoted 2 lines

return 0;
}