Subject: collecting rusage memory stats
To: None <tech-kern@netbsd.org>
From: Simon Burge <simonb@wasabisystems.com>
List: tech-kern
Date: 10/13/2006 23:09:22
--gBBFr7Ir9EOA20Yy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Anyone have a problem with the attached patch?  It's derived from
FreeBSD and calculates the memory usage stats in struct rusage.  Note
that in these days of modern machines with large amounts of memory
that the integral value fields are pretty useless for any mildly large
program that runs for any decent amount of time.  Changing the rusage
fields to be int64_t's is beyond the scope of what I want to do :)

Cheers,
Simon.
--
Simon Burge                                   <simonb@wasabisystems.com>
NetBSD Development, Support and Service:   http://www.wasabisystems.com/

This email and any attached documents may be confidential and property
of Wasabi Systems.  If you are not the intended recipient, you may not
disclose, copy, distribute, or act in reliance on the information in
this email.

--gBBFr7Ir9EOA20Yy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="kern_clock.c.diff"

Index: kern_clock.c
===================================================================
RCS file: /cvsroot/src/sys/kern/kern_clock.c,v
retrieving revision 1.102
diff -d -p -u -r1.102 kern_clock.c
--- kern_clock.c	2 Sep 2006 06:21:32 -0000	1.102
+++ kern_clock.c	13 Oct 2006 13:06:21 -0000
@@ -1153,6 +1153,9 @@ statclock(struct clockframe *frame)
 	struct schedstate_percpu *spc = &ci->ci_schedstate;
 	struct proc *p;
 	struct lwp *l;
+	struct vmspace *vm;
+	struct rusage *ru;
+	long rss;
 
 	/*
 	 * Notice changes in divisor frequency, and adjust clock
@@ -1230,7 +1233,21 @@ statclock(struct clockframe *frame)
 	spc->spc_pscnt = psdiv;
 
 	if (p != NULL) {
+		/* Update CPU time ticks used for the scheduler. */
 		++p->p_cpticks;
+
+		/* Update resource usage integrals and maximums. */
+#define	pgtok(a)	(((u_long) ((a) * PAGE_SIZE) / 1024))
+		vm = p->p_vmspace;
+		ru = &p->p_stats->p_ru;
+		ru->ru_ixrss += pgtok(vm->vm_tsize);
+		ru->ru_idrss += pgtok(vm->vm_dsize);
+		ru->ru_isrss += pgtok(vm->vm_ssize);
+		rss = pgtok(vm_resident_count(vm));
+		if (ru->ru_maxrss < rss)
+			ru->ru_maxrss = rss;
+#undef pgtok
+
 		/*
 		 * If no separate schedclock is provided, call it here
 		 * at about 16 Hz.

--gBBFr7Ir9EOA20Yy--