Introduction to a gadget PHP-Valgrind

Source: Internet
Author: User
I always thought that people would always write blogs out of time, but now it seems that I am wrong. the blog is not updated for a long time. it does not mean that there is no content to share. however, this year has indeed been a lot of busy, and there have been a lot of work tasks. in my spare time, it has also been filled up by PHP projects and Zend tasks. in addition, some small sentiments are all complained on Weibo... So ....

First, I will not update my blog for too long.

I always thought that people would always write blogs out of time, but now it seems that I am wrong. the blog is not updated for a long time. it does not mean that there is no content to share. however, this year has indeed been a lot of busy, and there have been a lot of work tasks. in my spare time, it has also been filled up by PHP projects and Zend tasks. in addition, some small sentiments are all complained on Weibo... So ....

Anyway, I am very grateful to all of you who often come to my blog. However, I suggest you do not leave a message if you ask a question. sometimes the blog may be used as a SPAM, but there are some minor problems, you can go to Weibo At me @ laruence

Let's get down to the point. today we share a little tool we just made the day before yesterday. The code can be found on my github: php-valgrind. this tool provides the Profile capability for enabling Valgrind (Callgrind strictly) in PHP scripts.

In general, when we use Callgrind, if we want to analyze a function, we can use toggle-collect = "function name" to tell Callgrind to start Profile when entering this function. but there is no way to analyze a specific piece of code ..

In fact, Callgrind provides a mechanism that allows us to control when to enable in code: CALLGRIND_TOGGLE_COLLECT

For example:

 
 
  1. #include
  2. #include
  3.  
  4. void foo(int a[100]) {
  5.     int i = 0;
  6. }
  7.  
  8. int main (int argc, char **argv) {
  9.     int i, a[100];
  10.  
  11.     CALLGRIND_START_INSTRUMENTATION;
  12.     CALLGRIND_TOGGLE_COLLECT;
  13.     for (i=0; i<100; i++) {
  14.         a[i] = 2;
  15.     }
  16.     CALLGRIND_TOGGLE_COLLECT;
  17.     CALLGRIND_STOP_INSTRUMENTATION;
  18.     return;
  19. }

Then, after compilation into a. out, we can analyze the specific Profile information of this loop code disconnection:

 
 
  1. $ valgrind --tool=callgrind --collect-atstart=no --instr-atstart=no ./a.ou

The generated callgrind. out can be analyzed by tools such as callgrind_annotate and kcachegrind.

 
 
  1. $callgrind_annotate callgrind.out.27538
  2. //OUTPUT:
  3. --------------------------------------------------------------------------------
  4.  Ir
  5. ---------------------------------------
  6. 617 PROGRAM TOTALS
  7.  
  8. ---------------------------------------
  9.  Ir file:function
  10. ---------------------------------------
  11. 617 test.c:main [***dev/a.out

Is it convenient?

However, sometimes, for example, we do expansion or other internal performance analysis. this can also be triggered in PHP scripts. there is no way. so I wrote this little tool php-valgrind. after installing this extension, let's look at an example:

 
 
  1. $a = array();
  2. callgrind_toggle();
  3. for ($i=0;$i<1000;$i++) {
  4.     $a[$i] = 2;
  5. }
  6. callgrind_toggle();

Then we start to analyze:

 
 
  1. $valgrind --tool=callgrind --collect-atstart=no --instr-atstart=no php /tmp/1.ph

Then we analyze the output:

 
 
  1. -------------------------
  2. Ir
  3. --------------------------
  4. 2,361,260 PROGRAM TOTALS
  5.  
  6. // Save

Then let's use qcachegrind (callgrind analysis tool with gui) to look at it:

It can be seen that PHP requires N times more code than C to implement the same function (so of course it is slower than C .. hey, I declare again: "C language is the best language, not one! ")

After the introduction of the tool is complete, you can play with it if you are interested. this tool can also be used to let us know that one of our PHP code will trigger calls to the underlying functions, or system calls, etc. Enjoy ~


 

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.