epoll源码分析---sys_epoll_wait()函数 http://blog.csdn.net/hbhhww/article/details/7746638

 

epoll源码分析---sys_epoll_wait()函数

分类: Linux内核   3070人阅读  评论(1)  收藏  举报

一、sys_epoll_wait()函数

源码及分析如下所示:

[cpp]  view plain copy
  1. /* 
  2.  * Implement the event wait interface for the eventpoll file. It is the kernel 
  3.  * part of the user space epoll_wait(2). 
  4.  */  
  5. SYSCALL_DEFINE4(epoll_wait, int, epfd, struct epoll_event __user *, events,  
  6.         int, maxevents, int, timeout)  
  7. {  
  8.     int error;  
  9.     struct file *file;  
  10.     struct eventpoll *ep;  
  11.   
  12.     /* The maximum number of event must be greater than zero */  
  13.     /* 
  14.      * 检查maxevents参数。 
  15.      */  
  16.     if (maxevents <= 0 || maxevents > EP_MAX_EVENTS)  
  17.         return -EINVAL;  
  18.   
  19.     /* Verify that the area passed by the user is writeable */  
  20.     /* 
  21.      * 检查用户空间传入的events指向的内存是否可写。参见__range_not_ok()。 
  22.      */  
  23.     if (!access_ok(VERIFY_WRITE, events, maxevents * sizeof(struct epoll_event))) {  
  24.         error = -EFAULT;  
  25.         goto error_return;  
  26.     }  
  27.   
  28.     /* Get the "struct file *" for the eventpoll file */  
  29.     /* 
  30.      * 获取epfd对应的eventpoll文件的file实例,file结构是在epoll_create中创建 
  31.      */  
  32.     error = -EBADF;  
  33.     file = fget(epfd);  
  34.     if (!file)  
  35.         goto error_return;  
  36.   
  37.     /* 
  38.      * We have to check that the file structure underneath the fd 
  39.      * the user passed to us _is_ an eventpoll file. 
  40.      */  
  41.     /* 
  42.      * 通过检查epfd对应的文件操作是不是eventpoll_fops 
  43.      * 来判断epfd是否是一个eventpoll文件。如果不是 
  44.      * 则返回EINVAL错误。 
  45.      */  
  46.     error = -EINVAL;  
  47.     if (!is_file_epoll(file))  
  48.         goto error_fput;  
  49.   
  50.     /* 
  51.      * At this point it is safe to assume that the "private_data" contains 
  52.      * our own data structure. 
  53.      */  
  54.     ep = file->private_data;  
  55.   
  56.     /* Time to fish for events ... */  
  57.     error = ep_poll(ep, events, maxevents, timeout);  
  58.   
  59. error_fput:  
  60.     fput(file);  
  61. error_return:  
  62.   
  63.     return error;  
  64. }  
sys_epoll_wait()是epoll_wait()对应的系统调用,主要用来获取文件状态已经就绪的事件,该函数检查参数、获取eventpoll文件后调用ep_poll()来完成主要的工作。在分析ep_poll()函数之前,先介绍一下使用epoll_wait()时可能犯的错误(接下来介绍的就是我犯过的错误):

  1、返回EBADF错误

    除非你故意指定一个不存在的文件描述符,否则几乎百分百肯定,你的程序有BUG了!从源码中可以看到调用fget()函数返回NULL时,会返回此错误。fget()源码如下:

[cpp]  view plain copy
  1. struct file *fget(unsigned int fd)  
  2. {  
  3.     struct file *file;  
  4.     struct files_struct *files = current->files;  
  5.   
  6.     rcu_read_lock();  
  7.     file = fcheck_files(files, fd);  
  8.     if (file) {  
  9.         if (!atomic_long_inc_not_zero(&file->f_count)) {  
  10.             /* File object ref couldn't be taken */  
  11.             rcu_read_unlock();  
  12.             return NULL;  
  13.         }  
  14.     }  
  15.     rcu_read_unlock();  
  16.   
  17.     return file;  
  18. }  
主要看这句(struct files_struct *files = current->files;),这条语句是获取描述当前进程已经打开的文件的files_struct结构,然后从这个结构中查找传入的fd对应的file实例,如果没有找到,说明当前进程中打开的文件不包括这个fd,所以几乎百分百肯定是程序设计的问题。我的程序出错,就是因为在父进程中创建了文件描述符,但是将子进程变为守护进程了,也就没有继承父进程中打开的文件。
  2、死循环(一般不会犯,但是我是第一次用,犯了)

 epoll_wait()中有一个设置超时时间的参数,所以我在循环中没有使用睡眠队列的操作,想依赖epoll的睡眠操作,所以在返回值小于等于0时,直接进行下一次循环,没有充分考虑epoll_wait()的返回值小于0时的不同情况,所以代码写成了下面的样子:

[cpp]  view plain copy
  1. for(;;) {  
  2.     ......  
  3.     events = epoll_wait(fcluster_epfd, fcluster_wait_events,   
  4.             fcluster_wait_size, 3000);  
  5.         if (unlikely(events <= 0)) {  
  6.             continue;  
  7.         }  
  8.     .......  
  9. }  

当epoll_wait()返回EBADF或EFAULT时,就会陷入死循环,因此此时还没有进入睡眠的操作。

二、ep_poll()函数

下面来看获取事件的主要函数ep_poll(),源码及分析如下:

[cpp]  view plain copy
  1. static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,  
  2.            int maxevents, long timeout)  
  3. {  
  4.     int res, eavail;  
  5.     unsigned long flags;  
  6.     long jtimeout;  
  7.     wait_queue_t wait;  
  8.   
  9.     /* 
  10.      * Calculate the timeout by checking for the "infinite" value (-1) 
  11.      * and the overflow condition. The passed timeout is in milliseconds, 
  12.      * that why (t * HZ) / 1000. 
  13.      */  
  14.     /* 
  15.      * timeout是以毫秒为单位,这里是要转换为jiffies时间。 
  16.      * 这里加上999(即1000-1),是为了向上取整。 
  17.      */  
  18.     jtimeout = (timeout < 0 || timeout >= EP_MAX_MSTIMEO) ?  
  19.         MAX_SCHEDULE_TIMEOUT : (timeout * HZ + 999) / 1000;  
  20.   
  21. retry:  
  22.     spin_lock_irqsave(&ep->lock, flags);  
  23.   
  24.     res = 0;  
  25.     if (list_empty(&ep->rdllist)) {  
  26.         /* 
  27.          * We don't have any available event to return to the caller. 
  28.          * We need to sleep here, and we will be wake up by 
  29.          * ep_poll_callback() when events will become available. 
  30.          */  
  31.         init_waitqueue_entry(&wait, current);  
  32.         wait.flags |= WQ_FLAG_EXCLUSIVE;  
  33.         /* 
  34.          * 将当前进程加入到eventpoll的等待队列中, 
  35.          * 等待文件状态就绪或直到超时,或被 
  36.          * 信号中断。 
  37.          */  
  38.         __add_wait_queue(&ep->wq, &wait);  
  39.   
  40.         for (;;) {  
  41.             /* 
  42.              * We don't want to sleep if the ep_poll_callback() sends us 
  43.              * a wakeup in between. That's why we set the task state 
  44.              * to TASK_INTERRUPTIBLE before doing the checks. 
  45.              */  
  46.             set_current_state(TASK_INTERRUPTIBLE);  
  47.             /* 
  48.              * 如果就绪队列不为空,也就是说已经有文件的状态 
  49.              * 就绪或者超时,则退出循环。 
  50.              */  
  51.             if (!list_empty(&ep->rdllist) || !jtimeout)  
  52.                 break;  
  53.             /* 
  54.              * 如果当前进程接收到信号,则退出 
  55.              * 循环,返回EINTR错误 
  56.              */  
  57.             if (signal_pending(current)) {  
  58.                 res = -EINTR;  
  59.                 break;  
  60.             }  
  61.   
  62.             spin_unlock_irqrestore(&ep->lock, flags);  
  63.             /* 
  64.              * 主动让出处理器,等待ep_poll_callback()将当前进程 
  65.              * 唤醒或者超时,返回值是剩余的时间。从这里开始 
  66.              * 当前进程会进入睡眠状态,直到某些文件的状态 
  67.              * 就绪或者超时。当文件状态就绪时,eventpoll的回调 
  68.              * 函数ep_poll_callback()会唤醒在ep->wq指向的等待队列中的进程。 
  69.              */  
  70.             jtimeout = schedule_timeout(jtimeout);  
  71.             spin_lock_irqsave(&ep->lock, flags);  
  72.         }  
  73.         __remove_wait_queue(&ep->wq, &wait);  
  74.   
  75.         set_current_state(TASK_RUNNING);  
  76.     }  
  77.     /* Is it worth to try to dig for events ? */  
  78.     /* 
  79.      * ep->ovflist链表存储的向用户传递事件时暂存就绪的文件。 
  80.      * 所以不管是就绪队列ep->rdllist不为空,或者ep->ovflist不等于 
  81.      * EP_UNACTIVE_PTR,都有可能现在已经有文件的状态就绪。 
  82.      * ep->ovflist不等于EP_UNACTIVE_PTR有两种情况,一种是NULL,此时 
  83.      * 可能正在向用户传递事件,不一定就有文件状态就绪, 
  84.      * 一种情况时不为NULL,此时可以肯定有文件状态就绪, 
  85.      * 参见ep_send_events()。 
  86.      */  
  87.     eavail = !list_empty(&ep->rdllist) || ep->ovflist != EP_UNACTIVE_PTR;  
  88.   
  89.     spin_unlock_irqrestore(&ep->lock, flags);  
  90.   
  91.     /* 
  92.      * Try to transfer events to user space. In case we get 0 events and 
  93.      * there's still timeout left over, we go trying again in search of 
  94.      * more luck. 
  95.      */  
  96.     /* 
  97.      * 如果没有被信号中断,并且有事件就绪, 
  98.      * 但是没有获取到事件(有可能被其他进程获取到了), 
  99.      * 并且没有超时,则跳转到retry标签处,重新等待 
  100.      * 文件状态就绪。 
  101.      */  
  102.     if (!res && eavail &&  
  103.         !(res = ep_send_events(ep, events, maxevents)) && jtimeout)  
  104.         goto retry;  
  105.   
  106.     /* 
  107.      * 返回获取到的事件的个数或者错误码 
  108.      */  
  109.     return res;  
  110. }  
ep_poll()的主要过程是:首先将超时时间(以毫秒为单位)转换为jiffies时间,然后检查是否有事件发生,如果没有事件发生,则将当前进程加入到eventpoll中的等待队列中,直到事件发生或者超时。如果有事件发生,则调用ep_send_events()将发生的事件传入用户空间的内存。ep_send_events()函数将用户传入的内存简单封装到ep_send_events_data结构中,然后调用ep_scan_ready_list()将就绪队列中的事件传入用户空间的内存。

三、ep_scan_ready_list()函数

源码及分析如下:

[cpp]  view plain copy
  1. /** 
  2.  * ep_scan_ready_list - Scans the ready list in a way that makes possible for 
  3.  *                      the scan code, to call f_op->poll(). Also allows for 
  4.  *                      O(NumReady) performance. 
  5.  * 
  6.  * @ep: Pointer to the epoll private data structure. 
  7.  * @sproc: Pointer to the scan callback. 
  8.  * @priv: Private opaque data passed to the @sproc callback. 
  9.  * 
  10.  * Returns: The same integer error code returned by the @sproc callback. 
  11.  */  
  12. static int ep_scan_ready_list(struct eventpoll *ep,  
  13.                   int (*sproc)(struct eventpoll *,  
  14.                        struct list_head *, void *),  
  15.                   void *priv)  
  16. {  
  17.     int error, pwake = 0;  
  18.     unsigned long flags;  
  19.     struct epitem *epi, *nepi;  
  20.     LIST_HEAD(txlist);  
  21.   
  22.     /* 
  23.      * We need to lock this because we could be hit by 
  24.      * eventpoll_release_file() and epoll_ctl(). 
  25.      */  
  26.     /* 
  27.      * 获取互斥锁,该互斥锁在移除eventpoll文件(eventpoll_release_file() )、 
  28.      * 操作文件描述符(epoll_ctl())和向用户传递事件(epoll_wait())之间进行互斥 
  29.      */  
  30.     mutex_lock(&ep->mtx);  
  31.   
  32.     /* 
  33.      * Steal the ready list, and re-init the original one to the 
  34.      * empty list. Also, set ep->ovflist to NULL so that events 
  35.      * happening while looping w/out locks, are not lost. We cannot 
  36.      * have the poll callback to queue directly on ep->rdllist, 
  37.      * because we want the "sproc" callback to be able to do it 
  38.      * in a lockless way. 
  39.      */  
  40.     spin_lock_irqsave(&ep->lock, flags);  
  41.     /* 
  42.      * 将就绪队列中就绪的文件链表暂存在临时 
  43.      * 表头txlist中,并且初始化就绪队列。 
  44.      */  
  45.     list_splice_init(&ep->rdllist, &txlist);  
  46.     /* 
  47.      * 将ovflist置为NULL,表示此时正在向用户空间传递 
  48.      * 事件。如果此时有文件状态就绪,不会放在 
  49.      * 就绪队列中,而是放在ovflist链表中。 
  50.      */  
  51.     ep->ovflist = NULL;  
  52.     spin_unlock_irqrestore(&ep->lock, flags);  
  53.   
  54.     /* 
  55.      * Now call the callback function. 
  56.      */  
  57.     /* 
  58.      * 调用ep_send_events_proc()将就绪队列中的事件 
  59.      * 存入用户传入的内存中。 
  60.      */  
  61.     error = (*sproc)(ep, &txlist, priv);  
  62.   
  63.     spin_lock_irqsave(&ep->lock, flags);  
  64.     /* 
  65.      * During the time we spent inside the "sproc" callback, some 
  66.      * other events might have been queued by the poll callback. 
  67.      * We re-insert them inside the main ready-list here. 
  68.      */  
  69.     /* 
  70.      * 在调用sproc指向的函数将就绪队列中的事件 
  71.      * 传递到用户传入的内存的过程中,可能有文件 
  72.      * 状态就绪,这些事件会暂存在ovflist链表中, 
  73.      * 所以这里要将ovflist中的事件移到就绪队列中。 
  74.      */  
  75.     for (nepi = ep->ovflist; (epi = nepi) != NULL;  
  76.          nepi = epi->next, epi->next = EP_UNACTIVE_PTR) {  
  77.         /* 
  78.          * We need to check if the item is already in the list. 
  79.          * During the "sproc" callback execution time, items are 
  80.          * queued into ->ovflist but the "txlist" might already 
  81.          * contain them, and the list_splice() below takes care of them. 
  82.          */  
  83.         if (!ep_is_linked(&epi->rdllink))  
  84.             list_add_tail(&epi->rdllink, &ep->rdllist);  
  85.     }  
  86.     /* 
  87.      * We need to set back ep->ovflist to EP_UNACTIVE_PTR, so that after 
  88.      * releasing the lock, events will be queued in the normal way inside 
  89.      * ep->rdllist. 
  90.      */  
  91.     /* 
  92.      * 重新初始化ovflist,表示传递事件已经完成, 
  93.      * 之后再有文件状态就绪,这些事件会直接 
  94.      * 放在就绪队列中。 
  95.      */  
  96.     ep->ovflist = EP_UNACTIVE_PTR;  
  97.   
  98.     /* 
  99.      * Quickly re-inject items left on "txlist". 
  100.      */  
  101.     /* 
  102.      * 如果sproc指向的函数ep_send_events_proc()中处理出错或者某些文件的 
  103.      * 触发方式设置为水平触发(Level Trigger),txlist中可能还有事件,需要 
  104.      * 将这些就绪的事件重新添加回eventpoll文件的就绪队列中。 
  105.      */  
  106.     list_splice(&txlist, &ep->rdllist);  
  107.   
  108.     if (!list_empty(&ep->rdllist)) {  
  109.         /* 
  110.          * Wake up (if active) both the eventpoll wait list and 
  111.          * the ->poll() wait list (delayed after we release the lock). 
  112.          */  
  113.         if (waitqueue_active(&ep->wq))  
  114.             wake_up_locked(&ep->wq);  
  115.         if (waitqueue_active(&ep->poll_wait))  
  116.             pwake++;  
  117.     }  
  118.     spin_unlock_irqrestore(&ep->lock, flags);  
  119.   
  120.     mutex_unlock(&ep->mtx);  
  121.   
  122.     /* We have to call this outside the lock */  
  123.     if (pwake)  
  124.         ep_poll_safewake(&ep->poll_wait);  
  125.   
  126.     return error;  
  127. }  
ep_scan_ready_list()函数的参数sproc指向的函数是ep_send_events_proc(),参见ep_send_events()函数。

四、ep_send_events_proc()函数

[cpp]  view plain copy
  1. /* 
  2.  * @head:已经就绪的文件列表 
  3.  * @priv:用来存储已经就绪的文件 
  4.  */  
  5. static int ep_send_events_proc(struct eventpoll *ep, struct list_head *head,  
  6.                    void *priv)  
  7. {  
  8.     struct ep_send_events_data *esed = priv;  
  9.     int eventcnt;  
  10.     unsigned int revents;  
  11.     struct epitem *epi;  
  12.     struct epoll_event __user *uevent;  
  13.   
  14.     /* 
  15.      * We can loop without lock because we are passed a task private list. 
  16.      * Items cannot vanish during the loop because ep_scan_ready_list() is 
  17.      * holding "mtx" during this call. 
  18.      */  
  19.     for (eventcnt = 0, uevent = esed->events;  
  20.          !list_empty(head) && eventcnt < esed->maxevents;) {  
  21.         epi = list_first_entry(head, struct epitem, rdllink);  
  22.   
  23.         list_del_init(&epi->rdllink);  
  24.   
  25.         /* 
  26.          * 调用文件的poll函数有两个作用,一是在文件的唤醒 
  27.          * 队列上注册回调函数,二是返回文件当前的事件状 
  28.          * 态,如果第二个参数为NULL,则只是查看文件当前 
  29.          * 状态。 
  30.          */  
  31.         revents = epi->ffd.file->f_op->poll(epi->ffd.file, NULL) &  
  32.             epi->event.events;  
  33.   
  34.         /* 
  35.          * If the event mask intersect the caller-requested one, 
  36.          * deliver the event to userspace. Again, ep_scan_ready_list() 
  37.          * is holding "mtx", so no operations coming from userspace 
  38.          * can change the item. 
  39.          */  
  40.         if (revents) {  
  41.             /* 
  42.              * 向用户内存传值失败时,将当前epitem实例重新放回 
  43.              * 到链表中,从这里也可以看出,在处理失败后,head指向的 
  44.              * 链表(对应ep_scan_ready_list()中的临时变量txlist)中 
  45.              * 有可能会没有完全处理完,因此在ep_scan_ready_list()中 
  46.              * 需要下面的语句 
  47.              *    list_splice(&txlist, &ep->rdllist); 
  48.              * 来将未处理的事件重新放回到eventpoll文件的就绪队列中。 
  49.              */  
  50.             if (__put_user(revents, &uevent->events) ||  
  51.                 __put_user(epi->event.data, &uevent->data)) {  
  52.                 list_add(&epi->rdllink, head);  
  53.                 /* 
  54.                  * 如果此时已经获取了部分事件,则返回已经获取的事件个数, 
  55.                  * 否则返回EFAULT错误。 
  56.                  */  
  57.                 return eventcnt ? eventcnt : -EFAULT;  
  58.             }  
  59.             eventcnt++;  
  60.             uevent++;  
  61.             if (epi->event.events & EPOLLONESHOT)  
  62.                 epi->event.events &= EP_PRIVATE_BITS;  
  63.             /* 
  64.              * 如果是触发方式不是边缘触发(Edge Trigger),而是水平 
  65.              * 触发(Level Trigger),需要将当前的epitem实例添加回 
  66.              * 链表中,下次读取事件时会再次上报。 
  67.              */  
  68.             else if (!(epi->event.events & EPOLLET)) {  
  69.                 /* 
  70.                  * If this file has been added with Level 
  71.                  * Trigger mode, we need to insert back inside 
  72.                  * the ready list, so that the next call to 
  73.                  * epoll_wait() will check again the events 
  74.                  * availability. At this point, noone can insert 
  75.                  * into ep->rdllist besides us. The epoll_ctl() 
  76.                  * callers are locked out by 
  77.                  * ep_scan_ready_list() holding "mtx" and the 
  78.                  * poll callback will queue them in ep->ovflist. 
  79.                  */  
  80.                 list_add_tail(&epi->rdllink, &ep->rdllist);  
  81.             }  
  82.         }  
  83.     }  
  84.   
  85.     return eventcnt;  
  86. }  


版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值