Gcov-and-Optimization.html 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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-2017 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 5.2, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <title>Using the GNU Compiler Collection (GCC): Gcov and Optimization</title>
  20. <meta name="description" content="Using the GNU Compiler Collection (GCC): Gcov and Optimization">
  21. <meta name="keywords" content="Using the GNU Compiler Collection (GCC): Gcov and Optimization">
  22. <meta name="resource-type" content="document">
  23. <meta name="distribution" content="global">
  24. <meta name="Generator" content="makeinfo">
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  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.smallquotation {font-size: smaller}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.indentedblock {margin-left: 3.2em}
  39. div.lisp {margin-left: 3.2em}
  40. div.smalldisplay {margin-left: 3.2em}
  41. div.smallexample {margin-left: 3.2em}
  42. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  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.nocodebreak {white-space:nowrap}
  54. span.nolinebreak {white-space:nowrap}
  55. span.roman {font-family:serif; font-weight:normal}
  56. span.sansserif {font-family:sans-serif; font-weight:normal}
  57. ul.no-bullet {list-style: none}
  58. -->
  59. </style>
  60. </head>
  61. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  62. <a name="Gcov-and-Optimization"></a>
  63. <div class="header">
  64. <p>
  65. 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>
  66. </div>
  67. <hr>
  68. <a name="Using-gcov-with-GCC-Optimization"></a>
  69. <h3 class="section">10.3 Using <code>gcov</code> with GCC Optimization</h3>
  70. <p>If you plan to use <code>gcov</code> to help optimize your code, you must
  71. first compile your program with two special GCC options:
  72. &lsquo;<samp>-fprofile-arcs -ftest-coverage</samp>&rsquo;. Aside from that, you can use any
  73. other GCC options; but if you want to prove that every single line
  74. in your program was executed, you should not compile with optimization
  75. at the same time. On some machines the optimizer can eliminate some
  76. simple code lines by combining them with other lines. For example, code
  77. like this:
  78. </p>
  79. <div class="smallexample">
  80. <pre class="smallexample">if (a != b)
  81. c = 1;
  82. else
  83. c = 0;
  84. </pre></div>
  85. <p>can be compiled into one instruction on some machines. In this case,
  86. there is no way for <code>gcov</code> to calculate separate execution counts
  87. for each line because there isn&rsquo;t separate code for each line. Hence
  88. the <code>gcov</code> output looks like this if you compiled the program with
  89. optimization:
  90. </p>
  91. <div class="smallexample">
  92. <pre class="smallexample"> 100: 12:if (a != b)
  93. 100: 13: c = 1;
  94. 100: 14:else
  95. 100: 15: c = 0;
  96. </pre></div>
  97. <p>The output shows that this block of code, combined by optimization,
  98. executed 100 times. In one sense this result is correct, because there
  99. was only one instruction representing all four of these lines. However,
  100. the output does not indicate how many times the result was 0 and how
  101. many times the result was 1.
  102. </p>
  103. <p>Inlineable functions can create unexpected line counts. Line counts are
  104. shown for the source code of the inlineable function, but what is shown
  105. depends on where the function is inlined, or if it is not inlined at all.
  106. </p>
  107. <p>If the function is not inlined, the compiler must emit an out of line
  108. copy of the function, in any object file that needs it. If
  109. <samp>fileA.o</samp> and <samp>fileB.o</samp> both contain out of line bodies of a
  110. particular inlineable function, they will also both contain coverage
  111. counts for that function. When <samp>fileA.o</samp> and <samp>fileB.o</samp> are
  112. linked together, the linker will, on many systems, select one of those
  113. out of line bodies for all calls to that function, and remove or ignore
  114. the other. Unfortunately, it will not remove the coverage counters for
  115. the unused function body. Hence when instrumented, all but one use of
  116. that function will show zero counts.
  117. </p>
  118. <p>If the function is inlined in several places, the block structure in
  119. each location might not be the same. For instance, a condition might
  120. now be calculable at compile time in some instances. Because the
  121. coverage of all the uses of the inline function will be shown for the
  122. same source lines, the line counts themselves might seem inconsistent.
  123. </p>
  124. <p>Long-running applications can use the <code>__gcov_reset</code> and <code>__gcov_dump</code>
  125. facilities to restrict profile collection to the program region of
  126. interest. Calling <code>__gcov_reset(void)</code> will clear all profile counters
  127. to zero, and calling <code>__gcov_dump(void)</code> will cause the profile information
  128. collected at that point to be dumped to <samp>.gcda</samp> output files.
  129. Instrumented applications use a static destructor with priority 99
  130. to invoke the <code>__gcov_dump</code> function. Thus <code>__gcov_dump</code>
  131. is executed after all user defined static destructors,
  132. as well as handlers registered with <code>atexit</code>.
  133. If an executable loads a dynamic shared object via dlopen functionality,
  134. <samp>-Wl,--dynamic-list-data</samp> is needed to dump all profile data.
  135. </p>
  136. <hr>
  137. <div class="header">
  138. <p>
  139. 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>
  140. </div>
  141. </body>
  142. </html>