Introducing trace buffer mapping by user-space
Subsystems (2)
Vincent Donnefort
maintainerv2 cover Introducing trace buffer mapping by user-space
The tracing ring-buffers can be stored on disk or sent to network without any copy via splice. However the later doesn't allow real time processing of the traces. A solution is to give access to userspace to the ring-buffer pages directly via a mapping. A piece of software can now become a reader of the ring-buffer, and drive a consuming or non-consuming read in a similar fashion to what trace and trace_pipe offer.
Attached to this cover letter an example of consuming read for a ring-buffer, using libtracefs.
Vincent
v1 -> v2:
* Hide data_pages from the userspace struct
* Fix META_PAGE_MAX_PAGES
* Support for order > 0 meta-page
* Add missing page->mapping.
Vincent Donnefort (2):
ring-buffer: Introducing ring-buffer mapping functions
tracing: Allow user-space mapping of the ring-buffer
--
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#include <tracefs.h>
#include <kbuffer.h>
#include <event-parse.h>
#include <asm/types.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#define TRACE_MMAP_IOCTL_GET_READER_PAGE _IO('T', 0x1)
struct ring_buffer_meta_page_header {
__u64 entries;
__u64 overrun;
__u32 pages_touched;
__u32 meta_page_size;
__u32 reader_page; /* ID of the current reader page */
__u32 nr_data_pages; /* doesn't take into account the reader_page */
__u32 data_page_head; /* ring-buffer head as an offset from data_start */
__u32 data_start; /* offset within the meta page */};
/* Need to access private struct to save counters */
struct kbuffer {
unsigned long long timestamp;
long long lost_events;
unsigned long flags;
void *subbuffer;
void *data;
unsigned int index;
unsigned int curr;
unsigned int next;
unsigned int size;
unsigned int start;
unsigned int first;
unsigned int (*read_4)(void *ptr);
unsigned long long (*read_8)(void *ptr);
unsigned long long (*read_long)(struct kbuffer *kbuf, void *ptr);
int (*next_event)(struct kbuffer *kbuf);
};static char *argv0;
static bool need_exit;
static char *get_this_name(void)
{
static char *this_name;
char *arg;
char *p;
if (this_name)
return this_name;
arg = argv0;
p = arg+strlen(arg);
while (p >= arg && *p != '/')
p--;
p++;
this_name = p;
return p;
}static void __vdie(const char *fmt, va_list ap, int err)
{
int ret = errno;
char *p = get_this_name();
if (err && errno)
perror(p);
else
ret = -1;
fprintf(stderr, " ");
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
exit(ret);
}void pdie(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
__vdie(fmt, ap, 1);
va_end(ap);
}static void read_page(struct tep_handle *tep, struct kbuffer *kbuf,
void *data, int page)
{
static struct trace_seq seq;
struct tep_record record;
if (seq.buffer)
trace_seq_reset(&seq);
else
trace_seq_init(&seq);
while ((record.data = kbuffer_read_event(kbuf, &record.ts))) {
kbuffer_next_event(kbuf, NULL);
tep_print_event(tep, &seq, &record,
"%s-%d %9d\t%s\n", TEP_PRINT_COMM,
TEP_PRINT_PID, TEP_PRINT_TIME, TEP_PRINT_NAME);
trace_seq_do_printf(&seq);
trace_seq_reset(&seq);
}
}static int next_reader_page(int fd, struct ring_buffer_meta_page_header *meta,
struct kbuffer *kbuf)
{
int prev_reader_page = meta->reader_page;
if (ioctl(fd, TRACE_MMAP_IOCTL_GET_READER_PAGE) < 0)
pdie("ioctl");
return meta->reader_page;
}static void signal_handler(int unused)
{
printf("Exit!\n");
need_exit = true;
}int main(int argc, char **argv)
{
int page_size, meta_len, data_len, page, fd, start = -1;
struct ring_buffer_meta_page_header *map;
struct kbuffer *kbuf, prev_kbuf;
struct tep_handle *tep;
__u64 prev_entries;
void *meta, *data;
char *buf, path[32];
int cpu;
argv0 = argv[0];
cpu = atoi(argv[1]);
snprintf(path, 32, "per_cpu/cpu%d/trace_pipe_raw", cpu);
signal(SIGINT, signal_handler);
tep = tracefs_local_events(NULL);
kbuf = tep_kbuffer(tep);
page_size = getpagesize();
fd = tracefs_instance_file_open(NULL, path, O_RDONLY);
if (fd < 0)
pdie("raw");
meta = mmap(NULL, page_size, PROT_READ, MAP_SHARED, fd, 0);
if (meta == MAP_FAILED)
pdie("mmap");
map = meta;
meta_len = map->meta_page_size;
if (meta_len > page_size) {
munmap(meta, page_size);
meta = mmap(NULL, meta_len, PROT_READ, MAP_SHARED, fd, 0);
if (meta == MAP_FAILED)
pdie("mmap");
map = meta;
}
printf("entries: %llu\n", map->entries);
printf("overrun: %llu\n", map->overrun);
printf("pages_touched: %u\n", map->pages_touched);
printf("reader_page: %u\n", map->reader_page);
printf("nr_data_pages: %u\n\n", map->nr_data_pages);
data_len = page_size * (map->nr_data_pages + 1);
data = mmap(NULL, data_len, PROT_READ, MAP_SHARED, fd, meta_len);
if (data == MAP_FAILED)
pdie("mmap data");
page = ((struct ring_buffer_meta_page_header *)meta)->reader_page;
again:
do {
kbuffer_load_subbuffer(kbuf, data + page_size * page);
if (page != start) {
printf("READER PAGE: %d\n", map->reader_page);
} else {
kbuf->curr = prev_kbuf.curr;
kbuf->index = prev_kbuf.index;
kbuf->next = prev_kbuf.next;
kbuf->timestamp = prev_kbuf.timestamp;
kbuffer_next_event(kbuf, NULL);
}
prev_entries = map->entries;
start = page;
read_page(tep, kbuf, data, page);
} while ((page = next_reader_page(fd, meta, kbuf)) != start);
prev_kbuf.curr = kbuf->curr;
prev_kbuf.index = kbuf->index;
prev_kbuf.next = kbuf->next;
prev_kbuf.timestamp = kbuf->timestamp;
while (prev_entries == *(volatile __u64 *)&map->entries && !need_exit)
usleep(100000);
if (!need_exit)
goto again;
munmap(data, data_len);
munmap(meta, page_size);
close(fd);
return 0;
}include/linux/ring_buffer.h | 8 + include/uapi/linux/trace_mmap.h | 28 +++ kernel/trace/ring_buffer.c | 384 +++++++++++++++++++++++++++++++- kernel/trace/trace.c | 76 ++++++- 4 files changed, 490 insertions(+), 6 deletions(-) create mode 100644 include/uapi/linux/trace_mmap.h
Signature · 1 line
2.40.0.rc1.284.g88254d51c5-goog
Vincent Donnefort
maintainerPATCH v2 1/2 ring-buffer: Introducing ring-buffer mapping functions
In preparation for allowing the user-space to map a ring-buffer, add a set of mapping functions:
ring_buffer_{map,unmap}()
ring_buffer_map_fault()
And controls on the ring-buffer:
ring_buffer_get_reader_page() /* swap reader and head */ ring_buffer_update_meta_page()
Mapping the ring-buffer also involves:
A unique ID for each page of the ring-buffer, as currently the pages are only identified through their in-kernel VA.
A meta-page, where are stored statistics about the ring-buffer and a page IDs list, ordered. A field gives what page is the reader one and one to gives where the ring-buffer starts in the list of data pages.
The linear mapping exposes the meta-page, and each page of the ring-buffer, ordered following their unique ID, assigned during the first mapping.
Once mapped, no page can get in or out of the ring-buffer: the buffer size will remain unmodified and the splice enabling functions will in reality simply memcpy the data instead of swapping pages.
include/linux/ring_buffer.h +8 -0
@@ -6,6 +6,8 @@#include <linux/seq_file.h> #include <linux/poll.h> +#include <uapi/linux/trace_mmap.h> +struct trace_buffer ;struct ring_buffer_iter ;
@@ -211,4 +213,10 @@ int trace_rb_cpu_prepare(unsigned int cpu, struct hlist_node *node);#define trace_rb_cpu_prepare NULL#endif +int ring_buffer_map (struct trace_buffer *buffer,int cpu); +int ring_buffer_unmap (struct trace_buffer *buffer,int cpu); +struct page *ring_buffer_map_fault (struct trace_buffer *buffer,int cpu, +unsigned long pgoff); +int ring_buffer_get_reader_page (struct trace_buffer *buffer,int cpu); +int ring_buffer_update_meta_page (struct trace_buffer *buffer,int cpu);#endif /* _LINUX_RING_BUFFER_H */
include/uapi/linux/trace_mmap.h new file +25 -0
@@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +#ifndef _UAPI_TRACE_MMAP_H_ +#define _UAPI_TRACE_MMAP_H_ + +#include <asm/bitsperlong.h> + +#include <linux/types.h> + +struct ring_buffer_meta_page_header { +#if __BITS_PER_LONG ==64 +__u64 entries; +__u64 overrun; +#else +__u32 entries; +__u32 overrun; +#endif +__u32 pages_touched; +__u32 meta_page_size; +__u32 reader_page;/* page ID for the reader page */ +__u32 nr_data_pages;/* doesn't take into account the reader_page */ +__u32 data_page_head;/* ring-buffer head as an offset from data_start */ +__u32 data_start;/* offset within the meta page */ +}; + +#endif /* _UAPI_TRACE_MMAP_H_ */
kernel/trace/ring_buffer.c +381 -3
490 lines not on this page · raw message
Signature · 1 line
2.40.0.rc1.284.g88254d51c5-goog
Steven Rostedt
maintainerRe: [PATCH v2 1/2] ring-buffer: Introducing ring-buffer mapping functions
Quoted 8 lines · On Wed, 22 Mar 2023 10:22:43 +0000 Vincent Donnefort <[email protected]>…
include/uapi/linux/trace_mmap.h new file +8
-0+#include <linux/types.h> + +struct ring_buffer_meta_page_header { +#if __BITS_PER_LONG ==64 +__u64 entries; +__u64 overrun; +#else +__u32 entries;
include/uapi/linux/trace_mmap.h line 24
Quoted 10 lines
include/uapi/linux/trace_mmap.h new file +10
-0+__u32 overrun; +#endif +__u32 pages_touched; +__u32 meta_page_size; +__u32 reader_page;/* page ID for the reader page */ +__u32 nr_data_pages;/* doesn't take into account the reader_page */ +__u32 data_page_head;/* ring-buffer head as an offset from data_start */ +__u32 data_start;/* offset within the meta page */ +}; +
I've been playing with this a bit, and I'm thinking, do we need the data_pages[] array on the meta page?
I noticed that I'm not even using it.
Currently, we need to do a ioctl every time we finish with the reader page, and that updates the reader_page in the meta data to point to the next page to read. When do we need to look at the data_start section?
-- Steve
Vincent Donnefort
maintainerRe: [PATCH v2 1/2] ring-buffer: Introducing ring-buffer mapping functions
Quoted 25 lines · Steven Rostedt
Quoted 8 lines · On Wed, 22 Mar 2023 10:22:43 +0000 Vincent Donnefort <[email protected]>…
include/uapi/linux/trace_mmap.h new file +8
-0+#include <linux/types.h> + +struct ring_buffer_meta_page_header { +#if __BITS_PER_LONG ==64 +__u64 entries; +__u64 overrun; +#else +__u32 entries;include/uapi/linux/trace_mmap.h line 24
Quoted 10 lines
include/uapi/linux/trace_mmap.h new file +10
-0+__u32 overrun; +#endif +__u32 pages_touched; +__u32 meta_page_size; +__u32 reader_page;/* page ID for the reader page */ +__u32 nr_data_pages;/* doesn't take into account the reader_page */ +__u32 data_page_head;/* ring-buffer head as an offset from data_start */ +__u32 data_start;/* offset within the meta page */ +}; +I've been playing with this a bit, and I'm thinking, do we need the data_pages[] array on the meta page?
Quoted 5 lines
I noticed that I'm not even using it.
Currently, we need to do a ioctl every time we finish with the reader page, and that updates the
reader_pagein the meta data to point to the next page to read. When do we need to look at thedata_startsection?
This is for non-consuming read, to get all the pages in order.
If we remove this section we would lose this ability ... but we'd also simplify the code by a good order of magnitude (don't need the update ioctl anymore, no need to keep those pages in order and everything can fit a 0-order meta-page). And the non-consuming read doesn't bring much to the user over the pipe version.
This will although impact our hypervisor tracing which will only be able to expose trace_pipe interfaces. But I don't think it is a problem, all userspace tools only relying on consuming read anyway.
So if you're happy dropping this support, let's get rid of it.
Signature · 1 line
Vincent
Steven Rostedt
maintainerRe: [PATCH v2 1/2] ring-buffer: Introducing ring-buffer mapping functions
Quoted 10 lines · On Wed, 29 Mar 2023 10:19:44 +0100 Vincent Donnefort <[email protected]>…
Quoted 8 lines
I've been playing with this a bit, and I'm thinking, do we need the data_pages[] array on the meta page?
I noticed that I'm not even using it.
Currently, we need to do a ioctl every time we finish with the reader page, and that updates the
reader_pagein the meta data to point to the next page to read. When do we need to look at thedata_startsection?This is for non-consuming read, to get all the pages in order.
Yeah, I was trying to see how a non consuming read would work, and was having issues figuring that out without the tail page being updated.
Quoted 11 lines
If we remove this section we would lose this ability ... but we'd also simplify the code by a good order of magnitude (don't need the update ioctl anymore, no need to keep those pages in order and everything can fit a 0-order meta-page). And the non-consuming read doesn't bring much to the user over the pipe version.
This will although impact our hypervisor tracing which will only be able to expose
trace_pipeinterfaces. But I don't think it is a problem, all userspace tools only relying on consuming read anyway.So if you're happy dropping this support, let's get rid of it.
I don't really want to get rid of it, but perhaps break it up where we don't have it in the first release, but add it in a second one. That will also make sure that we can expand the API if necessary (one reason I wanted the "data_start" in the first place).
Let's drop it for now, but be able to add it later, an have the current structure be:
struct ring_buffer_meta_page_header {
#if __BITS_PER_LONG == 64 __u64 entries; __u64 overrun; #else __u32 entries; __u32 overrun; #endif __u32 pages_touched; __u32 meta_page_size; __u32 reader_page; /* page ID for the reader page */ __u32 nr_data_pages; /* doesn't take into account the reader_page */ };
BTW, shouldn't the nr_data_pages take into account the reader page? As it is part of the array we traverse isn't it?
-- Steve
Steven Rostedt
maintainerRe: [PATCH v2 1/2] ring-buffer: Introducing ring-buffer mapping functions
Quoted 14 lines · On Wed, 29 Mar 2023 07:03:53 -0400 Steven Rostedt <[email protected]> wrote:
struct
ring_buffer_meta_page_header{#if __BITS_PER_LONG == 64 __u64 entries; __u64 overrun; #else __u32 entries; __u32 overrun; #endif __u32 pages_touched; __u32 meta_page_size; __u32 reader_page; /* page ID for the reader page */ __u32 nr_data_pages; /* doesn't take into account the reader_page */ };
Quoted 2 lines
BTW, shouldn't the
nr_data_pagestake into account the reader page? As it is part of the array we traverse isn't it?
Ah, I guess nr_data_pages is the length of the index mapping, not the array of pages mapped?
-- Steve
Vincent Donnefort
maintainerRe: [PATCH v2 1/2] ring-buffer: Introducing ring-buffer mapping functions
Quoted 17 lines · Steven Rostedt
Quoted 14 lines · On Wed, 29 Mar 2023 07:03:53 -0400 Steven Rostedt <[email protected]> wrote:
struct
ring_buffer_meta_page_header{#if __BITS_PER_LONG == 64 __u64 entries; __u64 overrun; #else __u32 entries; __u32 overrun; #endif __u32 pages_touched; __u32 meta_page_size; __u32 reader_page; /* page ID for the reader page */ __u32 nr_data_pages; /* doesn't take into account the reader_page */ };
Quoted 5 lines
Quoted 2 lines
BTW, shouldn't the
nr_data_pagestake into account the reader page? As it is part of the array we traverse isn't it?Ah, I guess
nr_data_pagesis the length of the index mapping, not the array of pages mapped?
Yes correct, data_pages[nr_data_pages] and the reader_page being excluded... which might not be the easiest interface, as the size of the buffer to read depends on if the reader_page has data to be read or not.
Quoted 2 lines
-- Steve
Vincent Donnefort
maintainerRe: [PATCH v2 1/2] ring-buffer: Introducing ring-buffer mapping functions
In reply to Steven RostedtQuoted 8 lines · Steven Rostedt
Quoted 5 lines · On Wed, 29 Mar 2023 10:19:44 +0100 Vincent Donnefort <[email protected]>…
Quoted 5 lines
I've been playing with this a bit, and I'm thinking, do we need the data_pages[] array on the meta page?
I noticed that I'm not even using it.
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Currently, we need to do a ioctl every time we finish with the reader page, and that updates the
reader_pagein the meta data to point to the next page to read. When do we need to look at thedata_startsection?This is for non-consuming read, to get all the pages in order.
Yeah, I was trying to see how a non consuming read would work, and was having issues figuring that out without the tail page being updated.
Would the userspace really need to know where is the tail page? It can just stop whenever it finds out a page doesn't have any events, and make sure it does not loop once back to the head?
Quoted 13 lines
Quoted 7 lines
If we remove this section we would lose this ability ... but we'd also simplify the code by a good order of magnitude (don't need the update ioctl anymore, no need to keep those pages in order and everything can fit a 0-order meta-page). And the non-consuming read doesn't bring much to the user over the pipe version.
Quoted 5 lines
This will although impact our hypervisor tracing which will only be able to expose
trace_pipeinterfaces. But I don't think it is a problem, all userspace tools only relying on consuming read anyway.So if you're happy dropping this support, let's get rid of it.
Quoted 7 lines
I don't really want to get rid of it, but perhaps break it up where we don't have it in the first release, but add it in a second one. That will also make sure that we can expand the API if necessary (one reason I wanted the "
data_start" in the first place).Let's drop it for now, but be able to add it later, an have the current structure be:
Ok, I will prepare a V3 accordingly.
Quoted 15 lines
struct
ring_buffer_meta_page_header{#if __BITS_PER_LONG == 64 __u64 entries; __u64 overrun; #else __u32 entries; __u32 overrun; #endif __u32 pages_touched; __u32 meta_page_size; __u32 reader_page; /* page ID for the reader page */ __u32 nr_data_pages; /* doesn't take into account the reader_page */ };
Quoted 2 lines
BTW, shouldn't the
nr_data_pagestake into account the reader page? As it is part of the array we traverse isn't it?
It depends if the reader page has ever been swapped out. If yes, the reader would have to start from reader_page and then switch to the data_pages. Which sounds like a fiddly interface for the userspace.
So yeah, consuming-read only feels like a better start.
Quoted 2 lines
-- Steve
Steven Rostedt
maintainerRe: [PATCH v2 1/2] ring-buffer: Introducing ring-buffer mapping functions
Quoted 13 lines · On Wed, 29 Mar 2023 13:23:01 +0100 Vincent Donnefort <[email protected]>…
Quoted 12 lines · Steven Rostedt
Quoted 9 lines · On Wed, 29 Mar 2023 10:19:44 +0100 Vincent Donnefort <[email protected]>…
Quoted 8 lines
I've been playing with this a bit, and I'm thinking, do we need the data_pages[] array on the meta page?
I noticed that I'm not even using it.
Currently, we need to do a ioctl every time we finish with the reader page, and that updates the
reader_pagein the meta data to point to the next page to read. When do we need to look at thedata_startsection?
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
This is for non-consuming read, to get all the pages in order.
Yeah, I was trying to see how a non consuming read would work, and was having issues figuring that out without the tail page being updated.
Would the userspace really need to know where is the tail page? It can just stop whenever it finds out a page doesn't have any events, and make sure it does not loop once back to the head?
I'm trying to come up with a possible algorithm that doesn't need ioctls. It would need to know if the writer moved or not. Probably need a counter that gets incremented every time the writer goes to a new page.
Having the tail page was just a convenient way to know where the end is.
Quoted 39 lines
Quoted 14 lines
Quoted 8 lines
If we remove this section we would lose this ability ... but we'd also simplify the code by a good order of magnitude (don't need the update ioctl anymore, no need to keep those pages in order and everything can fit a 0-order meta-page). And the non-consuming read doesn't bring much to the user over the pipe version.
Quoted 5 lines
This will although impact our hypervisor tracing which will only be able to expose
trace_pipeinterfaces. But I don't think it is a problem, all userspace tools only relying on consuming read anyway.So if you're happy dropping this support, let's get rid of it.
Quoted 7 lines
I don't really want to get rid of it, but perhaps break it up where we don't have it in the first release, but add it in a second one. That will also make sure that we can expand the API if necessary (one reason I wanted the "
data_start" in the first place).Let's drop it for now, but be able to add it later, an have the current structure be:
Ok, I will prepare a V3 accordingly.
Quoted 15 lines
struct
ring_buffer_meta_page_header{#if __BITS_PER_LONG == 64 __u64 entries; __u64 overrun; #else __u32 entries; __u32 overrun; #endif __u32 pages_touched; __u32 meta_page_size; __u32 reader_page; /* page ID for the reader page */ __u32 nr_data_pages; /* doesn't take into account the reader_page */ };
Quoted 9 lines
Quoted 2 lines
BTW, shouldn't the
nr_data_pagestake into account the reader page? As it is part of the array we traverse isn't it?It depends if the reader page has ever been swapped out. If yes, the reader would have to start from
reader_pageand then switch to thedata_pages. Which sounds like a fiddly interface for the userspace.So yeah, consuming-read only feels like a better start.
I agree. I'd like to get something in that can be extended, but simple enough that it's not too much of a barrier wrt getting the API correct.
-- Steve
Vincent Donnefort
maintainerRe: [PATCH v2 1/2] ring-buffer: Introducing ring-buffer mapping functions
Quoted 21 lines · Steven Rostedt
Quoted 18 lines · On Wed, 29 Mar 2023 13:23:01 +0100 Vincent Donnefort <[email protected]>…
Quoted 16 lines · Steven Rostedt
Quoted 10 lines · On Wed, 29 Mar 2023 10:19:44 +0100 Vincent Donnefort <[email protected]>…
Quoted 8 lines
I've been playing with this a bit, and I'm thinking, do we need the data_pages[] array on the meta page?
I noticed that I'm not even using it.
Currently, we need to do a ioctl every time we finish with the reader page, and that updates the
reader_pagein the meta data to point to the next page to read. When do we need to look at thedata_startsection?This is for non-consuming read, to get all the pages in order.
Yeah, I was trying to see how a non consuming read would work, and was having issues figuring that out without the tail page being updated.
Quoted 7 lines
Quoted 3 lines
Would the userspace really need to know where is the tail page? It can just stop whenever it finds out a page doesn't have any events, and make sure it does not loop once back to the head?
I'm trying to come up with a possible algorithm that doesn't need ioctls. It would need to know if the writer moved or not. Probably need a counter that gets incremented every time the writer goes to a new page.
The v2 of this series only updates the head page in the update ioctl (ring_buffer_update_meta_page()) Couldn't find a nice way around that as it can be either updated by the reader or the writer. So the best solution seemed a call to set_head_page().
[...]
Steven Rostedt
maintainerRe: [PATCH v2 1/2] ring-buffer: Introducing ring-buffer mapping functions
Quoted 4 lines · On Wed, 29 Mar 2023 14:10:09 +0100 Vincent Donnefort <[email protected]>…
The v2 of this series only updates the head page in the update ioctl (
ring_buffer_update_meta_page()) Couldn't find a nice way around that as it can be either updated by the reader or the writer. So the best solution seemed a call toset_head_page().
Yes, the update will race between readers and writers. Let's not worry about this at the moment and not expose it yet. I'm looking at other ways around this too.
-- Steve
Vincent Donnefort
maintainerRe: [PATCH v2 1/2] ring-buffer: Introducing ring-buffer mapping functions
In reply to Steven Rostedt[...]
Quoted 21 lines
Quoted 21 lines
Quoted 14 lines
struct
ring_buffer_meta_page_header{#if __BITS_PER_LONG == 64 __u64 entries; __u64 overrun; #else __u32 entries; __u32 overrun; #endif __u32 pages_touched; __u32 meta_page_size; __u32 reader_page; /* page ID for the reader page */ __u32 nr_data_pages; /* doesn't take into account the reader_page */ };Quoted 2 lines
BTW, shouldn't the
nr_data_pagestake into account the reader page? As it is part of the array we traverse isn't it?It depends if the reader page has ever been swapped out. If yes, the reader would have to start from
reader_pageand then switch to thedata_pages. Which sounds like a fiddly interface for the userspace.
Quoted 7 lines
Quoted 2 lines
So yeah, consuming-read only feels like a better start.
I agree. I'd like to get something in that can be extended, but simple enough that it's not too much of a barrier wrt getting the API correct.
-- Steve
Something I just realized though. In the event of being able to upstream the hypervisor tracing based on the ring_buffer_meta_page, without non-consumming support, we wouldn't have the "trace" file which is used to reset the buffers.
I'd guess we'd have to either create one that is read-only (a bit strange) or let trace_pipe reset the buffer(s).
Steven Rostedt
maintainerRe: [PATCH v2 1/2] ring-buffer: Introducing ring-buffer mapping functions
In reply to Steven RostedtQuoted 13 lines · On Wed, 29 Mar 2023 07:03:53 -0400 Steven Rostedt <[email protected]> wrote:
struct
ring_buffer_meta_page_header{#if __BITS_PER_LONG == 64 __u64 entries; __u64 overrun; #else __u32 entries; __u32 overrun; #endif __u32 pages_touched; __u32 meta_page_size; __u32 reader_page; /* page ID for the reader page */ __u32 nr_data_pages; /* doesn't take into account the reader_page */ };
Oh, I guess we should also expose the amount read on the reader page, that gets updated on the ioctl. That is, if the first time we read the reader page and the page is not full and unmap the pages, and then new events were added to the reader page, we should not re-read the events that were read previously.
That is, expose cpu_buffer->reader_page->read
-- Steve
Vincent Donnefort
maintainerRe: [PATCH v2 1/2] ring-buffer: Introducing ring-buffer mapping functions
Quoted 17 lines · Steven Rostedt
Quoted 13 lines · On Wed, 29 Mar 2023 07:03:53 -0400 Steven Rostedt <[email protected]> wrote:
struct
ring_buffer_meta_page_header{#if __BITS_PER_LONG == 64 __u64 entries; __u64 overrun; #else __u32 entries; __u32 overrun; #endif __u32 pages_touched; __u32 meta_page_size; __u32 reader_page; /* page ID for the reader page */ __u32 nr_data_pages; /* doesn't take into account the reader_page */ };
Quoted 7 lines
Oh, I guess we should also expose the amount read on the reader page, that gets updated on the ioctl. That is, if the first time we read the reader page and the page is not full and unmap the pages, and then new events were added to the reader page, we should not re-read the events that were read previously.
That is, expose
cpu_buffer->reader_page->read
Couldn't it be an issue of updating cpu_buffer->reader_page->read during the ioctl? I guess we would write the value of the current written events on that page, hopping for the userspace reader to read it all.
But then if new events are written, the reader doesn't need the ioctl to read them, it can just check the meta->entries field or the commit field in the reader_page header?
So it's much likely cpu_buffer->reader_page->read will go out of sync?
Quoted 2 lines
-- Steve
Steven Rostedt
maintainerRe: [PATCH v2 1/2] ring-buffer: Introducing ring-buffer mapping functions
Quoted 12 lines · On Wed, 29 Mar 2023 14:01:01 +0100 Vincent Donnefort <[email protected]>…
Quoted 7 lines
Oh, I guess we should also expose the amount read on the reader page, that gets updated on the ioctl. That is, if the first time we read the reader page and the page is not full and unmap the pages, and then new events were added to the reader page, we should not re-read the events that were read previously.
That is, expose
cpu_buffer->reader_page->readCouldn't it be an issue of updating
cpu_buffer->reader_page->readduring the ioctl? I guess we would write the value of the current written events on that page, hopping for the userspace reader to read it all.
Quoted 5 lines
But then if new events are written, the reader doesn't need the ioctl to read them, it can just check the
meta->entriesfield or the commit field in thereader_pageheader?So it's much likely
cpu_buffer->reader_page->readwill go out of sync?
Here's the issue I found during testing:
write 10 events to ring buffer (all go into the reader page)
Run application that maps the pages, and reads the 10 events, and exits.
Write 10 more events to ring buffer (all are appended to the reader page)
Run application that maps the pages and reads 20 events, and exits.
It read the 10 previous events, but should not have. It should have only read the last 10 that were not read previously.
-- Steve
Vincent Donnefort
maintainerRe: [PATCH v2 1/2] ring-buffer: Introducing ring-buffer mapping functions
Quoted 25 lines · Steven Rostedt
Quoted 12 lines · On Wed, 29 Mar 2023 14:01:01 +0100 Vincent Donnefort <[email protected]>…
Quoted 7 lines
Oh, I guess we should also expose the amount read on the reader page, that gets updated on the ioctl. That is, if the first time we read the reader page and the page is not full and unmap the pages, and then new events were added to the reader page, we should not re-read the events that were read previously.
That is, expose
cpu_buffer->reader_page->readCouldn't it be an issue of updating
cpu_buffer->reader_page->readduring the ioctl? I guess we would write the value of the current written events on that page, hopping for the userspace reader to read it all.Quoted 5 lines
But then if new events are written, the reader doesn't need the ioctl to read them, it can just check the
meta->entriesfield or the commit field in thereader_pageheader?So it's much likely
cpu_buffer->reader_page->readwill go out of sync?Here's the issue I found during testing:
write 10 events to ring buffer (all go into the reader page)
Quoted 8 lines
Run application that maps the pages, and reads the 10 events, and exits.
Write 10 more events to ring buffer (all are appended to the reader page)
Run application that maps the pages and reads 20 events, and exits.
It read the 10 previous events, but should not have. It should have only read the last 10 that were not read previously.
I see.
We can say we update cpu_buffer->reader_page->read on the get_reader_page ioctl, to the most recent value possible, which will have the consequence of actually "flushing" those events?
If the reader decides to read events past this value then it just can't expect them to not be duplicated?
I suppose it'd be down the reader to store meta->read somehwere?
prev_read = meta->read
ioctl(fd, TRACE_MMAP_IOCTL_GET_READER_PAGE)
/* read events from prev_read to meta->read */
Quoted 2 lines
-- Steve
Steven Rostedt
maintainerRe: [PATCH v2 1/2] ring-buffer: Introducing ring-buffer mapping functions
Quoted 3 lines · On Wed, 29 Mar 2023 14:31:07 +0100 Vincent Donnefort <[email protected]>…
We can say we update
cpu_buffer->reader_page->readon theget_reader_pageioctl, to the most recent value possible, which will have the consequence of actually "flushing" those events?
Yes. It should be no different than doing a normal read of the trace_pipe_raw file, which does the same.
Quoted 9 lines
If the reader decides to read events past this value then it just can't expect them to not be duplicated?
I suppose it'd be down the reader to store
meta->readsomehwere?prev_read = meta->read ioctl(fd, TRACE_MMAP_IOCTL_GET_READER_PAGE) /* read events from prev_read to meta->read */
Yes, in fact it shouldn't need to call the ioctl until after it read it.
Maybe, we should have the ioctl take a parameter of how much was read? To prevent races?
That is, it should pass in the page->commit that it used to to read the pages.
-- Steve
Vincent Donnefort
maintainerRe: [PATCH v2 1/2] ring-buffer: Introducing ring-buffer mapping functions
Quoted 16 lines · Steven Rostedt
Quoted 3 lines · On Wed, 29 Mar 2023 14:31:07 +0100 Vincent Donnefort <[email protected]>…
We can say we update
cpu_buffer->reader_page->readon theget_reader_pageioctl, to the most recent value possible, which will have the consequence of actually "flushing" those events?Yes. It should be no different than doing a normal read of the
trace_pipe_rawfile, which does the same.Quoted 6 lines
If the reader decides to read events past this value then it just can't expect them to not be duplicated?
I suppose it'd be down the reader to store
meta->readsomehwere?
Quoted 8 lines
Quoted 3 lines
prev_read = meta->read ioctl(fd, TRACE_MMAP_IOCTL_GET_READER_PAGE) /* read events from prev_read to meta->read */Yes, in fact it shouldn't need to call the ioctl until after it read it.
Maybe, we should have the ioctl take a parameter of how much was read? To prevent races?
Races would only be with other consuming readers. In that case we'd probably have many other problems anyway as I suppose nothing would prevent another one of swapping the page while our userspace reader is still processing it?
I don't know if this is worth splitting the ABI between the meta-page and the ioctl parameters for this?
Or maybe we should say the meta-page contains things modified by the writer and parameters modified by the reader are passed by the get_reader_page ioctl i.e. the reader page ID and cpu_buffer->reader_page->read? (for the hyp tracing, we have up to 4 registers for the HVC which would replace in our case the ioctl)
Quoted 5 lines
That is, it should pass in the
page->committhat it used to to read the pages.-- Steve
Vincent Donnefort
maintainerRe: [PATCH v2 1/2] ring-buffer: Introducing ring-buffer mapping functions
Quoted 30 lines · Vincent Donnefort
Quoted 16 lines · Steven Rostedt
Quoted 3 lines · On Wed, 29 Mar 2023 14:31:07 +0100 Vincent Donnefort <[email protected]>…
We can say we update
cpu_buffer->reader_page->readon theget_reader_pageioctl, to the most recent value possible, which will have the consequence of actually "flushing" those events?Yes. It should be no different than doing a normal read of the
trace_pipe_rawfile, which does the same.Quoted 6 lines
If the reader decides to read events past this value then it just can't expect them to not be duplicated?
I suppose it'd be down the reader to store
meta->readsomehwere?Quoted 8 lines
Quoted 3 lines
prev_read = meta->read ioctl(fd, TRACE_MMAP_IOCTL_GET_READER_PAGE) /* read events from prev_read to meta->read */Yes, in fact it shouldn't need to call the ioctl until after it read it.
Maybe, we should have the ioctl take a parameter of how much was read? To prevent races?
Races would only be with other consuming readers. In that case we'd probably have many other problems anyway as I suppose nothing would prevent another one of swapping the page while our userspace reader is still processing it?
Quoted 7 lines
I don't know if this is worth splitting the ABI between the meta-page and the ioctl parameters for this?
Or maybe we should say the meta-page contains things modified by the writer and parameters modified by the reader are passed by the
get_reader_pageioctl i.e. the reader page ID andcpu_buffer->reader_page->read? (for the hyp tracing, we have up to 4 registers for the HVC which would replace in our case the ioctl)
Or we can keep everything in the meta-page but update the "reader bits" only during the get_reader_page ioctl.
I can prepare something around those lines for the v3.
Quoted 6 lines
Quoted 6 lines
That is, it should pass in the
page->committhat it used to to read the pages.-- Steve
Steven Rostedt
maintainerRe: [PATCH v2 1/2] ring-buffer: Introducing ring-buffer mapping functions
In reply to Vincent DonnefortQuoted 8 lines · On Wed, 29 Mar 2023 14:55:41 +0100 Vincent Donnefort <[email protected]>…
Quoted 4 lines
Yes, in fact it shouldn't need to call the ioctl until after it read it.
Maybe, we should have the ioctl take a parameter of how much was read? To prevent races?
Races would only be with other consuming readers. In that case we'd probably have many other problems anyway as I suppose nothing would prevent another one of swapping the page while our userspace reader is still processing it?
I'm not worried about user space readers. I'm worried about writers, as the ioctl will update the reader_page->read = reader_page->commit. The time that the reader last read and stopped and then called the ioctl, a writer could fill the page, then the ioctl may even swap the page. By passing in the read amount, the ioctl will know if it needs to keep the same page or not.
Quoted 8 lines
I don't know if this is worth splitting the ABI between the meta-page and the ioctl parameters for this?
Or maybe we should say the meta-page contains things modified by the writer and parameters modified by the reader are passed by the
get_reader_pageioctl i.e. the reader page ID andcpu_buffer->reader_page->read? (for the hyp tracing, we have up to 4 registers for the HVC which would replace in our case the ioctl)
I don't think we need the reader_page id, as that should never move without reader involvement. If there's more than one reader, that's up to the readers to keep track of each other, not the kernel.
Which BTW, the more I look at doing this without ioctls, I think we may need to update things slightly different.
I would keep the current approach, but for clarification of terminology, we have:
meta_data - the data that holds information that is shared between user and
kernel space.
data_pages - this is a separate mapping that holds the mapped ring buffer
pages. In user space, this is one contiguous array and also holds the reader page.
data_index - This is an array of what the writer sees. It maps the index
into data_pages[] of where to find the mapped pages. It does not contain the reader page. We currently map this with the meta_data, but that's not a requirement (although we may continue to do so).
I'm thinking that we make the data_index[] elements into a structure:
struct trace_map_data_index {
int idx; /* index into data_pages[] */
int cnt; /* counter updated by writer */
};The cnt is initialized to zero when initially mapped.
Instead of having the bpage->id = index into data_pages[], have it equal the index into data_index[].
The cpu_buffer->reader_page->id = -1;
meta_data->reader_page = index into data_pages[] of reader page
The swapping of the header page would look something like this:
static inline voidrb_meta_page_head_swap(struct ring_buffer_per_cpu *cpu_buffer)
{
struct ring_buffer_meta_page *meta = cpu_buffer->meta_page;
int head_page;
if (!READ_ONCE(cpu_buffer->mapped))
return;
head_page = meta->data_pages[meta->hdr.data_page_head];
meta->data_pages[meta->hdr.data_page_head] = meta->hdr.reader_page;
meta->hdr.reader_page = head_page;
meta->data_pages[head_page]->id = -1;
}As hdr.data_page_head would be an index into data_index[] and not data_pages[].
The fact that bpage->id points to the data_index[] and not the data_pages[] means that the writer can easily get to that index, and modify the count. That way, in rb_tail_page_update() (between cmpxchgs) we can do something like:
if (cpu_buffer->mapped) {
meta = cpu_buffer->meta_page;
meta->data_index[next_page->id].cnt++;
}And this will allow the reader to know if the current page it is on just got overwritten by the writer, by doing:
prev_id = meta->data_index[this_page].cnt; smp_rmb(); read event (copy it, whatever) smp_rmb(); if (prev_id != meta->data_index[this_page].cnt) /* read data may be corrupted, abort it */
Does this make sense?
-- Steve
Vincent Donnefort
maintainerRe: [PATCH v2 1/2] ring-buffer: Introducing ring-buffer mapping functions
Quoted 12 lines · Steven Rostedt
Quoted 8 lines · On Wed, 29 Mar 2023 14:55:41 +0100 Vincent Donnefort <[email protected]>…
Quoted 4 lines
Yes, in fact it shouldn't need to call the ioctl until after it read it.
Maybe, we should have the ioctl take a parameter of how much was read? To prevent races?
Races would only be with other consuming readers. In that case we'd probably have many other problems anyway as I suppose nothing would prevent another one of swapping the page while our userspace reader is still processing it?
Quoted 6 lines
I'm not worried about user space readers. I'm worried about writers, as the ioctl will update the
reader_page->read=reader_page->commit. The time that the reader last read and stopped and then called the ioctl, a writer could fill the page, then the ioctl may even swap the page. By passing in the read amount, the ioctl will know if it needs to keep the same page or not.
How about?
userspace:
prev_read = meta->read; ioctl(TRACE_MMAP_IOCTL_GET_READER_PAGE)
kernel:
ring_buffer_get_reader_page()
rb_get_reader_page(cpu_buffer);
cpu_buffer->reader_page->read = rb_page_size(reader);
meta->read = cpu_buffer->reader_page->read;userspace:
/* if new page prev_read = 0 */
/* read between prev_read and meta->read */
If the writer does anything in-between, wouldn't rb_get_reader_page() handle it nicely by returning the same reader as more would be there to read?
It is similar to rb_advance_reader() except we'd be moving several events at once?
Quoted 81 lines
Quoted 9 lines
I don't know if this is worth splitting the ABI between the meta-page and the ioctl parameters for this?
Or maybe we should say the meta-page contains things modified by the writer and parameters modified by the reader are passed by the
get_reader_pageioctl i.e. the reader page ID andcpu_buffer->reader_page->read? (for the hyp tracing, we have up to 4 registers for the HVC which would replace in our case the ioctl)I don't think we need the
reader_pageid, as that should never move without reader involvement. If there's more than one reader, that's up to the readers to keep track of each other, not the kernel.Which BTW, the more I look at doing this without ioctls, I think we may need to update things slightly different.
I would keep the current approach, but for clarification of terminology, we have:
meta_data- the data that holds information that is shared between user and
kernel space.
data_pages- this is a separate mapping that holds the mapped ring buffer
pages. In user space, this is one contiguous array and also holds the reader page.
data_index- This is an array of what the writer sees. It maps the index
into data_pages[] of where to find the mapped pages. It does not contain the reader page. We currently map this with themeta_data, but that's not a requirement (although we may continue to do so).I'm thinking that we make the data_index[] elements into a structure:
struct trace_map_data_index { int idx; /* index into data_pages[] */ int cnt; /* counter updated by writer */ };The cnt is initialized to zero when initially mapped.
Instead of having the
bpage->id= index into data_pages[], have it equal the index into data_index[].The
cpu_buffer->reader_page->id= -1;
meta_data->reader_page= index into data_pages[] of reader pageThe swapping of the header page would look something like this:
static inline void
rb_meta_page_head_swap(struct ring_buffer_per_cpu *cpu_buffer){ struct ring_buffer_meta_page *meta = cpu_buffer->meta_page; int head_page; if (!READ_ONCE(cpu_buffer->mapped)) return; head_page = meta->data_pages[meta->hdr.data_page_head]; meta->data_pages[meta->hdr.data_page_head] = meta->hdr.reader_page; meta->hdr.reader_page = head_page; meta->data_pages[head_page]->id = -1; }As hdr.data_page_head would be an index into data_index[] and not data_pages[].
The fact that
bpage->idpoints to the data_index[] and not the data_pages[] means that the writer can easily get to that index, and modify the count. That way, inrb_tail_page_update()(between cmpxchgs) we can do something like:if (cpu_buffer->mapped) { meta = cpu_buffer->meta_page; meta->data_index[next_page->id].cnt++; }And this will allow the reader to know if the current page it is on just got overwritten by the writer, by doing:
Quoted 6 lines
prev_id = meta->data_index[this_page].cnt; smp_rmb(); read event (copy it, whatever) smp_rmb(); if (prev_id != meta->data_index[this_page].cnt) /* read data may be corrupted, abort it */
Couldn't the reader just check for the page commit field? rb_iter_head_event() does something like this to check if the writer is on its page.
Quoted 5 lines
Does this make sense?
-- Steve
Steven Rostedt
maintainerRe: [PATCH v2 1/2] ring-buffer: Introducing ring-buffer mapping functions
Quoted 14 lines · On Thu, 30 Mar 2023 11:30:51 +0100 Vincent Donnefort <[email protected]>…
How about?
userspace:
prev_read = meta->read; ioctl(TRACE_MMAP_IOCTL_GET_READER_PAGE)kernel:
ring_buffer_get_reader_page() rb_get_reader_page(cpu_buffer); cpu_buffer->reader_page->read = rb_page_size(reader); meta->read = cpu_buffer->reader_page->read;userspace:
Quoted 8 lines
/* if new page prev_read = 0 */ /* read between prev_read and meta->read */If the writer does anything in-between, wouldn't
rb_get_reader_page()handle it nicely by returning the same reader as more would be there to read?It is similar to
rb_advance_reader()except we'd be moving several events at once?
Yeah, I think that can work. So we just need to make sure that the meta page has the "read" variable passed through.
-- Steve
Vincent Donnefort
maintainerPATCH v2 2/2 tracing: Allow user-space mapping of the ring-buffer
In reply to Vincent DonnefortCurrently, user-space extracts data from the ring-buffer via splice, which is handy for storage or network sharing. However, due to splice limitations, it is imposible to do real-time analysis without a copy.
A solution for that problem is to let the user-space map the ring-buffer directly.
The mapping exposed via the per-CPU file trace_pipe_raw. The first page is the meta-page and is followed by each page of the ring-buffer, ordered by their unique page ID. It is therefore easy to translate a page-ID to an offset in the mapping.
* Meta-page -- include/uapi/linux/trace_mmap.h for a description
* Page ID 0
* Page ID 1
...
The mapper must then do what use to be the kernel jobs: swap the reader with the head. This is done with a newly introduced ioctl:TRACE_MMAP_IOCTL_GET_READER_PAGE.
Entries, pages_touched and overrun fields are automatically updated by the writer. Only readers keep the head page field updated. An additional ioctl TRACE_MMAP_IOCTL_UPDATE_META_PAGE allows to query that update, enabling non-consuming read from userspace.
include/uapi/linux/trace_mmap.h +3 -0
@@ -22,4 +22,7 @@ struct ring_buffer_meta_page_header { __u32 data_start; /* offset within the meta page */ }; +#define TRACE_MMAP_IOCTL_GET_READER_PAGE _IO('T', 0x1) +#define TRACE_MMAP_IOCTL_UPDATE_META_PAGE _IO('T', 0x2) +#endif /* _UAPI_TRACE_MMAP_H_ */
kernel/trace/trace.c +73 -3
107 lines not on this page · raw message
Signature · 1 line
2.40.0.rc1.284.g88254d51c5-goog