From 66aab8ec0d696eb329ff3c7abec9c26c288632f5 Mon Sep 17 00:00:00 2001 From: twosee Date: Thu, 27 May 2021 12:00:34 +0800 Subject: [PATCH 1/2] Add zend_getpagesize() and reuse it in accelerator and fiber --- Zend/zend.c | 15 +++++++++++++++ Zend/zend.h | 1 + Zend/zend_fibers.c | 20 +++++++++----------- ext/opcache/ZendAccelerator.c | 10 ++-------- 4 files changed, 27 insertions(+), 19 deletions(-) diff --git a/Zend/zend.c b/Zend/zend.c index d86158d7a97ed..4a49b18c47777 100644 --- a/Zend/zend.c +++ b/Zend/zend.c @@ -1207,6 +1207,21 @@ ZEND_API ZEND_COLD ZEND_NORETURN void _zend_bailout(const char *filename, uint32 /* }}} */ END_EXTERN_C() +ZEND_API size_t zend_getpagesize(void) +{ +#ifdef _WIN32 + SYSTEM_INFO system_info; + GetSystemInfo(&system_info); + return system_info.dwPageSize; +#elif defined(__FreeBSD__) + /* This returns the value obtained from + * the auxv vector, avoiding a syscall. */ + return getpagesize(); +#else + return (size_t) sysconf(_SC_PAGESIZE); +#endif +} + ZEND_API void zend_append_version_info(const zend_extension *extension) /* {{{ */ { char *new_info; diff --git a/Zend/zend.h b/Zend/zend.h index 4b791b0486764..481c5536428e8 100644 --- a/Zend/zend.h +++ b/Zend/zend.h @@ -265,6 +265,7 @@ zend_result zend_post_startup(void); void zend_set_utility_values(zend_utility_values *utility_values); ZEND_API ZEND_COLD ZEND_NORETURN void _zend_bailout(const char *filename, uint32_t lineno); +ZEND_API size_t zend_getpagesize(void); ZEND_API size_t zend_vspprintf(char **pbuf, size_t max_len, const char *format, va_list ap); ZEND_API size_t zend_spprintf(char **message, size_t max_len, const char *format, ...) ZEND_ATTRIBUTE_FORMAT(printf, 3, 4); diff --git a/Zend/zend_fibers.c b/Zend/zend_fibers.c index b489df1085311..ac534e94bfce8 100644 --- a/Zend/zend_fibers.c +++ b/Zend/zend_fibers.c @@ -90,27 +90,25 @@ extern transfer_t jump_fcontext(fcontext_t to, void *vp); # define ZEND_FIBER_STACK_FLAGS (MAP_PRIVATE | MAP_ANON) #endif - - -static size_t zend_fiber_page_size() +static size_t zend_fiber_getpagesize(void) { -#if _POSIX_MAPPED_FILES - static size_t page_size; + static size_t page_size = 0; if (!page_size) { - page_size = sysconf(_SC_PAGESIZE); + page_size = zend_getpagesize(); + if (!page_size || (page_size & (page_size - 1))) { + /* anyway, we have to return a valid result */ + page_size = ZEND_FIBER_DEFAULT_PAGE_SIZE; + } } return page_size; -#else - return ZEND_FIBER_DEFAULT_PAGE_SIZE; -#endif } static bool zend_fiber_stack_allocate(zend_fiber_stack *stack, size_t size) { void *pointer; - const size_t page_size = zend_fiber_page_size(); + const size_t page_size = zend_fiber_getpagesize(); ZEND_ASSERT(size >= page_size + ZEND_FIBER_GUARD_PAGES * page_size); @@ -167,7 +165,7 @@ static void zend_fiber_stack_free(zend_fiber_stack *stack) VALGRIND_STACK_DEREGISTER(stack->valgrind); #endif - const size_t page_size = zend_fiber_page_size(); + const size_t page_size = zend_fiber_getpagesize(); void *pointer = (void *) ((uintptr_t) stack->pointer - ZEND_FIBER_GUARD_PAGES * page_size); diff --git a/ext/opcache/ZendAccelerator.c b/ext/opcache/ZendAccelerator.c index 71a54c080d681..500332c5c8890 100644 --- a/ext/opcache/ZendAccelerator.c +++ b/ext/opcache/ZendAccelerator.c @@ -3171,14 +3171,8 @@ static zend_result accel_post_startup(void) && zend_jit_check_support() == SUCCESS) { size_t page_size; -# ifdef _WIN32 - SYSTEM_INFO system_info; - GetSystemInfo(&system_info); - page_size = system_info.dwPageSize; -# else - page_size = getpagesize(); -# endif - if (!page_size || (page_size & (page_size - 1))) { + page_size = zend_getpagesize(); + if (!page_size && (page_size & (page_size - 1))) { zend_accel_error_noreturn(ACCEL_LOG_FATAL, "Failure to initialize shared memory structures - can't get page size."); abort(); } From 6a908ece178653d54150b9a46f9fde6ce0c999aa Mon Sep 17 00:00:00 2001 From: twosee Date: Thu, 27 May 2021 17:25:28 +0800 Subject: [PATCH 2/2] Rename [ci skip] --- Zend/zend.c | 2 +- Zend/zend.h | 2 +- Zend/zend_fibers.c | 8 ++++---- ext/opcache/ZendAccelerator.c | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Zend/zend.c b/Zend/zend.c index 4a49b18c47777..b8583c342de2a 100644 --- a/Zend/zend.c +++ b/Zend/zend.c @@ -1207,7 +1207,7 @@ ZEND_API ZEND_COLD ZEND_NORETURN void _zend_bailout(const char *filename, uint32 /* }}} */ END_EXTERN_C() -ZEND_API size_t zend_getpagesize(void) +ZEND_API size_t zend_get_page_size(void) { #ifdef _WIN32 SYSTEM_INFO system_info; diff --git a/Zend/zend.h b/Zend/zend.h index 481c5536428e8..c051ce14ecc17 100644 --- a/Zend/zend.h +++ b/Zend/zend.h @@ -265,7 +265,7 @@ zend_result zend_post_startup(void); void zend_set_utility_values(zend_utility_values *utility_values); ZEND_API ZEND_COLD ZEND_NORETURN void _zend_bailout(const char *filename, uint32_t lineno); -ZEND_API size_t zend_getpagesize(void); +ZEND_API size_t zend_get_page_size(void); ZEND_API size_t zend_vspprintf(char **pbuf, size_t max_len, const char *format, va_list ap); ZEND_API size_t zend_spprintf(char **message, size_t max_len, const char *format, ...) ZEND_ATTRIBUTE_FORMAT(printf, 3, 4); diff --git a/Zend/zend_fibers.c b/Zend/zend_fibers.c index ac534e94bfce8..5045a9b0985a8 100644 --- a/Zend/zend_fibers.c +++ b/Zend/zend_fibers.c @@ -90,12 +90,12 @@ extern transfer_t jump_fcontext(fcontext_t to, void *vp); # define ZEND_FIBER_STACK_FLAGS (MAP_PRIVATE | MAP_ANON) #endif -static size_t zend_fiber_getpagesize(void) +static size_t zend_fiber_get_page_size(void) { static size_t page_size = 0; if (!page_size) { - page_size = zend_getpagesize(); + page_size = zend_get_page_size(); if (!page_size || (page_size & (page_size - 1))) { /* anyway, we have to return a valid result */ page_size = ZEND_FIBER_DEFAULT_PAGE_SIZE; @@ -108,7 +108,7 @@ static size_t zend_fiber_getpagesize(void) static bool zend_fiber_stack_allocate(zend_fiber_stack *stack, size_t size) { void *pointer; - const size_t page_size = zend_fiber_getpagesize(); + const size_t page_size = zend_fiber_get_page_size(); ZEND_ASSERT(size >= page_size + ZEND_FIBER_GUARD_PAGES * page_size); @@ -165,7 +165,7 @@ static void zend_fiber_stack_free(zend_fiber_stack *stack) VALGRIND_STACK_DEREGISTER(stack->valgrind); #endif - const size_t page_size = zend_fiber_getpagesize(); + const size_t page_size = zend_fiber_get_page_size(); void *pointer = (void *) ((uintptr_t) stack->pointer - ZEND_FIBER_GUARD_PAGES * page_size); diff --git a/ext/opcache/ZendAccelerator.c b/ext/opcache/ZendAccelerator.c index 500332c5c8890..a7a5a51c37c3d 100644 --- a/ext/opcache/ZendAccelerator.c +++ b/ext/opcache/ZendAccelerator.c @@ -3171,7 +3171,7 @@ static zend_result accel_post_startup(void) && zend_jit_check_support() == SUCCESS) { size_t page_size; - page_size = zend_getpagesize(); + page_size = zend_get_page_size(); if (!page_size && (page_size & (page_size - 1))) { zend_accel_error_noreturn(ACCEL_LOG_FATAL, "Failure to initialize shared memory structures - can't get page size."); abort();