GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||
GeezerVison is showing docs.kernel.org — relay interface (formerly relayfs) — The Linux Kernel documentation relay interface (formerly relayfs)¶The relay interface provides a means for kernel applications to efficiently log and transfer large quantities of data from the kernel to userspace via user-defined ‘relay channels’. A ‘relay channel’ is a kernel->user data relay mechanism implemented as a set of per-cpu kernel buffers (‘channel buffers’), each represented as a regular file (‘relay file’) in user space. Kernel clients write into the channel buffers using efficient write functions; these automatically log into the current cpu’s channel buffer. User space applications mmap() or read() from the relay files and retrieve the data as it becomes available. The relay files themselves are files created in a host filesystem, e.g. debugfs, and are associated with the channel buffers using the API described below. The format of the data logged into the channel buffers is completely up to the kernel client; the relay interface does however provide hooks which allow kernel clients to impose some structure on the buffer data. The relay interface doesn’t implement any form of data filtering - this also is left to the kernel client. The purpose is to keep things as simple as possible. This document provides an overview of the relay interface API. The details of the function parameters are documented along with the functions in the relay interface code - please see that for details. Semantics¶Each relay channel has one buffer per CPU; each buffer has one or more sub-buffers. Messages are written to the first sub-buffer until it is too full to contain a new message, in which case it is written to the next (if available). Messages are never split across sub-buffers. At this point, userspace can be notified so it empties the first sub-buffer, while the kernel continues writing to the next. When notified that a sub-buffer is full, the kernel knows how many bytes of it are padding, i.e., unused space occurring because a complete message couldn’t fit into a sub-buffer. Userspace can use this knowledge to copy only valid data. After copying it, userspace can notify the kernel that a sub-buffer has been consumed. A relay channel can operate in a mode where it will overwrite data not yet collected by userspace, and not wait for it to be consumed. The relay channel itself does not provide for communication of such data between userspace and kernel, allowing the kernel side to remain simple and not impose a single interface on userspace. It does provide a set of examples and a separate helper though, described below. The read() interface both removes padding and internally consumes the read sub-buffers; thus in cases where read(2) is being used to drain the channel buffers, special-purpose communication between kernel and user isn’t necessary for basic operation. One of the major goals of the relay interface is to provide a low overhead mechanism for conveying kernel data to userspace. While the read() interface is easy to use, it’s not as efficient as the mmap() approach; the example code attempts to make the tradeoff between the two approaches as small as possible. klog and relay-apps example code¶The relay interface itself is ready to use, but to make things easier, a couple of simple utility functions and a set of examples are provided. The relay-apps example tarball, available on the relay sourceforge site, contains a set of self-contained examples, each consisting of a pair of .c files containing boilerplate code for each of the user and kernel sides of a relay application. When combined these two sets of boilerplate code provide glue to easily stream data to disk, without having to bother with mundane housekeeping chores. The ‘klog debugging functions’ patch (klog.patch in the relay-apps tarball) provides a couple of high-level logging functions to the kernel which allow writing formatted text or raw data to a channel, regardless of whether a channel to write into exists or not, or even whether the relay interface is compiled into the kernel or not. These functions allow you to put unconditional ‘trace’ statements anywhere in the kernel or kernel modules; only when there is a ‘klog handler’ registered will data actually be logged (see the klog and kleak examples for details). It is of course possible to use the relay interface from scratch, i.e., without using any of the relay-apps example code or klog, but you’ll have to implement communication between userspace and kernel, allowing both to convey the state of buffers (full, empty, amount of padding). The read() interface both removes padding and internally consumes the read sub-buffers; thus in cases where read(2) is being used to drain the channel buffers, special-purpose communication between kernel and user isn’t necessary for basic operation. Things such as buffer-full conditions would still need to be communicated via some channel though. klog and the relay-apps examples can be found in the relay-apps tarball on http://relayfs.sourceforge.net The relay interface user space API¶The relay interface implements basic file operations for user space access to relay channel buffer data. Here are the file operations that are available and some comments regarding their behavior:
In order for a user application to make use of relay files, the host filesystem must be mounted. For example: mount -t debugfs debugfs /sys/kernel/debug Note The host filesystem doesn’t need to be mounted for kernel clients to create or use channels - it only needs to be mounted when user space applications need access to the buffer data. The relay interface kernel API¶Here’s a summary of the API the relay interface provides to in-kernel clients:
Creating a channel¶
In order for a channel to be created and the host filesystem’s files
associated with its channel buffers, the user must provide definitions
for two callback functions, Here are some typical definitions for these callbacks, in this case using debugfs: /*
* create_buf_file() callback. Creates relay file in debugfs.
*/
static struct dentry *create_buf_file_handler(const char *filename,
struct dentry *parent,
umode_t mode,
struct rchan_buf *buf,
int *is_global)
{
return debugfs_create_file(filename, mode, parent, buf,
&relay_file_operations);
}
/*
* remove_buf_file() callback. Removes relay file from debugfs.
*/
static int remove_buf_file_handler(struct dentry *dentry)
{
debugfs_remove(dentry);
return 0;
}
/*
* relay interface callbacks
*/
static struct rchan_callbacks relay_callbacks =
{
.create_buf_file = create_buf_file_handler,
.remove_buf_file = remove_buf_file_handler,
};
And an example chan = relay_open("cpu", NULL, SUBBUF_SIZE, N_SUBBUFS, &relay_callbacks, NULL);
If the The total size of each per-cpu buffer is calculated by multiplying the
number of sub-buffers by the sub-buffer size passed into The To have The private_data passed into Channel ‘modes’¶relay channels can be used in either of two modes - ‘overwrite’ or
‘no-overwrite’. The mode is entirely determined by the implementation
of the In ‘overwrite’ mode, also known as ‘flight recorder’ mode, writes continuously cycle around the buffer and will never fail, but will unconditionally overwrite old data regardless of whether it’s actually been consumed. In no-overwrite mode, writes will fail, i.e., data will be lost, if the number of unconsumed sub-buffers equals the total number of sub-buffers in the channel. It should be clear that if there is no consumer or if the consumer can’t consume sub-buffers fast enough, data will be lost in either case; the only difference is whether data is lost from the beginning or the end of a buffer. As explained above, a relay channel is made of up one or more
per-cpu channel buffers, each implemented as a circular buffer
subdivided into one or more sub-buffers. Messages are written into
the current sub-buffer of the channel’s current per-cpu buffer via the
write functions described below. Whenever a message can’t fit into
the current sub-buffer, because there’s no room left for it, the
client is notified via the To implement ‘no-overwrite’ mode, the userspace client provides
an implementation of the static int subbuf_start(struct rchan_buf *buf,
void *subbuf,
void *prev_subbuf,
unsigned int prev_padding)
{
if (prev_subbuf)
*((unsigned *)prev_subbuf) = prev_padding;
if (relay_buf_full(buf))
return 0;
subbuf_start_reserve(buf, sizeof(unsigned int));
return 1;
}
If the current buffer is full, i.e., all sub-buffers remain unconsumed,
the callback returns 0 to indicate that the buffer switch should not
occur yet, i.e., until the consumer has had a chance to read the
current set of ready sub-buffers. For the The implementation of the static int subbuf_start(struct rchan_buf *buf,
void *subbuf,
void *prev_subbuf,
size_t prev_padding)
{
if (prev_subbuf)
*((unsigned *)prev_subbuf) = prev_padding;
subbuf_start_reserve(buf, sizeof(unsigned int));
return 1;
}
In this case, the The default Header information can be reserved at the beginning of each sub-buffer
by calling the Writing to a channel¶Kernel clients write data into the current cpu’s channel buffer using
Closing a channel¶The client calls Misc¶Some applications may want to keep a channel around and reuse it
rather than open and close a new channel for each use. Finally, there are a couple of utility callbacks that can be used for
different purposes. Resources¶For news, example code, mailing list, etc. see the relay interface homepage: Credits¶The ideas and specs for the relay interface came about as a result of discussions on tracing involving the following: Michel Dagenais <michel.dagenais@polymtl.ca> Richard Moore <richardj_moore@uk.ibm.com> Bob Wisniewski <bob@watson.ibm.com> Karim Yaghmour <karim@opersys.com> Tom Zanussi <zanussi@us.ibm.com> Also thanks to Hubertus Franke for a lot of useful suggestions and bug reports. | ||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |