Preparing the Image


Let s now look at how an image is prepared for profiling with gprof . We ll first look at some basic uses of profiling with gprof , and in later sections we ll discuss some of the other options available. For our profiling example, we ll use the following sorting demo shown in Listing 8.1. This example source (   sort .c ) illustrates two sorting algorithms, the insert-sort (function insertSort , lines 5 “21) and the bubble-sort (function bubbleSort , lines 23 “50). Each is run with identical data to unambiguously understand their profiling properties for a given data set (as provided by function init_list , lines 53 “62).

Listing 8.1: Sample Source to Explore the gprof Utility (on the CD-ROM at ./source/ch8/sort.c)
start example
  1:  #include <stdio.h>  2:   3:  #define MAX_ELEMENTS 10000  4:   5:  void insertSort(int list[], int size)  6:  {  7:  int i, j, temp;  8:   9:  for (i = 1 ; i <= size-1 ; i++) {  10:   11:  temp = list[i];  12:   13:  for (j = i-1 ; j >= 0 && (list[j] > temp) ; j) {  14:  list[j+1] = list[j];  15:  }  16:   17:  list[j+1] = temp;  18:   19:  }  20:   21:  }  22:   23:  void bubbleSort(int list[], int size)  24:  {  25:  int i, j, temp, swap = 1;  26:   27:  while (swap) {  28:   29:  swap = 0;  30:   31:  for (i = (size-1) ; i >= 0 ; i) {  32:   33:  for (j = 1 ; j <= i ; j++) {  34:   35:  if (list[j-1] > list[j]) {  36:   37:  temp = list[j-1];  38:  list[j-1] = list[j];  39:  list[j] = temp;  40:  swap = 1;  41:   42:  }  43:   44:  }  45:   46:  }  47:   48:  }  49:   50:  }  51:   52:   53:  void init_list(int list[], int size)  54:  {  55:  int i;  56:   57:  for (i = 0 ; i < size ; i++) {  58:  list[i] = (size-i);  59:  }  60:   61:  return;  62:  }  63:   64:   65:  int main()  66:  {  67:  int list[MAX_ELEMENTS];  int i;  68:   69:  /* Invoke the bubble sort algorithm */  70:  init_list(list, MAX_ELEMENTS);  71:  bubbleSort(list, MAX_ELEMENTS);  72:  init_list(list, MAX_ELEMENTS);  73:  insertSort(list, MAX_ELEMENTS);  74:   75:  } 
end example
 

The gprof utility uses information from the executable image and the profiler output file, gmon.out , to generate its profiling data. In order to collect the profiling data, the image must be compiled and linked with a special set of compiler flags. These are illustrated below for compiling our sample source file,  sort.c :

 gcc sort.c -o sort  -pg  

The result is an image, sort , which is instrumented to collect profiling information. When the image is executed and completes normally, a file called gmon.out results, containing the profiling data.

Note  

The gmon.out file is written upon normal exit of the application. If the program exits abnormally or the user forces an exit with a Ctrl+C, the gmon.out file will not be written.




GNU/Linux Application Programming
GNU/Linux Application Programming (Programming Series)
ISBN: 1584505680
EAN: 2147483647
Year: 2006
Pages: 203
Authors: M. Tim Jones

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net