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.

Gcov-and-Optimization.html 7.8KB

пре 3 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2020 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Funding Free Software", the Front-Cover
  8. Texts being (a) (see below), and with the Back-Cover Texts being (b)
  9. (see below). A copy of the license is included in the section entitled
  10. "GNU Free Documentation License".
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  20. <title>Gcov and Optimization (Using the GNU Compiler Collection (GCC))</title>
  21. <meta name="description" content="Gcov and Optimization (Using the GNU Compiler Collection (GCC))">
  22. <meta name="keywords" content="Gcov and Optimization (Using the GNU Compiler Collection (GCC))">
  23. <meta name="resource-type" content="document">
  24. <meta name="distribution" content="global">
  25. <meta name="Generator" content="makeinfo">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="Gcov.html#Gcov" rel="up" title="Gcov">
  30. <link href="Gcov-Data-Files.html#Gcov-Data-Files" rel="next" title="Gcov Data Files">
  31. <link href="Invoking-Gcov.html#Invoking-Gcov" rel="prev" title="Invoking Gcov">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.indentedblock {margin-right: 0em}
  36. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  37. blockquote.smallquotation {font-size: smaller}
  38. div.display {margin-left: 3.2em}
  39. div.example {margin-left: 3.2em}
  40. div.lisp {margin-left: 3.2em}
  41. div.smalldisplay {margin-left: 3.2em}
  42. div.smallexample {margin-left: 3.2em}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style: oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nolinebreak {white-space: nowrap}
  54. span.roman {font-family: initial; font-weight: normal}
  55. span.sansserif {font-family: sans-serif; font-weight: normal}
  56. ul.no-bullet {list-style: none}
  57. -->
  58. </style>
  59. </head>
  60. <body lang="en">
  61. <a name="Gcov-and-Optimization"></a>
  62. <div class="header">
  63. <p>
  64. Next: <a href="Gcov-Data-Files.html#Gcov-Data-Files" accesskey="n" rel="next">Gcov Data Files</a>, Previous: <a href="Invoking-Gcov.html#Invoking-Gcov" accesskey="p" rel="prev">Invoking Gcov</a>, Up: <a href="Gcov.html#Gcov" accesskey="u" rel="up">Gcov</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  65. </div>
  66. <hr>
  67. <a name="Using-gcov-with-GCC-Optimization"></a>
  68. <h3 class="section">10.3 Using <code>gcov</code> with GCC Optimization</h3>
  69. <p>If you plan to use <code>gcov</code> to help optimize your code, you must
  70. first compile your program with a special GCC option
  71. &lsquo;<samp>--coverage</samp>&rsquo;. Aside from that, you can use any
  72. other GCC options; but if you want to prove that every single line
  73. in your program was executed, you should not compile with optimization
  74. at the same time. On some machines the optimizer can eliminate some
  75. simple code lines by combining them with other lines. For example, code
  76. like this:
  77. </p>
  78. <div class="smallexample">
  79. <pre class="smallexample">if (a != b)
  80. c = 1;
  81. else
  82. c = 0;
  83. </pre></div>
  84. <p>can be compiled into one instruction on some machines. In this case,
  85. there is no way for <code>gcov</code> to calculate separate execution counts
  86. for each line because there isn&rsquo;t separate code for each line. Hence
  87. the <code>gcov</code> output looks like this if you compiled the program with
  88. optimization:
  89. </p>
  90. <div class="smallexample">
  91. <pre class="smallexample"> 100: 12:if (a != b)
  92. 100: 13: c = 1;
  93. 100: 14:else
  94. 100: 15: c = 0;
  95. </pre></div>
  96. <p>The output shows that this block of code, combined by optimization,
  97. executed 100 times. In one sense this result is correct, because there
  98. was only one instruction representing all four of these lines. However,
  99. the output does not indicate how many times the result was 0 and how
  100. many times the result was 1.
  101. </p>
  102. <p>Inlineable functions can create unexpected line counts. Line counts are
  103. shown for the source code of the inlineable function, but what is shown
  104. depends on where the function is inlined, or if it is not inlined at all.
  105. </p>
  106. <p>If the function is not inlined, the compiler must emit an out of line
  107. copy of the function, in any object file that needs it. If
  108. <samp>fileA.o</samp> and <samp>fileB.o</samp> both contain out of line bodies of a
  109. particular inlineable function, they will also both contain coverage
  110. counts for that function. When <samp>fileA.o</samp> and <samp>fileB.o</samp> are
  111. linked together, the linker will, on many systems, select one of those
  112. out of line bodies for all calls to that function, and remove or ignore
  113. the other. Unfortunately, it will not remove the coverage counters for
  114. the unused function body. Hence when instrumented, all but one use of
  115. that function will show zero counts.
  116. </p>
  117. <p>If the function is inlined in several places, the block structure in
  118. each location might not be the same. For instance, a condition might
  119. now be calculable at compile time in some instances. Because the
  120. coverage of all the uses of the inline function will be shown for the
  121. same source lines, the line counts themselves might seem inconsistent.
  122. </p>
  123. <p>Long-running applications can use the <code>__gcov_reset</code> and <code>__gcov_dump</code>
  124. facilities to restrict profile collection to the program region of
  125. interest. Calling <code>__gcov_reset(void)</code> will clear all profile counters
  126. to zero, and calling <code>__gcov_dump(void)</code> will cause the profile information
  127. collected at that point to be dumped to <samp>.gcda</samp> output files.
  128. Instrumented applications use a static destructor with priority 99
  129. to invoke the <code>__gcov_dump</code> function. Thus <code>__gcov_dump</code>
  130. is executed after all user defined static destructors,
  131. as well as handlers registered with <code>atexit</code>.
  132. If an executable loads a dynamic shared object via dlopen functionality,
  133. <samp>-Wl,--dynamic-list-data</samp> is needed to dump all profile data.
  134. </p>
  135. <p>Profiling run-time library reports various errors related to profile
  136. manipulation and profile saving. Errors are printed into standard error output
  137. or &lsquo;<samp>GCOV_ERROR_FILE</samp>&rsquo; file, if environment variable is used.
  138. In order to terminate immediately after an errors occurs
  139. set &lsquo;<samp>GCOV_EXIT_AT_ERROR</samp>&rsquo; environment variable.
  140. That can help users to find profile clashing which leads
  141. to a misleading profile.
  142. </p>
  143. <hr>
  144. <div class="header">
  145. <p>
  146. Next: <a href="Gcov-Data-Files.html#Gcov-Data-Files" accesskey="n" rel="next">Gcov Data Files</a>, Previous: <a href="Invoking-Gcov.html#Invoking-Gcov" accesskey="p" rel="prev">Invoking Gcov</a>, Up: <a href="Gcov.html#Gcov" accesskey="u" rel="up">Gcov</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  147. </div>
  148. </body>
  149. </html>