Coverage Measurement and Profiling
Like gcov, gprof requires that your programs be compiled with particular options so GCC can instrument your code properly. To use gprof, you must use the -pg option. By default, this profiles each function executed. To profile line by line, also use the standard -g debugging option to insert debugging symbols that gprof can use to create line-by-line reports.
When your program is run, information about function calls and time spent is kept in memory. At program exit, this information is written to a file called gmon.out. gprof uses this file to create profiling reports. Unlike coverage monitoring, which saves data in separate files for each source file, one data file is created for the entire program. Instead of saving data in the same directory as the source file, gmon.out is written to the correct working directory at the time the program exits. So beware of chdir() calls.
By default, the profiling data counts the number of times each function is called, along with the function from which it was called. Runtime data is collected by a sampling process, where the program is examined every sample period, and the function currently executing is recorded. gprof interprets this sampling to report how much time was spent on each area of the code.
Because this is a statistical sampling, it is subject to statistical error. See the gprof manual for a detailed discussion of the error calculation. In short, the times reported by gprof may not be exact. Furthermore, if a function is executed for a relatively small amount of time, it may be missed during the sampling and show 0.0 execution time. This is not a problem for performance profiling. The information that a particular function or block of code did not run for enough time to be sampled tells you that you probably don't want to spend much time optimizing it.
Statistical errors are a problem, however, if you also want to use gprof to show statement or branch coverage in addition to performance data. For exact counts of how many times each line of code has been executed, enable basic block counting by using the -a option at compile time. This adds instrumentation to the code to count the execution of each block of code. It also allows gprof to show exactly how many times each line of code was executed, even if zero time is shown by way of sampling.
It is possible to combine multiple gmon.out files in order to build up measurements over many runs of a program. This is done with the gprof -s command, which takes any number of gmon.out files and creates a new gmon.sum with the combined data. It is possible to add profiling data incrementally by specifying gmon.sum as an input file.
I originally intended to reuse my glib example with gprof, but the different behavior of gprof and gcov made this prospect difficult. The glib test programs are run one after another by the make check command, so each test's gmon.out was overwritten by the next test. Also, the test programs actually are shell scripts, which set some reasonably complex environment variables to run the real test programs, which are kept in a hidden subdirectory. Moral of the story: gcov is the right tool for doing coverage testing for glib.
Instead, I created a simple program to calculate the Fibonacci numbers from zero to 42. The Fibonacci numbers are a series of numbers beginning with 0,1 where the next number in the series is found by adding the previous two numbers (so, 0,1,1,2,3,5,8,13,21,...) A good Web site on the Fibonacci numbers and related mathematical concepts can be found at www.mcs.surrey.ac.uk/Personal/R.Knott/Fibonacci/fib.html. The number 42 was chosen, not merely for its literary significance to Douglas Adams fans, but because the Fibonnacci numbers begin overflowing signed 32-bit integer arithmetic at 47, so a lower limit was useful.
The calculation of a Fibonacci number can make a good example of a recursive function. Because each Fibonacci number is the sum of the previous two, the simplest approach is to create a function int fibonacci(int n), which simply calls itself with n - 1 and with n - 2, adds the results and returns the value (Listing 3).
Listing 3. fib.c
#include <stdio.h>
int fibonacci(int n);
int main (int argc, char **argv)
{
int fib;
int n;
for (n = 0; n <= 42; n++) {
fib = fibonacci(n);
printf("fibonnaci(%d) = %d\n", n, fib);
}
return 0;
}
int fibonacci(int n)
{
int fib;
if (n <= 0) {
fib = 0;
}
else if (n == 1) {
fib = 1;
}
else {
fib = fibonacci(n -1) + fibonacci(n - 2);
}
return fib;
}
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
| Dart: a New Web Programming Experience | May 07, 2013 |
- RSS Feeds
- New Products
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Home, My Backup Data Center
- New Products
- Validate an E-Mail Address with PHP, the Right Way
- Developer Poll
- Trying to Tame the Tablet
- not living upto the mobile revolution
10 min 8 sec ago - Deceptive Advertising and
45 min 40 sec ago - Let\'s declare that you have
46 min 37 sec ago - Alterations in Contest Due
47 min 43 sec ago - At a numbers mindset, your
48 min 54 sec ago - Do not get Just Almost any
52 min 23 sec ago - A fantastic rule-of-thumb to
53 min 46 sec ago - Keren mastah..
Penting,
1 hour 51 min ago - mini tablet compare
3 hours 10 min ago - Looking Good
6 hours 43 min ago
Enter to Win an Adafruit Prototyping Pi Plate Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Prototyping Pi Plate Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- Next winner announced on 5-21-13!
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.



Comments
Is there any coverage tool
Is there any coverage tool available that only works on source files and not on executables. I mean I dont want to compile the source files even once still I want to find out the coverage in the code.
Is it possible?
Please help. I'll really appreciate your effort if you get me any information regarding the matter.
Thanks!
help needed in installing ggcov
We have tried installing ggcov
we had followed following steps.
./configure (it satisfies our pc configuration)
make
make install
It is not getting installed properly...
How can we check it is properly installed or not....?
Is there any other steps to covered...
please kindly help
Regards
savita
hai, i understood your
hai,
i understood your problem.....
me also struck with that problem but what i did was,
i just installed ggcov-06 instead of ggcov-08.....
in ggcov ,it is asking someother file which we could not get fro internet...
so better to go to install ggcov-06
thanks and regards,
Rajasekhar...
ggcov
when i try to run ggcov in SUSE LINUX 10.1 using the command line ggcov ,i do not get any error message.but the results are not displayed.
the installation had no problems.
If anybody who is familiar with ggcov can give me a detailed idea about how to use this tool,please reply.
ggcov
anybody kindly post a reply regarding how to implement ggcov
I could run ggcov successfully on the command prompt but I can't view any GUI.So please tell me the syntax/procedures of using ggcov.My linux version is SUSE 9.0
error on running kprof
when i try to make the kprof i am getting this errors..
Kindly help me to correct and run the application
parseprofile_pose.h:30: error: expected `)' before â&â token
parseprofile_pose.cpp:29: error: prototype for âCParseProfile_pose::CParseProfile_pose(QTextStream&, QPtrVector&)â does not match any in class âCParseProfile_poseâ
parseprofile_pose.h:28: error: candidates are: CParseProfile_pose::CParseProfile_pose(const CParseProfile_pose&)
parseprofile_pose.h:36: error: CParseProfile_pose::CParseProfile_pose()
need help for GCOV
I have a problem:
I'm not able to gcc -fprofile-arcs -ftest-coverage file.c
Could you let me know how I can set environemt CFLAGS variable?
I have problem when I gcc on glib files i.e garray.c
please hel--
valgrind coverage
I thought I'd add a comment to say that some people are using Valgrind to do coverage testing using the 'cachegrind' tool.
Great article, a nice clear introduction to the topic.
Thanks
Thanks very much for this informative example. I was surprised that 50% is about average for code coverage!
Re: Coverage Measurement and Profiling
Very nice article, I enjoyed it and learned new (for me) methods
thanks a lot
b.
Don't forget lcov!
lcov is a nice addition to gcof; like ggcov, it shows summaries
for multiple files. It goes beyond ggcov, I think, in that it
handles entire directory hierarchies. It can handle the Linux
kernel and Wine, for instance. It outputs HTML rather than
presenting a GUI, but that's kind of a plus in my book.
It's at ltp.sf.net/coverage/lcov.php
Re: Don't forget lcov!
Lcov does look very nice. My intent wasn't to do a comprehensive survey of tools, but to introduce the basics of gcov and gprof and give an example of a representative "add-on" for each.