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
fpm_request_get_stage_name(int stage)35 const char *fpm_request_get_stage_name(int stage) {
36 return requests_stages[stage];
37 }
38
fpm_request_accepting()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
fpm_request_reading_headers()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, 0, FPM_SCOREBOARD_ACTION_INC, NULL);
102 }
103 /* }}} */
104
fpm_request_info()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
fpm_request_executing()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
fpm_request_end(TSRMLS_D)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
fpm_request_finished()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 fpm_scoreboard_proc_release(proc);
225 }
226 /* }}} */
227
fpm_request_check_timed_out(struct fpm_child_s * child,struct timeval * now,int terminate_timeout,int slowlog_timeout)228 void fpm_request_check_timed_out(struct fpm_child_s *child, struct timeval *now, int terminate_timeout, int slowlog_timeout) /* {{{ */
229 {
230 struct fpm_scoreboard_proc_s proc, *proc_p;
231
232 proc_p = fpm_scoreboard_proc_acquire(child->wp->scoreboard, child->scoreboard_i, 1);
233 if (!proc_p) {
234 zlog(ZLOG_WARNING, "failed to acquire scoreboard");
235 return;
236 }
237
238 proc = *proc_p;
239 fpm_scoreboard_proc_release(proc_p);
240
241 #if HAVE_FPM_TRACE
242 if (child->slow_logged.tv_sec) {
243 if (child->slow_logged.tv_sec != proc.accepted.tv_sec || child->slow_logged.tv_usec != proc.accepted.tv_usec) {
244 child->slow_logged.tv_sec = 0;
245 child->slow_logged.tv_usec = 0;
246 }
247 }
248 #endif
249
250 if (proc.request_stage > FPM_REQUEST_ACCEPTING && proc.request_stage < FPM_REQUEST_END) {
251 char purified_script_filename[sizeof(proc.script_filename)];
252 struct timeval tv;
253
254 timersub(now, &proc.accepted, &tv);
255
256 #if HAVE_FPM_TRACE
257 if (child->slow_logged.tv_sec == 0 && slowlog_timeout &&
258 proc.request_stage == FPM_REQUEST_EXECUTING && tv.tv_sec >= slowlog_timeout) {
259
260 str_purify_filename(purified_script_filename, proc.script_filename, sizeof(proc.script_filename));
261
262 child->slow_logged = proc.accepted;
263 child->tracer = fpm_php_trace;
264
265 fpm_trace_signal(child->pid);
266
267 zlog(ZLOG_WARNING, "[pool %s] child %d, script '%s' (request: \"%s %s\") executing too slow (%d.%06d sec), logging",
268 child->wp->config->name, (int) child->pid, purified_script_filename, proc.request_method, proc.request_uri,
269 (int) tv.tv_sec, (int) tv.tv_usec);
270 }
271 else
272 #endif
273 if (terminate_timeout && tv.tv_sec >= terminate_timeout) {
274 str_purify_filename(purified_script_filename, proc.script_filename, sizeof(proc.script_filename));
275 fpm_pctl_kill(child->pid, FPM_PCTL_TERM);
276
277 zlog(ZLOG_WARNING, "[pool %s] child %d, script '%s' (request: \"%s %s\") execution timed out (%d.%06d sec), terminating",
278 child->wp->config->name, (int) child->pid, purified_script_filename, proc.request_method, proc.request_uri,
279 (int) tv.tv_sec, (int) tv.tv_usec);
280 }
281 }
282 }
283 /* }}} */
284
fpm_request_is_idle(struct fpm_child_s * child)285 int fpm_request_is_idle(struct fpm_child_s *child) /* {{{ */
286 {
287 struct fpm_scoreboard_proc_s *proc;
288
289 /* no need in atomicity here */
290 proc = fpm_scoreboard_proc_get(child->wp->scoreboard, child->scoreboard_i);
291 if (!proc) {
292 return 0;
293 }
294
295 return proc->request_stage == FPM_REQUEST_ACCEPTING;
296 }
297 /* }}} */
298
fpm_request_last_activity(struct fpm_child_s * child,struct timeval * tv)299 int fpm_request_last_activity(struct fpm_child_s *child, struct timeval *tv) /* {{{ */
300 {
301 struct fpm_scoreboard_proc_s *proc;
302
303 if (!tv) return -1;
304
305 proc = fpm_scoreboard_proc_get(child->wp->scoreboard, child->scoreboard_i);
306 if (!proc) {
307 return -1;
308 }
309
310 *tv = proc->tv;
311
312 return 1;
313 }
314 /* }}} */
315