The implementation principle of JAVA NiO selector __java

Source: Internet
Author: User
Tags epoll

Java NiO Core Class library Multiplexer selector is based on epoll multiplexing technology implementation

Compared to select, poll system calls, Epoll has the following advantages:

1. The socket descriptor (FD) that supports one process opens is unrestricted and limited to the maximum number of file handles for the operating system.
The biggest flaw in select is that there is a limit to the amount of FD opened by a single process, set by Fd_setsize, and the default value is 1024. You can choose to modify this macro to recompile the kernel, but this will bring down network efficiency. You can also choose a multiple-process scenario (the traditional Apache scenario) to address this problem, but the cost of creating the process and the data exchange between processes need to be considered.
Epoll specific support of the FD line value can be viewed through the Cat/proc/sys/fs/file-max, this value and the system's memory relationship is relatively large.

2.I/O efficiency does not decrease linearly as the number of FD increases.
When there is a large socket set, due to network delay or link idle, at any one time only a small portion of the socket is "active", but select/poll each call will be linear scan all sets, resulting in a linear decline in efficiency. The Epoll only operates on active sockets-only active sockets actively invoke the callback function, so you only need to traverse the set of FD that joins the ready queue by asynchronously waking the kernel I/O events.

3. Use mmap to accelerate messaging between the kernel and user space
Epoll through the kernel and user space mmap the same piece of memory to achieve. The mmap () system call allows a portion of the file or information stored on a block device to be mapped to part of the address space of the process.

See here, may have a question, why the kernel and user space for messaging it.

Here's what I'll do in deep analysis:

First, the two main goals that the operating system must complete:

1, interacting with the hardware part

2, provide an execution environment for applications running on computer systems, known as user programs.

Modern operating systems prohibit user programs from interacting directly with the underlying hardware, or direct access to arbitrary physical addresses. For this reason, the hardware introduces at least two different execution modes for the CPU: The unprivileged mode of the user program and the privileged mode of the kernel. UNIX calls them "user mode" and "Kernel State" (Kernel mode), respectively. Therefore, each actual file I/O operation must be in the kernel state.

The CPU can run either in the user state or in the kernel state. Some CPUs can have more than two execution states, such as the Intel80x86 microprocessor, which has four different execution states. However, all standard UNIX kernels only take advantage of the kernel state and user state.

When a program executes, most of the time is in the user state, and only the service required by the kernel is switched to the kernel state. When the kernel satisfies the user program's request, it returns the program back to the user state.

Then, there are several modes of accessing the file:

1, canonical mode

After the file is opened in spec mode, the flag O_sync and O_direct 0, and its contents are accessed by the system call read () and write (). The system call read () blocks the calling process until the data is copied into the user-State address space. However, unlike the system call write (), it ends immediately after the data has been copied to the page cache (deferred).

2, Sync mode

When the file is open in sync mode, the flag O_sync 1. This flag only affects the write operation (the read operation always blocks) and it blocks the calling process until the data is written to disk effectively.

3, Memory-mapped mode

After the file is opened in memory-mapped mode, the application issues the system call MMAP () to map the file into memory. Therefore, the file is called a byte array in RAM, and the application can access the array elements directly without the system call read (), write (), or Lseek ().

4, direct I/O mode

Direct I/O mode after the file is opened, the flag O_direct 1. Any read and write operation transmits data directly between the user-State address space and the disk, not through the page cache.

5, asynchronous mode

In asynchronous mode, file access can be done in two ways, that is, through a set of POSIX APIs or Linux-specific system calls. The so-called asynchronous pattern is that the data transfer request does not block the calling process, but instead executes in the background while the application continues its normal operation.

When it comes to memory mapping, talk about the memory management of the operating system.

Taking the 80x86 microprocessor as an example, we must distinguish between the following three different addresses:

A logical address that is contained in a machine language instruction to specify an operand or an instruction. Each logical address consists of a segment (segment) and offset (offset).

A linear address, also known as a virtual address, is a 32-bit unsigned integer that can be used to represent up to 4GB of addresses.

Physical address for memory chip-level memory unit addressing.

The Memory Control unit (MMU) converts a logical address into a linear address through a hardware circuit called a segmented unit; then the second hardware circuit, called the paging unit, converts the linear address into a physical address.

Note:
Linux uses a 4KB page frame size as the standard memory allocation unit.

Memory fragmentation arises primarily because the size of the request memory does not match the size assigned to it. A typical solution is to provide a geometric size of the memory area, where the size of the memory area depends on the power of 2, not on the size of the data being stored. In this way, memory fragmentation is guaranteed to be less than 50%, regardless of the size of the requested memory.

The kernel uses a new resource-linear region to implement deferred allocation of process dynamic memory. When a user-state process requests dynamic memory, it does not get the requested page frame, but only gets the right to a new linear address range, which becomes part of the process address space. This interval is called a "linear region".

The address space of a process consists of all linear addresses that are used by the process. The kernel expresses the linear address interval through the resource of the so-called linear region, and the linear region is described by the starting linear address, length and some access rights.

Memory mapping, the kernel assigns a new linear region to the process to map the file. A newly created memory map is a linear region that does not contain any pages, and when the process references an address in the linear region, the page fault occurs.

All information related to the process address space is contained in a data structure called a memory descriptor, with a type of mm_struct.

Linux implements a linear region through a type-vm_area_struct object

All linear areas owned by a process are linked by a simple list of memory address ascending, where the mmap field points to the first linear region descriptor in the list. When there are many linear regions in the process, Linux2.6 uses a red-black tree to store the memory descriptor.

The relationship between the address space of the process, its memory descriptor, and the list of linear regions, as shown in the following illustration:

Each UNIX process has a special linear area-a heap, which is used to satisfy the dynamic memory requests of the process.

For most operating systems, mapping a file to memory be more expensive than-reading or writing a few tens of kilobytes O F data via the usual read and write methods. From the standpoint of performance it are generally only worth mapping relatively large files into memory.

JDK1.7 upgraded the NIO class library, and the upgraded NiO class library is called NIO2.0.

NIO2.0 provides a formal implementation of asynchronous file channels and asynchronous socket channels. Corresponds to the event-driven I/O-AIO in UNIX network programming. It simplifies the programming model of NIO by not requiring the polling of registered channels through Multiplexer-selector to enable asynchronous reading and writing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.