Java uses epoll for NiO processing)

Source: Internet
Author: User

NIO of JDK 6.0 and JDK 5.0 Update 9 supports epoll (Linux only). This greatly improves the performance of concurrent idle connections, which is required by many network server applications.

The method for enabling is as follows:

-Djava.nio.channels.spi.SelectorProvider=sun.nio.ch.EPollSelectorProvider   

For example, if Tomcat running in Linux uses NiO connector, enabling epoll will help improve performance.

Tomcat needs to enable this option by adding the following line at the beginning of Catalina. Sh.

Catalina_opts = '-djava. NiO. channels. SPI. selectorprovider = sun. NiO. Ch. epollselectorprovider'

Epoll is a poll improved by the Linux kernel to handle large volumes of handles. To use epoll, you only need to call these three systems: epoll_create (2), epoll_ctl (2), and epoll_wait (2 ). It is introduced in kernel 2.5.44 (epoll (4) is a new API introduced in Linux kernel 2.5.44) and is widely used in kernel 2.6, such as lighthttpd.

 

 

Advantages of one epoll-epoll

 

  • Allows a process to open a large number of socket Descriptors (FD)

The most intolerable thing about the SELECT statement is that the FD opened by a process has certain limitations, which are set by fd_setsize. The default value is 2048. For im servers that need to support tens of thousands of connections, there are obviously too few. At this time, you can choose to modify this macro and then re-compile the kernel. However, the materials also point out that this will bring about a reduction in network efficiency, second, you can select a multi-process solution (the traditional Apache solution). However, although the cost of creating a process on Linux is relatively small, it cannot be ignored, in addition, data synchronization between processes is far less efficient than inter-thread synchronization, so it is not a perfect solution. However, epoll does not have this limit. The FD limit supported by epoll is the maximum number of files that can be opened. This number is generally greater than 2048. For example, the number of machines with 1 GB of memory is about 0.1 million.
/Proc/sys/fs/file-max. Generally, this number has a great relationship with the system memory.

  • Io efficiency does not linearly decrease with the increase in the number of FD

Another critical weakness of traditional select/poll is that when you have a large set of sockets, but due to network latency, only some of the sockets at any time are "active, however, each select/poll call will linearly scan all sets, resulting in a linear decline in efficiency. However, epoll does not have this problem. It only operates on "active" sockets-this is because epoll is implemented based on the callback function on each FD in kernel implementation. Then, only the "active" socket will take the initiative to call the callback function, other idle status socket will not, in this regard, epoll implements a "pseudo" AIO, this is because the driver is in the OS kernel. In some
In benchmark, if all the sockets are basically active-for example, in a high-speed LAN environment, epoll is not more efficient than select/poll. On the contrary, if epoll_ctl is used too much, the efficiency is also slightly lower. However, once idle connections is used to simulate the WAN environment, epoll is far more efficient than select/poll.

  • Use MMAP to accelerate message transmission between the kernel and the user space.

This actually involves the specific implementation of epoll. Both select, poll, and epoll require the kernel to notify users of FD messages. It is important to avoid unnecessary memory copies, epoll is implemented through the same memory of the user space MMAP kernel. If you want me to focus on epoll from the 2.5 kernel, you will not forget the manual MMAP step.

  • Kernel fine-tuning

This is not an advantage of epoll, but an advantage of the entire Linux platform. Maybe you can doubt the Linux platform, but you cannot avoid the Linux platform giving you the ability to fine-tune the kernel. For example, if the Kernel TCP/IP protocol stack uses a memory pool to manage the sk_buff structure, you can dynamically adjust the memory pool (skb_head_pool) during runtime) by echo XXXX>/proc/sys/NET/CORE/hot_list_length. For example, the listen function's 2nd parameters (TCP completes the length of the packet queue after three handshakes) can also be dynamically adjusted based on the memory size of your platform. Even in a special system with a large number of data packets but the size of each data packet itself is small, try the latest napi NIC driver architecture.

 

 

II epoll-epoll usage

 

Fortunately, epoll of the 2.6 kernel is much simpler than/dev/epoll of its 2.5 development version. Therefore, in most cases, powerful things are often simple. The only trouble is that epoll can work in two ways: Lt and ET.

LT (Level triggered) is the default working method, and supports both block and no-block socket. in this way, the kernel tells you whether a file descriptor is ready, and then you can perform Io operations on this ready FD. If you do not perform any operation, the kernel will continue to inform you, so the possibility of programming errors in this mode is lower. The traditional select/poll model is representative of this model.

Et (edge-triggered) is a high-speed operating method that only supports no-block socket. In this mode, when the descriptor is never ready, the kernel tells you through epoll. Then it will assume that you know that the file descriptor is ready and will not send more ready notifications for that file descriptor, until you do some operations, the file descriptor is no longer ready (for example, you are sending, receiving, or receiving requests, or an ewouldblock error occurs when the number of data sent and received is less than a certain amount ). However, please note that the kernel will not send more notifications (only
Once), but in the TCP protocol, more benchmark validation is still required for the acceleration effect of the et mode.

Epoll is only called by epoll_create, epoll_ctl, and epoll_wait,
For specific usage, see http://www.xmailserver.org/linux-patches/nio-improve.html
There is an example at http://www.kegel.com/rn.

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.