> > 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:}