home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gnumans.zip / gprof.inf (.txt) < prev    next >
OS/2 Help File  |  1997-07-02  |  31KB  |  859 lines

  1.  
  2. ΓòÉΓòÉΓòÉ 1. Title page ΓòÉΓòÉΓòÉ
  3.  
  4.                                     GNU gprof
  5.  
  6.                                 The gnu Profiler
  7.  
  8.                         Jay Fenlason and Richard Stallman
  9.  
  10. This manual describes the gnu profiler, gprof, and how you can use it to 
  11. determine which parts of a program are taking most of the execution time.  We 
  12. assume that you know how to write, compile, and execute programs.  gnu gprof 
  13. was written by Jay Fenlason. 
  14.  
  15. This manual was edited January 1993 by Jeffrey Osier. 
  16.  
  17. Copyright (C) 1988, 1992 Free Software Foundation, Inc. 
  18.  
  19. Permission is granted to make and distribute verbatim copies of this manual 
  20. provided the copyright notice and this permission notice are preserved on all 
  21. copies. 
  22.  
  23. Permission is granted to copy and distribute modified versions of this manual 
  24. under the conditions for verbatim copying, provided that the entire resulting 
  25. derived work is distributed under the terms of a permission notice identical to 
  26. this one. 
  27.  
  28. Permission is granted to copy and distribute translations of this manual into 
  29. another language, under the same conditions as for modified versions. 
  30.  
  31.  
  32. ΓòÉΓòÉΓòÉ 2. Top node: "Profiling a Program: Where Does It Spend Its Time?" ΓòÉΓòÉΓòÉ
  33.  
  34. This manual describes the gnu profiler, gprof, and how you can use it to 
  35. determine which parts of a program are taking most of the execution time.  We 
  36. assume that you know how to write, compile, and execute programs.  gnu gprof 
  37. was written by Jay Fenlason. 
  38.  
  39. This manual was updated January 1993. 
  40.  
  41.  Why                                     What profiling means, and why it is 
  42.                                          useful. 
  43.  Compiling                               How to compile your program for 
  44.                                          profiling. 
  45.  Executing                               How to execute your program to 
  46.                                          generate the profile data file 
  47.                                          gmon.out. 
  48.  Invoking                                How to run gprof, and how to specify 
  49.                                          options for it. 
  50.  
  51.  
  52.  Flat Profile                            The flat profile shows how much time 
  53.                                          was spent executing directly in each 
  54.                                          function. 
  55.  Call Graph                              The call graph shows which functions 
  56.                                          called which others, and how much time 
  57.                                          each function used when its subroutine 
  58.                                          calls are included. 
  59.  
  60.  
  61.  Implementation                          How the profile data is recorded and 
  62.                                          written. 
  63.  Sampling Error                          Statistical margins of error. How to 
  64.                                          accumulate data from several runs to 
  65.                                          make it more accurate. 
  66.  
  67.  
  68.  Assumptions                             Some of gprof's measurements are based 
  69.                                          on assumptions about your program that 
  70.                                          could be very wrong. 
  71.  
  72.  
  73.  Incompatibilities                       (between GNU gprof and Unix gprof.) 
  74.  
  75.  
  76. ΓòÉΓòÉΓòÉ 3. Why Profile ΓòÉΓòÉΓòÉ
  77.  
  78. Profiling allows you to learn where your program spent its time and which 
  79. functions called which other functions while it was executing.  This 
  80. information can show you which pieces of your program are slower than you 
  81. expected, and might be candidates for rewriting to make your program execute 
  82. faster.  It can also tell you which functions are being called more or less 
  83. often than you expected.  This may help you spot bugs that had otherwise been 
  84. unnoticed. 
  85.  
  86. Since the profiler uses information collected during the actual execution of 
  87. your program, it can be used on programs that are too large or too complex to 
  88. analyze by reading the source.  However, how your program is run will affect 
  89. the information that shows up in the profile data.  If you don't use some 
  90. feature of your program while it is being profiled, no profile information will 
  91. be generated for that feature. 
  92.  
  93. Profiling has several steps: 
  94.  
  95.      You must compile and link your program with profiling enabled. See 
  96.       Compiling. 
  97.  
  98.      You must execute your program to generate a profile data file. See 
  99.       Executing. 
  100.  
  101.      You must run gprof to analyze the profile data. See Invoking. 
  102.  
  103.  The next three chapters explain these steps in greater detail. 
  104.  
  105.  The result of the analysis is a file containing two tables, the flat profile 
  106.  and the call graph (plus blurbs which briefly explain the contents of these 
  107.  tables). 
  108.  
  109.  The flat profile shows how much time your program spent in each function, and 
  110.  how many times that function was called.  If you simply want to know which 
  111.  functions burn most of the cycles, it is stated concisely here. See Flat 
  112.  Profile. 
  113.  
  114.  The call graph shows, for each function, which functions called it, which 
  115.  other functions it called, and how many times.  There is also an estimate of 
  116.  how much time was spent in the subroutines of each function.  This can suggest 
  117.  places where you might try to eliminate function calls that use a lot of time. 
  118.  See Call Graph. 
  119.  
  120.  
  121. ΓòÉΓòÉΓòÉ 4. Compiling a Program for Profiling ΓòÉΓòÉΓòÉ
  122.  
  123. The first step in generating profile information for your program is to compile 
  124. and link it with profiling enabled. 
  125.  
  126. To compile a source file for profiling, specify the `-pg' option when you run 
  127. the compiler.  (This is in addition to the options you normally use.) 
  128.  
  129. To link the program for profiling, if you use a compiler such as cc to do the 
  130. linking, simply specify `-pg' in addition to your usual options.  The same 
  131. option, `-pg', alters either compilation or linking to do what is necessary for 
  132. profiling.  Here are examples: 
  133.  
  134. cc -g -c myprog.c utils.c -pg
  135. cc -o myprog myprog.o utils.o -pg
  136.  
  137. The `-pg' option also works with a command that both compiles and links: 
  138.  
  139. cc -o myprog myprog.c utils.c -g -pg
  140.  
  141. If you run the linker ld directly instead of through a compiler such as cc, you 
  142. must specify the profiling startup file `/lib/gcrt0.o' as the first input file 
  143. instead of the usual startup file `/lib/crt0.o'.  In addition, you would 
  144. probably want to specify the profiling C library, `/usr/lib/libc_p.a', by 
  145. writing `-lc_p' instead of the usual `-lc'.  This is not absolutely necessary, 
  146. but doing this gives you number-of-calls information for standard library 
  147. functions such as read and open.  For example: 
  148.  
  149. ld -o myprog /lib/gcrt0.o myprog.o utils.o -lc_p
  150.  
  151. If you compile only some of the modules of the program with `-pg', you can 
  152. still profile the program, but you won't get complete information about the 
  153. modules that were compiled without `-pg'.  The only information you get for the 
  154. functions in those modules is the total time spent in them; there is no record 
  155. of how many times they were called, or from where.  This will not affect the 
  156. flat profile (except that the calls field for the functions will be blank), but 
  157. will greatly reduce the usefulness of the call graph. 
  158.  
  159.  
  160. ΓòÉΓòÉΓòÉ 5. Executing the Program to Generate Profile Data ΓòÉΓòÉΓòÉ
  161.  
  162. Once the program is compiled for profiling, you must run it in order to 
  163. generate the information that gprof needs.  Simply run the program as usual, 
  164. using the normal arguments, file names, etc.  The program should run normally, 
  165. producing the same output as usual.  It will, however, run somewhat slower than 
  166. normal because of the time spent collecting and the writing the profile data. 
  167.  
  168. The way you run the program---the arguments and input that you give it---may 
  169. have a dramatic effect on what the profile information shows.  The profile data 
  170. will describe the parts of the program that were activated for the particular 
  171. input you use.  For example, if the first command you give to your program is 
  172. to quit, the profile data will show the time used in initialization and in 
  173. cleanup, but not much else. 
  174.  
  175. You program will write the profile data into a file called `gmon.out' just 
  176. before exiting.  If there is already a file called `gmon.out', its contents are 
  177. overwritten.  There is currently no way to tell the program to write the 
  178. profile data under a different name, but you can rename the file afterward if 
  179. you are concerned that it may be overwritten. 
  180.  
  181. In order to write the `gmon.out' file properly, your program must exit 
  182. normally: by returning from main or by calling exit.  Calling the low-level 
  183. function _exit does not write the profile data, and neither does abnormal 
  184. termination due to an unhandled signal. 
  185.  
  186. The `gmon.out' file is written in the program's current working directory at 
  187. the time it exits.  This means that if your program calls chdir, the `gmon.out' 
  188. file will be left in the last directory your program chdir'd to.  If you don't 
  189. have permission to write in this directory, the file is not written.  You may 
  190. get a confusing error message if this happens.  (We have not yet replaced the 
  191. part of Unix responsible for this; when we do, we will make the error message 
  192. comprehensible.) 
  193.  
  194.  
  195. ΓòÉΓòÉΓòÉ 6. gprof Command Summary ΓòÉΓòÉΓòÉ
  196.  
  197. After you have a profile data file `gmon.out', you can run gprof to interpret 
  198. the information in it.  The gprof program prints a flat profile and a call 
  199. graph on standard output.  Typically you would redirect the output of gprof 
  200. into a file with `>'. 
  201.  
  202. You run gprof like this: 
  203.  
  204. gprof options [executable-file [profile-data-files...]] [> outfile]
  205.  
  206. Here square-brackets indicate optional arguments. 
  207.  
  208. If you omit the executable file name, the file `a.out' is used.  If you give no 
  209. profile data file name, the file `gmon.out' is used.  If any file is not in the 
  210. proper format, or if the profile data file does not appear to belong to the 
  211. executable file, an error message is printed. 
  212.  
  213. You can give more than one profile data file by entering all their names after 
  214. the executable file name; then the statistics in all the data files are summed 
  215. together. 
  216.  
  217. The following options may be used to selectively include or exclude functions 
  218. in the output: 
  219.  
  220.  -a 
  221.            The `-a' option causes gprof to suppress the printing of statically 
  222.            declared (private) functions.  (These are functions whose names are 
  223.            not listed as global, and which are not visible outside the 
  224.            file/function/block where they were defined.)  Time spent in these 
  225.            functions, calls to/from them, etc, will all be attributed to the 
  226.            function that was loaded directly before it in the executable file. 
  227.            This option affects both the flat profile and the call graph. 
  228.  
  229.  -e function_name 
  230.            The `-e function' option tells gprof to not print information about 
  231.            the function function_name (and its children...) in the call graph. 
  232.            The function will still be listed as a child of any functions that 
  233.            call it, but its index number will be shown as `[not printed]'. 
  234.            More than one `-e' option may be given; only one function_name may 
  235.            be indicated with each `-e' option. 
  236.  
  237.  -E function_name 
  238.            The -E function option works like the -e option, but time spent in 
  239.            the function (and children who were not called from anywhere else), 
  240.            will not be used to compute the percentages-of-time for the call 
  241.            graph.  More than one `-E' option may be given; only one 
  242.            function_name may be indicated with each `-E' option. 
  243.  
  244.  -f function_name 
  245.            The `-f function' option causes gprof to limit the call graph to the 
  246.            function function_name and its children (and their children...). 
  247.            More than one `-f' option may be given; only one function_name may 
  248.            be indicated with each `-f' option. 
  249.  
  250.  -F function_name 
  251.            The `-F function' option works like the -f option, but only time 
  252.            spent in the function and its children (and their children...) will 
  253.            be used to determine total-time and percentages-of-time for the call 
  254.            graph.  More than one `-F' option may be given; only one 
  255.            function_name may be indicated with each `-F' option.  The `-F' 
  256.            option overrides the `-E' option. 
  257.  
  258.  -k from... to... 
  259.            The `-k' option allows you to delete from the profile any arcs from 
  260.            routine from to routine to. 
  261.  
  262.  -v 
  263.            The `-v' flag causes gprof to print the current version number, and 
  264.            then exit. 
  265.  
  266.  -z 
  267.            If you give the `-z' option, gprof will mention all functions in the 
  268.            flat profile, even those that were never called, and that had no 
  269.            time spent in them.  This is useful in conjunction with the `-c' 
  270.            option for discovering which routines were never called. 
  271.  
  272.  The order of these options does not matter. 
  273.  
  274.  Note that only one function can be specified with each -e, -E, -f or -F 
  275.  option.  To specify more than one function, use multiple options.  For 
  276.  example, this command: 
  277.  
  278.   gprof -e boring -f foo -f bar myprogram > gprof.output
  279.  
  280.  lists in the call graph all functions that were reached from either foo or bar 
  281.  and were not reachable from boring. 
  282.  
  283.  There are a few other useful gprof options: 
  284.  
  285.  -b 
  286.            If the `-b' option is given, gprof doesn't print the verbose blurbs 
  287.            that try to explain the meaning of all of the fields in the tables. 
  288.            This is useful if you intend to print out the output, or are tired 
  289.            of seeing the blurbs. 
  290.  
  291.  -c 
  292.            The `-c' option causes the static call-graph of the program to be 
  293.            discovered by a heuristic which examines the text space of the 
  294.            object file.  Static-only parents or children are indicated with 
  295.            call counts of `0'. 
  296.  
  297.  -d num 
  298.            The `-d num' option specifies debugging options. 
  299.  
  300.  - s 
  301.            The `-s' option causes gprof to summarize the information in the 
  302.            profile data files it read in, and write out a profile data file 
  303.            called `gmon.sum', which contains all the information from the 
  304.            profile data files that gprof read in.  The file `gmon.sum' may be 
  305.            one of the specified input files; the effect of this is to merge the 
  306.            data in the other input files into `gmon.sum'. See Sampling Error. 
  307.  
  308.            Eventually you can run gprof again without `-s' to analyze the 
  309.            cumulative data in the file `gmon.sum'. 
  310.  
  311.  -T 
  312.            The `-T' option causes gprof to print its output in ``traditional'' 
  313.            BSD style. 
  314.  
  315.  
  316. ΓòÉΓòÉΓòÉ 7. How to Understand the Flat Profile ΓòÉΓòÉΓòÉ
  317.  
  318. The flat profile shows the total amount of time your program spent executing 
  319. each function.  Unless the `-z' option is given, functions with no apparent 
  320. time spent in them, and no apparent calls to them, are not mentioned.  Note 
  321. that if a function was not compiled for profiling, and didn't run long enough 
  322. to show up on the program counter histogram, it will be indistinguishable from 
  323. a function that was never called. 
  324.  
  325. This is part of a flat profile for a small program: 
  326.  
  327. Flat profile:
  328.  
  329. Each sample counts as 0.01 seconds.
  330.   %   cumulative   self              self     total
  331.  time   seconds   seconds    calls  ms/call  ms/call  name
  332.  33.34      0.02     0.02     7208     0.00     0.00  open
  333.  16.67      0.03     0.01      244     0.04     0.12  offtime
  334.  16.67      0.04     0.01        8     1.25     1.25  memccpy
  335.  16.67      0.05     0.01        7     1.43     1.43  write
  336.  16.67      0.06     0.01                             mcount
  337.   0.00      0.06     0.00      236     0.00     0.00  tzset
  338.   0.00      0.06     0.00      192     0.00     0.00  tolower
  339.   0.00      0.06     0.00       47     0.00     0.00  strlen
  340.   0.00      0.06     0.00       45     0.00     0.00  strchr
  341.   0.00      0.06     0.00        1     0.00    50.00  main
  342.   0.00      0.06     0.00        1     0.00     0.00  memcpy
  343.   0.00      0.06     0.00        1     0.00    10.11  print
  344.   0.00      0.06     0.00        1     0.00     0.00  profil
  345.   0.00      0.06     0.00        1     0.00    50.00  report
  346. ...
  347.  
  348. The functions are sorted by decreasing run-time spent in them.  The functions 
  349. `mcount' and `profil' are part of the profiling aparatus and appear in every 
  350. flat profile; their time gives a measure of the amount of overhead due to 
  351. profiling. 
  352.  
  353. The sampling period estimates the margin of error in each of the time figures. 
  354. A time figure that is not much larger than this is not reliable.  In this 
  355. example, the `self seconds' field for `mcount' might well be `0' or `0.04' in 
  356. another run. See Sampling Error, for a complete discussion. 
  357.  
  358. Here is what the fields in each line mean: 
  359.  
  360.  % time 
  361.            This is the percentage of the total execution time your program 
  362.            spent in this function.  These should all add up to 100%. 
  363.  
  364.  cumulative seconds 
  365.            This is the cumulative total number of seconds the computer spent 
  366.            executing this functions, plus the time spent in all the functions 
  367.            above this one in this table. 
  368.  
  369.  self seconds 
  370.            This is the number of seconds accounted for by this function alone. 
  371.            The flat profile listing is sorted first by this number. 
  372.  
  373.  calls 
  374.            This is the total number of times the function was called.  If the 
  375.            function was never called, or the number of times it was called 
  376.            cannot be determined (probably because the function was not compiled 
  377.            with profiling enabled), the calls field is blank. 
  378.  
  379.  self ms/call 
  380.            This represents the average number of milliseconds spent in this 
  381.            function per call, if this function is profiled.  Otherwise, this 
  382.            field is blank for this function. 
  383.  
  384.  total ms/call 
  385.            This represents the average number of milliseconds spent in this 
  386.            function and its descendants per call, if this function is profiled. 
  387.            Otherwise, this field is blank for this function. 
  388.  
  389.  name 
  390.            This is the name of the function.  The flat profile is sorted by 
  391.            this field alphabetically after the self seconds field is sorted. 
  392.  
  393.  
  394. ΓòÉΓòÉΓòÉ 8. How to Read the Call Graph ΓòÉΓòÉΓòÉ
  395.  
  396. The call graph shows how much time was spent in each function and its children. 
  397. From this information, you can find functions that, while they themselves may 
  398. not have used much time, called other functions that did use unusual amounts of 
  399. time. 
  400.  
  401. Here is a sample call from a small program.  This call came from the same gprof 
  402. run as the flat profile example in the previous chapter. 
  403.  
  404. granularity: each sample hit covers 2 byte(s) for 20.00% of 0.05 seconds
  405.  
  406. index % time    self  children    called     name
  407.                                                  <spontaneous>
  408. [1]    100.0    0.00    0.05                 start [1]
  409.                 0.00    0.05       1/1           main [2]
  410.                 0.00    0.00       1/2           on_exit [28]
  411.                 0.00    0.00       1/1           exit [59]
  412. -----------------------------------------------
  413.                 0.00    0.05       1/1           start [1]
  414. [2]    100.0    0.00    0.05       1         main [2]
  415.                 0.00    0.05       1/1           report [3]
  416. -----------------------------------------------
  417.                 0.00    0.05       1/1           main [2]
  418. [3]    100.0    0.00    0.05       1         report [3]
  419.                 0.00    0.03       8/8           timelocal [6]
  420.                 0.00    0.01       1/1           print [9]
  421.                 0.00    0.01       9/9           fgets [12]
  422.                 0.00    0.00      12/34          strncmp <cycle 1> [40]
  423.                 0.00    0.00       8/8           lookup [20]
  424.                 0.00    0.00       1/1           fopen [21]
  425.                 0.00    0.00       8/8           chewtime [24]
  426.                 0.00    0.00       8/16          skipspace [44]
  427. -----------------------------------------------
  428. [4]     59.8    0.01        0.02       8+472     <cycle 2 as a whole>   l[4]
  429.                 0.01        0.02     244+260         offtime <cycle 2> [7]
  430.                 0.00        0.00     236+1           tzset <cycle 2> [26]
  431. -----------------------------------------------
  432.  
  433. The lines full of dashes divide this table into entries, one for each function. 
  434. Each entry has one or more lines. 
  435.  
  436. In each entry, the primary line is the one that starts with an index number in 
  437. square brackets.  The end of this line says which function the entry is for. 
  438. The preceding lines in the entry describe the callers of this function and the 
  439. following lines describe its subroutines (also called children when we speak of 
  440. the call graph). 
  441.  
  442. The entries are sorted by time spent in the function and its subroutines. 
  443.  
  444. The internal profiling function mcount (see Flat Profile) is never mentioned in 
  445. the call graph. 
  446.  
  447.  Primary                                 Details of the primary line's 
  448.                                          contents. 
  449.  Callers                                 Details of caller-lines' contents. 
  450.  Subroutines                             Details of subroutine-lines' contents. 
  451.  Cycles                                  When there are cycles of recursion, 
  452.            such as a calls b calls a... 
  453.  
  454.  
  455. ΓòÉΓòÉΓòÉ 8.1. The Primary Line ΓòÉΓòÉΓòÉ
  456.  
  457. The primary line in a call graph entry is the line that describes the function 
  458. which the entry is about and gives the overall statistics for this function. 
  459.  
  460. For reference, we repeat the primary line from the entry for function report in 
  461. our main example, together with the heading line that shows the names of the 
  462. fields: 
  463.  
  464. index  % time    self  children called     name
  465. ...
  466. [3]    100.0    0.00    0.05       1         report [3]
  467.  
  468. Here is what the fields in the primary line mean: 
  469.  
  470.  index 
  471.            Entries are numbered with consecutive integers.  Each function 
  472.            therefore has an index number, which appears at the beginning of its 
  473.            primary line. 
  474.  
  475.            Each cross-reference to a function, as a caller or subroutine of 
  476.            another, gives its index number as well as its name.  The index 
  477.            number guides you if you wish to look for the entry for that 
  478.            function. 
  479.  
  480.  % time 
  481.            This is the percentage of the total time that was spent in this 
  482.            function, including time spent in subroutines called from this 
  483.            function. 
  484.  
  485.            The time spent in this function is counted again for the callers of 
  486.            this function.  Therefore, adding up these percentages is 
  487.            meaningless. 
  488.  
  489.  self 
  490.            This is the total amount of time spent in this function.  This 
  491.            should be identical to the number printed in the seconds field for 
  492.            this function in the flat profile. 
  493.  
  494.  children 
  495.            This is the total amount of time spent in the subroutine calls made 
  496.            by this function.  This should be equal to the sum of all the self 
  497.            and children entries of the children listed directly below this 
  498.            function. 
  499.  
  500.  called 
  501.            This is the number of times the function was called. 
  502.  
  503.            If the function called itself recursively, there are two numbers, 
  504.            separated by a `+'.  The first number counts non-recursive calls, 
  505.            and the second counts recursive calls. 
  506.  
  507.            In the example above, the function report was called once from main. 
  508.  
  509.  name 
  510.            This is the name of the current function.  The index number is 
  511.            repeated after it. 
  512.  
  513.            If the function is part of a cycle of recursion, the cycle number is 
  514.            printed between the function's name and the index number (see 
  515.            Cycles).  For example, if function gnurr is part of cycle number 
  516.            one, and has index number twelve, its primary line would be end like 
  517.            this: 
  518.  
  519.                       gnurr <cycle 1> [12]
  520.  
  521.  
  522. ΓòÉΓòÉΓòÉ 8.2. Lines for a Function's Callers ΓòÉΓòÉΓòÉ
  523.  
  524. A function's entry has a line for each function it was called by. These lines' 
  525. fields correspond to the fields of the primary line, but their meanings are 
  526. different because of the difference in context. 
  527.  
  528. For reference, we repeat two lines from the entry for the function report, the 
  529. primary line and one caller-line preceding it, together with the heading line 
  530. that shows the names of the fields: 
  531.  
  532. index  % time    self  children called     name
  533. ...
  534.                 0.00    0.05       1/1           main [2]
  535. [3]    100.0    0.00    0.05       1         report [3]
  536.  
  537. Here are the meanings of the fields in the caller-line for report called from 
  538. main: 
  539.  
  540.  self 
  541.            An estimate of the amount of time spent in report itself when it was 
  542.            called from main. 
  543.  
  544.  children 
  545.            An estimate of the amount of time spent in subroutines of report 
  546.            when report was called from main. 
  547.  
  548.            The sum of the self and children fields is an estimate of the amount 
  549.            of time spent within calls to report from main. 
  550.  
  551.  called 
  552.            Two numbers: the number of times report was called from main, 
  553.            followed by the total number of nonrecursive calls to report from 
  554.            all its callers. 
  555.  
  556.  name and index number 
  557.            The name of the caller of report to which this line applies, 
  558.            followed by the caller's index number. 
  559.  
  560.            Not all functions have entries in the call graph; some options to 
  561.            gprof request the omission of certain functions. When a caller has 
  562.            no entry of its own, it still has caller-lines in the entries of the 
  563.            functions it calls. 
  564.  
  565.            If the caller is part of a recursion cycle, the cycle number is 
  566.            printed between the name and the index number. 
  567.  
  568.  If the identity of the callers of a function cannot be determined, a dummy 
  569.  caller-line is printed which has `<spontaneous>' as the ``caller's name'' and 
  570.  all other fields blank.  This can happen for signal handlers. 
  571.  
  572.  
  573. ΓòÉΓòÉΓòÉ 8.3. Lines for a Function's Subroutines ΓòÉΓòÉΓòÉ
  574.  
  575. A function's entry has a line for each of its subroutines---in other words, a 
  576. line for each other function that it called.  These lines' fields correspond to 
  577. the fields of the primary line, but their meanings are different because of the 
  578. difference in context. 
  579.  
  580. For reference, we repeat two lines from the entry for the function main, the 
  581. primary line and a line for a subroutine, together with the heading line that 
  582. shows the names of the fields: 
  583.  
  584. index  % time    self  children called     name
  585. ...
  586. [2]    100.0    0.00    0.05       1         main [2]
  587.                 0.00    0.05       1/1           report [3]
  588.  
  589. Here are the meanings of the fields in the subroutine-line for main calling 
  590. report: 
  591.  
  592.  self 
  593.            An estimate of the amount of time spent directly within report when 
  594.            report was called from main. 
  595.  
  596.  children 
  597.            An estimate of the amount of time spent in subroutines of report 
  598.            when report was called from main. 
  599.  
  600.            The sum of the self and children fields is an estimate of the total 
  601.            time spent in calls to report from main. 
  602.  
  603.  called 
  604.            Two numbers, the number of calls to report from main followed by the 
  605.            total number of nonrecursive calls to report. 
  606.  
  607.  name 
  608.            The name of the subroutine of main to which this line applies, 
  609.            followed by the subroutine's index number. 
  610.  
  611.            If the caller is part of a recursion cycle, the cycle number is 
  612.            printed between the name and the index number. 
  613.  
  614.  
  615. ΓòÉΓòÉΓòÉ 8.4. How Mutually Recursive Functions Are Described ΓòÉΓòÉΓòÉ
  616.  
  617. The graph may be complicated by the presence of cycles of recursion in the call 
  618. graph.  A cycle exists if a function calls another function that (directly or 
  619. indirectly) calls (or appears to call) the original function.  For example: if 
  620. a calls b, and b calls a, then a and b form a cycle. 
  621.  
  622. Whenever there are call-paths both ways between a pair of functions, they 
  623. belong to the same cycle.  If a and b call each other and b and c call each 
  624. other, all three make one cycle.  Note that even if b only calls a if it was 
  625. not called from a, gprof cannot determine this, so a and b are still considered 
  626. a cycle. 
  627.  
  628. The cycles are numbered with consecutive integers.  When a function belongs to 
  629. a cycle, each time the function name appears in the call graph it is followed 
  630. by `<cycle number>'. 
  631.  
  632. The reason cycles matter is that they make the time values in the call graph 
  633. paradoxical.  The ``time spent in children'' of a should include the time spent 
  634. in its subroutine b and in b's subroutines---but one of b's subroutines is a! 
  635. How much of a's time should be included in the children of a, when a is 
  636. indirectly recursive? 
  637.  
  638. The way gprof resolves this paradox is by creating a single entry for the cycle 
  639. as a whole.  The primary line of this entry describes the total time spent 
  640. directly in the functions of the cycle.  The ``subroutines'' of the cycle are 
  641. the individual functions of the cycle, and all other functions that were called 
  642. directly by them.  The ``callers'' of the cycle are the functions, outside the 
  643. cycle, that called functions in the cycle. 
  644.  
  645. Here is an example portion of a call graph which shows a cycle containing 
  646. functions a and b.  The cycle was entered by a call to a from main; both a and 
  647. b called c. 
  648.  
  649. index  % time    self  children called     name
  650. ----------------------------------------
  651.                  1.77        0    1/1        main [2]
  652. [3]     91.71    1.77        0    1+5    <cycle 1 as a whole> [3]
  653.                  1.02        0    3          b <cycle 1> [4]
  654.                  0.75        0    2          a <cycle 1> [5]
  655. ----------------------------------------
  656.                                   3          a <cycle 1> [5]
  657. [4]     52.85    1.02        0    0      b <cycle 1> [4]
  658.                                   2          a <cycle 1> [5]
  659.                     0        0    3/6        c [6]
  660. ----------------------------------------
  661.                  1.77        0    1/1        main [2]
  662.                                   2          b <cycle 1> [4]
  663. [5]     38.86    0.75        0    1      a <cycle 1> [5]
  664.                                   3          b <cycle 1> [4]
  665.                     0        0    3/6        c [6]
  666. ----------------------------------------
  667.  
  668. (The entire call graph for this program contains in addition an entry for main, 
  669. which calls a, and an entry for c, with callers a and b.) 
  670.  
  671. index  % time    self  children called     name
  672.                                              <spontaneous>
  673. [1]    100.00       0     1.93    0      start [1]
  674.                  0.16     1.77    1/1        main [2]
  675. ----------------------------------------
  676.                  0.16     1.77    1/1        start [1]
  677. [2]    100.00    0.16     1.77    1      main [2]
  678.                  1.77        0    1/1        a <cycle 1> [5]
  679. ----------------------------------------
  680.                  1.77        0    1/1        main [2]
  681. [3]     91.71    1.77        0    1+5    <cycle 1 as a whole> [3]
  682.                  1.02        0    3          b <cycle 1> [4]
  683.                  0.75        0    2          a <cycle 1> [5]
  684.                     0        0    6/6        c [6]
  685. ----------------------------------------
  686.                                   3          a <cycle 1> [5]
  687. [4]     52.85    1.02        0    0      b <cycle 1> [4]
  688.                                   2          a <cycle 1> [5]
  689.                     0        0    3/6        c [6]
  690. ----------------------------------------
  691.                  1.77        0    1/1        main [2]
  692.                                   2          b <cycle 1> [4]
  693. [5]     38.86    0.75        0    1      a <cycle 1> [5]
  694.                                   3          b <cycle 1> [4]
  695.                     0        0    3/6        c [6]
  696. ----------------------------------------
  697.                     0        0    3/6        b <cycle 1> [4]
  698.                     0        0    3/6        a <cycle 1> [5]
  699. [6]      0.00       0        0    6      c [6]
  700. ----------------------------------------
  701.  
  702. The self field of the cycle's primary line is the total time spent in all the 
  703. functions of the cycle.  It equals the sum of the self fields for the 
  704. individual functions in the cycle, found in the entry in the subroutine lines 
  705. for these functions. 
  706.  
  707. The children fields of the cycle's primary line and subroutine lines count only 
  708. subroutines outside the cycle.  Even though a calls b, the time spent in those 
  709. calls to b is not counted in a's children time.  Thus, we do not encounter the 
  710. problem of what to do when the time in those calls to b includes indirect 
  711. recursive calls back to a. 
  712.  
  713. The children field of a caller-line in the cycle's entry estimates the amount 
  714. of time spent in the whole cycle, and its other subroutines, on the times when 
  715. that caller called a function in the cycle. 
  716.  
  717. The calls field in the primary line for the cycle has two numbers: first, the 
  718. number of times functions in the cycle were called by functions outside the 
  719. cycle; second, the number of times they were called by functions in the cycle 
  720. (including times when a function in the cycle calls itself).  This is a 
  721. generalization of the usual split into nonrecursive and recursive calls. 
  722.  
  723. The calls field of a subroutine-line for a cycle member in the cycle's entry 
  724. says how many time that function was called from functions in the cycle.  The 
  725. total of all these is the second number in the primary line's calls field. 
  726.  
  727. In the individual entry for a function in a cycle, the other functions in the 
  728. same cycle can appear as subroutines and as callers.  These lines show how many 
  729. times each function in the cycle called or was called from each other function 
  730. in the cycle.  The self and children fields in these lines are blank because of 
  731. the difficulty of defining meanings for them when recursion is going on. 
  732.  
  733.  
  734. ΓòÉΓòÉΓòÉ 9. Implementation of Profiling ΓòÉΓòÉΓòÉ
  735.  
  736. Profiling works by changing how every function in your program is compiled so 
  737. that when it is called, it will stash away some information about where it was 
  738. called from.  From this, the profiler can figure out what function called it, 
  739. and can count how many times it was called.  This change is made by the 
  740. compiler when your program is compiled with the `-pg' option. 
  741.  
  742. Profiling also involves watching your program as it runs, and keeping a 
  743. histogram of where the program counter happens to be every now and then. 
  744. Typically the program counter is looked at around 100 times per second of run 
  745. time, but the exact frequency may vary from system to system. 
  746.  
  747. A special startup routine allocates memory for the histogram and sets up a 
  748. clock signal handler to make entries in it.  Use of this special startup 
  749. routine is one of the effects of using `gcc ... -pg' to link.  The startup file 
  750. also includes an `exit' function which is responsible for writing the file 
  751. `gmon.out'. 
  752.  
  753. Number-of-calls information for library routines is collected by using a 
  754. special version of the C library.  The programs in it are the same as in the 
  755. usual C library, but they were compiled with `-pg'.  If you link your program 
  756. with `gcc ... -pg', it automatically uses the profiling version of the library. 
  757.  
  758. The output from gprof gives no indication of parts of your program that are 
  759. limited by I/O or swapping bandwidth.  This is because samples of the program 
  760. counter are taken at fixed intervals of run time.  Therefore, the time 
  761. measurements in gprof output say nothing about time that your program was not 
  762. running.  For example, a part of the program that creates so much data that it 
  763. cannot all fit in physical memory at once may run very slowly due to thrashing, 
  764. but gprof will say it uses little time.  On the other hand, sampling by run 
  765. time has the advantage that the amount of load due to other users won't 
  766. directly affect the output you get. 
  767.  
  768.  
  769. ΓòÉΓòÉΓòÉ 10. Statistical Inaccuracy of gprof Output ΓòÉΓòÉΓòÉ
  770.  
  771. The run-time figures that gprof gives you are based on a sampling process, so 
  772. they are subject to statistical inaccuracy.  If a function runs only a small 
  773. amount of time, so that on the average the sampling process ought to catch that 
  774. function in the act only once, there is a pretty good chance it will actually 
  775. find that function zero times, or twice. 
  776.  
  777. By contrast, the number-of-calls figures are derived by counting, not sampling. 
  778. They are completely accurate and will not vary from run to run if your program 
  779. is deterministic. 
  780.  
  781. The sampling period that is printed at the beginning of the flat profile says 
  782. how often samples are taken.  The rule of thumb is that a run-time figure is 
  783. accurate if it is considerably bigger than the sampling period. 
  784.  
  785. The actual amount of error is usually more than one sampling period.  In fact, 
  786. if a value is n times the sampling period, the expected error in it is the 
  787. square-root of n sampling periods.  If the sampling period is 0.01 seconds and 
  788. foo's run-time is 1 second, the expected error in foo's run-time is 0.1 
  789. seconds.  It is likely to vary this much on the average from one profiling run 
  790. to the next. (Sometimes it will vary more.) 
  791.  
  792. This does not mean that a small run-time figure is devoid of information. If 
  793. the program's total run-time is large, a small run-time for one function does 
  794. tell you that that function used an insignificant fraction of the whole 
  795. program's time.  Usually this means it is not worth optimizing. 
  796.  
  797. One way to get more accuracy is to give your program more (but similar) input 
  798. data so it will take longer.  Another way is to combine the data from several 
  799. runs, using the `-s' option of gprof.  Here is how: 
  800.  
  801.    1. Run your program once. 
  802.  
  803.    2. Issue the command `mv gmon.out gmon.sum'. 
  804.  
  805.    3. Run your program again, the same as before. 
  806.  
  807.    4. Merge the new data in `gmon.out' into `gmon.sum' with this command: 
  808.  
  809.             gprof -s executable-file gmon.out gmon.sum
  810.  
  811.    5. Repeat the last two steps as often as you wish. 
  812.  
  813.    6. Analyze the cumulative data using this command: 
  814.  
  815.             gprof executable-file gmon.sum > output-file
  816.  
  817.  
  818. ΓòÉΓòÉΓòÉ 11. Estimating children Times Uses an Assumption ΓòÉΓòÉΓòÉ
  819.  
  820. Some of the figures in the call graph are estimates---for example, the children 
  821. time values and all the the time figures in caller and subroutine lines. 
  822.  
  823. There is no direct information about these measurements in the profile data 
  824. itself.  Instead, gprof estimates them by making an assumption about your 
  825. program that might or might not be true. 
  826.  
  827. The assumption made is that the average time spent in each call to any function 
  828. foo is not correlated with who called foo.  If foo used 5 seconds in all, and 
  829. 2/5 of the calls to foo came from a, then foo contributes 2 seconds to a's 
  830. children time, by assumption. 
  831.  
  832. This assumption is usually true enough, but for some programs it is far from 
  833. true.  Suppose that foo returns very quickly when its argument is zero; suppose 
  834. that a always passes zero as an argument, while other callers of foo pass other 
  835. arguments.  In this program, all the time spent in foo is in the calls from 
  836. callers other than a. But gprof has no way of knowing this; it will blindly and 
  837. incorrectly charge 2 seconds of time in foo to the children of a. 
  838.  
  839. We hope some day to put more complete data into `gmon.out', so that this 
  840. assumption is no longer needed, if we can figure out how.  For the nonce, the 
  841. estimated figures are usually more useful than misleading. 
  842.  
  843.  
  844. ΓòÉΓòÉΓòÉ 12. Incompatibilities with Unix gprof ΓòÉΓòÉΓòÉ
  845.  
  846. gnu gprof and Berkeley Unix gprof use the same data file `gmon.out', and 
  847. provide essentially the same information.  But there are a few differences. 
  848.  
  849.      For a recursive function, Unix gprof lists the function as a parent and 
  850.       as a child, with a calls field that lists the number of recursive calls. 
  851.       gnu gprof omits these lines and puts the number of recursive calls in the 
  852.       primary line. 
  853.  
  854.      When a function is suppressed from the call graph with `-e', gnu gprof 
  855.       still lists it as a subroutine of functions that call it. 
  856.  
  857.      The blurbs, field widths, and output formats are different.  gnu gprof 
  858.       prints blurbs after the tables, so that you can see the tables without 
  859.       skipping the blurbs.