moonbridge

changeset 128:ebaac38b7224

Moved functions in source code to avoid necessity of forward declaration
author jbe
date Tue Apr 14 19:32:46 2015 +0200 (2015-04-14)
parents 37532927dba9
children df08e63dc44b
files moonbridge.c
line diff
     1.1 --- a/moonbridge.c	Tue Apr 14 19:30:53 2015 +0200
     1.2 +++ b/moonbridge.c	Tue Apr 14 19:32:46 2015 +0200
     1.3 @@ -943,135 +943,6 @@
     1.4  }
     1.5  
     1.6  
     1.7 -/*** Functions to handle previously created 'struct moonbr_worker' structures ***/
     1.8 -
     1.9 -#define moonbr_try_destroy_worker_stat(str, field) \
    1.10 -  moonbr_log(LOG_INFO, "Resource usage in pool #%i for PID %i: " str " %li", worker->pool->poolnum, (int)worker->pid, (long)childusage.field);
    1.11 -
    1.12 -/* Destroys a worker structure if socket connections have been closed and child process has terminated */
    1.13 -static int moonbr_try_destroy_worker(struct moonbr_worker *worker) {
    1.14 -  if (worker->controlfd != -1 || worker->errorfd != -1) return MOONBR_DESTROY_NONE;
    1.15 -  {
    1.16 -    int childstatus;
    1.17 -    struct rusage childusage;
    1.18 -    {
    1.19 -      pid_t waitedpid;
    1.20 -      while (
    1.21 -        (waitedpid = wait4(worker->pid, &childstatus, WNOHANG, &childusage)) == -1
    1.22 -      ) {
    1.23 -        if (errno != EINTR) {
    1.24 -          moonbr_log(LOG_CRIT, "Error in wait4() call: %s", strerror(errno));
    1.25 -          moonbr_terminate_error();
    1.26 -        }
    1.27 -      }
    1.28 -      if (!waitedpid) return 0;  /* return 0 if worker couldn't be destroyed */
    1.29 -      if (waitedpid != worker->pid) {
    1.30 -        moonbr_log(LOG_CRIT, "Wrong PID returned by wait4() call");
    1.31 -        moonbr_terminate_error();
    1.32 -      }
    1.33 -    }
    1.34 -    if (WIFEXITED(childstatus)) {
    1.35 -      if (WEXITSTATUS(childstatus) || moonbr_stat) {
    1.36 -        moonbr_log(
    1.37 -          WEXITSTATUS(childstatus) ? LOG_WARNING : LOG_INFO,
    1.38 -          "Child process in pool #%i with PID %i returned with exit code %i", worker->pool->poolnum, (int)worker->pid, WEXITSTATUS(childstatus)
    1.39 -        );
    1.40 -      }
    1.41 -    } else if (WIFSIGNALED(childstatus)) {
    1.42 -      if (WCOREDUMP(childstatus)) {
    1.43 -        moonbr_log(LOG_ERR, "Child process in pool #%i with PID %i died from signal %i (core dump was created)", worker->pool->poolnum, (int)worker->pid, WTERMSIG(childstatus));
    1.44 -      } else if (WTERMSIG(childstatus) == SIGALRM) {
    1.45 -        moonbr_log(LOG_WARNING, "Child process in pool #%i with PID %i exited prematurely due to timeout", worker->pool->poolnum, (int)worker->pid);
    1.46 -      } else {
    1.47 -        moonbr_log(LOG_ERR, "Child process in pool #%i with PID %i died from signal %i", worker->pool->poolnum, (int)worker->pid, WTERMSIG(childstatus));
    1.48 -      }
    1.49 -    } else {
    1.50 -      moonbr_log(LOG_CRIT, "Illegal exit status from child process in pool #%i with PID %i", worker->pool->poolnum, (int)worker->pid);
    1.51 -      moonbr_terminate_error();
    1.52 -    }
    1.53 -    if (moonbr_stat) {
    1.54 -      moonbr_log(LOG_INFO, "Resource usage in pool #%i for PID %i: user time %s", worker->pool->poolnum, (int)worker->pid, moonbr_format_timeval(&childusage.ru_utime));
    1.55 -      moonbr_log(LOG_INFO, "Resource usage in pool #%i for PID %i: system time %s", worker->pool->poolnum, (int)worker->pid, moonbr_format_timeval(&childusage.ru_stime));
    1.56 -      moonbr_try_destroy_worker_stat("max resident set size", ru_maxrss);
    1.57 -      moonbr_try_destroy_worker_stat("integral shared memory size", ru_ixrss);
    1.58 -      moonbr_try_destroy_worker_stat("integral unshared data", ru_idrss);
    1.59 -      moonbr_try_destroy_worker_stat("integral unshared stack", ru_isrss);
    1.60 -      moonbr_try_destroy_worker_stat("page replaims", ru_minflt);
    1.61 -      moonbr_try_destroy_worker_stat("page faults", ru_majflt);
    1.62 -      moonbr_try_destroy_worker_stat("swaps", ru_nswap);
    1.63 -      moonbr_try_destroy_worker_stat("block input operations", ru_inblock);
    1.64 -      moonbr_try_destroy_worker_stat("block output operations", ru_oublock);
    1.65 -      moonbr_try_destroy_worker_stat("messages sent", ru_msgsnd);
    1.66 -      moonbr_try_destroy_worker_stat("messages received", ru_msgrcv);
    1.67 -      moonbr_try_destroy_worker_stat("signals received", ru_nsignals);
    1.68 -      moonbr_try_destroy_worker_stat("voluntary context switches", ru_nvcsw);
    1.69 -      moonbr_try_destroy_worker_stat("involuntary context switches", ru_nivcsw);
    1.70 -    }
    1.71 -  }
    1.72 -  {
    1.73 -    int retval = (
    1.74 -      (worker->idle || worker->assigned) ?
    1.75 -      MOONBR_DESTROY_IDLE_OR_ASSIGNED :
    1.76 -      MOONBR_DESTROY_PREPARE
    1.77 -    );
    1.78 -    //if (worker->main) moonbr_initiate_shutdown();  // TODO
    1.79 -    if (worker->prev_worker) worker->prev_worker->next_worker = worker->next_worker;
    1.80 -    else worker->pool->first_worker = worker->next_worker;
    1.81 -    if (worker->next_worker) worker->next_worker->prev_worker = worker->prev_worker;
    1.82 -    else worker->pool->last_worker = worker->prev_worker;
    1.83 -    if (worker->idle) {
    1.84 -      if (worker->prev_idle_worker) worker->prev_idle_worker->next_idle_worker = worker->next_idle_worker;
    1.85 -      else worker->pool->first_idle_worker = worker->next_idle_worker;
    1.86 -      if (worker->next_idle_worker) worker->next_idle_worker->prev_idle_worker = worker->prev_idle_worker;
    1.87 -      else worker->pool->last_idle_worker  = worker->prev_idle_worker;
    1.88 -      worker->pool->idle_worker_count--;
    1.89 -    }
    1.90 -    if (!worker->assigned) worker->pool->unassigned_worker_count--;
    1.91 -    worker->pool->total_worker_count--;
    1.92 -    worker->pool->worker_count_stat = 1;
    1.93 -    if (worker->errorlinebuf) free(worker->errorlinebuf);
    1.94 -    free(worker);
    1.95 -    return retval;
    1.96 -  }
    1.97 -}
    1.98 -
    1.99 -/* Marks a worker as idle and stores it in a queue, optionally setting 'idle_expiration' value */
   1.100 -static void moonbr_add_idle_worker(struct moonbr_worker *worker) {
   1.101 -  worker->prev_idle_worker = worker->pool->last_idle_worker;
   1.102 -  if (worker->prev_idle_worker) worker->prev_idle_worker->next_idle_worker = worker;
   1.103 -  else worker->pool->first_idle_worker = worker;
   1.104 -  worker->pool->last_idle_worker = worker;
   1.105 -  worker->idle = 1;
   1.106 -  worker->pool->idle_worker_count++;
   1.107 -  if (worker->assigned) {
   1.108 -    worker->assigned = 0;
   1.109 -    worker->pool->unassigned_worker_count++;
   1.110 -  }
   1.111 -  worker->pool->worker_count_stat = 1;
   1.112 -  if (timerisset(&worker->pool->idle_timeout)) {
   1.113 -    struct timeval now;
   1.114 -    moonbr_now(&now);
   1.115 -    timeradd(&now, &worker->pool->idle_timeout, &worker->idle_expiration);
   1.116 -  }
   1.117 -}
   1.118 -
   1.119 -/* Pops a worker from the queue of idle workers (idle queue must not be empty) */
   1.120 -static struct moonbr_worker *moonbr_pop_idle_worker(struct moonbr_pool *pool) {
   1.121 -  struct moonbr_worker *worker;
   1.122 -  worker = pool->first_idle_worker;
   1.123 -  pool->first_idle_worker = worker->next_idle_worker;
   1.124 -  if (pool->first_idle_worker) pool->first_idle_worker->prev_idle_worker = NULL;
   1.125 -  else pool->last_idle_worker = NULL;
   1.126 -  worker->next_idle_worker = NULL;
   1.127 -  worker->idle = 0;
   1.128 -  worker->pool->idle_worker_count--;
   1.129 -  worker->assigned = 1;
   1.130 -  worker->pool->unassigned_worker_count--;
   1.131 -  worker->pool->worker_count_stat = 1;
   1.132 -  return worker;
   1.133 -}
   1.134 -
   1.135 -
   1.136  /*** Functions for queues of 'struct moonbr_listener' ***/
   1.137  
   1.138  /* Appends a 'struct moonbr_listener' to the queue of idle listeners and registers it for poll() */
   1.139 @@ -1277,6 +1148,135 @@
   1.140  }
   1.141  
   1.142  
   1.143 +/*** Functions to handle previously created 'struct moonbr_worker' structures ***/
   1.144 +
   1.145 +#define moonbr_try_destroy_worker_stat(str, field) \
   1.146 +  moonbr_log(LOG_INFO, "Resource usage in pool #%i for PID %i: " str " %li", worker->pool->poolnum, (int)worker->pid, (long)childusage.field);
   1.147 +
   1.148 +/* Destroys a worker structure if socket connections have been closed and child process has terminated */
   1.149 +static int moonbr_try_destroy_worker(struct moonbr_worker *worker) {
   1.150 +  if (worker->controlfd != -1 || worker->errorfd != -1) return MOONBR_DESTROY_NONE;
   1.151 +  {
   1.152 +    int childstatus;
   1.153 +    struct rusage childusage;
   1.154 +    {
   1.155 +      pid_t waitedpid;
   1.156 +      while (
   1.157 +        (waitedpid = wait4(worker->pid, &childstatus, WNOHANG, &childusage)) == -1
   1.158 +      ) {
   1.159 +        if (errno != EINTR) {
   1.160 +          moonbr_log(LOG_CRIT, "Error in wait4() call: %s", strerror(errno));
   1.161 +          moonbr_terminate_error();
   1.162 +        }
   1.163 +      }
   1.164 +      if (!waitedpid) return 0;  /* return 0 if worker couldn't be destroyed */
   1.165 +      if (waitedpid != worker->pid) {
   1.166 +        moonbr_log(LOG_CRIT, "Wrong PID returned by wait4() call");
   1.167 +        moonbr_terminate_error();
   1.168 +      }
   1.169 +    }
   1.170 +    if (WIFEXITED(childstatus)) {
   1.171 +      if (WEXITSTATUS(childstatus) || moonbr_stat) {
   1.172 +        moonbr_log(
   1.173 +          WEXITSTATUS(childstatus) ? LOG_WARNING : LOG_INFO,
   1.174 +          "Child process in pool #%i with PID %i returned with exit code %i", worker->pool->poolnum, (int)worker->pid, WEXITSTATUS(childstatus)
   1.175 +        );
   1.176 +      }
   1.177 +    } else if (WIFSIGNALED(childstatus)) {
   1.178 +      if (WCOREDUMP(childstatus)) {
   1.179 +        moonbr_log(LOG_ERR, "Child process in pool #%i with PID %i died from signal %i (core dump was created)", worker->pool->poolnum, (int)worker->pid, WTERMSIG(childstatus));
   1.180 +      } else if (WTERMSIG(childstatus) == SIGALRM) {
   1.181 +        moonbr_log(LOG_WARNING, "Child process in pool #%i with PID %i exited prematurely due to timeout", worker->pool->poolnum, (int)worker->pid);
   1.182 +      } else {
   1.183 +        moonbr_log(LOG_ERR, "Child process in pool #%i with PID %i died from signal %i", worker->pool->poolnum, (int)worker->pid, WTERMSIG(childstatus));
   1.184 +      }
   1.185 +    } else {
   1.186 +      moonbr_log(LOG_CRIT, "Illegal exit status from child process in pool #%i with PID %i", worker->pool->poolnum, (int)worker->pid);
   1.187 +      moonbr_terminate_error();
   1.188 +    }
   1.189 +    if (moonbr_stat) {
   1.190 +      moonbr_log(LOG_INFO, "Resource usage in pool #%i for PID %i: user time %s", worker->pool->poolnum, (int)worker->pid, moonbr_format_timeval(&childusage.ru_utime));
   1.191 +      moonbr_log(LOG_INFO, "Resource usage in pool #%i for PID %i: system time %s", worker->pool->poolnum, (int)worker->pid, moonbr_format_timeval(&childusage.ru_stime));
   1.192 +      moonbr_try_destroy_worker_stat("max resident set size", ru_maxrss);
   1.193 +      moonbr_try_destroy_worker_stat("integral shared memory size", ru_ixrss);
   1.194 +      moonbr_try_destroy_worker_stat("integral unshared data", ru_idrss);
   1.195 +      moonbr_try_destroy_worker_stat("integral unshared stack", ru_isrss);
   1.196 +      moonbr_try_destroy_worker_stat("page replaims", ru_minflt);
   1.197 +      moonbr_try_destroy_worker_stat("page faults", ru_majflt);
   1.198 +      moonbr_try_destroy_worker_stat("swaps", ru_nswap);
   1.199 +      moonbr_try_destroy_worker_stat("block input operations", ru_inblock);
   1.200 +      moonbr_try_destroy_worker_stat("block output operations", ru_oublock);
   1.201 +      moonbr_try_destroy_worker_stat("messages sent", ru_msgsnd);
   1.202 +      moonbr_try_destroy_worker_stat("messages received", ru_msgrcv);
   1.203 +      moonbr_try_destroy_worker_stat("signals received", ru_nsignals);
   1.204 +      moonbr_try_destroy_worker_stat("voluntary context switches", ru_nvcsw);
   1.205 +      moonbr_try_destroy_worker_stat("involuntary context switches", ru_nivcsw);
   1.206 +    }
   1.207 +  }
   1.208 +  {
   1.209 +    int retval = (
   1.210 +      (worker->idle || worker->assigned) ?
   1.211 +      MOONBR_DESTROY_IDLE_OR_ASSIGNED :
   1.212 +      MOONBR_DESTROY_PREPARE
   1.213 +    );
   1.214 +    if (worker->main) moonbr_initiate_shutdown();
   1.215 +    if (worker->prev_worker) worker->prev_worker->next_worker = worker->next_worker;
   1.216 +    else worker->pool->first_worker = worker->next_worker;
   1.217 +    if (worker->next_worker) worker->next_worker->prev_worker = worker->prev_worker;
   1.218 +    else worker->pool->last_worker = worker->prev_worker;
   1.219 +    if (worker->idle) {
   1.220 +      if (worker->prev_idle_worker) worker->prev_idle_worker->next_idle_worker = worker->next_idle_worker;
   1.221 +      else worker->pool->first_idle_worker = worker->next_idle_worker;
   1.222 +      if (worker->next_idle_worker) worker->next_idle_worker->prev_idle_worker = worker->prev_idle_worker;
   1.223 +      else worker->pool->last_idle_worker  = worker->prev_idle_worker;
   1.224 +      worker->pool->idle_worker_count--;
   1.225 +    }
   1.226 +    if (!worker->assigned) worker->pool->unassigned_worker_count--;
   1.227 +    worker->pool->total_worker_count--;
   1.228 +    worker->pool->worker_count_stat = 1;
   1.229 +    if (worker->errorlinebuf) free(worker->errorlinebuf);
   1.230 +    free(worker);
   1.231 +    return retval;
   1.232 +  }
   1.233 +}
   1.234 +
   1.235 +/* Marks a worker as idle and stores it in a queue, optionally setting 'idle_expiration' value */
   1.236 +static void moonbr_add_idle_worker(struct moonbr_worker *worker) {
   1.237 +  worker->prev_idle_worker = worker->pool->last_idle_worker;
   1.238 +  if (worker->prev_idle_worker) worker->prev_idle_worker->next_idle_worker = worker;
   1.239 +  else worker->pool->first_idle_worker = worker;
   1.240 +  worker->pool->last_idle_worker = worker;
   1.241 +  worker->idle = 1;
   1.242 +  worker->pool->idle_worker_count++;
   1.243 +  if (worker->assigned) {
   1.244 +    worker->assigned = 0;
   1.245 +    worker->pool->unassigned_worker_count++;
   1.246 +  }
   1.247 +  worker->pool->worker_count_stat = 1;
   1.248 +  if (timerisset(&worker->pool->idle_timeout)) {
   1.249 +    struct timeval now;
   1.250 +    moonbr_now(&now);
   1.251 +    timeradd(&now, &worker->pool->idle_timeout, &worker->idle_expiration);
   1.252 +  }
   1.253 +}
   1.254 +
   1.255 +/* Pops a worker from the queue of idle workers (idle queue must not be empty) */
   1.256 +static struct moonbr_worker *moonbr_pop_idle_worker(struct moonbr_pool *pool) {
   1.257 +  struct moonbr_worker *worker;
   1.258 +  worker = pool->first_idle_worker;
   1.259 +  pool->first_idle_worker = worker->next_idle_worker;
   1.260 +  if (pool->first_idle_worker) pool->first_idle_worker->prev_idle_worker = NULL;
   1.261 +  else pool->last_idle_worker = NULL;
   1.262 +  worker->next_idle_worker = NULL;
   1.263 +  worker->idle = 0;
   1.264 +  worker->pool->idle_worker_count--;
   1.265 +  worker->assigned = 1;
   1.266 +  worker->pool->unassigned_worker_count--;
   1.267 +  worker->pool->worker_count_stat = 1;
   1.268 +  return worker;
   1.269 +}
   1.270 +
   1.271 +
   1.272  /*** Functions to communicate with child processes ***/
   1.273  
   1.274  /* Tells child process to terminate */

Impressum / About Us