OT: Memory profiling on Linux

R

Ram Prasad

I have written a c milter for my mail server.
I need to profile my milter daemon on linux. I have used memprof
before , but cant seem to get memprof installed on my Centos 4.4 box.

Which tool do you folks use for profiling.
I just want to see is my daemon process has any memory leaks and where
are they.

Thanks
Ram


PS: Sorry for the OT post. But I am not able to get a decent tool for
profiling. I assume that will be a very common requirement for C
programmers
 
D

Dave Vandervies

I have written a c milter for my mail server.
I need to profile my milter daemon on linux. I have used memprof
before , but cant seem to get memprof installed on my Centos 4.4 box.

Which tool do you folks use for profiling.
I just want to see is my daemon process has any memory leaks and where
are they.

The comp.lang.c answer is to use wrappers for the memory allocation
functions that log allocations and deallocations.
Then you can go through the allocation log, and any allocations that don't
get deallocated are probably leaks (though for a long-running process
some of them will be one-time allocations that shouldn't get deallocated).

So you could use something like this:
--------
/*mymalloc.h*/
/*WARNING!! Not compiled or tested, for explanatory purposes only*/
#ifndef H_MYMALLOC
#define H_MYMALLOC
#ifdef MYMALLOC_DEBUG
#include <stddef.h>
void *mymalloc(size_t sz,int line,const char *file);
void *myrealloc(void *p,size_t sz,int line,const char *file);
void myfree(void *p,int line,const char *file);
#define MALLOC(sz) mymalloc(sz,__LINE__,__FILE__)
#define REALLOC(p,sz) myrealloc(p,sz,__LINE__,__FILE__)
#define FREE(p) myfree(p,__LINE__,__FILE__)
#else
#include <stdlib.h>
#define MALLOC malloc
#define REALLOC realloc
#define FREE free
#endif
--------
/*mymalloc.c*/
/*WARNING!! Not compiled or tested, for explanatory purposes only*/
#include <stdio.h>
#include <stdlib.h>
#include "mymalloc.h"

#define LOGFILE "malloc-log"

void *mymalloc(size_t sz,int line,const char *file)
{
void *ptr=malloc(sz);
FILE *out=fopen(LOGFILE,"a");
if(out)
{
fprintf(out,"malloc(%s:%d): requested %lu bytes, malloc returned %p\n",
file,line,(unsigned long)sz,ptr);
fclose(out);
}
return ptr;
}

void *myrealloc(void *oldptr,size_t sz,int line,const char *file)
{
void *newptr;
FILE *out=fopen(LOGFILE,"a");
if(out)
{
fprintf(out,"realloc(%s:%d): old pointer %p, requested %lu bytes, ",
file,line,oldptr,(unsigned long)sz);
}
newptr=realloc(oldptr,sz);
if(out)
{
fprintf(out,"realloc returned %p\n");
fclose(out);
}
return newptr;
}

void *myfree(void *ptr,int line,const char *file)
{
FILE *out=fopen(LOGFILE,"a");
if(out)
{
fprintf(out,"free(%s:%d): freeing %p\n",file,line,ptr);
fclose(out);
}
free(ptr);
}
--------
Replace all of your calls to malloc, realloc, and free with the MALLOC,
REALLOC, and FREE macros, and for your leak-testing build define
MYMALLOC_DEBUG and include mymalloc.c in the build.

Most automated memory profilers will (I expect) do something like this
with a built-in analysis instead of writing a log file and then doing
the analysis offline (which using these wrappers you'll have to either
do by hand or build a tool to do).


dave
 
K

Keith Thompson

Ram Prasad said:
I have written a c milter for my mail server.
I need to profile my milter daemon on linux. I have used memprof
before , but cant seem to get memprof installed on my Centos 4.4 box.

Which tool do you folks use for profiling.
I just want to see is my daemon process has any memory leaks and where
are they.

Thanks
Ram


PS: Sorry for the OT post. But I am not able to get a decent tool for
profiling. I assume that will be a very common requirement for C
programmers

Profiling tools tend to be system-specific. Try comp.unix.programmer
or one of the Linux groups. (But try Google first.)
 
D

Duncan Muirhead

The comp.lang.c answer is to use wrappers for the memory allocation
functions that log allocations and deallocations.
Then you can go through the allocation log, and any allocations that don't
get deallocated are probably leaks (though for a long-running process
some of them will be one-time allocations that shouldn't get deallocated).
<snip>
continuing OT, on linux a "man malloc_hook" might be informative.
There's also valgrind:
http://valgrind.org/
Duncan
 
G

Giorgos Keramidas

What is a milter?

<off-topic>
A "mail filter" which Sendmail can hook into, to perform email
processing at various stages of the delivery of a message.
Look at your compiler documentation. OT here.

Agreed :) Both milters and memory usage profiling are off-topic here,
and best discussed in other Usenet groups.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top