Redisソース分析の7:イベント駆動ライブラリ分析——Ae


aeEventLoopは記録プログラムのイベント状態を記録する構造である.
/* State of an event based program */
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;

この構造はaeCreateEventLoop()関数で初期化される.
EventLoopのイベントタイプには、時間イベントが含まれます.
/* 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;

ファイルイベント:
/* File event structure */
typedef struct aeFileEvent {
    int mask; /* one of AE_(READABLE|WRITABLE) */
    aeFileProc *rfileProc;
    aeFileProc *wfileProc;
    void *clientData;
} aeFileEvent;

初期化は、それぞれaeCreateTimeEvent関数およびaeCreateTimeEvent関数で得られる.
イベントループEventLoop構造がinitServer()で作成され、初期化が完了すると、メイン関数はaeMain()関数を呼び出してイベントの処理を開始します.
void aeMain(aeEventLoop *eventLoop) {
    eventLoop->stop = 0;
    while (!eventLoop->stop) {
        if (eventLoop->beforesleep != NULL)
            eventLoop->beforesleep(eventLoop);
        aeProcessEvents(eventLoop, AE_ALL_EVENTS);
    }
}

eventLoop構造で処理を停止する識別stopが1でない限り,イベントループはaeProcessEventsを絶えず呼び出してイベントを処理することを見出した.
int aeProcessEvents(aeEventLoop *eventLoop, int flags);

この関数は、処理される各時間イベントとファイルイベントを処理し、転送されたflagは処理の方法を決定します.
 * If flags is 0, the function does nothing and returns.
 * if flags has AE_ALL_EVENTS set, all the kind of events are processed.
 * if flags has AE_FILE_EVENTS set, file events are processed.
 * if flags has AE_TIME_EVENTS set, time events are processed.
 * if flags has AE_DONT_WAIT set the function returns ASAP until all 
 * the events that's possible to process without to wait are processed.

関数は、処理が完了したイベントの数を返します.
関数は、まずaesearchNearestTimer関数を呼び出して、最近fireするタイマを選択し、tvpを更新します.このステップは展開されません.
関数では、select、kqueue、epollの3つのメカニズムをカプセル化したaeApiPollを呼び出してイベントを監視します.
numevents = aeApiPoll(eventLoop, tvp);

selectモデルでは、次のように実装されます.
retval = select(eventLoop->maxfd+1,
                &state->_rfds,&state->_wfds,NULL,tvp);

kqueueモデルでは、次のように実装されます.
if (tvp != NULL) {
        struct timespec timeout;
        timeout.tv_sec = tvp->tv_sec;
        timeout.tv_nsec = tvp->tv_usec * 1000;
        retval = kevent(state->kqfd, NULL, 0, state->events, AE_SETSIZE, &timeout);
    } else {
        retval = kevent(state->kqfd, NULL, 0, state->events, AE_SETSIZE, NULL);
    }    

epollモデルでは、次のように実装されます.
retval = epoll_wait(state->epfd,state->events,AE_SETSIZE,
            tvp ? (tvp->tv_sec*1000 + tvp->tv_usec/1000) : -1);

この3つの処理メカニズムの分析比較については、次のように参照してください.http://www.kegel.com/c10k.html
イベントのモニタリングスケジューリングが完了したら、イベントループのfiredレコードにイベントを追加し、numeventsに戻ります.
if (retval > 0) {
        int j;

        numevents = retval;
        for (j = 0; j < numevents; j++) {
            int mask = 0;
            struct epoll_event *e = state->events+j;

            if (e->events & EPOLLIN) mask |= AE_READABLE;
            if (e->events & EPOLLOUT) mask |= AE_WRITABLE;
            eventLoop->fired[j].fd = e->data.fd;
            eventLoop->fired[j].mask = mask;
        }
    }
    return numevents;

aeFiredEventは次のように定義されています.
/* A fired event */
typedef struct aeFiredEvent {
    int fd;
    int mask;
} aeFiredEvent;

aeProcessEvents関数に戻り、タグ付きfiredEventsを処理します.
for (j = 0; j < numevents; j++) {
            aeFileEvent *fe = &eventLoop->events[eventLoop->fired[j].fd];
            int mask = eventLoop->fired[j].mask;
            int fd = eventLoop->fired[j].fd;
            int rfired = 0;

/* note the fe->mask & mask & ... code: maybe an already processed
             * event removed an element that fired and we still didn't
             * processed, so we check if the event is still valid. */
            if (fe->mask & mask & AE_READABLE) {
                rfired = 1;
                fe->rfileProc(eventLoop,fd,fe->clientData,mask);
            }
            if (fe->mask & mask & AE_WRITABLE) {
                if (!rfired || fe->wfileProc != fe->rfileProc)
                    fe->wfileProc(eventLoop,fd,fe->clientData,mask);
            }
            processed++;
        }

aeFileEventでは、rfileProcとwfileProcの2つのファイル処理のコールバック関数が定義されており、それぞれ読み書きに対応しています.
ファイル・イベントの処理が完了してから、タイム・イベントを処理します.
 /* Check time events */
    if (flags & AE_TIME_EVENTS)
        processed += processTimeEvents(eventLoop);
は、以下のように実装される.
/* Process time events */
static int processTimeEvents(aeEventLoop *eventLoop) {
    int processed = 0;
    aeTimeEvent *te;
    long long maxId;

    te = eventLoop->timeEventHead;
    maxId = eventLoop->timeEventNextId-1;
    while(te) {
        long now_sec, now_ms;
        long long id;

        if (te->id > maxId) {
            te = te->next;
            continue;
        }
        aeGetTime(&now_sec, &now_ms);
        if (now_sec > te->when_sec ||
            (now_sec == te->when_sec && now_ms >= te->when_ms))
        {
            int retval;

            id = te->id;
            retval = te->timeProc(eventLoop, id, te->clientData);
            processed++;
            /* After an event is processed our time event list may
             * no longer be the same, so we restart from head.
             * Still we make sure to don't process events registered
             * by event handlers itself in order to don't loop forever.
             * To do so we saved the max ID we want to handle.
             *
             * FUTURE OPTIMIZATIONS:
             * Note that this is NOT great algorithmically. Redis uses
             * a single time event so it's not a problem but the right
             * way to do this is to add the new elements on head, and
             * to flag deleted elements in a special way for later
             * deletion (putting references to the nodes to delete into
             * another linked list). */
            if (retval != AE_NOMORE) {
                aeAddMillisecondsToNow(retval,&te->when_sec,&te->when_ms);
            } else {
                aeDeleteTimeEvent(eventLoop, id);
            }
            te = eventLoop->timeEventHead;
        } else {
            te = te->next;
        }
    }
    return processed;
}
は、aeTimeProc*タイプのコールバック関数を呼び出すことによってイベントを処理することも見られます.タイムイベントチェーンテーブルを巡回する過程で、各イベントに対して繰り返します.イベントが処理された場合、retvalの値はAE_とマークされます.NOMOREは、aeDeleteTimeEvent削除タイムイベントを呼び出します.そうでなければ、adAddMillisecondsToNow更新タイマを呼び出します.
最後に処理されたイベントの数を返します.
return processed; /* return the number of processed file/time events */