Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

splice (system call)

From Wikipedia, the free encyclopedia
icon
This articleneeds additional citations forverification. Please helpimprove this article byadding citations to reliable sources. Unsourced material may be challenged and removed.
Find sources: "Splice" system call – news ·newspapers ·books ·scholar ·JSTOR
(March 2011) (Learn how and when to remove this message)

splice() is aLinux-specificsystem call that moves data between afile descriptor and a pipe without a round trip to user space. The related system callvmsplice() 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 improveI/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

[edit]

Withsplice(), 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.splice() works by using thepipe 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

[edit]

Linus Torvalds describedsplice() in a 2006 email, which was included in aKernelTrap article.[1]

TheLinux splice implementation borrows some ideas from an original proposal byLarry McVoy in 1998.[2] The splice system calls first appeared inLinux kernel version 2.6.17[1] and were written byJens Axboe.

Prototype

[edit]
ssize_tsplice(intfd_in,loff_t*off_in,intfd_out,loff_t*off_out,size_tlen,unsignedintflags);

Some constants that are of interest are:

// Splice flags (not laid down in stone yet).#ifndef SPLICE_F_MOVE#define SPLICE_F_MOVE 0x01#endif#ifndef SPLICE_F_NONBLOCK#define SPLICE_F_NONBLOCK 0x02#endif#ifndef SPLICE_F_MORE#define SPLICE_F_MORE 0x04#endif#ifndef SPLICE_F_GIFT#define SPLICE_F_GIFT 0x08#endif

Example

[edit]

This is an example of splice in action:

// Transfer from disk to a log.intlog_blocks(structlog_handle*handle,intfd,loff_toffset,size_tsize){intfile_descriptions[2];size_tto_write=size;intret=pipe(file_descriptions);if(ret<0){gotoout;}// splice the file into the pipe (data in kernel memory).while(to_write>0){ret=splice(fd,&offset,file_descriptions[1],NULL,to_write,SPLICE_F_MORE|SPLICE_F_MOVE);if(ret<0){gotopipe;}else{to_write-=ret;}}to_write=size;// splice the data in the pipe (in kernel memory) into the file.while(to_write>0){ret=splice(file_descriptions[0],NULL,handle->fd,&(handle->fd_offset),to_write,SPLICE_F_MORE|SPLICE_F_MOVE);if(ret<0){gotopipe;}else{to_write-=ret;}}pipe:close(file_descriptions[0]);close(file_descriptions[1]);out:returnret<0?-errno:0;}

Complementary system calls

[edit]

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

Requirements

[edit]

When usingsplice() 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).

Not all filesystem types supportsplice().

See also

[edit]

References

[edit]
  1. ^ab"Linux: Explaining splice() and tee()". kerneltrap.org. 2006-04-21. Archived fromthe original on 2013-05-21. Retrieved2014-04-27.
  2. ^"Archived copy". Archived fromthe original on 2016-03-04. Retrieved2016-02-28.{{cite web}}: CS1 maint: archived copy as title (link)

External links

[edit]
Retrieved from "https://en.wikipedia.org/w/index.php?title=Splice_(system_call)&oldid=1321200163"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp