From 04a6aac59beaac2ef2e91b9e3c07299f7321eae7 Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Mon, 17 Jun 2019 18:43:05 +0200 Subject: [PATCH] Turn system_id into a true global The system_id is identical for all threads and can be computed during module startup, so there is no need to calculate and store it for each thread. --- ext/opcache/ZendAccelerator.c | 10 ++++------ ext/opcache/ZendAccelerator.h | 2 +- ext/opcache/shared_alloc_win32.c | 4 ++-- ext/opcache/zend_file_cache.c | 8 ++++---- 4 files changed, 11 insertions(+), 13 deletions(-) diff --git a/ext/opcache/ZendAccelerator.c b/ext/opcache/ZendAccelerator.c index 9fa178dd115..e75ae032349 100644 --- a/ext/opcache/ZendAccelerator.c +++ b/ext/opcache/ZendAccelerator.c @@ -109,6 +109,7 @@ ZEND_TSRMLS_CACHE_DEFINE() zend_accel_shared_globals *accel_shared_globals = NULL; /* true globals, no need for thread safety */ +char accel_system_id[32]; zend_bool accel_startup_ok = 0; static char *zps_failure_reason = NULL; char *zps_api_failure_reason = NULL; @@ -2584,9 +2585,6 @@ static void accel_globals_ctor(zend_accel_globals *accel_globals) ZEND_TSRMLS_CACHE_UPDATE(); #endif memset(accel_globals, 0, sizeof(zend_accel_globals)); - - /* TODO refactor to init this just once. */ - accel_gen_system_id(); } #define ZEND_BIN_ID "BIN_" ZEND_TOSTR(SIZEOF_INT) ZEND_TOSTR(SIZEOF_LONG) ZEND_TOSTR(SIZEOF_SIZE_T) ZEND_TOSTR(SIZEOF_ZEND_LONG) ZEND_TOSTR(ZEND_MM_ALIGNMENT) @@ -2595,7 +2593,7 @@ static void accel_gen_system_id(void) { PHP_MD5_CTX context; unsigned char digest[16], c; - char *md5str = ZCG(system_id); + char *md5str = accel_system_id; int i; PHP_MD5Init(&context); @@ -2795,14 +2793,14 @@ static int accel_startup(zend_extension *extension) # endif #endif + accel_gen_system_id(); + if (start_accel_module() == FAILURE) { accel_startup_ok = 0; zend_error(E_WARNING, ACCELERATOR_PRODUCT_NAME ": module registration failed!"); return FAILURE; } - accel_gen_system_id(); - #ifdef HAVE_HUGE_CODE_PAGES if (ZCG(accel_directives).huge_code_pages && (strcmp(sapi_module.name, "cli") == 0 || diff --git a/ext/opcache/ZendAccelerator.h b/ext/opcache/ZendAccelerator.h index 3830df7b42c..a0df1c148a2 100644 --- a/ext/opcache/ZendAccelerator.h +++ b/ext/opcache/ZendAccelerator.h @@ -206,7 +206,6 @@ typedef struct _zend_accel_globals { int auto_globals_mask; time_t request_time; time_t last_restart_time; /* used to synchronize SHM and in-process caches */ - char system_id[32]; HashTable xlat_table; #ifndef ZEND_WIN32 zend_ulong root_hash; @@ -270,6 +269,7 @@ typedef struct _zend_accel_shared_globals { zend_string_table interned_strings; } zend_accel_shared_globals; +extern char accel_system_id[32]; extern zend_bool accel_startup_ok; extern zend_bool file_cache_only; #if ENABLE_FILE_CACHE_FALLBACK diff --git a/ext/opcache/shared_alloc_win32.c b/ext/opcache/shared_alloc_win32.c index 2755f394865..2d0cea1e970 100644 --- a/ext/opcache/shared_alloc_win32.c +++ b/ext/opcache/shared_alloc_win32.c @@ -74,7 +74,7 @@ static char *create_name_with_username(char *name) if (!uname) { return NULL; } - snprintf(newname, sizeof(newname) - 1, "%s@%s@%.32s", name, uname, ZCG(system_id)); + snprintf(newname, sizeof(newname) - 1, "%s@%s@%.32s", name, uname, accel_system_id); free(uname); @@ -96,7 +96,7 @@ static char *get_mmap_base_file(void) if ('\\' == windir[l-1]) { l--; } - snprintf(windir + l, sizeof(windir) - l - 1, "\\%s@%s@%.32s", ACCEL_FILEMAP_BASE, uname, ZCG(system_id)); + snprintf(windir + l, sizeof(windir) - l - 1, "\\%s@%s@%.32s", ACCEL_FILEMAP_BASE, uname, accel_system_id); free(uname); diff --git a/ext/opcache/zend_file_cache.c b/ext/opcache/zend_file_cache.c index 816acf15d99..231affedf32 100644 --- a/ext/opcache/zend_file_cache.c +++ b/ext/opcache/zend_file_cache.c @@ -798,7 +798,7 @@ static void zend_file_cache_serialize(zend_persistent_script *script, zend_persistent_script *new_script; memcpy(info->magic, "OPCACHE", 8); - memcpy(info->system_id, ZCG(system_id), 32); + memcpy(info->system_id, accel_system_id, 32); info->mem_size = script->size; info->str_size = 0; info->script_offset = (char*)script - (char*)script->mem; @@ -827,7 +827,7 @@ static char *zend_file_cache_get_bin_file_path(zend_string *script_path) filename = emalloc(len + 33 + ZSTR_LEN(script_path) + sizeof(SUFFIX)); memcpy(filename, ZCG(accel_directives).file_cache, len); filename[len] = '/'; - memcpy(filename + len + 1, ZCG(system_id), 32); + memcpy(filename + len + 1, accel_system_id, 32); memcpy(filename + len + 33, ZSTR_VAL(script_path), ZSTR_LEN(script_path)); memcpy(filename + len + 33 + ZSTR_LEN(script_path), SUFFIX, sizeof(SUFFIX)); #else @@ -859,7 +859,7 @@ static char *zend_file_cache_get_bin_file_path(zend_string *script_path) len += 32; filename[len] = '\\'; - memcpy(filename + len + 1, ZCG(system_id), 32); + memcpy(filename + len + 1, accel_system_id, 32); if (ZSTR_LEN(script_path) >= 7 && ':' == ZSTR_VAL(script_path)[4] && '/' == ZSTR_VAL(script_path)[5] && '/' == ZSTR_VAL(script_path)[6]) { /* phar:// or file:// */ @@ -1550,7 +1550,7 @@ zend_persistent_script *zend_file_cache_script_load(zend_file_handle *file_handl efree(filename); return NULL; } - if (memcmp(info.system_id, ZCG(system_id), 32) != 0) { + if (memcmp(info.system_id, accel_system_id, 32) != 0) { zend_accel_error(ACCEL_LOG_WARNING, "opcache cannot read from file '%s' (wrong \"system_id\")\n", filename); zend_file_cache_flock(fd, LOCK_UN); close(fd);