Hi, I plan to allow php-fpm to be aware of systemd and so, use the type=notify mode.
I'd like to apply to attached patch to 5.4 and 5.5, as this change have no impact on standard build (need new --with-fpm-systemd build option). Any feedback before I process ? Remi.
diff -up ../sapi/fpm/config.m4.systemd ../sapi/fpm/config.m4 --- ../sapi/fpm/config.m4.systemd 2013-05-08 18:45:31.000000000 +0200 +++ ../sapi/fpm/config.m4 2013-05-17 12:20:19.199841304 +0200 @@ -563,6 +563,26 @@ if test "$PHP_FPM" != "no"; then [ --with-fpm-group[=GRP] Set the group for php-fpm to run as. For a system user, this should usually be set to match the fpm username (default: nobody)], nobody, no) + PHP_ARG_WITH(fpm-systemd,, + [ --with-fpm-systemd Activate systemd integration], no, no) + + if test "$PHP_FPM_SYSTEMD" != "no" ; then + AC_CHECK_LIB(systemd-daemon, sd_notify, SYSTEMD_LIBS="-lsystemd-daemon") + AC_CHECK_HEADERS(systemd/sd-daemon.h, [HAVE_SD_DAEMON_H="yes"], [HAVE_SD_DAEMON_H="no"]) + if test $HAVE_SD_DAEMON_H = "no" || test -z "${SYSTEMD_LIBS}"; then + AC_MSG_ERROR([Your system does not support systemd.]) + else + AC_DEFINE(HAVE_SYSTEMD, 1, [FPM use systemd integration]) + PHP_FPM_SD_FILES="fpm/fpm_systemd.c" + PHP_ADD_LIBRARY(systemd-daemon) + php_fpm_systemd=notify + fi + else + php_fpm_systemd=simple + fi + PHP_SUBST_OLD(php_fpm_systemd) + AC_DEFINE_UNQUOTED(PHP_FPM_SYSTEMD, "$php_fpm_systemd", [fpm systemd service type]) + if test -z "$PHP_FPM_USER" -o "$PHP_FPM_USER" = "yes" -o "$PHP_FPM_USER" = "no"; then php_fpm_user="nobody" else @@ -631,7 +651,7 @@ if test "$PHP_FPM" != "no"; then fpm/events/port.c \ " - PHP_SELECT_SAPI(fpm, program, $PHP_FPM_FILES $PHP_FPM_TRACE_FILES, $PHP_FPM_CFLAGS, '$(SAPI_FPM_PATH)') + PHP_SELECT_SAPI(fpm, program, $PHP_FPM_FILES $PHP_FPM_TRACE_FILES $PHP_FPM_SD_FILES, $PHP_FPM_CFLAGS, '$(SAPI_FPM_PATH)') case $host_alias in *aix*) diff -up ../sapi/fpm/fpm/fpm_events.c.systemd ../sapi/fpm/fpm/fpm_events.c --- ../sapi/fpm/fpm/fpm_events.c.systemd 2013-05-08 18:45:31.000000000 +0200 +++ ../sapi/fpm/fpm/fpm_events.c 2013-05-17 13:13:10.854896820 +0200 @@ -29,6 +29,10 @@ #include "events/port.h" #include "events/kqueue.h" +#ifdef HAVE_SYSTEMD +#include "fpm_systemd.h" +#endif + #define fpm_event_set_timeout(ev, now) timeradd(&(now), &(ev)->frequency, &(ev)->timeout); static void fpm_event_cleanup(int which, void *arg); @@ -361,6 +365,10 @@ void fpm_event_loop(int err) /* {{{ */ zlog(ZLOG_DEBUG, "%zu bytes have been reserved in SHM", fpm_shm_get_size_allocated()); zlog(ZLOG_NOTICE, "ready to handle connections"); + +#ifdef HAVE_SYSTEMD + fpm_systemd_heartbeat(NULL, 0, NULL); +#endif } while (1) { diff -up ../sapi/fpm/fpm/fpm_systemd.c.systemd ../sapi/fpm/fpm/fpm_systemd.c --- ../sapi/fpm/fpm/fpm_systemd.c.systemd 2013-05-17 12:20:19.200841309 +0200 +++ ../sapi/fpm/fpm/fpm_systemd.c 2013-05-17 14:18:36.851079208 +0200 @@ -0,0 +1,74 @@ +#include "fpm_config.h" + +#include <sys/types.h> +#include <systemd/sd-daemon.h> + +#include "fpm.h" +#include "fpm_clock.h" +#include "fpm_worker_pool.h" +#include "fpm_scoreboard.h" +#include "zlog.h" +#include "fpm_systemd.h" + + +static void fpm_systemd() /* {{{ */ +{ + static unsigned long int last=0; + struct fpm_worker_pool_s *wp; + unsigned long int requests=0, slow_req=0; + int active=0, idle=0, rv; + + + for (wp = fpm_worker_all_pools; wp; wp = wp->next) { + if (wp->scoreboard) { + active += wp->scoreboard->active; + idle += wp->scoreboard->idle; + requests += wp->scoreboard->requests; + slow_req += wp->scoreboard->slow_rq; + } + } +/* + zlog(ZLOG_DEBUG, "systemd heartbeat (Processes active:%d, idle:%d, Requests:%lu, slow:%lu, Traffic:%.3greq/sec)", + active, idle, requests, slow_req, ((float)requests - last) * 1000.0 / FPM_SYSTEMD_HEARTBEAT); +*/ + rv = sd_notifyf(0, "READY=1\n" + "STATUS=Processes active: %d, idle: %d, Requests: %lu, slow: %lu, Traffic: %.3greq/sec", + active, idle, requests, slow_req, ((float)requests - last) * 1000.0 / FPM_SYSTEMD_HEARTBEAT); + + if (rv < 0) { + zlog(ZLOG_NOTICE, "failed to notify systemd"); + } + + last = requests; +} +/* }}} */ + +void fpm_systemd_heartbeat(struct fpm_event_s *ev, short which, void *arg) /* {{{ */ +{ + static struct fpm_event_s heartbeat; + + if (fpm_globals.parent_pid != getpid()) { + return; /* sanity check */ + } + + if (which == FPM_EV_TIMEOUT) { + fpm_systemd(); + + return; + } + + if (sd_notifyf(0, "READY=1\n" + "STATUS=ready to handle connections\n" + "MAINPID=%lu", + (unsigned long) getpid()) < 0) { + zlog(ZLOG_WARNING, "failed to notify systemd"); + } else { + zlog(ZLOG_DEBUG, "have notify systemd"); + } + + /* first call without setting which to initialize the timer */ + fpm_event_set_timer(&heartbeat, FPM_EV_PERSIST, &fpm_systemd_heartbeat, NULL); + fpm_event_add(&heartbeat, FPM_SYSTEMD_HEARTBEAT); +} +/* }}} */ + diff -up ../sapi/fpm/fpm/fpm_systemd.h.systemd ../sapi/fpm/fpm/fpm_systemd.h --- ../sapi/fpm/fpm/fpm_systemd.h.systemd 2013-05-17 12:20:19.200841309 +0200 +++ ../sapi/fpm/fpm/fpm_systemd.h 2013-05-17 14:02:37.400247799 +0200 @@ -0,0 +1,12 @@ +#ifndef FPM_SYSTEMD_H +#define FPM_SYSTEMD_H 1 + +#include "fpm_events.h" + +/* 10s (in ms) heartbeat for systemd status */ +#define FPM_SYSTEMD_HEARTBEAT (10000) + +void fpm_systemd_heartbeat(struct fpm_event_s *ev, short which, void *arg); + +#endif + diff -up ../sapi/fpm/php-fpm.service.in.systemd ../sapi/fpm/php-fpm.service.in --- ../sapi/fpm/php-fpm.service.in.systemd 2013-05-08 18:45:31.000000000 +0200 +++ ../sapi/fpm/php-fpm.service.in 2013-05-17 12:20:19.201841314 +0200 @@ -3,6 +3,7 @@ Description=The PHP FastCGI Process Mana After=syslog.target network.target [Service] +Type=@php_fpm_systemd@ PIDFile=@localstatedir@/run/php-fpm.pid ExecStart=@sbindir@/php-fpm --nodaemonize --fpm-config @sysconfdir@/php-fpm.conf ExecReload=/bin/kill -USR2 $MAINPID
-- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php