Repository navigation
perf: reuse zend max execution timers between requests #25673
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
ec0f1ba
8b3fbd7
d2cf0af
1ca9492
4105225
03f0270
f44c1eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 <sanitizer/msan_interface.h> | ||
| #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) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A disarmed timer still consumes one |
||
| 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); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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-- | ||
| <?php | ||
| include "skipif.inc"; | ||
| if (PHP_OS !== 'Linux') { | ||
| die("skip Linux only"); | ||
| } | ||
| ob_start(); | ||
| phpinfo(INFO_GENERAL); | ||
| if (!str_contains(ob_get_clean(), 'Zend Max Execution Timers => enabled')) { | ||
| die("skip Zend max execution timers are disabled"); | ||
| } | ||
| ?> | ||
| --FILE-- | ||
| <?php | ||
| include "php_cli_server.inc"; | ||
| php_cli_server_start(<<<'PHP' | ||
|
henderkes marked this conversation as resolved.
Resolved
|
||
| set_time_limit(1); | ||
| if (isset($_GET['busy'])) { | ||
| while (true) {} | ||
| } | ||
| echo "ok\n"; | ||
| PHP); | ||
|
|
||
| /* A broken timer would leave the busy loop spinning; fail instead of hanging. */ | ||
| $context = stream_context_create(['http' => ['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 | ||
Uh oh!
There was an error while loading. Please reload this page.