Table of Contents
##Write operation" >##Write operation
Memory mapping" >Memory mapping
So in order to solve this problem, smart Linux developers wrote some new System calls are made to do this. There are two main ways: " >##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:
sendfile 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. " >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.
Home Java javaTutorial Learn about zero-copy in Linux and Java

Learn about zero-copy in Linux and Java

Jul 01, 2020 pm 05:41 PM
java linux

Learn about zero-copy in Linux and Java

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

Learn about zero-copy in Linux and Java

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 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.

Learn about zero-copy in Linux and Java

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 use

virtual 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:

Learn about zero-copy in Linux and Java

##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

write()

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.

mmap + write#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.

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.

Learn about zero-copy in Linux and Java

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.

mapThe 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.

transferToThe 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. transferFromThe 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);
Copy after login

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

  1. 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).

  2. 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

  1. mmap is suitable for reading and writing small amounts of data, and sendFile is suitable for large file transfers.

  2. mmap requires 4 context switches and 3 data copies; sendFile requires 3 context switches and at least 2 data copies.

  3. 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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use docker desktop How to use docker desktop Apr 15, 2025 am 11:45 AM

How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

What to do if the docker image fails What to do if the docker image fails Apr 15, 2025 am 11:21 AM

Troubleshooting steps for failed Docker image build: Check Dockerfile syntax and dependency version. Check if the build context contains the required source code and dependencies. View the build log for error details. Use the --target option to build a hierarchical phase to identify failure points. Make sure to use the latest version of Docker engine. Build the image with --t [image-name]:debug mode to debug the problem. Check disk space and make sure it is sufficient. Disable SELinux to prevent interference with the build process. Ask community platforms for help, provide Dockerfiles and build log descriptions for more specific suggestions.

How to view the docker process How to view the docker process Apr 15, 2025 am 11:48 AM

Docker process viewing method: 1. Docker CLI command: docker ps; 2. Systemd CLI command: systemctl status docker; 3. Docker Compose CLI command: docker-compose ps; 4. Process Explorer (Windows); 5. /proc directory (Linux).

PHP vs. Python: Use Cases and Applications PHP vs. Python: Use Cases and Applications Apr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

What is vscode What is vscode for? What is vscode What is vscode for? Apr 15, 2025 pm 06:45 PM

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages ​​and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.

vscode cannot install extension vscode cannot install extension Apr 15, 2025 pm 07:18 PM

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

How to switch Chinese mode with vscode How to switch Chinese mode with vscode Apr 15, 2025 pm 11:39 PM

VS Code To switch Chinese mode: Open the settings interface (Windows/Linux: Ctrl, macOS: Cmd,) Search for "Editor: Language" settings Select "Chinese" in the drop-down menu Save settings and restart VS Code

See all articles