home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / binutils-2.2.1 / gprof / gprof.info < prev    next >
Encoding:
GNU Info File  |  1993-05-18  |  40.1 KB  |  946 lines

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