illustrates a zero copy

Original link: https://www.jianshu.com/p/8c6b056f73ce

A traditional IO read and write

Traditional IO read in two ways: the terminal IO and DMA. Their respective works as follows.

1.1 IO interrupt principle

 

 

The whole process is as follows:

  • 1. User process calls read system calls to the operating system issue IO requests, request to read data into its own memory buffer. Himself into blocking state.
  • 2. After the operating system receives a request, further disk IO request.
  • 3. The disk drive receives the IO request of the kernel, the data read from the disk to the drive buffer. At this time, do not take up CPU. When the drive is reading the buffer is full, an interrupt signal to initiate the kernel inform himself buffer is full.
  • 4. The kernel interrupt is received, the CPU time used to copy data buffer in the disk drive to the kernel buffer.
  • 5. If the kernel buffer data is less than the user data read request, step 3 is repeated with step 4, the kernel buffer data until sufficient so far.
  • 6. The data is copied from the kernel buffer to the user buffer, returning from a system call. mission accomplished.

Disadvantages: Every IO request from a user, you need CPU has participated.

1.2 DMA principle

 
  • 1. User process calls read system calls to the operating system issue IO requests, request to read data into its own memory buffer. Himself into blocking state.
  • 2. After the operating system receives a request, the IO request further DMA. Then let the CPU do something else to live.
  • 3.DMA further IO requests to the disk.
  • 4. The disk drive receives a DMA IO request, the data read from the disk to the drive buffer. When the drive is reading the buffer is full, an interrupt signal to the DMA launched informed of their buffer is full.
  • 4.DMA the received signal of the disk drive, the disk drive copy of the data buffer in the kernel buffer. At this time, do not take up CPU. This time as long as the data is less than the kernel buffer to read the user's application data, the kernel will always repeat step 3 with step 4, until the kernel data buffers enough so far.
  • 5. When a DMA read enough data, it will send an interrupt signal to the CPU.
  • 6.CPU manual DMA signal, knows that the data is ready, then the data returned from the kernel space to the user copy, the system call.

Compared with the IO interrupt mode, DMA mode, DMA is a proxy of the CPU, which is responsible for a copy of part of the work, thereby reducing the burden on the CPU.
DMA is the advantage: less interruption, low CPU burden.

2 文件到网络场景的zero copy技术

2.1 传统IO读写方式的问题

在读取文件数据然后发送到网络这个场景中,传统IO读写方式的过程如下。

 

 

 

由图可知,整个过程总共发生了四次拷贝和四次的用户态和内核态的切换。
用户态和内核态的切换如下。借个网上的图。

 

 

2.2 zero copy技术

 

 

zero copy技术就是减少不必要的内核缓冲区跟用户缓冲区间的拷贝,从而减少CPU的开销和内核态切换开销,达到性能的提升。
zero copy下,同样的读取文件然后通过网络发送出去,只需要拷贝三次,只发生两次内核态和用户态的切换。
再次盗用一下别人的图。

 

 

3 linux下的zero copy技术

linux下的用来实现zero copy的常见接口由如下几个:

  • ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count)
  • long splice(int fdin, int fdout, size_t len, unsigned int flags);
    这两个接口都可以用来在两个文件描述符之间传输数据,实现所谓的zero copy。
    splice接口则要求两个文件描述符中至少要有一个是pipe。

3.1 sendfile跟splice的局限性

上面提到的用来实现零拷贝的sendfile和splice接口,仅限于文件跟文件,文件跟sock之间传输数据,但是没法直接在两个socket之间传输数据的。这就是sendfile和splice接口的局限性。
如果要实现socket跟socket之间的数据直接拷贝,需要开辟一个pipe,然后调用两次splice。这样还是带来跟传统IO读写一样的问题。系能其实并没有什么大的提升。

Guess you like

Origin www.cnblogs.com/fswhq/p/11517726.html