Mtrace

From PHASTA Wiki
Jump to: navigation, search

If you suspect that you have a memory leak, it is possible to get libc to log heap allocations to a text file using the "mtrace" call.

Serial

At it's simplest, to use mtrace you need to get your code to call the mtrace() function from the "mcheck.h" header as near to the beginning of execution as possible (or if you suspect you know where the leak is, right before the leak).

 #include <mcheck.h>
 
 int main(int x, char** y) { mtrace(); return(0); }

Then, prior to running your program, you must set the "MALLOC_TRACE" environment variable to the name of the file you'd like mtrace() to write it's information to.

 export MALLOC_TRACE=memory_allocations.dat
 ./a.out

Once you get the output, you can use the "mtrace" script to post process the results and try to tell you where any leaked memory was allocated

 mtrace ./a.out ./memory_allocations.dat

Parallel

Using mtrace() in parallel is similar to serial, but you need to ensure that each process gets a different value for MALLOC_TRACE You may be able to do this by passing options to mpirun, but it's probably simpler to write some code to do this:

 #include <mcheck.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 
 int main(int argc, char** argv) 
 {
    int rank;
    char* tracepath
    char* cwd = get_current_dir_name();
    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    asprintf(&tracepath, "%s/mtrace.dat.%d", cwd, rank);
    setenv("MALLOC_TRACE", tracepath, 1);
    mtrace()
    /*....*/
    MPI_Finalize();
 }

With this code applied, you can then simply run your program normally and analyze the results as before:

 mpirun -np 8 ./a.out
 mtrace ./a.out mtrace.dat.0
 mtrace ./a.out mtrace.dat.1
 #...

In PHASTA, you should probably put this code in common/phasta.cc