public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: linux-kernel@vger.kernel.org
Cc: Ingo Molnar <mingo@elte.hu>,
	Andrew Morton <akpm@linux-foundation.org>,
	Frederic Weisbecker <fweisbec@gmail.com>,
	Mathieu Desnoyers <compudj@krystal.dyndns.org>,
	Masami Hiramatsu <mhiramat@redhat.com>
Subject: [PATCH 12/13 v3] ring-buffer: Add cached pages when freeing reader page
Date: Fri, 14 May 2010 15:22:58 -0400	[thread overview]
Message-ID: <20100514192916.945900385@goodmis.org> (raw)
In-Reply-To: 20100514192246.079025623@goodmis.org

[-- Attachment #1: 0012-ring-buffer-Add-cached-pages-when-freeing-reader-pag.patch --]
[-- Type: text/plain, Size: 4172 bytes --]

From: Steven Rostedt <srostedt@redhat.com>

When the pages are removed from the ring buffer for things like
splice they are freed with ring_buffer_free_read_page().
They are also allocated with ring_buffer_alloc_read_page().

Currently the ring buffer does not take advantage of this situation.
Every time the page is freed, the ring buffer simply frees it.
When a new page is needed, it allocates it. This means that reading
several pages with splice will cause a page to be freed and allocated
several times. This is simply a waste.

This patch adds a cache of the pages freed (16 max). This allows
the pages to be reused quickly without need to go back to the memory
pool.

v2: Added in locking that should have been there in the first release.

Reported-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
---
 kernel/trace/ring_buffer.c |   52 ++++++++++++++++++++++++++++++++++++++------
 1 files changed, 45 insertions(+), 7 deletions(-)

diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c
index 7f6059c..7aded7d 100644
--- a/kernel/trace/ring_buffer.c
+++ b/kernel/trace/ring_buffer.c
@@ -157,6 +157,8 @@ static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON;
 
 #define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
 
+#define RB_MAX_FREE_PAGES	16
+
 /**
  * tracing_on - enable all tracing buffers
  *
@@ -325,7 +327,10 @@ EXPORT_SYMBOL_GPL(ring_buffer_event_data);
 #define RB_MISSED_STORED	(1 << 30)
 
 struct buffer_data_page {
-	u64		 time_stamp;	/* page time stamp */
+	union {
+		struct buffer_data_page	*next;	/* for free pages */
+		u64		 time_stamp;	/* page time stamp */
+	};
 	local_t		 commit;	/* write committed index */
 	unsigned char	 data[];	/* data of buffer page */
 };
@@ -472,6 +477,10 @@ struct ring_buffer {
 	atomic_t			record_disabled;
 	cpumask_var_t			cpumask;
 
+	struct buffer_data_page		*free_pages;
+	int				nr_free_pages;
+	spinlock_t			free_pages_lock;
+
 	struct lock_class_key		*reader_lock_key;
 
 	struct mutex			mutex;
@@ -1118,6 +1127,7 @@ struct ring_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
 	buffer->flags = flags;
 	buffer->clock = trace_clock_local;
 	buffer->reader_lock_key = key;
+	spin_lock_init(&buffer->free_pages_lock);
 
 	/* need at least two pages */
 	if (buffer->pages < 2)
@@ -1184,6 +1194,7 @@ EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
 void
 ring_buffer_free(struct ring_buffer *buffer)
 {
+	struct buffer_data_page *bpage;
 	int cpu;
 
 	get_online_cpus();
@@ -1200,6 +1211,11 @@ ring_buffer_free(struct ring_buffer *buffer)
 	kfree(buffer->buffers);
 	free_cpumask_var(buffer->cpumask);
 
+	while (buffer->free_pages) {
+		bpage = buffer->free_pages;
+		buffer->free_pages = bpage->next;
+		free_page((unsigned long)bpage);
+	};
 	kfree(buffer);
 }
 EXPORT_SYMBOL_GPL(ring_buffer_free);
@@ -3714,14 +3730,24 @@ EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
  */
 void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
 {
-	struct buffer_data_page *bpage;
+	struct buffer_data_page *bpage = NULL;
 	unsigned long addr;
 
-	addr = __get_free_page(GFP_KERNEL);
-	if (!addr)
-		return NULL;
+	spin_lock(&buffer->free_pages_lock);
+	if (buffer->free_pages) {
+		bpage = buffer->free_pages;
+		buffer->free_pages = bpage->next;
+		buffer->nr_free_pages--;
+	}
+	spin_unlock(&buffer->free_pages_lock);
 
-	bpage = (void *)addr;
+	if (!bpage) {
+		addr = __get_free_page(GFP_KERNEL);
+		if (!addr)
+			return NULL;
+
+		bpage = (void *)addr;
+	}
 
 	rb_init_page(bpage);
 
@@ -3738,7 +3764,19 @@ EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
  */
 void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
 {
-	free_page((unsigned long)data);
+	struct buffer_data_page *bpage = data;
+
+	spin_lock(&buffer->free_pages_lock);
+	if (buffer->nr_free_pages >= RB_MAX_FREE_PAGES) {
+		spin_unlock(&buffer->free_pages_lock);
+		free_page((unsigned long)data);
+		return;
+	}
+
+	bpage->next = buffer->free_pages;
+	buffer->free_pages = bpage;
+	buffer->nr_free_pages++;
+	spin_unlock(&buffer->free_pages_lock);
 }
 EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
 
-- 
1.7.0



  parent reply	other threads:[~2010-05-14 19:32 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-05-14 19:22 [PATCH 00/13 v3] [GIT PULL] tracing: shrinking trace events and updates Steven Rostedt
2010-05-14 19:22 ` [PATCH 01/13 v3] tracing: Create class struct for events Steven Rostedt
2010-05-14 19:22 ` [PATCH 02/13 v3] tracepoints: Add check trace callback type Steven Rostedt
2010-05-14 19:22 ` [PATCH 03/13 v3] tracing: Let tracepoints have data passed to tracepoint callbacks Steven Rostedt
2010-05-14 19:22 ` [PATCH 04/13 v3] tracing: Remove per event trace registering Steven Rostedt
2010-05-14 19:22 ` [PATCH 05/13 v3] tracing: Move fields from event to class structure Steven Rostedt
2010-05-18  4:51   ` Li Zefan
2010-05-14 19:22 ` [PATCH 06/13 v3] tracing: Move raw_init from events to class Steven Rostedt
2010-05-14 19:22 ` [PATCH 07/13 v3] tracing: Allow events to share their print functions Steven Rostedt
2010-05-14 19:22 ` [PATCH 08/13 v3] tracing: Move print functions into event class Steven Rostedt
2010-05-14 19:22 ` [PATCH 09/13 v3] tracing: Remove duplicate id information in event structure Steven Rostedt
2010-05-14 19:22 ` [PATCH 10/13 v3] tracing: Combine event filter_active and enable into single flags field Steven Rostedt
2010-05-14 19:22 ` [PATCH 11/13 v3] tracing: Fix function declarations if !CONFIG_STACKTRACE Steven Rostedt
2010-05-14 19:22 ` Steven Rostedt [this message]
2010-05-14 22:26   ` [PATCH 12/13 v3] ring-buffer: Add cached pages when freeing reader page Mathieu Desnoyers
2010-05-15  1:06     ` Steven Rostedt
2010-05-14 19:22 ` [PATCH 13/13 v3] tracing: Comment the use of event_mutex with trace event flags Steven Rostedt
2010-05-14 22:21   ` Mathieu Desnoyers
2010-05-15  0:44     ` Steven Rostedt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20100514192916.945900385@goodmis.org \
    --to=rostedt@goodmis.org \
    --cc=akpm@linux-foundation.org \
    --cc=compudj@krystal.dyndns.org \
    --cc=fweisbec@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mhiramat@redhat.com \
    --cc=mingo@elte.hu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox