Blame optimization-guide/C/massif.page

Packit 1470ea
Packit 1470ea
      type="guide" style="task"
Packit 1470ea
      id="massif">
Packit 1470ea
    <info>
Packit 1470ea
      <link type="guide" xref="index#massif"/>
Packit 1470ea
    </info>
Packit 1470ea
      <title>Using <app>Massif</app> for Profiling Memory Use in GNOME Software</title>
Packit 1470ea
Packit 1470ea
    

Packit 1470ea
        This article describes how to use the <app>Massif</app> heap profiler with GNOME applications. We describe how to invoke, interpret, and act on the output of <app>Massif</app>. The <app>Swell Foop</app> game is used as an example. 

Packit 1470ea
   <section id="optimization-massif-TBL-intro">
Packit 1470ea
        <title>Introduction</title>
Packit 1470ea
        

Packit 1470ea
            <app>Massif</app> is a member of the <link href="http://valgrind.org/">valgrind</link> suite of memory-profiling tools. Its purpose is to give a detailed view of dynamic memory usage during the lifetime of the program. Specifically it records the memory use of the heap and the stack.
Packit 1470ea
        

Packit 1470ea
        

Packit 1470ea
            The heap is the region of memory which is allocated with functions like malloc. It grows on demand and is usually the largest region of memory in a program. The stack is where all the local data for functions is stored. This includes the "automatic" variables in C and the return address for subroutines. The stack is typically a lot smaller and a lot more active than the heap. We won't consider the stack explicitly since <app>Massif</app> treats it as though it were just another part of the heap. <app>Massif</app> also gives information about how much memory is used to manage the heap.  

Packit 1470ea
        

Packit 1470ea
            <app>Massif</app> produces two output files: a graphical overview in a postscript file and a detailed breakdown in a text file.
Packit 1470ea
        

Packit 1470ea
    </section>
Packit 1470ea
    <section id="optimization-massif-TBL-using-massif">
Packit 1470ea
        <title>Using <app>Massif</app> with GNOME</title>
Packit 1470ea
        

Packit 1470ea
          <app>Massif</app> has very few options and for many programs does not need them. However for GNOME applications, where memory allocation might be buried deep in either glib or GTK, the number of levels down the call-stack Massif descends needs to be increased. This is achieved using the --depth parameter. By default this is 3; increasing it to 5 will guarantee the call-stack reaches down to your code. One or two more levels may also be desirable to provide your code with some context. Since the level of detail becomes quickly overwhelming it is best to start with the smaller depth parameter and only increase it when it becomes apparent that it isn't sufficient.
Packit 1470ea
        

Packit 1470ea
        

Packit 1470ea
            It is also useful to tell <app>Massif</app> which functions allocate memory in glib. It removes an unnecessary layer of function calls from the reports and gives you a clearer idea of what code is allocating memory. The allocating functions in glib are g_malloc, g_malloc0, g_realloc, g_try_malloc, and g_mem_chunk_alloc. You use the --alloc-fn option to tell Massif about them.
Packit 1470ea
        

Packit 1470ea
        

Packit 1470ea
            Your command-line should therefore look something like:
Packit 1470ea
        

Packit 1470ea
        
Packit 1470ea
valgrind --tool=massif --depth=5  --alloc-fn=g_malloc --alloc-fn=g_realloc --alloc-fn=g_try_malloc \
Packit 1470ea
         --alloc-fn=g_malloc0 --alloc-fn=g_mem_chunk_alloc swell-foop
Packit 1470ea
        
Packit 1470ea
        

Packit 1470ea
            <app>Swell Foop</app> is the program we will be using as an example. Be warned that, since valgrind emulates the CPU, it will run very slowly. You will also need a lot of memory. 

Packit 1470ea
    </section>
Packit 1470ea
    <section id="optimization-massif-TBL-interpreting-results">
Packit 1470ea
        <title>Interpreting the Results</title>
Packit 1470ea
        

Packit 1470ea
            The graphical output of <app>Massif</app> is largely self explanatory. Each band represents the memory allocated by one function over time. Once you identify which bands are using the most memory, usually the big thick ones at the top you will have to consult the text file for the details.
Packit 1470ea
        

Packit 1470ea
        

Packit 1470ea
            The text file is arranged as a hierarchy of sections, at the top is a list of the worst memory users arranged in order of decreasing spacetime. Below this are further sections, each breaking the results down into finer detail as you proceed down the call-stack. To illustrate this we will use the output of the command above.
Packit 1470ea
        

Packit 1470ea
        <figure>
Packit 1470ea
            <title><app>Massif</app> output for the unoptimized version of the <app>Swell Foop</app> program.</title>
Packit 1470ea
            <media type="image" src="figures/massif-before.png"/>
Packit 1470ea
         </figure>
Packit 1470ea
        

Packit 1470ea
            The image above shows a typical postscript output from
Packit 1470ea
            <app>Massif</app>. This is the result you would get from
Packit 1470ea
            playing a single game of <app>Swell Foop</app> (version
Packit 1470ea
            2.8.0) and then quitting. The postscript file will have a
Packit 1470ea
            name like <file>massif.12345.ps</file> and the text file
Packit 1470ea
            will be called <file>massif.12345.txt</file>. The number
Packit 1470ea
            in the middle is the process ID of the program that was
Packit 1470ea
            examined. If you actually try this example you will find
Packit 1470ea
            two versions of each file, with slightly different
Packit 1470ea
            numbers, this is because <app>Swell Foop</app> starts a
Packit 1470ea
            second process and <app>Massif</app>
Packit 1470ea
            follows that too. We will ignore this second process, it
Packit 1470ea
            consumes very little memory.

Packit 1470ea
        

Packit 1470ea
            At the top of the graph we see a large yellow band labelled gdk_pixbuf_new. This seems like an ideal candidate for optimization, but we will need to use the text file to find out what is calling gdk_pixbuf_new. The top of the text file will look something like this:
Packit 1470ea
        

Packit 1470ea
        
Packit 1470ea
Command: ./swell-foop
Packit 1470ea
Packit 1470ea
== 0 ===========================
Packit 1470ea
Heap allocation functions accounted for 90.4% of measured spacetime
Packit 1470ea
Packit 1470ea
Called from:
Packit 1470ea
  28.8% : 0x6BF83A: gdk_pixbuf_new (in /usr/lib/libgdk_pixbuf-2.0.so.0.400.9)
Packit 1470ea
Packit 1470ea
    6.1% : 0x5A32A5: g_strdup (in /usr/lib/libglib-2.0.so.0.400.6)
Packit 1470ea
Packit 1470ea
    5.9% : 0x510B3C: (within /usr/lib/libfreetype.so.6.3.7)
Packit 1470ea
Packit 1470ea
    3.5% : 0x2A4A6B: __gconv_open (in /lib/tls/libc-2.3.3.so)
Packit 1470ea
        
Packit 1470ea
        

Packit 1470ea
            The line with the '=' signs indicates how far down the stack trace we are, in this case we are at the top. After this it lists the heaviest users of memory in order of decreasing spacetime. Spacetime is the product of the amount of memory used and how long it was used for. It corresponds to the area of the bands in the graph. This part of the file tells us what we already know: most of the spacetime is dedicated to gdk_pixbuf_new. To find out what called gdk_pixbuf_new we need to search further down the text file:
Packit 1470ea
        

Packit 1470ea
        
Packit 1470ea
== 4 ===========================
Packit 1470ea
Context accounted for 28.8% of measured spacetime
Packit 1470ea
  0x6BF83A: gdk_pixbuf_new (in /usr/lib/libgdk_pixbuf-2.0.so.0.400.9)
Packit 1470ea
  0x3A998998: (within /usr/lib/gtk-2.0/2.4.0/loaders/libpixbufloader-png.so)
Packit 1470ea
  0x6C2760: (within /usr/lib/libgdk_pixbuf-2.0.so.0.400.9)
Packit 1470ea
  0x6C285E: gdk_pixbuf_new_from_file (in /usr/lib/libgdk_pixbuf-2.0.so.0.400.9)
Packit 1470ea
Packit 1470ea
Called from:
Packit 1470ea
  27.8% : 0x804C1A3: load_scenario (swell-foop.c:463)
Packit 1470ea
Packit 1470ea
    0.9% : 0x3E8095E: (within /usr/lib/libgnomeui-2.so.0.792.0)
Packit 1470ea
Packit 1470ea
  and 1 other insignificant place
Packit 1470ea
        
Packit 1470ea
        

Packit 1470ea
            The first line tells us we are now four levels deep into the stack. Below it is a listing of the function calls that leads from here to gdk_pixbuf_new. Finally there is a list of functions that are at the next level down and call these functions. There are, of course, also entries for levels 1, 2, and 3, but this is the first level to reach right down through the GDK code to the <app>Swell Foop</app> code. From this listing, we can see instantly that the problem code is load_scenario.
Packit 1470ea
        

Packit 1470ea
        

Packit 1470ea
            Now that we know what part of our code is using all the spacetime we can look at it and find out why. It turns out that the load_scenario is loading a pixbuf from file and then never freeing that memory. Having identified the problem code, we can start to fix it.
Packit 1470ea
        

Packit 1470ea
    </section>
Packit 1470ea
    <section id="optimization-massif-TBL-acting-on-results">
Packit 1470ea
        <title>Acting on the Results</title>
Packit 1470ea
        

Packit 1470ea
            Reducing spacetime consumption is good, but there are two ways of reducing it and they are not equal. You can either reduce the amount of memory allocated, or reduce the amount of time it is allocated for. Consider for a moment a model system with only two processes running. Both processes use up almost all the physical RAM and if they overlap at all then the system will swap and everything will slow down. Obviously if we reduce the memory usage of each process by a factor of two then they can peacefully coexist without the need for swapping. If instead we reduce the time the memory is allocated by a factor of two then the two programs can coexist, but only as long as their periods of high memory use don't overlap. So it is better to reduce the amount of memory allocated.
Packit 1470ea
        

Packit 1470ea
        

Packit 1470ea
            Unfortunately, the choice of optimization is also
Packit 1470ea
	    constrained by the needs of the program. The size of the
Packit 1470ea
	    pixbuf data in <app>Swell Foop</app> is determined by the
Packit 1470ea
	    size of the game's graphics and cannot be easily
Packit 1470ea
	    reduced. However, the amount of time it spends loaded into
Packit 1470ea
	    memory can be drastically reduced. The image below shows the <app>Massif</app> analysis of <app>Swell Foop</app> after being altered to dispose of the pixbufs once the images have been loaded into the X server.
Packit 1470ea
        

Packit 1470ea
        <figure>
Packit 1470ea
            <title><app>Massif</app> output for the optimized <app>Swell Foop</app> program.</title>
Packit 1470ea
           <media type="image" src="figures/massif-after.png" />
Packit 1470ea
            </figure>
Packit 1470ea
        

Packit 1470ea
            The spacetime use of gdk_pixbuf_new is now a thin band that only spikes briefly (it is now the sixteenth band down and shaded magenta). As a bonus, the peak memory use has dropped by 200 kB since the spike occurs before other memory is allocated. If two processes like this were run together the chances of the peak memory usage coinciding, and hence the risk of swapping, would be quite low.
Packit 1470ea
        

Packit 1470ea
        

Packit 1470ea
            Can we do better ? A quick examination of <app>Massif</app>'s text output reveals: g_strdup to be the new major offender.
Packit 1470ea
        

Packit 1470ea
        
Packit 1470ea
Command: ./swell-foop
Packit 1470ea
Packit 1470ea
== 0 ===========================
Packit 1470ea
Heap allocation functions accounted for 87.6% of measured spacetime
Packit 1470ea
Packit 1470ea
Called from:
Packit 1470ea
    7.7% : 0x5A32A5: g_strdup (in /usr/lib/libglib-2.0.so.0.400.6)
Packit 1470ea
Packit 1470ea
    7.6% : 0x43BC9F: (within /usr/lib/libgdk-x11-2.0.so.0.400.9)
Packit 1470ea
Packit 1470ea
    6.9% : 0x510B3C: (within /usr/lib/libfreetype.so.6.3.7)
Packit 1470ea
Packit 1470ea
    5.2% : 0x2A4A6B: __gconv_open (in /lib/tls/libc-2.3.3.so)
Packit 1470ea
        
Packit 1470ea
        

Packit 1470ea
            If we look closer though we see that it is called from many, many, places.
Packit 1470ea
        

Packit 1470ea
        
Packit 1470ea
== 1 ===========================
Packit 1470ea
Context accounted for  7.7% of measured spacetime
Packit 1470ea
  0x5A32A5: g_strdup (in /usr/lib/libglib-2.0.so.0.400.6)
Packit 1470ea
Packit 1470ea
Called from:
Packit 1470ea
    1.8% : 0x8BF606: gtk_icon_source_copy (in /usr/lib/libgtk-x11-2.0.so.0.400.9)
Packit 1470ea
Packit 1470ea
    1.1% : 0x67AF6B: g_param_spec_internal (in /usr/lib/libgobject-2.0.so.0.400.6)
Packit 1470ea
Packit 1470ea
    0.9% : 0x91FCFC: (within /usr/lib/libgtk-x11-2.0.so.0.400.9)
Packit 1470ea
Packit 1470ea
    0.8% : 0x57EEBF: g_quark_from_string (in /usr/lib/libglib-2.0.so.0.400.6)
Packit 1470ea
Packit 1470ea
  and 155 other insignificant places
Packit 1470ea
        
Packit 1470ea
        

Packit 1470ea
            We now face diminishing returns for our optimization efforts. The graph hints at another possible approach: Both the "other" and "heap admin" bands are quite large. This tells us that there are a lot of small allocations being made from a variety of places. Eliminating these will be difficult, but if they can be grouped then the individual allocations can be larger and the "heap admin" overhead can be reduced.
Packit 1470ea
        

Packit 1470ea
    </section>
Packit 1470ea
    <section id="optimization-massif-TBL-caveats">
Packit 1470ea
        <title>Caveats</title>
Packit 1470ea
        

Packit 1470ea
            There are a couple of things to watch out for: Firstly, spacetime is only reported as a percentage, you have to compare it to the overall size of the program to decide if the amount of memory is worth pursuing. The graph, with its kilobyte vertical axis, is good for this.
Packit 1470ea
        

Packit 1470ea
        

Packit 1470ea
            Secondly, <app>Massif</app> only takes into account the memory used by your own program. Resources like pixmaps are stored in the X server and aren't considered by <app>Massif</app>. In the <app>Swell Foop</app> example we have actually only moved the memory consumption from client-side pixbufs to server-side pixmaps. Even though we cheated there are performance gains. Keeping the image data in the X server makes the graphics routines quicker and removes a lot of inter-process communication. Also, the pixmaps will be stored in a native graphics format which is often more compact than the 32-bit RGBA format used by gdk_pixbuf. To measure the effect of pixmaps, and other X resources use the <link href="http://www.freedesktop.org/Software/xrestop">xrestop</link> program.
Packit 1470ea
        

Packit 1470ea
    </section>
Packit 1470ea
</page>