Profiling using Manual Instrumentation

Manual instrumentation is the addition of code by the programmer that measures timings in a program. This is more time consuming than using profilers that automate the process. However, it allows the programmer complete control over exactly how the timing is measured.

Contents

Prerequisite knowledge

To make use of the following examples, you should be familiar with writing, compiling and running programs using the C programming language.

Using manual instrumentation

Manual instrumentation of a program makes use of timing functions provided by the underlying operating system and libraries. The exact timing routines will vary depending on the choice of programming language or other libraries called by the program. Several examples of timing routines are provided in the sections below.

The timing functions are called before and after a region of interest in the code to capture the time that the program passes that point in the program. The difference between the two recorded times is then calculated and output in the examples below. More complex manual instrumentation may time multiple functions, or aggregate timings across multiple calls to a region of code.

Manual instrumentation in C

A typical example of manual instrumentation using the time() routine is:

Listing 1. Timing the do_some_work() function in C
#include <time.h>
#include <stdio.h>
...
time_t t1,t2;
t1 = time(NULL);
do_some_work();
t2 = time(NULL);
printf(“Elapsed time: %d (s)\n”,t2-t1);

Manual instrumentation using MPI

If your program uses MPI, it can use the MPI_Wtime() routine which is a portable way to query a high resolution elapsed clock.

Listing 2. Timing the do_some_work() function using MPI
#include “mpi.h”
...
double t1,t2;
t1 = MPI_Wtime();
do_some_work();
t2 = MPI_Wtime();

printf(“Elapsed time: %f\n”,t2-t1);

Manual instrumentation using OpenMP

Similarly, if your code uses OpenMP, it can use the omp_get_wtime() routine which is a portable way to query a high resolution elapsed clock.

Listing 3. Timing the do_some_work() function using OpenMP
#include “omp.h”
...
double t1,t2;
t1 = omp_get_wtime();
do_some_work();
t2 = omp_get_wtime();

printf(“Elapsed time: %f\n”,t2-t1);

Related pages