Sendfile optimized file copy

Source: Internet
Author: User
Tags file copy sendfile switches

Original address: http://www.vpsee.com/2009/07/linux-sendfile-improve-performance/

The popular Web server now offers Sendfile options to improve server performance, what exactly is sendfile and how does it affect performance. Sendfile is actually a system call after Linux 2.0+, and the Web server can adjust its own configuration to determine whether to use the Sendfile system call. Let's take a look at the traditional network transfer process without sendfile:

Read (file, tmp_buf, Len);
Write (socket, tmp_buf, Len);

HDD >> kernel buffer >> user buffer >> kernel socket buffer >> protocol stack

In general, a network application is to read the hard disk data, and then write data to the socket to complete the network transmission. 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 2 line of code executes:

1. The system call read () generates a context switch: switch from user mode to kernel mode, and then DMA executes the copy, reading the file data from the hard drive to a kernel buffer.
2, the data from kernel buffer copy to the user buffer, and then the system call Read () back, then produce a context switch: From kernel mode switch to user mode.
3. The system call write () produces a context switch: switch from user mode to kernel mode, and then copy the data from user buffer to kernel buffer (the 2nd copy of the data to kernel buffer), but this The second is a different kernel buffer, this buffer is associated with the socket.
4. The system call write () returns, resulting in a context switch: Switching from kernel mode to user mode (4th time switching), then DMA copy data from kernel buffer to the protocol stack (4th copy).

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

Let's take a look at the process of using sendfile () for network transmission:

Sendfile (socket, file, Len);

HDD >> kernel buffer (fast copy to kernel socket buffer) >> protocol stack

1, the system calls Sendfile () through DMA to the hard disk data copy to kernel buffer, and then the data is kernel directly to another socket-related kernel buffer. There is no switch between user mode and kernel mode, and a copy of the buffer from one buffer to another is done directly in kernel.
2, DMA data from kernel buffer directly to the stack, there is no switch, and do not need data from user mode copy to kernel mode, because the data is in kernel.

The steps are reduced, the switching is reduced, the copy is reduced, and the natural performance is improved. That's why opening the sendfile on option in the Nginx configuration file can improve the Web serve R performance. (See also: 64MB VPS optimized Nginx)

Performance test reference article address:

Http://blog.csdn.net/crazyguang/archive/2008/05/09/2423708.aspx

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.