@@ -765,6 +765,71 @@ static void memprof_disable()
765
765
allocs_set = (Pvoid_t ) NULL ;
766
766
}
767
767
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
+
768
833
ZEND_DLEXPORT int memprof_zend_startup (zend_extension * extension )
769
834
{
770
835
return zend_startup_module (& memprof_module_entry );
@@ -842,7 +907,7 @@ zend_module_entry memprof_module_entry = {
842
907
memprof_functions ,
843
908
PHP_MINIT (memprof ),
844
909
PHP_MSHUTDOWN (memprof ),
845
- NULL ,
910
+ PHP_RINIT ( memprof ) ,
846
911
PHP_RSHUTDOWN (memprof ),
847
912
PHP_MINFO (memprof ),
848
913
#if ZEND_MODULE_API_NO >= 20010901
@@ -901,6 +966,19 @@ PHP_MSHUTDOWN_FUNCTION(memprof)
901
966
}
902
967
/* }}} */
903
968
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
+
904
982
/* {{{ PHP_RSHUTDOWN_FUNCTION
905
983
*/
906
984
PHP_RSHUTDOWN_FUNCTION (memprof )
@@ -1319,6 +1397,8 @@ PHP_FUNCTION(memprof_enable)
1319
1397
return ;
1320
1398
}
1321
1399
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
+
1322
1402
memprof_enable ();
1323
1403
1324
1404
RETURN_TRUE ;
0 commit comments