Skip to contentSkip to conversation

perf: Split up buffer handling from core code

RFC PATCH In review Frederic Weisbecker 4 participants 15 messages May 19, 2011 – Jun 9, 2011 perf events

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 perf: Split up buffer handling from core code

Keeping perf_output_copy() inlined is responsible
of some ugliness there.

A solution could be to have an internal only
perf_output_copy_inline() version in internal.h
and a wrapper in buffer.c that exports it.

Or we can completely uninline it.

Cc Ingo Molnar <[email protected]>
Cc Stephane Eranian <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 include/linux/perf_event.h |   47 +++++-
 kernel/events/Makefile     |    2 +-
 kernel/events/buffer.c     |  394 +++++++++++++++++++++++++++++++++++++++
 kernel/events/core.c       |  442 +-------------------------------------------
 kernel/events/internal.h   |   28 +++
 5 files changed, 476 insertions(+), 437 deletions(-)
 create mode 100644 kernel/events/buffer.c
 create mode 100644 kernel/events/internal.h
include/linux/perf_event.h +45 -2
index 3412684..02952d4 100644
@@ -1142,12 +1142,55 @@ extern void perf_bp_event(struct perf_event *event, void *data);
 # define perf_instruction_pointer(regs)	instruction_pointer(regs)
 #endif
 
+#ifdef CONFIG_PERF_USE_VMALLOC
+/*
+ * Back perf_mmap() with vmalloc memory.
+ *
+ * Required for architectures that have d-cache aliasing issues.
+ */
+
+static inline int perf_output_page_order(struct perf_buffer *buffer)
+{
+	return buffer->page_order;
+}
+
+#else
+
+static inline int perf_output_page_order(struct perf_buffer *buffer)
+{
+	return 0;
+}
+#endif
+
 extern int perf_output_begin(struct perf_output_handle *handle,
 			     struct perf_event *event, unsigned int size,
 			     int nmi, int sample);
 extern void perf_output_end(struct perf_output_handle *handle);
-extern void perf_output_copy(struct perf_output_handle *handle,
-			     const void *buf, unsigned int len);
+
+static inline void
+perf_output_copy(struct perf_output_handle *handle,
+		 const void *buf, unsigned int len)
+{
+	do {
+		unsigned long size = min_t(unsigned long, handle->size, len);
+
+		memcpy(handle->addr, buf, size);
+
+		len -= size;
+		handle->addr += size;
+		buf += size;
+		handle->size -= size;
+		if (!handle->size) {
+			struct perf_buffer *buffer = handle->buffer;
+
+			handle->page++;
+			handle->page &= buffer->nr_pages - 1;
+			handle->addr = buffer->data_pages[handle->page];
+			handle->size = PAGE_SIZE << perf_output_page_order(buffer);
+		}
+	} while (len);
+}
+
 extern int perf_swevent_get_recursion_context(void);
 extern void perf_swevent_put_recursion_context(int rctx);
 extern void perf_event_enable(struct perf_event *event);
kernel/events/Makefile +1 -1
index 1ce23d3..b66b057 100644
@@ -2,5 +2,5 @@ ifdef CONFIG_FUNCTION_TRACER
 CFLAGS_REMOVE_core.o = -pg
 endif
 
-obj-y := core.o
+obj-y := core.o buffer.o
 obj-$(CONFIG_HAVE_HW_BREAKPOINT) += hw_breakpoint.o
kernel/events/buffer.c new file +394 -0

394 lines not on this page · raw message

kernel/events/core.c +8 -434

477 lines not on this page · raw message

kernel/events/internal.h new file +28 -0
new file mode 100644
index 0000000..2645cb9
@@ -0,0 +1,28 @@
+#ifndef _LINUX_KERNEL_EVENTS_H
+#define _LINUX_KERNEL_EVENTS_H
+
+#include <linux/perf_event.h>
+
+extern void perf_buffer_free(struct perf_buffer *buffer);
+extern struct perf_buffer *
+perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags);
+extern void perf_event_wakeup(struct perf_event *event);
+
+extern void
+perf_event_header__init_id(struct perf_event_header *header,
+			   struct perf_sample_data *data,
+			   struct perf_event *event);
+extern void
+perf_event__output_id_sample(struct perf_event *event,
+			     struct perf_output_handle *handle,
+			     struct perf_sample_data *sample);
+
+extern struct page *
+perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff);
+
+static unsigned long perf_data_size(struct perf_buffer *buffer)
+{
+	return buffer->nr_pages << (PAGE_SHIFT + perf_output_page_order(buffer));
+}
+
+#endif /* _LINUX_KERNEL_EVENTS_H */
Signature · 1 line
1.7.3.2
#1 lore raw

(Adding Borislav in Cc)

Quoted 13 lines · Frederic Weisbecker

Keeping perf_output_copy() inlined is responsible of some ugliness there.

A solution could be to have an internal only perf_output_copy_inline() version in internal.h and a wrapper in buffer.c that exports it.

Or we can completely uninline it.

Cc Ingo Molnar <[email protected]>
Cc Stephane Eranian <[email protected]>
Cc Peter Zijlstra <[email protected]>
#2 lore raw
In reply to Frederic Weisbecker
Quoted 71 lines · Frederic Weisbecker

Keeping perf_output_copy() inlined is responsible of some ugliness there.

A solution could be to have an internal only perf_output_copy_inline() version in internal.h and a wrapper in buffer.c that exports it.

Or we can completely uninline it.

Cc Ingo Molnar <[email protected]>
Cc Stephane Eranian <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 include/linux/perf_event.h |   47 +++++-
 kernel/events/Makefile     |    2 +-
 kernel/events/buffer.c     |  394 +++++++++++++++++++++++++++++++++++++++
 kernel/events/core.c       |  442 +-------------------------------------------
 kernel/events/internal.h   |   28 +++
 5 files changed, 476 insertions(+), 437 deletions(-)
 create mode 100644 kernel/events/buffer.c
 create mode 100644 kernel/events/internal.h
include/linux/perf_event.h +34 -2
index 3412684..02952d4 100644
@@ -1142,12 +1142,55 @@ extern void perf_bp_event(struct perf_event *event, void *data);
 # define perf_instruction_pointer(regs)	instruction_pointer(regs)
 #endif
 
+#ifdef CONFIG_PERF_USE_VMALLOC
+/*
+ * Back perf_mmap() with vmalloc memory.
+ *
+ * Required for architectures that have d-cache aliasing issues.
+ */
+
+static inline int perf_output_page_order(struct perf_buffer *buffer)
+{
+	return buffer->page_order;
+}
+
+#else
+
+static inline int perf_output_page_order(struct perf_buffer *buffer)
+{
+	return 0;
+}
+#endif
+
 extern int perf_output_begin(struct perf_output_handle *handle,
 			     struct perf_event *event, unsigned int size,
 			     int nmi, int sample);
 extern void perf_output_end(struct perf_output_handle *handle);
-extern void perf_output_copy(struct perf_output_handle *handle,
-			     const void *buf, unsigned int len);
+
+static inline void
+perf_output_copy(struct perf_output_handle *handle,
+		 const void *buf, unsigned int len)
+{
+	do {
+		unsigned long size = min_t(unsigned long, handle->size, len);
+
+		memcpy(handle->addr, buf, size);
+
+		len -= size;
+		handle->addr += size;
+		buf += size;
+		handle->size -= size;

include/linux/perf_event.h extern void perf_bp_event(struct perf_event *event, void *data); · line 1192

Quoted 10 lines
include/linux/perf_event.h +10 -0
+		if (!handle->size) {
+			struct perf_buffer *buffer = handle->buffer;
+
+			handle->page++;
+			handle->page &= buffer->nr_pages - 1;
+			handle->addr = buffer->data_pages[handle->page];
+			handle->size = PAGE_SIZE << perf_output_page_order(buffer);
+		}
+	} while (len);
+}

Ok, maybe I'm missing something looking at this from the side but perf_output_copy is used only internally by perf_event.c, i.e. events/core.c, why not put it into the internal.h header?

The rest looks ok.

Thanks.

Signature · 2 lines
Regards/Gruss,
    Boris.
#3 lore raw
Quoted 4 lines · Borislav Petkov

Ok, maybe I'm missing something looking at this from the side but perf_output_copy is used only internally by perf_event.c, i.e. events/core.c, why not put it into the internal.h header?

Not strictly true, but since the current inline is only valid for the existing perf_event.c its close enough :-)

Anyway, I agree, stick it in internal.h :-)

#4 lore raw
Quoted 10 lines · Peter Zijlstra
Quoted 4 lines · Borislav Petkov

Ok, maybe I'm missing something looking at this from the side but perf_output_copy is used only internally by perf_event.c, i.e. events/core.c, why not put it into the internal.h header?

Not strictly true, but since the current inline is only valid for the existing perf_event.c its close enough :-)

Anyway, I agree, stick it in internal.h :-)

Great. I kept it global because I read in a review from you that you wanted it to stay a visible API. But if you're fine with it internal, let's move it there.

Thanks.

#5 lore raw
Quoted 4 lines · Frederic Weisbecker

Great. I kept it global because I read in a review from you that you wanted it to stay a visible API. But if you're fine with it internal, let's move it there.

Well, its needed as an external symbol, but the inline isn't needed.

I thought that the BTS code used it as well, but apparently that code doesn't anymore (or never did any my memory if fuzzy).

#6 lore raw
Quoted 7 lines · Peter Zijlstra
Quoted 4 lines · Frederic Weisbecker

Great. I kept it global because I read in a review from you that you wanted it to stay a visible API. But if you're fine with it internal, let's move it there.

Well, its needed as an external symbol, but the inline isn't needed.

I thought it would be nice to keep it inline for core use, as it's called pretty often from the overflow path.

A solution is to have a perf_output_copy_inline() in internal.h and export it through perf_output_copy() in buffer.c

Quoted 2 lines

I thought that the BTS code used it as well, but apparently that code doesn't anymore (or never did any my memory if fuzzy).

I thought it did too, but it actually use perf_output_sample().

#7 lore raw
Quoted 2 lines · Frederic Weisbecker

I thought it would be nice to keep it inline for core use, as it's called pretty often from the overflow path.

That's what I meant.

Quoted 2 lines

A solution is to have a perf_output_copy_inline() in internal.h and export it through perf_output_copy() in buffer.c

If only "external inline" would work :-)

I'd call it __perf_output_copy() though, less typing.

#8 lore raw
Quoted 10 lines · Peter Zijlstra
Quoted 2 lines · Frederic Weisbecker

I thought it would be nice to keep it inline for core use, as it's called pretty often from the overflow path.

That's what I meant.

Quoted 2 lines

A solution is to have a perf_output_copy_inline() in internal.h and export it through perf_output_copy() in buffer.c

If only "external inline" would work :-)

Hehe :)

Quoted 1 line

I'd call it __perf_output_copy() though, less typing.

Fine!

#9 lore raw
In reply to Peter Zijlstra
Quoted 4 lines · Peter Zijlstra
Quoted 2 lines · Frederic Weisbecker

I thought it would be nice to keep it inline for core use, as it's called pretty often from the overflow path.

Quoted 8 lines

That's what I meant.

Quoted 2 lines

A solution is to have a perf_output_copy_inline() in internal.h and export it through perf_output_copy() in buffer.c

If only "external inline" would work :-)

I'd call it __perf_output_copy() though, less typing.

Just __output_copy() please - if someone is looking at kernel/events/*.c then it's pretty clear that it's related to perf.

I.e. non-exposed symbols should lose their perf_ prefixes.

Thanks,

Ingo

#10 lore raw
Quoted 12 lines · Ingo Molnar
Quoted 9 lines · Peter Zijlstra
Quoted 2 lines · Frederic Weisbecker

I thought it would be nice to keep it inline for core use, as it's called pretty often from the overflow path.

That's what I meant.

Quoted 2 lines

A solution is to have a perf_output_copy_inline() in internal.h and export it through perf_output_copy() in buffer.c

Quoted 8 lines
Quoted 3 lines

If only "external inline" would work :-)

I'd call it __perf_output_copy() though, less typing.

Just __output_copy() please - if someone is looking at kernel/events/*.c then it's pretty clear that it's related to perf.

I.e. non-exposed symbols should lose their perf_ prefixes.

Agreed, I'm fixing that.

Quoted 4 lines

Thanks,

Ingo

#11 lore raw

PATCH 1/1 perf: Split up buffer handling from core code

In reply to Frederic Weisbecker

And create the internal perf events header.

v2: Keep an internal inlined perf_output_copy()

Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Stephane Eranian <[email protected]>
---
 kernel/events/Makefile   |    2 +-
 kernel/events/buffer.c   |  400 ++++++++++++++++++++++++++++++++++++++++
 kernel/events/core.c     |  458 ++--------------------------------------------
 kernel/events/internal.h |   70 +++++++
 4 files changed, 487 insertions(+), 443 deletions(-)
 create mode 100644 kernel/events/buffer.c
 create mode 100644 kernel/events/internal.h
kernel/events/Makefile +1 -1
index 1ce23d3..b66b057 100644
@@ -2,5 +2,5 @@ ifdef CONFIG_FUNCTION_TRACER
 CFLAGS_REMOVE_core.o = -pg
 endif
 
-obj-y := core.o
+obj-y := core.o buffer.o
 obj-$(CONFIG_HAVE_HW_BREAKPOINT) += hw_breakpoint.o
kernel/events/buffer.c new file +400 -0

400 lines not on this page · raw message

kernel/events/core.c +16 -442

535 lines not on this page · raw message

kernel/events/internal.h new file +70 -0

70 lines not on this page · raw message

Signature · 1 line
1.7.3.2
#12 lore raw
Quoted 10 lines · Frederic Weisbecker

And create the internal perf events header.

v2: Keep an internal inlined perf_output_copy()

Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Stephane Eranian <[email protected]>

---

Are you guys fine with his patch?
If so, should I queue it myself and send a pull request?

I prefer to work step by step on this split, to avoid too much review burden. And also a single detail can require to rework the whole.

#13 lore raw
Quoted 12 lines · Frederic Weisbecker
Quoted 10 lines · Frederic Weisbecker

And create the internal perf events header.

v2: Keep an internal inlined perf_output_copy()

Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Stephane Eranian <[email protected]>

---

Quoted 2 lines

Are you guys fine with his patch?
If so, should I queue it myself and send a pull request?

Yeah, please do it ASAP.

Quoted 3 lines

I prefer to work step by step on this split, to avoid too much review burden. And also a single detail can require to rework the whole.

Please lets just start it so that the interactions get smaller!

core.c couldnt be uglier as it is today, so after a first raw split we can sort out any fine details - just make sure you don't break the build.

Thanks,

Ingo

#14 lore raw
In reply to Frederic Weisbecker
Commit-ID:  76369139ceb955deefc509e6e12ce9d6ce50ccab
Gitweb:     http://git.kernel.org/tip/76369139ceb955deefc509e6e12ce9d6ce50ccab
Author:     Frederic Weisbecker <[email protected]>
AuthorDate: Thu, 19 May 2011 19:55:04 +0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Thu, 9 Jun 2011 12:57:54 +0200

perf: Split up buffer handling from core code

And create the internal perf events header.

v2: Keep an internal inlined perf_output_copy()

Cc Borislav Petkov <[email protected]>
Cc Stephane Eranian <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Steven Rostedt <[email protected]>
[ v3: use clearer 'ring_buffer' and 'rb' naming ]
---
 include/linux/perf_event.h  |   33 +---
 kernel/events/Makefile      |    2 +-
 kernel/events/core.c        |  568 ++++++-------------------------------------
 kernel/events/internal.h    |   97 ++++++++
 kernel/events/ring_buffer.c |  399 ++++++++++++++++++++++++++++++
 5 files changed, 572 insertions(+), 527 deletions(-)
include/linux/perf_event.h +4 -29
index 3412684..779f6ed 100644
@@ -680,33 +680,6 @@ enum perf_event_active_state {
 };
 
 struct file;
-
-#define PERF_BUFFER_WRITABLE		0x01
-
-struct perf_buffer {
-	atomic_t			refcount;
-	struct rcu_head			rcu_head;
-#ifdef CONFIG_PERF_USE_VMALLOC
-	struct work_struct		work;
-	int				page_order;	/* allocation order  */
-#endif
-	int				nr_pages;	/* nr of data pages  */
-	int				writable;	/* are we writable   */
-
-	atomic_t			poll;		/* POLL_ for wakeups */
-
-	local_t				head;		/* write position    */
-	local_t				nest;		/* nested writers    */
-	local_t				events;		/* event limit       */
-	local_t				wakeup;		/* wakeup stamp      */
-	local_t				lost;		/* nr records lost   */
-
-	long				watermark;	/* wakeup watermark  */
-
-	struct perf_event_mmap_page	*user_page;
-	void				*data_pages[0];
-};
-
 struct perf_sample_data;
 
 typedef void (*perf_overflow_handler_t)(struct perf_event *, int,
@@ -745,6 +718,8 @@ struct perf_cgroup {
 };
 #endif
 
+struct ring_buffer;
+
 /**
  * struct perf_event - performance event kernel representation:
  */
@@ -834,7 +809,7 @@ struct perf_event {
 	atomic_t			mmap_count;
 	int				mmap_locked;
 	struct user_struct		*mmap_user;
-	struct perf_buffer		*buffer;
+	struct ring_buffer		*rb;
 
 	/* poll related */
 	wait_queue_head_t		waitq;
@@ -945,7 +920,7 @@ struct perf_cpu_context {
 
 struct perf_output_handle {
 	struct perf_event		*event;
-	struct perf_buffer		*buffer;
+	struct ring_buffer		*rb;
 	unsigned long			wakeup;
 	unsigned long			size;
 	void				*addr;
kernel/events/Makefile +1 -1
index 1ce23d3..89e5e8a 100644
@@ -2,5 +2,5 @@ ifdef CONFIG_FUNCTION_TRACER
 CFLAGS_REMOVE_core.o = -pg
 endif
 
-obj-y := core.o
+obj-y := core.o ring_buffer.o
 obj-$(CONFIG_HAVE_HW_BREAKPOINT) += hw_breakpoint.o
kernel/events/core.c +71 -497

791 lines not on this page · raw message

kernel/events/internal.h new file +97 -0

97 lines not on this page · raw message

kernel/events/ring_buffer.c new file +399 -0

399 lines not on this page · raw message