Skip to contentSkip to conversation

perf data: Allow to use stdio functions for pipe mode

PATCH Superseded Namhyung Kim 3 participants 5 messages Oct 28, 2020 – Oct 30, 2020 perf events

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Namhyung Kim

author maintainer
#0 lore raw

PATCH 1/1 perf data: Allow to use stdio functions for pipe mode

When perf data is in a pipe, it reads each event separately using read(2) syscall. This is a huge performance bottleneck when processing large data like in perf inject. Also perf inject needs to use write(2) syscall for the output.

So convert it to use buffer I/O functions in stdio library for pipe data. This makes inject-build-id bench time drops from 20ms to 8ms.

  $ perf bench internals inject-build-id
  # Running 'internals/inject-build-id' benchmark:
    Average build-id injection took: 8.074 msec (+- 0.013 msec)
    Average time per event: 0.792 usec (+- 0.001 usec)
    Average memory usage: 8328 KB (+- 0 KB)
    Average build-id-all injection took: 5.490 msec (+- 0.008 msec)
    Average time per event: 0.538 usec (+- 0.001 usec)
    Average memory usage: 7563 KB (+- 0 KB)

This patch enables it just for perf inject when used with pipe (it's a default behavior). Maybe we could do it for perf record and/or report later..

---
 tools/perf/builtin-inject.c |  2 ++
 tools/perf/util/data.c      | 36 +++++++++++++++++++++++++++++++++---
 tools/perf/util/data.h      | 11 ++++++++++-
 tools/perf/util/header.c    |  8 ++++----
 tools/perf/util/session.c   |  7 ++++---
 5 files changed, 53 insertions(+), 11 deletions(-)
tools/perf/builtin-inject.c +2 -0
index 452a75fe68e5..14d6c88fed76 100644
@@ -853,10 +853,12 @@ int cmd_inject(int argc, const char **argv)
 		.output = {
 			.path = "-",
 			.mode = PERF_DATA_MODE_WRITE,
+			.use_stdio = true,
 		},
 	};
 	struct perf_data data = {
 		.mode = PERF_DATA_MODE_READ,
+		.use_stdio = true,
 	};
 	int ret;
 
tools/perf/util/data.c +33 -3
index c47aa34fdc0a..47b5a4b50ca5 100644
@@ -174,8 +174,16 @@ static bool check_pipe(struct perf_data *data)
 			is_pipe = true;
 	}
 
-	if (is_pipe)
-		data->file.fd = fd;
+	if (is_pipe) {
+		if (data->use_stdio) {
+			const char *mode;
+
+			mode = perf_data__is_read(data) ? "r" : "w";
+			data->file.fptr = fdopen(fd, mode);
+		} else {
+			data->file.fd = fd;
+		}
+	}
 
 	return data->is_pipe = is_pipe;
 }
@@ -334,6 +342,9 @@ int perf_data__open(struct perf_data *data)
 	if (check_pipe(data))
 		return 0;
 
+	/* currently it allows stdio for pipe only */
+	data->use_stdio = false;
+
 	if (!data->path)
 		data->path = "perf.data";
 
@@ -353,7 +364,21 @@ void perf_data__close(struct perf_data *data)
 		perf_data__close_dir(data);
 
 	zfree(&data->file.path);
-	close(data->file.fd);
+
+	if (data->use_stdio)
+		fclose(data->file.fptr);
+	else
+		close(data->file.fd);
+}
+
+ssize_t perf_data__read(struct perf_data *data, void *buf, size_t size)
+{
+	if (data->use_stdio) {
+		if (fread(buf, size, 1, data->file.fptr) == 1)
+			return size;
+		return feof(data->file.fptr) ? 0 : -1;
+	}
+	return readn(data->file.fd, buf, size);
 }
 
 ssize_t perf_data_file__write(struct perf_data_file *file,
@@ -365,6 +390,11 @@ ssize_t perf_data_file__write(struct perf_data_file *file,
 ssize_t perf_data__write(struct perf_data *data,
 			      void *buf, size_t size)
 {
+	if (data->use_stdio) {
+		if (fwrite(buf, size, 1, data->file.fptr) == 1)
+			return size;
+		return -1;
+	}
 	return perf_data_file__write(&data->file, buf, size);
 }
 
tools/perf/util/data.h +10 -1
index 75947ef6bc17..c563fcbb0288 100644
@@ -2,6 +2,7 @@
 #ifndef __PERF_DATA_H
 #define __PERF_DATA_H
 
+#include <stdio.h>
 #include <stdbool.h>
 
 enum perf_data_mode {
@@ -16,7 +17,10 @@ enum perf_dir_version {
 
 struct perf_data_file {
 	char		*path;
-	int		 fd;
+	union {
+		int	 fd;
+		FILE	*fptr;
+	};
 	unsigned long	 size;
 };
 
@@ -26,6 +30,7 @@ struct perf_data {
 	bool			 is_pipe;
 	bool			 is_dir;
 	bool			 force;
+	bool			 use_stdio;
 	enum perf_data_mode	 mode;
 
 	struct {
@@ -62,11 +67,15 @@ static inline bool perf_data__is_single_file(struct perf_data *data)
 
 static inline int perf_data__fd(struct perf_data *data)
 {
+	if (data->use_stdio)
+		return fileno(data->file.fptr);
+
 	return data->file.fd;
 }
 
 int perf_data__open(struct perf_data *data);
 void perf_data__close(struct perf_data *data);
+ssize_t perf_data__read(struct perf_data *data, void *buf, size_t size);
 ssize_t perf_data__write(struct perf_data *data,
 			      void *buf, size_t size);
 ssize_t perf_data_file__write(struct perf_data_file *file,
tools/perf/util/header.c +4 -4
index be850e9f8852..d9a70154426b 100644
@@ -3652,7 +3652,8 @@ static int perf_file_section__process(struct perf_file_section *section,
 }
 
 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
-				       struct perf_header *ph, int fd,
+				       struct perf_header *ph,
+				       struct perf_data* data,
 				       bool repipe)
 {
 	struct feat_fd ff = {
@@ -3661,7 +3662,7 @@ static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
 	};
 	ssize_t ret;
 
-	ret = readn(fd, header, sizeof(*header));
+	ret = perf_data__read(data, header, sizeof(*header));
 	if (ret <= 0)
 		return -1;
 
@@ -3684,8 +3685,7 @@ static int perf_header__read_pipe(struct perf_session *session)
 	struct perf_header *header = &session->header;
 	struct perf_pipe_file_header f_header;
 
-	if (perf_file_header__read_pipe(&f_header, header,
-					perf_data__fd(session->data),
+	if (perf_file_header__read_pipe(&f_header, header, session->data,
 					session->repipe) < 0) {
 		pr_debug("incompatible file format\n");
 		return -EINVAL;
tools/perf/util/session.c +4 -3
index 7a5f03764702..f901a09a58e4 100644
@@ -1923,7 +1923,6 @@ static int __perf_session__process_pipe_events(struct perf_session *session)
 {
 	struct ordered_events *oe = &session->ordered_events;
 	struct perf_tool *tool = session->tool;
-	int fd = perf_data__fd(session->data);
 	union perf_event *event;
 	uint32_t size, cur_size = 0;
 	void *buf = NULL;
@@ -1943,7 +1942,8 @@ static int __perf_session__process_pipe_events(struct perf_session *session)
 	ordered_events__set_copy_on_queue(oe, true);
 more:
 	event = buf;
-	err = readn(fd, event, sizeof(struct perf_event_header));
+	err = perf_data__read(session->data, event,
+			      sizeof(struct perf_event_header));
 	if (err <= 0) {
 		if (err == 0)
 			goto done;
@@ -1975,7 +1975,8 @@ static int __perf_session__process_pipe_events(struct perf_session *session)
 	p += sizeof(struct perf_event_header);
 
 	if (size - sizeof(struct perf_event_header)) {
-		err = readn(fd, p, size - sizeof(struct perf_event_header));
+		err = perf_data__read(session->data, p,
+				      size - sizeof(struct perf_event_header));
 		if (err <= 0) {
 			if (err == 0) {
 				pr_err("unexpected end of event stream\n");
Signature · 1 line
2.29.0.rc2.309.g374f81d7ae-goog
reviewer
#1 lore raw
Quoted 8 lines · Namhyung Kim

When perf data is in a pipe, it reads each event separately using read(2) syscall. This is a huge performance bottleneck when processing large data like in perf inject. Also perf inject needs to use write(2) syscall for the output.

So convert it to use buffer I/O functions in stdio library for pipe data. This makes inject-build-id bench time drops from 20ms to 8ms.

This is great! Reading the numbers below the times are reduced to 67.9% of their previous, this is a little less than 20ms to 8ms so perhaps check that number.

Quoted 9 lines
  $ perf bench internals inject-build-id
  # Running 'internals/inject-build-id' benchmark:
    Average build-id injection took: 8.074 msec (+- 0.013 msec)
    Average time per event: 0.792 usec (+- 0.001 usec)
    Average memory usage: 8328 KB (+- 0 KB)
    Average build-id-all injection took: 5.490 msec (+- 0.008 msec)
    Average time per event: 0.538 usec (+- 0.001 usec)
    Average memory usage: 7563 KB (+- 0 KB)
Quoted 3 lines

This patch enables it just for perf inject when used with pipe (it's a default behavior). Maybe we could do it for perf record and/or report later..

For perf record there is also the async IO used for writing. I like that this patch is adding the reading to perf_data. Should the async IO code live in perf_data rather than record? Should async IO be used for reading in that case? What would be the policy of using stdio, not-stdio and async IO?

Thanks,
Ian

Quoted 210 lines
---
 tools/perf/builtin-inject.c |  2 ++
 tools/perf/util/data.c      | 36 +++++++++++++++++++++++++++++++++---
 tools/perf/util/data.h      | 11 ++++++++++-
 tools/perf/util/header.c    |  8 ++++----
 tools/perf/util/session.c   |  7 ++++---
 5 files changed, 53 insertions(+), 11 deletions(-)
tools/perf/builtin-inject.c +2 -0
index 452a75fe68e5..14d6c88fed76 100644
@@ -853,10 +853,12 @@ int cmd_inject(int argc, const char **argv)
                .output = {
                        .path = "-",
                        .mode = PERF_DATA_MODE_WRITE,
+                       .use_stdio = true,
                },
        };
        struct perf_data data = {
                .mode = PERF_DATA_MODE_READ,
+               .use_stdio = true,
        };
        int ret;
 
tools/perf/util/data.c +33 -3
index c47aa34fdc0a..47b5a4b50ca5 100644
@@ -174,8 +174,16 @@ static bool check_pipe(struct perf_data *data)
                        is_pipe = true;
        }
 
-       if (is_pipe)
-               data->file.fd = fd;
+       if (is_pipe) {
+               if (data->use_stdio) {
+                       const char *mode;
+
+                       mode = perf_data__is_read(data) ? "r" : "w";
+                       data->file.fptr = fdopen(fd, mode);
+               } else {
+                       data->file.fd = fd;
+               }
+       }
 
        return data->is_pipe = is_pipe;
 }
@@ -334,6 +342,9 @@ int perf_data__open(struct perf_data *data)
        if (check_pipe(data))
                return 0;
 
+       /* currently it allows stdio for pipe only */
+       data->use_stdio = false;
+
        if (!data->path)
                data->path = "perf.data";
 
@@ -353,7 +364,21 @@ void perf_data__close(struct perf_data *data)
                perf_data__close_dir(data);
 
        zfree(&data->file.path);
-       close(data->file.fd);
+
+       if (data->use_stdio)
+               fclose(data->file.fptr);
+       else
+               close(data->file.fd);
+}
+
+ssize_t perf_data__read(struct perf_data *data, void *buf, size_t size)
+{
+       if (data->use_stdio) {
+               if (fread(buf, size, 1, data->file.fptr) == 1)
+                       return size;
+               return feof(data->file.fptr) ? 0 : -1;
+       }
+       return readn(data->file.fd, buf, size);
 }
 
 ssize_t perf_data_file__write(struct perf_data_file *file,
@@ -365,6 +390,11 @@ ssize_t perf_data_file__write(struct perf_data_file *file,
 ssize_t perf_data__write(struct perf_data *data,
                              void *buf, size_t size)
 {
+       if (data->use_stdio) {
+               if (fwrite(buf, size, 1, data->file.fptr) == 1)
+                       return size;
+               return -1;
+       }
        return perf_data_file__write(&data->file, buf, size);
 }
 
tools/perf/util/data.h +10 -1
index 75947ef6bc17..c563fcbb0288 100644
@@ -2,6 +2,7 @@
 #ifndef __PERF_DATA_H
 #define __PERF_DATA_H
 
+#include <stdio.h>
 #include <stdbool.h>
 
 enum perf_data_mode {
@@ -16,7 +17,10 @@ enum perf_dir_version {
 
 struct perf_data_file {
        char            *path;
-       int              fd;
+       union {
+               int      fd;
+               FILE    *fptr;
+       };
        unsigned long    size;
 };
 
@@ -26,6 +30,7 @@ struct perf_data {
        bool                     is_pipe;
        bool                     is_dir;
        bool                     force;
+       bool                     use_stdio;
        enum perf_data_mode      mode;
 
        struct {
@@ -62,11 +67,15 @@ static inline bool perf_data__is_single_file(struct perf_data *data)
 
 static inline int perf_data__fd(struct perf_data *data)
 {
+       if (data->use_stdio)
+               return fileno(data->file.fptr);
+
        return data->file.fd;
 }
 
 int perf_data__open(struct perf_data *data);
 void perf_data__close(struct perf_data *data);
+ssize_t perf_data__read(struct perf_data *data, void *buf, size_t size);
 ssize_t perf_data__write(struct perf_data *data,
                              void *buf, size_t size);
 ssize_t perf_data_file__write(struct perf_data_file *file,
tools/perf/util/header.c +4 -4
index be850e9f8852..d9a70154426b 100644
@@ -3652,7 +3652,8 @@ static int perf_file_section__process(struct perf_file_section *section,
 }
 
 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
-                                      struct perf_header *ph, int fd,
+                                      struct perf_header *ph,
+                                      struct perf_data* data,
                                       bool repipe)
 {
        struct feat_fd ff = {
@@ -3661,7 +3662,7 @@ static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
        };
        ssize_t ret;
 
-       ret = readn(fd, header, sizeof(*header));
+       ret = perf_data__read(data, header, sizeof(*header));
        if (ret <= 0)
                return -1;
 
@@ -3684,8 +3685,7 @@ static int perf_header__read_pipe(struct perf_session *session)
        struct perf_header *header = &session->header;
        struct perf_pipe_file_header f_header;
 
-       if (perf_file_header__read_pipe(&f_header, header,
-                                       perf_data__fd(session->data),
+       if (perf_file_header__read_pipe(&f_header, header, session->data,
                                        session->repipe) < 0) {
                pr_debug("incompatible file format\n");
                return -EINVAL;
tools/perf/util/session.c +4 -4
index 7a5f03764702..f901a09a58e4 100644
@@ -1923,7 +1923,6 @@ static int __perf_session__process_pipe_events(struct perf_session *session)
 {
        struct ordered_events *oe = &session->ordered_events;
        struct perf_tool *tool = session->tool;
-       int fd = perf_data__fd(session->data);
        union perf_event *event;
        uint32_t size, cur_size = 0;
        void *buf = NULL;
@@ -1943,7 +1942,8 @@ static int __perf_session__process_pipe_events(struct perf_session *session)
        ordered_events__set_copy_on_queue(oe, true);
 more:
        event = buf;
-       err = readn(fd, event, sizeof(struct perf_event_header));
+       err = perf_data__read(session->data, event,
+                             sizeof(struct perf_event_header));
        if (err <= 0) {
                if (err == 0)
                        goto done;
@@ -1975,7 +1975,8 @@ static int __perf_session__process_pipe_events(struct perf_session *session)
        p += sizeof(struct perf_event_header);
 
        if (size - sizeof(struct perf_event_header)) {
-               err = readn(fd, p, size - sizeof(struct perf_event_header));
+               err = perf_data__read(session->data, p,
+                                     size - sizeof(struct perf_event_header));
                if (err <= 0) {
                        if (err == 0) {
                                pr_err("unexpected end of event stream\n");
--

2.29.0.rc2.309.g374f81d7ae-goog

author maintainer
#2 lore raw

Hi Ian,

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

When perf data is in a pipe, it reads each event separately using read(2) syscall. This is a huge performance bottleneck when processing large data like in perf inject. Also perf inject needs to use write(2) syscall for the output.

Quoted 6 lines
Quoted 2 lines

So convert it to use buffer I/O functions in stdio library for pipe data. This makes inject-build-id bench time drops from 20ms to 8ms.

This is great! Reading the numbers below the times are reduced to 67.9% of their previous, this is a little less than 20ms to 8ms so perhaps check that number.

The numbers before this patch:

$ ./perf bench internals inject-build-id
# Running 'internals/inject-build-id' benchmark:
  Average build-id injection took: 20.594 msec (+- 0.034 msec)
  Average time per event: 2.019 usec (+- 0.003 usec)
  Average memory usage: 8319 KB (+- 0 KB)
  Average build-id-all injection took: 19.443 msec (+- 0.177 msec)
  Average time per event: 1.906 usec (+- 0.017 usec)
  Average memory usage: 7490 KB (+- 0 KB)
Quoted 14 lines
Quoted 10 lines
  $ perf bench internals inject-build-id
  # Running 'internals/inject-build-id' benchmark:
    Average build-id injection took: 8.074 msec (+- 0.013 msec)
    Average time per event: 0.792 usec (+- 0.001 usec)
    Average memory usage: 8328 KB (+- 0 KB)
    Average build-id-all injection took: 5.490 msec (+- 0.008 msec)
    Average time per event: 0.538 usec (+- 0.001 usec)
    Average memory usage: 7563 KB (+- 0 KB)
Quoted 3 lines

This patch enables it just for perf inject when used with pipe (it's a default behavior). Maybe we could do it for perf record and/or report later..

Quoted 5 lines

For perf record there is also the async IO used for writing. I like that this patch is adding the reading to perf_data. Should the async IO code live in perf_data rather than record? Should async IO be used for reading in that case? What would be the policy of using stdio, not-stdio and async IO?

I'm not sure but using async IO for read is meaningless since we cannot do anything without the data. The reason I added this was that the pipe mode does so many small I/O with syscalls. But perf record will mostly do large I/O so I don't expect big speedup for that.

Thanks
Namhyung

#3 lore raw
In reply to Namhyung Kim
Quoted 53 lines · Namhyung Kim

When perf data is in a pipe, it reads each event separately using read(2) syscall. This is a huge performance bottleneck when processing large data like in perf inject. Also perf inject needs to use write(2) syscall for the output.

So convert it to use buffer I/O functions in stdio library for pipe data. This makes inject-build-id bench time drops from 20ms to 8ms.

  $ perf bench internals inject-build-id
  # Running 'internals/inject-build-id' benchmark:
    Average build-id injection took: 8.074 msec (+- 0.013 msec)
    Average time per event: 0.792 usec (+- 0.001 usec)
    Average memory usage: 8328 KB (+- 0 KB)
    Average build-id-all injection took: 5.490 msec (+- 0.008 msec)
    Average time per event: 0.538 usec (+- 0.001 usec)
    Average memory usage: 7563 KB (+- 0 KB)

This patch enables it just for perf inject when used with pipe (it's a default behavior). Maybe we could do it for perf record and/or report later..

---
 tools/perf/builtin-inject.c |  2 ++
 tools/perf/util/data.c      | 36 +++++++++++++++++++++++++++++++++---
 tools/perf/util/data.h      | 11 ++++++++++-
 tools/perf/util/header.c    |  8 ++++----
 tools/perf/util/session.c   |  7 ++++---
 5 files changed, 53 insertions(+), 11 deletions(-)
tools/perf/builtin-inject.c +2 -0
index 452a75fe68e5..14d6c88fed76 100644
@@ -853,10 +853,12 @@ int cmd_inject(int argc, const char **argv)
 		.output = {
 			.path = "-",
 			.mode = PERF_DATA_MODE_WRITE,
+			.use_stdio = true,
 		},
 	};
 	struct perf_data data = {
 		.mode = PERF_DATA_MODE_READ,
+		.use_stdio = true,
 	};
 	int ret;
 
tools/perf/util/data.c +0 -0
index c47aa34fdc0a..47b5a4b50ca5 100644
@@ -174,8 +174,16 @@ static bool check_pipe(struct perf_data *data)
 			is_pipe = true;

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

Quoted 10 lines
tools/perf/util/data.c +6 -2
 	}
 
-	if (is_pipe)
-		data->file.fd = fd;
+	if (is_pipe) {
+		if (data->use_stdio) {
+			const char *mode;
+
+			mode = perf_data__is_read(data) ? "r" : "w";
+			data->file.fptr = fdopen(fd, mode);

I guess fdopen should never fail right? but I think we should add BUG_ON(data->file.fptr == NULL) or something

other than this the change looks good, I can see the speedup in bench as well

jirka

author maintainer
#4 lore raw

Hi Jiri,

Quoted 57 lines · Jiri Olsa
Quoted 55 lines · Namhyung Kim

When perf data is in a pipe, it reads each event separately using read(2) syscall. This is a huge performance bottleneck when processing large data like in perf inject. Also perf inject needs to use write(2) syscall for the output.

So convert it to use buffer I/O functions in stdio library for pipe data. This makes inject-build-id bench time drops from 20ms to 8ms.

  $ perf bench internals inject-build-id
  # Running 'internals/inject-build-id' benchmark:
    Average build-id injection took: 8.074 msec (+- 0.013 msec)
    Average time per event: 0.792 usec (+- 0.001 usec)
    Average memory usage: 8328 KB (+- 0 KB)
    Average build-id-all injection took: 5.490 msec (+- 0.008 msec)
    Average time per event: 0.538 usec (+- 0.001 usec)
    Average memory usage: 7563 KB (+- 0 KB)

This patch enables it just for perf inject when used with pipe (it's a default behavior). Maybe we could do it for perf record and/or report later..

---
 tools/perf/builtin-inject.c |  2 ++
 tools/perf/util/data.c      | 36 +++++++++++++++++++++++++++++++++---
 tools/perf/util/data.h      | 11 ++++++++++-
 tools/perf/util/header.c    |  8 ++++----
 tools/perf/util/session.c   |  7 ++++---
 5 files changed, 53 insertions(+), 11 deletions(-)
tools/perf/builtin-inject.c +2 -0
index 452a75fe68e5..14d6c88fed76 100644
@@ -853,10 +853,12 @@ int cmd_inject(int argc, const char **argv)
              .output = {
                      .path = "-",
                      .mode = PERF_DATA_MODE_WRITE,
+                     .use_stdio = true,
              },
      };
      struct perf_data data = {
              .mode = PERF_DATA_MODE_READ,
+             .use_stdio = true,
      };
      int ret;
 
tools/perf/util/data.c +0 -0
index c47aa34fdc0a..47b5a4b50ca5 100644
@@ -174,8 +174,16 @@ static bool check_pipe(struct perf_data *data)
                      is_pipe = true;
      }
 
Quoted 11 lines

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

Quoted 8 lines
tools/perf/util/data.c +6 -2
-     if (is_pipe)
-             data->file.fd = fd;
+     if (is_pipe) {
+             if (data->use_stdio) {
+                     const char *mode;
+
+                     mode = perf_data__is_read(data) ? "r" : "w";
+                     data->file.fptr = fdopen(fd, mode);

I guess fdopen should never fail right? but I think we should add BUG_ON(data->file.fptr == NULL) or something

The man page says it may fail when mode is invalid or malloc failed internally. Will add the check.

Quoted 3 lines

other than this the change looks good, I can see the speedup in bench as well

Thanks!
Namhyung