Skip to content

Add PHP-FPM memory peak to scoreboard #14153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sapi/fpm/fpm/fpm_php_trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ static int fpm_php_trace_dump(struct fpm_child_s *child, FILE *slowlog) /* {{{ *

void fpm_php_trace(struct fpm_child_s *child) /* {{{ */
{
fpm_scoreboard_update(0, 0, 0, 0, 0, 0, 1, FPM_SCOREBOARD_ACTION_INC, child->wp->scoreboard);
fpm_scoreboard_update(0, 0, 0, 0, 0, 0, 1, 0, FPM_SCOREBOARD_ACTION_INC, child->wp->scoreboard);
FILE *slowlog;

zlog(ZLOG_NOTICE, "about to trace %d", (int) child->pid);
Expand Down
8 changes: 4 additions & 4 deletions sapi/fpm/fpm/fpm_process_ctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ static void fpm_pctl_perform_idle_server_maintenance(struct timeval *now) /* {{{
}
}

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

/* this is specific to PM_STYLE_ONDEMAND */
if (wp->config->pm == PM_STYLE_ONDEMAND) {
Expand Down Expand Up @@ -406,7 +406,7 @@ static void fpm_pctl_perform_idle_server_maintenance(struct timeval *now) /* {{{
if (idle < wp->config->pm_min_spare_servers) {
if (wp->running_children >= wp->config->pm_max_children) {
if (!wp->warn_max_children && !wp->shared) {
fpm_scoreboard_update(0, 0, 0, 0, 0, 1, 0, FPM_SCOREBOARD_ACTION_INC, wp->scoreboard);
fpm_scoreboard_update(0, 0, 0, 0, 0, 1, 0, 0, FPM_SCOREBOARD_ACTION_INC, wp->scoreboard);
zlog(ZLOG_WARNING, "[pool %s] server reached pm.max_children setting (%d), consider raising it", wp->config->name, wp->config->pm_max_children);
wp->warn_max_children = 1;
}
Expand All @@ -425,7 +425,7 @@ static void fpm_pctl_perform_idle_server_maintenance(struct timeval *now) /* {{{
children_to_fork = MIN(children_to_fork, wp->config->pm_max_children - wp->running_children);
if (children_to_fork <= 0) {
if (!wp->warn_max_children && !wp->shared) {
fpm_scoreboard_update(0, 0, 0, 0, 0, 1, 0, FPM_SCOREBOARD_ACTION_INC, wp->scoreboard);
fpm_scoreboard_update(0, 0, 0, 0, 0, 1, 0, 0, FPM_SCOREBOARD_ACTION_INC, wp->scoreboard);
zlog(ZLOG_WARNING, "[pool %s] server reached pm.max_children setting (%d), consider raising it", wp->config->name, wp->config->pm_max_children);
wp->warn_max_children = 1;
}
Expand Down Expand Up @@ -529,7 +529,7 @@ void fpm_pctl_on_socket_accept(struct fpm_event_s *ev, short which, void *arg) /

if (wp->running_children >= wp->config->pm_max_children) {
if (!wp->warn_max_children && !wp->shared) {
fpm_scoreboard_update(0, 0, 0, 0, 0, 1, 0, FPM_SCOREBOARD_ACTION_INC, wp->scoreboard);
fpm_scoreboard_update(0, 0, 0, 0, 0, 1, 0, 0, FPM_SCOREBOARD_ACTION_INC, wp->scoreboard);
zlog(ZLOG_WARNING, "[pool %s] server reached max_children setting (%d), consider raising it", wp->config->name, wp->config->pm_max_children);
wp->warn_max_children = 1;
}
Expand Down
7 changes: 5 additions & 2 deletions sapi/fpm/fpm/fpm_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void fpm_request_accepting(void)
fpm_scoreboard_proc_release(proc);

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

void fpm_request_reading_headers(void)
Expand Down Expand Up @@ -98,7 +98,7 @@ void fpm_request_reading_headers(void)
fpm_scoreboard_proc_release(proc);

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

void fpm_request_info(void)
Expand Down Expand Up @@ -199,6 +199,9 @@ void fpm_request_end(void)
#endif
proc->memory = memory;
fpm_scoreboard_proc_release(proc);

/* memory_peak */
fpm_scoreboard_update_commit(0, 0, 0, 0, 0, 0, 0, proc->memory, FPM_SCOREBOARD_ACTION_SET, NULL);
}

void fpm_request_finished(void)
Expand Down
9 changes: 6 additions & 3 deletions sapi/fpm/fpm/fpm_scoreboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ void fpm_scoreboard_update_begin(struct fpm_scoreboard_s *scoreboard) /* {{{ */

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) /* {{{ */
int slow_rq, size_t memory_peak, int action, struct fpm_scoreboard_s *scoreboard) /* {{{ */
{
scoreboard = fpm_scoreboard_get_for_update(scoreboard);
if (!scoreboard) {
Expand Down Expand Up @@ -169,6 +169,9 @@ void fpm_scoreboard_update_commit(
if (scoreboard->active > scoreboard->active_max) {
scoreboard->active_max = scoreboard->active;
}
if (scoreboard->memory_peak < memory_peak) {
scoreboard->memory_peak = memory_peak;
}

fpm_unlock(scoreboard->lock);
}
Expand All @@ -177,11 +180,11 @@ void fpm_scoreboard_update_commit(

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) /* {{{ */
int slow_rq, size_t memory_peak, int action, struct fpm_scoreboard_s *scoreboard) /* {{{ */
{
fpm_scoreboard_update_begin(scoreboard);
fpm_scoreboard_update_commit(
idle, active, lq, lq_len, requests, max_children_reached, slow_rq, action, scoreboard);
idle, active, lq, lq_len, requests, max_children_reached, slow_rq, memory_peak, action, scoreboard);
}
/* }}} */

Expand Down
5 changes: 3 additions & 2 deletions sapi/fpm/fpm/fpm_scoreboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ struct fpm_scoreboard_s {
unsigned int nprocs;
int free_proc;
unsigned long int slow_rq;
size_t memory_peak;
struct fpm_scoreboard_s *shared;
struct fpm_scoreboard_proc_s procs[] ZEND_ELEMENT_COUNT(nprocs);
};
Expand All @@ -74,8 +75,8 @@ int fpm_scoreboard_init_main(void);
int fpm_scoreboard_init_child(struct fpm_worker_pool_s *wp);

void fpm_scoreboard_update_begin(struct fpm_scoreboard_s *scoreboard);
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);
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);
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);
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);

struct fpm_scoreboard_s *fpm_scoreboard_get(void);
struct fpm_scoreboard_proc_s *fpm_scoreboard_proc_get(struct fpm_scoreboard_s *scoreboard, int child_index);
Expand Down
2 changes: 1 addition & 1 deletion sapi/fpm/fpm/fpm_sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ int fpm_sockets_init_main(void)
}

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

Expand Down
20 changes: 15 additions & 5 deletions sapi/fpm/fpm/fpm_status.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ int fpm_status_export_to_zval(zval *status)
add_assoc_long(status, "max-active-processes", scoreboard.active_max);
add_assoc_long(status, "max-children-reached", scoreboard.max_children_reached);
add_assoc_long(status, "slow-requests", scoreboard.slow_rq);
add_assoc_long(status, "memory-peak", scoreboard.memory_peak);

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

if (!full) {
Expand Down Expand Up @@ -309,7 +311,8 @@ int fpm_status_handle_request(void) /* {{{ */
"<total-processes>%d</total-processes>\n"
"<max-active-processes>%d</max-active-processes>\n"
"<max-children-reached>%u</max-children-reached>\n"
"<slow-requests>%lu</slow-requests>\n";
"<slow-requests>%lu</slow-requests>\n"
"<memory-peak>%zu</memory-peak>\n";

if (!full) {
short_post = "</status>";
Expand Down Expand Up @@ -357,7 +360,8 @@ int fpm_status_handle_request(void) /* {{{ */
"\"total processes\":%d,"
"\"max active processes\":%d,"
"\"max children reached\":%u,"
"\"slow requests\":%lu";
"\"slow requests\":%lu,"
"\"memory peak\":%zu";

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

has_start_time = 0;
Expand Down Expand Up @@ -457,7 +464,8 @@ int fpm_status_handle_request(void) /* {{{ */
"total processes: %d\n"
"max active processes: %d\n"
"max children reached: %u\n"
"slow requests: %lu\n";
"slow requests: %lu\n"
"memory peak: %zu\n";

if (full) {
full_syntax =
Expand Down Expand Up @@ -496,7 +504,8 @@ int fpm_status_handle_request(void) /* {{{ */
scoreboard_p->idle + scoreboard_p->active,
scoreboard_p->active_max,
scoreboard_p->max_children_reached,
scoreboard_p->slow_rq);
scoreboard_p->slow_rq,
scoreboard_p->memory_peak);
} else {
spprintf(&buffer, 0, short_syntax,
scoreboard_p->pool,
Expand All @@ -511,7 +520,8 @@ int fpm_status_handle_request(void) /* {{{ */
scoreboard_p->idle + scoreboard_p->active,
scoreboard_p->active_max,
scoreboard_p->max_children_reached,
scoreboard_p->slow_rq);
scoreboard_p->slow_rq,
scoreboard_p->memory_peak);
}

PUTS(buffer);
Expand Down
4 changes: 3 additions & 1 deletion sapi/fpm/tests/fpm_get_status_basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ $tester->close();
Done
--EXPECTF--
Test Start
array(15) {
array(16) {
["pool"]=>
string(10) "unconfined"
["process-manager"]=>
Expand Down Expand Up @@ -68,6 +68,8 @@ array(15) {
int(0)
["slow-requests"]=>
int(0)
["memory-peak"]=>
int(0)
["procs"]=>
array(1) {
[0]=>
Expand Down
4 changes: 4 additions & 0 deletions sapi/fpm/tests/status.inc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Status
'max active processes' => '\d+',
'max children reached' => '\d+',
'slow requests' => '\d+',
'memory peak' => '\d+',
];

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

if (!preg_match($pattern, $body)) {
Expand Down
Loading