Sendfile optimized file copy

Source: Internet
Author: User
Tags sendfile

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

Currently, the popular Web servers provide the sendfile option to improve server performance. What is sendfile? How does it affect performance? Sendfile is actually a system call launched after Linux 2.0 +. The web server can adjust its own configuration to determine whether to use sendfile. Let's take a look at the traditional network transmission process without sendfile:

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

Hard Disk> kernel buffer> user buffer> kernel Socket buffer> protocol stack

Generally, a network application reads the hard disk data and writes the data to the socket for network transmission. The above two lines explain this with code, but the above two lines of simple code mask a lot of underlying operations. Let's see how the bottom layer executes the above two lines of code:

1. The system calls read () to generate a context switch: switch from user mode to kernel mode, and then run the DMA command to copy the file data from the hard disk to a kernel buffer.
2. Data is copied from the kernel buffer to the user buffer, and then the system calls read () to return the data. In this case, another context switch is generated: switch from the kernel mode to the user mode.
3. The system calls write () to generate a context switch: switch from user mode to kernel mode, then, copy the data read from step 2 to the user buffer to the kernel buffer (data is copied 2nd times to the kernel buffer). However, this time 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 (4th switches ), then, DMA copies data from the kernel buffer to the protocol stack (4th copies ).

In the previous four steps, there were 4 context switches and 4 copies. We found that reducing the number of switches and the number of copies will effectively improve the performance. In kernel 2.0 +, the system calls sendfile () to simplify the above steps to improve performance. Sendfile () not only reduces the number of switching times, but also reduces the number of copies.

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

Sendfile (socket, file, Len );

Hard Disk> kernel buffer (fast copy to kernel Socket buffer)> protocol stack

1. The system calls sendfile () to copy the hard disk data to the kernel buffer through DMA, and then the data is directly copied by the kernel to another socket-related kernel buffer. There is no switching between the user mode and the kernel mode. In the kernel, the copy from one buffer to the other buffer is completed directly.
2. DMA directly copies the data from the kernel buffer to the protocol stack without switching, and does not need to copy the data from the user mode to the kernel mode, because the data is in the kernel.

The step is reduced, the switch is reduced, and the copy is reduced, so the natural performance is improved. This is why enabling the sendfile on option in the nginx configuration file can improve the Web Serve r performance. (See optimizing nginx on 64 MB VPs)

 

For more information about performance testing, see:

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.