Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 09 Aug 2012 09:51:01 +0000
From:      rudot@FreeBSD.org
To:        svn-soc-all@FreeBSD.org
Subject:   socsvn commit: r240207 - in soc2012/rudot/sys: kern sys
Message-ID:  <20120809095101.D5FC6106564A@hub.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: rudot
Date: Thu Aug  9 09:51:00 2012
New Revision: 240207
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=240207

Log:
  decay %cpu in racct containers - work in progress

Modified:
  soc2012/rudot/sys/kern/kern_racct.c
  soc2012/rudot/sys/sys/racct.h

Modified: soc2012/rudot/sys/kern/kern_racct.c
==============================================================================
--- soc2012/rudot/sys/kern/kern_racct.c	Wed Aug  8 21:05:07 2012	(r240206)
+++ soc2012/rudot/sys/kern/kern_racct.c	Thu Aug  9 09:51:00 2012	(r240207)
@@ -157,7 +157,7 @@
 		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
 	[RACCT_WALLCLOCK] =
 		RACCT_IN_MILLIONS,
-	[RACCT_PCTCPU] = RACCT_DENIABLE };
+	[RACCT_PCTCPU] = RACCT_DECAYING | RACCT_DENIABLE };
 
 static const fixpt_t RACCT_DECAY_FACTOR = 0.9 * FSCALE;
 
@@ -303,7 +303,7 @@
 #endif
 #ifdef SMP
 	struct pcpu *pc;
-	u_int cpuid;	
+	int found;
 #endif
 	fixpt_t p_pctcpu;
 	struct thread *td;
@@ -325,12 +325,15 @@
 		if (td == PCPU_GET(idlethread))
 			continue;
 #ifdef SMP
-		cpuid = td->td_oncpu;
-		if (cpuid != NOCPU) {
-			pc = pcpu_find(cpuid);
-			if (td == pc->pc_idlethread)
-				continue;
+		found = 0;
+		STAILQ_FOREACH(pc, &cpuhead, pc_allcpu) {
+			if (td == pc->pc_idlethread) {
+				found = 1;
+				break;
+			}
 		}
+		if (found)
+			continue;
 #endif
 		thread_lock(td);
 #ifdef SCHED_4BSD
@@ -398,7 +401,7 @@
 			KASSERT(src->r_resources[i] <= dest->r_resources[i],
 			    ("racct propagation meltdown: src > dest"));
 		}
-		if (RACCT_IS_RECLAIMABLE(i)) {
+		if (RACCT_CAN_DROP(i)) {
 			dest->r_resources[i] -= src->r_resources[i];
 			if (dest->r_resources[i] < 0) {
 				KASSERT(RACCT_IS_SLOPPY(i),
@@ -583,7 +586,7 @@
 {
 	int64_t available;
 	int64_t old_amount, decayed_amount;
-	int64_t diff, decayed_diff;
+	int64_t diff_proc, diff_cred;
 
 	SDT_PROBE(racct, kernel, rusage, set, p, resource, amount, 0, 0);
 
@@ -592,30 +595,36 @@
 	 */
 	PROC_LOCK_ASSERT(p, MA_OWNED);
 
+	old_amount = p->p_racct->r_resources[resource];
 	/*
-	 * may be negative
+	 * the diffs may be negative
 	 */
-	old_amount = p->p_racct->r_resources[resource];
-	diff = amount - old_amount;
-	decayed_amount = old_amount * RACCT_DECAY_FACTOR / FSCALE;
-	decayed_diff = amount - decayed_amount;
+	diff_proc = amount - old_amount;
+	if (RACCT_IS_DECAYING(resource)) {
+		decayed_amount = old_amount * RACCT_DECAY_FACTOR / FSCALE;
+		diff_cred = amount - decayed_amount;
+	} else {
+		diff_cred = diff_proc;
+	}
 	available = INT64_MAX;
+
 #ifdef RCTL
 	available = rctl_pcpu_available(p);
 #endif
-	racct_alloc_resource(p->p_racct, resource, diff);
-	if (decayed_diff > 0)
-		racct_add_cred_locked(p->p_ucred, resource, decayed_diff);
-	else if (decayed_diff < 0)
-		racct_sub_cred_locked(p->p_ucred, resource, -decayed_diff);
+	racct_alloc_resource(p->p_racct, resource, diff_proc);
+	if (diff_cred > 0)
+		racct_add_cred_locked(p->p_ucred, resource, diff_cred);
+	else if (diff_cred < 0)
+		racct_sub_cred_locked(p->p_ucred, resource, -diff_cred);
 
-	return (available <= diff);
+	return (available <= diff_proc);
 }
 
 static int
 racct_set_locked(struct proc *p, int resource, uint64_t amount)
 {
-	int64_t diff;
+	int64_t old_amount, decayed_amount;
+	int64_t diff_proc, diff_cred;
 #ifdef RCTL
 	int error;
 #endif
@@ -627,15 +636,25 @@
 	 */
 	PROC_LOCK_ASSERT(p, MA_OWNED);
 
-	diff = amount - p->p_racct->r_resources[resource];
+	old_amount = p->p_racct->r_resources[resource];
+	/*
+	 * the diffs may be negative
+	 */
+	diff_proc = amount - old_amount;
+	if (RACCT_IS_DECAYING(resource)) {
+		decayed_amount = old_amount * RACCT_DECAY_FACTOR / FSCALE;
+		diff_cred = amount - decayed_amount;
+	} else {
+		diff_cred = diff_proc;
+	}
 #ifdef notyet
-	KASSERT(diff >= 0 || RACCT_IS_RECLAIMABLE(resource),
-	    ("racct_set: usage of non-reclaimable resource %d dropping",
+	KASSERT(diff_proc >= 0 || RACCT_CAN_DROP(resource),
+	    ("racct_set: usage of non-droppable resource %d dropping",
 	     resource));
 #endif
 #ifdef RCTL
-	if (diff > 0) {
-		error = rctl_enforce(p, resource, diff);
+	if (diff_proc > 0) {
+		error = rctl_enforce(p, resource, diff_proc);
 		if (error && RACCT_IS_DENIABLE(resource)) {
 			SDT_PROBE(racct, kernel, rusage, set_failure, p,
 			    resource, amount, 0, 0);
@@ -643,11 +662,11 @@
 		}
 	}
 #endif
-	racct_alloc_resource(p->p_racct, resource, diff);
-	if (diff > 0)
-		racct_add_cred_locked(p->p_ucred, resource, diff);
-	else if (diff < 0)
-		racct_sub_cred_locked(p->p_ucred, resource, -diff);
+	racct_alloc_resource(p->p_racct, resource, diff_proc);
+	if (diff_cred > 0)
+		racct_add_cred_locked(p->p_ucred, resource, diff_cred);
+	else if (diff_cred < 0)
+		racct_sub_cred_locked(p->p_ucred, resource, -diff_cred);
 
 	return (0);
 }
@@ -745,8 +764,8 @@
 	 * We need proc lock to dereference p->p_ucred.
 	 */
 	PROC_LOCK_ASSERT(p, MA_OWNED);
-	KASSERT(RACCT_IS_RECLAIMABLE(resource),
-	    ("racct_sub: called for non-reclaimable resource %d", resource));
+	KASSERT(RACCT_CAN_DROP(resource),
+	    ("racct_sub: called for non-droppable resource %d", resource));
 
 	mtx_lock(&racct_lock);
 	KASSERT(amount <= p->p_racct->r_resources[resource],
@@ -768,8 +787,8 @@
 	    0, 0);
 
 #ifdef notyet
-	KASSERT(RACCT_IS_RECLAIMABLE(resource),
-	    ("racct_sub_cred: called for non-reclaimable resource %d",
+	KASSERT(RACCT_CAN_DROP(resource),
+	    ("racct_sub_cred: called for resource %d which can not drop",
 	     resource));
 #endif
 
@@ -1024,8 +1043,6 @@
 	int resource;
 	int64_t r_old, r_new;
 
-	mtx_assert(&racct_lock, MA_OWNED);
-
 	resource = *(int *) res;
 	r_old = racct->r_resources[resource];
 
@@ -1033,18 +1050,15 @@
 	if (r_old <= 0)
 		return;
 
+	mtx_lock(&racct_lock);
 	r_new = r_old * RACCT_DECAY_FACTOR / FSCALE;
 	racct->r_resources[resource] = r_new;
+	mtx_unlock(&racct_lock);
 }
 
 static void
-racct_decay_pcpu(void)
+racct_decay(int resource)
 {
-	int resource;
-
-	mtx_assert(&racct_lock, MA_OWNED);
-
-	resource = RACCT_PCTCPU;
 	ui_racct_foreach(racct_decay_resource, &resource, NULL);
 	loginclass_racct_foreach(racct_decay_resource, &resource, NULL);
 	prison_racct_foreach(racct_decay_resource, &resource, NULL);
@@ -1061,9 +1075,7 @@
 	int over_limits;
 
 	for (;;) {
-		mtx_lock(&racct_lock);
-		racct_decay_pcpu();
-		mtx_unlock(&racct_lock);
+		racct_decay(RACCT_PCTCPU);
 
 		sx_slock(&allproc_lock);
 

Modified: soc2012/rudot/sys/sys/racct.h
==============================================================================
--- soc2012/rudot/sys/sys/racct.h	Wed Aug  8 21:05:07 2012	(r240206)
+++ soc2012/rudot/sys/sys/racct.h	Thu Aug  9 09:51:00 2012	(r240207)
@@ -79,6 +79,7 @@
 #define	RACCT_INHERITABLE	0x04
 #define	RACCT_DENIABLE		0x08
 #define	RACCT_SLOPPY		0x10
+#define	RACCT_DECAYING		0x20
 
 extern int racct_types[];
 
@@ -90,7 +91,9 @@
 #define	RACCT_IS_IN_MILLIONS(X)	(racct_types[X] & RACCT_IN_MILLIONS)
 
 /*
- * Resource usage can drop, as opposed to only grow.
+ * Resource usage can drop, as opposed to only grow. When the process
+ * terminates, its resource usage is freed from the respective
+ * per-credential racct containers.
  */
 #define	RACCT_IS_RECLAIMABLE(X)	(racct_types[X] & RACCT_RECLAIMABLE)
 
@@ -114,6 +117,20 @@
 #define	RACCT_IS_SLOPPY(X)		(racct_types[X] & RACCT_SLOPPY)
 
 /*
+ * When a process terminates, its resource usage is not automatically
+ * subtracted from per-credential racct containers. Instead, the resource
+ * usage of per-credential racct containers decays in time.
+ * Resource usage can olso drop for such resource.
+ * So far, the only such resource is RACCT_PCTCPU.
+ */
+#define RACCT_IS_DECAYING(X)		(racct_types[X] & RACCT_DECAYING)
+
+/*
+ * Resource usage can drop, as opposed to only grow.
+ */
+#define RACCT_CAN_DROP(X)		(RACCT_IS_RECLAIMABLE(X) | RACCT_IS_DECAYING(X))
+
+/*
  * The 'racct' structure defines resource consumption for a particular
  * subject, such as process or jail.
  *



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