Chinaunix首页 | 论坛 | 博客
  • 博客访问: 751688
  • 博文数量: 116
  • 博客积分: 923
  • 博客等级: 准尉
  • 技术积分: 1635
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-06 21:43
个人简介

一直帮老板搬运代码!!!

文章分类
文章存档

2013年(47)

2012年(69)

分类: LINUX

2013-01-28 15:49:12

设置epoll wait 这里cst 应该为ngx

6 static cst_int_t
567 cst_epoll_process_events(cst_cycle_t *cycle, cst_msec_t timer, cst_uint_t flags)
568 {
569         int                events;
570         uint32_t           revents;
571         cst_int_t          instance, i;
572         cst_uint_t         level;
573         cst_err_t          err;
574         cst_event_t       *rev, *wev, **queue;
575         cst_connection_t  *c;
576
577         /* CST_TIMER_INFINITE == INFTIM */
578
579         cst_log_debug1(CST_LOG_INFO, cycle->log, 0,                                                                      
580                        "epoll timer: %M", timer);
   //这里是我添加的
581         cst_log_error(CST_LOG_ALERT, cst_cycle->log, 0, "cst_epollprocess_ %M === %d",timer,flags );
582         events = epoll_wait(ep, event_list, (int) nevents, timer);


    for (i = 0; i < events; i++) {
621                 c = event_list[i].data.ptr;
622
623                 instance = (uintptr_t) c & 1;
624                 c = (cst_connection_t *) ((uintptr_t) c & (uintptr_t) ~1);
625
626                 rev = c->read;
627
 //这里是我添加的
628                 cst_log_error(CST_LOG_INFO, c->log, 0, "epoll_wait *%d linsent %d fd to %d pid ",c->fd,0, getpid())

输出结果:

  42 2013/01/28 15:12:33 [alert] 3158#0: cst_epollprocess_ -1 === 3
  43 2013/01/28 15:12:34 [info] 3158#0: epoll_wait *13 linsent 0 fd to 3158 pid
  44 2013/01/28 15:12:34 [alert] 3158#0: cst_epollprocess_ -1 === 3
  45 2013/01/28 15:12:33 [alert] 3162#0: cst_epollprocess_ 500 === 1
  46 2013/01/28 15:12:33 [alert] 3157#0: cst_epollprocess_ 500 === 1
  47 2013/01/28 15:12:35 [info] 3157#0: epoll_wait *11 linsent 0 fd to 3157 pid
  48 2013/01/28 15:12:35 [alert] 3157#0: cst_epollprocess_ 500 === 1
  49 2013/01/28 15:12:33 [alert] 3159#0: cst_epollprocess_ 500 === 1
  50 2013/01/28 15:12:35 [info] 3159#0: epoll_wait *15 linsent 0 fd to 3159 pid
  51 2013/01/28 15:12:35 [alert] 3159#0: cst_epollprocess_ 500 === 1
  52 2013/01/28 15:12:33 [alert] 3161#0: cst_epollprocess_ 500 === 1
  53 2013/01/28 15:12:35 [info] 3161#0: epoll_wait *19 linsent 0 fd to 3161 pid
  54 2013/01/28 15:12:35 [alert] 3161#0: cst_epollprocess_ 500 === 1
  55 2013/01/28 15:12:33 [alert] 3156#0: cst_epollprocess_ 500 === 1
  56 2013/01/28 15:12:35 [info] 3156#0: epoll_wait *9 linsent 0 fd to 3156 pid
  57 2013/01/28 15:12:35 [alert] 3156#0: cst_epollprocess_ 500 === 1
  58 2013/01/28 15:12:33 [alert] 3155#0: cst_epollprocess_ 500 === 1
  59 2013/01/28 15:12:35 [info] 3155#0: epoll_wait *7 linsent 0 fd to 3155 pid
  60 2013/01/28 15:12:35 [alert] 3155#0: cst_epollprocess_ 500 === 1
  61 2013/01/28 15:12:33 [alert] 3160#0: cst_epollprocess_ 500 === 1
  62 2013/01/28 15:12:35 [info] 3160#0: epoll_wait *17 linsent 0 fd to 3160 pid
  63 2013/01/28 15:12:35 [alert] 3160#0: cst_epollprocess_ 500 === 1
  64 2013/01/28 15:12:35 [alert] 3162#0: cst_epollprocess_ 500 === 1
  65 2013/01/28 15:12:35 [alert] 3157#0: cst_epollprocess_ 500 === 1
  66 2013/01/28 15:12:35 [alert] 3159#0: cst_epollprocess_ 500 === 1
  67 2013/01/28 15:12:35 [alert] 3161#0: cst_epollprocess_ 500 === 1
  68 2013/01/28 15:12:35 [alert] 3155#0: cst_epollprocess_ 500 === 1
  69 2013/01/28 15:12:35 [alert] 3156#0: cst_epollprocess_ 500 === 1
  70 2013/01/28 15:12:35 [alert] 3160#0: cst_epollprocess_ 500 === 1
  71 2013/01/28 15:12:36 [alert] 3162#0: cst_epollprocess_ 500 === 1

这里的一个问题,希望有人能讲解:为什么启动的时候,会出现一次epoll id的存在:epoll_wait *17 linsent 0 fd to 3160 pid?我忘记是什么原因了,可能所父子通信管道的ID吧。

然后通过:3158#0: cst_epollprocess_ -1 === 3可以看出只有3158 在是永久等待的,就是说,只有一个获得accept的锁。然后,其他epoll进程,以500毫秒的时间,来询问是否可以接收锁。如果可以,那么获取接收,然后才可以accept客户端。


void
cst_process_events_and_timers(cst_cycle_t *cycle)
{
    cst_uint_t  flags;
    cst_msec_t  timer, delta;

    if (cst_timer_resolution) {
        timer = CST_TIMER_INFINITE;
        flags = 0;

    } else {
        timer = cst_event_find_timer();
        flags = CST_UPDATE_TIME;

#if (CST_THREADS)

        if (timer == CST_TIMER_INFINITE || timer > 500) {
            timer = 500;
        }

#endif
    }

    if (cst_use_accept_mutex) {
        if (cst_accept_disabled > 0) {
            cst_accept_disabled--;

        } else {
            if (cst_trylock_accept_mutex(cycle) == CST_ERROR) {
                return;
            }

            if (cst_accept_mutex_held) {
                flags |= CST_POST_EVENTS;

            } else {
                if (timer == CST_TIMER_INFINITE
                    || timer > cst_accept_mutex_delay)
                {
                    timer = cst_accept_mutex_delay;
                }
            }
        }
    }


这里,在不断尝试获取读写锁,有超过1/8的链接,就放弃锁。更重要的是,如果可以accept后,那么将

static cst_int_t
cst_enable_accept_events(cst_cycle_t *cycle)
{
    cst_uint_t         i;
    cst_listening_t   *ls;
    cst_connection_t  *c;

    ls = cycle->listening.elts;
    for (i = 0; i < cycle->listening.nelts; i++) {

        c = ls[i].connection;

        if (cst_event_flags & CST_USE_RTSIG_EVENT) {

            if (cst_add_conn(c) == CST_ERROR) {
                return CST_ERROR;
            }

        } else {
            if (cst_add_event(c->read, CST_READ_EVENT, 0) == CST_ERROR) {
                return CST_ERROR;
            }
        }
    }

    return CST_OK;
}

也就是把事件加入到对应的进程的epoll里面。

整个接受客户链接的情况就这些:

1、可以接收,添加如事件里面。然后永久等待。

2、其他进程以每500毫秒事件询问,是否可以接收,如果可以,那么添加入事件里面。事件就可以返回接受accept了.

3、如果大于1/8那么递减,直到可以接收位置,才去竞争接收锁。

阅读(4648) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~