Splice (system call) explained

is a Linux-specific system call that moves data between a file descriptor and a pipe without a round trip to user space. The related system call moves or copies data between a pipe and user space. Ideally, splice and vmsplice work by remapping pages and do not actually copy any data, which may improve I/O performance. As linear addresses do not necessarily correspond to contiguous physical addresses, this may not be possible in all cases and on all hardware combinations.

Workings

With, one can move data from one file descriptor to another without incurring any copies from user space into kernel space, which is usually required to enforce system security and also to keep a simple interface for processes to read and write to files. works by using the pipe buffer. A pipe buffer is an in-kernel memory buffer that is opaque to the userspace process. A user process can splice the contents of a source file into this pipe buffer, then splice the pipe buffer into the destination file, all without moving any data through userspace.

Origins

Linus Torvalds described in a 2006 email, which was included in a KernelTrap article.[1]

The Linux splice implementation borrows some ideas from an original proposal by Larry McVoy in 1998.[2] The splice system calls first appeared in Linux kernel version 2.6.17 and were written by Jens Axboe.

Prototype

ssize_t splice(int fd_in, loff_t *off_in, int fd_out, loff_t *off_out, size_t len, unsigned int flags);

Some constants that are of interest are:

/* Splice flags (not laid down in stone yet). */

  1. ifndef SPLICE_F_MOVE
  2. define SPLICE_F_MOVE 0x01
  3. endif
  4. ifndef SPLICE_F_NONBLOCK
  5. define SPLICE_F_NONBLOCK 0x02
  6. endif
  7. ifndef SPLICE_F_MORE
  8. define SPLICE_F_MORE 0x04
  9. endif
  10. ifndef SPLICE_F_GIFT
  11. define SPLICE_F_GIFT 0x08
  12. endif

Example

This is an example of splice in action:

/* Transfer from disk to a log. */int log_blocks (struct log_handle * handle, int fd, loff_t offset, size_t size)

Complementary system calls

is one of three system calls that complete the architecture. can map an application data area into a pipe (or vice versa), thus allowing transfers between pipes and user memory where transfers between a file descriptor and a pipe. is the last part of the trilogy. It duplicates one pipe to another, enabling forks in the way applications are connected with pipes.

Requirements

When using with sockets, the network controller (NIC) should support DMA, otherwise splice will not deliver a large performance improvement. The reason for this is that each page of the pipe will just fill up to frame size (1460 bytes of the available 4096 bytes per page). does not support UDP sockets.

Not all filesystem types support . Also, sockets do not support .

See also

External links

Notes and References

  1. Web site: Linux: Explaining splice and tee . https://web.archive.org/web/20130521163124/http://kerneltrap.org/node/6505 . kerneltrap.org . 2006-04-21 . 2014-04-27 . 2013-05-21.
  2. Web site: Archived copy . 2016-02-28 . dead . https://web.archive.org/web/20160304171221/http://ftp.tux.org/pub/sites/ftp.bitmover.com/pub/splice.ps . 2016-03-04 .