当前位置:网站首页>What are the problems with traditional IO? Why is zero copy introduced?

What are the problems with traditional IO? Why is zero copy introduced?

2022-06-25 07:49:00 Zhan sir (open source byte)

Conventional IO What are the problems ? Why zero copy ?

If the server wants to provide the function of file transfer , The simplest way we can think of is : Read out the files on the disk , And then send it to the client through the network protocol .

Tradition I/O The way we work is , Data read and write are copied back and forth from user space to kernel space , And the kernel space data is through the operating system level I/O Interface to read or write from disk .

The code is usually as follows , Generally, two system calls are required :

read(file, tmp_buf, len);
write(socket, tmp_buf, len);

The code is simple , Although only two lines of code , But there's a lot going on here .

First , A total of 4 Context switch between secondary user mode and kernel mode , Because there were two system calls , Once it was read() , Once it was write(), Every system call must first switch from user mode to kernel mode , After the kernel completes its task , Then switch from kernel mode to user mode .

The cost of context switching is not small , A switch takes tens of nanoseconds to microseconds , Although the time seems short , But in a high concurrency scenario , This kind of time is easy to accumulate and amplify , This will affect the performance of the system .

secondly , It also happened 4 Secondary data copy , among Twice DMA A copy of the , in addition Twice through CPU Copy Of , Let's talk about the process :

  • The first copy , Copy the data on the disk into the buffer of the operating system kernel , The process of copying is through DMA Carrying .
  • Second copy , Copy the data from the kernel buffer to the user's buffer , So our application can use this data , This copy to process is done by CPU Accomplished .
  • The third copy , Just copy the data to the user's buffer , And then copy it to the kernel socket In the buffer of , The process is still by CPU Carrying .
  • The fourth copy , Put the kernel of socket Data in the buffer , Copy to the network card buffer , This process is again caused by DMA Carrying .

Let's go back to the file transfer process , We're just carrying a piece of data , It turned out to be carrying 4 Time , Too many copies of data will no doubt consume CPU resources , Greatly reduces system performance .

This simple and traditional way of file transfer , There are redundant handoffs and data copies , It's very bad in a highly concurrent system , A lot of unnecessary expenses , Will seriously affect system performance .

therefore , To improve the performance of file transfer , You need to reduce 「 Context switch between user mode and kernel mode 」 and 「 Memory copy 」 The number of times .

 

If you reprint , Please indicate the source : Open source byte   https://sourcebyte.cn/article/169.html

原网站

版权声明
本文为[Zhan sir (open source byte)]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206250552375122.html