Skip to content

Commit b7f3eea

Browse files
committed
Allow to trigger profiling at request startup
1 parent 75de73f commit b7f3eea

File tree

1 file changed

+81
-1
lines changed

1 file changed

+81
-1
lines changed

memprof.c

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,71 @@ static void memprof_disable()
765765
allocs_set = (Pvoid_t) NULL;
766766
}
767767

768+
static void disable_opcache()
769+
{
770+
zend_string *key = zend_string_init(ZEND_STRL("opcache.enable"), 0);
771+
zend_alter_ini_entry_chars_ex(
772+
key,
773+
"0",
774+
1,
775+
ZEND_INI_USER,
776+
ZEND_INI_STAGE_ACTIVATE,
777+
0
778+
);
779+
zend_string_release(key);
780+
}
781+
782+
static zend_string* read_env_get_post(char *name, size_t len)
783+
{
784+
zval *value;
785+
786+
char *env = sapi_getenv(name, len);
787+
if (env != NULL) {
788+
zend_string *str = zend_string_init(env, strlen(env), 0);
789+
efree(env);
790+
return str;
791+
}
792+
793+
env = getenv(name);
794+
if (env != NULL) {
795+
return zend_string_init(env, strlen(env), 0);
796+
}
797+
798+
if (Z_ARR(PG(http_globals)[TRACK_VARS_GET]) != NULL) {
799+
value = zend_hash_str_find(Z_ARR(PG(http_globals)[TRACK_VARS_GET]), name, len);
800+
if (value != NULL) {
801+
convert_to_string_ex(value);
802+
zend_string_addref(Z_STR_P(value));
803+
return Z_STR_P(value);
804+
}
805+
}
806+
807+
if (Z_ARR(PG(http_globals)[TRACK_VARS_POST]) != NULL) {
808+
value = zend_hash_str_find(Z_ARR(PG(http_globals)[TRACK_VARS_POST]), name, len);
809+
if (value != NULL) {
810+
convert_to_string_ex(value);
811+
zend_string_addref(Z_STR_P(value));
812+
return Z_STR_P(value);
813+
}
814+
}
815+
816+
return NULL;
817+
}
818+
819+
static int should_autostart()
820+
{
821+
zend_string *value = read_env_get_post(MEMPROF_ENV_PROFILE, strlen(MEMPROF_ENV_PROFILE));
822+
if (value == NULL) {
823+
return 0;
824+
}
825+
826+
int autostart = ZSTR_LEN(value) > 0;
827+
828+
zend_string_release(value);
829+
830+
return autostart;
831+
}
832+
768833
ZEND_DLEXPORT int memprof_zend_startup(zend_extension *extension)
769834
{
770835
return zend_startup_module(&memprof_module_entry);
@@ -842,7 +907,7 @@ zend_module_entry memprof_module_entry = {
842907
memprof_functions,
843908
PHP_MINIT(memprof),
844909
PHP_MSHUTDOWN(memprof),
845-
NULL,
910+
PHP_RINIT(memprof),
846911
PHP_RSHUTDOWN(memprof),
847912
PHP_MINFO(memprof),
848913
#if ZEND_MODULE_API_NO >= 20010901
@@ -901,6 +966,19 @@ PHP_MSHUTDOWN_FUNCTION(memprof)
901966
}
902967
/* }}} */
903968

969+
/* {{{ PHP_RINIT_FUNCTION
970+
*/
971+
PHP_RINIT_FUNCTION(memprof)
972+
{
973+
if (should_autostart()) {
974+
disable_opcache();
975+
memprof_enable();
976+
}
977+
978+
return SUCCESS;
979+
}
980+
/* }}} */
981+
904982
/* {{{ PHP_RSHUTDOWN_FUNCTION
905983
*/
906984
PHP_RSHUTDOWN_FUNCTION(memprof)
@@ -1319,6 +1397,8 @@ PHP_FUNCTION(memprof_enable)
13191397
return;
13201398
}
13211399

1400+
zend_error(E_WARNING, "Calling memprof_enable() manually may not work as expected because of PHP optimizations. Prefer using MEMPROF_PROFILE=1 as environment variable, GET, or POST");
1401+
13221402
memprof_enable();
13231403

13241404
RETURN_TRUE;

0 commit comments

Comments
 (0)