GNU Profiler (gprof) 的简单使用

来源:互联网 发布:网络直播乱象丛生 编辑:程序博客网 时间:2024/05/22 08:29

简介

GNU Profiler (gprof) 是Linux 自带的性能测试工具,可以生成 C, Pascal, Fortran77 程序的函数级别的耗时报表

使用

以下面的 pgtest.cpp 为例说明

#include <stdio.h>#include <unistd.h>#include <stdlib.h>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 <No of Iterations>\n", argv[0]);                exit(-1);        }        else                iterations = atoi(argv[1]);        printf("No of iterations = %d\n", iterations);        while(iterations--)        {                a();                b();        }}

  1. 编译上面的文件:g++ pgtest.cpp -g -o pgtest -pg -lc
  2. 然后运行 ./pgtest 100
  3. 运行过后,会生成 gmon.out(下面通过这个二进制中间文件进而生成report)
  4. 通过 gprof pgtest gmon.out -p > flat-report 生成 Flat profile report;或者通过 gprof pgtest gmon.out -q > callgraph-report 生成 Call graph profile report

Flat profile report:

Flat profile:Each sample counts as 0.01 seconds.  %   cumulative   self              self     total time   seconds   seconds    calls  ms/call  ms/call  name 79.33      0.11     0.11      100     1.11     1.11  b() 21.64      0.14     0.03      100     0.30     0.30  a() %         the percentage of the total running time of thetime       program used by this function.cumulative a running sum of the number of seconds accounted seconds   for by this function and those listed above it. self      the number of seconds accounted for by thisseconds    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 thisms/call    function per call, if this function is profiled,           else blank. total     the average number of milliseconds spent in thisms/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 profile report:

                     Call graph (explanation follows)granularity: each sample hit covers 2 byte(s) for 7.07% of 0.14 secondsindex % time    self  children    called     name                                                 <spontaneous>[1]    100.0    0.00    0.14                 main [1]                0.11    0.00     100/100         b() [2]                0.03    0.00     100/100         a() [3]-----------------------------------------------                0.11    0.00     100/100         main [1][2]     78.6    0.11    0.00     100         b() [2]-----------------------------------------------                0.03    0.00     100/100         main [1][3]     21.4    0.03    0.00     100         a() [3]----------------------------------------------- This table describes the call tree of the program, and was sorted by the total amount of time spent in each function and its children. Each entry in this table consists of several lines.  The line with the index number at the left hand margin lists the current function. The lines above it list the functions that called this function, 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 was spent                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%.     self       This is the total amount of time spent in this function.     children   This is the total amount of time propagated into this                function by its children.     called     This is the number of times the function was called.                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. For the function's parents, the fields have the following meanings:     self       This is the amount of time that was propagated directly                from the child into the function.     children   This is the amount of time that was propagated from the                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 is an entry for the cycle-as-a-whole.  This entry shows who called the cycle (as parents) and the members of the cycle (as children.) The `+' recursive calls entry shows the number of function calls that were internal to the cycle, and the calls entry for each member shows, for that member, how many times it was called from other members of the cycle.^LIndex by function name   [3] a()                     [2] b()

注意

上面是单个cpp文件编译的情况。对于一个工程,用Makefile编译的情况说明如下。

点击这里下载示例工程。

OBJ_DIR = ../objBIN_DIR = ../binINC_DIR = ../hSRC_DIR = ./OBJS = \        $(OBJ_DIR)/main.o \        $(OBJ_DIR)/dmd.o \        $(OBJ_DIR)/thread.oTARGET = $(BIN_DIR)/tetrisINC_OPT = -I$(INC_DIR)LNK_OPT =$(TARGET) : chkobjdir chkbindir $(OBJS)        $(CXX) -pg -o $@ $(OBJS) $(LNK_OPT)$(OBJ_DIR)/%.o : $(SRC_DIR)/%.cpp        $(CXX) -pg $(INC_OPT) -c -o $@ $<chkobjdir :        @if test ! -d $(OBJ_DIR) ; \        then \                mkdir $(OBJ_DIR) ; \        fichkbindir :        @if test ! -d $(BIN_DIR) ; \        then \                mkdir $(BIN_DIR) ; \        fi.PHONY : cleanclean :        -rm -f $(TARGET)        -rm -rf $(OBJ_DIR)
第14行,如果没有加 -pg,则执行生成的文件后,不会生成gmon.out 文件。

第14行加了 -pg,第17行没有加 -pg,则执行生成的文件后,会生成gmon.out 文件,但 gprof ../bin/tetris gmon.out -p 生成的report 中表格为空,gprof ../bin/tetris gmon.out -q 不会生成report,而是报错:gmon.out file is missing call-graph data。

第14行和第17行都加了 -pg,则一切正常,即会生成gmon.out 文件,gprof ../bin/tetris gmon.out -p 和 gprof ../bin/tetris gmon.out -q 都能生成report,且report 中的表格不为空。

kill 程序时,不要用 kill -9 pid 的方式,检查程序从容退出的kill 方式,用这种方式kill 程序。例如 kill -15 pid ,否则可能不能生成 gmon.out 文件。

按照上面所说的方式kill 程序后,可能不能立刻看到生成的 gmon.out 文件,因为它还在后头生成中,请不要灰心,耐心等待一会再查看。


参考

http://www.cs.utah.edu/dept/old/texinfo/as/gprof.html

原创粉丝点击