xref: /freebsd/sys/kern/kern_racct.c (revision 16befafd162f81741897711b7839861cb45ebe99)
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 
35097055e2SEdward Tomasz Napierala #include "opt_kdtrace.h"
3636af9869SEdward Tomasz Napierala #include "opt_sched.h"
37097055e2SEdward Tomasz Napierala 
38097055e2SEdward Tomasz Napierala #include <sys/param.h>
390e225211SAndriy Gapon #include <sys/systm.h>
40097055e2SEdward Tomasz Napierala #include <sys/eventhandler.h>
41097055e2SEdward Tomasz Napierala #include <sys/jail.h>
42097055e2SEdward Tomasz Napierala #include <sys/kernel.h>
43097055e2SEdward Tomasz Napierala #include <sys/kthread.h>
44097055e2SEdward Tomasz Napierala #include <sys/lock.h>
45097055e2SEdward Tomasz Napierala #include <sys/loginclass.h>
46097055e2SEdward Tomasz Napierala #include <sys/malloc.h>
47097055e2SEdward Tomasz Napierala #include <sys/mutex.h>
48097055e2SEdward Tomasz Napierala #include <sys/proc.h>
49097055e2SEdward Tomasz Napierala #include <sys/racct.h>
50097055e2SEdward Tomasz Napierala #include <sys/resourcevar.h>
51097055e2SEdward Tomasz Napierala #include <sys/sbuf.h>
52097055e2SEdward Tomasz Napierala #include <sys/sched.h>
53097055e2SEdward Tomasz Napierala #include <sys/sdt.h>
5436af9869SEdward Tomasz Napierala #include <sys/smp.h>
55097055e2SEdward Tomasz Napierala #include <sys/sx.h>
5636af9869SEdward Tomasz Napierala #include <sys/sysctl.h>
57097055e2SEdward Tomasz Napierala #include <sys/sysent.h>
58097055e2SEdward Tomasz Napierala #include <sys/sysproto.h>
59097055e2SEdward Tomasz Napierala #include <sys/umtx.h>
6036af9869SEdward Tomasz Napierala #include <machine/smp.h>
61097055e2SEdward Tomasz Napierala 
62097055e2SEdward Tomasz Napierala #ifdef RCTL
63097055e2SEdward Tomasz Napierala #include <sys/rctl.h>
64097055e2SEdward Tomasz Napierala #endif
65097055e2SEdward Tomasz Napierala 
66097055e2SEdward Tomasz Napierala #ifdef RACCT
67097055e2SEdward Tomasz Napierala 
68097055e2SEdward Tomasz Napierala FEATURE(racct, "Resource Accounting");
69097055e2SEdward Tomasz Napierala 
7036af9869SEdward Tomasz Napierala /*
7136af9869SEdward Tomasz Napierala  * Do not block processes that have their %cpu usage <= pcpu_threshold.
7236af9869SEdward Tomasz Napierala  */
7336af9869SEdward Tomasz Napierala static int pcpu_threshold = 1;
7436af9869SEdward Tomasz Napierala 
7536af9869SEdward Tomasz Napierala SYSCTL_NODE(_kern, OID_AUTO, racct, CTLFLAG_RW, 0, "Resource Accounting");
7636af9869SEdward Tomasz Napierala SYSCTL_UINT(_kern_racct, OID_AUTO, pcpu_threshold, CTLFLAG_RW, &pcpu_threshold,
7736af9869SEdward Tomasz Napierala     0, "Processes with higher %cpu usage than this value can be throttled.");
7836af9869SEdward Tomasz Napierala 
7936af9869SEdward Tomasz Napierala /*
8036af9869SEdward Tomasz Napierala  * How many seconds it takes to use the scheduler %cpu calculations.  When a
8136af9869SEdward Tomasz Napierala  * process starts, we compute its %cpu usage by dividing its runtime by the
8236af9869SEdward Tomasz Napierala  * process wall clock time.  After RACCT_PCPU_SECS pass, we use the value
8336af9869SEdward Tomasz Napierala  * provided by the scheduler.
8436af9869SEdward Tomasz Napierala  */
8536af9869SEdward Tomasz Napierala #define RACCT_PCPU_SECS		3
8636af9869SEdward Tomasz Napierala 
87097055e2SEdward Tomasz Napierala static struct mtx racct_lock;
88097055e2SEdward Tomasz Napierala MTX_SYSINIT(racct_lock, &racct_lock, "racct lock", MTX_DEF);
89097055e2SEdward Tomasz Napierala 
90097055e2SEdward Tomasz Napierala static uma_zone_t racct_zone;
91097055e2SEdward Tomasz Napierala 
92097055e2SEdward Tomasz Napierala static void racct_sub_racct(struct racct *dest, const struct racct *src);
93097055e2SEdward Tomasz Napierala static void racct_sub_cred_locked(struct ucred *cred, int resource,
94097055e2SEdward Tomasz Napierala 		uint64_t amount);
95097055e2SEdward Tomasz Napierala static void racct_add_cred_locked(struct ucred *cred, int resource,
96097055e2SEdward Tomasz Napierala 		uint64_t amount);
97097055e2SEdward Tomasz Napierala 
98097055e2SEdward Tomasz Napierala SDT_PROVIDER_DEFINE(racct);
99097055e2SEdward Tomasz Napierala SDT_PROBE_DEFINE3(racct, kernel, rusage, add, add, "struct proc *", "int",
100097055e2SEdward Tomasz Napierala     "uint64_t");
101097055e2SEdward Tomasz Napierala SDT_PROBE_DEFINE3(racct, kernel, rusage, add_failure, add-failure,
102097055e2SEdward Tomasz Napierala     "struct proc *", "int", "uint64_t");
103097055e2SEdward Tomasz Napierala SDT_PROBE_DEFINE3(racct, kernel, rusage, add_cred, add-cred, "struct ucred *",
104097055e2SEdward Tomasz Napierala     "int", "uint64_t");
105097055e2SEdward Tomasz Napierala SDT_PROBE_DEFINE3(racct, kernel, rusage, add_force, add-force, "struct proc *",
106097055e2SEdward Tomasz Napierala     "int", "uint64_t");
107097055e2SEdward Tomasz Napierala SDT_PROBE_DEFINE3(racct, kernel, rusage, set, set, "struct proc *", "int",
108097055e2SEdward Tomasz Napierala     "uint64_t");
109097055e2SEdward Tomasz Napierala SDT_PROBE_DEFINE3(racct, kernel, rusage, set_failure, set-failure,
110097055e2SEdward Tomasz Napierala     "struct proc *", "int", "uint64_t");
111097055e2SEdward Tomasz Napierala SDT_PROBE_DEFINE3(racct, kernel, rusage, sub, sub, "struct proc *", "int",
112097055e2SEdward Tomasz Napierala     "uint64_t");
113097055e2SEdward Tomasz Napierala SDT_PROBE_DEFINE3(racct, kernel, rusage, sub_cred, sub-cred, "struct ucred *",
114097055e2SEdward Tomasz Napierala     "int", "uint64_t");
115097055e2SEdward Tomasz Napierala SDT_PROBE_DEFINE1(racct, kernel, racct, create, create, "struct racct *");
116097055e2SEdward Tomasz Napierala SDT_PROBE_DEFINE1(racct, kernel, racct, destroy, destroy, "struct racct *");
117097055e2SEdward Tomasz Napierala SDT_PROBE_DEFINE2(racct, kernel, racct, join, join, "struct racct *",
118097055e2SEdward Tomasz Napierala     "struct racct *");
119097055e2SEdward Tomasz Napierala SDT_PROBE_DEFINE2(racct, kernel, racct, join_failure, join-failure,
120097055e2SEdward Tomasz Napierala     "struct racct *", "struct racct *");
121097055e2SEdward Tomasz Napierala SDT_PROBE_DEFINE2(racct, kernel, racct, leave, leave, "struct racct *",
122097055e2SEdward Tomasz Napierala     "struct racct *");
123097055e2SEdward Tomasz Napierala 
124097055e2SEdward Tomasz Napierala int racct_types[] = {
125097055e2SEdward Tomasz Napierala 	[RACCT_CPU] =
12685a2f1b4SEdward Tomasz Napierala 		RACCT_IN_MILLIONS,
127097055e2SEdward Tomasz Napierala 	[RACCT_DATA] =
128097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
129097055e2SEdward Tomasz Napierala 	[RACCT_STACK] =
130097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
131097055e2SEdward Tomasz Napierala 	[RACCT_CORE] =
132097055e2SEdward Tomasz Napierala 		RACCT_DENIABLE,
133097055e2SEdward Tomasz Napierala 	[RACCT_RSS] =
134097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE,
135097055e2SEdward Tomasz Napierala 	[RACCT_MEMLOCK] =
136097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_DENIABLE,
137097055e2SEdward Tomasz Napierala 	[RACCT_NPROC] =
138097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_DENIABLE,
139097055e2SEdward Tomasz Napierala 	[RACCT_NOFILE] =
140097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
141097055e2SEdward Tomasz Napierala 	[RACCT_VMEM] =
142097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
143097055e2SEdward Tomasz Napierala 	[RACCT_NPTS] =
144097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
145097055e2SEdward Tomasz Napierala 	[RACCT_SWAP] =
146097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
147097055e2SEdward Tomasz Napierala 	[RACCT_NTHR] =
148097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_DENIABLE,
149097055e2SEdward Tomasz Napierala 	[RACCT_MSGQQUEUED] =
150097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
151097055e2SEdward Tomasz Napierala 	[RACCT_MSGQSIZE] =
152097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
153097055e2SEdward Tomasz Napierala 	[RACCT_NMSGQ] =
154097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
155097055e2SEdward Tomasz Napierala 	[RACCT_NSEM] =
156097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
157097055e2SEdward Tomasz Napierala 	[RACCT_NSEMOP] =
158097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
159097055e2SEdward Tomasz Napierala 	[RACCT_NSHM] =
160097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
161097055e2SEdward Tomasz Napierala 	[RACCT_SHMSIZE] =
162097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
163097055e2SEdward Tomasz Napierala 	[RACCT_WALLCLOCK] =
16436af9869SEdward Tomasz Napierala 		RACCT_IN_MILLIONS,
16536af9869SEdward Tomasz Napierala 	[RACCT_PCTCPU] =
16636af9869SEdward Tomasz Napierala 		RACCT_DECAYING | RACCT_DENIABLE | RACCT_IN_MILLIONS };
16736af9869SEdward Tomasz Napierala 
16836af9869SEdward Tomasz Napierala static const fixpt_t RACCT_DECAY_FACTOR = 0.3 * FSCALE;
16936af9869SEdward Tomasz Napierala 
17036af9869SEdward Tomasz Napierala #ifdef SCHED_4BSD
17136af9869SEdward Tomasz Napierala /*
17236af9869SEdward Tomasz Napierala  * Contains intermediate values for %cpu calculations to avoid using floating
17336af9869SEdward Tomasz Napierala  * point in the kernel.
17436af9869SEdward Tomasz Napierala  * ccpu_exp[k] = FSCALE * (ccpu/FSCALE)^k = FSCALE * exp(-k/20)
17536af9869SEdward Tomasz Napierala  * It is needed only for the 4BSD scheduler, because in ULE, the ccpu equals to
17636af9869SEdward Tomasz Napierala  * zero so the calculations are more straightforward.
17736af9869SEdward Tomasz Napierala  */
17836af9869SEdward Tomasz Napierala fixpt_t ccpu_exp[] = {
17936af9869SEdward Tomasz Napierala 	[0] = FSCALE * 1,
18036af9869SEdward Tomasz Napierala 	[1] = FSCALE * 0.95122942450071400909,
18136af9869SEdward Tomasz Napierala 	[2] = FSCALE * 0.90483741803595957316,
18236af9869SEdward Tomasz Napierala 	[3] = FSCALE * 0.86070797642505780722,
18336af9869SEdward Tomasz Napierala 	[4] = FSCALE * 0.81873075307798185866,
18436af9869SEdward Tomasz Napierala 	[5] = FSCALE * 0.77880078307140486824,
18536af9869SEdward Tomasz Napierala 	[6] = FSCALE * 0.74081822068171786606,
18636af9869SEdward Tomasz Napierala 	[7] = FSCALE * 0.70468808971871343435,
18736af9869SEdward Tomasz Napierala 	[8] = FSCALE * 0.67032004603563930074,
18836af9869SEdward Tomasz Napierala 	[9] = FSCALE * 0.63762815162177329314,
18936af9869SEdward Tomasz Napierala 	[10] = FSCALE * 0.60653065971263342360,
19036af9869SEdward Tomasz Napierala 	[11] = FSCALE * 0.57694981038048669531,
19136af9869SEdward Tomasz Napierala 	[12] = FSCALE * 0.54881163609402643262,
19236af9869SEdward Tomasz Napierala 	[13] = FSCALE * 0.52204577676101604789,
19336af9869SEdward Tomasz Napierala 	[14] = FSCALE * 0.49658530379140951470,
19436af9869SEdward Tomasz Napierala 	[15] = FSCALE * 0.47236655274101470713,
19536af9869SEdward Tomasz Napierala 	[16] = FSCALE * 0.44932896411722159143,
19636af9869SEdward Tomasz Napierala 	[17] = FSCALE * 0.42741493194872666992,
19736af9869SEdward Tomasz Napierala 	[18] = FSCALE * 0.40656965974059911188,
19836af9869SEdward Tomasz Napierala 	[19] = FSCALE * 0.38674102345450120691,
19936af9869SEdward Tomasz Napierala 	[20] = FSCALE * 0.36787944117144232159,
20036af9869SEdward Tomasz Napierala 	[21] = FSCALE * 0.34993774911115535467,
20136af9869SEdward Tomasz Napierala 	[22] = FSCALE * 0.33287108369807955328,
20236af9869SEdward Tomasz Napierala 	[23] = FSCALE * 0.31663676937905321821,
20336af9869SEdward Tomasz Napierala 	[24] = FSCALE * 0.30119421191220209664,
20436af9869SEdward Tomasz Napierala 	[25] = FSCALE * 0.28650479686019010032,
20536af9869SEdward Tomasz Napierala 	[26] = FSCALE * 0.27253179303401260312,
20636af9869SEdward Tomasz Napierala 	[27] = FSCALE * 0.25924026064589150757,
20736af9869SEdward Tomasz Napierala 	[28] = FSCALE * 0.24659696394160647693,
20836af9869SEdward Tomasz Napierala 	[29] = FSCALE * 0.23457028809379765313,
20936af9869SEdward Tomasz Napierala 	[30] = FSCALE * 0.22313016014842982893,
21036af9869SEdward Tomasz Napierala 	[31] = FSCALE * 0.21224797382674305771,
21136af9869SEdward Tomasz Napierala 	[32] = FSCALE * 0.20189651799465540848,
21236af9869SEdward Tomasz Napierala 	[33] = FSCALE * 0.19204990862075411423,
21336af9869SEdward Tomasz Napierala 	[34] = FSCALE * 0.18268352405273465022,
21436af9869SEdward Tomasz Napierala 	[35] = FSCALE * 0.17377394345044512668,
21536af9869SEdward Tomasz Napierala 	[36] = FSCALE * 0.16529888822158653829,
21636af9869SEdward Tomasz Napierala 	[37] = FSCALE * 0.15723716631362761621,
21736af9869SEdward Tomasz Napierala 	[38] = FSCALE * 0.14956861922263505264,
21836af9869SEdward Tomasz Napierala 	[39] = FSCALE * 0.14227407158651357185,
21936af9869SEdward Tomasz Napierala 	[40] = FSCALE * 0.13533528323661269189,
22036af9869SEdward Tomasz Napierala 	[41] = FSCALE * 0.12873490358780421886,
22136af9869SEdward Tomasz Napierala 	[42] = FSCALE * 0.12245642825298191021,
22236af9869SEdward Tomasz Napierala 	[43] = FSCALE * 0.11648415777349695786,
22336af9869SEdward Tomasz Napierala 	[44] = FSCALE * 0.11080315836233388333,
22436af9869SEdward Tomasz Napierala 	[45] = FSCALE * 0.10539922456186433678,
22536af9869SEdward Tomasz Napierala 	[46] = FSCALE * 0.10025884372280373372,
22636af9869SEdward Tomasz Napierala 	[47] = FSCALE * 0.09536916221554961888,
22736af9869SEdward Tomasz Napierala 	[48] = FSCALE * 0.09071795328941250337,
22836af9869SEdward Tomasz Napierala 	[49] = FSCALE * 0.08629358649937051097,
22936af9869SEdward Tomasz Napierala 	[50] = FSCALE * 0.08208499862389879516,
23036af9869SEdward Tomasz Napierala 	[51] = FSCALE * 0.07808166600115315231,
23136af9869SEdward Tomasz Napierala 	[52] = FSCALE * 0.07427357821433388042,
23236af9869SEdward Tomasz Napierala 	[53] = FSCALE * 0.07065121306042958674,
23336af9869SEdward Tomasz Napierala 	[54] = FSCALE * 0.06720551273974976512,
23436af9869SEdward Tomasz Napierala 	[55] = FSCALE * 0.06392786120670757270,
23536af9869SEdward Tomasz Napierala 	[56] = FSCALE * 0.06081006262521796499,
23636af9869SEdward Tomasz Napierala 	[57] = FSCALE * 0.05784432087483846296,
23736af9869SEdward Tomasz Napierala 	[58] = FSCALE * 0.05502322005640722902,
23836af9869SEdward Tomasz Napierala 	[59] = FSCALE * 0.05233970594843239308,
23936af9869SEdward Tomasz Napierala 	[60] = FSCALE * 0.04978706836786394297,
24036af9869SEdward Tomasz Napierala 	[61] = FSCALE * 0.04735892439114092119,
24136af9869SEdward Tomasz Napierala 	[62] = FSCALE * 0.04504920239355780606,
24236af9869SEdward Tomasz Napierala 	[63] = FSCALE * 0.04285212686704017991,
24336af9869SEdward Tomasz Napierala 	[64] = FSCALE * 0.04076220397836621516,
24436af9869SEdward Tomasz Napierala 	[65] = FSCALE * 0.03877420783172200988,
24536af9869SEdward Tomasz Napierala 	[66] = FSCALE * 0.03688316740124000544,
24636af9869SEdward Tomasz Napierala 	[67] = FSCALE * 0.03508435410084502588,
24736af9869SEdward Tomasz Napierala 	[68] = FSCALE * 0.03337326996032607948,
24836af9869SEdward Tomasz Napierala 	[69] = FSCALE * 0.03174563637806794323,
24936af9869SEdward Tomasz Napierala 	[70] = FSCALE * 0.03019738342231850073,
25036af9869SEdward Tomasz Napierala 	[71] = FSCALE * 0.02872463965423942912,
25136af9869SEdward Tomasz Napierala 	[72] = FSCALE * 0.02732372244729256080,
25236af9869SEdward Tomasz Napierala 	[73] = FSCALE * 0.02599112877875534358,
25336af9869SEdward Tomasz Napierala 	[74] = FSCALE * 0.02472352647033939120,
25436af9869SEdward Tomasz Napierala 	[75] = FSCALE * 0.02351774585600910823,
25536af9869SEdward Tomasz Napierala 	[76] = FSCALE * 0.02237077185616559577,
25636af9869SEdward Tomasz Napierala 	[77] = FSCALE * 0.02127973643837716938,
25736af9869SEdward Tomasz Napierala 	[78] = FSCALE * 0.02024191144580438847,
25836af9869SEdward Tomasz Napierala 	[79] = FSCALE * 0.01925470177538692429,
25936af9869SEdward Tomasz Napierala 	[80] = FSCALE * 0.01831563888873418029,
26036af9869SEdward Tomasz Napierala 	[81] = FSCALE * 0.01742237463949351138,
26136af9869SEdward Tomasz Napierala 	[82] = FSCALE * 0.01657267540176124754,
26236af9869SEdward Tomasz Napierala 	[83] = FSCALE * 0.01576441648485449082,
26336af9869SEdward Tomasz Napierala 	[84] = FSCALE * 0.01499557682047770621,
26436af9869SEdward Tomasz Napierala 	[85] = FSCALE * 0.01426423390899925527,
26536af9869SEdward Tomasz Napierala 	[86] = FSCALE * 0.01356855901220093175,
26636af9869SEdward Tomasz Napierala 	[87] = FSCALE * 0.01290681258047986886,
26736af9869SEdward Tomasz Napierala 	[88] = FSCALE * 0.01227733990306844117,
26836af9869SEdward Tomasz Napierala 	[89] = FSCALE * 0.01167856697039544521,
26936af9869SEdward Tomasz Napierala 	[90] = FSCALE * 0.01110899653824230649,
27036af9869SEdward Tomasz Napierala 	[91] = FSCALE * 0.01056720438385265337,
27136af9869SEdward Tomasz Napierala 	[92] = FSCALE * 0.01005183574463358164,
27236af9869SEdward Tomasz Napierala 	[93] = FSCALE * 0.00956160193054350793,
27336af9869SEdward Tomasz Napierala 	[94] = FSCALE * 0.00909527710169581709,
27436af9869SEdward Tomasz Napierala 	[95] = FSCALE * 0.00865169520312063417,
27536af9869SEdward Tomasz Napierala 	[96] = FSCALE * 0.00822974704902002884,
27636af9869SEdward Tomasz Napierala 	[97] = FSCALE * 0.00782837754922577143,
27736af9869SEdward Tomasz Napierala 	[98] = FSCALE * 0.00744658307092434051,
27836af9869SEdward Tomasz Napierala 	[99] = FSCALE * 0.00708340892905212004,
27936af9869SEdward Tomasz Napierala 	[100] = FSCALE * 0.00673794699908546709,
28036af9869SEdward Tomasz Napierala 	[101] = FSCALE * 0.00640933344625638184,
28136af9869SEdward Tomasz Napierala 	[102] = FSCALE * 0.00609674656551563610,
28236af9869SEdward Tomasz Napierala 	[103] = FSCALE * 0.00579940472684214321,
28336af9869SEdward Tomasz Napierala 	[104] = FSCALE * 0.00551656442076077241,
28436af9869SEdward Tomasz Napierala 	[105] = FSCALE * 0.00524751839918138427,
28536af9869SEdward Tomasz Napierala 	[106] = FSCALE * 0.00499159390691021621,
28636af9869SEdward Tomasz Napierala 	[107] = FSCALE * 0.00474815099941147558,
28736af9869SEdward Tomasz Napierala 	[108] = FSCALE * 0.00451658094261266798,
28836af9869SEdward Tomasz Napierala 	[109] = FSCALE * 0.00429630469075234057,
28936af9869SEdward Tomasz Napierala 	[110] = FSCALE * 0.00408677143846406699,
29036af9869SEdward Tomasz Napierala };
29136af9869SEdward Tomasz Napierala #endif
29236af9869SEdward Tomasz Napierala 
29336af9869SEdward Tomasz Napierala #define	CCPU_EXP_MAX	110
29436af9869SEdward Tomasz Napierala 
29536af9869SEdward Tomasz Napierala /*
29636af9869SEdward Tomasz Napierala  * This function is analogical to the getpcpu() function in the ps(1) command.
29736af9869SEdward Tomasz Napierala  * They should both calculate in the same way so that the racct %cpu
29836af9869SEdward Tomasz Napierala  * calculations are consistent with the values showed by the ps(1) tool.
29936af9869SEdward Tomasz Napierala  * The calculations are more complex in the 4BSD scheduler because of the value
30036af9869SEdward Tomasz Napierala  * of the ccpu variable.  In ULE it is defined to be zero which saves us some
30136af9869SEdward Tomasz Napierala  * work.
30236af9869SEdward Tomasz Napierala  */
30336af9869SEdward Tomasz Napierala static uint64_t
30436af9869SEdward Tomasz Napierala racct_getpcpu(struct proc *p, u_int pcpu)
30536af9869SEdward Tomasz Napierala {
30636af9869SEdward Tomasz Napierala 	u_int swtime;
30736af9869SEdward Tomasz Napierala #ifdef SCHED_4BSD
30836af9869SEdward Tomasz Napierala 	fixpt_t pctcpu, pctcpu_next;
30936af9869SEdward Tomasz Napierala #endif
31036af9869SEdward Tomasz Napierala #ifdef SMP
31136af9869SEdward Tomasz Napierala 	struct pcpu *pc;
31236af9869SEdward Tomasz Napierala 	int found;
31336af9869SEdward Tomasz Napierala #endif
31436af9869SEdward Tomasz Napierala 	fixpt_t p_pctcpu;
31536af9869SEdward Tomasz Napierala 	struct thread *td;
31636af9869SEdward Tomasz Napierala 
31736af9869SEdward Tomasz Napierala 	/*
31836af9869SEdward Tomasz Napierala 	 * If the process is swapped out, we count its %cpu usage as zero.
31936af9869SEdward Tomasz Napierala 	 * This behaviour is consistent with the userland ps(1) tool.
32036af9869SEdward Tomasz Napierala 	 */
32136af9869SEdward Tomasz Napierala 	if ((p->p_flag & P_INMEM) == 0)
32236af9869SEdward Tomasz Napierala 		return (0);
32336af9869SEdward Tomasz Napierala 	swtime = (ticks - p->p_swtick) / hz;
32436af9869SEdward Tomasz Napierala 
32536af9869SEdward Tomasz Napierala 	/*
32636af9869SEdward Tomasz Napierala 	 * For short-lived processes, the sched_pctcpu() returns small
32736af9869SEdward Tomasz Napierala 	 * values even for cpu intensive processes.  Therefore we use
32836af9869SEdward Tomasz Napierala 	 * our own estimate in this case.
32936af9869SEdward Tomasz Napierala 	 */
33036af9869SEdward Tomasz Napierala 	if (swtime < RACCT_PCPU_SECS)
33136af9869SEdward Tomasz Napierala 		return (pcpu);
33236af9869SEdward Tomasz Napierala 
33336af9869SEdward Tomasz Napierala 	p_pctcpu = 0;
33436af9869SEdward Tomasz Napierala 	FOREACH_THREAD_IN_PROC(p, td) {
33536af9869SEdward Tomasz Napierala 		if (td == PCPU_GET(idlethread))
33636af9869SEdward Tomasz Napierala 			continue;
33736af9869SEdward Tomasz Napierala #ifdef SMP
33836af9869SEdward Tomasz Napierala 		found = 0;
33936af9869SEdward Tomasz Napierala 		STAILQ_FOREACH(pc, &cpuhead, pc_allcpu) {
34036af9869SEdward Tomasz Napierala 			if (td == pc->pc_idlethread) {
34136af9869SEdward Tomasz Napierala 				found = 1;
34236af9869SEdward Tomasz Napierala 				break;
34336af9869SEdward Tomasz Napierala 			}
34436af9869SEdward Tomasz Napierala 		}
34536af9869SEdward Tomasz Napierala 		if (found)
34636af9869SEdward Tomasz Napierala 			continue;
34736af9869SEdward Tomasz Napierala #endif
34836af9869SEdward Tomasz Napierala 		thread_lock(td);
34936af9869SEdward Tomasz Napierala #ifdef SCHED_4BSD
35036af9869SEdward Tomasz Napierala 		pctcpu = sched_pctcpu(td);
35136af9869SEdward Tomasz Napierala 		/* Count also the yet unfinished second. */
35236af9869SEdward Tomasz Napierala 		pctcpu_next = (pctcpu * ccpu_exp[1]) >> FSHIFT;
35336af9869SEdward Tomasz Napierala 		pctcpu_next += sched_pctcpu_delta(td);
35436af9869SEdward Tomasz Napierala 		p_pctcpu += max(pctcpu, pctcpu_next);
35536af9869SEdward Tomasz Napierala #else
35636af9869SEdward Tomasz Napierala 		/*
35736af9869SEdward Tomasz Napierala 		 * In ULE the %cpu statistics are updated on every
35836af9869SEdward Tomasz Napierala 		 * sched_pctcpu() call.  So special calculations to
35936af9869SEdward Tomasz Napierala 		 * account for the latest (unfinished) second are
36036af9869SEdward Tomasz Napierala 		 * not needed.
36136af9869SEdward Tomasz Napierala 		 */
36236af9869SEdward Tomasz Napierala 		p_pctcpu += sched_pctcpu(td);
36336af9869SEdward Tomasz Napierala #endif
36436af9869SEdward Tomasz Napierala 		thread_unlock(td);
36536af9869SEdward Tomasz Napierala 	}
36636af9869SEdward Tomasz Napierala 
36736af9869SEdward Tomasz Napierala #ifdef SCHED_4BSD
36836af9869SEdward Tomasz Napierala 	if (swtime <= CCPU_EXP_MAX)
36936af9869SEdward Tomasz Napierala 		return ((100 * (uint64_t)p_pctcpu * 1000000) /
37036af9869SEdward Tomasz Napierala 		    (FSCALE - ccpu_exp[swtime]));
37136af9869SEdward Tomasz Napierala #endif
37236af9869SEdward Tomasz Napierala 
37336af9869SEdward Tomasz Napierala 	return ((100 * (uint64_t)p_pctcpu * 1000000) / FSCALE);
37436af9869SEdward Tomasz Napierala }
375097055e2SEdward Tomasz Napierala 
376097055e2SEdward Tomasz Napierala static void
377097055e2SEdward Tomasz Napierala racct_add_racct(struct racct *dest, const struct racct *src)
378097055e2SEdward Tomasz Napierala {
379097055e2SEdward Tomasz Napierala 	int i;
380097055e2SEdward Tomasz Napierala 
381097055e2SEdward Tomasz Napierala 	mtx_assert(&racct_lock, MA_OWNED);
382097055e2SEdward Tomasz Napierala 
383097055e2SEdward Tomasz Napierala 	/*
384097055e2SEdward Tomasz Napierala 	 * Update resource usage in dest.
385097055e2SEdward Tomasz Napierala 	 */
386097055e2SEdward Tomasz Napierala 	for (i = 0; i <= RACCT_MAX; i++) {
387097055e2SEdward Tomasz Napierala 		KASSERT(dest->r_resources[i] >= 0,
388baf85d0aSEdward Tomasz Napierala 		    ("%s: resource %d propagation meltdown: dest < 0",
389baf85d0aSEdward Tomasz Napierala 		    __func__, i));
390097055e2SEdward Tomasz Napierala 		KASSERT(src->r_resources[i] >= 0,
391baf85d0aSEdward Tomasz Napierala 		    ("%s: resource %d propagation meltdown: src < 0",
392baf85d0aSEdward Tomasz Napierala 		    __func__, i));
393097055e2SEdward Tomasz Napierala 		dest->r_resources[i] += src->r_resources[i];
394097055e2SEdward Tomasz Napierala 	}
395097055e2SEdward Tomasz Napierala }
396097055e2SEdward Tomasz Napierala 
397097055e2SEdward Tomasz Napierala static void
398097055e2SEdward Tomasz Napierala racct_sub_racct(struct racct *dest, const struct racct *src)
399097055e2SEdward Tomasz Napierala {
400097055e2SEdward Tomasz Napierala 	int i;
401097055e2SEdward Tomasz Napierala 
402097055e2SEdward Tomasz Napierala 	mtx_assert(&racct_lock, MA_OWNED);
403097055e2SEdward Tomasz Napierala 
404097055e2SEdward Tomasz Napierala 	/*
405097055e2SEdward Tomasz Napierala 	 * Update resource usage in dest.
406097055e2SEdward Tomasz Napierala 	 */
407097055e2SEdward Tomasz Napierala 	for (i = 0; i <= RACCT_MAX; i++) {
40884c9193bSEdward Tomasz Napierala 		if (!RACCT_IS_SLOPPY(i) && !RACCT_IS_DECAYING(i)) {
409097055e2SEdward Tomasz Napierala 			KASSERT(dest->r_resources[i] >= 0,
410baf85d0aSEdward Tomasz Napierala 			    ("%s: resource %d propagation meltdown: dest < 0",
411baf85d0aSEdward Tomasz Napierala 			    __func__, i));
412097055e2SEdward Tomasz Napierala 			KASSERT(src->r_resources[i] >= 0,
413baf85d0aSEdward Tomasz Napierala 			    ("%s: resource %d propagation meltdown: src < 0",
414baf85d0aSEdward Tomasz Napierala 			    __func__, i));
415097055e2SEdward Tomasz Napierala 			KASSERT(src->r_resources[i] <= dest->r_resources[i],
416baf85d0aSEdward Tomasz Napierala 			    ("%s: resource %d propagation meltdown: src > dest",
417baf85d0aSEdward Tomasz Napierala 			    __func__, i));
418097055e2SEdward Tomasz Napierala 		}
41936af9869SEdward Tomasz Napierala 		if (RACCT_CAN_DROP(i)) {
420097055e2SEdward Tomasz Napierala 			dest->r_resources[i] -= src->r_resources[i];
421097055e2SEdward Tomasz Napierala 			if (dest->r_resources[i] < 0) {
42284c9193bSEdward Tomasz Napierala 				KASSERT(RACCT_IS_SLOPPY(i) ||
42384c9193bSEdward Tomasz Napierala 				    RACCT_IS_DECAYING(i),
424baf85d0aSEdward Tomasz Napierala 				    ("%s: resource %d usage < 0", __func__, i));
425097055e2SEdward Tomasz Napierala 				dest->r_resources[i] = 0;
426097055e2SEdward Tomasz Napierala 			}
427097055e2SEdward Tomasz Napierala 		}
428097055e2SEdward Tomasz Napierala 	}
429097055e2SEdward Tomasz Napierala }
430097055e2SEdward Tomasz Napierala 
431097055e2SEdward Tomasz Napierala void
432097055e2SEdward Tomasz Napierala racct_create(struct racct **racctp)
433097055e2SEdward Tomasz Napierala {
434097055e2SEdward Tomasz Napierala 
435097055e2SEdward Tomasz Napierala 	SDT_PROBE(racct, kernel, racct, create, racctp, 0, 0, 0, 0);
436097055e2SEdward Tomasz Napierala 
437097055e2SEdward Tomasz Napierala 	KASSERT(*racctp == NULL, ("racct already allocated"));
438097055e2SEdward Tomasz Napierala 
439097055e2SEdward Tomasz Napierala 	*racctp = uma_zalloc(racct_zone, M_WAITOK | M_ZERO);
440097055e2SEdward Tomasz Napierala }
441097055e2SEdward Tomasz Napierala 
442097055e2SEdward Tomasz Napierala static void
443097055e2SEdward Tomasz Napierala racct_destroy_locked(struct racct **racctp)
444097055e2SEdward Tomasz Napierala {
445097055e2SEdward Tomasz Napierala 	int i;
446097055e2SEdward Tomasz Napierala 	struct racct *racct;
447097055e2SEdward Tomasz Napierala 
448097055e2SEdward Tomasz Napierala 	SDT_PROBE(racct, kernel, racct, destroy, racctp, 0, 0, 0, 0);
449097055e2SEdward Tomasz Napierala 
450097055e2SEdward Tomasz Napierala 	mtx_assert(&racct_lock, MA_OWNED);
451097055e2SEdward Tomasz Napierala 	KASSERT(racctp != NULL, ("NULL racctp"));
452097055e2SEdward Tomasz Napierala 	KASSERT(*racctp != NULL, ("NULL racct"));
453097055e2SEdward Tomasz Napierala 
454097055e2SEdward Tomasz Napierala 	racct = *racctp;
455097055e2SEdward Tomasz Napierala 
456097055e2SEdward Tomasz Napierala 	for (i = 0; i <= RACCT_MAX; i++) {
4574fe84775SEdward Tomasz Napierala 		if (RACCT_IS_SLOPPY(i))
458097055e2SEdward Tomasz Napierala 			continue;
4594fe84775SEdward Tomasz Napierala 		if (!RACCT_IS_RECLAIMABLE(i))
460097055e2SEdward Tomasz Napierala 			continue;
461097055e2SEdward Tomasz Napierala 		KASSERT(racct->r_resources[i] == 0,
462097055e2SEdward Tomasz Napierala 		    ("destroying non-empty racct: "
463097055e2SEdward Tomasz Napierala 		    "%ju allocated for resource %d\n",
464097055e2SEdward Tomasz Napierala 		    racct->r_resources[i], i));
465097055e2SEdward Tomasz Napierala 	}
466097055e2SEdward Tomasz Napierala 	uma_zfree(racct_zone, racct);
467097055e2SEdward Tomasz Napierala 	*racctp = NULL;
468097055e2SEdward Tomasz Napierala }
469097055e2SEdward Tomasz Napierala 
470097055e2SEdward Tomasz Napierala void
471097055e2SEdward Tomasz Napierala racct_destroy(struct racct **racct)
472097055e2SEdward Tomasz Napierala {
473097055e2SEdward Tomasz Napierala 
474097055e2SEdward Tomasz Napierala 	mtx_lock(&racct_lock);
475097055e2SEdward Tomasz Napierala 	racct_destroy_locked(racct);
476097055e2SEdward Tomasz Napierala 	mtx_unlock(&racct_lock);
477097055e2SEdward Tomasz Napierala }
478097055e2SEdward Tomasz Napierala 
479097055e2SEdward Tomasz Napierala /*
480097055e2SEdward Tomasz Napierala  * Increase consumption of 'resource' by 'amount' for 'racct'
481097055e2SEdward Tomasz Napierala  * and all its parents.  Differently from other cases, 'amount' here
482097055e2SEdward Tomasz Napierala  * may be less than zero.
483097055e2SEdward Tomasz Napierala  */
484097055e2SEdward Tomasz Napierala static void
485097055e2SEdward Tomasz Napierala racct_alloc_resource(struct racct *racct, int resource,
486097055e2SEdward Tomasz Napierala     uint64_t amount)
487097055e2SEdward Tomasz Napierala {
488097055e2SEdward Tomasz Napierala 
489097055e2SEdward Tomasz Napierala 	mtx_assert(&racct_lock, MA_OWNED);
490097055e2SEdward Tomasz Napierala 	KASSERT(racct != NULL, ("NULL racct"));
491097055e2SEdward Tomasz Napierala 
492097055e2SEdward Tomasz Napierala 	racct->r_resources[resource] += amount;
493097055e2SEdward Tomasz Napierala 	if (racct->r_resources[resource] < 0) {
49436af9869SEdward Tomasz Napierala 		KASSERT(RACCT_IS_SLOPPY(resource) || RACCT_IS_DECAYING(resource),
495baf85d0aSEdward Tomasz Napierala 		    ("%s: resource %d usage < 0", __func__, resource));
496097055e2SEdward Tomasz Napierala 		racct->r_resources[resource] = 0;
497097055e2SEdward Tomasz Napierala 	}
49836af9869SEdward Tomasz Napierala 
49936af9869SEdward Tomasz Napierala 	/*
50036af9869SEdward Tomasz Napierala 	 * There are some cases where the racct %cpu resource would grow
50136af9869SEdward Tomasz Napierala 	 * beyond 100%.
50236af9869SEdward Tomasz Napierala 	 * For example in racct_proc_exit() we add the process %cpu usage
50336af9869SEdward Tomasz Napierala 	 * to the ucred racct containers.  If too many processes terminated
50436af9869SEdward Tomasz Napierala 	 * in a short time span, the ucred %cpu resource could grow too much.
50536af9869SEdward Tomasz Napierala 	 * Also, the 4BSD scheduler sometimes returns for a thread more than
50636af9869SEdward Tomasz Napierala 	 * 100% cpu usage.  So we set a boundary here to 100%.
50736af9869SEdward Tomasz Napierala 	 */
50836af9869SEdward Tomasz Napierala 	if ((resource == RACCT_PCTCPU) &&
50936af9869SEdward Tomasz Napierala 	    (racct->r_resources[RACCT_PCTCPU] > 100 * 1000000))
51036af9869SEdward Tomasz Napierala 		racct->r_resources[RACCT_PCTCPU] = 100 * 1000000;
511097055e2SEdward Tomasz Napierala }
512097055e2SEdward Tomasz Napierala 
5132d8696d1SEdward Tomasz Napierala static int
5142d8696d1SEdward Tomasz Napierala racct_add_locked(struct proc *p, int resource, uint64_t amount)
515097055e2SEdward Tomasz Napierala {
516097055e2SEdward Tomasz Napierala #ifdef RCTL
517097055e2SEdward Tomasz Napierala 	int error;
518097055e2SEdward Tomasz Napierala #endif
519097055e2SEdward Tomasz Napierala 
520097055e2SEdward Tomasz Napierala 	SDT_PROBE(racct, kernel, rusage, add, p, resource, amount, 0, 0);
521097055e2SEdward Tomasz Napierala 
522097055e2SEdward Tomasz Napierala 	/*
523097055e2SEdward Tomasz Napierala 	 * We need proc lock to dereference p->p_ucred.
524097055e2SEdward Tomasz Napierala 	 */
525097055e2SEdward Tomasz Napierala 	PROC_LOCK_ASSERT(p, MA_OWNED);
526097055e2SEdward Tomasz Napierala 
527097055e2SEdward Tomasz Napierala #ifdef RCTL
528097055e2SEdward Tomasz Napierala 	error = rctl_enforce(p, resource, amount);
5294fe84775SEdward Tomasz Napierala 	if (error && RACCT_IS_DENIABLE(resource)) {
530097055e2SEdward Tomasz Napierala 		SDT_PROBE(racct, kernel, rusage, add_failure, p, resource,
531097055e2SEdward Tomasz Napierala 		    amount, 0, 0);
532097055e2SEdward Tomasz Napierala 		return (error);
533097055e2SEdward Tomasz Napierala 	}
534097055e2SEdward Tomasz Napierala #endif
535097055e2SEdward Tomasz Napierala 	racct_alloc_resource(p->p_racct, resource, amount);
536097055e2SEdward Tomasz Napierala 	racct_add_cred_locked(p->p_ucred, resource, amount);
537097055e2SEdward Tomasz Napierala 
538097055e2SEdward Tomasz Napierala 	return (0);
539097055e2SEdward Tomasz Napierala }
540097055e2SEdward Tomasz Napierala 
5412d8696d1SEdward Tomasz Napierala /*
5422d8696d1SEdward Tomasz Napierala  * Increase allocation of 'resource' by 'amount' for process 'p'.
5432d8696d1SEdward Tomasz Napierala  * Return 0 if it's below limits, or errno, if it's not.
5442d8696d1SEdward Tomasz Napierala  */
5452d8696d1SEdward Tomasz Napierala int
5462d8696d1SEdward Tomasz Napierala racct_add(struct proc *p, int resource, uint64_t amount)
5472d8696d1SEdward Tomasz Napierala {
5482d8696d1SEdward Tomasz Napierala 	int error;
5492d8696d1SEdward Tomasz Napierala 
5502d8696d1SEdward Tomasz Napierala 	mtx_lock(&racct_lock);
5512d8696d1SEdward Tomasz Napierala 	error = racct_add_locked(p, resource, amount);
5522d8696d1SEdward Tomasz Napierala 	mtx_unlock(&racct_lock);
5532d8696d1SEdward Tomasz Napierala 	return (error);
5542d8696d1SEdward Tomasz Napierala }
5552d8696d1SEdward Tomasz Napierala 
556097055e2SEdward Tomasz Napierala static void
557097055e2SEdward Tomasz Napierala racct_add_cred_locked(struct ucred *cred, int resource, uint64_t amount)
558097055e2SEdward Tomasz Napierala {
559097055e2SEdward Tomasz Napierala 	struct prison *pr;
560097055e2SEdward Tomasz Napierala 
561097055e2SEdward Tomasz Napierala 	SDT_PROBE(racct, kernel, rusage, add_cred, cred, resource, amount,
562097055e2SEdward Tomasz Napierala 	    0, 0);
563097055e2SEdward Tomasz Napierala 
564097055e2SEdward Tomasz Napierala 	racct_alloc_resource(cred->cr_ruidinfo->ui_racct, resource, amount);
565097055e2SEdward Tomasz Napierala 	for (pr = cred->cr_prison; pr != NULL; pr = pr->pr_parent)
566a7ad07bfSEdward Tomasz Napierala 		racct_alloc_resource(pr->pr_prison_racct->prr_racct, resource,
567a7ad07bfSEdward Tomasz Napierala 		    amount);
568097055e2SEdward Tomasz Napierala 	racct_alloc_resource(cred->cr_loginclass->lc_racct, resource, amount);
569097055e2SEdward Tomasz Napierala }
570097055e2SEdward Tomasz Napierala 
571097055e2SEdward Tomasz Napierala /*
572097055e2SEdward Tomasz Napierala  * Increase allocation of 'resource' by 'amount' for credential 'cred'.
573097055e2SEdward Tomasz Napierala  * Doesn't check for limits and never fails.
574097055e2SEdward Tomasz Napierala  *
575097055e2SEdward Tomasz Napierala  * XXX: Shouldn't this ever return an error?
576097055e2SEdward Tomasz Napierala  */
577097055e2SEdward Tomasz Napierala void
578097055e2SEdward Tomasz Napierala racct_add_cred(struct ucred *cred, int resource, uint64_t amount)
579097055e2SEdward Tomasz Napierala {
580097055e2SEdward Tomasz Napierala 
581097055e2SEdward Tomasz Napierala 	mtx_lock(&racct_lock);
582097055e2SEdward Tomasz Napierala 	racct_add_cred_locked(cred, resource, amount);
583097055e2SEdward Tomasz Napierala 	mtx_unlock(&racct_lock);
584097055e2SEdward Tomasz Napierala }
585097055e2SEdward Tomasz Napierala 
586097055e2SEdward Tomasz Napierala /*
587097055e2SEdward Tomasz Napierala  * Increase allocation of 'resource' by 'amount' for process 'p'.
588097055e2SEdward Tomasz Napierala  * Doesn't check for limits and never fails.
589097055e2SEdward Tomasz Napierala  */
590097055e2SEdward Tomasz Napierala void
591097055e2SEdward Tomasz Napierala racct_add_force(struct proc *p, int resource, uint64_t amount)
592097055e2SEdward Tomasz Napierala {
593097055e2SEdward Tomasz Napierala 
594097055e2SEdward Tomasz Napierala 	SDT_PROBE(racct, kernel, rusage, add_force, p, resource, amount, 0, 0);
595097055e2SEdward Tomasz Napierala 
596097055e2SEdward Tomasz Napierala 	/*
597097055e2SEdward Tomasz Napierala 	 * We need proc lock to dereference p->p_ucred.
598097055e2SEdward Tomasz Napierala 	 */
599097055e2SEdward Tomasz Napierala 	PROC_LOCK_ASSERT(p, MA_OWNED);
600097055e2SEdward Tomasz Napierala 
601097055e2SEdward Tomasz Napierala 	mtx_lock(&racct_lock);
602097055e2SEdward Tomasz Napierala 	racct_alloc_resource(p->p_racct, resource, amount);
603097055e2SEdward Tomasz Napierala 	mtx_unlock(&racct_lock);
604097055e2SEdward Tomasz Napierala 	racct_add_cred(p->p_ucred, resource, amount);
605097055e2SEdward Tomasz Napierala }
606097055e2SEdward Tomasz Napierala 
607097055e2SEdward Tomasz Napierala static int
608097055e2SEdward Tomasz Napierala racct_set_locked(struct proc *p, int resource, uint64_t amount)
609097055e2SEdward Tomasz Napierala {
61036af9869SEdward Tomasz Napierala 	int64_t old_amount, decayed_amount;
61136af9869SEdward Tomasz Napierala 	int64_t diff_proc, diff_cred;
612097055e2SEdward Tomasz Napierala #ifdef RCTL
613097055e2SEdward Tomasz Napierala 	int error;
614097055e2SEdward Tomasz Napierala #endif
615097055e2SEdward Tomasz Napierala 
616097055e2SEdward Tomasz Napierala 	SDT_PROBE(racct, kernel, rusage, set, p, resource, amount, 0, 0);
617097055e2SEdward Tomasz Napierala 
618097055e2SEdward Tomasz Napierala 	/*
619097055e2SEdward Tomasz Napierala 	 * We need proc lock to dereference p->p_ucred.
620097055e2SEdward Tomasz Napierala 	 */
621097055e2SEdward Tomasz Napierala 	PROC_LOCK_ASSERT(p, MA_OWNED);
622097055e2SEdward Tomasz Napierala 
62336af9869SEdward Tomasz Napierala 	old_amount = p->p_racct->r_resources[resource];
62436af9869SEdward Tomasz Napierala 	/*
62536af9869SEdward Tomasz Napierala 	 * The diffs may be negative.
62636af9869SEdward Tomasz Napierala 	 */
62736af9869SEdward Tomasz Napierala 	diff_proc = amount - old_amount;
62836af9869SEdward Tomasz Napierala 	if (RACCT_IS_DECAYING(resource)) {
62936af9869SEdward Tomasz Napierala 		/*
63036af9869SEdward Tomasz Napierala 		 * Resources in per-credential racct containers may decay.
63136af9869SEdward Tomasz Napierala 		 * If this is the case, we need to calculate the difference
63236af9869SEdward Tomasz Napierala 		 * between the new amount and the proportional value of the
63336af9869SEdward Tomasz Napierala 		 * old amount that has decayed in the ucred racct containers.
63436af9869SEdward Tomasz Napierala 		 */
63536af9869SEdward Tomasz Napierala 		decayed_amount = old_amount * RACCT_DECAY_FACTOR / FSCALE;
63636af9869SEdward Tomasz Napierala 		diff_cred = amount - decayed_amount;
63736af9869SEdward Tomasz Napierala 	} else
63836af9869SEdward Tomasz Napierala 		diff_cred = diff_proc;
639097055e2SEdward Tomasz Napierala #ifdef notyet
64036af9869SEdward Tomasz Napierala 	KASSERT(diff_proc >= 0 || RACCT_CAN_DROP(resource),
641baf85d0aSEdward Tomasz Napierala 	    ("%s: usage of non-droppable resource %d dropping", __func__,
642097055e2SEdward Tomasz Napierala 	     resource));
643097055e2SEdward Tomasz Napierala #endif
644097055e2SEdward Tomasz Napierala #ifdef RCTL
64536af9869SEdward Tomasz Napierala 	if (diff_proc > 0) {
64636af9869SEdward Tomasz Napierala 		error = rctl_enforce(p, resource, diff_proc);
6474fe84775SEdward Tomasz Napierala 		if (error && RACCT_IS_DENIABLE(resource)) {
648097055e2SEdward Tomasz Napierala 			SDT_PROBE(racct, kernel, rusage, set_failure, p,
649097055e2SEdward Tomasz Napierala 			    resource, amount, 0, 0);
650097055e2SEdward Tomasz Napierala 			return (error);
651097055e2SEdward Tomasz Napierala 		}
652097055e2SEdward Tomasz Napierala 	}
653097055e2SEdward Tomasz Napierala #endif
65436af9869SEdward Tomasz Napierala 	racct_alloc_resource(p->p_racct, resource, diff_proc);
65536af9869SEdward Tomasz Napierala 	if (diff_cred > 0)
65636af9869SEdward Tomasz Napierala 		racct_add_cred_locked(p->p_ucred, resource, diff_cred);
65736af9869SEdward Tomasz Napierala 	else if (diff_cred < 0)
65836af9869SEdward Tomasz Napierala 		racct_sub_cred_locked(p->p_ucred, resource, -diff_cred);
659097055e2SEdward Tomasz Napierala 
660097055e2SEdward Tomasz Napierala 	return (0);
661097055e2SEdward Tomasz Napierala }
662097055e2SEdward Tomasz Napierala 
663097055e2SEdward Tomasz Napierala /*
664097055e2SEdward Tomasz Napierala  * Set allocation of 'resource' to 'amount' for process 'p'.
665097055e2SEdward Tomasz Napierala  * Return 0 if it's below limits, or errno, if it's not.
666097055e2SEdward Tomasz Napierala  *
667097055e2SEdward Tomasz Napierala  * Note that decreasing the allocation always returns 0,
668097055e2SEdward Tomasz Napierala  * even if it's above the limit.
669097055e2SEdward Tomasz Napierala  */
670097055e2SEdward Tomasz Napierala int
671097055e2SEdward Tomasz Napierala racct_set(struct proc *p, int resource, uint64_t amount)
672097055e2SEdward Tomasz Napierala {
673097055e2SEdward Tomasz Napierala 	int error;
674097055e2SEdward Tomasz Napierala 
675097055e2SEdward Tomasz Napierala 	mtx_lock(&racct_lock);
676097055e2SEdward Tomasz Napierala 	error = racct_set_locked(p, resource, amount);
677097055e2SEdward Tomasz Napierala 	mtx_unlock(&racct_lock);
678097055e2SEdward Tomasz Napierala 	return (error);
679097055e2SEdward Tomasz Napierala }
680097055e2SEdward Tomasz Napierala 
68136af9869SEdward Tomasz Napierala static void
68236af9869SEdward Tomasz Napierala racct_set_force_locked(struct proc *p, int resource, uint64_t amount)
683097055e2SEdward Tomasz Napierala {
68436af9869SEdward Tomasz Napierala 	int64_t old_amount, decayed_amount;
68536af9869SEdward Tomasz Napierala 	int64_t diff_proc, diff_cred;
686097055e2SEdward Tomasz Napierala 
687097055e2SEdward Tomasz Napierala 	SDT_PROBE(racct, kernel, rusage, set, p, resource, amount, 0, 0);
688097055e2SEdward Tomasz Napierala 
689097055e2SEdward Tomasz Napierala 	/*
690097055e2SEdward Tomasz Napierala 	 * We need proc lock to dereference p->p_ucred.
691097055e2SEdward Tomasz Napierala 	 */
692097055e2SEdward Tomasz Napierala 	PROC_LOCK_ASSERT(p, MA_OWNED);
693097055e2SEdward Tomasz Napierala 
69436af9869SEdward Tomasz Napierala 	old_amount = p->p_racct->r_resources[resource];
69536af9869SEdward Tomasz Napierala 	/*
69636af9869SEdward Tomasz Napierala 	 * The diffs may be negative.
69736af9869SEdward Tomasz Napierala 	 */
69836af9869SEdward Tomasz Napierala 	diff_proc = amount - old_amount;
69936af9869SEdward Tomasz Napierala 	if (RACCT_IS_DECAYING(resource)) {
70036af9869SEdward Tomasz Napierala 		/*
70136af9869SEdward Tomasz Napierala 		 * Resources in per-credential racct containers may decay.
70236af9869SEdward Tomasz Napierala 		 * If this is the case, we need to calculate the difference
70336af9869SEdward Tomasz Napierala 		 * between the new amount and the proportional value of the
70436af9869SEdward Tomasz Napierala 		 * old amount that has decayed in the ucred racct containers.
70536af9869SEdward Tomasz Napierala 		 */
70636af9869SEdward Tomasz Napierala 		decayed_amount = old_amount * RACCT_DECAY_FACTOR / FSCALE;
70736af9869SEdward Tomasz Napierala 		diff_cred = amount - decayed_amount;
70836af9869SEdward Tomasz Napierala 	} else
70936af9869SEdward Tomasz Napierala 		diff_cred = diff_proc;
71036af9869SEdward Tomasz Napierala 
71136af9869SEdward Tomasz Napierala 	racct_alloc_resource(p->p_racct, resource, diff_proc);
71236af9869SEdward Tomasz Napierala 	if (diff_cred > 0)
71336af9869SEdward Tomasz Napierala 		racct_add_cred_locked(p->p_ucred, resource, diff_cred);
71436af9869SEdward Tomasz Napierala 	else if (diff_cred < 0)
71536af9869SEdward Tomasz Napierala 		racct_sub_cred_locked(p->p_ucred, resource, -diff_cred);
71636af9869SEdward Tomasz Napierala }
71736af9869SEdward Tomasz Napierala 
71836af9869SEdward Tomasz Napierala void
71936af9869SEdward Tomasz Napierala racct_set_force(struct proc *p, int resource, uint64_t amount)
72036af9869SEdward Tomasz Napierala {
721097055e2SEdward Tomasz Napierala 	mtx_lock(&racct_lock);
72236af9869SEdward Tomasz Napierala 	racct_set_force_locked(p, resource, amount);
723097055e2SEdward Tomasz Napierala 	mtx_unlock(&racct_lock);
724097055e2SEdward Tomasz Napierala }
725097055e2SEdward Tomasz Napierala 
726097055e2SEdward Tomasz Napierala /*
727097055e2SEdward Tomasz Napierala  * Returns amount of 'resource' the process 'p' can keep allocated.
728097055e2SEdward Tomasz Napierala  * Allocating more than that would be denied, unless the resource
729097055e2SEdward Tomasz Napierala  * is marked undeniable.  Amount of already allocated resource does
730097055e2SEdward Tomasz Napierala  * not matter.
731097055e2SEdward Tomasz Napierala  */
732097055e2SEdward Tomasz Napierala uint64_t
733097055e2SEdward Tomasz Napierala racct_get_limit(struct proc *p, int resource)
734097055e2SEdward Tomasz Napierala {
735097055e2SEdward Tomasz Napierala 
736097055e2SEdward Tomasz Napierala #ifdef RCTL
737097055e2SEdward Tomasz Napierala 	return (rctl_get_limit(p, resource));
738097055e2SEdward Tomasz Napierala #else
739097055e2SEdward Tomasz Napierala 	return (UINT64_MAX);
740097055e2SEdward Tomasz Napierala #endif
741097055e2SEdward Tomasz Napierala }
742097055e2SEdward Tomasz Napierala 
743097055e2SEdward Tomasz Napierala /*
744097055e2SEdward Tomasz Napierala  * Returns amount of 'resource' the process 'p' can keep allocated.
745097055e2SEdward Tomasz Napierala  * Allocating more than that would be denied, unless the resource
746097055e2SEdward Tomasz Napierala  * is marked undeniable.  Amount of already allocated resource does
747097055e2SEdward Tomasz Napierala  * matter.
748097055e2SEdward Tomasz Napierala  */
749097055e2SEdward Tomasz Napierala uint64_t
750097055e2SEdward Tomasz Napierala racct_get_available(struct proc *p, int resource)
751097055e2SEdward Tomasz Napierala {
752097055e2SEdward Tomasz Napierala 
753097055e2SEdward Tomasz Napierala #ifdef RCTL
754097055e2SEdward Tomasz Napierala 	return (rctl_get_available(p, resource));
755097055e2SEdward Tomasz Napierala #else
756097055e2SEdward Tomasz Napierala 	return (UINT64_MAX);
757097055e2SEdward Tomasz Napierala #endif
758097055e2SEdward Tomasz Napierala }
759097055e2SEdward Tomasz Napierala 
760097055e2SEdward Tomasz Napierala /*
76136af9869SEdward Tomasz Napierala  * Returns amount of the %cpu resource that process 'p' can add to its %cpu
76236af9869SEdward Tomasz Napierala  * utilization.  Adding more than that would lead to the process being
76336af9869SEdward Tomasz Napierala  * throttled.
76436af9869SEdward Tomasz Napierala  */
76536af9869SEdward Tomasz Napierala static int64_t
76636af9869SEdward Tomasz Napierala racct_pcpu_available(struct proc *p)
76736af9869SEdward Tomasz Napierala {
76836af9869SEdward Tomasz Napierala 
76936af9869SEdward Tomasz Napierala #ifdef RCTL
77036af9869SEdward Tomasz Napierala 	return (rctl_pcpu_available(p));
77136af9869SEdward Tomasz Napierala #else
77236af9869SEdward Tomasz Napierala 	return (INT64_MAX);
77336af9869SEdward Tomasz Napierala #endif
77436af9869SEdward Tomasz Napierala }
77536af9869SEdward Tomasz Napierala 
77636af9869SEdward Tomasz Napierala /*
777097055e2SEdward Tomasz Napierala  * Decrease allocation of 'resource' by 'amount' for process 'p'.
778097055e2SEdward Tomasz Napierala  */
779097055e2SEdward Tomasz Napierala void
780097055e2SEdward Tomasz Napierala racct_sub(struct proc *p, int resource, uint64_t amount)
781097055e2SEdward Tomasz Napierala {
782097055e2SEdward Tomasz Napierala 
783097055e2SEdward Tomasz Napierala 	SDT_PROBE(racct, kernel, rusage, sub, p, resource, amount, 0, 0);
784097055e2SEdward Tomasz Napierala 
785097055e2SEdward Tomasz Napierala 	/*
786097055e2SEdward Tomasz Napierala 	 * We need proc lock to dereference p->p_ucred.
787097055e2SEdward Tomasz Napierala 	 */
788097055e2SEdward Tomasz Napierala 	PROC_LOCK_ASSERT(p, MA_OWNED);
78936af9869SEdward Tomasz Napierala 	KASSERT(RACCT_CAN_DROP(resource),
790baf85d0aSEdward Tomasz Napierala 	    ("%s: called for non-droppable resource %d", __func__, resource));
791097055e2SEdward Tomasz Napierala 
792097055e2SEdward Tomasz Napierala 	mtx_lock(&racct_lock);
793097055e2SEdward Tomasz Napierala 	KASSERT(amount <= p->p_racct->r_resources[resource],
794baf85d0aSEdward Tomasz Napierala 	    ("%s: freeing %ju of resource %d, which is more "
795baf85d0aSEdward Tomasz Napierala 	     "than allocated %jd for %s (pid %d)", __func__, amount, resource,
796097055e2SEdward Tomasz Napierala 	    (intmax_t)p->p_racct->r_resources[resource], p->p_comm, p->p_pid));
797097055e2SEdward Tomasz Napierala 
798097055e2SEdward Tomasz Napierala 	racct_alloc_resource(p->p_racct, resource, -amount);
799097055e2SEdward Tomasz Napierala 	racct_sub_cred_locked(p->p_ucred, resource, amount);
800097055e2SEdward Tomasz Napierala 	mtx_unlock(&racct_lock);
801097055e2SEdward Tomasz Napierala }
802097055e2SEdward Tomasz Napierala 
803097055e2SEdward Tomasz Napierala static void
804097055e2SEdward Tomasz Napierala racct_sub_cred_locked(struct ucred *cred, int resource, uint64_t amount)
805097055e2SEdward Tomasz Napierala {
806097055e2SEdward Tomasz Napierala 	struct prison *pr;
807097055e2SEdward Tomasz Napierala 
808097055e2SEdward Tomasz Napierala 	SDT_PROBE(racct, kernel, rusage, sub_cred, cred, resource, amount,
809097055e2SEdward Tomasz Napierala 	    0, 0);
810097055e2SEdward Tomasz Napierala 
811097055e2SEdward Tomasz Napierala #ifdef notyet
81236af9869SEdward Tomasz Napierala 	KASSERT(RACCT_CAN_DROP(resource),
813baf85d0aSEdward Tomasz Napierala 	    ("%s: called for resource %d which can not drop", __func__,
814097055e2SEdward Tomasz Napierala 	     resource));
815097055e2SEdward Tomasz Napierala #endif
816097055e2SEdward Tomasz Napierala 
817097055e2SEdward Tomasz Napierala 	racct_alloc_resource(cred->cr_ruidinfo->ui_racct, resource, -amount);
818097055e2SEdward Tomasz Napierala 	for (pr = cred->cr_prison; pr != NULL; pr = pr->pr_parent)
819a7ad07bfSEdward Tomasz Napierala 		racct_alloc_resource(pr->pr_prison_racct->prr_racct, resource,
820a7ad07bfSEdward Tomasz Napierala 		    -amount);
821097055e2SEdward Tomasz Napierala 	racct_alloc_resource(cred->cr_loginclass->lc_racct, resource, -amount);
822097055e2SEdward Tomasz Napierala }
823097055e2SEdward Tomasz Napierala 
824097055e2SEdward Tomasz Napierala /*
825097055e2SEdward Tomasz Napierala  * Decrease allocation of 'resource' by 'amount' for credential 'cred'.
826097055e2SEdward Tomasz Napierala  */
827097055e2SEdward Tomasz Napierala void
828097055e2SEdward Tomasz Napierala racct_sub_cred(struct ucred *cred, int resource, uint64_t amount)
829097055e2SEdward Tomasz Napierala {
830097055e2SEdward Tomasz Napierala 
831097055e2SEdward Tomasz Napierala 	mtx_lock(&racct_lock);
832097055e2SEdward Tomasz Napierala 	racct_sub_cred_locked(cred, resource, amount);
833097055e2SEdward Tomasz Napierala 	mtx_unlock(&racct_lock);
834097055e2SEdward Tomasz Napierala }
835097055e2SEdward Tomasz Napierala 
836097055e2SEdward Tomasz Napierala /*
837097055e2SEdward Tomasz Napierala  * Inherit resource usage information from the parent process.
838097055e2SEdward Tomasz Napierala  */
839097055e2SEdward Tomasz Napierala int
840097055e2SEdward Tomasz Napierala racct_proc_fork(struct proc *parent, struct proc *child)
841097055e2SEdward Tomasz Napierala {
842097055e2SEdward Tomasz Napierala 	int i, error = 0;
843097055e2SEdward Tomasz Napierala 
844097055e2SEdward Tomasz Napierala 	/*
845097055e2SEdward Tomasz Napierala 	 * Create racct for the child process.
846097055e2SEdward Tomasz Napierala 	 */
847097055e2SEdward Tomasz Napierala 	racct_create(&child->p_racct);
848097055e2SEdward Tomasz Napierala 
849097055e2SEdward Tomasz Napierala 	PROC_LOCK(parent);
850097055e2SEdward Tomasz Napierala 	PROC_LOCK(child);
851097055e2SEdward Tomasz Napierala 	mtx_lock(&racct_lock);
852097055e2SEdward Tomasz Napierala 
853c0c09362SEdward Tomasz Napierala #ifdef RCTL
854c0c09362SEdward Tomasz Napierala 	error = rctl_proc_fork(parent, child);
855c0c09362SEdward Tomasz Napierala 	if (error != 0)
856c0c09362SEdward Tomasz Napierala 		goto out;
857c0c09362SEdward Tomasz Napierala #endif
858c0c09362SEdward Tomasz Napierala 
85936af9869SEdward Tomasz Napierala 	/* Init process cpu time. */
86036af9869SEdward Tomasz Napierala 	child->p_prev_runtime = 0;
86136af9869SEdward Tomasz Napierala 	child->p_throttled = 0;
86236af9869SEdward Tomasz Napierala 
863097055e2SEdward Tomasz Napierala 	/*
864097055e2SEdward Tomasz Napierala 	 * Inherit resource usage.
865097055e2SEdward Tomasz Napierala 	 */
866097055e2SEdward Tomasz Napierala 	for (i = 0; i <= RACCT_MAX; i++) {
867097055e2SEdward Tomasz Napierala 		if (parent->p_racct->r_resources[i] == 0 ||
8684fe84775SEdward Tomasz Napierala 		    !RACCT_IS_INHERITABLE(i))
869097055e2SEdward Tomasz Napierala 			continue;
870097055e2SEdward Tomasz Napierala 
871097055e2SEdward Tomasz Napierala 		error = racct_set_locked(child, i,
872097055e2SEdward Tomasz Napierala 		    parent->p_racct->r_resources[i]);
873ac6fafe6SEdward Tomasz Napierala 		if (error != 0)
874097055e2SEdward Tomasz Napierala 			goto out;
875097055e2SEdward Tomasz Napierala 	}
876097055e2SEdward Tomasz Napierala 
8772d8696d1SEdward Tomasz Napierala 	error = racct_add_locked(child, RACCT_NPROC, 1);
8782d8696d1SEdward Tomasz Napierala 	error += racct_add_locked(child, RACCT_NTHR, 1);
8792d8696d1SEdward Tomasz Napierala 
880097055e2SEdward Tomasz Napierala out:
881097055e2SEdward Tomasz Napierala 	mtx_unlock(&racct_lock);
882097055e2SEdward Tomasz Napierala 	PROC_UNLOCK(child);
883097055e2SEdward Tomasz Napierala 	PROC_UNLOCK(parent);
884097055e2SEdward Tomasz Napierala 
885ab27d5d8SEdward Tomasz Napierala 	if (error != 0)
886ab27d5d8SEdward Tomasz Napierala 		racct_proc_exit(child);
887ab27d5d8SEdward Tomasz Napierala 
888097055e2SEdward Tomasz Napierala 	return (error);
889097055e2SEdward Tomasz Napierala }
890097055e2SEdward Tomasz Napierala 
89172a401d9SEdward Tomasz Napierala /*
89272a401d9SEdward Tomasz Napierala  * Called at the end of fork1(), to handle rules that require the process
89372a401d9SEdward Tomasz Napierala  * to be fully initialized.
89472a401d9SEdward Tomasz Napierala  */
89572a401d9SEdward Tomasz Napierala void
89672a401d9SEdward Tomasz Napierala racct_proc_fork_done(struct proc *child)
89772a401d9SEdward Tomasz Napierala {
89872a401d9SEdward Tomasz Napierala 
89972a401d9SEdward Tomasz Napierala #ifdef RCTL
90072a401d9SEdward Tomasz Napierala 	PROC_LOCK(child);
90172a401d9SEdward Tomasz Napierala 	mtx_lock(&racct_lock);
90272a401d9SEdward Tomasz Napierala 	rctl_enforce(child, RACCT_NPROC, 0);
90372a401d9SEdward Tomasz Napierala 	rctl_enforce(child, RACCT_NTHR, 0);
90472a401d9SEdward Tomasz Napierala 	mtx_unlock(&racct_lock);
90572a401d9SEdward Tomasz Napierala 	PROC_UNLOCK(child);
90672a401d9SEdward Tomasz Napierala #endif
90772a401d9SEdward Tomasz Napierala }
90872a401d9SEdward Tomasz Napierala 
909097055e2SEdward Tomasz Napierala void
910097055e2SEdward Tomasz Napierala racct_proc_exit(struct proc *p)
911097055e2SEdward Tomasz Napierala {
9122419d7f9SEdward Tomasz Napierala 	int i;
913097055e2SEdward Tomasz Napierala 	uint64_t runtime;
91436af9869SEdward Tomasz Napierala 	struct timeval wallclock;
91536af9869SEdward Tomasz Napierala 	uint64_t pct_estimate, pct;
916097055e2SEdward Tomasz Napierala 
917097055e2SEdward Tomasz Napierala 	PROC_LOCK(p);
918097055e2SEdward Tomasz Napierala 	/*
919097055e2SEdward Tomasz Napierala 	 * We don't need to calculate rux, proc_reap() has already done this.
920097055e2SEdward Tomasz Napierala 	 */
921097055e2SEdward Tomasz Napierala 	runtime = cputick2usec(p->p_rux.rux_runtime);
922097055e2SEdward Tomasz Napierala #ifdef notyet
923097055e2SEdward Tomasz Napierala 	KASSERT(runtime >= p->p_prev_runtime, ("runtime < p_prev_runtime"));
924097055e2SEdward Tomasz Napierala #else
925097055e2SEdward Tomasz Napierala 	if (runtime < p->p_prev_runtime)
926097055e2SEdward Tomasz Napierala 		runtime = p->p_prev_runtime;
927097055e2SEdward Tomasz Napierala #endif
92836af9869SEdward Tomasz Napierala 	microuptime(&wallclock);
92936af9869SEdward Tomasz Napierala 	timevalsub(&wallclock, &p->p_stats->p_start);
93084590fd8SEdward Tomasz Napierala 	if (wallclock.tv_sec > 0 || wallclock.tv_usec > 0) {
93136af9869SEdward Tomasz Napierala 		pct_estimate = (1000000 * runtime * 100) /
93236af9869SEdward Tomasz Napierala 		    ((uint64_t)wallclock.tv_sec * 1000000 +
93336af9869SEdward Tomasz Napierala 		    wallclock.tv_usec);
93484590fd8SEdward Tomasz Napierala 	} else
93584590fd8SEdward Tomasz Napierala 		pct_estimate = 0;
93636af9869SEdward Tomasz Napierala 	pct = racct_getpcpu(p, pct_estimate);
93736af9869SEdward Tomasz Napierala 
9382419d7f9SEdward Tomasz Napierala 	mtx_lock(&racct_lock);
9392419d7f9SEdward Tomasz Napierala 	racct_set_locked(p, RACCT_CPU, runtime);
94036af9869SEdward Tomasz Napierala 	racct_add_cred_locked(p->p_ucred, RACCT_PCTCPU, pct);
941097055e2SEdward Tomasz Napierala 
9422419d7f9SEdward Tomasz Napierala 	for (i = 0; i <= RACCT_MAX; i++) {
9432419d7f9SEdward Tomasz Napierala 		if (p->p_racct->r_resources[i] == 0)
9442419d7f9SEdward Tomasz Napierala 			continue;
9452419d7f9SEdward Tomasz Napierala 	    	if (!RACCT_IS_RECLAIMABLE(i))
9462419d7f9SEdward Tomasz Napierala 			continue;
9472419d7f9SEdward Tomasz Napierala 		racct_set_locked(p, i, 0);
9482419d7f9SEdward Tomasz Napierala 	}
9492419d7f9SEdward Tomasz Napierala 
9502419d7f9SEdward Tomasz Napierala 	mtx_unlock(&racct_lock);
951097055e2SEdward Tomasz Napierala 	PROC_UNLOCK(p);
952097055e2SEdward Tomasz Napierala 
953097055e2SEdward Tomasz Napierala #ifdef RCTL
954097055e2SEdward Tomasz Napierala 	rctl_racct_release(p->p_racct);
955097055e2SEdward Tomasz Napierala #endif
956097055e2SEdward Tomasz Napierala 	racct_destroy(&p->p_racct);
957097055e2SEdward Tomasz Napierala }
958097055e2SEdward Tomasz Napierala 
959097055e2SEdward Tomasz Napierala /*
960097055e2SEdward Tomasz Napierala  * Called after credentials change, to move resource utilisation
961097055e2SEdward Tomasz Napierala  * between raccts.
962097055e2SEdward Tomasz Napierala  */
963097055e2SEdward Tomasz Napierala void
964097055e2SEdward Tomasz Napierala racct_proc_ucred_changed(struct proc *p, struct ucred *oldcred,
965097055e2SEdward Tomasz Napierala     struct ucred *newcred)
966097055e2SEdward Tomasz Napierala {
967097055e2SEdward Tomasz Napierala 	struct uidinfo *olduip, *newuip;
968097055e2SEdward Tomasz Napierala 	struct loginclass *oldlc, *newlc;
969097055e2SEdward Tomasz Napierala 	struct prison *oldpr, *newpr, *pr;
970097055e2SEdward Tomasz Napierala 
971097055e2SEdward Tomasz Napierala 	PROC_LOCK_ASSERT(p, MA_NOTOWNED);
972097055e2SEdward Tomasz Napierala 
973097055e2SEdward Tomasz Napierala 	newuip = newcred->cr_ruidinfo;
974097055e2SEdward Tomasz Napierala 	olduip = oldcred->cr_ruidinfo;
975097055e2SEdward Tomasz Napierala 	newlc = newcred->cr_loginclass;
976097055e2SEdward Tomasz Napierala 	oldlc = oldcred->cr_loginclass;
977097055e2SEdward Tomasz Napierala 	newpr = newcred->cr_prison;
978097055e2SEdward Tomasz Napierala 	oldpr = oldcred->cr_prison;
979097055e2SEdward Tomasz Napierala 
980097055e2SEdward Tomasz Napierala 	mtx_lock(&racct_lock);
981097055e2SEdward Tomasz Napierala 	if (newuip != olduip) {
982097055e2SEdward Tomasz Napierala 		racct_sub_racct(olduip->ui_racct, p->p_racct);
983097055e2SEdward Tomasz Napierala 		racct_add_racct(newuip->ui_racct, p->p_racct);
984097055e2SEdward Tomasz Napierala 	}
985097055e2SEdward Tomasz Napierala 	if (newlc != oldlc) {
986097055e2SEdward Tomasz Napierala 		racct_sub_racct(oldlc->lc_racct, p->p_racct);
987097055e2SEdward Tomasz Napierala 		racct_add_racct(newlc->lc_racct, p->p_racct);
988097055e2SEdward Tomasz Napierala 	}
989097055e2SEdward Tomasz Napierala 	if (newpr != oldpr) {
990097055e2SEdward Tomasz Napierala 		for (pr = oldpr; pr != NULL; pr = pr->pr_parent)
991a7ad07bfSEdward Tomasz Napierala 			racct_sub_racct(pr->pr_prison_racct->prr_racct,
992a7ad07bfSEdward Tomasz Napierala 			    p->p_racct);
993097055e2SEdward Tomasz Napierala 		for (pr = newpr; pr != NULL; pr = pr->pr_parent)
994a7ad07bfSEdward Tomasz Napierala 			racct_add_racct(pr->pr_prison_racct->prr_racct,
995a7ad07bfSEdward Tomasz Napierala 			    p->p_racct);
996097055e2SEdward Tomasz Napierala 	}
997097055e2SEdward Tomasz Napierala 	mtx_unlock(&racct_lock);
998097055e2SEdward Tomasz Napierala 
999097055e2SEdward Tomasz Napierala #ifdef RCTL
1000097055e2SEdward Tomasz Napierala 	rctl_proc_ucred_changed(p, newcred);
1001097055e2SEdward Tomasz Napierala #endif
1002097055e2SEdward Tomasz Napierala }
1003097055e2SEdward Tomasz Napierala 
1004c34bbd2aSEdward Tomasz Napierala void
1005c34bbd2aSEdward Tomasz Napierala racct_move(struct racct *dest, struct racct *src)
1006c34bbd2aSEdward Tomasz Napierala {
1007c34bbd2aSEdward Tomasz Napierala 
1008c34bbd2aSEdward Tomasz Napierala 	mtx_lock(&racct_lock);
1009c34bbd2aSEdward Tomasz Napierala 
1010c34bbd2aSEdward Tomasz Napierala 	racct_add_racct(dest, src);
1011c34bbd2aSEdward Tomasz Napierala 	racct_sub_racct(src, src);
1012c34bbd2aSEdward Tomasz Napierala 
1013c34bbd2aSEdward Tomasz Napierala 	mtx_unlock(&racct_lock);
1014c34bbd2aSEdward Tomasz Napierala }
1015c34bbd2aSEdward Tomasz Napierala 
1016097055e2SEdward Tomasz Napierala static void
101736af9869SEdward Tomasz Napierala racct_proc_throttle(struct proc *p)
101836af9869SEdward Tomasz Napierala {
101936af9869SEdward Tomasz Napierala 	struct thread *td;
102036af9869SEdward Tomasz Napierala #ifdef SMP
102136af9869SEdward Tomasz Napierala 	int cpuid;
102236af9869SEdward Tomasz Napierala #endif
102336af9869SEdward Tomasz Napierala 
102436af9869SEdward Tomasz Napierala 	PROC_LOCK_ASSERT(p, MA_OWNED);
102536af9869SEdward Tomasz Napierala 
102636af9869SEdward Tomasz Napierala 	/*
102736af9869SEdward Tomasz Napierala 	 * Do not block kernel processes.  Also do not block processes with
102836af9869SEdward Tomasz Napierala 	 * low %cpu utilization to improve interactivity.
102936af9869SEdward Tomasz Napierala 	 */
103036af9869SEdward Tomasz Napierala 	if (((p->p_flag & (P_SYSTEM | P_KTHREAD)) != 0) ||
103136af9869SEdward Tomasz Napierala 	    (p->p_racct->r_resources[RACCT_PCTCPU] <= pcpu_threshold))
103236af9869SEdward Tomasz Napierala 		return;
103336af9869SEdward Tomasz Napierala 	p->p_throttled = 1;
103436af9869SEdward Tomasz Napierala 
103536af9869SEdward Tomasz Napierala 	FOREACH_THREAD_IN_PROC(p, td) {
1036*16befafdSEdward Tomasz Napierala 		thread_lock(td);
103736af9869SEdward Tomasz Napierala 		switch (td->td_state) {
103836af9869SEdward Tomasz Napierala 		case TDS_RUNQ:
103936af9869SEdward Tomasz Napierala 			/*
104036af9869SEdward Tomasz Napierala 			 * If the thread is on the scheduler run-queue, we can
104136af9869SEdward Tomasz Napierala 			 * not just remove it from there.  So we set the flag
104236af9869SEdward Tomasz Napierala 			 * TDF_NEEDRESCHED for the thread, so that once it is
104336af9869SEdward Tomasz Napierala 			 * running, it is taken off the cpu as soon as possible.
104436af9869SEdward Tomasz Napierala 			 */
104536af9869SEdward Tomasz Napierala 			td->td_flags |= TDF_NEEDRESCHED;
104636af9869SEdward Tomasz Napierala 			break;
104736af9869SEdward Tomasz Napierala 		case TDS_RUNNING:
104836af9869SEdward Tomasz Napierala 			/*
104936af9869SEdward Tomasz Napierala 			 * If the thread is running, we request a context
105036af9869SEdward Tomasz Napierala 			 * switch for it by setting the TDF_NEEDRESCHED flag.
105136af9869SEdward Tomasz Napierala 			 */
105236af9869SEdward Tomasz Napierala 			td->td_flags |= TDF_NEEDRESCHED;
105336af9869SEdward Tomasz Napierala #ifdef SMP
105436af9869SEdward Tomasz Napierala 			cpuid = td->td_oncpu;
105536af9869SEdward Tomasz Napierala 			if ((cpuid != NOCPU) && (td != curthread))
105636af9869SEdward Tomasz Napierala 				ipi_cpu(cpuid, IPI_AST);
105736af9869SEdward Tomasz Napierala #endif
105836af9869SEdward Tomasz Napierala 			break;
105936af9869SEdward Tomasz Napierala 		default:
106036af9869SEdward Tomasz Napierala 			break;
106136af9869SEdward Tomasz Napierala 		}
1062*16befafdSEdward Tomasz Napierala 		thread_unlock(td);
106336af9869SEdward Tomasz Napierala 	}
106436af9869SEdward Tomasz Napierala }
106536af9869SEdward Tomasz Napierala 
106636af9869SEdward Tomasz Napierala static void
106736af9869SEdward Tomasz Napierala racct_proc_wakeup(struct proc *p)
106836af9869SEdward Tomasz Napierala {
106936af9869SEdward Tomasz Napierala 	PROC_LOCK_ASSERT(p, MA_OWNED);
107036af9869SEdward Tomasz Napierala 
107136af9869SEdward Tomasz Napierala 	if (p->p_throttled) {
107236af9869SEdward Tomasz Napierala 		p->p_throttled = 0;
107336af9869SEdward Tomasz Napierala 		wakeup(p->p_racct);
107436af9869SEdward Tomasz Napierala 	}
107536af9869SEdward Tomasz Napierala }
107636af9869SEdward Tomasz Napierala 
107736af9869SEdward Tomasz Napierala static void
107836af9869SEdward Tomasz Napierala racct_decay_resource(struct racct *racct, void * res, void* dummy)
107936af9869SEdward Tomasz Napierala {
108036af9869SEdward Tomasz Napierala 	int resource;
108136af9869SEdward Tomasz Napierala 	int64_t r_old, r_new;
108236af9869SEdward Tomasz Napierala 
108336af9869SEdward Tomasz Napierala 	resource = *(int *)res;
108436af9869SEdward Tomasz Napierala 	r_old = racct->r_resources[resource];
108536af9869SEdward Tomasz Napierala 
108636af9869SEdward Tomasz Napierala 	/* If there is nothing to decay, just exit. */
108736af9869SEdward Tomasz Napierala 	if (r_old <= 0)
108836af9869SEdward Tomasz Napierala 		return;
108936af9869SEdward Tomasz Napierala 
109036af9869SEdward Tomasz Napierala 	mtx_lock(&racct_lock);
109136af9869SEdward Tomasz Napierala 	r_new = r_old * RACCT_DECAY_FACTOR / FSCALE;
109236af9869SEdward Tomasz Napierala 	racct->r_resources[resource] = r_new;
109336af9869SEdward Tomasz Napierala 	mtx_unlock(&racct_lock);
109436af9869SEdward Tomasz Napierala }
109536af9869SEdward Tomasz Napierala 
109636af9869SEdward Tomasz Napierala static void
109736af9869SEdward Tomasz Napierala racct_decay(int resource)
109836af9869SEdward Tomasz Napierala {
109936af9869SEdward Tomasz Napierala 	ui_racct_foreach(racct_decay_resource, &resource, NULL);
110036af9869SEdward Tomasz Napierala 	loginclass_racct_foreach(racct_decay_resource, &resource, NULL);
110136af9869SEdward Tomasz Napierala 	prison_racct_foreach(racct_decay_resource, &resource, NULL);
110236af9869SEdward Tomasz Napierala }
110336af9869SEdward Tomasz Napierala 
110436af9869SEdward Tomasz Napierala static void
1105097055e2SEdward Tomasz Napierala racctd(void)
1106097055e2SEdward Tomasz Napierala {
1107097055e2SEdward Tomasz Napierala 	struct thread *td;
1108097055e2SEdward Tomasz Napierala 	struct proc *p;
1109097055e2SEdward Tomasz Napierala 	struct timeval wallclock;
1110097055e2SEdward Tomasz Napierala 	uint64_t runtime;
111136af9869SEdward Tomasz Napierala 	uint64_t pct, pct_estimate;
1112097055e2SEdward Tomasz Napierala 
1113097055e2SEdward Tomasz Napierala 	for (;;) {
111436af9869SEdward Tomasz Napierala 		racct_decay(RACCT_PCTCPU);
111536af9869SEdward Tomasz Napierala 
1116097055e2SEdward Tomasz Napierala 		sx_slock(&allproc_lock);
1117097055e2SEdward Tomasz Napierala 
111836af9869SEdward Tomasz Napierala 		LIST_FOREACH(p, &zombproc, p_list) {
111936af9869SEdward Tomasz Napierala 			PROC_LOCK(p);
112036af9869SEdward Tomasz Napierala 			racct_set(p, RACCT_PCTCPU, 0);
112136af9869SEdward Tomasz Napierala 			PROC_UNLOCK(p);
112236af9869SEdward Tomasz Napierala 		}
112336af9869SEdward Tomasz Napierala 
1124097055e2SEdward Tomasz Napierala 		FOREACH_PROC_IN_SYSTEM(p) {
112536af9869SEdward Tomasz Napierala 			PROC_LOCK(p);
112636af9869SEdward Tomasz Napierala 			if (p->p_state != PRS_NORMAL) {
112736af9869SEdward Tomasz Napierala 				PROC_UNLOCK(p);
1128097055e2SEdward Tomasz Napierala 				continue;
112936af9869SEdward Tomasz Napierala 			}
1130097055e2SEdward Tomasz Napierala 
1131097055e2SEdward Tomasz Napierala 			microuptime(&wallclock);
1132097055e2SEdward Tomasz Napierala 			timevalsub(&wallclock, &p->p_stats->p_start);
1133097055e2SEdward Tomasz Napierala 			PROC_SLOCK(p);
11340a53cd57SEdward Tomasz Napierala 			FOREACH_THREAD_IN_PROC(p, td)
1135097055e2SEdward Tomasz Napierala 				ruxagg(p, td);
1136097055e2SEdward Tomasz Napierala 			runtime = cputick2usec(p->p_rux.rux_runtime);
1137097055e2SEdward Tomasz Napierala 			PROC_SUNLOCK(p);
1138097055e2SEdward Tomasz Napierala #ifdef notyet
1139097055e2SEdward Tomasz Napierala 			KASSERT(runtime >= p->p_prev_runtime,
1140097055e2SEdward Tomasz Napierala 			    ("runtime < p_prev_runtime"));
1141097055e2SEdward Tomasz Napierala #else
1142097055e2SEdward Tomasz Napierala 			if (runtime < p->p_prev_runtime)
1143097055e2SEdward Tomasz Napierala 				runtime = p->p_prev_runtime;
1144097055e2SEdward Tomasz Napierala #endif
1145097055e2SEdward Tomasz Napierala 			p->p_prev_runtime = runtime;
114684590fd8SEdward Tomasz Napierala 			if (wallclock.tv_sec > 0 || wallclock.tv_usec > 0) {
114736af9869SEdward Tomasz Napierala 				pct_estimate = (1000000 * runtime * 100) /
114836af9869SEdward Tomasz Napierala 				    ((uint64_t)wallclock.tv_sec * 1000000 +
114936af9869SEdward Tomasz Napierala 				    wallclock.tv_usec);
115084590fd8SEdward Tomasz Napierala 			} else
115184590fd8SEdward Tomasz Napierala 				pct_estimate = 0;
115236af9869SEdward Tomasz Napierala 			pct = racct_getpcpu(p, pct_estimate);
1153097055e2SEdward Tomasz Napierala 			mtx_lock(&racct_lock);
115436af9869SEdward Tomasz Napierala 			racct_set_force_locked(p, RACCT_PCTCPU, pct);
1155097055e2SEdward Tomasz Napierala 			racct_set_locked(p, RACCT_CPU, runtime);
1156097055e2SEdward Tomasz Napierala 			racct_set_locked(p, RACCT_WALLCLOCK,
115759f513cdSJaakko Heinonen 			    (uint64_t)wallclock.tv_sec * 1000000 +
115859f513cdSJaakko Heinonen 			    wallclock.tv_usec);
1159097055e2SEdward Tomasz Napierala 			mtx_unlock(&racct_lock);
1160097055e2SEdward Tomasz Napierala 			PROC_UNLOCK(p);
1161097055e2SEdward Tomasz Napierala 		}
116236af9869SEdward Tomasz Napierala 
116336af9869SEdward Tomasz Napierala 		/*
116436af9869SEdward Tomasz Napierala 		 * To ensure that processes are throttled in a fair way, we need
116536af9869SEdward Tomasz Napierala 		 * to iterate over all processes again and check the limits
116636af9869SEdward Tomasz Napierala 		 * for %cpu resource only after ucred racct containers have been
116736af9869SEdward Tomasz Napierala 		 * properly filled.
116836af9869SEdward Tomasz Napierala 		 */
116936af9869SEdward Tomasz Napierala 		FOREACH_PROC_IN_SYSTEM(p) {
117036af9869SEdward Tomasz Napierala 			PROC_LOCK(p);
117136af9869SEdward Tomasz Napierala 			if (p->p_state != PRS_NORMAL) {
117236af9869SEdward Tomasz Napierala 				PROC_UNLOCK(p);
117336af9869SEdward Tomasz Napierala 				continue;
117436af9869SEdward Tomasz Napierala 			}
117536af9869SEdward Tomasz Napierala 
117636af9869SEdward Tomasz Napierala 			if (racct_pcpu_available(p) <= 0)
117736af9869SEdward Tomasz Napierala 				racct_proc_throttle(p);
117836af9869SEdward Tomasz Napierala 			else if (p->p_throttled)
117936af9869SEdward Tomasz Napierala 				racct_proc_wakeup(p);
118036af9869SEdward Tomasz Napierala 			PROC_UNLOCK(p);
118136af9869SEdward Tomasz Napierala 		}
1182097055e2SEdward Tomasz Napierala 		sx_sunlock(&allproc_lock);
1183097055e2SEdward Tomasz Napierala 		pause("-", hz);
1184097055e2SEdward Tomasz Napierala 	}
1185097055e2SEdward Tomasz Napierala }
1186097055e2SEdward Tomasz Napierala 
1187097055e2SEdward Tomasz Napierala static struct kproc_desc racctd_kp = {
1188097055e2SEdward Tomasz Napierala 	"racctd",
1189097055e2SEdward Tomasz Napierala 	racctd,
1190097055e2SEdward Tomasz Napierala 	NULL
1191097055e2SEdward Tomasz Napierala };
1192097055e2SEdward Tomasz Napierala SYSINIT(racctd, SI_SUB_RACCTD, SI_ORDER_FIRST, kproc_start, &racctd_kp);
1193097055e2SEdward Tomasz Napierala 
1194097055e2SEdward Tomasz Napierala static void
1195097055e2SEdward Tomasz Napierala racct_init(void)
1196097055e2SEdward Tomasz Napierala {
1197097055e2SEdward Tomasz Napierala 
1198097055e2SEdward Tomasz Napierala 	racct_zone = uma_zcreate("racct", sizeof(struct racct),
1199097055e2SEdward Tomasz Napierala 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
1200097055e2SEdward Tomasz Napierala 	/*
1201097055e2SEdward Tomasz Napierala 	 * XXX: Move this somewhere.
1202097055e2SEdward Tomasz Napierala 	 */
1203a7ad07bfSEdward Tomasz Napierala 	prison0.pr_prison_racct = prison_racct_find("0");
1204097055e2SEdward Tomasz Napierala }
1205097055e2SEdward Tomasz Napierala SYSINIT(racct, SI_SUB_RACCT, SI_ORDER_FIRST, racct_init, NULL);
1206097055e2SEdward Tomasz Napierala 
1207097055e2SEdward Tomasz Napierala #else /* !RACCT */
1208097055e2SEdward Tomasz Napierala 
1209097055e2SEdward Tomasz Napierala int
1210097055e2SEdward Tomasz Napierala racct_add(struct proc *p, int resource, uint64_t amount)
1211097055e2SEdward Tomasz Napierala {
1212097055e2SEdward Tomasz Napierala 
1213097055e2SEdward Tomasz Napierala 	return (0);
1214097055e2SEdward Tomasz Napierala }
1215097055e2SEdward Tomasz Napierala 
1216097055e2SEdward Tomasz Napierala void
1217097055e2SEdward Tomasz Napierala racct_add_cred(struct ucred *cred, int resource, uint64_t amount)
1218097055e2SEdward Tomasz Napierala {
1219097055e2SEdward Tomasz Napierala }
1220097055e2SEdward Tomasz Napierala 
1221097055e2SEdward Tomasz Napierala void
1222097055e2SEdward Tomasz Napierala racct_add_force(struct proc *p, int resource, uint64_t amount)
1223097055e2SEdward Tomasz Napierala {
1224097055e2SEdward Tomasz Napierala 
1225097055e2SEdward Tomasz Napierala 	return;
1226097055e2SEdward Tomasz Napierala }
1227097055e2SEdward Tomasz Napierala 
1228097055e2SEdward Tomasz Napierala int
1229097055e2SEdward Tomasz Napierala racct_set(struct proc *p, int resource, uint64_t amount)
1230097055e2SEdward Tomasz Napierala {
1231097055e2SEdward Tomasz Napierala 
1232097055e2SEdward Tomasz Napierala 	return (0);
1233097055e2SEdward Tomasz Napierala }
1234097055e2SEdward Tomasz Napierala 
1235097055e2SEdward Tomasz Napierala void
1236c98fe0a5SEdward Tomasz Napierala racct_set_force(struct proc *p, int resource, uint64_t amount)
1237c98fe0a5SEdward Tomasz Napierala {
1238c98fe0a5SEdward Tomasz Napierala }
1239c98fe0a5SEdward Tomasz Napierala 
1240c98fe0a5SEdward Tomasz Napierala void
1241097055e2SEdward Tomasz Napierala racct_sub(struct proc *p, int resource, uint64_t amount)
1242097055e2SEdward Tomasz Napierala {
1243097055e2SEdward Tomasz Napierala }
1244097055e2SEdward Tomasz Napierala 
1245097055e2SEdward Tomasz Napierala void
1246097055e2SEdward Tomasz Napierala racct_sub_cred(struct ucred *cred, int resource, uint64_t amount)
1247097055e2SEdward Tomasz Napierala {
1248097055e2SEdward Tomasz Napierala }
1249097055e2SEdward Tomasz Napierala 
1250097055e2SEdward Tomasz Napierala uint64_t
1251097055e2SEdward Tomasz Napierala racct_get_limit(struct proc *p, int resource)
1252097055e2SEdward Tomasz Napierala {
1253097055e2SEdward Tomasz Napierala 
1254097055e2SEdward Tomasz Napierala 	return (UINT64_MAX);
1255097055e2SEdward Tomasz Napierala }
1256097055e2SEdward Tomasz Napierala 
1257c98fe0a5SEdward Tomasz Napierala uint64_t
1258c98fe0a5SEdward Tomasz Napierala racct_get_available(struct proc *p, int resource)
1259c98fe0a5SEdward Tomasz Napierala {
1260c98fe0a5SEdward Tomasz Napierala 
1261c98fe0a5SEdward Tomasz Napierala 	return (UINT64_MAX);
1262c98fe0a5SEdward Tomasz Napierala }
1263c98fe0a5SEdward Tomasz Napierala 
1264097055e2SEdward Tomasz Napierala void
1265097055e2SEdward Tomasz Napierala racct_create(struct racct **racctp)
1266097055e2SEdward Tomasz Napierala {
1267097055e2SEdward Tomasz Napierala }
1268097055e2SEdward Tomasz Napierala 
1269097055e2SEdward Tomasz Napierala void
1270097055e2SEdward Tomasz Napierala racct_destroy(struct racct **racctp)
1271097055e2SEdward Tomasz Napierala {
1272097055e2SEdward Tomasz Napierala }
1273097055e2SEdward Tomasz Napierala 
1274097055e2SEdward Tomasz Napierala int
1275097055e2SEdward Tomasz Napierala racct_proc_fork(struct proc *parent, struct proc *child)
1276097055e2SEdward Tomasz Napierala {
1277097055e2SEdward Tomasz Napierala 
1278097055e2SEdward Tomasz Napierala 	return (0);
1279097055e2SEdward Tomasz Napierala }
1280097055e2SEdward Tomasz Napierala 
1281097055e2SEdward Tomasz Napierala void
128272a401d9SEdward Tomasz Napierala racct_proc_fork_done(struct proc *child)
128372a401d9SEdward Tomasz Napierala {
128472a401d9SEdward Tomasz Napierala }
128572a401d9SEdward Tomasz Napierala 
128672a401d9SEdward Tomasz Napierala void
1287097055e2SEdward Tomasz Napierala racct_proc_exit(struct proc *p)
1288097055e2SEdward Tomasz Napierala {
1289097055e2SEdward Tomasz Napierala }
1290097055e2SEdward Tomasz Napierala 
1291097055e2SEdward Tomasz Napierala #endif /* !RACCT */
1292