Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

211 lines
10KB

  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-2020 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 6.5, http://www.gnu.org/software/texinfo/ -->
  13. <head>
  14. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  15. <title>Cycles (GNU gprof)</title>
  16. <meta name="description" content="Cycles (GNU gprof)">
  17. <meta name="keywords" content="Cycles (GNU gprof)">
  18. <meta name="resource-type" content="document">
  19. <meta name="distribution" content="global">
  20. <meta name="Generator" content="makeinfo">
  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.indentedblock {margin-right: 0em}
  30. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  31. blockquote.smallquotation {font-size: smaller}
  32. div.display {margin-left: 3.2em}
  33. div.example {margin-left: 3.2em}
  34. div.lisp {margin-left: 3.2em}
  35. div.smalldisplay {margin-left: 3.2em}
  36. div.smallexample {margin-left: 3.2em}
  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.nolinebreak {white-space: nowrap}
  48. span.roman {font-family: initial; font-weight: normal}
  49. span.sansserif {font-family: sans-serif; font-weight: normal}
  50. ul.no-bullet {list-style: none}
  51. -->
  52. </style>
  53. </head>
  54. <body lang="en">
  55. <a name="Cycles"></a>
  56. <div class="header">
  57. <p>
  58. 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>
  59. </div>
  60. <hr>
  61. <a name="How-Mutually-Recursive-Functions-Are-Described"></a>
  62. <h4 class="subsection">5.2.4 How Mutually Recursive Functions Are Described</h4>
  63. <a name="index-cycle"></a>
  64. <a name="index-recursion-cycle"></a>
  65. <p>The graph may be complicated by the presence of <em>cycles of
  66. recursion</em> in the call graph. A cycle exists if a function calls
  67. another function that (directly or indirectly) calls (or appears to
  68. call) the original function. For example: if <code>a</code> calls <code>b</code>,
  69. and <code>b</code> calls <code>a</code>, then <code>a</code> and <code>b</code> form a cycle.
  70. </p>
  71. <p>Whenever there are call paths both ways between a pair of functions, they
  72. belong to the same cycle. If <code>a</code> and <code>b</code> call each other and
  73. <code>b</code> and <code>c</code> call each other, all three make one cycle. Note that
  74. even if <code>b</code> only calls <code>a</code> if it was not called from <code>a</code>,
  75. <code>gprof</code> cannot determine this, so <code>a</code> and <code>b</code> are still
  76. considered a cycle.
  77. </p>
  78. <p>The cycles are numbered with consecutive integers. When a function
  79. belongs to a cycle, each time the function name appears in the call graph
  80. it is followed by &lsquo;<samp>&lt;cycle <var>number</var>&gt;</samp>&rsquo;.
  81. </p>
  82. <p>The reason cycles matter is that they make the time values in the call
  83. graph paradoxical. The &ldquo;time spent in children&rdquo; of <code>a</code> should
  84. include the time spent in its subroutine <code>b</code> and in <code>b</code>&rsquo;s
  85. subroutines&mdash;but one of <code>b</code>&rsquo;s subroutines is <code>a</code>! How much of
  86. <code>a</code>&rsquo;s time should be included in the children of <code>a</code>, when
  87. <code>a</code> is indirectly recursive?
  88. </p>
  89. <p>The way <code>gprof</code> resolves this paradox is by creating a single entry
  90. for the cycle as a whole. The primary line of this entry describes the
  91. total time spent directly in the functions of the cycle. The
  92. &ldquo;subroutines&rdquo; of the cycle are the individual functions of the cycle, and
  93. all other functions that were called directly by them. The &ldquo;callers&rdquo; of
  94. the cycle are the functions, outside the cycle, that called functions in
  95. the cycle.
  96. </p>
  97. <p>Here is an example portion of a call graph which shows a cycle containing
  98. functions <code>a</code> and <code>b</code>. The cycle was entered by a call to
  99. <code>a</code> from <code>main</code>; both <code>a</code> and <code>b</code> called <code>c</code>.
  100. </p>
  101. <div class="smallexample">
  102. <pre class="smallexample">index % time self children called name
  103. ----------------------------------------
  104. 1.77 0 1/1 main [2]
  105. [3] 91.71 1.77 0 1+5 &lt;cycle 1 as a whole&gt; [3]
  106. 1.02 0 3 b &lt;cycle 1&gt; [4]
  107. 0.75 0 2 a &lt;cycle 1&gt; [5]
  108. ----------------------------------------
  109. 3 a &lt;cycle 1&gt; [5]
  110. [4] 52.85 1.02 0 0 b &lt;cycle 1&gt; [4]
  111. 2 a &lt;cycle 1&gt; [5]
  112. 0 0 3/6 c [6]
  113. ----------------------------------------
  114. 1.77 0 1/1 main [2]
  115. 2 b &lt;cycle 1&gt; [4]
  116. [5] 38.86 0.75 0 1 a &lt;cycle 1&gt; [5]
  117. 3 b &lt;cycle 1&gt; [4]
  118. 0 0 3/6 c [6]
  119. ----------------------------------------
  120. </pre></div>
  121. <p>(The entire call graph for this program contains in addition an entry for
  122. <code>main</code>, which calls <code>a</code>, and an entry for <code>c</code>, with callers
  123. <code>a</code> and <code>b</code>.)
  124. </p>
  125. <div class="smallexample">
  126. <pre class="smallexample">index % time self children called name
  127. &lt;spontaneous&gt;
  128. [1] 100.00 0 1.93 0 start [1]
  129. 0.16 1.77 1/1 main [2]
  130. ----------------------------------------
  131. 0.16 1.77 1/1 start [1]
  132. [2] 100.00 0.16 1.77 1 main [2]
  133. 1.77 0 1/1 a &lt;cycle 1&gt; [5]
  134. ----------------------------------------
  135. 1.77 0 1/1 main [2]
  136. [3] 91.71 1.77 0 1+5 &lt;cycle 1 as a whole&gt; [3]
  137. 1.02 0 3 b &lt;cycle 1&gt; [4]
  138. 0.75 0 2 a &lt;cycle 1&gt; [5]
  139. 0 0 6/6 c [6]
  140. ----------------------------------------
  141. 3 a &lt;cycle 1&gt; [5]
  142. [4] 52.85 1.02 0 0 b &lt;cycle 1&gt; [4]
  143. 2 a &lt;cycle 1&gt; [5]
  144. 0 0 3/6 c [6]
  145. ----------------------------------------
  146. 1.77 0 1/1 main [2]
  147. 2 b &lt;cycle 1&gt; [4]
  148. [5] 38.86 0.75 0 1 a &lt;cycle 1&gt; [5]
  149. 3 b &lt;cycle 1&gt; [4]
  150. 0 0 3/6 c [6]
  151. ----------------------------------------
  152. 0 0 3/6 b &lt;cycle 1&gt; [4]
  153. 0 0 3/6 a &lt;cycle 1&gt; [5]
  154. [6] 0.00 0 0 6 c [6]
  155. ----------------------------------------
  156. </pre></div>
  157. <p>The <code>self</code> field of the cycle&rsquo;s primary line is the total time
  158. spent in all the functions of the cycle. It equals the sum of the
  159. <code>self</code> fields for the individual functions in the cycle, found
  160. in the entry in the subroutine lines for these functions.
  161. </p>
  162. <p>The <code>children</code> fields of the cycle&rsquo;s primary line and subroutine lines
  163. count only subroutines outside the cycle. Even though <code>a</code> calls
  164. <code>b</code>, the time spent in those calls to <code>b</code> is not counted in
  165. <code>a</code>&rsquo;s <code>children</code> time. Thus, we do not encounter the problem of
  166. what to do when the time in those calls to <code>b</code> includes indirect
  167. recursive calls back to <code>a</code>.
  168. </p>
  169. <p>The <code>children</code> field of a caller-line in the cycle&rsquo;s entry estimates
  170. the amount of time spent <em>in the whole cycle</em>, and its other
  171. subroutines, on the times when that caller called a function in the cycle.
  172. </p>
  173. <p>The <code>called</code> field in the primary line for the cycle has two numbers:
  174. first, the number of times functions in the cycle were called by functions
  175. outside the cycle; second, the number of times they were called by
  176. functions in the cycle (including times when a function in the cycle calls
  177. itself). This is a generalization of the usual split into non-recursive and
  178. recursive calls.
  179. </p>
  180. <p>The <code>called</code> field of a subroutine-line for a cycle member in the
  181. cycle&rsquo;s entry says how many time that function was called from functions in
  182. the cycle. The total of all these is the second number in the primary line&rsquo;s
  183. <code>called</code> field.
  184. </p>
  185. <p>In the individual entry for a function in a cycle, the other functions in
  186. the same cycle can appear as subroutines and as callers. These lines show
  187. how many times each function in the cycle called or was called from each other
  188. function in the cycle. The <code>self</code> and <code>children</code> fields in these
  189. lines are blank because of the difficulty of defining meanings for them
  190. when recursion is going on.
  191. </p>
  192. <hr>
  193. <div class="header">
  194. <p>
  195. 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>
  196. </div>
  197. </body>
  198. </html>