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)

Hot Topics

Java Tutorial
1655
14
PHP Tutorial
1252
29
C# Tutorial
1226
24
Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

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.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

laravel installation code laravel installation code Apr 18, 2025 pm 12:30 PM

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

git software installation git software installation Apr 17, 2025 am 11:57 AM

Installing Git software includes the following steps: Download the installation package and run the installation package to verify the installation configuration Git installation Git Bash (Windows only)

How to set important Git configuration global properties How to set important Git configuration global properties Apr 17, 2025 pm 12:21 PM

There are many ways to customize a development environment, but the global Git configuration file is one that is most likely to be used for custom settings such as usernames, emails, preferred text editors, and remote branches. Here are the key things you need to know about global Git configuration files.

See all articles