Skip to contentSkip to conversation

perf tools: Detect when pipe is passed as perf data

PATCH Superseded Jiri Olsa 3 participants 3 messages Dec 25, 2020 – Dec 29, 2020 perf events

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jiri Olsa

author reviewer
#0 lore raw

PATCH 1/1 perf tools: Detect when pipe is passed as perf data

Currently we allow pipe input/output only through '-' string being passed to '-o' or '-i' options, like:

  # mkfifo perf.pipe
  # perf record --no-buffering -e 'sched:sched_switch' -o - > perf.pipe &
  [1] 354406
  # cat perf.pipe | ./perf --no-pager script -i - | head -3
            perf 354406 [000] 168190.164921: sched:sched_switch: perf:354406..
     migration/0    12 [000] 168190.164928: sched:sched_switch: migration/0:..
            perf 354406 [001] 168190.164981: sched:sched_switch: perf:354406..
  ...

This patch detects if given path is pipe and set the perf data object accordingly, so it's possible now to do above with:

  # mkfifo perf.pipe
  # perf record --no-buffering -e 'sched:sched_switch' -o perf.pipe &
  [1] 360188
  # perf --no-pager script -i ./perf.pipe | head -3
            perf 354442 [000] 168275.464895: sched:sched_switch: perf:354442..
     migration/0    12 [000] 168275.464902: sched:sched_switch: migration/0:..
            perf 354442 [001] 168275.464953: sched:sched_switch: perf:354442..

It's of course possible to combine any of above ways.

---
 tools/perf/util/data.c | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)
tools/perf/util/data.c +21 -6
index f29af4fc3d09..767b6c903cf5 100644
@@ -159,7 +159,7 @@ int perf_data__update_dir(struct perf_data *data)
 	return 0;
 }
 
-static bool check_pipe(struct perf_data *data)
+static int check_pipe(struct perf_data *data)
 {
 	struct stat st;
 	bool is_pipe = false;
@@ -172,6 +172,15 @@ static bool check_pipe(struct perf_data *data)
 	} else {
 		if (!strcmp(data->path, "-"))
 			is_pipe = true;
+		else if (!stat(data->path, &st) && S_ISFIFO(st.st_mode)) {
+			int flags = perf_data__is_read(data) ?
+				    O_RDONLY : O_WRONLY|O_CREAT|O_TRUNC;
+
+			fd = open(data->path, flags);
+			if (fd < 0)
+				return -EINVAL;
+			is_pipe = true;
+		}
 	}
 
 	if (is_pipe) {
@@ -190,7 +199,8 @@ static bool check_pipe(struct perf_data *data)
 		}
 	}
 
-	return data->is_pipe = is_pipe;
+	data->is_pipe = is_pipe;
+	return 0;
 }
 
 static int check_backup(struct perf_data *data)
@@ -344,8 +354,11 @@ static int open_dir(struct perf_data *data)
 
 int perf_data__open(struct perf_data *data)
 {
-	if (check_pipe(data))
-		return 0;
+	int err;
+
+	err = check_pipe(data);
+	if (err || data->is_pipe)
+		return err;
 
 	/* currently it allows stdio for pipe only */
 	data->use_stdio = false;
@@ -410,8 +423,10 @@ int perf_data__switch(struct perf_data *data,
 {
 	int ret;
 
-	if (check_pipe(data))
-		return -EINVAL;
+	ret = check_pipe(data);
+	if (ret || data->is_pipe)
+		return ret;
+
 	if (perf_data__is_read(data))
 		return -EINVAL;
 
Signature · 1 line
2.26.2
maintainer
#1 lore raw
Quoted 45 lines · Jiri Olsa

Currently we allow pipe input/output only through '-' string being passed to '-o' or '-i' options, like:

  # mkfifo perf.pipe
  # perf record --no-buffering -e 'sched:sched_switch' -o - > perf.pipe &
  [1] 354406
  # cat perf.pipe | ./perf --no-pager script -i - | head -3
            perf 354406 [000] 168190.164921: sched:sched_switch: perf:354406..
     migration/0    12 [000] 168190.164928: sched:sched_switch: migration/0:..
            perf 354406 [001] 168190.164981: sched:sched_switch: perf:354406..
  ...

This patch detects if given path is pipe and set the perf data object accordingly, so it's possible now to do above with:

  # mkfifo perf.pipe
  # perf record --no-buffering -e 'sched:sched_switch' -o perf.pipe &
  [1] 360188
  # perf --no-pager script -i ./perf.pipe | head -3
            perf 354442 [000] 168275.464895: sched:sched_switch: perf:354442..
     migration/0    12 [000] 168275.464902: sched:sched_switch: migration/0:..
            perf 354442 [001] 168275.464953: sched:sched_switch: perf:354442..

It's of course possible to combine any of above ways.

---
 tools/perf/util/data.c | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)
tools/perf/util/data.c +1 -1
index f29af4fc3d09..767b6c903cf5 100644
@@ -159,7 +159,7 @@ int perf_data__update_dir(struct perf_data *data)
        return 0;
 }
 
-static bool check_pipe(struct perf_data *data)
+static int check_pipe(struct perf_data *data)
 {
        struct stat st;
        bool is_pipe = false;

tools/perf/util/data.c static bool check_pipe(struct perf_data *data) · line 177

Quoted 6 lines
tools/perf/util/data.c +3 -0
@@ -172,6 +172,15 @@ static bool check_pipe(struct perf_data *data)
        } else {
                if (!strcmp(data->path, "-"))
                        is_pipe = true;
+               else if (!stat(data->path, &st) && S_ISFIFO(st.st_mode)) {
+                       int flags = perf_data__is_read(data) ?
+                                   O_RDONLY : O_WRONLY|O_CREAT|O_TRUNC;

I don't think we need O_CREAT here (maybe O_TRUNC as well).

Thanks,
Namhyung

Quoted 17 lines
tools/perf/util/data.c +8 -1
+
+                       fd = open(data->path, flags);
+                       if (fd < 0)
+                               return -EINVAL;
+                       is_pipe = true;
+               }
        }
 
        if (is_pipe) {
@@ -190,7 +199,8 @@ static bool check_pipe(struct perf_data *data)
                }
        }
 
-       return data->is_pipe = is_pipe;
+       data->is_pipe = is_pipe;
+       return 0;
 }
#2 lore raw
Quoted 40 lines · Namhyung Kim
Quoted 39 lines · Jiri Olsa

Currently we allow pipe input/output only through '-' string being passed to '-o' or '-i' options, like:

  # mkfifo perf.pipe
  # perf record --no-buffering -e 'sched:sched_switch' -o - > perf.pipe &
  [1] 354406
  # cat perf.pipe | ./perf --no-pager script -i - | head -3
            perf 354406 [000] 168190.164921: sched:sched_switch: perf:354406..
     migration/0    12 [000] 168190.164928: sched:sched_switch: migration/0:..
            perf 354406 [001] 168190.164981: sched:sched_switch: perf:354406..
  ...

This patch detects if given path is pipe and set the perf data object accordingly, so it's possible now to do above with:

  # mkfifo perf.pipe
  # perf record --no-buffering -e 'sched:sched_switch' -o perf.pipe &
  [1] 360188
  # perf --no-pager script -i ./perf.pipe | head -3
            perf 354442 [000] 168275.464895: sched:sched_switch: perf:354442..
     migration/0    12 [000] 168275.464902: sched:sched_switch: migration/0:..
            perf 354442 [001] 168275.464953: sched:sched_switch: perf:354442..

It's of course possible to combine any of above ways.

---
 tools/perf/util/data.c | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)
tools/perf/util/data.c +0 -0
index f29af4fc3d09..767b6c903cf5 100644
@@ -159,7 +159,7 @@ int perf_data__update_dir(struct perf_data *data)
        return 0;
 }
 
Quoted 14 lines

tools/perf/util/data.c static bool check_pipe(struct perf_data *data) · line 177

Quoted 12 lines
tools/perf/util/data.c +4 -1
-static bool check_pipe(struct perf_data *data)
+static int check_pipe(struct perf_data *data)
 {
        struct stat st;
        bool is_pipe = false;
@@ -172,6 +172,15 @@ static bool check_pipe(struct perf_data *data)
        } else {
                if (!strcmp(data->path, "-"))
                        is_pipe = true;
+               else if (!stat(data->path, &st) && S_ISFIFO(st.st_mode)) {
+                       int flags = perf_data__is_read(data) ?
+                                   O_RDONLY : O_WRONLY|O_CREAT|O_TRUNC;

I don't think we need O_CREAT here (maybe O_TRUNC as well).

I copied that from bash opening write end like:
openat(AT_FDCWD, "perf.pipe", O_WRONLY|O_CREAT|O_TRUNC, 0666 <detached ...>

however I can't see any check on kernel side for that, so it can removed.. I'll send new version

thanks,
jirka