RFC relay revamp v5
Get series with b4
b4 am 1223270564.7054.75.camel@charm-linuxVersions newest first
- v1 RFC Latest1 patch 2 repliesIn review
Patches in v1 No applies recorded yet
| # | Subject and files | Size | Replies | Applied |
|---|---|---|---|---|
| 1/1 | relay revamp v5block/ include/ include/ +5 more | +1466 | 2 replies |
Reviews trailers per patch
No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.
Cover letter [RFC PATCH 0/1] relay revamp v5
I decided to go another round with this after all...
The patch following this mail contains the full patch; because there have been so many changes and it's hard to see from just looking at the patch the end result, I'm including relay.c and relay.h at the end of this mail. The full patch also includes the two new files, relay_pagewriter.c and .h in for anyone interested in seeing what those look like.
Basically the patch includes the changes from the previous 11 that I posted and in addition completely separates the reading part of relay from the writing part. With the new changes, relay really does become just what its name says and and nothing more - it accepts pages from tracers, and relays the data to userspace via read(2) or splice(2) (and therefore sendfile(2)). It doesn't allocate any buffer space and provides no write functions - those are expected to be supplied by some other component such as possibly the unified ring-buffer or any other tracer that might want relay pages of trace data to userspace.
One example of such a component would be the original relay write functions and buffers (the no-vmap page-based versions of the previous patchset), which have been split out into a new file called relay_pagewriter.c and provide one means of writing into pages and feeding them into relay. blktrace and kvmtrace have been 'ported' over to using pagewriter instead of relay directly.
I've only tested the new relay lightly via blktrace, which seems to work fine, and haven't looked at plugging anything else into it, but after applying the full patch you should be able to use it to stream e.g. ftrace/unified trace buffer/ring-buffer trace data to disk or over the network...
Anyway, here's a brief overview of the new API (see code for details):
- relay_open():
Creates a per-cpu relay channel and by default associates debugfs files with each per-cpu 'buffer'. No buffer space is allocated for the 'buffers', rather they collect pages added by tracers in a list which is drained by read()/splice(), etc. Tracers add pages to the 'buffers' using relay_write_page() and relay_write_pages(). One of the parameters to relay_open() is n_pages_wakeup, which specifies that readers should be woken up every time n_pages have been added; if this is 0, readers are never woken up.
- relay_add_page():
Adds a page of trace data to relay. After it has been consumed by userspace, the tracer is notified by the 'relay page' callback function page_released(). The page passed via the callback can then be re-used by the tracer (see for example the pagewriter code, which simply adds the page back into pagewriter's per-cpu page pool). If the page has been stolen instead (if SPLICE_F_MOVE succeeded, which can't happen in current kernels since support for it isn't there), the page_stolen() callback is called, at which point the tracer can allocate a new page to replace the stolen page (see the pagewriter code, which does this too).
- relay_add_pages():
The same as relay_add_page(), but adds a set of pages to relay and guarantees that they'll stay together and remain in the same order they were added.
- relay_close():
Releases unread pages to the tracer(s) and frees the channel.
- relay_flush():
Wakes up readers.
- relay_reset():
Releases unread pages to the tracer(s) and resets the channel state.
That's basically the entire kernel API; the userspace API is of course just read(), splice(), and sendfile().
Tom
linux/relay.h new file +170 -0
@@ -0,0 +1,170 @@ +/* +* linux/include/linux/relay.h +* +* Copyright (C) 2002, 2003 - Tom Zanussi ([email protected]), IBM Corp +* Copyright (C) 1999, 2000, 2001, 2002 - Karim Yaghmour ([email protected]) +* Copyright (C) 2008 - Tom Zanussi ([email protected]) +* +* CONFIG_RELAY definitions and declarations +*/ + +#ifndef _LINUX_RELAY_H +#define _LINUX_RELAY_H + +#include <linux/types.h> +#include <linux/sched.h> +#include <linux/timer.h> +#include <linux/wait.h> +#include <linux/list.h> +#include <linux/fs.h> +#include <linux/poll.h> +#include <linux/kref.h> +#include <linux/pagevec.h> + +/* +* relay channel flags +*/ +#define RCHAN_GLOBAL_BUFFER 0x00000001/* not using per-cpu */ + +/* +* For page lists +*/ +struct relay_page { +struct page *page; +struct list_head list; +struct relay_page_callbacks *cb; +void *private_data; +}; + +/* +* Per-cpu relay channel buffer +*/ +struct rchan_buf { +struct rchan *chan;/* associated channel */ +wait_queue_head_t read_wait;/* reader wait queue */ +struct timer_list timer;/* reader wake-up timer */ +struct dentry *dentry;/* channel file dentry */ +struct kref kref;/* channel buffer refcount */ +struct list_head pages;/* current set of unconsumed pages */ +size_t nr_pages;/* number of unconsumed pages */ +spinlock_t lock;/* protect pages list */ +size_t consumed_offset;/* bytes consumed in cur page */ +unsigned int finalized;/* buffer has been finalized */ +unsigned int cpu;/* this buf's cpu */ +}____cacheline_aligned ; + +/* +* Relay channel data structure +*/ +struct rchan +{ +size_t n_pages_wakeup;/* wake up readers after filling n */ +struct rchan_callbacks *cb;/* client callbacks */ +struct kref kref;/* channel refcount */ +void *private_data;/* for user-defined data */ +struct rchan_buf *buf[NR_CPUS ];/* per-cpu channel buffers */ +struct list_head list;/* for channel list */ +struct dentry *parent;/* parent dentry passed to open */ +char base_filename[NAME_MAX ];/* saved base filename */ +unsigned long flags;/* relay flags for this channel */ +}; + +/* +* Relay channel client callbacks +*/ +struct rchan_callbacks +{ +/* +* create_buf_file - create file to represent a relay channel buffer +* @filename: the name of the file to create +* @parent: the parent of the file to create +* @mode: the mode of the file to create +* @buf: the channel buffer +* +* Called during relay_open(), once for each per-cpu buffer, +* to allow the client to create a file to be used to +* represent the corresponding channel buffer. If the file is +* created outside of relay, the parent must also exist in +* that filesystem. +* +* The callback should return the dentry of the file created +* to represent the relay buffer. +* +* See Documentation/filesystems/relayfs.txt for more info. +*/ +struct dentry *(*create_buf_file)(const char *filename, +struct dentry *parent, +int mode, +struct rchan_buf *buf); + +/* +* remove_buf_file - remove file representing a relay channel buffer +* @dentry: the dentry of the file to remove +* +* Called during relay_close(), once for each per-cpu buffer, +* to allow the client to remove a file used to represent a +* channel buffer. +* +* The callback should return 0 if successful, negative if not. +*/ +int (*remove_buf_file)(struct dentry *dentry); +}; + +/* +* Relay page callbacks +*/ +struct relay_page_callbacks +{ +/* +* page_released - notification that a page is ready for re-use +* @page: the released page +* @private_data: user-defined data associated with the page +* +* This callback is a notification that a given page has been +* read by userspace and can be re-used. Always called in +* user context. +*/ +void (*page_released) (struct page *page,void *private_data); + +/* +* page_released - notification that a page has been stolen +* @page: the stolen page +* @private_data: user-defined data associated with the page +* +* This callback is a notification that a given page has been +* stolen by userspace. The owner may wish to replace it; +* this gives it the opportunity to do so. Always called in +* user context. +*/ +void (*page_stolen) (struct page *page,void *private_data); +}; + +/* +* CONFIG_RELAY kernel API, kernel/relay.c +*/ + +extern struct rchan *relay_open (const char *base_filename, +struct dentry *parent, +size_t n_pages_wakeup, +struct rchan_callbacks *cb, +void *private_data, +unsigned long rchan_flags); +extern void relay_add_page (struct rchan *chan, +struct page *page, +struct relay_page_callbacks *cb, +void *private_data); +extern void relay_add_pages (struct rchan *chan, +struct pagevec *pages, +struct relay_page_callbacks *cb, +void *private_data); +extern void relay_flush (struct rchan *chan); +extern void relay_close (struct rchan *chan); +extern void relay_reset (struct rchan *chan); + +/* +* exported relay file operations, kernel/relay.c +*/ +extern const struct file_operations relay_file_operations; + +#endif /* _LINUX_RELAY_H */ +
relay.c new file +969 -0
969 lines not on this page · raw message