The Linux kernel sendfile analysis


In the Apache, nginx, lighttpd, web server, there is a sendfile related configuration, in some online information has said sendfile will improve the file transmission performance, what exactly is the sendfile? It's principle and how it?
In the traditional file transfer (read/write), which in fact is more complex, need to go through multiple context switching, we look at the following two lines of code

  1. read(file, tmp_buf, len);
  2. write(socket, tmp_buf, len);

More than two lines of code is transmitted the traditional way of read/write file to socket.

When the need for transmission to a file, the specific details of the process are as follows:

1, Call the read function, the file data is copy to the kernel buffer

2, The read function returns the file data from the kernel buffer, copy into the user buffer

3, The write function calls, the file data from the copy user buffer to buffer the socket kernel and related.

4, Data from the copy socket buffer to the protocol engine.

These details is the traditional way of read/write network file transmission mode, we can see, in this process, the file data is actually after four copy operation:

Disk > kernel buf - > user buf; > buffer - related socket protocol engine >

While the sendfile system call provides a reduced more than many copy, methods to enhance the file transmission performance. The Sendfile system call was introduced in the 2.1 version of the kernel:

sendfile(socket, file, len);

The operation procedure is as follows:

1, The sendfile system call, data file is copy to the kernel buffer

2, Then from the kernel buffer copy to kernel socket buffer related

3, Finally, socket related copy buffers to the protocol engine

Compared with the traditional read/write method, The introduction of the sendfile 2.1 kernel has reduced the kernel buffer to the user buffer, Then the user buffer to buffer socket file copy, And in the kernel version 2.4, The file descriptor results change, Sendfile to achieve a more simple way, System call is still the same, The difference lies in the details and version 2.1, When the file data is copied into a kernel buffer, No longer will copy all data to the buffer associated with socket, But only to record data related to the location and length of socket related cache, But the actual data will be sent directly to the protocol engine module of DMA, To reduce the time of copy operation, But the support of not more than 2G.


Posted by Blair at March 22, 2014 - 1:33 AM