Annotation of embedaddon/php/sapi/fpm/fpm/fpm_request.c, revision 1.1.1.1

1.1       misho       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, 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->query_string[0] = '\0';
                     96:        proc->auth_user[0] = '\0';
                     97:        proc->content_length = 0;
                     98:        fpm_scoreboard_proc_release(proc);
                     99: 
                    100:        /* idle--, active++, request++ */
                    101:        fpm_scoreboard_update(-1, 1, 0, 0, 1, 0, FPM_SCOREBOARD_ACTION_INC, NULL);
                    102: }
                    103: /* }}} */
                    104: 
                    105: void fpm_request_info() /* {{{ */
                    106: {
                    107:        TSRMLS_FETCH();
                    108:        struct fpm_scoreboard_proc_s *proc;
                    109:        char *request_uri = fpm_php_request_uri(TSRMLS_C);
                    110:        char *request_method = fpm_php_request_method(TSRMLS_C);
                    111:        char *script_filename = fpm_php_script_filename(TSRMLS_C);
                    112:        char *query_string = fpm_php_query_string(TSRMLS_C);
                    113:        char *auth_user = fpm_php_auth_user(TSRMLS_C);
                    114:        size_t content_length = fpm_php_content_length(TSRMLS_C);
                    115:        struct timeval now;
                    116: 
                    117:        fpm_clock_get(&now);
                    118: 
                    119:        proc = fpm_scoreboard_proc_acquire(NULL, -1, 0);
                    120:        if (proc == NULL) {
                    121:                zlog(ZLOG_WARNING, "failed to acquire proc scoreboard");
                    122:                return;
                    123:        }
                    124: 
                    125:        proc->request_stage = FPM_REQUEST_INFO;
                    126:        proc->tv = now;
                    127: 
                    128:        if (request_uri) {
                    129:                strlcpy(proc->request_uri, request_uri, sizeof(proc->request_uri));
                    130:        }
                    131: 
                    132:        if (request_method) {
                    133:                strlcpy(proc->request_method, request_method, sizeof(proc->request_method));
                    134:        }
                    135: 
                    136:        if (query_string) {
                    137:                strlcpy(proc->query_string, query_string, sizeof(proc->query_string));
                    138:        }
                    139: 
                    140:        if (auth_user) {
                    141:                strlcpy(proc->auth_user, auth_user, sizeof(proc->auth_user));
                    142:        }
                    143: 
                    144:        proc->content_length = content_length;
                    145: 
                    146:        /* if cgi.fix_pathinfo is set to "1" and script cannot be found (404)
                    147:                the sapi_globals.request_info.path_translated is set to NULL */
                    148:        if (script_filename) {
                    149:                strlcpy(proc->script_filename, script_filename, sizeof(proc->script_filename));
                    150:        }
                    151: 
                    152:        fpm_scoreboard_proc_release(proc);
                    153: }
                    154: /* }}} */
                    155: 
                    156: void fpm_request_executing() /* {{{ */
                    157: {
                    158:        struct fpm_scoreboard_proc_s *proc;
                    159:        struct timeval now;
                    160: 
                    161:        fpm_clock_get(&now);
                    162: 
                    163:        proc = fpm_scoreboard_proc_acquire(NULL, -1, 0);
                    164:        if (proc == NULL) {
                    165:                zlog(ZLOG_WARNING, "failed to acquire proc scoreboard");
                    166:                return;
                    167:        }
                    168: 
                    169:        proc->request_stage = FPM_REQUEST_EXECUTING;
                    170:        proc->tv = now;
                    171:        fpm_scoreboard_proc_release(proc);
                    172: }
                    173: /* }}} */
                    174: 
                    175: void fpm_request_end(TSRMLS_D) /* {{{ */
                    176: {
                    177:        struct fpm_scoreboard_proc_s *proc;
                    178:        struct timeval now;
                    179: #ifdef HAVE_TIMES
                    180:        struct tms cpu;
                    181: #endif
                    182:        size_t memory = zend_memory_peak_usage(1 TSRMLS_CC);
                    183: 
                    184:        fpm_clock_get(&now);
                    185: #ifdef HAVE_TIMES
                    186:        times(&cpu);
                    187: #endif
                    188: 
                    189:        proc = fpm_scoreboard_proc_acquire(NULL, -1, 0);
                    190:        if (proc == NULL) {
                    191:                zlog(ZLOG_WARNING, "failed to acquire proc scoreboard");
                    192:                return;
                    193:        }
                    194:        proc->request_stage = FPM_REQUEST_FINISHED;
                    195:        proc->tv = now;
                    196:        timersub(&now, &proc->accepted, &proc->duration);
                    197: #ifdef HAVE_TIMES
                    198:        timersub(&proc->tv, &proc->accepted, &proc->cpu_duration);
                    199:        proc->last_request_cpu.tms_utime = cpu.tms_utime - proc->cpu_accepted.tms_utime;
                    200:        proc->last_request_cpu.tms_stime = cpu.tms_stime - proc->cpu_accepted.tms_stime;
                    201:        proc->last_request_cpu.tms_cutime = cpu.tms_cutime - proc->cpu_accepted.tms_cutime;
                    202:        proc->last_request_cpu.tms_cstime = cpu.tms_cstime - proc->cpu_accepted.tms_cstime;
                    203: #endif
                    204:        proc->memory = memory;
                    205:        fpm_scoreboard_proc_release(proc);
                    206: }
                    207: /* }}} */
                    208: 
                    209: void fpm_request_finished() /* {{{ */
                    210: {
                    211:        struct fpm_scoreboard_proc_s *proc;
                    212:        struct timeval now;
                    213: 
                    214:        fpm_clock_get(&now);
                    215: 
                    216:        proc = fpm_scoreboard_proc_acquire(NULL, -1, 0);
                    217:        if (proc == NULL) {
                    218:                zlog(ZLOG_WARNING, "failed to acquire proc scoreboard");
                    219:                return;
                    220:        }
                    221: 
                    222:        proc->request_stage = FPM_REQUEST_FINISHED;
                    223:        proc->tv = now;
                    224:        memset(&proc->accepted, 0, sizeof(proc->accepted));
                    225:        proc->accepted_epoch = 0;
                    226:        fpm_scoreboard_proc_release(proc);
                    227: }
                    228: /* }}} */
                    229: 
                    230: void fpm_request_check_timed_out(struct fpm_child_s *child, struct timeval *now, int terminate_timeout, int slowlog_timeout) /* {{{ */
                    231: {
                    232:        struct fpm_scoreboard_proc_s proc, *proc_p;
                    233: 
                    234:        proc_p = fpm_scoreboard_proc_acquire(child->wp->scoreboard, child->scoreboard_i, 1);
                    235:        if (!proc_p) {
                    236:                zlog(ZLOG_WARNING, "failed to acquire scoreboard");
                    237:                return;
                    238:        }
                    239: 
                    240:        proc = *proc_p;
                    241:        fpm_scoreboard_proc_release(proc_p);
                    242: 
                    243: #if HAVE_FPM_TRACE
                    244:        if (child->slow_logged.tv_sec) {
                    245:                if (child->slow_logged.tv_sec != proc.accepted.tv_sec || child->slow_logged.tv_usec != proc.accepted.tv_usec) {
                    246:                        child->slow_logged.tv_sec = 0;
                    247:                        child->slow_logged.tv_usec = 0;
                    248:                }
                    249:        }
                    250: #endif
                    251: 
                    252:        if (proc.request_stage > FPM_REQUEST_ACCEPTING && proc.request_stage < FPM_REQUEST_END) {
                    253:                char purified_script_filename[sizeof(proc.script_filename)];
                    254:                struct timeval tv;
                    255: 
                    256:                timersub(now, &proc.accepted, &tv);
                    257: 
                    258: #if HAVE_FPM_TRACE
                    259:                if (child->slow_logged.tv_sec == 0 && slowlog_timeout &&
                    260:                                proc.request_stage == FPM_REQUEST_EXECUTING && tv.tv_sec >= slowlog_timeout) {
                    261:                        
                    262:                        str_purify_filename(purified_script_filename, proc.script_filename, sizeof(proc.script_filename));
                    263: 
                    264:                        child->slow_logged = proc.accepted;
                    265:                        child->tracer = fpm_php_trace;
                    266: 
                    267:                        fpm_trace_signal(child->pid);
                    268: 
                    269:                        zlog(ZLOG_WARNING, "[pool %s] child %d, script '%s' (request: \"%s %s\") executing too slow (%d.%06d sec), logging",
                    270:                                child->wp->config->name, (int) child->pid, purified_script_filename, proc.request_method, proc.request_uri,
                    271:                                (int) tv.tv_sec, (int) tv.tv_usec);
                    272:                }
                    273:                else
                    274: #endif
                    275:                if (terminate_timeout && tv.tv_sec >= terminate_timeout) {
                    276:                        str_purify_filename(purified_script_filename, proc.script_filename, sizeof(proc.script_filename));
                    277:                        fpm_pctl_kill(child->pid, FPM_PCTL_TERM);
                    278: 
                    279:                        zlog(ZLOG_WARNING, "[pool %s] child %d, script '%s' (request: \"%s %s\") execution timed out (%d.%06d sec), terminating",
                    280:                                child->wp->config->name, (int) child->pid, purified_script_filename, proc.request_method, proc.request_uri,
                    281:                                (int) tv.tv_sec, (int) tv.tv_usec);
                    282:                }
                    283:        }
                    284: }
                    285: /* }}} */
                    286: 
                    287: int fpm_request_is_idle(struct fpm_child_s *child) /* {{{ */
                    288: {
                    289:        struct fpm_scoreboard_proc_s *proc;
                    290: 
                    291:        /* no need in atomicity here */
                    292:        proc = fpm_scoreboard_proc_get(child->wp->scoreboard, child->scoreboard_i);
                    293:        if (!proc) {
                    294:                return 0;
                    295:        }
                    296: 
                    297:        return proc->request_stage == FPM_REQUEST_ACCEPTING;
                    298: }
                    299: /* }}} */
                    300: 
                    301: int fpm_request_last_activity(struct fpm_child_s *child, struct timeval *tv) /* {{{ */
                    302: {
                    303:        struct fpm_scoreboard_proc_s *proc;
                    304: 
                    305:        if (!tv) return -1;
                    306: 
                    307:        proc = fpm_scoreboard_proc_get(child->wp->scoreboard, child->scoreboard_i);
                    308:        if (!proc) {
                    309:                return -1;
                    310:        }
                    311: 
                    312:        *tv = proc->tv;
                    313: 
                    314:        return 1;
                    315: }
                    316: /* }}} */

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>