Home > Articles > Operating Systems, Server > Linux/UNIX/Open Source

  • Print
  • + Share This
This chapter is from the book

Performance Tuning Using GNU gprof

A profiler provides execution profiles. In other words, it tells you how much time is being spent in each subroutine or function. You can view two kinds of extreme profiles: a sharp profile and a flat profile.

Typically, scientific and engineering applications are dominated by a few routines and give sharp profiles. These routines are usually built around linear algebra solutions. Tuning code should focus on the most time-consuming routines and can be very rewarding if successful.

Programs with flat profiles are more difficult to tune than ones with sharp profiles. Regardless of the code’s profile, a subroutine (function) profiler, gprof, can provide a key way to tune applications.

Profiling tells you where a program is spending its time and which functions are called while the program is being executed. With profile information, you can determine which pieces of the program are slower than expected. These sections of the code can be good candidates to be rewritten to make the program execute faster. Profiling is also the best way to determine how often each function is called. With this information, you can determine which function will give the most performance boost by changing the code to perform faster.

The profiler collects data during the program’s execution. Having a complete analysis of the program helps you ensure that all its important paths are while the program is being profiled. Profiling can also be used on programs that are very complex. This could be another way to learn the source code in addition to just reading it. Now let’s look at the steps needed to profile a program using gprof:

  • Profiling must be enabled when compiling and linking the program.
  • A profiling data file is generated when the program is executed.
  • Profiling data can be analyzed by running gprof.

gprof can display two different forms of output:

  • A flat profile displays the amount of time the program went into each function and the number of times the function was executed.
  • A call graph displays details for each function, which function(s) called it, the number of times it was called, and the amount of time that was spent in the subroutines of each function. Figure 1.10 shows part of a call graph.
Figure 1.10

Figure 1.10 A typical fragment of a call graph.

gprof is useful not only to determine how much time is spent in various routines, but also to tell you which routines call (invoke) other routines. Suppose you examine gprof’s output and see that xyz is consuming a lot of time, but the output doesn’t tell you which routine is calling xyz. If there were a call tree, it would tell you where the calls to xyz were coming from.

  • + Share This
  • 🔖 Save To Your Account