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