winzik/docker/ext/cryo/cryo_process.c

274 lines
8.1 KiB
C

/*
* cryo_process.c — milestone 4: cryo_restart(), cryo_memory_watch().
* (The fork mode, cryo_fork_pool() included, lives in cryo_fork.c.)
*
* POSIX: restart re-executes the same PHP command line with CRYO_THAW=<file>
* (cryo_freeze.c thaws it before {main} runs), memory_watch wakes the VM every
* 10 ms from a small thread (EG(vm_interrupt)) and calls the callback at a
* safe point. WebAssembly: restart goes through the JS host; memory_watch is
* checked at the VM interrupts the engine raises by itself.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "php.h"
#include "php_cryo.h"
#include "cryo_host.h"
#include "zend_exceptions.h"
#include "main/php_output.h"
#ifndef CRYO_BACKEND_WASM
# include <unistd.h>
# include <errno.h>
# include <pthread.h>
# include <time.h>
#endif
/* ------------------------------------------------------------------------ */
/* cryo_restart() */
/* ------------------------------------------------------------------------ */
/* Replaces this process by the same PHP command line, which thaws `thaw_file`
* before {main} runs (cryo_freeze.c). Returns only on failure, with an exception. */
zend_result cryo_exec_self(const char *full)
{
php_output_end_all();
fflush(stdout);
fflush(stderr);
#ifdef CRYO_BACKEND_WASM
cryo_host_restart(full, (uint32_t) strlen(full));
zend_throw_unwind_exit();
return FAILURE;
#else
/* the same command line: /proc/self/cmdline (Linux), else options['argv'] */
char **argv = NULL;
int argc = 0;
zval *zargv = NULL; /* reserved: an explicit argv */
zend_string *cmd = NULL;
if (zargv && Z_TYPE_P(zargv) == IS_ARRAY) {
argv = ecalloc(zend_hash_num_elements(Z_ARRVAL_P(zargv)) + 1, sizeof(char *));
zval *a;
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(zargv), a) {
if (Z_TYPE_P(a) == IS_STRING) argv[argc++] = Z_STRVAL_P(a);
} ZEND_HASH_FOREACH_END();
} else {
php_stream *s = php_stream_open_wrapper("/proc/self/cmdline", "rb", 0, NULL);
if (s) {
cmd = php_stream_copy_to_mem(s, PHP_STREAM_COPY_ALL, 0);
php_stream_close(s);
}
if (!cmd || ZSTR_LEN(cmd) == 0) {
if (cmd) zend_string_release(cmd);
zend_throw_exception(cryo_ce_exception, "Cannot read the command line: pass options['argv']", 0);
return FAILURE;
}
size_t n = 0;
for (size_t i = 0; i < ZSTR_LEN(cmd); i++) if (ZSTR_VAL(cmd)[i] == '\0') n++;
argv = ecalloc(n + 2, sizeof(char *));
char *p = ZSTR_VAL(cmd), *end = p + ZSTR_LEN(cmd);
while (p < end) {
argv[argc++] = p;
p += strlen(p) + 1;
}
}
argv[argc] = NULL;
if (argc == 0) {
efree(argv);
if (cmd) zend_string_release(cmd);
zend_throw_exception(cryo_ce_exception, "Empty command line", 0);
return FAILURE;
}
setenv("CRYO_THAW", full, 1);
const char *exe = PG(php_binary) ? PG(php_binary) : "/proc/self/exe";
execv(exe, argv);
execv("/proc/self/exe", argv);
int err = errno;
unsetenv("CRYO_THAW");
efree(argv);
if (cmd) zend_string_release(cmd);
zend_throw_exception_ex(cryo_ce_exception, 0, "Cannot restart: %s", strerror(err));
return FAILURE;
#endif
}
/* ------------------------------------------------------------------------ */
/* cryo_memory_watch() */
/* ------------------------------------------------------------------------ */
static struct {
bool active;
bool fired;
bool in_callback;
zend_long bytes;
zval callback;
#ifndef CRYO_BACKEND_WASM
pthread_t thread;
bool thread_on;
volatile bool stop;
#endif
} cryo_watch;
static void (*cryo_prev_interrupt)(zend_execute_data *) = NULL;
bool cryo_watch_in_callback = false;
static void cryo_interrupt(zend_execute_data *execute_data)
{
if (cryo_watch.active && !cryo_watch.in_callback) {
size_t used = zend_memory_usage(0);
if (!cryo_watch.fired && (zend_long) used >= cryo_watch.bytes) {
cryo_watch.fired = true;
cryo_watch.in_callback = true;
cryo_watch_in_callback = true;
zval params[2], ret;
ZVAL_LONG(&params[0], (zend_long) used);
ZVAL_LONG(&params[1], cryo_watch.bytes);
zval cb;
ZVAL_COPY(&cb, &cryo_watch.callback);
call_user_function(NULL, NULL, &cb, &ret, 2, params);
zval_ptr_dtor(&cb);
zval_ptr_dtor(&ret);
cryo_watch.in_callback = false;
cryo_watch_in_callback = false;
} else if (cryo_watch.fired && (zend_long) used < cryo_watch.bytes - cryo_watch.bytes / 10) {
cryo_watch.fired = false; /* back under 90 %: armed again */
}
}
cryo_torture_tick(execute_data);
if (cryo_restart_requested() && !EG(exception)) {
cryo_freeze_deferred(execute_data); /* a cryo_restart() asked from the callback */
if (cryo_restart_requested()) zend_atomic_bool_store(&EG(vm_interrupt), true);
}
if (cryo_prev_interrupt) {
cryo_prev_interrupt(execute_data);
}
}
#ifndef CRYO_BACKEND_WASM
static void *cryo_watch_thread(void *arg)
{
(void) arg;
struct timespec ts = { 0, 10 * 1000 * 1000 };
while (!cryo_watch.stop) {
nanosleep(&ts, NULL);
if (!cryo_watch.stop) {
zend_atomic_bool_store(&EG(vm_interrupt), true);
}
}
return NULL;
}
#endif
static void cryo_watch_stop(void)
{
#ifndef CRYO_BACKEND_WASM
if (cryo_watch.thread_on) {
cryo_watch.stop = true;
pthread_join(cryo_watch.thread, NULL);
cryo_watch.thread_on = false;
}
#endif
if (cryo_watch.active) {
zval_ptr_dtor(&cryo_watch.callback);
ZVAL_UNDEF(&cryo_watch.callback);
cryo_watch.active = false;
}
}
static void cryo_watch_start(zend_long bytes, zval *cb, bool fired);
/* cryo_memory_watch(int $bytes, callable $callback): calls
* $callback(int $used, int $limit) once when the memory used by PHP reaches
* $bytes, and again after it went back under 90 %. $bytes <= 0 stops it. */
PHP_FUNCTION(cryo_memory_watch)
{
zend_long bytes;
zval *cb;
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_LONG(bytes)
Z_PARAM_ZVAL(cb)
ZEND_PARSE_PARAMETERS_END();
cryo_watch_stop();
if (bytes <= 0) {
return;
}
if (!zend_is_callable(cb, 0, NULL)) {
zend_argument_type_error(2, "must be a valid callback");
RETURN_THROWS();
}
cryo_watch_start(bytes, cb, false);
}
static void cryo_watch_start(zend_long bytes, zval *cb, bool fired)
{
ZVAL_COPY(&cryo_watch.callback, cb);
cryo_watch.bytes = bytes;
cryo_watch.fired = fired;
cryo_watch.active = true;
#ifndef CRYO_BACKEND_WASM
cryo_watch.stop = false;
if (pthread_create(&cryo_watch.thread, NULL, cryo_watch_thread, NULL) == 0) {
cryo_watch.thread_on = true;
}
#endif
zend_atomic_bool_store(&EG(vm_interrupt), true);
}
/* ['bytes' => int, 'callback' => callable, 'fired' => bool], or nothing (out stays UNDEF) */
void cryo_watch_export(zval *out)
{
if (!cryo_watch.active) return;
array_init(out);
add_assoc_long(out, "bytes", cryo_watch.bytes);
Z_TRY_ADDREF(cryo_watch.callback);
add_assoc_zval(out, "callback", &cryo_watch.callback);
/* frozen from inside the callback (cryo_restart() when the limit is reached): the new process waits to go
* back under 90 % before calling it again, as this one would have, instead of restarting in a loop */
add_assoc_bool(out, "fired", cryo_watch.fired);
}
void cryo_watch_import(zval *in)
{
if (!in || Z_TYPE_P(in) != IS_ARRAY) return;
zval *b = zend_hash_str_find(Z_ARRVAL_P(in), "bytes", 5), *cb = zend_hash_str_find(Z_ARRVAL_P(in), "callback", 8);
zval *f = zend_hash_str_find(Z_ARRVAL_P(in), "fired", 5);
if (!b || Z_TYPE_P(b) != IS_LONG || Z_LVAL_P(b) <= 0 || !cb || !zend_is_callable(cb, 0, NULL)) return;
cryo_watch_stop();
cryo_watch_start(Z_LVAL_P(b), cb, f && zend_is_true(f));
}
/* In a child forked by cryo_fork.c: threads do not survive fork(), start the
* memory watch thread again. */
void cryo_process_atfork_child(void)
{
#ifndef CRYO_BACKEND_WASM
if (cryo_watch.thread_on) {
cryo_watch.thread_on = false;
cryo_watch.stop = false;
if (pthread_create(&cryo_watch.thread, NULL, cryo_watch_thread, NULL) == 0) {
cryo_watch.thread_on = true;
}
}
#endif
}
void cryo_process_minit(void)
{
cryo_fork_minit();
memset(&cryo_watch, 0, sizeof(cryo_watch));
ZVAL_UNDEF(&cryo_watch.callback);
cryo_prev_interrupt = zend_interrupt_function;
zend_interrupt_function = cryo_interrupt;
}
void cryo_process_rshutdown(void)
{
cryo_watch_stop();
cryo_fork_rshutdown();
}
void cryo_process_mshutdown(void)
{
zend_interrupt_function = cryo_prev_interrupt;
}