xref: /freebsd/sys/kern/kern_racct.c (revision 4c230cdafd698f20a23f42f6c66fe725fb1b6bbf)
1097055e2SEdward Tomasz Napierala /*-
2097055e2SEdward Tomasz Napierala  * Copyright (c) 2010 The FreeBSD Foundation
3097055e2SEdward Tomasz Napierala  * All rights reserved.
4097055e2SEdward Tomasz Napierala  *
5097055e2SEdward Tomasz Napierala  * This software was developed by Edward Tomasz Napierala under sponsorship
6097055e2SEdward Tomasz Napierala  * from the FreeBSD Foundation.
7097055e2SEdward Tomasz Napierala  *
8097055e2SEdward Tomasz Napierala  * Redistribution and use in source and binary forms, with or without
9097055e2SEdward Tomasz Napierala  * modification, are permitted provided that the following conditions
10097055e2SEdward Tomasz Napierala  * are met:
11097055e2SEdward Tomasz Napierala  * 1. Redistributions of source code must retain the above copyright
12097055e2SEdward Tomasz Napierala  *    notice, this list of conditions and the following disclaimer.
13097055e2SEdward Tomasz Napierala  * 2. Redistributions in binary form must reproduce the above copyright
14097055e2SEdward Tomasz Napierala  *    notice, this list of conditions and the following disclaimer in the
15097055e2SEdward Tomasz Napierala  *    documentation and/or other materials provided with the distribution.
16097055e2SEdward Tomasz Napierala  *
17097055e2SEdward Tomasz Napierala  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18097055e2SEdward Tomasz Napierala  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19097055e2SEdward Tomasz Napierala  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20097055e2SEdward Tomasz Napierala  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21097055e2SEdward Tomasz Napierala  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22097055e2SEdward Tomasz Napierala  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23097055e2SEdward Tomasz Napierala  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24097055e2SEdward Tomasz Napierala  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25097055e2SEdward Tomasz Napierala  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26097055e2SEdward Tomasz Napierala  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27097055e2SEdward Tomasz Napierala  * SUCH DAMAGE.
28097055e2SEdward Tomasz Napierala  *
29097055e2SEdward Tomasz Napierala  * $FreeBSD$
30097055e2SEdward Tomasz Napierala  */
31097055e2SEdward Tomasz Napierala 
32097055e2SEdward Tomasz Napierala #include <sys/cdefs.h>
33097055e2SEdward Tomasz Napierala __FBSDID("$FreeBSD$");
34097055e2SEdward Tomasz Napierala 
3536af9869SEdward Tomasz Napierala #include "opt_sched.h"
36097055e2SEdward Tomasz Napierala 
37097055e2SEdward Tomasz Napierala #include <sys/param.h>
380e225211SAndriy Gapon #include <sys/systm.h>
39097055e2SEdward Tomasz Napierala #include <sys/eventhandler.h>
40097055e2SEdward Tomasz Napierala #include <sys/jail.h>
41097055e2SEdward Tomasz Napierala #include <sys/kernel.h>
42097055e2SEdward Tomasz Napierala #include <sys/kthread.h>
43097055e2SEdward Tomasz Napierala #include <sys/lock.h>
44097055e2SEdward Tomasz Napierala #include <sys/loginclass.h>
45097055e2SEdward Tomasz Napierala #include <sys/malloc.h>
46097055e2SEdward Tomasz Napierala #include <sys/mutex.h>
47097055e2SEdward Tomasz Napierala #include <sys/proc.h>
48097055e2SEdward Tomasz Napierala #include <sys/racct.h>
49097055e2SEdward Tomasz Napierala #include <sys/resourcevar.h>
50097055e2SEdward Tomasz Napierala #include <sys/sbuf.h>
51097055e2SEdward Tomasz Napierala #include <sys/sched.h>
52097055e2SEdward Tomasz Napierala #include <sys/sdt.h>
5336af9869SEdward Tomasz Napierala #include <sys/smp.h>
54097055e2SEdward Tomasz Napierala #include <sys/sx.h>
5536af9869SEdward Tomasz Napierala #include <sys/sysctl.h>
56097055e2SEdward Tomasz Napierala #include <sys/sysent.h>
57097055e2SEdward Tomasz Napierala #include <sys/sysproto.h>
58097055e2SEdward Tomasz Napierala #include <sys/umtx.h>
5936af9869SEdward Tomasz Napierala #include <machine/smp.h>
60097055e2SEdward Tomasz Napierala 
61097055e2SEdward Tomasz Napierala #ifdef RCTL
62097055e2SEdward Tomasz Napierala #include <sys/rctl.h>
63097055e2SEdward Tomasz Napierala #endif
64097055e2SEdward Tomasz Napierala 
65097055e2SEdward Tomasz Napierala #ifdef RACCT
66097055e2SEdward Tomasz Napierala 
67097055e2SEdward Tomasz Napierala FEATURE(racct, "Resource Accounting");
68097055e2SEdward Tomasz Napierala 
6936af9869SEdward Tomasz Napierala /*
7036af9869SEdward Tomasz Napierala  * Do not block processes that have their %cpu usage <= pcpu_threshold.
7136af9869SEdward Tomasz Napierala  */
7236af9869SEdward Tomasz Napierala static int pcpu_threshold = 1;
73ba8f0eb8SEdward Tomasz Napierala #ifdef RACCT_DEFAULT_TO_DISABLED
744b5c9cf6SEdward Tomasz Napierala int racct_enable = 0;
754b5c9cf6SEdward Tomasz Napierala #else
764b5c9cf6SEdward Tomasz Napierala int racct_enable = 1;
774b5c9cf6SEdward Tomasz Napierala #endif
7836af9869SEdward Tomasz Napierala 
7936af9869SEdward Tomasz Napierala SYSCTL_NODE(_kern, OID_AUTO, racct, CTLFLAG_RW, 0, "Resource Accounting");
804b5c9cf6SEdward Tomasz Napierala SYSCTL_UINT(_kern_racct, OID_AUTO, enable, CTLFLAG_RDTUN, &racct_enable,
814b5c9cf6SEdward Tomasz Napierala     0, "Enable RACCT/RCTL");
8236af9869SEdward Tomasz Napierala SYSCTL_UINT(_kern_racct, OID_AUTO, pcpu_threshold, CTLFLAG_RW, &pcpu_threshold,
8336af9869SEdward Tomasz Napierala     0, "Processes with higher %cpu usage than this value can be throttled.");
8436af9869SEdward Tomasz Napierala 
8536af9869SEdward Tomasz Napierala /*
8636af9869SEdward Tomasz Napierala  * How many seconds it takes to use the scheduler %cpu calculations.  When a
8736af9869SEdward Tomasz Napierala  * process starts, we compute its %cpu usage by dividing its runtime by the
8836af9869SEdward Tomasz Napierala  * process wall clock time.  After RACCT_PCPU_SECS pass, we use the value
8936af9869SEdward Tomasz Napierala  * provided by the scheduler.
9036af9869SEdward Tomasz Napierala  */
9136af9869SEdward Tomasz Napierala #define RACCT_PCPU_SECS		3
9236af9869SEdward Tomasz Napierala 
93097055e2SEdward Tomasz Napierala static struct mtx racct_lock;
94097055e2SEdward Tomasz Napierala MTX_SYSINIT(racct_lock, &racct_lock, "racct lock", MTX_DEF);
95097055e2SEdward Tomasz Napierala 
96*4c230cdaSEdward Tomasz Napierala #define RACCT_LOCK()		mtx_lock(&racct_lock)
97*4c230cdaSEdward Tomasz Napierala #define RACCT_UNLOCK()		mtx_unlock(&racct_lock)
98*4c230cdaSEdward Tomasz Napierala #define RACCT_LOCK_ASSERT()	mtx_assert(&racct_lock, MA_OWNED)
99*4c230cdaSEdward Tomasz Napierala 
100097055e2SEdward Tomasz Napierala static uma_zone_t racct_zone;
101097055e2SEdward Tomasz Napierala 
102097055e2SEdward Tomasz Napierala static void racct_sub_racct(struct racct *dest, const struct racct *src);
103097055e2SEdward Tomasz Napierala static void racct_sub_cred_locked(struct ucred *cred, int resource,
104097055e2SEdward Tomasz Napierala 		uint64_t amount);
105097055e2SEdward Tomasz Napierala static void racct_add_cred_locked(struct ucred *cred, int resource,
106097055e2SEdward Tomasz Napierala 		uint64_t amount);
107097055e2SEdward Tomasz Napierala 
108097055e2SEdward Tomasz Napierala SDT_PROVIDER_DEFINE(racct);
10936160958SMark Johnston SDT_PROBE_DEFINE3(racct, , rusage, add,
110097055e2SEdward Tomasz Napierala     "struct proc *", "int", "uint64_t");
11136160958SMark Johnston SDT_PROBE_DEFINE3(racct, , rusage, add__failure,
112097055e2SEdward Tomasz Napierala     "struct proc *", "int", "uint64_t");
11336160958SMark Johnston SDT_PROBE_DEFINE3(racct, , rusage, add__cred,
11436160958SMark Johnston     "struct ucred *", "int", "uint64_t");
11536160958SMark Johnston SDT_PROBE_DEFINE3(racct, , rusage, add__force,
11636160958SMark Johnston     "struct proc *", "int", "uint64_t");
11736160958SMark Johnston SDT_PROBE_DEFINE3(racct, , rusage, set,
11836160958SMark Johnston     "struct proc *", "int", "uint64_t");
11936160958SMark Johnston SDT_PROBE_DEFINE3(racct, , rusage, set__failure,
12036160958SMark Johnston     "struct proc *", "int", "uint64_t");
12110287198SEdward Tomasz Napierala SDT_PROBE_DEFINE3(racct, , rusage, set__force,
12210287198SEdward Tomasz Napierala     "struct proc *", "int", "uint64_t");
12336160958SMark Johnston SDT_PROBE_DEFINE3(racct, , rusage, sub,
12436160958SMark Johnston     "struct proc *", "int", "uint64_t");
12536160958SMark Johnston SDT_PROBE_DEFINE3(racct, , rusage, sub__cred,
12636160958SMark Johnston     "struct ucred *", "int", "uint64_t");
12736160958SMark Johnston SDT_PROBE_DEFINE1(racct, , racct, create,
128097055e2SEdward Tomasz Napierala     "struct racct *");
12936160958SMark Johnston SDT_PROBE_DEFINE1(racct, , racct, destroy,
13036160958SMark Johnston     "struct racct *");
13136160958SMark Johnston SDT_PROBE_DEFINE2(racct, , racct, join,
132097055e2SEdward Tomasz Napierala     "struct racct *", "struct racct *");
13336160958SMark Johnston SDT_PROBE_DEFINE2(racct, , racct, join__failure,
13436160958SMark Johnston     "struct racct *", "struct racct *");
13536160958SMark Johnston SDT_PROBE_DEFINE2(racct, , racct, leave,
13636160958SMark Johnston     "struct racct *", "struct racct *");
137097055e2SEdward Tomasz Napierala 
138097055e2SEdward Tomasz Napierala int racct_types[] = {
139097055e2SEdward Tomasz Napierala 	[RACCT_CPU] =
14085a2f1b4SEdward Tomasz Napierala 		RACCT_IN_MILLIONS,
141097055e2SEdward Tomasz Napierala 	[RACCT_DATA] =
142097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
143097055e2SEdward Tomasz Napierala 	[RACCT_STACK] =
144097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
145097055e2SEdward Tomasz Napierala 	[RACCT_CORE] =
146097055e2SEdward Tomasz Napierala 		RACCT_DENIABLE,
147097055e2SEdward Tomasz Napierala 	[RACCT_RSS] =
148097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE,
149097055e2SEdward Tomasz Napierala 	[RACCT_MEMLOCK] =
150097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_DENIABLE,
151097055e2SEdward Tomasz Napierala 	[RACCT_NPROC] =
152097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_DENIABLE,
153097055e2SEdward Tomasz Napierala 	[RACCT_NOFILE] =
154097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
155097055e2SEdward Tomasz Napierala 	[RACCT_VMEM] =
156097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
157097055e2SEdward Tomasz Napierala 	[RACCT_NPTS] =
158097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
159097055e2SEdward Tomasz Napierala 	[RACCT_SWAP] =
160097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
161097055e2SEdward Tomasz Napierala 	[RACCT_NTHR] =
162097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_DENIABLE,
163097055e2SEdward Tomasz Napierala 	[RACCT_MSGQQUEUED] =
164097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
165097055e2SEdward Tomasz Napierala 	[RACCT_MSGQSIZE] =
166097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
167097055e2SEdward Tomasz Napierala 	[RACCT_NMSGQ] =
168097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
169097055e2SEdward Tomasz Napierala 	[RACCT_NSEM] =
170097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
171097055e2SEdward Tomasz Napierala 	[RACCT_NSEMOP] =
172097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
173097055e2SEdward Tomasz Napierala 	[RACCT_NSHM] =
174097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
175097055e2SEdward Tomasz Napierala 	[RACCT_SHMSIZE] =
176097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
177097055e2SEdward Tomasz Napierala 	[RACCT_WALLCLOCK] =
17836af9869SEdward Tomasz Napierala 		RACCT_IN_MILLIONS,
17936af9869SEdward Tomasz Napierala 	[RACCT_PCTCPU] =
18036af9869SEdward Tomasz Napierala 		RACCT_DECAYING | RACCT_DENIABLE | RACCT_IN_MILLIONS };
18136af9869SEdward Tomasz Napierala 
18236af9869SEdward Tomasz Napierala static const fixpt_t RACCT_DECAY_FACTOR = 0.3 * FSCALE;
18336af9869SEdward Tomasz Napierala 
18436af9869SEdward Tomasz Napierala #ifdef SCHED_4BSD
18536af9869SEdward Tomasz Napierala /*
18636af9869SEdward Tomasz Napierala  * Contains intermediate values for %cpu calculations to avoid using floating
18736af9869SEdward Tomasz Napierala  * point in the kernel.
18836af9869SEdward Tomasz Napierala  * ccpu_exp[k] = FSCALE * (ccpu/FSCALE)^k = FSCALE * exp(-k/20)
18936af9869SEdward Tomasz Napierala  * It is needed only for the 4BSD scheduler, because in ULE, the ccpu equals to
19036af9869SEdward Tomasz Napierala  * zero so the calculations are more straightforward.
19136af9869SEdward Tomasz Napierala  */
19236af9869SEdward Tomasz Napierala fixpt_t ccpu_exp[] = {
19336af9869SEdward Tomasz Napierala 	[0] = FSCALE * 1,
19436af9869SEdward Tomasz Napierala 	[1] = FSCALE * 0.95122942450071400909,
19536af9869SEdward Tomasz Napierala 	[2] = FSCALE * 0.90483741803595957316,
19636af9869SEdward Tomasz Napierala 	[3] = FSCALE * 0.86070797642505780722,
19736af9869SEdward Tomasz Napierala 	[4] = FSCALE * 0.81873075307798185866,
19836af9869SEdward Tomasz Napierala 	[5] = FSCALE * 0.77880078307140486824,
19936af9869SEdward Tomasz Napierala 	[6] = FSCALE * 0.74081822068171786606,
20036af9869SEdward Tomasz Napierala 	[7] = FSCALE * 0.70468808971871343435,
20136af9869SEdward Tomasz Napierala 	[8] = FSCALE * 0.67032004603563930074,
20236af9869SEdward Tomasz Napierala 	[9] = FSCALE * 0.63762815162177329314,
20336af9869SEdward Tomasz Napierala 	[10] = FSCALE * 0.60653065971263342360,
20436af9869SEdward Tomasz Napierala 	[11] = FSCALE * 0.57694981038048669531,
20536af9869SEdward Tomasz Napierala 	[12] = FSCALE * 0.54881163609402643262,
20636af9869SEdward Tomasz Napierala 	[13] = FSCALE * 0.52204577676101604789,
20736af9869SEdward Tomasz Napierala 	[14] = FSCALE * 0.49658530379140951470,
20836af9869SEdward Tomasz Napierala 	[15] = FSCALE * 0.47236655274101470713,
20936af9869SEdward Tomasz Napierala 	[16] = FSCALE * 0.44932896411722159143,
21036af9869SEdward Tomasz Napierala 	[17] = FSCALE * 0.42741493194872666992,
21136af9869SEdward Tomasz Napierala 	[18] = FSCALE * 0.40656965974059911188,
21236af9869SEdward Tomasz Napierala 	[19] = FSCALE * 0.38674102345450120691,
21336af9869SEdward Tomasz Napierala 	[20] = FSCALE * 0.36787944117144232159,
21436af9869SEdward Tomasz Napierala 	[21] = FSCALE * 0.34993774911115535467,
21536af9869SEdward Tomasz Napierala 	[22] = FSCALE * 0.33287108369807955328,
21636af9869SEdward Tomasz Napierala 	[23] = FSCALE * 0.31663676937905321821,
21736af9869SEdward Tomasz Napierala 	[24] = FSCALE * 0.30119421191220209664,
21836af9869SEdward Tomasz Napierala 	[25] = FSCALE * 0.28650479686019010032,
21936af9869SEdward Tomasz Napierala 	[26] = FSCALE * 0.27253179303401260312,
22036af9869SEdward Tomasz Napierala 	[27] = FSCALE * 0.25924026064589150757,
22136af9869SEdward Tomasz Napierala 	[28] = FSCALE * 0.24659696394160647693,
22236af9869SEdward Tomasz Napierala 	[29] = FSCALE * 0.23457028809379765313,
22336af9869SEdward Tomasz Napierala 	[30] = FSCALE * 0.22313016014842982893,
22436af9869SEdward Tomasz Napierala 	[31] = FSCALE * 0.21224797382674305771,
22536af9869SEdward Tomasz Napierala 	[32] = FSCALE * 0.20189651799465540848,
22636af9869SEdward Tomasz Napierala 	[33] = FSCALE * 0.19204990862075411423,
22736af9869SEdward Tomasz Napierala 	[34] = FSCALE * 0.18268352405273465022,
22836af9869SEdward Tomasz Napierala 	[35] = FSCALE * 0.17377394345044512668,
22936af9869SEdward Tomasz Napierala 	[36] = FSCALE * 0.16529888822158653829,
23036af9869SEdward Tomasz Napierala 	[37] = FSCALE * 0.15723716631362761621,
23136af9869SEdward Tomasz Napierala 	[38] = FSCALE * 0.14956861922263505264,
23236af9869SEdward Tomasz Napierala 	[39] = FSCALE * 0.14227407158651357185,
23336af9869SEdward Tomasz Napierala 	[40] = FSCALE * 0.13533528323661269189,
23436af9869SEdward Tomasz Napierala 	[41] = FSCALE * 0.12873490358780421886,
23536af9869SEdward Tomasz Napierala 	[42] = FSCALE * 0.12245642825298191021,
23636af9869SEdward Tomasz Napierala 	[43] = FSCALE * 0.11648415777349695786,
23736af9869SEdward Tomasz Napierala 	[44] = FSCALE * 0.11080315836233388333,
23836af9869SEdward Tomasz Napierala 	[45] = FSCALE * 0.10539922456186433678,
23936af9869SEdward Tomasz Napierala 	[46] = FSCALE * 0.10025884372280373372,
24036af9869SEdward Tomasz Napierala 	[47] = FSCALE * 0.09536916221554961888,
24136af9869SEdward Tomasz Napierala 	[48] = FSCALE * 0.09071795328941250337,
24236af9869SEdward Tomasz Napierala 	[49] = FSCALE * 0.08629358649937051097,
24336af9869SEdward Tomasz Napierala 	[50] = FSCALE * 0.08208499862389879516,
24436af9869SEdward Tomasz Napierala 	[51] = FSCALE * 0.07808166600115315231,
24536af9869SEdward Tomasz Napierala 	[52] = FSCALE * 0.07427357821433388042,
24636af9869SEdward Tomasz Napierala 	[53] = FSCALE * 0.07065121306042958674,
24736af9869SEdward Tomasz Napierala 	[54] = FSCALE * 0.06720551273974976512,
24836af9869SEdward Tomasz Napierala 	[55] = FSCALE * 0.06392786120670757270,
24936af9869SEdward Tomasz Napierala 	[56] = FSCALE * 0.06081006262521796499,
25036af9869SEdward Tomasz Napierala 	[57] = FSCALE * 0.05784432087483846296,
25136af9869SEdward Tomasz Napierala 	[58] = FSCALE * 0.05502322005640722902,
25236af9869SEdward Tomasz Napierala 	[59] = FSCALE * 0.05233970594843239308,
25336af9869SEdward Tomasz Napierala 	[60] = FSCALE * 0.04978706836786394297,
25436af9869SEdward Tomasz Napierala 	[61] = FSCALE * 0.04735892439114092119,
25536af9869SEdward Tomasz Napierala 	[62] = FSCALE * 0.04504920239355780606,
25636af9869SEdward Tomasz Napierala 	[63] = FSCALE * 0.04285212686704017991,
25736af9869SEdward Tomasz Napierala 	[64] = FSCALE * 0.04076220397836621516,
25836af9869SEdward Tomasz Napierala 	[65] = FSCALE * 0.03877420783172200988,
25936af9869SEdward Tomasz Napierala 	[66] = FSCALE * 0.03688316740124000544,
26036af9869SEdward Tomasz Napierala 	[67] = FSCALE * 0.03508435410084502588,
26136af9869SEdward Tomasz Napierala 	[68] = FSCALE * 0.03337326996032607948,
26236af9869SEdward Tomasz Napierala 	[69] = FSCALE * 0.03174563637806794323,
26336af9869SEdward Tomasz Napierala 	[70] = FSCALE * 0.03019738342231850073,
26436af9869SEdward Tomasz Napierala 	[71] = FSCALE * 0.02872463965423942912,
26536af9869SEdward Tomasz Napierala 	[72] = FSCALE * 0.02732372244729256080,
26636af9869SEdward Tomasz Napierala 	[73] = FSCALE * 0.02599112877875534358,
26736af9869SEdward Tomasz Napierala 	[74] = FSCALE * 0.02472352647033939120,
26836af9869SEdward Tomasz Napierala 	[75] = FSCALE * 0.02351774585600910823,
26936af9869SEdward Tomasz Napierala 	[76] = FSCALE * 0.02237077185616559577,
27036af9869SEdward Tomasz Napierala 	[77] = FSCALE * 0.02127973643837716938,
27136af9869SEdward Tomasz Napierala 	[78] = FSCALE * 0.02024191144580438847,
27236af9869SEdward Tomasz Napierala 	[79] = FSCALE * 0.01925470177538692429,
27336af9869SEdward Tomasz Napierala 	[80] = FSCALE * 0.01831563888873418029,
27436af9869SEdward Tomasz Napierala 	[81] = FSCALE * 0.01742237463949351138,
27536af9869SEdward Tomasz Napierala 	[82] = FSCALE * 0.01657267540176124754,
27636af9869SEdward Tomasz Napierala 	[83] = FSCALE * 0.01576441648485449082,
27736af9869SEdward Tomasz Napierala 	[84] = FSCALE * 0.01499557682047770621,
27836af9869SEdward Tomasz Napierala 	[85] = FSCALE * 0.01426423390899925527,
27936af9869SEdward Tomasz Napierala 	[86] = FSCALE * 0.01356855901220093175,
28036af9869SEdward Tomasz Napierala 	[87] = FSCALE * 0.01290681258047986886,
28136af9869SEdward Tomasz Napierala 	[88] = FSCALE * 0.01227733990306844117,
28236af9869SEdward Tomasz Napierala 	[89] = FSCALE * 0.01167856697039544521,
28336af9869SEdward Tomasz Napierala 	[90] = FSCALE * 0.01110899653824230649,
28436af9869SEdward Tomasz Napierala 	[91] = FSCALE * 0.01056720438385265337,
28536af9869SEdward Tomasz Napierala 	[92] = FSCALE * 0.01005183574463358164,
28636af9869SEdward Tomasz Napierala 	[93] = FSCALE * 0.00956160193054350793,
28736af9869SEdward Tomasz Napierala 	[94] = FSCALE * 0.00909527710169581709,
28836af9869SEdward Tomasz Napierala 	[95] = FSCALE * 0.00865169520312063417,
28936af9869SEdward Tomasz Napierala 	[96] = FSCALE * 0.00822974704902002884,
29036af9869SEdward Tomasz Napierala 	[97] = FSCALE * 0.00782837754922577143,
29136af9869SEdward Tomasz Napierala 	[98] = FSCALE * 0.00744658307092434051,
29236af9869SEdward Tomasz Napierala 	[99] = FSCALE * 0.00708340892905212004,
29336af9869SEdward Tomasz Napierala 	[100] = FSCALE * 0.00673794699908546709,
29436af9869SEdward Tomasz Napierala 	[101] = FSCALE * 0.00640933344625638184,
29536af9869SEdward Tomasz Napierala 	[102] = FSCALE * 0.00609674656551563610,
29636af9869SEdward Tomasz Napierala 	[103] = FSCALE * 0.00579940472684214321,
29736af9869SEdward Tomasz Napierala 	[104] = FSCALE * 0.00551656442076077241,
29836af9869SEdward Tomasz Napierala 	[105] = FSCALE * 0.00524751839918138427,
29936af9869SEdward Tomasz Napierala 	[106] = FSCALE * 0.00499159390691021621,
30036af9869SEdward Tomasz Napierala 	[107] = FSCALE * 0.00474815099941147558,
30136af9869SEdward Tomasz Napierala 	[108] = FSCALE * 0.00451658094261266798,
30236af9869SEdward Tomasz Napierala 	[109] = FSCALE * 0.00429630469075234057,
30336af9869SEdward Tomasz Napierala 	[110] = FSCALE * 0.00408677143846406699,
30436af9869SEdward Tomasz Napierala };
30536af9869SEdward Tomasz Napierala #endif
30636af9869SEdward Tomasz Napierala 
30736af9869SEdward Tomasz Napierala #define	CCPU_EXP_MAX	110
30836af9869SEdward Tomasz Napierala 
30936af9869SEdward Tomasz Napierala /*
31036af9869SEdward Tomasz Napierala  * This function is analogical to the getpcpu() function in the ps(1) command.
31136af9869SEdward Tomasz Napierala  * They should both calculate in the same way so that the racct %cpu
31236af9869SEdward Tomasz Napierala  * calculations are consistent with the values showed by the ps(1) tool.
31336af9869SEdward Tomasz Napierala  * The calculations are more complex in the 4BSD scheduler because of the value
31436af9869SEdward Tomasz Napierala  * of the ccpu variable.  In ULE it is defined to be zero which saves us some
31536af9869SEdward Tomasz Napierala  * work.
31636af9869SEdward Tomasz Napierala  */
31736af9869SEdward Tomasz Napierala static uint64_t
31836af9869SEdward Tomasz Napierala racct_getpcpu(struct proc *p, u_int pcpu)
31936af9869SEdward Tomasz Napierala {
32036af9869SEdward Tomasz Napierala 	u_int swtime;
32136af9869SEdward Tomasz Napierala #ifdef SCHED_4BSD
32236af9869SEdward Tomasz Napierala 	fixpt_t pctcpu, pctcpu_next;
32336af9869SEdward Tomasz Napierala #endif
32436af9869SEdward Tomasz Napierala #ifdef SMP
32536af9869SEdward Tomasz Napierala 	struct pcpu *pc;
32636af9869SEdward Tomasz Napierala 	int found;
32736af9869SEdward Tomasz Napierala #endif
32836af9869SEdward Tomasz Napierala 	fixpt_t p_pctcpu;
32936af9869SEdward Tomasz Napierala 	struct thread *td;
33036af9869SEdward Tomasz Napierala 
3314b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
3324b5c9cf6SEdward Tomasz Napierala 
33336af9869SEdward Tomasz Napierala 	/*
33436af9869SEdward Tomasz Napierala 	 * If the process is swapped out, we count its %cpu usage as zero.
33536af9869SEdward Tomasz Napierala 	 * This behaviour is consistent with the userland ps(1) tool.
33636af9869SEdward Tomasz Napierala 	 */
33736af9869SEdward Tomasz Napierala 	if ((p->p_flag & P_INMEM) == 0)
33836af9869SEdward Tomasz Napierala 		return (0);
33936af9869SEdward Tomasz Napierala 	swtime = (ticks - p->p_swtick) / hz;
34036af9869SEdward Tomasz Napierala 
34136af9869SEdward Tomasz Napierala 	/*
34236af9869SEdward Tomasz Napierala 	 * For short-lived processes, the sched_pctcpu() returns small
34336af9869SEdward Tomasz Napierala 	 * values even for cpu intensive processes.  Therefore we use
34436af9869SEdward Tomasz Napierala 	 * our own estimate in this case.
34536af9869SEdward Tomasz Napierala 	 */
34636af9869SEdward Tomasz Napierala 	if (swtime < RACCT_PCPU_SECS)
34736af9869SEdward Tomasz Napierala 		return (pcpu);
34836af9869SEdward Tomasz Napierala 
34936af9869SEdward Tomasz Napierala 	p_pctcpu = 0;
35036af9869SEdward Tomasz Napierala 	FOREACH_THREAD_IN_PROC(p, td) {
35136af9869SEdward Tomasz Napierala 		if (td == PCPU_GET(idlethread))
35236af9869SEdward Tomasz Napierala 			continue;
35336af9869SEdward Tomasz Napierala #ifdef SMP
35436af9869SEdward Tomasz Napierala 		found = 0;
35536af9869SEdward Tomasz Napierala 		STAILQ_FOREACH(pc, &cpuhead, pc_allcpu) {
35636af9869SEdward Tomasz Napierala 			if (td == pc->pc_idlethread) {
35736af9869SEdward Tomasz Napierala 				found = 1;
35836af9869SEdward Tomasz Napierala 				break;
35936af9869SEdward Tomasz Napierala 			}
36036af9869SEdward Tomasz Napierala 		}
36136af9869SEdward Tomasz Napierala 		if (found)
36236af9869SEdward Tomasz Napierala 			continue;
36336af9869SEdward Tomasz Napierala #endif
36436af9869SEdward Tomasz Napierala 		thread_lock(td);
36536af9869SEdward Tomasz Napierala #ifdef SCHED_4BSD
36636af9869SEdward Tomasz Napierala 		pctcpu = sched_pctcpu(td);
36736af9869SEdward Tomasz Napierala 		/* Count also the yet unfinished second. */
36836af9869SEdward Tomasz Napierala 		pctcpu_next = (pctcpu * ccpu_exp[1]) >> FSHIFT;
36936af9869SEdward Tomasz Napierala 		pctcpu_next += sched_pctcpu_delta(td);
37036af9869SEdward Tomasz Napierala 		p_pctcpu += max(pctcpu, pctcpu_next);
37136af9869SEdward Tomasz Napierala #else
37236af9869SEdward Tomasz Napierala 		/*
37336af9869SEdward Tomasz Napierala 		 * In ULE the %cpu statistics are updated on every
37436af9869SEdward Tomasz Napierala 		 * sched_pctcpu() call.  So special calculations to
37536af9869SEdward Tomasz Napierala 		 * account for the latest (unfinished) second are
37636af9869SEdward Tomasz Napierala 		 * not needed.
37736af9869SEdward Tomasz Napierala 		 */
37836af9869SEdward Tomasz Napierala 		p_pctcpu += sched_pctcpu(td);
37936af9869SEdward Tomasz Napierala #endif
38036af9869SEdward Tomasz Napierala 		thread_unlock(td);
38136af9869SEdward Tomasz Napierala 	}
38236af9869SEdward Tomasz Napierala 
38336af9869SEdward Tomasz Napierala #ifdef SCHED_4BSD
38436af9869SEdward Tomasz Napierala 	if (swtime <= CCPU_EXP_MAX)
38536af9869SEdward Tomasz Napierala 		return ((100 * (uint64_t)p_pctcpu * 1000000) /
38636af9869SEdward Tomasz Napierala 		    (FSCALE - ccpu_exp[swtime]));
38736af9869SEdward Tomasz Napierala #endif
38836af9869SEdward Tomasz Napierala 
38936af9869SEdward Tomasz Napierala 	return ((100 * (uint64_t)p_pctcpu * 1000000) / FSCALE);
39036af9869SEdward Tomasz Napierala }
391097055e2SEdward Tomasz Napierala 
392097055e2SEdward Tomasz Napierala static void
393097055e2SEdward Tomasz Napierala racct_add_racct(struct racct *dest, const struct racct *src)
394097055e2SEdward Tomasz Napierala {
395097055e2SEdward Tomasz Napierala 	int i;
396097055e2SEdward Tomasz Napierala 
3974b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
398*4c230cdaSEdward Tomasz Napierala 	RACCT_LOCK_ASSERT();
399097055e2SEdward Tomasz Napierala 
400097055e2SEdward Tomasz Napierala 	/*
401097055e2SEdward Tomasz Napierala 	 * Update resource usage in dest.
402097055e2SEdward Tomasz Napierala 	 */
403097055e2SEdward Tomasz Napierala 	for (i = 0; i <= RACCT_MAX; i++) {
404097055e2SEdward Tomasz Napierala 		KASSERT(dest->r_resources[i] >= 0,
405baf85d0aSEdward Tomasz Napierala 		    ("%s: resource %d propagation meltdown: dest < 0",
406baf85d0aSEdward Tomasz Napierala 		    __func__, i));
407097055e2SEdward Tomasz Napierala 		KASSERT(src->r_resources[i] >= 0,
408baf85d0aSEdward Tomasz Napierala 		    ("%s: resource %d propagation meltdown: src < 0",
409baf85d0aSEdward Tomasz Napierala 		    __func__, i));
410097055e2SEdward Tomasz Napierala 		dest->r_resources[i] += src->r_resources[i];
411097055e2SEdward Tomasz Napierala 	}
412097055e2SEdward Tomasz Napierala }
413097055e2SEdward Tomasz Napierala 
414097055e2SEdward Tomasz Napierala static void
415097055e2SEdward Tomasz Napierala racct_sub_racct(struct racct *dest, const struct racct *src)
416097055e2SEdward Tomasz Napierala {
417097055e2SEdward Tomasz Napierala 	int i;
418097055e2SEdward Tomasz Napierala 
4194b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
420*4c230cdaSEdward Tomasz Napierala 	RACCT_LOCK_ASSERT();
421097055e2SEdward Tomasz Napierala 
422097055e2SEdward Tomasz Napierala 	/*
423097055e2SEdward Tomasz Napierala 	 * Update resource usage in dest.
424097055e2SEdward Tomasz Napierala 	 */
425097055e2SEdward Tomasz Napierala 	for (i = 0; i <= RACCT_MAX; i++) {
42684c9193bSEdward Tomasz Napierala 		if (!RACCT_IS_SLOPPY(i) && !RACCT_IS_DECAYING(i)) {
427097055e2SEdward Tomasz Napierala 			KASSERT(dest->r_resources[i] >= 0,
428baf85d0aSEdward Tomasz Napierala 			    ("%s: resource %d propagation meltdown: dest < 0",
429baf85d0aSEdward Tomasz Napierala 			    __func__, i));
430097055e2SEdward Tomasz Napierala 			KASSERT(src->r_resources[i] >= 0,
431baf85d0aSEdward Tomasz Napierala 			    ("%s: resource %d propagation meltdown: src < 0",
432baf85d0aSEdward Tomasz Napierala 			    __func__, i));
433097055e2SEdward Tomasz Napierala 			KASSERT(src->r_resources[i] <= dest->r_resources[i],
434baf85d0aSEdward Tomasz Napierala 			    ("%s: resource %d propagation meltdown: src > dest",
435baf85d0aSEdward Tomasz Napierala 			    __func__, i));
436097055e2SEdward Tomasz Napierala 		}
43736af9869SEdward Tomasz Napierala 		if (RACCT_CAN_DROP(i)) {
438097055e2SEdward Tomasz Napierala 			dest->r_resources[i] -= src->r_resources[i];
439097055e2SEdward Tomasz Napierala 			if (dest->r_resources[i] < 0) {
44084c9193bSEdward Tomasz Napierala 				KASSERT(RACCT_IS_SLOPPY(i) ||
44184c9193bSEdward Tomasz Napierala 				    RACCT_IS_DECAYING(i),
442baf85d0aSEdward Tomasz Napierala 				    ("%s: resource %d usage < 0", __func__, i));
443097055e2SEdward Tomasz Napierala 				dest->r_resources[i] = 0;
444097055e2SEdward Tomasz Napierala 			}
445097055e2SEdward Tomasz Napierala 		}
446097055e2SEdward Tomasz Napierala 	}
447097055e2SEdward Tomasz Napierala }
448097055e2SEdward Tomasz Napierala 
449097055e2SEdward Tomasz Napierala void
450097055e2SEdward Tomasz Napierala racct_create(struct racct **racctp)
451097055e2SEdward Tomasz Napierala {
452097055e2SEdward Tomasz Napierala 
4534b5c9cf6SEdward Tomasz Napierala 	if (!racct_enable)
4544b5c9cf6SEdward Tomasz Napierala 		return;
4554b5c9cf6SEdward Tomasz Napierala 
45636160958SMark Johnston 	SDT_PROBE1(racct, , racct, create, racctp);
457097055e2SEdward Tomasz Napierala 
458097055e2SEdward Tomasz Napierala 	KASSERT(*racctp == NULL, ("racct already allocated"));
459097055e2SEdward Tomasz Napierala 
460097055e2SEdward Tomasz Napierala 	*racctp = uma_zalloc(racct_zone, M_WAITOK | M_ZERO);
461097055e2SEdward Tomasz Napierala }
462097055e2SEdward Tomasz Napierala 
463097055e2SEdward Tomasz Napierala static void
464097055e2SEdward Tomasz Napierala racct_destroy_locked(struct racct **racctp)
465097055e2SEdward Tomasz Napierala {
466097055e2SEdward Tomasz Napierala 	int i;
467097055e2SEdward Tomasz Napierala 	struct racct *racct;
468097055e2SEdward Tomasz Napierala 
4694b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
4704b5c9cf6SEdward Tomasz Napierala 
47136160958SMark Johnston 	SDT_PROBE1(racct, , racct, destroy, racctp);
472097055e2SEdward Tomasz Napierala 
473*4c230cdaSEdward Tomasz Napierala 	RACCT_LOCK_ASSERT();
474097055e2SEdward Tomasz Napierala 	KASSERT(racctp != NULL, ("NULL racctp"));
475097055e2SEdward Tomasz Napierala 	KASSERT(*racctp != NULL, ("NULL racct"));
476097055e2SEdward Tomasz Napierala 
477097055e2SEdward Tomasz Napierala 	racct = *racctp;
478097055e2SEdward Tomasz Napierala 
479097055e2SEdward Tomasz Napierala 	for (i = 0; i <= RACCT_MAX; i++) {
4804fe84775SEdward Tomasz Napierala 		if (RACCT_IS_SLOPPY(i))
481097055e2SEdward Tomasz Napierala 			continue;
4824fe84775SEdward Tomasz Napierala 		if (!RACCT_IS_RECLAIMABLE(i))
483097055e2SEdward Tomasz Napierala 			continue;
484097055e2SEdward Tomasz Napierala 		KASSERT(racct->r_resources[i] == 0,
485097055e2SEdward Tomasz Napierala 		    ("destroying non-empty racct: "
486097055e2SEdward Tomasz Napierala 		    "%ju allocated for resource %d\n",
487097055e2SEdward Tomasz Napierala 		    racct->r_resources[i], i));
488097055e2SEdward Tomasz Napierala 	}
489097055e2SEdward Tomasz Napierala 	uma_zfree(racct_zone, racct);
490097055e2SEdward Tomasz Napierala 	*racctp = NULL;
491097055e2SEdward Tomasz Napierala }
492097055e2SEdward Tomasz Napierala 
493097055e2SEdward Tomasz Napierala void
494097055e2SEdward Tomasz Napierala racct_destroy(struct racct **racct)
495097055e2SEdward Tomasz Napierala {
496097055e2SEdward Tomasz Napierala 
4974b5c9cf6SEdward Tomasz Napierala 	if (!racct_enable)
4984b5c9cf6SEdward Tomasz Napierala 		return;
4994b5c9cf6SEdward Tomasz Napierala 
500*4c230cdaSEdward Tomasz Napierala 	RACCT_LOCK();
501097055e2SEdward Tomasz Napierala 	racct_destroy_locked(racct);
502*4c230cdaSEdward Tomasz Napierala 	RACCT_UNLOCK();
503097055e2SEdward Tomasz Napierala }
504097055e2SEdward Tomasz Napierala 
505097055e2SEdward Tomasz Napierala /*
506af1a7b25SEdward Tomasz Napierala  * Increase consumption of 'resource' by 'amount' for 'racct',
507af1a7b25SEdward Tomasz Napierala  * but not its parents.  Differently from other cases, 'amount' here
508097055e2SEdward Tomasz Napierala  * may be less than zero.
509097055e2SEdward Tomasz Napierala  */
510097055e2SEdward Tomasz Napierala static void
5113768a5dfSJeremie Le Hen racct_adjust_resource(struct racct *racct, int resource,
512a8e0740eSEdward Tomasz Napierala     int64_t amount)
513097055e2SEdward Tomasz Napierala {
514097055e2SEdward Tomasz Napierala 
5154b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
516*4c230cdaSEdward Tomasz Napierala 	RACCT_LOCK_ASSERT();
517097055e2SEdward Tomasz Napierala 	KASSERT(racct != NULL, ("NULL racct"));
518097055e2SEdward Tomasz Napierala 
519097055e2SEdward Tomasz Napierala 	racct->r_resources[resource] += amount;
520097055e2SEdward Tomasz Napierala 	if (racct->r_resources[resource] < 0) {
52136af9869SEdward Tomasz Napierala 		KASSERT(RACCT_IS_SLOPPY(resource) || RACCT_IS_DECAYING(resource),
522baf85d0aSEdward Tomasz Napierala 		    ("%s: resource %d usage < 0", __func__, resource));
523097055e2SEdward Tomasz Napierala 		racct->r_resources[resource] = 0;
524097055e2SEdward Tomasz Napierala 	}
52536af9869SEdward Tomasz Napierala 
52636af9869SEdward Tomasz Napierala 	/*
52736af9869SEdward Tomasz Napierala 	 * There are some cases where the racct %cpu resource would grow
52809766dd5SJosh Paetzel 	 * beyond 100% per core.  For example in racct_proc_exit() we add
52909766dd5SJosh Paetzel 	 * the process %cpu usage to the ucred racct containers.  If too
53009766dd5SJosh Paetzel 	 * many processes terminated in a short time span, the ucred %cpu
53109766dd5SJosh Paetzel 	 * resource could grow too much.  Also, the 4BSD scheduler sometimes
53209766dd5SJosh Paetzel 	 * returns for a thread more than 100% cpu usage. So we set a sane
53309766dd5SJosh Paetzel 	 * boundary here to 100% * the maxumum number of CPUs.
53436af9869SEdward Tomasz Napierala 	 */
53536af9869SEdward Tomasz Napierala 	if ((resource == RACCT_PCTCPU) &&
53609766dd5SJosh Paetzel 	    (racct->r_resources[RACCT_PCTCPU] > 100 * 1000000 * (int64_t)MAXCPU))
53709766dd5SJosh Paetzel 		racct->r_resources[RACCT_PCTCPU] = 100 * 1000000 * (int64_t)MAXCPU;
538097055e2SEdward Tomasz Napierala }
539097055e2SEdward Tomasz Napierala 
5402d8696d1SEdward Tomasz Napierala static int
5417cea96f6SEdward Tomasz Napierala racct_add_locked(struct proc *p, int resource, uint64_t amount, int force)
542097055e2SEdward Tomasz Napierala {
543097055e2SEdward Tomasz Napierala #ifdef RCTL
544097055e2SEdward Tomasz Napierala 	int error;
545097055e2SEdward Tomasz Napierala #endif
546097055e2SEdward Tomasz Napierala 
5474b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
5484b5c9cf6SEdward Tomasz Napierala 
549097055e2SEdward Tomasz Napierala 	/*
550097055e2SEdward Tomasz Napierala 	 * We need proc lock to dereference p->p_ucred.
551097055e2SEdward Tomasz Napierala 	 */
552097055e2SEdward Tomasz Napierala 	PROC_LOCK_ASSERT(p, MA_OWNED);
553097055e2SEdward Tomasz Napierala 
554097055e2SEdward Tomasz Napierala #ifdef RCTL
555097055e2SEdward Tomasz Napierala 	error = rctl_enforce(p, resource, amount);
556659e7466SEdward Tomasz Napierala 	if (error && !force && RACCT_IS_DENIABLE(resource)) {
55736160958SMark Johnston 		SDT_PROBE3(racct, , rusage, add__failure, p, resource, amount);
558097055e2SEdward Tomasz Napierala 		return (error);
559097055e2SEdward Tomasz Napierala 	}
560097055e2SEdward Tomasz Napierala #endif
5613768a5dfSJeremie Le Hen 	racct_adjust_resource(p->p_racct, resource, amount);
562097055e2SEdward Tomasz Napierala 	racct_add_cred_locked(p->p_ucred, resource, amount);
563097055e2SEdward Tomasz Napierala 
564097055e2SEdward Tomasz Napierala 	return (0);
565097055e2SEdward Tomasz Napierala }
566097055e2SEdward Tomasz Napierala 
5672d8696d1SEdward Tomasz Napierala /*
5682d8696d1SEdward Tomasz Napierala  * Increase allocation of 'resource' by 'amount' for process 'p'.
5692d8696d1SEdward Tomasz Napierala  * Return 0 if it's below limits, or errno, if it's not.
5702d8696d1SEdward Tomasz Napierala  */
5712d8696d1SEdward Tomasz Napierala int
5722d8696d1SEdward Tomasz Napierala racct_add(struct proc *p, int resource, uint64_t amount)
5732d8696d1SEdward Tomasz Napierala {
5742d8696d1SEdward Tomasz Napierala 	int error;
5752d8696d1SEdward Tomasz Napierala 
5764b5c9cf6SEdward Tomasz Napierala 	if (!racct_enable)
5774b5c9cf6SEdward Tomasz Napierala 		return (0);
5784b5c9cf6SEdward Tomasz Napierala 
5797cea96f6SEdward Tomasz Napierala 	SDT_PROBE3(racct, , rusage, add, p, resource, amount);
5807cea96f6SEdward Tomasz Napierala 
581*4c230cdaSEdward Tomasz Napierala 	RACCT_LOCK();
5827cea96f6SEdward Tomasz Napierala 	error = racct_add_locked(p, resource, amount, 0);
583*4c230cdaSEdward Tomasz Napierala 	RACCT_UNLOCK();
5842d8696d1SEdward Tomasz Napierala 	return (error);
5852d8696d1SEdward Tomasz Napierala }
5862d8696d1SEdward Tomasz Napierala 
5870b9f1ecbSEdward Tomasz Napierala /*
5880b9f1ecbSEdward Tomasz Napierala  * Increase allocation of 'resource' by 'amount' for process 'p'.
5890b9f1ecbSEdward Tomasz Napierala  * Doesn't check for limits and never fails.
5900b9f1ecbSEdward Tomasz Napierala  */
5910b9f1ecbSEdward Tomasz Napierala void
5920b9f1ecbSEdward Tomasz Napierala racct_add_force(struct proc *p, int resource, uint64_t amount)
5930b9f1ecbSEdward Tomasz Napierala {
5940b9f1ecbSEdward Tomasz Napierala 
5950b9f1ecbSEdward Tomasz Napierala 	if (!racct_enable)
5960b9f1ecbSEdward Tomasz Napierala 		return;
5970b9f1ecbSEdward Tomasz Napierala 
5980b9f1ecbSEdward Tomasz Napierala 	SDT_PROBE3(racct, , rusage, add__force, p, resource, amount);
5990b9f1ecbSEdward Tomasz Napierala 
600*4c230cdaSEdward Tomasz Napierala 	RACCT_LOCK();
6010b9f1ecbSEdward Tomasz Napierala 	racct_add_locked(p, resource, amount, 1);
602*4c230cdaSEdward Tomasz Napierala 	RACCT_UNLOCK();
6030b9f1ecbSEdward Tomasz Napierala }
6040b9f1ecbSEdward Tomasz Napierala 
605097055e2SEdward Tomasz Napierala static void
606097055e2SEdward Tomasz Napierala racct_add_cred_locked(struct ucred *cred, int resource, uint64_t amount)
607097055e2SEdward Tomasz Napierala {
608097055e2SEdward Tomasz Napierala 	struct prison *pr;
609097055e2SEdward Tomasz Napierala 
6104b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
6114b5c9cf6SEdward Tomasz Napierala 
61236160958SMark Johnston 	SDT_PROBE3(racct, , rusage, add__cred, cred, resource, amount);
613097055e2SEdward Tomasz Napierala 
6143768a5dfSJeremie Le Hen 	racct_adjust_resource(cred->cr_ruidinfo->ui_racct, resource, amount);
615097055e2SEdward Tomasz Napierala 	for (pr = cred->cr_prison; pr != NULL; pr = pr->pr_parent)
6163768a5dfSJeremie Le Hen 		racct_adjust_resource(pr->pr_prison_racct->prr_racct, resource,
617a7ad07bfSEdward Tomasz Napierala 		    amount);
6183768a5dfSJeremie Le Hen 	racct_adjust_resource(cred->cr_loginclass->lc_racct, resource, amount);
619097055e2SEdward Tomasz Napierala }
620097055e2SEdward Tomasz Napierala 
621097055e2SEdward Tomasz Napierala /*
622097055e2SEdward Tomasz Napierala  * Increase allocation of 'resource' by 'amount' for credential 'cred'.
623097055e2SEdward Tomasz Napierala  * Doesn't check for limits and never fails.
624097055e2SEdward Tomasz Napierala  */
625097055e2SEdward Tomasz Napierala void
626097055e2SEdward Tomasz Napierala racct_add_cred(struct ucred *cred, int resource, uint64_t amount)
627097055e2SEdward Tomasz Napierala {
628097055e2SEdward Tomasz Napierala 
6294b5c9cf6SEdward Tomasz Napierala 	if (!racct_enable)
6304b5c9cf6SEdward Tomasz Napierala 		return;
6314b5c9cf6SEdward Tomasz Napierala 
632*4c230cdaSEdward Tomasz Napierala 	RACCT_LOCK();
633097055e2SEdward Tomasz Napierala 	racct_add_cred_locked(cred, resource, amount);
634*4c230cdaSEdward Tomasz Napierala 	RACCT_UNLOCK();
635097055e2SEdward Tomasz Napierala }
636097055e2SEdward Tomasz Napierala 
637097055e2SEdward Tomasz Napierala static int
63810287198SEdward Tomasz Napierala racct_set_locked(struct proc *p, int resource, uint64_t amount, int force)
639097055e2SEdward Tomasz Napierala {
64036af9869SEdward Tomasz Napierala 	int64_t old_amount, decayed_amount;
64136af9869SEdward Tomasz Napierala 	int64_t diff_proc, diff_cred;
642097055e2SEdward Tomasz Napierala #ifdef RCTL
643097055e2SEdward Tomasz Napierala 	int error;
644097055e2SEdward Tomasz Napierala #endif
645097055e2SEdward Tomasz Napierala 
6464b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
6474b5c9cf6SEdward Tomasz Napierala 
648097055e2SEdward Tomasz Napierala 	/*
649097055e2SEdward Tomasz Napierala 	 * We need proc lock to dereference p->p_ucred.
650097055e2SEdward Tomasz Napierala 	 */
651097055e2SEdward Tomasz Napierala 	PROC_LOCK_ASSERT(p, MA_OWNED);
652097055e2SEdward Tomasz Napierala 
65336af9869SEdward Tomasz Napierala 	old_amount = p->p_racct->r_resources[resource];
65436af9869SEdward Tomasz Napierala 	/*
65536af9869SEdward Tomasz Napierala 	 * The diffs may be negative.
65636af9869SEdward Tomasz Napierala 	 */
65736af9869SEdward Tomasz Napierala 	diff_proc = amount - old_amount;
65836af9869SEdward Tomasz Napierala 	if (RACCT_IS_DECAYING(resource)) {
65936af9869SEdward Tomasz Napierala 		/*
66036af9869SEdward Tomasz Napierala 		 * Resources in per-credential racct containers may decay.
66136af9869SEdward Tomasz Napierala 		 * If this is the case, we need to calculate the difference
66236af9869SEdward Tomasz Napierala 		 * between the new amount and the proportional value of the
66336af9869SEdward Tomasz Napierala 		 * old amount that has decayed in the ucred racct containers.
66436af9869SEdward Tomasz Napierala 		 */
66536af9869SEdward Tomasz Napierala 		decayed_amount = old_amount * RACCT_DECAY_FACTOR / FSCALE;
66636af9869SEdward Tomasz Napierala 		diff_cred = amount - decayed_amount;
66736af9869SEdward Tomasz Napierala 	} else
66836af9869SEdward Tomasz Napierala 		diff_cred = diff_proc;
669097055e2SEdward Tomasz Napierala #ifdef notyet
67036af9869SEdward Tomasz Napierala 	KASSERT(diff_proc >= 0 || RACCT_CAN_DROP(resource),
671baf85d0aSEdward Tomasz Napierala 	    ("%s: usage of non-droppable resource %d dropping", __func__,
672097055e2SEdward Tomasz Napierala 	     resource));
673097055e2SEdward Tomasz Napierala #endif
674097055e2SEdward Tomasz Napierala #ifdef RCTL
675659e7466SEdward Tomasz Napierala 	if (diff_proc > 0) {
67636af9869SEdward Tomasz Napierala 		error = rctl_enforce(p, resource, diff_proc);
677659e7466SEdward Tomasz Napierala 		if (error && !force && RACCT_IS_DENIABLE(resource)) {
67836160958SMark Johnston 			SDT_PROBE3(racct, , rusage, set__failure, p, resource,
67936160958SMark Johnston 			    amount);
680097055e2SEdward Tomasz Napierala 			return (error);
681097055e2SEdward Tomasz Napierala 		}
682097055e2SEdward Tomasz Napierala 	}
683097055e2SEdward Tomasz Napierala #endif
6843768a5dfSJeremie Le Hen 	racct_adjust_resource(p->p_racct, resource, diff_proc);
68536af9869SEdward Tomasz Napierala 	if (diff_cred > 0)
68636af9869SEdward Tomasz Napierala 		racct_add_cred_locked(p->p_ucred, resource, diff_cred);
68736af9869SEdward Tomasz Napierala 	else if (diff_cred < 0)
68836af9869SEdward Tomasz Napierala 		racct_sub_cred_locked(p->p_ucred, resource, -diff_cred);
689097055e2SEdward Tomasz Napierala 
690097055e2SEdward Tomasz Napierala 	return (0);
691097055e2SEdward Tomasz Napierala }
692097055e2SEdward Tomasz Napierala 
693097055e2SEdward Tomasz Napierala /*
694097055e2SEdward Tomasz Napierala  * Set allocation of 'resource' to 'amount' for process 'p'.
695097055e2SEdward Tomasz Napierala  * Return 0 if it's below limits, or errno, if it's not.
696097055e2SEdward Tomasz Napierala  *
697097055e2SEdward Tomasz Napierala  * Note that decreasing the allocation always returns 0,
698097055e2SEdward Tomasz Napierala  * even if it's above the limit.
699097055e2SEdward Tomasz Napierala  */
700097055e2SEdward Tomasz Napierala int
701097055e2SEdward Tomasz Napierala racct_set(struct proc *p, int resource, uint64_t amount)
702097055e2SEdward Tomasz Napierala {
703097055e2SEdward Tomasz Napierala 	int error;
704097055e2SEdward Tomasz Napierala 
7054b5c9cf6SEdward Tomasz Napierala 	if (!racct_enable)
7064b5c9cf6SEdward Tomasz Napierala 		return (0);
7074b5c9cf6SEdward Tomasz Napierala 
70810287198SEdward Tomasz Napierala 	SDT_PROBE3(racct, , rusage, set__force, p, resource, amount);
70910287198SEdward Tomasz Napierala 
710*4c230cdaSEdward Tomasz Napierala 	RACCT_LOCK();
71110287198SEdward Tomasz Napierala 	error = racct_set_locked(p, resource, amount, 0);
712*4c230cdaSEdward Tomasz Napierala 	RACCT_UNLOCK();
713097055e2SEdward Tomasz Napierala 	return (error);
714097055e2SEdward Tomasz Napierala }
715097055e2SEdward Tomasz Napierala 
7160b9f1ecbSEdward Tomasz Napierala void
7170b9f1ecbSEdward Tomasz Napierala racct_set_force(struct proc *p, int resource, uint64_t amount)
7180b9f1ecbSEdward Tomasz Napierala {
7190b9f1ecbSEdward Tomasz Napierala 
7200b9f1ecbSEdward Tomasz Napierala 	if (!racct_enable)
7210b9f1ecbSEdward Tomasz Napierala 		return;
7220b9f1ecbSEdward Tomasz Napierala 
7230b9f1ecbSEdward Tomasz Napierala 	SDT_PROBE3(racct, , rusage, set, p, resource, amount);
7240b9f1ecbSEdward Tomasz Napierala 
725*4c230cdaSEdward Tomasz Napierala 	RACCT_LOCK();
7260b9f1ecbSEdward Tomasz Napierala 	racct_set_locked(p, resource, amount, 1);
727*4c230cdaSEdward Tomasz Napierala 	RACCT_UNLOCK();
7280b9f1ecbSEdward Tomasz Napierala }
7290b9f1ecbSEdward Tomasz Napierala 
730097055e2SEdward Tomasz Napierala /*
731097055e2SEdward Tomasz Napierala  * Returns amount of 'resource' the process 'p' can keep allocated.
732097055e2SEdward Tomasz Napierala  * Allocating more than that would be denied, unless the resource
733097055e2SEdward Tomasz Napierala  * is marked undeniable.  Amount of already allocated resource does
734097055e2SEdward Tomasz Napierala  * not matter.
735097055e2SEdward Tomasz Napierala  */
736097055e2SEdward Tomasz Napierala uint64_t
737097055e2SEdward Tomasz Napierala racct_get_limit(struct proc *p, int resource)
738097055e2SEdward Tomasz Napierala {
739097055e2SEdward Tomasz Napierala 
7404b5c9cf6SEdward Tomasz Napierala 	if (!racct_enable)
7414b5c9cf6SEdward Tomasz Napierala 		return (UINT64_MAX);
7424b5c9cf6SEdward Tomasz Napierala 
743097055e2SEdward Tomasz Napierala #ifdef RCTL
744097055e2SEdward Tomasz Napierala 	return (rctl_get_limit(p, resource));
745097055e2SEdward Tomasz Napierala #else
746097055e2SEdward Tomasz Napierala 	return (UINT64_MAX);
747097055e2SEdward Tomasz Napierala #endif
748097055e2SEdward Tomasz Napierala }
749097055e2SEdward Tomasz Napierala 
750097055e2SEdward Tomasz Napierala /*
751097055e2SEdward Tomasz Napierala  * Returns amount of 'resource' the process 'p' can keep allocated.
752097055e2SEdward Tomasz Napierala  * Allocating more than that would be denied, unless the resource
753097055e2SEdward Tomasz Napierala  * is marked undeniable.  Amount of already allocated resource does
754097055e2SEdward Tomasz Napierala  * matter.
755097055e2SEdward Tomasz Napierala  */
756097055e2SEdward Tomasz Napierala uint64_t
757097055e2SEdward Tomasz Napierala racct_get_available(struct proc *p, int resource)
758097055e2SEdward Tomasz Napierala {
759097055e2SEdward Tomasz Napierala 
7604b5c9cf6SEdward Tomasz Napierala 	if (!racct_enable)
7614b5c9cf6SEdward Tomasz Napierala 		return (UINT64_MAX);
7624b5c9cf6SEdward Tomasz Napierala 
763097055e2SEdward Tomasz Napierala #ifdef RCTL
764097055e2SEdward Tomasz Napierala 	return (rctl_get_available(p, resource));
765097055e2SEdward Tomasz Napierala #else
766097055e2SEdward Tomasz Napierala 	return (UINT64_MAX);
767097055e2SEdward Tomasz Napierala #endif
768097055e2SEdward Tomasz Napierala }
769097055e2SEdward Tomasz Napierala 
770097055e2SEdward Tomasz Napierala /*
77136af9869SEdward Tomasz Napierala  * Returns amount of the %cpu resource that process 'p' can add to its %cpu
77236af9869SEdward Tomasz Napierala  * utilization.  Adding more than that would lead to the process being
77336af9869SEdward Tomasz Napierala  * throttled.
77436af9869SEdward Tomasz Napierala  */
77536af9869SEdward Tomasz Napierala static int64_t
77636af9869SEdward Tomasz Napierala racct_pcpu_available(struct proc *p)
77736af9869SEdward Tomasz Napierala {
77836af9869SEdward Tomasz Napierala 
7794b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
7804b5c9cf6SEdward Tomasz Napierala 
78136af9869SEdward Tomasz Napierala #ifdef RCTL
78236af9869SEdward Tomasz Napierala 	return (rctl_pcpu_available(p));
78336af9869SEdward Tomasz Napierala #else
78436af9869SEdward Tomasz Napierala 	return (INT64_MAX);
78536af9869SEdward Tomasz Napierala #endif
78636af9869SEdward Tomasz Napierala }
78736af9869SEdward Tomasz Napierala 
78836af9869SEdward Tomasz Napierala /*
789097055e2SEdward Tomasz Napierala  * Decrease allocation of 'resource' by 'amount' for process 'p'.
790097055e2SEdward Tomasz Napierala  */
791097055e2SEdward Tomasz Napierala void
792097055e2SEdward Tomasz Napierala racct_sub(struct proc *p, int resource, uint64_t amount)
793097055e2SEdward Tomasz Napierala {
794097055e2SEdward Tomasz Napierala 
7954b5c9cf6SEdward Tomasz Napierala 	if (!racct_enable)
7964b5c9cf6SEdward Tomasz Napierala 		return;
7974b5c9cf6SEdward Tomasz Napierala 
79836160958SMark Johnston 	SDT_PROBE3(racct, , rusage, sub, p, resource, amount);
799097055e2SEdward Tomasz Napierala 
800097055e2SEdward Tomasz Napierala 	/*
801097055e2SEdward Tomasz Napierala 	 * We need proc lock to dereference p->p_ucred.
802097055e2SEdward Tomasz Napierala 	 */
803097055e2SEdward Tomasz Napierala 	PROC_LOCK_ASSERT(p, MA_OWNED);
80436af9869SEdward Tomasz Napierala 	KASSERT(RACCT_CAN_DROP(resource),
805baf85d0aSEdward Tomasz Napierala 	    ("%s: called for non-droppable resource %d", __func__, resource));
806097055e2SEdward Tomasz Napierala 
807*4c230cdaSEdward Tomasz Napierala 	RACCT_LOCK();
808097055e2SEdward Tomasz Napierala 	KASSERT(amount <= p->p_racct->r_resources[resource],
809baf85d0aSEdward Tomasz Napierala 	    ("%s: freeing %ju of resource %d, which is more "
810baf85d0aSEdward Tomasz Napierala 	     "than allocated %jd for %s (pid %d)", __func__, amount, resource,
811097055e2SEdward Tomasz Napierala 	    (intmax_t)p->p_racct->r_resources[resource], p->p_comm, p->p_pid));
812097055e2SEdward Tomasz Napierala 
8133768a5dfSJeremie Le Hen 	racct_adjust_resource(p->p_racct, resource, -amount);
814097055e2SEdward Tomasz Napierala 	racct_sub_cred_locked(p->p_ucred, resource, amount);
815*4c230cdaSEdward Tomasz Napierala 	RACCT_UNLOCK();
816097055e2SEdward Tomasz Napierala }
817097055e2SEdward Tomasz Napierala 
818097055e2SEdward Tomasz Napierala static void
819097055e2SEdward Tomasz Napierala racct_sub_cred_locked(struct ucred *cred, int resource, uint64_t amount)
820097055e2SEdward Tomasz Napierala {
821097055e2SEdward Tomasz Napierala 	struct prison *pr;
822097055e2SEdward Tomasz Napierala 
8234b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
8244b5c9cf6SEdward Tomasz Napierala 
82536160958SMark Johnston 	SDT_PROBE3(racct, , rusage, sub__cred, cred, resource, amount);
826097055e2SEdward Tomasz Napierala 
827097055e2SEdward Tomasz Napierala #ifdef notyet
82836af9869SEdward Tomasz Napierala 	KASSERT(RACCT_CAN_DROP(resource),
829baf85d0aSEdward Tomasz Napierala 	    ("%s: called for resource %d which can not drop", __func__,
830097055e2SEdward Tomasz Napierala 	     resource));
831097055e2SEdward Tomasz Napierala #endif
832097055e2SEdward Tomasz Napierala 
8333768a5dfSJeremie Le Hen 	racct_adjust_resource(cred->cr_ruidinfo->ui_racct, resource, -amount);
834097055e2SEdward Tomasz Napierala 	for (pr = cred->cr_prison; pr != NULL; pr = pr->pr_parent)
8353768a5dfSJeremie Le Hen 		racct_adjust_resource(pr->pr_prison_racct->prr_racct, resource,
836a7ad07bfSEdward Tomasz Napierala 		    -amount);
8373768a5dfSJeremie Le Hen 	racct_adjust_resource(cred->cr_loginclass->lc_racct, resource, -amount);
838097055e2SEdward Tomasz Napierala }
839097055e2SEdward Tomasz Napierala 
840097055e2SEdward Tomasz Napierala /*
841097055e2SEdward Tomasz Napierala  * Decrease allocation of 'resource' by 'amount' for credential 'cred'.
842097055e2SEdward Tomasz Napierala  */
843097055e2SEdward Tomasz Napierala void
844097055e2SEdward Tomasz Napierala racct_sub_cred(struct ucred *cred, int resource, uint64_t amount)
845097055e2SEdward Tomasz Napierala {
846097055e2SEdward Tomasz Napierala 
8474b5c9cf6SEdward Tomasz Napierala 	if (!racct_enable)
8484b5c9cf6SEdward Tomasz Napierala 		return;
8494b5c9cf6SEdward Tomasz Napierala 
850*4c230cdaSEdward Tomasz Napierala 	RACCT_LOCK();
851097055e2SEdward Tomasz Napierala 	racct_sub_cred_locked(cred, resource, amount);
852*4c230cdaSEdward Tomasz Napierala 	RACCT_UNLOCK();
853097055e2SEdward Tomasz Napierala }
854097055e2SEdward Tomasz Napierala 
855097055e2SEdward Tomasz Napierala /*
856097055e2SEdward Tomasz Napierala  * Inherit resource usage information from the parent process.
857097055e2SEdward Tomasz Napierala  */
858097055e2SEdward Tomasz Napierala int
859097055e2SEdward Tomasz Napierala racct_proc_fork(struct proc *parent, struct proc *child)
860097055e2SEdward Tomasz Napierala {
861097055e2SEdward Tomasz Napierala 	int i, error = 0;
862097055e2SEdward Tomasz Napierala 
8634b5c9cf6SEdward Tomasz Napierala 	if (!racct_enable)
8644b5c9cf6SEdward Tomasz Napierala 		return (0);
8654b5c9cf6SEdward Tomasz Napierala 
866097055e2SEdward Tomasz Napierala 	/*
867097055e2SEdward Tomasz Napierala 	 * Create racct for the child process.
868097055e2SEdward Tomasz Napierala 	 */
869097055e2SEdward Tomasz Napierala 	racct_create(&child->p_racct);
870097055e2SEdward Tomasz Napierala 
871097055e2SEdward Tomasz Napierala 	PROC_LOCK(parent);
872097055e2SEdward Tomasz Napierala 	PROC_LOCK(child);
873*4c230cdaSEdward Tomasz Napierala 	RACCT_LOCK();
874097055e2SEdward Tomasz Napierala 
875c0c09362SEdward Tomasz Napierala #ifdef RCTL
876c0c09362SEdward Tomasz Napierala 	error = rctl_proc_fork(parent, child);
877c0c09362SEdward Tomasz Napierala 	if (error != 0)
878c0c09362SEdward Tomasz Napierala 		goto out;
879c0c09362SEdward Tomasz Napierala #endif
880c0c09362SEdward Tomasz Napierala 
88136af9869SEdward Tomasz Napierala 	/* Init process cpu time. */
88236af9869SEdward Tomasz Napierala 	child->p_prev_runtime = 0;
88336af9869SEdward Tomasz Napierala 	child->p_throttled = 0;
88436af9869SEdward Tomasz Napierala 
885097055e2SEdward Tomasz Napierala 	/*
886097055e2SEdward Tomasz Napierala 	 * Inherit resource usage.
887097055e2SEdward Tomasz Napierala 	 */
888097055e2SEdward Tomasz Napierala 	for (i = 0; i <= RACCT_MAX; i++) {
889097055e2SEdward Tomasz Napierala 		if (parent->p_racct->r_resources[i] == 0 ||
8904fe84775SEdward Tomasz Napierala 		    !RACCT_IS_INHERITABLE(i))
891097055e2SEdward Tomasz Napierala 			continue;
892097055e2SEdward Tomasz Napierala 
893097055e2SEdward Tomasz Napierala 		error = racct_set_locked(child, i,
89410287198SEdward Tomasz Napierala 		    parent->p_racct->r_resources[i], 0);
895ac6fafe6SEdward Tomasz Napierala 		if (error != 0)
896097055e2SEdward Tomasz Napierala 			goto out;
897097055e2SEdward Tomasz Napierala 	}
898097055e2SEdward Tomasz Napierala 
8997cea96f6SEdward Tomasz Napierala 	error = racct_add_locked(child, RACCT_NPROC, 1, 0);
9007cea96f6SEdward Tomasz Napierala 	error += racct_add_locked(child, RACCT_NTHR, 1, 0);
9012d8696d1SEdward Tomasz Napierala 
902097055e2SEdward Tomasz Napierala out:
903*4c230cdaSEdward Tomasz Napierala 	RACCT_UNLOCK();
904097055e2SEdward Tomasz Napierala 	PROC_UNLOCK(child);
905097055e2SEdward Tomasz Napierala 	PROC_UNLOCK(parent);
906097055e2SEdward Tomasz Napierala 
907ab27d5d8SEdward Tomasz Napierala 	if (error != 0)
908ab27d5d8SEdward Tomasz Napierala 		racct_proc_exit(child);
909ab27d5d8SEdward Tomasz Napierala 
910097055e2SEdward Tomasz Napierala 	return (error);
911097055e2SEdward Tomasz Napierala }
912097055e2SEdward Tomasz Napierala 
91372a401d9SEdward Tomasz Napierala /*
91472a401d9SEdward Tomasz Napierala  * Called at the end of fork1(), to handle rules that require the process
91572a401d9SEdward Tomasz Napierala  * to be fully initialized.
91672a401d9SEdward Tomasz Napierala  */
91772a401d9SEdward Tomasz Napierala void
91872a401d9SEdward Tomasz Napierala racct_proc_fork_done(struct proc *child)
91972a401d9SEdward Tomasz Napierala {
92072a401d9SEdward Tomasz Napierala 
921813361c1SMateusz Guzik 	PROC_LOCK_ASSERT(child, MA_OWNED);
92272a401d9SEdward Tomasz Napierala #ifdef RCTL
9234b5c9cf6SEdward Tomasz Napierala 	if (!racct_enable)
9244b5c9cf6SEdward Tomasz Napierala 		return;
9254b5c9cf6SEdward Tomasz Napierala 
926*4c230cdaSEdward Tomasz Napierala 	RACCT_LOCK();
92772a401d9SEdward Tomasz Napierala 	rctl_enforce(child, RACCT_NPROC, 0);
92872a401d9SEdward Tomasz Napierala 	rctl_enforce(child, RACCT_NTHR, 0);
929*4c230cdaSEdward Tomasz Napierala 	RACCT_UNLOCK();
93072a401d9SEdward Tomasz Napierala #endif
93172a401d9SEdward Tomasz Napierala }
93272a401d9SEdward Tomasz Napierala 
933097055e2SEdward Tomasz Napierala void
934097055e2SEdward Tomasz Napierala racct_proc_exit(struct proc *p)
935097055e2SEdward Tomasz Napierala {
9362419d7f9SEdward Tomasz Napierala 	int i;
937097055e2SEdward Tomasz Napierala 	uint64_t runtime;
93836af9869SEdward Tomasz Napierala 	struct timeval wallclock;
93936af9869SEdward Tomasz Napierala 	uint64_t pct_estimate, pct;
940097055e2SEdward Tomasz Napierala 
9414b5c9cf6SEdward Tomasz Napierala 	if (!racct_enable)
9424b5c9cf6SEdward Tomasz Napierala 		return;
9434b5c9cf6SEdward Tomasz Napierala 
944097055e2SEdward Tomasz Napierala 	PROC_LOCK(p);
945097055e2SEdward Tomasz Napierala 	/*
946097055e2SEdward Tomasz Napierala 	 * We don't need to calculate rux, proc_reap() has already done this.
947097055e2SEdward Tomasz Napierala 	 */
948097055e2SEdward Tomasz Napierala 	runtime = cputick2usec(p->p_rux.rux_runtime);
949097055e2SEdward Tomasz Napierala #ifdef notyet
950097055e2SEdward Tomasz Napierala 	KASSERT(runtime >= p->p_prev_runtime, ("runtime < p_prev_runtime"));
951097055e2SEdward Tomasz Napierala #else
952097055e2SEdward Tomasz Napierala 	if (runtime < p->p_prev_runtime)
953097055e2SEdward Tomasz Napierala 		runtime = p->p_prev_runtime;
954097055e2SEdward Tomasz Napierala #endif
95536af9869SEdward Tomasz Napierala 	microuptime(&wallclock);
95636af9869SEdward Tomasz Napierala 	timevalsub(&wallclock, &p->p_stats->p_start);
95784590fd8SEdward Tomasz Napierala 	if (wallclock.tv_sec > 0 || wallclock.tv_usec > 0) {
95836af9869SEdward Tomasz Napierala 		pct_estimate = (1000000 * runtime * 100) /
95936af9869SEdward Tomasz Napierala 		    ((uint64_t)wallclock.tv_sec * 1000000 +
96036af9869SEdward Tomasz Napierala 		    wallclock.tv_usec);
96184590fd8SEdward Tomasz Napierala 	} else
96284590fd8SEdward Tomasz Napierala 		pct_estimate = 0;
96336af9869SEdward Tomasz Napierala 	pct = racct_getpcpu(p, pct_estimate);
96436af9869SEdward Tomasz Napierala 
965*4c230cdaSEdward Tomasz Napierala 	RACCT_LOCK();
96610287198SEdward Tomasz Napierala 	racct_set_locked(p, RACCT_CPU, runtime, 0);
96736af9869SEdward Tomasz Napierala 	racct_add_cred_locked(p->p_ucred, RACCT_PCTCPU, pct);
968097055e2SEdward Tomasz Napierala 
9692419d7f9SEdward Tomasz Napierala 	for (i = 0; i <= RACCT_MAX; i++) {
9702419d7f9SEdward Tomasz Napierala 		if (p->p_racct->r_resources[i] == 0)
9712419d7f9SEdward Tomasz Napierala 			continue;
9722419d7f9SEdward Tomasz Napierala 	    	if (!RACCT_IS_RECLAIMABLE(i))
9732419d7f9SEdward Tomasz Napierala 			continue;
97410287198SEdward Tomasz Napierala 		racct_set_locked(p, i, 0, 0);
9752419d7f9SEdward Tomasz Napierala 	}
9762419d7f9SEdward Tomasz Napierala 
977*4c230cdaSEdward Tomasz Napierala 	RACCT_UNLOCK();
978097055e2SEdward Tomasz Napierala 	PROC_UNLOCK(p);
979097055e2SEdward Tomasz Napierala 
980097055e2SEdward Tomasz Napierala #ifdef RCTL
981097055e2SEdward Tomasz Napierala 	rctl_racct_release(p->p_racct);
982097055e2SEdward Tomasz Napierala #endif
983097055e2SEdward Tomasz Napierala 	racct_destroy(&p->p_racct);
984097055e2SEdward Tomasz Napierala }
985097055e2SEdward Tomasz Napierala 
986097055e2SEdward Tomasz Napierala /*
987097055e2SEdward Tomasz Napierala  * Called after credentials change, to move resource utilisation
988097055e2SEdward Tomasz Napierala  * between raccts.
989097055e2SEdward Tomasz Napierala  */
990097055e2SEdward Tomasz Napierala void
991097055e2SEdward Tomasz Napierala racct_proc_ucred_changed(struct proc *p, struct ucred *oldcred,
992097055e2SEdward Tomasz Napierala     struct ucred *newcred)
993097055e2SEdward Tomasz Napierala {
994097055e2SEdward Tomasz Napierala 	struct uidinfo *olduip, *newuip;
995097055e2SEdward Tomasz Napierala 	struct loginclass *oldlc, *newlc;
996097055e2SEdward Tomasz Napierala 	struct prison *oldpr, *newpr, *pr;
997097055e2SEdward Tomasz Napierala 
9984b5c9cf6SEdward Tomasz Napierala 	if (!racct_enable)
9994b5c9cf6SEdward Tomasz Napierala 		return;
10004b5c9cf6SEdward Tomasz Napierala 
1001097055e2SEdward Tomasz Napierala 	PROC_LOCK_ASSERT(p, MA_NOTOWNED);
1002097055e2SEdward Tomasz Napierala 
1003097055e2SEdward Tomasz Napierala 	newuip = newcred->cr_ruidinfo;
1004097055e2SEdward Tomasz Napierala 	olduip = oldcred->cr_ruidinfo;
1005097055e2SEdward Tomasz Napierala 	newlc = newcred->cr_loginclass;
1006097055e2SEdward Tomasz Napierala 	oldlc = oldcred->cr_loginclass;
1007097055e2SEdward Tomasz Napierala 	newpr = newcred->cr_prison;
1008097055e2SEdward Tomasz Napierala 	oldpr = oldcred->cr_prison;
1009097055e2SEdward Tomasz Napierala 
1010*4c230cdaSEdward Tomasz Napierala 	RACCT_LOCK();
1011097055e2SEdward Tomasz Napierala 	if (newuip != olduip) {
1012097055e2SEdward Tomasz Napierala 		racct_sub_racct(olduip->ui_racct, p->p_racct);
1013097055e2SEdward Tomasz Napierala 		racct_add_racct(newuip->ui_racct, p->p_racct);
1014097055e2SEdward Tomasz Napierala 	}
1015097055e2SEdward Tomasz Napierala 	if (newlc != oldlc) {
1016097055e2SEdward Tomasz Napierala 		racct_sub_racct(oldlc->lc_racct, p->p_racct);
1017097055e2SEdward Tomasz Napierala 		racct_add_racct(newlc->lc_racct, p->p_racct);
1018097055e2SEdward Tomasz Napierala 	}
1019097055e2SEdward Tomasz Napierala 	if (newpr != oldpr) {
1020097055e2SEdward Tomasz Napierala 		for (pr = oldpr; pr != NULL; pr = pr->pr_parent)
1021a7ad07bfSEdward Tomasz Napierala 			racct_sub_racct(pr->pr_prison_racct->prr_racct,
1022a7ad07bfSEdward Tomasz Napierala 			    p->p_racct);
1023097055e2SEdward Tomasz Napierala 		for (pr = newpr; pr != NULL; pr = pr->pr_parent)
1024a7ad07bfSEdward Tomasz Napierala 			racct_add_racct(pr->pr_prison_racct->prr_racct,
1025a7ad07bfSEdward Tomasz Napierala 			    p->p_racct);
1026097055e2SEdward Tomasz Napierala 	}
1027*4c230cdaSEdward Tomasz Napierala 	RACCT_UNLOCK();
1028097055e2SEdward Tomasz Napierala 
1029097055e2SEdward Tomasz Napierala #ifdef RCTL
1030097055e2SEdward Tomasz Napierala 	rctl_proc_ucred_changed(p, newcred);
1031097055e2SEdward Tomasz Napierala #endif
1032097055e2SEdward Tomasz Napierala }
1033097055e2SEdward Tomasz Napierala 
1034c34bbd2aSEdward Tomasz Napierala void
1035c34bbd2aSEdward Tomasz Napierala racct_move(struct racct *dest, struct racct *src)
1036c34bbd2aSEdward Tomasz Napierala {
1037c34bbd2aSEdward Tomasz Napierala 
10384b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
10394b5c9cf6SEdward Tomasz Napierala 
1040*4c230cdaSEdward Tomasz Napierala 	RACCT_LOCK();
1041c34bbd2aSEdward Tomasz Napierala 	racct_add_racct(dest, src);
1042c34bbd2aSEdward Tomasz Napierala 	racct_sub_racct(src, src);
1043*4c230cdaSEdward Tomasz Napierala 	RACCT_UNLOCK();
1044c34bbd2aSEdward Tomasz Napierala }
1045c34bbd2aSEdward Tomasz Napierala 
1046097055e2SEdward Tomasz Napierala static void
104736af9869SEdward Tomasz Napierala racct_proc_throttle(struct proc *p)
104836af9869SEdward Tomasz Napierala {
104936af9869SEdward Tomasz Napierala 	struct thread *td;
105036af9869SEdward Tomasz Napierala #ifdef SMP
105136af9869SEdward Tomasz Napierala 	int cpuid;
105236af9869SEdward Tomasz Napierala #endif
105336af9869SEdward Tomasz Napierala 
10544b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
105536af9869SEdward Tomasz Napierala 	PROC_LOCK_ASSERT(p, MA_OWNED);
105636af9869SEdward Tomasz Napierala 
105736af9869SEdward Tomasz Napierala 	/*
105836af9869SEdward Tomasz Napierala 	 * Do not block kernel processes.  Also do not block processes with
105936af9869SEdward Tomasz Napierala 	 * low %cpu utilization to improve interactivity.
106036af9869SEdward Tomasz Napierala 	 */
1061db57c70aSKonstantin Belousov 	if (((p->p_flag & (P_SYSTEM | P_KPROC)) != 0) ||
106236af9869SEdward Tomasz Napierala 	    (p->p_racct->r_resources[RACCT_PCTCPU] <= pcpu_threshold))
106336af9869SEdward Tomasz Napierala 		return;
106436af9869SEdward Tomasz Napierala 	p->p_throttled = 1;
106536af9869SEdward Tomasz Napierala 
106636af9869SEdward Tomasz Napierala 	FOREACH_THREAD_IN_PROC(p, td) {
106716befafdSEdward Tomasz Napierala 		thread_lock(td);
106836af9869SEdward Tomasz Napierala 		switch (td->td_state) {
106936af9869SEdward Tomasz Napierala 		case TDS_RUNQ:
107036af9869SEdward Tomasz Napierala 			/*
107136af9869SEdward Tomasz Napierala 			 * If the thread is on the scheduler run-queue, we can
107236af9869SEdward Tomasz Napierala 			 * not just remove it from there.  So we set the flag
107336af9869SEdward Tomasz Napierala 			 * TDF_NEEDRESCHED for the thread, so that once it is
107436af9869SEdward Tomasz Napierala 			 * running, it is taken off the cpu as soon as possible.
107536af9869SEdward Tomasz Napierala 			 */
107636af9869SEdward Tomasz Napierala 			td->td_flags |= TDF_NEEDRESCHED;
107736af9869SEdward Tomasz Napierala 			break;
107836af9869SEdward Tomasz Napierala 		case TDS_RUNNING:
107936af9869SEdward Tomasz Napierala 			/*
108036af9869SEdward Tomasz Napierala 			 * If the thread is running, we request a context
108136af9869SEdward Tomasz Napierala 			 * switch for it by setting the TDF_NEEDRESCHED flag.
108236af9869SEdward Tomasz Napierala 			 */
108336af9869SEdward Tomasz Napierala 			td->td_flags |= TDF_NEEDRESCHED;
108436af9869SEdward Tomasz Napierala #ifdef SMP
108536af9869SEdward Tomasz Napierala 			cpuid = td->td_oncpu;
108636af9869SEdward Tomasz Napierala 			if ((cpuid != NOCPU) && (td != curthread))
108736af9869SEdward Tomasz Napierala 				ipi_cpu(cpuid, IPI_AST);
108836af9869SEdward Tomasz Napierala #endif
108936af9869SEdward Tomasz Napierala 			break;
109036af9869SEdward Tomasz Napierala 		default:
109136af9869SEdward Tomasz Napierala 			break;
109236af9869SEdward Tomasz Napierala 		}
109316befafdSEdward Tomasz Napierala 		thread_unlock(td);
109436af9869SEdward Tomasz Napierala 	}
109536af9869SEdward Tomasz Napierala }
109636af9869SEdward Tomasz Napierala 
109736af9869SEdward Tomasz Napierala static void
109836af9869SEdward Tomasz Napierala racct_proc_wakeup(struct proc *p)
109936af9869SEdward Tomasz Napierala {
11004b5c9cf6SEdward Tomasz Napierala 
11014b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
11024b5c9cf6SEdward Tomasz Napierala 
110336af9869SEdward Tomasz Napierala 	PROC_LOCK_ASSERT(p, MA_OWNED);
110436af9869SEdward Tomasz Napierala 
110536af9869SEdward Tomasz Napierala 	if (p->p_throttled) {
110636af9869SEdward Tomasz Napierala 		p->p_throttled = 0;
110736af9869SEdward Tomasz Napierala 		wakeup(p->p_racct);
110836af9869SEdward Tomasz Napierala 	}
110936af9869SEdward Tomasz Napierala }
111036af9869SEdward Tomasz Napierala 
111136af9869SEdward Tomasz Napierala static void
1112862d03fbSEdward Tomasz Napierala racct_decay_callback(struct racct *racct, void *dummy1, void *dummy2)
111336af9869SEdward Tomasz Napierala {
111436af9869SEdward Tomasz Napierala 	int64_t r_old, r_new;
111536af9869SEdward Tomasz Napierala 
11164b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
1117*4c230cdaSEdward Tomasz Napierala 	RACCT_LOCK_ASSERT();
11184b5c9cf6SEdward Tomasz Napierala 
1119862d03fbSEdward Tomasz Napierala 	r_old = racct->r_resources[RACCT_PCTCPU];
112036af9869SEdward Tomasz Napierala 
112136af9869SEdward Tomasz Napierala 	/* If there is nothing to decay, just exit. */
112236af9869SEdward Tomasz Napierala 	if (r_old <= 0)
112336af9869SEdward Tomasz Napierala 		return;
112436af9869SEdward Tomasz Napierala 
112536af9869SEdward Tomasz Napierala 	r_new = r_old * RACCT_DECAY_FACTOR / FSCALE;
1126862d03fbSEdward Tomasz Napierala 	racct->r_resources[RACCT_PCTCPU] = r_new;
112715db3c07SEdward Tomasz Napierala }
112815db3c07SEdward Tomasz Napierala 
112915db3c07SEdward Tomasz Napierala static void
113015db3c07SEdward Tomasz Napierala racct_decay_pre(void)
113115db3c07SEdward Tomasz Napierala {
113215db3c07SEdward Tomasz Napierala 
1133*4c230cdaSEdward Tomasz Napierala 	RACCT_LOCK();
113415db3c07SEdward Tomasz Napierala }
113515db3c07SEdward Tomasz Napierala 
113615db3c07SEdward Tomasz Napierala static void
113715db3c07SEdward Tomasz Napierala racct_decay_post(void)
113815db3c07SEdward Tomasz Napierala {
113915db3c07SEdward Tomasz Napierala 
1140*4c230cdaSEdward Tomasz Napierala 	RACCT_UNLOCK();
114136af9869SEdward Tomasz Napierala }
114236af9869SEdward Tomasz Napierala 
114336af9869SEdward Tomasz Napierala static void
1144097e0da7SEdward Tomasz Napierala racct_decay(void)
114536af9869SEdward Tomasz Napierala {
11464b5c9cf6SEdward Tomasz Napierala 
11474b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
11484b5c9cf6SEdward Tomasz Napierala 
1149862d03fbSEdward Tomasz Napierala 	ui_racct_foreach(racct_decay_callback, racct_decay_pre,
1150862d03fbSEdward Tomasz Napierala 	    racct_decay_post, NULL, NULL);
1151862d03fbSEdward Tomasz Napierala 	loginclass_racct_foreach(racct_decay_callback, racct_decay_pre,
1152862d03fbSEdward Tomasz Napierala 	    racct_decay_post, NULL, NULL);
1153862d03fbSEdward Tomasz Napierala 	prison_racct_foreach(racct_decay_callback, racct_decay_pre,
1154862d03fbSEdward Tomasz Napierala 	    racct_decay_post, NULL, NULL);
115536af9869SEdward Tomasz Napierala }
115636af9869SEdward Tomasz Napierala 
115736af9869SEdward Tomasz Napierala static void
1158097055e2SEdward Tomasz Napierala racctd(void)
1159097055e2SEdward Tomasz Napierala {
1160097055e2SEdward Tomasz Napierala 	struct thread *td;
1161097055e2SEdward Tomasz Napierala 	struct proc *p;
1162097055e2SEdward Tomasz Napierala 	struct timeval wallclock;
1163097055e2SEdward Tomasz Napierala 	uint64_t runtime;
116436af9869SEdward Tomasz Napierala 	uint64_t pct, pct_estimate;
1165097055e2SEdward Tomasz Napierala 
11664b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
11674b5c9cf6SEdward Tomasz Napierala 
1168097055e2SEdward Tomasz Napierala 	for (;;) {
1169862d03fbSEdward Tomasz Napierala 		racct_decay();
117036af9869SEdward Tomasz Napierala 
1171097055e2SEdward Tomasz Napierala 		sx_slock(&allproc_lock);
1172097055e2SEdward Tomasz Napierala 
117336af9869SEdward Tomasz Napierala 		LIST_FOREACH(p, &zombproc, p_list) {
117436af9869SEdward Tomasz Napierala 			PROC_LOCK(p);
117536af9869SEdward Tomasz Napierala 			racct_set(p, RACCT_PCTCPU, 0);
117636af9869SEdward Tomasz Napierala 			PROC_UNLOCK(p);
117736af9869SEdward Tomasz Napierala 		}
117836af9869SEdward Tomasz Napierala 
1179097055e2SEdward Tomasz Napierala 		FOREACH_PROC_IN_SYSTEM(p) {
118036af9869SEdward Tomasz Napierala 			PROC_LOCK(p);
118136af9869SEdward Tomasz Napierala 			if (p->p_state != PRS_NORMAL) {
118236af9869SEdward Tomasz Napierala 				PROC_UNLOCK(p);
1183097055e2SEdward Tomasz Napierala 				continue;
118436af9869SEdward Tomasz Napierala 			}
1185097055e2SEdward Tomasz Napierala 
1186097055e2SEdward Tomasz Napierala 			microuptime(&wallclock);
1187097055e2SEdward Tomasz Napierala 			timevalsub(&wallclock, &p->p_stats->p_start);
11885c7bebf9SKonstantin Belousov 			PROC_STATLOCK(p);
11890a53cd57SEdward Tomasz Napierala 			FOREACH_THREAD_IN_PROC(p, td)
1190097055e2SEdward Tomasz Napierala 				ruxagg(p, td);
1191097055e2SEdward Tomasz Napierala 			runtime = cputick2usec(p->p_rux.rux_runtime);
11925c7bebf9SKonstantin Belousov 			PROC_STATUNLOCK(p);
1193097055e2SEdward Tomasz Napierala #ifdef notyet
1194097055e2SEdward Tomasz Napierala 			KASSERT(runtime >= p->p_prev_runtime,
1195097055e2SEdward Tomasz Napierala 			    ("runtime < p_prev_runtime"));
1196097055e2SEdward Tomasz Napierala #else
1197097055e2SEdward Tomasz Napierala 			if (runtime < p->p_prev_runtime)
1198097055e2SEdward Tomasz Napierala 				runtime = p->p_prev_runtime;
1199097055e2SEdward Tomasz Napierala #endif
1200097055e2SEdward Tomasz Napierala 			p->p_prev_runtime = runtime;
120184590fd8SEdward Tomasz Napierala 			if (wallclock.tv_sec > 0 || wallclock.tv_usec > 0) {
120236af9869SEdward Tomasz Napierala 				pct_estimate = (1000000 * runtime * 100) /
120336af9869SEdward Tomasz Napierala 				    ((uint64_t)wallclock.tv_sec * 1000000 +
120436af9869SEdward Tomasz Napierala 				    wallclock.tv_usec);
120584590fd8SEdward Tomasz Napierala 			} else
120684590fd8SEdward Tomasz Napierala 				pct_estimate = 0;
120736af9869SEdward Tomasz Napierala 			pct = racct_getpcpu(p, pct_estimate);
1208*4c230cdaSEdward Tomasz Napierala 			RACCT_LOCK();
120910287198SEdward Tomasz Napierala 			racct_set_locked(p, RACCT_PCTCPU, pct, 1);
121010287198SEdward Tomasz Napierala 			racct_set_locked(p, RACCT_CPU, runtime, 0);
1211097055e2SEdward Tomasz Napierala 			racct_set_locked(p, RACCT_WALLCLOCK,
121259f513cdSJaakko Heinonen 			    (uint64_t)wallclock.tv_sec * 1000000 +
121310287198SEdward Tomasz Napierala 			    wallclock.tv_usec, 0);
1214*4c230cdaSEdward Tomasz Napierala 			RACCT_UNLOCK();
1215097055e2SEdward Tomasz Napierala 			PROC_UNLOCK(p);
1216097055e2SEdward Tomasz Napierala 		}
121736af9869SEdward Tomasz Napierala 
121836af9869SEdward Tomasz Napierala 		/*
121936af9869SEdward Tomasz Napierala 		 * To ensure that processes are throttled in a fair way, we need
122036af9869SEdward Tomasz Napierala 		 * to iterate over all processes again and check the limits
122136af9869SEdward Tomasz Napierala 		 * for %cpu resource only after ucred racct containers have been
122236af9869SEdward Tomasz Napierala 		 * properly filled.
122336af9869SEdward Tomasz Napierala 		 */
122436af9869SEdward Tomasz Napierala 		FOREACH_PROC_IN_SYSTEM(p) {
122536af9869SEdward Tomasz Napierala 			PROC_LOCK(p);
122636af9869SEdward Tomasz Napierala 			if (p->p_state != PRS_NORMAL) {
122736af9869SEdward Tomasz Napierala 				PROC_UNLOCK(p);
122836af9869SEdward Tomasz Napierala 				continue;
122936af9869SEdward Tomasz Napierala 			}
123036af9869SEdward Tomasz Napierala 
123136af9869SEdward Tomasz Napierala 			if (racct_pcpu_available(p) <= 0)
123236af9869SEdward Tomasz Napierala 				racct_proc_throttle(p);
123336af9869SEdward Tomasz Napierala 			else if (p->p_throttled)
123436af9869SEdward Tomasz Napierala 				racct_proc_wakeup(p);
123536af9869SEdward Tomasz Napierala 			PROC_UNLOCK(p);
123636af9869SEdward Tomasz Napierala 		}
1237097055e2SEdward Tomasz Napierala 		sx_sunlock(&allproc_lock);
1238097055e2SEdward Tomasz Napierala 		pause("-", hz);
1239097055e2SEdward Tomasz Napierala 	}
1240097055e2SEdward Tomasz Napierala }
1241097055e2SEdward Tomasz Napierala 
1242097055e2SEdward Tomasz Napierala static struct kproc_desc racctd_kp = {
1243097055e2SEdward Tomasz Napierala 	"racctd",
1244097055e2SEdward Tomasz Napierala 	racctd,
1245097055e2SEdward Tomasz Napierala 	NULL
1246097055e2SEdward Tomasz Napierala };
12474b5c9cf6SEdward Tomasz Napierala 
12484b5c9cf6SEdward Tomasz Napierala static void
12494b5c9cf6SEdward Tomasz Napierala racctd_init(void)
12504b5c9cf6SEdward Tomasz Napierala {
12514b5c9cf6SEdward Tomasz Napierala 	if (!racct_enable)
12524b5c9cf6SEdward Tomasz Napierala 		return;
12534b5c9cf6SEdward Tomasz Napierala 
12544b5c9cf6SEdward Tomasz Napierala 	kproc_start(&racctd_kp);
12554b5c9cf6SEdward Tomasz Napierala }
12564b5c9cf6SEdward Tomasz Napierala SYSINIT(racctd, SI_SUB_RACCTD, SI_ORDER_FIRST, racctd_init, NULL);
1257097055e2SEdward Tomasz Napierala 
1258097055e2SEdward Tomasz Napierala static void
1259097055e2SEdward Tomasz Napierala racct_init(void)
1260097055e2SEdward Tomasz Napierala {
12614b5c9cf6SEdward Tomasz Napierala 	if (!racct_enable)
12624b5c9cf6SEdward Tomasz Napierala 		return;
1263097055e2SEdward Tomasz Napierala 
1264097055e2SEdward Tomasz Napierala 	racct_zone = uma_zcreate("racct", sizeof(struct racct),
1265097055e2SEdward Tomasz Napierala 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
1266097055e2SEdward Tomasz Napierala 	/*
1267097055e2SEdward Tomasz Napierala 	 * XXX: Move this somewhere.
1268097055e2SEdward Tomasz Napierala 	 */
1269a7ad07bfSEdward Tomasz Napierala 	prison0.pr_prison_racct = prison_racct_find("0");
1270097055e2SEdward Tomasz Napierala }
1271097055e2SEdward Tomasz Napierala SYSINIT(racct, SI_SUB_RACCT, SI_ORDER_FIRST, racct_init, NULL);
1272097055e2SEdward Tomasz Napierala 
1273097055e2SEdward Tomasz Napierala #endif /* !RACCT */
1274