Cycles.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- This file documents the gprof profiler of the GNU system.
  4. Copyright (C) 1988-2017 Free Software Foundation, Inc.
  5. Permission is granted to copy, distribute and/or modify this document
  6. under the terms of the GNU Free Documentation License, Version 1.3
  7. or any later version published by the Free Software Foundation;
  8. with no Invariant Sections, with no Front-Cover Texts, and with no
  9. Back-Cover Texts. A copy of the license is included in the
  10. section entitled "GNU Free Documentation License".
  11. -->
  12. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  13. <head>
  14. <title>GNU gprof: Cycles</title>
  15. <meta name="description" content="GNU gprof: Cycles">
  16. <meta name="keywords" content="GNU gprof: Cycles">
  17. <meta name="resource-type" content="document">
  18. <meta name="distribution" content="global">
  19. <meta name="Generator" content="makeinfo">
  20. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  21. <link href="index.html#Top" rel="start" title="Top">
  22. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  23. <link href="Call-Graph.html#Call-Graph" rel="up" title="Call Graph">
  24. <link href="Line_002dby_002dline.html#Line_002dby_002dline" rel="next" title="Line-by-line">
  25. <link href="Subroutines.html#Subroutines" rel="prev" title="Subroutines">
  26. <style type="text/css">
  27. <!--
  28. a.summary-letter {text-decoration: none}
  29. blockquote.smallquotation {font-size: smaller}
  30. div.display {margin-left: 3.2em}
  31. div.example {margin-left: 3.2em}
  32. div.indentedblock {margin-left: 3.2em}
  33. div.lisp {margin-left: 3.2em}
  34. div.smalldisplay {margin-left: 3.2em}
  35. div.smallexample {margin-left: 3.2em}
  36. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  37. div.smalllisp {margin-left: 3.2em}
  38. kbd {font-style:oblique}
  39. pre.display {font-family: inherit}
  40. pre.format {font-family: inherit}
  41. pre.menu-comment {font-family: serif}
  42. pre.menu-preformatted {font-family: serif}
  43. pre.smalldisplay {font-family: inherit; font-size: smaller}
  44. pre.smallexample {font-size: smaller}
  45. pre.smallformat {font-family: inherit; font-size: smaller}
  46. pre.smalllisp {font-size: smaller}
  47. span.nocodebreak {white-space:nowrap}
  48. span.nolinebreak {white-space:nowrap}
  49. span.roman {font-family:serif; font-weight:normal}
  50. span.sansserif {font-family:sans-serif; font-weight:normal}
  51. ul.no-bullet {list-style: none}
  52. -->
  53. </style>
  54. </head>
  55. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  56. <a name="Cycles"></a>
  57. <div class="header">
  58. <p>
  59. Previous: <a href="Subroutines.html#Subroutines" accesskey="p" rel="prev">Subroutines</a>, Up: <a href="Call-Graph.html#Call-Graph" accesskey="u" rel="up">Call Graph</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>]</p>
  60. </div>
  61. <hr>
  62. <a name="How-Mutually-Recursive-Functions-Are-Described"></a>
  63. <h4 class="subsection">5.2.4 How Mutually Recursive Functions Are Described</h4>
  64. <a name="index-cycle"></a>
  65. <a name="index-recursion-cycle"></a>
  66. <p>The graph may be complicated by the presence of <em>cycles of
  67. recursion</em> in the call graph. A cycle exists if a function calls
  68. another function that (directly or indirectly) calls (or appears to
  69. call) the original function. For example: if <code>a</code> calls <code>b</code>,
  70. and <code>b</code> calls <code>a</code>, then <code>a</code> and <code>b</code> form a cycle.
  71. </p>
  72. <p>Whenever there are call paths both ways between a pair of functions, they
  73. belong to the same cycle. If <code>a</code> and <code>b</code> call each other and
  74. <code>b</code> and <code>c</code> call each other, all three make one cycle. Note that
  75. even if <code>b</code> only calls <code>a</code> if it was not called from <code>a</code>,
  76. <code>gprof</code> cannot determine this, so <code>a</code> and <code>b</code> are still
  77. considered a cycle.
  78. </p>
  79. <p>The cycles are numbered with consecutive integers. When a function
  80. belongs to a cycle, each time the function name appears in the call graph
  81. it is followed by &lsquo;<samp>&lt;cycle <var>number</var>&gt;</samp>&rsquo;.
  82. </p>
  83. <p>The reason cycles matter is that they make the time values in the call
  84. graph paradoxical. The &ldquo;time spent in children&rdquo; of <code>a</code> should
  85. include the time spent in its subroutine <code>b</code> and in <code>b</code>&rsquo;s
  86. subroutines&mdash;but one of <code>b</code>&rsquo;s subroutines is <code>a</code>! How much of
  87. <code>a</code>&rsquo;s time should be included in the children of <code>a</code>, when
  88. <code>a</code> is indirectly recursive?
  89. </p>
  90. <p>The way <code>gprof</code> resolves this paradox is by creating a single entry
  91. for the cycle as a whole. The primary line of this entry describes the
  92. total time spent directly in the functions of the cycle. The
  93. &ldquo;subroutines&rdquo; of the cycle are the individual functions of the cycle, and
  94. all other functions that were called directly by them. The &ldquo;callers&rdquo; of
  95. the cycle are the functions, outside the cycle, that called functions in
  96. the cycle.
  97. </p>
  98. <p>Here is an example portion of a call graph which shows a cycle containing
  99. functions <code>a</code> and <code>b</code>. The cycle was entered by a call to
  100. <code>a</code> from <code>main</code>; both <code>a</code> and <code>b</code> called <code>c</code>.
  101. </p>
  102. <div class="smallexample">
  103. <pre class="smallexample">index % time self children called name
  104. ----------------------------------------
  105. 1.77 0 1/1 main [2]
  106. [3] 91.71 1.77 0 1+5 &lt;cycle 1 as a whole&gt; [3]
  107. 1.02 0 3 b &lt;cycle 1&gt; [4]
  108. 0.75 0 2 a &lt;cycle 1&gt; [5]
  109. ----------------------------------------
  110. 3 a &lt;cycle 1&gt; [5]
  111. [4] 52.85 1.02 0 0 b &lt;cycle 1&gt; [4]
  112. 2 a &lt;cycle 1&gt; [5]
  113. 0 0 3/6 c [6]
  114. ----------------------------------------
  115. 1.77 0 1/1 main [2]
  116. 2 b &lt;cycle 1&gt; [4]
  117. [5] 38.86 0.75 0 1 a &lt;cycle 1&gt; [5]
  118. 3 b &lt;cycle 1&gt; [4]
  119. 0 0 3/6 c [6]
  120. ----------------------------------------
  121. </pre></div>
  122. <p>(The entire call graph for this program contains in addition an entry for
  123. <code>main</code>, which calls <code>a</code>, and an entry for <code>c</code>, with callers
  124. <code>a</code> and <code>b</code>.)
  125. </p>
  126. <div class="smallexample">
  127. <pre class="smallexample">index % time self children called name
  128. &lt;spontaneous&gt;
  129. [1] 100.00 0 1.93 0 start [1]
  130. 0.16 1.77 1/1 main [2]
  131. ----------------------------------------
  132. 0.16 1.77 1/1 start [1]
  133. [2] 100.00 0.16 1.77 1 main [2]
  134. 1.77 0 1/1 a &lt;cycle 1&gt; [5]
  135. ----------------------------------------
  136. 1.77 0 1/1 main [2]
  137. [3] 91.71 1.77 0 1+5 &lt;cycle 1 as a whole&gt; [3]
  138. 1.02 0 3 b &lt;cycle 1&gt; [4]
  139. 0.75 0 2 a &lt;cycle 1&gt; [5]
  140. 0 0 6/6 c [6]
  141. ----------------------------------------
  142. 3 a &lt;cycle 1&gt; [5]
  143. [4] 52.85 1.02 0 0 b &lt;cycle 1&gt; [4]
  144. 2 a &lt;cycle 1&gt; [5]
  145. 0 0 3/6 c [6]
  146. ----------------------------------------
  147. 1.77 0 1/1 main [2]
  148. 2 b &lt;cycle 1&gt; [4]
  149. [5] 38.86 0.75 0 1 a &lt;cycle 1&gt; [5]
  150. 3 b &lt;cycle 1&gt; [4]
  151. 0 0 3/6 c [6]
  152. ----------------------------------------
  153. 0 0 3/6 b &lt;cycle 1&gt; [4]
  154. 0 0 3/6 a &lt;cycle 1&gt; [5]
  155. [6] 0.00 0 0 6 c [6]
  156. ----------------------------------------
  157. </pre></div>
  158. <p>The <code>self</code> field of the cycle&rsquo;s primary line is the total time
  159. spent in all the functions of the cycle. It equals the sum of the
  160. <code>self</code> fields for the individual functions in the cycle, found
  161. in the entry in the subroutine lines for these functions.
  162. </p>
  163. <p>The <code>children</code> fields of the cycle&rsquo;s primary line and subroutine lines
  164. count only subroutines outside the cycle. Even though <code>a</code> calls
  165. <code>b</code>, the time spent in those calls to <code>b</code> is not counted in
  166. <code>a</code>&rsquo;s <code>children</code> time. Thus, we do not encounter the problem of
  167. what to do when the time in those calls to <code>b</code> includes indirect
  168. recursive calls back to <code>a</code>.
  169. </p>
  170. <p>The <code>children</code> field of a caller-line in the cycle&rsquo;s entry estimates
  171. the amount of time spent <em>in the whole cycle</em>, and its other
  172. subroutines, on the times when that caller called a function in the cycle.
  173. </p>
  174. <p>The <code>called</code> field in the primary line for the cycle has two numbers:
  175. first, the number of times functions in the cycle were called by functions
  176. outside the cycle; second, the number of times they were called by
  177. functions in the cycle (including times when a function in the cycle calls
  178. itself). This is a generalization of the usual split into non-recursive and
  179. recursive calls.
  180. </p>
  181. <p>The <code>called</code> field of a subroutine-line for a cycle member in the
  182. cycle&rsquo;s entry says how many time that function was called from functions in
  183. the cycle. The total of all these is the second number in the primary line&rsquo;s
  184. <code>called</code> field.
  185. </p>
  186. <p>In the individual entry for a function in a cycle, the other functions in
  187. the same cycle can appear as subroutines and as callers. These lines show
  188. how many times each function in the cycle called or was called from each other
  189. function in the cycle. The <code>self</code> and <code>children</code> fields in these
  190. lines are blank because of the difficulty of defining meanings for them
  191. when recursion is going on.
  192. </p>
  193. <hr>
  194. <div class="header">
  195. <p>
  196. Previous: <a href="Subroutines.html#Subroutines" accesskey="p" rel="prev">Subroutines</a>, Up: <a href="Call-Graph.html#Call-Graph" accesskey="u" rel="up">Call Graph</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>]</p>
  197. </div>
  198. </body>
  199. </html>