OT: Memory profiling on Linux

Discussion in 'C Programming' started by Ram Prasad, May 1, 2007.

  1. Ram  Prasad

    Ram Prasad Guest

    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
    Ram Prasad, May 1, 2007
    #1
    1. Advertising

  2. In article <>,
    Ram Prasad <> wrote:
    >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

    --
    Dave Vandervies
    I prefer to display my ignorance on comp.lang.c where I can understand
    the corrections.
    --Morris Dovey in comp.lang.c
    Dave Vandervies, May 1, 2007
    #2
    1. Advertising

  3. Ram Prasad <> writes:
    > 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.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, May 1, 2007
    #3
  4. On Tue, 01 May 2007 16:37:56 +0000, Dave Vandervies wrote:

    > In article <>,
    > Ram Prasad <> wrote:
    >>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).
    >

    <snip>
    continuing OT, on linux a "man malloc_hook" might be informative.
    There's also valgrind:
    http://valgrind.org/
    Duncan
    Duncan Muirhead, May 1, 2007
    #4
  5. In article <>,
    CBFalconer <> wrote:

    >What is a milter?


    Milt is the seminal fluid of fish. So you can probably deduce what
    a milter is.

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
    Richard Tobin, May 1, 2007
    #5
  6. On Tue, 01 May 2007 17:27:15 -0400, CBFalconer <> wrote:
    > Ram Prasad wrote:
    >> 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.

    >
    > 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.
    </off-topic>

    >> 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.

    >
    > 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.
    Giorgos Keramidas, May 2, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patrick
    Replies:
    3
    Views:
    465
    Nigel Wade
    Jul 19, 2004
  2. dayjah
    Replies:
    3
    Views:
    12,696
  3. Rune Froysa

    Memory profiling

    Rune Froysa, Sep 24, 2003, in forum: Python
    Replies:
    1
    Views:
    359
    Michael Hudson
    Sep 25, 2003
  4. Jorgen Grahn

    Re: GNU C++ Profiling under Linux

    Jorgen Grahn, Jun 14, 2009, in forum: C++
    Replies:
    0
    Views:
    597
    Jorgen Grahn
    Jun 14, 2009
  5. James Kanze

    Re: Profiling under Linux

    James Kanze, Nov 5, 2009, in forum: C++
    Replies:
    1
    Views:
    351
    Gerhard Fiedler
    Nov 9, 2009
Loading...

Share This Page