Introducing trace buffer mapping by user-space
Subsystems (4)
Vincent Donnefort
maintainerv7 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 userspace direct access to the ring-buffer pages via a mapping. An application can now become a consumer of the ring-buffer, in a similar fashion to what trace_pipe offers.
Attached to this cover letter an example of consuming read for a ring-buffer, using libtracefs.
Vincent
v6 -> v7:
* Rebase onto lore.kernel.org/lkml/[email protected]/
* Support for subbufs
* Rename subbufs into bpages
v5 -> v6:
* Rebase on next-20230802.
* (unsigned long) -> (void *) cast for virt_to_page().
* Add a wait for the GET_READER_PAGE ioctl.
* Move writer fields update (overrun/pages_lost/entries/pages_touched) in the irq_work.
* Rearrange id in struct buffer_page.
* Rearrange the meta-page.
* ring_buffer_meta_page -> trace_buffer_meta_page.
* Add meta_struct_len into the meta-page.
v4 -> v5:
* Trivial rebase onto 6.5-rc3 (previously 6.4-rc3)
v3 -> v4:
* Add to the meta-page:
- pages_lost / pages_read (allow to compute how full is the ring-buffer)
- read (allow to compute how many entries can be read)
- A reader_page struct.
* Rename ring_buffer_meta_header -> ring_buffer_meta
* Rename ring_buffer_get_reader_page -> ring_buffer_map_get_reader_page
* Properly consume events on ring_buffer_map_get_reader_page() with rb_advance_reader().
v2 -> v3:
* Remove data page list (for non-consuming read)
** Implies removing order > 0 meta-page
* Add a new meta page field ->read
* Rename ring_buffer_meta_page_header into ring_buffer_meta_header
v1 -> v2:
* Hide data_pages from the userspace struct
* Fix META_PAGE_MAX_PAGES
* Support for order > 0 meta-page
* Add missing page->mapping.
---
/* 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);
};struct trace_buffer_meta {
unsigned long entries;
unsigned long overrun;
unsigned long read;
unsigned long bpages_touched;
unsigned long bpages_lost;
unsigned long bpages_read;
struct {
unsigned long lost_events; /* Events lost at the time of the reader swap */
__u32 id; /* Reader bpage ID from 0 to nr_bpages - 1 */
__u32 read; /* Number of bytes read on the reader bpage */
} reader;
__u32 bpage_size; /* Size of each buffer page including the header */
__u32 nr_bpages; /* Number of buffer pages in the ring-buffer */
__u32 meta_page_size; /* Size of the meta-page */
__u32 meta_struct_len; /* Len of this struct */
};static char *argv0;
static bool exit_requested;
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)
{
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))) {
record.size = kbuffer_event_size(kbuf);
kbuffer_next_event(kbuf, NULL);
tep_print_event(tep, &seq, &record,
"%s-%d %9d\t%s: %s", TEP_PRINT_COMM,
TEP_PRINT_PID, TEP_PRINT_TIME, TEP_PRINT_NAME,
TEP_PRINT_INFO);
trace_seq_do_printf(&seq);
trace_seq_reset(&seq);
}
}static int next_reader_subbuf(int fd, struct trace_buffer_meta *meta, unsigned long *read)
{
__u32 prev_read, prev_reader, new_reader;
prev_read = READ_ONCE(meta->reader.read);
prev_reader = READ_ONCE(meta->reader.id);
if (ioctl(fd, TRACE_MMAP_IOCTL_GET_READER) < 0)
pdie("ioctl");
new_reader = READ_ONCE(meta->reader.id);
if (prev_reader != new_reader) {
*read = 0;
printf("NEW READER PAGE: %d\n", new_reader);
} else
*read = prev_read;
return new_reader;
}static void signal_handler(int unused)
{
exit_requested = true;
}int main(int argc, char **argv)
{
int page_size, meta_len, data_len, subbuf, fd;
struct trace_buffer_meta *map;
struct tep_handle *tep;
struct kbuffer *kbuf;
unsigned long read;
void *meta, *data;
char path[32];
int cpu;
if (argc != 2)
return -EINVAL;
argv0 = argv[0];
cpu = atoi(argv[1]);
snprintf(path, 32, "per_cpu/cpu%d/trace_pipe_raw", cpu);
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 = (struct trace_buffer_meta *)meta;
meta_len = map->meta_page_size;
printf("entries: %lu\n", map->entries);
printf("overrun: %lu\n", map->overrun);
printf("read: %lu\n", map->read);
printf("bpages_touched: %lu\n", map->bpages_touched);
printf("bpages_lost: %lu\n", map->bpages_lost);
printf("bpages_read: %lu\n", map->bpages_read);
printf("bpage_size: %u\n", map->bpage_size);
printf("nr_bpages: %u\n", map->nr_bpages);
data_len = map->bpage_size * map->nr_bpages;
data = mmap(NULL, data_len, PROT_READ, MAP_SHARED, fd, meta_len);
if (data == MAP_FAILED)
pdie("mmap data");
signal(SIGINT, signal_handler);
while (!exit_requested) {
subbuf = next_reader_subbuf(fd, map, &read);
kbuffer_load_subbuffer(kbuf, data + map->bpage_size * subbuf);
while (kbuf->curr < read)
kbuffer_next_event(kbuf, NULL);
read_page(tep, kbuf);
}
munmap(data, data_len);
munmap(meta, page_size);
close(fd);
return 0;
}Vincent Donnefort (2):
ring-buffer: Introducing ring-buffer mapping functions
tracing: Allow user-space mapping of the ring-buffer
include/linux/ring_buffer.h | 7 + include/uapi/linux/trace_mmap.h | 31 +++ kernel/trace/ring_buffer.c | 373 +++++++++++++++++++++++++++++++- kernel/trace/trace.c | 79 ++++++- 4 files changed, 486 insertions(+), 4 deletions(-) create mode 100644 include/uapi/linux/trace_mmap.h
Signature · 1 line
2.43.0.472.g3155946c3a-goog
Vincent Donnefort
maintainerPATCH v7 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_map_get_reader() /* swap reader and head */
Mapping the ring-buffer also involves:
A unique ID for each buffer page of the ring-buffer, currently they are only identified through their in-kernel VA.
A meta-page, where are stored ring-buffer statistics and a description for the current reader
The linear mapping exposes the meta-page, and each bpage of the ring-buffer, ordered following their unique ID, assigned during the first mapping.
Once mapped, no bpage 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 the buffer pages.
include/linux/ring_buffer.h +7 -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 ;
@@ -221,4 +223,9 @@ 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_map_get_reader (struct trace_buffer *buffer,int cpu);#endif /* _LINUX_RING_BUFFER_H */
include/uapi/linux/trace_mmap.h new file +29 -0
@@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +#ifndef _UAPI_TRACE_MMAP_H_ +#define _UAPI_TRACE_MMAP_H_ + +#include <linux/types.h> + +struct trace_buffer_meta { +unsigned long entries; +unsigned long overrun; +unsigned long read; + +unsigned long bpages_touched; +unsigned long bpages_lost; +unsigned long bpages_read; + +struct { +unsigned long lost_events;/* Events lost at the time of the reader swap */ +__u32 id;/* Reader bpage ID from 0 to nr_bpages - 1 */ +__u32 read;/* Number of bytes read on the reader bpage */ + } reader; + +__u32 bpage_size;/* Size of each buffer page including the header */ +__u32 nr_bpages;/* Number of buffer pages in the ring-buffer */ + +__u32 meta_page_size;/* Size of the meta-page */ +__u32 meta_struct_len;/* Len of this struct */ +}; + +#endif /* _UAPI_TRACE_MMAP_H_ */
kernel/trace/ring_buffer.c +372 -1
451 lines not on this page · raw message
Signature · 1 line
2.43.0.472.g3155946c3a-goog
Vincent Donnefort
maintainerPATCH v7 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 is exposed via the per-CPU file trace_pipe_raw. The first element of the mapping is the meta-page. It is followed by each bpage constituting the ring-buffer, ordered by their unique page ID:
* Meta-page -- include/uapi/linux/trace_mmap.h for a description
* buffer-page ID 0
* buffer-page ID 1
...
It is therefore easy to translate a buffer page ID into an offset in the mapping:
reader_id = meta->reader->id; reader_offset = meta->meta_page_size + reader_id * meta->bpage_size;
When new data is available, the mapper must call a newly introduced ioctl:TRACE_MMAP_IOCTL_GET_READER. This will update the Meta-page reader ID to point to the next reader containing unread data.
include/uapi/linux/trace_mmap.h +2 -0
@@ -26,4 +26,6 @@ struct trace_buffer_meta { __u32 meta_struct_len; /* Len of this struct */ }; +#define TRACE_MMAP_IOCTL_GET_READER _IO('T', 0x1) +#endif /* _UAPI_TRACE_MMAP_H_ */
kernel/trace/trace.c +76 -3
103 lines not on this page · raw message
Signature · 1 line
2.43.0.472.g3155946c3a-goog
Vincent Donnefort
maintainerv7 cover ring-buffer: Rename sub-buffer into buffer page
In reply to Vincent DonnefortPreviously was introduced the ability to change the ring-buffer page size. It also introduced the concept of sub-buffer that is, a contiguous virtual memory space which can now be bigger than the system page size (4K on most systems). But behind the scene this is really just a page with an order > 0 and a struct buffer_page (often refered as "bpage") already exists. We have then an unnecessary duplicate subbuffer == bpage.
Remove all references to sub-buffer and replace them with either bpage or ring_buffer_page.
---
I forgot this patch when sending the v7 :-(
Documentation/trace/ftrace.rst +20 -20
@@ -203,26 +203,26 @@ of ftrace. Here is a list of some of the key files: This displays the total combined size of all the trace buffers. - buffer_subbuf_size_kb: - - This sets or displays the sub buffer size. The ring buffer is broken up - into several same size "sub buffers". An event can not be bigger than - the size of the sub buffer. Normally, the sub buffer is the size of the - architecture's page (4K on x86). The sub buffer also contains meta data - at the start which also limits the size of an event. That means when - the sub buffer is a page size, no event can be larger than the page - size minus the sub buffer meta data. - - Note, the buffer_subbuf_size_kb is a way for the user to specify the - minimum size of the subbuffer. The kernel may make it bigger due to the - implementation details, or simply fail the operation if the kernel can - not handle the request. - - Changing the sub buffer size allows for events to be larger than the - page size. - - Note: When changing the sub-buffer size, tracing is stopped and any - data in the ring buffer and the snapshot buffer will be discarded. + buffer_page_size_kb: + + This sets or displays the ring-buffer page size. The ring buffer is + broken up into several same size "buffer pages". An event can not be + bigger than the size of the buffer page. Normally, the buffer page is + the size of the architecture's page (4K on x86). The buffer page also + contains meta data at the start which also limits the size of an event. + That means when the buffer page is a system page size, no event can be + larger than the system page size minus the buffer page meta data. + + Note, the buffer_page_size_kb is a way for the user to specify the + minimum size for each buffer page. The kernel may make it bigger due to + the implementation details, or simply fail the operation if the kernel + can not handle the request. + + Changing the ring-buffer page size allows for events to be larger than + the system page size. + + Note: When changing the buffer page size, tracing is stopped and any + data in the ring buffer and the snapshot buffer will be discarded. free_buffer:
include/linux/ring_buffer.h +3 -3
@@ -207,9 +207,9 @@ struct trace_seq;int ring_buffer_print_entry_header (struct trace_seq *s);int ring_buffer_print_page_header (struct trace_buffer *buffer,struct trace_seq *s); -int ring_buffer_subbuf_order_get (struct trace_buffer *buffer); -int ring_buffer_subbuf_order_set (struct trace_buffer *buffer,int order); -int ring_buffer_subbuf_size_get (struct trace_buffer *buffer); +int ring_buffer_page_order_get (struct trace_buffer *buffer); +int ring_buffer_page_order_set (struct trace_buffer *buffer,int order); +int ring_buffer_page_size_get (struct trace_buffer *buffer);enum ring_buffer_flags {RB_FL_OVERWRITE =1 <<0 ,
kernel/trace/ring_buffer.c +46 -46
289 lines not on this page · raw message
kernel/trace/ring_buffer_benchmark.c +1 -1
@@ -118,7 +118,7 @@ static enum event_status read_page(int cpu)if (IS_ERR (bpage))return EVENT_DROPPED ; - page_size =ring_buffer_subbuf_size_get (buffer); + page_size =ring_buffer_page_size_get (buffer); ret =ring_buffer_read_page (buffer, bpage, page_size, cpu,1 );if (ret >=0 ) { rpage =ring_buffer_read_page_data (bpage);
kernel/trace/trace.c +18 -18
@@ -1269,8 +1269,8 @@ int tracing_alloc_snapshot_instance(struct trace_array *tr)if (!tr->allocated_snapshot) {/* Make the snapshot buffer have the same order as main buffer */ - order =ring_buffer_subbuf_order_get (tr->array_buffer.buffer); - ret =ring_buffer_subbuf_order_set (tr->max_buffer.buffer, order); + order =ring_buffer_page_order_get (tr->array_buffer.buffer); + ret =ring_buffer_page_order_set (tr->max_buffer.buffer, order);if (ret <0 )return ret;
@@ -1293,7 +1293,7 @@ static void free_snapshot(struct trace_array *tr)* The max_tr ring buffer has some state (e.g. ring->clock) and * we want preserve it. */ -ring_buffer_subbuf_order_set (tr->max_buffer.buffer,0 ); +ring_buffer_page_order_set (tr->max_buffer.buffer,0 );ring_buffer_resize (tr->max_buffer.buffer,1 ,RING_BUFFER_ALL_CPUS );set_buffer_entries (&tr->max_buffer,1 );tracing_reset_online_cpus (&tr->max_buffer);
@@ -8315,7 +8315,7 @@ tracing_buffers_read(struct file *filp, char __user *ubuf,return -EBUSY ;#endif - page_size =ring_buffer_subbuf_size_get (iter->array_buffer->buffer); + page_size =ring_buffer_page_size_get (iter->array_buffer->buffer);/* Make sure the spare matches the current sub buffer size */ if (info->spare) {
@@ -8492,7 +8492,7 @@ tracing_buffers_splice_read(struct file *file, loff_t *ppos,return -EBUSY ;#endif - page_size =ring_buffer_subbuf_size_get (iter->array_buffer->buffer); + page_size =ring_buffer_page_size_get (iter->array_buffer->buffer);if (*ppos & (page_size -1 ))return -EINVAL ;
@@ -9391,7 +9391,7 @@ static const struct file_operations buffer_percent_fops = { };static ssize_t -buffer_subbuf_size_read (struct file *filp,char __user *ubuf,size_t cnt,loff_t *ppos) +buffer_page_size_read (struct file *filp,char __user *ubuf,size_t cnt,loff_t *ppos) {struct trace_array *tr = filp->private_data;size_t size;
@@ -9399,7 +9399,7 @@ buffer_subbuf_size_read(struct file *filp, char __user *ubuf, size_t cnt, loff_tint order;int r; - order =ring_buffer_subbuf_order_get (tr->array_buffer.buffer); + order =ring_buffer_page_order_get (tr->array_buffer.buffer); size = (PAGE_SIZE << order) /1024 ; r =sprintf (buf,"%zd\n" , size);
@@ -9408,8 +9408,8 @@ buffer_subbuf_size_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t }static ssize_t -buffer_subbuf_size_write (struct file *filp,const char __user *ubuf, -size_t cnt,loff_t *ppos) +buffer_page_size_write (struct file *filp,const char __user *ubuf, +size_t cnt,loff_t *ppos) {struct trace_array *tr = filp->private_data;unsigned long val;
@@ -9434,11 +9434,11 @@ buffer_subbuf_size_write(struct file *filp, const char __user *ubuf,/* Do not allow tracing while changing the order of the ring buffer */ tracing_stop_tr (tr); - old_order =ring_buffer_subbuf_order_get (tr->array_buffer.buffer); + old_order =ring_buffer_page_order_get (tr->array_buffer.buffer);if (old_order == order) goto out; - ret =ring_buffer_subbuf_order_set (tr->array_buffer.buffer, order); + ret =ring_buffer_page_order_set (tr->array_buffer.buffer, order);if (ret) goto out;
@@ -9447,10 +9447,10 @@ buffer_subbuf_size_write(struct file *filp, const char __user *ubuf,if (!tr->allocated_snapshot) goto out_max; - ret =ring_buffer_subbuf_order_set (tr->max_buffer.buffer, order); + ret =ring_buffer_page_order_set (tr->max_buffer.buffer, order);if (ret) {/* Put back the old order */ - cnt =ring_buffer_subbuf_order_set (tr->array_buffer.buffer, old_order); + cnt =ring_buffer_page_order_set (tr->array_buffer.buffer, old_order);if (WARN_ON_ONCE (cnt)) {/* * AARGH! We are left with different orders!
@@ -9479,10 +9479,10 @@ buffer_subbuf_size_write(struct file *filp, const char __user *ubuf,return cnt; } -static const struct file_operations buffer_subbuf_size_fops = { +static const struct file_operations buffer_page_size_fops = { .open = tracing_open_generic_tr, - .read = buffer_subbuf_size_read, - .write = buffer_subbuf_size_write, + .read = buffer_page_size_read, + .write = buffer_page_size_write, .release = tracing_release_generic_tr, .llseek = default_llseek, };
@@ -9953,8 +9953,8 @@ init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer)trace_create_file ("buffer_percent" ,TRACE_MODE_WRITE , d_tracer, tr, &buffer_percent_fops); -trace_create_file ("buffer_subbuf_size_kb" ,TRACE_MODE_WRITE , d_tracer, - tr, &buffer_subbuf_size_fops); +trace_create_file ("buffer_page_size_kb" ,TRACE_MODE_WRITE , d_tracer, + tr, &buffer_page_size_fops);create_trace_options_dir (tr);
Signature · 1 line
2.43.0.472.g3155946c3a-goog
Steven Rostedt
maintainerRe: [PATCH v7 0/2] ring-buffer: Rename sub-buffer into buffer page
Quoted 10 lines · On Mon, 18 Dec 2023 15:46:18 +0000 Vincent Donnefort <[email protected]>…
Previously was introduced the ability to change the ring-buffer page size. It also introduced the concept of sub-buffer that is, a contiguous virtual memory space which can now be bigger than the system page size (4K on most systems). But behind the scene this is really just a page with an order > 0 and a struct
buffer_page(often refered as "bpage") already exists. We have then an unnecessary duplicate subbuffer == bpage.Remove all references to sub-buffer and replace them with either bpage or
ring_buffer_page.
No! I think you misunderstood me before.
I want it the other way around. In Linux, "page" usually means the architecture page (4096 on x86). If we are going to remove the duplicate of subbuffer == bpage, remove the "page" part, *not* the subbuffer part. The "subbuf" part is part of the design, not an implementation detail. The page part, was just an implementation detail that we do not want to expose!
When you say "page" you mean a single page. If you say buffer_page_size, one will think this is an architecture page, not the subbuffer size.
The ring buffer is broken up into subbuffers, The page is just an implementation detail. Let's not confuse that with the design.
Thanks,
-- Steve
Steven Rostedt
maintainerRe: [PATCH v7 0/2] ring-buffer: Rename sub-buffer into buffer page
Quoted 2 lines · On Mon, 18 Dec 2023 11:28:17 -0500 Steven Rostedt <[email protected]> wrote:
Quoted 2 lines
Remove all references to sub-buffer and replace them with either bpage or
ring_buffer_page.
The user interface should not be changed.
But what I would like to have changed (and this will come after all other changes are complete), is to do the following renaming:
original structure name new name variable reference
----------------------- -------- ------------------
struct buffer_data_page struct buffer_data bdata (was bpage)
struct buffer_page struct buffer_subbuf subbuf (was also bpage)
Also change:
struct buffer_page {
[..]
struct buffer_data_page *page; /* Actual data page */
};Into:
struct buffer_subbuf {
[..]
struct buffer_data *data; /* Actual data */
};Then we can do a global rename of functions like:
free_buffer_page() -> free_buffer_subbuf()
And things like rb_init_page() into rb_init_subbuf()
__rb_allocate_pages() -> rb_allocate_subbufs()
etc.
This should be broken up into steps of changes:
1. Rename the struct buffer_data_page and bpage->bdata
2. Rename the struct buffer_page and bpage->subbuf
3. Rename static functions
And I just realized that the two functions exposed outside of the file are not used anywhere else. We can remove one and make the other one static (as it is used within the file).
ring_buffer_nr_pages() -> delete ring_buffer_nr_dirty_pages -> convert to static rb_nr_dirty_pages()
Thanks,
-- Steve