confusion over getrusage(2)

2010-12-29 Thread patsy
Hello list,

I have written a program and would like to trace its memory usage as it runs 
and output the data to a file for easy logging/comparison/graphing.

getrusage(2) looks as though it would be appropriate for this. Unfortunately I 
seem to be misunderstanding how it should be used, since I would not expect the 
program below to output five 0's, yet it does on 4.8-release (i386) and a 
slightly out of date 4.8-current (amd64).

Could somebody give me a hint as to where my misunderstanding lies or how else 
I might track the memory usage of my program?

Many thanks,
Patsy



#include stdio.h
#include sys/time.h
#include sys/resource.h

int main()
{
struct rusage val;
int retval;
long buf[1];

retval = getrusage(RUSAGE_SELF, val);

printf(retval = %d\n, retval);
printf(mem = %ld\n, val.ru_maxrss);
printf(mem = %ld\n, val.ru_ixrss);
printf(mem = %ld\n, val.ru_idrss);
printf(mem = %ld\n, val.ru_isrss);

return 0;
}



Re: confusion over getrusage(2)

2010-12-29 Thread Michael Grigoni
On 29 Dec 2010 at 14:25, patsy wrote:

 Hello list,
 
 I have written a program and would like to trace its memory usage as it runs 
 and output the data to a file for easy logging/comparison/graphing.
 
 getrusage(2) looks as though it would be appropriate for this. Unfortunately 
 I seem to be misunderstanding how it should be used, since I would not expect 
 the program below to output five 0's, yet it does on 4.8-release (i386) and a 
 slightly out of date 4.8-current (amd64).
 
 Could somebody give me a hint as to where my misunderstanding lies or how 
 else I might track the memory usage of my program?
 
 Many thanks,
 Patsy
 
 
 
 #include stdio.h
 #include sys/time.h
 #include sys/resource.h
 
 int main()
 {
   struct rusage val;
   int retval;
   long buf[1];
 
   retval = getrusage(RUSAGE_SELF, val);
 
   printf(retval = %d\n, retval);
   printf(mem = %ld\n, val.ru_maxrss);
   printf(mem = %ld\n, val.ru_ixrss);
   printf(mem = %ld\n, val.ru_idrss);
   printf(mem = %ld\n, val.ru_isrss);
 
   return 0;
 }
 

Got my attention -- discovered a missing declaration in 
/usr/include/sys/resource.h
on my SVR4 system -- added it from Solaris, compiled your program, an on SVR4
I get:

# ./rusage
retval = 0
mem = 0
mem = 0
mem = 0
mem = 0

Anyway, I appreciate discovering the omission for the structure declaration in 
the
header file (not obsd however)

Michael



Re: confusion over getrusage(2)

2010-12-29 Thread Philip Guenther
On Wed, Dec 29, 2010 at 6:25 AM, patsy open...@ethernull.org wrote:
 I have written a program and would like to trace its memory usage as it runs
and output the data to a file for easy logging/comparison/graphing.

 getrusage(2) looks as though it would be appropriate for this. Unfortunately
I seem to be misunderstanding how it should be used, since I would not expect
the program below to output five 0's, yet it does on 4.8-release (i386) and a
slightly out of date 4.8-current (amd64).

 Could somebody give me a hint as to where my misunderstanding lies or how
else I might track the memory usage of my program?
...
printf(mem = %ld\n, val.ru_maxrss);
printf(mem = %ld\n, val.ru_ixrss);
printf(mem = %ld\n, val.ru_idrss);
printf(mem = %ld\n, val.ru_isrss);

Support for those four members of struct rusage, as well as the
ru_nswap member, is not implemented in OpenBSD.

You can get the RSS value reported by ps (and top, etc) by using
kvm_getproc2(), presumably with the KERN_PROC_PID op to request just
the process you care about; the RSS value is is in the p_vm_rssize
member.


Philip Guenther