Skip to contentSkip to conversation

perf: mmap output file - v4

PATCH In review David Ahern 5 participants 11 messages Nov 8, 2013 – Nov 12, 2013 perf events

lore
Share & tools
lore
#0 lore raw

cover perf: mmap output file - v4

Version 4. I think I have addressed all of Ingo's comments plus a few extra refactorings.

Ingo: updated responses to your comment:

1. out-pages and rounding up to power of 2
- leveraging the same code used for mmap-pages. A follow on patch will do that for both mmap use cases.

2. what happens if a user sets out-pages to zero
- the parsing function does not allow it

David Ahern (2):
perf record: Move existing write_output into helper function perf record: mmap output file - v4

 tools/perf/Documentation/perf-record.txt |   5 +
 tools/perf/builtin-record.c              | 162 ++++++++++++++++++++++++++++++-
 2 files changed, 166 insertions(+), 1 deletion(-)
Signature · 1 line
1.8.3.4 (Apple Git-47)
#3 lore raw

PATCH 2/2 perf record: mmap output file - v4

When recording raw_syscalls for the entire system, e.g.,
perf record -e raw_syscalls:*,sched:sched_switch -a -- sleep 1

you end up with a negative feedback loop as perf itself calls write() fairly often. This patch handles the problem by mmap'ing the file in chunks of 64M at a time and copies events from the event buffers to the file avoiding write system calls.

Before (with write syscall):

perf record -o /tmp/perf.data -e raw_syscalls:*,sched:sched_switch -a -- sleep 1 [ perf record: Woken up 0 times to write data ]
[ perf record: Captured and wrote 81.843 MB /tmp/perf.data (~3575786 samples) ]

After (using mmap):

perf record -o /tmp/perf.data -e raw_syscalls:*,sched:sched_switch -a -- sleep 1 [ perf record: Woken up 31 times to write data ]
[ perf record: Captured and wrote 8.203 MB /tmp/perf.data (~358388 samples) ]

In addition to perf-trace benefits using mmap lowers the overhead of perf-record. For example,

perf stat -i -- perf record -g -o /tmp/perf.data openssl speed aes

shows a drop in time, CPU cycles, and instructions all drop by more than a factor of 3. Jiri also ran a test that showed a big improvement.

v4: Refactoring per Ingo's comments

v3: Removed use of bytes_at_mmap_start at the stat() that set it
Added user option to control the size of the mmap for writing file.

v2: Removed msync call before munmap per Jiri's suggestion

Cc Ingo Molnar <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Mike Galbraith <[email protected]>
Cc Stephane Eranian <[email protected]>
---
 tools/perf/Documentation/perf-record.txt |   5 +
 tools/perf/builtin-record.c              | 155 +++++++++++++++++++++++++++++++
 2 files changed, 160 insertions(+)
tools/perf/Documentation/perf-record.txt +5 -0
index 052f7c4dc00c..af11c2dd2360 100644
@@ -201,6 +201,11 @@ abort events and some memory events in precise mode on modern Intel CPUs.
 --transaction::
 Record transaction flags for transaction related events.
 
+--out-pages=::
+Number of pages to mmap for writing data to file or size specification
+with appended unit character - B/K/M/G. The size is rounded up to have nearest
+pages power of two value.  Default size is 64M.
+
 SEE ALSO
 --------
 linkperf:perf-stat[1], linkperf:perf-list[1]
tools/perf/builtin-record.c +155 -0

209 lines not on this page · raw message

Signature · 1 line
1.8.3.4 (Apple Git-47)
#1 lore raw

PATCH 1/2 perf record: Move existing write_output into helper function

In reply to David Ahern

Code move only; no logic changes. In preparation for the mmap based output option in the next patch.

Cc Ingo Molnar <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Mike Galbraith <[email protected]>
Cc Stephane Eranian <[email protected]>
---
 tools/perf/builtin-record.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
tools/perf/builtin-record.c +6 -1
index 15280b5e5574..6e6a41856c41 100644
@@ -76,7 +76,7 @@ struct perf_record {
 	long			samples;
 };
 
-static int write_output(struct perf_record *rec, void *buf, size_t size)
+static int do_write_output(struct perf_record *rec, void *buf, size_t size)
 {
 	struct perf_data_file *file = &rec->file;
 
@@ -97,6 +97,11 @@ static int write_output(struct perf_record *rec, void *buf, size_t size)
 	return 0;
 }
 
+static int write_output(struct perf_record *rec, void *buf, size_t size)
+{
+	return do_write_output(rec, buf, size);
+}
+
 static int process_synthesized_event(struct perf_tool *tool,
 				     union perf_event *event,
 				     struct perf_sample *sample __maybe_unused,
Signature · 1 line
1.8.3.4 (Apple Git-47)
#4 lore raw

Re: [PATCH 2/2] perf record: mmap output file - v4

In reply to David Ahern

On Thu, Nov 07, 2013 at 09:23:25PM -0700, David Ahern wrote:

SNIP

Quoted 9 lines
tools/perf/builtin-record.c +8 -0
+static void mmap_output_fini(struct perf_record *rec)
+{
+	off_t len;
+	int fd;
+
+	if (!rec->mmap.use)
+		return;
+

tools/perf/builtin-record.c static void perf_record__init_features(struct perf_record *rec) · line 486

Quoted 10 lines
tools/perf/builtin-record.c +10 -0
+	rec->mmap.use = false;
+
+	len = rec->session->header.data_offset + rec->bytes_written;
+	fd = rec->file.fd;
+
+	munmap(rec->mmap.addr, rec->mmap.out_size);
+	rec->mmap.addr = NULL;
+
+	if (ftruncate(fd, len) != 0)
+		pr_err("ftruncate failed\n");

I think we should fail here and dont let the finishing code run on probably corrupted file.

the code that process build IDs could even get stuck

jirka

#5 lore raw

Re: [PATCH 2/2] perf record: mmap output file - v4

Quoted 7 lines · Jiri Olsa

Review

Quoted 2 lines
(quoted diff) +2 -0
+	if (ftruncate(fd, len) != 0)
+		pr_err("ftruncate failed\n");

I think we should fail here and dont let the finishing code run on probably corrupted file.

the code that process build IDs could even get stuck

Yes, I'll fix that in v5.

It also got me to thinking about failure scenarios -- like out of space. I was looking at this path using a size-limited tmpfs (max 1M) and writing perf.data into it.

Today (without this mmap output page) if the write() fails due to lack of space then perf emits the message:
failed to write perf data, error: No space left on device

and stops — killing the workload too. Trying to read and process the perf.data file fails with a SIGBUS error. I just sent a patch this addresses that problem.

Using that patch as a start point and moving to mmap-based output if the filesystem runs out of space the memcpy generates a SIGBUS and we need to jump out of the memcpy. The attached fixes that problem.

Any objections to using a sigjmp? Other option is to die in the signal handler. That option is harder to clean up.

David

tools/perf/builtin-record.c +16 -6
index 947970e182a5..b50f384d9a36 100644
@@ -29,9 +29,11 @@
 #include <unistd.h>
 #include <sched.h>
 #include <sys/mman.h>
+#include <setjmp.h>
 
 /* output file mmap'ed N chunks at a time */
 #define MMAP_OUTPUT_SIZE   (64*1024*1024)
+sigjmp_buf mmap_jmp;
 
 #ifndef HAVE_ON_EXIT_SUPPORT
 #ifndef ATEXIT_MAX
@@ -141,6 +143,7 @@ static int do_mmap_output(struct perf_record *rec, void *buf, size_t size)
 {
 	u64 remaining;
 	off_t offset;
+	volatile size_t total_len = 0;
 
 	if (rec->mmap.addr == NULL) {
 next_segment:
@@ -157,20 +160,23 @@ next_segment:
 	 * space write what we can then go back and create the
 	 * next segment
 	 */
-	if (size > remaining) {
-		memcpy(rec->mmap.addr + rec->mmap.offset, buf, remaining);
+	if (setjmp(mmap_jmp) != 0) {
+		pr_err("mmap copy failed.\n");
+		return -1;
+	}
+	if (size-total_len > remaining) {
+		memcpy(rec->mmap.addr + rec->mmap.offset, buf+total_len, remaining);
 		rec->bytes_written += remaining;
 
-		size -= remaining;
-		buf  += remaining;
+		total_len += remaining;
 
 		munmap(rec->mmap.addr, rec->mmap.out_size);
 		goto next_segment;
 	}
 
 	/* more data to copy and it fits in the current segment */
-	if (size) {
-		memcpy(rec->mmap.addr + rec->mmap.offset, buf, size);
+	if (size - total_len) {
+		memcpy(rec->mmap.addr + rec->mmap.offset, buf+total_len, size-total_len);
 		rec->bytes_written += size;
 		rec->mmap.offset += size;
 	}
@@ -272,6 +278,9 @@ static void sig_handler(int sig)
 	if (sig == SIGCHLD)
 		child_finished = 1;
 
+	if (sig == SIGBUS)
+		longjmp(mmap_jmp, 1);
+
 	done = 1;
 	signr = sig;
 }
@@ -526,6 +535,7 @@ static int __cmd_record(struct perf_record *rec, int argc, const char **argv)
 	signal(SIGINT, sig_handler);
 	signal(SIGUSR1, sig_handler);
 	signal(SIGTERM, sig_handler);
+	signal(SIGBUS, sig_handler);
 
 	session = perf_session__new(file, false, NULL);
 	if (session == NULL) {
Acked-by on v1 2/2
#6 lore raw

Re: [PATCH 2/2] perf record: mmap output file - v4

In reply to David Ahern
Quoted 43 lines · David Ahern

When recording raw_syscalls for the entire system, e.g.,
perf record -e raw_syscalls:*,sched:sched_switch -a -- sleep 1

you end up with a negative feedback loop as perf itself calls write() fairly often. This patch handles the problem by mmap'ing the file in chunks of 64M at a time and copies events from the event buffers to the file avoiding write system calls.

Before (with write syscall):

perf record -o /tmp/perf.data -e raw_syscalls:*,sched:sched_switch -a -- sleep 1 [ perf record: Woken up 0 times to write data ]
[ perf record: Captured and wrote 81.843 MB /tmp/perf.data (~3575786 samples) ]

After (using mmap):

perf record -o /tmp/perf.data -e raw_syscalls:*,sched:sched_switch -a -- sleep 1 [ perf record: Woken up 31 times to write data ]
[ perf record: Captured and wrote 8.203 MB /tmp/perf.data (~358388 samples) ]

In addition to perf-trace benefits using mmap lowers the overhead of perf-record. For example,

perf stat -i -- perf record -g -o /tmp/perf.data openssl speed aes

shows a drop in time, CPU cycles, and instructions all drop by more than a factor of 3. Jiri also ran a test that showed a big improvement.

v4: Refactoring per Ingo's comments

v3: Removed use of bytes_at_mmap_start at the stat() that set it
Added user option to control the size of the mmap for writing file.

v2: Removed msync call before munmap per Jiri's suggestion

Cc Ingo Molnar <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Mike Galbraith <[email protected]>
Cc Stephane Eranian <[email protected]>
Quoted 4 lines
---
 tools/perf/Documentation/perf-record.txt |   5 +
 tools/perf/builtin-record.c              | 155 +++++++++++++++++++++++++++++++
 2 files changed, 160 insertions(+)

Looks very clean now!

Thanks,

Ingo

#7 lore raw

Re: [PATCH 2/2] perf record: mmap output file - v4

Em Mon, Nov 11, 2013 at 12:29:06PM +0100, Ingo Molnar escreveu:

Quoted 7 lines · David Ahern
Quoted 6 lines

shows a drop in time, CPU cycles, and instructions all drop by more than a factor of 3. Jiri also ran a test that showed a big improvement.

v4: Refactoring per Ingo's comments

Quoted 8 lines
Quoted 4 lines

v3: Removed use of bytes_at_mmap_start at the stat() that set it
Added user option to control the size of the mmap for writing file.

v2: Removed msync call before munmap per Jiri's suggestion

Looks very clean now!

Applied the prep patch, but waiting for v5 as stated by David when answering Jiri's concerns.

- Arnaldo

#8 lore raw

Re: [PATCH 2/2] perf record: mmap output file - v4

Quoted 7 lines · Arnaldo Carvalho de Melo
Quoted 4 lines

Looks very clean now!

Applied the prep patch, but waiting for v5 as stated by David when answering Jiri's concerns.

I'll re-send - giving some time for comments on the use of setjmp/longjmp to bounce out of memcpy and error exit when filesystem runs out of space.

David

#9 lore raw

Re: [PATCH 2/2] perf record: mmap output file - v4

Quoted 4 lines · David Ahern
Quoted 3 lines · Arnaldo Carvalho de Melo
Quoted 2 lines

Looks very clean now!

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Applied the prep patch, but waiting for v5 as stated by David when answering Jiri's concerns.

I'll re-send - giving some time for comments on the use of setjmp/longjmp to bounce out of memcpy and error exit when filesystem runs out of space.

Maybe that could be an add-on patch instead?

All the rest would already be an improvement, right?

Thanks,

Ingo

#10 lore raw

Re: [PATCH 2/2] perf record: mmap output file - v4

Quoted 9 lines · Ingo Molnar
Quoted 6 lines · David Ahern
Quoted 5 lines · Arnaldo Carvalho de Melo
Quoted 4 lines

Looks very clean now!

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Applied the prep patch, but waiting for v5 as stated by David when answering Jiri's concerns.

I'll re-send - giving some time for comments on the use of setjmp/longjmp to bounce out of memcpy and error exit when filesystem runs out of space.

Maybe that could be an add-on patch instead?

That's what Arnaldo and I were thinking.

Quoted 2 lines

All the rest would already be an improvement, right?

Yes. The longjmp handles the out-of-space failure condition trying to write the data to a file. With mmap writes you get a SIGBUS when the filesystem is full and you need to bail out of the memcpy.

David

#2 lore raw

[tip:perf/urgent] perf record: Move existing write_output into helper function

In reply to David Ahern
perf record: Move existing write_output into helper function
Commit-ID:  a9986fad6645b98d5bb3c2f83c22efb0761ca272
Gitweb:     http://git.kernel.org/tip/a9986fad6645b98d5bb3c2f83c22efb0761ca272
Author:     David Ahern <[email protected]>
AuthorDate: Thu, 7 Nov 2013 21:23:24 -0700
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Mon, 11 Nov 2013 15:56:40 -0300

perf record: Move existing write_output into helper function

Code move only; no logic changes. In preparation for the mmap based output option in the next patch.

Cc Frederic Weisbecker <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Mike Galbraith <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Stephane Eranian <[email protected]>
---
 tools/perf/builtin-record.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
tools/perf/builtin-record.c +6 -1
index 8f5af32..880227e 100644
@@ -76,7 +76,7 @@ struct perf_record {
 	long			samples;
 };
 
-static int write_output(struct perf_record *rec, void *buf, size_t size)
+static int do_write_output(struct perf_record *rec, void *buf, size_t size)
 {
 	struct perf_data_file *file = &rec->file;
 
@@ -97,6 +97,11 @@ static int write_output(struct perf_record *rec, void *buf, size_t size)
 	return 0;
 }
 
+static int write_output(struct perf_record *rec, void *buf, size_t size)
+{
+	return do_write_output(rec, buf, size);
+}
+
 static int process_synthesized_event(struct perf_tool *tool,
 				     union perf_event *event,
 				     struct perf_sample *sample __maybe_unused,