Mem Usage of own process

Mem Usage of own process

Post by Norbert Sense » Sat, 12 Dec 1998 04:00:00



Hello,
how can a process/programm determine his own memory usage
while it is running ?
How big is the difference of the memory allocated by new/malloc
and the really used memory ? Is the management-overhead of
these malloc-routines the only one ?

Thanks for any hints,

Norbert
--
------------------------------------------------------------------
Norbert Sensen            
University of Paderborn       office: F2.411
Dept. of Computer Science     phone: +49 +5251 60-6728

33102 Paderborn               http://www.uni-paderborn.de/cs/sensen
Germany
------------------------------------------------------------------

 
 
 

Mem Usage of own process

Post by F.R.M.Barn » Tue, 15 Dec 1998 04:00:00


: Hello,
: how can a process/programm determine his own memory usage
: while it is running ?
: How big is the difference of the memory allocated by new/malloc
: and the really used memory ? Is the management-overhead of
: these malloc-routines the only one ?

getrusage()

Fred.
--
+----------------------------------------------------------------------+
| Fred Barnes, CS Student, UKC                http://teddy.xylene.com/ |

+----------------------------------------------------------------------+

 
 
 

Mem Usage of own process

Post by Hein Roehri » Tue, 15 Dec 1998 04:00:00




> : Hello,
> : how can a process/programm determine his own memory usage
> : while it is running ?
> : How big is the difference of the memory allocated by new/malloc
> : and the really used memory ? Is the management-overhead of
> : these malloc-routines the only one ?

> getrusage()

Unfortunately, the relevant fields are not implemented in major Unix
versions (e.g. Solaris, but also Linux, AFAIK).

I resorted to extracting information from the /proc file system
(Solaris and Linux). Mail me if interested in code snippets.

Regards,
Hein

 
 
 

Mem Usage of own process

Post by Norbert Sense » Tue, 15 Dec 1998 04:00:00




> > getrusage()

> Unfortunately, the relevant fields are not implemented in major Unix
> versions (e.g. Solaris, but also Linux, AFAIK).

That's it. Here i'm using Solaris and getrusage fills only
the time-fields, all other fields are zero !!!

Quote:

> I resorted to extracting information from the /proc file system
> (Solaris and Linux). Mail me if interested in code snippets.

That sounds interesting. What kind of code is it ? Could i
simply compile it with my programm and call a routine which then
would return the used memory ? This would be great.

Thanks,

Norbert Sensen

--
------------------------------------------------------------------
Norbert Sensen            
University of Paderborn       office: F2.411
Dept. of Computer Science     phone: +49 +5251 60-6728

33102 Paderborn               http://www.uni-paderborn.de/cs/sensen
Germany
------------------------------------------------------------------

 
 
 

Mem Usage of own process

Post by Hein Roehri » Tue, 15 Dec 1998 04:00:00



> > I resorted to extracting information from the /proc file system
> > (Solaris and Linux). Mail me if interested in code snippets.
> That sounds interesting. What kind of code is it ? Could i
> simply compile it with my programm and call a routine which then
> would return the used memory ? This would be great.

On Solaris, check out man -s4 proc. The following code is for Solaris
2.6 and then for Linux. On Linux, you need to link against
libproc. These functions give you the virtual size of your
process. Other values can be obtained similarly.

Regards,
Hein Roehrig

Solaris:

#include <assert.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <procfs.h>

static inline
int
pfile(pid_t pid, const char *file, char **result)
{
    int alloclen=80;
    while (1) {
        int len;
        *result=(char*)malloc(alloclen);
        if (*result==NULL)
            return -1;
        len=snprintf(*result, alloclen, "/proc/%d/%s", pid, file);
        if (len>alloclen) {
            free(*result);
            alloclen*=2;
        } else
            return 0;
    }

Quote:}

static inline
void *
readbin(const char *filename)
{
    int fd;
    struct stat s;
    void *buf;
    ssize_t rb;
    fd=open(filename, O_RDONLY);
    if (fd==-1)
        return 0;
    if (-1==fstat(fd, &s) || s.st_size==0) {
        close(fd);
        return NULL;
    }
    buf=(void*)malloc(s.st_size);
    if (buf==NULL) {
        close(fd);
        return NULL;
    }
    rb=read(fd, buf, s.st_size);
    if (rb==-1) {
        free(buf);
        close(fd);
        return NULL;
    }
    assert(rb==s.st_size);
    close(fd);
    return buf;

Quote:}

static inline
struct psinfo *
read_psinfo(pid_t pid)
{
    struct psinfo *p;
    char *filename;
    pfile(pid, "psinfo", &filename);
    if (filename==NULL)
        return NULL;
    p=(struct psinfo *)readbin(filename);
    free(filename);
    return p;

Quote:}

unsigned long
pstat_getvsize(pid_t pid)
{
    size_t s;
    struct psinfo *p=read_psinfo(pid);
    if (p==NULL)
        return (unsigned long)-1L;

    s=p->pr_size;
    free(p);
    return s;

Quote:}

Linux:

#include <sys/types.h>
#include <proc/readproc.h>

unsigned long
pstat_getvsize(pid_t pid)
{
    unsigned long vsize=0;
    pid_t pids[2] = { pid, 0 };
    proc_t **pt=readproctab(PROC_PID, pids);
    if (pt==NULL)
        return 0;
    if (pt[0]!=NULL && pt[1]==NULL)
        vsize=pt[0]->vsize;
    freeproctab(pt);
    return vsize/1024;

Quote:}