xref: /freebsd/sys/kern/kern_racct.c (revision f4b37ed0f8b307b1f3f0f630ca725d68f1dff30d)
1 /*-
2  * Copyright (c) 2010 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Edward Tomasz Napierala under sponsorship
6  * from the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_sched.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/eventhandler.h>
40 #include <sys/jail.h>
41 #include <sys/kernel.h>
42 #include <sys/kthread.h>
43 #include <sys/lock.h>
44 #include <sys/loginclass.h>
45 #include <sys/malloc.h>
46 #include <sys/mutex.h>
47 #include <sys/proc.h>
48 #include <sys/racct.h>
49 #include <sys/resourcevar.h>
50 #include <sys/sbuf.h>
51 #include <sys/sched.h>
52 #include <sys/sdt.h>
53 #include <sys/smp.h>
54 #include <sys/sx.h>
55 #include <sys/sysctl.h>
56 #include <sys/sysent.h>
57 #include <sys/sysproto.h>
58 #include <sys/umtx.h>
59 #include <machine/smp.h>
60 
61 #ifdef RCTL
62 #include <sys/rctl.h>
63 #endif
64 
65 #ifdef RACCT
66 
67 FEATURE(racct, "Resource Accounting");
68 
69 /*
70  * Do not block processes that have their %cpu usage <= pcpu_threshold.
71  */
72 static int pcpu_threshold = 1;
73 #ifdef RACCT_DEFAULT_TO_DISABLED
74 int racct_enable = 0;
75 #else
76 int racct_enable = 1;
77 #endif
78 
79 SYSCTL_NODE(_kern, OID_AUTO, racct, CTLFLAG_RW, 0, "Resource Accounting");
80 SYSCTL_UINT(_kern_racct, OID_AUTO, enable, CTLFLAG_RDTUN, &racct_enable,
81     0, "Enable RACCT/RCTL");
82 SYSCTL_UINT(_kern_racct, OID_AUTO, pcpu_threshold, CTLFLAG_RW, &pcpu_threshold,
83     0, "Processes with higher %cpu usage than this value can be throttled.");
84 
85 /*
86  * How many seconds it takes to use the scheduler %cpu calculations.  When a
87  * process starts, we compute its %cpu usage by dividing its runtime by the
88  * process wall clock time.  After RACCT_PCPU_SECS pass, we use the value
89  * provided by the scheduler.
90  */
91 #define RACCT_PCPU_SECS		3
92 
93 static struct mtx racct_lock;
94 MTX_SYSINIT(racct_lock, &racct_lock, "racct lock", MTX_DEF);
95 
96 static uma_zone_t racct_zone;
97 
98 static void racct_sub_racct(struct racct *dest, const struct racct *src);
99 static void racct_sub_cred_locked(struct ucred *cred, int resource,
100 		uint64_t amount);
101 static void racct_add_cred_locked(struct ucred *cred, int resource,
102 		uint64_t amount);
103 
104 SDT_PROVIDER_DEFINE(racct);
105 SDT_PROBE_DEFINE3(racct, kernel, rusage, add, "struct proc *", "int",
106     "uint64_t");
107 SDT_PROBE_DEFINE3(racct, kernel, rusage, add__failure,
108     "struct proc *", "int", "uint64_t");
109 SDT_PROBE_DEFINE3(racct, kernel, rusage, add__cred, "struct ucred *",
110     "int", "uint64_t");
111 SDT_PROBE_DEFINE3(racct, kernel, rusage, add__force, "struct proc *",
112     "int", "uint64_t");
113 SDT_PROBE_DEFINE3(racct, kernel, rusage, set, "struct proc *", "int",
114     "uint64_t");
115 SDT_PROBE_DEFINE3(racct, kernel, rusage, set__failure,
116     "struct proc *", "int", "uint64_t");
117 SDT_PROBE_DEFINE3(racct, kernel, rusage, sub, "struct proc *", "int",
118     "uint64_t");
119 SDT_PROBE_DEFINE3(racct, kernel, rusage, sub__cred, "struct ucred *",
120     "int", "uint64_t");
121 SDT_PROBE_DEFINE1(racct, kernel, racct, create, "struct racct *");
122 SDT_PROBE_DEFINE1(racct, kernel, racct, destroy, "struct racct *");
123 SDT_PROBE_DEFINE2(racct, kernel, racct, join, "struct racct *",
124     "struct racct *");
125 SDT_PROBE_DEFINE2(racct, kernel, racct, join__failure,
126     "struct racct *", "struct racct *");
127 SDT_PROBE_DEFINE2(racct, kernel, racct, leave, "struct racct *",
128     "struct racct *");
129 
130 int racct_types[] = {
131 	[RACCT_CPU] =
132 		RACCT_IN_MILLIONS,
133 	[RACCT_DATA] =
134 		RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
135 	[RACCT_STACK] =
136 		RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
137 	[RACCT_CORE] =
138 		RACCT_DENIABLE,
139 	[RACCT_RSS] =
140 		RACCT_RECLAIMABLE,
141 	[RACCT_MEMLOCK] =
142 		RACCT_RECLAIMABLE | RACCT_DENIABLE,
143 	[RACCT_NPROC] =
144 		RACCT_RECLAIMABLE | RACCT_DENIABLE,
145 	[RACCT_NOFILE] =
146 		RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
147 	[RACCT_VMEM] =
148 		RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
149 	[RACCT_NPTS] =
150 		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
151 	[RACCT_SWAP] =
152 		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
153 	[RACCT_NTHR] =
154 		RACCT_RECLAIMABLE | RACCT_DENIABLE,
155 	[RACCT_MSGQQUEUED] =
156 		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
157 	[RACCT_MSGQSIZE] =
158 		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
159 	[RACCT_NMSGQ] =
160 		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
161 	[RACCT_NSEM] =
162 		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
163 	[RACCT_NSEMOP] =
164 		RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
165 	[RACCT_NSHM] =
166 		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
167 	[RACCT_SHMSIZE] =
168 		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
169 	[RACCT_WALLCLOCK] =
170 		RACCT_IN_MILLIONS,
171 	[RACCT_PCTCPU] =
172 		RACCT_DECAYING | RACCT_DENIABLE | RACCT_IN_MILLIONS };
173 
174 static const fixpt_t RACCT_DECAY_FACTOR = 0.3 * FSCALE;
175 
176 #ifdef SCHED_4BSD
177 /*
178  * Contains intermediate values for %cpu calculations to avoid using floating
179  * point in the kernel.
180  * ccpu_exp[k] = FSCALE * (ccpu/FSCALE)^k = FSCALE * exp(-k/20)
181  * It is needed only for the 4BSD scheduler, because in ULE, the ccpu equals to
182  * zero so the calculations are more straightforward.
183  */
184 fixpt_t ccpu_exp[] = {
185 	[0] = FSCALE * 1,
186 	[1] = FSCALE * 0.95122942450071400909,
187 	[2] = FSCALE * 0.90483741803595957316,
188 	[3] = FSCALE * 0.86070797642505780722,
189 	[4] = FSCALE * 0.81873075307798185866,
190 	[5] = FSCALE * 0.77880078307140486824,
191 	[6] = FSCALE * 0.74081822068171786606,
192 	[7] = FSCALE * 0.70468808971871343435,
193 	[8] = FSCALE * 0.67032004603563930074,
194 	[9] = FSCALE * 0.63762815162177329314,
195 	[10] = FSCALE * 0.60653065971263342360,
196 	[11] = FSCALE * 0.57694981038048669531,
197 	[12] = FSCALE * 0.54881163609402643262,
198 	[13] = FSCALE * 0.52204577676101604789,
199 	[14] = FSCALE * 0.49658530379140951470,
200 	[15] = FSCALE * 0.47236655274101470713,
201 	[16] = FSCALE * 0.44932896411722159143,
202 	[17] = FSCALE * 0.42741493194872666992,
203 	[18] = FSCALE * 0.40656965974059911188,
204 	[19] = FSCALE * 0.38674102345450120691,
205 	[20] = FSCALE * 0.36787944117144232159,
206 	[21] = FSCALE * 0.34993774911115535467,
207 	[22] = FSCALE * 0.33287108369807955328,
208 	[23] = FSCALE * 0.31663676937905321821,
209 	[24] = FSCALE * 0.30119421191220209664,
210 	[25] = FSCALE * 0.28650479686019010032,
211 	[26] = FSCALE * 0.27253179303401260312,
212 	[27] = FSCALE * 0.25924026064589150757,
213 	[28] = FSCALE * 0.24659696394160647693,
214 	[29] = FSCALE * 0.23457028809379765313,
215 	[30] = FSCALE * 0.22313016014842982893,
216 	[31] = FSCALE * 0.21224797382674305771,
217 	[32] = FSCALE * 0.20189651799465540848,
218 	[33] = FSCALE * 0.19204990862075411423,
219 	[34] = FSCALE * 0.18268352405273465022,
220 	[35] = FSCALE * 0.17377394345044512668,
221 	[36] = FSCALE * 0.16529888822158653829,
222 	[37] = FSCALE * 0.15723716631362761621,
223 	[38] = FSCALE * 0.14956861922263505264,
224 	[39] = FSCALE * 0.14227407158651357185,
225 	[40] = FSCALE * 0.13533528323661269189,
226 	[41] = FSCALE * 0.12873490358780421886,
227 	[42] = FSCALE * 0.12245642825298191021,
228 	[43] = FSCALE * 0.11648415777349695786,
229 	[44] = FSCALE * 0.11080315836233388333,
230 	[45] = FSCALE * 0.10539922456186433678,
231 	[46] = FSCALE * 0.10025884372280373372,
232 	[47] = FSCALE * 0.09536916221554961888,
233 	[48] = FSCALE * 0.09071795328941250337,
234 	[49] = FSCALE * 0.08629358649937051097,
235 	[50] = FSCALE * 0.08208499862389879516,
236 	[51] = FSCALE * 0.07808166600115315231,
237 	[52] = FSCALE * 0.07427357821433388042,
238 	[53] = FSCALE * 0.07065121306042958674,
239 	[54] = FSCALE * 0.06720551273974976512,
240 	[55] = FSCALE * 0.06392786120670757270,
241 	[56] = FSCALE * 0.06081006262521796499,
242 	[57] = FSCALE * 0.05784432087483846296,
243 	[58] = FSCALE * 0.05502322005640722902,
244 	[59] = FSCALE * 0.05233970594843239308,
245 	[60] = FSCALE * 0.04978706836786394297,
246 	[61] = FSCALE * 0.04735892439114092119,
247 	[62] = FSCALE * 0.04504920239355780606,
248 	[63] = FSCALE * 0.04285212686704017991,
249 	[64] = FSCALE * 0.04076220397836621516,
250 	[65] = FSCALE * 0.03877420783172200988,
251 	[66] = FSCALE * 0.03688316740124000544,
252 	[67] = FSCALE * 0.03508435410084502588,
253 	[68] = FSCALE * 0.03337326996032607948,
254 	[69] = FSCALE * 0.03174563637806794323,
255 	[70] = FSCALE * 0.03019738342231850073,
256 	[71] = FSCALE * 0.02872463965423942912,
257 	[72] = FSCALE * 0.02732372244729256080,
258 	[73] = FSCALE * 0.02599112877875534358,
259 	[74] = FSCALE * 0.02472352647033939120,
260 	[75] = FSCALE * 0.02351774585600910823,
261 	[76] = FSCALE * 0.02237077185616559577,
262 	[77] = FSCALE * 0.02127973643837716938,
263 	[78] = FSCALE * 0.02024191144580438847,
264 	[79] = FSCALE * 0.01925470177538692429,
265 	[80] = FSCALE * 0.01831563888873418029,
266 	[81] = FSCALE * 0.01742237463949351138,
267 	[82] = FSCALE * 0.01657267540176124754,
268 	[83] = FSCALE * 0.01576441648485449082,
269 	[84] = FSCALE * 0.01499557682047770621,
270 	[85] = FSCALE * 0.01426423390899925527,
271 	[86] = FSCALE * 0.01356855901220093175,
272 	[87] = FSCALE * 0.01290681258047986886,
273 	[88] = FSCALE * 0.01227733990306844117,
274 	[89] = FSCALE * 0.01167856697039544521,
275 	[90] = FSCALE * 0.01110899653824230649,
276 	[91] = FSCALE * 0.01056720438385265337,
277 	[92] = FSCALE * 0.01005183574463358164,
278 	[93] = FSCALE * 0.00956160193054350793,
279 	[94] = FSCALE * 0.00909527710169581709,
280 	[95] = FSCALE * 0.00865169520312063417,
281 	[96] = FSCALE * 0.00822974704902002884,
282 	[97] = FSCALE * 0.00782837754922577143,
283 	[98] = FSCALE * 0.00744658307092434051,
284 	[99] = FSCALE * 0.00708340892905212004,
285 	[100] = FSCALE * 0.00673794699908546709,
286 	[101] = FSCALE * 0.00640933344625638184,
287 	[102] = FSCALE * 0.00609674656551563610,
288 	[103] = FSCALE * 0.00579940472684214321,
289 	[104] = FSCALE * 0.00551656442076077241,
290 	[105] = FSCALE * 0.00524751839918138427,
291 	[106] = FSCALE * 0.00499159390691021621,
292 	[107] = FSCALE * 0.00474815099941147558,
293 	[108] = FSCALE * 0.00451658094261266798,
294 	[109] = FSCALE * 0.00429630469075234057,
295 	[110] = FSCALE * 0.00408677143846406699,
296 };
297 #endif
298 
299 #define	CCPU_EXP_MAX	110
300 
301 /*
302  * This function is analogical to the getpcpu() function in the ps(1) command.
303  * They should both calculate in the same way so that the racct %cpu
304  * calculations are consistent with the values showed by the ps(1) tool.
305  * The calculations are more complex in the 4BSD scheduler because of the value
306  * of the ccpu variable.  In ULE it is defined to be zero which saves us some
307  * work.
308  */
309 static uint64_t
310 racct_getpcpu(struct proc *p, u_int pcpu)
311 {
312 	u_int swtime;
313 #ifdef SCHED_4BSD
314 	fixpt_t pctcpu, pctcpu_next;
315 #endif
316 #ifdef SMP
317 	struct pcpu *pc;
318 	int found;
319 #endif
320 	fixpt_t p_pctcpu;
321 	struct thread *td;
322 
323 	ASSERT_RACCT_ENABLED();
324 
325 	/*
326 	 * If the process is swapped out, we count its %cpu usage as zero.
327 	 * This behaviour is consistent with the userland ps(1) tool.
328 	 */
329 	if ((p->p_flag & P_INMEM) == 0)
330 		return (0);
331 	swtime = (ticks - p->p_swtick) / hz;
332 
333 	/*
334 	 * For short-lived processes, the sched_pctcpu() returns small
335 	 * values even for cpu intensive processes.  Therefore we use
336 	 * our own estimate in this case.
337 	 */
338 	if (swtime < RACCT_PCPU_SECS)
339 		return (pcpu);
340 
341 	p_pctcpu = 0;
342 	FOREACH_THREAD_IN_PROC(p, td) {
343 		if (td == PCPU_GET(idlethread))
344 			continue;
345 #ifdef SMP
346 		found = 0;
347 		STAILQ_FOREACH(pc, &cpuhead, pc_allcpu) {
348 			if (td == pc->pc_idlethread) {
349 				found = 1;
350 				break;
351 			}
352 		}
353 		if (found)
354 			continue;
355 #endif
356 		thread_lock(td);
357 #ifdef SCHED_4BSD
358 		pctcpu = sched_pctcpu(td);
359 		/* Count also the yet unfinished second. */
360 		pctcpu_next = (pctcpu * ccpu_exp[1]) >> FSHIFT;
361 		pctcpu_next += sched_pctcpu_delta(td);
362 		p_pctcpu += max(pctcpu, pctcpu_next);
363 #else
364 		/*
365 		 * In ULE the %cpu statistics are updated on every
366 		 * sched_pctcpu() call.  So special calculations to
367 		 * account for the latest (unfinished) second are
368 		 * not needed.
369 		 */
370 		p_pctcpu += sched_pctcpu(td);
371 #endif
372 		thread_unlock(td);
373 	}
374 
375 #ifdef SCHED_4BSD
376 	if (swtime <= CCPU_EXP_MAX)
377 		return ((100 * (uint64_t)p_pctcpu * 1000000) /
378 		    (FSCALE - ccpu_exp[swtime]));
379 #endif
380 
381 	return ((100 * (uint64_t)p_pctcpu * 1000000) / FSCALE);
382 }
383 
384 static void
385 racct_add_racct(struct racct *dest, const struct racct *src)
386 {
387 	int i;
388 
389 	ASSERT_RACCT_ENABLED();
390 	mtx_assert(&racct_lock, MA_OWNED);
391 
392 	/*
393 	 * Update resource usage in dest.
394 	 */
395 	for (i = 0; i <= RACCT_MAX; i++) {
396 		KASSERT(dest->r_resources[i] >= 0,
397 		    ("%s: resource %d propagation meltdown: dest < 0",
398 		    __func__, i));
399 		KASSERT(src->r_resources[i] >= 0,
400 		    ("%s: resource %d propagation meltdown: src < 0",
401 		    __func__, i));
402 		dest->r_resources[i] += src->r_resources[i];
403 	}
404 }
405 
406 static void
407 racct_sub_racct(struct racct *dest, const struct racct *src)
408 {
409 	int i;
410 
411 	ASSERT_RACCT_ENABLED();
412 	mtx_assert(&racct_lock, MA_OWNED);
413 
414 	/*
415 	 * Update resource usage in dest.
416 	 */
417 	for (i = 0; i <= RACCT_MAX; i++) {
418 		if (!RACCT_IS_SLOPPY(i) && !RACCT_IS_DECAYING(i)) {
419 			KASSERT(dest->r_resources[i] >= 0,
420 			    ("%s: resource %d propagation meltdown: dest < 0",
421 			    __func__, i));
422 			KASSERT(src->r_resources[i] >= 0,
423 			    ("%s: resource %d propagation meltdown: src < 0",
424 			    __func__, i));
425 			KASSERT(src->r_resources[i] <= dest->r_resources[i],
426 			    ("%s: resource %d propagation meltdown: src > dest",
427 			    __func__, i));
428 		}
429 		if (RACCT_CAN_DROP(i)) {
430 			dest->r_resources[i] -= src->r_resources[i];
431 			if (dest->r_resources[i] < 0) {
432 				KASSERT(RACCT_IS_SLOPPY(i) ||
433 				    RACCT_IS_DECAYING(i),
434 				    ("%s: resource %d usage < 0", __func__, i));
435 				dest->r_resources[i] = 0;
436 			}
437 		}
438 	}
439 }
440 
441 void
442 racct_create(struct racct **racctp)
443 {
444 
445 	if (!racct_enable)
446 		return;
447 
448 	SDT_PROBE(racct, kernel, racct, create, racctp, 0, 0, 0, 0);
449 
450 	KASSERT(*racctp == NULL, ("racct already allocated"));
451 
452 	*racctp = uma_zalloc(racct_zone, M_WAITOK | M_ZERO);
453 }
454 
455 static void
456 racct_destroy_locked(struct racct **racctp)
457 {
458 	int i;
459 	struct racct *racct;
460 
461 	ASSERT_RACCT_ENABLED();
462 
463 	SDT_PROBE(racct, kernel, racct, destroy, racctp, 0, 0, 0, 0);
464 
465 	mtx_assert(&racct_lock, MA_OWNED);
466 	KASSERT(racctp != NULL, ("NULL racctp"));
467 	KASSERT(*racctp != NULL, ("NULL racct"));
468 
469 	racct = *racctp;
470 
471 	for (i = 0; i <= RACCT_MAX; i++) {
472 		if (RACCT_IS_SLOPPY(i))
473 			continue;
474 		if (!RACCT_IS_RECLAIMABLE(i))
475 			continue;
476 		KASSERT(racct->r_resources[i] == 0,
477 		    ("destroying non-empty racct: "
478 		    "%ju allocated for resource %d\n",
479 		    racct->r_resources[i], i));
480 	}
481 	uma_zfree(racct_zone, racct);
482 	*racctp = NULL;
483 }
484 
485 void
486 racct_destroy(struct racct **racct)
487 {
488 
489 	if (!racct_enable)
490 		return;
491 
492 	mtx_lock(&racct_lock);
493 	racct_destroy_locked(racct);
494 	mtx_unlock(&racct_lock);
495 }
496 
497 /*
498  * Increase consumption of 'resource' by 'amount' for 'racct'
499  * and all its parents.  Differently from other cases, 'amount' here
500  * may be less than zero.
501  */
502 static void
503 racct_adjust_resource(struct racct *racct, int resource,
504     uint64_t amount)
505 {
506 
507 	ASSERT_RACCT_ENABLED();
508 	mtx_assert(&racct_lock, MA_OWNED);
509 	KASSERT(racct != NULL, ("NULL racct"));
510 
511 	racct->r_resources[resource] += amount;
512 	if (racct->r_resources[resource] < 0) {
513 		KASSERT(RACCT_IS_SLOPPY(resource) || RACCT_IS_DECAYING(resource),
514 		    ("%s: resource %d usage < 0", __func__, resource));
515 		racct->r_resources[resource] = 0;
516 	}
517 
518 	/*
519 	 * There are some cases where the racct %cpu resource would grow
520 	 * beyond 100%.
521 	 * For example in racct_proc_exit() we add the process %cpu usage
522 	 * to the ucred racct containers.  If too many processes terminated
523 	 * in a short time span, the ucred %cpu resource could grow too much.
524 	 * Also, the 4BSD scheduler sometimes returns for a thread more than
525 	 * 100% cpu usage.  So we set a boundary here to 100%.
526 	 */
527 	if ((resource == RACCT_PCTCPU) &&
528 	    (racct->r_resources[RACCT_PCTCPU] > 100 * 1000000))
529 		racct->r_resources[RACCT_PCTCPU] = 100 * 1000000;
530 }
531 
532 static int
533 racct_add_locked(struct proc *p, int resource, uint64_t amount)
534 {
535 #ifdef RCTL
536 	int error;
537 #endif
538 
539 	ASSERT_RACCT_ENABLED();
540 
541 	SDT_PROBE(racct, kernel, rusage, add, p, resource, amount, 0, 0);
542 
543 	/*
544 	 * We need proc lock to dereference p->p_ucred.
545 	 */
546 	PROC_LOCK_ASSERT(p, MA_OWNED);
547 
548 #ifdef RCTL
549 	error = rctl_enforce(p, resource, amount);
550 	if (error && RACCT_IS_DENIABLE(resource)) {
551 		SDT_PROBE(racct, kernel, rusage, add__failure, p, resource,
552 		    amount, 0, 0);
553 		return (error);
554 	}
555 #endif
556 	racct_adjust_resource(p->p_racct, resource, amount);
557 	racct_add_cred_locked(p->p_ucred, resource, amount);
558 
559 	return (0);
560 }
561 
562 /*
563  * Increase allocation of 'resource' by 'amount' for process 'p'.
564  * Return 0 if it's below limits, or errno, if it's not.
565  */
566 int
567 racct_add(struct proc *p, int resource, uint64_t amount)
568 {
569 	int error;
570 
571 	if (!racct_enable)
572 		return (0);
573 
574 	mtx_lock(&racct_lock);
575 	error = racct_add_locked(p, resource, amount);
576 	mtx_unlock(&racct_lock);
577 	return (error);
578 }
579 
580 static void
581 racct_add_cred_locked(struct ucred *cred, int resource, uint64_t amount)
582 {
583 	struct prison *pr;
584 
585 	ASSERT_RACCT_ENABLED();
586 
587 	SDT_PROBE(racct, kernel, rusage, add__cred, cred, resource, amount,
588 	    0, 0);
589 
590 	racct_adjust_resource(cred->cr_ruidinfo->ui_racct, resource, amount);
591 	for (pr = cred->cr_prison; pr != NULL; pr = pr->pr_parent)
592 		racct_adjust_resource(pr->pr_prison_racct->prr_racct, resource,
593 		    amount);
594 	racct_adjust_resource(cred->cr_loginclass->lc_racct, resource, amount);
595 }
596 
597 /*
598  * Increase allocation of 'resource' by 'amount' for credential 'cred'.
599  * Doesn't check for limits and never fails.
600  *
601  * XXX: Shouldn't this ever return an error?
602  */
603 void
604 racct_add_cred(struct ucred *cred, int resource, uint64_t amount)
605 {
606 
607 	if (!racct_enable)
608 		return;
609 
610 	mtx_lock(&racct_lock);
611 	racct_add_cred_locked(cred, resource, amount);
612 	mtx_unlock(&racct_lock);
613 }
614 
615 /*
616  * Increase allocation of 'resource' by 'amount' for process 'p'.
617  * Doesn't check for limits and never fails.
618  */
619 void
620 racct_add_force(struct proc *p, int resource, uint64_t amount)
621 {
622 
623 	if (!racct_enable)
624 		return;
625 
626 	SDT_PROBE(racct, kernel, rusage, add__force, p, resource, amount, 0, 0);
627 
628 	/*
629 	 * We need proc lock to dereference p->p_ucred.
630 	 */
631 	PROC_LOCK_ASSERT(p, MA_OWNED);
632 
633 	mtx_lock(&racct_lock);
634 	racct_adjust_resource(p->p_racct, resource, amount);
635 	mtx_unlock(&racct_lock);
636 	racct_add_cred(p->p_ucred, resource, amount);
637 }
638 
639 static int
640 racct_set_locked(struct proc *p, int resource, uint64_t amount)
641 {
642 	int64_t old_amount, decayed_amount;
643 	int64_t diff_proc, diff_cred;
644 #ifdef RCTL
645 	int error;
646 #endif
647 
648 	ASSERT_RACCT_ENABLED();
649 
650 	SDT_PROBE(racct, kernel, rusage, set, p, resource, amount, 0, 0);
651 
652 	/*
653 	 * We need proc lock to dereference p->p_ucred.
654 	 */
655 	PROC_LOCK_ASSERT(p, MA_OWNED);
656 
657 	old_amount = p->p_racct->r_resources[resource];
658 	/*
659 	 * The diffs may be negative.
660 	 */
661 	diff_proc = amount - old_amount;
662 	if (RACCT_IS_DECAYING(resource)) {
663 		/*
664 		 * Resources in per-credential racct containers may decay.
665 		 * If this is the case, we need to calculate the difference
666 		 * between the new amount and the proportional value of the
667 		 * old amount that has decayed in the ucred racct containers.
668 		 */
669 		decayed_amount = old_amount * RACCT_DECAY_FACTOR / FSCALE;
670 		diff_cred = amount - decayed_amount;
671 	} else
672 		diff_cred = diff_proc;
673 #ifdef notyet
674 	KASSERT(diff_proc >= 0 || RACCT_CAN_DROP(resource),
675 	    ("%s: usage of non-droppable resource %d dropping", __func__,
676 	     resource));
677 #endif
678 #ifdef RCTL
679 	if (diff_proc > 0) {
680 		error = rctl_enforce(p, resource, diff_proc);
681 		if (error && RACCT_IS_DENIABLE(resource)) {
682 			SDT_PROBE(racct, kernel, rusage, set__failure, p,
683 			    resource, amount, 0, 0);
684 			return (error);
685 		}
686 	}
687 #endif
688 	racct_adjust_resource(p->p_racct, resource, diff_proc);
689 	if (diff_cred > 0)
690 		racct_add_cred_locked(p->p_ucred, resource, diff_cred);
691 	else if (diff_cred < 0)
692 		racct_sub_cred_locked(p->p_ucred, resource, -diff_cred);
693 
694 	return (0);
695 }
696 
697 /*
698  * Set allocation of 'resource' to 'amount' for process 'p'.
699  * Return 0 if it's below limits, or errno, if it's not.
700  *
701  * Note that decreasing the allocation always returns 0,
702  * even if it's above the limit.
703  */
704 int
705 racct_set(struct proc *p, int resource, uint64_t amount)
706 {
707 	int error;
708 
709 	if (!racct_enable)
710 		return (0);
711 
712 	mtx_lock(&racct_lock);
713 	error = racct_set_locked(p, resource, amount);
714 	mtx_unlock(&racct_lock);
715 	return (error);
716 }
717 
718 static void
719 racct_set_force_locked(struct proc *p, int resource, uint64_t amount)
720 {
721 	int64_t old_amount, decayed_amount;
722 	int64_t diff_proc, diff_cred;
723 
724 	ASSERT_RACCT_ENABLED();
725 
726 	SDT_PROBE(racct, kernel, rusage, set, p, resource, amount, 0, 0);
727 
728 	/*
729 	 * We need proc lock to dereference p->p_ucred.
730 	 */
731 	PROC_LOCK_ASSERT(p, MA_OWNED);
732 
733 	old_amount = p->p_racct->r_resources[resource];
734 	/*
735 	 * The diffs may be negative.
736 	 */
737 	diff_proc = amount - old_amount;
738 	if (RACCT_IS_DECAYING(resource)) {
739 		/*
740 		 * Resources in per-credential racct containers may decay.
741 		 * If this is the case, we need to calculate the difference
742 		 * between the new amount and the proportional value of the
743 		 * old amount that has decayed in the ucred racct containers.
744 		 */
745 		decayed_amount = old_amount * RACCT_DECAY_FACTOR / FSCALE;
746 		diff_cred = amount - decayed_amount;
747 	} else
748 		diff_cred = diff_proc;
749 
750 	racct_adjust_resource(p->p_racct, resource, diff_proc);
751 	if (diff_cred > 0)
752 		racct_add_cred_locked(p->p_ucred, resource, diff_cred);
753 	else if (diff_cred < 0)
754 		racct_sub_cred_locked(p->p_ucred, resource, -diff_cred);
755 }
756 
757 void
758 racct_set_force(struct proc *p, int resource, uint64_t amount)
759 {
760 
761 	if (!racct_enable)
762 		return;
763 
764 	mtx_lock(&racct_lock);
765 	racct_set_force_locked(p, resource, amount);
766 	mtx_unlock(&racct_lock);
767 }
768 
769 /*
770  * Returns amount of 'resource' the process 'p' can keep allocated.
771  * Allocating more than that would be denied, unless the resource
772  * is marked undeniable.  Amount of already allocated resource does
773  * not matter.
774  */
775 uint64_t
776 racct_get_limit(struct proc *p, int resource)
777 {
778 
779 	if (!racct_enable)
780 		return (UINT64_MAX);
781 
782 #ifdef RCTL
783 	return (rctl_get_limit(p, resource));
784 #else
785 	return (UINT64_MAX);
786 #endif
787 }
788 
789 /*
790  * Returns amount of 'resource' the process 'p' can keep allocated.
791  * Allocating more than that would be denied, unless the resource
792  * is marked undeniable.  Amount of already allocated resource does
793  * matter.
794  */
795 uint64_t
796 racct_get_available(struct proc *p, int resource)
797 {
798 
799 	if (!racct_enable)
800 		return (UINT64_MAX);
801 
802 #ifdef RCTL
803 	return (rctl_get_available(p, resource));
804 #else
805 	return (UINT64_MAX);
806 #endif
807 }
808 
809 /*
810  * Returns amount of the %cpu resource that process 'p' can add to its %cpu
811  * utilization.  Adding more than that would lead to the process being
812  * throttled.
813  */
814 static int64_t
815 racct_pcpu_available(struct proc *p)
816 {
817 
818 	ASSERT_RACCT_ENABLED();
819 
820 #ifdef RCTL
821 	return (rctl_pcpu_available(p));
822 #else
823 	return (INT64_MAX);
824 #endif
825 }
826 
827 /*
828  * Decrease allocation of 'resource' by 'amount' for process 'p'.
829  */
830 void
831 racct_sub(struct proc *p, int resource, uint64_t amount)
832 {
833 
834 	if (!racct_enable)
835 		return;
836 
837 	SDT_PROBE(racct, kernel, rusage, sub, p, resource, amount, 0, 0);
838 
839 	/*
840 	 * We need proc lock to dereference p->p_ucred.
841 	 */
842 	PROC_LOCK_ASSERT(p, MA_OWNED);
843 	KASSERT(RACCT_CAN_DROP(resource),
844 	    ("%s: called for non-droppable resource %d", __func__, resource));
845 
846 	mtx_lock(&racct_lock);
847 	KASSERT(amount <= p->p_racct->r_resources[resource],
848 	    ("%s: freeing %ju of resource %d, which is more "
849 	     "than allocated %jd for %s (pid %d)", __func__, amount, resource,
850 	    (intmax_t)p->p_racct->r_resources[resource], p->p_comm, p->p_pid));
851 
852 	racct_adjust_resource(p->p_racct, resource, -amount);
853 	racct_sub_cred_locked(p->p_ucred, resource, amount);
854 	mtx_unlock(&racct_lock);
855 }
856 
857 static void
858 racct_sub_cred_locked(struct ucred *cred, int resource, uint64_t amount)
859 {
860 	struct prison *pr;
861 
862 	ASSERT_RACCT_ENABLED();
863 
864 	SDT_PROBE(racct, kernel, rusage, sub__cred, cred, resource, amount,
865 	    0, 0);
866 
867 #ifdef notyet
868 	KASSERT(RACCT_CAN_DROP(resource),
869 	    ("%s: called for resource %d which can not drop", __func__,
870 	     resource));
871 #endif
872 
873 	racct_adjust_resource(cred->cr_ruidinfo->ui_racct, resource, -amount);
874 	for (pr = cred->cr_prison; pr != NULL; pr = pr->pr_parent)
875 		racct_adjust_resource(pr->pr_prison_racct->prr_racct, resource,
876 		    -amount);
877 	racct_adjust_resource(cred->cr_loginclass->lc_racct, resource, -amount);
878 }
879 
880 /*
881  * Decrease allocation of 'resource' by 'amount' for credential 'cred'.
882  */
883 void
884 racct_sub_cred(struct ucred *cred, int resource, uint64_t amount)
885 {
886 
887 	if (!racct_enable)
888 		return;
889 
890 	mtx_lock(&racct_lock);
891 	racct_sub_cred_locked(cred, resource, amount);
892 	mtx_unlock(&racct_lock);
893 }
894 
895 /*
896  * Inherit resource usage information from the parent process.
897  */
898 int
899 racct_proc_fork(struct proc *parent, struct proc *child)
900 {
901 	int i, error = 0;
902 
903 	if (!racct_enable)
904 		return (0);
905 
906 	/*
907 	 * Create racct for the child process.
908 	 */
909 	racct_create(&child->p_racct);
910 
911 	PROC_LOCK(parent);
912 	PROC_LOCK(child);
913 	mtx_lock(&racct_lock);
914 
915 #ifdef RCTL
916 	error = rctl_proc_fork(parent, child);
917 	if (error != 0)
918 		goto out;
919 #endif
920 
921 	/* Init process cpu time. */
922 	child->p_prev_runtime = 0;
923 	child->p_throttled = 0;
924 
925 	/*
926 	 * Inherit resource usage.
927 	 */
928 	for (i = 0; i <= RACCT_MAX; i++) {
929 		if (parent->p_racct->r_resources[i] == 0 ||
930 		    !RACCT_IS_INHERITABLE(i))
931 			continue;
932 
933 		error = racct_set_locked(child, i,
934 		    parent->p_racct->r_resources[i]);
935 		if (error != 0)
936 			goto out;
937 	}
938 
939 	error = racct_add_locked(child, RACCT_NPROC, 1);
940 	error += racct_add_locked(child, RACCT_NTHR, 1);
941 
942 out:
943 	mtx_unlock(&racct_lock);
944 	PROC_UNLOCK(child);
945 	PROC_UNLOCK(parent);
946 
947 	if (error != 0)
948 		racct_proc_exit(child);
949 
950 	return (error);
951 }
952 
953 /*
954  * Called at the end of fork1(), to handle rules that require the process
955  * to be fully initialized.
956  */
957 void
958 racct_proc_fork_done(struct proc *child)
959 {
960 
961 #ifdef RCTL
962 	if (!racct_enable)
963 		return;
964 
965 	PROC_LOCK(child);
966 	mtx_lock(&racct_lock);
967 	rctl_enforce(child, RACCT_NPROC, 0);
968 	rctl_enforce(child, RACCT_NTHR, 0);
969 	mtx_unlock(&racct_lock);
970 	PROC_UNLOCK(child);
971 #endif
972 }
973 
974 void
975 racct_proc_exit(struct proc *p)
976 {
977 	int i;
978 	uint64_t runtime;
979 	struct timeval wallclock;
980 	uint64_t pct_estimate, pct;
981 
982 	if (!racct_enable)
983 		return;
984 
985 	PROC_LOCK(p);
986 	/*
987 	 * We don't need to calculate rux, proc_reap() has already done this.
988 	 */
989 	runtime = cputick2usec(p->p_rux.rux_runtime);
990 #ifdef notyet
991 	KASSERT(runtime >= p->p_prev_runtime, ("runtime < p_prev_runtime"));
992 #else
993 	if (runtime < p->p_prev_runtime)
994 		runtime = p->p_prev_runtime;
995 #endif
996 	microuptime(&wallclock);
997 	timevalsub(&wallclock, &p->p_stats->p_start);
998 	if (wallclock.tv_sec > 0 || wallclock.tv_usec > 0) {
999 		pct_estimate = (1000000 * runtime * 100) /
1000 		    ((uint64_t)wallclock.tv_sec * 1000000 +
1001 		    wallclock.tv_usec);
1002 	} else
1003 		pct_estimate = 0;
1004 	pct = racct_getpcpu(p, pct_estimate);
1005 
1006 	mtx_lock(&racct_lock);
1007 	racct_set_locked(p, RACCT_CPU, runtime);
1008 	racct_add_cred_locked(p->p_ucred, RACCT_PCTCPU, pct);
1009 
1010 	for (i = 0; i <= RACCT_MAX; i++) {
1011 		if (p->p_racct->r_resources[i] == 0)
1012 			continue;
1013 	    	if (!RACCT_IS_RECLAIMABLE(i))
1014 			continue;
1015 		racct_set_locked(p, i, 0);
1016 	}
1017 
1018 	mtx_unlock(&racct_lock);
1019 	PROC_UNLOCK(p);
1020 
1021 #ifdef RCTL
1022 	rctl_racct_release(p->p_racct);
1023 #endif
1024 	racct_destroy(&p->p_racct);
1025 }
1026 
1027 /*
1028  * Called after credentials change, to move resource utilisation
1029  * between raccts.
1030  */
1031 void
1032 racct_proc_ucred_changed(struct proc *p, struct ucred *oldcred,
1033     struct ucred *newcred)
1034 {
1035 	struct uidinfo *olduip, *newuip;
1036 	struct loginclass *oldlc, *newlc;
1037 	struct prison *oldpr, *newpr, *pr;
1038 
1039 	if (!racct_enable)
1040 		return;
1041 
1042 	PROC_LOCK_ASSERT(p, MA_NOTOWNED);
1043 
1044 	newuip = newcred->cr_ruidinfo;
1045 	olduip = oldcred->cr_ruidinfo;
1046 	newlc = newcred->cr_loginclass;
1047 	oldlc = oldcred->cr_loginclass;
1048 	newpr = newcred->cr_prison;
1049 	oldpr = oldcred->cr_prison;
1050 
1051 	mtx_lock(&racct_lock);
1052 	if (newuip != olduip) {
1053 		racct_sub_racct(olduip->ui_racct, p->p_racct);
1054 		racct_add_racct(newuip->ui_racct, p->p_racct);
1055 	}
1056 	if (newlc != oldlc) {
1057 		racct_sub_racct(oldlc->lc_racct, p->p_racct);
1058 		racct_add_racct(newlc->lc_racct, p->p_racct);
1059 	}
1060 	if (newpr != oldpr) {
1061 		for (pr = oldpr; pr != NULL; pr = pr->pr_parent)
1062 			racct_sub_racct(pr->pr_prison_racct->prr_racct,
1063 			    p->p_racct);
1064 		for (pr = newpr; pr != NULL; pr = pr->pr_parent)
1065 			racct_add_racct(pr->pr_prison_racct->prr_racct,
1066 			    p->p_racct);
1067 	}
1068 	mtx_unlock(&racct_lock);
1069 
1070 #ifdef RCTL
1071 	rctl_proc_ucred_changed(p, newcred);
1072 #endif
1073 }
1074 
1075 void
1076 racct_move(struct racct *dest, struct racct *src)
1077 {
1078 
1079 	ASSERT_RACCT_ENABLED();
1080 
1081 	mtx_lock(&racct_lock);
1082 
1083 	racct_add_racct(dest, src);
1084 	racct_sub_racct(src, src);
1085 
1086 	mtx_unlock(&racct_lock);
1087 }
1088 
1089 static void
1090 racct_proc_throttle(struct proc *p)
1091 {
1092 	struct thread *td;
1093 #ifdef SMP
1094 	int cpuid;
1095 #endif
1096 
1097 	ASSERT_RACCT_ENABLED();
1098 	PROC_LOCK_ASSERT(p, MA_OWNED);
1099 
1100 	/*
1101 	 * Do not block kernel processes.  Also do not block processes with
1102 	 * low %cpu utilization to improve interactivity.
1103 	 */
1104 	if (((p->p_flag & (P_SYSTEM | P_KTHREAD)) != 0) ||
1105 	    (p->p_racct->r_resources[RACCT_PCTCPU] <= pcpu_threshold))
1106 		return;
1107 	p->p_throttled = 1;
1108 
1109 	FOREACH_THREAD_IN_PROC(p, td) {
1110 		thread_lock(td);
1111 		switch (td->td_state) {
1112 		case TDS_RUNQ:
1113 			/*
1114 			 * If the thread is on the scheduler run-queue, we can
1115 			 * not just remove it from there.  So we set the flag
1116 			 * TDF_NEEDRESCHED for the thread, so that once it is
1117 			 * running, it is taken off the cpu as soon as possible.
1118 			 */
1119 			td->td_flags |= TDF_NEEDRESCHED;
1120 			break;
1121 		case TDS_RUNNING:
1122 			/*
1123 			 * If the thread is running, we request a context
1124 			 * switch for it by setting the TDF_NEEDRESCHED flag.
1125 			 */
1126 			td->td_flags |= TDF_NEEDRESCHED;
1127 #ifdef SMP
1128 			cpuid = td->td_oncpu;
1129 			if ((cpuid != NOCPU) && (td != curthread))
1130 				ipi_cpu(cpuid, IPI_AST);
1131 #endif
1132 			break;
1133 		default:
1134 			break;
1135 		}
1136 		thread_unlock(td);
1137 	}
1138 }
1139 
1140 static void
1141 racct_proc_wakeup(struct proc *p)
1142 {
1143 
1144 	ASSERT_RACCT_ENABLED();
1145 
1146 	PROC_LOCK_ASSERT(p, MA_OWNED);
1147 
1148 	if (p->p_throttled) {
1149 		p->p_throttled = 0;
1150 		wakeup(p->p_racct);
1151 	}
1152 }
1153 
1154 static void
1155 racct_decay_resource(struct racct *racct, void * res, void* dummy)
1156 {
1157 	int resource;
1158 	int64_t r_old, r_new;
1159 
1160 	ASSERT_RACCT_ENABLED();
1161 
1162 	resource = *(int *)res;
1163 	r_old = racct->r_resources[resource];
1164 
1165 	/* If there is nothing to decay, just exit. */
1166 	if (r_old <= 0)
1167 		return;
1168 
1169 	mtx_lock(&racct_lock);
1170 	r_new = r_old * RACCT_DECAY_FACTOR / FSCALE;
1171 	racct->r_resources[resource] = r_new;
1172 	mtx_unlock(&racct_lock);
1173 }
1174 
1175 static void
1176 racct_decay(int resource)
1177 {
1178 
1179 	ASSERT_RACCT_ENABLED();
1180 
1181 	ui_racct_foreach(racct_decay_resource, &resource, NULL);
1182 	loginclass_racct_foreach(racct_decay_resource, &resource, NULL);
1183 	prison_racct_foreach(racct_decay_resource, &resource, NULL);
1184 }
1185 
1186 static void
1187 racctd(void)
1188 {
1189 	struct thread *td;
1190 	struct proc *p;
1191 	struct timeval wallclock;
1192 	uint64_t runtime;
1193 	uint64_t pct, pct_estimate;
1194 
1195 	ASSERT_RACCT_ENABLED();
1196 
1197 	for (;;) {
1198 		racct_decay(RACCT_PCTCPU);
1199 
1200 		sx_slock(&allproc_lock);
1201 
1202 		LIST_FOREACH(p, &zombproc, p_list) {
1203 			PROC_LOCK(p);
1204 			racct_set(p, RACCT_PCTCPU, 0);
1205 			PROC_UNLOCK(p);
1206 		}
1207 
1208 		FOREACH_PROC_IN_SYSTEM(p) {
1209 			PROC_LOCK(p);
1210 			if (p->p_state != PRS_NORMAL) {
1211 				PROC_UNLOCK(p);
1212 				continue;
1213 			}
1214 
1215 			microuptime(&wallclock);
1216 			timevalsub(&wallclock, &p->p_stats->p_start);
1217 			PROC_STATLOCK(p);
1218 			FOREACH_THREAD_IN_PROC(p, td)
1219 				ruxagg(p, td);
1220 			runtime = cputick2usec(p->p_rux.rux_runtime);
1221 			PROC_STATUNLOCK(p);
1222 #ifdef notyet
1223 			KASSERT(runtime >= p->p_prev_runtime,
1224 			    ("runtime < p_prev_runtime"));
1225 #else
1226 			if (runtime < p->p_prev_runtime)
1227 				runtime = p->p_prev_runtime;
1228 #endif
1229 			p->p_prev_runtime = runtime;
1230 			if (wallclock.tv_sec > 0 || wallclock.tv_usec > 0) {
1231 				pct_estimate = (1000000 * runtime * 100) /
1232 				    ((uint64_t)wallclock.tv_sec * 1000000 +
1233 				    wallclock.tv_usec);
1234 			} else
1235 				pct_estimate = 0;
1236 			pct = racct_getpcpu(p, pct_estimate);
1237 			mtx_lock(&racct_lock);
1238 			racct_set_force_locked(p, RACCT_PCTCPU, pct);
1239 			racct_set_locked(p, RACCT_CPU, runtime);
1240 			racct_set_locked(p, RACCT_WALLCLOCK,
1241 			    (uint64_t)wallclock.tv_sec * 1000000 +
1242 			    wallclock.tv_usec);
1243 			mtx_unlock(&racct_lock);
1244 			PROC_UNLOCK(p);
1245 		}
1246 
1247 		/*
1248 		 * To ensure that processes are throttled in a fair way, we need
1249 		 * to iterate over all processes again and check the limits
1250 		 * for %cpu resource only after ucred racct containers have been
1251 		 * properly filled.
1252 		 */
1253 		FOREACH_PROC_IN_SYSTEM(p) {
1254 			PROC_LOCK(p);
1255 			if (p->p_state != PRS_NORMAL) {
1256 				PROC_UNLOCK(p);
1257 				continue;
1258 			}
1259 
1260 			if (racct_pcpu_available(p) <= 0)
1261 				racct_proc_throttle(p);
1262 			else if (p->p_throttled)
1263 				racct_proc_wakeup(p);
1264 			PROC_UNLOCK(p);
1265 		}
1266 		sx_sunlock(&allproc_lock);
1267 		pause("-", hz);
1268 	}
1269 }
1270 
1271 static struct kproc_desc racctd_kp = {
1272 	"racctd",
1273 	racctd,
1274 	NULL
1275 };
1276 
1277 static void
1278 racctd_init(void)
1279 {
1280 	if (!racct_enable)
1281 		return;
1282 
1283 	kproc_start(&racctd_kp);
1284 }
1285 SYSINIT(racctd, SI_SUB_RACCTD, SI_ORDER_FIRST, racctd_init, NULL);
1286 
1287 static void
1288 racct_init(void)
1289 {
1290 	if (!racct_enable)
1291 		return;
1292 
1293 	racct_zone = uma_zcreate("racct", sizeof(struct racct),
1294 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
1295 	/*
1296 	 * XXX: Move this somewhere.
1297 	 */
1298 	prison0.pr_prison_racct = prison_racct_find("0");
1299 }
1300 SYSINIT(racct, SI_SUB_RACCT, SI_ORDER_FIRST, racct_init, NULL);
1301 
1302 #endif /* !RACCT */
1303