Skip to content

Commit 67aac59

Browse files
flaviohelenobukka
authored andcommitted
Add PHP-FPM memory peak to the scoreboard
Closes #14153
1 parent a092bcb commit 67aac59

11 files changed

+46
-19
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ PHP NEWS
2525
. Fixed bug GH-15551 (Segmentation fault (access null pointer) in
2626
ext/dom/xml_common.h). (nielsdos)
2727

28+
- FPM:
29+
. Added memory peak to the scoreboard / status page. (Flávio Heleno)
30+
2831
- MySQLnd:
2932
. Fixed bug GH-15432 (Heap corruption when querying a vector). (cmb,
3033
Kamil Tekiela)

UPGRADING

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ PHP 8.4 UPGRADE NOTES
290290

291291
- FPM:
292292
. Flushing headers without a body will now succeed. See GH-12785.
293+
. Status page has a new field to display a memory peak.
293294

294295
- Hash:
295296
. Added HashContext::__debugInfo().

sapi/fpm/fpm/fpm_php_trace.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ static int fpm_php_trace_dump(struct fpm_child_s *child, FILE *slowlog) /* {{{ *
189189

190190
void fpm_php_trace(struct fpm_child_s *child) /* {{{ */
191191
{
192-
fpm_scoreboard_update(0, 0, 0, 0, 0, 0, 1, FPM_SCOREBOARD_ACTION_INC, child->wp->scoreboard);
192+
fpm_scoreboard_update(0, 0, 0, 0, 0, 0, 1, 0, FPM_SCOREBOARD_ACTION_INC, child->wp->scoreboard);
193193
FILE *slowlog;
194194

195195
zlog(ZLOG_NOTICE, "about to trace %d", (int) child->pid);

sapi/fpm/fpm/fpm_process_ctl.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ static void fpm_pctl_perform_idle_server_maintenance(struct timeval *now) /* {{{
373373
}
374374
}
375375

376-
fpm_scoreboard_update_commit(idle, active, cur_lq, -1, -1, -1, 0, FPM_SCOREBOARD_ACTION_SET, wp->scoreboard);
376+
fpm_scoreboard_update_commit(idle, active, cur_lq, -1, -1, -1, 0, 0, FPM_SCOREBOARD_ACTION_SET, wp->scoreboard);
377377

378378
/* this is specific to PM_STYLE_ONDEMAND */
379379
if (wp->config->pm == PM_STYLE_ONDEMAND) {
@@ -406,7 +406,7 @@ static void fpm_pctl_perform_idle_server_maintenance(struct timeval *now) /* {{{
406406
if (idle < wp->config->pm_min_spare_servers) {
407407
if (wp->running_children >= wp->config->pm_max_children) {
408408
if (!wp->warn_max_children && !wp->shared) {
409-
fpm_scoreboard_update(0, 0, 0, 0, 0, 1, 0, FPM_SCOREBOARD_ACTION_INC, wp->scoreboard);
409+
fpm_scoreboard_update(0, 0, 0, 0, 0, 1, 0, 0, FPM_SCOREBOARD_ACTION_INC, wp->scoreboard);
410410
zlog(ZLOG_WARNING, "[pool %s] server reached pm.max_children setting (%d), consider raising it", wp->config->name, wp->config->pm_max_children);
411411
wp->warn_max_children = 1;
412412
}
@@ -425,7 +425,7 @@ static void fpm_pctl_perform_idle_server_maintenance(struct timeval *now) /* {{{
425425
children_to_fork = MIN(children_to_fork, wp->config->pm_max_children - wp->running_children);
426426
if (children_to_fork <= 0) {
427427
if (!wp->warn_max_children && !wp->shared) {
428-
fpm_scoreboard_update(0, 0, 0, 0, 0, 1, 0, FPM_SCOREBOARD_ACTION_INC, wp->scoreboard);
428+
fpm_scoreboard_update(0, 0, 0, 0, 0, 1, 0, 0, FPM_SCOREBOARD_ACTION_INC, wp->scoreboard);
429429
zlog(ZLOG_WARNING, "[pool %s] server reached pm.max_children setting (%d), consider raising it", wp->config->name, wp->config->pm_max_children);
430430
wp->warn_max_children = 1;
431431
}
@@ -529,7 +529,7 @@ void fpm_pctl_on_socket_accept(struct fpm_event_s *ev, short which, void *arg) /
529529

530530
if (wp->running_children >= wp->config->pm_max_children) {
531531
if (!wp->warn_max_children && !wp->shared) {
532-
fpm_scoreboard_update(0, 0, 0, 0, 0, 1, 0, FPM_SCOREBOARD_ACTION_INC, wp->scoreboard);
532+
fpm_scoreboard_update(0, 0, 0, 0, 0, 1, 0, 0, FPM_SCOREBOARD_ACTION_INC, wp->scoreboard);
533533
zlog(ZLOG_WARNING, "[pool %s] server reached max_children setting (%d), consider raising it", wp->config->name, wp->config->pm_max_children);
534534
wp->warn_max_children = 1;
535535
}

sapi/fpm/fpm/fpm_request.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void fpm_request_accepting(void)
5454
fpm_scoreboard_proc_release(proc);
5555

5656
/* idle++, active-- */
57-
fpm_scoreboard_update_commit(1, -1, 0, 0, 0, 0, 0, FPM_SCOREBOARD_ACTION_INC, NULL);
57+
fpm_scoreboard_update_commit(1, -1, 0, 0, 0, 0, 0, 0, FPM_SCOREBOARD_ACTION_INC, NULL);
5858
}
5959

6060
void fpm_request_reading_headers(void)
@@ -98,7 +98,7 @@ void fpm_request_reading_headers(void)
9898
fpm_scoreboard_proc_release(proc);
9999

100100
/* idle--, active++, request++ */
101-
fpm_scoreboard_update_commit(-1, 1, 0, 0, 1, 0, 0, FPM_SCOREBOARD_ACTION_INC, NULL);
101+
fpm_scoreboard_update_commit(-1, 1, 0, 0, 1, 0, 0, 0, FPM_SCOREBOARD_ACTION_INC, NULL);
102102
}
103103

104104
void fpm_request_info(void)
@@ -199,6 +199,9 @@ void fpm_request_end(void)
199199
#endif
200200
proc->memory = memory;
201201
fpm_scoreboard_proc_release(proc);
202+
203+
/* memory_peak */
204+
fpm_scoreboard_update_commit(0, 0, 0, 0, 0, 0, 0, proc->memory, FPM_SCOREBOARD_ACTION_SET, NULL);
202205
}
203206

204207
void fpm_request_finished(void)

sapi/fpm/fpm/fpm_scoreboard.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ void fpm_scoreboard_update_begin(struct fpm_scoreboard_s *scoreboard) /* {{{ */
9999

100100
void fpm_scoreboard_update_commit(
101101
int idle, int active, int lq, int lq_len, int requests, int max_children_reached,
102-
int slow_rq, int action, struct fpm_scoreboard_s *scoreboard) /* {{{ */
102+
int slow_rq, size_t memory_peak, int action, struct fpm_scoreboard_s *scoreboard) /* {{{ */
103103
{
104104
scoreboard = fpm_scoreboard_get_for_update(scoreboard);
105105
if (!scoreboard) {
@@ -169,6 +169,9 @@ void fpm_scoreboard_update_commit(
169169
if (scoreboard->active > scoreboard->active_max) {
170170
scoreboard->active_max = scoreboard->active;
171171
}
172+
if (scoreboard->memory_peak < memory_peak) {
173+
scoreboard->memory_peak = memory_peak;
174+
}
172175

173176
fpm_unlock(scoreboard->lock);
174177
}
@@ -177,11 +180,11 @@ void fpm_scoreboard_update_commit(
177180

178181
void fpm_scoreboard_update(
179182
int idle, int active, int lq, int lq_len, int requests, int max_children_reached,
180-
int slow_rq, int action, struct fpm_scoreboard_s *scoreboard) /* {{{ */
183+
int slow_rq, size_t memory_peak, int action, struct fpm_scoreboard_s *scoreboard) /* {{{ */
181184
{
182185
fpm_scoreboard_update_begin(scoreboard);
183186
fpm_scoreboard_update_commit(
184-
idle, active, lq, lq_len, requests, max_children_reached, slow_rq, action, scoreboard);
187+
idle, active, lq, lq_len, requests, max_children_reached, slow_rq, memory_peak, action, scoreboard);
185188
}
186189
/* }}} */
187190

sapi/fpm/fpm/fpm_scoreboard.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ struct fpm_scoreboard_s {
6666
unsigned int nprocs;
6767
int free_proc;
6868
unsigned long int slow_rq;
69+
size_t memory_peak;
6970
struct fpm_scoreboard_s *shared;
7071
struct fpm_scoreboard_proc_s procs[] ZEND_ELEMENT_COUNT(nprocs);
7172
};
@@ -74,8 +75,8 @@ int fpm_scoreboard_init_main(void);
7475
int fpm_scoreboard_init_child(struct fpm_worker_pool_s *wp);
7576

7677
void fpm_scoreboard_update_begin(struct fpm_scoreboard_s *scoreboard);
77-
void fpm_scoreboard_update_commit(int idle, int active, int lq, int lq_len, int requests, int max_children_reached, int slow_rq, int action, struct fpm_scoreboard_s *scoreboard);
78-
void fpm_scoreboard_update(int idle, int active, int lq, int lq_len, int requests, int max_children_reached, int slow_rq, int action, struct fpm_scoreboard_s *scoreboard);
78+
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_max, int action, struct fpm_scoreboard_s *scoreboard);
79+
void fpm_scoreboard_update(int idle, int active, int lq, int lq_len, int requests, int max_children_reached, int slow_rq, size_t memory_max, int action, struct fpm_scoreboard_s *scoreboard);
7980

8081
struct fpm_scoreboard_s *fpm_scoreboard_get(void);
8182
struct fpm_scoreboard_proc_s *fpm_scoreboard_proc_get(struct fpm_scoreboard_s *scoreboard, int child_index);

sapi/fpm/fpm/fpm_sockets.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ int fpm_sockets_init_main(void)
514514
}
515515

516516
if (wp->listen_address_domain == FPM_AF_INET && fpm_socket_get_listening_queue(wp->listening_socket, NULL, &lq_len) >= 0) {
517-
fpm_scoreboard_update(-1, -1, -1, (int)lq_len, -1, -1, 0, FPM_SCOREBOARD_ACTION_SET, wp->scoreboard);
517+
fpm_scoreboard_update(-1, -1, -1, (int)lq_len, -1, -1, 0, 0, FPM_SCOREBOARD_ACTION_SET, wp->scoreboard);
518518
}
519519
}
520520

sapi/fpm/fpm/fpm_status.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ int fpm_status_export_to_zval(zval *status)
9494
add_assoc_long(status, "max-active-processes", scoreboard.active_max);
9595
add_assoc_long(status, "max-children-reached", scoreboard.max_children_reached);
9696
add_assoc_long(status, "slow-requests", scoreboard.slow_rq);
97+
add_assoc_long(status, "memory-peak", scoreboard.memory_peak);
9798

9899
array_init(&fpm_proc_stats);
99100
for(i=0; i<scoreboard.nprocs; i++) {
@@ -244,6 +245,7 @@ int fpm_status_handle_request(void) /* {{{ */
244245
"<tr><th>max active processes</th><td>%d</td></tr>\n"
245246
"<tr><th>max children reached</th><td>%u</td></tr>\n"
246247
"<tr><th>slow requests</th><td>%lu</td></tr>\n"
248+
"<tr><th>memory peak</th><td>%zu</td></tr>\n"
247249
"</table>\n";
248250

249251
if (!full) {
@@ -309,7 +311,8 @@ int fpm_status_handle_request(void) /* {{{ */
309311
"<total-processes>%d</total-processes>\n"
310312
"<max-active-processes>%d</max-active-processes>\n"
311313
"<max-children-reached>%u</max-children-reached>\n"
312-
"<slow-requests>%lu</slow-requests>\n";
314+
"<slow-requests>%lu</slow-requests>\n"
315+
"<memory-peak>%zu</memory-peak>\n";
313316

314317
if (!full) {
315318
short_post = "</status>";
@@ -357,7 +360,8 @@ int fpm_status_handle_request(void) /* {{{ */
357360
"\"total processes\":%d,"
358361
"\"max active processes\":%d,"
359362
"\"max children reached\":%u,"
360-
"\"slow requests\":%lu";
363+
"\"slow requests\":%lu,"
364+
"\"memory peak\":%zu";
361365

362366
if (!full) {
363367
short_post = "}";
@@ -426,6 +430,9 @@ int fpm_status_handle_request(void) /* {{{ */
426430
"# HELP phpfpm_slow_requests The number of requests that exceeded your 'request_slowlog_timeout' value.\n"
427431
"# TYPE phpfpm_slow_requests counter\n"
428432
"phpfpm_slow_requests %lu\n"
433+
"# HELP phpfpm_memory_peak The memory usage peak since FPM has started.\n"
434+
"# TYPE phpfpm_memory_peak gauge\n"
435+
"phpfpm_memory_peak %zu\n"
429436
"# EOF\n";
430437

431438
has_start_time = 0;
@@ -457,7 +464,8 @@ int fpm_status_handle_request(void) /* {{{ */
457464
"total processes: %d\n"
458465
"max active processes: %d\n"
459466
"max children reached: %u\n"
460-
"slow requests: %lu\n";
467+
"slow requests: %lu\n"
468+
"memory peak: %zu\n";
461469

462470
if (full) {
463471
full_syntax =
@@ -496,7 +504,8 @@ int fpm_status_handle_request(void) /* {{{ */
496504
scoreboard_p->idle + scoreboard_p->active,
497505
scoreboard_p->active_max,
498506
scoreboard_p->max_children_reached,
499-
scoreboard_p->slow_rq);
507+
scoreboard_p->slow_rq,
508+
scoreboard_p->memory_peak);
500509
} else {
501510
spprintf(&buffer, 0, short_syntax,
502511
scoreboard_p->pool,
@@ -511,7 +520,8 @@ int fpm_status_handle_request(void) /* {{{ */
511520
scoreboard_p->idle + scoreboard_p->active,
512521
scoreboard_p->active_max,
513522
scoreboard_p->max_children_reached,
514-
scoreboard_p->slow_rq);
523+
scoreboard_p->slow_rq,
524+
scoreboard_p->memory_peak);
515525
}
516526

517527
PUTS(buffer);

sapi/fpm/tests/fpm_get_status_basic.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ $tester->close();
3939
Done
4040
--EXPECTF--
4141
Test Start
42-
array(15) {
42+
array(16) {
4343
["pool"]=>
4444
string(10) "unconfined"
4545
["process-manager"]=>
@@ -68,6 +68,8 @@ array(15) {
6868
int(0)
6969
["slow-requests"]=>
7070
int(0)
71+
["memory-peak"]=>
72+
int(0)
7173
["procs"]=>
7274
array(1) {
7375
[0]=>

sapi/fpm/tests/status.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class Status
3535
'max active processes' => '\d+',
3636
'max children reached' => '\d+',
3737
'slow requests' => '\d+',
38+
'memory peak' => '\d+',
3839
];
3940

4041
/**
@@ -266,6 +267,9 @@ class Status
266267
"# HELP phpfpm_slow_requests The number of requests that exceeded your 'request_slowlog_timeout' value\.\n" .
267268
"# TYPE phpfpm_slow_requests counter\n" .
268269
"phpfpm_slow_requests " . $fields['slow requests'] . "\n" .
270+
"# HELP phpfpm_memory_peak The memory usage peak since FPM has started\.\n" .
271+
"# TYPE phpfpm_memory_peak gauge\n" .
272+
"phpfpm_memory_peak " . $fields['memory peak'] . "\n" .
269273
"# EOF)\n";
270274

271275
if (!preg_match($pattern, $body)) {

0 commit comments

Comments
 (0)