]> code.citadel.org Git - citadel.git/blobdiff - citadel/threads.c
Added a new command line option to citserver "-s" takes a numerical parameter.
[citadel.git] / citadel / threads.c
index 63e6ba0724b72aa7ae708b1da24eab3f10c93aa8..aa6636e0aa3c720a2a7d173ed57884a455c4c33e 100644 (file)
@@ -1,8 +1,8 @@
 /*
- * $Id: sysdep.c 5882 2007-12-13 19:46:05Z davew $
+ * $Id$
  *
  * Citadel "system dependent" stuff.
- * See copyright.txt for copyright information.
+ * See COPYING for copyright information.
  *
  * Here's where we have the Citadel thread implimentation
  *
@@ -38,6 +38,7 @@
 #include "config.h"
 #include "citserver.h"
 #include "sysdep_decls.h"
+#include "context.h"
 
 /*
  * define this to use the new worker_thread method of handling connections
@@ -59,6 +60,8 @@
 
 static int num_threads = 0;                    /* Current number of threads */
 static int num_workers = 0;                    /* Current number of worker threads */
+long statcount = 0;            /* are we doing a stats check? */
+static long stats_done = 0;
 
 CtdlThreadNode *CtdlThreadList = NULL;
 CtdlThreadNode *CtdlThreadSchedList = NULL;
@@ -328,6 +331,9 @@ void ctdl_thread_internal_change_state (CtdlThreadNode *this_thread, enum CtdlTh
  */
 void CtdlThreadStopAll(void)
 {
+       /* First run any registered shutdown hooks.  This probably doesn't belong here. */
+       PerformSessionHooks(EVT_SHUTDOWN);
+
        //FIXME: The signalling of the condition should not be in the critical_section
        // We need to build a list of threads we are going to signal and then signal them afterwards
        
@@ -404,12 +410,33 @@ double CtdlThreadGetWorkerAvg(void)
 
 double CtdlThreadGetLoadAvg(void)
 {
-       double ret;
-       
+       double load_avg[3] ;
+
+       int ret;
+       int smp_num_cpus;
+
+       /* Borrowed this straight from procps */
+       smp_num_cpus = sysconf(_SC_NPROCESSORS_ONLN);
+       if(smp_num_cpus<1) smp_num_cpus=1; /* SPARC glibc is buggy */
+
+       ret = getloadavg(load_avg, 3);
+       if (ret < 0)
+               return 0;
+       return load_avg[0] / smp_num_cpus;
+/*
+ * This old chunk of code return a value that indicated the load on citserver
+ * This value could easily reach 100 % even when citserver was doing very little and
+ * hence the machine has much more spare capacity.
+ * Because this value was used to determine if the machine was under heavy load conditions
+ * from other processes in the system then citserver could be strangled un-necesarily
+ * What we are actually trying to achieve is to strangle citserver if the machine is heavily loaded.
+ * So we have changed this.
+
        begin_critical_section(S_THREAD_LIST);
        ret =  CtdlThreadLoadAvg;
        end_critical_section(S_THREAD_LIST);
        return ret;
+*/
 }
 
 
@@ -620,7 +647,7 @@ void ctdl_thread_internal_calc_loadavg(void)
        CtdlThreadLoadAvg = load_avg/num_threads;
        CtdlThreadWorkerAvg = worker_avg/workers;
 #ifdef WITH_THREADLOG
-       CtdlLogPrintf(CTDL_INFO, "System load average %.2f, workers averag %.2f, threads %d, workers %d, sessions %d\n", CtdlThreadLoadAvg, CtdlThreadWorkerAvg, num_threads, num_workers, num_sessions);
+       CtdlLogPrintf(CTDL_INFO, "System load average %.2f, workers averag %.2f, threads %d, workers %d, sessions %d\n", CtdlThreadGetLoadAvg(), CtdlThreadWorkerAvg, num_threads, num_workers, num_sessions);
 #endif
 }
 
@@ -1172,14 +1199,7 @@ int CtdlThreadSelect(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds
         * or timeout so this thread could stop if asked to do so.
         * Anything else means it needs to continue unless the system is shutting down
         */
-       if (ret <= 0)
-       {
-               /**
-                * select says nothing to do so we can change to running if we haven't been asked to stop.
-                */
-               ctdl_thread_internal_change_state(CT, CTDL_THREAD_RUNNING);
-       }
-       else
+       if (ret > 0)
        {
                /**
                 * The select says this thread needs to do something useful.
@@ -1203,6 +1223,8 @@ int CtdlThreadSelect(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds
                citthread_mutex_unlock(&CT->ThreadMutex);
        }
 
+       ctdl_thread_internal_change_state(CT, CTDL_THREAD_RUNNING);
+
        return ret;
 }
 
@@ -1212,11 +1234,38 @@ void *new_worker_thread(void *arg);
 extern void close_masters (void);
 
 
+void *simulation_worker (void*arg) {
+       struct CitContext *this;
+
+       this = CreateNewContext();
+       this->kill_me = 1;
+       this->state = CON_IDLE;
+       dead_session_purge(1);
+       begin_critical_section(S_SESSION_TABLE);
+       stats_done++;
+       end_critical_section(S_SESSION_TABLE);
+       return NULL;
+}
+
+
+void *simulation_thread (void *arg)
+{
+       long stats = statcount;
+
+       while(stats) {
+               CtdlThreadCreate("Connection simulation worker", CTDLTHREAD_BIGSTACK, simulation_worker, NULL);
+               stats--;
+       }
+       CtdlThreadStopAll();
+       return NULL;
+}
 
 void go_threading(void)
 {
        int i;
        CtdlThreadNode *last_worker;
+       struct timeval start, now, result;
+       double last_duration;
        
        /*
         * Initialise the thread system
@@ -1224,7 +1273,14 @@ void go_threading(void)
        ctdl_thread_internal_init();
 
        /* Second call to module init functions now that threading is up */
-       initialise_modules(1);
+       if (!statcount)
+               initialise_modules(1);
+       else {
+               CtdlLogPrintf(CTDL_EMERG, "Running connection simulation stats\n");
+               gettimeofday(&start, NULL);
+               CtdlThreadCreate("Connection simulation master", CTDLTHREAD_BIGSTACK, simulation_thread, NULL);
+       }
+
 
        /*
         * This thread is now used for garbage collection of other threads in the thread list
@@ -1294,32 +1350,38 @@ void go_threading(void)
                /* FIXME: come up with a better way to dynamically alter the number of threads
                 * based on the system load
                 */
+               if (!statcount) {
 #ifdef NEW_WORKER
                if ((((CtdlThreadGetWorkers() < config.c_max_workers) && (CtdlThreadGetWorkers() <= num_sessions) ) || CtdlThreadGetWorkers() < config.c_min_workers) && (CT->state > CTDL_THREAD_STOP_REQ))
 #else
-               if ((((CtdlThreadGetWorkers() < config.c_max_workers) && (CtdlThreadGetWorkerAvg() > 60) && (CtdlThreadGetLoadAvg() < 90) ) || CtdlThreadGetWorkers() < config.c_min_workers) && (CT->state > CTDL_THREAD_STOP_REQ))
+               if ((((CtdlThreadGetWorkers() < config.c_max_workers) && (CtdlThreadGetWorkerAvg() > 60)) || CtdlThreadGetWorkers() < config.c_min_workers) && (CT->state > CTDL_THREAD_STOP_REQ))
 #endif /* NEW_WORKER */
                {
-                       for (i=0; i<5 ; i++)
-                       {
+                       /* Only start new threads if we are not going to overload the machine */
+                       if (CtdlThreadGetLoadAvg() < ((double)1.00)) {
+                               for (i=0; i<5 ; i++) {
 #ifdef NEW_WORKER
-                               CtdlThreadCreate("Worker Thread (new)",
-                                       CTDLTHREAD_BIGSTACK + CTDLTHREAD_WORKER,
-                                       new_worker_thread,
-                                       NULL
-                                       );
+                                       CtdlThreadCreate("Worker Thread (new)",
+                                               CTDLTHREAD_BIGSTACK + CTDLTHREAD_WORKER,
+                                               new_worker_thread,
+                                               NULL
+                                               );
 #else
-                               CtdlThreadCreate("Worker Thread",
-                                       CTDLTHREAD_BIGSTACK + CTDLTHREAD_WORKER,
-                                       worker_thread,
-                                       NULL
-                                       );
+                                       CtdlThreadCreate("Worker Thread",
+                                               CTDLTHREAD_BIGSTACK + CTDLTHREAD_WORKER,
+                                               worker_thread,
+                                               NULL
+                                               );
 #endif /* NEW_WORKER */
+                               }
                        }
+                       else
+                               CtdlLogPrintf (CTDL_WARNING, "Server strangled due to machine load average too high.\n");
                }
-               
+               }
+
                CtdlThreadGC();
-               
+
                if (CtdlThreadGetCount() <= 1) // Shutting down clean up the garbage collector
                {
                        CtdlThreadGC();
@@ -1328,7 +1390,7 @@ void go_threading(void)
 #ifdef THREADS_USESIGNALS
                if (CtdlThreadGetCount() && CT->state > CTDL_THREAD_STOP_REQ)
 #else
-               if (CtdlThreadGetCount())
+               if (CtdlThreadGetCount() && !statcount)
 #endif
                        CtdlThreadSleep(1);
        }
@@ -1336,6 +1398,13 @@ void go_threading(void)
         * If the above loop exits we must be shutting down since we obviously have no threads
         */
        ctdl_thread_internal_cleanup();
+
+       if (statcount) {
+               gettimeofday(&now, NULL);
+               timersub(&now, &start, &result);
+               last_duration = (double)result.tv_sec + ((double)result.tv_usec / (double) 1000000);
+               CtdlLogPrintf(CTDL_EMERG, "Simulated %ld connections in %f seconds\n", stats_done, last_duration);
+       }
 }
 
 
@@ -1546,7 +1615,6 @@ int execute_session(struct CitContext *bind_me)
 
 
 
-extern void dead_session_purge(int force);
 
 /*
  * A new worker_thread loop.