Write a PHP high-load troubleshooting experience

Source: Internet
Author: User
In the last project, the background ran deamon to handle the task. There have been no problems before, in an increase in task processing time, such as monitoring escalation, deamon machines appear high load, average over 20, the task backlog.

The first step is to use Strace to see the system call situation:

Strace-tt-t-c-p 31420

Strace-c found that the system time is mainly spent on the rt_sigprocmask, the function is the signal shielding function of the system, generally used in the signal processing program, the signal is shielded to ensure that the signal processing program is not interrupted. Rt_sigprocmask is a system calling function for Linux, not called directly, but using GLIBC for its encapsulation: Sigprocmask.

Further starce invoke the actual situation:

The Rt_singprocmask call has a feature: after the mask word is set, it can be recalled that it might be called in the signal processing function of PHP.

In the PHP source code search Sigprocmask, found that the PHP source calls Singprocmask in the pnctl extension, the Deamon program used PNCTL extension to create and manage child processes. The signal processing function of PHP is located in Pcntl_signal_dispatch () by first blocking the signal and then recovering the characteristic of the signal.

We then verify our idea by adding a log to confirm that the function is actually called frequently. This leads to the analysis of the PHP signal processing mechanism.

Confined to space, the specific analysis process omitted, here gives the results of the analysis:

    1. The tick mechanism of PHP:

Phpmanul's explanation of the Tick is:

A Tick is an event, occurs for every N low-level tickable statements executed by the parser within the declare block. The value for N is specified using Ticks=n within the Declare block ' s Directive section.

Not all statements is tickable. Typically, condition expressions and argument expressions is not tickable.

The event (s) that occur on each tick is specified using the Register_tick_function (). See the example below for more details. Note that the more than one event can occur for each tick.

It probably means that the tick and register_tick_function mates, each executing the n tick=n of the underlying instruction, triggers the execution of Register_tick_function registered events.

2. PHP Processing of the signal:

PHP's management control of processes and signals is implemented through the PCNTL extension, PCNTL uses the tick mechanism as the callback mechanism of signal, and you can allow callback to be used in PHP where declare is allowed to occur.

Through the analysis of Pcntl source code, PCNTL's signal processing mechanism is this:

1), Signal registration

2), Signal processing:

Analysis here, you can see: in PHP, according to the tick value of declare declared periodically, when the tick value is configured low (Ticks=1 defined in this program), will trigger a large number of signal handlers, the program calls Rt_sigprocmask cause a lot of wasted CPU, The machine load becomes higher.

So why should PHP use this signal processing mechanism?

Look at PHP manual when you find such an introduction:

Pcntl now uses ticks as the signal handle callback mechanism, which is much faster than the previous mechanism. This change follows the same semantics as using "user ticks". The declare () statement to specify the locations in your program where callbacks is allowed to occur. This allows minimize the overhead of handling asynchronous events. In the past, compiling PHP with PCNTL enabled would all incur this overhead, whether or not your script actually used P Cntl.

Focus on: The DECLARE () statement to specify the locations in your program where callbacks is allowed to occur. This means that you can specify the area of code where you want the signal to be captured, without signal processing outside the area.

From the above can be summed up the way to modify the problem:

1), using declare to develop the area where the application captures the signal,

2), for a program that does not require signal processing, does not declare declear (TICKS=N), thus does not trigger the call signal handler, the process needs to be processed manually call signal handler Pcntl_signal_dispatch instead of PHP timing detection.

It is proven that using either of these methods can solve the problem. The machine load is reduced to normal.

Summarize:

1), PHP uses a similar Linux kernel interrupt processing mechanism (hard interrupt + soft terminal) mechanism to process the signal, need to pay attention to the frequency of detection signal, to avoid CPU performance waste.

2), because the PCNTL extension management creation process is not recommended in CGI, and this mechanism does not affect the CGI of webserver

  • 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.