Gprof is a compiler-assisted performance profiler for C, Fortran, and Pascal applications running on Unix systems. You can use Gprof to help identify hotspots in your application where code optimization efforts may be most useful.
Gprof uses a hybrid of sampling and instrumentation, and provides the following information:
The number of calls to each function and the amount of time spent there.
Information about the caller-callee relationship.
Note: Gprof only measures the user code; it does not provide information on time spent in the kernel (such as system calls or I/O wait time).
The profiling data will be collected in a file called gmon.out, which will be generated at the end of a successful, uninterrupted run.
Gprof is available in the /usr/bin directory on all NAS compute systems. To use this tool, follow the instructions in the sections below.
Compiling and Linking to Enable Profiling with Gprof
To enable profiling with Gprof, add one of the options shown below when you compile your code:
With Intel compilers, add the -p option (alternatively you can add -pg, which is deprecated, but still works).
With PGI compilers, add the -pg option.
With GNU compilers, add the -pg option. You might also have to use the -O0 option, if you do not get meaningful profiling results when using higher levels of optimization.
Collecting Profiling Data
To collect profiling data, simply run your gprof-enabled executable the same way you would run a non-gprof-enabled executable. The data will be collected in a file called gmon.out.
OpenMP Applications
If you are using an OpenMP application, gprof does not generate per-thread profiling data. Only one gmon.out file is produced.
Note: If a file named gmon.out already exists in the directory, it will be overwritten.
MPI Applications
For MPI applications, if you use the Intel MPI library, no additional steps are required before you run your code. However, if you use the HPE MPT library, you also need to set the MPI_SHEPHERD variable as follows before you run the code; otherwise, the timing information will not be shown:
Each MPI process will generate a profile with the same filename, gmon.out. These files will overwrite one another when they are written to a global filesystem. Therefore, to avoid this behavior and produce a profile with a distinct filename for each process, do:
This operation will generate a file called gmon.out.pid, where pid is the process ID of an MPI process. With N ranks, you should get N such files, with filenames differing only in their .pid extensions. You can then analyze an individual gmon.out.pid file or several at the same time.
Generating ASCII Gprof Output
You can use the gprof command to convert binary data in gmon.out into a human-readable format.
There are two types of output: flat profile and call graph.
The flat profile shows how much time your program spent in each function, and how many times that function was called. This profile helps to identify hotspots in your application. Hotspots are shown at the top of the flat profile.
The call graph shows, for each function, which functions called it; which other functions it called; and how many times the calls occurred. The call graph also provides an estimate of how much time was spent in the subroutines of each function, which can suggest places where you might try to eliminate function calls that use a lot of time.
Commonly Used gprof Command Options
Some common options are described here. Read man gprof for more information.
-p prints a flat profile. For example:
gprof -p a.out gmon.out.1001 gmon.out.1002
-q prints a call graph. For example:
gprof -q a.out gmon.out.*
-s sums up the information from multiple profiling data files and produces a file called gmon.sum for analysis. For example: