Home  >  Article  >  Backend Development  >  PHP extension - life cycle and memory management

PHP extension - life cycle and memory management

高洛峰
高洛峰Original
2016-11-17 14:06:281066browse

1. PHP source code structure

PHP has two kernel subsystems, ZE (Zend Engine) and PHP Core.
ZE is responsible for parsing PHP scripts into machine codes (also known as tokens) and executing these machines in the process space Code; ZE is also responsible for memory management, variable scope management and scheduling management of PHP functions.

PHP Core is responsible for communicating with the SAPI layer; PHP Core also provides a unified control layer for safe_mode and open_basedir checks; PHP Core also provides a streams layer for file and network IO operations in the user domain. Among them, SAPI (Server Application Programming Interface) usually includes Nginx, Apache, IIS, CLI, CGI and other host environments.

PHP extension provides encapsulation of various common operations based on ZE and PHP Core, such as reading and writing mysql, redis, memcache, sqlite, etc., parsing json, xml files, soap, sokcet, curl Encapsulation of network protocols, encapsulation of encryption, decryption, compression and decompression, etc., encapsulation of image processing, etc. Some extensions implement a certain function from scratch, such as using C to communicate with redis according to the redis communication protocol; some extensions call existing libraries in the system, such as the gb extension for image processing, which needs to be installed on the system itself. The corresponding gd library.
78 extensions are provided in the PHP source code php-5.6.24/ext.

In short, ZE and PHP Core provide the basic architecture, and EXT (extension) provides various operations in the user domain.
Take the php-5.6.24 source code as an example. ZE corresponds to the folder php-5.6.24/Zend, PHP Core corresponds to the folder php-5.6.24/main, and extension corresponds to the folder php-5.6.24/ext.

2. Life cycle of PHP extension

When PHP receives the SAPI command, it first initializes and starts its kernel subsystem. At the end of the startup of the kernel subsystem, PHP begins to load its extension code and initialize the extension. , at this time PHP will call the initialization routine of each module Module Initialization routine (MINIT).

MINIT (Module Initialization)
PHP calls MINIT related routines, giving each extension the opportunity to initialize internal variables, allocate resources, register resource processing handles, and register its own functions with ZE, so that when the script calls these functions ZE knows which code to execute

RINIT (Request Initialization)
After the module initialization is completed, PHP waits for the request from SAPI. When the SAPI request is received, ZE creates a running environment for the currently requested php script and calls each The extended Request Initialization (RINIT) function gives each extension the opportunity to set specific environment variables, allocate resources based on requests, or perform other tasks such as auditing.

The SAPI requests mentioned here are divided into two categories. One is Apache, IIS, and other mature web server SAPIs. When they start, PHP first executes MINIT, and then waits for page requests from users. When the request is received Then execute RINIT; another type of SAPI request is CGI or CLI SAPIs. When PHP receives this type of SAPI request, it executes RINIT immediately after executing MINIT.

After the RINIT request is initialized, ZE takes back control and translates the currently requested script into tokens, which ultimately form opcodes (operation codes). During the execution of opcodes, if an opcode requires the execution of an extension function, This is when ZE will bind the relevant parameters to the modified function and temporarily hand over the control to the function for execution until the function is executed.

RSHUTDOWN (Request Shutdown)
After the PHP script runs, PHP calls the request shutdown (RSHUTDOWN) function of each extension to perform final cleanup work (such as saving session variables to disk). Next, ZE performs a cleanup process (garbage collection), effectively unset()ing every variable used during the previous request.

MSHUTDOWN (Module Shutdown)
When RSHUTDOWN is completed, PHP continues to wait for other document requests from SAPI or a shutdown signal. For SAPIs like CGI and CLI, there is no "next request", so the SAPI starts shutting down immediately. During shutdown, PHP again iterates through each extension, calls its module shutdown (MSHUTDOWN) function, and eventually shuts down its own kernel subsystem.

GINIT
Initialize global variables

GSHUTDOWN
Release global variables

MINFO
Set the information of the phpinfo module, phpinfo needs to level the configuration information of each extension

// main/php.h
 #define PHP_MINIT       ZEND_MODULE_STARTUP_N
 #define PHP_MSHUTDOWN   ZEND_MODULE_SHUTDOWN_N
 #define PHP_RINIT       ZEND_MODULE_ACTIVATE_N
 #define PHP_RSHUTDOWN   ZEND_MODULE_DEACTIVATE_N
 #define PHP_MINFO       ZEND_MODULE_INFO_N
 #define PHP_GINIT       ZEND_GINIT
 #define PHP_GSHUTDOWN   ZEND_GSHUTDOWN

 #define PHP_MINIT_FUNCTION      ZEND_MODULE_STARTUP_D
 #define PHP_MSHUTDOWN_FUNCTION  ZEND_MODULE_SHUTDOWN_D
 #define PHP_RINIT_FUNCTION      ZEND_MODULE_ACTIVATE_D
 #define PHP_RSHUTDOWN_FUNCTION  ZEND_MODULE_DEACTIVATE_D
 #define PHP_MINFO_FUNCTION      ZEND_MODULE_INFO_D
 #define PHP_GINIT_FUNCTION      ZEND_GINIT_FUNCTION
 #define PHP_GSHUTDOWN_FUNCTION  ZEND_GSHUTDOWN_FUNCTION

3. Memory management of PHP extensions

ZE during execution During its own internal memory management, additional flags are used to identify whether a certain memory variable is persistent. For non-persistent memory, ZE will clean it up. However, it is best to clean up the non-persistent memory yourself within the extension, because the non-persistent memory requested by the extension will remain unrecycled for a long time, so the resources related to it will not be released for a long time.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn