AE模块是一个简单的文件事件和定时器事件的处理模块。
==》支持事件ms级时间粒度
==》支持定时器事件处理(单链表)
-- 支持删除定时器事件操作
-- 支持事件处理流程及私有数据
==》支持文件事件处理(数组)
-- 支持文件读写事件处理流程及私有数据
上述几个回调函数分别是文件事件读写处理函数、定时器事件处理函数、定时器事件删除处理函数、事件处理预操作。
在主程序最后三行代码就是ae模块的事件处理流程入口和出口(模块清理)
首先,使用aeSetBeforeSleepProc(server.el,beforeSleep)对事件处理前进行预处理钩子设置;
然后,调用aeMain进入事件处理循环;
aeMain(server.el)
--> beforesleep
--> aeProcessEvents
--> <check if any file or time event, or return immediately>
--> <check if use select> aeSearchNearestTimer
--> <check if use select> <shortest> modify nearest timer
--> <check if use select> <no shortest> <AE_DONT_WAIT> check events
--> <check if use select> <no shortest> <no AE_DONT_WAIT> block definitly
--> <check if use select> aeApiPoll
--> <check if use select> file event operation (rfileProc, wfileProc)
--> processTimeEvents /* time event operation */
--> <system clock future check> fix to process event asap
--> [trigger events] aeGetTime
--> [process time event and related operations] timeProc
最后,当aeStop被触发后,程序跳出循环,进入aeDeleteEventLoop(server.el)模块清理过程。
server.el全局变量是在initServer里面初始化的
server.el = aeCreateEventLoop(server.maxclients+REDIS_EVENTLOOP_FDSET_INCR);
--> struct aeEventLoop variable zmalloc size
--> aeApiCreate -- ae_evport.c ae_epoll.c ae_kqueue.c ae_select.c 性能依次下降,linux系统采用的是epoll,pool大小设置为setsize,从代码上看是10000 + 32 + 96
首先,调用aeCreateEventLoop来创建事件处理需要的初始化变量;
然后,采用aeApiCreate根据编译设置选择evport/epool/kqueue/select机制,并初始化api私有变量;
从宏定义,我们可以看出,Linux系统默认采用的是epool机制。
最后,调用aeCreateTimeEvent和aeCreateFileEvent进行程序初始化。
long long aeCreateTimeEvent(aeEventLoop *eventLoop, long long milliseconds,
aeTimeProc *proc, void *clientData,
aeEventFinalizerProc *finalizerProc);
aeCreateTimeEvent(server.el, 1, serverCron, NULL, NULL)
--> zmalloc struct aeTimeEvent
--> [variable and callback assignment]
--> aeAddMillisecondsToNow
--> [add time event to time event head, which is a signle linked, looped list]
int aeCreateFileEvent(aeEventLoop *eventLoop, int fd, int mask,
aeFileProc *proc, void *clientData);
aeCreateFileEvent(server.el, server.ipfd[j], AE_READABLE, acceptTcpHandler,NULL)
aeCreateFileEvent(server.el,server.sofd,AE_READABLE,acceptUnixHandler,NULL)
--> aeApiAddEvent
--> [variable and callback assignment]
aeEventLoop *aeCreateEventLoop(int setsize);
创建AE模块全局变量
void aeDeleteEventLoop(aeEventLoop *eventLoop);
释放AE模块全局变量
void aeSetBeforeSleepProc(aeEventLoop *eventLoop, aeBeforeSleepProc *beforesleep);
向AE模块注册每次事件处理函数前的一个预处理callback函数
void aeStop(aeEventLoop *eventLoop);
停止AE模块处理
long long aeCreateTimeEvent(aeEventLoop *eventLoop, long long milliseconds, aeTimeProc *proc, void *clientData, aeEventFinalizerProc *finalizerProc);
创建一个定时器事件,并返回定时器ID
int aeDeleteTimeEvent(aeEventLoop *eventLoop, long long id);
根据ID删除定时器事件,并执行事件的最终处理函数
int aeCreateFileEvent(aeEventLoop *eventLoop, int fd, int mask, aeFileProc *proc, void *clientData);
创建一个文件事件
void aeDeleteFileEvent(aeEventLoop *eventLoop, int fd, int mask);
释放文件事件,更新最大文件时间句柄
int aeGetFileEvents(aeEventLoop *eventLoop, int fd);
获取文件事件的mask值
char *aeGetApiName(void);
获取AE模块采用的文件事件等待机制描述
void aeMain(aeEventLoop *eventLoop);
AE模块处理主程序入口
int aeProcessEvents(aeEventLoop *eventLoop, int flags);
AE模块事件处理流程
int aeWait(int fd, int mask, long long milliseconds);
AE模块强等待机制
int aeGetSetSize(aeEventLoop *eventLoop);
获取AE模块处理的事件句柄数量
int aeResizeSetSize(aeEventLoop *eventLoop, int setsize);
AE模块功能
==》支持事件ms级时间粒度
==》支持定时器事件处理(单链表)
-- 支持删除定时器事件操作
-- 支持事件处理流程及私有数据
==》支持文件事件处理(数组)
-- 支持文件读写事件处理流程及私有数据
AE模块数据结构
/* Types and data structures */
typedef void aeFileProc(struct aeEventLoop *eventLoop, int fd, void *clientData, int mask);
typedef int aeTimeProc(struct aeEventLoop *eventLoop, long long id, void *clientData);
typedef void aeEventFinalizerProc(struct aeEventLoop *eventLoop, void *clientData);
typedef void aeBeforeSleepProc(struct aeEventLoop *eventLoop);
上述几个回调函数分别是文件事件读写处理函数、定时器事件处理函数、定时器事件删除处理函数、事件处理预操作。
/* File event structure */
typedef struct aeFileEvent {
int mask; /* one of AE_(READABLE|WRITABLE) */
aeFileProc *rfileProc;
aeFileProc *wfileProc;
void *clientData;
} aeFileEvent;
/* Time event structure */
typedef struct aeTimeEvent {
long long id; /* time event identifier. */
long when_sec; /* seconds */
long when_ms; /* milliseconds */
aeTimeProc *timeProc;
aeEventFinalizerProc *finalizerProc;
void *clientData;
struct aeTimeEvent *next;
} aeTimeEvent;
/* A fired event */
typedef struct aeFiredEvent {
int fd;
int mask;
} aeFiredEvent;
/* State of an event based program */
typedef struct aeEventLoop {
int maxfd; /* highest file descriptor currently registered */
int setsize; /* max number of file descriptors tracked */
long long timeEventNextId;
time_t lastTime; /* Used to detect system clock skew */
aeFileEvent *events; /* Registered events */
aeFiredEvent *fired; /* Fired events */
aeTimeEvent *timeEventHead;
int stop;
void *apidata; /* This is used for polling API specific data */
aeBeforeSleepProc *beforesleep;
} aeEventLoop;
AE模块代码应用分析
在主程序最后三行代码就是ae模块的事件处理流程入口和出口(模块清理)
首先,使用aeSetBeforeSleepProc(server.el,beforeSleep)对事件处理前进行预处理钩子设置;
然后,调用aeMain进入事件处理循环;
aeMain(server.el)
--> beforesleep
--> aeProcessEvents
--> <check if any file or time event, or return immediately>
--> <check if use select> aeSearchNearestTimer
--> <check if use select> <shortest> modify nearest timer
--> <check if use select> <no shortest> <AE_DONT_WAIT> check events
--> <check if use select> <no shortest> <no AE_DONT_WAIT> block definitly
--> <check if use select> aeApiPoll
--> <check if use select> file event operation (rfileProc, wfileProc)
--> processTimeEvents /* time event operation */
--> <system clock future check> fix to process event asap
--> [trigger events] aeGetTime
--> [process time event and related operations] timeProc
最后,当aeStop被触发后,程序跳出循环,进入aeDeleteEventLoop(server.el)模块清理过程。
server.el全局变量是在initServer里面初始化的
#define REDIS_MAX_CLIENTS 10000
#define REDIS_MIN_RESERVED_FDS 32
/* When configuring the Redis eventloop, we setup it so that the total number
* of file descriptors we can handle are server.maxclients + RESERVED_FDS + FDSET_INCR
* that is our safety margin. */
#define REDIS_EVENTLOOP_FDSET_INCR (REDIS_MIN_RESERVED_FDS+96)
server.el = aeCreateEventLoop(server.maxclients+REDIS_EVENTLOOP_FDSET_INCR);
--> struct aeEventLoop variable zmalloc size
--> aeApiCreate -- ae_evport.c ae_epoll.c ae_kqueue.c ae_select.c 性能依次下降,linux系统采用的是epoll,pool大小设置为setsize,从代码上看是10000 + 32 + 96
首先,调用aeCreateEventLoop来创建事件处理需要的初始化变量;
然后,采用aeApiCreate根据编译设置选择evport/epool/kqueue/select机制,并初始化api私有变量;
从宏定义,我们可以看出,Linux系统默认采用的是epool机制。
最后,调用aeCreateTimeEvent和aeCreateFileEvent进行程序初始化。
long long aeCreateTimeEvent(aeEventLoop *eventLoop, long long milliseconds,
aeTimeProc *proc, void *clientData,
aeEventFinalizerProc *finalizerProc);
aeCreateTimeEvent(server.el, 1, serverCron, NULL, NULL)
--> zmalloc struct aeTimeEvent
--> [variable and callback assignment]
--> aeAddMillisecondsToNow
--> [add time event to time event head, which is a signle linked, looped list]
int aeCreateFileEvent(aeEventLoop *eventLoop, int fd, int mask,
aeFileProc *proc, void *clientData);
aeCreateFileEvent(server.el, server.ipfd[j], AE_READABLE, acceptTcpHandler,NULL)
aeCreateFileEvent(server.el,server.sofd,AE_READABLE,acceptUnixHandler,NULL)
--> aeApiAddEvent
--> [variable and callback assignment]
AE模块基本接口
aeEventLoop *aeCreateEventLoop(int setsize);
创建AE模块全局变量
void aeDeleteEventLoop(aeEventLoop *eventLoop);
释放AE模块全局变量
void aeSetBeforeSleepProc(aeEventLoop *eventLoop, aeBeforeSleepProc *beforesleep);
向AE模块注册每次事件处理函数前的一个预处理callback函数
void aeStop(aeEventLoop *eventLoop);
停止AE模块处理
long long aeCreateTimeEvent(aeEventLoop *eventLoop, long long milliseconds, aeTimeProc *proc, void *clientData, aeEventFinalizerProc *finalizerProc);
创建一个定时器事件,并返回定时器ID
int aeDeleteTimeEvent(aeEventLoop *eventLoop, long long id);
根据ID删除定时器事件,并执行事件的最终处理函数
int aeCreateFileEvent(aeEventLoop *eventLoop, int fd, int mask, aeFileProc *proc, void *clientData);
创建一个文件事件
void aeDeleteFileEvent(aeEventLoop *eventLoop, int fd, int mask);
释放文件事件,更新最大文件时间句柄
int aeGetFileEvents(aeEventLoop *eventLoop, int fd);
获取文件事件的mask值
char *aeGetApiName(void);
获取AE模块采用的文件事件等待机制描述
void aeMain(aeEventLoop *eventLoop);
AE模块处理主程序入口
int aeProcessEvents(aeEventLoop *eventLoop, int flags);
AE模块事件处理流程
int aeWait(int fd, int mask, long long milliseconds);
AE模块强等待机制
int aeGetSetSize(aeEventLoop *eventLoop);
获取AE模块处理的事件句柄数量
int aeResizeSetSize(aeEventLoop *eventLoop, int setsize);
重新调整AE模块处理的事件句柄数量