閱讀395 返回首頁    go 阿裏雲 go 技術社區[雲棲]


[轉貼]gprof使用備忘


gprof是GNU profiler工具。可以顯示程序運行的“flatprofile”,包括每個函數的調用次數,每個函數消耗的處理器時間。也可以顯示“調用圖”,包括函數的調用關係,每個函數調用花費了多少時間。還可以顯示“注釋的源代碼”,是程序源代碼的一個複本,標記有程序中每行代碼的執行次數。


在編譯或鏈接源程序的時候在編譯器的命令行參數中加入“-pg”選項,編譯時編譯器會自動在目標代碼中插入用於性能測試的代碼片斷,這些代碼在程序在運行時采集並記錄函數的調用關係和調用次數,以及采集並記錄函數自身執行時間和子函數的調用時間,程序運行結束後,會在程序退出的路徑下生成一個gmon.out文件。這個文件就是記錄並保存下來的監控數據。可以通過命令行方式的gprof或圖形化的Kprof來解讀這些數據並對程序的性能進行分析。另外,如果想查看庫函數的profiling,需要在編譯是再加入“-lc_p”編譯參數代替“-lc”編譯參數,這樣程序會鏈接libc_p.a庫,才可以產生庫函數的profiling信息。如果想執行一行一行的profiling,還需要加入“-g”編譯參數。
例如如下命令行:
gcc -Wall -g -pg -lc_p example.c -oexample


執行如下命令行,即可執行gprof:



 
the percentage of the total running time of the
time 
program used by this function.
 
函數使用時間占所有時間的百分比。
cumulative 
a running sum of the number of seconds accounted
 
for by this function and those listed above it.
 
函數和上列函數累計執行的時間。
 
the number of seconds accounted for by this
seconds 
This is the major sort forthis
 
listing.
 
函數本身所執行的時間。
calls 
the number of times this function was invoked, if
 
this function is profiled, else blank.
 
函數被調用的次數
 
the average number of milliseconds spent in this
ms/call 
function per call, if this function is profiled,
 
else blank.
 
每一次調用花費在函數的時間microseconds。
 
the average number of milliseconds spent in this
ms/call 
function and its descendents per call, if this
 
function is profiled, else blank.
 
每一次調用,花費在函數及其衍生函數的平均時間microseconds。
name 
This is the minorsort
 
for this listing. The index shows the location of
 
the function in the gprof listing. If the index is
 
in parenthesis it shows where it would appear in
 
the gprof listing if it were to be printed.
 

 

$ uname -a
Linux dev 2.4.21-9.30AXsmp #1 SMP Wed May 26 23:37:09 EDT 2004 i686i686 i386 GNU/Linux

$ gprof --version
GNU gprof 2.14.90.0.4
Based on BSD gprof, copyright 1983 Regents of the University ofCalifornia.
This program is free software. This program has absolutely nowarranty.

int main(void)
{
 
printf("helloworld/n");
 
return0;
}

$ ./a.out
hello world

$ gprof
gprof: gmon.out file is missing call-graphdata

這個結果有點莫名其妙。難道這個簡單的一個例子都過不了?其實這是gprof的一個“feature”,也許是專門用來迷惑初學者的。出錯的原因參見:https://gcc.gnu.org/ml/gcc-help/2006-06/msg00037.html,是我們的函數過於簡單,其中沒有什麼函數調用導致的。

int main(void)
{
 

for(i = 0; i < 10; i++)
 

return0;
}

static int sub(void)
{
 
return0;
}

$ gcc -g -pg bar.c
$ ./a.out
hello world

$ gprof
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 10 0.00 0.00 sub

% the percentage of the total running time of the
time program used by this function.

cumulative a running sum of the number of secondsaccounted
seconds for by this function and those listed above it.

self the number of seconds accounted for by this
seconds function alone. This is the major sort for this
listing.

calls the number of times this function was invoked, if
this function is profiled, else blank.

self the average number of milliseconds spent in this
ms/call function per call, if this function is profiled,
else blank.

total the average number of milliseconds spent in this
ms/call function and its descendents per call, if this
function is profiled, else blank.

name the name of the function. This is the minor sort
for this listing. The index shows the location of
the function in the gprof listing. If the index is
in parenthesis it shows where it would appear in
the gprof listing if it were to be printed.

Call graph (explanation follows)

index % time self children called name
0.00 0.00 10/10 main [8]
[1] 0.0 0.00 0.00 10 sub [1]
-----------------------------------------------

This table describes the call tree of the program, and wassorted by
the total amount of time spent in each function and itschildren.

Each entry in this table consists of several lines. The linewith the
index number at the left hand margin lists the currentfunction.
The lines above it list the functions that called thisfunction,
and the lines below it list the functions this one called.
This line lists:
index A unique number given to each element of the table.
Index numbers are sorted numerically.
The index number is printed next to every function name so
it is easier to look up where the function in the table.

% time This is the percentage of the `total' time that wasspent
in this function and its children. Note that due to
different viewpoints, functions excluded by options, etc,
these numbers will NOT add up to 100%.

children This is the total amount of time propagated intothis
function by its children.

called This is the number of times the function wascalled.
If the function called itself recursively, the number
only includes non-recursive calls, and is followed by
a `+' and the number of recursive calls.

name The name of the current function. The index number is
printed after it. If the function is a member of a
cycle, the cycle number is printed between the
function's name and the index number.

self This is the amount of time that was propagateddirectly
from the function into this parent.

children This is the amount of time that was propagatedfrom
the function's children into this parent.

called This is the number of times this parent called the
function `/' the total number of times the function
was called. Recursive calls to the function are not
included in the number after the `/'.

name This is the name of the parent. The parent's index
number is printed after it. If the parent is a
member of a cycle, the cycle number is printed between
the name and the index number.

If the parents of the function cannot be determined, theword
`<spontaneous>' is printed in the `name' field, and all theother
fields are blank.

self This is the amount of time that was propagateddirectly
from the child into the function.

children This is the amount of time that was propagated fromthe
child's children to the function.

called This is the number of times the function called
this child `/' the total number of times the child
was called. Recursive calls by the child are not
listed in the number after the `/'.

name This is the name of the child. The child's index
number is printed after it. If the child is a
member of a cycle, the cycle number is printed
between the name and the index number.

If there are any cycles (circles) in the call graph, there isan
entry for the cycle-as-a-whole. This entry shows who calledthe
cycle (as parents) and the members of the cycle (aschildren.)
The `+' recursive calls entry shows the number of function callsthat
were internal to the cycle, and the calls entry for each membershows,
for that member, how many times it was called from other membersof
the cycle.

[1] sub (bar.c)

$ gprof -b
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 10 0.00 0.00 sub

Call graph

index % time self children called name
0.00 0.00 10/10 main [8]
[1] 0.0 0.00 0.00 10 sub [1]
-----------------------------------------------

static int sub(void);
static int sub2(void);

int main(void)
{
 
sub();
 

return0;
}

static int sub(void)
{
 
inti;
 

for(i = 0; i < 10000000; i++)
 

returnres;
}

static int sub2(void)
{
 
inti;
 

for(i = 0; i < 10000000; i++)
 

returnres;
}

$gprof -b
Flat profile:

Each sample counts as 0.01 seconds.
% cumulative self self total
time seconds seconds calls ms/call ms/call name
53.33 0.08 0.08 1 80.00 80.00sub2
46.67 0.15 0.07 1 70.00 70.00sub

Call graph

index % time self children called name
<spontaneous>
[1] 100.0 0.00 0.15 main [1]
0.08 0.00 1/1 sub2 [2]
0.07 0.00 1/1 sub [3]
-----------------------------------------------
0.08 0.00 1/1 main [1]
[2] 53.3 0.08 0.00 1 sub2 [2]
-----------------------------------------------
0.07 0.00 1/1 main [1]
[3] 46.7 0.07 0.00 1 sub [3]
-----------------------------------------------

最後更新:2017-04-02 00:06:29

  上一篇:go 傳參數空值類型問題解決
  下一篇:go PHP擴展導出類