CSc 352: Lecture-21

Profiling

Note: All the programs mentioned in this lecture are in:

/home/cs352/SUMMER02/lecture21progs/

- Profiling allows us to see where our program spent its time
  and which functions called which other functions while it 
  was executing. 

- We can do profiling using gprof.

- Using -pg option in gcc turns on profiling info.

- After compilation, run the program to completion to get the file 
  gmon.out which contains profiling info in a gprof readable form.

- Now we can give the command "gprof a.out > profilinginfo"
  the profiling info about the program a.out will be
  written into file "profilinginfo". (In this case we don't
  have to write the name of the executable, i.e. a.out, 
  since without the executable name, it considers a.out
  by default.)

  Note: Your program must exit normally: by returning from main 
    or by calling exit

- Two kinds of analysis are performed: 
    - The flat profile:
      Shows the total amount of time your program spent 
      executing each function. 

	% time 
        This is the percentage of the total execution time 
        your program spent in this function. These should 
        all add up to 100%. 
      
	cumulative seconds 
        This is the cumulative total number of seconds the 
	computer spent executing this functions, plus the 
	time spent in all the functions above this one in this table. 
	
	self seconds 
        This is the number of seconds accounted for by this function 
	alone. The flat profile listing is sorted first by this number. 

	calls 
     	This is the total number of times the function was called. 
	If the function was never called, or the number of times it was
     	called cannot be determined (probably because the function was 
	not compiled with profiling enabled), the calls field is blank. 

	self ms/call 
     	This represents the average number of milliseconds spent in 
	this function per call, if this function is profiled. Otherwise,
     	this field is blank for this function. 

	total ms/call 
     	This represents the average number of milliseconds spent in 
	this function and its descendants per call, if this function is
     	profiled. Otherwise, this field is blank for this function. 
	This is the only field in the flat profile that uses call graph 
	analysis. 

	name 
	This is the name of the function. The flat profile is sorted by 
	this field alphabetically after the self seconds and calls fields
     	are sorted. 

    - The call graph:
      Shows how much time was spent in each function 
      and its children. 

	% time 
     	This is the percentage of the total time that was spent in
	this function, including time spent in subroutines called 
	from this function. The time spent in this function is counted 
	again for the callers of this function. Therefore, adding up these
     	percentages is meaningless. 

	self 
	This is the total amount of time spent in this function. This 
	should be identical to the number printed in the seconds field
     	for this function in the flat profile. 
	
	children 
     	This is the total amount of time spent in the subroutine calls 
	made by this function. This should be equal to the sum of all
     	the self and children entries of the children listed directly 
	below this function. 

	called 
     	This is the number of times the function was called. If the 
	function called itself recursively, there are two numbers,
     	separated by a `+'. The first number counts non-recursive calls, 
	and the second counts recursive calls. 

	NOTE: The same fields will have different meanings for the
	parents of the function and its children(always interpreted
	with regard to child being called inside the parent...)