前言:
redis是一个和memcached相似的kv存储。
作者自己在里边实现了一个小的事件库。
为什么作者要自造轮子呢?作者自己解释如下:
https://groups.google.com/group/redis-db/browse_thread/thread/b52814e9ef15b8d0/
redis的事件库最大的特点就是实现很简洁。
版本:
2.4.16
主要的数据结构:
在Ae.h中就可以看到所有事件处理相关的数据结构和接口。
三种event:
typedef struct aeFileEvent {以上三个数据结构只被eventloop用于管理,不提供给外界。
int mask; /* one of AE_(READABLE|WRITABLE) */
aeFileProc *rfileProc;
aeFileProc *wfileProc;
void *clientData;
} aeFileEvent;
aeFileEvent的读写回调函数分开,对用户透明,在aeCreateFileEvent内设置。
读写共享同一份clientData。
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;
id初始化为eventLoop->timeEventNextId++
when_sec和when_ms保存截止时间(当前时间+用户指定的时间)。
typedef struct aeFiredEvent {
int fd;
int mask;
} aeFiredEvent;
表示已经发生的fileevent。
详细操作参见aeApiPoll。
重要的eventloop:
typedef struct aeEventLoop {主要的接口:
int maxfd;
long long timeEventNextId;
aeFileEvent events[AE_SETSIZE]; /* Registered events */
aeFiredEvent fired[AE_SETSIZE]; /* Fired events,事件发生后在这记录 */
aeTimeEvent *timeEventHead;
int stop;
void *apidata; /* This is used for polling API specific data */
aeBeforeSleepProc *beforesleep;
} aeEventLoop;
注意,eventloop里的events和fired数组,不是指针,而是直接申请了空间。
对于时间event的管理是使用简单的单链表,头指针timeEventHead。
eventloop操作:
aeEventLoop *aeCreateEventLoop(void);event操作:
分配内存,初始化field。
调用后端的aeApiCreate。
void aeDeleteEventLoop(aeEventLoop *eventLoop) {
aeApiFree(eventLoop);
zfree(eventLoop);
}
void aeStop(aeEventLoop *eventLoop) {
eventLoop->stop = 1;
}
删除、停止eventLoop就这么简单。
event的create和delete操作是以(fd, mask)为标识的,使用者不直接得到event结构。事件循环:
int aeCreateFileEvent(aeEventLoop *eventLoop, int fd, int mask, aeFileProc *proc, void *clientData);
直接将信息保存到eventLoop的events[fd]中。
将(fd, mask)添加到后端。
更新eventLoop->maxfd。
void aeDeleteFileEvent(aeEventLoop *eventLoop, int fd, int mask);
从events[fd]的mask中去掉参数mask。
如果events[fd]的mask变为0了,更新eventLoop->maxfd。
从后端删除(fd, mask)。
注意:当你添加了(fd, 读|写),后来又删除了(fd, 读)时,(fd, 读)依然有效。
long long aeCreateTimeEvent(aeEventLoop *eventLoop, long long milliseconds, aeTimeProc *proc, void *clientData, aeEventFinalizerProc *finalizerProc);
分配内存,初始化field。
添加到eventLoop->timeEventHead的单链表中。
返回id。
int aeDeleteTimeEvent(aeEventLoop *eventLoop, long long id);
从eventLoop->timeEventHead的单链表中遍历找到id,free掉。
Ae.h里有三个关于事件循环的接口:aeWait,aeMain, aeProcessEvents。
aeWait是对poll的简单包装,用于实现syncRead,syncWrite(两个带超时的safe read/write实现)。
个人感觉aeWait和其他部分关系不大,不应该放在Ae这部分。
不细说,有兴趣的可以自己看看代码。
void aeMain(aeEventLoop *eventLoop) {
eventLoop->stop = 0;
while (!eventLoop->stop) {
if (eventLoop->beforesleep != NULL)
eventLoop->beforesleep(eventLoop);
aeProcessEvents(eventLoop, AE_ALL_EVENTS);
}
}
aeMain由main函数最后调用进入事件循环。看代码即可,也不多说了。
int aeProcessEvents(aeEventLoop *eventLoop, int flags);
aeMain中的一轮,也可以被单独调用,处理发生的fileevent 或timeevent或两者。如果没有设置DONT_WAIT,则会阻塞等到有fileevent fired或者有timeevent到期。
大体流程:
如果有注册的event 或者 (flags指示要处理timeevent 而且 没有设置DONT_WAIT)
case:要处理timeevent且没有设置DONT_WAIT,则tvp置为等待最近要发生的 timeevent需要的时间。
case:不需要处理timeevent且没有DONT_WAIT,tvp = NULL。意味着接下来我们可以尽情阻塞。
case:设置了DONT_WAIT,tvp设为{0, 0},意味着马上返回不阻塞。
numevents = aeApiPoll(eventLoop, tvp);
一个对firedevent的for循环,处理所有发生了的event。
如果要处理timeevent,processTimeEvents。
返回处理的event数量。
以下两个是aeProcessEvents调用的函数:后端的封装
static aeTimeEvent *aeSearchNearestTimer(aeEventLoop *eventLoop);
找到返回截止时间最近的aeTimeEvent。
根据不过早优化的原则和timeevent插入时的无序,作者还是使用遍历:遍历单链表,找到最近要发生的timeevent。 我很喜欢这种风格!
虽然现在不需要,但给出了两个需要时可以考虑的优化:
1) Insert the event in order, so that the nearest is just the head. Much better but still insertion or deletion of timers is O(N).
2) Use a skiplist to have this operation as O(1) and insertion as O(log(N)).
(为什么不考虑优先级队列呢?跟skiplist时间复杂度差不多,感觉空间上更省。)
static int processTimeEvents(aeEventLoop *eventLoop);
从头遍历timeevent队列,执行已到期timeevent的timeProc函数。如果返回AE_NOMORE,就将该timveevent删除。否则返回增加的时间,调整timeevent的截止时间。
每当处理过timeevent后,都将从头再来。
使用三个后端,分别封装在三个c文件中:
Ae_epoll.c优先级从上到下,编译时确定。直接include进Ae.c:
Ae_kqueue.c
Ae_select.c
#ifdef HAVE_EPOLL
#include "ae_epoll.c"
#else
#ifdef HAVE_KQUEUE
#include "ae_kqueue.c"
#else
#include "ae_select.c"
#endif
#endif
提供六个接口:
aeApiCreate(aeEventLoop *eventLoop);
aeCreateEventLoop时,进行backend特定的初始化。
aeApiFree(aeEventLoop *eventLoop) ;
aeDeleteEventLoop时,进行backend特定的清理。
aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask);
aeCreateFileEvent时,将fd, mask注册到后端,需要监听其状态变化。
aeApiDelEvent(aeEventLoop *eventLoop, int fd, int delmask);
aeDeleteFileEvent时,将fd, mask从后端注销,不再监听其状态变化。
aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp);
开始监听,将发生改变的event的(fd, mask)复制到对应eventLoop->fired中,从0开始顺着来。
最后,返回发生的事件数。不会返回-1,只会是0或>0。
static char *aeApiName(void) {
return "epoll";
}
这个就不用说什么了。。。
总结:
redis的事件驱动设计的相当简洁。一共就4个c文件,1个h文件。代码也非常少,从看代码到写完这篇总结的初稿也就5个小时左右。非常推荐作为入门来看。
event类型只提供fileevent和timeevent。相关的接口也非常少。
fileevent不直接暴露给用户。
用户的create, delete操作通过(fd, mask)标识。内部使用firedevent来记录已经发生的事件。
所需的数据结构直接以数组的形式分配在eventloop中。
timeevent设计更是简单,同样不直接暴露给用户。
没有与fileevent结合,只是单纯的表示一个定时事件。如果需要,在timeevent的回调函数内部可以进行fileevent的create操作。
时间戳简单使用了gettimeofday+过期时间获得。maxid只是用来避免处理timeevents时的循环。
timeevent使用单链表管理,插入操作均在链表头。当需要某些查找操作时,只有遍历。
eventloop一共也只有8个field。事件循环也不过百行。。。
但是毕竟不是通用的事件驱动库,与libevent, libev相比它很多地方做的不好,比如跨平台,多线程支持,事件类型过少,性能还有提升空间等。不过已经完全满足了redis的需要。
如果有时间,我也想仿照这个思路自己写一个事件驱动库,替换掉evhttpd中的libevent。
本来想在最后画张图来表示event和eventloop的关系,以及event的状态转移。可当在纸上画完草稿后,感觉还是不画的好。因为我觉着看图太复杂了,不如直接看代码。
同样,如果想更好了解redis/ae的话,忘掉这篇文章,留出两三个小时去看代码吧!