winzik/docker/ext/cryo/cryo_metrics.c

2886 lines
86 KiB
C

/*
* cryo_metrics.c — metrics of ext-cryo.
*
* cryo_metrics() memory (Zend + system), objects, GC, Cryo counters
* cryo_metrics_class_counts() live objects per class
* cryo_metrics_sample() background series in a .cryom file (cryo_format.h)
* cryo_metrics_mark() named mark in the series
* cryo_metrics_read() reads a .cryom file
* cryo_metrics_serve() HTTP exporter in a light child process: Prometheus
* /metrics, /metrics.json, /series.json, POST /snapshot
* (Live mode of Cryo Studio), CORS, bearer token
* cryo_metrics_push() StatsD / DogStatsD gauges over UDP
* ini cryo.otlp_endpoint every sample also POSTed as OTLP/HTTP JSON
* ini cryo.auto_snapshot_every cryo_snapshot() every N samples
*
* Live class counts (cryo.metrics=1)
* ----------------------------------
* Counting by walking the objects store costs one hash lookup per object at
* every read. With cryo.metrics=1 the counts are kept up to date instead:
*
* - Plain objects (handlers == &std_object_handlers, i.e. every user class
* not extending an internal class) are "tagged": their handlers pointer is
* swapped for a Cryo copy of std_object_handlers that differs only by
* free_obj (decrements the counter of the class, then zend_object_std_dtor)
* and clone_obj (zend_objects_clone_obj, then tags the clone). Tagging a
* single object, not the class, leaves classes untouched, so classes
* living in opcache shared memory (immutable) work too.
* - Tagging happens right after creation: a ZEND_NEW user opcode handler
* notes the handle the new object will take (head of the free list or top
* of the store) and the next NEW, or the next read, tags it. O(1) per new
* and per free.
* - Objects created another way (unserialize(), Reflection, internal code)
* are picked up by a light pass on objects_store at each read: one pointer
* comparison per slot, the counters themselves come from the table.
* Objects with other handlers (internal classes, closures, enums) are
* counted by that pass at each read.
* The result is exact at every read. Engine fast paths that test
* handlers == &std_object_handlers (get_object_vars, (array) cast, clone of
* dynamic properties) take their generic path for tagged objects. Objects are
* untagged at request shutdown (keeps the fast shutdown path).
*
* Background sampling
* -------------------
* POSIX: a thread sleeps on a condition variable and, every period, sets a
* flag and EG(vm_interrupt). The VM then calls zend_interrupt_function (ours,
* chained: the previous handler, e.g. cryo_memory_watch or pcntl, is called
* at the end), and the sample is taken in the PHP thread. A script blocked in
* a long internal call (sleep(), I/O) is sampled when it returns.
* WASM (CRYO_BACKEND_WASM): no thread; cryo_host_timer() arms a JS timer that
* calls cryo_timer_fired(), and every interrupt checks the clock.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "main/fopen_wrappers.h"
#include "main/php_open_temporary_file.h"
#include "ext/json/php_json.h"
#include "zend_exceptions.h"
#include "zend_smart_str.h"
#include "zend_gc.h"
#include "zend_atomic.h"
#include "zend_objects.h"
#include "zend_objects_API.h"
#include "php_cryo.h"
#include "cryo_format.h"
#include "cryo_host.h"
#include <errno.h>
#include <fcntl.h>
#include <time.h>
#include <math.h>
#include <limits.h>
#include <stdarg.h>
#include <sys/stat.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifndef CRYO_BACKEND_WASM
# define CRYO_HAVE_SOCKETS 1
# include <pthread.h>
# include <signal.h>
# include <poll.h>
# include <netdb.h>
# include <sys/types.h>
# include <sys/socket.h>
# include <sys/wait.h>
# include <netinet/in.h>
# ifdef __linux__
# include <sys/prctl.h>
# endif
# ifndef SOCK_CLOEXEC
# define SOCK_CLOEXEC 0
# endif
# ifndef MSG_NOSIGNAL
# define MSG_NOSIGNAL 0
# endif
# ifndef MSG_DONTWAIT
# define MSG_DONTWAIT 0
# endif
# ifndef O_NOFOLLOW
# define O_NOFOLLOW 0
# endif
#endif
#ifdef __EMSCRIPTEN__
# include <emscripten.h>
#endif
#ifndef O_CLOEXEC
# define O_CLOEXEC 0
#endif
cryo_stats_t cryo_stats;
/* ======================================================================== */
/* Live class counts */
/* ======================================================================== */
static bool cm_live_installed; /* cryo.metrics=1 at MINIT */
static zend_object_handlers cm_tag_handlers; /* std_object_handlers + our free/clone */
static user_opcode_handler_t cm_prev_new_handler;
static struct {
bool ready; /* counts initialised for this request */
bool has_pending;
uint32_t pending; /* handle the last ZEND_NEW was about to use */
HashTable counts; /* class entry -> live tagged objects (IS_LONG) */
} cm_live;
#define CM_KEY(ce) ((zend_ulong) (((uintptr_t) (ce)) >> 3))
#define CM_CE(key) ((zend_class_entry *) (((uintptr_t) (key)) << 3))
static void cm_live_ready(void)
{
if (!cm_live.ready) {
zend_hash_init(&cm_live.counts, 64, NULL, NULL, 0);
cm_live.ready = true;
cm_live.has_pending = false;
}
}
static zend_always_inline void cm_tag(zend_object *o)
{
zval *c = zend_hash_index_lookup(&cm_live.counts, CM_KEY(o->ce));
o->handlers = &cm_tag_handlers;
if (Z_TYPE_P(c) == IS_LONG) {
Z_LVAL_P(c)++;
} else {
ZVAL_LONG(c, 1);
}
}
static void cm_tag_free(zend_object *o)
{
if (cm_live.ready) {
zval *c = zend_hash_index_find(&cm_live.counts, CM_KEY(o->ce));
if (c && Z_LVAL_P(c) > 0) {
Z_LVAL_P(c)--;
}
}
o->handlers = &std_object_handlers;
zend_object_std_dtor(o);
}
static zend_object *cm_tag_clone(zend_object *old)
{
zend_object *n = zend_objects_clone_obj(old);
if (n && cm_live.ready && n->handlers == &std_object_handlers) {
cm_tag(n);
}
return n;
}
static void cm_settle(void)
{
if (!cm_live.has_pending) {
return;
}
cm_live.has_pending = false;
uint32_t h = cm_live.pending;
if (h > 0 && h < EG(objects_store).top) {
zend_object *o = EG(objects_store).object_buckets[h];
if (IS_OBJ_VALID(o) && o->handlers == &std_object_handlers) {
cm_tag(o);
}
}
}
static int cm_new_handler(zend_execute_data *execute_data)
{
cm_live_ready();
cm_settle();
cm_live.pending = EG(objects_store).free_list_head != -1
? (uint32_t) EG(objects_store).free_list_head
: EG(objects_store).top;
cm_live.has_pending = true;
if (cm_prev_new_handler) {
return cm_prev_new_handler(execute_data);
}
return ZEND_USER_OPCODE_DISPATCH;
}
/* Pass on objects_store: tags what is not tagged yet, counts live objects
* and, if `others` is given, the untaggable ones per class. */
static uint32_t cm_reconcile(HashTable *others)
{
uint32_t alive = 0;
zend_object **b = EG(objects_store).object_buckets;
cm_live_ready();
cm_settle();
for (uint32_t h = 1; h < EG(objects_store).top; h++) {
zend_object *o = b[h];
if (!IS_OBJ_VALID(o)) {
continue;
}
alive++;
if (o->handlers == &cm_tag_handlers) {
continue;
}
if (o->handlers == &std_object_handlers) {
cm_tag(o);
} else if (others) {
zval *c = zend_hash_index_lookup(others, CM_KEY(o->ce));
if (Z_TYPE_P(c) == IS_LONG) {
Z_LVAL_P(c)++;
} else {
ZVAL_LONG(c, 1);
}
}
}
return alive;
}
static void cm_live_shutdown(void)
{
if (!cm_live.ready) {
return;
}
zend_object **b = EG(objects_store).object_buckets;
if (b) {
for (uint32_t h = 1; h < EG(objects_store).top; h++) {
zend_object *o = b[h];
if (IS_OBJ_VALID(o) && o->handlers == &cm_tag_handlers) {
o->handlers = &std_object_handlers;
}
}
}
zend_hash_destroy(&cm_live.counts);
cm_live.ready = false;
cm_live.has_pending = false;
}
static int cm_count_cmp(Bucket *a, Bucket *b)
{
zend_long x = Z_LVAL(a->val), y = Z_LVAL(b->val);
if (x != y) {
return x < y ? 1 : -1;
}
return zend_binary_strcmp(ZSTR_VAL(a->key), ZSTR_LEN(a->key), ZSTR_VAL(b->key), ZSTR_LEN(b->key));
}
static void cm_add_count(HashTable *by_name, zend_string *name, zend_long n)
{
zval *c = zend_hash_lookup(by_name, name);
if (Z_TYPE_P(c) == IS_LONG) {
Z_LVAL_P(c) += n;
} else {
ZVAL_LONG(c, n);
}
}
/* Fills `out` (initialised array) with class => count, sorted by count desc,
* at most `top` entries (top <= 0: all). */
static void cm_class_counts(zval *out, zend_long top)
{
HashTable by_name;
zend_hash_init(&by_name, 64, NULL, NULL, 0);
if (cm_live_installed) {
HashTable others;
zend_hash_init(&others, 16, NULL, NULL, 0);
cm_reconcile(&others);
zend_ulong key; zval *v;
ZEND_HASH_FOREACH_NUM_KEY_VAL(&cm_live.counts, key, v) {
if (Z_LVAL_P(v) > 0) {
cm_add_count(&by_name, CM_CE(key)->name, Z_LVAL_P(v));
}
} ZEND_HASH_FOREACH_END();
ZEND_HASH_FOREACH_NUM_KEY_VAL(&others, key, v) {
cm_add_count(&by_name, CM_CE(key)->name, Z_LVAL_P(v));
} ZEND_HASH_FOREACH_END();
zend_hash_destroy(&others);
} else {
for (uint32_t h = 1; h < EG(objects_store).top; h++) {
zend_object *o = EG(objects_store).object_buckets[h];
if (IS_OBJ_VALID(o)) {
cm_add_count(&by_name, o->ce->name, 1);
}
}
}
zend_hash_sort(&by_name, cm_count_cmp, 0);
zend_string *k; zval *v; zend_long i = 0;
ZEND_HASH_FOREACH_STR_KEY_VAL(&by_name, k, v) {
if (top > 0 && i++ >= top) {
break;
}
add_assoc_long_ex(out, ZSTR_VAL(k), ZSTR_LEN(k), Z_LVAL_P(v));
} ZEND_HASH_FOREACH_END();
zend_hash_destroy(&by_name);
}
/* ======================================================================== */
/* Collecting a sample */
/* ======================================================================== */
enum {
M_MEM, M_MEM_REAL, M_PEAK, M_PEAK_REAL, M_RSS, M_OBJ, M_STORE,
M_GC_RUNS, M_GC_COLL, M_GC_NS, M_FREEZES, M_THAWS, M_RESTARTS,
M_LAST_US, M_LAST_BYTES, M_RECLAIMED, M_COUNT
};
_Static_assert(M_COUNT == CRYOM_SAMPLE_FIELDS, "sample fields out of sync with cryo_format.h");
typedef struct {
double time; /* unix seconds */
uint32_t seq;
uint32_t flags; /* CRYOM_FLAG_RSS */
uint64_t v[M_COUNT];
uint32_t gc_roots;
} cm_snap;
static double cm_now(void)
{
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
return (double) ts.tv_sec + (double) ts.tv_nsec / 1e9;
}
static bool cm_rss(uint64_t *out)
{
#if defined(__linux__) && !defined(CRYO_BACKEND_WASM)
char buf[128];
int fd = open("/proc/self/statm", O_RDONLY | O_CLOEXEC);
if (fd < 0) {
return false;
}
ssize_t n = read(fd, buf, sizeof(buf) - 1);
close(fd);
if (n <= 0) {
return false;
}
buf[n] = '\0';
char *p = buf;
strtoull(p, &p, 10); /* size */
unsigned long long pages = strtoull(p, NULL, 10); /* resident */
long pg = sysconf(_SC_PAGESIZE);
*out = (uint64_t) pages * (uint64_t) (pg > 0 ? pg : 4096);
return true;
#else
(void) out;
return false;
#endif
}
static void cm_collect(cm_snap *s)
{
memset(s, 0, sizeof(*s));
s->time = cm_now();
s->v[M_MEM] = zend_memory_usage(0);
s->v[M_MEM_REAL] = zend_memory_usage(1);
s->v[M_PEAK] = zend_memory_peak_usage(0);
s->v[M_PEAK_REAL] = zend_memory_peak_usage(1);
if (cm_rss(&s->v[M_RSS])) {
s->flags |= CRYOM_FLAG_RSS;
}
uint32_t alive = 0;
if (cm_live_installed && EG(objects_store).object_buckets) {
alive = cm_reconcile(NULL);
} else {
for (uint32_t h = 1; h < EG(objects_store).top; h++) {
if (IS_OBJ_VALID(EG(objects_store).object_buckets[h])) {
alive++;
}
}
}
s->v[M_OBJ] = alive;
s->v[M_STORE] = EG(objects_store).size;
zend_gc_status gc;
zend_gc_get_status(&gc);
s->v[M_GC_RUNS] = gc.runs;
s->v[M_GC_COLL] = gc.collected;
s->v[M_GC_NS] = (uint64_t) gc.collector_time;
s->gc_roots = gc.num_roots;
s->v[M_FREEZES] = cryo_stats.freezes;
s->v[M_THAWS] = cryo_stats.thaws;
s->v[M_RESTARTS] = cryo_stats.restarts;
s->v[M_LAST_US] = cryo_stats.last_freeze_ms > 0 ? (uint64_t) (cryo_stats.last_freeze_ms * 1000.0 + 0.5) : 0;
s->v[M_LAST_BYTES] = cryo_stats.last_freeze_bytes;
s->v[M_RECLAIMED] = cryo_stats.bytes_reclaimed;
}
/* Metric catalogue shared by Prometheus, StatsD and OTLP. */
typedef struct {
const char *dot; /* StatsD / OTLP name (after the prefix) */
const char *prom; /* Prometheus name */
const char *help;
const char *unit; /* OTLP unit */
uint8_t counter; /* monotonic */
uint8_t idx; /* M_* or 255 = overhead (derived) */
double scale;
} cm_def;
#define M_OVERHEAD 255
static const cm_def cm_defs[] = {
{ "memory.used", "php_memory_used_bytes", "Zend MM memory in use (memory_get_usage())", "By", 0, M_MEM, 1 },
{ "memory.allocated", "php_memory_allocated_bytes", "Zend MM memory reserved from the system (memory_get_usage(true))", "By", 0, M_MEM_REAL, 1 },
{ "memory.peak", "php_memory_peak_bytes", "Peak Zend MM memory in use", "By", 0, M_PEAK, 1 },
{ "memory.peak_allocated", "php_memory_peak_allocated_bytes", "Peak Zend MM memory reserved from the system", "By", 0, M_PEAK_REAL, 1 },
{ "memory.rss", "php_memory_rss_bytes", "Resident set size of the process", "By", 0, M_RSS, 1 },
{ "memory.overhead", "php_memory_overhead_bytes", "System memory not accounted by Zend MM (rss - allocated)", "By", 0, M_OVERHEAD, 1 },
{ "objects.alive", "php_objects_alive", "Live objects in the objects store", "{objects}", 0, M_OBJ, 1 },
{ "objects.store_size", "php_objects_store_size", "Capacity of the objects store", "{slots}", 0, M_STORE, 1 },
{ "gc.runs", "php_gc_runs_total", "Cycle collector runs", "{runs}", 1, M_GC_RUNS, 1 },
{ "gc.collected", "php_gc_collected_total", "Values freed by the cycle collector", "{values}", 1, M_GC_COLL, 1 },
{ "gc.time", "php_gc_time_seconds_total", "Time spent in the cycle collector", "s", 1, M_GC_NS, 1e-9 },
{ "cryo.freezes", "php_cryo_freezes_total", "Successful cryo_freeze() calls", "{freezes}", 1, M_FREEZES, 1 },
{ "cryo.thaws", "php_cryo_thaws_total", "Successful thaws", "{thaws}", 1, M_THAWS, 1 },
{ "cryo.restarts", "php_cryo_restarts_total", "cryo_restart() calls", "{restarts}", 1, M_RESTARTS, 1 },
{ "cryo.last_freeze_ms", "php_cryo_last_freeze_milliseconds","Duration of the last freeze", "ms", 0, M_LAST_US, 0.001 },
{ "cryo.last_freeze_bytes","php_cryo_last_freeze_bytes", "Size of the last freeze file", "By", 0, M_LAST_BYTES, 1 },
{ "cryo.bytes_reclaimed", "php_cryo_bytes_reclaimed_total", "Memory given back by restarts", "By", 1, M_RECLAIMED, 1 },
};
#define CM_NDEFS (sizeof(cm_defs) / sizeof(cm_defs[0]))
#define CM_CLASS_TOP 50
static bool cm_def_value(const cm_def *d, const cm_snap *s, double *out)
{
if (d->idx == M_RSS || d->idx == M_OVERHEAD) {
if (!(s->flags & CRYOM_FLAG_RSS)) {
return false;
}
}
if (d->idx == M_OVERHEAD) {
*out = (double) s->v[M_RSS] - (double) s->v[M_MEM_REAL];
} else {
*out = (double) s->v[d->idx] * d->scale;
}
return true;
}
static void cm_fmt(char *buf, size_t len, double v)
{
if (v == floor(v) && fabs(v) < 9007199254740992.0) {
snprintf(buf, len, "%.0f", v);
} else {
snprintf(buf, len, "%.9g", v);
}
}
/* ======================================================================== */
/* Output formats */
/* ======================================================================== */
static void cm_prom_text(smart_str *out, const cm_snap *s, zval *classes)
{
char num[64];
for (size_t i = 0; i < CM_NDEFS; i++) {
double v;
if (!cm_def_value(&cm_defs[i], s, &v)) {
continue;
}
cm_fmt(num, sizeof(num), v);
smart_str_append_printf(out, "# HELP %s %s\n# TYPE %s %s\n%s %s\n",
cm_defs[i].prom, cm_defs[i].help, cm_defs[i].prom,
cm_defs[i].counter ? "counter" : "gauge", cm_defs[i].prom, num);
}
if (classes && zend_hash_num_elements(Z_ARRVAL_P(classes))) {
smart_str_appends(out, "# HELP php_class_objects Live objects per class (top " ZEND_TOSTR(CM_CLASS_TOP) ")\n# TYPE php_class_objects gauge\n");
zend_string *k; zval *v;
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(classes), k, v) {
smart_str_appends(out, "php_class_objects{class=\"");
for (size_t j = 0; j < ZSTR_LEN(k); j++) {
char c = ZSTR_VAL(k)[j];
if (c == '\\' || c == '"') {
smart_str_appendc(out, '\\');
smart_str_appendc(out, c);
} else if (c == '\n') {
smart_str_appends(out, "\\n");
} else {
smart_str_appendc(out, c);
}
}
smart_str_append_printf(out, "\"} " ZEND_LONG_FMT "\n", Z_LVAL_P(v));
} ZEND_HASH_FOREACH_END();
}
smart_str_appends(out, "# HELP php_cryo_samples_total Samples taken by Cryo\n# TYPE php_cryo_samples_total counter\n");
smart_str_append_printf(out, "php_cryo_samples_total %u\n", s->seq);
smart_str_0(out);
}
static void cm_statsd_lines(smart_str *out, const cm_snap *s, zval *classes, const char *prefix, size_t plen)
{
char num[64];
for (size_t i = 0; i < CM_NDEFS; i++) {
double v;
if (!cm_def_value(&cm_defs[i], s, &v)) {
continue;
}
cm_fmt(num, sizeof(num), v);
if (plen) {
smart_str_appendl(out, prefix, plen);
smart_str_appendc(out, '.');
}
smart_str_append_printf(out, "%s:%s|g\n", cm_defs[i].dot, num);
}
if (classes) {
zend_string *k; zval *v;
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(classes), k, v) {
if (plen) {
smart_str_appendl(out, prefix, plen);
smart_str_appendc(out, '.');
}
smart_str_append_printf(out, "class.objects:" ZEND_LONG_FMT "|g|#class:", Z_LVAL_P(v));
for (size_t j = 0; j < ZSTR_LEN(k); j++) {
char c = ZSTR_VAL(k)[j];
smart_str_appendc(out, (c == ',' || c == '|' || c == '#' || c == '\n') ? '_' : c);
}
smart_str_appendc(out, '\n');
} ZEND_HASH_FOREACH_END();
}
smart_str_0(out);
}
static void cm_json_str(smart_str *out, const char *s, size_t len)
{
smart_str_appendc(out, '"');
for (size_t i = 0; i < len; i++) {
unsigned char c = (unsigned char) s[i];
if (c == '"' || c == '\\') {
smart_str_appendc(out, '\\');
smart_str_appendc(out, (char) c);
} else if (c < 0x20) {
smart_str_append_printf(out, "\\u%04x", c);
} else {
smart_str_appendc(out, (char) c);
}
}
smart_str_appendc(out, '"');
}
static double cm_series_start;
static void cm_otlp_json(smart_str *out, const cm_snap *s, zval *classes)
{
char num[64], tns[32], sns[32];
snprintf(tns, sizeof(tns), "%.0f", s->time * 1e9);
snprintf(sns, sizeof(sns), "%.0f", (cm_series_start > 0 ? cm_series_start : s->time) * 1e9);
smart_str_appends(out, "{\"resourceMetrics\":[{\"resource\":{\"attributes\":["
"{\"key\":\"service.name\",\"value\":{\"stringValue\":\"php\"}},"
"{\"key\":\"telemetry.sdk.name\",\"value\":{\"stringValue\":\"ext-cryo\"}},");
smart_str_append_printf(out, "{\"key\":\"process.pid\",\"value\":{\"intValue\":\"%ld\"}}]},", (long) getpid());
smart_str_appends(out, "\"scopeMetrics\":[{\"scope\":{\"name\":\"cryo\",\"version\":\"" PHP_CRYO_VERSION "\"},\"metrics\":[");
bool first = true;
for (size_t i = 0; i < CM_NDEFS; i++) {
const cm_def *d = &cm_defs[i];
double v;
if (!cm_def_value(d, s, &v)) {
continue;
}
cm_fmt(num, sizeof(num), v);
bool is_int = d->scale == 1;
if (!first) {
smart_str_appendc(out, ',');
}
first = false;
smart_str_append_printf(out, "{\"name\":\"php.%s\",\"description\":", d->dot);
cm_json_str(out, d->help, strlen(d->help));
smart_str_append_printf(out, ",\"unit\":\"%s\",", d->unit);
if (d->counter) {
smart_str_append_printf(out, "\"sum\":{\"aggregationTemporality\":2,\"isMonotonic\":true,"
"\"dataPoints\":[{\"startTimeUnixNano\":\"%s\",\"timeUnixNano\":\"%s\",", sns, tns);
} else {
smart_str_append_printf(out, "\"gauge\":{\"dataPoints\":[{\"timeUnixNano\":\"%s\",", tns);
}
if (is_int) {
smart_str_append_printf(out, "\"asInt\":\"%s\"}]}}", num);
} else {
smart_str_append_printf(out, "\"asDouble\":%s}]}}", num);
}
}
if (classes && zend_hash_num_elements(Z_ARRVAL_P(classes))) {
smart_str_appends(out, ",{\"name\":\"php.class.objects\",\"description\":\"Live objects per class\","
"\"unit\":\"{objects}\",\"gauge\":{\"dataPoints\":[");
zend_string *k; zval *v; bool f2 = true;
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(classes), k, v) {
if (!f2) {
smart_str_appendc(out, ',');
}
f2 = false;
smart_str_append_printf(out, "{\"timeUnixNano\":\"%s\",\"asInt\":\"" ZEND_LONG_FMT "\","
"\"attributes\":[{\"key\":\"class\",\"value\":{\"stringValue\":", tns, Z_LVAL_P(v));
cm_json_str(out, ZSTR_VAL(k), ZSTR_LEN(k));
smart_str_appends(out, "}}]}");
} ZEND_HASH_FOREACH_END();
smart_str_appends(out, "]}}");
}
smart_str_appends(out, "]}]}]}");
smart_str_0(out);
}
/* ======================================================================== */
/* Binary encoding (little-endian, see cryo_format.h) */
/* ======================================================================== */
static inline void put_u16(unsigned char *p, uint16_t v) { p[0] = (unsigned char) v; p[1] = (unsigned char) (v >> 8); }
static inline void put_u32(unsigned char *p, uint32_t v) { for (int i = 0; i < 4; i++) p[i] = (unsigned char) (v >> (8 * i)); }
static inline void put_u64(unsigned char *p, uint64_t v) { put_u32(p, (uint32_t) v); put_u32(p + 4, (uint32_t) (v >> 32)); }
static inline void put_f64(unsigned char *p, double d) { uint64_t v; memcpy(&v, &d, 8); put_u64(p, v); }
static inline uint16_t get_u16(const unsigned char *p) { return (uint16_t) (p[0] | (p[1] << 8)); }
static inline uint32_t get_u32(const unsigned char *p) { return (uint32_t) p[0] | ((uint32_t) p[1] << 8) | ((uint32_t) p[2] << 16) | ((uint32_t) p[3] << 24); }
static inline uint64_t get_u64(const unsigned char *p) { return (uint64_t) get_u32(p) | ((uint64_t) get_u32(p + 4) << 32); }
static inline double get_f64(const unsigned char *p) { uint64_t v = get_u64(p); double d; memcpy(&d, &v, 8); return d; }
static void cm_encode_sample(unsigned char *p, const cm_snap *s)
{
put_f64(p, s->time);
put_u32(p + 8, s->seq);
put_u32(p + 12, s->flags);
for (int i = 0; i < M_COUNT; i++) {
put_u64(p + 16 + 8 * i, s->v[i]);
}
}
/* ======================================================================== */
/* State */
/* ======================================================================== */
static struct {
bool active;
int fd;
zend_string *path; /* absolute path of the .cryom */
uint32_t ms;
uint32_t seq; /* next sample number */
bool in_tick;
bool otlp_tls_warned;
uint64_t otlp_sent, otlp_errors;
} cm_series = { false, -1, NULL, 0, 0, false, false, 0, 0 };
static zend_atomic_bool cm_tick_due;
static void (*cm_prev_interrupt)(zend_execute_data *execute_data);
#ifdef CRYO_HAVE_SOCKETS
/* Exporter child (cryo_metrics_serve) and its channel, see "Live channel". */
static struct {
pid_t pid;
pid_t owner; /* process that started it: a fork of it does not own the child */
int fd; /* parent end of the socketpair */
int port;
bool dead; /* the child went away */
zval snap_opts; /* IS_UNDEF or the options passed to cryo_snapshot() */
char *out; /* frames not written yet (the parent never blocks) */
size_t out_len, out_cap;
} cm_serve = { 0, 0, -1, 0, false };
/* Set by the ticker thread when the child asks for a snapshot. */
static zend_atomic_bool cm_snap_due;
static zend_atomic_int cm_snap_id;
static struct {
bool running;
pid_t owner; /* threads do not survive fork() */
uint32_t ms;
int chan; /* channel watched by the thread, -1 = none */
int wake[2]; /* self-pipe: stop request */
pthread_t thread;
zend_atomic_bool stop;
zend_atomic_bool *interrupt;
} cm_ticker = { false, 0, 0, -1, { -1, -1 } };
#else
static struct {
uint32_t ms;
double next_due; /* cryo_host_now() ms */
bool serving;
} cm_ticker = { 0, 0, false };
#endif
/* ======================================================================== */
/* Ticker */
/* ======================================================================== */
#ifdef CRYO_HAVE_SOCKETS
/* Channel frames: u32 length (of what follows) | u8 type | payload.
* Parent -> child: */
#define CM_F_PROM 'P' /* Prometheus text */
#define CM_F_JSON 'J' /* /metrics.json body */
#define CM_F_SAMPLE 'S' /* one .cryom sample (CRYOM_SAMPLE_LEN bytes) */
#define CM_F_MARK 'M' /* f64 time | u32 seq | label */
#define CM_F_SERIES 'A' /* u8 active | u32 interval_ms | path */
#define CM_F_SNAPSHOT 'F' /* u32 id | u8 ok | path of the .cryo, or error message */
/* Child -> parent (read by the ticker thread): */
#define CM_F_REQUEST 'R' /* u32 id: take a snapshot at the next safe point */
#define CM_F_CANCEL 'X' /* u32 id: the child gave up waiting */
#define CM_CHAN_IN 64
static int64_t cm_mono_ms(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (int64_t) ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
}
/* Ticker thread: reads the child's requests. False when the channel is gone. */
static bool cm_chan_pump(int fd, unsigned char *buf, size_t *len)
{
for (;;) {
ssize_t r = recv(fd, buf + *len, CM_CHAN_IN - *len, MSG_DONTWAIT);
if (r < 0) {
if (errno == EINTR) {
continue;
}
return errno == EAGAIN || errno == EWOULDBLOCK;
}
if (r == 0) {
return false;
}
*len += (size_t) r;
while (*len >= 4) {
uint32_t fl = get_u32(buf);
if (fl < 1 || fl > CM_CHAN_IN - 4) {
return false;
}
if (*len < 4 + (size_t) fl) {
break;
}
if (fl >= 5) {
uint32_t id = get_u32(buf + 5);
if (buf[4] == CM_F_REQUEST) {
zend_atomic_int_store_ex(&cm_snap_id, (int) id);
zend_atomic_bool_store_ex(&cm_snap_due, true);
zend_atomic_bool_store_ex(cm_ticker.interrupt, true);
} else if (buf[4] == CM_F_CANCEL && (uint32_t) zend_atomic_int_load_ex(&cm_snap_id) == id) {
zend_atomic_bool_store_ex(&cm_snap_due, false);
}
}
memmove(buf, buf + 4 + fl, *len - 4 - fl);
*len -= 4 + (size_t) fl;
}
}
}
static void *cm_ticker_main(void *arg)
{
(void) arg;
unsigned char in[CM_CHAN_IN];
size_t inlen = 0;
int chan = cm_ticker.chan;
int64_t ms = cm_ticker.ms;
int64_t next = cm_mono_ms() + ms;
while (!zend_atomic_bool_load_ex(&cm_ticker.stop)) {
int64_t now = cm_mono_ms();
if (now >= next) {
zend_atomic_bool_store_ex(&cm_tick_due, true);
zend_atomic_bool_store_ex(cm_ticker.interrupt, true);
next += ms;
if (next <= now) {
next = now + ms;
}
continue;
}
struct pollfd pf[2] = { { cm_ticker.wake[0], POLLIN, 0 }, { chan, POLLIN, 0 } };
int64_t wait = next - now;
int n = poll(pf, chan >= 0 ? 2 : 1, wait > INT_MAX ? INT_MAX : (int) wait);
if (n <= 0) {
continue;
}
if (pf[0].revents) {
break; /* stop */
}
if (chan >= 0 && pf[1].revents && !cm_chan_pump(chan, in, &inlen)) {
chan = -1; /* child gone: keep ticking */
}
}
return NULL;
}
static void cm_ticker_stop(void)
{
if (!cm_ticker.running) {
return;
}
if (cm_ticker.owner == getpid()) {
zend_atomic_bool_store_ex(&cm_ticker.stop, true);
char b = 1;
while (write(cm_ticker.wake[1], &b, 1) < 0 && errno == EINTR) {
}
pthread_join(cm_ticker.thread, NULL);
}
close(cm_ticker.wake[0]);
close(cm_ticker.wake[1]);
cm_ticker.wake[0] = cm_ticker.wake[1] = -1;
cm_ticker.running = false;
cm_ticker.ms = 0;
cm_ticker.chan = -1;
}
static void cm_ticker_start(uint32_t ms, int chan)
{
if (pipe(cm_ticker.wake) != 0) {
php_error_docref(NULL, E_WARNING, "Cannot start the sampling thread: %s", strerror(errno));
return;
}
fcntl(cm_ticker.wake[0], F_SETFD, FD_CLOEXEC);
fcntl(cm_ticker.wake[1], F_SETFD, FD_CLOEXEC);
zend_atomic_bool_store_ex(&cm_ticker.stop, false);
cm_ticker.ms = ms;
cm_ticker.chan = chan;
cm_ticker.owner = getpid();
cm_ticker.interrupt = &EG(vm_interrupt);
/* signals stay with the PHP thread (max_execution_time, pcntl) */
sigset_t all, old;
sigfillset(&all);
pthread_sigmask(SIG_SETMASK, &all, &old);
int rc = pthread_create(&cm_ticker.thread, NULL, cm_ticker_main, NULL);
pthread_sigmask(SIG_SETMASK, &old, NULL);
if (rc != 0) {
close(cm_ticker.wake[0]);
close(cm_ticker.wake[1]);
cm_ticker.wake[0] = cm_ticker.wake[1] = -1;
cm_ticker.ms = 0;
cm_ticker.chan = -1;
php_error_docref(NULL, E_WARNING, "Cannot start the sampling thread: %s", strerror(rc));
return;
}
cm_ticker.running = true;
}
static bool cm_serving(void);
static void cm_ticker_update(void)
{
bool serving = cm_serving();
uint32_t want = cm_series.active ? cm_series.ms : (serving ? 1000 : 0);
int chan = serving ? cm_serve.fd : -1;
if (cm_ticker.running && cm_ticker.owner == getpid() && want == cm_ticker.ms && chan == cm_ticker.chan) {
return;
}
if (!cm_ticker.running && want == 0) {
return;
}
cm_ticker_stop();
if (want) {
cm_ticker_start(want, chan);
}
}
#else /* WASM */
static void cm_ticker_update(void)
{
uint32_t want = cm_series.active ? cm_series.ms : (cm_ticker.serving ? 1000 : 0);
if (want == cm_ticker.ms) {
return;
}
cm_ticker.ms = want;
if (want) {
cm_ticker.next_due = cryo_host_now() + want;
cryo_host_timer(want);
}
}
#endif
void cryo_metrics_timer_fired(void)
{
#ifdef CRYO_BACKEND_WASM
if (cm_ticker.ms == 0) {
return;
}
#endif
zend_atomic_bool_store_ex(&cm_tick_due, true);
zend_atomic_bool_store_ex(&EG(vm_interrupt), true);
}
#ifdef CRYO_BACKEND_WASM
/* Called by @cryo/wasm-host when cryo_host_timer() elapses. Weak: another
* part of ext-cryo may own it, it must then call cryo_metrics_timer_fired(). */
__attribute__((weak, used)) void cryo_timer_fired(void)
{
cryo_metrics_timer_fired();
}
#endif
/* ======================================================================== */
/* Exporters (sockets) */
/* ======================================================================== */
#ifdef CRYO_HAVE_SOCKETS
static bool cm_write_all(int fd, const char *p, size_t n)
{
while (n > 0) {
ssize_t w = send(fd, p, n, MSG_NOSIGNAL);
if (w < 0) {
if (errno == EINTR) {
continue;
}
return false;
}
p += w;
n -= (size_t) w;
}
return true;
}
/* "host:port", "[v6]:port", with default port. host/port buffers filled. */
static bool cm_split_hostport(const char *s, size_t len, char *host, size_t hcap, char *port, size_t pcap, const char *defport)
{
const char *hs = s, *he, *ps = NULL;
const char *end = s + len;
if (len && s[0] == '[') {
hs = s + 1;
he = memchr(hs, ']', (size_t) (end - hs));
if (!he) {
return false;
}
if (he + 1 < end && he[1] == ':') {
ps = he + 2;
}
} else {
he = memchr(s, ':', len);
if (he) {
ps = he + 1;
} else {
he = end;
}
}
size_t hl = (size_t) (he - hs);
if (hl == 0 || hl >= hcap) {
return false;
}
memcpy(host, hs, hl);
host[hl] = '\0';
if (ps && ps < end) {
size_t pl = (size_t) (end - ps);
if (pl >= pcap) {
return false;
}
memcpy(port, ps, pl);
port[pl] = '\0';
} else {
snprintf(port, pcap, "%s", defport);
}
return true;
}
static int cm_tcp_connect(const char *host, const char *port, int timeout_ms)
{
struct addrinfo hints, *res = NULL, *ai;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if (getaddrinfo(host, port, &hints, &res) != 0) {
return -1;
}
int fd = -1;
for (ai = res; ai; ai = ai->ai_next) {
fd = socket(ai->ai_family, ai->ai_socktype | SOCK_CLOEXEC, ai->ai_protocol);
if (fd < 0) {
continue;
}
int fl = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, fl | O_NONBLOCK);
int rc = connect(fd, ai->ai_addr, ai->ai_addrlen);
if (rc != 0 && errno == EINPROGRESS) {
struct pollfd pf = { fd, POLLOUT, 0 };
int err = 0;
socklen_t el = sizeof(err);
if (poll(&pf, 1, timeout_ms) == 1 && getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &el) == 0 && err == 0) {
rc = 0;
}
}
if (rc == 0) {
fcntl(fd, F_SETFL, fl);
struct timeval tv = { timeout_ms / 1000, (timeout_ms % 1000) * 1000 };
setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
break;
}
close(fd);
fd = -1;
}
freeaddrinfo(res);
return fd;
}
/* POST body to http://host[:port][/path] (no TLS). Returns the HTTP status or -1. */
static int cm_http_post(const char *url, const char *body, size_t blen)
{
if (strncasecmp(url, "http://", 7) != 0) {
return -2;
}
const char *a = url + 7;
const char *slash = strchr(a, '/');
size_t alen = slash ? (size_t) (slash - a) : strlen(a);
char host[256], port[16];
if (!cm_split_hostport(a, alen, host, sizeof(host), port, sizeof(port), "4318")) {
return -1;
}
smart_str path = {0};
if (!slash || strcmp(slash, "/") == 0) {
smart_str_appends(&path, "/v1/metrics");
} else {
size_t sl = strlen(slash);
while (sl > 1 && slash[sl - 1] == '/') {
sl--;
}
smart_str_appendl(&path, slash, sl);
if (sl < 11 || memcmp(slash + sl - 11, "/v1/metrics", 11) != 0) {
smart_str_appends(&path, "/v1/metrics");
}
}
smart_str_0(&path);
int status = -1;
int fd = cm_tcp_connect(host, port, 1000);
if (fd >= 0) {
smart_str req = {0};
smart_str_append_printf(&req, "POST %s HTTP/1.1\r\nHost: %.*s\r\nUser-Agent: ext-cryo/" PHP_CRYO_VERSION "\r\n"
"Content-Type: application/json\r\nContent-Length: %zu\r\nConnection: close\r\n\r\n",
ZSTR_VAL(path.s), (int) alen, a, blen);
smart_str_appendl(&req, body, blen);
if (cm_write_all(fd, ZSTR_VAL(req.s), ZSTR_LEN(req.s))) {
char resp[64];
size_t got = 0;
while (got < sizeof(resp) - 1) {
ssize_t r = recv(fd, resp + got, sizeof(resp) - 1 - got, 0);
if (r < 0 && errno == EINTR) {
continue;
}
if (r <= 0) {
break;
}
got += (size_t) r;
if (memchr(resp, '\n', got)) {
break;
}
}
resp[got] = '\0';
if (got > 12 && strncmp(resp, "HTTP/", 5) == 0) {
const char *sp = strchr(resp, ' ');
if (sp) {
status = atoi(sp + 1);
}
}
}
smart_str_free(&req);
close(fd);
}
smart_str_free(&path);
return status;
}
#endif /* CRYO_HAVE_SOCKETS */
static bool cm_exporters_want_classes(void)
{
return cm_live_installed;
}
static void cm_otlp_send(const cm_snap *s, zval *classes)
{
const char *ep = INI_STR("cryo.otlp_endpoint");
if (!ep || !ep[0]) {
return;
}
smart_str body = {0};
cm_otlp_json(&body, s, classes);
#ifdef CRYO_HAVE_SOCKETS
if (strncasecmp(ep, "https://", 8) == 0) {
if (!cm_series.otlp_tls_warned) {
php_error_docref(NULL, E_WARNING, "cryo.otlp_endpoint: TLS is not supported, use http:// (e.g. a local collector)");
cm_series.otlp_tls_warned = true;
}
cm_series.otlp_errors++;
} else {
int st = cm_http_post(ep, ZSTR_VAL(body.s), ZSTR_LEN(body.s));
if (st >= 200 && st < 300) {
cm_series.otlp_sent++;
} else {
cm_series.otlp_errors++;
}
}
#else
smart_str msg = {0};
smart_str_appends(&msg, ep);
smart_str_appendc(&msg, '\n');
smart_str_append(&msg, body.s);
cryo_host_emit(CRYO_EMIT_OTLP, (const uint8_t *) ZSTR_VAL(msg.s), (uint32_t) ZSTR_LEN(msg.s));
smart_str_free(&msg);
cm_series.otlp_sent++;
#endif
smart_str_free(&body);
}
/* ======================================================================== */
/* Prometheus child */
/* ======================================================================== */
static void cm_add_u64(zval *arr, const char *key, uint64_t v)
{
if (v <= (uint64_t) ZEND_LONG_MAX) {
add_assoc_long(arr, key, (zend_long) v);
} else {
add_assoc_double(arr, key, (double) v);
}
}
/* Names of the sample fields (cryo_metrics_read(), /series.json). */
static const char *const cm_sample_names[M_COUNT] = {
"memory", "memory_real", "memory_peak", "memory_peak_real", "rss", "objects_alive",
"objects_store_size", "gc_runs", "gc_collected", "gc_time", "freezes", "thaws",
"restarts", "last_freeze_ms", "last_freeze_bytes", "bytes_reclaimed",
};
/* The array returned by cryo_metrics(). */
static void cm_metrics_array(zval *rv, const cm_snap *s)
{
cm_add_u64(rv, "memory", s->v[M_MEM]);
cm_add_u64(rv, "memory_real", s->v[M_MEM_REAL]);
cm_add_u64(rv, "memory_peak", s->v[M_PEAK]);
cm_add_u64(rv, "memory_peak_real", s->v[M_PEAK_REAL]);
if (s->flags & CRYOM_FLAG_RSS) {
cm_add_u64(rv, "memory_rss", s->v[M_RSS]);
double over = (double) s->v[M_RSS] - (double) s->v[M_MEM_REAL];
if (fabs(over) <= (double) ZEND_LONG_MAX) {
add_assoc_long(rv, "memory_overhead", (zend_long) over);
} else {
add_assoc_double(rv, "memory_overhead", over);
}
} else {
add_assoc_null(rv, "memory_rss");
add_assoc_null(rv, "memory_overhead");
}
cm_add_u64(rv, "objects_alive", s->v[M_OBJ]);
cm_add_u64(rv, "objects_store_size", s->v[M_STORE]);
cm_add_u64(rv, "gc_runs", s->v[M_GC_RUNS]);
cm_add_u64(rv, "gc_collected", s->v[M_GC_COLL]);
cm_add_u64(rv, "gc_roots", s->gc_roots);
add_assoc_double(rv, "gc_time", (double) s->v[M_GC_NS] / 1e9);
cm_add_u64(rv, "freezes", cryo_stats.freezes);
cm_add_u64(rv, "thaws", cryo_stats.thaws);
cm_add_u64(rv, "restarts", cryo_stats.restarts);
add_assoc_double(rv, "last_freeze_ms", cryo_stats.last_freeze_ms);
cm_add_u64(rv, "last_freeze_bytes", cryo_stats.last_freeze_bytes);
cm_add_u64(rv, "bytes_reclaimed", cryo_stats.bytes_reclaimed);
cm_add_u64(rv, "snapshot_epoch", cryo_track.epoch);
add_assoc_bool(rv, "tracking", cryo_track.enabled);
add_assoc_bool(rv, "class_counting", cm_live_installed);
add_assoc_bool(rv, "sampling", cm_series.active);
cm_add_u64(rv, "samples", cm_series.seq);
cm_add_u64(rv, "otlp_sent", cm_series.otlp_sent);
cm_add_u64(rv, "otlp_errors", cm_series.otlp_errors);
#ifdef CRYO_HAVE_SOCKETS
bool serving = cm_serve.fd >= 0 && !cm_serve.dead && cm_serve.owner == getpid();
add_assoc_long(rv, "serve_pid", serving ? (zend_long) cm_serve.pid : 0);
add_assoc_long(rv, "serve_port", serving ? (zend_long) cm_serve.port : 0);
#else
add_assoc_long(rv, "serve_pid", 0);
add_assoc_long(rv, "serve_port", 0);
#endif
}
#ifdef CRYO_HAVE_SOCKETS
/*
* Live channel
* ------------
* cryo_metrics_serve() forks a plain child (no PHP in it) that owns the HTTP
* socket. Parent and child talk over a socketpair with frames
* "u32 length | u8 type | payload" (CM_F_*):
* - at every tick the PHP thread sends the Prometheus text, the
* /metrics.json body and, while cryo_metrics_sample() runs, the new sample;
* marks and series start/stop are sent when they happen. The parent writes
* with MSG_DONTWAIT and keeps what did not fit for the next tick (a frame is
* never cut): it never blocks on the child.
* - POST /snapshot: the child sends R(id) and keeps the client waiting. The
* ticker thread of the parent (which polls the channel) reads it, sets a
* flag and EG(vm_interrupt); at the next safe point the PHP thread writes
* cryo_snapshot() into a 0600 temporary file and answers F(id, path). The
* child streams the file to the client and deletes it. After the timeout
* the child answers 504 and sends X(id); a late F is deleted.
*/
#define CM_OUT_MAX (8u << 20)
#define CM_RING 1024
#define CM_MARKS 256
#define CM_REQ_MAX 8192
#define CM_BODY_MAX (1u << 20)
static void cm_serve_stop(void)
{
zend_atomic_bool_store_ex(&cm_snap_due, false);
if (cm_serve.owner != getpid()) {
/* inherited through fork(): the child and the thread belong to the parent */
if (cm_serve.fd >= 0) {
close(cm_serve.fd);
}
cm_serve.fd = -1;
cm_serve.pid = 0;
cm_ticker_stop();
} else {
cm_ticker_stop(); /* it polls cm_serve.fd */
if (cm_serve.fd >= 0) {
close(cm_serve.fd);
cm_serve.fd = -1;
}
if (cm_serve.pid > 0) {
kill(cm_serve.pid, SIGTERM);
int st;
while (waitpid(cm_serve.pid, &st, 0) < 0 && errno == EINTR) {
}
cm_serve.pid = 0;
}
}
free(cm_serve.out);
cm_serve.out = NULL;
cm_serve.out_len = cm_serve.out_cap = 0;
zval_ptr_dtor(&cm_serve.snap_opts);
ZVAL_UNDEF(&cm_serve.snap_opts);
cm_serve.dead = false;
cm_serve.port = 0;
}
static void cm_chan_flush(void)
{
size_t off = 0;
while (off < cm_serve.out_len) {
ssize_t w = send(cm_serve.fd, cm_serve.out + off, cm_serve.out_len - off, MSG_NOSIGNAL | MSG_DONTWAIT);
if (w < 0) {
if (errno == EINTR) {
continue;
}
if (errno != EAGAIN && errno != EWOULDBLOCK) {
/* child gone; the fd is closed by cm_serve_stop() (the thread polls it) */
cm_serve.dead = true;
off = cm_serve.out_len;
int st;
waitpid(cm_serve.pid, &st, WNOHANG);
}
break;
}
off += (size_t) w;
}
if (off) {
memmove(cm_serve.out, cm_serve.out + off, cm_serve.out_len - off);
cm_serve.out_len -= off;
}
}
/* Queues one frame (type, a, b) and writes what the socket takes. */
static void cm_chan_send(uint8_t type, const void *a, size_t alen, const void *b, size_t blen)
{
if (cm_serve.fd < 0 || cm_serve.dead || cm_serve.owner != getpid()) {
return;
}
size_t need = 5 + alen + blen;
if (cm_serve.out_len + need > CM_OUT_MAX) {
cm_chan_flush();
if (cm_serve.out_len + need > CM_OUT_MAX) {
return; /* child not reading: drop (the next tick sends fresh values) */
}
}
if (cm_serve.out_len + need > cm_serve.out_cap) {
size_t cap = cm_serve.out_cap ? cm_serve.out_cap : 4096;
while (cap < cm_serve.out_len + need) {
cap *= 2;
}
char *n = realloc(cm_serve.out, cap);
if (!n) {
return;
}
cm_serve.out = n;
cm_serve.out_cap = cap;
}
unsigned char *p = (unsigned char *) cm_serve.out + cm_serve.out_len;
put_u32(p, (uint32_t) (1 + alen + blen));
p[4] = type;
if (alen) {
memcpy(p + 5, a, alen);
}
if (blen) {
memcpy(p + 5 + alen, b, blen);
}
cm_serve.out_len += need;
cm_chan_flush();
}
static void cm_chan_series_state(void)
{
unsigned char h[5];
h[0] = cm_series.active ? 1 : 0;
put_u32(h + 1, cm_series.active ? cm_series.ms : 0);
cm_chan_send(CM_F_SERIES, h, 5,
cm_series.active && cm_series.path ? ZSTR_VAL(cm_series.path) : NULL,
cm_series.active && cm_series.path ? ZSTR_LEN(cm_series.path) : 0);
}
/* Remote snapshot, at a safe point of the PHP thread (cm_interrupt). */
static void cm_remote_snapshot(void)
{
if (cm_serve.fd < 0 || cm_serve.dead || cm_serve.owner != getpid()) {
zend_atomic_bool_store_ex(&cm_snap_due, false);
return;
}
if (EG(exception) || cm_series.in_tick) {
return; /* still due: retried at the next interrupt */
}
if (!zend_atomic_bool_exchange_ex(&cm_snap_due, false)) {
return;
}
uint32_t id = (uint32_t) zend_atomic_int_load_ex(&cm_snap_id);
unsigned char h[5];
put_u32(h, id);
const char *tmp = php_get_temporary_directory();
zend_string *path = zend_strpprintf(0, "%s%scryo-live-%ld-%u.cryo", tmp,
tmp[0] && tmp[strlen(tmp) - 1] == '/' ? "" : "/", (long) getpid(), id);
zend_string *err = NULL;
int fd = open(ZSTR_VAL(path), O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC, 0600);
if (fd < 0 && errno == EEXIST && unlink(ZSTR_VAL(path)) == 0) {
fd = open(ZSTR_VAL(path), O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC, 0600);
}
if (fd < 0) {
err = zend_strpprintf(0, "cannot create %s: %s", ZSTR_VAL(path), strerror(errno));
} else {
close(fd);
HashTable *opts = Z_TYPE(cm_serve.snap_opts) == IS_ARRAY ? Z_ARRVAL(cm_serve.snap_opts) : NULL;
zend_result rc = cryo_write_snapshot(path, opts);
if (EG(exception)) {
zval rv;
zval *m = zend_read_property_ex(EG(exception)->ce, EG(exception), ZSTR_KNOWN(ZEND_STR_MESSAGE), 1, &rv);
err = m ? zval_get_string(m) : zend_string_init("exception", sizeof("exception") - 1, 0);
zend_clear_exception();
} else if (rc == FAILURE) {
err = zend_strpprintf(0, "cannot write %s", ZSTR_VAL(path));
}
if (err) {
unlink(ZSTR_VAL(path));
}
}
if (err) {
h[4] = 0;
cm_chan_send(CM_F_SNAPSHOT, h, 5, ZSTR_VAL(err), ZSTR_LEN(err));
zend_string_release(err);
} else {
h[4] = 1;
cm_chan_send(CM_F_SNAPSHOT, h, 5, ZSTR_VAL(path), ZSTR_LEN(path));
}
zend_string_release(path);
}
/* ---- child process: plain C, no PHP --------------------------------------- */
typedef struct {
pid_t parent;
const char *cors; /* NULL: no CORS headers */
const char *token; /* NULL: no authentication */
bool snap_enabled;
int snap_timeout_ms;
} cm_child_cfg;
typedef struct { char *p; size_t len, cap; } cm_buf;
typedef struct {
double time;
uint32_t seq;
char *label;
} cm_child_mark;
typedef struct {
cm_child_cfg cfg;
int pfd;
cm_buf prom, json;
bool series_active;
uint32_t series_ms, session;
char *series_file;
unsigned char *ring; /* CM_RING samples of CRYOM_SAMPLE_LEN bytes */
uint32_t ring_n, ring_head;
cm_child_mark marks[CM_MARKS];
uint32_t marks_n, marks_head;
cm_buf in; /* bytes from the parent */
int pend_fd; /* client waiting for a snapshot, -1 = none */
uint32_t pend_id, next_id;
int64_t pend_deadline;
} cm_child;
static void cb_reserve(cm_buf *b, size_t n)
{
if (b->len + n + 1 <= b->cap) {
return;
}
size_t cap = b->cap ? b->cap : 256;
while (cap < b->len + n + 1) {
cap *= 2;
}
char *p = realloc(b->p, cap);
if (!p) {
_exit(1);
}
b->p = p;
b->cap = cap;
}
static void cb_put(cm_buf *b, const char *s, size_t n)
{
cb_reserve(b, n);
memcpy(b->p + b->len, s, n);
b->len += n;
b->p[b->len] = '\0';
}
static void cb_puts(cm_buf *b, const char *s)
{
cb_put(b, s, strlen(s));
}
static void cb_printf(cm_buf *b, const char *fmt, ...) ZEND_ATTRIBUTE_FORMAT(printf, 2, 3);
static void cb_printf(cm_buf *b, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
int n = vsnprintf(NULL, 0, fmt, ap);
va_end(ap);
if (n <= 0) {
return;
}
cb_reserve(b, (size_t) n);
va_start(ap, fmt);
vsnprintf(b->p + b->len, (size_t) n + 1, fmt, ap);
va_end(ap);
b->len += (size_t) n;
}
static void cb_json_str(cm_buf *b, const char *s, size_t len)
{
cb_put(b, "\"", 1);
for (size_t i = 0; i < len; i++) {
unsigned char c = (unsigned char) s[i];
if (c == '"' || c == '\\') {
char e[2] = { '\\', (char) c };
cb_put(b, e, 2);
} else if (c < 0x20) {
cb_printf(b, "\\u%04x", c);
} else {
cb_put(b, (const char *) &c, 1);
}
}
cb_put(b, "\"", 1);
}
static void cm_child_close(int c)
{
shutdown(c, SHUT_WR);
close(c);
}
/* One response; CORS headers when configured. */
static void cm_child_send(cm_child *ch, int c, const char *status, const char *ctype, const char *extra,
const char *body, size_t blen, bool head)
{
cm_buf h = {0};
cb_printf(&h, "HTTP/1.1 %s\r\n", status);
if (ctype) {
cb_printf(&h, "Content-Type: %s\r\n", ctype);
}
cb_printf(&h, "Content-Length: %zu\r\nCache-Control: no-store\r\nConnection: close\r\n", blen);
if (ch->cfg.cors) {
cb_printf(&h, "Access-Control-Allow-Origin: %s\r\n", ch->cfg.cors);
if (strcmp(ch->cfg.cors, "*") != 0) {
cb_puts(&h, "Vary: Origin\r\n");
}
cb_puts(&h, "Access-Control-Expose-Headers: Content-Disposition, X-Cryo-Pid, X-Cryo-Snapshot\r\n");
}
if (extra) {
cb_puts(&h, extra);
}
cb_puts(&h, "\r\n");
if (!head && blen) {
cb_put(&h, body, blen);
}
cm_write_all(c, h.p, h.len);
free(h.p);
}
static void cm_child_error(cm_child *ch, int c, const char *status, const char *extra, const char *msg)
{
cm_buf b = {0};
cb_puts(&b, "{\"error\":");
cb_json_str(&b, msg, strlen(msg));
cb_puts(&b, "}\n");
cm_child_send(ch, c, status, "application/json", extra, b.p, b.len, false);
free(b.p);
}
/* Value of header `name` (case-insensitive) in the request head. */
static bool cm_header(const char *head, const char *name, char *out, size_t cap)
{
size_t nl = strlen(name);
const char *p = strchr(head, '\n');
while (p && p[1]) {
p++;
if (*p == '\r' || *p == '\n') {
break;
}
const char *e = strchr(p, '\n');
size_t ll = e ? (size_t) (e - p) : strlen(p);
if (ll > nl && strncasecmp(p, name, nl) == 0 && p[nl] == ':') {
const char *v = p + nl + 1;
const char *ve = p + ll;
while (v < ve && (*v == ' ' || *v == '\t')) {
v++;
}
while (ve > v && (ve[-1] == '\r' || ve[-1] == ' ' || ve[-1] == '\t')) {
ve--;
}
size_t vl = (size_t) (ve - v);
if (vl >= cap) {
vl = cap - 1;
}
memcpy(out, v, vl);
out[vl] = '\0';
return true;
}
p = e;
}
return false;
}
static int cm_hexval(char c)
{
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
return -1;
}
/* URL-decoded value of `name` in a query string (ends at ' ' or '\0'). */
static bool cm_query_param(const char *q, const char *name, char *out, size_t cap)
{
size_t nl = strlen(name);
while (q && *q && *q != ' ') {
const char *amp = q;
while (*amp && *amp != '&' && *amp != ' ') {
amp++;
}
if ((size_t) (amp - q) >= nl && strncmp(q, name, nl) == 0 && (q[nl] == '=' || q + nl == amp)) {
const char *v = q + nl + (q[nl] == '=' ? 1 : 0);
size_t o = 0;
while (v < amp && o + 1 < cap) {
if (*v == '%' && v + 2 < amp && cm_hexval(v[1]) >= 0 && cm_hexval(v[2]) >= 0) {
out[o++] = (char) (cm_hexval(v[1]) * 16 + cm_hexval(v[2]));
v += 3;
} else {
out[o++] = *v == '+' ? ' ' : *v;
v++;
}
}
out[o] = '\0';
return true;
}
q = *amp == '&' ? amp + 1 : amp;
}
return false;
}
/* Constant-time comparison (time depends on the secret's length only). */
static bool cm_secret_equal(const char *given, const char *secret)
{
size_t gl = strlen(given), sl = strlen(secret);
unsigned char d = gl != sl;
for (size_t i = 0; i < sl; i++) {
d |= (unsigned char) ((i < gl ? given[i] : 0) ^ secret[i]);
}
return d == 0;
}
static bool cm_child_authorized(cm_child *ch, const char *head, const char *query)
{
if (!ch->cfg.token) {
return true;
}
char v[1024];
if (cm_header(head, "Authorization", v, sizeof(v)) && strncasecmp(v, "Bearer ", 7) == 0) {
const char *t = v + 7;
while (*t == ' ') {
t++;
}
if (cm_secret_equal(t, ch->cfg.token)) {
return true;
}
}
return query && cm_query_param(query, "token", v, sizeof(v)) && cm_secret_equal(v, ch->cfg.token);
}
static void cm_child_series_json(cm_child *ch, cm_buf *b, uint32_t since)
{
cb_printf(b, "{\"active\":%s,\"session\":%u,\"interval_ms\":%u,\"pid\":%ld,\"file\":",
ch->series_active ? "true" : "false", ch->session, ch->series_ms, (long) ch->cfg.parent);
if (ch->series_file) {
cb_json_str(b, ch->series_file, strlen(ch->series_file));
} else {
cb_puts(b, "null");
}
uint32_t next = since;
cb_puts(b, ",\"samples\":[");
bool first = true;
for (uint32_t k = 0; k < ch->ring_n; k++) {
uint32_t idx = (ch->ring_head + CM_RING - ch->ring_n + k) % CM_RING;
const unsigned char *pl = ch->ring + (size_t) idx * CRYOM_SAMPLE_LEN;
uint32_t seq = get_u32(pl + 8);
if (seq + 1 > next) {
next = seq + 1;
}
if (seq < since) {
continue;
}
uint32_t flags = get_u32(pl + 12);
cb_printf(b, "%s{\"time\":%.6f,\"seq\":%u", first ? "" : ",", get_f64(pl), seq);
first = false;
for (int i = 0; i < M_COUNT; i++) {
uint64_t v = get_u64(pl + 16 + 8 * i);
if (i == M_RSS && !(flags & CRYOM_FLAG_RSS)) {
cb_printf(b, ",\"%s\":null", cm_sample_names[i]);
} else if (i == M_GC_NS) {
cb_printf(b, ",\"%s\":%.9g", cm_sample_names[i], (double) v / 1e9);
} else if (i == M_LAST_US) {
cb_printf(b, ",\"%s\":%.9g", cm_sample_names[i], (double) v / 1000.0);
} else {
cb_printf(b, ",\"%s\":%llu", cm_sample_names[i], (unsigned long long) v);
}
}
cb_puts(b, "}");
}
cb_puts(b, "],\"marks\":[");
for (uint32_t k = 0; k < ch->marks_n; k++) {
const cm_child_mark *m = &ch->marks[(ch->marks_head + CM_MARKS - ch->marks_n + k) % CM_MARKS];
cb_printf(b, "%s{\"time\":%.6f,\"seq\":%u,\"label\":", k ? "," : "", m->time, m->seq);
cb_json_str(b, m->label, strlen(m->label));
cb_puts(b, "}");
}
cb_printf(b, "],\"next\":%u}\n", next);
}
static void cm_child_frame(cm_child *ch, uint8_t type, const unsigned char *p, size_t len);
/* Reads what the parent sent. False when the parent is gone. */
static bool cm_child_pump(cm_child *ch)
{
for (;;) {
cb_reserve(&ch->in, 65536);
ssize_t r = recv(ch->pfd, ch->in.p + ch->in.len, ch->in.cap - ch->in.len - 1, MSG_DONTWAIT);
if (r < 0) {
if (errno == EINTR) {
continue;
}
if (errno != EAGAIN && errno != EWOULDBLOCK) {
return false;
}
break;
}
if (r == 0) {
return false;
}
ch->in.len += (size_t) r;
}
size_t off = 0;
while (ch->in.len - off >= 4) {
const unsigned char *f = (const unsigned char *) ch->in.p + off;
uint32_t fl = get_u32(f);
if (fl < 1 || fl > CM_OUT_MAX) {
return false;
}
if (ch->in.len - off < 4 + (size_t) fl) {
break;
}
cm_child_frame(ch, f[4], f + 5, fl - 1);
off += 4 + (size_t) fl;
}
if (off) {
memmove(ch->in.p, ch->in.p + off, ch->in.len - off);
ch->in.len -= off;
}
return true;
}
static void cm_child_to_parent(cm_child *ch, uint8_t type, uint32_t id)
{
unsigned char f[9];
put_u32(f, 5);
f[4] = type;
put_u32(f + 5, id);
cm_write_all(ch->pfd, (const char *) f, sizeof(f));
}
static void cm_child_snapshot_done(cm_child *ch, uint32_t id, bool ok, const char *text, size_t tlen)
{
char path[4096];
if (tlen >= sizeof(path)) {
tlen = sizeof(path) - 1;
}
memcpy(path, text, tlen);
path[tlen] = '\0';
if (ch->pend_fd < 0 || id != ch->pend_id) {
if (ok) {
unlink(path); /* too late: the client got a 504 */
}
return;
}
int c = ch->pend_fd;
ch->pend_fd = -1;
if (!ok) {
char msg[600];
snprintf(msg, sizeof(msg), "snapshot failed: %s", path);
cm_child_error(ch, c, "500 Internal Server Error", NULL, msg);
cm_child_close(c);
return;
}
struct stat st;
int fd = open(path, O_RDONLY | O_CLOEXEC);
if (fd < 0 || fstat(fd, &st) != 0) {
if (fd >= 0) {
close(fd);
}
unlink(path);
cm_child_error(ch, c, "500 Internal Server Error", NULL, "cannot read the snapshot file");
cm_child_close(c);
return;
}
const char *base = strrchr(path, '/');
base = base ? base + 1 : path;
char extra[512];
snprintf(extra, sizeof(extra), "Content-Disposition: attachment; filename=\"%s\"\r\nX-Cryo-Pid: %ld\r\nX-Cryo-Snapshot: %u\r\n",
base, (long) ch->cfg.parent, id);
struct timeval tv = { 30, 0 };
setsockopt(c, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
cm_child_send(ch, c, "200 OK", "application/octet-stream", extra, NULL, (size_t) st.st_size, true);
char buf[65536];
for (;;) {
ssize_t r = read(fd, buf, sizeof(buf));
if (r < 0 && errno == EINTR) {
continue;
}
if (r <= 0 || !cm_write_all(c, buf, (size_t) r)) {
break;
}
}
close(fd);
unlink(path);
cm_child_close(c);
}
static void cm_child_frame(cm_child *ch, uint8_t type, const unsigned char *p, size_t len)
{
switch (type) {
case CM_F_PROM:
case CM_F_JSON: {
cm_buf *b = type == CM_F_PROM ? &ch->prom : &ch->json;
b->len = 0;
cb_put(b, (const char *) p, len);
break;
}
case CM_F_SAMPLE:
if (len >= CRYOM_SAMPLE_LEN) {
if (!ch->ring) {
ch->ring = malloc((size_t) CM_RING * CRYOM_SAMPLE_LEN);
if (!ch->ring) {
_exit(1);
}
}
memcpy(ch->ring + (size_t) ch->ring_head * CRYOM_SAMPLE_LEN, p, CRYOM_SAMPLE_LEN);
ch->ring_head = (ch->ring_head + 1) % CM_RING;
if (ch->ring_n < CM_RING) {
ch->ring_n++;
}
}
break;
case CM_F_MARK:
if (len >= 12) {
cm_child_mark *m = &ch->marks[ch->marks_head];
free(m->label);
m->time = get_f64(p);
m->seq = get_u32(p + 8);
m->label = strndup((const char *) p + 12, len - 12);
if (!m->label) {
_exit(1);
}
ch->marks_head = (ch->marks_head + 1) % CM_MARKS;
if (ch->marks_n < CM_MARKS) {
ch->marks_n++;
}
}
break;
case CM_F_SERIES:
if (len >= 5) {
ch->series_active = p[0] != 0;
ch->series_ms = get_u32(p + 1);
if (ch->series_active) {
ch->session++;
ch->ring_n = ch->ring_head = 0;
for (uint32_t i = 0; i < CM_MARKS; i++) {
free(ch->marks[i].label);
ch->marks[i].label = NULL;
}
ch->marks_n = ch->marks_head = 0;
free(ch->series_file);
ch->series_file = strndup((const char *) p + 5, len - 5);
}
}
break;
case CM_F_SNAPSHOT:
if (len >= 5) {
cm_child_snapshot_done(ch, get_u32(p), p[4] != 0, (const char *) p + 5, len - 5);
}
break;
}
}
static void cm_child_client(cm_child *ch, int c)
{
char req[CM_REQ_MAX];
size_t got = 0, head_end = 0;
struct timeval tv = { 2, 0 };
setsockopt(c, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
setsockopt(c, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
while (got < sizeof(req) - 1) {
ssize_t r = recv(c, req + got, sizeof(req) - 1 - got, 0);
if (r < 0 && errno == EINTR) {
continue;
}
if (r <= 0) {
break;
}
got += (size_t) r;
req[got] = '\0';
char *e = strstr(req, "\r\n\r\n");
if (e) {
head_end = (size_t) (e - req) + 4;
break;
}
if ((e = strstr(req, "\n\n"))) {
head_end = (size_t) (e - req) + 2;
break;
}
}
req[got] = '\0';
if (!head_end) {
if (got) {
cm_child_error(ch, c, got >= sizeof(req) - 1 ? "431 Request Header Fields Too Large" : "400 Bad Request", NULL, "bad request");
}
cm_child_close(c);
return;
}
/* request line */
char method[16];
size_t ml = strcspn(req, " ");
if (ml == 0 || ml >= sizeof(method) || req[ml] != ' ') {
cm_child_error(ch, c, "400 Bad Request", NULL, "bad request");
cm_child_close(c);
return;
}
memcpy(method, req, ml);
method[ml] = '\0';
const char *target = req + ml + 1;
size_t tl = strcspn(target, " \r\n");
const char *q = memchr(target, '?', tl);
size_t pl = q ? (size_t) (q - target) : tl;
const char *query = q ? q + 1 : NULL;
char path[256];
if (pl >= sizeof(path)) {
cm_child_error(ch, c, "414 URI Too Long", NULL, "URI too long");
cm_child_close(c);
return;
}
memcpy(path, target, pl);
path[pl] = '\0';
/* the query ends at the space before HTTP/1.x (cm_query_param stops there);
* cm_header() skips the request line */
const char *headers = req;
/* body: read and dropped (a POST /snapshot may carry one) */
char cl[32];
if (cm_header(headers, "Content-Length", cl, sizeof(cl))) {
unsigned long long want = strtoull(cl, NULL, 10);
if (want > CM_BODY_MAX) {
cm_child_error(ch, c, "413 Content Too Large", NULL, "body too large");
cm_child_close(c);
return;
}
size_t have = got - head_end;
char sink[4096];
while (have < want) {
size_t n = want - have < sizeof(sink) ? (size_t) (want - have) : sizeof(sink);
ssize_t r = recv(c, sink, n, 0);
if (r < 0 && errno == EINTR) {
continue;
}
if (r <= 0) {
break;
}
have += (size_t) r;
}
}
bool is_get = strcmp(method, "GET") == 0, is_head = strcmp(method, "HEAD") == 0;
bool is_post = strcmp(method, "POST") == 0;
if (strcmp(method, "OPTIONS") == 0) {
/* CORS preflight: no authentication (browsers never send credentials here) */
cm_buf x = {0};
cb_puts(&x, "Allow: GET, HEAD, POST, OPTIONS\r\n");
if (ch->cfg.cors) {
char v[16];
cb_puts(&x, "Access-Control-Allow-Methods: GET, HEAD, POST, OPTIONS\r\n"
"Access-Control-Allow-Headers: Authorization, Content-Type\r\n"
"Access-Control-Max-Age: 600\r\n");
if (cm_header(headers, "Access-Control-Request-Private-Network", v, sizeof(v)) && strcasecmp(v, "true") == 0) {
cb_puts(&x, "Access-Control-Allow-Private-Network: true\r\n");
}
}
cm_child_send(ch, c, "204 No Content", NULL, x.p, NULL, 0, true);
free(x.p);
cm_child_close(c);
return;
}
if (!cm_child_authorized(ch, headers, query)) {
cm_child_error(ch, c, "401 Unauthorized", "WWW-Authenticate: Bearer realm=\"cryo\"\r\n",
"a token is required: ?token= or Authorization: Bearer");
cm_child_close(c);
return;
}
bool known = strcmp(path, "/") == 0 || strcmp(path, "/metrics") == 0 || strcmp(path, "/metrics.json") == 0
|| strcmp(path, "/series.json") == 0;
if (strcmp(path, "/snapshot") == 0) {
if (!is_post && !(is_get && ch->cfg.token)) {
cm_child_error(ch, c, "405 Method Not Allowed", ch->cfg.token ? "Allow: GET, POST, OPTIONS\r\n" : "Allow: POST, OPTIONS\r\n",
"use POST (GET only when a token is set)");
} else if (!ch->cfg.snap_enabled) {
cm_child_error(ch, c, "403 Forbidden", NULL, "remote snapshots are disabled (option snapshot => false)");
} else if (ch->pend_fd >= 0) {
cm_child_error(ch, c, "409 Conflict", NULL, "a snapshot is already in progress");
} else {
ch->pend_id = ++ch->next_id;
ch->pend_fd = c;
ch->pend_deadline = cm_mono_ms() + ch->cfg.snap_timeout_ms;
cm_child_to_parent(ch, CM_F_REQUEST, ch->pend_id);
return; /* answered by cm_child_snapshot_done() or the timeout */
}
} else if (!known) {
cm_child_error(ch, c, "404 Not Found", NULL, "not found");
} else if (!is_get && !is_head) {
cm_child_error(ch, c, "405 Method Not Allowed", "Allow: GET, HEAD, OPTIONS\r\n", "method not allowed");
} else if (strcmp(path, "/metrics") == 0) {
cm_child_send(ch, c, "200 OK", "text/plain; version=0.0.4; charset=utf-8", NULL,
ch->prom.p ? ch->prom.p : "", ch->prom.len, is_head);
} else if (strcmp(path, "/metrics.json") == 0) {
if (ch->json.len) {
cm_child_send(ch, c, "200 OK", "application/json", NULL, ch->json.p, ch->json.len, is_head);
} else {
cm_child_error(ch, c, "503 Service Unavailable", NULL, "no data yet");
}
} else if (strcmp(path, "/series.json") == 0) {
char v[32];
uint32_t since = query && cm_query_param(query, "since", v, sizeof(v)) ? (uint32_t) strtoul(v, NULL, 10) : 0;
cm_buf b = {0};
cm_child_series_json(ch, &b, since);
cm_child_send(ch, c, "200 OK", "application/json", NULL, b.p, b.len, is_head);
free(b.p);
} else {
static const char idx[] = "ext-cryo metrics exporter\n"
"GET /metrics Prometheus text\n"
"GET /metrics.json cryo_metrics() + time + class_counts\n"
"GET /series.json?since=SEQ samples of the active cryo_metrics_sample() series\n"
"POST /snapshot cryo_snapshot() of the running script (.cryo)\n";
cm_child_send(ch, c, "200 OK", "text/plain; charset=utf-8", NULL, idx, sizeof(idx) - 1, is_head);
}
cm_child_close(c);
}
static void cm_child_check_timeout(cm_child *ch)
{
if (ch->pend_fd >= 0 && cm_mono_ms() >= ch->pend_deadline) {
int c = ch->pend_fd;
ch->pend_fd = -1;
cm_child_to_parent(ch, CM_F_CANCEL, ch->pend_id);
char msg[160];
snprintf(msg, sizeof(msg), "the PHP process did not reach a safe point within %.1f s", ch->cfg.snap_timeout_ms / 1000.0);
cm_child_error(ch, c, "504 Gateway Timeout", NULL, msg);
cm_child_close(c);
}
}
static ZEND_NORETURN void cm_child_main(int lfd, int pfd, const cm_child_cfg *cfg)
{
/* plain process: default signals, nothing inherited from PHP */
for (int sig = 1; sig < NSIG; sig++) {
if (sig != SIGKILL && sig != SIGSTOP) {
signal(sig, SIG_DFL);
}
}
signal(SIGPIPE, SIG_IGN);
sigset_t none;
sigemptyset(&none);
sigprocmask(SIG_SETMASK, &none, NULL);
#ifdef __linux__
prctl(PR_SET_PDEATHSIG, SIGTERM);
#endif
if (getppid() != cfg->parent) {
_exit(0);
}
long maxfd = sysconf(_SC_OPEN_MAX);
if (maxfd < 0 || maxfd > 65536) {
maxfd = 65536;
}
for (int fd = 3; fd < maxfd; fd++) {
if (fd != lfd && fd != pfd) {
close(fd);
}
}
static cm_child ch;
memset(&ch, 0, sizeof(ch));
ch.cfg = *cfg;
ch.pfd = pfd;
ch.pend_fd = -1;
for (;;) {
int timeout = 5000;
if (ch.pend_fd >= 0) {
int64_t left = ch.pend_deadline - cm_mono_ms();
timeout = left < 0 ? 0 : (left < timeout ? (int) left : timeout);
}
struct pollfd pf[2] = { { pfd, POLLIN, 0 }, { lfd, POLLIN, 0 } };
int n = poll(pf, 2, timeout);
if (n < 0) {
if (errno == EINTR) {
continue;
}
_exit(1);
}
if (n == 0 && getppid() != cfg->parent) {
_exit(0);
}
if (n > 0 && pf[0].revents && !cm_child_pump(&ch)) {
_exit(0); /* parent closed the channel or died */
}
cm_child_check_timeout(&ch);
if (n > 0 && (pf[1].revents & POLLIN)) {
int c = accept(lfd, NULL, NULL);
if (c >= 0) {
cm_child_client(&ch, c);
}
}
}
}
static zend_long cm_serve_start(zend_long port, const char *host, const cm_child_cfg *cfg)
{
char pbuf[16];
snprintf(pbuf, sizeof(pbuf), ZEND_LONG_FMT, port);
struct addrinfo hints, *res = NULL, *ai;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
int rc = getaddrinfo(host, pbuf, &hints, &res);
if (rc != 0) {
zend_throw_exception_ex(cryo_ce_exception, 0, "cryo_metrics_serve(): cannot resolve %s: %s", host, gai_strerror(rc));
return -1;
}
int lfd = -1, err = 0;
for (ai = res; ai; ai = ai->ai_next) {
lfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (lfd < 0) {
err = errno;
continue;
}
int one = 1;
setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
if (bind(lfd, ai->ai_addr, ai->ai_addrlen) == 0 && listen(lfd, 16) == 0) {
break;
}
err = errno;
close(lfd);
lfd = -1;
}
freeaddrinfo(res);
if (lfd < 0) {
zend_throw_exception_ex(cryo_ce_exception, 0, "cryo_metrics_serve(): cannot listen on %s:" ZEND_LONG_FMT ": %s",
host, port, strerror(err));
return -1;
}
struct sockaddr_storage ss;
socklen_t sl = sizeof(ss);
int real_port = (int) port;
if (getsockname(lfd, (struct sockaddr *) &ss, &sl) == 0) {
if (ss.ss_family == AF_INET) {
real_port = ntohs(((struct sockaddr_in *) &ss)->sin_port);
} else if (ss.ss_family == AF_INET6) {
real_port = ntohs(((struct sockaddr_in6 *) &ss)->sin6_port);
}
}
int sv[2];
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) != 0) {
close(lfd);
zend_throw_exception_ex(cryo_ce_exception, 0, "cryo_metrics_serve(): socketpair: %s", strerror(errno));
return -1;
}
cm_child_cfg ccfg = *cfg;
ccfg.parent = getpid();
pid_t pid = fork();
if (pid < 0) {
close(lfd); close(sv[0]); close(sv[1]);
zend_throw_exception_ex(cryo_ce_exception, 0, "cryo_metrics_serve(): fork: %s", strerror(errno));
return -1;
}
if (pid == 0) {
close(sv[0]);
cm_child_main(lfd, sv[1], &ccfg);
}
close(lfd);
close(sv[1]);
fcntl(sv[0], F_SETFD, FD_CLOEXEC);
cm_serve.pid = pid;
cm_serve.owner = ccfg.parent;
cm_serve.fd = sv[0];
cm_serve.port = real_port;
cm_serve.dead = false;
return pid;
}
/* /metrics.json: cryo_metrics() + time + pid (+ class_counts with cryo.metrics=1). */
static void cm_metrics_json(smart_str *out, const cm_snap *s, zval *classes)
{
zval arr;
array_init(&arr);
add_assoc_double(&arr, "time", s->time);
add_assoc_long(&arr, "pid", (zend_long) getpid());
cm_metrics_array(&arr, s);
php_json_encode_ex(out, &arr, PHP_JSON_UNESCAPED_SLASHES | PHP_JSON_PARTIAL_OUTPUT_ON_ERROR, 8);
zval_ptr_dtor(&arr);
if (cm_live_installed && out->s && ZSTR_LEN(out->s) > 2) {
ZSTR_LEN(out->s)--; /* the closing brace */
smart_str_appends(out, ",\"class_counts\":{");
if (classes) {
zend_string *k; zval *v; bool first = true;
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(classes), k, v) {
if (!first) {
smart_str_appendc(out, ',');
}
first = false;
cm_json_str(out, ZSTR_VAL(k), ZSTR_LEN(k));
smart_str_append_printf(out, ":" ZEND_LONG_FMT, Z_LVAL_P(v));
} ZEND_HASH_FOREACH_END();
}
smart_str_appends(out, "}}");
}
smart_str_appendc(out, '\n');
smart_str_0(out);
}
#endif /* CRYO_HAVE_SOCKETS */
static bool cm_serving(void)
{
#ifdef CRYO_HAVE_SOCKETS
return cm_serve.fd >= 0 && !cm_serve.dead && cm_serve.owner == getpid();
#else
return cm_ticker.serving;
#endif
}
static void cm_publish(const cm_snap *s, zval *classes)
{
if (!cm_serving()) {
return;
}
smart_str t = {0};
cm_prom_text(&t, s, classes);
#ifdef CRYO_HAVE_SOCKETS
cm_chan_send(CM_F_PROM, ZSTR_VAL(t.s), ZSTR_LEN(t.s), NULL, 0);
smart_str j = {0};
cm_metrics_json(&j, s, classes);
cm_chan_send(CM_F_JSON, ZSTR_VAL(j.s), ZSTR_LEN(j.s), NULL, 0);
smart_str_free(&j);
#else
cryo_host_emit(CRYO_EMIT_PROMETHEUS, (const uint8_t *) ZSTR_VAL(t.s), (uint32_t) ZSTR_LEN(t.s));
#endif
smart_str_free(&t);
}
/* ======================================================================== */
/* Series */
/* ======================================================================== */
static bool cm_write_rec(uint8_t kind, const unsigned char *payload, size_t len)
{
if (cm_series.fd < 0 || len > 0xFFFF) {
return false;
}
unsigned char stackbuf[4 + CRYOM_SAMPLE_LEN];
unsigned char *buf = (4 + len <= sizeof(stackbuf)) ? stackbuf : emalloc(4 + len);
buf[0] = kind;
buf[1] = 0;
put_u16(buf + 2, (uint16_t) len);
memcpy(buf + 4, payload, len);
size_t n = 4 + len, off = 0;
bool ok = true;
while (off < n) {
ssize_t w = write(cm_series.fd, buf + off, n - off);
if (w < 0) {
if (errno == EINTR) {
continue;
}
ok = false;
break;
}
off += (size_t) w;
}
if (buf != stackbuf) {
efree(buf);
}
return ok;
}
static void cm_write_text_rec(uint8_t kind, double t, uint32_t seq, const char *txt, size_t len)
{
if (len > 0xFFFF - 12) {
len = 0xFFFF - 12;
}
unsigned char *p = emalloc(12 + len);
put_f64(p, t);
put_u32(p + 8, seq);
memcpy(p + 12, txt, len);
cm_write_rec(kind, p, 12 + len);
efree(p);
}
static void cm_auto_snapshot(const cm_snap *s)
{
zend_long every = INI_INT("cryo.auto_snapshot_every");
if (every <= 0 || ((zend_long) s->seq + 1) % every != 0 || !cm_series.path) {
return;
}
size_t bl = ZSTR_LEN(cm_series.path);
if (bl > 6 && memcmp(ZSTR_VAL(cm_series.path) + bl - 6, ".cryom", 6) == 0) {
bl -= 6;
}
zend_string *snap = zend_strpprintf(0, "%.*s-%u.cryo", (int) bl, ZSTR_VAL(cm_series.path), s->seq);
if (cryo_write_snapshot(snap, NULL) == SUCCESS) {
cm_write_text_rec(CRYOM_REC_SNAPSHOT, s->time, s->seq, ZSTR_VAL(snap), ZSTR_LEN(snap));
} else if (!EG(exception)) {
php_error_docref(NULL, E_WARNING, "cryo.auto_snapshot_every: cannot write %s", ZSTR_VAL(snap));
}
zend_string_release(snap);
}
/* One tick: a sample in the series (if any) and the exporters. */
static void cm_tick(bool allow_snapshot)
{
if (cm_series.in_tick) {
return;
}
cm_series.in_tick = true;
cm_snap s;
cm_collect(&s);
s.seq = cm_series.active ? cm_series.seq : 0;
zval classes;
bool have_classes = false;
bool otlp = cm_series.active && INI_STR("cryo.otlp_endpoint") && INI_STR("cryo.otlp_endpoint")[0];
if (cm_exporters_want_classes() && (otlp || cm_serving())) {
array_init(&classes);
cm_class_counts(&classes, CM_CLASS_TOP);
have_classes = true;
}
if (cm_series.active) {
unsigned char rec[CRYOM_SAMPLE_LEN];
cm_encode_sample(rec, &s);
cm_write_rec(CRYOM_REC_SAMPLE, rec, sizeof(rec));
cm_series.seq++;
#ifdef CRYO_HAVE_SOCKETS
cm_chan_send(CM_F_SAMPLE, rec, sizeof(rec), NULL, 0);
#endif
#ifdef CRYO_BACKEND_WASM
cryo_host_emit(CRYO_EMIT_SAMPLE, rec, sizeof(rec));
#endif
if (otlp) {
cm_otlp_send(&s, have_classes ? &classes : NULL);
}
if (allow_snapshot) {
cm_auto_snapshot(&s);
}
}
s.seq = cm_series.seq;
cm_publish(&s, have_classes ? &classes : NULL);
if (have_classes) {
zval_ptr_dtor(&classes);
}
#ifdef CRYO_BACKEND_WASM
if (cm_ticker.ms) {
cm_ticker.next_due = cryo_host_now() + cm_ticker.ms;
cryo_host_timer(cm_ticker.ms);
}
#endif
cm_series.in_tick = false;
}
static void cm_interrupt(zend_execute_data *execute_data)
{
bool due = zend_atomic_bool_exchange_ex(&cm_tick_due, false);
#ifdef CRYO_BACKEND_WASM
if (!due && cm_ticker.ms && cryo_host_now() >= cm_ticker.next_due) {
due = true;
}
#endif
if (due && (cm_series.active || cm_serving())) {
cm_tick(true);
}
#ifdef CRYO_HAVE_SOCKETS
if (zend_atomic_bool_load_ex(&cm_snap_due)) {
cm_remote_snapshot();
}
#endif
if (cm_prev_interrupt) {
cm_prev_interrupt(execute_data);
}
}
void cryo_metrics_sample_now(void)
{
if (cm_series.active || cm_serving()) {
cm_tick(false);
}
}
static void cm_series_stop(bool final_sample)
{
if (!cm_series.active) {
return;
}
if (final_sample) {
cm_tick(false);
}
cm_series.active = false;
if (cm_series.fd >= 0) {
close(cm_series.fd);
cm_series.fd = -1;
}
if (cm_series.path) {
zend_string_release(cm_series.path);
cm_series.path = NULL;
}
cm_series.ms = 0;
cm_series_start = 0;
#ifdef CRYO_HAVE_SOCKETS
cm_chan_series_state();
#endif
}
static bool cm_series_start_file(zend_string *file, uint32_t ms)
{
char resolved[MAXPATHLEN];
if (!expand_filepath(ZSTR_VAL(file), resolved)) {
zend_throw_exception_ex(cryo_ce_exception, 0, "Invalid path %s", ZSTR_VAL(file));
return false;
}
if (php_check_open_basedir(resolved)) {
return false;
}
int fd = open(resolved, O_RDWR | O_CREAT | O_APPEND | O_CLOEXEC, 0644);
if (fd < 0) {
zend_throw_exception_ex(cryo_ce_exception, 0, "Cannot open %s: %s", resolved, strerror(errno));
return false;
}
unsigned char hdr[CRYOM_HEADER_SIZE];
ssize_t n = pread(fd, hdr, 4, 0);
double now = cm_now();
if (n == 0) {
memset(hdr, 0, sizeof(hdr));
memcpy(hdr, CRYOM_MAGIC, 4);
put_u16(hdr + 4, CRYOM_VERSION);
put_u16(hdr + 6, 0);
put_u32(hdr + 8, PHP_VERSION_ID);
put_u32(hdr + 12, (uint32_t) getpid());
put_f64(hdr + 16, now);
put_u32(hdr + 24, ms);
if (write(fd, hdr, sizeof(hdr)) != (ssize_t) sizeof(hdr)) {
close(fd);
zend_throw_exception_ex(cryo_ce_exception, 0, "Cannot write %s", resolved);
return false;
}
} else if (n != 4 || memcmp(hdr, CRYOM_MAGIC, 4) != 0) {
close(fd);
zend_throw_exception_ex(cryo_ce_exception, 0, "%s exists and is not a .cryom file", resolved);
return false;
}
cm_series.fd = fd;
cm_series.path = zend_string_init(resolved, strlen(resolved), 0);
cm_series.ms = ms;
cm_series.seq = 0;
cm_series.active = true;
cm_series_start = now;
unsigned char st[16];
put_f64(st, now);
put_u32(st + 8, (uint32_t) getpid());
put_u32(st + 12, ms);
cm_write_rec(CRYOM_REC_START, st, sizeof(st));
#ifdef CRYO_HAVE_SOCKETS
cm_chan_series_state();
#endif
return true;
}
/* ======================================================================== */
/* PHP functions */
/* ======================================================================== */
PHP_FUNCTION(cryo_metrics)
{
ZEND_PARSE_PARAMETERS_NONE();
cm_snap s;
cm_collect(&s);
array_init(return_value);
cm_metrics_array(return_value, &s);
}
PHP_FUNCTION(cryo_metrics_class_counts)
{
zend_long top = 20;
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(top)
ZEND_PARSE_PARAMETERS_END();
array_init(return_value);
cm_class_counts(return_value, top);
}
PHP_FUNCTION(cryo_metrics_sample)
{
zend_long ms;
zend_string *file;
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_LONG(ms)
Z_PARAM_PATH_STR(file)
ZEND_PARSE_PARAMETERS_END();
cm_series_stop(true);
if (ms > 0) {
if (ms > 86400000) {
ms = 86400000;
}
if (cm_series_start_file(file, (uint32_t) ms)) {
cm_tick(false); /* first sample right away */
}
}
cm_ticker_update();
}
PHP_FUNCTION(cryo_metrics_mark)
{
zend_string *label;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STR(label)
ZEND_PARSE_PARAMETERS_END();
#ifdef CRYO_BACKEND_WASM
cryo_host_emit(CRYO_EMIT_MARK, (const uint8_t *) ZSTR_VAL(label), (uint32_t) ZSTR_LEN(label));
#endif
if (!cm_series.active) {
php_error_docref(NULL, E_WARNING, "No active series, call cryo_metrics_sample() first");
return;
}
double now = cm_now();
uint32_t seq = cm_series.seq ? cm_series.seq - 1 : 0;
cm_write_text_rec(CRYOM_REC_MARK, now, seq, ZSTR_VAL(label), ZSTR_LEN(label));
#ifdef CRYO_HAVE_SOCKETS
unsigned char mh[12];
put_f64(mh, now);
put_u32(mh + 8, seq);
cm_chan_send(CM_F_MARK, mh, sizeof(mh), ZSTR_VAL(label), ZSTR_LEN(label) > 4096 ? 4096 : ZSTR_LEN(label));
#endif
}
PHP_FUNCTION(cryo_metrics_read)
{
zend_string *file;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_PATH_STR(file)
ZEND_PARSE_PARAMETERS_END();
php_stream *st = php_stream_open_wrapper(ZSTR_VAL(file), "rb", REPORT_ERRORS, NULL);
if (!st) {
zend_throw_exception_ex(cryo_ce_exception, 0, "Cannot open %s", ZSTR_VAL(file));
RETURN_THROWS();
}
zend_string *data = php_stream_copy_to_mem(st, PHP_STREAM_COPY_ALL, 0);
php_stream_close(st);
if (!data || ZSTR_LEN(data) < CRYOM_HEADER_SIZE || memcmp(ZSTR_VAL(data), CRYOM_MAGIC, 4) != 0) {
if (data) {
zend_string_release(data);
}
zend_throw_exception_ex(cryo_ce_exception, 0, "%s is not a .cryom file", ZSTR_VAL(file));
RETURN_THROWS();
}
const unsigned char *p = (const unsigned char *) ZSTR_VAL(data);
const unsigned char *end = p + ZSTR_LEN(data);
uint16_t version = get_u16(p + 4);
if (version != CRYOM_VERSION) {
zend_string_release(data);
zend_throw_exception_ex(cryo_ce_exception, 0, "Unsupported .cryom version %u", version);
RETURN_THROWS();
}
zval samples, marks, snaps, sessions;
array_init(return_value);
array_init(&samples);
array_init(&marks);
array_init(&snaps);
array_init(&sessions);
add_assoc_long(return_value, "version", version);
add_assoc_long(return_value, "php_version", (zend_long) get_u32(p + 8));
cm_add_u64(return_value, "pid", get_u32(p + 12));
add_assoc_double(return_value, "start", get_f64(p + 16));
cm_add_u64(return_value, "interval_ms", get_u32(p + 24));
bool truncated = false;
p += CRYOM_HEADER_SIZE;
while (p < end) {
if (end - p < 4) {
truncated = true;
break;
}
uint8_t kind = p[0];
uint16_t len = get_u16(p + 2);
const unsigned char *pl = p + 4;
if ((size_t) (end - pl) < len) {
truncated = true;
break;
}
zval row;
if (kind == CRYOM_REC_SAMPLE && len >= CRYOM_SAMPLE_LEN) {
array_init(&row);
uint32_t flags = get_u32(pl + 12);
add_assoc_double(&row, "time", get_f64(pl));
cm_add_u64(&row, "seq", get_u32(pl + 8));
for (int i = 0; i < M_COUNT; i++) {
uint64_t v = get_u64(pl + 16 + 8 * i);
if (i == M_RSS && !(flags & CRYOM_FLAG_RSS)) {
add_assoc_null(&row, cm_sample_names[i]);
} else if (i == M_GC_NS) {
add_assoc_double(&row, cm_sample_names[i], (double) v / 1e9);
} else if (i == M_LAST_US) {
add_assoc_double(&row, cm_sample_names[i], (double) v / 1000.0);
} else {
cm_add_u64(&row, cm_sample_names[i], v);
}
}
add_next_index_zval(&samples, &row);
} else if ((kind == CRYOM_REC_MARK || kind == CRYOM_REC_SNAPSHOT) && len >= 12) {
array_init(&row);
add_assoc_double(&row, "time", get_f64(pl));
cm_add_u64(&row, "seq", get_u32(pl + 8));
add_assoc_stringl(&row, kind == CRYOM_REC_MARK ? "label" : "file", (const char *) pl + 12, len - 12);
add_next_index_zval(kind == CRYOM_REC_MARK ? &marks : &snaps, &row);
} else if (kind == CRYOM_REC_START && len >= 16) {
array_init(&row);
add_assoc_double(&row, "time", get_f64(pl));
cm_add_u64(&row, "pid", get_u32(pl + 8));
cm_add_u64(&row, "interval_ms", get_u32(pl + 12));
add_next_index_zval(&sessions, &row);
}
p = pl + len;
}
zend_string_release(data);
add_assoc_zval(return_value, "samples", &samples);
add_assoc_zval(return_value, "marks", &marks);
add_assoc_zval(return_value, "snapshots", &snaps);
add_assoc_zval(return_value, "sessions", &sessions);
add_assoc_bool(return_value, "truncated", truncated);
}
#ifdef CRYO_HAVE_SOCKETS
/* A header value given by the user: printable, no CR/LF. */
static bool cm_header_safe(zend_string *s, size_t max)
{
if (ZSTR_LEN(s) == 0 || ZSTR_LEN(s) > max) {
return false;
}
for (size_t i = 0; i < ZSTR_LEN(s); i++) {
unsigned char c = (unsigned char) ZSTR_VAL(s)[i];
if (c < 0x20 || c == 0x7f) {
return false;
}
}
return true;
}
#endif
PHP_FUNCTION(cryo_metrics_serve)
{
zend_long port;
zend_string *host = NULL;
HashTable *opts = NULL;
ZEND_PARSE_PARAMETERS_START(1, 3)
Z_PARAM_LONG(port)
Z_PARAM_OPTIONAL
Z_PARAM_STR(host)
Z_PARAM_ARRAY_HT(opts)
ZEND_PARSE_PARAMETERS_END();
if (port > 65535) {
zend_argument_value_error(1, "must be between -1 and 65535");
RETURN_THROWS();
}
#ifdef CRYO_HAVE_SOCKETS
cm_child_cfg cfg = { 0, NULL, NULL, true, 10000 };
zval *snap_opts = NULL;
if (opts) {
zend_string *key;
zval *v;
ZEND_HASH_FOREACH_STR_KEY_VAL(opts, key, v) {
ZVAL_DEREF(v);
if (!key) {
zend_argument_value_error(3, "must be an array with string keys");
RETURN_THROWS();
}
if (zend_string_equals_literal(key, "cors")) {
if (Z_TYPE_P(v) == IS_NULL || Z_TYPE_P(v) == IS_FALSE) {
cfg.cors = NULL;
} else if (Z_TYPE_P(v) != IS_STRING || !cm_header_safe(Z_STR_P(v), 512)) {
zend_argument_value_error(3, "option \"cors\" must be an origin such as \"*\" or \"https://example.org\"");
RETURN_THROWS();
} else {
cfg.cors = Z_STRVAL_P(v);
}
} else if (zend_string_equals_literal(key, "token")) {
if (Z_TYPE_P(v) == IS_NULL) {
cfg.token = NULL;
} else if (Z_TYPE_P(v) != IS_STRING || !cm_header_safe(Z_STR_P(v), 512) || strchr(Z_STRVAL_P(v), ' ')) {
zend_argument_value_error(3, "option \"token\" must be a non-empty string without spaces (at most 512 bytes)");
RETURN_THROWS();
} else {
cfg.token = Z_STRVAL_P(v);
}
} else if (zend_string_equals_literal(key, "snapshot")) {
if (Z_TYPE_P(v) == IS_TRUE || Z_TYPE_P(v) == IS_FALSE) {
cfg.snap_enabled = Z_TYPE_P(v) == IS_TRUE;
snap_opts = NULL;
} else if (Z_TYPE_P(v) == IS_ARRAY) {
cfg.snap_enabled = true;
snap_opts = v;
} else {
zend_argument_value_error(3, "option \"snapshot\" must be a bool or an array of cryo_snapshot() options");
RETURN_THROWS();
}
} else if (zend_string_equals_literal(key, "snapshot_timeout")) {
double t = Z_TYPE_P(v) == IS_LONG ? (double) Z_LVAL_P(v) : (Z_TYPE_P(v) == IS_DOUBLE ? Z_DVAL_P(v) : -1);
if (!(t >= 0.05 && t <= 3600)) {
zend_argument_value_error(3, "option \"snapshot_timeout\" must be a number of seconds between 0.05 and 3600");
RETURN_THROWS();
}
cfg.snap_timeout_ms = (int) (t * 1000 + 0.5);
} else {
zend_argument_value_error(3, "unknown option \"%s\" (cors, token, snapshot, snapshot_timeout)", ZSTR_VAL(key));
RETURN_THROWS();
}
} ZEND_HASH_FOREACH_END();
}
cm_serve_stop();
if (port < 0) {
cm_ticker_update();
RETURN_LONG(0);
}
zend_long pid = cm_serve_start(port, host ? ZSTR_VAL(host) : "127.0.0.1", &cfg);
if (pid < 0) {
cm_ticker_update();
RETURN_THROWS();
}
if (snap_opts) {
ZVAL_COPY(&cm_serve.snap_opts, snap_opts);
}
if (cm_series.active) {
cm_chan_series_state();
}
cryo_metrics_sample_now(); /* first content right away */
cm_ticker_update();
RETURN_LONG(pid);
#else
/* WASM: no process and no socket. The host receives the Prometheus text
* through cryo_host_emit(CRYO_EMIT_PROMETHEUS, ...) and serves it if it
* wants; the HTTP options of the POSIX exporter have no meaning here. */
(void) host;
if (opts && zend_hash_num_elements(opts) > 0 && port >= 0) {
zend_throw_exception_ex(cryo_ce_exception, 0,
"cryo_metrics_serve(): the options (cors, token, snapshot) need the HTTP exporter process, which "
"does not exist in WebAssembly: the host receives the metrics through cryo_host_emit() instead");
RETURN_THROWS();
}
cm_ticker.serving = port >= 0;
if (cm_ticker.serving) {
cryo_metrics_sample_now();
}
cm_ticker_update();
RETURN_LONG(0);
#endif
}
PHP_FUNCTION(cryo_metrics_push)
{
zend_string *url, *prefix = NULL;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_STR(url)
Z_PARAM_OPTIONAL
Z_PARAM_STR(prefix)
ZEND_PARSE_PARAMETERS_END();
cm_snap s;
cm_collect(&s);
zval classes;
bool have_classes = cm_exporters_want_classes();
if (have_classes) {
array_init(&classes);
cm_class_counts(&classes, CM_CLASS_TOP);
}
const char *pf = prefix ? ZSTR_VAL(prefix) : "php";
size_t pl = prefix ? ZSTR_LEN(prefix) : 3;
while (pl && pf[pl - 1] == '.') {
pl--;
}
smart_str lines = {0};
cm_statsd_lines(&lines, &s, have_classes ? &classes : NULL, pf, pl);
if (have_classes) {
zval_ptr_dtor(&classes);
}
#ifdef CRYO_HAVE_SOCKETS
const char *u = ZSTR_VAL(url);
size_t ul = ZSTR_LEN(url);
if (ul > 6 && strncasecmp(u, "udp://", 6) == 0) {
u += 6;
ul -= 6;
} else if (strstr(u, "://")) {
smart_str_free(&lines);
zend_argument_value_error(1, "must be udp://host:port");
RETURN_THROWS();
}
while (ul && u[ul - 1] == '/') {
ul--;
}
char host[256], port[16];
if (!cm_split_hostport(u, ul, host, sizeof(host), port, sizeof(port), "8125")) {
smart_str_free(&lines);
zend_argument_value_error(1, "must be udp://host:port");
RETURN_THROWS();
}
struct addrinfo hints, *res = NULL;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
int rc = getaddrinfo(host, port, &hints, &res);
if (rc != 0) {
smart_str_free(&lines);
php_error_docref(NULL, E_WARNING, "Cannot resolve %s: %s", host, gai_strerror(rc));
RETURN_FALSE;
}
int fd = socket(res->ai_family, res->ai_socktype | SOCK_CLOEXEC, res->ai_protocol);
bool ok = fd >= 0;
/* packets of at most ~1400 bytes, split on line boundaries */
const char *b = ZSTR_VAL(lines.s), *e = b + ZSTR_LEN(lines.s);
while (ok && b < e) {
const char *cut = b;
for (;;) {
const char *nl = memchr(cut, '\n', (size_t) (e - cut));
const char *next = nl ? nl + 1 : e;
if (cut > b && (size_t) (next - b) > 1400) {
break;
}
cut = next;
if (cut >= e) {
break;
}
}
size_t plen = (size_t) (cut - b);
if (plen && b[plen - 1] == '\n') {
plen--; /* no trailing newline */
}
if (sendto(fd, b, plen, 0, res->ai_addr, res->ai_addrlen) < 0) {
ok = false;
}
b = cut;
}
if (fd >= 0) {
close(fd);
}
freeaddrinfo(res);
smart_str_free(&lines);
RETURN_BOOL(ok);
#else
smart_str msg = {0};
smart_str_append(&msg, url);
smart_str_appendc(&msg, '\n');
smart_str_append(&msg, lines.s);
cryo_host_emit(CRYO_EMIT_STATSD, (const uint8_t *) ZSTR_VAL(msg.s), (uint32_t) ZSTR_LEN(msg.s));
smart_str_free(&msg);
smart_str_free(&lines);
RETURN_TRUE;
#endif
}
/* ======================================================================== */
/* Module hooks */
/* ======================================================================== */
void cryo_metrics_minit(void)
{
memset(&cryo_stats, 0, sizeof(cryo_stats));
zend_atomic_bool_store_ex(&cm_tick_due, false);
#ifdef CRYO_HAVE_SOCKETS
zend_atomic_bool_store_ex(&cm_snap_due, false);
zend_atomic_int_store_ex(&cm_snap_id, 0);
ZVAL_UNDEF(&cm_serve.snap_opts);
#endif
cm_live_installed = INI_BOOL("cryo.metrics");
if (cm_live_installed) {
memcpy(&cm_tag_handlers, &std_object_handlers, sizeof(cm_tag_handlers));
cm_tag_handlers.free_obj = cm_tag_free;
cm_tag_handlers.clone_obj = cm_tag_clone;
cm_prev_new_handler = zend_get_user_opcode_handler(ZEND_NEW);
zend_set_user_opcode_handler(ZEND_NEW, cm_new_handler);
}
cm_prev_interrupt = zend_interrupt_function;
zend_interrupt_function = cm_interrupt;
}
void cryo_metrics_rshutdown(void)
{
#ifdef CRYO_HAVE_SOCKETS
cm_ticker_stop();
#else
cm_ticker.ms = 0;
cm_ticker.serving = false;
#endif
cm_series_stop(true);
#ifdef CRYO_HAVE_SOCKETS
cm_serve_stop();
#endif
cm_series.otlp_tls_warned = false;
cm_live_shutdown();
}
void cryo_metrics_mshutdown(void)
{
if (cm_live_installed) {
zend_set_user_opcode_handler(ZEND_NEW, cm_prev_new_handler);
}
if (zend_interrupt_function == cm_interrupt) {
zend_interrupt_function = cm_prev_interrupt;
}
}