All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/5] perf core: Read from overwrite ring buffer
@ 2016-01-25  8:33 Wang Nan
  2016-01-25  8:33 ` [PATCH 1/5] perf core: Introduce new ioctl options to pause and resume " Wang Nan
                   ` (6 more replies)
  0 siblings, 7 replies; 16+ messages in thread
From: Wang Nan @ 2016-01-25  8:33 UTC (permalink / raw)
  To: peterz, alexei.starovoitov, acme
  Cc: linux-kernel, Wang Nan, He Kuang, Alexei Starovoitov,
	Arnaldo Carvalho de Melo, Brendan Gregg, Jiri Olsa,
	Masami Hiramatsu, Namhyung Kim, Zefan Li, pi3orama

This is the v3 of this series. Compare with v2, tailsize method is
removed, ioctl command PERF_EVENT_IOC_PAUSE_OUTPUT is changed to
_IOW('$', 9, __u32) since it has an input value, commit message
is slightly adjusted.

New test result on ARM64 is below (x86_64 result is copied from previous
email, test method is described in previous version [1]):

                       x86_64        |       ARM64
              -----------------------+---------------------
                MEAN         STDVAR  |    MEAN       STDVAR
   BASE     :  879870.81   11913.13  |  808302.67   6951.47
   RAWPERF  : 2603854.70  706658.40  | 3461675.12  54075.69
   WRTBKWRD : 2313301.22    6727.96  | 3350177.98  23125.48
   TAILSIZE : 2383051.86    5248.06  | 3556496.56  24802.17
   RAWOVWRT : 2315273.18    5221.03  | 3458767.49  41560.32
   RAWOVWRT*: 2323970.45    5103.39  | 3438967.06  34095.83

ARM64 platform is a smartphone with 8 Hisilicon arm64 cores
(big/little).

Benckmarking result on both architechure support removing tailsize
method because its performance penalty is higher than WRTBKWRD. Also, it
seems we don't need to consider tailheader method (putting whole header
at the end of records, kernel write to the ring buffer from beginning to
end), because the above result shows WRTBKWRD and RAWOVWRT are similar,
and tailheader is impossible to outperform RAWOVWRT in theory.

[1] http://lkml.kernel.org/g/1453464834-233200-1-git-send-email-wangnan0@huawei.com

Wang Nan (5):
  perf core: Introduce new ioctl options to pause and resume ring buffer
  perf core: Set event's default overflow_handler
  perf core: Prepare writing into ring buffer from end
  perf core: Add backward attribute to perf event
  perf core: Reduce perf event output overhead by new overflow handler

 include/linux/perf_event.h      | 22 +++++++++++--
 include/uapi/linux/perf_event.h |  4 ++-
 kernel/events/core.c            | 73 +++++++++++++++++++++++++++++++++++------
 kernel/events/internal.h        | 11 +++++++
 kernel/events/ring_buffer.c     | 63 +++++++++++++++++++++++++++++++----
 5 files changed, 153 insertions(+), 20 deletions(-)

Signed-off-by: Wang Nan <wangnan0@huawei.com>
Cc: He Kuang <hekuang@huawei.com>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Zefan Li <lizefan@huawei.com>
Cc: pi3orama@163.com

-- 
1.8.3.4

^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH 1/5] perf core: Introduce new ioctl options to pause and resume ring buffer
  2016-01-25  8:33 [PATCH 0/5] perf core: Read from overwrite ring buffer Wang Nan
@ 2016-01-25  8:33 ` Wang Nan
  2016-01-25  8:33 ` [PATCH 2/5] perf core: Set event's default overflow_handler Wang Nan
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 16+ messages in thread
From: Wang Nan @ 2016-01-25  8:33 UTC (permalink / raw)
  To: peterz, alexei.starovoitov, acme
  Cc: linux-kernel, Wang Nan, He Kuang, Alexei Starovoitov,
	Arnaldo Carvalho de Melo, Brendan Gregg, Jiri Olsa,
	Masami Hiramatsu, Namhyung Kim, Zefan Li, pi3orama

Add new ioctl() to pause/resume ring-buffer output.

In some situations we want to read from ring buffer only when we
ensure nothing can write to the ring buffer during reading. Without
this patch we have to turn off all events attached to this ring buffer
to achieve this.

This patch is for supporting overwrite ring buffer. Following
commits will introduce new methods support reading from overwrite ring
buffer. Before reading caller must ensure the ring buffer is frozen, or
the reading is unreliable.

Signed-off-by: Wang Nan <wangnan0@huawei.com>
Cc: He Kuang <hekuang@huawei.com>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Zefan Li <lizefan@huawei.com>
Cc: pi3orama@163.com
---
 include/uapi/linux/perf_event.h |  1 +
 kernel/events/core.c            | 13 +++++++++++++
 kernel/events/internal.h        | 11 +++++++++++
 kernel/events/ring_buffer.c     |  7 ++++++-
 4 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_event.h
index 1afe962..a3c1903 100644
--- a/include/uapi/linux/perf_event.h
+++ b/include/uapi/linux/perf_event.h
@@ -401,6 +401,7 @@ struct perf_event_attr {
 #define PERF_EVENT_IOC_SET_FILTER	_IOW('$', 6, char *)
 #define PERF_EVENT_IOC_ID		_IOR('$', 7, __u64 *)
 #define PERF_EVENT_IOC_SET_BPF		_IOW('$', 8, __u32)
+#define PERF_EVENT_IOC_PAUSE_OUTPUT	_IOW('$', 9, __u32)
 
 enum perf_event_ioc_flags {
 	PERF_IOC_FLAG_GROUP		= 1U << 0,
diff --git a/kernel/events/core.c b/kernel/events/core.c
index bf82441..9e9c84da 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -4241,6 +4241,19 @@ static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned lon
 	case PERF_EVENT_IOC_SET_BPF:
 		return perf_event_set_bpf_prog(event, arg);
 
+	case PERF_EVENT_IOC_PAUSE_OUTPUT: {
+		struct ring_buffer *rb;
+
+		rcu_read_lock();
+		rb = rcu_dereference(event->rb);
+		if (!event->rb) {
+			rcu_read_unlock();
+			return -EINVAL;
+		}
+		rb_toggle_paused(rb, !!arg);
+		rcu_read_unlock();
+		return 0;
+	}
 	default:
 		return -ENOTTY;
 	}
diff --git a/kernel/events/internal.h b/kernel/events/internal.h
index 2bbad9c..6a93d1b 100644
--- a/kernel/events/internal.h
+++ b/kernel/events/internal.h
@@ -18,6 +18,7 @@ struct ring_buffer {
 #endif
 	int				nr_pages;	/* nr of data pages  */
 	int				overwrite;	/* can overwrite itself */
+	int				paused;		/* can write into ring buffer */
 
 	atomic_t			poll;		/* POLL_ for wakeups */
 
@@ -65,6 +66,16 @@ static inline void rb_free_rcu(struct rcu_head *rcu_head)
 	rb_free(rb);
 }
 
+static inline void
+rb_toggle_paused(struct ring_buffer *rb,
+		 bool pause)
+{
+	if (!pause && rb->nr_pages)
+		rb->paused = 0;
+	else
+		rb->paused = 1;
+}
+
 extern struct ring_buffer *
 rb_alloc(int nr_pages, long watermark, int cpu, int flags);
 extern void perf_event_wakeup(struct perf_event *event);
diff --git a/kernel/events/ring_buffer.c b/kernel/events/ring_buffer.c
index adfdc05..9f1a93f 100644
--- a/kernel/events/ring_buffer.c
+++ b/kernel/events/ring_buffer.c
@@ -125,8 +125,11 @@ int perf_output_begin(struct perf_output_handle *handle,
 	if (unlikely(!rb))
 		goto out;
 
-	if (unlikely(!rb->nr_pages))
+	if (unlikely(rb->paused)) {
+		if (rb->nr_pages)
+			local_inc(&rb->lost);
 		goto out;
+	}
 
 	handle->rb    = rb;
 	handle->event = event;
@@ -244,6 +247,8 @@ ring_buffer_init(struct ring_buffer *rb, long watermark, int flags)
 	INIT_LIST_HEAD(&rb->event_list);
 	spin_lock_init(&rb->event_lock);
 	init_irq_work(&rb->irq_work, rb_irq_work);
+
+	rb->paused = rb->nr_pages ? 0 : 1;
 }
 
 static void ring_buffer_put_async(struct ring_buffer *rb)
-- 
1.8.3.4

^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH 2/5] perf core: Set event's default overflow_handler
  2016-01-25  8:33 [PATCH 0/5] perf core: Read from overwrite ring buffer Wang Nan
  2016-01-25  8:33 ` [PATCH 1/5] perf core: Introduce new ioctl options to pause and resume " Wang Nan
@ 2016-01-25  8:33 ` Wang Nan
  2016-01-25  8:33 ` [PATCH 3/5] perf core: Prepare writing into ring buffer from end Wang Nan
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 16+ messages in thread
From: Wang Nan @ 2016-01-25  8:33 UTC (permalink / raw)
  To: peterz, alexei.starovoitov, acme
  Cc: linux-kernel, Wang Nan, He Kuang, Alexei Starovoitov,
	Arnaldo Carvalho de Melo, Brendan Gregg, Jiri Olsa,
	Masami Hiramatsu, Namhyung Kim, Zefan Li, pi3orama

Set a default event->overflow_handler in perf_event_alloc() so don't
need checking event->overflow_handler in __perf_event_overflow().
Following commits can give a different default overflow_handler.

No extra performance introduced into hot path because in the original
code we still need reading this handler from memory. A conditional branch
is avoided so actually we remove some instructions.

Initial idea comes from Peter at [1].

[1] http://lkml.kernel.org/r/20130708121557.GA17211@twins.programming.kicks-ass.net

Signed-off-by: Wang Nan <wangnan0@huawei.com>
Cc: He Kuang <hekuang@huawei.com>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Zefan Li <lizefan@huawei.com>
Cc: pi3orama@163.com
---
 kernel/events/core.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/kernel/events/core.c b/kernel/events/core.c
index 9e9c84da..f79c4be 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -6402,10 +6402,7 @@ static int __perf_event_overflow(struct perf_event *event,
 		irq_work_queue(&event->pending);
 	}
 
-	if (event->overflow_handler)
-		event->overflow_handler(event, data, regs);
-	else
-		perf_event_output(event, data, regs);
+	event->overflow_handler(event, data, regs);
 
 	if (*perf_event_fasync(event) && event->pending_kill) {
 		event->pending_wakeup = 1;
@@ -7874,8 +7871,13 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu,
 		context = parent_event->overflow_handler_context;
 	}
 
-	event->overflow_handler	= overflow_handler;
-	event->overflow_handler_context = context;
+	if (overflow_handler) {
+		event->overflow_handler	= overflow_handler;
+		event->overflow_handler_context = context;
+	} else {
+		event->overflow_handler = perf_event_output;
+		event->overflow_handler_context = NULL;
+	}
 
 	perf_event__state_init(event);
 
-- 
1.8.3.4

^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH 3/5] perf core: Prepare writing into ring buffer from end
  2016-01-25  8:33 [PATCH 0/5] perf core: Read from overwrite ring buffer Wang Nan
  2016-01-25  8:33 ` [PATCH 1/5] perf core: Introduce new ioctl options to pause and resume " Wang Nan
  2016-01-25  8:33 ` [PATCH 2/5] perf core: Set event's default overflow_handler Wang Nan
@ 2016-01-25  8:33 ` Wang Nan
  2016-01-25  8:33 ` [PATCH 4/5] perf core: Add backward attribute to perf event Wang Nan
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 16+ messages in thread
From: Wang Nan @ 2016-01-25  8:33 UTC (permalink / raw)
  To: peterz, alexei.starovoitov, acme
  Cc: linux-kernel, Wang Nan, He Kuang, Alexei Starovoitov,
	Arnaldo Carvalho de Melo, Brendan Gregg, Jiri Olsa,
	Masami Hiramatsu, Namhyung Kim, Zefan Li, pi3orama

Convert perf_output_begin to __perf_output_begin and make the later
function able to write records from the end of the ring buffer.
Following commits will utilize the 'backward' flag.

This patch doesn't introduce any extra performance overhead since we
use always_inline.

Signed-off-by: Wang Nan <wangnan0@huawei.com>
Cc: He Kuang <hekuang@huawei.com>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Zefan Li <lizefan@huawei.com>
Cc: pi3orama@163.com
---
 kernel/events/ring_buffer.c | 42 ++++++++++++++++++++++++++++++++++++------
 1 file changed, 36 insertions(+), 6 deletions(-)

diff --git a/kernel/events/ring_buffer.c b/kernel/events/ring_buffer.c
index 9f1a93f..0684e880 100644
--- a/kernel/events/ring_buffer.c
+++ b/kernel/events/ring_buffer.c
@@ -102,8 +102,21 @@ out:
 	preempt_enable();
 }
 
-int perf_output_begin(struct perf_output_handle *handle,
-		      struct perf_event *event, unsigned int size)
+static bool __always_inline
+ring_buffer_has_space(unsigned long head, unsigned long tail,
+		      unsigned long data_size, unsigned int size,
+		      bool backward)
+{
+	if (!backward)
+		return CIRC_SPACE(head, tail, data_size) >= size;
+	else
+		return CIRC_SPACE(tail, head, data_size) >= size;
+}
+
+static int __always_inline
+__perf_output_begin(struct perf_output_handle *handle,
+		    struct perf_event *event, unsigned int size,
+		    bool backward)
 {
 	struct ring_buffer *rb;
 	unsigned long tail, offset, head;
@@ -146,9 +159,12 @@ int perf_output_begin(struct perf_output_handle *handle,
 	do {
 		tail = READ_ONCE(rb->user_page->data_tail);
 		offset = head = local_read(&rb->head);
-		if (!rb->overwrite &&
-		    unlikely(CIRC_SPACE(head, tail, perf_data_size(rb)) < size))
-			goto fail;
+		if (!rb->overwrite) {
+			if (unlikely(!ring_buffer_has_space(head, tail,
+							    perf_data_size(rb),
+							    size, backward)))
+				goto fail;
+		}
 
 		/*
 		 * The above forms a control dependency barrier separating the
@@ -162,9 +178,17 @@ int perf_output_begin(struct perf_output_handle *handle,
 		 * See perf_output_put_handle().
 		 */
 
-		head += size;
+		if (!backward)
+			head += size;
+		else
+			head -= size;
 	} while (local_cmpxchg(&rb->head, offset, head) != offset);
 
+	if (backward) {
+		offset = head;
+		head = (u64)(-head);
+	}
+
 	/*
 	 * We rely on the implied barrier() by local_cmpxchg() to ensure
 	 * none of the data stores below can be lifted up by the compiler.
@@ -206,6 +230,12 @@ out:
 	return -ENOSPC;
 }
 
+int perf_output_begin(struct perf_output_handle *handle,
+		      struct perf_event *event, unsigned int size)
+{
+	return __perf_output_begin(handle, event, size, false);
+}
+
 unsigned int perf_output_copy(struct perf_output_handle *handle,
 		      const void *buf, unsigned int len)
 {
-- 
1.8.3.4

^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH 4/5] perf core: Add backward attribute to perf event
  2016-01-25  8:33 [PATCH 0/5] perf core: Read from overwrite ring buffer Wang Nan
                   ` (2 preceding siblings ...)
  2016-01-25  8:33 ` [PATCH 3/5] perf core: Prepare writing into ring buffer from end Wang Nan
@ 2016-01-25  8:33 ` Wang Nan
  2016-01-25  8:33 ` [PATCH 5/5] perf core: Reduce perf event output overhead by new overflow handler Wang Nan
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 16+ messages in thread
From: Wang Nan @ 2016-01-25  8:33 UTC (permalink / raw)
  To: peterz, alexei.starovoitov, acme
  Cc: linux-kernel, Wang Nan, He Kuang, Alexei Starovoitov,
	Arnaldo Carvalho de Melo, Brendan Gregg, Jiri Olsa,
	Masami Hiramatsu, Namhyung Kim, Zefan Li, pi3orama

In perf_event_attr a new bit 'write_backward' is appended to indicate
this event should write ring buffer from its end to beginning.

In perf_output_begin(), prepare ring buffer according this bit.

This patch introduces small overhead into perf_output_begin():
an extra memory read and a conditional branch. Further patch can remove
this overhead by using custom output handler.

Signed-off-by: Wang Nan <wangnan0@huawei.com>
Cc: He Kuang <hekuang@huawei.com>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Zefan Li <lizefan@huawei.com>
Cc: pi3orama@163.com
---
 include/linux/perf_event.h      | 5 +++++
 include/uapi/linux/perf_event.h | 3 ++-
 kernel/events/core.c            | 7 +++++++
 kernel/events/ring_buffer.c     | 2 ++
 4 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
index f9828a4..54c3fb2 100644
--- a/include/linux/perf_event.h
+++ b/include/linux/perf_event.h
@@ -1032,6 +1032,11 @@ static inline bool has_aux(struct perf_event *event)
 	return event->pmu->setup_aux;
 }
 
+static inline bool is_write_backward(struct perf_event *event)
+{
+	return !!event->attr.write_backward;
+}
+
 extern int perf_output_begin(struct perf_output_handle *handle,
 			     struct perf_event *event, unsigned int size);
 extern void perf_output_end(struct perf_output_handle *handle);
diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_event.h
index a3c1903..43fc8d2 100644
--- a/include/uapi/linux/perf_event.h
+++ b/include/uapi/linux/perf_event.h
@@ -340,7 +340,8 @@ struct perf_event_attr {
 				comm_exec      :  1, /* flag comm events that are due to an exec */
 				use_clockid    :  1, /* use @clockid for time fields */
 				context_switch :  1, /* context switch data */
-				__reserved_1   : 37;
+				write_backward :  1, /* Write ring buffer from end to beginning */
+				__reserved_1   : 36;
 
 	union {
 		__u32		wakeup_events;	  /* wakeup every n events */
diff --git a/kernel/events/core.c b/kernel/events/core.c
index f79c4be..8ad22a5 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -8107,6 +8107,13 @@ perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
 		goto out;
 
 	/*
+	 * Either writing ring buffer from beginning or from end.
+	 * Mixing is not allowed.
+	 */
+	if (is_write_backward(output_event) != is_write_backward(event))
+		goto out;
+
+	/*
 	 * If both events generate aux data, they must be on the same PMU
 	 */
 	if (has_aux(event) && has_aux(output_event) &&
diff --git a/kernel/events/ring_buffer.c b/kernel/events/ring_buffer.c
index 0684e880..28543e1 100644
--- a/kernel/events/ring_buffer.c
+++ b/kernel/events/ring_buffer.c
@@ -233,6 +233,8 @@ out:
 int perf_output_begin(struct perf_output_handle *handle,
 		      struct perf_event *event, unsigned int size)
 {
+	if (unlikely(is_write_backward(event)))
+		return __perf_output_begin(handle, event, size, true);
 	return __perf_output_begin(handle, event, size, false);
 }
 
-- 
1.8.3.4

^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH 5/5] perf core: Reduce perf event output overhead by new overflow handler
  2016-01-25  8:33 [PATCH 0/5] perf core: Read from overwrite ring buffer Wang Nan
                   ` (3 preceding siblings ...)
  2016-01-25  8:33 ` [PATCH 4/5] perf core: Add backward attribute to perf event Wang Nan
@ 2016-01-25  8:33 ` Wang Nan
  2016-01-26  0:24 ` [PATCH 0/5] perf core: Read from overwrite ring buffer Alexei Starovoitov
  2016-01-26  8:26 ` Wangnan (F)
  6 siblings, 0 replies; 16+ messages in thread
From: Wang Nan @ 2016-01-25  8:33 UTC (permalink / raw)
  To: peterz, alexei.starovoitov, acme
  Cc: linux-kernel, Wang Nan, He Kuang, Alexei Starovoitov,
	Arnaldo Carvalho de Melo, Brendan Gregg, Jiri Olsa,
	Masami Hiramatsu, Namhyung Kim, Zefan Li, pi3orama

By creating onward and backward specific overflow handlers and setting
them according to event's backward setting, normal sampling events
don't need checking backward setting of an event any more.

This is the last patch of backward writing patchset. After this patch,
there's no extra overhead introduced to the fast path of sampling
output.

Signed-off-by: Wang Nan <wangnan0@huawei.com>
Cc: He Kuang <hekuang@huawei.com>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Zefan Li <lizefan@huawei.com>
Cc: pi3orama@163.com
---
 include/linux/perf_event.h  | 17 +++++++++++++++--
 kernel/events/core.c        | 41 ++++++++++++++++++++++++++++++++++++-----
 kernel/events/ring_buffer.c | 12 ++++++++++++
 3 files changed, 63 insertions(+), 7 deletions(-)

diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
index 54c3fb2..c0335b9 100644
--- a/include/linux/perf_event.h
+++ b/include/linux/perf_event.h
@@ -830,9 +830,15 @@ extern int perf_event_overflow(struct perf_event *event,
 				 struct perf_sample_data *data,
 				 struct pt_regs *regs);
 
+extern void perf_event_output_onward(struct perf_event *event,
+				     struct perf_sample_data *data,
+				     struct pt_regs *regs);
+extern void perf_event_output_backward(struct perf_event *event,
+				       struct perf_sample_data *data,
+				       struct pt_regs *regs);
 extern void perf_event_output(struct perf_event *event,
-				struct perf_sample_data *data,
-				struct pt_regs *regs);
+			      struct perf_sample_data *data,
+			      struct pt_regs *regs);
 
 extern void
 perf_event_header__init_id(struct perf_event_header *header,
@@ -1039,6 +1045,13 @@ static inline bool is_write_backward(struct perf_event *event)
 
 extern int perf_output_begin(struct perf_output_handle *handle,
 			     struct perf_event *event, unsigned int size);
+extern int perf_output_begin_onward(struct perf_output_handle *handle,
+				    struct perf_event *event,
+				    unsigned int size);
+extern int perf_output_begin_backward(struct perf_output_handle *handle,
+				      struct perf_event *event,
+				      unsigned int size);
+
 extern void perf_output_end(struct perf_output_handle *handle);
 extern unsigned int perf_output_copy(struct perf_output_handle *handle,
 			     const void *buf, unsigned int len);
diff --git a/kernel/events/core.c b/kernel/events/core.c
index 8ad22a5..8a25e46 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -5541,9 +5541,13 @@ void perf_prepare_sample(struct perf_event_header *header,
 	}
 }
 
-void perf_event_output(struct perf_event *event,
-			struct perf_sample_data *data,
-			struct pt_regs *regs)
+static void __always_inline
+__perf_event_output(struct perf_event *event,
+		    struct perf_sample_data *data,
+		    struct pt_regs *regs,
+		    int (*output_begin)(struct perf_output_handle *,
+					struct perf_event *,
+					unsigned int))
 {
 	struct perf_output_handle handle;
 	struct perf_event_header header;
@@ -5553,7 +5557,7 @@ void perf_event_output(struct perf_event *event,
 
 	perf_prepare_sample(&header, data, event, regs);
 
-	if (perf_output_begin(&handle, event, header.size))
+	if (output_begin(&handle, event, header.size))
 		goto exit;
 
 	perf_output_sample(&handle, &header, data, event);
@@ -5564,6 +5568,30 @@ exit:
 	rcu_read_unlock();
 }
 
+void
+perf_event_output_onward(struct perf_event *event,
+			 struct perf_sample_data *data,
+			 struct pt_regs *regs)
+{
+	__perf_event_output(event, data, regs, perf_output_begin_onward);
+}
+
+void
+perf_event_output_backward(struct perf_event *event,
+			   struct perf_sample_data *data,
+			   struct pt_regs *regs)
+{
+	__perf_event_output(event, data, regs, perf_output_begin_backward);
+}
+
+void
+perf_event_output(struct perf_event *event,
+		  struct perf_sample_data *data,
+		  struct pt_regs *regs)
+{
+	__perf_event_output(event, data, regs, perf_output_begin);
+}
+
 /*
  * read event_id
  */
@@ -7874,8 +7902,11 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu,
 	if (overflow_handler) {
 		event->overflow_handler	= overflow_handler;
 		event->overflow_handler_context = context;
+	} else if (is_write_backward(event)){
+		event->overflow_handler = perf_event_output_backward;
+		event->overflow_handler_context = NULL;
 	} else {
-		event->overflow_handler = perf_event_output;
+		event->overflow_handler = perf_event_output_onward;
 		event->overflow_handler_context = NULL;
 	}
 
diff --git a/kernel/events/ring_buffer.c b/kernel/events/ring_buffer.c
index 28543e1..ca11809 100644
--- a/kernel/events/ring_buffer.c
+++ b/kernel/events/ring_buffer.c
@@ -230,6 +230,18 @@ out:
 	return -ENOSPC;
 }
 
+int perf_output_begin_onward(struct perf_output_handle *handle,
+			     struct perf_event *event, unsigned int size)
+{
+	return __perf_output_begin(handle, event, size, false);
+}
+
+int perf_output_begin_backward(struct perf_output_handle *handle,
+			       struct perf_event *event, unsigned int size)
+{
+	return __perf_output_begin(handle, event, size, true);
+}
+
 int perf_output_begin(struct perf_output_handle *handle,
 		      struct perf_event *event, unsigned int size)
 {
-- 
1.8.3.4

^ permalink raw reply related	[flat|nested] 16+ messages in thread

* Re: [PATCH 0/5] perf core: Read from overwrite ring buffer
  2016-01-25  8:33 [PATCH 0/5] perf core: Read from overwrite ring buffer Wang Nan
                   ` (4 preceding siblings ...)
  2016-01-25  8:33 ` [PATCH 5/5] perf core: Reduce perf event output overhead by new overflow handler Wang Nan
@ 2016-01-26  0:24 ` Alexei Starovoitov
  2016-01-26  8:26 ` Wangnan (F)
  6 siblings, 0 replies; 16+ messages in thread
From: Alexei Starovoitov @ 2016-01-26  0:24 UTC (permalink / raw)
  To: Wang Nan
  Cc: peterz, acme, linux-kernel, He Kuang, Alexei Starovoitov,
	Arnaldo Carvalho de Melo, Brendan Gregg, Jiri Olsa,
	Masami Hiramatsu, Namhyung Kim, Zefan Li, pi3orama

On Mon, Jan 25, 2016 at 08:33:48AM +0000, Wang Nan wrote:
> This is the v3 of this series. Compare with v2, tailsize method is
> removed, ioctl command PERF_EVENT_IOC_PAUSE_OUTPUT is changed to
> _IOW('$', 9, __u32) since it has an input value, commit message
> is slightly adjusted.
> 
> New test result on ARM64 is below (x86_64 result is copied from previous
> email, test method is described in previous version [1]):
> 
>                        x86_64        |       ARM64
>               -----------------------+---------------------
>                 MEAN         STDVAR  |    MEAN       STDVAR
>    BASE     :  879870.81   11913.13  |  808302.67   6951.47
>    RAWPERF  : 2603854.70  706658.40  | 3461675.12  54075.69
>    WRTBKWRD : 2313301.22    6727.96  | 3350177.98  23125.48
>    TAILSIZE : 2383051.86    5248.06  | 3556496.56  24802.17
>    RAWOVWRT : 2315273.18    5221.03  | 3458767.49  41560.32
>    RAWOVWRT*: 2323970.45    5103.39  | 3438967.06  34095.83
> 
> ARM64 platform is a smartphone with 8 Hisilicon arm64 cores
> (big/little).
> 
> Benckmarking result on both architechure support removing tailsize
> method because its performance penalty is higher than WRTBKWRD. Also, it
> seems we don't need to consider tailheader method (putting whole header
> at the end of records, kernel write to the ring buffer from beginning to
> end), because the above result shows WRTBKWRD and RAWOVWRT are similar,
> and tailheader is impossible to outperform RAWOVWRT in theory.

looks ok to me.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 0/5] perf core: Read from overwrite ring buffer
  2016-01-25  8:33 [PATCH 0/5] perf core: Read from overwrite ring buffer Wang Nan
                   ` (5 preceding siblings ...)
  2016-01-26  0:24 ` [PATCH 0/5] perf core: Read from overwrite ring buffer Alexei Starovoitov
@ 2016-01-26  8:26 ` Wangnan (F)
  6 siblings, 0 replies; 16+ messages in thread
From: Wangnan (F) @ 2016-01-26  8:26 UTC (permalink / raw)
  To: peterz
  Cc: alexei.starovoitov, acme, linux-kernel, He Kuang,
	Alexei Starovoitov, Arnaldo Carvalho de Melo, Brendan Gregg,
	Jiri Olsa, Masami Hiramatsu, Namhyung Kim, Zefan Li, pi3orama

Hi Peter,

Do you have any further comments about this patchset? What should be
the correct route for them to be merged into mainline?

Thank you.

On 2016/1/25 16:33, Wang Nan wrote:
> This is the v3 of this series. Compare with v2, tailsize method is
> removed, ioctl command PERF_EVENT_IOC_PAUSE_OUTPUT is changed to
> _IOW('$', 9, __u32) since it has an input value, commit message
> is slightly adjusted.
>
> New test result on ARM64 is below (x86_64 result is copied from previous
> email, test method is described in previous version [1]):
>
>                         x86_64        |       ARM64
>                -----------------------+---------------------
>                  MEAN         STDVAR  |    MEAN       STDVAR
>     BASE     :  879870.81   11913.13  |  808302.67   6951.47
>     RAWPERF  : 2603854.70  706658.40  | 3461675.12  54075.69
>     WRTBKWRD : 2313301.22    6727.96  | 3350177.98  23125.48
>     TAILSIZE : 2383051.86    5248.06  | 3556496.56  24802.17
>     RAWOVWRT : 2315273.18    5221.03  | 3458767.49  41560.32
>     RAWOVWRT*: 2323970.45    5103.39  | 3438967.06  34095.83
>
> ARM64 platform is a smartphone with 8 Hisilicon arm64 cores
> (big/little).
>
> Benckmarking result on both architechure support removing tailsize
> method because its performance penalty is higher than WRTBKWRD. Also, it
> seems we don't need to consider tailheader method (putting whole header
> at the end of records, kernel write to the ring buffer from beginning to
> end), because the above result shows WRTBKWRD and RAWOVWRT are similar,
> and tailheader is impossible to outperform RAWOVWRT in theory.
>
> [1] http://lkml.kernel.org/g/1453464834-233200-1-git-send-email-wangnan0@huawei.com
>
> Wang Nan (5):
>    perf core: Introduce new ioctl options to pause and resume ring buffer
>    perf core: Set event's default overflow_handler
>    perf core: Prepare writing into ring buffer from end
>    perf core: Add backward attribute to perf event
>    perf core: Reduce perf event output overhead by new overflow handler
>
>   include/linux/perf_event.h      | 22 +++++++++++--
>   include/uapi/linux/perf_event.h |  4 ++-
>   kernel/events/core.c            | 73 +++++++++++++++++++++++++++++++++++------
>   kernel/events/internal.h        | 11 +++++++
>   kernel/events/ring_buffer.c     | 63 +++++++++++++++++++++++++++++++----
>   5 files changed, 153 insertions(+), 20 deletions(-)
>
> Signed-off-by: Wang Nan <wangnan0@huawei.com>
> Cc: He Kuang <hekuang@huawei.com>
> Cc: Alexei Starovoitov <ast@kernel.org>
> Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
> Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
> Cc: Jiri Olsa <jolsa@kernel.org>
> Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
> Cc: Namhyung Kim <namhyung@kernel.org>
> Cc: Peter Zijlstra <peterz@infradead.org>
> Cc: Zefan Li <lizefan@huawei.com>
> Cc: pi3orama@163.com
>

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 1/5] perf core: Introduce new ioctl options to pause and resume ring buffer
  2016-10-21  7:13         ` Wangnan (F)
@ 2016-10-21  8:55           ` Michael Kerrisk (man-pages)
  0 siblings, 0 replies; 16+ messages in thread
From: Michael Kerrisk (man-pages) @ 2016-10-21  8:55 UTC (permalink / raw)
  To: Wangnan (F), Peter Zijlstra
  Cc: mtk.manpages, mingo, linux-kernel, He Kuang, Alexei Starovoitov,
	Arnaldo Carvalho de Melo, Brendan Gregg, Jiri Olsa,
	Masami Hiramatsu, Namhyung Kim, Zefan Li, pi3orama, Vince Weaver

Hi Wangnan

On 10/21/2016 09:13 AM, Wangnan (F) wrote:
> 
> 
> On 2016/10/21 15:06, Michael Kerrisk (man-pages) wrote:
>> Hello Wangnan,
>>
>> The patch below seems to have landed in Linux 4.7,
>> commit 86e7972f690c1017fd086cdfe53d8524e68c661c
>>
>> Could you draft a man-pages patch for this interface
>> change, please? Or, failing that, a plain-text
>> description that we can integrate into the man-page.
> 
> I sent man-pages patches at March:
> 
> https://patchwork.kernel.org/patch/8678861/
> http://www.spinics.net/lists/linux-man/msg10177.html
> 
> Let me resend them again.


Ahhh -- my apologies. I found these now.

Cheers,

Michael



-- 
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 1/5] perf core: Introduce new ioctl options to pause and resume ring buffer
  2016-10-21  7:06       ` Michael Kerrisk (man-pages)
@ 2016-10-21  7:13         ` Wangnan (F)
  2016-10-21  8:55           ` Michael Kerrisk (man-pages)
  0 siblings, 1 reply; 16+ messages in thread
From: Wangnan (F) @ 2016-10-21  7:13 UTC (permalink / raw)
  To: Michael Kerrisk (man-pages), Peter Zijlstra
  Cc: mingo, linux-kernel, He Kuang, Alexei Starovoitov,
	Arnaldo Carvalho de Melo, Brendan Gregg, Jiri Olsa,
	Masami Hiramatsu, Namhyung Kim, Zefan Li, pi3orama, Vince Weaver



On 2016/10/21 15:06, Michael Kerrisk (man-pages) wrote:
> Hello Wangnan,
>
> The patch below seems to have landed in Linux 4.7,
> commit 86e7972f690c1017fd086cdfe53d8524e68c661c
>
> Could you draft a man-pages patch for this interface
> change, please? Or, failing that, a plain-text
> description that we can integrate into the man-page.

I sent man-pages patches at March:

https://patchwork.kernel.org/patch/8678861/
http://www.spinics.net/lists/linux-man/msg10177.html

Let me resend them again.

Thank you.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 1/5] perf core: Introduce new ioctl options to pause and resume ring buffer
  2016-03-23  9:33     ` Wangnan (F)
  2016-03-23  9:52       ` Peter Zijlstra
  2016-03-23 12:43       ` Vince Weaver
@ 2016-10-21  7:06       ` Michael Kerrisk (man-pages)
  2016-10-21  7:13         ` Wangnan (F)
  2 siblings, 1 reply; 16+ messages in thread
From: Michael Kerrisk (man-pages) @ 2016-10-21  7:06 UTC (permalink / raw)
  To: Wangnan (F), Peter Zijlstra
  Cc: mtk.manpages, mingo, linux-kernel, He Kuang, Alexei Starovoitov,
	Arnaldo Carvalho de Melo, Brendan Gregg, Jiri Olsa,
	Masami Hiramatsu, Namhyung Kim, Zefan Li, pi3orama, Vince Weaver

Hello Wangnan,

The patch below seems to have landed in Linux 4.7,
commit 86e7972f690c1017fd086cdfe53d8524e68c661c

Could you draft a man-pages patch for this interface
change, please? Or, failing that, a plain-text 
description that we can integrate into the man-page.

Thanks,

Michael

On 03/23/2016 10:33 AM, Wangnan (F) wrote:
> 
> 
> On 2016/3/23 17:16, Peter Zijlstra wrote:
>> On Mon, Mar 14, 2016 at 09:59:41AM +0000, Wang Nan wrote:
>>> Add new ioctl() to pause/resume ring-buffer output.
>>>
>>> In some situations we want to read from ring buffer only when we
>>> ensure nothing can write to the ring buffer during reading. Without
>>> this patch we have to turn off all events attached to this ring buffer
>>> to achieve this.
>>>
>>> This patch is for supporting overwrite ring buffer. Following
>>> commits will introduce new methods support reading from overwrite ring
>>> buffer. Before reading caller must ensure the ring buffer is frozen, or
>>> the reading is unreliable.
>>> diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_event.h
>>> index 1afe962..a3c1903 100644
>>> --- a/include/uapi/linux/perf_event.h
>>> +++ b/include/uapi/linux/perf_event.h
>>> @@ -401,6 +401,7 @@ struct perf_event_attr {
>>>   #define PERF_EVENT_IOC_SET_FILTER	_IOW('$', 6, char *)
>>>   #define PERF_EVENT_IOC_ID		_IOR('$', 7, __u64 *)
>>>   #define PERF_EVENT_IOC_SET_BPF		_IOW('$', 8, __u32)
>>> +#define PERF_EVENT_IOC_PAUSE_OUTPUT	_IOW('$', 9, __u32)
> 
> Glad to see you start to look at this patchset.
> 
> 
> 
>> Can you also do a patch to the man-pages?
>>
>>    http://man7.org/linux/man-pages/man2/perf_event_open.2.html
> 
> Sure.
> 
> I think I need to provide a patch for:
> 
>   http://git.kernel.org/cgit/docs/man-pages/man-pages.git
> 
> But which one should be the first? Shall we update man pages before
> this patch be merged by upstream? Or Michael and Vince will consider
> this problem?
> 
> Thank you.
> 
> 
> 


-- 
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 1/5] perf core: Introduce new ioctl options to pause and resume ring buffer
  2016-03-23  9:33     ` Wangnan (F)
  2016-03-23  9:52       ` Peter Zijlstra
@ 2016-03-23 12:43       ` Vince Weaver
  2016-10-21  7:06       ` Michael Kerrisk (man-pages)
  2 siblings, 0 replies; 16+ messages in thread
From: Vince Weaver @ 2016-03-23 12:43 UTC (permalink / raw)
  To: Wangnan (F)
  Cc: Peter Zijlstra, mingo, linux-kernel, He Kuang,
	Alexei Starovoitov, Arnaldo Carvalho de Melo, Brendan Gregg,
	Jiri Olsa, Masami Hiramatsu, Namhyung Kim, Zefan Li, pi3orama,
	Michael Kerrisk (man-pages)

On Wed, 23 Mar 2016, Wangnan (F) wrote:

> 
> > Can you also do a patch to the man-pages?
> > 
> >    http://man7.org/linux/man-pages/man2/perf_event_open.2.html
> 
> Sure.
> 
> I think I need to provide a patch for:
> 
>  http://git.kernel.org/cgit/docs/man-pages/man-pages.git
> 
> But which one should be the first? Shall we update man pages before
> this patch be merged by upstream? Or Michael and Vince will consider
> this problem?

It is good to see a rough draft of the manpage changes for an ABI 
addition like this before the patch gets merged just so we can catch 
anything odd about the interface.

Also include (as a comment) the git commit id that introduces the change.

In general manpage patches do not get committed until after the change
hits a full released kernel (as things have been known to get reverted 
from -rc kernels in the past).

If you send a rough patch I can fix it up and queue it up with other 
manpage patches I have (I'm a bit backlogged but working on catching up).
I'm never quite sure that I get all of the various Signed-off- lines right 
for contributed patches though.

Vince

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 1/5] perf core: Introduce new ioctl options to pause and resume ring buffer
  2016-03-23  9:33     ` Wangnan (F)
@ 2016-03-23  9:52       ` Peter Zijlstra
  2016-03-23 12:43       ` Vince Weaver
  2016-10-21  7:06       ` Michael Kerrisk (man-pages)
  2 siblings, 0 replies; 16+ messages in thread
From: Peter Zijlstra @ 2016-03-23  9:52 UTC (permalink / raw)
  To: Wangnan (F)
  Cc: mingo, linux-kernel, He Kuang, Alexei Starovoitov,
	Arnaldo Carvalho de Melo, Brendan Gregg, Jiri Olsa,
	Masami Hiramatsu, Namhyung Kim, Zefan Li, pi3orama, Vince Weaver,
	Michael Kerrisk (man-pages)

On Wed, Mar 23, 2016 at 05:33:53PM +0800, Wangnan (F) wrote:
> Glad to see you start to look at this patchset.

My brain is completely fried from staring at fuzzer output for weeks, I
just need to do _something_, _anything_ else for a while :-)

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 1/5] perf core: Introduce new ioctl options to pause and resume ring buffer
  2016-03-23  9:16   ` Peter Zijlstra
@ 2016-03-23  9:33     ` Wangnan (F)
  2016-03-23  9:52       ` Peter Zijlstra
                         ` (2 more replies)
  0 siblings, 3 replies; 16+ messages in thread
From: Wangnan (F) @ 2016-03-23  9:33 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: mingo, linux-kernel, He Kuang, Alexei Starovoitov,
	Arnaldo Carvalho de Melo, Brendan Gregg, Jiri Olsa,
	Masami Hiramatsu, Namhyung Kim, Zefan Li, pi3orama, Vince Weaver,
	Michael Kerrisk (man-pages)



On 2016/3/23 17:16, Peter Zijlstra wrote:
> On Mon, Mar 14, 2016 at 09:59:41AM +0000, Wang Nan wrote:
>> Add new ioctl() to pause/resume ring-buffer output.
>>
>> In some situations we want to read from ring buffer only when we
>> ensure nothing can write to the ring buffer during reading. Without
>> this patch we have to turn off all events attached to this ring buffer
>> to achieve this.
>>
>> This patch is for supporting overwrite ring buffer. Following
>> commits will introduce new methods support reading from overwrite ring
>> buffer. Before reading caller must ensure the ring buffer is frozen, or
>> the reading is unreliable.
>> diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_event.h
>> index 1afe962..a3c1903 100644
>> --- a/include/uapi/linux/perf_event.h
>> +++ b/include/uapi/linux/perf_event.h
>> @@ -401,6 +401,7 @@ struct perf_event_attr {
>>   #define PERF_EVENT_IOC_SET_FILTER	_IOW('$', 6, char *)
>>   #define PERF_EVENT_IOC_ID		_IOR('$', 7, __u64 *)
>>   #define PERF_EVENT_IOC_SET_BPF		_IOW('$', 8, __u32)
>> +#define PERF_EVENT_IOC_PAUSE_OUTPUT	_IOW('$', 9, __u32)

Glad to see you start to look at this patchset.



> Can you also do a patch to the man-pages?
>
>    http://man7.org/linux/man-pages/man2/perf_event_open.2.html

Sure.

I think I need to provide a patch for:

  http://git.kernel.org/cgit/docs/man-pages/man-pages.git

But which one should be the first? Shall we update man pages before
this patch be merged by upstream? Or Michael and Vince will consider
this problem?

Thank you.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 1/5] perf core: Introduce new ioctl options to pause and resume ring buffer
  2016-03-14  9:59 ` [PATCH 1/5] perf core: Introduce new ioctl options to pause and resume " Wang Nan
@ 2016-03-23  9:16   ` Peter Zijlstra
  2016-03-23  9:33     ` Wangnan (F)
  0 siblings, 1 reply; 16+ messages in thread
From: Peter Zijlstra @ 2016-03-23  9:16 UTC (permalink / raw)
  To: Wang Nan
  Cc: mingo, linux-kernel, He Kuang, Alexei Starovoitov,
	Arnaldo Carvalho de Melo, Brendan Gregg, Jiri Olsa,
	Masami Hiramatsu, Namhyung Kim, Zefan Li, pi3orama, Vince Weaver,
	Michael Kerrisk (man-pages)

On Mon, Mar 14, 2016 at 09:59:41AM +0000, Wang Nan wrote:
> Add new ioctl() to pause/resume ring-buffer output.
> 
> In some situations we want to read from ring buffer only when we
> ensure nothing can write to the ring buffer during reading. Without
> this patch we have to turn off all events attached to this ring buffer
> to achieve this.
> 
> This patch is for supporting overwrite ring buffer. Following
> commits will introduce new methods support reading from overwrite ring
> buffer. Before reading caller must ensure the ring buffer is frozen, or
> the reading is unreliable.

> diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_event.h
> index 1afe962..a3c1903 100644
> --- a/include/uapi/linux/perf_event.h
> +++ b/include/uapi/linux/perf_event.h
> @@ -401,6 +401,7 @@ struct perf_event_attr {
>  #define PERF_EVENT_IOC_SET_FILTER	_IOW('$', 6, char *)
>  #define PERF_EVENT_IOC_ID		_IOR('$', 7, __u64 *)
>  #define PERF_EVENT_IOC_SET_BPF		_IOW('$', 8, __u32)
> +#define PERF_EVENT_IOC_PAUSE_OUTPUT	_IOW('$', 9, __u32)

Can you also do a patch to the man-pages? 

  http://man7.org/linux/man-pages/man2/perf_event_open.2.html

Michael and Vince (who has so far been stellar in composing all that)
might be able to point you to the 'right' resources for that.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH 1/5] perf core: Introduce new ioctl options to pause and resume ring buffer
  2016-03-14  9:59 [PATCH 0/5] perf core: Support " Wang Nan
@ 2016-03-14  9:59 ` Wang Nan
  2016-03-23  9:16   ` Peter Zijlstra
  0 siblings, 1 reply; 16+ messages in thread
From: Wang Nan @ 2016-03-14  9:59 UTC (permalink / raw)
  To: peterz, mingo
  Cc: linux-kernel, Wang Nan, He Kuang, Alexei Starovoitov,
	Arnaldo Carvalho de Melo, Brendan Gregg, Jiri Olsa,
	Masami Hiramatsu, Namhyung Kim, Zefan Li, pi3orama

Add new ioctl() to pause/resume ring-buffer output.

In some situations we want to read from ring buffer only when we
ensure nothing can write to the ring buffer during reading. Without
this patch we have to turn off all events attached to this ring buffer
to achieve this.

This patch is for supporting overwrite ring buffer. Following
commits will introduce new methods support reading from overwrite ring
buffer. Before reading caller must ensure the ring buffer is frozen, or
the reading is unreliable.

Signed-off-by: Wang Nan <wangnan0@huawei.com>
Cc: He Kuang <hekuang@huawei.com>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Zefan Li <lizefan@huawei.com>
Cc: pi3orama@163.com
---
 include/uapi/linux/perf_event.h |  1 +
 kernel/events/core.c            | 13 +++++++++++++
 kernel/events/internal.h        | 11 +++++++++++
 kernel/events/ring_buffer.c     |  7 ++++++-
 4 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_event.h
index 1afe962..a3c1903 100644
--- a/include/uapi/linux/perf_event.h
+++ b/include/uapi/linux/perf_event.h
@@ -401,6 +401,7 @@ struct perf_event_attr {
 #define PERF_EVENT_IOC_SET_FILTER	_IOW('$', 6, char *)
 #define PERF_EVENT_IOC_ID		_IOR('$', 7, __u64 *)
 #define PERF_EVENT_IOC_SET_BPF		_IOW('$', 8, __u32)
+#define PERF_EVENT_IOC_PAUSE_OUTPUT	_IOW('$', 9, __u32)
 
 enum perf_event_ioc_flags {
 	PERF_IOC_FLAG_GROUP		= 1U << 0,
diff --git a/kernel/events/core.c b/kernel/events/core.c
index b723149..1a1312e 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -4306,6 +4306,19 @@ static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned lon
 	case PERF_EVENT_IOC_SET_BPF:
 		return perf_event_set_bpf_prog(event, arg);
 
+	case PERF_EVENT_IOC_PAUSE_OUTPUT: {
+		struct ring_buffer *rb;
+
+		rcu_read_lock();
+		rb = rcu_dereference(event->rb);
+		if (!event->rb) {
+			rcu_read_unlock();
+			return -EINVAL;
+		}
+		rb_toggle_paused(rb, !!arg);
+		rcu_read_unlock();
+		return 0;
+	}
 	default:
 		return -ENOTTY;
 	}
diff --git a/kernel/events/internal.h b/kernel/events/internal.h
index 2bbad9c..6a93d1b 100644
--- a/kernel/events/internal.h
+++ b/kernel/events/internal.h
@@ -18,6 +18,7 @@ struct ring_buffer {
 #endif
 	int				nr_pages;	/* nr of data pages  */
 	int				overwrite;	/* can overwrite itself */
+	int				paused;		/* can write into ring buffer */
 
 	atomic_t			poll;		/* POLL_ for wakeups */
 
@@ -65,6 +66,16 @@ static inline void rb_free_rcu(struct rcu_head *rcu_head)
 	rb_free(rb);
 }
 
+static inline void
+rb_toggle_paused(struct ring_buffer *rb,
+		 bool pause)
+{
+	if (!pause && rb->nr_pages)
+		rb->paused = 0;
+	else
+		rb->paused = 1;
+}
+
 extern struct ring_buffer *
 rb_alloc(int nr_pages, long watermark, int cpu, int flags);
 extern void perf_event_wakeup(struct perf_event *event);
diff --git a/kernel/events/ring_buffer.c b/kernel/events/ring_buffer.c
index 1faad2c..22e1a47 100644
--- a/kernel/events/ring_buffer.c
+++ b/kernel/events/ring_buffer.c
@@ -125,8 +125,11 @@ int perf_output_begin(struct perf_output_handle *handle,
 	if (unlikely(!rb))
 		goto out;
 
-	if (unlikely(!rb->nr_pages))
+	if (unlikely(rb->paused)) {
+		if (rb->nr_pages)
+			local_inc(&rb->lost);
 		goto out;
+	}
 
 	handle->rb    = rb;
 	handle->event = event;
@@ -244,6 +247,8 @@ ring_buffer_init(struct ring_buffer *rb, long watermark, int flags)
 	INIT_LIST_HEAD(&rb->event_list);
 	spin_lock_init(&rb->event_lock);
 	init_irq_work(&rb->irq_work, rb_irq_work);
+
+	rb->paused = rb->nr_pages ? 0 : 1;
 }
 
 static void ring_buffer_put_async(struct ring_buffer *rb)
-- 
1.8.3.4

^ permalink raw reply related	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2016-10-21  8:56 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-01-25  8:33 [PATCH 0/5] perf core: Read from overwrite ring buffer Wang Nan
2016-01-25  8:33 ` [PATCH 1/5] perf core: Introduce new ioctl options to pause and resume " Wang Nan
2016-01-25  8:33 ` [PATCH 2/5] perf core: Set event's default overflow_handler Wang Nan
2016-01-25  8:33 ` [PATCH 3/5] perf core: Prepare writing into ring buffer from end Wang Nan
2016-01-25  8:33 ` [PATCH 4/5] perf core: Add backward attribute to perf event Wang Nan
2016-01-25  8:33 ` [PATCH 5/5] perf core: Reduce perf event output overhead by new overflow handler Wang Nan
2016-01-26  0:24 ` [PATCH 0/5] perf core: Read from overwrite ring buffer Alexei Starovoitov
2016-01-26  8:26 ` Wangnan (F)
2016-03-14  9:59 [PATCH 0/5] perf core: Support " Wang Nan
2016-03-14  9:59 ` [PATCH 1/5] perf core: Introduce new ioctl options to pause and resume " Wang Nan
2016-03-23  9:16   ` Peter Zijlstra
2016-03-23  9:33     ` Wangnan (F)
2016-03-23  9:52       ` Peter Zijlstra
2016-03-23 12:43       ` Vince Weaver
2016-10-21  7:06       ` Michael Kerrisk (man-pages)
2016-10-21  7:13         ` Wangnan (F)
2016-10-21  8:55           ` Michael Kerrisk (man-pages)

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.