home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Atari / Gnu / gpfbin09.zoo / gprof.txt < prev    next >
Text File  |  1991-06-27  |  37KB  |  867 lines

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