Linux Traditional IO
Hello everyone, I am a piece of data lying on the Linux disk. Now to send me from the disk to the network card, I need to go through the following steps:
Read operation
As shown above: the operating system Memory is divided into kernel space and user space. First, the application in user space initiates a data read operation, such as the JVM initiating the read()
system call. At this time, the operating system will perform a context switch: switching from user space to kernel space.
Then the kernel space notifies the disk, and the kernel copies me from the disk to the kernel buffer. This process is done by a piece of hardware called "DMA (Direct memory access)", so it does not require the participation of the CPU.
Then the kernel copies me from the kernel buffer to the application buffer, which requires the participation of the CPU.
Finally perform a context switch and switch back to the user space context.
The entire read operation process requires two context switches and two copies.
Related learning recommendations: Java video tutorial
##Write operation
Write operation It is similar to the read operation, but in the opposite direction. It still requires two context switches and two data copies. I may be written to disk, or I may be written to the network card.Memory mapping
As you can see from the above process, if you want to send me from the disk to the network card, a total of 4 context switch and 4 copy operations. I was copied back and forth between kernel space and user space by the operating system, but in fact I did nothing during this period, nothing changed, it was just copying, so this IO model was a waste of operating system resources, and I was copied so Many times, physically and mentally exhausted. Moreover, the resources of the operating system are very precious~Now mainstream operating systems all usevirtual memory. To put it simply, virtual address is used to replace the physical address . This allows multiple virtual memories to only want the same physical address, and the virtual memory space can be much larger than the physical memory space.
If the operating system can map the application buffer in user space and the kernel buffer in kernel space to the same physical address, wouldn't it eliminate a lot of copying processes? As shown below:##Linux Zero CopySo in order to solve this problem, smart Linux developers wrote some new System calls are made to do this. There are two main ways:
mmap write- sendfile
mmap write
mmap()The system call will first use DMA copy to read from the disk to the kernel buffer, and then use memory mapping to make the memory addresses of the user buffer and the kernel read buffer the same Memory address, that is to say, there is no need for the CPU to copy me from the kernel read buffer to the user buffer!
When using the
system call, the CPU writes directly from the kernel buffer (equivalent to the user buffer) to the kernel buffer that needs to be sent. For example, network send buffer (socket buffer)
, and then pass it into the network card driver (or disk) through DMA to prepare for sending.
#The mmap write method requires a total of two system calls, 4 context switches, 2 DMA Copy and 1 CPU Copy to read and write data.
sendfilesendfile is also a system call. It essentially combines the functions of the above two system calls into one call. The advantage of this is that the operating system only needs two context switches, reducing the overhead of two context switches.
Learn about zero-copy in Linux and Java
Linux2.4 kernel optimizes sendfile and provides Learn about zero-copy in Linux and Java operation. This operation can remove the last CPU copy in the above picture. The principle is not to copy data. , instead, the memory address and offset record of the data in the previous kernel buffer (such as the Read Buffer in the case in the figure) are sent to the target kernel buffer (such as the Socket Buffer in the case in the figure), so that in the final DMA In the copy stage, you can use this pointer to directly copy the data.
Java NIO uses zero copy
Linux’s zero copy can indeed save some operating system resources. Therefore, Java's NIO provides some classes in order to support zero copy:
- DirectByteBuffer
- FileChannel
In the previous "Java NIO - Buffer" This article briefly introduces DirectByteBuffer. There are two main implementations of ByteBuffer, one is DirectByteBuffer and the other is HeapByteBuffer.
Among them, DirectByteBuffer allocates memory directly outside the heap, and the bottom layer directly calls the NIO system call of the operating system through JNI, so the performance will be relatively high. The HeapByteBuffer is in-heap memory, and the data needs to be copied one more time, so the performance is relatively low.
FileChannel
is a class provided by Java NIO for copying files. It can copy files to disk or network, etc.
map
The method actually uses the memory mapping method in the operating system to map the memory of the kernel buffer and the memory of the user buffer into an address.
transferTo
The method directly transfers the current channel content to another channel, which means that this method does not have the problem of reading and writing from the kernel buffer to the user buffer. The bottom layer is the sendfile system call. transferFrom
The method is the same.
Sample code:
File file = new File("test.txt");RandomAccessFile raf = new RandomAccessFile(file, "rw");FileChannel fileChannel = raf.getChannel();SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("", 8080));// 直接使用了transferTo()进行通道间的数据传输fileChannel.transferTo(0, fileChannel.size(), socketChannel);
Author: Public account _xy’s technical circle
Link: www.imooc.com/article/289550
Source: MOOC.com
The above content comes from MOOC.com
Re-understanding of zero copy
Zero copy is from the operation From a system perspective. Because no data is duplicated between kernel buffers (only the kernel buffer has one copy of data).
Zero copy not only brings less data copying, but also brings other performance advantages, such as fewer context switches, less CPU cache pseudo sharing and no CPU Checksum calculation.
The difference between mmap and sendFile
mmap is suitable for reading and writing small amounts of data, and sendFile is suitable for large file transfers.
mmap requires 4 context switches and 3 data copies; sendFile requires 3 context switches and at least 2 data copies.
sendFile can use DMA to reduce CPU copying, but mmap cannot (it must be copied from the kernel to the Socket buffer).
The above is the detailed content of Learn about zero-copy in Linux and Java. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)