Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Linux \ Linux more efficient than select a model epoll     - Use Visual Studio 2015 to develop Android program (Programming)

- WinSCP to transfer text files will automatically convert the format (Linux)

- xCAT Installation Kit (Linux)

- Install Git on CentOS (Linux)

- Linux variable learning experience (Linux)

- Getting Started with Linux: Nginx Web Server How to Block Specific User Agents (UA) (Server)

- First start with Kali Linux 2.0 (Linux)

- Memory leak analysis using Android studio (Programming)

- Broadcom transplanted to OpenWrt summary (Programming)

- SME Linux network security policy server security (Linux)

- How to Debian Linux the default Python version switch to alternative version (Linux)

- Configuring DNS process under CentOS 6.5 (Server)

- Java, hashcode, equals and == (Programming)

- Easily solve the MySQL database connection error too many (Database)

- Linux system security norms (Linux)

- RT-11SJ run at ambient PDP-11 MACRO-11 assembly (Programming)

- Two programs HAProxy + Heartbeat (note, the secondary server vip, normal HAProxy boot method) (Server)

- Copy U disk files to the Linux system on a virtual machine (Linux)

- Yii2 Advanced Version Copy New Project Problem Solved (Programming)

- Use the command line MySQL database backup and recovery (Database)

  Linux more efficient than select a model epoll
  Add Date : 2018-11-21      
  Epoll essence

In linux network programming, a very long time to do using the select event trigger. In the new linux kernel, it has an alternative mechanism is epoll.
Compared to select, epoll biggest benefit is that it will not end with the growth of the number of listeners fd reduce efficiency. Because the kernel select implementations, it is handled using polling, the number of polling fd, the more natural the more time-consuming. Also, in linux / posix_types.h header file such a declaration:
#define __FD_SETSIZE 1024
Max represents select monitor 1024 fd, of course, you can expand this number by modifying the header files and then recompile the kernel, but that does not seem to cure.

epoll interface is very simple, of a total of three functions:
1. int epoll_create (int size);
Create an epoll handle, size of the number of listeners to tell the kernel of a total of how much. This parameter is different from the select () in the first parameter, giving maximum listening fd value + 1. Note that, when created epoll handle, it will take up a fd value is, if you look at linux / proc / process id / fd /, is able to see the fd, so after using epoll, must call close () to close, it may cause fd be exhausted.

2. int epoll_ctl (int epfd, int op, int fd, struct epoll_event * event);
epoll event registration function, it is different with select () is when the monitored event tells the kernel to monitor what types of events, but here to register event types to listen. The first parameter is epoll_create () return value, and the second parameter indicates the action, represented by three macros:
EPOLL_CTL_ADD: register new fd to epfd in;
EPOLL_CTL_MOD: Modify registered fd monitoring events;
EPOLL_CTL_DEL: Remove from epfd in a fd;
The third argument is the need to listen fd, the fourth parameter tells the kernel needs to listen to anything, struct epoll_event structured as follows:
struct epoll_event {
  __uint32_t events; / * Epoll events * /
  epoll_data_t data; / * User data variable * /

events can be set following macro:
EPOLLIN: indicates that the corresponding file descriptor can be read (including peer SOCKET normally closed);
EPOLLOUT: indicates that the corresponding file descriptor can be written;
EPOLLPRI: indicates that the corresponding file descriptor has urgent data to read (represented here should have come with external data);
EPOLLERR: indicates that the corresponding file descriptor errors occur;
EPOLLHUP: indicates that the corresponding file descriptor is dropped;
EPOLLET: EPOLL to the edge trigger (Edge Triggered) mode, which is relative to the trigger level (Level Triggered) for the.
EPOLLONESHOT: listening only one incident, after this incident finished listening, if you need to continue to monitor the socket, then again, this socket needs to be added to the queue EPOLL

3. int epoll_wait (int epfd, struct epoll_event * events, int maxevents, int timeout);
Waiting to generate an event, similar to select () call. Events parameters used to obtain the set of events from the kernel, the kernel of the events maxevents Advertisement how big this value can not be greater than maxevents create epoll_create (size, parameter timeout) when the timeout (in milliseconds, 0 will return immediately, -1 Sure, there are also claims said to be permanently blocked). This function returns the number of events that need to be addressed, such as the return of 0 indicates that the timeout.

From the man page, get detailed description ET and LT are as follows

EPOLL event, there are two models:
Edge Triggered (ET)
Level Triggered (LT)

If there is such an example:
1. We have a pipeline used to read data from the file handle (RFD) added to the epoll descriptor
2. this time from the other end of the pipe was written 2KB of data
3. Call epoll_wait (2), and it will return RFD, indicating that it is ready to read
4. Then we read 1KB of data
5. Call epoll_wait (2) ......

Edge Triggered mode:
If we are in step 1 to add RFD to epoll descriptor when using EPOLLET flag, then in the fifth step with epoll_wait it will be possible after (2) will be suspended, because the remaining data also exist in the input file buffer inside, and the data issued by the end still waiting for a feedback information for the data already sent. Only the file handle to monitor an event occurred when ET mode will report the event. Therefore, in step 5, when the caller may abandon the wait is still present in the file input remaining data in the buffer. In the above example, there will be an event generated on RFD handle as in step 2 to perform a write operation, then, the event will be destroyed in step 3. Because of step 4 reads the input data file does not have read empty buffer, so we (2) Upon completion, is uncertain whether pending in the fifth step with epoll_wait. epoll work in ET mode, you must use non-blocking sockets, in order to avoid blocking a file handle read / write blocking file descriptors to handle multiple tasks starve. Best to call in the following manner ET model epoll interface, will be introduced later to avoid possible defects.
   i-based non-blocking file handle
   ii only when read (2) or write (2) return EAGAIN need to suspend, wait. This does not mean () each time you read cycles are required to read, read until it produces a EAGAIN think this event completes, when the data length of the read () returns the read request is less than the length of the data, you can has been determined at this time there is no data in the buffer, it can read the event that the matter has been processed.

Level Triggered mode
Conversely, in order to invoke the LT epoll interface, when it is equivalent to a speed faster poll (2), and regardless of whether the data to be used later, so they have the same function. Because even with ET model epoll, multiple chunk of data received in time will still produce multiple events. The caller can set EPOLLONESHOT flag file handle epoll_wait (2) Upon receipt of epoll event will be associated with the event is prohibited off from epoll descriptor. So when EPOLLONESHOT settings, usage epoll_ctl with EPOLL_CTL_MOD flag (2) file handles becomes the caller must do something.

Then a detailed explanation of ET, LT:

LT (level triggered) is the default operating mode, and supports both block and no-block socket. In this approach, the kernel tells you whether a file descriptor is ready, and then you can perform the IO operation ready fd . If you do not make any operation, the kernel will continue to inform you, therefore, this mode programming errors more likely to be smaller. Traditional select / poll is representative of this model.

ET (edge-triggered) is a high-speed mode, only supports no-block socket. In this mode, when the descriptor becomes ready never ready, the kernel epoll tell you. Then it will assume that you know the file descriptor is ready, and will not send more ready for that file descriptor notice until you do certain actions that lead to the file descriptor is no longer a ready state (for example, you sent, received or receives a request or transmission and reception of data is less than a certain amount of time has led to a EWOULDBLOCK error). Note, however, if this has not fd for IO operations (which causes it to become again not ready), the kernel will not send more notifications (only once), but in the TCP protocol acceleration utility ET mode still need more more benchmark confirm (this sentence does not understand).

In many tests, we'll see if not a lot of idle -connection or dead-connection, epoll efficiency and not much higher than the select / poll, but when we come across a lot of idle- connection (such as a WAN environment, there are a lot slow connection), you will find epoll is much higher than the efficiency of select / poll. (Not tested)


In addition, when using the ET model epoll work, when generating a EPOLLIN event,
When reading data to consider is that when recv () returns if the size is equal to the size of the request, it is likely that there is a buffer data is not read, it means that the incident has not been processed, so it needs again read:
while (rs)
  buflen = recv (activeevents [i] .data.fd, buf, sizeof (buf), 0);
  if (buflen <0)
    // Because it is non-blocking mode, so when errno to EAGAIN, it indicates that the current buffer has no data to be read
    // Here it is deemed to have been processed at the incident.
    if (errno == EAGAIN)
   else if (buflen == 0)
     // Here represents the peer socket has been shut down.
   if (buflen == sizeof (buf)
     rs = 1; // need to read again
     rs = 0;

Also, if the sender is greater than the traffic flow at the receiving end (where the meaning is the program epoll read faster than the forward socket), because it is non-blocking socket, then send () function returns though, but the data is not actually the actual buffer to the receiving side, so continue to read and send, when the buffer is full will produce an error EAGAIN (refer man send), while ignoring the request for data transmission. Therefore, the function needs to be encapsulated socket_send () is used to process this case, the function will try to finish the data and then return, return -1 indicates an error. In socket_send () inside, when the write buffer is full (send () returns -1 and errno to EAGAIN), it will wait and try again. This approach is not perfect, in theory, might be blocked for a long time in socket_send () inside, but there is no better way temporarily.

ssize_t socket_send (int sockfd, const char * buffer, size_t buflen)
  ssize_t tmp;
  size_t total = buflen;
  const char * p = buffer;

  while (1)
    tmp = send (sockfd, p, total, 0);
    if (tmp <0)
      // When the send signal is received, you can continue to write, but here returns -1.
      if (errno == EINTR)
        return -1;

      // When the socket is non-blocking when such returns this error, a write buffer queue is full,
      // Do here delay and try again.
      if (errno == EAGAIN)
        usleep (1000);

      return -1;

    if ((size_t) tmp == total)
      return buflen;

    total - = tmp;
    p + = tmp;

  return tmp;
- OpenGL shadow map (Programming)
- RHEL / CentOS / Fedora Install Nagios 4.0.1 (Linux)
- VirtualBox install Windows 8.1 has encountered an error 0x000000C4 solutions (Linux)
- Enterprise Hadoop cluster architecture - DNS installation (Server)
- How to improve the performance of Ruby On Rails (Linux)
- MySQL 5.7 and 5.6 group by differences (Database)
- Mount NFS network file system (Linux)
- 10 Best Swift Tutorial examples (Programming)
- KVM add virtual disks (Linux)
- and localhost difference (Server)
- Linux, how to filter, split, and merge pcap file (Linux)
- Inject script commands manually annotated summary (Linux)
- jQuery get value drop-down list and select text (Programming)
- Oracle inverted reverse function (Database)
- To setup Cocos2dx project under Ubuntu (Linux)
- How to manage the time and date at systemd Linux systems (Linux)
- Manager Docker browser (Server)
- MariaDB phpMyAdmin installation and configuration issues to resolve under CentOS7 (Database)
- Ubuntu 14.04 install Sublime Text 3 plug and use SublimeClang (Linux)
- Linux itself disguised illusion strengthen security (Linux)
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.