Press "Enter" to skip to content

一个小玩意PHP-Valgrind的介绍

首先, 太久不更新博客了, 容我先啰嗦几句.
我一直以为人总是会挤出时间写博客的, 但现在看来我错了. 博客很久不更新, 倒不是说没有内容可以分享. 而是这一年来确实忙了很多, 本身工作上的事情就很多, 业余时间也被PHP项目上的事情, Zend的事情填满. 再加上一些小感悟也都在微博上牢骚了... 所以....
Anyway, 很感谢各位经常来我博客的朋友, 不过我建议大家如果是提问的话, 不要在留言里, 有的时候会被博客当做SPAM, 有一些小问题, 可以来微博At我 @laruence
言归正传, 今天分享个前天刚刚做的小工具, 代码可以在我的github上找到: php-valgrind. 这个工具主要是为PHP脚本提供了可以在脚本中开启Valgrind(严格说是Callgrind)的Profile能力.
一般来说, 我们用Callgrind的时候, 如果要分析某一个函数, 可以通过toggle-collect="函数名", 来告诉Callgrind在进入这个函数的时候开始Profile. 但是这样没有办法分析具体某一段代码..
其实Callgrind提供了一个机制, 可以让我们在代码中控制何时开启: CALLGRIND_TOGGLE_COLLECT
比如:

#include <stdio.h>
#include <valgrind/callgrind.h>
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;
}

然后, 编译成a.out以后, 我们就可以分析具体的这个循环代码断的相关Profile信息:

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

之后生成的callgrind.out就可以被callgrind_annotate, kcachegrind等工具来分析了

$callgrind_annotate callgrind.out.27538
//OUTPUT:
--------------------------------------------------------------------------------
 Ir
---------------------------------------
617  PROGRAM TOTALS
---------------------------------------
 Ir  file:function
---------------------------------------
617  test.c:main [***dev/a.out]

是不是很方便呢?
但是呢, 有的时候, 比如我们做扩展, 或者其他的一类内部的性能分析的时候. 需要在PHP脚本也能做这样的触发. 就没有办法了. 于是我就写了这个小工具php-valgrind, 装好这个扩展以后, 来看个例子:

<?php
$a = array();
callgrind_toggle();
for ($i=0;$i<1000;$i++) {
    $a[$i] = 2;
}
callgrind_toggle();

然后我们开始分析:

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

然后我们分析下输出:

-------------------------
       Ir
--------------------------
2,361,260  PROGRAM TOTALS
//以下省略

然后让我们用qcachegrind(带gui的callgrind分析工具), 来看看:
callgrind.output
可见, PHP要实现同样的功能需要的各种代码数相比C语言来说, 那可是多了N倍的(所以当然要比C慢了.. 嘿嘿, 再次申明: "C语言是最好的语言, 没有之一!")
好了工具介绍完毕, 大家有兴趣的可以去玩玩, 这个工具还可以用来让我们了解, 我们的一个PHP代码, 会触发调用那些底层的函数, 或者系统调用等等, Enjoy~

42 Comments

  1. example
    example April 28, 2017

    Thanks for sharing your thoughts on callgrind. Regards

  2. example
    example April 28, 2017

    I do not even know the way I stopped up right here, however I believed this post used to be great.
    I don’t understand who you are but definitely you’re going to a famous blogger in the event you aren’t already.
    Cheers!

  3. example
    example April 27, 2017

    This is really fascinating, You’re an excessively skilled blogger.
    I’ve joined your rss feed and sit up for searching for extra of your fantastic post.
    Additionally, I have shared your web site in my social networks

  4. If you might be saving a lot of cash a year with
    this machine, can it balance out inside end.
    Whirlpool cabrio Platinum Gas dryer ld error Overall,
    this Whirlpool Cabrio WGD7600XW Gas Dryer is surely an excellent purchase.
    Get yours just a little greener with these tips from
    Anna Rosemann of Eco – Consulting LA. If you don’t know what meaning, than ask a buddy, or
    perhaps your mother, and they will be more than thrilled to tell you.

  5. Though the latest celebrity gossip makes for an excellent entertainment package but these
    gossips have ruined the house of several stars.
    Generally speaking, local and international artists are pretty much aware that stepping in the celebrity
    industry means that media and press would enter their private lives and broadcast it to the public.
    Watch online episodes gossip girl However, with all
    the security features Lucky Nugget Casino has to offer, it
    is most certain that this casino is scam-free.
    the other day and it strongly resonated with this desire to pare back;
    to simplify; to become more real as a participant in this world
    and begin operating in a more authentic way. Xem online gossip girl season 1
    vietsub To understand this a bit more we need to look at the
    “feel good factor”. Viewed as one of the most famous video games for
    reducing weight, Zumba Fitness has managed to offer positive results for numerous women world-wide.
    Previous bans on short selling yielded a similar kind of effects on share prices,
    Mr. Watch online gossip girl the return of the
    ring Theyre (http://brooklynatlantis.poly.Edu/View_profile.Php?userid=44471) Amar Ujala
    is trusted for grounded news whilst still being known because of its Honesty and Authenticity.

  6. cobra cxt 1035r we
    cobra cxt 1035r we January 21, 2016

    Interesting facts to understand, isn’t it Let me help you using this.
    You should use a nice reward as well as a bit of the reputation boost using the NCR.
    Uv-5r On the plus side, you are able to change out CB antennas and
    fix something much bigger.
    A powerful web presence has become one part from the vital
    ingredients of the branding strategy. Baofeng uv5ra vs uv5r When people sleep side-by-side, everyone
    is influenced by the other person’s disruptive movements and sounds, including breathing
    and snoring. High quality handheld transceiver HAM radio
    set-ups might be bought for around $400.
    Even though subscribing to stock news in simple, it
    is going to greatly allow you to a lot to generate rightful changes according to your market.
    cobra cxt 1035r we
    baofeng promo code With language come various significant things like literature, history, culture, science and lifestyle.

  7. led light bar off road again
    led light bar off road again January 21, 2016

    You may also be treated to some incremental improvements in Friend – Stream, the contacts bar and dialer.
    It had lots of plant life, too a waterfall that led into a lagoon,
    where she loved to visit swimming. Xtreme led light bar off road again headlights Once the files are saved in the folder specified by you, it’s possible
    to unzip them later after you’ve installed your app.
    But, in spite of their small size you’ll find quite a number of features packed into advanced
    mini light bars, that is what means they are so reliable and useful.
    One other journey we wanted to make sure to ride early was called Stunt Fall, an exceptional reversing
    looper with two vertical towers nearby the front with the park.

  8. 100 grams sugar to cups
    100 grams sugar to cups January 20, 2016

    After it has been dried, the plants are tied into four inch bundles.
    What it means is that parents need to be mindful of the sugars
    their families consume. Silver conversion 100 grams sugar to cups to ounces

  9. netgear router extender
    netgear router extender January 20, 2016

    Failure to reset the wireless adapter might cause an IP conflict inside wireless netgear router extender regardless
    if it is really a Dynex you aren’t. Each of such luxury hotels in Buenos Aires will place
    you inside downtown district, within just a short distance of countless famous attractions
    and sights.
    Fortunately there are good wireless options including 3G, 4G and
    satellite. Once you’ve done this all you’ve to do is place a new
    “extender” or “repeater” in the window that lets you get your Wi – Fi
    reception and presto. Hawking hw2r1 review In the initial block of
    options you are going to find Wi-Fi, which is right under Airplane Mode and right above Notifications.

  10. usually
    usually January 20, 2016

    We wanted to ensure that an individual experience became functional and great
    launching initially on i – Phones i – Pads. Conveniently located on the list of lush greenery on Kaas plateau, Sahyadri mountain ranges,
    this health resort provides ample oxygen supply for better oxygenation. Beauty tips
    hindi mai She worked very difficult at combining her skill that has a technical idea that’s earning a living for everyone ‘ using You – Tube.
    Both were blended together to ‘make a soft mushroom matte smoky eye.
    Make some changes to your diet plan to increase your complexion as well as your figure.
    The very worthwhile thought process that comes from the mind may be
    the intelligence volume of our scientists that exceeds
    the expectations. usually beauty tips before bed While
    the trading community may prove to become profitable as it can be, you might also
    need to tap the entire world of creating foreign exchange exchange news.

  11. Hublot watches Big Bang he
    Hublot watches Big Bang he January 18, 2016

    Creation watches portfolio caters into a wide selection of audience and dominates the online watch market.
    At now, with costs for production being strategy to high, Wilsdorf obtained and
    gone after Geneva fulltime, where it absolutely was established
    since the Rolex Watch Company. Brand Hublot watches Big Bang he copy The
    reviews tell of your impressive watch, from Omega Seamaster enthusiasts.
    Whatever they purchase, it needs being the best possible, abdominal
    muscles finest, specimen of the company’s kind. It may seem
    to me which the comedy magician have to do only two things.

  12. cost home alarm system
    cost home alarm system January 17, 2016

    Although, regular alarms sometiems takes the perfect time to install and price hours and cash, making it more
    recommend to utilize this wireless cost home alarm system security system to protect
    your house and your familly. When you work with a gas alarm as part of your boat, it can alert you in the
    earliest about any LPG and deadly carbon monoxide leakage from the environment.
    Cheap alarm system for home The continuity produced because of
    the reed switch keeps the transistor BC547 switched OFF and hence
    the full system remains silent.
    As a runner you must pay special attention to your kind of personal protection alarm you buy.
    There certainly are a couple of circumstances to remember when choosing an invisible security system.

  13. things
    things January 17, 2016

    Don’t allow the name fool you: home security systems aren’t just for the property.
    If you are within the market for a security systems system you should go
    online and visit The Home Security Store. Home security hardware products There are many ways to shield your home but the top way to protect it’s with
    a security camera surveillance system.
    Article Source: Reiner is really a business
    owner and professional blogger that enjoys writing on subject matter in connection with lifestyle, do-it-yourself, security etc.
    You should ask any and questions you’ve got to make
    sure you are able to make the best decision as which security monitoring company
    is right for you personally.
    The press is socially obliged to protect yourself from spreading material depicting gratuitous violence, and inciting
    religious, racial, or ethnic hatred. Home security benefits things
    Carlson then noted that lots of African American Democrats often observe the Republican party as “racist,” that Jackson agreed.

  14. What does lawl mean
    What does lawl mean January 17, 2016

    Rather than carrying around a wad of papers, you could use the PC to keep up with inventory and manage everything on screen. Fortune 500
    companies as well as other SME’s still practice SWOT analysis for crafting
    strategies which assist them in grabbing a competitive position locally
    as well as globally. Quotes about how much someone means to you
    The standard deviation is the spread of how far they deviate from the average of
    43. After drinking more, you’ll feel a tasteful sweetness that
    reminds you of succulent fruits like peach
    and mango.
    You can continue to keep a record from the conversation by
    sending the playback quality directly for your email. What does swag mean What does lawl mean You
    could have a few different alternatives for scrolling via your i –
    Phone’s contact checklist.

  15. garmin golf gps maps
    garmin golf gps maps January 17, 2016

    You need the very best motorcycle GPS (global positioning system) you are able to
    afford. When you acquire a Garmin GPS device, you simply need to pay the purchase price so you would no longer must shoulder any
    setup charges as well as subscription fees.
    He said, overall, the Garmin Montana 650t handheld GPS was
    a superlative product. Four: Gather information – You want to gather all the
    information since you can about the forms of GPS units that you are interested in. Best deals on garmin golf gps maps gps systems Specific
    Garmin 1350LMT is essentially an upgrade to the excellent Device 1350T.

  16. Security First
    Security First January 17, 2016

    Most importantly, these cameras need for being moisture, dust and smoke proof to work with an efficient
    manner. No wonder, wireless home security camera systems have gained huge popularity in this particular
    arena, because of these varied qualities and hassle-free setting.
    Security First cameras training Never hightail it with a concept that because
    your home security camera system worked once, it’ll
    forever are very effective in future.
    Although these cameras use radio signals to send out their video data into a receiver
    of DVR, they still need to be supplied with lv Direct Current (DC) power.
    The images become muddled if whatsoever apparent
    inside the lowered light.

  17. plug in dildo me
    plug in dildo me January 17, 2016

    Another concern is most men finish earlier, don’t control their erection to get
    a longer period and for that reason on. This is definitely an advanced move that requires someone to flex your PM
    about 3-5 seconds before orgasm. Best pocket pussy 2015 This toys males have turn out to
    get the new must have males whether or otherwise they have
    partners.
    Photography is often a great way for just a child to unleash their creativity and
    prepare them for that future. The wholesaler gains advantage from added contact with their products
    plus a greater retail base marketing their items which produces a mutually beneficial arrangement.
    A neutral viewpoint with this stage can help gather information. Making flesh light plug in dildo me Terrorism
    removed from Latest World news is but one face of their violence.

  18. underwater camera comparison
    underwater camera comparison January 16, 2016

    For instance, you can find extremely small surveillance cameras that
    are used for spying, night viewing, are vandal resistant and that are specifically generated for indoor or outdoor uses.
    The Diana can be a prized camera which includes the power to tell any narrative.
    Latest underwater camera comparison camera Always keep memory card
    inside protective case it came in, never allow it to go lie loose inside your drawer or camera case.
    Now the professional digital camera has just about caught
    up with the film camera. Installation of a wireless home security system requires someone that’s trained inside
    the cameras and receivers, and knows the newest tips and tricks
    on you get one to work properly too.

  19. weed
    weed January 16, 2016

    Wine needs being stored with a temperature around 58 degrees Fahrenheit correctly to keep properly.
    The recommended vitamin to utilize would offer the ingredient inside the B-12 vitamin, called methylcobalamin.
    Eggs In many cases, the company could be restricted to borrowing a
    compact sum they had intended.
    Keeping the bananas faraway from the heat or sunlight will extend their lifespan. Easter eggs usually stay outside in the refrigerator for under 2 hours, therefore your Easter egg hunt lasted all afternoon, your eggs won’t be safe to
    nibble on.
    Therefore what people could possibly be reading might stop fresh news.
    Weed weed Various forms of newspapers exist for conveying this news of each activity.

  20. security cameras mod 1.7.10
    security cameras mod 1.7.10 January 14, 2016

    These activities while using people is frequently followed and also
    non-compliant recreation can readily be monitored. Like other kinds of cameras, IP cameras could be configured based on the protection demands of any business.
    security cameras mod 1.7.10 camera
    zoom night vision Kathryn Dawson writes articles for Traders Warehouse, a store from
    the UK selling a wide range of surveillance camera and equipment systems.
    Article Source: Brook can be a pro in reviewing the technical gadgets, specifically in Security Camera Systems of
    latest trends. The device has features an increased-quality camera allowing the users to capture precious moments of the lives inside
    a different way.

  21. Nigel
    Nigel October 17, 2014

    模板大神而来~~~~~

  22. real estate preview
    real estate preview February 27, 2014

    Ԝe’re a ցroup օf volunteers and starting a brand new scheme in ߋur community.
    Your web site provided us with uѕеful information to work on.
    You have done a formidable job and ouг whole groսp can
    be thankful to you.
    Ңere iѕ my blog: real estate preview

  23. Winona
    Winona February 19, 2014

    I was curious if you ever thought of changing the page layout of your website?
    Its very well written; I love what youve got to say.
    But maybe you could a little more in the way of content so people
    could connect with it better. Youve got an awful lot of text for only having one or two pictures.
    Maybe you could space it out better?

  24. goodboy
    goodboy December 18, 2013

    “C语言是最好的语言” ……

  25. wclssdn
    wclssdn October 14, 2013

    我也在微博问过大牛好几个问题.. 就是不理我… 汗… 还有. 类似大牛的这个工具可以用xhprof.. 有图片展示方式… 太直观了… 数据表格也更丰富…

  26. eagle
    eagle August 15, 2013

    需要另行安装Callgrind吗?

  27. eagle
    eagle August 15, 2013

    需要另行安装Callgrind吗?

  28. 我叫张大熊
    我叫张大熊 August 14, 2013

    微博私信问了你一个问题 可是还没有得到回答 求回答啊

  29. 我叫张大熊
    我叫张大熊 August 14, 2013

    微博私信问了你一个问题 可是还没有得到回答 求回答啊

  30. 微历史
    微历史 August 14, 2013

    保持更新很重要

  31. pakey
    pakey August 14, 2013

    没有抢到沙发

  32. 夜丶有雪
    夜丶有雪 August 14, 2013

    前排留名、广告位招租 C语言是最好的语言, 没有之一!

  33. 秋风
    秋风 August 14, 2013

    终于更新了,试试!

Comments are closed.