Patch for getrusage

Colten Edwards (edwards@panasync.canuck.ca)
Sun, 10 Mar 1996 03:03:31 -0600 (CST)


I was on irc last night and someone mentioned that getrusage didn't
return the correct information on linux systems so I had a quick look and
found the well we don't return anything for mem usage in getrusage. So I
looked around and found that fs/proc/array.c basically returns most of
the getrusage structure. I copied a part of that function over to
kernel/sys.c and came up with this solution. Don't know if it's the best
solution but it seems to work. Notice the linus coding style :)

*** kernel/sys.c.old Fri Mar 8 02:40:32 1996
--- kernel/sys.c Sun Mar 10 02:55:16 1996
***************
*** 805,810 ****
--- 805,847 ----
}

/*
+ * Instead of doing this three times a function to fill in the
+ * mem usage of rusage is used. This was taken from fs/proc/array.c
+ * should we check for the init process as well ??
+ * Colten Edwards edwac@sasknet.sk.ca
+ */
+
+ static void get_memrusage(struct task_struct *p, struct rusage *ru)
+ {
+ struct vm_area_struct * vma = p->mm->mmap;
+ unsigned long data = 0, stack = 0;
+ unsigned long exec = 0, lib = 0;
+
+ if (p->mm) {
+ for (vma = p->mm->mmap; vma; vma = vma->vm_next) {
+ unsigned long len = (vma->vm_end - vma->vm_start) >> 10;
+ if (!vma->vm_inode) {
+ data += len;
+ if (vma->vm_flags & VM_GROWSDOWN)
+ stack += len;
+ continue;
+ }
+ if (vma->vm_flags & VM_WRITE)
+ continue;
+ if (vma->vm_flags & VM_EXEC) {
+ exec += len;
+ if (vma->vm_flags & VM_EXECUTABLE)
+ continue;
+ lib += len;
+ }
+ }
+ ru->ru_maxrss = p->mm->rss << (PAGE_SHIFT-10);
+ ru->ru_ixrss = p->mm->locked_vm << (PAGE_SHIFT-10);
+ ru->ru_idrss = data - stack;
+ ru->ru_isrss = stack;
+ }
+ }
+
+ /*
* It would make sense to put struct rusage in the task_struct,
* except that would make the task_struct be *really big*. After
* task_struct gets moved into malloc'ed memory, it would
***************
*** 830,835 ****
--- 867,873 ----
r.ru_minflt = p->min_flt;
r.ru_majflt = p->maj_flt;
r.ru_nswap = p->nswap;
+ get_memrusage(p, &r);
break;
case RUSAGE_CHILDREN:
r.ru_utime.tv_sec = CT_TO_SECS(p->cutime);
***************
*** 839,844 ****
--- 877,883 ----
r.ru_minflt = p->cmin_flt;
r.ru_majflt = p->cmaj_flt;
r.ru_nswap = p->cnswap;
+ get_memrusage(p, &r);
break;
default:
r.ru_utime.tv_sec = CT_TO_SECS(p->utime + p->cutime);

Colten Edwards