Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 31 May 2012 15:25:55 +0000
From:      rudot@FreeBSD.org
To:        svn-soc-all@FreeBSD.org
Subject:   socsvn commit: r236820 - in soc2012/rudot: aux sys/kern sys/sys
Message-ID:  <20120531152555.18481106566B@hub.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: rudot
Date: Thu May 31 15:25:53 2012
New Revision: 236820
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=236820

Log:
  updated %cpu calculations for short-lived processes. I had to work around using floating point in the kernel.

Added:
  soc2012/rudot/aux/check.sh   (contents, props changed)
Modified:
  soc2012/rudot/sys/kern/kern_racct.c
  soc2012/rudot/sys/kern/sched_4bsd.c
  soc2012/rudot/sys/sys/racct.h

Added: soc2012/rudot/aux/check.sh
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ soc2012/rudot/aux/check.sh	Thu May 31 15:25:53 2012	(r236820)
@@ -0,0 +1,3 @@
+./user_pcpu.sh
+
+ps ax -O %cpu

Modified: soc2012/rudot/sys/kern/kern_racct.c
==============================================================================
--- soc2012/rudot/sys/kern/kern_racct.c	Thu May 31 14:47:02 2012	(r236819)
+++ soc2012/rudot/sys/kern/kern_racct.c	Thu May 31 15:25:53 2012	(r236820)
@@ -143,6 +143,152 @@
 		RACCT_IN_MILLIONS,
 	[RACCT_PCTCPU] = RACCT_RECLAIMABLE | RACCT_DENIABLE };
 
+/*
+ * Contains intermediate values for %cpu calculations to avoid using floating
+ * point in the kernel.
+ * ccpu_exp[k] = FSCALE * exp(-k/20)
+ */
+fixpt_t ccpu_exp[] = {
+	[0] = FSCALE * 1,
+	[1] = FSCALE * 0.95122942450071400909,
+	[2] = FSCALE * 0.90483741803595957316,
+	[3] = FSCALE * 0.86070797642505780722,
+	[4] = FSCALE * 0.81873075307798185866,
+	[5] = FSCALE * 0.77880078307140486824,
+	[6] = FSCALE * 0.74081822068171786606,
+	[7] = FSCALE * 0.70468808971871343435,
+	[8] = FSCALE * 0.67032004603563930074,
+	[9] = FSCALE * 0.63762815162177329314,
+	[10] = FSCALE * 0.60653065971263342360,
+	[11] = FSCALE * 0.57694981038048669531,
+	[12] = FSCALE * 0.54881163609402643262,
+	[13] = FSCALE * 0.52204577676101604789,
+	[14] = FSCALE * 0.49658530379140951470,
+	[15] = FSCALE * 0.47236655274101470713,
+	[16] = FSCALE * 0.44932896411722159143,
+	[17] = FSCALE * 0.42741493194872666992,
+	[18] = FSCALE * 0.40656965974059911188,
+	[19] = FSCALE * 0.38674102345450120691,
+	[20] = FSCALE * 0.36787944117144232159,
+	[21] = FSCALE * 0.34993774911115535467,
+	[22] = FSCALE * 0.33287108369807955328,
+	[23] = FSCALE * 0.31663676937905321821,
+	[24] = FSCALE * 0.30119421191220209664,
+	[25] = FSCALE * 0.28650479686019010032,
+	[26] = FSCALE * 0.27253179303401260312,
+	[27] = FSCALE * 0.25924026064589150757,
+	[28] = FSCALE * 0.24659696394160647693,
+	[29] = FSCALE * 0.23457028809379765313,
+	[30] = FSCALE * 0.22313016014842982893,
+	[31] = FSCALE * 0.21224797382674305771,
+	[32] = FSCALE * 0.20189651799465540848,
+	[33] = FSCALE * 0.19204990862075411423,
+	[34] = FSCALE * 0.18268352405273465022,
+	[35] = FSCALE * 0.17377394345044512668,
+	[36] = FSCALE * 0.16529888822158653829,
+	[37] = FSCALE * 0.15723716631362761621,
+	[38] = FSCALE * 0.14956861922263505264,
+	[39] = FSCALE * 0.14227407158651357185,
+	[40] = FSCALE * 0.13533528323661269189,
+	[41] = FSCALE * 0.12873490358780421886,
+	[42] = FSCALE * 0.12245642825298191021,
+	[43] = FSCALE * 0.11648415777349695786,
+	[44] = FSCALE * 0.11080315836233388333,
+	[45] = FSCALE * 0.10539922456186433678,
+	[46] = FSCALE * 0.10025884372280373372,
+	[47] = FSCALE * 0.09536916221554961888,
+	[48] = FSCALE * 0.09071795328941250337,
+	[49] = FSCALE * 0.08629358649937051097,
+	[50] = FSCALE * 0.08208499862389879516,
+	[51] = FSCALE * 0.07808166600115315231,
+	[52] = FSCALE * 0.07427357821433388042,
+	[53] = FSCALE * 0.07065121306042958674,
+	[54] = FSCALE * 0.06720551273974976512,
+	[55] = FSCALE * 0.06392786120670757270,
+	[56] = FSCALE * 0.06081006262521796499,
+	[57] = FSCALE * 0.05784432087483846296,
+	[58] = FSCALE * 0.05502322005640722902,
+	[59] = FSCALE * 0.05233970594843239308,
+	[60] = FSCALE * 0.04978706836786394297,
+	[61] = FSCALE * 0.04735892439114092119,
+	[62] = FSCALE * 0.04504920239355780606,
+	[63] = FSCALE * 0.04285212686704017991,
+	[64] = FSCALE * 0.04076220397836621516,
+	[65] = FSCALE * 0.03877420783172200988,
+	[66] = FSCALE * 0.03688316740124000544,
+	[67] = FSCALE * 0.03508435410084502588,
+	[68] = FSCALE * 0.03337326996032607948,
+	[69] = FSCALE * 0.03174563637806794323,
+	[70] = FSCALE * 0.03019738342231850073,
+	[71] = FSCALE * 0.02872463965423942912,
+	[72] = FSCALE * 0.02732372244729256080,
+	[73] = FSCALE * 0.02599112877875534358,
+	[74] = FSCALE * 0.02472352647033939120,
+	[75] = FSCALE * 0.02351774585600910823,
+	[76] = FSCALE * 0.02237077185616559577,
+	[77] = FSCALE * 0.02127973643837716938,
+	[78] = FSCALE * 0.02024191144580438847,
+	[79] = FSCALE * 0.01925470177538692429,
+	[80] = FSCALE * 0.01831563888873418029,
+	[81] = FSCALE * 0.01742237463949351138,
+	[82] = FSCALE * 0.01657267540176124754,
+	[83] = FSCALE * 0.01576441648485449082,
+	[84] = FSCALE * 0.01499557682047770621,
+	[85] = FSCALE * 0.01426423390899925527,
+	[86] = FSCALE * 0.01356855901220093175,
+	[87] = FSCALE * 0.01290681258047986886,
+	[88] = FSCALE * 0.01227733990306844117,
+	[89] = FSCALE * 0.01167856697039544521,
+	[90] = FSCALE * 0.01110899653824230649,
+	[91] = FSCALE * 0.01056720438385265337,
+	[92] = FSCALE * 0.01005183574463358164,
+	[93] = FSCALE * 0.00956160193054350793,
+	[94] = FSCALE * 0.00909527710169581709,
+	[95] = FSCALE * 0.00865169520312063417,
+	[96] = FSCALE * 0.00822974704902002884,
+	[97] = FSCALE * 0.00782837754922577143,
+	[98] = FSCALE * 0.00744658307092434051,
+	[99] = FSCALE * 0.00708340892905212004,
+	[100] = FSCALE * 0.00673794699908546709,
+	[101] = FSCALE * 0.00640933344625638184,
+	[102] = FSCALE * 0.00609674656551563610,
+	[103] = FSCALE * 0.00579940472684214321,
+	[104] = FSCALE * 0.00551656442076077241,
+	[105] = FSCALE * 0.00524751839918138427,
+	[106] = FSCALE * 0.00499159390691021621,
+	[107] = FSCALE * 0.00474815099941147558,
+	[108] = FSCALE * 0.00451658094261266798,
+	[109] = FSCALE * 0.00429630469075234057,
+	[110] = FSCALE * 0.00408677143846406699,
+};
+
+#define	CCPU_EXP_MAX	110
+
+u_int
+racct_getpcpu(struct proc *p)
+{
+	u_int swtime;
+	fixpt_t pctcpu;
+	struct thread *td;
+
+	swtime = (ticks - p->p_swtick) / hz;
+	if ((swtime == 0) || ((p->p_flag & P_INMEM) == 0))
+		return (0);
+
+	pctcpu = 0;
+	FOREACH_THREAD_IN_PROC(p, td) {
+		thread_lock(td);
+		pctcpu += sched_pctcpu(td);
+		thread_unlock(td);
+	}
+
+	if (swtime <= CCPU_EXP_MAX) {
+		return ((100 * pctcpu) / (FSCALE - ccpu_exp[swtime]));
+	}
+
+	return ((100 * pctcpu) / FSCALE);
+}
+
 static void
 racct_add_racct(struct racct *dest, const struct racct *src)
 {
@@ -702,8 +848,7 @@
 	struct proc *p;
 	struct timeval wallclock;
 	uint64_t runtime;
-	fixpt_t pctcpu;
-	int pct_human;
+	u_int pct;
 
 	for (;;) {
 		sx_slock(&allproc_lock);
@@ -714,15 +859,10 @@
 
 			microuptime(&wallclock);
 			timevalsub(&wallclock, &p->p_stats->p_start);
-			pctcpu = 0;
 			PROC_LOCK(p);
 			PROC_SLOCK(p);
-			FOREACH_THREAD_IN_PROC(p, td) {
+			FOREACH_THREAD_IN_PROC(p, td)
 				ruxagg(p, td);
-				thread_lock(td);
-				pctcpu += sched_pctcpu(td);
-				thread_unlock(td);
-			}
 			runtime = cputick2usec(p->p_rux.rux_runtime);
 			PROC_SUNLOCK(p);
 #ifdef notyet
@@ -733,14 +873,14 @@
 				runtime = p->p_prev_runtime;
 #endif
 			p->p_prev_runtime = runtime;
-			pct_human = (100 * pctcpu) / FSCALE;
 			mtx_lock(&racct_lock);
+			pct = racct_getpcpu(p);
 			/*
 			 * I use _force_ here because we always want to have
 			 * the real value in the RACCT_PCTCPU resource
 			 * regardless of the limits set.
 			 */
-			racct_set_force_locked(p, RACCT_PCTCPU, pct_human);
+			racct_set_force_locked(p, RACCT_PCTCPU, pct);
 			racct_set_locked(p, RACCT_CPU, runtime);
 			racct_set_locked(p, RACCT_WALLCLOCK,
 			    (uint64_t)wallclock.tv_sec * 1000000 +

Modified: soc2012/rudot/sys/kern/sched_4bsd.c
==============================================================================
--- soc2012/rudot/sys/kern/sched_4bsd.c	Thu May 31 14:47:02 2012	(r236819)
+++ soc2012/rudot/sys/kern/sched_4bsd.c	Thu May 31 15:25:53 2012	(r236820)
@@ -694,9 +694,7 @@
 	struct pcpuidlestat *stat;
 	struct td_sched *ts;
 #ifdef RACCT
-	int pct_human;
-	fixpt_t pctcpu;
-	struct thread *tdp;
+	u_int pct;
 #endif
 
 	THREAD_LOCK_ASSERT(td, MA_OWNED);
@@ -722,17 +720,9 @@
 	stat->idlecalls = 0;
 
 #ifdef RACCT
-	pctcpu = sched_pctcpu(td);
-	FOREACH_THREAD_IN_PROC(td->td_proc, tdp) {
-		if (td == tdp)
-		    continue;
-		thread_lock(tdp);
-		pctcpu += sched_pctcpu(tdp);
-		thread_unlock(tdp);
-	}
-	pct_human = (100 * pctcpu) / FSCALE;
-	racct_set_force(td->td_proc, RACCT_PCTCPU, pct_human);
-	if (pct_human >= racct_get_limit(td->td_proc, RACCT_PCTCPU)) {
+	pct = racct_getpcpu(td->td_proc);
+	racct_set_force(td->td_proc, RACCT_PCTCPU, pct);
+	if (pct >= racct_get_limit(td->td_proc, RACCT_PCTCPU)) {
 		pause("racct", hz);
 	}
 #endif

Modified: soc2012/rudot/sys/sys/racct.h
==============================================================================
--- soc2012/rudot/sys/sys/racct.h	Thu May 31 14:47:02 2012	(r236819)
+++ soc2012/rudot/sys/sys/racct.h	Thu May 31 15:25:53 2012	(r236820)
@@ -144,5 +144,6 @@
 void	racct_proc_ucred_changed(struct proc *p, struct ucred *oldcred,
 	    struct ucred *newcred);
 void	racct_move(struct racct *dest, struct racct *src);
+u_int	racct_getpcpu(struct proc *p);
 
 #endif /* !_RACCT_H_ */



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20120531152555.18481106566B>