blob: 4ec6d8567c99543bd3cb7e15dd3a6eef69382952 [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Copyright (C) 1988-2015 Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "Funding Free Software", the Front-Cover
Texts being (a) (see below), and with the Back-Cover Texts being (b)
(see below). A copy of the license is included in the section entitled
"GNU Free Documentation License".
(a) The FSF's Front-Cover Text is:
A GNU Manual
(b) The FSF's Back-Cover Text is:
You have freedom to copy and modify this GNU Manual, like GNU
software. Copies published by the Free Software Foundation raise
funds for GNU development. -->
<!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
<head>
<title>Using the GNU Compiler Collection (GCC): Gcov and Optimization</title>
<meta name="description" content="Using the GNU Compiler Collection (GCC): Gcov and Optimization">
<meta name="keywords" content="Using the GNU Compiler Collection (GCC): Gcov and Optimization">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Gcov.html#Gcov" rel="up" title="Gcov">
<link href="Gcov-Data-Files.html#Gcov-Data-Files" rel="next" title="Gcov Data Files">
<link href="Invoking-Gcov.html#Invoking-Gcov" rel="prev" title="Invoking Gcov">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {margin-left: 3.2em}
kbd {font-style:oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
-->
</style>
</head>
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Gcov-and-Optimization"></a>
<div class="header">
<p>
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>
</div>
<hr>
<a name="Using-gcov-with-GCC-Optimization"></a>
<h3 class="section">10.3 Using <code>gcov</code> with GCC Optimization</h3>
<p>If you plan to use <code>gcov</code> to help optimize your code, you must
first compile your program with two special GCC options:
&lsquo;<samp>-fprofile-arcs -ftest-coverage</samp>&rsquo;. Aside from that, you can use any
other GCC options; but if you want to prove that every single line
in your program was executed, you should not compile with optimization
at the same time. On some machines the optimizer can eliminate some
simple code lines by combining them with other lines. For example, code
like this:
</p>
<div class="smallexample">
<pre class="smallexample">if (a != b)
c = 1;
else
c = 0;
</pre></div>
<p>can be compiled into one instruction on some machines. In this case,
there is no way for <code>gcov</code> to calculate separate execution counts
for each line because there isn&rsquo;t separate code for each line. Hence
the <code>gcov</code> output looks like this if you compiled the program with
optimization:
</p>
<div class="smallexample">
<pre class="smallexample"> 100: 12:if (a != b)
100: 13: c = 1;
100: 14:else
100: 15: c = 0;
</pre></div>
<p>The output shows that this block of code, combined by optimization,
executed 100 times. In one sense this result is correct, because there
was only one instruction representing all four of these lines. However,
the output does not indicate how many times the result was 0 and how
many times the result was 1.
</p>
<p>Inlineable functions can create unexpected line counts. Line counts are
shown for the source code of the inlineable function, but what is shown
depends on where the function is inlined, or if it is not inlined at all.
</p>
<p>If the function is not inlined, the compiler must emit an out of line
copy of the function, in any object file that needs it. If
<samp>fileA.o</samp> and <samp>fileB.o</samp> both contain out of line bodies of a
particular inlineable function, they will also both contain coverage
counts for that function. When <samp>fileA.o</samp> and <samp>fileB.o</samp> are
linked together, the linker will, on many systems, select one of those
out of line bodies for all calls to that function, and remove or ignore
the other. Unfortunately, it will not remove the coverage counters for
the unused function body. Hence when instrumented, all but one use of
that function will show zero counts.
</p>
<p>If the function is inlined in several places, the block structure in
each location might not be the same. For instance, a condition might
now be calculable at compile time in some instances. Because the
coverage of all the uses of the inline function will be shown for the
same source lines, the line counts themselves might seem inconsistent.
</p>
<p>Long-running applications can use the <code>_gcov_reset</code> and <code>_gcov_dump</code>
facilities to restrict profile collection to the program region of
interest. Calling <code>_gcov_reset(void)</code> will clear all profile counters
to zero, and calling <code>_gcov_dump(void)</code> will cause the profile information
collected at that point to be dumped to <samp>.gcda</samp> output files.
</p>
<hr>
<div class="header">
<p>
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>
</div>
</body>
</html>