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