mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Wang Nan <wangnan0@huawei.com>
To: <acme@kernel.org>, <peterz@infradead.org>
Cc: <linux-kernel@vger.kernel.org>, <paulus@samba.org>,
	<kan.liang@intel.com>, <pi3orama@163.com>,
	<adrian.hunter@intel.com>, <dsahern@gmail.com>,
	<mingo@kernel.org>, <yunlong.song@huawei.com>,
	Wang Nan <wangnan0@huawei.com>,
	Arnaldo Carvalho de Melo <acme@redhat.com>,
	"Peter Zijlstra" <a.p.zijlstra@chello.nl>
Subject: [RFC PATCH v2 1/3] perf/core: Put size of a sample at the end of it
Date: Mon, 7 Dec 2015 13:28:35 +0000	[thread overview]
Message-ID: <1449494917-62970-2-git-send-email-wangnan0@huawei.com> (raw)
In-Reply-To: <1449494917-62970-1-git-send-email-wangnan0@huawei.com>

This is an RFC patch which is for overwrite mode ring buffer. I'd like
to discuss the correctness of this new idea for retriving as many
events as possible from overwrite mode ring buffer. If there's no
fundamental problem, I'll start perf side work.

The biggest problem for overwrite ring buffer is that it is hard to find
the start position of valid record. [1] and [2] tries to solve this
problem by introducing 'tail' and 'next_tail' into metadata page, and
update them each time the ring buffer is half full. Which adds more
instructions to event output code path, hurt performance. In addition,
even with them we still unable to recover all possible records. For
example:

             data_tail          head
                |                 |
                V                 V
 +------+-------+----------+------+---+
 |  A   |   B   |   C      |  D   |   |
 +------+-------+----------+------+---+

If a record written at head pointer and it overwrites record A:

   head      data_tail
    |           |
    V           V
 +--+---+-------+----------+------+---+
 |E |...|   B   |   C      |  D   | E |
 +--+---+-------+----------+------+---+

Record B is still valid but we can't get it through data_tail.

This patch suggests a different solution for this problem that, by
appending the length of a record at the end of it, user program is
possible to get every possible record in a backward manner, don't
need saving tail pointer.

For example:

   head
    |
    V
 +--+---+-------+----------+------+---+
 |E6|...|   B  8|   C    11|  D  7|E..|
 +--+---+-------+----------+------+---+

In this case, from the 'head' pointer provided by kernel, user program
can first see '6' by (*(head - sizeof(u16))), then it can get the start
pointer of record 'E', then it can read size and find start position
of record D, C, B in similar way.

Kernel side implementation is easy: simply adding a PERF_SAMPLE_SIZE_AT_END
for the size output.

This sloution requires user program (perf) do more things. At least
following things and limitations should be considered:

 1. Before reading such ring buffer, perf must ensure all events which
    may output to it is already stopped, so the 'head' pointer it get
    is the end of the last record.

Further improvement can be taken:

 1. We must ensure all events attached this ring buffer has
    'PERF_SAMPLE_SIZE_AT_END' selected.

 2. 8 bytes extra space is required for each record.

 3. We can find a way to append size information for tracking events.

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

Signed-off-by: Wang Nan <wangnan0@huawei.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Yunlong Song <yunlong.song@huawei.com>
---
 include/uapi/linux/perf_event.h | 3 ++-
 kernel/events/core.c            | 9 +++++++++
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_event.h
index 1afe962..e79606c 100644
--- a/include/uapi/linux/perf_event.h
+++ b/include/uapi/linux/perf_event.h
@@ -139,8 +139,9 @@ enum perf_event_sample_format {
 	PERF_SAMPLE_IDENTIFIER			= 1U << 16,
 	PERF_SAMPLE_TRANSACTION			= 1U << 17,
 	PERF_SAMPLE_REGS_INTR			= 1U << 18,
+	PERF_SAMPLE_SIZE_AT_END			= 1U << 19,
 
-	PERF_SAMPLE_MAX = 1U << 19,		/* non-ABI */
+	PERF_SAMPLE_MAX = 1U << 20,		/* non-ABI */
 };
 
 /*
diff --git a/kernel/events/core.c b/kernel/events/core.c
index c3d61b9..cfe9336 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -5452,6 +5452,12 @@ void perf_output_sample(struct perf_output_handle *handle,
 		}
 	}
 
+	/* Should be the last one */
+	if (sample_type & PERF_SAMPLE_SIZE_AT_END) {
+		perf_output_skip(handle, sizeof(u64) - sizeof(header->size));
+		perf_output_put(handle, header->size);
+	}
+
 	if (!event->attr.watermark) {
 		int wakeup_events = event->attr.wakeup_events;
 
@@ -5571,6 +5577,9 @@ void perf_prepare_sample(struct perf_event_header *header,
 
 		header->size += size;
 	}
+
+	if (sample_type & PERF_SAMPLE_SIZE_AT_END)
+		header->size += sizeof(u64);
 }
 
 void perf_event_output(struct perf_event *event,
-- 
1.8.3.4


  reply	other threads:[~2015-12-07 13:32 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-24 14:00 [PATCH] perf record: Add snapshot mode support for perf's regular events Yunlong Song
2015-11-24 14:00 ` Yunlong Song
2015-11-24 14:30   ` Arnaldo Carvalho de Melo
2015-11-25 12:44     ` Yunlong Song
2015-11-24 15:06   ` David Ahern
2015-11-24 15:20     ` Arnaldo Carvalho de Melo
2015-11-24 15:24       ` David Ahern
2015-11-24 15:40         ` Arnaldo Carvalho de Melo
2015-11-24 16:16           ` David Ahern
2015-11-25  3:50       ` Wangnan (F)
2015-11-25  5:06         ` David Ahern
2015-11-25  7:22         ` Adrian Hunter
2015-11-25  7:47           ` Wangnan (F)
2015-11-25  8:27             ` Adrian Hunter
2015-11-25  8:43               ` Wangnan (F)
2015-11-25  9:05                 ` Adrian Hunter
2015-11-25  7:50     ` Yunlong Song
2015-11-25  9:27 ` Peter Zijlstra
2015-11-25  9:44   ` Wangnan (F)
2015-11-25 12:20     ` Peter Zijlstra
2015-11-25 12:54       ` Wangnan (F)
2015-11-26  9:19         ` Ingo Molnar
2015-11-26  9:24           ` Wangnan (F)
2015-11-26  9:27           ` Ingo Molnar
2015-11-26  9:40             ` Ingo Molnar
2015-11-26  9:57             ` Ingo Molnar
2015-12-02  8:25   ` Wangnan (F)
2015-12-02 13:38     ` [RFC PATCH] perf/core: Put size of a sample at the end of it Wang Nan
2015-12-03 10:08       ` Peter Zijlstra
2015-12-03 10:31         ` Wangnan (F)
2015-12-07 13:28       ` [RFC PATCH v2 0/3] perf core/perf tools: Utilizing overwrite ring buffer Wang Nan
2015-12-07 13:28         ` Wang Nan [this message]
2015-12-07 13:28         ` [RFC PATCH v2 2/3] perf tools: Enable overwrite settings Wang Nan
2015-12-07 13:28         ` [RFC PATCH v2 3/3] perf record: Find tail pointer through size at end of event Wang Nan

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=1449494917-62970-2-git-send-email-wangnan0@huawei.com \
    --to=wangnan0@huawei.com \
    --cc=a.p.zijlstra@chello.nl \
    --cc=acme@kernel.org \
    --cc=acme@redhat.com \
    --cc=adrian.hunter@intel.com \
    --cc=dsahern@gmail.com \
    --cc=kan.liang@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=paulus@samba.org \
    --cc=peterz@infradead.org \
    --cc=pi3orama@163.com \
    --cc=yunlong.song@huawei.com \
    /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

all inboxes | Powered by JetHome®