PHP ticks mechanism

Source: Internet
Author: User
Tags php sample code
According to this year's plan, there will be at least two articles per month, and there will be only one 2012 Summary for various reasons in August. for whatever reason, it is always wrong. This is a supplement, and it is also the beginning of this year.

The Chinese New Year is coming. I completed this article a year ago. if you have any chance to see it, I wish you a happy new year. in the new year, everything goes well!

According to this year's plan, there will be at least two articles per month, and there will be only one 2012 Summary for various reasons in August. for whatever reason, it is always wrong. This is a supplement, and it is also the beginning of this year.

Back to the question, what we want to study today is the ticks mechanism of PHP.

PHP provides the declare keyword and ticks keyword to declare the ticks mechanism. For example, declare (ticks = N); this indicates that the current service statement is interrupted every time N internal statements (opcodes) statements are executed in the current scope, execute the function registered through register_tick_function (if any), and then continue the previous code. Note that N here refers to some php opcode, but OPCODE does not correspond to the PHP statements we have seen one by one.

At first, I thought that the PHP kernel was recording whether there was a ticks mechanism during compilation. when the intermediate code was actually executed, I inserted the judgment code to implement this mechanism. But this is not the case.

See PHP sample code 1:

    $name = "phppan"; echo $name; class Tipi { public function test() { echo "test"; } } function f_tipi() { }

The above code includes several common statements, such as assignment, output, definition class, and function definition. We usually use VLD to view the intermediate code generated by PHP. the above code uses php-dvld. active = 1 t. php and we will see ECHO, ASSIGN, NOP, and other intermediate code.

Now we add the ticks mechanism to the code in example 1. For example, PHP code Example 2:

    declare(ticks=1); $name = "phppan"; echo $name; class Tipi { public function test() { echo "test"; } } function f_tipi() { }

Compared with example 1, example 2 has the first statement declare (ticks = 1). if we view the intermediate code through VLD again, you will find that there is an additional intermediate code behind each intermediate code: TICKS.

Is ticks added to the end of each intermediate code for TICKS = 1? Replace declare (ticks = 1); with declare (ticks = 100);, VLD again, and the result remains unchanged. From the above results, we can see that the PHP kernel implements the ticks mechanism in the syntax analysis process.

From the implementation process, the ticks mechanism is defined into two processes: one is to define whether the ticks mechanism needs to be executed or whether the ticks mechanism is declared, and the other is to control the statement execution when the ticks mechanism is declared.

Process of declaring the ticks mechanism

The declaration process is to call declare (ticks = N). during syntax analysis, the ticks mechanism is declared based on the declare keyword and the ticks keyword in the parameter. Use the zend_do_declare_begin, declare_statement, and zend_do_declare_end functions in the zend_compile.c file to compile and declare the ticks mechanism. In the declare_statement function, we can see that in addition to declaring ticks, declare can also declare encoding, which is an if else judgment in the code.

The declaration of the ticks mechanism is only useful in the compilation process. it serves the subsequent declaration control statements. The global variable in the compilation process is CG (declarables ). This is a struct with only one member: ticks. Of course there should be more members later.

Declarative control statement

Examples 1 and 2 fully demonstrate that the ticks intermediate code is added based on whether the TICKS mechanism is declared during syntax analysis of each statement, in fact, a function call zend_do_ticks is added to each statement during syntax parsing. The zend_mongoage_parser.y file shows that the zend_do_ticks function is added after the class definition statements, function definition statements, and general statements. The zend_do_ticks function in the zend_compile.c file determines whether to generate the ZEND_TICKS intermediate code based on the CG (declarables). ticks mentioned above (the intermediate code displayed in the VLD does not start with ZEND ).

In addition to declaring the ticks mechanism, there are also executions. The key variable during execution is ticks = N at the time of declaration. In fact, here N can be understood from another angle: the number specified by ticks refers to the number of TICKS statements executed. In the execution function ZEND_TICKS_SPEC_CONST_HANDLER of the TICKS intermediate code, the number of times the current function is executed is counted, and the storage variable is EG (ticks_count ). When the defined boundary is reached, all functions registered through register_tick_function will be called and the count is cleared.

Compared with the original implementation, the PHP kernel's implementation of the ticks mechanism satisfies the single functional principle and the loose coupling principle. Add the ticks mechanism as an intermediate code to the execution system of the entire intermediate code, including state transfer and function switching.

Application scenarios of the ticks mechanism

In the manual, Ticks is suitable for debugging, simple multitasking, background I/O, and many other tasks.

In the debugging process, it is useful to locate slow statements in a specific code. we can record the time every two low-level statements are executed. Although this process can be completed in other ways, it is easier to use tick.

PCNTL also uses the ticks mechanism as the signal processing mechanism (signal handle callback mechanism) to minimize the load for processing asynchronous events. The key here is the PCNTL extended module initialization function (PHP_MINIT_FUNCTION (pcntl )). During module initialization, the module calls php_add_tick_function (pcntl_signal_dispatch) and adds the pcntl distribution execution function to the call function of the ticks mechanism, when ticks is triggered, all methods specified in the PCNTL extension function are called.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.