Logging: add timestamps for eventdriven IO
[citadel.git] / citadel / modules / eventclient / serv_eventclient.c
index eb3f97d1dc84efabfd0cee18485a3bee25e7712c..ce5d7f0412c12246e8663e6d2025b992cd6992f7 100644 (file)
@@ -121,6 +121,8 @@ gotstatus(int nnrun)
                                       curl_easy_strerror(sta));
                        IO = (AsyncIO *)chandle;
 
+                       IO->Now = ev_now(event_base);
+
                        ev_io_stop(event_base, &IO->recv_event);
                        ev_io_stop(event_base, &IO->send_event);
 
@@ -248,6 +250,7 @@ gotdata(void *data, size_t size, size_t nmemb, void *cglobal) {
        {
                IO->HttpReq.ReplyData = NewStrBufPlain(NULL, SIZ);
        }
+       IO->Now = ev_now(event_base);
        return CurlFillStrBuf_callback(data,
                                       size,
                                       nmemb,
@@ -306,6 +309,8 @@ gotwatchsock(CURL *easy,
                curl_multi_assign(mhnd, fd, IO);
        }
 
+       IO->Now = ev_now(event_base);
+
        Action = "";
        switch (action)
        {
@@ -458,6 +463,7 @@ static void IOcurl_abort_shutdown_callback(struct ev_loop *loop,
 {
        CURLMcode msta;
        AsyncIO *IO = watcher->data;
+       IO->Now = ev_now(event_base);
        EV_syslog(LOG_DEBUG, "EVENT Curl: %s\n", __FUNCTION__);
 
        curl_slist_free_all(IO->HttpReq.headers);
@@ -559,6 +565,7 @@ ev_async ExitEventLoop;
 
 static void QueueEventAddCallback(EV_P_ ev_async *w, int revents)
 {
+       ev_tstamp Now;
        HashList *q;
        void *v;
        HashPos*It;
@@ -577,13 +584,17 @@ static void QueueEventAddCallback(EV_P_ ev_async *w, int revents)
                q = InboundEventQueues[1];
        }
        pthread_mutex_unlock(&EventQueueMutex);
-
+       Now = ev_now (event_base);
        It = GetNewHashPos(q, 0);
        while (GetNextHashPos(q, It, &len, &Key, &v))
        {
                IOAddHandler *h = v;
-               if (h->IO->ID == 0)
+               if (h->IO->ID == 0) {
                        h->IO->ID = EvIDSource++;
+               }
+               if (h->IO->StartIO == 0.0)
+                       h->IO->StartIO = Now;
+               h->IO->Now = Now;
                h->EvAttch(h->IO);
        }
        DeleteHashPos(&It);
@@ -622,7 +633,9 @@ void InitEventQueue(void)
        InboundEventQueues[1] = NewHash(1, Flathash);
        InboundEventQueue = InboundEventQueues[0];
 }
+extern void CtdlDestroyEVCleanupHooks(void);
 
+extern int EVQShutDown;
 /*
  * this thread operates the select() etc. via libev.
  */
@@ -659,6 +672,9 @@ void *client_event_thread(void *arg)
        close(event_add_pipe[0]);
        close(event_add_pipe[1]);
 
+       CtdlDestroyEVCleanupHooks();
+
+       EVQShutDown = 1;        
        return(NULL);
 }
 
@@ -682,6 +698,7 @@ extern void ShutDownDBCLient(AsyncIO *IO);
 
 static void DBQueueEventAddCallback(EV_P_ ev_async *w, int revents)
 {
+       ev_tstamp Now;
        HashList *q;
        void *v;
        HashPos *It;
@@ -701,6 +718,7 @@ static void DBQueueEventAddCallback(EV_P_ ev_async *w, int revents)
        }
        pthread_mutex_unlock(&DBEventQueueMutex);
 
+       Now = ev_now (event_db);
        It = GetNewHashPos(q, 0);
        while (GetNextHashPos(q, It, &len, &Key, &v))
        {
@@ -708,6 +726,9 @@ static void DBQueueEventAddCallback(EV_P_ ev_async *w, int revents)
                eNextState rc;
                if (h->IO->ID == 0)
                        h->IO->ID = EvIDSource++;
+               if (h->IO->StartDB == 0.0)
+                       h->IO->StartDB = Now;
+               h->IO->Now = Now;
                rc = h->EvAttch(h->IO);
                switch (rc)
                {
@@ -805,7 +826,6 @@ CTDL_MODULE_INIT(event_client)
 {
        if (!threading)
        {
-               CtdlRegisterCleanupHook(ShutDownEventQueues);
                InitEventQueue();
                DBInitEventQueue();
                CtdlThreadCreate(client_event_thread);