From 7cf0106d33037f2098ec029297dfb4dd59b92660 Mon Sep 17 00:00:00 2001 From: Antony Dovgal Date: Mon, 2 Nov 2020 21:24:21 +0300 Subject: [PATCH 1/4] Force file revalidation in long running CLI scripts --- ext/opcache/ZendAccelerator.c | 36 ++++++++++++++++++++++++++++------- ext/opcache/ZendAccelerator.h | 1 + 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/ext/opcache/ZendAccelerator.c b/ext/opcache/ZendAccelerator.c index bd6b323871bcc..990b41dcd46cc 100644 --- a/ext/opcache/ZendAccelerator.c +++ b/ext/opcache/ZendAccelerator.c @@ -1162,14 +1162,35 @@ zend_result validate_timestamp_and_record(zend_persistent_script *persistent_scr { if (persistent_script->timestamp == 0) { return SUCCESS; /* Don't check timestamps of preloaded scripts */ - } else if (ZCG(accel_directives).revalidate_freq && - persistent_script->dynamic_members.revalidate >= ZCG(request_time)) { - return SUCCESS; - } else if (do_validate_timestamps(persistent_script, file_handle) == FAILURE) { - return FAILURE; + } + + if (ZCG(cli_mode)) { + struct timeval tp = {0}; + + //check current time as opposed to the "time of request" + if (gettimeofday(&tp, NULL) != 0) { + return SUCCESS; + } + + double now = (double)(tp.tv_sec + tp.tv_usec / 1000000.00); + + if (ZCG(accel_directives).revalidate_freq && persistent_script->dynamic_members.revalidate >= now) { + return SUCCESS; + } else if (do_validate_timestamps(persistent_script, file_handle) == FAILURE) { + return FAILURE; + } else { + persistent_script->dynamic_members.revalidate = now + ZCG(accel_directives).revalidate_freq; + return SUCCESS; + } } else { - persistent_script->dynamic_members.revalidate = ZCG(request_time) + ZCG(accel_directives).revalidate_freq; - return SUCCESS; + if (ZCG(accel_directives).revalidate_freq && persistent_script->dynamic_members.revalidate >= ZCG(request_time)) { + return SUCCESS; + } else if (do_validate_timestamps(persistent_script, file_handle) == FAILURE) { + return FAILURE; + } else { + persistent_script->dynamic_members.revalidate = ZCG(request_time) + ZCG(accel_directives).revalidate_freq; + return SUCCESS; + } } } @@ -2832,6 +2853,7 @@ static inline zend_result accel_find_sapi(void) if (ZCG(accel_directives).enable_cli && ( strcmp(sapi_module.name, "cli") == 0 || strcmp(sapi_module.name, "phpdbg") == 0)) { + ZCG(cli_mode) = true; return SUCCESS; } } diff --git a/ext/opcache/ZendAccelerator.h b/ext/opcache/ZendAccelerator.h index a25f9a1bdf037..bcd3548ec1e50 100644 --- a/ext/opcache/ZendAccelerator.h +++ b/ext/opcache/ZendAccelerator.h @@ -224,6 +224,7 @@ typedef struct _zend_accel_globals { zend_persistent_script *cache_persistent_script; /* preallocated buffer for keys */ zend_string *key; + bool cli_mode; } zend_accel_globals; typedef struct _zend_string_table { From eaa7380287a512d13d598fc2ff69bc11d3ba228c Mon Sep 17 00:00:00 2001 From: Oleg Efimov Date: Tue, 27 May 2025 14:26:47 +0100 Subject: [PATCH 2/4] Simplify the logic and fix Windows build --- ext/opcache/ZendAccelerator.c | 45 +++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/ext/opcache/ZendAccelerator.c b/ext/opcache/ZendAccelerator.c index 990b41dcd46cc..7c3abcbce5fba 100644 --- a/ext/opcache/ZendAccelerator.c +++ b/ext/opcache/ZendAccelerator.c @@ -76,7 +76,12 @@ typedef int gid_t; #endif #include #include + +#ifndef PHP_WIN32 #include +#else +#include "win32/time.h" +#endif #ifndef ZEND_WIN32 # include @@ -1164,33 +1169,31 @@ zend_result validate_timestamp_and_record(zend_persistent_script *persistent_scr return SUCCESS; /* Don't check timestamps of preloaded scripts */ } + double revalidate_reference_time = 0.0; + if (ZCG(cli_mode)) { +#if HAVE_GETTIMEOFDAY struct timeval tp = {0}; - //check current time as opposed to the "time of request" - if (gettimeofday(&tp, NULL) != 0) { - return SUCCESS; - } - - double now = (double)(tp.tv_sec + tp.tv_usec / 1000000.00); - - if (ZCG(accel_directives).revalidate_freq && persistent_script->dynamic_members.revalidate >= now) { - return SUCCESS; - } else if (do_validate_timestamps(persistent_script, file_handle) == FAILURE) { - return FAILURE; + if (UNEXPECTED(gettimeofday(&tp, NULL) != 0)) { + revalidate_reference_time = (double)time(NULL); } else { - persistent_script->dynamic_members.revalidate = now + ZCG(accel_directives).revalidate_freq; - return SUCCESS; + revalidate_reference_time = (double)(tp.tv_sec + tp.tv_usec / 1000000.00); } +#else + revalidate_reference_time = (double)time(NULL); +#endif } else { - if (ZCG(accel_directives).revalidate_freq && persistent_script->dynamic_members.revalidate >= ZCG(request_time)) { - return SUCCESS; - } else if (do_validate_timestamps(persistent_script, file_handle) == FAILURE) { - return FAILURE; - } else { - persistent_script->dynamic_members.revalidate = ZCG(request_time) + ZCG(accel_directives).revalidate_freq; - return SUCCESS; - } + revalidate_reference_time = (double)ZCG(request_time); + } + + if (ZCG(accel_directives).revalidate_freq && persistent_script->dynamic_members.revalidate >= revalidate_reference_time) { + return SUCCESS; + } else if (do_validate_timestamps(persistent_script, file_handle) == FAILURE) { + return FAILURE; + } else { + persistent_script->dynamic_members.revalidate = revalidate_reference_time + ZCG(accel_directives).revalidate_freq; + return SUCCESS; } } From e210c08ab341983a69cf67163a34aa68e22de8c7 Mon Sep 17 00:00:00 2001 From: Oleg Efimov Date: Tue, 27 May 2025 13:29:58 +0100 Subject: [PATCH 3/4] Add test case for opcache revalidation in cli --- .../tests/opcache_revalidation_in_cli.phpt | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 ext/opcache/tests/opcache_revalidation_in_cli.phpt diff --git a/ext/opcache/tests/opcache_revalidation_in_cli.phpt b/ext/opcache/tests/opcache_revalidation_in_cli.phpt new file mode 100644 index 0000000000000..e63ba4000b06d --- /dev/null +++ b/ext/opcache/tests/opcache_revalidation_in_cli.phpt @@ -0,0 +1,42 @@ +--TEST-- +opcache revalidation should work properly in CLI mode +--EXTENSIONS-- +opcache +--INI-- +opcache.enable=1 +opcache.enable_cli=1 +opcache.validate_timestamps=1 +opcache.revalidate_freq=1 +--FILE-- + +--EXPECT-- +bool(true) +int(42) +int(42) +int(1234) From 3980773fa556685a761e9a2c67b8f34383930654 Mon Sep 17 00:00:00 2001 From: Oleg Efimov Date: Wed, 28 May 2025 11:31:49 +0100 Subject: [PATCH 4/4] Fix test case when run with --repeat --- ext/opcache/tests/opcache_revalidation_in_cli.phpt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/opcache/tests/opcache_revalidation_in_cli.phpt b/ext/opcache/tests/opcache_revalidation_in_cli.phpt index e63ba4000b06d..3cd9a29e74346 100644 --- a/ext/opcache/tests/opcache_revalidation_in_cli.phpt +++ b/ext/opcache/tests/opcache_revalidation_in_cli.phpt @@ -16,7 +16,7 @@ file_put_contents($file, <<