Creation Zone

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Thursday, 22 September 2005

Code Coverage Analysis with tcov

Posted on 19:36 by Unknown
It is a good practice to do code coverage analysis in the development environment, to find the cold and hot spots of the application. Cold spots are those parts of code that are rarely used (or executed); and the Hot spots are those parts of the code, that are very frequently used. Doing this analysis in the development stage, helps: (i) improving the performance of the application by tuning the hot spots, and (ii) removing the dead code and/or rewriting or moving the cold spots, in such a way that the code will be executed for decent number of times in the life time of the program. The tricky part is creating extensive test cases to cover all the branches of the source code. Since we may not be able to generate test cases for all scenarios that can exercise all parts of the code, this analysis also helps detecting new test cases that we could invent to cover certain features. For example, code coverage analysis on regular builds certainly helps us discover the blocks of code that was never executed; so it gives us an opportunity to come up with better test cases to run against profile feedback collect builds during training run.

Sun Microsystems bundled a tool called tcov (short for: test coverage), with Sun Studio compiler suite, for code coverage analysis. tcov gives line-by-line information on how a program executes. Most of this information is annotated in a copy of the source file {with .toc extension}, when the {tcov} tool is run against the source file, along with profile data from the execution of test cases.

Steps to do the Code Coverage Analysis with tcov
  1. Compile the program with additional option -xprofile=tcov

  2. Run the program and all the test cases against the application. Since it was compiled with -xprofile option, {by default} the run-time will create a directory called <executable>.profile in the same directory from where the executable is run. This behavior can be overridden by setting SUN_PROFDATA_DIR or SUN_PROFDATA environment variables.

    <executable>.profile directory contains a file called tcovd. tcovd holds the information about the line numbers, and the execution count. This is a plain text file.

  3. Run tcov with -x option, over each source file to generate the annotated source file
Here's an example. Example was taken from the article on profile feedback optimization.

eg.,
% cat tcovex.c
#include <stdio.h>
#include <stdlib.h>

static unsigned _sum (unsigned *a0, unsigned *a1, unsigned *a2) {

unsigned result = 0;

if (a0 == NULL) {
printf("a0 == NULL");
} else {
result += (*a0);
}

if (a1 == NULL) {
printf("a1 == NULL");
} else {
result += (*a1);
}

if (a2 == NULL) {
printf("a2 == NULL");
} else {
result += (*a2);
}

return (result);
}

int main(int argc, const char *argv[]) {
int i, j, niters = 1, n=3;
unsigned sum, answer = 0, a[3];

niters = 1000000000;

if (argc == 2) {
niters = atoi(argv[1]);
}

for (j = 0; j < n; j++) {
a[j] = rand();
answer += a[j];
}

for (i = 0; i < niters; i++) {
sum = _sum (a+0, a+1, a+2);
}

if (sum == answer) {
printf("answer = %u\n", answer);
} else {
printf("error sum=%u, answer=%u", sum, answer);
}

return (0);
}

% cc -xO2 -xprofile=tcov -o tcovex tcovex.c

% setenv SUN_PROFDATA_DIR /tmp

% ls -ld /tmp/*.profile
No match

% ./tcovex 10000000
answer = 32709

% ls -ld /tmp/*.profile
drwxrwxrwx 2 build engr 179 Sep 22 19:25 /tmp/tcovex.profile/

% ls -lR /tmp/tcovex.profile/
/tmp/tcovex.profile/:
total 16
-rw-rw-rw- 1 build engr 318 Sep 22 19:25 tcovd

% tcov -x $SUN_PROFDATA_DIR/tcovex.profile tcovex.c

% ls -l $SUN_PROFDATA_DIR/*.tcov
-rw-rw-rw- 1 build engr 1857 Sep 22 19:27 /tmp/tcovex.c.tcov

% cat /tmp/tcovex.c.tcov
#include
#include

static unsigned _sum (unsigned *a0, unsigned *a1, unsigned *a2) {

10000000 -> unsigned result = 0;

if (a0 == NULL) {
##### -> printf("a0 == NULL");
} else {
10000000 -> result += (*a0);
}

10000000 -> if (a1 == NULL) {
##### -> printf("a1 == NULL");
} else {
10000000 -> result += (*a1);
}

10000000 -> if (a2 == NULL) {
##### -> printf("a2 == NULL");
} else {
10000000 -> result += (*a2);
}

return (result);
}

int main(int argc, const char *argv[]) {
1 -> int i, j, niters = 1, n=3;
unsigned sum, answer = 0, a[3];

niters = 1000000000;

if (argc == 2) {
1 -> niters = atoi(argv[1]);
}

1 -> for (j = 0; j < n; j++) {
3 -> a[j] = rand();
answer += a[j];
}

1 -> for (i = 0; i < niters; i++) {
10000000 -> sum = _sum (a+0, a+1, a+2);
}

1 -> if (sum == answer) {
1 -> printf("answer = %u\n", answer);
} else {
##### -> printf("error sum=%u, answer=%u", sum, answer);
}

return (0);
}


Top 10 Blocks

Line Count

6 10000000
11 10000000
14 10000000
17 10000000
20 10000000
23 10000000
45 10000000
40 3
30 1
36 1


18 Basic blocks in this file
14 Basic blocks executed
77.78 Percent of the file executed

70000009 Total basic block executions
3888889.25 Average executions per basic block
Note:
Lines with prefix "#####" were never executed.

Reference and suggested reading:
Sun Studio 10 man page of tcov
__________________
Technorati tags: sun studio
C
C++
Email ThisBlogThis!Share to XShare to Facebook
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • *nix: Workaround to cannot find zipfile directory in one of file.zip or file.zip.zip ..
    Symptom: You are trying to extract the archived files off of a huge (any file with size > 2 GB or 4GB, depending on the OS) ZIP file with...
  • JDS: Installing Sun Java Desktop System 2.0
    This document will guide you through the process of installing JDS 2.0 on a PC from integrated CDROM images Requirements I...
  • Linux: Installing Source RPM (SRPM) package
    RPM stands for RedHat Package Manager. RPM is a system for installing and managing software & most common software package manager used ...
  • Solaris: malloc Vs mtmalloc
    Performance of Single Vs Multi-threaded application Memory allocation performance in single and multithreaded environments is an important a...
  • C/C++: Printing Stack Trace with printstack() on Solaris
    libc on Solaris 9 and later, provides a useful function called printstack , to print a symbolic stack trace to the specified file descripto...
  • Installing MySQL 5.0.51b from the Source Code on Sun Solaris
    Building and installing the MySQL server from the source code is relatively very easy when compared to many other OSS applications. At least...
  • Oracle Apps on T2000: ORA-04020 during Autoinvoice
    The goal of this brief blog post is to provide a quick solution to all Sun-Oracle customers who may run into a deadlock when a handful of th...
  • Siebel Connection Broker Load Balancing Algorithm
    Siebel server architecture supports spawning multiple application object manager processes. The Siebel Connection Broker, SCBroker, tries to...
  • 64-bit dbx: internal error: signal SIGBUS (invalid address alignment)
    The other day I was chasing some lock contention issue with a 64-bit application running on Solaris 10 Update 1; and stumbled with an unexpe...
  • Oracle 10gR2/Solaris x64: Fixing ORA-20000: Oracle Text errors
    First, some facts: * Oracle Applications 11.5.10 (aka E-Business Suite 11 i ) database is now supported on Solaris 10 for x86-64 architectur...

Categories

  • 80s music playlist
  • bandwidth iperf network solaris
  • best
  • black friday
  • breakdown database groups locality oracle pmap sga solaris
  • buy
  • deal
  • ebiz ebs hrms oracle payroll
  • emca oracle rdbms database ORA-01034
  • friday
  • Garmin
  • generic+discussion software installer
  • GPS
  • how-to solaris mmap
  • impdp ora-01089 oracle rdbms solaris tips upgrade workarounds zombie
  • Magellan
  • music
  • Navigation
  • OATS Oracle
  • Oracle Business+Intelligence Analytics Solaris SPARC T4
  • oracle database flashback FDA
  • Oracle Database RDBMS Redo Flash+Storage
  • oracle database solaris
  • oracle database solaris resource manager virtualization consolidation
  • Oracle EBS E-Business+Suite SPARC SuperCluster Optimized+Solution
  • Oracle EBS E-Business+Suite Workaround Tip
  • oracle lob bfile blob securefile rdbms database tips performance clob
  • oracle obiee analytics presentation+services
  • Oracle OID LDAP ADS
  • Oracle OID LDAP SPARC T5 T5-2 Benchmark
  • oracle pls-00201 dbms_system
  • oracle siebel CRM SCBroker load+balancing
  • Oracle Siebel Sun SPARC T4 Benchmark
  • Oracle Siebel Sun SPARC T5 Benchmark T5-2
  • Oracle Solaris
  • Oracle Solaris Database RDBMS Redo Flash F40 AWR
  • oracle solaris rpc statd RPC troubleshooting
  • oracle solaris svm solaris+volume+manager
  • Oracle Solaris Tips
  • oracle+solaris
  • RDC
  • sale
  • Smartphone Samsung Galaxy S2 Phone+Shutter Tip Android ICS
  • solaris oracle database fmw weblogic java dfw
  • SuperCluster Oracle Database RDBMS RAC Solaris Zones
  • tee
  • thanksgiving sale
  • tips
  • TomTom
  • windows

Blog Archive

  • ►  2013 (16)
    • ►  December (3)
    • ►  November (2)
    • ►  October (1)
    • ►  September (1)
    • ►  August (1)
    • ►  July (1)
    • ►  June (1)
    • ►  May (1)
    • ►  April (1)
    • ►  March (1)
    • ►  February (2)
    • ►  January (1)
  • ►  2012 (14)
    • ►  December (1)
    • ►  November (1)
    • ►  October (1)
    • ►  September (1)
    • ►  August (1)
    • ►  July (1)
    • ►  June (2)
    • ►  May (1)
    • ►  April (1)
    • ►  March (1)
    • ►  February (1)
    • ►  January (2)
  • ►  2011 (15)
    • ►  December (2)
    • ►  November (1)
    • ►  October (2)
    • ►  September (1)
    • ►  August (2)
    • ►  July (1)
    • ►  May (2)
    • ►  April (1)
    • ►  March (1)
    • ►  February (1)
    • ►  January (1)
  • ►  2010 (19)
    • ►  December (3)
    • ►  November (1)
    • ►  October (2)
    • ►  September (1)
    • ►  August (1)
    • ►  July (1)
    • ►  June (1)
    • ►  May (5)
    • ►  April (1)
    • ►  March (1)
    • ►  February (1)
    • ►  January (1)
  • ►  2009 (25)
    • ►  December (1)
    • ►  November (2)
    • ►  October (1)
    • ►  September (1)
    • ►  August (2)
    • ►  July (2)
    • ►  June (1)
    • ►  May (2)
    • ►  April (3)
    • ►  March (1)
    • ►  February (5)
    • ►  January (4)
  • ►  2008 (34)
    • ►  December (2)
    • ►  November (2)
    • ►  October (2)
    • ►  September (1)
    • ►  August (4)
    • ►  July (2)
    • ►  June (3)
    • ►  May (3)
    • ►  April (2)
    • ►  March (5)
    • ►  February (4)
    • ►  January (4)
  • ►  2007 (33)
    • ►  December (2)
    • ►  November (4)
    • ►  October (2)
    • ►  September (5)
    • ►  August (3)
    • ►  June (2)
    • ►  May (3)
    • ►  April (5)
    • ►  March (3)
    • ►  February (1)
    • ►  January (3)
  • ►  2006 (40)
    • ►  December (2)
    • ►  November (6)
    • ►  October (2)
    • ►  September (2)
    • ►  August (1)
    • ►  July (2)
    • ►  June (2)
    • ►  May (4)
    • ►  April (5)
    • ►  March (5)
    • ►  February (3)
    • ►  January (6)
  • ▼  2005 (72)
    • ►  December (5)
    • ►  November (2)
    • ►  October (6)
    • ▼  September (5)
      • Setting up Webmin on Solaris 10
      • Troubleshooting: Solaris {vold} refuses to mount &...
      • Code Coverage Analysis with tcov
      • One Year {Blog} Anniversary
      • Sun Studio C/C++: Profile Feedback Optimization II
    • ►  August (5)
    • ►  July (10)
    • ►  June (8)
    • ►  May (9)
    • ►  April (6)
    • ►  March (6)
    • ►  February (5)
    • ►  January (5)
  • ►  2004 (36)
    • ►  December (1)
    • ►  November (5)
    • ►  October (12)
    • ►  September (18)
Powered by Blogger.

About Me

Unknown
View my complete profile