php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #52569
Patch fpm-ondemand.v11-5.3.patch revision 2011-07-14 22:38 UTC by fat@php.net
Patch fpm-ondemand.v11.patch revision 2011-07-14 22:27 UTC by fat@php.net
Patch fpm-ondemand.v10-5.3.patch revision 2011-07-10 17:49 UTC by fat@php.net
Patch fpm-ondemand.v10.patch revision 2011-07-10 17:49 UTC by fat@php.net
Patch fpm-ondemand.v9-5.3.patch revision 2011-07-09 12:30 UTC by fat@php.net
Patch fpm-ondemand.v9.patch revision 2011-07-09 12:30 UTC by fat@php.net
Patch fpm-ondemand.v8-5.3.patch revision 2011-07-09 00:22 UTC by fat@php.net
Patch fpm-ondemand.v8.patch revision 2011-07-09 00:21 UTC by fat@php.net
Patch fpm-ondemand.v7-5.3.patch revision 2011-07-05 23:12 UTC by fat@php.net
Patch fpm-ondemand.v7.patch revision 2011-07-05 23:08 UTC by fat@php.net
Patch fpm-ondemand-pm-v6 revision 2010-09-25 16:27 UTC by mplomer at gmx dot de
Patch php-fpm-ondemand-pm-v5 revision 2010-08-30 08:16 UTC by mplomer at gmx dot de
Patch fpm-ondemand.v4.patch revision 2010-08-27 06:27 UTC by fat@php.net
Patch fpm-ondemand-pm-v3 revision 2010-08-25 22:12 UTC by mplomer at gmx dot de
Patch fpm-ondemand.v2.patch.txt revision 2010-08-23 22:51 UTC by fat@php.net
Patch fpm-ondemand-pm-php53 revision 2010-08-10 18:01 UTC by mplomer at gmx dot de
Patch fpm-ondemand-pm revision 2010-08-09 20:14 UTC by fat@php.net

Patch fpm-ondemand.v7-5.3.patch for FPM related Bug #52569

Patch version 2011-07-05 23:12 UTC

Return to Bug #52569 | Download this patch
This patch is obsolete

Obsoleted by patches:

Patch Revisions:

Developer: fat@php.net

Index: sapi/fpm/fpm/fpm_request.h
===================================================================
--- sapi/fpm/fpm/fpm_request.h	(revision 312956)
+++ sapi/fpm/fpm/fpm_request.h	(working copy)
@@ -17,6 +17,7 @@
 
 void fpm_request_check_timed_out(struct fpm_child_s *child, struct timeval *tv, int terminate_timeout, int slowlog_timeout);
 int fpm_request_is_idle(struct fpm_child_s *child);
+int fpm_request_last_activity(struct fpm_child_s *child, struct timeval *tv);
 
 enum fpm_request_stage_e {
 	FPM_REQUEST_ACCEPTING = 1,
Index: sapi/fpm/fpm/fpm_process_ctl.c
===================================================================
--- sapi/fpm/fpm/fpm_process_ctl.c	(revision 312956)
+++ sapi/fpm/fpm/fpm_process_ctl.c	(working copy)
@@ -355,7 +355,24 @@
 			fpm_scoreboard_update(idle, active, cur_lq, -1, -1, -1, FPM_SCOREBOARD_ACTION_SET, wp->scoreboard);
 		}
 
+		/* this is specific to PM_STYLE_ONDEMAND */
+		if (wp->config->pm == PM_STYLE_ONDEMAND) {
+			struct timeval last, now;
 
+			zlog(ZLOG_DEBUG, "[pool %s] currently %d active children, %d spare children", wp->config->name, active, idle);
+
+			if (!last_idle_child) continue;
+
+			fpm_request_last_activity(last_idle_child, &last);
+			fpm_clock_get(&now);
+			if (last.tv_sec < now.tv_sec - wp->config->pm_process_idle_timeout) {
+				last_idle_child->idle_kill = 1;
+				fpm_pctl_kill(last_idle_child->pid, FPM_PCTL_QUIT);
+			}
+
+			continue;
+		}
+
 		/* the rest is only used by PM_STYLE_DYNAMIC */
 		if (wp->config->pm != PM_STYLE_DYNAMIC) continue;
 
@@ -472,3 +489,58 @@
 }
 /* }}} */
 
+void fpm_pctl_on_socket_accept(struct fpm_event_s *ev, short which, void *arg) /* {{{ */
+{
+	struct fpm_worker_pool_s *wp = (struct fpm_worker_pool_s *)arg;
+	struct fpm_child_s *child;
+	struct timeval now, diff;
+
+
+	if (fpm_globals.parent_pid != getpid()) {
+		/* prevent a event race condition when child process
+		 * have not set up its own event loop */
+		return;
+	}
+
+	wp->socket_event_set = 0;
+
+//	zlog(ZLOG_DEBUG, "[pool %s] heartbeat running_children=%d", wp->config->name, wp->running_children);
+
+	if (wp->running_children >= wp->config->pm_max_children) {
+		if (!wp->warn_max_children) {
+			fpm_scoreboard_update(0, 0, 0, 0, 0, 1, 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;
+		}
+
+		return;
+	}
+
+	fpm_clock_get(&now);
+	timersub(&now, &wp->last_fork, &diff);
+	// if we forked in the last pm_min_delay_between_fork µs, do nothing
+	if (diff.tv_sec == 0 && diff.tv_usec < wp->config->pm_min_delay_between_fork) {
+		return;
+	}
+	wp->last_fork.tv_sec = 0;
+	wp->last_fork.tv_usec = 0;
+
+	for (child = wp->children; child; child = child->next) {
+		/* if there is at least on idle child, it will handle the connection, stop here */
+		if (fpm_request_is_idle(child)) {
+			return;
+		}
+	}
+
+	wp->warn_max_children = 0;
+	fpm_clock_get(&wp->last_fork);
+	fpm_children_make(wp, 1, 1, 1);
+
+	if (fpm_globals.is_child) {
+		return;
+	}
+
+	zlog(ZLOG_DEBUG, "[pool %s] got accept without idle child available .... I forked, now=%d.%d", wp->config->name, (int)now.tv_sec, (int)now.tv_usec);
+}
+/* }}} */
+
Index: sapi/fpm/fpm/fpm_status.c
===================================================================
--- sapi/fpm/fpm/fpm_status.c	(revision 312956)
+++ sapi/fpm/fpm/fpm_status.c	(working copy)
@@ -215,7 +215,7 @@
 		now_epoch = time(NULL);
 		spprintf(&buffer, 0, syntax,
 				scoreboard.pool,
-				scoreboard.pm == PM_STYLE_STATIC ? "static" : "dynamic",
+				scoreboard.pm == PM_STYLE_STATIC ? "static" : (scoreboard.pm == PM_STYLE_DYNAMIC ? "dynamic" : "ondemand"),
 				time_buffer,
 				now_epoch - scoreboard.start_epoch,
 				scoreboard.requests,
Index: sapi/fpm/fpm/fpm_conf.c
===================================================================
--- sapi/fpm/fpm/fpm_conf.c	(revision 312956)
+++ sapi/fpm/fpm/fpm_conf.c	(working copy)
@@ -103,6 +103,8 @@
 	{ "pm.start_servers",          &fpm_conf_set_integer,     WPO(pm_start_servers) },
 	{ "pm.min_spare_servers",      &fpm_conf_set_integer,     WPO(pm_min_spare_servers) },
 	{ "pm.max_spare_servers",      &fpm_conf_set_integer,     WPO(pm_max_spare_servers) },
+	{ "pm.process_idle_timeout",   &fpm_conf_set_time,        WPO(pm_process_idle_timeout) },
+	{ "pm.min_delay_between_fork", &fpm_conf_set_integer,     WPO(pm_min_delay_between_fork) },
 	{ "pm.status_path",            &fpm_conf_set_string,      WPO(pm_status_path) },
 	{ "ping.path",                 &fpm_conf_set_string,      WPO(ping_path) },
 	{ "ping.response",             &fpm_conf_set_string,      WPO(ping_response) },
@@ -308,8 +310,10 @@
 		c->pm = PM_STYLE_STATIC;
 	} else if (!strcasecmp(val, "dynamic")) {
 		c->pm = PM_STYLE_DYNAMIC;
+	} else if (!strcasecmp(val, "ondemand")) {
+		c->pm = PM_STYLE_ONDEMAND;
 	} else {
-		return "invalid process manager (static or dynamic)";
+		return "invalid process manager (static, dynamic or ondemand)";
 	}
 	return NULL;
 }
@@ -375,6 +379,8 @@
 
 	memset(wp->config, 0, sizeof(struct fpm_worker_pool_config_s));
 	wp->config->listen_backlog = FPM_BACKLOG_DEFAULT;
+	wp->config->pm_process_idle_timeout = 10; /* 10s by default */
+	wp->config->pm_min_delay_between_fork = 100; /* 100µs by default */
 
 	if (!fpm_worker_all_pools) {
 		fpm_worker_all_pools = wp;
@@ -534,8 +540,8 @@
 			return -1;
 		}
 
-		if (wp->config->pm != PM_STYLE_STATIC && wp->config->pm != PM_STYLE_DYNAMIC) {
-			zlog(ZLOG_ALERT, "[pool %s] the process manager is missing (static or dynamic)", wp->config->name);
+		if (wp->config->pm != PM_STYLE_STATIC && wp->config->pm != PM_STYLE_DYNAMIC && wp->config->pm != PM_STYLE_ONDEMAND) {
+			zlog(ZLOG_ALERT, "[pool %s] the process manager is missing (static, dynamic or ondemand)", wp->config->name);
 			return -1;
 		}
 
@@ -576,7 +582,28 @@
 				zlog(ZLOG_ALERT, "[pool %s] pm.start_servers(%d) must not be less than pm.min_spare_servers(%d) and not greater than pm.max_spare_servers(%d)", wp->config->name, config->pm_start_servers, config->pm_min_spare_servers, config->pm_max_spare_servers);
 				return -1;
 			}
+		} else if (wp->config->pm == PM_STYLE_ONDEMAND) {
+			struct fpm_worker_pool_config_s *config = wp->config;
 
+			if (config->pm_process_idle_timeout < 1) {
+				zlog(ZLOG_ALERT, "[pool %s] pm.process_idle_timeout(%ds) must be greater than 0s", wp->config->name, config->pm_process_idle_timeout);
+				return -1;
+			}
+
+			if (config->listen_backlog < FPM_BACKLOG_DEFAULT) {
+				zlog(ZLOG_WARNING, "[pool %s] listen.backlog(%d) was too low for the ondemand process manager. I updated it for you to %d.", wp->config->name, config->listen_backlog, FPM_BACKLOG_DEFAULT);
+				config->listen_backlog = FPM_BACKLOG_DEFAULT;
+			}
+
+			if (config->pm_min_delay_between_fork < 1 || config->pm_min_delay_between_fork > 999999) {
+				zlog(ZLOG_ALERT, "[pool %s] pm.min_delay_between_fork(%d) must be greater than 0µs and less than 1000000µs", wp->config->name, config->pm_min_delay_between_fork);
+				return -1;
+			}
+
+			/* certainely useless but proper */
+			config->pm_start_servers = 0;
+			config->pm_min_spare_servers = 0;
+			config->pm_max_spare_servers = 0;
 		}
 
 		if (wp->config->slowlog && *wp->config->slowlog) {
Index: sapi/fpm/fpm/fpm_process_ctl.h
===================================================================
--- sapi/fpm/fpm/fpm_process_ctl.h	(revision 312956)
+++ sapi/fpm/fpm/fpm_process_ctl.h	(working copy)
@@ -22,6 +22,7 @@
 void fpm_pctl_kill_all(int signo);
 void fpm_pctl_heartbeat(struct fpm_event_s *ev, short which, void *arg);
 void fpm_pctl_perform_idle_server_maintenance_heartbeat(struct fpm_event_s *ev, short which, void *arg);
+void fpm_pctl_on_socket_accept(struct fpm_event_s *ev, short which, void *arg);
 int fpm_pctl_child_exited();
 int fpm_pctl_init_main();
 
Index: sapi/fpm/fpm/fpm_conf.h
===================================================================
--- sapi/fpm/fpm/fpm_conf.h	(revision 312956)
+++ sapi/fpm/fpm/fpm_conf.h	(working copy)
@@ -51,6 +51,8 @@
 	int pm_start_servers;
 	int pm_min_spare_servers;
 	int pm_max_spare_servers;
+	int pm_process_idle_timeout;
+	int pm_min_delay_between_fork;
 	char *ping_path;
 	char *ping_response;
 	char *access_log;
@@ -74,7 +76,8 @@
 
 enum {
 	PM_STYLE_STATIC = 1,
-	PM_STYLE_DYNAMIC = 2
+	PM_STYLE_DYNAMIC = 2,
+	PM_STYLE_ONDEMAND = 3
 };
 
 int fpm_conf_init_main(int test_conf);
Index: sapi/fpm/fpm/fpm_worker_pool.h
===================================================================
--- sapi/fpm/fpm/fpm_worker_pool.h	(revision 312956)
+++ sapi/fpm/fpm/fpm_worker_pool.h	(working copy)
@@ -37,6 +37,11 @@
 #endif
 	struct fpm_scoreboard_s *scoreboard;
 	int log_fd;
+
+	/* for ondemand PM */
+	struct timeval last_fork;
+	struct fpm_event_s *ondemand_event;
+	int socket_event_set;
 };
 
 struct fpm_worker_pool_s *fpm_worker_pool_alloc();
Index: sapi/fpm/fpm/fpm_request.c
===================================================================
--- sapi/fpm/fpm/fpm_request.c	(revision 312956)
+++ sapi/fpm/fpm/fpm_request.c	(working copy)
@@ -276,3 +276,20 @@
 	return proc->request_stage == FPM_REQUEST_ACCEPTING;
 }
 /* }}} */
+
+int fpm_request_last_activity(struct fpm_child_s *child, struct timeval *tv) /* {{{ */
+{
+	struct fpm_scoreboard_proc_s *proc;
+
+	if (!tv) return -1;
+
+	proc = fpm_scoreboard_proc_get(child->wp->scoreboard, child->scoreboard_i);
+	if (!proc) {
+		return -1;
+	}
+
+	*tv = proc->tv;
+
+	return 1;
+}
+/* }}} */
Index: sapi/fpm/fpm/fpm_children.c
===================================================================
--- sapi/fpm/fpm/fpm_children.c	(revision 312956)
+++ sapi/fpm/fpm/fpm_children.c	(working copy)
@@ -369,6 +369,12 @@
 		} else {
 			max = wp->running_children + nb_to_spawn;
 		}
+	} else if (wp->config->pm == PM_STYLE_ONDEMAND) {
+		if (!in_event_loop) { /* starting */
+			max = 0; /* do not create any child at startup */
+		} else {
+			max = wp->running_children + nb_to_spawn;
+		}
 	} else { /* PM_STYLE_STATIC */
 		max = wp->config->pm_max_children;
 	}
@@ -412,6 +418,22 @@
 
 int fpm_children_create_initial(struct fpm_worker_pool_s *wp) /* {{{ */
 {
+	if (wp->config->pm == PM_STYLE_ONDEMAND) {
+		wp->ondemand_event = (struct fpm_event_s *)malloc(sizeof(struct fpm_event_s));
+
+		if (!wp->ondemand_event) {
+			zlog(ZLOG_ERROR, "[pool %s] unable to malloc the ondemand socket event", wp->config->name);
+			// FIXME handle crash
+			return 1;
+		}
+
+		memset(wp->ondemand_event, 0, sizeof(struct fpm_event_s));
+		fpm_event_set(wp->ondemand_event, wp->listening_socket, FPM_EV_READ, fpm_pctl_on_socket_accept, wp);
+		wp->socket_event_set = 1;
+		fpm_event_add(wp->ondemand_event, 0);
+
+		return 1;
+	}
 	return fpm_children_make(wp, 0 /* not in event loop yet */, 0, 1);
 }
 /* }}} */
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 11:01:28 2024 UTC