diff --git a/NEWS b/NEWS index eaeb9894e2b2..78e772d44b7c 100644 --- a/NEWS +++ b/NEWS @@ -11,6 +11,8 @@ PHP NEWS . Improved performance of integer to string conversion, used by string casts, echo, implode(), json_encode(), serialize() and other places, by emitting two digits per step. (Alexander Danilov) + . Zend max execution timers are now reused across requests on Linux + instead of being recreated for every request. (henderkes) - Curl: . Negative values for CURLOPT_POSTREDIR are now rejected, causing diff --git a/TSRM/TSRM_atexit.h b/TSRM/TSRM_atexit.h index ae73597e0e31..74cfcee2f3cd 100644 --- a/TSRM/TSRM_atexit.h +++ b/TSRM/TSRM_atexit.h @@ -21,6 +21,9 @@ typedef struct _tsrm_tls_entry tsrm_tls_entry; void tsrm_atexit_init(void); void tsrm_atexit_set_data(tsrm_tls_entry *thread_resources); +#if !defined(TSRM_WIN32) && !defined(__APPLE__) +bool tsrm_atexit_is_registered(void); +#endif void tsrm_atexit_disarm(void); void tsrm_free_thread_at_exit(tsrm_tls_entry *thread_resources); diff --git a/TSRM/TSRM_atexit_posix.c b/TSRM/TSRM_atexit_posix.c index cbbaafaedfe9..cfb84b8054dc 100644 --- a/TSRM/TSRM_atexit_posix.c +++ b/TSRM/TSRM_atexit_posix.c @@ -44,6 +44,11 @@ void tsrm_atexit_set_data(tsrm_tls_entry *thread_resources) pthread_setspecific(tsrm_exit_key, thread_resources); } +bool tsrm_atexit_is_registered(void) +{ + return tsrm_thread_exit_armed && pthread_getspecific(tsrm_exit_key) != NULL; +} + void tsrm_atexit_disarm(void) { if (!tsrm_thread_exit_armed) { diff --git a/Zend/zend.c b/Zend/zend.c index 560a365318de..49fd16a5b222 100644 --- a/Zend/zend.c +++ b/Zend/zend.c @@ -851,7 +851,6 @@ static void executor_globals_ctor(zend_executor_globals *executor_globals) /* {{ #endif #ifdef ZEND_MAX_EXECUTION_TIMERS executor_globals->pid = 0; - executor_globals->oldact = (struct sigaction){0}; #endif memset(executor_globals->strtod_state.freelist, 0, sizeof(executor_globals->strtod_state.freelist)); @@ -1090,6 +1089,7 @@ void zend_startup(zend_utility_functions *utility_functions) /* {{{ */ zend_optimizer_startup(); #ifdef ZTS + zend_max_execution_timer_cache_startup(); tsrm_set_new_thread_end_handler(zend_new_thread_end_handler); tsrm_set_thread_free_handler(zend_thread_free_handler); tsrm_set_shutdown_handler(zend_interned_strings_dtor); diff --git a/Zend/zend_API.c b/Zend/zend_API.c index c19e9d64e266..568bff81d604 100644 --- a/Zend/zend_API.c +++ b/Zend/zend_API.c @@ -2595,6 +2595,7 @@ ZEND_API void zend_destroy_modules(void) /* {{{ */ free(module_request_startup_handlers); module_request_startup_handlers = NULL; zend_hash_graceful_reverse_destroy(&module_registry); + zend_max_execution_timer_cache_shutdown(); } /* }}} */ diff --git a/Zend/zend_execute_API.c b/Zend/zend_execute_API.c index 94273a4bc59f..4317ba0de1d7 100644 --- a/Zend/zend_execute_API.c +++ b/Zend/zend_execute_API.c @@ -1520,6 +1520,10 @@ ZEND_NORETURN ZEND_API void ZEND_FASTCALL zend_timeout(void) /* {{{ */ #ifndef ZEND_WIN32 # ifdef ZEND_MAX_EXECUTION_TIMERS +/* The handler we displaced is process-wide. A null sigaction selects the one-argument handler. */ +static _Atomic(void (*)(int, siginfo_t *, void *)) previous_sigaction; +static _Atomic(void (*)(int)) previous_handler; + static void zend_timeout_handler(int dummy, siginfo_t *si, void *uc) /* {{{ */ { #ifdef ZTS @@ -1535,12 +1539,16 @@ static void zend_timeout_handler(int dummy, siginfo_t *si, void *uc) /* {{{ */ fprintf(stderr, "Executing previous handler (if set) for unexpected signal SIGRTMIN received on thread %d\n", (pid_t) syscall(SYS_gettid)); #endif - if (EG(oldact).sa_sigaction) { - EG(oldact).sa_sigaction(dummy, si, uc); + void (*action)(int, siginfo_t *, void *) = atomic_load(&previous_sigaction); + if (action) { + action(dummy, si, uc); return; } - if (EG(oldact).sa_handler) EG(oldact).sa_handler(dummy); + void (*handler)(int) = atomic_load(&previous_handler); + if (handler != SIG_IGN && handler != SIG_DFL) { + handler(dummy); + } return; } @@ -1658,12 +1666,21 @@ static void zend_set_timeout_ex(zend_long seconds, bool reset_signals) /* {{{ */ if (reset_signals) { sigset_t sigset; - struct sigaction act; + struct sigaction act, old; act.sa_sigaction = zend_timeout_handler; sigemptyset(&act.sa_mask); act.sa_flags = SA_ONSTACK | SA_SIGINFO; - sigaction(SIGRTMIN, &act, NULL); + if (sigaction(SIGRTMIN, NULL, &old) == 0 + && !((old.sa_flags & SA_SIGINFO) && old.sa_sigaction == zend_timeout_handler)) { + if ((old.sa_flags & SA_SIGINFO) && old.sa_handler != SIG_DFL && old.sa_handler != SIG_IGN) { + atomic_store(&previous_sigaction, old.sa_sigaction); + } else { + atomic_store(&previous_handler, old.sa_handler); + atomic_store(&previous_sigaction, NULL); + } + sigaction(SIGRTMIN, &act, NULL); + } sigemptyset(&sigset); sigaddset(&sigset, SIGRTMIN); sigprocmask(SIG_UNBLOCK, &sigset, NULL); diff --git a/Zend/zend_globals.h b/Zend/zend_globals.h index bf93bf0013a9..17882a3bc9b8 100644 --- a/Zend/zend_globals.h +++ b/Zend/zend_globals.h @@ -327,7 +327,6 @@ struct _zend_executor_globals { #ifdef ZEND_MAX_EXECUTION_TIMERS timer_t max_execution_timer_timer; pid_t pid; - struct sigaction oldact; #endif zend_strtod_state strtod_state; diff --git a/Zend/zend_max_execution_timer.c b/Zend/zend_max_execution_timer.c index 5d0eb1219c0b..7ac1e40637eb 100644 --- a/Zend/zend_max_execution_timer.c +++ b/Zend/zend_max_execution_timer.c @@ -29,6 +29,10 @@ #include "zend_globals.h" #include "zend_portability.h" +#if defined(ZEND_MAX_EXECUTION_TIMER_CACHE) && defined(ZTS) +# include "TSRM/TSRM_atexit.h" +#endif + #if __has_feature(memory_sanitizer) # include #endif @@ -46,6 +50,56 @@ # define ZEND_MAX_EXECUTION_TIMERS_CLOCK CLOCK_BOOTTIME # endif +#ifdef ZEND_MAX_EXECUTION_TIMER_CACHE +/* Owns the timer from creation until deletion, including during a request. */ +typedef struct { + pid_t pid; + timer_t timer; +} zend_cached_timer; + +static void timer_cache_dtor(void *ptr) +{ + zend_cached_timer *cache = ptr; + if (cache->pid == getpid()) { + timer_delete(cache->timer); + } + cache->pid = 0; +} + +# ifdef ZTS +static ts_rsrc_id timer_cache_id; +# define TIMER_CACHE (timer_cache_id ? TSRMG_BULK_STATIC(timer_cache_id, zend_cached_timer *) : NULL) +# define CAN_CACHE_TIMER() tsrm_atexit_is_registered() + +static void timer_cache_ctor(void *ptr) +{ + ((zend_cached_timer *) ptr)->pid = 0; +} + +void zend_max_execution_timer_cache_startup(void) +{ + ts_allocate_id(&timer_cache_id, sizeof(zend_cached_timer), timer_cache_ctor, timer_cache_dtor); +} + +# else +static zend_cached_timer process_timer_cache; +# define TIMER_CACHE (&process_timer_cache) +# define CAN_CACHE_TIMER() true +# endif + +void zend_max_execution_timer_cache_shutdown(void) +{ +# ifdef ZTS + if (timer_cache_id) { + ts_free_id(timer_cache_id); + timer_cache_id = 0; + } +# else + timer_cache_dtor(&process_timer_cache); +# endif +} +#endif + ZEND_API void zend_max_execution_timer_init(void) /* {{{ */ { pid_t pid = getpid(); @@ -54,6 +108,18 @@ ZEND_API void zend_max_execution_timer_init(void) /* {{{ */ return; } +#ifdef ZEND_MAX_EXECUTION_TIMER_CACHE + zend_cached_timer *cache = TIMER_CACHE; + if (cache && cache->pid == pid) { + EG(max_execution_timer_timer) = cache->timer; + EG(pid) = pid; +# ifdef MAX_EXECUTION_TIMERS_DEBUG + fprintf(stderr, "Timer %#jx reused on thread %d\n", (uintmax_t) EG(max_execution_timer_timer), (pid_t) syscall(SYS_gettid)); +# endif + return; + } +#endif + struct sigevent sev; sev.sigev_notify = SIGEV_THREAD_ID; sev.sigev_value.sival_ptr = &EG(max_execution_timer_timer); @@ -76,12 +142,15 @@ ZEND_API void zend_max_execution_timer_init(void) /* {{{ */ } EG(pid) = pid; +#ifdef ZEND_MAX_EXECUTION_TIMER_CACHE + if (cache) { + *cache = (zend_cached_timer) {pid, EG(max_execution_timer_timer)}; + } +#endif # ifdef MAX_EXECUTION_TIMERS_DEBUG fprintf(stderr, "Timer %#jx created on thread %d\n", (uintmax_t) EG(max_execution_timer_timer), sev.sigev_notify_thread_id); # endif - - sigaction(sev.sigev_signo, NULL, &EG(oldact)); } /* }}} */ @@ -120,6 +189,22 @@ void zend_max_execution_timer_shutdown(void) /* {{{ */ return; } +#ifdef ZEND_MAX_EXECUTION_TIMER_CACHE + static const struct itimerspec disarmed = {0}; + zend_cached_timer *cache = TIMER_CACHE; + if (cache && cache->pid == EG(pid) && CAN_CACHE_TIMER() + && timer_settime(EG(max_execution_timer_timer), 0, &disarmed, NULL) == 0) { + EG(pid) = 0; +# ifdef MAX_EXECUTION_TIMERS_DEBUG + fprintf(stderr, "Timer %#jx disarmed and cached on thread %d\n", (uintmax_t) cache->timer, (pid_t) syscall(SYS_gettid)); +# endif + return; + } + if (cache) { + cache->pid = 0; + } +#endif + EG(pid) = 0; timer_t timer = EG(max_execution_timer_timer); diff --git a/Zend/zend_max_execution_timer.h b/Zend/zend_max_execution_timer.h index 3839ea4cf676..ddb1e0ca44a0 100644 --- a/Zend/zend_max_execution_timer.h +++ b/Zend/zend_max_execution_timer.h @@ -19,6 +19,11 @@ #include "zend_long.h" +/* Linux reuses each thread's timer across requests instead of recreating it. */ +# ifdef __linux__ +# define ZEND_MAX_EXECUTION_TIMER_CACHE +# endif + BEGIN_EXTERN_C() /* Must be called after calls to fork() */ ZEND_API void zend_max_execution_timer_init(void); @@ -33,4 +38,15 @@ void zend_max_execution_timer_shutdown(void); #define zend_max_execution_timer_shutdown() # endif + +# ifdef ZEND_MAX_EXECUTION_TIMER_CACHE +# ifdef ZTS +void zend_max_execution_timer_cache_startup(void); +# endif +void zend_max_execution_timer_cache_shutdown(void); +# else +# define zend_max_execution_timer_cache_startup() +# define zend_max_execution_timer_cache_shutdown() +# endif + #endif diff --git a/sapi/cli/tests/max_execution_timer_reuse.phpt b/sapi/cli/tests/max_execution_timer_reuse.phpt new file mode 100644 index 000000000000..830c9a2862ed --- /dev/null +++ b/sapi/cli/tests/max_execution_timer_reuse.phpt @@ -0,0 +1,47 @@ +--TEST-- +Zend max execution timers keep firing when reused across requests on one thread +--ENV-- +PHP_CLI_SERVER_WORKERS= +--INI-- +allow_url_fopen=1 +--SKIPIF-- + enabled')) { + die("skip Zend max execution timers are disabled"); +} +?> +--FILE-- + ['ignore_errors' => true, 'timeout' => 10]]); +foreach (['busy', 'idle', 'busy'] as $request) { + $query = $request === 'busy' ? '?busy=1' : ''; + $body = @file_get_contents("http://" . PHP_CLI_SERVER_ADDRESS . "/$query", false, $context); + if ($body === false) { + echo "$request: no response\n"; + } elseif (preg_match('/Maximum execution time of 1 second exceeded/', $body)) { + echo "$request: timed out\n"; + } else { + echo "$request: ", trim($body), "\n"; + } +} +?> +--EXPECT-- +busy: timed out +idle: ok +busy: timed out