Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand Zero copy Zero-Copy Technology

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article will explain in detail how to understand the zero-copy Zero-Copy technology, the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

1. Preface

Today with you to learn a low-level technical point-zero copy Zero-Copy.

Everything in the Linux system is a file. Consider carefully that many activities in the Linux system are nothing more than read operations and write operations. Zero copy appears in order to improve read and write performance.

Don't talk too much nonsense, drive the cart right now, let's go!

two。 Basic process of data copy

In the Linux system, the capacity of cache and memory is limited, and more data is stored on disk. For Web servers, it is often necessary to read data from disk to memory and then transmit it to the user through the network card:

The above data flow is just a big box, let's take a look at several modes.

2.1 CPU-only mode

When the application needs to read disk data, the call read () falls from the user state to the kernel state, and the system call read () is finally completed by CPU.

CPU initiates an Iramo request to the disk, and after receiving it, the disk begins to prepare the data.

After the disk has put the data into the disk buffer, it initiates an Imax O interrupt to CPU, reporting that the CPU data has been Ready.

After receiving the interrupt of the disk controller, CPU starts copying data, returns read () when it is finished, and then switches from kernel mode to user mode.

2.2 CPU&DMA mode

CPU's time is precious, and letting it do chores is a waste of resources.

Direct memory access (Direct Memory Access) is a mechanism by which hardware devices bypass CPU to access memory independently. So DMA liberates CPU to a certain extent, leaving the chores of CPU to the hardware to do directly, thus improving the efficiency of CPU.

At present, the hardware that supports DMA includes network card, sound card, graphics card, disk controller and so on.

Some changes have taken place in the process with the participation of DMA:

The main change is that instead of interacting directly with the disk, CPU interacts with the disk and copies the data from the disk buffer to the kernel buffer, which is similar later.

"[knocking on the blackboard] there are multiple redundant data copies and kernel-user mode switching in both CPU-only mode and DMA&CPU mode."

We continue to think about the detailed process in which the Web server reads the local disk file data and transmits it to the user over the network.

3. Normal mode data interaction

The data exchange completed at one time includes several parts: system call syscall, CPU, DMA, network card, disk and so on.

The system call syscall is a bridge between the application and the kernel, and two switches occur each time the call / return is made:

Call syscall to switch from user mode to kernel mode

Syscall returns the switch from kernel mode to user mode.

Take a look at the complete schematic diagram of the data copying process:

The process of reading data:

The application needs to read the disk data and call the read () function to switch the kernel state in user mode. This is the first time that the state has been switched.

The DMA controller copies data from disk to the kernel buffer, which is the first DMA copy

CPU copies data from the kernel buffer to the user buffer, which is the first CPU copy

After the copy of CPU is completed, the read () function returns to switch between user mode and user mode. This is the second state switch.

Process of writing data:

The application needs to write data to the network card and call the write () function to switch the kernel state from the user mode to the user mode. This is the first time to switch.

CPU copies the user buffer data to the kernel buffer, which is the first CPU copy

The DMA controller copies data from the kernel buffer to the socket buffer, which is the first DMA copy

After the copy is completed, the write () function returns to switch the user mode in kernel state, which is the second time.

To sum up:

The reading process involves 2 space switches, 1 DMA copy, 1 CPU copy

The writing process involves 2 space switches, 1 DMA copy, 1 CPU copy

It can be seen that in the traditional mode, it is not efficient to involve multiple space switching and redundant copies of data, so it is time for zero-copy technology to come out.

4. Zero copy technology

4.1 cause of occurrence

We can see that if the application does not modify the data, from the kernel buffer to the user buffer, and then from the user buffer to the kernel buffer. Both data copies require the participation of CPU, and involve multiple switching between user mode and kernel state, which increases the burden of CPU.

We need to reduce redundant data copies and liberate CPU, which is zero-copy Zero-Copy technology.

4.2 ideas for solution

At present, several implementation methods of zero-copy technology include: mmap+write, sendfile, sendfile+DMA collection, splice and so on.

4.2.1 mmap mode

Mmap is a memory mapping file mechanism provided by Linux, which maps the read buffer address to the user space buffer address in the kernel, thus realizing the sharing of the kernel buffer and the user buffer.

This reduces one copy of CPU in both user and kernel mode, but there is still one copy of CPU in kernel space.

Mmap has some advantages over large file transfers, but small files may be fragmented, and signal that raises coredump may occur when multiple processes manipulate files at the same time.

4.2.2 sendfile mode

There is some improvement in mmap+write mode, but the state switching caused by system calls has not been reduced.

Sendfile system call was introduced in version 2.1 of the Linux kernel, which establishes a transfer channel between two files.

In sendfile mode, only one function can be used to complete the previous functions of read+write and mmap+write, so there are two fewer state switches, and because the data does not pass through the user buffer, the data cannot be modified.

As you can see from the figure, the application only needs to call the sendfile function, and there are only 2 state switches, 1 CPU copy, and 2 DMA copies.

But sendfile still has a copy of CPU in the kernel buffer and socket buffer, and maybe this can be optimized.

4.2.3 sendfile+DMA Collection

Linux 2.4 kernel optimizes sendfile system calls, but requires the cooperation of hardware DMA controllers.

The upgraded sendfile records the corresponding data description information (file descriptor, address offset, etc.) in the kernel space buffer into the socket buffer.

The DMA controller copies data from the kernel buffer to the network card according to the address and offset in the socket buffer, thus saving only one CPU copy in the kernel space.

This method has 2 state switches, 0 CPU copies, 2 DMA copies, but still can not modify the data, and requires the support of hardware-level DMA, and sendfile can only copy file data to socket descriptors, which has some limitations.

4.2.4 splice mode

Splice system call is introduced by Linux in version 2.6. it does not need hardware support and is no longer limited to socket to achieve zero copy of data between two ordinary files.

Splice system call can establish a pipeline between kernel buffer and socket buffer to transfer data, avoiding the CPU copy operation between the two.

Splice also has some limitations. One of its two file descriptor parameters must be a pipe device.

On how to understand zero-copy Zero-Copy technology to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report