root/sapi/fpm/fpm/fpm_request.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. fpm_request_get_stage_name
  2. fpm_request_accepting
  3. fpm_request_reading_headers
  4. fpm_request_info
  5. fpm_request_executing
  6. fpm_request_end
  7. fpm_request_finished
  8. fpm_request_check_timed_out
  9. fpm_request_is_idle
  10. fpm_request_last_activity

   1 
   2         /* $Id: fpm_request.c,v 1.9.2.1 2008/11/15 00:57:24 anight Exp $ */
   3         /* (c) 2007,2008 Andrei Nigmatulin */
   4 #ifdef HAVE_TIMES
   5 #include <sys/times.h>
   6 #endif
   7 
   8 #include "fpm_config.h"
   9 
  10 #include "fpm.h"
  11 #include "fpm_php.h"
  12 #include "fpm_str.h"
  13 #include "fpm_clock.h"
  14 #include "fpm_conf.h"
  15 #include "fpm_trace.h"
  16 #include "fpm_php_trace.h"
  17 #include "fpm_process_ctl.h"
  18 #include "fpm_children.h"
  19 #include "fpm_scoreboard.h"
  20 #include "fpm_status.h"
  21 #include "fpm_request.h"
  22 #include "fpm_log.h"
  23 
  24 #include "zlog.h"
  25 
  26 static const char *requests_stages[] = {
  27         [FPM_REQUEST_ACCEPTING]       = "Idle",
  28         [FPM_REQUEST_READING_HEADERS] = "Reading headers",
  29         [FPM_REQUEST_INFO]            = "Getting request informations",
  30         [FPM_REQUEST_EXECUTING]       = "Running",
  31         [FPM_REQUEST_END]             = "Ending",
  32         [FPM_REQUEST_FINISHED]        = "Finishing",
  33 };
  34 
  35 const char *fpm_request_get_stage_name(int stage) {
  36         return requests_stages[stage];
  37 }
  38 
  39 void fpm_request_accepting() /* {{{ */
  40 {
  41         struct fpm_scoreboard_proc_s *proc;
  42         struct timeval now;
  43 
  44         fpm_clock_get(&now);
  45 
  46         proc = fpm_scoreboard_proc_acquire(NULL, -1, 0);
  47         if (proc == NULL) {
  48                 zlog(ZLOG_WARNING, "failed to acquire proc scoreboard");
  49                 return;
  50         }
  51 
  52         proc->request_stage = FPM_REQUEST_ACCEPTING;
  53         proc->tv = now;
  54         fpm_scoreboard_proc_release(proc);
  55 
  56         /* idle++, active-- */
  57         fpm_scoreboard_update(1, -1, 0, 0, 0, 0, 0, FPM_SCOREBOARD_ACTION_INC, NULL);
  58 }
  59 /* }}} */
  60 
  61 void fpm_request_reading_headers() /* {{{ */
  62 {
  63         struct fpm_scoreboard_proc_s *proc;
  64 
  65         struct timeval now;
  66         clock_t now_epoch;
  67 #ifdef HAVE_TIMES
  68         struct tms cpu;
  69 #endif
  70 
  71         fpm_clock_get(&now);
  72         now_epoch = time(NULL);
  73 #ifdef HAVE_TIMES
  74         times(&cpu);
  75 #endif
  76 
  77         proc = fpm_scoreboard_proc_acquire(NULL, -1, 0);
  78         if (proc == NULL) {
  79                 zlog(ZLOG_WARNING, "failed to acquire proc scoreboard");
  80                 return;
  81         }
  82 
  83         proc->request_stage = FPM_REQUEST_READING_HEADERS;
  84         proc->tv = now;
  85         proc->accepted = now;
  86         proc->accepted_epoch = now_epoch;
  87 #ifdef HAVE_TIMES
  88         proc->cpu_accepted = cpu;
  89 #endif
  90         proc->requests++;
  91         proc->request_uri[0] = '\0';
  92         proc->request_method[0] = '\0';
  93         proc->script_filename[0] = '\0';
  94         proc->query_string[0] = '\0';
  95         proc->auth_user[0] = '\0';
  96         proc->content_length = 0;
  97         fpm_scoreboard_proc_release(proc);
  98 
  99         /* idle--, active++, request++ */
 100         fpm_scoreboard_update(-1, 1, 0, 0, 1, 0, 0, FPM_SCOREBOARD_ACTION_INC, NULL);
 101 }
 102 /* }}} */
 103 
 104 void fpm_request_info() /* {{{ */
 105 {
 106         struct fpm_scoreboard_proc_s *proc;
 107         char *request_uri = fpm_php_request_uri();
 108         char *request_method = fpm_php_request_method();
 109         char *script_filename = fpm_php_script_filename();
 110         char *query_string = fpm_php_query_string();
 111         char *auth_user = fpm_php_auth_user();
 112         size_t content_length = fpm_php_content_length();
 113         struct timeval now;
 114 
 115         fpm_clock_get(&now);
 116 
 117         proc = fpm_scoreboard_proc_acquire(NULL, -1, 0);
 118         if (proc == NULL) {
 119                 zlog(ZLOG_WARNING, "failed to acquire proc scoreboard");
 120                 return;
 121         }
 122 
 123         proc->request_stage = FPM_REQUEST_INFO;
 124         proc->tv = now;
 125 
 126         if (request_uri) {
 127                 strlcpy(proc->request_uri, request_uri, sizeof(proc->request_uri));
 128         }
 129 
 130         if (request_method) {
 131                 strlcpy(proc->request_method, request_method, sizeof(proc->request_method));
 132         }
 133 
 134         if (query_string) {
 135                 strlcpy(proc->query_string, query_string, sizeof(proc->query_string));
 136         }
 137 
 138         if (auth_user) {
 139                 strlcpy(proc->auth_user, auth_user, sizeof(proc->auth_user));
 140         }
 141 
 142         proc->content_length = content_length;
 143 
 144         /* if cgi.fix_pathinfo is set to "1" and script cannot be found (404)
 145                 the sapi_globals.request_info.path_translated is set to NULL */
 146         if (script_filename) {
 147                 strlcpy(proc->script_filename, script_filename, sizeof(proc->script_filename));
 148         }
 149 
 150         fpm_scoreboard_proc_release(proc);
 151 }
 152 /* }}} */
 153 
 154 void fpm_request_executing() /* {{{ */
 155 {
 156         struct fpm_scoreboard_proc_s *proc;
 157         struct timeval now;
 158 
 159         fpm_clock_get(&now);
 160 
 161         proc = fpm_scoreboard_proc_acquire(NULL, -1, 0);
 162         if (proc == NULL) {
 163                 zlog(ZLOG_WARNING, "failed to acquire proc scoreboard");
 164                 return;
 165         }
 166 
 167         proc->request_stage = FPM_REQUEST_EXECUTING;
 168         proc->tv = now;
 169         fpm_scoreboard_proc_release(proc);
 170 }
 171 /* }}} */
 172 
 173 void fpm_request_end(void) /* {{{ */
 174 {
 175         struct fpm_scoreboard_proc_s *proc;
 176         struct timeval now;
 177 #ifdef HAVE_TIMES
 178         struct tms cpu;
 179 #endif
 180         size_t memory = zend_memory_peak_usage(1);
 181 
 182         fpm_clock_get(&now);
 183 #ifdef HAVE_TIMES
 184         times(&cpu);
 185 #endif
 186 
 187         proc = fpm_scoreboard_proc_acquire(NULL, -1, 0);
 188         if (proc == NULL) {
 189                 zlog(ZLOG_WARNING, "failed to acquire proc scoreboard");
 190                 return;
 191         }
 192         proc->request_stage = FPM_REQUEST_FINISHED;
 193         proc->tv = now;
 194         timersub(&now, &proc->accepted, &proc->duration);
 195 #ifdef HAVE_TIMES
 196         timersub(&proc->tv, &proc->accepted, &proc->cpu_duration);
 197         proc->last_request_cpu.tms_utime = cpu.tms_utime - proc->cpu_accepted.tms_utime;
 198         proc->last_request_cpu.tms_stime = cpu.tms_stime - proc->cpu_accepted.tms_stime;
 199         proc->last_request_cpu.tms_cutime = cpu.tms_cutime - proc->cpu_accepted.tms_cutime;
 200         proc->last_request_cpu.tms_cstime = cpu.tms_cstime - proc->cpu_accepted.tms_cstime;
 201 #endif
 202         proc->memory = memory;
 203         fpm_scoreboard_proc_release(proc);
 204 }
 205 /* }}} */
 206 
 207 void fpm_request_finished() /* {{{ */
 208 {
 209         struct fpm_scoreboard_proc_s *proc;
 210         struct timeval now;
 211 
 212         fpm_clock_get(&now);
 213 
 214         proc = fpm_scoreboard_proc_acquire(NULL, -1, 0);
 215         if (proc == NULL) {
 216                 zlog(ZLOG_WARNING, "failed to acquire proc scoreboard");
 217                 return;
 218         }
 219 
 220         proc->request_stage = FPM_REQUEST_FINISHED;
 221         proc->tv = now;
 222         fpm_scoreboard_proc_release(proc);
 223 }
 224 /* }}} */
 225 
 226 void fpm_request_check_timed_out(struct fpm_child_s *child, struct timeval *now, int terminate_timeout, int slowlog_timeout) /* {{{ */
 227 {
 228         struct fpm_scoreboard_proc_s proc, *proc_p;
 229 
 230         proc_p = fpm_scoreboard_proc_acquire(child->wp->scoreboard, child->scoreboard_i, 1);
 231         if (!proc_p) {
 232                 zlog(ZLOG_WARNING, "failed to acquire scoreboard");
 233                 return;
 234         }
 235 
 236         proc = *proc_p;
 237         fpm_scoreboard_proc_release(proc_p);
 238 
 239 #if HAVE_FPM_TRACE
 240         if (child->slow_logged.tv_sec) {
 241                 if (child->slow_logged.tv_sec != proc.accepted.tv_sec || child->slow_logged.tv_usec != proc.accepted.tv_usec) {
 242                         child->slow_logged.tv_sec = 0;
 243                         child->slow_logged.tv_usec = 0;
 244                 }
 245         }
 246 #endif
 247 
 248         if (proc.request_stage > FPM_REQUEST_ACCEPTING && proc.request_stage < FPM_REQUEST_END) {
 249                 char purified_script_filename[sizeof(proc.script_filename)];
 250                 struct timeval tv;
 251 
 252                 timersub(now, &proc.accepted, &tv);
 253 
 254 #if HAVE_FPM_TRACE
 255                 if (child->slow_logged.tv_sec == 0 && slowlog_timeout &&
 256                                 proc.request_stage == FPM_REQUEST_EXECUTING && tv.tv_sec >= slowlog_timeout) {
 257 
 258                         str_purify_filename(purified_script_filename, proc.script_filename, sizeof(proc.script_filename));
 259 
 260                         child->slow_logged = proc.accepted;
 261                         child->tracer = fpm_php_trace;
 262 
 263                         fpm_trace_signal(child->pid);
 264 
 265                         zlog(ZLOG_WARNING, "[pool %s] child %d, script '%s' (request: \"%s %s\") executing too slow (%d.%06d sec), logging",
 266                                 child->wp->config->name, (int) child->pid, purified_script_filename, proc.request_method, proc.request_uri,
 267                                 (int) tv.tv_sec, (int) tv.tv_usec);
 268                 }
 269                 else
 270 #endif
 271                 if (terminate_timeout && tv.tv_sec >= terminate_timeout) {
 272                         str_purify_filename(purified_script_filename, proc.script_filename, sizeof(proc.script_filename));
 273                         fpm_pctl_kill(child->pid, FPM_PCTL_TERM);
 274 
 275                         zlog(ZLOG_WARNING, "[pool %s] child %d, script '%s' (request: \"%s %s\") execution timed out (%d.%06d sec), terminating",
 276                                 child->wp->config->name, (int) child->pid, purified_script_filename, proc.request_method, proc.request_uri,
 277                                 (int) tv.tv_sec, (int) tv.tv_usec);
 278                 }
 279         }
 280 }
 281 /* }}} */
 282 
 283 int fpm_request_is_idle(struct fpm_child_s *child) /* {{{ */
 284 {
 285         struct fpm_scoreboard_proc_s *proc;
 286 
 287         /* no need in atomicity here */
 288         proc = fpm_scoreboard_proc_get(child->wp->scoreboard, child->scoreboard_i);
 289         if (!proc) {
 290                 return 0;
 291         }
 292 
 293         return proc->request_stage == FPM_REQUEST_ACCEPTING;
 294 }
 295 /* }}} */
 296 
 297 int fpm_request_last_activity(struct fpm_child_s *child, struct timeval *tv) /* {{{ */
 298 {
 299         struct fpm_scoreboard_proc_s *proc;
 300 
 301         if (!tv) return -1;
 302 
 303         proc = fpm_scoreboard_proc_get(child->wp->scoreboard, child->scoreboard_i);
 304         if (!proc) {
 305                 return -1;
 306         }
 307 
 308         *tv = proc->tv;
 309 
 310         return 1;
 311 }
 312 /* }}} */

/* [<][>][^][v][top][bottom][index][help] */