Linux kernel system call--sendfile function

Source: Internet
Author: User
Tags file copy sendfile switches visibility

In the APACHE,NGINX,LIGHTTPD and other Web servers, there is a sendfile-related configuration, in some online information has talked about Sendfile will improve the file transfer performance, that sendfile what is it? What is the principle of it?

In the traditional file transfer inside (Read/write way), in the implementation is actually more complex, need to go through the context of the switch, we look at the following two lines of code:

Java code
    1. Read (file, tmp_buf, Len);
    2. Write (socket, tmp_buf, Len);


The above two lines of code are the traditional Read/write way for file-to-socket transmission.

When a file needs to be transferred, the details of the process are as follows:
1. Call the Read function to copy the file data to the kernel buffer
2. read function return, file data from kernel buffer copy to user buffer
3. Write function call, copy file data from user buffer to kernel buffer associated with socket.
4, the data from the socket buffer copy to the relevant protocol engine.

In general, a network application is to complete the network transmission by reading the hard disk data and then writing the data to the socket. The above 2 lines explain this in code, but the above 2 lines of simple code mask Many of the underlying operations. Let's see how the bottom line executes the above 2 lines of code:

1. The system calls read () to produce a context switch: switch from user mode to kernel mode, then perform a copy of the DMA and read the file data from the hard disk into a kernel buffer.
2. The data is copied from kernel buffer to user buffer, and then the system calls read () to return, then a context switch is generated: switch from kernel mode to user mode.
3. The system call write () produces a context switch: switch from user mode to kernel mode, and then read step 2 to the data copy of user buffer to kernel buffer (the 2nd copy of the data to kernel buffer), but this The second is a different kernel buffer, which is associated with the socket.
4. The system calls write () to return a context switch: switch from kernel mode to user mode (the 4th switch), and then DMA copies the data from kernel buffer to the protocol stack (4th copy).

The above 4 steps have 4 context switches and 4 copies, and we find that reducing the number of switches and the number of copies will effectively improve performance. In the kernel 2.0+ version, the system call Sendfile () is used to simplify the above steps to improve performance. Sendfile () Not only reduces the number of transitions but also reduces the number of copies.

The above details are the traditional read/write way of network File transfer, we can see that in this process, the file data is actually four copy operation:


Hard disk, kernel buf-> user Buf->socket related buffers---Protocol engine

The Sendfile system call provides a way to reduce the number of copy above and improve file transfer performance. The Sendfile system call was introduced at the 2.1 kernel:

Java code
    1. Sendfile (socket, file, Len);


The running process is as follows:
1, sendfile system call, file data is copied to the kernel buffer
2. Copy from kernel buffer to socket-related buffer in kernel
3. Finally, the socket-related buffer copy to the Protocol engine

Compared to the traditional Read/write method, the introduction of the version 2.1 kernel Sendfile has reduced the kernel buffer to the user buffer, and then from the user buffer to the socket-related buffer file copy, After kernel version 2.4, the file descriptor results were changed, Sendfile implemented a simpler way, the system is still called the same way, the details and the 2.1 version of the difference is that when the file data is copied to the kernel buffer, no longer copy all the data to the socket-related buffer, Instead, only data related to the location and length of the record data is saved to the socket-related cache, and the actual data is sent directly by the DMA module to the protocol engine, reducing the copy operation once again.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.