打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
高性能linux socket server API:epoll(1)

高性能linux socket server API:epoll(1)

其实,epoll与select原理类似,只不过,epoll作出了一点重大改进,即:

当它们所监听的集合中有状态发生改变时,select需要循环检查整个集合,才能确定那个文件描述符状态发生改变,进而进行操作;

而epoll在添加文件描述符到集合时,已经绑定了该文件描述符的对应函数,因此,当该文件描述符状态改变时,不需要循环查询整个集合,因而将复杂度由0(n)将为o(1),性能得到几何量级的提高,尤其是在大量连接的情况下。

libevent是一个跨平台、高性能的函数库,在linux平台,它使用epoll,在freebsd平台,它使用kqueue,在windows平台,它使用iocp,原理上都是一致的。libevent官方主页上有一副图,对比说明了select与epoll/kqueue之间的性能差距,如下:

epoll有三个主要函数:

epoll_create

epoll_ctl

epoll_wait

 

epoll_create

NAME

       epoll_create – open an epoll file descriptor

 

SYNOPSIS

       #include <sys/epoll.h>

 

       int epoll_create(int size)

 

DESCRIPTION

       Open  an  epoll  file  descriptor by requesting the kernelallocate an event backing store dimensioned for size descriptors. Thesize is not the maximum size of the backing store but just a hint tothe kernel about  how  to  dimension  internal  structures.  Thereturned file descriptor will be used for all the subsequent calls tothe  epoll interface. The file descriptor returned by epoll_create(2)must be closed by using close(2).

 

RETURN VALUE

       When successful, epoll_create(2) returns a non-negativeinteger identifying  the  descriptor.   When  an  error  occurs,epoll_create(2) returns -1 and errno is set appropriately.

 

ERRORS

       EINVAL size is not positive.

 

       ENFILE The system limit on the total number of open files has been reached.

 

       ENOMEM There was insufficient memory to create the kernel object.

 

CONFORMING TO

       epoll_create(2)  is  a  new API introduced in Linux kernel2.5.44.  The interface should be finalized by  Linux kernel 2.5.66.

 

epoll_ctl

NAME

       epoll_ctl – control interface for an epoll descriptor

 

SYNOPSIS

       #include <sys/epoll.h>

 

       int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event)

 

DESCRIPTION

       Control an epoll descriptor, epfd, by requesting that theoperation op be performed on the target file descrip-tor, fd.  Theevent describes the object linked to the file descriptor fd.  Thestruct epoll_event  is  defined as :
            typedef union epoll_data {

                 void *ptr;

                 int fd;

                 __uint32_t u32;

                 __uint64_t u64;

            } epoll_data_t;

 

            struct epoll_event {

                 __uint32_t events;  /* Epoll events */

                 epoll_data_t data;  /* User data variable */

            };

 

       The events member is a bit set composed using the following available event types :

       EPOLLIN

              The associated file is available for read(2) operations.
 
       EPOLLOUT

              The associated file is available for write(2) operations.

       EPOLLPRI

              There is urgent data available for read(2) operations.

       EPOLLERR

              Error  condition  happened  on  the associated filedescriptor.  epoll_wait(2) will always wait for this event; it is notnecessary to set it in events.

       EPOLLHUP

              Hang up happened on the associated file descriptor. epoll_wait(2) will always wait for this  event;  it is not necessaryto set it in events.

       EPOLLET

              Sets  the  Edge Triggered behaviour for the associatedfile descriptor.  The default behaviour for epoll is Level Triggered.See epoll(4) for more detailed information about Edge and LevelTriggered event distribution architectures.

       EPOLLONESHOT

              Sets  the  one-shot  behaviour  for  the  associatedfile descriptor.  This means that after an event is pulled out withepoll_wait(2) the associated file descriptor is internally disabled andno other  events will be reported by the epoll interface. The user mustcall epoll_ctl(2) with EPOLL_CTL_MOD to re-enable the file descriptorwith a new event mask.
 
       The epoll interface supports all file descriptors that support poll(2).  Valid values for the op parameter  are  :

              EPOLL_CTL_ADD

                     Add the target file descriptor fd to the epolldescriptor epfd and associate the event event with the internal filelinked to fd.

              EPOLL_CTL_MOD

                     Change the event event associated with the target file descriptor fd.

              EPOLL_CTL_DEL

                     Remove the target file descriptor fd from theepoll file descriptor, epfd.  The event is  ignored and can be NULL(but see BUGS below).

RETURN VALUE

       When  successful,  epoll_ctl(2)  returns  zero.  When anerror occurs, epoll_ctl(2) returns -1 and errno is set appropriately.

 

ERRORS

       EBADF  epfd is not a valid file descriptor.

       EEXIST op was EPOLL_CTL_ADD, and the supplied file descriptor fd is already in epfd.

       EINVAL epfd is not an epoll file descriptor, or fd is thesame as epfd, or the requested operation  op  is  not supported by thisinterface.

       ENOENT op was EPOLL_CTL_MOD or EPOLL_CTL_DEL, and fd is not in epfd.

       ENOMEM There was insufficient memory to handle the requested op control operation.

       EPERM  The target file fd does not support epoll.

CONFORMING TO

       epoll_ctl(2) is a new API introduced in Linux kernel 2.5.44.  The interface should be finalized by Linux kernel 2.5.66.

BUGS

       In kernel versions before 2.6.9, the EPOLL_CTL_DEL operationrequired a non-NULL pointer in event, even  though this argument isignored.  Since kernel 2.6.9, event can be specified as NULL when usingEPOLL_CTL_DEL.

 

epoll_wait

NAME

       epoll_wait – wait for an I/O event on an epoll file descriptor

 

SYNOPSIS

       #include <sys/epoll.h>

 

       int epoll_wait(int epfd, struct epoll_event * events, int maxevents, int timeout)

 

DESCRIPTION

       Wait  for  events on the epoll file descriptor epfd for amaximum time of timeout milliseconds. The memory area  pointed to byevents will contain the events that will be available  for  the caller.   Up  to  maxevents  are returned by epoll_wait(2).  Themaxevents parameter must be greater than zero. Specifying a timeout of-1 makes epoll_wait(2) wait indefinitely, while specifying a timeoutequal to zero makes epoll_wait(2) to return immediately even if noevents are available ( return code equal to zero ).  The structepoll_event is defined as :

 

            typedef union epoll_data {

                 void *ptr;

                 int fd;

                 __uint32_t u32;

                 __uint64_t u64;

            } epoll_data_t;

 

            struct epoll_event {

                 __uint32_t events;  /* Epoll events */

                 epoll_data_t data;  /* User data variable */

            };

 

       The  data  of  each  returned  structure  will  contain  the same  data  the  user  set  with  a epoll_ctl(2)  (EPOLL_CTL_ADD,EPOLL_CTL_MOD) while the events memberwill contain the returned event bit field.

 

RETURN VALUE

       When successful, epoll_wait(2) returns the number of filedescriptors ready for the requested I/O, or  zero  if  no filedescriptor became ready during the requested timeout milliseconds. When an error occurs, epoll_wait(2) returns -1 and errno is setappropriately.

 

ERRORS

       EBADF  epfd is not a valid file descriptor.

       EFAULT The memory area pointed to by events is not accessible with write permissions.

       EINTR  The call was interrupted by a signal handler before any of the requested events occurred or the  timeout expired.

       EINVAL epfd is not an epoll file descriptor, or maxevents is less than or equal to zero.

 

CONFORMING TO

       epoll_wait(2)  is a new API introduced in Linux kernel 2.5.44.  The interface should be finalized by Linux kernel 2.5.66.

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
问题集锦(16
linuxepoll模型源码分析一函数实现
Linux epoll
Linux 2.6内核中提高网络I/O性能的新方法
Linux中select poll和epoll的区别
线程池epoll
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服