php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
fpm_systemd.c
Go to the documentation of this file.
1#include "fpm_config.h"
2
3#include <sys/types.h>
4#include <systemd/sd-daemon.h>
5
6#include "fpm.h"
7#include "fpm_clock.h"
8#include "fpm_worker_pool.h"
9#include "fpm_scoreboard.h"
10#include "zlog.h"
11#include "fpm_systemd.h"
12
13
14static void fpm_systemd(void)
15{
16 static unsigned long int last=0;
17 struct fpm_worker_pool_s *wp;
18 unsigned long int requests=0, slow_req=0;
19 int active=0, idle=0;
20
21
22 for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
23 if (wp->scoreboard) {
24 active += wp->scoreboard->active;
25 idle += wp->scoreboard->idle;
26 requests += wp->scoreboard->requests;
27 slow_req += wp->scoreboard->slow_rq;
28 }
29 }
30
31/*
32 zlog(ZLOG_DEBUG, "systemd %s (Processes active:%d, idle:%d, Requests:%lu, slow:%lu, Traffic:%.2freq/sec)",
33 fpm_global_config.systemd_watchdog ? "watchdog" : "heartbeat",
34 active, idle, requests, slow_req, ((float)requests - last) * 1000.0 / fpm_global_config.systemd_interval);
35*/
36
37 if (0 > sd_notifyf(0, "READY=1\n%s"
38 "STATUS=Processes active: %d, idle: %d, Requests: %lu, slow: %lu, Traffic: %.2freq/sec",
39 fpm_global_config.systemd_watchdog ? "WATCHDOG=1\n" : "",
40 active, idle, requests, slow_req, ((float)requests - last) * 1000.0 / fpm_global_config.systemd_interval)) {
41 zlog(ZLOG_NOTICE, "failed to notify status to systemd");
42 }
43
44 last = requests;
45}
46
47void fpm_systemd_heartbeat(struct fpm_event_s *ev, short which, void *arg) /* {{{ */
48{
49 static struct fpm_event_s heartbeat;
50
51 if (fpm_globals.parent_pid != getpid()) {
52 return; /* sanity check */
53 }
54
55 if (which == FPM_EV_TIMEOUT) {
56 fpm_systemd();
57
58 return;
59 }
60
61 if (0 > sd_notifyf(0, "READY=1\n"
62 "STATUS=Ready to handle connections\n"
63 "MAINPID=%lu",
64 (unsigned long) getpid())) {
65 zlog(ZLOG_WARNING, "failed to notify start to systemd");
66 } else {
67 zlog(ZLOG_DEBUG, "have notify start to systemd");
68 }
69
70 /* first call without setting which to initialize the timer */
71 if (fpm_global_config.systemd_interval > 0) {
73 fpm_event_add(&heartbeat, fpm_global_config.systemd_interval);
74 zlog(ZLOG_NOTICE, "systemd monitor interval set to %dms", fpm_global_config.systemd_interval);
75 } else {
76 zlog(ZLOG_NOTICE, "systemd monitor disabled");
77 }
78}
79/* }}} */
80
82{
83 char *watchdog;
84 int interval = 0;
85
86 watchdog = getenv("WATCHDOG_USEC");
87 if (watchdog) {
88 /* usec to msec, and half the configured delay */
89 interval = (int)(atol(watchdog) / 2000L);
90 zlog(ZLOG_DEBUG, "WATCHDOG_USEC=%s, interval=%d", watchdog, interval);
91 }
92
93 if (interval > 1000) {
94 if (fpm_global_config.systemd_interval > 0) {
95 zlog(ZLOG_WARNING, "systemd_interval option ignored");
96 }
97 zlog(ZLOG_NOTICE, "systemd watchdog configured to %.3gsec", (float)interval / 1000.0);
98 fpm_global_config.systemd_watchdog = 1;
99 fpm_global_config.systemd_interval = interval;
100
101 } else if (fpm_global_config.systemd_interval < 0) {
102 /* not set => default value */
104
105 } else {
106 /* sec to msec */
107 fpm_global_config.systemd_interval *= 1000;
108 }
109 return 0;
110}
getenv(?string $name=null, bool $local_only=false)
zval * arg
Definition ffi.c:3975
struct fpm_globals_s fpm_globals
Definition fpm.c:24
struct fpm_global_config_s fpm_global_config
Definition fpm_conf.c:64
int fpm_event_add(struct fpm_event_s *ev, unsigned long int frequency)
Definition fpm_events.c:496
#define fpm_event_set_timer(ev, flags, cb, arg)
Definition fpm_events.h:11
#define FPM_EV_TIMEOUT
Definition fpm_events.h:6
#define FPM_EV_PERSIST
Definition fpm_events.h:8
void fpm_systemd_heartbeat(struct fpm_event_s *ev, short which, void *arg)
Definition fpm_systemd.c:47
int fpm_systemd_conf(void)
Definition fpm_systemd.c:81
#define FPM_SYSTEMD_DEFAULT_HEARTBEAT
Definition fpm_systemd.h:7
struct fpm_worker_pool_s * fpm_worker_all_pools
#define NULL
Definition gdcache.h:45
php_output_handler * active
Definition php_output.h:140
short which
Definition fpm_events.h:21
unsigned long int slow_rq
unsigned long int requests
struct fpm_scoreboard_s * scoreboard
struct fpm_worker_pool_s * next
int last
@ ZLOG_DEBUG
Definition zlog.h:42
@ ZLOG_NOTICE
Definition zlog.h:43
@ ZLOG_WARNING
Definition zlog.h:44
#define zlog(flags,...)
Definition zlog.h:9