php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
fpm_request.c
Go to the documentation of this file.
1 /* (c) 2007,2008 Andrei Nigmatulin */
2#ifdef HAVE_TIMES
3#include <sys/times.h>
4#endif
5
6#include "fpm_config.h"
7
8#include "fpm.h"
9#include "fpm_php.h"
10#include "fpm_str.h"
11#include "fpm_clock.h"
12#include "fpm_conf.h"
13#include "fpm_trace.h"
14#include "fpm_php_trace.h"
15#include "fpm_process_ctl.h"
16#include "fpm_children.h"
17#include "fpm_scoreboard.h"
18#include "fpm_status.h"
19#include "fpm_request.h"
20#include "fpm_log.h"
21
22#include "zlog.h"
23
24static const char *requests_stages[] = {
25 [FPM_REQUEST_ACCEPTING] = "Idle",
26 [FPM_REQUEST_READING_HEADERS] = "Reading headers",
27 [FPM_REQUEST_INFO] = "Getting request information",
28 [FPM_REQUEST_EXECUTING] = "Running",
29 [FPM_REQUEST_END] = "Ending",
30 [FPM_REQUEST_FINISHED] = "Finishing",
31};
32
33const char *fpm_request_get_stage_name(int stage) {
34 return requests_stages[stage];
35}
36
38{
39 struct fpm_scoreboard_proc_s *proc;
40 struct timeval now;
41
43
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
53 proc->tv = now;
55
56 /* idle++, active-- */
58}
59
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
71 now_epoch = time(NULL);
72#ifdef HAVE_TIMES
73 times(&cpu);
74#endif
75
77
78 proc = fpm_scoreboard_proc_acquire(NULL, -1, 0);
79 if (proc == NULL) {
80 zlog(ZLOG_WARNING, "failed to acquire proc scoreboard");
81 return;
82 }
83
85 proc->tv = now;
86 proc->accepted = now;
87 proc->accepted_epoch = now_epoch;
88#ifdef HAVE_TIMES
89 proc->cpu_accepted = cpu;
90#endif
91 proc->requests++;
92 proc->request_uri[0] = '\0';
93 proc->request_method[0] = '\0';
94 proc->script_filename[0] = '\0';
95 proc->query_string[0] = '\0';
96 proc->auth_user[0] = '\0';
97 proc->content_length = 0;
99
100 /* idle--, active++, request++ */
102}
103
105{
106 struct fpm_scoreboard_proc_s *proc;
109 char *script_filename = fpm_php_script_filename();
113 struct timeval now;
114
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
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
151}
152
154{
155 struct fpm_scoreboard_proc_s *proc;
156 struct timeval now;
157
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
167 proc->tv = now;
169}
170
172{
173 struct fpm_scoreboard_proc_s *proc;
174 struct timeval now;
175#ifdef HAVE_TIMES
176 struct tms cpu;
177#endif
178 size_t memory = zend_memory_peak_usage(1);
179
181#ifdef HAVE_TIMES
182 times(&cpu);
183#endif
184
185 proc = fpm_scoreboard_proc_acquire(NULL, -1, 0);
186 if (proc == NULL) {
187 zlog(ZLOG_WARNING, "failed to acquire proc scoreboard");
188 return;
189 }
191 proc->tv = now;
192 timersub(&now, &proc->accepted, &proc->duration);
193#ifdef HAVE_TIMES
194 timersub(&proc->tv, &proc->accepted, &proc->cpu_duration);
195 proc->last_request_cpu.tms_utime = cpu.tms_utime - proc->cpu_accepted.tms_utime;
196 proc->last_request_cpu.tms_stime = cpu.tms_stime - proc->cpu_accepted.tms_stime;
197 proc->last_request_cpu.tms_cutime = cpu.tms_cutime - proc->cpu_accepted.tms_cutime;
198 proc->last_request_cpu.tms_cstime = cpu.tms_cstime - proc->cpu_accepted.tms_cstime;
199#endif
200 proc->memory = memory;
202
203 /* memory_peak */
204 fpm_scoreboard_update_commit(-1, -1, -1, -1, -1, -1, -1, proc->memory, FPM_SCOREBOARD_ACTION_SET, NULL);
205}
206
208{
209 struct fpm_scoreboard_proc_s *proc;
210 struct timeval now;
211
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
221 proc->tv = now;
223}
224
225void fpm_request_check_timed_out(struct fpm_child_s *child, struct timeval *now, int terminate_timeout, int slowlog_timeout, int track_finished) /* {{{ */
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_NOTICE, "failed to acquire scoreboard");
232 return;
233 }
234
235 proc = *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) || track_finished)) {
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));
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
284int fpm_request_is_idle(struct fpm_child_s *child) /* {{{ */
285{
286 struct fpm_scoreboard_proc_s *proc;
287
288 /* no need in atomicity here */
290 if (!proc) {
291 return 0;
292 }
293
294 return proc->request_stage == FPM_REQUEST_ACCEPTING;
295}
296/* }}} */
297
298int 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
305 if (!proc) {
306 return -1;
307 }
308
309 *tv = proc->tv;
310
311 return 1;
312}
313/* }}} */
int fpm_clock_get(struct timeval *tv)
Definition fpm_clock.c:110
#define timersub(tvp, uvp, vvp)
Definition fpm_config.h:36
char * fpm_php_script_filename(void)
Definition fpm_php.c:191
size_t fpm_php_content_length(void)
Definition fpm_php.c:216
char * fpm_php_request_method(void)
Definition fpm_php.c:201
char * fpm_php_query_string(void)
Definition fpm_php.c:206
char * fpm_php_request_uri(void)
Definition fpm_php.c:196
char * fpm_php_auth_user(void)
Definition fpm_php.c:211
void fpm_php_trace(struct fpm_child_s *)
int fpm_pctl_kill(pid_t pid, int how)
@ FPM_PCTL_TERM
void fpm_request_accepting(void)
Definition fpm_request.c:37
void fpm_request_finished(void)
void fpm_request_info(void)
void fpm_request_reading_headers(void)
Definition fpm_request.c:60
void fpm_request_end(void)
void fpm_request_check_timed_out(struct fpm_child_s *child, struct timeval *now, int terminate_timeout, int slowlog_timeout, int track_finished)
void fpm_request_executing(void)
int fpm_request_last_activity(struct fpm_child_s *child, struct timeval *tv)
int fpm_request_is_idle(struct fpm_child_s *child)
const char * fpm_request_get_stage_name(int stage)
Definition fpm_request.c:33
@ FPM_REQUEST_INFO
Definition fpm_request.h:30
@ FPM_REQUEST_READING_HEADERS
Definition fpm_request.h:29
@ FPM_REQUEST_FINISHED
Definition fpm_request.h:33
@ FPM_REQUEST_END
Definition fpm_request.h:32
@ FPM_REQUEST_ACCEPTING
Definition fpm_request.h:28
@ FPM_REQUEST_EXECUTING
Definition fpm_request.h:31
struct fpm_scoreboard_proc_s * fpm_scoreboard_proc_acquire(struct fpm_scoreboard_s *scoreboard, int child_index, int nohang)
struct fpm_scoreboard_proc_s * fpm_scoreboard_proc_get_from_child(struct fpm_child_s *child)
void fpm_scoreboard_update_begin(struct fpm_scoreboard_s *scoreboard)
void fpm_scoreboard_proc_release(struct fpm_scoreboard_proc_s *proc)
void fpm_scoreboard_update_commit(int idle, int active, int lq, int lq_len, int requests, int max_children_reached, int slow_rq, size_t memory_peak, int action, struct fpm_scoreboard_s *scoreboard)
#define FPM_SCOREBOARD_ACTION_INC
#define FPM_SCOREBOARD_ACTION_SET
int fpm_trace_signal(pid_t pid)
#define NULL
Definition gdcache.h:45
#define strlcpy
Definition php.h:159
time()
struct timeval tv
Definition session.c:1280
time_t now
Definition session.c:1281
void(* tracer)(struct fpm_child_s *)
struct timeval slow_logged
struct fpm_worker_pool_s * wp
struct timeval accepted
struct timeval duration
enum fpm_request_stage_e request_stage
struct fpm_worker_pool_config_s * config
struct fpm_scoreboard_s * scoreboard
ZEND_API size_t zend_memory_peak_usage(bool real_usage)
@ ZLOG_NOTICE
Definition zlog.h:43
@ ZLOG_WARNING
Definition zlog.h:44
#define zlog(flags,...)
Definition zlog.h:9