📄 relay.txt
字号:
relay interface (formerly relayfs)==================================The relay interface provides a means for kernel applications toefficiently log and transfer large quantities of data from the kernelto userspace via user-defined 'relay channels'.A 'relay channel' is a kernel->user data relay mechanism implementedas a set of per-cpu kernel buffers ('channel buffers'), eachrepresented as a regular file ('relay file') in user space. Kernelclients write into the channel buffers using efficient writefunctions; these automatically log into the current cpu's channelbuffer. User space applications mmap() or read() from the relay filesand retrieve the data as it becomes available. The relay filesthemselves are files created in a host filesystem, e.g. debugfs, andare associated with the channel buffers using the API described below.The format of the data logged into the channel buffers is completelyup to the kernel client; the relay interface does however providehooks which allow kernel clients to impose some structure on thebuffer data. The relay interface doesn't implement any form of datafiltering - this also is left to the kernel client. The purpose is tokeep things as simple as possible.This document provides an overview of the relay interface API. Thedetails of the function parameters are documented along with thefunctions in the relay interface code - please see that for details.Semantics=========Each relay channel has one buffer per CPU, each buffer has one or moresub-buffers. Messages are written to the first sub-buffer until it istoo full to contain a new message, in which case it it is written tothe next (if available). Messages are never split across sub-buffers.At this point, userspace can be notified so it empties the firstsub-buffer, while the kernel continues writing to the next.When notified that a sub-buffer is full, the kernel knows how manybytes of it are padding i.e. unused space occurring because a completemessage couldn't fit into a sub-buffer. Userspace can use thisknowledge to copy only valid data.After copying it, userspace can notify the kernel that a sub-bufferhas been consumed.A relay channel can operate in a mode where it will overwrite data notyet collected by userspace, and not wait for it to be consumed.The relay channel itself does not provide for communication of suchdata between userspace and kernel, allowing the kernel side to remainsimple and not impose a single interface on userspace. It doesprovide a set of examples and a separate helper though, describedbelow.The read() interface both removes padding and internally consumes theread sub-buffers; thus in cases where read(2) is being used to drainthe channel buffers, special-purpose communication between kernel anduser isn't necessary for basic operation.One of the major goals of the relay interface is to provide a lowoverhead mechanism for conveying kernel data to userspace. While theread() interface is easy to use, it's not as efficient as the mmap()approach; the example code attempts to make the tradeoff between thetwo 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 simple utility functions and a set of examples are provided.The relay-apps example tarball, available on the relay sourceforgesite, contains a set of self-contained examples, each consisting of apair of .c files containing boilerplate code for each of the user andkernel sides of a relay application. When combined these two sets ofboilerplate code provide glue to easily stream data to disk, withouthaving to bother with mundane housekeeping chores.The 'klog debugging functions' patch (klog.patch in the relay-appstarball) provides a couple of high-level logging functions to thekernel which allow writing formatted text or raw data to a channel,regardless of whether a channel to write into exists or not, or evenwhether the relay interface is compiled into the kernel or not. Thesefunctions allow you to put unconditional 'trace' statements anywherein the kernel or kernel modules; only when there is a 'klog handler'registered will data actually be logged (see the klog and kleakexamples 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, butyou'll have to implement communication between userspace and kernel,allowing both to convey the state of buffers (full, empty, amount ofpadding). The read() interface both removes padding and internallyconsumes the read sub-buffers; thus in cases where read(2) is beingused to drain the channel buffers, special-purpose communicationbetween kernel and user isn't necessary for basic operation. Thingssuch as buffer-full conditions would still need to be communicated viasome channel though.klog and the relay-apps examples can be found in the relay-appstarball on http://relayfs.sourceforge.netThe relay interface user space API==================================The relay interface implements basic file operations for user spaceaccess to relay channel buffer data. Here are the file operationsthat are available and some comments regarding their behavior:open() enables user to open an _existing_ channel buffer.mmap() results in channel buffer being mapped into the caller's memory space. Note that you can't do a partial mmap - you must map the entire file, which is NRBUF * SUBBUFSIZE.read() read the contents of a channel buffer. The bytes read are 'consumed' by the reader, i.e. they won't be available again to subsequent reads. If the channel is being used in no-overwrite mode (the default), it can be read at any time even if there's an active kernel writer. If the channel is being used in overwrite mode and there are active channel writers, results may be unpredictable - users should make sure that all logging to the channel has ended before using read() with overwrite mode. Sub-buffer padding is automatically removed and will not be seen by the reader.sendfile() transfer data from a channel buffer to an output file descriptor. Sub-buffer padding is automatically removed and will not be seen by the reader.poll() POLLIN/POLLRDNORM/POLLERR supported. User applications are notified when sub-buffer boundaries are crossed.close() decrements the channel buffer's refcount. When the refcount reaches 0, i.e. when no process or kernel client has the buffer open, the channel buffer is freed.In order for a user application to make use of relay files, thehost filesystem must be mounted. For example, mount -t debugfs debugfs /debugNOTE: 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:TBD(curr. line MT:/API/) channel management functions: relay_open(base_filename, parent, subbuf_size, n_subbufs, callbacks, private_data) relay_close(chan) relay_flush(chan) relay_reset(chan) channel management typically called on instigation of userspace: relay_subbufs_consumed(chan, cpu, subbufs_consumed) write functions: relay_write(chan, data, length) __relay_write(chan, data, length) relay_reserve(chan, length) callbacks: subbuf_start(buf, subbuf, prev_subbuf, prev_padding) buf_mapped(buf, filp) buf_unmapped(buf, filp) create_buf_file(filename, parent, mode, buf, is_global) remove_buf_file(dentry) helper functions: relay_buf_full(buf) subbuf_start_reserve(buf, length)Creating a channel------------------relay_open() is used to create a channel, along with its per-cpuchannel buffers. Each channel buffer will have an associated filecreated for it in the host filesystem, which can be and mmapped orread from in user space. The files are named basename0...basenameN-1where N is the number of online cpus, and by default will be createdin the root of the filesystem (if the parent param is NULL). If youwant a directory structure to contain your relay files, you shouldcreate it using the host filesystem's directory creation function,e.g. debugfs_create_dir(), and pass the parent directory torelay_open(). Users are responsible for cleaning up any directorystructure they create, when the channel is closed - again the hostfilesystem's directory removal functions should be used for that,e.g. debugfs_remove().In order for a channel to be created and the host filesystem's filesassociated with its channel buffers, the user must provide definitionsfor two callback functions, create_buf_file() and remove_buf_file().create_buf_file() is called once for each per-cpu buffer fromrelay_open() and allows the user to create the file which will be usedto represent the corresponding channel buffer. The callback shouldreturn the dentry of the file created to represent the channel buffer.remove_buf_file() must also be defined; it's responsible for deletingthe file(s) created in create_buf_file() and is called duringrelay_close().Here are some typical definitions for these callbacks, in this caseusing debugfs:/* * create_buf_file() callback. Creates relay file in debugfs. */static struct dentry *create_buf_file_handler(const char *filename, struct dentry *parent, int 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;}/*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -