Got it

Zero copy

658 0 3 0 0

Zero copy


1.Pre-concept:

Zero copy: copy data directly from the hard disk file to the socket, and no longer bypass the application. Zero copy improves the performance of the application and reduces the contextual interaction between the kernel model and the user mode.

DMA: Direct memory access is an important feature of all modern computers, allowing hardware devices of different speeds to communicate without relying on a large amount of interrupt load on the CPU.


2.Traditional data transmission methods:


A common simple scenario: reading data from a file on the server disk and passing the data to the client over the network. For example, nginx provides static content, outlook mail service, FTP service, etc. The data transmission process and context switching diagram are as follows: 

152305e4skisssskymrjzz.png?Zero%20copy1.

Traditional data transmission process

 152320u6kelk4pebgxx141.png?Zero%20copy2.

Traditional context switching


Steps involved:

1. The user issues a read command, triggering the first context switch: from the user context to the kernel context; the DMA copy engine performs the first copy, from disk to read buffer.

2. the user read command returns, triggered a second context switch: from the kernel context to the user context; CPU copy, executed a second copy, data from the read buffer to the application buffer.

3. the user issued a write command, triggered a third context switch: from user context to kernel context; CPU copy, executed a third copy, data from application buffer to socket buffer.

4. the user writes the instruction to return, triggering the fourth context switch: from the kernel context to the user context; DMA copy, the fourth copy is executed, the data is from the socket buffer to the NIC buffer.

 

The above is an ideal scenario without error. If the file transferred is larger than the read buffer and the socket buffer, the number of copy and context switches will be more.

 


In summary, the traditional data transmission method has at least 4 context switches and 4 copies.


3. Zero copy method

By combing the traditional data transmission method, it is not difficult to find that if it is pure static data, the second and third copies are completely unnecessary. Draw a line from the read buffer to the socket buffer. And this process is happening in the kernel context, it is obvious that the implementation of this function requires the support of the underlying operating system.

3.1 First optimization

The first optimized data transmission process and context switching diagram are as follows:

 152342tio7forhklj3luub.png?Zero%20copy3.

152348gsitiirtrsfycvrh.png?Zero%20copy4.

 

 

Steps involved:

1. The user issues a transfer instruction, triggering the first context switch: from the user context to the kernel context; the user receives the response of the transfer instruction, triggering the second context switch: from the kernel context to the user context;

2. involving three copies: the first time from the disk to the read buffer (DMA copy); the second time from the read buffer to the socket buffer (CPU copy); the third time from the socket buffer to the NIC buffer (DMA copy);

 

At this point, the reduction of 2 context switching and 1 copy operation, the performance is improved; from the flow in the figure we can easily see that the read buffer to the socket buffer this copy, it seems unnecessary. If the NIC supports direct reading from the read buffer, omitting the socket step, we can reduce 1 copy. In fact, there is already related hardware support, and this kind of operation is called the collection operation of the network card.

3.2 Second optimization

The second optimized data transmission process is as follows:

 152400j83v30drr3oc77d5.png?Zero%20copy5.

 

Steps involved:

1. the DMA engine copies the contents of the file to the read buffer;

2. Only the information descriptor of the position and length of the data is appended to the socket buffer. DMA directly copies the data from the read buffer to the NIC buffer; thus omitting the CPU copy;

 

In summary, combined with the hardware (underlying network interface card) and the support of the underlying operating system, the data transmission can be optimized to two context switches, two DMA copies (in fact, there is still a negligible metadata transmission: the location of the data) And the length of the information descriptor is appended to the socket buffer).

Review

Zero copy is an efficient disk-to-network card transfer method that relies on the underlying operating system: zero copy is one of kafka's efficient guarantees, meaning that java and Scala are already supported and mature. In nginx's http configuration, there is an option sendfile. On can open the zero copy, as the c language written nginx on the underlying system calls, it is a unique advantage.


Comment

You need to log in to comment to the post Login | Register
Comment

Notice: To protect the legitimate rights and interests of you, the community, and third parties, do not release content that may bring legal risks to all parties, including but are not limited to the following:
  • Politically sensitive content
  • Content concerning pornography, gambling, and drug abuse
  • Content that may disclose or infringe upon others ' commercial secrets, intellectual properties, including trade marks, copyrights, and patents, and personal privacy
Do not share your account and password with others. All operations performed using your account will be regarded as your own actions and all consequences arising therefrom will be borne by you. For details, see " User Agreement."

My Followers

Login and enjoy all the member benefits

Login

Block
Are you sure to block this user?
Users on your blacklist cannot comment on your post,cannot mention you, cannot send you private messages.
Reminder
Please bind your phone number to obtain invitation bonus.