You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Implementation.html 9.3KB

пре 3 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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>Implementation (GNU gprof)</title>
  16. <meta name="description" content="Implementation (GNU gprof)">
  17. <meta name="keywords" content="Implementation (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="Details.html#Details" rel="up" title="Details">
  24. <link href="File-Format.html#File-Format" rel="next" title="File Format">
  25. <link href="Details.html#Details" rel="prev" title="Details">
  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="Implementation"></a>
  56. <div class="header">
  57. <p>
  58. Next: <a href="File-Format.html#File-Format" accesskey="n" rel="next">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>
  59. </div>
  60. <hr>
  61. <a name="Implementation-of-Profiling"></a>
  62. <h3 class="section">9.1 Implementation of Profiling</h3>
  63. <p>Profiling works by changing how every function in your program is compiled
  64. so that when it is called, it will stash away some information about where
  65. it was called from. From this, the profiler can figure out what function
  66. called it, and can count how many times it was called. This change is made
  67. by the compiler when your program is compiled with the &lsquo;<samp>-pg</samp>&rsquo; option,
  68. which causes every function to call <code>mcount</code>
  69. (or <code>_mcount</code>, or <code>__mcount</code>, depending on the OS and compiler)
  70. as one of its first operations.
  71. </p>
  72. <p>The <code>mcount</code> routine, included in the profiling library,
  73. is responsible for recording in an in-memory call graph table
  74. both its parent routine (the child) and its parent&rsquo;s parent. This is
  75. typically done by examining the stack frame to find both
  76. the address of the child, and the return address in the original parent.
  77. Since this is a very machine-dependent operation, <code>mcount</code>
  78. itself is typically a short assembly-language stub routine
  79. that extracts the required
  80. information, and then calls <code>__mcount_internal</code>
  81. (a normal C function) with two arguments&mdash;<code>frompc</code> and <code>selfpc</code>.
  82. <code>__mcount_internal</code> is responsible for maintaining
  83. the in-memory call graph, which records <code>frompc</code>, <code>selfpc</code>,
  84. and the number of times each of these call arcs was traversed.
  85. </p>
  86. <p>GCC Version 2 provides a magical function (<code>__builtin_return_address</code>),
  87. which allows a generic <code>mcount</code> function to extract the
  88. required information from the stack frame. However, on some
  89. architectures, most notably the SPARC, using this builtin can be
  90. very computationally expensive, and an assembly language version
  91. of <code>mcount</code> is used for performance reasons.
  92. </p>
  93. <p>Number-of-calls information for library routines is collected by using a
  94. special version of the C library. The programs in it are the same as in
  95. the usual C library, but they were compiled with &lsquo;<samp>-pg</samp>&rsquo;. If you
  96. link your program with &lsquo;<samp>gcc &hellip; -pg</samp>&rsquo;, it automatically uses the
  97. profiling version of the library.
  98. </p>
  99. <p>Profiling also involves watching your program as it runs, and keeping a
  100. histogram of where the program counter happens to be every now and then.
  101. Typically the program counter is looked at around 100 times per second of
  102. run time, but the exact frequency may vary from system to system.
  103. </p>
  104. <p>This is done is one of two ways. Most UNIX-like operating systems
  105. provide a <code>profil()</code> system call, which registers a memory
  106. array with the kernel, along with a scale
  107. factor that determines how the program&rsquo;s address space maps
  108. into the array.
  109. Typical scaling values cause every 2 to 8 bytes of address space
  110. to map into a single array slot.
  111. On every tick of the system clock
  112. (assuming the profiled program is running), the value of the
  113. program counter is examined and the corresponding slot in
  114. the memory array is incremented. Since this is done in the kernel,
  115. which had to interrupt the process anyway to handle the clock
  116. interrupt, very little additional system overhead is required.
  117. </p>
  118. <p>However, some operating systems, most notably Linux 2.0 (and earlier),
  119. do not provide a <code>profil()</code> system call. On such a system,
  120. arrangements are made for the kernel to periodically deliver
  121. a signal to the process (typically via <code>setitimer()</code>),
  122. which then performs the same operation of examining the
  123. program counter and incrementing a slot in the memory array.
  124. Since this method requires a signal to be delivered to
  125. user space every time a sample is taken, it uses considerably
  126. more overhead than kernel-based profiling. Also, due to the
  127. added delay required to deliver the signal, this method is
  128. less accurate as well.
  129. </p>
  130. <p>A special startup routine allocates memory for the histogram and
  131. either calls <code>profil()</code> or sets up
  132. a clock signal handler.
  133. This routine (<code>monstartup</code>) can be invoked in several ways.
  134. On Linux systems, a special profiling startup file <code>gcrt0.o</code>,
  135. which invokes <code>monstartup</code> before <code>main</code>,
  136. is used instead of the default <code>crt0.o</code>.
  137. Use of this special startup file is one of the effects
  138. of using &lsquo;<samp>gcc &hellip; -pg</samp>&rsquo; to link.
  139. On SPARC systems, no special startup files are used.
  140. Rather, the <code>mcount</code> routine, when it is invoked for
  141. the first time (typically when <code>main</code> is called),
  142. calls <code>monstartup</code>.
  143. </p>
  144. <p>If the compiler&rsquo;s &lsquo;<samp>-a</samp>&rsquo; option was used, basic-block counting
  145. is also enabled. Each object file is then compiled with a static array
  146. of counts, initially zero.
  147. In the executable code, every time a new basic-block begins
  148. (i.e., when an <code>if</code> statement appears), an extra instruction
  149. is inserted to increment the corresponding count in the array.
  150. At compile time, a paired array was constructed that recorded
  151. the starting address of each basic-block. Taken together,
  152. the two arrays record the starting address of every basic-block,
  153. along with the number of times it was executed.
  154. </p>
  155. <p>The profiling library also includes a function (<code>mcleanup</code>) which is
  156. typically registered using <code>atexit()</code> to be called as the
  157. program exits, and is responsible for writing the file <samp>gmon.out</samp>.
  158. Profiling is turned off, various headers are output, and the histogram
  159. is written, followed by the call-graph arcs and the basic-block counts.
  160. </p>
  161. <p>The output from <code>gprof</code> gives no indication of parts of your program that
  162. are limited by I/O or swapping bandwidth. This is because samples of the
  163. program counter are taken at fixed intervals of the program&rsquo;s run time.
  164. Therefore, the
  165. time measurements in <code>gprof</code> output say nothing about time that your
  166. program was not running. For example, a part of the program that creates
  167. so much data that it cannot all fit in physical memory at once may run very
  168. slowly due to thrashing, but <code>gprof</code> will say it uses little time. On
  169. the other hand, sampling by run time has the advantage that the amount of
  170. load due to other users won&rsquo;t directly affect the output you get.
  171. </p>
  172. <hr>
  173. <div class="header">
  174. <p>
  175. Next: <a href="File-Format.html#File-Format" accesskey="n" rel="next">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>
  176. </div>
  177. </body>
  178. </html>