ºìÁªLinuxÃÅ»§
Linux°ïÖú

ÓÃGNU profilerÌá¸ß´úÂëÔËÐÐËÙ¶È

·¢²¼Ê±¼ä:2006-06-04 00:02:58À´Ô´:ºìÁª×÷Õß:ckaces
¸Ä½øÓ¦ÓóÌÐòµÄÐÔÄÜÊÇÒ»Ïî·Ç³£ºÄʱºÄÁ¦µÄ¹¤×÷£¬µ«ÊǾ¿¾¹³ÌÐòÖÐÊÇÄÄЩº¯ÊýÏûºÄµôÁ˴󲿷ÖÖ´ÐÐʱ¼ä£¬Õâͨ³£¶¼²»ÊǷdz£Ã÷ÏԵġ£ÔÚ±¾ÎÄÖÐÎÒÃǽ«Ñ§Ï°ÈçºÎʹÓà gprof Ϊ Linux £¿£¿ ƽ̨ÉϵÄÓû§¿Õ¼äºÍϵͳµ÷Óþ«È··ÖÎöÐÔÄÜÆ¿¾±¡£

¼ò½é

¸÷ÖÖÈí¼þ¶ÔÓÚÐÔÄܵÄÐèÇó¿ÉÄÜ»áÓкܴóµÄÇø±ð£¬µ«ÊǺܶàÓ¦ÓóÌÐò¶¼Óзdz£ÑϸñµÄÐÔÄÜÐèÇó£¬ÕâÒ»µã²¢²»Ææ¹Ö¡£µçÓ°²¥·ÅÆ÷¾ÍÊÇÒ»¸öºÜºÃµÄÀý×Ó£ºÈç¹ûÒ»¸öµçÓ°²¥·ÅÆ÷Ö»ÄÜÒÔËùÐèÒªËÙ¶ÈµÄ 75% À´²¥·ÅµçÓ°£¬ÄÇôËü¼¸ºõ¾ÍûʲôÓô¦ÁË¡£

ÆäËûÓ¦ÓóÌÐò£¨ÀýÈçÊÓƵ±àÂ룩Èç¹ûÊǺÄʱ·Ç³£³¤µÄ²Ù×÷£¬×îºÃÒÔ ¡°Åú´¦Àí¡± ÈÎÎñµÄ·½Ê½ÔËÐУ¬´ËʱÆô¶¯Ò»¸ö×÷Òµ£¬ÈÃÆäÒ»Ö±ÔËÐУ¬È»ºóÎÒÃǾͿÉÒÔÈ¥¸É±ðµÄÊÂÇéÁË¡£¾¡¹ÜÕâЩÀàÐ͵ÄÓ¦ÓóÌÐòûÓÐÕâÖÖÓ²ÐÔÐÔÄÜÖ¸±êµÄÏÞÖÆ£¬µ«ÊÇÌá¸ßËÙ¶ÈÈÔÈ»»á´øÀ´ºÜ¶àºÃ´¦£¬ÀýÈç¿ÉÒÔÔÚ¸ø¶¨µÄʱ¼äÄÚ¿ÉÒÔ¶Ô¸ü¶àµçÓ°½øÐбàÂ룬ÔÚͬÑùµÄʱ¼äÄÚ¿ÉÒÔÒÔ¸ü¸ßµÄÆ·ÖʽøÐбàÂë¡£

ͨ³££¬³ýÁË×î¼òµ¥µÄÓ¦ÓóÌÐòÖ®Í⣬¶ÔÓÚÆäËûÓ¦ÓóÌÐòÀ´Ëµ£¬ÐÔÄÜÔ½ºÃ£¬Õâ¸öÓ¦ÓóÌÐòµÄÓô¦¾ÍÔ½´ó£¬Ò²¾Í»áÔ½Á÷ÐС£ÓÉÓÚÕâ¸öÔ­Òò£¬ÐÔÄÜ¿¼ÂÇÊÇ£¨Ò²Ó¦¸ÃÊÇ£©ºÜ¶àÓ¦ÓóÌÐò¿ª·¢ÈËÔ±ÄÔ´üÖеĵÚÒ»¸ùÏÒ¡£

²»ÐÒµÄÊÇ£¬ºÜ¶à³¢ÊÔÈÃÓ¦ÓóÌÐòËٶȸü¿ìµÄŬÁ¦¶¼°×·ÑÁË£¬ÒòΪ¿ª·¢ÈËԱͨ³£¶¼ÊǶÔ×Ô¼ºµÄÈí¼þ½øÐÐһЩСÐ͵ÄÓÅ»¯£¬¶øûÓÐÈ¥Ñо¿³ÌÐòÔÚ¸ü´óµÄ·¶Î§ÄÚÊÇÈçºÎ²Ù×÷µÄ¡£ÀýÈ磬ÎÒÃÇ¿ÉÄܻỨ·Ñ´óÁ¿µÄʱ¼äÀ´ÈÃij¸öÌض¨º¯ÊýµÄÔËÐÐËٶȴﵽԭÀ´µÄÁ½±¶£¬ÕâÒ»µã·Ç³£²»´í£¬µ«ÊÇÈç¹ûÕâ¸öº¯ÊýºÜÉÙ±»µ÷Óã¨ÀýÈç´ò¿ªÎļþ£©£¬ÄÇô½«Õâ¸öº¯ÊýµÄÖ´ÐÐʱ¼ä´Ó 200ms ¼õÉÙµ½ 100ms£¬¶ÔÓÚÕû¸öÈí¼þµÄ×ÜÌåÖ´ÐÐʱ¼äÀ´Ëµ²¢²»»áÓÐÌ«´óµÄÓ°Ïì¡£

ÓÐЧµØÀûÓÃÄúµÄʱ¼äµÄ·½·¨ÊÇ£¬¾¡Á¿ÓÅ»¯Èí¼þÖб»Æµ·±µ÷ÓõIJ¿·Ö¡£ÀýÈ磬¼ÙÉèÓ¦ÓóÌÐò»¨ÁË 50% µÄʱ¼äÔÚ×Ö·û´®´¦Àíº¯ÊýÉÏ£¬Èç¹û¿ÉÒÔ¶ÔÕâЩº¯Êý½øÐÐÓÅ»¯£¬Ìá¸ß 10% µÄЧÂÊ£¬ÄÇôӦÓóÌÐòµÄ×ÜÌåÖ´ÐÐʱ¼ä¾Í»á¸Ä½ø 5%.

Òò´Ë£¬Èç¹ûÏ£ÍûÄܹ»ÓÐЧµØ¶Ô³ÌÐò½øÐÐÓÅ»¯£¬ÄÇô¾«È·µØÁ˽âʱ¼äÔÚÓ¦ÓóÌÐòÖÐÊÇÈçºÎ»¨·ÑµÄ£¬ÒÔ¼°ÕæʵµÄÊäÈëÊý¾Ý£¬ÕâÒ»µã·Ç³£ÖØÒª¡£ÕâÖÖÐÐΪ¾Í³ÆΪ´úÂëÆÊÎö£¨code profiling£©¡£±¾ÎĽ«¼òÒª½éÉÜ GNU ±àÒëÆ÷¹¤¾ß°üËùÌṩµÄÒ»ÖÖÆÊÎö¹¤¾ß£¬ËüµÄÃû×ÖÈÃÈË¿ÉÒÔ²úÉúÎÞÏÞåÚÏ룬½Ð GNU profiler£¨gprof£©¡£±¾ÎÄÖ÷ÒªÃæÏòÄÇЩ¿ª·ÅÔ´ÂëÈí¼þ¿ª·¢¹¤¾ßµÄÐÂÊÖ¡£

gprof À´¾ÈÔ®ÁË

ÔÚ¿ªÊ¼½éÉÜÈçºÎʹÓà gprof ֮ǰ£¬ÐèÒªÊ×ÏÈÁ˽âÒ»ÏÂÔÚÕû¸ö¿ª·¢ÖÜÆÚÖУ¬ÆÊÎöÓ¦¸ÃÔں䦽øÐС£Í¨³£À´Ëµ£¬±àд´úÂëÓ¦¸ÃÓÐ 3 ¸öÄ¿±ê£¬°´ÕÕÖØÒªÐԵĴÎÐò·Ö±ðÈçÏÂËùʾ£º

±£Ö¤Èí¼þ¿ÉÒÔÕýÈ·µØ¹¤×÷¡£Õâͨ³£ÊÇ¿ª·¢¹ý³ÌµÄÖص㡣ͨ³££¬Èç¹ûÒ»¸öÈí¼þ¸ù±¾Á¬ÎÒÃÇÆÚÍûËü×öµÄÊÂÇ鶼ʵÏÖ²»ÁË£¬ÄÇô¼´Ê¹ËüÔËÐÐËٶȷdz£¿ì£¬Ò²¸ù±¾Ã»ÓÐÈκÎÒâÒ壡ÏÔÈ»£¬ÕýÈ·ÐÔÔÚijЩÇé¿öÏ¿ÉÄܲ¢²»ÊÇÖÁ¹ØÖØÒªµÄ£»ÀýÈ磬Èç¹ûÒ»¸öµçÓ°²¥·ÅÆ÷¿ÉÒÔÕýÈ·µØ²¥·Å 99% µÄµçÓ°Îļþ£¬µ«ÊÇżȻ»áÓÐЩÏÔʾÎÊÌ⣬ÄÇËüÒÀÈ»¿ÉÒÔʹÓᣵ«ÊÇͨ³£À´Ëµ£¬ÕýÈ·ÐÔÒªÔ¶Ô¶±ÈËٶȸü¼ÓÖØÒª¡£

±£Ö¤Èí¼þÊÇ¿Éά»¤µÄ¡£Õâʵ¼ÊÉÏÊǵÚÒ»¸öÄ¿±êµÄÒ»¸ö×ÓÏͨ³££¬Èç¹ûÈí¼þ±àдµÃ¿Éά»¤ÐÔ²»ºÃ£¬ÄÇô¼´Ê¹Ëü×ʼʱ¿ÉÒԺܺõع¤×÷£¬ºÜ¿ìÄú£¨»òÆäËûÈË£©ÔÚÐÞÕý bug »òÌí¼ÓÐÂÌØÐÔʱ¿ÉÄÜÒ²»áÆÆ»µ³ÌÐòµÄÕýÈ·ÐÔ¡£

ÈÃÈí¼þ¿ÉÒÔ¿ìËÙÔËÐС£Õâ¾ÍÊÇÆÊÎöµÄÓÃÎäÖ®µØ¡£µ±Èí¼þ¿ÉÒÔÕýÈ·ÔËÐÐÖ®ºó£¬ÎÒÃǾͿÉÒÔ¿ªÊ¼ÆÊÎöµÄ¹ý³ÌÀ´°ïÖúËü¸ü¿ìµØÔËÐÐÁË¡£

¼ÙÉèÎÒÃÇÏÖÔÚÒѾ­ÓÐÁËÒ»¸ö¿ÉÒÔ¹¤×÷µÄÓ¦ÓóÌÐò£¬½ÓÏÂÀ´ÈÃÎÒÃÇÀ´¿´Ò»ÏÂÈçºÎʹÓà gprof À´¾«È·²âÁ¿Ó¦ÓóÌÐòÖ´Ðйý³ÌÖÐʱ¼ä¶¼»¨·Ñµ½Ê²Ã´µØ·½È¥ÁË£¬ÕâÑù×öµÄÄ¿µÄÊÇÁ˽âÒ»ÏÂÔÚʲôµØ·½½øÐÐÓÅ»¯Ð§¹û×î¼Ñ¡£

gprof ¿ÉÒÔ¶Ô C¡¢C++¡¢Pascal ºÍ Fortran 77 Ó¦ÓóÌÐò½øÐÐÆÊÎö¡£±¾ÎÄÖеÄÀý×ÓʹÓõÄÊÇ C.

Çåµ¥ 1. ºÄʱµÄÓ¦ÓóÌÐòʾÀý



#include

int a(void) {
int i=0,g=0;
while(i++<100000)
{
g+=i;
}
return g;
}
int b(void) {
int i=0,g=0;
while(i++<400000)
{
g+=i;
}
return g;
}

int main(int argc, char** argv)
{
int iterations;

if(argc != 2)
{
printf("Usage %s \n", argv[0]);
exit(-1);
}
else
iterations = atoi(argv[1]);

printf("No of iterations = %d\n", iterations);

while(iterations--)
{
a();
b();
}
}



ÕýÈçÎÒÃÇ´Ó´úÂëÖпÉÒÔ¿´µ½µÄ£¬Õâ¸ö·Ç³£¼òµ¥µÄÓ¦ÓóÌÐò°üÀ¨Á½¸öº¯Êý£ºa ºÍ b£¬ËüÃǶ¼´¦ÓÚÒ»¸ö·±Ã¦µÄÑ­»·ÖÐÏûºÄ CPU ÖÜÆÚ¡£main º¯ÊýÖвÉÓÃÁËÒ»¸öÑ­»·À´·´¸´µ÷ÓÃÕâÁ½¸öº¯Êý¡£µÚ¶þ¸öº¯Êý b Ñ­»·µÄ´ÎÊýÊÇ a º¯ÊýµÄ 4 ±¶£¬Òò´ËÎÒÃÇÆÚÍûÔÚ¶Ô´úÂë·ÖÎöÍêÖ®ºó£¬¿ÉÒÔ¿´³ö´ó¸ÅÓÐ 20% µÄʱ¼ä»¨ÔÚÁË a º¯ÊýÖУ¬¶ø 80% µÄʱ¼ä»¨ÔÚÁË b º¯ÊýÖС£ÏÂÃæ¾Í¿ªÊ¼ÆÊÎö´úÂ룬²¢¿´Ò»ÏÂÎÒÃǵÄÕâЩÆÚÍûÊÇ·ñÕýÈ·¡£

ÆôÓÃÆÊÎö·Ç³£¼òµ¥£¬Ö»ÐèÒªÔÚ gcc ±àÒë±êÖ¾ÖмÓÉÏ -pg ¼´¿É¡£±àÒë·½·¨ÈçÏ£º

gcc example1.c -pg -o example1 -O2 -lc

ÔÚ±àÒëºÃÕâ¸öÓ¦ÓóÌÐòÖ®ºó£¬¿ÉÒÔ°´ÕÕÆÕͨ·½Ê½ÔËÐÐÕâ¸ö³ÌÐò£º

¡£/example1 50000

µ±Õâ¸ö³ÌÐòÔËÐÐÍêÖ®ºó£¬Ó¦¸Ã»á¿´µ½ÔÚµ±Ç°Ä¿Â¼ÖÐд´½¨ÁËÒ»¸öÎļþ gmon.out.

ʹÓÃÊä³ö½á¹û

Ê×ÏÈ¿´Ò»Ï ¡°flat profile¡±£¬ÎÒÃÇ¿ÉÒÔʹÓà gprof ÃüÁî»ñµÃËü£¬ÕâÐèҪΪÆä´«µÝ¿ÉÖ´ÐÐÎļþºÍ gmon.out Îļþ×÷Ϊ²ÎÊý£¬ÈçÏÂËùʾ£º

gprof example1 gmon.out -p

Õâ»áÊä³öÒÔÏÂÄÚÈÝ£º

Çåµ¥ 2. flat profile µÄ½á¹û



Flat profile:

Each sample counts as 0.01 seconds.
% cumulative self self total
time seconds seconds calls ms/call ms/call name
80.24 63.85 63.85 50000 1.28 1.28 b
20.26 79.97 16.12 50000 0.32 0.32 a



´ÓÕâ¸öÊä³ö½á¹ûÖпÉÒÔ¿´µ½£¬ÕýÈçÎÒÃÇÆÚÍûµÄÒ»Ñù£¬b º¯ÊýËù»¨·ÑµÄʱ¼ä´ó¸ÅÊÇ a º¯ÊýËù»¨·ÑµÄʱ¼äµÄ 4 ±¶¡£ÕæÕýµÄÊý×Ö²¢²»ÊÇÊ®·ÖÓÐÓã»ÓÉÓÚÈ¡ÕûÉáÈë´íÎó£¬ÕâЩÊý×Ö¿ÉÄܲ¢²»ÊǷdz£¾«È·¡£

´ÏÃ÷µÄ¶ÁÕß¿ÉÄÜ»á×¢Òâµ½£¬ºÜ¶àº¯Êýµ÷Óã¨ÀýÈç printf£©ÔÚÕâ¸öÊä³öÖж¼Ã»ÓгöÏÖ¡£ÕâÊÇÒòΪÕâЩº¯Êý¶¼ÊÇÔÚ C ÔËÐÐʱ¿â£¨libc.so£©Öеģ¬£¨ÔÚ±¾ÀýÖУ©ËüÃǶ¼Ã»ÓÐʹÓà -pg ½øÐбàÒ룬Òò´Ë¾ÍûÓжÔÕâ¸ö¿âÖеĺ¯ÊýÊÕ¼¯ÆÊÎöÐÅÏ¢¡£ÉÔºóÎÒÃÇ»á»Øµ½Õâ¸öÎÊÌâÉÏÀ´¡£

½ÓÏÂÀ´ÎÒÃÇÏ£ÍûÁ˽âµÄÊÇ ¡°call graph¡±£¬Õâ¿ÉÒÔͨ¹ýÏÂÃæµÄ·½Ê½»ñµÃ£º

gprof example1 gmon.out -q

Õâ»áÊä³öÏÂÃæµÄ½á¹û¡£

Çåµ¥ 3. Call graph



Call graph (explanation follows)
granularity: each sample hit covers 2 byte(s) for 0.01% of 79.97 seconds

index % time self children called name

[1] 100.0 0.00 79.97 main [1]
63.85 0.00 50000/50000 b [2]
16.12 0.00 50000/50000 a [3]
-----------------------------------------------
63.85 0.00 50000/50000 main [1]
[2] 79.8 63.85 0.00 50000 b [2]
-----------------------------------------------
16.12 0.00 50000/50000 main [1]
[3] 20.2 16.12 0.00 50000 a [3]
-----------------------------------------------



×îºó£¬ÎÒÃÇ¿ÉÄÜ»áÏ£Íû»ñµÃÒ»¸ö ¡°´ø×¢½âµÄÔ´´úÂ롱 Çåµ¥£¬Ëü»á½«Ô´´úÂëÊä³öµ½Ó¦ÓóÌÐòÖУ¬²¢¼ÓÉÏÿ¸öº¯Êý±»µ÷ÓÃÁ˶àÉٴεÄ×¢ÊÍ¡£

ҪʹÓÃÕâÖÖ¹¦ÄÜ£¬ÇëʹÓÃÆôÓõ÷ÊÔ¹¦ÄܵıêÖ¾À´±àÒëÔ´´úÂ룬ÕâÑùÔ´´úÂë¾Í»á±»¼ÓÈë¿ÉÖ´ÐгÌÐòÖУº

gcc example1.c -g -pg -o example1 -O2 -lc

ÏñÒÔÇ°Ò»ÑùÖØÐÂÔËÐÐÕâ¸öÓ¦ÓóÌÐò£º

¡£/example1 50000

gprof ÃüÁîÏÖÔÚÓ¦¸ÃÊÇ£º

gprof example1 gmon.out -A

Õâ»áÊä³öÏÂÃæµÄ½á¹û£º

Çåµ¥ 4. ´ø×¢Ê͵ÄÔ´´úÂë





*** File /home/martynh/profarticle/example1.c:
#include

50000 -> int a(void) {
int i=0,g=0;
while(i++<100000)
{
g+=i;
}
return g;
}
50000 -> int b(void) {
int i=0,g=0;
while(i++<400000)
{
g+=i;
}
return g;
}

int main(int argc, char** argv)
##### -> {
int iterations;

if(argc != 2)
{
printf("Usage %s \n", argv[0]);
exit(-1);
}
else
iterations = atoi(argv[1]);

printf("No of iterations = %d\n", iterations);

while(iterations--)
{
a();
b();
}
}



Top 10 Lines:

Line Count

3 50000
11 50000
Execution Summary:

3 Executable lines in this file
3 Lines executed
100.00 Percent of the file executed

100000 Total number of line executions
33333.33 Average executions per line


¹²Ïí¿âµÄÖ§³Ö

ÕýÈçÔÚÇ°ÃæÔø¾­½éÉܵģ¬¶ÔÓÚ´úÂëÆÊÎöµÄÖ§³ÖÊÇÓɱàÒëÆ÷Ôö¼ÓµÄ£¬Òò´ËÈç¹ûÏ£Íû´Ó¹²Ïí¿â£¨°üÀ¨ C ¿â libc.a£©ÖлñµÃÆÊÎöÐÅÏ¢£¬¾ÍÐèҪʹÓà -pg À´±àÒëÕâЩ¿â¡£ÐÒÔ˵ÄÊÇ£¬ºÜ¶à·¢Ðа涼ÌṩÁËÒѾ­ÆôÓôúÂëÆÊÎöÖ§³Ö¶ø±àÒëµÄ C ¿â°æ±¾£¨libc_p.a£©¡£

ÔÚÎÒʹÓõķ¢Ðаæ gentoo ÖУ¬ÐèÒª½« ¡°profile¡± Ìí¼Óµ½ USE ±êÖ¾ÖУ¬²¢ÖØÐÂÖ´ÐÐ emerge glibc.µ±Õâ¸ö¹ý³ÌÍê³ÉÖ®ºó£¬¾Í»á¿´µ½ /usr/lib/libc_p.a ÎļþÒѾ­´´½¨ºÃÁË¡£¶ÔÓÚûÓа´ÕÕ±ê×¼Ìṩ libc_p µÄ·¢Ðа汾À´Ëµ£¬ÐèÒª¼ì²éËüÊÇ·ñ¿ÉÒÔµ¥¶À°²×°£¬»òÕß¿ÉÄÜÐèÒª×Ô¼ºÏÂÔØ glibc µÄÔ´´úÂë²¢½øÐбàÒë¡£

ÔÚ»ñµÃ libc_p.a ÎļþÖ®ºó£¬¾Í¿ÉÒÔ¼òµ¥µØÖØбàÒëÇ°ÃæµÄÀý×ÓÁË£¬·½·¨ÈçÏ£º

gcc example1.c -g -pg -o example1 -O2 -lc_p

È»ºó£¬¿ÉÒÔÏñÒÔÇ°Ò»ÑùÔËÐÐÕâ¸öÓ¦ÓóÌÐò£¬²¢»ñµÃ flat profile »ò call graph£¬Ó¦¸Ã»á¿´µ½ºÜ¶à C ÔËÐк¯Êý£¬°üÀ¨ printf£¨ÕâЩº¯ÊýÔÚÎÒÃǵIJâÊÔº¯ÊýÖв¢²»ÊÇÌ«ÖØÒª£©¡£

Óû§Ê±¼äÓëÄÚºËʱ¼ä

ÏÖÔÚÎÒÃÇÒѾ­ÖªµÀÈçºÎʹÓà gprof ÁË£¬½ÓÏÂÀ´¿ÉÒÔ¼òµ¥ÇÒÓÐЧµØ¶ÔÓ¦ÓóÌÐò½øÐзÖÎöÁË£¬Ï£Íû¿ÉÒÔÏû³ýÐÔÄÜÆ¿¾±¡£

²»¹ýÏÖÔÚÄú¿ÉÄÜÒѾ­×¢Òâµ½ÁË gprof µÄ×î´óȱÏÝ£ºËüÖ»ÄÜ·ÖÎöÓ¦ÓóÌÐòÔÚÔËÐÐ ¹ý³ÌÖÐËùÏûºÄµôµÄÓû§ ʱ¼ä¡£Í¨³£À´Ëµ£¬Ó¦ÓóÌÐòÔÚÔËÐÐʱ¼ÈÒª»¨·ÑһЩʱ¼äÀ´ÔËÐÐÓû§´úÂ룬ҲҪ»¨·ÑһЩʱ¼äÀ´ÔËÐÐ ¡°ÏµÍ³´úÂ롱£¬ÀýÈçÄÚºËϵͳµ÷Óá£

Èç¹û¶ÔÇåµ¥ 1 ÉÔ¼ÓÐ޸ģ¬¾Í¿ÉÒÔÇå³þµØ¿´³öÕâ¸öÎÊÌ⣺

Çåµ¥ 5. ΪÇåµ¥ 1 Ìí¼Óϵͳµ÷Ó÷ÖÎö¹¦ÄÜ



#include

int a(void) {
sleep(1);
return 0;
}
int b(void) {
sleep(4);
return 0;
}

int main(int argc, char** argv)
{
int iterations;

if(argc != 2)
{
printf("Usage %s \n", argv[0]);
exit(-1);
}
else
iterations = atoi(argv[1]);

printf("No of iterations = %d\n", iterations);

while(iterations--)
{
a();
b();
}
}



ÕýÈçÄú¿ÉÒÔ¿´µ½µÄ£¬ÎÒÃǶÔÇåµ¥ 1 ÖеĴúÂë½øÐÐÁËÐ޸ģ¬ÏÖÔÚ a º¯ÊýºÍ b º¯Êý²»ÔÙÖ»´¦Àí·±Ã¦µÄÑ­»·ÁË£¬¶øÊÇ·Ö±ðµ÷Óà C ÔËÐÐʱº¯Êý sleep À´¹ÒÆðÖ´ÐÐ 1 ÃëºÍ 4 Ãë¡£

ÏñÒÔÇ°Ò»Ñù±àÒëÕâ¸öÓ¦ÓóÌÐò£º

gcc example2.c -g -pg -o example2 -O2 -lc_p

²¢ÈÃÕâ¸ö³ÌÐòÑ­»· 30 ´Î£º

¡£/example2 30

ËùÉú³ÉµÄ flat profile ÈçÏÂËùʾ£º

Çåµ¥ 6. flat profile ÏÔʾÁËϵͳµ÷ÓõĽá¹û



Flat profile:

Each sample counts as 0.01 seconds.
no time accumulated

% cumulative self self total
time seconds seconds calls Ts/call Ts/call name
0.00 0.00 0.00 120 0.00 0.00 sigprocmask
0.00 0.00 0.00 61 0.00 0.00 __libc_sigaction
0.00 0.00 0.00 61 0.00 0.00 sigaction
0.00 0.00 0.00 60 0.00 0.00 nanosleep
0.00 0.00 0.00 60 0.00 0.00 sleep
0.00 0.00 0.00 30 0.00 0.00 a
0.00 0.00 0.00 30 0.00 0.00 b
0.00 0.00 0.00 21 0.00 0.00 _IO_file_overflow
0.00 0.00 0.00 3 0.00 0.00 _IO_new_file_xsputn
0.00 0.00 0.00 2 0.00 0.00 _IO_new_do_write
0.00 0.00 0.00 2 0.00 0.00 __find_specmb
0.00 0.00 0.00 2 0.00 0.00 __guard_setup
0.00 0.00 0.00 1 0.00 0.00 _IO_default_xsputn
0.00 0.00 0.00 1 0.00 0.00 _IO_doallocbuf
0.00 0.00 0.00 1 0.00 0.00 _IO_file_doallocate
0.00 0.00 0.00 1 0.00 0.00 _IO_file_stat
0.00 0.00 0.00 1 0.00 0.00 _IO_file_write
0.00 0.00 0.00 1 0.00 0.00 _IO_setb
0.00 0.00 0.00 1 0.00 0.00 ____strtol_l_internal
0.00 0.00 0.00 1 0.00 0.00 ___fxstat64
0.00 0.00 0.00 1 0.00 0.00 __cxa_atexit
0.00 0.00 0.00 1 0.00 0.00 __errno_location
0.00 0.00 0.00 1 0.00 0.00 __new_exitfn
0.00 0.00 0.00 1 0.00 0.00 __strtol_internal
0.00 0.00 0.00 1 0.00 0.00 _itoa_word
0.00 0.00 0.00 1 0.00 0.00 _mcleanup
0.00 0.00 0.00 1 0.00 0.00 atexit
0.00 0.00 0.00 1 0.00 0.00 atoi
0.00 0.00 0.00 1 0.00 0.00 exit
0.00 0.00 0.00 1 0.00 0.00 flockfile
0.00 0.00 0.00 1 0.00 0.00 funlockfile
0.00 0.00 0.00 1 0.00 0.00 main
0.00 0.00 0.00 1 0.00 0.00 mmap
0.00 0.00 0.00 1 0.00 0.00 moncontrol
0.00 0.00 0.00 1 0.00 0.00 new_do_write
0.00 0.00 0.00 1 0.00 0.00 printf
0.00 0.00 0.00 1 0.00 0.00 setitimer
0.00 0.00 0.00 1 0.00 0.00 vfprintf
0.00 0.00 0.00 1 0.00 0.00 write



Èç¹û¶ÔÕâ¸öÊä³ö½á¹û½øÐзÖÎö£¬ÎÒÃǾͻῴµ½£¬¾¡¹Ü profiler ÒѾ­¼Ç¼ÁËÿ¸öº¯Êý±»µ÷ÓõÄÈ·ÇдÎÊý£¬µ«ÊÇΪÕâЩº¯Êý¼Ç¼µÄʱ¼ä£¨Êµ¼ÊÉÏÊÇËùÓк¯Êý£©¶¼ÊÇ 0.00.ÕâÊÇÒòΪ sleep º¯Êýʵ¼ÊÉÏÊÇÖ´ÐÐÁËÒ»´Î¶ÔÄں˿ռäµÄµ÷Ó㬴Ӷø½«Ó¦ÓóÌÐòµÄÖ´ÐйÒÆðÁË£¬È»ºóÓÐЧµØÔÝÍ£Ö´ÐУ¬²¢µÈ´ýÄÚºËÔٴν«Æ份ÐÑ¡£ÓÉÓÚ»¨ÔÚÓû§¿Õ¼äÖ´ÐеÄʱ¼äÓ뻨ÔÚÄÚºËÖÐ˯ÃßµÄʱ¼äÏà±È·Ç³£Ð¡£¬Òò´Ë¾Í±»È¡Õû³ÉÁãÁË¡£ÆäÔ­ÒòÊÇ gprof ½ö½öÊÇͨ¹ýÒԹ̶¨µÄÖÜÆÚ¶Ô³ÌÐòÔËÐÐʱ¼ä ½øÐвÉÑù²âÁ¿À´¹¤×÷µÄ¡£Òò´Ë£¬µ±³ÌÐò²»ÔËÐÐʱ£¬¾Í²»»á¶Ô³ÌÐò½øÐвÉÑù²âÁ¿¡£

Õâʵ¼ÊÉÏÊÇÒ»°ÑË«Èн£¡£´ÓÒ»¸ö·½ÃæÀ´Ëµ£¬ÕâʹµÃÓÐЩ³ÌÐò·Ç³£ÄÑÒÔ½øÐÐÓÅ»¯£¬ÀýÈ绨·Ñ´ó²¿·Öʱ¼äÔÚÄں˿ռäµÄ³ÌÐò£¬»òÕßÓÉÓÚÍⲿÒòËØ£¨ÀýÈç²Ù×÷ϵͳµÄ I/O ×Óϵͳ¹ýÔØ£©¶øÔËÐе÷dz£ÂýµÄ³ÌÐò¡£´ÓÁíÒ»¸ö·½ÃæÀ´Ëµ£¬ÕâÒâζ×ÅÆÊÎö²»»áÊܵ½ÏµÍ³ÖÐÆäËûʼþµÄÓ°Ï죨ÀýÈçÁíÍâÒ»¸öÓû§Ê¹ÓÃÁË´óÁ¿µÄ CPU ʱ¼ä£©¡£

ͨ³££¬ÓÐÒ»¸öºÜºÃµÄ»ù×¼²âÊÔ¿ÉÒÔÓÃÀ´²é¿´ gprof ¶ÔÓÚ°ïÖú¶ÔÓ¦ÓóÌÐò½øÐÐÓÅ»¯ÊǶàôÓÐÓ㬷½·¨ÊÇÔÚ time ÃüÁîÏÂÃæÖ´ÐÐËü¡£Õâ¸öÃüÁî»áÏÔʾһ¸öÓ¦ÓóÌÐòÔËÐÐÍê³ÉÐèÒª¶àÉÙʱ¼ä£¬²¢¿ÉÒÔ²âÁ¿ËüÔÚÓû§¿Õ¼äºÍÄں˿ռä¸÷»¨·ÑÁ˶àÉÙʱ¼ä¡£

Èç¹û²é¿´Ò»ÏÂÇåµ¥ 2 ÖеÄÀý×Ó£º

time ./example2 30

Êä³ö½á¹ûÓ¦¸ÃÈçÏÂËùʾ£º

Çåµ¥ 7. time ÃüÁîµÄÊä³ö½á¹û



No of iterations = 30

real 2m30.295s
user 0m0.000s
sys 0m0.004s



ÎÒÃÇ¿ÉÒÔ¿´³ö¼¸ºõûÓжàÉÙʱ¼ä±»»¨·ÑÔÚÖ´ÐÐÓû§¿Õ¼äµÄ´úÂëÉÏ£¬Òò´Ë gprof ÔÚ´Ë´¦²»»á·Ç³£ÓÐÓá£

½áÊøÓï

¾¡¹Ü gprof ´æÔÚÉÏÃæµÄÏÞÖÆ£¬µ«ÊÇËü¶ÔÓÚÓÅ»¯´úÂëÀ´ËµÒÀÈ»ÊǸö·Ç³£ÓÐÓõŤ¾ß£¬Èç¹ûÄúµÄ´úÂë´ó²¿·ÖÊÇÓû§¿Õ¼ä CPU Ãܼ¯Ð͵ģ¬ËüµÄÓô¦¾Í¸ü¼ÓÃ÷ÏÔ¡£Ê×ÏÈʹÓà time À´ÔËÐгÌÐò´Ó¶øÅÐ¶Ï gprof ÊÇ·ñÄܲúÉúÓÐÓÃÐÅÏ¢ÊǸöºÃÖ÷Òâ¡£

Èç¹û gprof ²»ÊʺÏÄúµÄÆÊÎöÐèÒª£¬ÄÇô»¹ÓÐÆäËûһЩ¹¤¾ß¿ÉÒÔ¿Ë·þ gprof ²¿·ÖȱÏÝ£¬°üÀ¨ OProfile ºÍ Sysprof £¨Çë²Î¿´ ²Î¿¼×ÊÁÏ ÖÐÓйØÕâЩ¹¤¾ßÐÅÏ¢µÄÁ´½Ó£©¡£

´ÓÁíÒ»¸ö·½ÃæÀ´Ëµ£¬¼ÙÉèÎÒÃÇÒѾ­°²×°ÁË gcc£¬gprof Ïà¶ÔÓÚÆäËû¹¤¾ßÀ´Ëµ£¬Ò»¸öÖ÷ÒªµÄÓŵãÊǺܿÉÄÜÔçÒÑÔÚ Linux »úÆ÷ÉÏ°²×°ÁËÐèҪʹÓõŤ¾ß¡£

¹ØÓÚ×÷Õß

Martyn Honeyford 1996 Äê±ÏÒµÓÚŵ¶¡ºº´óѧ£¬»ñ¼ÆËã»ú¿Æѧѧʿѧλ¡£´ÓÄÇʱÆð£¬Ëû¾Í³ÉΪλÓÚÓ¢¸ñÀ¼ Hursley µÄ IBM Ó¢¹úʵÑéÊÒµÄÒ»ÃûÈí¼þ¹¤³Ìʦ¡£ËûÄ¿Ç°µÄÖ°ÎñÊÇ WebSphere MQ Everyplace ¿ª·¢ÍŶÓÖеÄÒ»Ãû¿ª·¢ÈËÔ±¡£ÔÚ²»¹¤×÷µÄʱºò£¬Ëû¾­³£µ¯µç¼ªËû£¨µ¯µÃºÜ²î£©»òÕß·è¿ñµØÍæµç×ÓÓÎÏ·¡£¿ÉÒÔͨ¹ý martynh@uk.ibm.com Óë Martyn ÁªÏµ¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ