Skip to content

Add opcache_preloading() internal function #19288

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ext/opcache/ZendAccelerator.c
Original file line number Diff line number Diff line change
Expand Up @@ -4859,6 +4859,8 @@ static zend_result accel_finish_startup_preload(bool in_child)
bool old_reset_signals = SIGG(reset);
#endif

ZCG(preloading) = true;

sapi_module.activate = NULL;
sapi_module.deactivate = NULL;
sapi_module.register_server_variables = NULL;
Expand Down Expand Up @@ -4940,6 +4942,8 @@ static zend_result accel_finish_startup_preload(bool in_child)
sapi_module.ub_write = orig_ub_write;
sapi_module.flush = orig_flush;

ZCG(preloading) = false;

sapi_activate();

return ret;
Expand Down
1 change: 1 addition & 0 deletions ext/opcache/ZendAccelerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ typedef struct _zend_accel_globals {
#endif
void *preloaded_internal_run_time_cache;
size_t preloaded_internal_run_time_cache_size;
bool preloading;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know much about packing data structure declarations to reduce space, would it be better to put this next to the other bool fields above?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hesitated, as I was weighting the benefits of packing vs placing the fields near related fields. I chose the latter because there is only one "instance" of this struct at a time, so this should not make a huge difference.

/* preallocated shared-memory block to save current script */
void *mem;
zend_persistent_script *current_persistent_script;
Expand Down
3 changes: 3 additions & 0 deletions ext/opcache/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ PHP_NEW_EXTENSION([opcache], m4_normalize([
shared_alloc_mmap.c
shared_alloc_posix.c
shared_alloc_shm.c
zend_accelerator_api.c
zend_accelerator_blacklist.c
zend_accelerator_debug.c
zend_accelerator_hash.c
Expand Down Expand Up @@ -362,3 +363,5 @@ AS_VAR_IF([PHP_OPCACHE_JIT], [yes], [
PHP_ADD_MAKEFILE_FRAGMENT([$ext_srcdir/jit/Makefile.frag])
])
PHP_SUBST([OPCACHE_SHARED_LIBADD])

PHP_INSTALL_HEADERS([ext/opcache], [zend_accelerator_api.h])
3 changes: 3 additions & 0 deletions ext/opcache/config.w32
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ PHP_OPCACHE="yes";

ZEND_EXTENSION('opcache', "\
ZendAccelerator.c \
zend_accelerator_api.c \
zend_accelerator_blacklist.c \
zend_accelerator_debug.c \
zend_accelerator_hash.c \
Expand Down Expand Up @@ -68,3 +69,5 @@ if (PHP_OPCACHE_JIT == "yes") {
}

ADD_FLAG('CFLAGS_OPCACHE', "/I " + configure_module_dirname);

PHP_INSTALL_HEADERS("ext/opcache", "zend_accelerator_api.h");
3 changes: 3 additions & 0 deletions ext/opcache/tests/api/opcache_preloading.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
printf("%s: %d\n", __FILE__, zend_test_opcache_preloading());
?>
19 changes: 19 additions & 0 deletions ext/opcache/tests/api/opcache_preloading_001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
opcache_preloading() api 001
--INI--
opcache.enable=1
opcache.enable_cli=1
opcache.preload={PWD}/opcache_preloading.inc
--SKIPIF--
<?php
if (PHP_OS_FAMILY == 'Windows') die('skip Preloading is not supported on Windows');
?>
--FILE--
<?php

printf("%s: %d\n", __FILE__, zend_test_opcache_preloading());

?>
--EXPECTF--
%sopcache_preloading.inc: 1
%sopcache_preloading_001.php: 0
23 changes: 23 additions & 0 deletions ext/opcache/tests/api/opcache_preloading_002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--TEST--
opcache_preloading() api 002
--INI--
opcache.enable=1
opcache.enable_cli=1
opcache.preload={PWD}/opcache_preloading.inc
opcache.preload_user={ENV:TEST_NON_ROOT_USER}
--EXTENSIONS--
posix
--SKIPIF--
<?php
if (PHP_OS_FAMILY == 'Windows') die('skip Preloading is not supported on Windows');
if (posix_geteuid() !== 0) die('skip Test needs root user');
?>
--FILE--
<?php

printf("%s: %d\n", __FILE__, zend_test_opcache_preloading());

?>
--EXPECTF--
%sopcache_preloading.inc: 1
%sopcache_preloading_002.php: 0
23 changes: 23 additions & 0 deletions ext/opcache/zend_accelerator_api.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
+----------------------------------------------------------------------+
| Zend OPcache |
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/

#include "zend_accelerator_api.h"
#include "ZendAccelerator.h"

ZEND_API bool opcache_preloading(void)
{
return ZCG(preloading);
}
29 changes: 29 additions & 0 deletions ext/opcache/zend_accelerator_api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
+----------------------------------------------------------------------+
| Zend OPcache |
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/

#ifndef ZEND_ACCELERATOR_API_H
#define ZEND_ACCELERATOR_API_H

#include "Zend/zend_portability.h"

BEGIN_EXTERN_C()

/* Returns true during preloading */
ZEND_API bool opcache_preloading(void);

END_EXTERN_C()

#endif /* ZEND_ACCELERATOR_API_H */
9 changes: 9 additions & 0 deletions ext/zend_test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
+----------------------------------------------------------------------+
*/

#include "ext/opcache/zend_accelerator_api.h"
#include "zend_API.h"
#include "zend_modules.h"
#include "zend_types.h"
#ifdef HAVE_CONFIG_H
Expand Down Expand Up @@ -1623,3 +1625,10 @@ static PHP_FUNCTION(zend_test_gh18756)
zend_mm_gc(heap);
zend_mm_shutdown(heap, true, false);
}

static PHP_FUNCTION(zend_test_opcache_preloading)
{
ZEND_PARSE_PARAMETERS_NONE();

RETURN_BOOL(opcache_preloading());
}
2 changes: 2 additions & 0 deletions ext/zend_test/test.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ function zend_test_log_err_debug(string $str): void {}
function zend_test_compile_to_ast(string $str): string {}

function zend_test_gh18756(): void {}

function zend_test_opcache_preloading(): bool {}
}

namespace ZendTestNS {
Expand Down
6 changes: 5 additions & 1 deletion ext/zend_test/test_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.