home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / binutils-2.7-src.tgz / tar.out / fsf / binutils / gprof / gprof.info (.txt) < prev    next >
GNU Info File  |  1996-09-28  |  43KB  |  771 lines

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