tags:

views:

376

answers:

4

I need to figure out which part of a linux program that I am running, is taking how much (either percentage, or absolute) memory. I need to create a profile of multiple such programs, so that I can identify some of the bigger consumers of memory in my code, and see if I can optimize them to use less. I need it on MIPS platform, and unfortunately, Valgrind doesn't work on MIPS.

Any help/pointers would be greatly appreciated.

A: 

Memory consumption should not be massively affected by the underlying processor architecture so you might be able to do the memory profiling on x86 Linux. Yes, the absolute amounts of memory probably are a affected but as you're looking more for relative than absolute numbers, this should work.

That said, this solution is unlikely to be an option if a Linux x86 build is more than a recompile away.

Timo Geusch
...assuming that porting from MIPS to x86 is of that program is trivial.
Laurynas Biveinis
Admittedly, that would be the fly in the ointment.
Timo Geusch
Yes. The program that I want to profile, if I have to port that program and all associated libraries to x86, it could run into months :-). So this doesn't work for me
Harty
Fair enough, if it's more than a simple recompile it's probably not the best solution...
Timo Geusch
+4  A: 

You could wrap all your calls to free and malloc with your own functions in which you also supply for instance in which file and at what line number each allocation is done. From this information it's easy to see what memory is being used where.

Andreas Brinck
override them runtime with LD_PRELOAD...
Stefano Borini
@Stefano, This won't work if you want extra arguments to your `malloc` calls, will it? (I'm a complete Linux n00b)
Andreas Brinck
It should work. Check here : http://www-2.cs.cmu.edu/afs/cs.cmu.edu/academic/class/15213-s03/src/interposition/mymalloc.c
Stefano Borini
oh wait.. why would you need to pass extra args ?
Stefano Borini
@Stefano, How will you know which code is allocating what memory if you're not passing this information to your wrapped `malloc`? In my C++ code I pass __FILE__ and __LINE__ into `new` for this purpose.
Andreas Brinck
@Stefano, that should have been `__FILE__` and `__LINE__`
Andreas Brinck
+3  A: 

Beside Valgrind, there exists a lot of other memory debugger/profiler. All the following seems to support MIPS (but I've not tried them on that architecture) :

CCMALLOC, mpatrol, NJAMD, Dmalloc, and even Google's own google-perftools.

Laurent Parenteau
A: 

You can use Google's perftools for memory profiling. The project provides a very fast, multi-threaded malloc implementation, a Heap profiler, a Heap checker and a CPU profiler.

dmeister