Epump Save

ePump是一个基于I/O事件通知、非阻塞通信、多路复用、多线程等机制开发的事件驱动模型的 C 语言应用开发框架,利用该框架可以很容易地开发出高性能、大并发连接的服务器程序。

Project README

ePump - an event-driven, multi-threaded c-framework

A C-language framework based on I/O event notification, non-blocking communication and multi-threaded event-driven model helps you to develop servers with high-performance and numerous concurrent connections.

ePump??һ??????I/O?¼?֪ͨ????????ͨ?š???·???á????̵߳Ȼ??ƿ??????¼?????ģ?͵? C ????Ӧ?ÿ?????ܣ????øÿ?ܿ??Ժ????׵ؿ??????????ܡ??󲢷????ӵķ?????????

Ŀ¼


һ. ePump??ʲô??

ePump??һ??????I/O?¼?֪ͨ????????ͨ?š???·???á????̵߳Ȼ??ƿ??????¼?????ģ?͵? C ????Ӧ?ÿ?????ܣ????øÿ?ܿ??Ժ????׵ؿ??????????ܡ??󲢷????ӵķ?????????

ePump???¼??ã?Event Pump????Ӣ?ļ?ƣ?????˼?壬??˼?ǶԸ????????д?¼?????ʱ???¼??Ƚ???ѭ???????ıã???Щ?ײ??¼??????ļ???????FD?Ķ???????Read Readiness????д??????Write Readiness???????ӳɹ???Connected??????ʱ????ʱ??Timeout???ȵȡ?

ePump????????ͼ?ش??ڷ?????ģʽ???ļ???????FD?Ͷ?ʱ??????????״̬?仯??????Ӧ???¼??????ɷ?????Ӧ?Ĺ????̻߳?ePump?̵߳??¼??????У???Щ?߳?ͨ?????ø??¼??????Ļص???????Callback?????????¼???

Ӧ?ó??????ePump????ṩ?Ľӿں?????Ԥ?ȴ??????򿪸???????ͨ??Socket?ļ???????FD??????????ʱ???ȣ??????????ӻ?󶨵?ePump?̵߳ļ?ض????У?????ЩFD?Ͷ?ʱ????״̬????Dz??ò???ϵͳ?ṩ??I/O?¼?֪ͨ??ʩ????epoll??select??poll??kqueue??completion port?ȡ?

??. ePump???ʲô??

???????????????Ҫ???????Կͻ??෢??Ĵ󲢷?TCP????????UDP??????Web????????Online??????????Ϣϵͳ?ȡ?????ʵ?ֵ?ͨ?ŷ???????ϵͳ?У?һ??????????ͨ??????һ???????Ľ??̻??߳??????ܲ?????ͨ??ϸ?ڣ??????ȵ?Apache Web??????????????????OS?ṩ??I/O?첽?¼?֪ͨ????·???û???ʵ?ֵ??????´??????????????????????????SQUIDϵͳ??

??Щϵͳ???õĿ?ܣ?Ҫ???ڵȴ??????I/O?豸?????ݵ???֮ǰ?????Լ???Ҫ?????õ????̶?·????ģ?ͣ????Ƕ?CPU??????Ч?ʶ??ٴ???һ???ľ??ޣ???ePump?????һ?ֳ?ָ?Ч????CPU???????????¼?????ģ?Ϳ?ܡ?

ePump?????һ?????̣߳?δ?????Ӷ???̣??¼?????ģ?Ϳ?ܣ??????ļ??????????첽????֪ͨ??Readiness Notification?????ƣ?????Ϊ?ȴ?"??·??"?????ݶ??????ȴ??????̻߳??????̡?

?ÿ??Ϊÿ???ļ???????????iodev_t????Ϊ??ʱ??????Ӧ?ó??򴴽???ʱ??iotimer_t???????ò???ϵͳ?ṩ??I/O?¼?֪ͨ??ʩ??epoll??select?ȣ?????????򿪵??ļ???????FD????Ϊ??????ģʽ???????ӵ?ϵͳ?ļ?ع????б??У?????״̬?仯?????첽?ص?֪ͨ??

???????????ļ?ع??????¼?֪ͨ?ɷ?????ePump?̳߳???ʵ?֣????¼??Ļص?????????Worker?????̳߳ػ?ePump?̳߳?????ɡ?Ϊ?˳?????÷?????Ӳ???????ܣ??????????̵߳ĸ???һ???CPU Core??????һ?¡?

???????ӵĵײ㴦??ϸ?ڶ?????װ??һЩ?????õ?API?ӿں?????ͨ????ЩAPI???????????߿??Կ??ٿ?????֧?Ŵ󲢷??ĸ????ܷ?????????

??. ePump??ܹ???ԭ??

ePump?????????????2003?꿪????eProbe??ܵĻ????Ϸ?չ????????Event Pump????д??????˼??????һ???¼??????ܹ???

???ڲ?ͬ??I/O?¼?֪ͨ????????ͨ?š???·???û??ƣ?????epoll??select??kqueue??completion port i/o?ȣ??????????ԭ????????

  • ??FD???ӵ??????б???
  • ??FD?Ӽ????б???ɾ??
  • ??????????????ʱ??
  • ?????ȴ??????б???FD set???Ⱥ?R/W?¼?????
  • ??ѯFD set?б?????FD?Ƿ????R/W?¼???ִ?и??¼???Ӧ?Ļص?????
  • ???Timeout??ִ??Timeout?¼???Ӧ?Ļص?????

3.1 ePump???????ݽṹ

???????Ϲ???ԭ???????????ePump??ܵļ??????????ݽṹ??

3.1.1 ?豸??iodev_t??

???ÿ??FD???????ݽṹΪiodev_t???????????ļ???????FD????iodev_t?豸??????豸????????д״̬??FD???͡?Ҫ?????Ķ?д?¼????ص??????ͻص?????????Ԫ???ַ?ȵ?. ???ǰ?TCP????socket??TCP????socket?????????ӵġ????????յģ???UDP????socket??UDP?ͻ?socket??Unix Socket??ICMP Raw Socket??UDP Raw Socket?ȵȣ???ͨ??iodev_t?豸????????

???е?iodev_t?豸????????¼???ePumpϵͳ??iodev_t?豸???????¼????д???????ͨ???¼????????߳??????ûص???????

3.1.2 ??ʱ????iotimer_t??

????iodev_t?豸???ܲ????????¼??Ļ???iotimer_t??ʱ??, ?趨һ??ʱ?䲢??????ʱ????ϵͳ???ӵ?ǰʱ????ָ??ʱ?䵽??ʱ??????Timeout?¼???

iotimer_t??ʱ????һ???Եĺ??????Եģ?iotimer_t??ʱ?????ݽṹ??????ʱ??id???ص??????ͻص?????????ʱ??ʱ??ȡ?

??Unix??OSϵͳ??һ??????ֻ??????һ??ʱ?Ӷ?ʱ??????ϵͳ?ṩ?Ľӿ??????ã????õ???alarm()??setitimer()??????ͨ??ϵͳ?д??????ڸ??ֶ?ʱ??????ͬʱ???ǿ?ƽ̨?Եȣ?ϵͳ?ṩ?Ķ?ʱ???ӿ?һ?㶼??????????????????ePumpϵͳ???????iotimer_t???ݽṹ?????ṩ???뼶???ȡ?ͬʱ?󲢷??????Ķ?ʱ??????ʵ?֡?

ePump?ܹ??аѶ?ʱ??????һ????Ҫ?Ļ?????ʩ?????ļ????????豸һ????ePump?̼߳????͹?????

3.1.3 ?¼???ioevent_t??

ioevent_t?¼???ePump????ʹ???????¼????͡??????¼??Ķ????¼??Ļص??????Ͳ?????

iodev_t?豸???ڸ???Ӳ???豸??R/W״̬?䶯??????ioevent_t?¼??IJ???????iotimer_t??ʱ???????趨?Ķ?ʱʱ?䣬??ָ??ʱ?䳬ʱ???ʹ?????ʱTimeout?¼???

???⣬Ӧ?ó??????ע???û????ӣ?Hook???¼???ע????û????ӣ?Hook???¼???Ҫ??Callback?ص??????ͻص???????????Ҫ???Ƕ????û??¼???????????

?????????²???????Щ?¼??????ᱻ???͵??????̵߳??¼????У??????????߳????????¼??????????߼?????Ӧ?Ļص????????????¼???

3.2 ePump???̼ܹ߳?

ePump?ܹ????ɶ??߳??????ɵģ????չ??????̣???Щ?̷ֳ߳????࣬һ????ePump?̣߳???һ????worker?̡߳?ePump?߳?ְ????Ҫ?Ǹ???????ļ???????R/W??д״̬?Ͷ?ʱ?????У???????д?¼??Ͷ?ʱ???¼???????ioevent_t?¼??ɷ???????worker?̵߳??¼??????С?worker?߳?ְ???Ǽ????¼????У?ִ???¼??????и????¼??????Ļص???????

ÿ??ePump?̲߳???I/O?¼??첽֪ͨ????????ͨ?š???·???õȻ??ƺ?ģ?ͣ?????select/poll/epoll??ϵͳ???ã????????????ļ???????????I/O??д??????I/O Readiness??ʱ??ePump?ͻᴴ???????Щ?ļ?????????R/W??д?¼???????ЩR/W??д?¼???װ??ePump????б?׼??ioevent_t?¼??????????ɷ???????worker?????̵߳?FIFO?¼??????С???ЩEvent Queue FIFO?¼????????߳??¼?????ģ?͵ĺ??ģ?ÿ??ePump?̺߳?ÿ??worker?̶߳???һ????????FIFO?¼????С????⣬ePump?̻߳?Ҫά?ֲ???????ʱ?????У?????ʱ????ʱʱ????????ʱ????ʱioevent_t?¼????ɷ?????Ӧ??worker?????̵߳??¼??????С?

worker?̵߳???Ҫְ?ܾ????????ȴ????̰߳󶨵??¼????У??????¼?????ʱ??ͨ?????ѻ??ƣ????Ѵ??ڹ???״̬??worker?̣߳??????ѵĹ????߳̽?????FIFO?¼??????У?????ء?ѭ????ȡ?߲??????¼????е?ioevent_t?¼???

ioevent_t?¼??Ĵ??????̻??ڻص?????ע????ƣ?Ӧ?ò??ڴ???????ļ???????FD??????????ʱ??ʱ??????FD??Ӧ??iodev_t?豸?Ͷ?ʱ??ʵ????ע?Ტ?󶨻ص?????????????ePump?????iodev_t?豸??iotimer_t??ʱ?????ڴ???ioevent_t?¼?ʱ?????Ὣ??ע??󶨵Ļص??????ͻص??????????õ?ioevent_t?¼??С???worker?????̴߳??¼??????л?ȡ??ioevent_t?¼???ִ???????õĻص????????ɡ?

ePump?̳߳??˼????ļ???????FD??Ӧ??iodev_t?豸??????iotimer_t??ʱ?????С?????ioevent_t?¼????ɷ?ioevent_t?¼????????¼??????⣬Ҳ???԰?һ??FIFO?¼????У????Ե????¼??ص??????ķ?ʽ?????¼????е??¼???

Ϊ?˱?֤????Ч?ʣ?ePump?ܹ????߳?????????????ePump?̺߳?worker?????̣߳????ΪCPU??Core Processor????????????ȷ????ȫ???д?????

??. ePump??ܹ???ģ??

?ȶ??????ʲô?ǿ?ҵ?????ҵ?񡣿?ҵ????ָ???յ??ͻ??˵????????ҵ??????????Լ򵥿??٣?û?г?ʱ???????͵ȴ???ҵ???????̣??෴????ҵ??????ָ?ڴ????ͻ??˵?????ʱ????Ҫ?ϳ?ʱ????????͵ȴ???????????ݿ?????ѯ?????????ҵ?????̵ȡ?

ePump??ܽṹ?dz???????ҵ????????ɷֳ????๤??ģ?ͣ?

4.1 ??ҵ??ģ?? -- û??worker?̣߳?ֻ??ePump?߳?

  • ePump?̼߳ȸ???iodev_t??iotimer_t?ļ?????ioevent_t?¼??Ĵ????ͷַ???ͬʱ?????Գ䵱?????̵߳?ְ?ܣ???????FIFO?¼??????е?ioevent_t?¼??????????????ģ?͵?Ӧ??ϵͳ??Nginx Web????????

  • ???ģ??????ȱ???ǣ?һ??ͨ?????ûص??????????¼??ڼ䣬??????ҵ???????????ʱ??ȴ????????ȣ?Ʃ???д???ݿ?ʱ????ʱ???????ȴ???ѯ????ȣ??ͻᵼ?º?????????iodev_t?豸?е??ļ?????FD??I/O??????Readiness??״̬????iotimer_t??ʱ????ʱ״̬?????ܱ???ʱ??Ч?ش?????һ???¼??Ĵ????ӳ٣??ᵼ?´???????iodev_t?豸??״̬?仯????ʱ???ij?ʱ?ȵò?????ʱ???ٵĴ??????Ӷ????????崦???ϵ??ӳ١???????????û????Ӧ???߱?????

  • ?????????ҵ?񣬲?????????Apache Web?????????ֶ?ռʽ????/?̼ܹ߳?ģ?ͱȽ??ʺϣ?????????˵?????ֶ?ռʽ????/?߳?ģ?ͣ??Զ??CPU???д?????????????Ч?ʷdz????£??????????ϵ͡?

  • ??ģ?????ĺô??ǣ??Զ??CPU???м???ʹ?????????Ч?ʿɴﵽ???£??ʺϴ?????????Ҫ??????Ӧ?͵?ͨ?Ż?ҵ??ϵͳ??

4.2 ????ҵ??ģ?? -- ????ePump?̣߳??????worker?߳?

  • ePump?߳?ֻ????iodev_t??iotimer_t?ļ?????ioevent_t?¼??Ĵ????ͷַ????????????¼???worker?̸߳????????в?????ioevent_t?¼?????????Щ?¼??Ļص????????Ӷ?????Ӧ?ò?ҵ?????̡?

  • worker?߳?ִ???ϲ?Ӧ??ע??Ļص?????ʱ??ִ?й??̵???????????̱??????iodev_t?豸??ʱ???ȵ??¼?????ȷ???????豸??ʱ???¼???ͨ??????worker?????߳̽??м?ʱ??Ч?Ĵ?????

  • ????ģ?͵ĺô??ǿ???һ???̶Ⱥܺõؽ??????ҵ????Ӧ?õ?????ͬʱ?dz???Ч?????ö??CPU?IJ??м??㴦????????

  • ʹ??ePump??ܵĸ???ҵ??ģ??ʱ???߳?????????ΪCPU??Core Processor????????????ePump?߳?????ΪCPU Core??????10-20%??worker?߳?????ΪCPU Core??????80-90%??Ʃ??CPUΪ32?˵ķ???????????ePump?ܹ??????ij???ʱ??ePump?߳???????Ϊ3-6????worker?????߳???????Ϊ26-29????

??. ePump????е??ļ???????FD

??Unix??Linux????ϵͳ?У???һ????I/O??д??ص??????豸???????豸?????????ļ?????????ͨ?ļ???Ŀ¼???ַ??豸?ļ???????̡???꣩?????豸?ļ?????Ӳ?̡??????????????׽???Socket?ȣ?????????ļ????ļ????????Dz???ϵͳ?ں?kernel???????򿪵??ļ??ṹ???????????????һ????????ֵ???ں?Ϊÿ??????ά??һ???ļ???????????Ըñ??????????ļ???????fd??0??ʼ??0Ϊ??׼???룬1Ϊ??׼?????2Ϊ??׼??????????ڽ????д򿪵?ÿ???ļ??????????һ???ļ???????fd??????Ӧ???ý??̵??ļ???????ij?????????У?ͨ??fd????д?ͷ????ļ???

ȱʡ?أ?һ?????̴򿪵??ļ??????????????????Ƶģ?Linuxϵͳ?????????ư??????????棬?û??????ƺ??ں˼????ơ??ں˼?????????????Ӳ????Դ?Ͳ???ϵͳ??????I/O?????????ƶ???һ???????û??????ܼ??ܴ򿪵?????ļ???????????????????shell???

        sysctl -a | grep file

??

        cat /proc/sys/fs/file-max

?鿴?ں˼????ơ?????ϵͳ?ں?ͬʱ?򿪵??ļ??????????ƣ???ÿ???û??ͽ?????Ӧ?????ƴ򿪵??ļ????????????????û????????ƣ????????ȱʡһ????1024????ȱʡ????£??????ܴ򿪵??ļ?????????????1024??

ePumpϵͳ?ڳ?ʼ??ʱ???Ѵ򿪵??ļ?????????????Ϊ??ʼ???????????ͨ??ϵͳ????setrlimit???޸ģ?????߰???????socket???ڵ??ļ??????????????Ӷ?????ϵͳ???I/O??????????????

ePump??ܶ??ļ????????????˷?װ??????iodev_t???ݽṹ??????ÿһ???ļ????????????ļ????????????͡??ص?????????Ԫ???ַ????д״̬???????̵߳???Ϣͳһ??װ??????ePump?̸߳????iodev_t?豸??I/O??д״̬???м?????һ???յ?I/O??д????֪ͨ??Readiness Notification???ʹ???ioevent_t?¼?????ͬ??I/O??д״̬???ͻᴴ????ͬ???¼???ͨ??????Щ?¼?ע?᲻ͬ?Ļص?????????ʵ???¼?????ģ?͵Ĵ????ջ???

????ļ????????ĸ??ֲ?ͬ??I/O??д״̬??ePump?ܹ??ж????˶????ļ??????????ͣ?

    #define FDT_LISTEN            0x01
    #define FDT_CONNECTED         0x02
    #define FDT_ACCEPTED          0x04
    #define FDT_UDPSRV            0x08
    #define FDT_UDPCLI            0x10
    #define FDT_USOCK_LISTEN      0x20
    #define FDT_USOCK_CONNECTED   0x40
    #define FDT_USOCK_ACCEPTED    0x80
    #define FDT_RAWSOCK           0x100
    #define FDT_FILEDEV           0x200
    #define FDT_TIMER             0x10000
    #define FDT_USERCMD           0x20000
    #define FDT_LINGER_CLOSE      0x40000
    #define FDT_STDIN             0x100000
    #define FDT_STDOUT            0x200000

?????ļ?????????????iodev_t?豸??ePump????????????????ʩ????????˵??ePump????һ???????ļ?????????ϵͳ???ļ??????????????¼?????ѪҺһ????????ת????ePump??ܡ?

??. ePump??ܵĻص???Call Back??????

????ҵ???߼???????ģ??һ????÷ֲ?ģ?ͣ???ͬ??ģ??֮??һ??ͨ???????ӿ????໥???ã????ڷֲ??߼????²?ģ??ͨ????Ϊ??????????ʩ??Ʃ????????㡢I/O??д?ȹ??ܣ??ṩ???????ýӿڸ??ϲ?ģ?飬?ϲ?ģ??ͨ???²?ģ??Ľӿں?????ʹ???????㡢??д?ȹ??ܡ???Ϊ?ײ?֧??ģ?飬?²?ģ????ε????ϲ?ģ??ĺ????????أ?????ǻص???CallBack?????ơ?

ePump?????Ϊ?ײ??????ʩ??????ͬ??ҵ??ϵͳ?ṩ????֧?ţ?ҵ??ϵͳ??????ʵ?ַ׷????ӣ?ͨ???ص???Callback?????ƣ???ʵ???ϲ?ҵ??ϵͳ?ĺ???ָ??ע?ᵽePump??ܵ??ļ????????豸??ʱ???У???ePump???????豸?Ͷ?ʱ????I/O??д״̬????ʱ????ʱ״̬?????仯ʱ??ͨ???¼?????ģ?ͣ?ִ???ϲ?ϵͳע?ᵽ????״̬?仯???豸?Ͷ?ʱ???Ļص????????Ӷ?????ePump?ײ???߳?CPU???д??????㴦??????????????ӵ?ҵ?????̵?Ŀ?ġ?

ePump?Ļص???CallBack?????Ʒ?װ??ePump???ϲ?ģ???ṩ?Ľӿں????У???ePump?Ľӿں????У?һ?????????Ҫ????ĺ???ָ?룬???????ָ??ָ??????ϲ?ҵ????????????ePump?Ļص????????ص???????ԭ?Ͷ??????£?

   typedef int IOHandler (void * vmgmt, void * pobj, int event, int fdtype);

??һ?????????ϲ?ģ??ePump?ӿں????IJ??????룬?ڶ?????pobj????????????event?????ĸ?????fdtype????ePump?ص?????ʱ???ݵIJ?????????

  • pobj ??ePump????I/O??д????readyʱ??iodev_t?豸???????iotimer_t??ʱ??????
  • event ???¼?????
  • fdtype ???ļ???????????

ePump?й?????iodev_t?豸?????iotime_t??ʱ????????״̬?????仯ʱ??ePump???????Ӧ???¼?????Щ?¼????????£?

    /* event types include getting connected, connection accepted, readable,
     * writable, timeout. the working threads will be driven by these events */
    #define IOE_CONNECTED        1
    #define IOE_CONNFAIL         2
    #define IOE_ACCEPT           3
    #define IOE_READ             4
    #define IOE_WRITE            5
    #define IOE_INVALID_DEV      6
    #define IOE_TIMEOUT          100
    #define IOE_USER_DEFINED     10000

ePump???ϲ??ṩ?Ļ????ӿں??????£?

void * eptcp_listen  (void * vpcore, char * localip, int port, void * para, int * retval,
                      IOHandler * cb, void * cbpara, int bindtype);
void * eptcp_mlisten (void * vpcore, char * localip, int port, void * para,
                      IOHandler * cb, void * cbpara);
void * eptcp_accept  (void * vpcore, void * vld, void * para, int * retval,
                      IOHandler * cb, void * cbpara, int bindtype);
void * eptcp_connect (void * vpcore, struct in_addr ip, int port, char * localip, int localport,
                      void * para, int * retval, IOHandler * cb, void * cbpara);
 
void * epudp_listen (void * pcore, char * lip, int port, void * para, int * ret, IOHandler * cb, void * cbp);
void * epudp_client (void * pcore, char * lip, int port, void * para, int * ret, IOHandler * cb, void * cbp);
 
void * epusock_connect (void * pcore, char * sock, void * para, int * ret, IOHandler * cb, void * cbp);
void * epusock_listen  (void * pcore, char * sock, void * para, int * ret, IOHandler * cb, void * cbp);
void * epusock_accept  (void * pcore, void * vld, void * para, int * ret, IOHandler * cb, void * cbp);
 
void * epfile_bind_fd    (void * pcore, int fd, void * para, IOHandler * cb, void * cbp);
void * epfile_bind_stdin (void * pcore, void * para, IOHandler * cb, void * cbp);
 
void * iotimer_start (void * pcore, int ms, int cmdid, void * para, IOHandler * cb, void * cbp);
int    iotimer_stop  (void * viot);

ePump????ṩ?Ĺ??ܽӿں?????????TCP??UDP??Unix Socket??ͨ????ʩ?????????ļ????????¼????????Ͷ?ʱ???¼??ļ????????ڳ???TCP??UDP??Unix Socket֮????ļ?????????????ʹ??epfile_bind_fd?ӿ????????????ļ????????豸????????????չ???????ļ???????FD?????Լ??뵽ePump?ܹ??н??й??????¼???????

??. ePump??ܵĵ??ȣ?Scheduling??????

???ȣ?scheduling???ǰ???һ???Ļ??ƺ??㷨???????Դ???з???Ĺ??̣?ePump??ܵ???Դ??Ҫ??iodev_t?豸??iodev_t??ʱ????ioevent_t?¼???ePump?̡߳?worker?????̣߳????Ȼ???Ҳ??Χ????Щ??Դ?ķ???????ơ?

7.1 iodev_t?豸??ePump?߳?

ͨ??????Ӧ?ýӿڴ???iodev_t?豸????Ҫѡ??һ??ePump?߳???ִ?и??豸?ļ????;???֪ͨ??Readiness Notification??????????ǰiodev_t?豸??ѡ???ePump?߳̽????󶨹?ϵ???а󶨵?ePump?߳????????Ͳ???????R/W?¼?????η???ePump?߳???Ҫȡ????iodev_t???豸???ͺͰ????͡?

7.1.1 Listen????˿????iodev_t?豸

??Ҫ????ePump?̶߳??󶨸?iodev_t?豸???????֧??SO_REUSEPORT Socketѡ??IJ???ϵͳ????ҪΪÿһ??ePump?߳???ͬһ????????ͬһ??Listen?˿??ϴ??????iodev_t Listen?豸?????󶨵???ePump?߳??С?????????Ŀ????ȷ?????пͻ???????????????ʱ??????ePump?̶߳??ܾ????ƽ?ָ??ء???Ȼ??????Linux?ں˰汾????3.9.x??ϵͳ?????ھ?ȺЧӦ????δ?????μ???8.3.2?ڡ?

7.1.2 ??Listen??iodev_t?豸

  • ָ??ePump?߳?
    ???ݵ??ò???ָ????ePump?߳????????󶨹?ϵ??

  • ????ePump?̵߳???͸???
    ePump?ĸ?????Ҫ?Ǹ??̰߳󶨵?iodev_t?豸??????iotimer_t??ʱ???????????߳??????λʱ???ڲ?????ioevent_t??????ָ??????????ѡ????͸??ص?ePump?̣߳??????ø??ؾ???ط?̯??????ePump?߳??У???????ϵͳ????Ч?ʡ?

7.2 iotimer_t??ʱ??

Ӧ?ó???????iotimer_t??ʱ??ʱ??ePump???һ?????ePump?̵߳ĵ?ǰ???أ?ѡ??????͵?ePump?߳????󶨣??ɰ󶨵?ePump?߳????????ͼ?أ????????????ʱ?¼???

??ePump?߳?һ???ǽ?iotimer_t??ʱ?????????ӵ???ePump?̵߳Ĺ?????ʱ???б??ĺ?????ṹ?У??????ǰePump?̴߳???????????״̬??ͨ????????ƻ??ѵ?ǰePump?̣߳??????ڶ?ʱ?????ͽṹ???뵱ǰʱ?????ʱ????????????ϵͳ???á?

7.3 ioevent_t?¼?

?????û??¼??⣬????????ioevent_t?¼?????ePump?̲߳???????ȻҲ??ePump?̸߳?????ػ??ƺ??㷨?????ȣ??????ɷ???worker?????̻߳?ePump?̵߳?FIFO?¼??????У??????¼??ص??????ĵ??ô?????ioevent_t?¼??????????ڽ϶̣????????????????ɵ??¼????С????߳?ִ????ص???????ִ????ϣ???ʵ??????ᱻ???ն???????????

ioevent_t?¼?һ?㶼????ij??iodev_t?豸??iotimer_t??ʱ??????ǰioevent_t?¼??ɷ????ȵ???һ??worker?̣߳?ֱ?Ӿ???ePump???ϵͳ???е?Ч?ʡ?

ͬһ??iodev_t?豸?????????Ļ?????ͬ??ioevent_t?¼???ᱻ???Ȼ?????????

7.4 ePump?߳?

ePump?߳???ePump??ܵĺ?????ʩ???????iodev_t?豸?б???iotimer_t??ʱ???б????й?????ͨ??epoll_wait??select??ϵͳ???ã??????ȴ??豸R/W????֪ͨ??ʱ????ʱ????????ioevent_t?¼??????????Щ?¼????е????ɷ???

???ڵ?eProbe????У?ֻ??һ??ȫ?ֵ?FIFO?¼????У????????????¼??????ӵ??¼?????β????Ȼ?????п??е?worker?????̶߳???????FIFO?????¼??????д????????????ģ?ͼ򵥣??ܾ???ط??乤??????һ??ij???¼??????????ж???ʱ??????Ӱ???????¼??Ĵ???????????FIFO???еĹ????????㲥ʽ???????й????̵߳ľ?ȺЧӦ??ͬһ???豸???????????Ķ???¼??ɷ?????ͬ?̴߳????ȵ????أ????ؽ???CPU????Ч?ʣ??????????????Ԥ֪?Ĺ??ϵ????⡣

?Ľ????ePump???????ÿ??worker?????̺߳?ePump?߳??У?????һ????????FIFO?¼????У???Щ?߳?Ҳֻ???Լ???FIFO?????л?ȡ?¼????????¼???ePump?߳̽???????ÿһ???¼????ȷַ?????Щ?̵߳?FIFO?¼??????У??????ü?????ƣ????ѵ?ǰ???ڹ???״̬???̡߳???Ȼ???????worker?????̣߳?????Ȼ??ƽ????Ȱ?ioevent_t?¼???????????̡߳?

ePump?̵߳????ɷ?ioevent_t?¼????㷨???????£?

  • ?¼????ȵĻ????㷨?ǵ͸????????㷨????ѡ??ǰ??????͵?worker?????̣߳??????¼??ɷ??????̵߳??¼??????С?

  • ????ͬһ??iodev_t?豸?????ĺ???????ioevent_t?¼?????????pipeline??ʽ???ȵ?ͬһ??worker?߳??С?

  • ????ͬһ??iodev_t?豸??????????ͬһ???͵?ioevent_t?¼??????????ͬһ??worker?????̵߳?FIFO?¼??????У???δ??ȡ??ִ?У???ô??????????ͬ?豸ͬ?????¼??ͻᱻ??????

  • ???ĸ?worker?߳???????iotimer_t??ʱ?????䳬ʱ?¼???????Ȼ?ɸ?worker?????̴߳?????

  • ???ePump?????û??????worker?????̣߳???ѡ??ǰ??????͵?ePump?̣߳??????¼??ɷ??????̵߳??¼????С?

???ڴ??ģ??ʱ??Ϣͨ??ϵͳ????̨?????????ܻ?ͬʱά??30???????????ģ??TCP???????ӣ?ÿ????????ʱ???????д?¼??????????շ???????????ePump??ܵĶ??ePump?߳̿??Ծ???ֲ?ʽ?طֵ?30???iodev_t?豸????Щ?豸???????¼???Ҳ?ܿ?ؾ?????ȵ?????worker?????߳??У?û?й???????ɵij?ͻ????ͬ?豸???????¼?????pipeline??ʽ??ͬһ???̴߳?????????˶??߳??????豸??Դ?ij?ͻ???????⣬Ҳ?ر???һ???̹߳ر??ͷ???iodev_t?豸??Դ??????һ???̻߳???ʹ?ø???Դ???쳣???????⡣

7.5 worker?????߳?

ePump????У?worker?????߳??Ǵ???ioevent_t?¼?????Ҫ???壬??????????ѭ??????ȡFIFO?¼??????е??¼???ִ?и??¼??еĻص???????????????ͷŸ?ioevent_t?¼????󣬼?????ȡ??һ??ioevent_t?¼????д?????ֱ????????ȫ???¼???ͨ???첽֪ͨ???????????????????ȴ????¼??ĵ?????

worker?????̵߳?ʵʱ??????ePump?????㷨????Ҫ?????????صļ????????????¼??????ӣ?

  • ??ǰ?????߳??¼??????е??ŶӵȺ??ioevent_t?¼???????ռ?????߳??¼??????е?ioevent_t?¼??????İٷֱȣ???Ȩ??Ϊ600??
  • ??ǰ?????߳??ڵ?λʱ????ռ??CPU???д?????ʱ?????????Ȩ??Ϊ300??
  • ??ǰ?????߳??ۼƴ????¼???????ռ?????¼??????ı???????Ȩ??Ϊ100??

???????????ӵİٷ???ֵ??Ȩ?ر?????ʵʱ????ó???ֵ??Ϊworker?????̵߳ĸ??ء?

ePump?̵߳??¼??????ɷ???????Ҫ?????ڹ????̵߳ĸ??أ????͸????????㷨???????????㷨?????ս???Ƕ???????߳??ս?ƽ??سе?ϵͳ?е????д???????

??. ePump????о?ȺЧӦ?Ĵ???????

8.1 ??ȺЧӦ??Thundering Herd Problem??

??ȺЧӦ??ָ????̣????̣߳???ͬʱ?????ȴ?ͬһ???¼???ʱ??????״̬????????ȴ???????¼?????????ô???ͻỽ?ѵȴ??????н??̣??????̣߳???????????ȴֻ????һ?????̣??̣߳???????ʱ??ġ?????Ȩ?????Ը??¼????д??????????????̣??̣߳???ȡ??????Ȩ??ʧ?ܣ?ֻ?????½???????״̬????????????????˷Ѿͽ?????ȺЧӦ??

8.2 ??ȺЧӦ????ʲô??

????ϵͳ?ں˶??û????̣??̣߳?Ƶ????????Ч?ĵ??ȡ????????л??????񣬻?ʹϵͳ???ܴ???ۿۡ????????л???context switch?????߻ᵼ?? CPU Ƶ?????ڼĴ????????ж???֮?䱼?????????ʱ?仨???˽??̣??̣߳??л????????????????????Ľ??̣??̣߳????档ֱ?ӵ????İ??? CPU ?Ĵ???Ҫ????ͼ??أ?????????????????ϵͳ???????Ĵ?????Ҫִ?С???ӵ????????ڶ?? cache ֮??Ĺ??????ݡ?

8.3 ePump????д??ڵľ?Ⱥ????

????libevent???û????ƽ??̻??̣߳?ֻ?????˽ӿڵ??ã??????̺??̵߳?ʹ?ý?????Ӧ?ó???????????ePump??ܲ????˶??̣߳?δ???汾??֧?ֶ???̣????????ʹ????????¼???ʹ?ö???̻???̵߳?ϵͳ????????????ͬ??Դ?????ٶ?????ڽ??̻??̵߳ľ?Ⱥ???⡣

8.3.1 worker?߳??鲻???ھ?Ⱥ????

ePump????У?Ϊÿ??worker?????̵߳???????˽??պʹ????¼???FIFO???У?????worker?????߳???û???¼?????ʱ?????????𲢵Ⱥ?FIFO???е??????????ں˶????ϣ?ֱ???????¼????ӵ?FIFO???к󣬱??????????ں˶????ѡ?

worker?߳???û?й???һ????FIFO?¼????У??????????ӵ??¼??????ỽ?????д??????ߵ?worker?????̣߳?????ֱ????ePump?߳????ʵ?Աһ???????ż??ʹ??ͥ?????У?Ҳ?????????ӵ??¼?????ePump?̵߳????ɷ???ij??worker?????̵߳?FIFO?¼??????У???ֱ?ӻ??Ѹ??̣߳?worker?????߳????е??????߳̾Ͳ?????ܵ?????ָ?

???ַ?ʽ???׹????worker?߳???ľ?ȺЧӦ????????ϵͳ????Ч?ʺ?CPU???????ʡ?

8.3.2 ePump?߳???ľ?Ⱥ????

ePump????е?ePump?̶߳???????????I/O?¼?֪ͨ??ϵͳ?????ϣ???select??poll??epoll_wait?ȣ??Ⱥ??ļ?????????R/W????״̬????ȴ???ʱʱ?䳬ʱ??????????????״̬??ePump?̣߳??????ѵ?????ֻ?????ࣺ

  • һ???ļ????????ɶ???readable?????д??writable??
  • ???????õ?timeoutʱ?䵽????

?????һ??iodev_t?豸???ļ????????????е?ePump?̶߳???????monitor???ˣ???????豸??R/W Readiness??д????ʱ?????е?ePump?߳̾ͻᱻ???ѣ????б????ѵ??߳̽?ȥ??????ļ????????Ĵ???Ȩ????Ȼ????Ҳֻ??һ???߳???ȡ?ô??????豸R/W?¼???Ȩ?ޣ????????????ePump?̵߳ľ?ȺЧӦ??

ePump?????ȷʵ????һ??iodev_t?豸???ͣ????Ǽ???ij??????˿ڵ?Listen?豸??????TCP Listen??UDP Listen????ij??????˿?ʱ????????iodev_t?豸??????Ҫ?????е?ePump?̣߳???????ePump?̶߳Ը??豸????R/W״̬??ش???????????????Ŀ???ǽ???ͬ?ն??û??Ըö˿ڷ?????????ܹ?????ط??䵽??ͬ??ePump?߳??У???????????⴦???????ᵼ??ijһ??ePump?̷߳dz???æ????????ePump?߳?????????е?״̬??

????ePump?̶߳??󶨼????˿ڷ????iodev_t?豸??????????????Ҫ?ֱ?????

  • 1. ????ϵͳ?ں?֧??SO_REUSEPORT Socketѡ?????

    • ֧??SO_REUSEPORT Socketѡ??IJ???ϵͳ?????ں˰汾????3.9.x??Linuxϵͳ?????Դ??????Socket?󶨵?ͬһ??IP??ַ??ͬһ???˿??ϣ???????Socket?ֱ??ò?ͬ?Ľ??̻??߳????????ͻ??˵????????󡣵??ͻ??˵?TCP??·???ֳɹ????ں˾ͻ????ؽ???ǰ???????󽻸?ijһ???߳???accept?????ں˲??????˶???߳??ڸ?Socket?ļ???????R/W״̬Ϊ???Ӿ???ʱ??????????Ȩ?ľ??????⡣
    • ePump????У?????жϲ???ϵͳ֧??SO_REUSEPORT Socketѡ?????tcp_mlisten??udp_mlisten?Ƚӿ????ͬһ???????˿ڣ?Ϊÿһ??ePump?̵߳???????һ??Listen iodev_t?豸?????????󶨹?ϵ??????ÿ??ePump?̶߳????????һ???˿ڣ????տͻ??????󣬲???????Щ????
    • ???????ϴ??????̣????ͻ??˷???????????󣬼????ö˿ڷ????ePump?߳?????ֻ??һ???̲߳Żᱻ???????ePump?̲߳???????յ?R/W Readiness Notification?¼?֪ͨ??
  • 2. ????ϵͳ?ں˲?֧??SO_REUSEPORTѡ?????

    • ePump????У????????ϵͳ?ں˲?֧??SO_REUSEPORT Socketѡ?????ij??????˿?ʱ??ϵͳֻ??Ҫ????һ??????Socket??iodev_t?豸????????Listen iodev_t?豸?󶨵????е?ePump?߳??У?
    • iodev_t?豸??????һ?????????????пͻ?????????ʱ??????ePump?̶߳????յ??ں˷????R/W Readiness Notification????֪ͨ??????ePump?̶߳??ᱻ???ѣ??????̶߳????ᴦ???ÿͻ????󣬲??ù?????ȷ??ֻ??һ??ePump?߳??ܹ???øÿͻ?????Ĵ?????
    • ??????????ǵ??͵ľ?ȺЧӦ??

8.3.3 ??ܻ?????ePump??ܾ?Ⱥ????Ĵ?ʩ

  • ????ʹ??֧??SO_REUSEPORTѡ???OS?汾??֧??SO_REUSEPORTѡ??IJ???ϵͳ???᳹?׽??ePump?߳???ľ?Ⱥ???⡣
  • ????ʹ??ePump??ܵĸ???ҵ??ģ?ͣ???ePump?߳????????٣?worker?????߳??????϶࣬???????˿??ж?д????ʱ??ePump?߳?????Խ?٣???Ⱥ????ĸ???Ч??Ҳ??Խ?ͣ???Ȼ????Ҫ?ڴ????û?????????֮??Ѱ??ƽ?⡣

??. How to build ePump

The framework ePump can run on most Unix-like system and Windows OS, especially work better on Linux.

If you get the copy of ePump package on Unix-like system and find the Makefile in the top directory, please type the following commands before getting the library installed:

$ make && make install

ʮ. How to integrate

The new generated ePump libraries will be installed into the default directory /usr/local/lib, and the header file epump.h is copied to the location /usr/local/include.

After including the header "epump.h", your program can call the APIs provided in it.
#include <epump.h>

Adding the following compiler options in Makefile, you'll be ready to go!
-I/usr/local/include -L/usr/local/lib -lepump

Please refer to the test program for your coding. Further tutorial or documentation will be coming later. Hope you enjoy it!

ʮһ. ePump?????ص???????????Դ??Ŀ

adif ??Ŀ

ePump?????Ŀ?????? adif ??Ŀ?ṩ?Ļ??????ݽṹ???㷨?⡣adif ???ñ?׼ c ???Կ????ij??????ݽṹ???㷨?????⣬??ΪӦ?ó??򿪷??ӿڻ????⣬Ϊ??д?????ܳ????ṩ???????ɼ??????????????Ŀ?Ŀ??????ڣ????????̿???Ч?ʣ???ȷ??????ϵͳ???е?>?ɿ??ԡ??ȶ??ԡ?adif ??Ŀ?ṩ?????ݽṹ???㷨?⣬??Ҫ???????????ݽṹ?????????ݽṹ?????????ݴ????㷨?????õ??ַ??????ֽ??????ַ?????????ʱ??ȴ??????ڴ???ڴ?صķ????ͷŹ??????????ļ?????־???ԡ??ļ????ʡ??ļ????桢JSon??MIME?ȹ?????ͨ?ű?̡??ļ??????ź??????????????¼?֪ͨ???????ڴ?ȵȡ?

eJet Web????????Ŀ

???? adif ??? ePump ??ܿ?????????һ????Դ??Ŀ?? eJet Web ????????eJet Web ????????Ŀ?????? adif ??? ePump ??ܿ??????????????????? Web ????????ϵͳ???????? Zero-Copy ??????֧?? HTTP/1.1??HTTPS ??ȫ?????ܣ??ṩ??????????URI rewrite??Script?ű?????????Cookie??????TLS/SSL???Զ?Redirect??Cache???ش洢????־?ļ??ȹ??ܣ??Ǿ?̬?ļ????ʡ????ء??Լ?PHP???ص?????ƽ̨?????Գ????ļ????ϴ??????ṩ??Ч֧?š????⣬??֧?? Proxy?? ?????????????????? TLS/SSL??FastCGI?? uWSGI?????? Cache ?洢??????CDN?ڵ????ȸ߼????ܡ?eJetϵͳ??????ΪWeb?????????? PHP Ӧ?á?Python Ӧ?ã?ͬʱ???û???? Proxy ???ܣ????????׵?????Ϊ CDN ?ַ?ϵͳ????Ҫ?ַ??ڵ㡣


ʮ??. ???????? ?Ͽ? (laoke)

?д???Linux??ϵͳ?ϵ?Ӧ??ƽ̨??ͨ??ϵͳ???????????????????Ա??????ʦ?????ʼ?[email protected]?????ҵ????ߣ?????ͨ??QQ????571527??΢?ź?beijingkehz?????????ԡ?

ePump?????Ŀ????????????????Դ??Ŀ?ĵڶ?????Ŀ????Ϊ??????ϵͳ??????????ܣ??Ǵ???ϵͳ?з?ʵ?????????????ģ?Ϊ?????󲢷???????ϵͳ?ṩ???֧?š?????ĿԴ????2003?꿪????ɵ?eProbe??Ŀ??????????????˴??????Ż????????ø??Ӽ???Ч??

Open Source Agenda is not affiliated with "Epump" Project. README Source: kehengzhong/epump
Stars
41
Open Issues
1
Last Commit
1 month ago
Repository
License
MIT

Open Source Agenda Badge

Open Source Agenda Rating