socsvn commit: r237311 - soc2012/rudot/sys/kern

rudot at FreeBSD.org rudot at FreeBSD.org
Fri Jun 8 15:09:33 UTC 2012


Author: rudot
Date: Fri Jun  8 15:09:30 2012
New Revision: 237311
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=237311

Log:
  more precise %cpu accounting - use also data for the yet unfinished second

Modified:
  soc2012/rudot/sys/kern/kern_racct.c
  soc2012/rudot/sys/kern/sched_4bsd.c

Modified: soc2012/rudot/sys/kern/kern_racct.c
==============================================================================
--- soc2012/rudot/sys/kern/kern_racct.c	Fri Jun  8 14:43:46 2012	(r237310)
+++ soc2012/rudot/sys/kern/kern_racct.c	Fri Jun  8 15:09:30 2012	(r237311)
@@ -55,6 +55,7 @@
 #include <sys/sysproto.h>
 #include <sys/umtx.h>
 #include <machine/smp.h>
+#include <sys/sched.h>
 
 #ifdef RCTL
 #include <sys/rctl.h>
@@ -272,25 +273,29 @@
 racct_getpcpu(struct proc *p)
 {
 	u_int swtime;
-	fixpt_t pctcpu;
+	fixpt_t p_pctcpu, pctcpu, pctcpu_next;
 	struct thread *td;
 
 	swtime = (ticks - p->p_swtick) / hz;
 	if ((swtime == 0) || ((p->p_flag & P_INMEM) == 0))
 		return (0);
 
-	pctcpu = 0;
+	p_pctcpu = 0;
 	FOREACH_THREAD_IN_PROC(p, td) {
 		thread_lock(td);
-		pctcpu += sched_pctcpu(td);
+		pctcpu = sched_pctcpu(td);
+		/* Count also the yet unfinished second. */
+		pctcpu_next = (pctcpu * ccpu_exp[1]) >> FSHIFT;
+		pctcpu_next += sched_pctcpu_delta(td);
+		p_pctcpu += max(pctcpu, pctcpu_next);
 		thread_unlock(td);
 	}
 
 	if (swtime <= CCPU_EXP_MAX) {
-		return ((100 * pctcpu) / (FSCALE - ccpu_exp[swtime]));
+		return ((100 * p_pctcpu) / (FSCALE - ccpu_exp[swtime]));
 	}
 
-	return ((100 * pctcpu) / FSCALE);
+	return ((100 * p_pctcpu) / FSCALE);
 }
 
 static void

Modified: soc2012/rudot/sys/kern/sched_4bsd.c
==============================================================================
--- soc2012/rudot/sys/kern/sched_4bsd.c	Fri Jun  8 14:43:46 2012	(r237310)
+++ soc2012/rudot/sys/kern/sched_4bsd.c	Fri Jun  8 15:09:30 2012	(r237311)
@@ -1545,6 +1545,34 @@
 	return (ts->ts_pctcpu);
 }
 
+fixpt_t
+sched_pctcpu_delta(struct thread *td)
+{
+	struct td_sched *ts;
+	fixpt_t delta;
+	int realstathz;
+
+	THREAD_LOCK_ASSERT(td, MA_OWNED);
+	ts = td->td_sched;
+	delta = 0;
+	realstathz = stathz ? stathz : hz;
+	if (ts->ts_cpticks != 0) {
+#if	(FSHIFT >= CCPU_SHIFT)
+		delta = (realstathz == 100)
+		    ? ((fixpt_t) ts->ts_cpticks) <<
+		    (FSHIFT - CCPU_SHIFT) :
+		    100 * (((fixpt_t) ts->ts_cpticks)
+		    << (FSHIFT - CCPU_SHIFT)) / realstathz;
+#else
+		delta = ((FSCALE - ccpu) *
+		    (ts->ts_cpticks *
+		    FSCALE / realstathz)) >> FSHIFT;
+#endif
+	}
+
+	return (delta);
+}
+
 void
 sched_tick(int cnt)
 {


More information about the svn-soc-all mailing list