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:
- #include
- #include
-
- void foo(int a[100]) {
- int i = 0;
- }
-
- int main (int argc, char **argv) {
- int i, a[100];
-
- CALLGRIND_START_INSTRUMENTATION;
- CALLGRIND_TOGGLE_COLLECT;
- for (i=0; i<100; i++) {
- a[i] = 2;
- }
- CALLGRIND_TOGGLE_COLLECT;
- CALLGRIND_STOP_INSTRUMENTATION;
- return;
- }
Then, after compilation into a. out, we can analyze the specific Profile information of this loop code disconnection:
- $ 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.
- $callgrind_annotate callgrind.out.27538
- //OUTPUT:
- --------------------------------------------------------------------------------
- Ir
- ---------------------------------------
- 617 PROGRAM TOTALS
-
- ---------------------------------------
- Ir file:function
- ---------------------------------------
- 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:
- $a = array();
- callgrind_toggle();
- for ($i=0;$i<1000;$i++) {
- $a[$i] = 2;
- }
- callgrind_toggle();
Then we start to analyze:
- $valgrind --tool=callgrind --collect-atstart=no --instr-atstart=no php /tmp/1.ph
Then we analyze the output:
- -------------------------
- Ir
- --------------------------
- 2,361,260 PROGRAM TOTALS
-
- // 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 ~