]> code.citadel.org Git - citadel.git/blobdiff - citadel/threads.c
Increased the load average before strangling the server. Its now 10.00
[citadel.git] / citadel / threads.c
index 9782d05fc005c5bf9a3f0567cb1c32dd5db65e90..c13a2aed52c700e216e70b1d3095e8efe2879772 100644 (file)
@@ -60,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;
@@ -1232,19 +1234,54 @@ void *new_worker_thread(void *arg);
 extern void close_masters (void);
 
 
+void *simulation_worker (void*arg) {
+       struct CitContext *this;
+
+       this = CreateNewContext();
+       CtdlThreadSleep(1);
+       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 && !CtdlThreadCheckStop()) {
+               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
         */
        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
@@ -1314,6 +1351,7 @@ 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
@@ -1321,7 +1359,9 @@ void go_threading(void)
 #endif /* NEW_WORKER */
                {
                        /* Only start new threads if we are not going to overload the machine */
-                       if (CtdlThreadGetLoadAvg() < ((double)1.00)) {
+                       /* Temporarily set to 10 should be enough to make sure we don't stranglew the server
+                        * at least until we make this a config option */
+                       if (CtdlThreadGetLoadAvg() < ((double)10.00)) {
                                for (i=0; i<5 ; i++) {
 #ifdef NEW_WORKER
                                        CtdlThreadCreate("Worker Thread (new)",
@@ -1341,7 +1381,8 @@ void go_threading(void)
                        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
@@ -1360,6 +1401,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);
+       }
 }
 
 
@@ -1387,7 +1435,7 @@ void select_on_master(void)
         int m, i;
         int retval = 0;
         struct timeval tv;
-        struct CitContext *con;
+        CitContext *con;
         const char *old_name;
 
 
@@ -1473,7 +1521,7 @@ void select_on_master(void)
  * If the select succeeds the thread goes off to handle the client request.
  * If the list of client connections is empty the threads all sleep for one second
  */
-struct CitContext *select_on_client(void)
+CitContext *select_on_client(void)
 {
        fd_set readfds;
        struct timeval tv;
@@ -1540,7 +1588,7 @@ struct CitContext *select_on_client(void)
 /*
  * Do the worker threads work when needed
  */
-int execute_session(struct CitContext *bind_me)
+int execute_session(CitContext *bind_me)
 {
        int force_purge;
        
@@ -1577,7 +1625,7 @@ int execute_session(struct CitContext *bind_me)
  
 void *new_worker_thread(void *arg)
 {
-       struct CitContext *bind_me;
+       CitContext *bind_me;
        int force_purge;
        
        while (!CtdlThreadCheckStop()) {