Skip to content

Commit b0daa46

Browse files
committed
Preliminary file cache support
1 parent 696ac99 commit b0daa46

File tree

3 files changed

+45
-26
lines changed

3 files changed

+45
-26
lines changed

ext/opcache/ZendAccelerator.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1890,7 +1890,7 @@ static zend_persistent_script *opcache_compile_file(zend_file_handle *file_handl
18901890
return new_persistent_script;
18911891
}
18921892

1893-
static zend_op_array *file_cache_compile_file(zend_file_handle *file_handle, int type)
1893+
static zend_op_array *file_cache_compile_file(zend_file_handle *file_handle, int type, zend_persistent_script **persistent_script_p)
18941894
{
18951895
zend_persistent_script *persistent_script;
18961896
zend_op_array *op_array = NULL;
@@ -1949,6 +1949,9 @@ static zend_op_array *file_cache_compile_file(zend_file_handle *file_handle, int
19491949
zend_accel_set_auto_globals(persistent_script->ping_auto_globals_mask & ~ZCG(auto_globals_mask));
19501950
}
19511951

1952+
if (persistent_script_p) {
1953+
*persistent_script_p = persistent_script;
1954+
}
19521955
return zend_accel_load_script(persistent_script, 1);
19531956
}
19541957

@@ -1957,6 +1960,9 @@ static zend_op_array *file_cache_compile_file(zend_file_handle *file_handle, int
19571960
if (persistent_script) {
19581961
from_memory = false;
19591962
persistent_script = cache_script_in_file_cache(persistent_script, &from_memory);
1963+
if (persistent_script_p) {
1964+
*persistent_script_p = persistent_script;
1965+
}
19601966
return zend_accel_load_script(persistent_script, from_memory);
19611967
}
19621968

@@ -1998,16 +2004,16 @@ zend_op_array *persistent_compile_file_ex(zend_file_handle *file_handle, int typ
19982004
if (file_handle->filename
19992005
&& ZCG(accel_directives).file_cache
20002006
&& ZCG(enabled) && accel_startup_ok) {
2001-
return file_cache_compile_file(file_handle, type);
2007+
return file_cache_compile_file(file_handle, type, persistent_script_p);
20022008
}
20032009
return accelerator_orig_compile_file(file_handle, type);
20042010
} else if (file_cache_only) {
20052011
ZCG(cache_opline) = NULL;
20062012
ZCG(cache_persistent_script) = NULL;
2007-
return file_cache_compile_file(file_handle, type);
2013+
return file_cache_compile_file(file_handle, type, persistent_script_p);
20082014
} else if ((ZCSG(restart_in_progress) && accel_restart_is_active())) {
20092015
if (ZCG(accel_directives).file_cache) {
2010-
return file_cache_compile_file(file_handle, type);
2016+
return file_cache_compile_file(file_handle, type, persistent_script_p);
20112017
}
20122018
ZCG(cache_opline) = NULL;
20132019
ZCG(cache_persistent_script) = NULL;
@@ -2099,7 +2105,7 @@ zend_op_array *persistent_compile_file_ex(zend_file_handle *file_handle, int typ
20992105
if (!ZCG(counted)) {
21002106
if (accel_activate_add() == FAILURE) {
21012107
if (ZCG(accel_directives).file_cache) {
2102-
return file_cache_compile_file(file_handle, type);
2108+
return file_cache_compile_file(file_handle, type, persistent_script_p);
21032109
}
21042110
return accelerator_orig_compile_file(file_handle, type);
21052111
}
@@ -2150,7 +2156,7 @@ zend_op_array *persistent_compile_file_ex(zend_file_handle *file_handle, int typ
21502156
SHM_PROTECT();
21512157
HANDLE_UNBLOCK_INTERRUPTIONS();
21522158
if (ZCG(accel_directives).file_cache) {
2153-
return file_cache_compile_file(file_handle, type);
2159+
return file_cache_compile_file(file_handle, type, persistent_script_p);
21542160
}
21552161
return accelerator_orig_compile_file(file_handle, type);
21562162
}
@@ -2239,7 +2245,7 @@ zend_op_array *persistent_compile_file_ex(zend_file_handle *file_handle, int typ
22392245
zend_accel_set_auto_globals(persistent_script->ping_auto_globals_mask & ~ZCG(auto_globals_mask));
22402246
}
22412247

2242-
if (persistent_script_p && from_shared_memory) {
2248+
if (persistent_script_p) {
22432249
*persistent_script_p = persistent_script;
22442250
}
22452251
return zend_accel_load_script(persistent_script, from_shared_memory);

ext/opcache/modules.c

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ static zend_persistent_script *zum_compile_file(zend_user_module *module, zend_f
225225
if (!persistent_script) {
226226
/* For PoC purpose, we only support files that can be cached in SHM for
227227
* now. */
228-
zend_error_noreturn(E_ERROR, "File %s could not be cached in SHM. This is unsupported in modules (yet).",
228+
zend_error_noreturn(E_ERROR, "File %s could not be cached in opcache (SHM or file cache). This is unsupported in modules (yet).",
229229
ZSTR_VAL(file_handle->filename));
230230
return NULL;
231231
}
@@ -584,7 +584,7 @@ static zend_result zum_check_deps_class(zend_user_module *module, zend_class_ent
584584
if (!zend_string_equals(ce->info.user.module, module->desc.lcname)) {
585585
ZUM_DEBUG("Module %s depends on %s\n", ZSTR_VAL(module->desc.lcname), ZSTR_VAL(ce->info.user.module));
586586

587-
if (!(ce->ce_flags & ZEND_ACC_IMMUTABLE)) {
587+
if (!(ce->ce_flags & ZEND_ACC_IMMUTABLE) && !ZCG(accel_directives).file_cache_only) {
588588
zend_throw_error(NULL,
589589
"Module %s can not depend on uncacheable class %s",
590590
ZSTR_VAL(module->desc.name), ZSTR_VAL(ce->name));
@@ -616,7 +616,7 @@ static zend_result zum_check_deps_class_decl(zend_user_module *module, zend_clas
616616
ZEND_ASSERT(!EG(exception));
617617
ZEND_ASSERT(ce->ce_flags & ZEND_ACC_LINKED);
618618

619-
if (!(ce->ce_flags & ZEND_ACC_IMMUTABLE)) {
619+
if (!(ce->ce_flags & ZEND_ACC_IMMUTABLE) && !ZCG(accel_directives).file_cache_only) {
620620
zend_throw_error(NULL,
621621
"Class %s is uncacheable",
622622
ZSTR_VAL(ce->name));
@@ -1313,7 +1313,7 @@ ZEND_API void zend_require_user_module(zend_string *module_path)
13131313

13141314
ZUM_DEBUG("require module: %s\n", ZSTR_VAL(module_path));
13151315

1316-
if (!ZCG(accelerator_enabled)) {
1316+
if (!ZCG(accelerator_enabled) && !ZCG(accel_directives).file_cache) {
13171317
zend_throw_error(NULL, "require_modules() not supported when opcache is not enabled, yet");
13181318
return;
13191319
}
@@ -1330,21 +1330,23 @@ ZEND_API void zend_require_user_module(zend_string *module_path)
13301330
zend_stream_init_filename_ex(&file_handle, full_path);
13311331
zend_string_release(full_path);
13321332

1333-
zend_string *module_key = zend_string_concat2(
1334-
"module://", strlen("module://"),
1335-
ZSTR_VAL(file_handle.filename), ZSTR_LEN(file_handle.filename));
1336-
zend_persistent_user_module *cached_module = zend_accel_hash_find(&ZCSG(hash), module_key);
1337-
zend_string_release(module_key);
1338-
if (cached_module) {
1339-
zend_user_module *loaded_module = zend_hash_find_ptr(EG(module_table), cached_module->module.desc.lcname);
1340-
if (loaded_module) {
1341-
zum_handle_loaded_module(loaded_module, file_handle.filename);
1342-
zend_destroy_file_handle(&file_handle);
1343-
return;
1344-
}
1345-
if (zum_load(&file_handle, cached_module) == SUCCESS) {
1346-
zend_destroy_file_handle(&file_handle);
1347-
return;
1333+
if (ZCG(accelerator_enabled)) {
1334+
zend_string *module_key = zend_string_concat2(
1335+
"module://", strlen("module://"),
1336+
ZSTR_VAL(file_handle.filename), ZSTR_LEN(file_handle.filename));
1337+
zend_persistent_user_module *cached_module = zend_accel_hash_find(&ZCSG(hash), module_key);
1338+
zend_string_release(module_key);
1339+
if (cached_module) {
1340+
zend_user_module *loaded_module = zend_hash_find_ptr(EG(module_table), cached_module->module.desc.lcname);
1341+
if (loaded_module) {
1342+
zum_handle_loaded_module(loaded_module, file_handle.filename);
1343+
zend_destroy_file_handle(&file_handle);
1344+
return;
1345+
}
1346+
if (zum_load(&file_handle, cached_module) == SUCCESS) {
1347+
zend_destroy_file_handle(&file_handle);
1348+
return;
1349+
}
13481350
}
13491351
}
13501352

@@ -1600,6 +1602,11 @@ static void zum_persist_modules(uint32_t module_table_offset)
16001602
{
16011603
ZEND_ASSERT(!CG(active_module));
16021604

1605+
if (ZCG(accel_directives).file_cache_only) {
1606+
// TODO: leaks
1607+
return;
1608+
}
1609+
16031610
zend_shared_alloc_init_xlat_table();
16041611

16051612
/* Replace zend_user_modules by zend_persistent_user_modules

ext/opcache/zend_file_cache.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ static void zend_file_cache_serialize_op_array(zend_op_array *op_arra
484484
SERIALIZE_PTR(op_array->arg_info);
485485
SERIALIZE_PTR(op_array->vars);
486486
SERIALIZE_STR(op_array->function_name);
487+
SERIALIZE_STR(op_array->user_module);
487488
SERIALIZE_STR(op_array->filename);
488489
SERIALIZE_PTR(op_array->live_range);
489490
SERIALIZE_PTR(op_array->scope);
@@ -636,6 +637,7 @@ static void zend_file_cache_serialize_op_array(zend_op_array *op_arra
636637
}
637638

638639
SERIALIZE_STR(op_array->function_name);
640+
SERIALIZE_STR(op_array->user_module);
639641
SERIALIZE_STR(op_array->filename);
640642
SERIALIZE_PTR(op_array->live_range);
641643
SERIALIZE_PTR(op_array->scope);
@@ -772,6 +774,7 @@ static void zend_file_cache_serialize_class(zval *zv,
772774
}
773775
}
774776
zend_file_cache_serialize_hash(&ce->constants_table, script, info, buf, zend_file_cache_serialize_class_constant);
777+
SERIALIZE_STR(ce->info.user.module);
775778
SERIALIZE_STR(ce->info.user.filename);
776779
SERIALIZE_STR(ce->doc_comment);
777780
SERIALIZE_ATTRIBUTES(ce->attributes);
@@ -1373,6 +1376,7 @@ static void zend_file_cache_unserialize_op_array(zend_op_array *op_arr
13731376
UNSERIALIZE_PTR(op_array->arg_info);
13741377
UNSERIALIZE_PTR(op_array->vars);
13751378
UNSERIALIZE_STR(op_array->function_name);
1379+
UNSERIALIZE_STR(op_array->user_module);
13761380
UNSERIALIZE_STR(op_array->filename);
13771381
UNSERIALIZE_PTR(op_array->live_range);
13781382
UNSERIALIZE_PTR(op_array->scope);
@@ -1509,6 +1513,7 @@ static void zend_file_cache_unserialize_op_array(zend_op_array *op_arr
15091513
}
15101514

15111515
UNSERIALIZE_STR(op_array->function_name);
1516+
UNSERIALIZE_STR(op_array->user_module);
15121517
UNSERIALIZE_STR(op_array->filename);
15131518
UNSERIALIZE_PTR(op_array->live_range);
15141519
UNSERIALIZE_STR(op_array->doc_comment);
@@ -1637,6 +1642,7 @@ static void zend_file_cache_unserialize_class(zval *zv,
16371642
}
16381643
zend_file_cache_unserialize_hash(&ce->constants_table,
16391644
script, buf, zend_file_cache_unserialize_class_constant, NULL);
1645+
UNSERIALIZE_STR(ce->info.user.module);
16401646
UNSERIALIZE_STR(ce->info.user.filename);
16411647
UNSERIALIZE_STR(ce->doc_comment);
16421648
UNSERIALIZE_ATTRIBUTES(ce->attributes);

0 commit comments

Comments
 (0)