PERFORCE change 188137 for review

Edward Tomasz Napierala trasz at FreeBSD.org
Mon Jan 24 21:34:31 UTC 2011


http://p4web.freebsd.org/@@188137?ac=10

Change 188137 by trasz at trasz_victim on 2011/01/24 21:33:25

	Make %CPU slightly more usable by adding accounting for exiting
	processes and removing the part of dampening that was breaking
	stuff.

Affected files ...

.. //depot/projects/soc2009/trasz_limits/sys/kern/kern_container.c#63 edit
.. //depot/projects/soc2009/trasz_limits/sys/kern/kern_exit.c#31 edit

Differences ...

==== //depot/projects/soc2009/trasz_limits/sys/kern/kern_container.c#63 (text+ko) ====

@@ -645,10 +645,26 @@
 void
 container_proc_exit(struct proc *p)
 {
+	uint64_t runtime, pctcpu;
+
+	PROC_LOCK(p);
+	/*
+	 * We don't need to calculate rux, proc_reap() has already done this.
+	 */
+	runtime = cputick2usec(p->p_rux.rux_runtime);
+#ifdef notyet
+	KASSERT(runtime >= p->p_prev_runtime, ("runtime < p_prev_runtime"));
+#else
+	if (runtime < p->p_prev_runtime)
+		runtime = p->p_prev_runtime;
+#endif
+	pctcpu = (runtime - p->p_prev_runtime) / 10000;
+	rusage_set(p, RUSAGE_CPU, runtime);
+	rusage_add(p, RUSAGE_PCTCPU, pctcpu);
+
 	/*
 	 * XXX: Free this some other way.
 	 */
-	PROC_LOCK(p);
 	rusage_set(p, RUSAGE_FSIZE, 0);
 	rusage_set(p, RUSAGE_NPTS, 0);
 	rusage_set(p, RUSAGE_NTHR, 0);
@@ -739,26 +755,19 @@
 	}
 }
 
+/*
+ * %CPU is special.  Each second we zero out RUSAGE_PCTCPU for all
+ * the processes and other containers before calculating %CPU.  Reason
+ * for this is that we also to update %CPU when process exits,
+ * and that would cause the %CPU for per-user or per-jail containers
+ * to grow indefinitely.
+ */
 static void
 container_dampen_callback(struct container *container, void *arg2, void *arg3)
 {
-	int orig, diff, hz;
-
-	hz = *(int *)arg2;
 
 	mtx_lock(&container_lock);
-	orig = container->c_resources[RUSAGE_PCTCPU];
-	KASSERT(orig >= 0, ("container_dampen_callback: orig < 0"));
-	if (orig == 0) {
-		mtx_unlock(&container_lock);
-		return;
-	}
-	diff = orig / 10;
-	if (diff == 0)
-		diff = 1;
-	container->c_resources[RUSAGE_PCTCPU] -= diff;
-	KASSERT(container->c_resources[RUSAGE_PCTCPU] >= 0,
-	    ("container_dampen_callback: result < 0"));
+	container->c_resources[RUSAGE_PCTCPU] = 0;
 	mtx_unlock(&container_lock);
 }
 
@@ -771,12 +780,17 @@
 	uint64_t pctcpu, pctcpu_limit, runtime;
 
 	for (;;) {
-		loginclass_container_foreach(container_dampen_callback, &hz,
+		sx_slock(&allproc_lock);
+		/*
+		 * XXX: There is a window between zeroing the stats and setting
+		 *      them to a proper value.
+		 */
+		loginclass_container_foreach(container_dampen_callback, NULL,
+		    NULL);
+		ui_container_foreach(container_dampen_callback, NULL, NULL);
+		prison_container_foreach(container_dampen_callback, NULL,
 		    NULL);
-		ui_container_foreach(container_dampen_callback, &hz, NULL);
-		prison_container_foreach(container_dampen_callback, &hz, NULL);
 
-		sx_slock(&allproc_lock);
 		FOREACH_PROC_IN_SYSTEM(p) {
 			microuptime(&wallclock);
 			timevalsub(&wallclock, &p->p_stats->p_start);
@@ -803,10 +817,13 @@
 				rusage_throttle(p, 1);
 			else
 				rusage_throttle(p, 0);
-			rusage_set(p, RUSAGE_CPU, runtime);
-			rusage_set(p, RUSAGE_PCTCPU, pctcpu);
-			rusage_set(p, RUSAGE_WALLCLOCK,
+			mtx_lock(&container_lock);
+			rusage_set_locked(p, RUSAGE_CPU, runtime);
+			p->p_container.c_resources[RUSAGE_PCTCPU] = 0;
+			rusage_set_locked(p, RUSAGE_PCTCPU, pctcpu);
+			rusage_set_locked(p, RUSAGE_WALLCLOCK,
 			    wallclock.tv_sec * 1000000 + wallclock.tv_usec);
+			mtx_unlock(&container_lock);
 			PROC_UNLOCK(p);
 		}
 		sx_sunlock(&allproc_lock);

==== //depot/projects/soc2009/trasz_limits/sys/kern/kern_exit.c#31 (text+ko) ====

@@ -745,9 +745,6 @@
 	/*
 	 * Destroy resource container associated with the process.
 	 */
-	PROC_LOCK(p);
-	rusage_set(p, RUSAGE_CPU, cputick2usec(p->p_rux.rux_runtime));
-	PROC_UNLOCK(p);
 	container_proc_exit(p);
 	PROC_LOCK(p->p_pptr);
 	rusage_sub(p->p_pptr, RUSAGE_NPROC, 1);


More information about the p4-projects mailing list