PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/share/doc/gprof/gprof.html/Internals.html

https://gitlab.com/infected_/linaro_aarch64-linux-android-5.3.x
HTML | 252 lines | 206 code | 8 blank | 38 comment | 0 complexity | 06133226bc6d14a70c3489e8018f1112 MD5 | raw file
  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-2014 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: Internals</title>
  15. <meta name="description" content="GNU gprof: Internals">
  16. <meta name="keywords" content="GNU gprof: Internals">
  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="Details.html#Details" rel="up" title="Details">
  24. <link href="Debugging.html#Debugging" rel="next" title="Debugging">
  25. <link href="File-Format.html#File-Format" rel="prev" title="File Format">
  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="Internals"></a>
  57. <div class="header">
  58. <p>
  59. Next: <a href="Debugging.html#Debugging" accesskey="n" rel="next">Debugging</a>, Previous: <a href="File-Format.html#File-Format" accesskey="p" rel="prev">File Format</a>, Up: <a href="Details.html#Details" accesskey="u" rel="up">Details</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>]</p>
  60. </div>
  61. <hr>
  62. <a name="gprof_0027s-Internal-Operation"></a>
  63. <h3 class="section">9.3 <code>gprof</code>&rsquo;s Internal Operation</h3>
  64. <p>Like most programs, <code>gprof</code> begins by processing its options.
  65. During this stage, it may building its symspec list
  66. (<code>sym_ids.c:sym_id_add</code>), if
  67. options are specified which use symspecs.
  68. <code>gprof</code> maintains a single linked list of symspecs,
  69. which will eventually get turned into 12 symbol tables,
  70. organized into six include/exclude pairs&mdash;one
  71. pair each for the flat profile (INCL_FLAT/EXCL_FLAT),
  72. the call graph arcs (INCL_ARCS/EXCL_ARCS),
  73. printing in the call graph (INCL_GRAPH/EXCL_GRAPH),
  74. timing propagation in the call graph (INCL_TIME/EXCL_TIME),
  75. the annotated source listing (INCL_ANNO/EXCL_ANNO),
  76. and the execution count listing (INCL_EXEC/EXCL_EXEC).
  77. </p>
  78. <p>After option processing, <code>gprof</code> finishes
  79. building the symspec list by adding all the symspecs in
  80. <code>default_excluded_list</code> to the exclude lists
  81. EXCL_TIME and EXCL_GRAPH, and if line-by-line profiling is specified,
  82. EXCL_FLAT as well.
  83. These default excludes are not added to EXCL_ANNO, EXCL_ARCS, and EXCL_EXEC.
  84. </p>
  85. <p>Next, the BFD library is called to open the object file,
  86. verify that it is an object file,
  87. and read its symbol table (<code>core.c:core_init</code>),
  88. using <code>bfd_canonicalize_symtab</code> after mallocing
  89. an appropriately sized array of symbols. At this point,
  90. function mappings are read (if the &lsquo;<samp>--file-ordering</samp>&rsquo; option
  91. has been specified), and the core text space is read into
  92. memory (if the &lsquo;<samp>-c</samp>&rsquo; option was given).
  93. </p>
  94. <p><code>gprof</code>&rsquo;s own symbol table, an array of Sym structures,
  95. is now built.
  96. This is done in one of two ways, by one of two routines, depending
  97. on whether line-by-line profiling (&lsquo;<samp>-l</samp>&rsquo; option) has been
  98. enabled.
  99. For normal profiling, the BFD canonical symbol table is scanned.
  100. For line-by-line profiling, every
  101. text space address is examined, and a new symbol table entry
  102. gets created every time the line number changes.
  103. In either case, two passes are made through the symbol
  104. table&mdash;one to count the size of the symbol table required,
  105. and the other to actually read the symbols. In between the
  106. two passes, a single array of type <code>Sym</code> is created of
  107. the appropriate length.
  108. Finally, <code>symtab.c:symtab_finalize</code>
  109. is called to sort the symbol table and remove duplicate entries
  110. (entries with the same memory address).
  111. </p>
  112. <p>The symbol table must be a contiguous array for two reasons.
  113. First, the <code>qsort</code> library function (which sorts an array)
  114. will be used to sort the symbol table.
  115. Also, the symbol lookup routine (<code>symtab.c:sym_lookup</code>),
  116. which finds symbols
  117. based on memory address, uses a binary search algorithm
  118. which requires the symbol table to be a sorted array.
  119. Function symbols are indicated with an <code>is_func</code> flag.
  120. Line number symbols have no special flags set.
  121. Additionally, a symbol can have an <code>is_static</code> flag
  122. to indicate that it is a local symbol.
  123. </p>
  124. <p>With the symbol table read, the symspecs can now be translated
  125. into Syms (<code>sym_ids.c:sym_id_parse</code>). Remember that a single
  126. symspec can match multiple symbols.
  127. An array of symbol tables
  128. (<code>syms</code>) is created, each entry of which is a symbol table
  129. of Syms to be included or excluded from a particular listing.
  130. The master symbol table and the symspecs are examined by nested
  131. loops, and every symbol that matches a symspec is inserted
  132. into the appropriate syms table. This is done twice, once to
  133. count the size of each required symbol table, and again to build
  134. the tables, which have been malloced between passes.
  135. From now on, to determine whether a symbol is on an include
  136. or exclude symspec list, <code>gprof</code> simply uses its
  137. standard symbol lookup routine on the appropriate table
  138. in the <code>syms</code> array.
  139. </p>
  140. <p>Now the profile data file(s) themselves are read
  141. (<code>gmon_io.c:gmon_out_read</code>),
  142. first by checking for a new-style &lsquo;<samp>gmon.out</samp>&rsquo; header,
  143. then assuming this is an old-style BSD &lsquo;<samp>gmon.out</samp>&rsquo;
  144. if the magic number test failed.
  145. </p>
  146. <p>New-style histogram records are read by <code>hist.c:hist_read_rec</code>.
  147. For the first histogram record, allocate a memory array to hold
  148. all the bins, and read them in.
  149. When multiple profile data files (or files with multiple histogram
  150. records) are read, the memory ranges of each pair of histogram records
  151. must be either equal, or non-overlapping. For each pair of histogram
  152. records, the resolution (memory region size divided by the number of
  153. bins) must be the same. The time unit must be the same for all
  154. histogram records. If the above containts are met, all histograms
  155. for the same memory range are merged.
  156. </p>
  157. <p>As each call graph record is read (<code>call_graph.c:cg_read_rec</code>),
  158. the parent and child addresses
  159. are matched to symbol table entries, and a call graph arc is
  160. created by <code>cg_arcs.c:arc_add</code>, unless the arc fails a symspec
  161. check against INCL_ARCS/EXCL_ARCS. As each arc is added,
  162. a linked list is maintained of the parent&rsquo;s child arcs, and of the child&rsquo;s
  163. parent arcs.
  164. Both the child&rsquo;s call count and the arc&rsquo;s call count are
  165. incremented by the record&rsquo;s call count.
  166. </p>
  167. <p>Basic-block records are read (<code>basic_blocks.c:bb_read_rec</code>),
  168. but only if line-by-line profiling has been selected.
  169. Each basic-block address is matched to a corresponding line
  170. symbol in the symbol table, and an entry made in the symbol&rsquo;s
  171. bb_addr and bb_calls arrays. Again, if multiple basic-block
  172. records are present for the same address, the call counts
  173. are cumulative.
  174. </p>
  175. <p>A gmon.sum file is dumped, if requested (<code>gmon_io.c:gmon_out_write</code>).
  176. </p>
  177. <p>If histograms were present in the data files, assign them to symbols
  178. (<code>hist.c:hist_assign_samples</code>) by iterating over all the sample
  179. bins and assigning them to symbols. Since the symbol table
  180. is sorted in order of ascending memory addresses, we can
  181. simple follow along in the symbol table as we make our pass
  182. over the sample bins.
  183. This step includes a symspec check against INCL_FLAT/EXCL_FLAT.
  184. Depending on the histogram
  185. scale factor, a sample bin may span multiple symbols,
  186. in which case a fraction of the sample count is allocated
  187. to each symbol, proportional to the degree of overlap.
  188. This effect is rare for normal profiling, but overlaps
  189. are more common during line-by-line profiling, and can
  190. cause each of two adjacent lines to be credited with half
  191. a hit, for example.
  192. </p>
  193. <p>If call graph data is present, <code>cg_arcs.c:cg_assemble</code> is called.
  194. First, if &lsquo;<samp>-c</samp>&rsquo; was specified, a machine-dependent
  195. routine (<code>find_call</code>) scans through each symbol&rsquo;s machine code,
  196. looking for subroutine call instructions, and adding them
  197. to the call graph with a zero call count.
  198. A topological sort is performed by depth-first numbering
  199. all the symbols (<code>cg_dfn.c:cg_dfn</code>), so that
  200. children are always numbered less than their parents,
  201. then making a array of pointers into the symbol table and sorting it into
  202. numerical order, which is reverse topological
  203. order (children appear before parents).
  204. Cycles are also detected at this point, all members
  205. of which are assigned the same topological number.
  206. Two passes are now made through this sorted array of symbol pointers.
  207. The first pass, from end to beginning (parents to children),
  208. computes the fraction of child time to propagate to each parent
  209. and a print flag.
  210. The print flag reflects symspec handling of INCL_GRAPH/EXCL_GRAPH,
  211. with a parent&rsquo;s include or exclude (print or no print) property
  212. being propagated to its children, unless they themselves explicitly appear
  213. in INCL_GRAPH or EXCL_GRAPH.
  214. A second pass, from beginning to end (children to parents) actually
  215. propagates the timings along the call graph, subject
  216. to a check against INCL_TIME/EXCL_TIME.
  217. With the print flag, fractions, and timings now stored in the symbol
  218. structures, the topological sort array is now discarded, and a
  219. new array of pointers is assembled, this time sorted by propagated time.
  220. </p>
  221. <p>Finally, print the various outputs the user requested, which is now fairly
  222. straightforward. The call graph (<code>cg_print.c:cg_print</code>) and
  223. flat profile (<code>hist.c:hist_print</code>) are regurgitations of values
  224. already computed. The annotated source listing
  225. (<code>basic_blocks.c:print_annotated_source</code>) uses basic-block
  226. information, if present, to label each line of code with call counts,
  227. otherwise only the function call counts are presented.
  228. </p>
  229. <p>The function ordering code is marginally well documented
  230. in the source code itself (<code>cg_print.c</code>). Basically,
  231. the functions with the most use and the most parents are
  232. placed first, followed by other functions with the most use,
  233. followed by lower use functions, followed by unused functions
  234. at the end.
  235. </p>
  236. <hr>
  237. <div class="header">
  238. <p>
  239. Next: <a href="Debugging.html#Debugging" accesskey="n" rel="next">Debugging</a>, Previous: <a href="File-Format.html#File-Format" accesskey="p" rel="prev">File Format</a>, Up: <a href="Details.html#Details" accesskey="u" rel="up">Details</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>]</p>
  240. </div>
  241. </body>
  242. </html>