Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 31 Mar 2011 19:22:11 +0000 (UTC)
From:      Edward Tomasz Napierala <trasz@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r220222 - head/sys/kern
Message-ID:  <201103311922.p2VJMBVM016495@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: trasz
Date: Thu Mar 31 19:22:11 2011
New Revision: 220222
URL: http://svn.freebsd.org/changeset/base/220222

Log:
  Enable accounting for RACCT_NPROC and RACCT_NTHR.
  
  Sponsored by:	The FreeBSD Foundation
  Reviewed by:	kib (earlier version)

Modified:
  head/sys/kern/init_main.c
  head/sys/kern/kern_exit.c
  head/sys/kern/kern_fork.c
  head/sys/kern/kern_thr.c

Modified: head/sys/kern/init_main.c
==============================================================================
--- head/sys/kern/init_main.c	Thu Mar 31 18:35:44 2011	(r220221)
+++ head/sys/kern/init_main.c	Thu Mar 31 19:22:11 2011	(r220222)
@@ -557,6 +557,9 @@ proc0_init(void *dummy __unused)
 	 * Charge root for one process.
 	 */
 	(void)chgproccnt(p->p_ucred->cr_ruidinfo, 1, 0);
+	PROC_LOCK(p);
+	racct_add_force(p, RACCT_NPROC, 1);
+	PROC_UNLOCK(p);
 }
 SYSINIT(p0init, SI_SUB_INTRINSIC, SI_ORDER_FIRST, proc0_init, NULL);
 

Modified: head/sys/kern/kern_exit.c
==============================================================================
--- head/sys/kern/kern_exit.c	Thu Mar 31 18:35:44 2011	(r220221)
+++ head/sys/kern/kern_exit.c	Thu Mar 31 19:22:11 2011	(r220222)
@@ -177,6 +177,7 @@ exit1(struct thread *td, int rv)
 	}
 	KASSERT(p->p_numthreads == 1,
 	    ("exit1: proc %p exiting with %d threads", p, p->p_numthreads));
+	racct_sub(p, RACCT_NTHR, 1);
 	/*
 	 * Wakeup anyone in procfs' PIOCWAIT.  They should have a hold
 	 * on our vmspace, so we should block below until they have
@@ -745,6 +746,9 @@ proc_reap(struct thread *td, struct proc
 	 * Destroy resource accounting information associated with the process.
 	 */
 	racct_proc_exit(p);
+	PROC_LOCK(p->p_pptr);
+	racct_sub(p->p_pptr, RACCT_NPROC, 1);
+	PROC_UNLOCK(p->p_pptr);
 
 	/*
 	 * Free credentials, arguments, and sigacts.
@@ -905,7 +909,11 @@ proc_reparent(struct proc *child, struct
 	if (child->p_pptr == parent)
 		return;
 
+	PROC_LOCK(parent);
+	racct_add_force(parent, RACCT_NPROC, 1);
+	PROC_UNLOCK(parent);
 	PROC_LOCK(child->p_pptr);
+	racct_sub(child->p_pptr, RACCT_NPROC, 1);
 	sigqueue_take(child->p_ksi);
 	PROC_UNLOCK(child->p_pptr);
 	LIST_REMOVE(child, p_sibling);

Modified: head/sys/kern/kern_fork.c
==============================================================================
--- head/sys/kern/kern_fork.c	Thu Mar 31 18:35:44 2011	(r220221)
+++ head/sys/kern/kern_fork.c	Thu Mar 31 19:22:11 2011	(r220222)
@@ -734,6 +734,12 @@ fork1(struct thread *td, int flags, int 
 		return (fork_norfproc(td, flags));
 	}
 
+	PROC_LOCK(p1);
+	error = racct_add(p1, RACCT_NPROC, 1);
+	PROC_UNLOCK(p1);
+	if (error != 0)
+		return (EAGAIN);
+
 	mem_charged = 0;
 	vm2 = NULL;
 	if (pages == 0)
@@ -817,6 +823,17 @@ fork1(struct thread *td, int flags, int 
 	}
 
 	/*
+	 * After fork, there is exactly one thread running.
+	 */
+	PROC_LOCK(newproc);
+	error = racct_set(newproc, RACCT_NTHR, 1);
+	PROC_UNLOCK(newproc);
+	if (error != 0) {
+		error = EAGAIN;
+		goto fail;
+	}
+
+	/*
 	 * Increment the count of procs running with this uid. Don't allow
 	 * a nonprivileged user to exceed their current limit.
 	 *
@@ -857,6 +874,9 @@ fail1:
 		vmspace_free(vm2);
 	uma_zfree(proc_zone, newproc);
 	pause("fork", hz / 2);
+	PROC_LOCK(p1);
+	racct_sub(p1, RACCT_NPROC, 1);
+	PROC_UNLOCK(p1);
 	return (error);
 }
 

Modified: head/sys/kern/kern_thr.c
==============================================================================
--- head/sys/kern/kern_thr.c	Thu Mar 31 18:35:44 2011	(r220221)
+++ head/sys/kern/kern_thr.c	Thu Mar 31 19:22:11 2011	(r220222)
@@ -36,6 +36,7 @@ __FBSDID("$FreeBSD$");
 #include <sys/priv.h>
 #include <sys/proc.h>
 #include <sys/posix4.h>
+#include <sys/racct.h>
 #include <sys/resourcevar.h>
 #include <sys/rwlock.h>
 #include <sys/sched.h>
@@ -184,10 +185,18 @@ create_thread(struct thread *td, mcontex
 		}
 	}
 
+	PROC_LOCK(td->td_proc);
+	error = racct_add(p, RACCT_NTHR, 1);
+	PROC_UNLOCK(td->td_proc);
+	if (error != 0)
+		return (EPROCLIM);
+
 	/* Initialize our td */
 	newtd = thread_alloc(0);
-	if (newtd == NULL)
-		return (ENOMEM);
+	if (newtd == NULL) {
+		error = ENOMEM;
+		goto fail;
+	}
 
 	/*
 	 * Try the copyout as soon as we allocate the td so we don't
@@ -203,7 +212,8 @@ create_thread(struct thread *td, mcontex
 	    (parent_tid != NULL &&
 	    suword_lwpid(parent_tid, newtd->td_tid))) {
 		thread_free(newtd);
-		return (EFAULT);
+		error = EFAULT;
+		goto fail;
 	}
 
 	bzero(&newtd->td_startzero,
@@ -220,7 +230,7 @@ create_thread(struct thread *td, mcontex
 		if (error != 0) {
 			thread_free(newtd);
 			crfree(td->td_ucred);
-			return (error);
+			goto fail;
 		}
 	} else {
 		/* Set up our machine context. */
@@ -233,7 +243,7 @@ create_thread(struct thread *td, mcontex
 		if (error != 0) {
 			thread_free(newtd);
 			crfree(td->td_ucred);
-			return (error);
+			goto fail;
 		}
 	}
 
@@ -265,6 +275,12 @@ create_thread(struct thread *td, mcontex
 	thread_unlock(newtd);
 
 	return (0);
+
+fail:
+	PROC_LOCK(p);
+	racct_sub(p, RACCT_NTHR, 1);
+	PROC_UNLOCK(p);
+	return (error);
 }
 
 int
@@ -294,7 +310,10 @@ thr_exit(struct thread *td, struct thr_e
 	}
 
 	rw_wlock(&tidhash_lock);
+
 	PROC_LOCK(p);
+	racct_sub(p, RACCT_NTHR, 1);
+
 	/*
 	 * Shutting down last thread in the proc.  This will actually
 	 * call exit() in the trampoline when it returns.



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