Skip to content

Commit

Permalink
status and ping online feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Jérôme Loyet committed Dec 21, 2009
1 parent 2d79ec1 commit 7598007
Show file tree
Hide file tree
Showing 14 changed files with 412 additions and 4 deletions.
1 change: 1 addition & 0 deletions sapi/fpm/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,7 @@ if test "$PHP_FPM" != "no"; then
fpm/fpm_shm_slots.c \
fpm/fpm_signals.c \
fpm/fpm_sockets.c \
fpm/fpm_status.c \
fpm/fpm_stdio.c \
fpm/fpm_unix.c \
fpm/fpm_worker_pool.c \
Expand Down
2 changes: 2 additions & 0 deletions sapi/fpm/fpm/fpm_children.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "fpm_unix.h"
#include "fpm_env.h"
#include "fpm_shm_slots.h"
#include "fpm_status.h"

#include "zlog.h"

Expand Down Expand Up @@ -145,6 +146,7 @@ static void fpm_child_init(struct fpm_worker_pool_s *wp) /* {{{ */
fpm_globals.max_requests = wp->config->max_requests;

if (0 > fpm_stdio_init_child(wp) ||
0 > fpm_status_init_child(wp) ||
0 > fpm_unix_init_child(wp) ||
0 > fpm_signals_init_child() ||
0 > fpm_env_init_child(wp) ||
Expand Down
77 changes: 77 additions & 0 deletions sapi/fpm/fpm/fpm_conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#if HAVE_INTTYPES_H
# include <inttypes.h>
#else
Expand All @@ -26,6 +27,8 @@
#include "fpm_cleanup.h"
#include "fpm_php.h"
#include "fpm_sockets.h"
#include "fpm_shm.h"
#include "fpm_status.h"
#include "xml_config.h"
#include "zlog.h"

Expand Down Expand Up @@ -190,6 +193,9 @@ static struct xml_conf_section fpm_conf_set_pm_subsection_conf = {
.parsers = (struct xml_value_parser []) {
{ XML_CONF_SCALAR, "style", &fpm_conf_set_pm_style, 0 },
{ XML_CONF_SCALAR, "max_children", &xml_conf_set_slot_integer, offsetof(struct fpm_pm_s, max_children) },
{ XML_CONF_SCALAR, "status", &xml_conf_set_slot_string, offsetof(struct fpm_pm_s, status) },
{ XML_CONF_SCALAR, "ping", &xml_conf_set_slot_string, offsetof(struct fpm_pm_s, ping) },
{ XML_CONF_SCALAR, "pong", &xml_conf_set_slot_string, offsetof(struct fpm_pm_s, pong) },
{ XML_CONF_SUBSECTION, "dynamic", &fpm_conf_set_dynamic_subsection, offsetof(struct fpm_pm_s, dynamic) },
{ 0, 0, 0, 0 }
}
Expand Down Expand Up @@ -288,6 +294,9 @@ int fpm_worker_pool_config_free(struct fpm_worker_pool_config_s *wpc) /* {{{ */

free(wpc->name);
free(wpc->listen_address);
free(wpc->pm->status);
free(wpc->pm->ping);
free(wpc->pm->pong);
if (wpc->listen_options) {
free(wpc->listen_options->owner);
free(wpc->listen_options->group);
Expand Down Expand Up @@ -466,6 +475,74 @@ static int fpm_conf_process_all_pools() /* {{{ */
close(fd);
}
}

if (wp->config->pm->ping && *wp->config->pm->ping) {
char *ping = wp->config->pm->ping;
int i;

if (*ping != '/') {
zlog(ZLOG_STUFF, ZLOG_ERROR, "[pool %s] the ping page '%s' must start with a '/'", wp->config->name, ping);
return(-1);
}

if (strlen(ping) < 2) {
zlog(ZLOG_STUFF, ZLOG_ERROR, "[pool %s] the ping page '%s' is not long enough", wp->config->name, ping);
return(-1);
}

for (i=0; i<strlen(ping); i++) {
if (!isalnum(ping[i]) && ping[i] != '/' && ping[i] != '-' && ping[i] != '_' && ping[i] != '.') {
zlog(ZLOG_STUFF, ZLOG_ERROR, "[pool %s] the ping page '%s' must containt only the following characters '[alphanum]/_-.'", wp->config->name, ping);
return(-1);
}
}

if (!wp->config->pm->pong) {
wp->config->pm->pong = strdup("pong");
} else {
if (strlen(wp->config->pm->pong) < 1) {
zlog(ZLOG_STUFF, ZLOG_ERROR, "[pool %s] the ping response page '%s' is not long enough", wp->config->name, wp->config->pm->pong);
return(-1);
}
}
} else {
if (wp->config->pm->pong) {
free(wp->config->pm->pong);
wp->config->pm->pong = NULL;
}
}

if (wp->config->pm->status && *wp->config->pm->status) {
int i;
char *status = wp->config->pm->status;
struct fpm_status_s fpm_status;

if (*status != '/') {
zlog(ZLOG_STUFF, ZLOG_ERROR, "[pool %s] the status page '%s' must start with a '/'", wp->config->name, status);
return(-1);
}

if (strlen(status) < 2) {
zlog(ZLOG_STUFF, ZLOG_ERROR, "[pool %s] the status page '%s' is not long enough", wp->config->name, status);
return(-1);
}

for (i=0; i<strlen(status); i++) {
if (!isalnum(status[i]) && status[i] != '/' && status[i] != '-' && status[i] != '_' && status[i] != '.') {
zlog(ZLOG_STUFF, ZLOG_ERROR, "[pool %s] the status page '%s' must containt only the following characters '[alphanum]/_-.'", wp->config->name, status);
return(-1);
}
}
wp->shm_status = fpm_shm_alloc(sizeof(struct fpm_status_s));
if (!wp->shm_status) {
zlog(ZLOG_STUFF, ZLOG_ERROR, "[pool %s] unable to allocate shared memory for status page '%s'", wp->config->name, status);
return(-1);
}
fpm_status_update_accepted_conn(wp->shm_status, 0);
fpm_status_update_activity(wp->shm_status, -1, -1, -1, 1);
fpm_status_set_pm(wp->shm_status, wp->config->pm->style);
//memset(&fpm_status.last_update, 0, sizeof(fpm_status.last_update));
}
}
return 0;
}
Expand Down
5 changes: 5 additions & 0 deletions sapi/fpm/fpm/fpm_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#ifndef FPM_CONF_H
#define FPM_CONF_H 1

#define FPM_CONF_MAX_PONG_LENGTH 64

struct key_value_s;

struct key_value_s {
Expand All @@ -27,6 +29,9 @@ extern struct fpm_global_config_s fpm_global_config;
struct fpm_pm_s {
int style;
int max_children;
char *status;
char *ping;
char *pong;
struct {
int start_servers;
int min_spare_servers;
Expand Down
23 changes: 23 additions & 0 deletions sapi/fpm/fpm/fpm_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ int __riscosify_control = __RISCOSIFY_STRICT_UNIX_SPECS;
#endif
#include <fpm/fpm.h>
#include <fpm/fpm_request.h>
#include <fpm/fpm_status.h>

#ifndef PHP_WIN32
/* XXX this will need to change later when threaded fastcgi is implemented. shane */
Expand Down Expand Up @@ -1764,6 +1765,7 @@ consult the installation file that came with this distribution, or visit \n\
SG(server_context) = (void *) &request;
init_request_info(TSRMLS_C);
CG(interactive) = 0;
char *status_buffer;

fpm_request_info();

Expand All @@ -1776,6 +1778,27 @@ consult the installation file that came with this distribution, or visit \n\
return FAILURE;
}

if (fpm_status_handle_status(SG(request_info).request_uri, &status_buffer)) {
sapi_add_header_ex(ZEND_STRL("Content-Type: text/plain"), 1, 1 TSRMLS_CC);
if (status_buffer) {
int i;
SG(sapi_headers).http_response_code = 200;
PUTS(status_buffer);
efree(status_buffer);
} else {
SG(sapi_headers).http_response_code = 500;
PUTS("Unable to retrieve status\n");
}
goto fastcgi_request_done;
}

if (status_buffer = fpm_status_handle_ping(SG(request_info).request_uri)) {
sapi_add_header_ex(ZEND_STRL("Content-Type: text/plain"), 1, 1 TSRMLS_CC);
SG(sapi_headers).http_response_code = 200;
PUTS(status_buffer);
goto fastcgi_request_done;
}

/* If path_translated is NULL, terminate here with a 404 */
if (!SG(request_info).path_translated) {
zend_try {
Expand Down
12 changes: 9 additions & 3 deletions sapi/fpm/fpm/fpm_process_ctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "fpm_cleanup.h"
#include "fpm_request.h"
#include "fpm_worker_pool.h"
#include "fpm_status.h"
#include "zlog.h"


Expand Down Expand Up @@ -322,7 +323,6 @@ static void fpm_pctl_perform_idle_server_maintenance(struct timeval *now) /* {{{
int active = 0;

if (wp->config == NULL) continue;
if (wp->config->pm->style != PM_STYLE_DYNAMIC) continue;

for (child = wp->children; child; child = child->next) {
int ret = fpm_request_is_idle(child);
Expand All @@ -340,13 +340,19 @@ static void fpm_pctl_perform_idle_server_maintenance(struct timeval *now) /* {{{
}
}

zlog(ZLOG_STUFF, ZLOG_DEBUG, "[pool %s] currently %d active children, %d spare children, %d running children. Spawning rate %d", wp->config->name, active, idle, wp->running_children, wp->idle_spawn_rate);

if ((active + idle) != wp->running_children) {
zlog(ZLOG_STUFF, ZLOG_ERROR, "[pool %s] unable to retrieve process activiry of one or more child(ren). Will try again later.", wp->config->name);
continue;
}

/* update status structure for all PMs */
fpm_status_update_activity(wp->shm_status, idle, active, idle + active, 0);

/* the rest is only used by PM_STYLE_DYNAMIC */
if (wp->config->pm->style != PM_STYLE_DYNAMIC) continue;

zlog(ZLOG_STUFF, ZLOG_DEBUG, "[pool %s] currently %d active children, %d spare children, %d running children. Spawning rate %d", wp->config->name, active, idle, wp->running_children, wp->idle_spawn_rate);

if (idle > wp->config->pm->dynamic.max_spare_servers && last_idle_child) {
last_idle_child->idle_kill = 1;
fpm_pctl_kill(last_idle_child->pid, FPM_PCTL_TERM);
Expand Down
3 changes: 3 additions & 0 deletions sapi/fpm/fpm/fpm_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "fpm_process_ctl.h"
#include "fpm_children.h"
#include "fpm_shm_slots.h"
#include "fpm_status.h"
#include "fpm_request.h"

#include "zlog.h"
Expand Down Expand Up @@ -40,6 +41,8 @@ void fpm_request_reading_headers() /* {{{ */
fpm_clock_get(&slot->tv);
slot->accepted = slot->tv;
fpm_shm_slots_release(slot);

fpm_status_increment_accepted_conn(fpm_status_shm);
}
/* }}} */

Expand Down
2 changes: 1 addition & 1 deletion sapi/fpm/fpm/fpm_shm.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct fpm_shm_s *fpm_shm_alloc(size_t sz) /* {{{ */
}
/* }}} */

static void fpm_shm_free(struct fpm_shm_s *shm, int do_unmap) /* {{{ */
void fpm_shm_free(struct fpm_shm_s *shm, int do_unmap) /* {{{ */
{
if (do_unmap) {
munmap(shm->mem, shm->sz);
Expand Down
1 change: 1 addition & 0 deletions sapi/fpm/fpm/fpm_shm.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ struct fpm_shm_s {
};

struct fpm_shm_s *fpm_shm_alloc(size_t sz);
void fpm_shm_free(struct fpm_shm_s *shm, int do_unmap);
void fpm_shm_free_list(struct fpm_shm_s *, void *);
void *fpm_shm_alloc_chunk(struct fpm_shm_s **head, size_t sz, void **mem);

Expand Down
Loading

0 comments on commit 7598007

Please sign in to comment.