分享

三、I/O模型

 sven_ 2013-09-13

数据结构

  1. [include/types/fd.h]  
  2. /* info about one given fd */  
  3. struct fdtab {  
  4.     struct {  
  5.         int (*f)(int fd);          /* read/write function */  
  6.         struct buffer *b;         /* read/write buffer */  
  7.     } cb[DIR_SIZE];  
  8.     void *owner;               /* the session (or proxy) associated with this fd */  
  9.     struct {                    /* used by pollers which support speculative polling */  
  10.         unsigned char e;        /* read and write events status. 4 bits*/  
  11.         unsigned int s1;         /* Position in spec list+1. 0=not in list. */  
  12.     } spec;  
  13.     unsigned short flags;         /* various flags precising the exact status of this fd */  
  14.     unsigned char state;          /* the state of this fd */  
  15.     unsigned char ev;            /* event seen in return of poll() : FD_POLL_* */  
  16. };  
  17.   
  18. struct poller {  
  19.     void   *private;                 /* any private data for the poller */  
  20.     int REGPRM2 (*is_set)(const int fd, int dir); /* check if <fd> is being polled for dir <dir> */  
  21.     int  REGPRM2    (*set)(const int fd, int dir);    /* set   polling on <fd> for <dir> */  
  22.     int  REGPRM2    (*clr)(const int fd, int dir);       /* clear polling on <fd> for <dir> */  
  23.     int  REGPRM2 (*cond_s)(const int fd, int dir); * set   polling on <fd> for <dir> if unset */  
  24.     int  REGPRM2 (*cond_c)(const int fd, int dir); /* clear polling on <fd> for <dir> if set */  
  25.     void REGPRM1    (*rem)(const int fd);      /* remove any polling on <fd> */  
  26.     void REGPRM1    (*clo)(const int fd);      /* mark <fd> as closed */  
  27.         void REGPRM2   (*poll)(struct poller *p, int exp);   /* the poller itself */  
  28.     int  REGPRM1   (*init)(struct poller *p);            /* poller initialization */  
  29.     void REGPRM1   (*term)(struct poller *p);            /* termination of this poller */  
  30.     int  REGPRM1   (*test)(struct poller *p);            /* pre-init check of the poller */  
  31.     int  REGPRM1   (*fork)(struct poller *p);            /* post-fork re-opening */  
  32.     const char   *name;                                  /* poller name */  
  33.     int    pref;                           /* try pollers with higher preference first */  
  34. };  
  35.   
  36. [src/fd.c]  
  37. struct fdtab *fdtab = NULL;     /* array of all the file descriptors */  
  38. struct fdinfo *fdinfo = NULL;   /* less-often used infos for file descriptors */  
  39. int maxfd;                      /* # of the highest fd + 1 */  
  40. int totalconn;                  /* total # of terminated sessions */  
  41. int actconn;                    /* # of active sessions */  
  42.   
  43. struct poller pollers[MAX_POLLERS];  
  44. struct poller cur_poller;  
  45. int nbpollers = 0;  

在看到fdtabpoller的结构体时,然后查看ev_epoll.c的时候可能会奇怪为什么会设置成这样。但是如果先查看ev_sepoll.c的话可能很多疑惑都没有了。

sepoll

Haproxy中,作者在epoll上将模型推进至sepoll(我不知道是否在此之前就有人提出或者使用这种方法),从理论上来说,这种模型的总体效率应该比epoll更好,虽然说它是基于epoll的,因为它能够减少较多与epoll相关的昂贵的系统调用。

sepoll,作者在代码注释中称为speculative I/O。Sepoll的原理就是,对于刚accept完的套接字描述符,一般都是直接能够读取导数据的;对于connect完的描述符,一般都是可写的;即使是对于在传输数据的链接,它也是能提升效率的,因为假设对于某一条链接的某端已经处于epoll的等待队列中,那么另一端也是需要做出反应的,要么发送数据,要么接收数据,这依赖于(读/写)缓冲区的水位。

当然,作者也描述了sepoll的缺点,那就是这可能会导致在epoll队列中的可用事件缺少而变得饥饿(starve the polled events)(我对此处饥饿的理解是,有足够资源的时候不给予需要的人;poll本来就是用于处理多个描述符专用,假设只处理几个描述符,那么poll根本就提升不了多少性能,因为它本身也是系统调用,因此需要保持poll队列含有一定数量的fd,否则就是出现饥饿情况),作者说实验证明,当epoll队列出现饥饿的情况时,压力会转到spec I/O上面,此时由于每次去读取或者写入,但是都失败,陷入恶性循环,会严重的降低系统性能(spec list描述符较多,一直轮询肯定会导致性能问题)。用于解决此问题的方法,可以通过减少epoll一次处理的事件来解决这个问题(对spec list的不能使用这个方法,因为实验显示,spec list中2/3的fd是新的,只有1/3的fd是老的)。作者说这是基于以下两点事实,第一,对于位于spec listfd,不能也将它们注册在epoll中等待;第二是,即使在系统压力非常大的时候,我们基本上也不会同时对同一个fd进行读与写的流操作。作者所说的后面一个事实我认为是这样的,对于客户端,一个请求都是将请求数据发送完成之后,后端才会对其进行响应;对于服务器,都是接收玩请求之后,才会发回响应数据。

作者说第一个事实意味着在饥饿期间,poll等待队列中不会有超过一半的fd。否则的话,说明spec list中的fdpoll list少,那么也就没有饥饿情况。第二个事实意味着我们只对最大数量描述符的一半事件感兴趣(每个描述符要么读,要么写)。

减少poll list一次处理的数量用于解决poll list饥饿的情况,可以这么理解,假设每个fd经过一次读和一次写之后就被销毁,那么对于第二个事实,在进行读的时候,poll listfd不会减少,影响不大,但是在写的时候,由于读与写都已经完成了,那么可能这一次会导致大量的fd被移除,而补充又跟不上,这就可能会导致饥饿;但是由于第一个事实限制每次可处理的最大数量,那么一次读写完成被撤掉的fd数量就减少了,而且把poll list中的fd分成了两部分,错开了它们移出poll list的时间,减少了一次被移除的fd数量,那么就应该能够使后续的fd补充跟上。

那么对于fd本来就不多,导致poll list分配到的很少导致的饥饿怎么办?此时由于fd不多,spec listfd也不多,,对性能的影响不是很大,基本上忽略了。

作者最后说明,如果我们能够在负载高峰时段保证poll list拥有maxsock/2/2数量的事件,这意味着我们应该给poll list分配maxsock/4的事件,就不会受饥饿的影响。Maxsock/2/2来源作者没有明确说明,不过从上面的的解释来看,第一除2应该是表示如果poll list如果有不小于maxsock/2的fd,那么就不会受饥饿的影响;第二个除2暂时还不能确定,假如是根据第二个事实来的,那也不是很合理,因为一个sock肯定包含两个事件,一次处理只做一个事件的话,那么时间数量也是和sock数量本身一样的。

接下来看看sepoll的处理流程。

  1. [src/ev_sepoll.c]  
  2. #define FD_EV_IN_SL 1  
  3. #define FD_EV_IN_PL 4  
  4.   
  5. #define FD_EV_IDLE  0  
  6. #define FD_EV_SPEC  (FD_EV_IN_SL)  
  7. #define FD_EV_WAIT  (FD_EV_IN_PL)  
  8. #define FD_EV_STOP  (FD_EV_IN_SL|FD_EV_IN_PL)  
  9.   
  10. /* Those match any of R or W for Spec list or Poll list */  
  11. #define FD_EV_RW_SL (FD_EV_IN_SL | (FD_EV_IN_SL << 1))  
  12. #define FD_EV_RW_PL (FD_EV_IN_PL | (FD_EV_IN_PL << 1))  
  13. #define FD_EV_MASK_DIR  (FD_EV_IN_SL|FD_EV_IN_PL)  
  14.   
  15. #define FD_EV_IDLE_R    0  
  16. #define FD_EV_SPEC_R    (FD_EV_IN_SL)  
  17. #define FD_EV_WAIT_R    (FD_EV_IN_PL)  
  18. #define FD_EV_STOP_R    (FD_EV_IN_SL|FD_EV_IN_PL)  
  19. #define FD_EV_MASK_R    (FD_EV_IN_SL|FD_EV_IN_PL)  
  20.   
  21. #define FD_EV_IDLE_W    (FD_EV_IDLE_R << 1)  
  22. #define FD_EV_SPEC_W    (FD_EV_SPEC_R << 1)  
  23. #define FD_EV_WAIT_W    (FD_EV_WAIT_R << 1)  
  24. #define FD_EV_STOP_W    (FD_EV_STOP_R << 1)  
  25. #define FD_EV_MASK_W    (FD_EV_MASK_R << 1)  
  26.   
  27. #define FD_EV_MASK  (FD_EV_MASK_W | FD_EV_MASK_R)  

从以上宏定义可以看出,对于位于spec list的读写事件分别对应的最低两位;对于位于poll list的读写事件位于第三、四位。

  1. [src/ev_sepoll.c]_do_poll()  
  2. REGPRM2 static void _do_poll(struct poller *p, int exp)  
  3. {  
  4.     static unsigned int last_skipped;  
  5.     static unsigned int spec_processed;  
  6.     int status, eo;  
  7.     int fd, opcode;  
  8.     int count;  
  9.     int spec_idx;  
  10.     int wait_time;  
  11.     int looping = 0;  
  12.   
  13.  re_poll_once:  
  14.     /* Here we have two options : 
  15.      * - either walk the list forwards and hope to match more events 
  16.      * - or walk it backwards to minimize the number of changes and 
  17.      *   to make better use of the cache. 
  18.      * Tests have shown that walking backwards improves perf by 0.2%. 
  19.      */  

首先处理的是位于spec list的fd,作者说从后面遍历spec list能够提高0.2%的效率,这是因为spec list总是把最新的fd存储在最后,而对于最新的fd,基本上很可能是直接可读或者可写的。

  1. [src/ev_sepoll.c]  
  2.     status = 0;  
  3.     spec_idx = nbspec;  
  4.     while (likely(spec_idx > 0)) {  
  5.         int done;  
  6.   
  7.         spec_idx--;  
  8.         fd = spec_list[spec_idx];  
  9.         eo = fdtab[fd].spec.e;  /* save old events */  
  10.   
  11.         if (looping && --fd_created < 0) {  
  12.             /* we were just checking the newly created FDs */  
  13.             break;  
  14.         }  

拿到fd,然后根据fdfdtab中拿到对应的信息。如果这是第二次处理循环,只是为了检查由于listen fd进行accept之后新创建的fd,因此作者专门使用一个变量fd_created用于记录新创建的fd数量,当新的fd处理完成之后,直接跳出循环了。

  1. [src/ev_sepoll.c]_do_poll()  
  2.         /* 
  3.          * Process the speculative events. 
  4.          * 
  5.          * Principle: events which are marked FD_EV_SPEC are processed 
  6.          * with their assigned function. If the function returns 0, it 
  7.          * means there is nothing doable without polling first. We will 
  8.          * then convert the event to a pollable one by assigning them 
  9.          * the WAIT status. 
  10.          */  

作者说明规则是处理标志了FD_EV_SPEC事件的,并且调用他们指定的函数,如果函数返回0,那么表示现在没有任何事可做,我们应该先对其进行一个poll等待先。

  1. [src/ev_sepoll.c]_do_poll()  
  2. #ifdef DEBUG_DEV  
  3.         if (fdtab[fd].state == FD_STCLOSE) {  
  4.             fprintf(stderr,"fd=%d, fdtab[].ev=%x, fdtab[].spec.e=%x, .s=%d, idx=%d\n",  
  5.                 fd, fdtab[fd].ev, fdtab[fd].spec.e, fdtab[fd].spec.s1, spec_idx);  
  6.         }  
  7. #endif  
  8.         done = 0;  
  9.         fdtab[fd].ev &= FD_POLL_STICKY;  
  10.         if ((eo & FD_EV_MASK_R) == FD_EV_SPEC_R) {  
  11.             /* The owner is interested in reading from this FD */  
  12.             if (fdtab[fd].state != FD_STERROR) {  
  13.                 /* Pretend there is something to read */  
  14.                 fdtab[fd].ev |= FD_POLL_IN;  
  15.                 if (!fdtab[fd].cb[DIR_RD].f(fd))  
  16.                     fdtab[fd].spec.e ^= (FD_EV_WAIT_R ^ FD_EV_SPEC_R);  
  17.                 else  
  18.                     done = 1;  
  19.             }  
  20.         }  
  21.         else if ((eo & FD_EV_MASK_R) == FD_EV_STOP_R) {  
  22.             /* This FD was being polled and is now being removed. */  
  23.             fdtab[fd].spec.e &= ~FD_EV_MASK_R;  
  24.         }  
  25.   
  26.         if ((eo & FD_EV_MASK_W) == FD_EV_SPEC_W) {  
  27.             /* The owner is interested in writing to this FD */  
  28.             if (fdtab[fd].state != FD_STERROR) {  
  29.                 /* Pretend there is something to write */  
  30.                 fdtab[fd].ev |= FD_POLL_OUT;  
  31.                 if (!fdtab[fd].cb[DIR_WR].f(fd))  
  32.                     fdtab[fd].spec.e ^= (FD_EV_WAIT_W ^ FD_EV_SPEC_W);  
  33.                 else  
  34.                     done = 1;  
  35.             }  
  36.         }  
  37.         else if ((eo & FD_EV_MASK_W) == FD_EV_STOP_W) {  
  38.             /* This FD was being polled and is now being removed. */  
  39.             fdtab[fd].spec.e &= ~FD_EV_MASK_W;  
  40.     }  

对于位于spec fd的读事件,当函数返回0时,去掉FD_EV_SPEC_R事件,转为FD_EV_SPEC_WAIT_R事件,表示这个描述符应该放入poll等待队列。函数返回不为0,那么表示此次spec处理时成功的,那么依然将其留在spec队列中,记录成功标志。在处理相应事件的时候还用fdtab[fd].ev记录下了相应fd被处理的事件。

对于被标志为停止了的fd,那么将其相应的读事件全部清空。

写事件的处理与读事件的处理相同。

  1. [src/ev_sepoll.c]_do_poll()  
  2.         status += done;  
  3.         /* one callback might already have closed the fd by itself */  
  4.         if (fdtab[fd].state == FD_STCLOSE)  
  5.         continue;  

前面只要读或者写成功,那么表示此次的spec处理是成功的,因此对其进行数量统计,当然有可能对应的fd在其相应的读或者写函数中已经关闭,那么以下的事情就没必要做了。

  1. [src/ev_sepoll.c]_do_poll()  
  2.         /* Now, we will adjust the event in the poll list. Indeed, it 
  3.          * is possible that an event which was previously in the poll 
  4.          * list now goes out, and the opposite is possible too. We can 
  5.          * have opposite changes for READ and WRITE too. 
  6.          */  
  7.         if ((eo ^ fdtab[fd].spec.e) & FD_EV_RW_PL) {  
  8.             /* poll status changed*/  
  9.             if ((fdtab[fd].spec.e & FD_EV_RW_PL) == 0) {  
  10.                 /* fd removed from poll list */  
  11.                 opcode = EPOLL_CTL_DEL;  
  12.             }  
  13.             else if ((eo & FD_EV_RW_PL) == 0) {  
  14.                 /* new fd in the poll list */  
  15.                 opcode = EPOLL_CTL_ADD;  
  16.             }  
  17.             else {  
  18.                 /* fd status changed */  
  19.                 opcode = EPOLL_CTL_MOD;  
  20.             }  
  21.   
  22.             /* construct the epoll events based on new state */  
  23.             ev.events = 0;  
  24.             if (fdtab[fd].spec.e & FD_EV_WAIT_R)  
  25.                 ev.events |= EPOLLIN;  
  26.   
  27.             if (fdtab[fd].spec.e & FD_EV_WAIT_W)  
  28.                 ev.events |= EPOLLOUT;  
  29.   
  30.             ev.data.fd = fd;  
  31.             epoll_ctl(epoll_fd, opcode, fd, &ev);  
  32.         }  

对于此处的表达式结果,结合以上三种情况即可知道其结果。首先是对于done的情况,此时o^fdtab[fd].spec.e==0,所以不会进入分支;接着是对于函数返回值为0的情况,这种情况下,FD_EV_SPEC的事件被清除,FD_EV_POLL的事件被设置,因此结果为不为0,会进入分支,进入分支后,易知内部分支会进入第二分支,也就是将fd加到epoll中;第三种是FD_EV_STOP类型导致事件被清空,计算结果不为0,进入分支,由于spec.e被清零,因此进入第一个分支,也就是从epoll list中移除fd

在进行操作判断之后,然后对poll listfd进行相应的操作。

  1. [src/ev_sepoll.c]_do_poll()  
  2.         if (!(fdtab[fd].spec.e & FD_EV_RW_SL)) {  
  3.             /* This fd switched to combinations of either WAIT or 
  4.              * IDLE. It must be removed from the spec list. 
  5.              */  
  6.             release_spec_entry(fd);  
  7.             continue;  
  8.         }  
  9.     }  

在对poll list更新之后,还需要检查fd新的事件中是否已经不再包含spec的事件,如果是,那么需要将fdfdtab中移除。至此spec的循环处理已经结束。

总结一下上面的流程。从后往前遍历spec list,根据对fd有兴趣的事件调用相应函数进行数据的输入和输出(所有的fd都是非阻塞形式的),如果调用成功,那么相应的fd仍然保留于spec list中,并统计在spec中成功处理的fd数量;若失败,那么需要将其放入poll list去等待,因为在等待数据到来之前在spec list中并不能做什么;如果描述符已经被停止使用,那么将会从poll list或者spec list中移除。

  1. [src/ev_sepoll.c]_do_poll()  
  2.     /* It may make sense to immediately return here if there are enough 
  3.      * processed events, without passing through epoll_wait() because we 
  4.      * have exactly done a poll. 
  5.      * Measures have shown a great performance increase if we call the 
  6.      * epoll_wait() only the second time after speculative accesses have 
  7.      * succeeded. This reduces the number of unsucessful calls to 
  8.      * epoll_wait() by a factor of about 3, and the total number of calls 
  9.      * by about 2. 
  10.      * However, when we do that after having processed too many events, 
  11.      * events waiting in epoll() starve for too long a time and tend to 
  12.      * become themselves eligible for speculative polling. So we try to 
  13.      * limit this practise to reasonable situations. 
  14.     */  
  15.   
  16.     spec_processed += status;  
  17.   
  18.     if (looping) {  
  19.         last_skipped++;  
  20.         return;  
  21.     }  
  22.   
  23.     if (status >= MIN_RETURN_EVENTS && spec_processed < absmaxevents) {  
  24.         /* We have processed at least MIN_RETURN_EVENTS, it's worth 
  25.          * returning now without checking epoll_wait(). 
  26.          */  
  27.         if (++last_skipped <= 1) {  
  28.             tv_update_date(0, 1);  
  29.             return;  
  30.         }  
  31.     }  
  32.     last_skipped = 0;  

如果是第二次处理,也就是再回来处理新建的fd,那将last_skipped++并返回,这是为什么呢?因为之前作者描述过,一次对poll队列处理的数量减少点,既然要减少,之前做过一次了,那么这次就不再检查了。

last_skipped是用来标志当处理数量符合最小可返回数量时是否返回,如果本次返回是由于第二次处理而导致返回,那么下次出现处理数量达到最小可返回数量时不再返回。

如果spec处理成功的数量超过最小可以返回的数量并且spec_proc处理的数量不超过poll list最大的事件数,那么要是之前没设置跳过标志则返回。

第一次__do_poll循环,并且已处理数量不足以返回,那么将下次跳过标志清空。

以下流程除了最后的判断是否新建了fd而决定是否跳转到__do_poll开始再做一次spec处理之外,其他的流程和其他的I/O模型基本上是一致,因此对于其他的I/O模型不再解释。

  1. [src/ev_sepoll.c]_do_poll()  
  2.     if (nbspec || status || run_queue || signal_queue_len) {  
  3.         /* Maybe we have processed some events that we must report, or 
  4.          * maybe we still have events in the spec list, or there are 
  5.          * some tasks left pending in the run_queue, so we must not 
  6.          * wait in epoll() otherwise we will delay their delivery by 
  7.          * the next timeout. 
  8.          */  
  9.         wait_time = 0;  
  10.     }  
  11.     else {  
  12.         if (!exp)  
  13.             wait_time = MAX_DELAY_MS;  
  14.         else if (tick_is_expired(exp, now_ms))  
  15.             wait_time = 0;  
  16.         else {  
  17.             wait_time = TICKS_TO_MS(tick_remain(now_ms, exp)) + 1;  
  18.             if (wait_time > MAX_DELAY_MS)  
  19.                 wait_time = MAX_DELAY_MS;  
  20.         }  
  21. }  

要是之前的处理没有能够返回,那么接下来就需要真正的对epoll进行处理了,但是在处理之前则需要计算epoll_wait调用应该等待的时间。

如果spec队列还有fd(事件)存在,或者是spec已经有处理成功需要回去报告,或者是任务可执行队列有需要执行的任务,或者是信号队列有未决信号需要处理,那么对于epoll_wait的操作使用无阻塞的。

如果没有设置超时时间,那么将等待时间设置为程序允许的最大值。

如果给定的超时时间已经到期,那么对epoll_wait的调用也是无阻塞。

如果给定的超时时间还没到,那么计算余下的时间,如果余下的时间比程序允许的最大值还大那么将其设置为程序允许的最大值。

  1. [src/ev_sepoll.c]_do_poll()  
  2.     /* now let's wait for real events. We normally use maxpollevents as a 
  3.      * high limit, unless <nbspec> is already big, in which case we need 
  4.      * to compensate for the high number of events processed there. 
  5.      */  
  6.     fd = MIN(absmaxevents, spec_processed);  
  7.     fd = MAX(global.tune.maxpollevents, fd);  
  8.     fd = MIN(maxfd, fd);  
  9.     /* we want to detect if an accept() will create new speculative FDs here */  
  10.     //从此处可以看出,listen fd是放在epoll等待队列中的。  
  11.     fd_created = 0;  
  12.     spec_processed = 0;  
  13.     status = epoll_wait(epoll_fd, epoll_events, fd, wait_time);  
  14.     tv_update_date(wait_time, status);  

对于wait使用的数量,作者说明一般是使用maxpollevents作为限制的,除非spec list已经非常大了,那么才需要对其大处理量进行补偿。

epoll_wait返回之后需要对时间进行更新,如果是超时返回,那么需要将等待时间加上,否则根据返回值适当调整。

  1. [src/ev_sepoll.c]_do_poll()  
  2.     for (count = 0; count < status; count++) {  
  3.         int e = epoll_events[count].events;  
  4.         fd = epoll_events[count].data.fd;  
  5.   
  6.         /* it looks complicated but gcc can optimize it away when constants 
  7.          * have same values. 
  8.          */  
  9.         DPRINTF(stderr, "%s:%d: fd=%d, ev=0x%08x, e=0x%08x\n",  
  10.             __FUNCTION__, __LINE__,  
  11.             fd, fdtab[fd].ev, e);  
  12.   
  13.         fdtab[fd].ev &= FD_POLL_STICKY;  
  14.         fdtab[fd].ev |=   
  15.             ((e & EPOLLIN ) ? FD_POLL_IN  : 0) |  
  16.             ((e & EPOLLPRI) ? FD_POLL_PRI : 0) |  
  17.             ((e & EPOLLOUT) ? FD_POLL_OUT : 0) |  
  18.             ((e & EPOLLERR) ? FD_POLL_ERR : 0) |  
  19.             ((e & EPOLLHUP) ? FD_POLL_HUP : 0);  
  20.           
  21.         if ((fdtab[fd].spec.e & FD_EV_MASK_R) == FD_EV_WAIT_R) {  
  22.             if (fdtab[fd].state == FD_STCLOSE || fdtab[fd].state == FD_STERROR)  
  23.                 continue;  
  24.             if (fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP|FD_POLL_ERR))  
  25.                 fdtab[fd].cb[DIR_RD].f(fd);  
  26.         }  
  27.   
  28.         if ((fdtab[fd].spec.e & FD_EV_MASK_W) == FD_EV_WAIT_W) {  
  29.             if (fdtab[fd].state == FD_STCLOSE || fdtab[fd].state == FD_STERROR)  
  30.                 continue;  
  31.             if (fdtab[fd].ev & (FD_POLL_OUT|FD_POLL_ERR))  
  32.                 fdtab[fd].cb[DIR_WR].f(fd);  
  33.         }  
  34.     }  
  35.   
  36.     if (fd_created) {  
  37.         /* we have created some fds, certainly in return of an accept(), 
  38.          * and they're marked as speculative. If we can manage to perform 
  39.          * a read(), we're almost sure to collect all the request at once 
  40.          * and avoid several expensive wakeups. So let's try now. Anyway, 
  41.          * if we fail, the tasks are still woken up, and the FD gets marked 
  42.          * for poll mode. 
  43.          */  
  44.   
  45.         looping = 1;  
  46.         goto re_poll_once;  
  47.     }  
  48. }  

spec list的处理一样,对于出现的事件会在fdtab[fd].ev中保存下来。

epoll的相应事件处理完成之后。因为读事件中包括accept,因此可能创建了新的链接。如果创建了新的fd,那么可以转回去直接用spec对他们进行处理,这第二次轮询只处理新建的fd

epoll_wait调用之前将fd_created置为了0,那么是什么地方对其进行更改呢?是在poller的fd_set函数中,fd_set函数会在event_accept()函数中调用。后者源代码位于src/client.c中。

Poller的初始化

之前看完了poller的处理流程,那么看看poller是如何初始化的。

  1. [src/fd.c]  
  2. int init_pollers()  
  3. {  
  4.     int p;  
  5.     struct poller *bp;  
  6.   
  7.   
  8.     do {  
  9.         bp = NULL;  
  10.         for (p = 0; p < nbpollers; p++)  
  11.             if (!bp || (pollers[p].pref > bp->pref))  
  12.                 bp = &pollers[p];  
  13.   
  14.         if (!bp || bp->pref == 0)  
  15.             break;  
  16.   
  17.         if (bp->init(bp)) {  
  18.             memcpy(&cur_poller, bp, sizeof(*bp));  
  19.             return 1;  
  20.         }  
  21.     } while (!bp || bp->pref == 0);  
  22.     return 0;  
  23. }  

很简单的代码,仅仅是遍历poller全局数组pollers来查找pref值最大的一个poller,并将其设置为cur_poller。

那么pollers的值如何来的呢?通过查看ev_*.c的代码可知,每一个文件均有如下函数,

  1. __attribute__((constructor))  
  2. static void _do_register(void)  
  3. {  
  4.     ...  
  5. }  

这是使用了GCC的特性。GCC编译之后的代码将会在main函数运行之前将带有此特性的函数先运行。因此,pollers数组就是通过每个I/O模型的_do_register函数来初始化的。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多