The prof command

The prof command displays a profile of CPU usage for each external symbol, or routine, of a specified program.

In detail, it displays the following:

  • The percentage of execution time spent between the address of that symbol and the address of the next
  • The number of times that function was called
  • The average number of milliseconds per call

The prof command interprets the profile data collected by the monitor() subroutine for the object file (a.out by default), reads the symbol table in the object file, and correlates it with the profile file (mon.out by default) generated by the monitor() subroutine. A usage report is sent to the terminal, or can be redirected to a file.

To use the prof command, use the -p option to compile a source program in C, FORTRAN, or COBOL. This inserts a special profiling startup function into the object file that calls the monitor() subroutine to track function calls. When the program is executed, the monitor() subroutine creates a mon.out file to track execution time. Therefore, only programs that explicitly exit or return from the main program cause the mon.out file to be produced. Also, the -p flag causes the compiler to insert a call to the mcount() subroutine into the object code generated for each recompiled function of your program. While the program runs, each time a parent calls a child function, the child calls the mcount() subroutine to increment a distinct counter for that parent-child pair. This counts the number of calls to a function.
Note: You cannot use the prof command for profiling optimized code.

By default, the displayed report is sorted by decreasing percentage of CPU time. This is the same as when specifying the -t option.

The -c option sorts by decreasing number of calls and the -n option sorts alphabetically by symbol name.

If the -s option is used, a summary file mon.sum is produced. This is useful when more than one profile file is specified with the -m option (the -m option specifies files containing monitor data).

The -z option includes all symbols, even if there are zero calls and time associated.

Other options are available and explained in the prof command in the Files Reference.

The following example shows the first part of the prof command output for a modified version of the Whetstone benchmark (Double Precision) program.
# cc -o cwhet -p -lm cwhet.c
# cwhet > cwhet.out
# prof
Name                 %Time     Seconds     Cumsecs  #Calls   msec/call
.main                 32.6       17.63       17.63       1  17630.
.__mcount             28.2       15.25       32.88
.mod8                 16.3        8.82       41.70 8990000      0.0010
.mod9                  9.9        5.38       47.08 6160000      0.0009
.cos                   2.9        1.57       48.65 1920000      0.0008
.exp                   2.4        1.32       49.97  930000      0.0014
.log                   2.4        1.31       51.28  930000      0.0014
.mod3                  1.9        1.01       52.29  140000      0.0072
.sin                   1.2        0.63       52.92  640000      0.0010
.sqrt                  1.1        0.59       53.51
.atan                  1.1        0.57       54.08  640000      0.0009
.pout                  0.0        0.00       54.08      10      0.0
.exit                  0.0        0.00       54.08       1      0.
.free                  0.0        0.00       54.08       2      0.
.free_y                0.0        0.00       54.08       2      0.
In this example, we see many calls to the mod8() and mod9() routines. As a starting point, examine the source code to see why they are used so much. Another starting point could be to investigate why a routine requires so much time.
Note: If the program you want to monitor uses a fork() system call, be aware that the parent and the child create the same file (mon.out). To avoid this problem, change the current directory of the child process.