Skip to contentSkip to conversation

: perf: reduce data loss when profiling highly parallel CPU bound workloads

PATCH Superseded Alexey Budankov 2 participants 16 messages Oct 3, 2018 – Oct 5, 2018 perf events

v9 of 15 Superseded Latest is v15 →

lore
Share & tools
lore
#0 lore raw

v9 cover : perf: reduce data loss when profiling highly parallel CPU bound workloads

Currently in record mode the tool implements trace writing serially. The algorithm loops over mapped per-cpu data buffers and stores ready data chunks into a trace file using write() system call.

At some circumstances the kernel may lack free space in a buffer because the other buffer's half is not yet written to disk due to some other buffer's data writing by the tool at the moment.

Thus serial trace writing implementation may cause the kernel to loose profiling data and that is what observed when profiling highly parallel CPU bound workloads on machines with big number of cores.

Experiment with profiling matrix multiplication code executing 128 threads on Intel Xeon Phi (KNM) with 272 cores, like below, demonstrates data loss metrics value of 98%:

/usr/bin/time perf record -o /tmp/perf-ser.data -a -N -B -T -R -g \
    --call-graph dwarf,1024 --user-regs=IP,SP,BP --switch-events \
    -e cycles,instructions,ref-cycles,software/period=1,name=cs,config=0x3/Duk -- \
    matrix.gcc

Data loss metrics is the ratio lost_time/elapsed_time where lost_time is the sum of time intervals containing PERF_RECORD_LOST records and elapsed_time is the elapsed application run time under profiling.

Applying asynchronous trace streaming thru Posix AIO API [1] lowers data loss metrics value providing 2x improvement (from 98% to ~1%)

Asynchronous trace streaming currently limited to glibc linkage because there may be other libc libraries linked by Perf tool that currently lack Posix AIO API support [2], [3], [4]. NO_AIO define may be used to limit Perf tool binary to serial streaming only.

---
Alexey Budankov (3):
perf util: map data buffer for preserving collected data perf record: enable asynchronous trace writing perf record: extend trace writing to multi AIO

 tools/perf/Makefile.config  |   5 ++
 tools/perf/Makefile.perf    |   7 +-
 tools/perf/builtin-record.c | 197 +++++++++++++++++++++++++++++++++++++++++++-
 tools/perf/perf.h           |   3 +
 tools/perf/util/evlist.c    |   9 +-
 tools/perf/util/evlist.h    |   3 +-
 tools/perf/util/mmap.c      | 137 ++++++++++++++++++++++++++++++
 tools/perf/util/mmap.h      |  16 ++++
 8 files changed, 369 insertions(+), 8 deletions(-)

---
Changes in v9:
- enable AIO streaming only when --aio-cblocks option is specified explicitly
- enable AIO based implementation when linking with glibc only
- define NO_AIO to limit Perf binary to serial implementation
Changes in v8:
- run the whole thing thru checkpatch.pl and corrected found issues except lines longer than 80 symbols
- corrected comments alignment and formatting
- moved multi AIO implementation into 3rd patch in the series
- implemented explicit cblocks array allocation
- split AIO completion check into separate record__aio_complete()
- set nr_cblocks default to 1 and max allowed value to 4
Changes in v7:
- implemented handling record.aio setting from perfconfig file
Changes in v6:
- adjusted setting of priorities for cblocks;
- handled errno == EAGAIN case from aio_write() return;
Changes in v5:
- resolved livelock on perf record -e intel_pt// -- dd if=/dev/zero of=/dev/null count=100000
- data loss metrics decreased from 25% to 2x in trialed configuration;
- reshaped layout of data structures;
- implemented --aio option;
- avoided nanosleep() prior calling aio_suspend();
- switched to per-cpu aio multi buffer record__aio_sync();
- record_mmap_read_sync() now does global sync just before
switching trace file or collection stop;
Changes in v4:
- converted mmap()/munmap() to malloc()/free() for mmap->data buffer management
- converted void *bf to struct perf_mmap *md in signatures
- written comment in perf_mmap__push() just before perf_mmap__get();
- written comment in record__mmap_read_sync() on possible restarting
of aio_write() operation and releasing perf_mmap object after all;
- added perf_mmap__put() for the cases of failed aio_write();
Changes in v3:
- written comments about nanosleep(0.5ms) call prior aio_suspend()
to cope with intrusiveness of its implementation in glibc;
- written comments about rationale behind coping profiling data
into mmap->data buffer;
Changes in v2:
- converted zalloc() to calloc() for allocation of mmap_aio array,
- cleared typo and adjusted fallback branch code;

---

[1] http://man7.org/linux/man-pages/man7/aio.7.html
[2] https://android.googlesource.com/platform/bionic/+/master/docs/status.md
[3] https://www.uclibc.org/
[4] https://uclibc-ng.org/

#1 lore raw

PATCH v9 1/3 : perf util: map data buffer for preserving collected data

The map->data buffer is used to preserve map->base profiling data for writing to disk. AIO map->cblock is used to queue corresponding map->data buffer for asynchronous writing.

---
Changes in v9:
- implemented NO_AIO and HAVE_AIO_SUPPORT defines to cover cases of libc implementations without Posix AIO API support
Changes in v7:
- implemented handling record.aio setting from perfconfig file
Changes in v6:
- adjusted setting of priorities for cblocks;
Changes in v5:
- reshaped layout of data structures;
- implemented --aio option;
Changes in v4:
- converted mmap()/munmap() to malloc()/free() for mmap->data buffer management
Changes in v2:
- converted zalloc() to calloc() for allocation of mmap_aio array,
- cleared typo and adjusted fallback branch code;

---
 tools/perf/Makefile.config |  5 +++++
 tools/perf/Makefile.perf   |  7 ++++++-
 tools/perf/util/evlist.c   |  4 +++-
 tools/perf/util/mmap.c     | 31 +++++++++++++++++++++++++++++++
 tools/perf/util/mmap.h     | 11 +++++++++++
 5 files changed, 56 insertions(+), 2 deletions(-)
tools/perf/Makefile.config +5 -0
index f6d1a03c7523..2e90f4ce9214 100644
@@ -355,6 +355,11 @@ endif # NO_LIBELF
 
 ifeq ($(feature-glibc), 1)
   CFLAGS += -DHAVE_GLIBC_SUPPORT
+  ifndef NO_AIO
+    ifndef BIONIC
+        CFLAGS += -DHAVE_AIO_SUPPORT
+    endif
+  endif
 endif
 
 ifdef NO_DWARF
tools/perf/Makefile.perf +6 -1
index 92514fb3689f..7becc6a72cf2 100644
@@ -97,8 +97,13 @@ include ../scripts/utilities.mak
 # Define LIBCLANGLLVM if you DO want builtin clang and llvm support.
 # When selected, pass LLVM_CONFIG=/path/to/llvm-config to `make' if
 # llvm-config is not in $PATH.
-
+#
 # Define NO_CORESIGHT if you do not want support for CoreSight trace decoding.
+#
+# Define NO_AIO if you do not want support of Posix AIO based trace
+# streaming for record mode. Currently Posix AIO trace streaming is
+# supported only when linking with glibc.
+#
 
 # As per kernel Makefile, avoid funny character set dependencies
 unexport LC_ALL
tools/perf/util/evlist.c +3 -1
index be440df29615..af2f8c965d7a 100644
@@ -1029,7 +1029,9 @@ int perf_evlist__mmap_ex(struct perf_evlist *evlist, unsigned int pages,
 	 * So &mp should not be passed through const pointer.
 	 */
 	struct mmap_params mp;
-
+#ifdef HAVE_AIO_SUPPORT
+	mp.nr_cblocks = 0;
+#endif
 	if (!evlist->mmap)
 		evlist->mmap = perf_evlist__alloc_mmap(evlist, false);
 	if (!evlist->mmap)
tools/perf/util/mmap.c +31 -0
index cdb95b3a1213..5b3727b0c7c0 100644
@@ -155,6 +155,10 @@ void __weak auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp __mayb
 
 void perf_mmap__munmap(struct perf_mmap *map)
 {
+#ifdef HAVE_AIO_SUPPORT
+	if (map->data)
+		zfree(&map->data);
+#endif
 	if (map->base != NULL) {
 		munmap(map->base, perf_mmap__mmap_len(map));
 		map->base = NULL;
@@ -166,6 +170,9 @@ void perf_mmap__munmap(struct perf_mmap *map)
 
 int perf_mmap__mmap(struct perf_mmap *map, struct mmap_params *mp, int fd, int cpu)
 {
+#ifdef HAVE_AIO_SUPPORT
+	int delta_max;
+#endif
 	/*
 	 * The last one will be done at perf_mmap__consume(), so that we
 	 * make sure we don't prevent tools from consuming every last event in
@@ -190,6 +197,30 @@ int perf_mmap__mmap(struct perf_mmap *map, struct mmap_params *mp, int fd, int c
 		map->base = NULL;
 		return -1;
 	}
+#ifdef HAVE_AIO_SUPPORT
+	map->nr_cblocks = mp->nr_cblocks;
+	if (map->nr_cblocks) {
+		map->data = malloc(perf_mmap__mmap_len(map));
+		if (!map->data) {
+			pr_debug2("failed to allocate data buffer, error %d\n",
+					errno);
+			return -1;
+		}
+		/*
+		 * Use cblock.aio_fildes value different from -1
+		 * to denote started aio write operation on the
+		 * cblock so it requires explicit record__aio_sync()
+		 * call prior the cblock may be reused again.
+		 */
+		map->cblock.aio_fildes = -1;
+		/*
+		 * Allocate cblock with max priority delta to
+		 * have faster aio write system calls.
+		 */
+		delta_max = sysconf(_SC_AIO_PRIO_DELTA_MAX);
+		map->cblock.aio_reqprio = delta_max;
+	}
+#endif
 	map->fd = fd;
 	map->cpu = cpu;
 
tools/perf/util/mmap.h +11 -0
index e603314dc792..1b63b6cc7cf9 100644
@@ -6,6 +6,9 @@
 #include <linux/types.h>
 #include <asm/barrier.h>
 #include <stdbool.h>
+#ifdef HAVE_AIO_SUPPORT
+#include <aio.h>
+#endif
 #include "auxtrace.h"
 #include "event.h"
 
@@ -26,6 +29,11 @@ struct perf_mmap {
 	bool		 overwrite;
 	struct auxtrace_mmap auxtrace_mmap;
 	char		 event_copy[PERF_SAMPLE_MAX_SIZE] __aligned(8);
+#ifdef HAVE_AIO_SUPPORT
+	void 		 *data;
+	struct aiocb	 cblock;
+	int		 nr_cblocks;
+#endif
 };
 
 /*
@@ -59,6 +67,9 @@ enum bkw_mmap_state {
 struct mmap_params {
 	int			    prot, mask;
 	struct auxtrace_mmap_params auxtrace_mp;
+#ifdef HAVE_AIO_SUPPORT
+	int			    nr_cblocks;
+#endif
 };
 
 int perf_mmap__mmap(struct perf_mmap *map, struct mmap_params *mp, int fd, int cpu);
maintainer
#2 lore raw

Re: [PATCH v9 1/3]: perf util: map data buffer for preserving collected data

Quoted 106 lines · Alexey Budankov

The map->data buffer is used to preserve map->base profiling data for writing to disk. AIO map->cblock is used to queue corresponding map->data buffer for asynchronous writing.

---
Changes in v9:
- implemented NO_AIO and HAVE_AIO_SUPPORT defines to cover cases of libc implementations without Posix AIO API support
Changes in v7:
- implemented handling record.aio setting from perfconfig file
Changes in v6:
- adjusted setting of priorities for cblocks;
Changes in v5:
- reshaped layout of data structures;
- implemented --aio option;
Changes in v4:
- converted mmap()/munmap() to malloc()/free() for mmap->data buffer management
Changes in v2:
- converted zalloc() to calloc() for allocation of mmap_aio array,
- cleared typo and adjusted fallback branch code;

---
 tools/perf/Makefile.config |  5 +++++
 tools/perf/Makefile.perf   |  7 ++++++-
 tools/perf/util/evlist.c   |  4 +++-
 tools/perf/util/mmap.c     | 31 +++++++++++++++++++++++++++++++
 tools/perf/util/mmap.h     | 11 +++++++++++
 5 files changed, 56 insertions(+), 2 deletions(-)
tools/perf/Makefile.config +5 -0
index f6d1a03c7523..2e90f4ce9214 100644
@@ -355,6 +355,11 @@ endif # NO_LIBELF
 
 ifeq ($(feature-glibc), 1)
   CFLAGS += -DHAVE_GLIBC_SUPPORT
+  ifndef NO_AIO
+    ifndef BIONIC
+        CFLAGS += -DHAVE_AIO_SUPPORT
+    endif
+  endif
 endif
 
 ifdef NO_DWARF
tools/perf/Makefile.perf +6 -1
index 92514fb3689f..7becc6a72cf2 100644
@@ -97,8 +97,13 @@ include ../scripts/utilities.mak
 # Define LIBCLANGLLVM if you DO want builtin clang and llvm support.
 # When selected, pass LLVM_CONFIG=/path/to/llvm-config to `make' if
 # llvm-config is not in $PATH.
-
+#
 # Define NO_CORESIGHT if you do not want support for CoreSight trace decoding.
+#
+# Define NO_AIO if you do not want support of Posix AIO based trace
+# streaming for record mode. Currently Posix AIO trace streaming is
+# supported only when linking with glibc.
+#
 
 # As per kernel Makefile, avoid funny character set dependencies
 unexport LC_ALL
tools/perf/util/evlist.c +3 -1
index be440df29615..af2f8c965d7a 100644
@@ -1029,7 +1029,9 @@ int perf_evlist__mmap_ex(struct perf_evlist *evlist, unsigned int pages,
 	 * So &mp should not be passed through const pointer.
 	 */
 	struct mmap_params mp;
-
+#ifdef HAVE_AIO_SUPPORT
+	mp.nr_cblocks = 0;
+#endif
 	if (!evlist->mmap)
 		evlist->mmap = perf_evlist__alloc_mmap(evlist, false);
 	if (!evlist->mmap)
tools/perf/util/mmap.c +7 -0
index cdb95b3a1213..5b3727b0c7c0 100644
@@ -155,6 +155,10 @@ void __weak auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp __mayb
 
 void perf_mmap__munmap(struct perf_mmap *map)
 {
+#ifdef HAVE_AIO_SUPPORT
+	if (map->data)
+		zfree(&map->data);
+#endif
 	if (map->base != NULL) {
 		munmap(map->base, perf_mmap__mmap_len(map));
 		map->base = NULL;
@@ -166,6 +170,9 @@ void perf_mmap__munmap(struct perf_mmap *map)
 
 int perf_mmap__mmap(struct perf_mmap *map, struct mmap_params *mp, int fd, int cpu)
 {
+#ifdef HAVE_AIO_SUPPORT
+	int delta_max;
+#endif
 	/*
 	 * The last one will be done at perf_mmap__consume(), so that we
 	 * make sure we don't prevent tools from consuming every last event in

tools/perf/util/mmap.c int perf_mmap__mmap(struct perf_mmap *map, struct mmap_params *mp, int fd, int c · line 206

Quoted 10 lines
tools/perf/util/mmap.c +7 -0
@@ -190,6 +197,30 @@ int perf_mmap__mmap(struct perf_mmap *map, struct mmap_params *mp, int fd, int c
 		map->base = NULL;
 		return -1;
 	}
+#ifdef HAVE_AIO_SUPPORT
+	map->nr_cblocks = mp->nr_cblocks;
+	if (map->nr_cblocks) {
+		map->data = malloc(perf_mmap__mmap_len(map));
+		if (!map->data) {
+			pr_debug2("failed to allocate data buffer, error %d\n",
+					errno);

I guess the error code is not very useful than the message. But in this case it's probably ENOMEM so we can simply omit it.

Quoted 7 lines
tools/perf/util/mmap.c +7 -0
+			return -1;
+		}
+		/*
+		 * Use cblock.aio_fildes value different from -1
+		 * to denote started aio write operation on the
+		 * cblock so it requires explicit record__aio_sync()
+		 * call prior the cblock may be reused again.

tools/perf/util/mmap.c int perf_mmap__mmap(struct perf_mmap *map, struct mmap_params *mp, int fd, int c · line 223

Quoted 10 lines
tools/perf/util/mmap.c +10 -0
+		 */
+		map->cblock.aio_fildes = -1;
+		/*
+		 * Allocate cblock with max priority delta to
+		 * have faster aio write system calls.
+		 */
+		delta_max = sysconf(_SC_AIO_PRIO_DELTA_MAX);
+		map->cblock.aio_reqprio = delta_max;
+	}
+#endif

What about separating this part into a function with the #ifdef. Providing a dummy function when not defined would make the code cleaner IMHO. You may consider adding util/aio.[ch].

Thanks,
Namhyung

Quoted 39 lines
tools/perf/util/mmap.c +0 -0
 	map->fd = fd;
 	map->cpu = cpu;
 
tools/perf/util/mmap.h +11 -0
index e603314dc792..1b63b6cc7cf9 100644
@@ -6,6 +6,9 @@
 #include <linux/types.h>
 #include <asm/barrier.h>
 #include <stdbool.h>
+#ifdef HAVE_AIO_SUPPORT
+#include <aio.h>
+#endif
 #include "auxtrace.h"
 #include "event.h"
 
@@ -26,6 +29,11 @@ struct perf_mmap {
 	bool		 overwrite;
 	struct auxtrace_mmap auxtrace_mmap;
 	char		 event_copy[PERF_SAMPLE_MAX_SIZE] __aligned(8);
+#ifdef HAVE_AIO_SUPPORT
+	void 		 *data;
+	struct aiocb	 cblock;
+	int		 nr_cblocks;
+#endif
 };
 
 /*
@@ -59,6 +67,9 @@ enum bkw_mmap_state {
 struct mmap_params {
 	int			    prot, mask;
 	struct auxtrace_mmap_params auxtrace_mp;
+#ifdef HAVE_AIO_SUPPORT
+	int			    nr_cblocks;
+#endif
 };
 
 int perf_mmap__mmap(struct perf_mmap *map, struct mmap_params *mp, int fd, int cpu);
#3 lore raw

Re: [PATCH v9 1/3]: perf util: map data buffer for preserving collected data

Hi,

Quoted 133 lines · Namhyung Kim
Quoted 106 lines · Alexey Budankov

The map->data buffer is used to preserve map->base profiling data for writing to disk. AIO map->cblock is used to queue corresponding map->data buffer for asynchronous writing.

---
Changes in v9:
- implemented NO_AIO and HAVE_AIO_SUPPORT defines to cover cases of libc implementations without Posix AIO API support
Changes in v7:
- implemented handling record.aio setting from perfconfig file
Changes in v6:
- adjusted setting of priorities for cblocks;
Changes in v5:
- reshaped layout of data structures;
- implemented --aio option;
Changes in v4:
- converted mmap()/munmap() to malloc()/free() for mmap->data buffer management
Changes in v2:
- converted zalloc() to calloc() for allocation of mmap_aio array,
- cleared typo and adjusted fallback branch code;

---
 tools/perf/Makefile.config |  5 +++++
 tools/perf/Makefile.perf   |  7 ++++++-
 tools/perf/util/evlist.c   |  4 +++-
 tools/perf/util/mmap.c     | 31 +++++++++++++++++++++++++++++++
 tools/perf/util/mmap.h     | 11 +++++++++++
 5 files changed, 56 insertions(+), 2 deletions(-)
tools/perf/Makefile.config +5 -0
index f6d1a03c7523..2e90f4ce9214 100644
@@ -355,6 +355,11 @@ endif # NO_LIBELF
 
 ifeq ($(feature-glibc), 1)
   CFLAGS += -DHAVE_GLIBC_SUPPORT
+  ifndef NO_AIO
+    ifndef BIONIC
+        CFLAGS += -DHAVE_AIO_SUPPORT
+    endif
+  endif
 endif
 
 ifdef NO_DWARF
tools/perf/Makefile.perf +6 -1
index 92514fb3689f..7becc6a72cf2 100644
@@ -97,8 +97,13 @@ include ../scripts/utilities.mak
 # Define LIBCLANGLLVM if you DO want builtin clang and llvm support.
 # When selected, pass LLVM_CONFIG=/path/to/llvm-config to `make' if
 # llvm-config is not in $PATH.
-
+#
 # Define NO_CORESIGHT if you do not want support for CoreSight trace decoding.
+#
+# Define NO_AIO if you do not want support of Posix AIO based trace
+# streaming for record mode. Currently Posix AIO trace streaming is
+# supported only when linking with glibc.
+#
 
 # As per kernel Makefile, avoid funny character set dependencies
 unexport LC_ALL
tools/perf/util/evlist.c +3 -1
index be440df29615..af2f8c965d7a 100644
@@ -1029,7 +1029,9 @@ int perf_evlist__mmap_ex(struct perf_evlist *evlist, unsigned int pages,
 	 * So &mp should not be passed through const pointer.
 	 */
 	struct mmap_params mp;
-
+#ifdef HAVE_AIO_SUPPORT
+	mp.nr_cblocks = 0;
+#endif
 	if (!evlist->mmap)
 		evlist->mmap = perf_evlist__alloc_mmap(evlist, false);
 	if (!evlist->mmap)
tools/perf/util/mmap.c +7 -0
index cdb95b3a1213..5b3727b0c7c0 100644
@@ -155,6 +155,10 @@ void __weak auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp __mayb
 
 void perf_mmap__munmap(struct perf_mmap *map)
 {
+#ifdef HAVE_AIO_SUPPORT
+	if (map->data)
+		zfree(&map->data);
+#endif
 	if (map->base != NULL) {
 		munmap(map->base, perf_mmap__mmap_len(map));
 		map->base = NULL;
@@ -166,6 +170,9 @@ void perf_mmap__munmap(struct perf_mmap *map)
 
 int perf_mmap__mmap(struct perf_mmap *map, struct mmap_params *mp, int fd, int cpu)
 {
+#ifdef HAVE_AIO_SUPPORT
+	int delta_max;
+#endif
 	/*
 	 * The last one will be done at perf_mmap__consume(), so that we
 	 * make sure we don't prevent tools from consuming every last event in

tools/perf/util/mmap.c int perf_mmap__mmap(struct perf_mmap *map, struct mmap_params *mp, int fd, int c · line 206

Quoted 10 lines
tools/perf/util/mmap.c +7 -0
@@ -190,6 +197,30 @@ int perf_mmap__mmap(struct perf_mmap *map, struct mmap_params *mp, int fd, int c
 		map->base = NULL;
 		return -1;
 	}
+#ifdef HAVE_AIO_SUPPORT
+	map->nr_cblocks = mp->nr_cblocks;
+	if (map->nr_cblocks) {
+		map->data = malloc(perf_mmap__mmap_len(map));
+		if (!map->data) {
+			pr_debug2("failed to allocate data buffer, error %d\n",
+					errno);

I guess the error code is not very useful than the message. But in this case it's probably ENOMEM so we can simply omit it.

Quoted 11 lines
tools/perf/util/mmap.c +11 -0
+			return -1;
+		}
+		/*
+		 * Use cblock.aio_fildes value different from -1
+		 * to denote started aio write operation on the
+		 * cblock so it requires explicit record__aio_sync()
+		 * call prior the cblock may be reused again.
+		 */
+		map->cblock.aio_fildes = -1;
+		/*
+		 * Allocate cblock with max priority delta to
Quoted 8 lines

tools/perf/util/mmap.c int perf_mmap__mmap(struct perf_mmap *map, struct mmap_params *mp, int fd, int c · line 223

Quoted 6 lines
tools/perf/util/mmap.c +6 -0
+		 * have faster aio write system calls.
+		 */
+		delta_max = sysconf(_SC_AIO_PRIO_DELTA_MAX);
+		map->cblock.aio_reqprio = delta_max;
+	}
+#endif

What about separating this part into a function with the #ifdef.

That's possible e.g. like this:

static int perf_mmap__aio_mmap(struct perf_mmap *map, struct mmap_params *mp); static void perf_mmap__aio_munmap(struct perf_mmap *map);

Thanks,
Alexey

#4 lore raw

Re: [PATCH v9 1/3]: perf util: map data buffer for preserving collected data

In reply to Namhyung Kim

Hi,

On 05.10.2018 9:23, Namhyung Kim wrote:
<SNIP>

Quoted 1 line · Alexey Budankov

<SNIP>

Quoted 10 lines

tools/perf/util/mmap.c int perf_mmap__mmap(struct perf_mmap *map, struct mmap_params *mp, int fd, int c · line 206

Quoted 7 lines
tools/perf/util/mmap.c +7 -0
+#ifdef HAVE_AIO_SUPPORT
+	map->nr_cblocks = mp->nr_cblocks;
+	if (map->nr_cblocks) {
+		map->data = malloc(perf_mmap__mmap_len(map));
+		if (!map->data) {
+			pr_debug2("failed to allocate data buffer, error %d\n",
+					errno);

I guess the error code is not very useful than the message. But in this case it's probably ENOMEM so we can simply omit it.

Possibly like this:

			pr_debug2("failed to allocate data buffer, error %d: %s\n",
					errno, strerror(errno));

would be more informative.

Thanks,
Alexey

#5 lore raw

PATCH v9 2/3 : perf record: enable asynchronous trace writing

In reply to Alexey Budankov

Trace file offset is calculated and updated linearly prior enqueuing aio write at record__pushfn().

record__aio_sync() blocks till completion of started AIO operation and then proceeds.

record__mmap_read_sync() implements a barrier for all incomplete aio write requests.

---
Changes in v9:
- enable AIO streaming only when --aio-cblocks option is specified explicitly Changes in v8:
- split AIO completion check into separate record__aio_complete()
Changes in v6:
- handled errno == EAGAIN case from aio_write();
Changes in v5:
- data loss metrics decreased from 25% to 2x in trialed configuration;
- avoided nanosleep() prior calling aio_suspend();
- switched to per cpu multi record__aio_sync() aio
- record_mmap_read_sync() now does global barrier just before
switching trace file or collection stop;
- resolved livelock on perf record -e intel_pt// -- dd if=/dev/zero of=/dev/null count=100000 Changes in v4:
- converted void *bf to struct perf_mmap *md in signatures
- written comment in perf_mmap__push() just before perf_mmap__get();
- written comment in record__mmap_read_sync() on possible restarting
of aio_write() operation and releasing perf_mmap object after all;
- added perf_mmap__put() for the cases of failed aio_write();
Changes in v3:
- written comments about nanosleep(0.5ms) call prior aio_suspend()
to cope with intrusiveness of its implementation in glibc;
- written comments about rationale behind coping profiling data
into mmap->data buffer;

---
 tools/perf/builtin-record.c | 154 +++++++++++++++++++++++++++++++++++++++++++-
 tools/perf/perf.h           |   3 +
 tools/perf/util/mmap.c      |  73 +++++++++++++++++++++
 tools/perf/util/mmap.h      |   4 ++
 4 files changed, 233 insertions(+), 1 deletion(-)
tools/perf/builtin-record.c +153 -1
index 0980dfe3396b..c63fe8c021a2 100644
@@ -124,6 +124,118 @@ static int record__write(struct record *rec, struct perf_mmap *map __maybe_unuse
 	return 0;
 }
 
+#ifdef HAVE_AIO_SUPPORT
+static int record__aio_write(struct aiocb *cblock, int trace_fd,
+		void *buf, size_t size, off_t off)
+{
+	int rc;
+
+	cblock->aio_fildes = trace_fd;
+	cblock->aio_buf    = buf;
+	cblock->aio_nbytes = size;
+	cblock->aio_offset = off;
+	cblock->aio_sigevent.sigev_notify = SIGEV_NONE;
+
+	do {
+		rc = aio_write(cblock);
+		if (rc == 0) {
+			break;
+		} else if (errno != EAGAIN) {
+			cblock->aio_fildes = -1;
+			pr_err("failed to queue perf data, error: %m\n");
+			break;
+		}
+	} while (1);
+
+	return rc;
+}
+
+static int record__aio_complete(struct perf_mmap *md, struct aiocb *cblock)
+{
+	void *rem_buf;
+	off_t rem_off;
+	size_t rem_size;
+	int rc, aio_errno;
+	ssize_t aio_ret, written;
+
+	aio_errno = aio_error(cblock);
+	if (aio_errno == EINPROGRESS)
+		return 0;
+
+	written = aio_ret = aio_return(cblock);
+	if (aio_ret < 0) {
+		if (aio_errno != EINTR)
+			pr_err("failed to write perf data, error: %m\n");
+		written = 0;
+	}
+
+	rem_size = cblock->aio_nbytes - written;
+
+	if (rem_size == 0) {
+		cblock->aio_fildes = -1;
+		/*
+		 * md->refcount is incremented in perf_mmap__push() for
+		 * every enqueued aio write request so decrement it because
+		 * the request is now complete.
+		 */
+		perf_mmap__put(md);
+		rc = 1;
+	} else {
+		/*
+		 * aio write request may require restart with the
+		 * reminder if the kernel didn't write whole
+		 * chunk at once.
+		 */
+		rem_off = cblock->aio_offset + written;
+		rem_buf = (void *)(cblock->aio_buf + written);
+		record__aio_write(cblock, cblock->aio_fildes,
+				rem_buf, rem_size, rem_off);
+		rc = 0;
+	}
+
+	return rc;
+}
+
+static void record__aio_sync(struct perf_mmap *md)
+{
+	struct aiocb *cblock = &md->cblock;
+	struct timespec timeout = { 0, 1000 * 1000  * 1 }; // 1ms
+
+	do {
+		if (cblock->aio_fildes == -1 || record__aio_complete(md, cblock))
+			return;
+
+		while (aio_suspend((const struct aiocb**)&cblock, 1, &timeout)) {
+			if (!(errno == EAGAIN || errno == EINTR))
+				pr_err("failed to sync perf data, error: %m\n");
+		}
+	} while (1);
+}
+
+static int record__aio_pushfn(void *to, struct aiocb *cblock, void *bf, size_t size)
+{
+	off_t off;
+	struct record *rec = to;
+	int ret, trace_fd = rec->session->data->file.fd;
+
+	rec->samples++;
+
+	off = lseek(trace_fd, 0, SEEK_CUR);
+	lseek(trace_fd, off + size, SEEK_SET);
+	ret = record__aio_write(cblock, trace_fd, bf, size, off);
+	if (!ret) {
+		rec->bytes_written += size;
+
+		if (switch_output_size(rec))
+			trigger_hit(&switch_output_trigger);
+	} else {
+		lseek(trace_fd, off, SEEK_SET);
+	}
+
+	return ret;
+}
+#endif
+
 static int process_synthesized_event(struct perf_tool *tool,
 				     union perf_event *event,
 				     struct perf_sample *sample __maybe_unused,
@@ -136,7 +248,6 @@ static int process_synthesized_event(struct perf_tool *tool,
 static int record__pushfn(struct perf_mmap *map, void *to, void *bf, size_t size)
 {
 	struct record *rec = to;
-
 	rec->samples++;
 	return record__write(rec, map, bf, size);
 }
@@ -513,6 +624,24 @@ static struct perf_event_header finished_round_event = {
 	.type = PERF_RECORD_FINISHED_ROUND,
 };
 
+static void record__mmap_read_sync(struct record *rec __maybe_unused)
+{
+#ifdef HAVE_AIO_SUPPORT
+	int i;
+	struct perf_evlist *evlist = rec->evlist;
+	struct perf_mmap *maps = evlist->mmap;
+
+	if (!rec->opts.nr_cblocks)
+		return;
+
+	for (i = 0; i < evlist->nr_mmaps; i++) {
+		struct perf_mmap *map = &maps[i];
+		if (map->base)
+			record__aio_sync(map);
+	}
+#endif
+}
+
 static int record__mmap_read_evlist(struct record *rec, struct perf_evlist *evlist,
 				    bool overwrite)
 {
@@ -535,10 +664,29 @@ static int record__mmap_read_evlist(struct record *rec, struct perf_evlist *evli
 		struct perf_mmap *map = &maps[i];
 
 		if (map->base) {
+#ifndef HAVE_AIO_SUPPORT
 			if (perf_mmap__push(map, rec, record__pushfn) != 0) {
 				rc = -1;
 				goto out;
 			}
+#else
+			if (!rec->opts.nr_cblocks) {
+				if (perf_mmap__push(map, rec, record__pushfn) != 0) {
+					rc = -1;
+					goto out;
+				}
+			} else {
+				/*
+				 * Call record__aio_sync() to wait till map->data buffer
+				 * becomes available after previous aio write request.
+				 */
+				record__aio_sync(map);
+				if (perf_mmap__aio_push(map, rec, record__aio_pushfn) != 0) {
+					rc = -1;
+					goto out;
+				}
+			}
+#endif
 		}
 
 		if (map->auxtrace_mmap.base && !rec->opts.auxtrace_snapshot_mode &&
@@ -1057,6 +1205,7 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
 			perf_evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_DATA_PENDING);
 
 		if (record__mmap_read_all(rec) < 0) {
+			record__mmap_read_sync(rec);
 			trigger_error(&auxtrace_snapshot_trigger);
 			trigger_error(&switch_output_trigger);
 			err = -1;
@@ -1068,6 +1217,7 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
 			if (!trigger_is_error(&auxtrace_snapshot_trigger))
 				record__read_auxtrace_snapshot(rec);
 			if (trigger_is_error(&auxtrace_snapshot_trigger)) {
+				record__mmap_read_sync(rec);
 				pr_err("AUX area tracing snapshot failed\n");
 				err = -1;
 				goto out_child;
@@ -1086,6 +1236,7 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
 			 */
 			if (rec->evlist->bkw_mmap_state == BKW_MMAP_RUNNING)
 				continue;
+			record__mmap_read_sync(rec);
 			trigger_ready(&switch_output_trigger);
 
 			/*
@@ -1139,6 +1290,7 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
 			disabled = true;
 		}
 	}
+	record__mmap_read_sync(rec);
 	trigger_off(&auxtrace_snapshot_trigger);
 	trigger_off(&switch_output_trigger);
 
tools/perf/perf.h +3 -0
index 21bf7f5a3cf5..ef700d8bb610 100644
@@ -82,6 +82,9 @@ struct record_opts {
 	bool         use_clockid;
 	clockid_t    clockid;
 	unsigned int proc_map_timeout;
+#ifdef HAVE_AIO_SUPPORT
+	int	     nr_cblocks;
+#endif
 };
 
 struct option;
tools/perf/util/mmap.c +73 -0

79 lines not on this page · raw message

tools/perf/util/mmap.h +4 -0
index 1b63b6cc7cf9..ac011777c38f 100644
@@ -105,6 +105,10 @@ union perf_event *perf_mmap__read_event(struct perf_mmap *map);
 
 int perf_mmap__push(struct perf_mmap *md, void *to,
 		    int push(struct perf_mmap *map, void *to, void *buf, size_t size));
+#ifdef HAVE_AIO_SUPPORT
+int perf_mmap__aio_push(struct perf_mmap *md, void *to,
+			int push(void *to, struct aiocb *cblock, void *buf, size_t size));
+#endif
 
 size_t perf_mmap__mmap_len(struct perf_mmap *map);
maintainer
#6 lore raw

Re: [PATCH v9 2/3]: perf record: enable asynchronous trace writing

Quoted 125 lines · Alexey Budankov

Trace file offset is calculated and updated linearly prior enqueuing aio write at record__pushfn().

record__aio_sync() blocks till completion of started AIO operation and then proceeds.

record__mmap_read_sync() implements a barrier for all incomplete aio write requests.

---
Changes in v9:
- enable AIO streaming only when --aio-cblocks option is specified explicitly Changes in v8:
- split AIO completion check into separate record__aio_complete()
Changes in v6:
- handled errno == EAGAIN case from aio_write();
Changes in v5:
- data loss metrics decreased from 25% to 2x in trialed configuration;
- avoided nanosleep() prior calling aio_suspend();
- switched to per cpu multi record__aio_sync() aio
- record_mmap_read_sync() now does global barrier just before
switching trace file or collection stop;
- resolved livelock on perf record -e intel_pt// -- dd if=/dev/zero of=/dev/null count=100000 Changes in v4:
- converted void *bf to struct perf_mmap *md in signatures
- written comment in perf_mmap__push() just before perf_mmap__get();
- written comment in record__mmap_read_sync() on possible restarting
of aio_write() operation and releasing perf_mmap object after all;
- added perf_mmap__put() for the cases of failed aio_write();
Changes in v3:
- written comments about nanosleep(0.5ms) call prior aio_suspend()
to cope with intrusiveness of its implementation in glibc;
- written comments about rationale behind coping profiling data
into mmap->data buffer;

---
 tools/perf/builtin-record.c | 154 +++++++++++++++++++++++++++++++++++++++++++-
 tools/perf/perf.h           |   3 +
 tools/perf/util/mmap.c      |  73 +++++++++++++++++++++
 tools/perf/util/mmap.h      |   4 ++
 4 files changed, 233 insertions(+), 1 deletion(-)
tools/perf/builtin-record.c +74 -0
index 0980dfe3396b..c63fe8c021a2 100644
@@ -124,6 +124,118 @@ static int record__write(struct record *rec, struct perf_mmap *map __maybe_unuse
 	return 0;
 }
 
+#ifdef HAVE_AIO_SUPPORT
+static int record__aio_write(struct aiocb *cblock, int trace_fd,
+		void *buf, size_t size, off_t off)
+{
+	int rc;
+
+	cblock->aio_fildes = trace_fd;
+	cblock->aio_buf    = buf;
+	cblock->aio_nbytes = size;
+	cblock->aio_offset = off;
+	cblock->aio_sigevent.sigev_notify = SIGEV_NONE;
+
+	do {
+		rc = aio_write(cblock);
+		if (rc == 0) {
+			break;
+		} else if (errno != EAGAIN) {
+			cblock->aio_fildes = -1;
+			pr_err("failed to queue perf data, error: %m\n");
+			break;
+		}
+	} while (1);
+
+	return rc;
+}
+
+static int record__aio_complete(struct perf_mmap *md, struct aiocb *cblock)
+{
+	void *rem_buf;
+	off_t rem_off;
+	size_t rem_size;
+	int rc, aio_errno;
+	ssize_t aio_ret, written;
+
+	aio_errno = aio_error(cblock);
+	if (aio_errno == EINPROGRESS)
+		return 0;
+
+	written = aio_ret = aio_return(cblock);
+	if (aio_ret < 0) {
+		if (aio_errno != EINTR)
+			pr_err("failed to write perf data, error: %m\n");
+		written = 0;
+	}
+
+	rem_size = cblock->aio_nbytes - written;
+
+	if (rem_size == 0) {
+		cblock->aio_fildes = -1;
+		/*
+		 * md->refcount is incremented in perf_mmap__push() for
+		 * every enqueued aio write request so decrement it because
+		 * the request is now complete.
+		 */
+		perf_mmap__put(md);
+		rc = 1;
+	} else {
+		/*
+		 * aio write request may require restart with the
+		 * reminder if the kernel didn't write whole
+		 * chunk at once.
+		 */
+		rem_off = cblock->aio_offset + written;
+		rem_buf = (void *)(cblock->aio_buf + written);
+		record__aio_write(cblock, cblock->aio_fildes,
+				rem_buf, rem_size, rem_off);
+		rc = 0;
+	}
+
+	return rc;
+}
+
+static void record__aio_sync(struct perf_mmap *md)
+{

tools/perf/builtin-record.c static int record__write(struct record *rec, struct perf_mmap *map __maybe_unuse · line 210

Quoted 10 lines
tools/perf/builtin-record.c +10 -0
+	struct aiocb *cblock = &md->cblock;
+	struct timespec timeout = { 0, 1000 * 1000  * 1 }; // 1ms
+
+	do {
+		if (cblock->aio_fildes == -1 || record__aio_complete(md, cblock))
+			return;
+
+		while (aio_suspend((const struct aiocb**)&cblock, 1, &timeout)) {
+			if (!(errno == EAGAIN || errno == EINTR))
+				pr_err("failed to sync perf data, error: %m\n");

Is there somthing we can do in this error case? Any chance it gets stuck in the loop?

Quoted 4 lines
tools/perf/builtin-record.c +4 -0
+		}
+	} while (1);
+}
+

tools/perf/builtin-record.c static int record__write(struct record *rec, struct perf_mmap *map __maybe_unuse · line 224

Quoted 10 lines
tools/perf/builtin-record.c +10 -0
+static int record__aio_pushfn(void *to, struct aiocb *cblock, void *bf, size_t size)
+{
+	off_t off;
+	struct record *rec = to;
+	int ret, trace_fd = rec->session->data->file.fd;
+
+	rec->samples++;
+
+	off = lseek(trace_fd, 0, SEEK_CUR);
+	lseek(trace_fd, off + size, SEEK_SET);

It'd be nice if these lseek() could be removed and use rec->bytes_written instead.

tools/perf/builtin-record.c static int record__write(struct record *rec, struct perf_mmap *map __maybe_unuse · line 230

Quoted 6 lines
tools/perf/builtin-record.c +6 -0
+	ret = record__aio_write(cblock, trace_fd, bf, size, off);
+	if (!ret) {
+		rec->bytes_written += size;
+
+		if (switch_output_size(rec))
+			trigger_hit(&switch_output_trigger);

Doesn't it need the _sync() before the trigger? Maybe it should be moved to record__mmap_read_evlist() or so..

Quoted 61 lines
tools/perf/builtin-record.c +35 -1
+	} else {
+		lseek(trace_fd, off, SEEK_SET);
+	}
+
+	return ret;
+}
+#endif
+
 static int process_synthesized_event(struct perf_tool *tool,
 				     union perf_event *event,
 				     struct perf_sample *sample __maybe_unused,
@@ -136,7 +248,6 @@ static int process_synthesized_event(struct perf_tool *tool,
 static int record__pushfn(struct perf_mmap *map, void *to, void *bf, size_t size)
 {
 	struct record *rec = to;
-
 	rec->samples++;
 	return record__write(rec, map, bf, size);
 }
@@ -513,6 +624,24 @@ static struct perf_event_header finished_round_event = {
 	.type = PERF_RECORD_FINISHED_ROUND,
 };
 
+static void record__mmap_read_sync(struct record *rec __maybe_unused)
+{
+#ifdef HAVE_AIO_SUPPORT
+	int i;
+	struct perf_evlist *evlist = rec->evlist;
+	struct perf_mmap *maps = evlist->mmap;
+
+	if (!rec->opts.nr_cblocks)
+		return;
+
+	for (i = 0; i < evlist->nr_mmaps; i++) {
+		struct perf_mmap *map = &maps[i];
+		if (map->base)
+			record__aio_sync(map);
+	}
+#endif
+}
+
 static int record__mmap_read_evlist(struct record *rec, struct perf_evlist *evlist,
 				    bool overwrite)
 {
@@ -535,10 +664,29 @@ static int record__mmap_read_evlist(struct record *rec, struct perf_evlist *evli
 		struct perf_mmap *map = &maps[i];
 
 		if (map->base) {
+#ifndef HAVE_AIO_SUPPORT
 			if (perf_mmap__push(map, rec, record__pushfn) != 0) {
 				rc = -1;
 				goto out;
 			}
+#else
+			if (!rec->opts.nr_cblocks) {
+				if (perf_mmap__push(map, rec, record__pushfn) != 0) {
+					rc = -1;
+					goto out;
+				}
+			} else {
+				/*

tools/perf/builtin-record.c static int record__mmap_read_evlist(struct record *rec, struct perf_evlist *evli · line 689

Quoted 10 lines
tools/perf/builtin-record.c +10 -0
+				 * Call record__aio_sync() to wait till map->data buffer
+				 * becomes available after previous aio write request.
+				 */
+				record__aio_sync(map);
+				if (perf_mmap__aio_push(map, rec, record__aio_pushfn) != 0) {
+					rc = -1;
+					goto out;
+				}
+			}
+#endif

If dummy aio functions are provided, the #ifdef can be removed and just use the #else part assuming opts.nr_cblocks == 0.

Thanks,
Namhyung

Quoted 34 lines
tools/perf/builtin-record.c +4 -0
 		}
 
 		if (map->auxtrace_mmap.base && !rec->opts.auxtrace_snapshot_mode &&
@@ -1057,6 +1205,7 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
 			perf_evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_DATA_PENDING);
 
 		if (record__mmap_read_all(rec) < 0) {
+			record__mmap_read_sync(rec);
 			trigger_error(&auxtrace_snapshot_trigger);
 			trigger_error(&switch_output_trigger);
 			err = -1;
@@ -1068,6 +1217,7 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
 			if (!trigger_is_error(&auxtrace_snapshot_trigger))
 				record__read_auxtrace_snapshot(rec);
 			if (trigger_is_error(&auxtrace_snapshot_trigger)) {
+				record__mmap_read_sync(rec);
 				pr_err("AUX area tracing snapshot failed\n");
 				err = -1;
 				goto out_child;
@@ -1086,6 +1236,7 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
 			 */
 			if (rec->evlist->bkw_mmap_state == BKW_MMAP_RUNNING)
 				continue;
+			record__mmap_read_sync(rec);
 			trigger_ready(&switch_output_trigger);
 
 			/*
@@ -1139,6 +1290,7 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
 			disabled = true;
 		}
 	}
+	record__mmap_read_sync(rec);
 	trigger_off(&auxtrace_snapshot_trigger);
 	trigger_off(&switch_output_trigger);
#7 lore raw

Re: [PATCH v9 2/3]: perf record: enable asynchronous trace writing

Hi,

Quoted 1 line · Namhyung Kim

On Wed, Oct 03, 2018 at 07:12:10PM +0300, Alexey Budankov wrote:

<SNIP>

Quoted 7 lines
Quoted 7 lines
tools/perf/builtin-record.c +7 -0
+static void record__aio_sync(struct perf_mmap *md)
+{
+	struct aiocb *cblock = &md->cblock;
+	struct timespec timeout = { 0, 1000 * 1000  * 1 }; // 1ms
+
+	do {
+		if (cblock->aio_fildes == -1 || record__aio_complete(md, cblock))
Quoted 8 lines

tools/perf/builtin-record.c static int record__write(struct record *rec, struct perf_mmap *map __maybe_unuse · line 210

Quoted 5 lines
tools/perf/builtin-record.c +5 -0
+			return;
+
+		while (aio_suspend((const struct aiocb**)&cblock, 1, &timeout)) {
+			if (!(errno == EAGAIN || errno == EINTR))
+				pr_err("failed to sync perf data, error: %m\n");

Is there somthing we can do in this error case? Any chance it gets stuck in the loop?

Not really. Currently, in glibc, it can block on a mutex only.

Quoted 11 lines
Quoted 11 lines
tools/perf/builtin-record.c +9 -0
+		}
+	} while (1);
+}
+
+static int record__aio_pushfn(void *to, struct aiocb *cblock, void *bf, size_t size)
+{
+	off_t off;
+	struct record *rec = to;
+	int ret, trace_fd = rec->session->data->file.fd;
Quoted 8 lines

tools/perf/builtin-record.c static int record__write(struct record *rec, struct perf_mmap *map __maybe_unuse · line 224

Quoted 5 lines
tools/perf/builtin-record.c +5 -0
+
+	rec->samples++;
+
+	off = lseek(trace_fd, 0, SEEK_CUR);
+	lseek(trace_fd, off + size, SEEK_SET);

It'd be nice if these lseek() could be removed and use rec->bytes_written instead.

Well, this could be implemented like this avoiding lseek() in else branch:

	off = lseek(trace_fd, 0, SEEK_CUR);
	ret = record__aio_write(cblock, trace_fd, bf, size, off);
	if (!ret) {
		lseek(trace_fd, off + size, SEEK_SET);
		rec->bytes_written += size;

		if (switch_output_size(rec))
			trigger_hit(&switch_output_trigger);
	}
Quoted 11 lines

tools/perf/builtin-record.c static int record__write(struct record *rec, struct perf_mmap *map __maybe_unuse · line 230

Quoted 8 lines
tools/perf/builtin-record.c +6 -0
+	ret = record__aio_write(cblock, trace_fd, bf, size, off);
+	if (!ret) {
+		rec->bytes_written += size;
+
+		if (switch_output_size(rec))
+			trigger_hit(&switch_output_trigger);

Doesn't it need the _sync() before the trigger? Maybe it should be moved to record__mmap_read_evlist() or so..

Currently trigger just updates variable state.
The state is then checked thru separate API at __cmd_record() where record__mmap_read_sync() is called prior switching to a new trace file or finishing collection.

Quoted 1 line
Quoted 1 line

<SNIP>

Quoted 19 lines
Quoted 19 lines
tools/perf/builtin-record.c +14 -0
 		if (map->base) {
+#ifndef HAVE_AIO_SUPPORT
 			if (perf_mmap__push(map, rec, record__pushfn) != 0) {
 				rc = -1;
 				goto out;
 			}
+#else
+			if (!rec->opts.nr_cblocks) {
+				if (perf_mmap__push(map, rec, record__pushfn) != 0) {
+					rc = -1;
+					goto out;
+				}
+			} else {
+				/*
+				 * Call record__aio_sync() to wait till map->data buffer
+				 * becomes available after previous aio write request.
+				 */
+				record__aio_sync(map);
+				if (perf_mmap__aio_push(map, rec, record__aio_pushfn) != 0) {
Quoted 8 lines

tools/perf/builtin-record.c static int record__mmap_read_evlist(struct record *rec, struct perf_evlist *evli · line 689

Quoted 5 lines
tools/perf/builtin-record.c +5 -0
+					rc = -1;
+					goto out;
+				}
+			}
+#endif

If dummy aio functions are provided, the #ifdef can be removed and just use the #else part assuming opts.nr_cblocks == 0.

Yes, it looks a little bit cumbersome. Would this be more compact?

		if (map->base) {
#ifdef HAVE_AIO_SUPPORT
			if (!rec->opts.nr_cblocks) {
#endif
				if (perf_mmap__push(map, rec, record__pushfn) != 0) {
					rc = -1;
					goto out;
				}
#ifdef HAVE_AIO_SUPPORT
			} else {
				int idx;
				/*
				 * Call record__aio_sync() to wait till map->data buffer
				 * becomes available after previous aio write request.
				 */
				idx = record__aio_sync(map, false);
				if (perf_mmap__aio_push(map, rec, idx, record__aio_pushfn) != 0) {
					rc = -1;
					goto out;
				}
			}
#endif
		}

Thanks,
Alexey

maintainer
#8 lore raw

Re: [PATCH v9 2/3]: perf record: enable asynchronous trace writing

Quoted 1 line · Alexey Budankov

Hi,

Hi,

Quoted 20 lines · Namhyung Kim
Quoted 1 line · Alexey Budankov

<SNIP>

Quoted 7 lines
Quoted 7 lines
tools/perf/builtin-record.c +7 -0
+static void record__aio_sync(struct perf_mmap *md)
+{
+	struct aiocb *cblock = &md->cblock;
+	struct timespec timeout = { 0, 1000 * 1000  * 1 }; // 1ms
+
+	do {
+		if (cblock->aio_fildes == -1 || record__aio_complete(md, cblock))
Quoted 8 lines

tools/perf/builtin-record.c static int record__write(struct record *rec, struct perf_mmap *map __maybe_unuse · line 210

Quoted 5 lines
tools/perf/builtin-record.c +5 -0
+			return;
+
+		while (aio_suspend((const struct aiocb**)&cblock, 1, &timeout)) {
+			if (!(errno == EAGAIN || errno == EINTR))
+				pr_err("failed to sync perf data, error: %m\n");

Is there somthing we can do in this error case? Any chance it gets stuck in the loop?

Quoted 1 line

Not really. Currently, in glibc, it can block on a mutex only.

OK, I was thinking of a situation where the aio_suspend() keeps returning a same error code repeated..

Quoted 23 lines
Quoted 12 lines
Quoted 12 lines
tools/perf/builtin-record.c +9 -0
+		}
+	} while (1);
+}
+
+static int record__aio_pushfn(void *to, struct aiocb *cblock, void *bf, size_t size)
+{
+	off_t off;
+	struct record *rec = to;
+	int ret, trace_fd = rec->session->data->file.fd;
Quoted 8 lines

tools/perf/builtin-record.c static int record__write(struct record *rec, struct perf_mmap *map __maybe_unuse · line 224

Quoted 5 lines
tools/perf/builtin-record.c +5 -0
+
+	rec->samples++;
+
+	off = lseek(trace_fd, 0, SEEK_CUR);
+	lseek(trace_fd, off + size, SEEK_SET);

It'd be nice if these lseek() could be removed and use rec->bytes_written instead.

Well, this could be implemented like this avoiding lseek() in else branch:

Quoted 9 lines
	off = lseek(trace_fd, 0, SEEK_CUR);
	ret = record__aio_write(cblock, trace_fd, bf, size, off);
	if (!ret) {
		lseek(trace_fd, off + size, SEEK_SET);
		rec->bytes_written += size;

		if (switch_output_size(rec))
			trigger_hit(&switch_output_trigger);
	}

Oh I meant the both like:

	off = rec->bytes_written;

ret = record__aio_write(cblock, trace_fd, bf, size, off); if (!ret) {
rec->bytes_written += size;

		...
Quoted 47 lines
Quoted 11 lines

tools/perf/builtin-record.c static int record__write(struct record *rec, struct perf_mmap *map __maybe_unuse · line 230

Quoted 8 lines
tools/perf/builtin-record.c +6 -0
+	ret = record__aio_write(cblock, trace_fd, bf, size, off);
+	if (!ret) {
+		rec->bytes_written += size;
+
+		if (switch_output_size(rec))
+			trigger_hit(&switch_output_trigger);

Doesn't it need the _sync() before the trigger? Maybe it should be moved to record__mmap_read_evlist() or so..

Currently trigger just updates variable state.
The state is then checked thru separate API at __cmd_record() where record__mmap_read_sync() is called prior switching to a new trace file or finishing collection.

Quoted 1 line
Quoted 1 line

<SNIP>

Quoted 19 lines
Quoted 19 lines
tools/perf/builtin-record.c +14 -0
 		if (map->base) {
+#ifndef HAVE_AIO_SUPPORT
 			if (perf_mmap__push(map, rec, record__pushfn) != 0) {
 				rc = -1;
 				goto out;
 			}
+#else
+			if (!rec->opts.nr_cblocks) {
+				if (perf_mmap__push(map, rec, record__pushfn) != 0) {
+					rc = -1;
+					goto out;
+				}
+			} else {
+				/*
+				 * Call record__aio_sync() to wait till map->data buffer
+				 * becomes available after previous aio write request.
+				 */
+				record__aio_sync(map);
+				if (perf_mmap__aio_push(map, rec, record__aio_pushfn) != 0) {
Quoted 8 lines

tools/perf/builtin-record.c static int record__mmap_read_evlist(struct record *rec, struct perf_evlist *evli · line 689

Quoted 5 lines
tools/perf/builtin-record.c +5 -0
+					rc = -1;
+					goto out;
+				}
+			}
+#endif

If dummy aio functions are provided, the #ifdef can be removed and just use the #else part assuming opts.nr_cblocks == 0.

Quoted 1 line

Yes, it looks a little bit cumbersome. Would this be more compact?

Why not exposing opts.nr_cblocks regardless of the #ifdef? It'll have 0 when it's not compiled in. Then it could be like below (assuming you have all the dummy aio funcitons):

Quoted 12 lines
		if (map->base) {
			if (!rec->opts.nr_cblocks) {
				if (perf_mmap__push(map, rec, record__pushfn) != 0) {
					rc = -1;
					goto out;
				}
			} else {
				int idx;
				/*
				 * Call record__aio_sync() to wait till map->data buffer
				 * becomes available after previous aio write request.
Quoted 9 lines
				 */
				idx = record__aio_sync(map, false);
				if (perf_mmap__aio_push(map, rec, idx, record__aio_pushfn) != 0) {
					rc = -1;
					goto out;
				}
			}
		}

Thanks,
Namhyung

#9 lore raw

Re: [PATCH v9 2/3]: perf record: enable asynchronous trace writing

Hi,

Quoted 1 line · Namhyung Kim

On Fri, Oct 05, 2018 at 11:31:11AM +0300, Alexey Budankov wrote:

<SNIP>

Quoted 17 lines
Quoted 12 lines

Well, this could be implemented like this avoiding lseek() in else branch:

	off = lseek(trace_fd, 0, SEEK_CUR);
	ret = record__aio_write(cblock, trace_fd, bf, size, off);
	if (!ret) {
		lseek(trace_fd, off + size, SEEK_SET);
		rec->bytes_written += size;

		if (switch_output_size(rec))
			trigger_hit(&switch_output_trigger);
	}

Oh I meant the both like:

	off = rec->bytes_written;

Review

Quoted 5 lines
(quoted diff) +0 -0
 	ret = record__aio_write(cblock, trace_fd, bf, size, off);
 	if (!ret) {
 		rec->bytes_written += size;
 
...

It still have to adjust the file pos thru lseek() prior leaving record__aio_pushfn() so space in trace file would be pre-allocated for enqueued record and file pos be moved beyond the record data, possibly for the next record.

Quoted 1 line

<SNIP>

Quoted 27 lines

Why not exposing opts.nr_cblocks regardless of the #ifdef? It'll have 0 when it's not compiled in. Then it could be like below (assuming you have all the dummy aio funcitons):

Quoted 21 lines
		if (map->base) {
			if (!rec->opts.nr_cblocks) {
				if (perf_mmap__push(map, rec, record__pushfn) != 0) {
					rc = -1;
					goto out;
				}
			} else {
				int idx;
				/*
				 * Call record__aio_sync() to wait till map->data buffer
				 * becomes available after previous aio write request.
				 */
				idx = record__aio_sync(map, false);
				if (perf_mmap__aio_push(map, rec, idx, record__aio_pushfn) != 0) {
					rc = -1;
					goto out;
				}
			}
		}

Well, if it has AIO symbols + opts.nr_cblocks exposed unconditionally of HAVE_AIO_SUPPORT, but keeps the symbols implementation under the define, then as far aio-cblocks option is not exposed thru command line, we end up in whole bunch of symbols referenced under the else branch that, after all, can cause Perf binary size increase, which is, probably, worth avoiding.

Thanks,
Alexey

maintainer
#10 lore raw

Re: [PATCH v9 2/3]: perf record: enable asynchronous trace writing

Hi,

Quoted 25 lines · Alexey Budankov

Hi,

Quoted 1 line · Namhyung Kim

On Fri, Oct 05, 2018 at 11:31:11AM +0300, Alexey Budankov wrote:

<SNIP>

Quoted 20 lines
Quoted 12 lines

Well, this could be implemented like this avoiding lseek() in else branch:

	off = lseek(trace_fd, 0, SEEK_CUR);
	ret = record__aio_write(cblock, trace_fd, bf, size, off);
	if (!ret) {
		lseek(trace_fd, off + size, SEEK_SET);
		rec->bytes_written += size;

		if (switch_output_size(rec))
			trigger_hit(&switch_output_trigger);
	}

Oh I meant the both like:

	off = rec->bytes_written;
(quoted diff) +0 -0
 	ret = record__aio_write(cblock, trace_fd, bf, size, off);
 	if (!ret) {
 		rec->bytes_written += size;
Quoted 7 lines
Quoted 2 lines
		...

It still have to adjust the file pos thru lseek() prior leaving record__aio_pushfn() so space in trace file would be pre-allocated for enqueued record and file pos be moved beyond the record data, possibly for the next record.

For that purpose, isn't it better calling ftruncate() with a reasonable batch size to reduce number of syscalls?

Quoted 31 lines
Quoted 2 lines

<SNIP>

Quoted 27 lines

Why not exposing opts.nr_cblocks regardless of the #ifdef? It'll have 0 when it's not compiled in. Then it could be like below (assuming you have all the dummy aio funcitons):

Quoted 21 lines
		if (map->base) {
			if (!rec->opts.nr_cblocks) {
				if (perf_mmap__push(map, rec, record__pushfn) != 0) {
					rc = -1;
					goto out;
				}
			} else {
				int idx;
				/*
				 * Call record__aio_sync() to wait till map->data buffer
				 * becomes available after previous aio write request.
				 */
				idx = record__aio_sync(map, false);
				if (perf_mmap__aio_push(map, rec, idx, record__aio_pushfn) != 0) {
					rc = -1;
					goto out;
				}
			}
		}
Quoted 5 lines

Well, if it has AIO symbols + opts.nr_cblocks exposed unconditionally of HAVE_AIO_SUPPORT, but keeps the symbols implementation under the define, then as far aio-cblocks option is not exposed thru command line, we end up in whole bunch of symbols referenced under the else branch that, after all, can cause Perf binary size increase, which is, probably, worth avoiding.

I think it's ok as long as they're empty.

Thanks,
Namhyung

#11 lore raw

Re: [PATCH v9 2/3]: perf record: enable asynchronous trace writing

Hi,

On 05.10.2018 13:55, Namhyung Kim wrote:
<SNIP>

Quoted 1 line · Alexey Budankov

<SNIP>

Quoted 9 lines
Quoted 5 lines

It still have to adjust the file pos thru lseek() prior leaving record__aio_pushfn() so space in trace file would be pre-allocated for enqueued record and file pos be moved beyond the record data, possibly for the next record.

For that purpose, isn't it better calling ftruncate() with a reasonable batch size to reduce number of syscalls?

According to docs [1] ftruncate() does not advance file pos which is essential here.

Quoted 1 line

<SNIP>

Quoted 7 lines
Quoted 5 lines

Well, if it has AIO symbols + opts.nr_cblocks exposed unconditionally of HAVE_AIO_SUPPORT, but keeps the symbols implementation under the define, then as far aio-cblocks option is not exposed thru command line, we end up in whole bunch of symbols referenced under the else branch that, after all, can cause Perf binary size increase, which is, probably, worth avoiding.

I think it's ok as long as they're empty.

Well, the both designs are possible and acceptable.
The only thing that matters here is contradictive requests from other reviewing folks. Let me share the version without dummy functions so
we could jointly decide how to proceed from there.
Ok?

Thanks,
Alexey

[1] http://man7.org/linux/man-pages/man2/ftruncate.2.html

#12 lore raw

Re: [PATCH v9 2/3]: perf record: enable asynchronous trace writing

Hi,
On 05.10.2018 14:50, Alexey Budankov wrote:<SNIP>

Quoted 4 lines

According to docs [1] ftruncate() does not advance file pos which is essential here.

Ideally, the both lseek() syscalls in every loop iteration can be replaced by the only two syscalls just before and after the loop and advancing *in-flight* off file pos value at perf_mmap__aio_push() after every successful record__aio_pushfn().

Thanks,
Alexey

#13 lore raw

PATCH v9 3/3 : perf record: extend trace writing to multi AIO

In reply to Alexey Budankov

Multi AIO trace writing allows caching more kernel data into userspace memory postponing trace writing for the sake of overall profiling data thruput increase. It could be seen as kernel data buffer extension into userspace memory.

With aio-cblocks option value different from 0, current default value, tool has capability to cache more and more data into user space along with delegating spill to AIO.

That allows avoiding suspend at record__aio_sync() between calls of record__mmap_read_evlist() and increase profiling data thruput for the cost of userspace memory.

---
 tools/perf/builtin-record.c | 59 +++++++++++++++++++++++++++------
 tools/perf/util/evlist.c    |  7 ++--
 tools/perf/util/evlist.h    |  3 +-
 tools/perf/util/mmap.c      | 79 ++++++++++++++++++++++++++++++++-------------
 tools/perf/util/mmap.h      |  7 ++--
 5 files changed, 115 insertions(+), 40 deletions(-)
tools/perf/builtin-record.c +49 -10
index c63fe8c021a2..7b67574be5b7 100644
@@ -196,16 +196,35 @@ static int record__aio_complete(struct perf_mmap *md, struct aiocb *cblock)
 	return rc;
 }
 
-static void record__aio_sync(struct perf_mmap *md)
+static int record__aio_sync(struct perf_mmap *md, bool sync_all)
 {
-	struct aiocb *cblock = &md->cblock;
+	struct aiocb **aiocb = md->aiocb;
+	struct aiocb *cblocks = md->cblocks;
 	struct timespec timeout = { 0, 1000 * 1000  * 1 }; // 1ms
+	int i, do_suspend;
 
 	do {
-		if (cblock->aio_fildes == -1 || record__aio_complete(md, cblock))
-			return;
+		do_suspend = 0;
+		for (i = 0; i < md->nr_cblocks; ++i) {
+			if (cblocks[i].aio_fildes == -1 || record__aio_complete(md, &cblocks[i])) {
+				if (sync_all)
+					aiocb[i] = NULL;
+				else
+					return i;
+			} else {
+				/*
+				 * Started aio write is not complete yet
+				 * so it has to be waited before the
+				 * next allocation.
+				 */
+				aiocb[i] = &cblocks[i];
+				do_suspend = 1;
+			}
+		}
+		if (!do_suspend)
+			return -1;
 
-		while (aio_suspend((const struct aiocb**)&cblock, 1, &timeout)) {
+		while (aio_suspend((const struct aiocb **)aiocb, md->nr_cblocks, &timeout)) {
 			if (!(errno == EAGAIN || errno == EINTR))
 				pr_err("failed to sync perf data, error: %m\n");
 		}
@@ -436,11 +455,15 @@ static int record__mmap_evlist(struct record *rec,
 			       struct perf_evlist *evlist)
 {
 	struct record_opts *opts = &rec->opts;
+	int nr_cblocks = 0;
 	char msg[512];
-
+#ifdef HAVE_AIO_SUPPORT
+	nr_cblocks = opts->nr_cblocks;
+#endif
 	if (perf_evlist__mmap_ex(evlist, opts->mmap_pages,
 				 opts->auxtrace_mmap_pages,
-				 opts->auxtrace_snapshot_mode) < 0) {
+				 opts->auxtrace_snapshot_mode,
+				 nr_cblocks) < 0) {
 		if (errno == EPERM) {
 			pr_err("Permission error mapping pages.\n"
 			       "Consider increasing "
@@ -637,7 +660,7 @@ static void record__mmap_read_sync(struct record *rec __maybe_unused)
 	for (i = 0; i < evlist->nr_mmaps; i++) {
 		struct perf_mmap *map = &maps[i];
 		if (map->base)
-			record__aio_sync(map);
+			record__aio_sync(map, true);
 	}
 #endif
 }
@@ -676,12 +699,13 @@ static int record__mmap_read_evlist(struct record *rec, struct perf_evlist *evli
 					goto out;
 				}
 			} else {
+				int idx;
 				/*
 				 * Call record__aio_sync() to wait till map->data buffer
 				 * becomes available after previous aio write request.
 				 */
-				record__aio_sync(map);
-				if (perf_mmap__aio_push(map, rec, record__aio_pushfn) != 0) {
+				idx = record__aio_sync(map, false);
+				if (perf_mmap__aio_push(map, rec, idx, record__aio_pushfn) != 0) {
 					rc = -1;
 					goto out;
 				}
@@ -1442,6 +1466,10 @@ static int perf_record_config(const char *var, const char *value, void *cb)
 		var = "call-graph.record-mode";
 		return perf_default_config(var, value, cb);
 	}
+#ifdef HAVE_AIO_SUPPORT
+	if (!strcmp(var, "record.aio-cblocks"))
+		rec->opts.nr_cblocks = strtol(value, NULL, 0);
+#endif
 
 	return 0;
 }
@@ -1833,6 +1861,10 @@ static struct option __record_options[] = {
 			  "signal"),
 	OPT_BOOLEAN(0, "dry-run", &dry_run,
 		    "Parse options then exit"),
+#ifdef HAVE_AIO_SUPPORT
+	OPT_INTEGER(0, "aio-cblocks", &record.opts.nr_cblocks,
+		    "Max number of simultaneous per-mmap trace writes (default: 0 - serial, max: 4)"),
+#endif
 	OPT_END()
 };
 
@@ -2025,6 +2057,13 @@ int cmd_record(int argc, const char **argv)
 		goto out;
 	}
 
+#ifdef HAVE_AIO_SUPPORT
+	if (rec->opts.nr_cblocks > 4)
+		rec->opts.nr_cblocks = 4;
+	if (verbose > 0)
+		pr_info("nr_cblocks: %d\n", rec->opts.nr_cblocks);
+#endif
+
 	err = __cmd_record(&record, argc, argv);
 out:
 	perf_evlist__delete(rec->evlist);
tools/perf/util/evlist.c +4 -3
index af2f8c965d7a..4bb5dd5b58e9 100644
@@ -1018,7 +1018,8 @@ int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
  */
 int perf_evlist__mmap_ex(struct perf_evlist *evlist, unsigned int pages,
 			 unsigned int auxtrace_pages,
-			 bool auxtrace_overwrite)
+			 bool auxtrace_overwrite,
+			 int nr_cblocks __maybe_unused)
 {
 	struct perf_evsel *evsel;
 	const struct cpu_map *cpus = evlist->cpus;
@@ -1030,7 +1031,7 @@ int perf_evlist__mmap_ex(struct perf_evlist *evlist, unsigned int pages,
 	 */
 	struct mmap_params mp;
 #ifdef HAVE_AIO_SUPPORT
-	mp.nr_cblocks = 0;
+	mp.nr_cblocks = nr_cblocks;
 #endif
 	if (!evlist->mmap)
 		evlist->mmap = perf_evlist__alloc_mmap(evlist, false);
@@ -1062,7 +1063,7 @@ int perf_evlist__mmap_ex(struct perf_evlist *evlist, unsigned int pages,
 
 int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages)
 {
-	return perf_evlist__mmap_ex(evlist, pages, 0, false);
+	return perf_evlist__mmap_ex(evlist, pages, 0, false, 0);
 }
 
 int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target)
tools/perf/util/evlist.h +2 -1
index dc66436add98..a94d3c613254 100644
@@ -162,7 +162,8 @@ unsigned long perf_event_mlock_kb_in_pages(void);
 
 int perf_evlist__mmap_ex(struct perf_evlist *evlist, unsigned int pages,
 			 unsigned int auxtrace_pages,
-			 bool auxtrace_overwrite);
+			 bool auxtrace_overwrite,
+			 int nr_cblocks);
 int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages);
 void perf_evlist__munmap(struct perf_evlist *evlist);
 
tools/perf/util/mmap.c +56 -23

137 lines not on this page · raw message

tools/perf/util/mmap.h +4 -3
index ac011777c38f..68050d48d767 100644
@@ -30,8 +30,9 @@ struct perf_mmap {
 	struct auxtrace_mmap auxtrace_mmap;
 	char		 event_copy[PERF_SAMPLE_MAX_SIZE] __aligned(8);
 #ifdef HAVE_AIO_SUPPORT
-	void 		 *data;
-	struct aiocb	 cblock;
+	void		 **data;
+	struct aiocb	 *cblocks;
+	struct aiocb	 **aiocb;
 	int		 nr_cblocks;
 #endif
 };
@@ -106,7 +107,7 @@ union perf_event *perf_mmap__read_event(struct perf_mmap *map);
 int perf_mmap__push(struct perf_mmap *md, void *to,
 		    int push(struct perf_mmap *map, void *to, void *buf, size_t size));
 #ifdef HAVE_AIO_SUPPORT
-int perf_mmap__aio_push(struct perf_mmap *md, void *to,
+int perf_mmap__aio_push(struct perf_mmap *md, void *to, int idx,
 			int push(void *to, struct aiocb *cblock, void *buf, size_t size));
 #endif
 
maintainer
#14 lore raw

Re: [PATCH v9 3/3]: perf record: extend trace writing to multi AIO

Quoted 123 lines · Alexey Budankov

Multi AIO trace writing allows caching more kernel data into userspace memory postponing trace writing for the sake of overall profiling data thruput increase. It could be seen as kernel data buffer extension into userspace memory.

With aio-cblocks option value different from 0, current default value, tool has capability to cache more and more data into user space along with delegating spill to AIO.

That allows avoiding suspend at record__aio_sync() between calls of record__mmap_read_evlist() and increase profiling data thruput for the cost of userspace memory.

---
 tools/perf/builtin-record.c | 59 +++++++++++++++++++++++++++------
 tools/perf/util/evlist.c    |  7 ++--
 tools/perf/util/evlist.h    |  3 +-
 tools/perf/util/mmap.c      | 79 ++++++++++++++++++++++++++++++++-------------
 tools/perf/util/mmap.h      |  7 ++--
 5 files changed, 115 insertions(+), 40 deletions(-)
tools/perf/builtin-record.c +38 -10
index c63fe8c021a2..7b67574be5b7 100644
@@ -196,16 +196,35 @@ static int record__aio_complete(struct perf_mmap *md, struct aiocb *cblock)
 	return rc;
 }
 
-static void record__aio_sync(struct perf_mmap *md)
+static int record__aio_sync(struct perf_mmap *md, bool sync_all)
 {
-	struct aiocb *cblock = &md->cblock;
+	struct aiocb **aiocb = md->aiocb;
+	struct aiocb *cblocks = md->cblocks;
 	struct timespec timeout = { 0, 1000 * 1000  * 1 }; // 1ms
+	int i, do_suspend;
 
 	do {
-		if (cblock->aio_fildes == -1 || record__aio_complete(md, cblock))
-			return;
+		do_suspend = 0;
+		for (i = 0; i < md->nr_cblocks; ++i) {
+			if (cblocks[i].aio_fildes == -1 || record__aio_complete(md, &cblocks[i])) {
+				if (sync_all)
+					aiocb[i] = NULL;
+				else
+					return i;
+			} else {
+				/*
+				 * Started aio write is not complete yet
+				 * so it has to be waited before the
+				 * next allocation.
+				 */
+				aiocb[i] = &cblocks[i];
+				do_suspend = 1;
+			}
+		}
+		if (!do_suspend)
+			return -1;
 
-		while (aio_suspend((const struct aiocb**)&cblock, 1, &timeout)) {
+		while (aio_suspend((const struct aiocb **)aiocb, md->nr_cblocks, &timeout)) {
 			if (!(errno == EAGAIN || errno == EINTR))
 				pr_err("failed to sync perf data, error: %m\n");
 		}
@@ -436,11 +455,15 @@ static int record__mmap_evlist(struct record *rec,
 			       struct perf_evlist *evlist)
 {
 	struct record_opts *opts = &rec->opts;
+	int nr_cblocks = 0;
 	char msg[512];
-
+#ifdef HAVE_AIO_SUPPORT
+	nr_cblocks = opts->nr_cblocks;
+#endif
 	if (perf_evlist__mmap_ex(evlist, opts->mmap_pages,
 				 opts->auxtrace_mmap_pages,
-				 opts->auxtrace_snapshot_mode) < 0) {
+				 opts->auxtrace_snapshot_mode,
+				 nr_cblocks) < 0) {
 		if (errno == EPERM) {
 			pr_err("Permission error mapping pages.\n"
 			       "Consider increasing "
@@ -637,7 +660,7 @@ static void record__mmap_read_sync(struct record *rec __maybe_unused)
 	for (i = 0; i < evlist->nr_mmaps; i++) {
 		struct perf_mmap *map = &maps[i];
 		if (map->base)
-			record__aio_sync(map);
+			record__aio_sync(map, true);
 	}
 #endif
 }
@@ -676,12 +699,13 @@ static int record__mmap_read_evlist(struct record *rec, struct perf_evlist *evli
 					goto out;
 				}
 			} else {
+				int idx;
 				/*
 				 * Call record__aio_sync() to wait till map->data buffer
 				 * becomes available after previous aio write request.
 				 */
-				record__aio_sync(map);
-				if (perf_mmap__aio_push(map, rec, record__aio_pushfn) != 0) {
+				idx = record__aio_sync(map, false);
+				if (perf_mmap__aio_push(map, rec, idx, record__aio_pushfn) != 0) {
 					rc = -1;
 					goto out;
 				}
@@ -1442,6 +1466,10 @@ static int perf_record_config(const char *var, const char *value, void *cb)
 		var = "call-graph.record-mode";
 		return perf_default_config(var, value, cb);
 	}
+#ifdef HAVE_AIO_SUPPORT
+	if (!strcmp(var, "record.aio-cblocks"))
+		rec->opts.nr_cblocks = strtol(value, NULL, 0);
+#endif
 
 	return 0;
 }

tools/perf/builtin-record.c static struct option __record_options[] = { · line 1869

Quoted 9 lines
tools/perf/builtin-record.c +4 -0
@@ -1833,6 +1861,10 @@ static struct option __record_options[] = {
 			  "signal"),
 	OPT_BOOLEAN(0, "dry-run", &dry_run,
 		    "Parse options then exit"),
+#ifdef HAVE_AIO_SUPPORT
+	OPT_INTEGER(0, "aio-cblocks", &record.opts.nr_cblocks,
+		    "Max number of simultaneous per-mmap trace writes (default: 0 - serial, max: 4)"),
+#endif
 	OPT_END()
 };

Please update the documentation for the new option and config variable. Perhaps it can provide the option always and check the availability at runtime (with a warning message if not enabled).

Thanks,
Namhyung

Quoted 15 lines
tools/perf/builtin-record.c +7 -0
@@ -2025,6 +2057,13 @@ int cmd_record(int argc, const char **argv)
 		goto out;
 	}
 
+#ifdef HAVE_AIO_SUPPORT
+	if (rec->opts.nr_cblocks > 4)
+		rec->opts.nr_cblocks = 4;
+	if (verbose > 0)
+		pr_info("nr_cblocks: %d\n", rec->opts.nr_cblocks);
+#endif
+
 	err = __cmd_record(&record, argc, argv);
 out:
 	perf_evlist__delete(rec->evlist);
#15 lore raw

Re: [PATCH v9 3/3]: perf record: extend trace writing to multi AIO

Hi,

Quoted 1 line · Namhyung Kim

On Wed, Oct 03, 2018 at 07:17:01PM +0300, Alexey Budankov wrote:

<SNIP>

Quoted 4 lines
Quoted 4 lines
tools/perf/builtin-record.c +0 -0
@@ -1833,6 +1861,10 @@ static struct option __record_options[] = {
 			  "signal"),
 	OPT_BOOLEAN(0, "dry-run", &dry_run,
 		    "Parse options then exit"),
Quoted 8 lines

tools/perf/builtin-record.c static struct option __record_options[] = { · line 1869

Quoted 6 lines
tools/perf/builtin-record.c +4 -0
+#ifdef HAVE_AIO_SUPPORT
+	OPT_INTEGER(0, "aio-cblocks", &record.opts.nr_cblocks,
+		    "Max number of simultaneous per-mmap trace writes (default: 0 - serial, max: 4)"),
+#endif
 	OPT_END()
 };

Please update the documentation for the new option and config

Accepted.

Quoted 2 lines

variable. Perhaps it can provide the option always and check the availability at runtime (with a warning message if not enabled).

Not sure if this complication is really required.
So far NO_AIO define is possible at compile time.

Thanks,
Alexey