Princeton University
COS 217:  Introduction to Programming System

Precept 11:  gprof

Purpose

Give overview of  the UNIX gprof tool

Reading

Loukides and Oram, Chapter 9

man page:  gprof

Introduction

GNU Performance Profiler

Generates a report showing how much time the computer spent executing each function of your program

Very useful for optimizing your program

Example

testgprof
   testsort.c

Report Generation

To generate a report:

gcc -pg -o testsort testsort.c

-pg option tells compiler and linker to add statistics-collecting code to testsort

testsort

Generates binary file gmon.out containing statistics

gprof -b testsort > gprofreport

Uses gmon.out and testsort to print a human-readable report to stdout

Analyzing the Report

The result is a textual gprof report

The index

At end of report

Gives each function a unique number

Note:  gprof reports statistics for many functions that do not concern you

Can use options to trim -- see textbook or man pages

The flat profile

Precedes index

One line for each function

Columns:

name:  Name of this function, and its index

%time:  Percentage of execution time spent executing this function

cumulative seconds:  Seconds spent executing this function, and all functions listed before it

self seconds:  Seconds spent executing this function (excluding descendents)

calls:  Times this function was called (not including recursive calls)

self ms/call:  Average milliseconds spent executing this function (excluding descendents) each time it was called

total ms/call:  Average milliseconds spent executing this function and its descendents each time it was called

The call graph profile

Precedes flat profile

One section for each function, separated from other sections by lines

Columns:

Index:  The index of this function

Parents/name/children (last column) (note indentation):

Names of parents

Name of this function

Names of children

%time:  Percentage of execution time spent executing this function and its descendents

self:  

Seconds spent executing this function as a result of calls from each parent

Seconds spent executing this function

Seconds spent executing each child as a result of calls from this function

descendents:  

Seconds spent executing the descendents of this function

called (informally):

Times that each parent called this function / times this function was called

Times this function was called

Times that each child was called by this function / times the child was called

Optimizing the Program

Having found a function that is particularly time consuming:

Redesign function to use a different algorithm or data structure

Unwind/unroll loops

Call an expensive child function less often

Rewrite the function to assembly language

Limitations

gprof statistics-collection code in executable file itself consumes a significant amount of time

gprof measures "wall-clock" time, not CPU time

In a multiprocessing system, results will be influenced by activity of other processes

Should average several executions to get reliable results

Alternatives

Quantify

(Purify)

Copyright © 2002 by Robert M. Dondero, Jr.