Chapter 5. Profiling Developers profile programs to focus attention on the areas of the program that have the largest impact on performance. The types of data collected include what section of the program consumes the most processor time, and where memory is allocated. Profiling collects data from the actual program execution. Thus, the quality of the data collect is influenced by the actual tasks being performed by the program. The tasks performed during profiling should be representative of actual use; this ensures that problems arising from realistic use of the program are addressed during development. Red Hat Enterprise Linux 6 includes a number of different tools (Valgrind, OProfile, perf , and SystemTap) to collect profiling data. Each tool is suitable for performing specific types of profile runs, as described in the following sections. 5.1. Valgrind Valgrind is an instrumentation framework for building dynamic analysis tools that can be used to profile applications in detail. The default installation alrready provides five standard tools. Valgrind tools are generally used to investigate memory management and threading problems. The Valgrind suite also includes tools that allow the building of new profiling tools as required. Valgrind provides instrumentation for user-space binaries to check for errors, such as the use of uninitialized memory, improper allocation/freeing of memory, and improper arguments for systemcalls. Its profiling tools can be used by normal users on most binaries; however, compared to other profilers, Valgrind profile runs are significantly slower. To profile a binary, Valgrind rewrites its executable and instruments the rewritten binary. Valgrind 's tools are most useful for looking for memory-related issues in user-space programs; it is not suitable for debugging time-specific issues or kernel-space instrumentation and debugging. Valgrind reports are most useful and accurate whhen debuginfo packages are installed for the programs or libraries under investigation. See Section 4.2, "Installing Debuginfo Packages" . 5.1.1. Valgrind Tools The Valgrind suite is composed of the following tools: memcheck This tool detects memory management problems in programs by checking all reads from and writes to memory and intercepting all system calls to malloc , new , free , and delete . memcheck is perhaps the most used Valgrind tool, as memory management problems can be difficult to detect using other means. Such problems often remain undetected for long periods, eventually causing crashes that are difficult to diagnose. cachegrind cachegrind is a cache profiler that accurately pinpoints sources of cache misses in code by performing a detailed simulation of the I1, D1 and L2 caches in the CPU. It shows the number of cache misses, memory references, and instructions accruing to each line of source code; cachegrind also provides per-function, per-module, and whole-program summaries, and can even show counts for each individual machine instructions. callgrind Like cachegrind , callgrind can model cache behavior. However, the main purpose of callgrind is to record callgraphs data for the executed code. massif massif is a heap profiler; it measures how much heap memory a program uses, providing information on heap blocks, heap administration overheads, and stack sizes. Heap profilers are useful in finding ways to reduce heap memory usage. On systems that use virtual memory, programs with optimized heap memory usage are less likely to run out of memory, and may be faster as they require less paging. helgrind In programs that use the POSIX pthreads threading primitives, helgrind detects synchronization errors. Such errors are: Misuses of the POSIX pthreads API Potential deadlocks arising from lock ordering problems Data races (that is, accessing memory without adequate locking) Valgrind also allows you to develop your own profiling tools. In line with this, Valgrind includes the lackey tool, which is a sample that can be used as a template for generating your own tools. 5.1.2. Using Valgrind The valgrind package and its dependencies install all the necessary tools for performing a Valgrind profile run. To profile a program with Valgrind , use: See Section 5.1.1, "Valgrind Tools" for a list of arguments for toolname . In addition to the suite of Valgrind tools, none is also a valid argument for toolname ; this argument allows you to run a program under Valgrind without performing any profiling. This is useful for debugging or benchmarking Valgrind itself. You can also instruct Valgrind to send all of its information to a specific file. To do so, use the option --log-file= filename . For example, to check the memory usage of the executable file hello and send profile information to output , use: See Section 5.1.3, "Additional information" for more information on Valgrind , along with other available documentation on the Valgrind suite of tools. 5.1.3. Additional information For more extensive information on Valgrind , see man valgrind . Red Hat Enterprise Linux also provides a comprehensive Valgrind Documentation book available as PDF and HTML in: /usr/share/doc/valgrind- version /valgrind_manual.pdf /usr/share/doc/valgrind- version /html/index.html

Commands:
~]USD valgrind --tool= toolname program
~]USD valgrind --tool=memcheck --log-file=output hello