Software: Apache/2.0.54 (Fedora). PHP/5.0.4 uname -a: Linux mina-info.me 2.6.17-1.2142_FC4smp #1 SMP Tue Jul 11 22:57:02 EDT 2006 i686 uid=48(apache) gid=48(apache) groups=48(apache) Safe-mode: OFF (not secure) /usr/share/doc/valgrind-2.4.0/ drwxr-xr-x |
Viewing file: Select action/file-type: 7 Massif: a heap profilerTo use this tool, you must specify--tool=massif
on the Valgrind command line.
7.1 Heap profilingMassif is a heap profiler, i.e. it measures how much heap memory programs use. In particular, it can give you information about:
7.2 Why Use a Heap Profiler?Everybody knows how useful time profilers are for speeding up programs. They are particularly useful because people are notoriously bad at predicting where are the bottlenecks in their programs.But the story is different for heap profilers. Some programming languages, particularly lazy functional languages like Haskell, have quite sophisticated heap profilers. But there are few tools as powerful for profiling C and C++ programs.
Why is this? Maybe it's because C and C++ programmers must think that
they know where the memory is being allocated. After all, you can see all the
calls to Massif can tell you these things. Or maybe it's because it's relatively easy to add basic heap profiling functionality into a program, to tell you how many bytes you have allocated for certain objects, or similar. But this information might only be simple like total counts for the whole program's execution. What about space usage at different points in the program's execution, for example? And reimplementing heap profiling code for each project is a pain. Massif can save you this effort. 7.3 OverviewFirst off, as for normal Valgrind use, you probably want to compile with debugging info (the-g flag). But, as opposed to Memcheck,
you probably do want to turn optimisation on, since you should profile
your program as it will be normally run.
Then, run your program with
It also puts detailed information about heap consumption in a file file
7.4 Basic Results of ProfilingTo gather heap profiling information about the programprog ,
type:
The program will execute (slowly). Upon completion, summary statistics that look like this will be printed: ==27519== Total spacetime: 2,258,106 ms.B ==27519== heap: 24.0% ==27519== heap admin: 2.2% ==27519== stack(s): 73.7%All measurements are done in spacetime, i.e. space (in bytes) multiplied by time (in milliseconds). Note that because Massif slows a program down a lot, the actual spacetime figure is fairly meaningless; it's the relative values that are interesting. Which entries you see in the breakdown depends on the command line options given. The above example measures all the possible parts of memory:
7.5 Spacetime GraphsAs well as printing summary information, Massif also creates a file representing a spacetime graph,massif.pid.hp .
It will produce a file called massif.pid.ps , which can be
viewed in a PostScript viewer.
Massif uses a program called
Here's an example graph: The graph is broken into several bands. Most bands represent a single line of your program that does some heap allocation; each such band represents all the allocations and deallocations done from that line. Up to twenty bands are shown; less significant allocation sites are merged into "other" and/or "OTHER" bands. The accompanying text/HTML file produced by Massif has more detail about these heap allocation bands. Then there are single bands for the stack(s) and heap admin bytes.
Note: it's the height of a band that's important. Don't let the ups and downs
caused by other bands confuse you. For example, the
The triangles on the x-axis show each point at which a memory census was taken. These aren't necessarily evenly spread; Massif only takes a census when memory is allocated or deallocated. The time on the x-axis is wallclock time, which is not ideal because you can get different graphs for different executions of the same program, due to random OS delays. But it's not too bad, and it becomes less of a problem the longer a program runs. Massif takes censuses at an appropriate timescale; censuses take place less frequently as the program runs for longer. There is no point having more than 100-200 censuses on a single graph. The graphs give a good overview of where your program's space use comes from, and how that varies over time. The accompanying text/HTML file gives a lot more information about heap use. 7.6 Details of Heap AllocationsThe text/HTML file contains information to help interpret the heap bands of the graph. It also contains a lot of extra information about heap allocations that you don't see in the graph.Here's part of the information that accompanies the above graph. == 0 =========================== Heap allocation functions accounted for 50.8% of measured spacetime Called from:
The first part shows the total spacetime due to heap allocations, and the places in the program where most memory was allocated (nb: if this program had been compiled with -g , actual line numbers would be given). These
places are sorted, from most significant to least, and correspond to the bands
seen in the graph. Insignificant sites (accounting for less than 0.5% of total
spacetime) are omitted.
That alone can be useful, but often isn't enough. What if one of these
functions was called from several different places in the program? Which one
of these is responsible for most of the memory used? For
== 1 =========================== Called from:
At this level, we can see all the places from which _nl_load_locale_from_archive() was called such that it allocated
memory at 0x401767D0. (We can click on the top 22.1%
link to go back to the parent entry.) At this level, we have moved beyond the
information presented in the graph. In this case, it is only called from one
place. We can again follow the link for more detail, moving to the following
part of the file.
== 2 =========================== Called from: In this way we can dig deeper into the call stack, to work out exactly what sequence of calls led to some memory being allocated. At this point, with a call depth of 3, the information runs out (thus the address of the child entry, 0x40176184, isn't a link). We could rerun the program with a greater --depth value if we wanted more information.
Sometimes you will get a code location like this:
Massif produces this information in a plain text file by default, or HTML with
the 7.7 Massif optionsMassif-specific options are:
7.8 AccuracyThe information should be pretty accurate. Some approximations made might cause some allocation contexts to be attributed with less memory than they actually allocated, but the amounts should be miniscule.The heap admin spacetime figure is an approximation, as described above. If anyone knows how to improve its accuracy, please let us know. |
:: Command execute :: | |
--[ c99shell v. 1.0 pre-release build #16 powered by Captain Crunch Security Team | http://ccteam.ru | Generation time: 0.0029 ]-- |