Added --with-threadlog. Use this if you want the thread table written to
authorDave West <davew@uncensored.citadel.org>
Tue, 27 Nov 2007 20:00:11 +0000 (20:00 +0000)
committerDave West <davew@uncensored.citadel.org>
Tue, 27 Nov 2007 20:00:11 +0000 (20:00 +0000)
the log.
Fixed CtdlThreadName(char *name) now renames thread to name if name is
set and always returns old name caller frees old name.
Added a couple of macros to change the thread names in some places for
debugging.

citadel/configure.ac
citadel/housekeeping.c
citadel/include/ctdl_module.h
citadel/journaling.c
citadel/serv_extensions.c
citadel/sysdep.c

index 74e707208efad53ded8cc3b96064d192e6dcd1de..566eb7df4d706e82f1903c411646815612b46361 100644 (file)
@@ -333,6 +333,15 @@ AC_ARG_WITH(gprof,
                        ]
 )
 
+dnl disable thread table reporting
+AC_ARG_WITH(threadlog,
+               [ --with-threadlog      enable logging of thread table],
+                       [ if test "x$withval" != "xno" ; then
+                               AC_DEFINE(WITH_THREADLOG, [], [Define if you want logging of the thread tables.])
+                         fi
+                       ]
+)
+
 
 if test "$ac_cv_func_gethostbyname" = no; then
        AC_CHECK_LIB(nsl, gethostbyname)
index 1dd18d61e82532a12556f58baf8cba923eaa128a..8d65e0ec7b10792e31b86673c7154d11a29518e5 100644 (file)
@@ -71,14 +71,14 @@ void terminate_idle_sessions(void) {
        }
        end_critical_section(S_SESSION_TABLE);
        if (killed > 0)
-               lprintf(CTDL_INFO, "Terminated %d idle sessions\n", killed);
+               CtdlLogPrintf(CTDL_INFO, "Terminated %d idle sessions\n", killed);
 }
 
 
 
 void check_sched_shutdown(void) {
        if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
-               lprintf(CTDL_NOTICE, "Scheduled shutdown initiating.\n");
+               CtdlLogPrintf(CTDL_NOTICE, "Scheduled shutdown initiating.\n");
                CtdlThreadStopAll();
        }
 }
@@ -104,7 +104,7 @@ void check_ref_counts(void) {
 
        int new_refcounts[MAXFLOORS];
 
-       lprintf(CTDL_DEBUG, "Checking floor reference counts\n");
+       CtdlLogPrintf(CTDL_DEBUG, "Checking floor reference counts\n");
        for (a=0; a<MAXFLOORS; ++a) {
                new_refcounts[a] = 0;
        }
@@ -123,7 +123,7 @@ void check_ref_counts(void) {
                        flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
                }
                lputfloor(&flbuf, a);
-               lprintf(CTDL_DEBUG, "Floor %d: %d rooms\n", a, new_refcounts[a]);
+               CtdlLogPrintf(CTDL_DEBUG, "Floor %d: %d rooms\n", a, new_refcounts[a]);
        }
 }      
 
@@ -140,6 +140,8 @@ void do_housekeeping(void) {
        int do_perminute_housekeeping_now = 0;
        time_t now;
 
+       CtdlThreadPushName("do_housekeeping");
+       
        /*
         * We do it this way instead of wrapping the whole loop in an
         * S_HOUSEKEEPING critical section because it eliminates the need to
@@ -158,6 +160,7 @@ void do_housekeeping(void) {
        end_critical_section(S_HOUSEKEEPING);
 
        if (do_housekeeping_now == 0) {
+               CtdlThreadPopName();
                return;
        }
 
@@ -181,4 +184,5 @@ void do_housekeeping(void) {
         * All done.
         */
        housekeeping_in_progress = 0;
+       CtdlThreadPopName();
 }
index 587adb4a81f007816e9f4e95f36501ef2da720fc..e979830e8506da47b2903ff33170a1070786e702 100644 (file)
@@ -118,5 +118,16 @@ struct CtdlThreadNode *CtdlThreadSelf(void);
 int CtdlThreadGetCount(void);
 void CtdlThreadGC(void);
 void CtdlThreadStopAll(void);
+#ifdef WITH_THREADLOG
+#define CtdlThreadPushName(NAME) \
+       char *_push_name; \
+       _push_name = CtdlThreadName(CtdlThreadSelf(), NAME)
+
+#define CtdlThreadPopName() \
+       free (CtdlThreadName(CtdlThreadSelf(), _push_name))
+#else
+#define CtdlThreadPushName(NAME)
+#define CtdlThreadPopName()
+#endif
 
 #endif /* CTDL_MODULE_H */
index e344e6ba9fa680c9a872e363a3a49169386a0dbf..267d781a4e2b8b8e425280f3f0cd0c0d991b4be9 100644 (file)
@@ -48,6 +48,8 @@
 #include "serv_vcard.h"                        /* Needed for vcard_getuser and extract_inet_email_addrs */
 #include "journaling.h"
 
+#include "ctdl_module.h"
+
 struct jnlq *jnlq = NULL;      /* journal queue */
 
 /*
@@ -260,6 +262,7 @@ void JournalRunQueueMsg(struct jnlq *jmsg) {
 void JournalRunQueue(void) {
        struct jnlq *jptr = NULL;
 
+       CtdlThreadPushName("JournalRunQueue");
        while (jnlq != NULL) {
                begin_critical_section(S_JOURNAL_QUEUE);
                if (jnlq != NULL) {
@@ -269,6 +272,7 @@ void JournalRunQueue(void) {
                end_critical_section(S_JOURNAL_QUEUE);
                JournalRunQueueMsg(jptr);
        }
+       CtdlThreadPopName();
 }
 
 
index 18e42583002fd618c03dfb93c030575860226b85..5ea03d6737ed7d5ccd8a099f09729b7c1b808e2d 100644 (file)
@@ -922,22 +922,29 @@ void PerformSessionHooks(int EventType)
 {
        struct SessionFunctionHook *fcn = NULL;
 
+       CtdlThreadPushName("PerformSessionHooks");
+       
        for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
                if (fcn->eventtype == EventType) {
                        (*fcn->h_function_pointer) ();
                }
        }
+       CtdlThreadPopName();
+       
 }
 
 void PerformUserHooks(struct ctdluser *usbuf, int EventType)
 {
        struct UserFunctionHook *fcn = NULL;
 
+       CtdlThreadPushName("PerformUserHooks");
+       
        for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
                if (fcn->eventtype == EventType) {
                        (*fcn->h_function_pointer) (usbuf);
                }
        }
+       CtdlThreadPopName();
 }
 
 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
@@ -945,6 +952,8 @@ int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
        struct MessageFunctionHook *fcn = NULL;
        int total_retval = 0;
 
+       CtdlThreadPushName("PerformMessageHooks");
+       
        /* Other code may elect to protect this message from server-side
         * handlers; if this is the case, don't do anything.
        lprintf(CTDL_DEBUG, "** Event type is %d, flags are %d\n",
@@ -952,6 +961,7 @@ int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
         */
        if (msg->cm_flags & CM_SKIP_HOOKS) {
                lprintf(CTDL_DEBUG, "Skipping hooks\n");
+               CtdlThreadPopName();
                return(0);
        }
 
@@ -968,6 +978,7 @@ int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
         * this is an EVT_BEFORESAVE event, a nonzero return code will cause
         * the save operation to abort.
         */
+       CtdlThreadPopName();
        return total_retval;
 }
 
@@ -977,6 +988,8 @@ int PerformRoomHooks(struct ctdlroom *target_room)
        struct RoomFunctionHook *fcn;
        int total_retval = 0;
 
+       CtdlThreadPushName("PerformRoomHooks");
+       
        lprintf(CTDL_DEBUG, "Performing room hooks for <%s>\n", target_room->QRname);
 
        for (fcn = RoomHookTable; fcn != NULL; fcn = fcn->next) {
@@ -985,6 +998,7 @@ int PerformRoomHooks(struct ctdlroom *target_room)
 
        /* Return the sum of the return codes from the hook functions.
         */
+       CtdlThreadPopName();
        return total_retval;
 }
 
@@ -994,6 +1008,8 @@ int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
        struct NetprocFunctionHook *fcn;
        int total_retval = 0;
 
+       CtdlThreadPushName("PerformNetprocHooks");
+       
        for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
                total_retval = total_retval +
                        (*fcn->h_function_pointer) (msg, target_room);
@@ -1002,6 +1018,7 @@ int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
        /* Return the sum of the return codes from the hook functions.
         * A nonzero return code will cause the message to *not* be imported.
         */
+       CtdlThreadPopName();
        return total_retval;
 }
 
@@ -1010,9 +1027,12 @@ void PerformDeleteHooks(char *room, long msgnum)
 {
        struct DeleteFunctionHook *fcn;
 
+       CtdlThreadPushName("PerformDeleteHooks");
+       
        for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
                (*fcn->h_function_pointer) (room, msgnum);
        }
+       CtdlThreadPopName();
 }
 
 
@@ -1025,6 +1045,8 @@ int PerformXmsgHooks(char *sender, char *recp, char *msg)
        int total_sent = 0;
        int p;
 
+       CtdlThreadPushName("PerformXmsgHooks");
+       
        for (p=0; p<MAX_XMSG_PRI; ++p) {
                for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
                        if (fcn->order == p) {
@@ -1040,6 +1062,7 @@ int PerformXmsgHooks(char *sender, char *recp, char *msg)
                 */
                if (total_sent) break;
        }
+       CtdlThreadPopName();
        return total_sent;
 }
 
index faa6d2468cabdd55a5566b05dfdaf4d5c0b392a7..e22a967c44c714ebce0fe0cb189e30eaae4e4c29 100644 (file)
@@ -1263,13 +1263,11 @@ char *CtdlThreadName(struct CtdlThreadNode *thread, char *name)
                return NULL;
        }
        begin_critical_section(S_THREAD_LIST);
+       old_name = this_thread->name;
        if (name)
-       {
-               old_name = this_thread->name;
                this_thread->name = strdup (name);
-               free(old_name);
-       }
-       old_name = strdup(this_thread->name);
+       else
+               old_name = strdup(old_name);
        end_critical_section (S_THREAD_LIST);
        return (old_name);
 }      
@@ -1441,6 +1439,7 @@ void ctdl_thread_internal_calc_loadavg(void)
                        worker_avg += that_thread->load_avg;
                        workers++;
                }
+#ifdef WITH_THREADLOG
                CtdlLogPrintf(CTDL_DEBUG, "CtdlThread, \"%s\" (%ld) \"%s\" %f %f %f %f.\n",
                        that_thread->name,
                        that_thread->tid,
@@ -1449,13 +1448,15 @@ void ctdl_thread_internal_calc_loadavg(void)
                        that_thread->avg_running,
                        that_thread->avg_blocked,
                        that_thread->load_avg);
-
+#endif
                pthread_mutex_unlock(&that_thread->ThreadMutex);
                that_thread = that_thread->next;
        }
        CtdlThreadLoadAvg = load_avg/num_threads;
        CtdlThreadWorkerAvg = worker_avg/workers;
+#ifdef WITH_THREADLOG
        CtdlLogPrintf(CTDL_INFO, "System load average %f, workers averag %f\n", CtdlThreadLoadAvg, CtdlThreadWorkerAvg);
+#endif
        end_critical_section(S_THREAD_LIST);
 }
 
@@ -1473,7 +1474,9 @@ void ctdl_internal_thread_gc (void)
        if(num_threads == 1)
                CtdlThreadList->state = CTDL_THREAD_EXITED;
        
+#ifdef WITH_THREADLOG
        CtdlLogPrintf(CTDL_DEBUG, "Thread system running garbage collection.\n");
+#endif
        /*
         * Woke up to do garbage collection
         */
@@ -1767,9 +1770,12 @@ int CtdlThreadSelect(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds
 void dead_session_purge(int force) {
        struct CitContext *ptr, *ptr2;          /* general-purpose utility pointer */
        struct CitContext *rem = NULL;  /* list of sessions to be destroyed */
-
+       
+       CtdlThreadPushName("dead_session_purge");
+       
        if (force == 0) {
                if ( (time(NULL) - last_purge) < 5 ) {
+                       CtdlThreadPopName();
                        return; /* Too soon, go away */
                }
        }
@@ -1823,6 +1829,9 @@ void dead_session_purge(int force) {
        }
        end_critical_section(S_THREAD_LIST);
        // FIXME: reduce the number of worker threads too
+       
+       CtdlThreadPopName();
+       
 }