2006-05-28

Broken getrusage() in Linux

I was writing a program and wanted to measure both its execution time and used memory. For time measurement, getrusage(3) works just fine. However, it returned 0 for memory usage. A quote from the man page:
"The above struct was taken from BSD 4.3 Reno. Not all fields are meaningful under Linux. Right now (Linux 2.4, 2.6) only the fields ru_utime, ru_stime, ru_minflt, ru_majflt, and ru_nswap are maintained."
(This refers to struct rusage, filled in by the getrusage() function.)

The question is why? At least some of memory statistics that should be returned by getrusage() can already be obtained through /proc/*/stat.

Tags:

3 comments:

Unknown said...

Hi. Excellent question -- why can't I get a process' memory usage (RSS, code/text size, stack/data size, etc) by calling getrusage() in Linux? If anyone out there knows of an equivalent system call, PLEASE tell me. And no, I don't want to dig it out of /proc... that is slow and hoakey. There should be a system call for this...

Linas said...

I agree. I'd like to see a working getrusage for process memory consumption. Why, I needed it just today ...

Unknown said...

I totally agree. Using /proc feels so wrong and unportable.

I made the mistake of just assuming getrusage() works just fine in Linux. Didn't realize my mistake until after I committed to CVS and got others in my group complaining that memory usage was coming out as 0b in all cases... :(

If I was motivated I would write a patch for the kernel and submit it.