xref: /freebsd/sys/kern/kern_racct.c (revision 5c93966020a07834ab882d92c0c28b5397c8dfa7)
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>
38ae34b6ffSEdward Tomasz Napierala #include <sys/buf.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;
74ba8f0eb8SEdward Tomasz Napierala #ifdef RACCT_DEFAULT_TO_DISABLED
754b5c9cf6SEdward Tomasz Napierala int racct_enable = 0;
764b5c9cf6SEdward Tomasz Napierala #else
774b5c9cf6SEdward Tomasz Napierala int racct_enable = 1;
784b5c9cf6SEdward Tomasz Napierala #endif
7936af9869SEdward Tomasz Napierala 
8036af9869SEdward Tomasz Napierala SYSCTL_NODE(_kern, OID_AUTO, racct, CTLFLAG_RW, 0, "Resource Accounting");
814b5c9cf6SEdward Tomasz Napierala SYSCTL_UINT(_kern_racct, OID_AUTO, enable, CTLFLAG_RDTUN, &racct_enable,
824b5c9cf6SEdward Tomasz Napierala     0, "Enable RACCT/RCTL");
8336af9869SEdward Tomasz Napierala SYSCTL_UINT(_kern_racct, OID_AUTO, pcpu_threshold, CTLFLAG_RW, &pcpu_threshold,
8436af9869SEdward Tomasz Napierala     0, "Processes with higher %cpu usage than this value can be throttled.");
8536af9869SEdward Tomasz Napierala 
8636af9869SEdward Tomasz Napierala /*
8736af9869SEdward Tomasz Napierala  * How many seconds it takes to use the scheduler %cpu calculations.  When a
8836af9869SEdward Tomasz Napierala  * process starts, we compute its %cpu usage by dividing its runtime by the
8936af9869SEdward Tomasz Napierala  * process wall clock time.  After RACCT_PCPU_SECS pass, we use the value
9036af9869SEdward Tomasz Napierala  * provided by the scheduler.
9136af9869SEdward Tomasz Napierala  */
9236af9869SEdward Tomasz Napierala #define RACCT_PCPU_SECS		3
9336af9869SEdward Tomasz Napierala 
94bbe4eb6dSEdward Tomasz Napierala struct mtx racct_lock;
95097055e2SEdward Tomasz Napierala MTX_SYSINIT(racct_lock, &racct_lock, "racct lock", MTX_DEF);
96097055e2SEdward Tomasz Napierala 
97097055e2SEdward Tomasz Napierala static uma_zone_t racct_zone;
98097055e2SEdward Tomasz Napierala 
99097055e2SEdward Tomasz Napierala static void racct_sub_racct(struct racct *dest, const struct racct *src);
100097055e2SEdward Tomasz Napierala static void racct_sub_cred_locked(struct ucred *cred, int resource,
101097055e2SEdward Tomasz Napierala 		uint64_t amount);
102097055e2SEdward Tomasz Napierala static void racct_add_cred_locked(struct ucred *cred, int resource,
103097055e2SEdward Tomasz Napierala 		uint64_t amount);
104097055e2SEdward Tomasz Napierala 
105097055e2SEdward Tomasz Napierala SDT_PROVIDER_DEFINE(racct);
10636160958SMark Johnston SDT_PROBE_DEFINE3(racct, , rusage, add,
107097055e2SEdward Tomasz Napierala     "struct proc *", "int", "uint64_t");
10836160958SMark Johnston SDT_PROBE_DEFINE3(racct, , rusage, add__failure,
109097055e2SEdward Tomasz Napierala     "struct proc *", "int", "uint64_t");
110bbe4eb6dSEdward Tomasz Napierala SDT_PROBE_DEFINE3(racct, , rusage, add__buf,
111bbe4eb6dSEdward Tomasz Napierala     "struct proc *", "const struct buf *", "int");
11236160958SMark Johnston SDT_PROBE_DEFINE3(racct, , rusage, add__cred,
11336160958SMark Johnston     "struct ucred *", "int", "uint64_t");
11436160958SMark Johnston SDT_PROBE_DEFINE3(racct, , rusage, add__force,
11536160958SMark Johnston     "struct proc *", "int", "uint64_t");
11636160958SMark Johnston SDT_PROBE_DEFINE3(racct, , rusage, set,
11736160958SMark Johnston     "struct proc *", "int", "uint64_t");
11836160958SMark Johnston SDT_PROBE_DEFINE3(racct, , rusage, set__failure,
11936160958SMark Johnston     "struct proc *", "int", "uint64_t");
12010287198SEdward Tomasz Napierala SDT_PROBE_DEFINE3(racct, , rusage, set__force,
12110287198SEdward Tomasz Napierala     "struct proc *", "int", "uint64_t");
12236160958SMark Johnston SDT_PROBE_DEFINE3(racct, , rusage, sub,
12336160958SMark Johnston     "struct proc *", "int", "uint64_t");
12436160958SMark Johnston SDT_PROBE_DEFINE3(racct, , rusage, sub__cred,
12536160958SMark Johnston     "struct ucred *", "int", "uint64_t");
12636160958SMark Johnston SDT_PROBE_DEFINE1(racct, , racct, create,
127097055e2SEdward Tomasz Napierala     "struct racct *");
12836160958SMark Johnston SDT_PROBE_DEFINE1(racct, , racct, destroy,
12936160958SMark Johnston     "struct racct *");
13036160958SMark Johnston SDT_PROBE_DEFINE2(racct, , racct, join,
131097055e2SEdward Tomasz Napierala     "struct racct *", "struct racct *");
13236160958SMark Johnston SDT_PROBE_DEFINE2(racct, , racct, join__failure,
13336160958SMark Johnston     "struct racct *", "struct racct *");
13436160958SMark Johnston SDT_PROBE_DEFINE2(racct, , racct, leave,
13536160958SMark Johnston     "struct racct *", "struct racct *");
136097055e2SEdward Tomasz Napierala 
137097055e2SEdward Tomasz Napierala int racct_types[] = {
138097055e2SEdward Tomasz Napierala 	[RACCT_CPU] =
13985a2f1b4SEdward Tomasz Napierala 		RACCT_IN_MILLIONS,
140097055e2SEdward Tomasz Napierala 	[RACCT_DATA] =
141097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
142097055e2SEdward Tomasz Napierala 	[RACCT_STACK] =
143097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
144097055e2SEdward Tomasz Napierala 	[RACCT_CORE] =
145097055e2SEdward Tomasz Napierala 		RACCT_DENIABLE,
146097055e2SEdward Tomasz Napierala 	[RACCT_RSS] =
147097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE,
148097055e2SEdward Tomasz Napierala 	[RACCT_MEMLOCK] =
149097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_DENIABLE,
150097055e2SEdward Tomasz Napierala 	[RACCT_NPROC] =
151097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_DENIABLE,
152097055e2SEdward Tomasz Napierala 	[RACCT_NOFILE] =
153097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
154097055e2SEdward Tomasz Napierala 	[RACCT_VMEM] =
155097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
156097055e2SEdward Tomasz Napierala 	[RACCT_NPTS] =
157097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
158097055e2SEdward Tomasz Napierala 	[RACCT_SWAP] =
159097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
160097055e2SEdward Tomasz Napierala 	[RACCT_NTHR] =
161097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_DENIABLE,
162097055e2SEdward Tomasz Napierala 	[RACCT_MSGQQUEUED] =
163097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
164097055e2SEdward Tomasz Napierala 	[RACCT_MSGQSIZE] =
165097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
166097055e2SEdward Tomasz Napierala 	[RACCT_NMSGQ] =
167097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
168097055e2SEdward Tomasz Napierala 	[RACCT_NSEM] =
169097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
170097055e2SEdward Tomasz Napierala 	[RACCT_NSEMOP] =
171097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
172097055e2SEdward Tomasz Napierala 	[RACCT_NSHM] =
173097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
174097055e2SEdward Tomasz Napierala 	[RACCT_SHMSIZE] =
175097055e2SEdward Tomasz Napierala 		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
176097055e2SEdward Tomasz Napierala 	[RACCT_WALLCLOCK] =
17736af9869SEdward Tomasz Napierala 		RACCT_IN_MILLIONS,
17836af9869SEdward Tomasz Napierala 	[RACCT_PCTCPU] =
179ae34b6ffSEdward Tomasz Napierala 		RACCT_DECAYING | RACCT_DENIABLE | RACCT_IN_MILLIONS,
180ae34b6ffSEdward Tomasz Napierala 	[RACCT_READBPS] =
181ae34b6ffSEdward Tomasz Napierala 		RACCT_DECAYING,
182ae34b6ffSEdward Tomasz Napierala 	[RACCT_WRITEBPS] =
183ae34b6ffSEdward Tomasz Napierala 		RACCT_DECAYING,
184ae34b6ffSEdward Tomasz Napierala 	[RACCT_READIOPS] =
185ae34b6ffSEdward Tomasz Napierala 		RACCT_DECAYING,
186ae34b6ffSEdward Tomasz Napierala 	[RACCT_WRITEIOPS] =
187ae34b6ffSEdward Tomasz Napierala 		RACCT_DECAYING };
18836af9869SEdward Tomasz Napierala 
18936af9869SEdward Tomasz Napierala static const fixpt_t RACCT_DECAY_FACTOR = 0.3 * FSCALE;
19036af9869SEdward Tomasz Napierala 
19136af9869SEdward Tomasz Napierala #ifdef SCHED_4BSD
19236af9869SEdward Tomasz Napierala /*
19336af9869SEdward Tomasz Napierala  * Contains intermediate values for %cpu calculations to avoid using floating
19436af9869SEdward Tomasz Napierala  * point in the kernel.
19536af9869SEdward Tomasz Napierala  * ccpu_exp[k] = FSCALE * (ccpu/FSCALE)^k = FSCALE * exp(-k/20)
19636af9869SEdward Tomasz Napierala  * It is needed only for the 4BSD scheduler, because in ULE, the ccpu equals to
19736af9869SEdward Tomasz Napierala  * zero so the calculations are more straightforward.
19836af9869SEdward Tomasz Napierala  */
19936af9869SEdward Tomasz Napierala fixpt_t ccpu_exp[] = {
20036af9869SEdward Tomasz Napierala 	[0] = FSCALE * 1,
20136af9869SEdward Tomasz Napierala 	[1] = FSCALE * 0.95122942450071400909,
20236af9869SEdward Tomasz Napierala 	[2] = FSCALE * 0.90483741803595957316,
20336af9869SEdward Tomasz Napierala 	[3] = FSCALE * 0.86070797642505780722,
20436af9869SEdward Tomasz Napierala 	[4] = FSCALE * 0.81873075307798185866,
20536af9869SEdward Tomasz Napierala 	[5] = FSCALE * 0.77880078307140486824,
20636af9869SEdward Tomasz Napierala 	[6] = FSCALE * 0.74081822068171786606,
20736af9869SEdward Tomasz Napierala 	[7] = FSCALE * 0.70468808971871343435,
20836af9869SEdward Tomasz Napierala 	[8] = FSCALE * 0.67032004603563930074,
20936af9869SEdward Tomasz Napierala 	[9] = FSCALE * 0.63762815162177329314,
21036af9869SEdward Tomasz Napierala 	[10] = FSCALE * 0.60653065971263342360,
21136af9869SEdward Tomasz Napierala 	[11] = FSCALE * 0.57694981038048669531,
21236af9869SEdward Tomasz Napierala 	[12] = FSCALE * 0.54881163609402643262,
21336af9869SEdward Tomasz Napierala 	[13] = FSCALE * 0.52204577676101604789,
21436af9869SEdward Tomasz Napierala 	[14] = FSCALE * 0.49658530379140951470,
21536af9869SEdward Tomasz Napierala 	[15] = FSCALE * 0.47236655274101470713,
21636af9869SEdward Tomasz Napierala 	[16] = FSCALE * 0.44932896411722159143,
21736af9869SEdward Tomasz Napierala 	[17] = FSCALE * 0.42741493194872666992,
21836af9869SEdward Tomasz Napierala 	[18] = FSCALE * 0.40656965974059911188,
21936af9869SEdward Tomasz Napierala 	[19] = FSCALE * 0.38674102345450120691,
22036af9869SEdward Tomasz Napierala 	[20] = FSCALE * 0.36787944117144232159,
22136af9869SEdward Tomasz Napierala 	[21] = FSCALE * 0.34993774911115535467,
22236af9869SEdward Tomasz Napierala 	[22] = FSCALE * 0.33287108369807955328,
22336af9869SEdward Tomasz Napierala 	[23] = FSCALE * 0.31663676937905321821,
22436af9869SEdward Tomasz Napierala 	[24] = FSCALE * 0.30119421191220209664,
22536af9869SEdward Tomasz Napierala 	[25] = FSCALE * 0.28650479686019010032,
22636af9869SEdward Tomasz Napierala 	[26] = FSCALE * 0.27253179303401260312,
22736af9869SEdward Tomasz Napierala 	[27] = FSCALE * 0.25924026064589150757,
22836af9869SEdward Tomasz Napierala 	[28] = FSCALE * 0.24659696394160647693,
22936af9869SEdward Tomasz Napierala 	[29] = FSCALE * 0.23457028809379765313,
23036af9869SEdward Tomasz Napierala 	[30] = FSCALE * 0.22313016014842982893,
23136af9869SEdward Tomasz Napierala 	[31] = FSCALE * 0.21224797382674305771,
23236af9869SEdward Tomasz Napierala 	[32] = FSCALE * 0.20189651799465540848,
23336af9869SEdward Tomasz Napierala 	[33] = FSCALE * 0.19204990862075411423,
23436af9869SEdward Tomasz Napierala 	[34] = FSCALE * 0.18268352405273465022,
23536af9869SEdward Tomasz Napierala 	[35] = FSCALE * 0.17377394345044512668,
23636af9869SEdward Tomasz Napierala 	[36] = FSCALE * 0.16529888822158653829,
23736af9869SEdward Tomasz Napierala 	[37] = FSCALE * 0.15723716631362761621,
23836af9869SEdward Tomasz Napierala 	[38] = FSCALE * 0.14956861922263505264,
23936af9869SEdward Tomasz Napierala 	[39] = FSCALE * 0.14227407158651357185,
24036af9869SEdward Tomasz Napierala 	[40] = FSCALE * 0.13533528323661269189,
24136af9869SEdward Tomasz Napierala 	[41] = FSCALE * 0.12873490358780421886,
24236af9869SEdward Tomasz Napierala 	[42] = FSCALE * 0.12245642825298191021,
24336af9869SEdward Tomasz Napierala 	[43] = FSCALE * 0.11648415777349695786,
24436af9869SEdward Tomasz Napierala 	[44] = FSCALE * 0.11080315836233388333,
24536af9869SEdward Tomasz Napierala 	[45] = FSCALE * 0.10539922456186433678,
24636af9869SEdward Tomasz Napierala 	[46] = FSCALE * 0.10025884372280373372,
24736af9869SEdward Tomasz Napierala 	[47] = FSCALE * 0.09536916221554961888,
24836af9869SEdward Tomasz Napierala 	[48] = FSCALE * 0.09071795328941250337,
24936af9869SEdward Tomasz Napierala 	[49] = FSCALE * 0.08629358649937051097,
25036af9869SEdward Tomasz Napierala 	[50] = FSCALE * 0.08208499862389879516,
25136af9869SEdward Tomasz Napierala 	[51] = FSCALE * 0.07808166600115315231,
25236af9869SEdward Tomasz Napierala 	[52] = FSCALE * 0.07427357821433388042,
25336af9869SEdward Tomasz Napierala 	[53] = FSCALE * 0.07065121306042958674,
25436af9869SEdward Tomasz Napierala 	[54] = FSCALE * 0.06720551273974976512,
25536af9869SEdward Tomasz Napierala 	[55] = FSCALE * 0.06392786120670757270,
25636af9869SEdward Tomasz Napierala 	[56] = FSCALE * 0.06081006262521796499,
25736af9869SEdward Tomasz Napierala 	[57] = FSCALE * 0.05784432087483846296,
25836af9869SEdward Tomasz Napierala 	[58] = FSCALE * 0.05502322005640722902,
25936af9869SEdward Tomasz Napierala 	[59] = FSCALE * 0.05233970594843239308,
26036af9869SEdward Tomasz Napierala 	[60] = FSCALE * 0.04978706836786394297,
26136af9869SEdward Tomasz Napierala 	[61] = FSCALE * 0.04735892439114092119,
26236af9869SEdward Tomasz Napierala 	[62] = FSCALE * 0.04504920239355780606,
26336af9869SEdward Tomasz Napierala 	[63] = FSCALE * 0.04285212686704017991,
26436af9869SEdward Tomasz Napierala 	[64] = FSCALE * 0.04076220397836621516,
26536af9869SEdward Tomasz Napierala 	[65] = FSCALE * 0.03877420783172200988,
26636af9869SEdward Tomasz Napierala 	[66] = FSCALE * 0.03688316740124000544,
26736af9869SEdward Tomasz Napierala 	[67] = FSCALE * 0.03508435410084502588,
26836af9869SEdward Tomasz Napierala 	[68] = FSCALE * 0.03337326996032607948,
26936af9869SEdward Tomasz Napierala 	[69] = FSCALE * 0.03174563637806794323,
27036af9869SEdward Tomasz Napierala 	[70] = FSCALE * 0.03019738342231850073,
27136af9869SEdward Tomasz Napierala 	[71] = FSCALE * 0.02872463965423942912,
27236af9869SEdward Tomasz Napierala 	[72] = FSCALE * 0.02732372244729256080,
27336af9869SEdward Tomasz Napierala 	[73] = FSCALE * 0.02599112877875534358,
27436af9869SEdward Tomasz Napierala 	[74] = FSCALE * 0.02472352647033939120,
27536af9869SEdward Tomasz Napierala 	[75] = FSCALE * 0.02351774585600910823,
27636af9869SEdward Tomasz Napierala 	[76] = FSCALE * 0.02237077185616559577,
27736af9869SEdward Tomasz Napierala 	[77] = FSCALE * 0.02127973643837716938,
27836af9869SEdward Tomasz Napierala 	[78] = FSCALE * 0.02024191144580438847,
27936af9869SEdward Tomasz Napierala 	[79] = FSCALE * 0.01925470177538692429,
28036af9869SEdward Tomasz Napierala 	[80] = FSCALE * 0.01831563888873418029,
28136af9869SEdward Tomasz Napierala 	[81] = FSCALE * 0.01742237463949351138,
28236af9869SEdward Tomasz Napierala 	[82] = FSCALE * 0.01657267540176124754,
28336af9869SEdward Tomasz Napierala 	[83] = FSCALE * 0.01576441648485449082,
28436af9869SEdward Tomasz Napierala 	[84] = FSCALE * 0.01499557682047770621,
28536af9869SEdward Tomasz Napierala 	[85] = FSCALE * 0.01426423390899925527,
28636af9869SEdward Tomasz Napierala 	[86] = FSCALE * 0.01356855901220093175,
28736af9869SEdward Tomasz Napierala 	[87] = FSCALE * 0.01290681258047986886,
28836af9869SEdward Tomasz Napierala 	[88] = FSCALE * 0.01227733990306844117,
28936af9869SEdward Tomasz Napierala 	[89] = FSCALE * 0.01167856697039544521,
29036af9869SEdward Tomasz Napierala 	[90] = FSCALE * 0.01110899653824230649,
29136af9869SEdward Tomasz Napierala 	[91] = FSCALE * 0.01056720438385265337,
29236af9869SEdward Tomasz Napierala 	[92] = FSCALE * 0.01005183574463358164,
29336af9869SEdward Tomasz Napierala 	[93] = FSCALE * 0.00956160193054350793,
29436af9869SEdward Tomasz Napierala 	[94] = FSCALE * 0.00909527710169581709,
29536af9869SEdward Tomasz Napierala 	[95] = FSCALE * 0.00865169520312063417,
29636af9869SEdward Tomasz Napierala 	[96] = FSCALE * 0.00822974704902002884,
29736af9869SEdward Tomasz Napierala 	[97] = FSCALE * 0.00782837754922577143,
29836af9869SEdward Tomasz Napierala 	[98] = FSCALE * 0.00744658307092434051,
29936af9869SEdward Tomasz Napierala 	[99] = FSCALE * 0.00708340892905212004,
30036af9869SEdward Tomasz Napierala 	[100] = FSCALE * 0.00673794699908546709,
30136af9869SEdward Tomasz Napierala 	[101] = FSCALE * 0.00640933344625638184,
30236af9869SEdward Tomasz Napierala 	[102] = FSCALE * 0.00609674656551563610,
30336af9869SEdward Tomasz Napierala 	[103] = FSCALE * 0.00579940472684214321,
30436af9869SEdward Tomasz Napierala 	[104] = FSCALE * 0.00551656442076077241,
30536af9869SEdward Tomasz Napierala 	[105] = FSCALE * 0.00524751839918138427,
30636af9869SEdward Tomasz Napierala 	[106] = FSCALE * 0.00499159390691021621,
30736af9869SEdward Tomasz Napierala 	[107] = FSCALE * 0.00474815099941147558,
30836af9869SEdward Tomasz Napierala 	[108] = FSCALE * 0.00451658094261266798,
30936af9869SEdward Tomasz Napierala 	[109] = FSCALE * 0.00429630469075234057,
31036af9869SEdward Tomasz Napierala 	[110] = FSCALE * 0.00408677143846406699,
31136af9869SEdward Tomasz Napierala };
31236af9869SEdward Tomasz Napierala #endif
31336af9869SEdward Tomasz Napierala 
31436af9869SEdward Tomasz Napierala #define	CCPU_EXP_MAX	110
31536af9869SEdward Tomasz Napierala 
31636af9869SEdward Tomasz Napierala /*
31736af9869SEdward Tomasz Napierala  * This function is analogical to the getpcpu() function in the ps(1) command.
31836af9869SEdward Tomasz Napierala  * They should both calculate in the same way so that the racct %cpu
31936af9869SEdward Tomasz Napierala  * calculations are consistent with the values showed by the ps(1) tool.
32036af9869SEdward Tomasz Napierala  * The calculations are more complex in the 4BSD scheduler because of the value
32136af9869SEdward Tomasz Napierala  * of the ccpu variable.  In ULE it is defined to be zero which saves us some
32236af9869SEdward Tomasz Napierala  * work.
32336af9869SEdward Tomasz Napierala  */
32436af9869SEdward Tomasz Napierala static uint64_t
32536af9869SEdward Tomasz Napierala racct_getpcpu(struct proc *p, u_int pcpu)
32636af9869SEdward Tomasz Napierala {
32736af9869SEdward Tomasz Napierala 	u_int swtime;
32836af9869SEdward Tomasz Napierala #ifdef SCHED_4BSD
32936af9869SEdward Tomasz Napierala 	fixpt_t pctcpu, pctcpu_next;
33036af9869SEdward Tomasz Napierala #endif
33136af9869SEdward Tomasz Napierala #ifdef SMP
33236af9869SEdward Tomasz Napierala 	struct pcpu *pc;
33336af9869SEdward Tomasz Napierala 	int found;
33436af9869SEdward Tomasz Napierala #endif
33536af9869SEdward Tomasz Napierala 	fixpt_t p_pctcpu;
33636af9869SEdward Tomasz Napierala 	struct thread *td;
33736af9869SEdward Tomasz Napierala 
3384b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
3394b5c9cf6SEdward Tomasz Napierala 
34036af9869SEdward Tomasz Napierala 	/*
34136af9869SEdward Tomasz Napierala 	 * If the process is swapped out, we count its %cpu usage as zero.
34236af9869SEdward Tomasz Napierala 	 * This behaviour is consistent with the userland ps(1) tool.
34336af9869SEdward Tomasz Napierala 	 */
34436af9869SEdward Tomasz Napierala 	if ((p->p_flag & P_INMEM) == 0)
34536af9869SEdward Tomasz Napierala 		return (0);
34636af9869SEdward Tomasz Napierala 	swtime = (ticks - p->p_swtick) / hz;
34736af9869SEdward Tomasz Napierala 
34836af9869SEdward Tomasz Napierala 	/*
34936af9869SEdward Tomasz Napierala 	 * For short-lived processes, the sched_pctcpu() returns small
35036af9869SEdward Tomasz Napierala 	 * values even for cpu intensive processes.  Therefore we use
35136af9869SEdward Tomasz Napierala 	 * our own estimate in this case.
35236af9869SEdward Tomasz Napierala 	 */
35336af9869SEdward Tomasz Napierala 	if (swtime < RACCT_PCPU_SECS)
35436af9869SEdward Tomasz Napierala 		return (pcpu);
35536af9869SEdward Tomasz Napierala 
35636af9869SEdward Tomasz Napierala 	p_pctcpu = 0;
35736af9869SEdward Tomasz Napierala 	FOREACH_THREAD_IN_PROC(p, td) {
35836af9869SEdward Tomasz Napierala 		if (td == PCPU_GET(idlethread))
35936af9869SEdward Tomasz Napierala 			continue;
36036af9869SEdward Tomasz Napierala #ifdef SMP
36136af9869SEdward Tomasz Napierala 		found = 0;
36236af9869SEdward Tomasz Napierala 		STAILQ_FOREACH(pc, &cpuhead, pc_allcpu) {
36336af9869SEdward Tomasz Napierala 			if (td == pc->pc_idlethread) {
36436af9869SEdward Tomasz Napierala 				found = 1;
36536af9869SEdward Tomasz Napierala 				break;
36636af9869SEdward Tomasz Napierala 			}
36736af9869SEdward Tomasz Napierala 		}
36836af9869SEdward Tomasz Napierala 		if (found)
36936af9869SEdward Tomasz Napierala 			continue;
37036af9869SEdward Tomasz Napierala #endif
37136af9869SEdward Tomasz Napierala 		thread_lock(td);
37236af9869SEdward Tomasz Napierala #ifdef SCHED_4BSD
37336af9869SEdward Tomasz Napierala 		pctcpu = sched_pctcpu(td);
37436af9869SEdward Tomasz Napierala 		/* Count also the yet unfinished second. */
37536af9869SEdward Tomasz Napierala 		pctcpu_next = (pctcpu * ccpu_exp[1]) >> FSHIFT;
37636af9869SEdward Tomasz Napierala 		pctcpu_next += sched_pctcpu_delta(td);
37736af9869SEdward Tomasz Napierala 		p_pctcpu += max(pctcpu, pctcpu_next);
37836af9869SEdward Tomasz Napierala #else
37936af9869SEdward Tomasz Napierala 		/*
38036af9869SEdward Tomasz Napierala 		 * In ULE the %cpu statistics are updated on every
38136af9869SEdward Tomasz Napierala 		 * sched_pctcpu() call.  So special calculations to
38236af9869SEdward Tomasz Napierala 		 * account for the latest (unfinished) second are
38336af9869SEdward Tomasz Napierala 		 * not needed.
38436af9869SEdward Tomasz Napierala 		 */
38536af9869SEdward Tomasz Napierala 		p_pctcpu += sched_pctcpu(td);
38636af9869SEdward Tomasz Napierala #endif
38736af9869SEdward Tomasz Napierala 		thread_unlock(td);
38836af9869SEdward Tomasz Napierala 	}
38936af9869SEdward Tomasz Napierala 
39036af9869SEdward Tomasz Napierala #ifdef SCHED_4BSD
39136af9869SEdward Tomasz Napierala 	if (swtime <= CCPU_EXP_MAX)
39236af9869SEdward Tomasz Napierala 		return ((100 * (uint64_t)p_pctcpu * 1000000) /
39336af9869SEdward Tomasz Napierala 		    (FSCALE - ccpu_exp[swtime]));
39436af9869SEdward Tomasz Napierala #endif
39536af9869SEdward Tomasz Napierala 
39636af9869SEdward Tomasz Napierala 	return ((100 * (uint64_t)p_pctcpu * 1000000) / FSCALE);
39736af9869SEdward Tomasz Napierala }
398097055e2SEdward Tomasz Napierala 
399097055e2SEdward Tomasz Napierala static void
400097055e2SEdward Tomasz Napierala racct_add_racct(struct racct *dest, const struct racct *src)
401097055e2SEdward Tomasz Napierala {
402097055e2SEdward Tomasz Napierala 	int i;
403097055e2SEdward Tomasz Napierala 
4044b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
4054c230cdaSEdward Tomasz Napierala 	RACCT_LOCK_ASSERT();
406097055e2SEdward Tomasz Napierala 
407097055e2SEdward Tomasz Napierala 	/*
408097055e2SEdward Tomasz Napierala 	 * Update resource usage in dest.
409097055e2SEdward Tomasz Napierala 	 */
410097055e2SEdward Tomasz Napierala 	for (i = 0; i <= RACCT_MAX; i++) {
411097055e2SEdward Tomasz Napierala 		KASSERT(dest->r_resources[i] >= 0,
412baf85d0aSEdward Tomasz Napierala 		    ("%s: resource %d propagation meltdown: dest < 0",
413baf85d0aSEdward Tomasz Napierala 		    __func__, i));
414097055e2SEdward Tomasz Napierala 		KASSERT(src->r_resources[i] >= 0,
415baf85d0aSEdward Tomasz Napierala 		    ("%s: resource %d propagation meltdown: src < 0",
416baf85d0aSEdward Tomasz Napierala 		    __func__, i));
417097055e2SEdward Tomasz Napierala 		dest->r_resources[i] += src->r_resources[i];
418097055e2SEdward Tomasz Napierala 	}
419097055e2SEdward Tomasz Napierala }
420097055e2SEdward Tomasz Napierala 
421097055e2SEdward Tomasz Napierala static void
422097055e2SEdward Tomasz Napierala racct_sub_racct(struct racct *dest, const struct racct *src)
423097055e2SEdward Tomasz Napierala {
424097055e2SEdward Tomasz Napierala 	int i;
425097055e2SEdward Tomasz Napierala 
4264b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
4274c230cdaSEdward Tomasz Napierala 	RACCT_LOCK_ASSERT();
428097055e2SEdward Tomasz Napierala 
429097055e2SEdward Tomasz Napierala 	/*
430097055e2SEdward Tomasz Napierala 	 * Update resource usage in dest.
431097055e2SEdward Tomasz Napierala 	 */
432097055e2SEdward Tomasz Napierala 	for (i = 0; i <= RACCT_MAX; i++) {
43384c9193bSEdward Tomasz Napierala 		if (!RACCT_IS_SLOPPY(i) && !RACCT_IS_DECAYING(i)) {
434097055e2SEdward Tomasz Napierala 			KASSERT(dest->r_resources[i] >= 0,
435baf85d0aSEdward Tomasz Napierala 			    ("%s: resource %d propagation meltdown: dest < 0",
436baf85d0aSEdward Tomasz Napierala 			    __func__, i));
437097055e2SEdward Tomasz Napierala 			KASSERT(src->r_resources[i] >= 0,
438baf85d0aSEdward Tomasz Napierala 			    ("%s: resource %d propagation meltdown: src < 0",
439baf85d0aSEdward Tomasz Napierala 			    __func__, i));
440097055e2SEdward Tomasz Napierala 			KASSERT(src->r_resources[i] <= dest->r_resources[i],
441baf85d0aSEdward Tomasz Napierala 			    ("%s: resource %d propagation meltdown: src > dest",
442baf85d0aSEdward Tomasz Napierala 			    __func__, i));
443097055e2SEdward Tomasz Napierala 		}
44436af9869SEdward Tomasz Napierala 		if (RACCT_CAN_DROP(i)) {
445097055e2SEdward Tomasz Napierala 			dest->r_resources[i] -= src->r_resources[i];
446*5c939660SEdward Tomasz Napierala 			if (dest->r_resources[i] < 0)
447097055e2SEdward Tomasz Napierala 				dest->r_resources[i] = 0;
448097055e2SEdward Tomasz Napierala 		}
449097055e2SEdward Tomasz Napierala 	}
450097055e2SEdward Tomasz Napierala }
451097055e2SEdward Tomasz Napierala 
452097055e2SEdward Tomasz Napierala void
453097055e2SEdward Tomasz Napierala racct_create(struct racct **racctp)
454097055e2SEdward Tomasz Napierala {
455097055e2SEdward Tomasz Napierala 
4564b5c9cf6SEdward Tomasz Napierala 	if (!racct_enable)
4574b5c9cf6SEdward Tomasz Napierala 		return;
4584b5c9cf6SEdward Tomasz Napierala 
45936160958SMark Johnston 	SDT_PROBE1(racct, , racct, create, racctp);
460097055e2SEdward Tomasz Napierala 
461097055e2SEdward Tomasz Napierala 	KASSERT(*racctp == NULL, ("racct already allocated"));
462097055e2SEdward Tomasz Napierala 
463097055e2SEdward Tomasz Napierala 	*racctp = uma_zalloc(racct_zone, M_WAITOK | M_ZERO);
464097055e2SEdward Tomasz Napierala }
465097055e2SEdward Tomasz Napierala 
466097055e2SEdward Tomasz Napierala static void
467097055e2SEdward Tomasz Napierala racct_destroy_locked(struct racct **racctp)
468097055e2SEdward Tomasz Napierala {
469097055e2SEdward Tomasz Napierala 	struct racct *racct;
470c1a43e73SEdward Tomasz Napierala 	int i;
471097055e2SEdward Tomasz Napierala 
4724b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
4734b5c9cf6SEdward Tomasz Napierala 
47436160958SMark Johnston 	SDT_PROBE1(racct, , racct, destroy, racctp);
475097055e2SEdward Tomasz Napierala 
4764c230cdaSEdward Tomasz Napierala 	RACCT_LOCK_ASSERT();
477097055e2SEdward Tomasz Napierala 	KASSERT(racctp != NULL, ("NULL racctp"));
478097055e2SEdward Tomasz Napierala 	KASSERT(*racctp != NULL, ("NULL racct"));
479097055e2SEdward Tomasz Napierala 
480097055e2SEdward Tomasz Napierala 	racct = *racctp;
481097055e2SEdward Tomasz Napierala 
482097055e2SEdward Tomasz Napierala 	for (i = 0; i <= RACCT_MAX; i++) {
4834fe84775SEdward Tomasz Napierala 		if (RACCT_IS_SLOPPY(i))
484097055e2SEdward Tomasz Napierala 			continue;
4854fe84775SEdward Tomasz Napierala 		if (!RACCT_IS_RECLAIMABLE(i))
486097055e2SEdward Tomasz Napierala 			continue;
487097055e2SEdward Tomasz Napierala 		KASSERT(racct->r_resources[i] == 0,
488097055e2SEdward Tomasz Napierala 		    ("destroying non-empty racct: "
489097055e2SEdward Tomasz Napierala 		    "%ju allocated for resource %d\n",
490097055e2SEdward Tomasz Napierala 		    racct->r_resources[i], i));
491097055e2SEdward Tomasz Napierala 	}
492097055e2SEdward Tomasz Napierala 	uma_zfree(racct_zone, racct);
493097055e2SEdward Tomasz Napierala 	*racctp = NULL;
494097055e2SEdward Tomasz Napierala }
495097055e2SEdward Tomasz Napierala 
496097055e2SEdward Tomasz Napierala void
497097055e2SEdward Tomasz Napierala racct_destroy(struct racct **racct)
498097055e2SEdward Tomasz Napierala {
499097055e2SEdward Tomasz Napierala 
5004b5c9cf6SEdward Tomasz Napierala 	if (!racct_enable)
5014b5c9cf6SEdward Tomasz Napierala 		return;
5024b5c9cf6SEdward Tomasz Napierala 
5034c230cdaSEdward Tomasz Napierala 	RACCT_LOCK();
504097055e2SEdward Tomasz Napierala 	racct_destroy_locked(racct);
5054c230cdaSEdward Tomasz Napierala 	RACCT_UNLOCK();
506097055e2SEdward Tomasz Napierala }
507097055e2SEdward Tomasz Napierala 
508097055e2SEdward Tomasz Napierala /*
509af1a7b25SEdward Tomasz Napierala  * Increase consumption of 'resource' by 'amount' for 'racct',
510af1a7b25SEdward Tomasz Napierala  * but not its parents.  Differently from other cases, 'amount' here
511097055e2SEdward Tomasz Napierala  * may be less than zero.
512097055e2SEdward Tomasz Napierala  */
513097055e2SEdward Tomasz Napierala static void
5143768a5dfSJeremie Le Hen racct_adjust_resource(struct racct *racct, int resource,
515a8e0740eSEdward Tomasz Napierala     int64_t amount)
516097055e2SEdward Tomasz Napierala {
517097055e2SEdward Tomasz Napierala 
5184b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
5194c230cdaSEdward Tomasz Napierala 	RACCT_LOCK_ASSERT();
520097055e2SEdward Tomasz Napierala 	KASSERT(racct != NULL, ("NULL racct"));
521097055e2SEdward Tomasz Napierala 
522097055e2SEdward Tomasz Napierala 	racct->r_resources[resource] += amount;
523097055e2SEdward Tomasz Napierala 	if (racct->r_resources[resource] < 0) {
52436af9869SEdward Tomasz Napierala 		KASSERT(RACCT_IS_SLOPPY(resource) || RACCT_IS_DECAYING(resource),
525baf85d0aSEdward Tomasz Napierala 		    ("%s: resource %d usage < 0", __func__, resource));
526097055e2SEdward Tomasz Napierala 		racct->r_resources[resource] = 0;
527097055e2SEdward Tomasz Napierala 	}
52836af9869SEdward Tomasz Napierala 
52936af9869SEdward Tomasz Napierala 	/*
53036af9869SEdward Tomasz Napierala 	 * There are some cases where the racct %cpu resource would grow
53109766dd5SJosh Paetzel 	 * beyond 100% per core.  For example in racct_proc_exit() we add
53209766dd5SJosh Paetzel 	 * the process %cpu usage to the ucred racct containers.  If too
53309766dd5SJosh Paetzel 	 * many processes terminated in a short time span, the ucred %cpu
53409766dd5SJosh Paetzel 	 * resource could grow too much.  Also, the 4BSD scheduler sometimes
53509766dd5SJosh Paetzel 	 * returns for a thread more than 100% cpu usage. So we set a sane
53609766dd5SJosh Paetzel 	 * boundary here to 100% * the maxumum number of CPUs.
53736af9869SEdward Tomasz Napierala 	 */
53836af9869SEdward Tomasz Napierala 	if ((resource == RACCT_PCTCPU) &&
53909766dd5SJosh Paetzel 	    (racct->r_resources[RACCT_PCTCPU] > 100 * 1000000 * (int64_t)MAXCPU))
54009766dd5SJosh Paetzel 		racct->r_resources[RACCT_PCTCPU] = 100 * 1000000 * (int64_t)MAXCPU;
541097055e2SEdward Tomasz Napierala }
542097055e2SEdward Tomasz Napierala 
5432d8696d1SEdward Tomasz Napierala static int
5447cea96f6SEdward Tomasz Napierala racct_add_locked(struct proc *p, int resource, uint64_t amount, int force)
545097055e2SEdward Tomasz Napierala {
546097055e2SEdward Tomasz Napierala #ifdef RCTL
547097055e2SEdward Tomasz Napierala 	int error;
548097055e2SEdward Tomasz Napierala #endif
549097055e2SEdward Tomasz Napierala 
5504b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
5514b5c9cf6SEdward Tomasz Napierala 
552097055e2SEdward Tomasz Napierala 	/*
553097055e2SEdward Tomasz Napierala 	 * We need proc lock to dereference p->p_ucred.
554097055e2SEdward Tomasz Napierala 	 */
555097055e2SEdward Tomasz Napierala 	PROC_LOCK_ASSERT(p, MA_OWNED);
556097055e2SEdward Tomasz Napierala 
557097055e2SEdward Tomasz Napierala #ifdef RCTL
558097055e2SEdward Tomasz Napierala 	error = rctl_enforce(p, resource, amount);
559659e7466SEdward Tomasz Napierala 	if (error && !force && RACCT_IS_DENIABLE(resource)) {
56036160958SMark Johnston 		SDT_PROBE3(racct, , rusage, add__failure, p, resource, amount);
561097055e2SEdward Tomasz Napierala 		return (error);
562097055e2SEdward Tomasz Napierala 	}
563097055e2SEdward Tomasz Napierala #endif
5643768a5dfSJeremie Le Hen 	racct_adjust_resource(p->p_racct, resource, amount);
565097055e2SEdward Tomasz Napierala 	racct_add_cred_locked(p->p_ucred, resource, amount);
566097055e2SEdward Tomasz Napierala 
567097055e2SEdward Tomasz Napierala 	return (0);
568097055e2SEdward Tomasz Napierala }
569097055e2SEdward Tomasz Napierala 
5702d8696d1SEdward Tomasz Napierala /*
5712d8696d1SEdward Tomasz Napierala  * Increase allocation of 'resource' by 'amount' for process 'p'.
5722d8696d1SEdward Tomasz Napierala  * Return 0 if it's below limits, or errno, if it's not.
5732d8696d1SEdward Tomasz Napierala  */
5742d8696d1SEdward Tomasz Napierala int
5752d8696d1SEdward Tomasz Napierala racct_add(struct proc *p, int resource, uint64_t amount)
5762d8696d1SEdward Tomasz Napierala {
5772d8696d1SEdward Tomasz Napierala 	int error;
5782d8696d1SEdward Tomasz Napierala 
5794b5c9cf6SEdward Tomasz Napierala 	if (!racct_enable)
5804b5c9cf6SEdward Tomasz Napierala 		return (0);
5814b5c9cf6SEdward Tomasz Napierala 
5827cea96f6SEdward Tomasz Napierala 	SDT_PROBE3(racct, , rusage, add, p, resource, amount);
5837cea96f6SEdward Tomasz Napierala 
5844c230cdaSEdward Tomasz Napierala 	RACCT_LOCK();
5857cea96f6SEdward Tomasz Napierala 	error = racct_add_locked(p, resource, amount, 0);
5864c230cdaSEdward Tomasz Napierala 	RACCT_UNLOCK();
5872d8696d1SEdward Tomasz Napierala 	return (error);
5882d8696d1SEdward Tomasz Napierala }
5892d8696d1SEdward Tomasz Napierala 
5900b9f1ecbSEdward Tomasz Napierala /*
5910b9f1ecbSEdward Tomasz Napierala  * Increase allocation of 'resource' by 'amount' for process 'p'.
5920b9f1ecbSEdward Tomasz Napierala  * Doesn't check for limits and never fails.
5930b9f1ecbSEdward Tomasz Napierala  */
5940b9f1ecbSEdward Tomasz Napierala void
5950b9f1ecbSEdward Tomasz Napierala racct_add_force(struct proc *p, int resource, uint64_t amount)
5960b9f1ecbSEdward Tomasz Napierala {
5970b9f1ecbSEdward Tomasz Napierala 
5980b9f1ecbSEdward Tomasz Napierala 	if (!racct_enable)
5990b9f1ecbSEdward Tomasz Napierala 		return;
6000b9f1ecbSEdward Tomasz Napierala 
6010b9f1ecbSEdward Tomasz Napierala 	SDT_PROBE3(racct, , rusage, add__force, p, resource, amount);
6020b9f1ecbSEdward Tomasz Napierala 
6034c230cdaSEdward Tomasz Napierala 	RACCT_LOCK();
6040b9f1ecbSEdward Tomasz Napierala 	racct_add_locked(p, resource, amount, 1);
6054c230cdaSEdward Tomasz Napierala 	RACCT_UNLOCK();
6060b9f1ecbSEdward Tomasz Napierala }
6070b9f1ecbSEdward Tomasz Napierala 
608097055e2SEdward Tomasz Napierala static void
609097055e2SEdward Tomasz Napierala racct_add_cred_locked(struct ucred *cred, int resource, uint64_t amount)
610097055e2SEdward Tomasz Napierala {
611097055e2SEdward Tomasz Napierala 	struct prison *pr;
612097055e2SEdward Tomasz Napierala 
6134b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
6144b5c9cf6SEdward Tomasz Napierala 
6153768a5dfSJeremie Le Hen 	racct_adjust_resource(cred->cr_ruidinfo->ui_racct, resource, amount);
616097055e2SEdward Tomasz Napierala 	for (pr = cred->cr_prison; pr != NULL; pr = pr->pr_parent)
6173768a5dfSJeremie Le Hen 		racct_adjust_resource(pr->pr_prison_racct->prr_racct, resource,
618a7ad07bfSEdward Tomasz Napierala 		    amount);
6193768a5dfSJeremie Le Hen 	racct_adjust_resource(cred->cr_loginclass->lc_racct, resource, amount);
620097055e2SEdward Tomasz Napierala }
621097055e2SEdward Tomasz Napierala 
622097055e2SEdward Tomasz Napierala /*
623097055e2SEdward Tomasz Napierala  * Increase allocation of 'resource' by 'amount' for credential 'cred'.
624097055e2SEdward Tomasz Napierala  * Doesn't check for limits and never fails.
625097055e2SEdward Tomasz Napierala  */
626097055e2SEdward Tomasz Napierala void
627097055e2SEdward Tomasz Napierala racct_add_cred(struct ucred *cred, int resource, uint64_t amount)
628097055e2SEdward Tomasz Napierala {
629097055e2SEdward Tomasz Napierala 
6304b5c9cf6SEdward Tomasz Napierala 	if (!racct_enable)
6314b5c9cf6SEdward Tomasz Napierala 		return;
6324b5c9cf6SEdward Tomasz Napierala 
633bbe4eb6dSEdward Tomasz Napierala 	SDT_PROBE3(racct, , rusage, add__cred, cred, resource, amount);
634bbe4eb6dSEdward Tomasz Napierala 
6354c230cdaSEdward Tomasz Napierala 	RACCT_LOCK();
636097055e2SEdward Tomasz Napierala 	racct_add_cred_locked(cred, resource, amount);
6374c230cdaSEdward Tomasz Napierala 	RACCT_UNLOCK();
638097055e2SEdward Tomasz Napierala }
639097055e2SEdward Tomasz Napierala 
640ae34b6ffSEdward Tomasz Napierala /*
641ae34b6ffSEdward Tomasz Napierala  * Account for disk IO resource consumption.  Checks for limits,
642ae34b6ffSEdward Tomasz Napierala  * but never fails, due to disk limits being undeniable.
643ae34b6ffSEdward Tomasz Napierala  */
644ae34b6ffSEdward Tomasz Napierala void
645ae34b6ffSEdward Tomasz Napierala racct_add_buf(struct proc *p, const struct buf *bp, int is_write)
646ae34b6ffSEdward Tomasz Napierala {
647ae34b6ffSEdward Tomasz Napierala 
648ae34b6ffSEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
649ae34b6ffSEdward Tomasz Napierala 	PROC_LOCK_ASSERT(p, MA_OWNED);
650ae34b6ffSEdward Tomasz Napierala 
651bbe4eb6dSEdward Tomasz Napierala 	SDT_PROBE3(racct, , rusage, add__buf, p, bp, is_write);
652bbe4eb6dSEdward Tomasz Napierala 
653ae34b6ffSEdward Tomasz Napierala 	RACCT_LOCK();
654ae34b6ffSEdward Tomasz Napierala 	if (is_write) {
655ae34b6ffSEdward Tomasz Napierala 		racct_add_locked(curproc, RACCT_WRITEBPS, bp->b_bcount, 1);
656ae34b6ffSEdward Tomasz Napierala 		racct_add_locked(curproc, RACCT_WRITEIOPS, 1, 1);
657ae34b6ffSEdward Tomasz Napierala 	} else {
658ae34b6ffSEdward Tomasz Napierala 		racct_add_locked(curproc, RACCT_READBPS, bp->b_bcount, 1);
659ae34b6ffSEdward Tomasz Napierala 		racct_add_locked(curproc, RACCT_READIOPS, 1, 1);
660ae34b6ffSEdward Tomasz Napierala 	}
661ae34b6ffSEdward Tomasz Napierala 	RACCT_UNLOCK();
662ae34b6ffSEdward Tomasz Napierala }
663ae34b6ffSEdward Tomasz Napierala 
664097055e2SEdward Tomasz Napierala static int
66510287198SEdward Tomasz Napierala racct_set_locked(struct proc *p, int resource, uint64_t amount, int force)
666097055e2SEdward Tomasz Napierala {
667c1a43e73SEdward Tomasz Napierala 	int64_t old_amount, decayed_amount, diff_proc, diff_cred;
668097055e2SEdward Tomasz Napierala #ifdef RCTL
669097055e2SEdward Tomasz Napierala 	int error;
670097055e2SEdward Tomasz Napierala #endif
671097055e2SEdward Tomasz Napierala 
6724b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
6734b5c9cf6SEdward Tomasz Napierala 
674097055e2SEdward Tomasz Napierala 	/*
675097055e2SEdward Tomasz Napierala 	 * We need proc lock to dereference p->p_ucred.
676097055e2SEdward Tomasz Napierala 	 */
677097055e2SEdward Tomasz Napierala 	PROC_LOCK_ASSERT(p, MA_OWNED);
678097055e2SEdward Tomasz Napierala 
67936af9869SEdward Tomasz Napierala 	old_amount = p->p_racct->r_resources[resource];
68036af9869SEdward Tomasz Napierala 	/*
68136af9869SEdward Tomasz Napierala 	 * The diffs may be negative.
68236af9869SEdward Tomasz Napierala 	 */
68336af9869SEdward Tomasz Napierala 	diff_proc = amount - old_amount;
684ae34b6ffSEdward Tomasz Napierala 	if (resource == RACCT_PCTCPU) {
68536af9869SEdward Tomasz Napierala 		/*
68636af9869SEdward Tomasz Napierala 		 * Resources in per-credential racct containers may decay.
68736af9869SEdward Tomasz Napierala 		 * If this is the case, we need to calculate the difference
68836af9869SEdward Tomasz Napierala 		 * between the new amount and the proportional value of the
68936af9869SEdward Tomasz Napierala 		 * old amount that has decayed in the ucred racct containers.
69036af9869SEdward Tomasz Napierala 		 */
69136af9869SEdward Tomasz Napierala 		decayed_amount = old_amount * RACCT_DECAY_FACTOR / FSCALE;
69236af9869SEdward Tomasz Napierala 		diff_cred = amount - decayed_amount;
69336af9869SEdward Tomasz Napierala 	} else
69436af9869SEdward Tomasz Napierala 		diff_cred = diff_proc;
695097055e2SEdward Tomasz Napierala #ifdef notyet
69636af9869SEdward Tomasz Napierala 	KASSERT(diff_proc >= 0 || RACCT_CAN_DROP(resource),
697baf85d0aSEdward Tomasz Napierala 	    ("%s: usage of non-droppable resource %d dropping", __func__,
698097055e2SEdward Tomasz Napierala 	     resource));
699097055e2SEdward Tomasz Napierala #endif
700097055e2SEdward Tomasz Napierala #ifdef RCTL
701659e7466SEdward Tomasz Napierala 	if (diff_proc > 0) {
70236af9869SEdward Tomasz Napierala 		error = rctl_enforce(p, resource, diff_proc);
703659e7466SEdward Tomasz Napierala 		if (error && !force && RACCT_IS_DENIABLE(resource)) {
70436160958SMark Johnston 			SDT_PROBE3(racct, , rusage, set__failure, p, resource,
70536160958SMark Johnston 			    amount);
706097055e2SEdward Tomasz Napierala 			return (error);
707097055e2SEdward Tomasz Napierala 		}
708097055e2SEdward Tomasz Napierala 	}
709097055e2SEdward Tomasz Napierala #endif
7103768a5dfSJeremie Le Hen 	racct_adjust_resource(p->p_racct, resource, diff_proc);
71136af9869SEdward Tomasz Napierala 	if (diff_cred > 0)
71236af9869SEdward Tomasz Napierala 		racct_add_cred_locked(p->p_ucred, resource, diff_cred);
71336af9869SEdward Tomasz Napierala 	else if (diff_cred < 0)
71436af9869SEdward Tomasz Napierala 		racct_sub_cred_locked(p->p_ucred, resource, -diff_cred);
715097055e2SEdward Tomasz Napierala 
716097055e2SEdward Tomasz Napierala 	return (0);
717097055e2SEdward Tomasz Napierala }
718097055e2SEdward Tomasz Napierala 
719097055e2SEdward Tomasz Napierala /*
720097055e2SEdward Tomasz Napierala  * Set allocation of 'resource' to 'amount' for process 'p'.
721097055e2SEdward Tomasz Napierala  * Return 0 if it's below limits, or errno, if it's not.
722097055e2SEdward Tomasz Napierala  *
723097055e2SEdward Tomasz Napierala  * Note that decreasing the allocation always returns 0,
724097055e2SEdward Tomasz Napierala  * even if it's above the limit.
725097055e2SEdward Tomasz Napierala  */
726097055e2SEdward Tomasz Napierala int
727097055e2SEdward Tomasz Napierala racct_set(struct proc *p, int resource, uint64_t amount)
728097055e2SEdward Tomasz Napierala {
729097055e2SEdward Tomasz Napierala 	int error;
730097055e2SEdward Tomasz Napierala 
7314b5c9cf6SEdward Tomasz Napierala 	if (!racct_enable)
7324b5c9cf6SEdward Tomasz Napierala 		return (0);
7334b5c9cf6SEdward Tomasz Napierala 
73410287198SEdward Tomasz Napierala 	SDT_PROBE3(racct, , rusage, set__force, p, resource, amount);
73510287198SEdward Tomasz Napierala 
7364c230cdaSEdward Tomasz Napierala 	RACCT_LOCK();
73710287198SEdward Tomasz Napierala 	error = racct_set_locked(p, resource, amount, 0);
7384c230cdaSEdward Tomasz Napierala 	RACCT_UNLOCK();
739097055e2SEdward Tomasz Napierala 	return (error);
740097055e2SEdward Tomasz Napierala }
741097055e2SEdward Tomasz Napierala 
7420b9f1ecbSEdward Tomasz Napierala void
7430b9f1ecbSEdward Tomasz Napierala racct_set_force(struct proc *p, int resource, uint64_t amount)
7440b9f1ecbSEdward Tomasz Napierala {
7450b9f1ecbSEdward Tomasz Napierala 
7460b9f1ecbSEdward Tomasz Napierala 	if (!racct_enable)
7470b9f1ecbSEdward Tomasz Napierala 		return;
7480b9f1ecbSEdward Tomasz Napierala 
7490b9f1ecbSEdward Tomasz Napierala 	SDT_PROBE3(racct, , rusage, set, p, resource, amount);
7500b9f1ecbSEdward Tomasz Napierala 
7514c230cdaSEdward Tomasz Napierala 	RACCT_LOCK();
7520b9f1ecbSEdward Tomasz Napierala 	racct_set_locked(p, resource, amount, 1);
7534c230cdaSEdward Tomasz Napierala 	RACCT_UNLOCK();
7540b9f1ecbSEdward Tomasz Napierala }
7550b9f1ecbSEdward Tomasz Napierala 
756097055e2SEdward Tomasz Napierala /*
757097055e2SEdward Tomasz Napierala  * Returns amount of 'resource' the process 'p' can keep allocated.
758097055e2SEdward Tomasz Napierala  * Allocating more than that would be denied, unless the resource
759097055e2SEdward Tomasz Napierala  * is marked undeniable.  Amount of already allocated resource does
760097055e2SEdward Tomasz Napierala  * not matter.
761097055e2SEdward Tomasz Napierala  */
762097055e2SEdward Tomasz Napierala uint64_t
763097055e2SEdward Tomasz Napierala racct_get_limit(struct proc *p, int resource)
764097055e2SEdward Tomasz Napierala {
765bbe4eb6dSEdward Tomasz Napierala #ifdef RCTL
766bbe4eb6dSEdward Tomasz Napierala 	uint64_t available;
767097055e2SEdward Tomasz Napierala 
7684b5c9cf6SEdward Tomasz Napierala 	if (!racct_enable)
7694b5c9cf6SEdward Tomasz Napierala 		return (UINT64_MAX);
7704b5c9cf6SEdward Tomasz Napierala 
771bbe4eb6dSEdward Tomasz Napierala 	RACCT_LOCK();
772bbe4eb6dSEdward Tomasz Napierala 	available = rctl_get_limit(p, resource);
773bbe4eb6dSEdward Tomasz Napierala 	RACCT_UNLOCK();
774bbe4eb6dSEdward Tomasz Napierala 
775bbe4eb6dSEdward Tomasz Napierala 	return (available);
776097055e2SEdward Tomasz Napierala #else
777bbe4eb6dSEdward Tomasz Napierala 
778097055e2SEdward Tomasz Napierala 	return (UINT64_MAX);
779097055e2SEdward Tomasz Napierala #endif
780097055e2SEdward Tomasz Napierala }
781097055e2SEdward Tomasz Napierala 
782097055e2SEdward Tomasz Napierala /*
783097055e2SEdward Tomasz Napierala  * Returns amount of 'resource' the process 'p' can keep allocated.
784097055e2SEdward Tomasz Napierala  * Allocating more than that would be denied, unless the resource
785097055e2SEdward Tomasz Napierala  * is marked undeniable.  Amount of already allocated resource does
786097055e2SEdward Tomasz Napierala  * matter.
787097055e2SEdward Tomasz Napierala  */
788097055e2SEdward Tomasz Napierala uint64_t
789097055e2SEdward Tomasz Napierala racct_get_available(struct proc *p, int resource)
790097055e2SEdward Tomasz Napierala {
791bbe4eb6dSEdward Tomasz Napierala #ifdef RCTL
792bbe4eb6dSEdward Tomasz Napierala 	uint64_t available;
793097055e2SEdward Tomasz Napierala 
7944b5c9cf6SEdward Tomasz Napierala 	if (!racct_enable)
7954b5c9cf6SEdward Tomasz Napierala 		return (UINT64_MAX);
7964b5c9cf6SEdward Tomasz Napierala 
797bbe4eb6dSEdward Tomasz Napierala 	RACCT_LOCK();
798bbe4eb6dSEdward Tomasz Napierala 	available = rctl_get_available(p, resource);
799bbe4eb6dSEdward Tomasz Napierala 	RACCT_UNLOCK();
800bbe4eb6dSEdward Tomasz Napierala 
801bbe4eb6dSEdward Tomasz Napierala 	return (available);
802097055e2SEdward Tomasz Napierala #else
803bbe4eb6dSEdward Tomasz Napierala 
804097055e2SEdward Tomasz Napierala 	return (UINT64_MAX);
805097055e2SEdward Tomasz Napierala #endif
806097055e2SEdward Tomasz Napierala }
807097055e2SEdward Tomasz Napierala 
808097055e2SEdward Tomasz Napierala /*
80936af9869SEdward Tomasz Napierala  * Returns amount of the %cpu resource that process 'p' can add to its %cpu
81036af9869SEdward Tomasz Napierala  * utilization.  Adding more than that would lead to the process being
81136af9869SEdward Tomasz Napierala  * throttled.
81236af9869SEdward Tomasz Napierala  */
81336af9869SEdward Tomasz Napierala static int64_t
81436af9869SEdward Tomasz Napierala racct_pcpu_available(struct proc *p)
81536af9869SEdward Tomasz Napierala {
816bbe4eb6dSEdward Tomasz Napierala #ifdef RCTL
817bbe4eb6dSEdward Tomasz Napierala 	uint64_t available;
81836af9869SEdward Tomasz Napierala 
8194b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
8204b5c9cf6SEdward Tomasz Napierala 
821bbe4eb6dSEdward Tomasz Napierala 	RACCT_LOCK();
822bbe4eb6dSEdward Tomasz Napierala 	available = rctl_pcpu_available(p);
823bbe4eb6dSEdward Tomasz Napierala 	RACCT_UNLOCK();
824bbe4eb6dSEdward Tomasz Napierala 
825bbe4eb6dSEdward Tomasz Napierala 	return (available);
82636af9869SEdward Tomasz Napierala #else
827bbe4eb6dSEdward Tomasz Napierala 
82836af9869SEdward Tomasz Napierala 	return (INT64_MAX);
82936af9869SEdward Tomasz Napierala #endif
83036af9869SEdward Tomasz Napierala }
83136af9869SEdward Tomasz Napierala 
83236af9869SEdward Tomasz Napierala /*
833097055e2SEdward Tomasz Napierala  * Decrease allocation of 'resource' by 'amount' for process 'p'.
834097055e2SEdward Tomasz Napierala  */
835097055e2SEdward Tomasz Napierala void
836097055e2SEdward Tomasz Napierala racct_sub(struct proc *p, int resource, uint64_t amount)
837097055e2SEdward Tomasz Napierala {
838097055e2SEdward Tomasz Napierala 
8394b5c9cf6SEdward Tomasz Napierala 	if (!racct_enable)
8404b5c9cf6SEdward Tomasz Napierala 		return;
8414b5c9cf6SEdward Tomasz Napierala 
84236160958SMark Johnston 	SDT_PROBE3(racct, , rusage, sub, p, resource, amount);
843097055e2SEdward Tomasz Napierala 
844097055e2SEdward Tomasz Napierala 	/*
845097055e2SEdward Tomasz Napierala 	 * We need proc lock to dereference p->p_ucred.
846097055e2SEdward Tomasz Napierala 	 */
847097055e2SEdward Tomasz Napierala 	PROC_LOCK_ASSERT(p, MA_OWNED);
84836af9869SEdward Tomasz Napierala 	KASSERT(RACCT_CAN_DROP(resource),
849baf85d0aSEdward Tomasz Napierala 	    ("%s: called for non-droppable resource %d", __func__, resource));
850097055e2SEdward Tomasz Napierala 
8514c230cdaSEdward Tomasz Napierala 	RACCT_LOCK();
852097055e2SEdward Tomasz Napierala 	KASSERT(amount <= p->p_racct->r_resources[resource],
853baf85d0aSEdward Tomasz Napierala 	    ("%s: freeing %ju of resource %d, which is more "
854baf85d0aSEdward Tomasz Napierala 	     "than allocated %jd for %s (pid %d)", __func__, amount, resource,
855097055e2SEdward Tomasz Napierala 	    (intmax_t)p->p_racct->r_resources[resource], p->p_comm, p->p_pid));
856097055e2SEdward Tomasz Napierala 
8573768a5dfSJeremie Le Hen 	racct_adjust_resource(p->p_racct, resource, -amount);
858097055e2SEdward Tomasz Napierala 	racct_sub_cred_locked(p->p_ucred, resource, amount);
8594c230cdaSEdward Tomasz Napierala 	RACCT_UNLOCK();
860097055e2SEdward Tomasz Napierala }
861097055e2SEdward Tomasz Napierala 
862097055e2SEdward Tomasz Napierala static void
863097055e2SEdward Tomasz Napierala racct_sub_cred_locked(struct ucred *cred, int resource, uint64_t amount)
864097055e2SEdward Tomasz Napierala {
865097055e2SEdward Tomasz Napierala 	struct prison *pr;
866097055e2SEdward Tomasz Napierala 
8674b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
8684b5c9cf6SEdward Tomasz Napierala 
8693768a5dfSJeremie Le Hen 	racct_adjust_resource(cred->cr_ruidinfo->ui_racct, resource, -amount);
870097055e2SEdward Tomasz Napierala 	for (pr = cred->cr_prison; pr != NULL; pr = pr->pr_parent)
8713768a5dfSJeremie Le Hen 		racct_adjust_resource(pr->pr_prison_racct->prr_racct, resource,
872a7ad07bfSEdward Tomasz Napierala 		    -amount);
8733768a5dfSJeremie Le Hen 	racct_adjust_resource(cred->cr_loginclass->lc_racct, resource, -amount);
874097055e2SEdward Tomasz Napierala }
875097055e2SEdward Tomasz Napierala 
876097055e2SEdward Tomasz Napierala /*
877097055e2SEdward Tomasz Napierala  * Decrease allocation of 'resource' by 'amount' for credential 'cred'.
878097055e2SEdward Tomasz Napierala  */
879097055e2SEdward Tomasz Napierala void
880097055e2SEdward Tomasz Napierala racct_sub_cred(struct ucred *cred, int resource, uint64_t amount)
881097055e2SEdward Tomasz Napierala {
882097055e2SEdward Tomasz Napierala 
8834b5c9cf6SEdward Tomasz Napierala 	if (!racct_enable)
8844b5c9cf6SEdward Tomasz Napierala 		return;
8854b5c9cf6SEdward Tomasz Napierala 
886bbe4eb6dSEdward Tomasz Napierala 	SDT_PROBE3(racct, , rusage, sub__cred, cred, resource, amount);
887bbe4eb6dSEdward Tomasz Napierala 
888bbe4eb6dSEdward Tomasz Napierala #ifdef notyet
889bbe4eb6dSEdward Tomasz Napierala 	KASSERT(RACCT_CAN_DROP(resource),
890bbe4eb6dSEdward Tomasz Napierala 	    ("%s: called for resource %d which can not drop", __func__,
891bbe4eb6dSEdward Tomasz Napierala 	     resource));
892bbe4eb6dSEdward Tomasz Napierala #endif
893bbe4eb6dSEdward Tomasz Napierala 
8944c230cdaSEdward Tomasz Napierala 	RACCT_LOCK();
895097055e2SEdward Tomasz Napierala 	racct_sub_cred_locked(cred, resource, amount);
8964c230cdaSEdward Tomasz Napierala 	RACCT_UNLOCK();
897097055e2SEdward Tomasz Napierala }
898097055e2SEdward Tomasz Napierala 
899097055e2SEdward Tomasz Napierala /*
900097055e2SEdward Tomasz Napierala  * Inherit resource usage information from the parent process.
901097055e2SEdward Tomasz Napierala  */
902097055e2SEdward Tomasz Napierala int
903097055e2SEdward Tomasz Napierala racct_proc_fork(struct proc *parent, struct proc *child)
904097055e2SEdward Tomasz Napierala {
905097055e2SEdward Tomasz Napierala 	int i, error = 0;
906097055e2SEdward Tomasz Napierala 
9074b5c9cf6SEdward Tomasz Napierala 	if (!racct_enable)
9084b5c9cf6SEdward Tomasz Napierala 		return (0);
9094b5c9cf6SEdward Tomasz Napierala 
910097055e2SEdward Tomasz Napierala 	/*
911097055e2SEdward Tomasz Napierala 	 * Create racct for the child process.
912097055e2SEdward Tomasz Napierala 	 */
913097055e2SEdward Tomasz Napierala 	racct_create(&child->p_racct);
914097055e2SEdward Tomasz Napierala 
915097055e2SEdward Tomasz Napierala 	PROC_LOCK(parent);
916097055e2SEdward Tomasz Napierala 	PROC_LOCK(child);
9174c230cdaSEdward Tomasz Napierala 	RACCT_LOCK();
918097055e2SEdward Tomasz Napierala 
919c0c09362SEdward Tomasz Napierala #ifdef RCTL
920c0c09362SEdward Tomasz Napierala 	error = rctl_proc_fork(parent, child);
921c0c09362SEdward Tomasz Napierala 	if (error != 0)
922c0c09362SEdward Tomasz Napierala 		goto out;
923c0c09362SEdward Tomasz Napierala #endif
924c0c09362SEdward Tomasz Napierala 
92536af9869SEdward Tomasz Napierala 	/* Init process cpu time. */
92636af9869SEdward Tomasz Napierala 	child->p_prev_runtime = 0;
92736af9869SEdward Tomasz Napierala 	child->p_throttled = 0;
92836af9869SEdward Tomasz Napierala 
929097055e2SEdward Tomasz Napierala 	/*
930097055e2SEdward Tomasz Napierala 	 * Inherit resource usage.
931097055e2SEdward Tomasz Napierala 	 */
932097055e2SEdward Tomasz Napierala 	for (i = 0; i <= RACCT_MAX; i++) {
933097055e2SEdward Tomasz Napierala 		if (parent->p_racct->r_resources[i] == 0 ||
9344fe84775SEdward Tomasz Napierala 		    !RACCT_IS_INHERITABLE(i))
935097055e2SEdward Tomasz Napierala 			continue;
936097055e2SEdward Tomasz Napierala 
937097055e2SEdward Tomasz Napierala 		error = racct_set_locked(child, i,
93810287198SEdward Tomasz Napierala 		    parent->p_racct->r_resources[i], 0);
939ac6fafe6SEdward Tomasz Napierala 		if (error != 0)
940097055e2SEdward Tomasz Napierala 			goto out;
941097055e2SEdward Tomasz Napierala 	}
942097055e2SEdward Tomasz Napierala 
9437cea96f6SEdward Tomasz Napierala 	error = racct_add_locked(child, RACCT_NPROC, 1, 0);
9447cea96f6SEdward Tomasz Napierala 	error += racct_add_locked(child, RACCT_NTHR, 1, 0);
9452d8696d1SEdward Tomasz Napierala 
946097055e2SEdward Tomasz Napierala out:
9474c230cdaSEdward Tomasz Napierala 	RACCT_UNLOCK();
948097055e2SEdward Tomasz Napierala 	PROC_UNLOCK(child);
949097055e2SEdward Tomasz Napierala 	PROC_UNLOCK(parent);
950097055e2SEdward Tomasz Napierala 
951ab27d5d8SEdward Tomasz Napierala 	if (error != 0)
952ab27d5d8SEdward Tomasz Napierala 		racct_proc_exit(child);
953ab27d5d8SEdward Tomasz Napierala 
954097055e2SEdward Tomasz Napierala 	return (error);
955097055e2SEdward Tomasz Napierala }
956097055e2SEdward Tomasz Napierala 
95772a401d9SEdward Tomasz Napierala /*
95872a401d9SEdward Tomasz Napierala  * Called at the end of fork1(), to handle rules that require the process
95972a401d9SEdward Tomasz Napierala  * to be fully initialized.
96072a401d9SEdward Tomasz Napierala  */
96172a401d9SEdward Tomasz Napierala void
96272a401d9SEdward Tomasz Napierala racct_proc_fork_done(struct proc *child)
96372a401d9SEdward Tomasz Napierala {
96472a401d9SEdward Tomasz Napierala 
9654b5c9cf6SEdward Tomasz Napierala 	if (!racct_enable)
9664b5c9cf6SEdward Tomasz Napierala 		return;
9674b5c9cf6SEdward Tomasz Napierala 
968bbe4eb6dSEdward Tomasz Napierala 	PROC_LOCK_ASSERT(child, MA_OWNED);
969bbe4eb6dSEdward Tomasz Napierala 
970bbe4eb6dSEdward Tomasz Napierala #ifdef RCTL
9714c230cdaSEdward Tomasz Napierala 	RACCT_LOCK();
97272a401d9SEdward Tomasz Napierala 	rctl_enforce(child, RACCT_NPROC, 0);
97372a401d9SEdward Tomasz Napierala 	rctl_enforce(child, RACCT_NTHR, 0);
9744c230cdaSEdward Tomasz Napierala 	RACCT_UNLOCK();
97572a401d9SEdward Tomasz Napierala #endif
97672a401d9SEdward Tomasz Napierala }
97772a401d9SEdward Tomasz Napierala 
978097055e2SEdward Tomasz Napierala void
979097055e2SEdward Tomasz Napierala racct_proc_exit(struct proc *p)
980097055e2SEdward Tomasz Napierala {
98136af9869SEdward Tomasz Napierala 	struct timeval wallclock;
982c1a43e73SEdward Tomasz Napierala 	uint64_t pct_estimate, pct, runtime;
983c1a43e73SEdward Tomasz Napierala 	int i;
984097055e2SEdward Tomasz Napierala 
9854b5c9cf6SEdward Tomasz Napierala 	if (!racct_enable)
9864b5c9cf6SEdward Tomasz Napierala 		return;
9874b5c9cf6SEdward Tomasz Napierala 
988097055e2SEdward Tomasz Napierala 	PROC_LOCK(p);
989097055e2SEdward Tomasz Napierala 	/*
990097055e2SEdward Tomasz Napierala 	 * We don't need to calculate rux, proc_reap() has already done this.
991097055e2SEdward Tomasz Napierala 	 */
992097055e2SEdward Tomasz Napierala 	runtime = cputick2usec(p->p_rux.rux_runtime);
993097055e2SEdward Tomasz Napierala #ifdef notyet
994097055e2SEdward Tomasz Napierala 	KASSERT(runtime >= p->p_prev_runtime, ("runtime < p_prev_runtime"));
995097055e2SEdward Tomasz Napierala #else
996097055e2SEdward Tomasz Napierala 	if (runtime < p->p_prev_runtime)
997097055e2SEdward Tomasz Napierala 		runtime = p->p_prev_runtime;
998097055e2SEdward Tomasz Napierala #endif
99936af9869SEdward Tomasz Napierala 	microuptime(&wallclock);
100036af9869SEdward Tomasz Napierala 	timevalsub(&wallclock, &p->p_stats->p_start);
100184590fd8SEdward Tomasz Napierala 	if (wallclock.tv_sec > 0 || wallclock.tv_usec > 0) {
100236af9869SEdward Tomasz Napierala 		pct_estimate = (1000000 * runtime * 100) /
100336af9869SEdward Tomasz Napierala 		    ((uint64_t)wallclock.tv_sec * 1000000 +
100436af9869SEdward Tomasz Napierala 		    wallclock.tv_usec);
100584590fd8SEdward Tomasz Napierala 	} else
100684590fd8SEdward Tomasz Napierala 		pct_estimate = 0;
100736af9869SEdward Tomasz Napierala 	pct = racct_getpcpu(p, pct_estimate);
100836af9869SEdward Tomasz Napierala 
10094c230cdaSEdward Tomasz Napierala 	RACCT_LOCK();
101010287198SEdward Tomasz Napierala 	racct_set_locked(p, RACCT_CPU, runtime, 0);
101136af9869SEdward Tomasz Napierala 	racct_add_cred_locked(p->p_ucred, RACCT_PCTCPU, pct);
1012097055e2SEdward Tomasz Napierala 
10132419d7f9SEdward Tomasz Napierala 	for (i = 0; i <= RACCT_MAX; i++) {
10142419d7f9SEdward Tomasz Napierala 		if (p->p_racct->r_resources[i] == 0)
10152419d7f9SEdward Tomasz Napierala 			continue;
10162419d7f9SEdward Tomasz Napierala 	    	if (!RACCT_IS_RECLAIMABLE(i))
10172419d7f9SEdward Tomasz Napierala 			continue;
101810287198SEdward Tomasz Napierala 		racct_set_locked(p, i, 0, 0);
10192419d7f9SEdward Tomasz Napierala 	}
10202419d7f9SEdward Tomasz Napierala 
1021097055e2SEdward Tomasz Napierala #ifdef RCTL
1022097055e2SEdward Tomasz Napierala 	rctl_racct_release(p->p_racct);
1023097055e2SEdward Tomasz Napierala #endif
1024bbe4eb6dSEdward Tomasz Napierala 	racct_destroy_locked(&p->p_racct);
1025bbe4eb6dSEdward Tomasz Napierala 	RACCT_UNLOCK();
1026bbe4eb6dSEdward Tomasz Napierala 	PROC_UNLOCK(p);
1027097055e2SEdward Tomasz Napierala }
1028097055e2SEdward Tomasz Napierala 
1029097055e2SEdward Tomasz Napierala /*
1030097055e2SEdward Tomasz Napierala  * Called after credentials change, to move resource utilisation
1031097055e2SEdward Tomasz Napierala  * between raccts.
1032097055e2SEdward Tomasz Napierala  */
1033097055e2SEdward Tomasz Napierala void
1034097055e2SEdward Tomasz Napierala racct_proc_ucred_changed(struct proc *p, struct ucred *oldcred,
1035097055e2SEdward Tomasz Napierala     struct ucred *newcred)
1036097055e2SEdward Tomasz Napierala {
1037097055e2SEdward Tomasz Napierala 	struct uidinfo *olduip, *newuip;
1038097055e2SEdward Tomasz Napierala 	struct loginclass *oldlc, *newlc;
1039097055e2SEdward Tomasz Napierala 	struct prison *oldpr, *newpr, *pr;
1040097055e2SEdward Tomasz Napierala 
10414b5c9cf6SEdward Tomasz Napierala 	if (!racct_enable)
10424b5c9cf6SEdward Tomasz Napierala 		return;
10434b5c9cf6SEdward Tomasz Napierala 
1044097055e2SEdward Tomasz Napierala 	PROC_LOCK_ASSERT(p, MA_NOTOWNED);
1045097055e2SEdward Tomasz Napierala 
1046097055e2SEdward Tomasz Napierala 	newuip = newcred->cr_ruidinfo;
1047097055e2SEdward Tomasz Napierala 	olduip = oldcred->cr_ruidinfo;
1048097055e2SEdward Tomasz Napierala 	newlc = newcred->cr_loginclass;
1049097055e2SEdward Tomasz Napierala 	oldlc = oldcred->cr_loginclass;
1050097055e2SEdward Tomasz Napierala 	newpr = newcred->cr_prison;
1051097055e2SEdward Tomasz Napierala 	oldpr = oldcred->cr_prison;
1052097055e2SEdward Tomasz Napierala 
10534c230cdaSEdward Tomasz Napierala 	RACCT_LOCK();
1054097055e2SEdward Tomasz Napierala 	if (newuip != olduip) {
1055097055e2SEdward Tomasz Napierala 		racct_sub_racct(olduip->ui_racct, p->p_racct);
1056097055e2SEdward Tomasz Napierala 		racct_add_racct(newuip->ui_racct, p->p_racct);
1057097055e2SEdward Tomasz Napierala 	}
1058097055e2SEdward Tomasz Napierala 	if (newlc != oldlc) {
1059097055e2SEdward Tomasz Napierala 		racct_sub_racct(oldlc->lc_racct, p->p_racct);
1060097055e2SEdward Tomasz Napierala 		racct_add_racct(newlc->lc_racct, p->p_racct);
1061097055e2SEdward Tomasz Napierala 	}
1062097055e2SEdward Tomasz Napierala 	if (newpr != oldpr) {
1063097055e2SEdward Tomasz Napierala 		for (pr = oldpr; pr != NULL; pr = pr->pr_parent)
1064a7ad07bfSEdward Tomasz Napierala 			racct_sub_racct(pr->pr_prison_racct->prr_racct,
1065a7ad07bfSEdward Tomasz Napierala 			    p->p_racct);
1066097055e2SEdward Tomasz Napierala 		for (pr = newpr; pr != NULL; pr = pr->pr_parent)
1067a7ad07bfSEdward Tomasz Napierala 			racct_add_racct(pr->pr_prison_racct->prr_racct,
1068a7ad07bfSEdward Tomasz Napierala 			    p->p_racct);
1069097055e2SEdward Tomasz Napierala 	}
10704c230cdaSEdward Tomasz Napierala 	RACCT_UNLOCK();
1071097055e2SEdward Tomasz Napierala 
1072097055e2SEdward Tomasz Napierala #ifdef RCTL
1073097055e2SEdward Tomasz Napierala 	rctl_proc_ucred_changed(p, newcred);
1074097055e2SEdward Tomasz Napierala #endif
1075097055e2SEdward Tomasz Napierala }
1076097055e2SEdward Tomasz Napierala 
1077c34bbd2aSEdward Tomasz Napierala void
1078c34bbd2aSEdward Tomasz Napierala racct_move(struct racct *dest, struct racct *src)
1079c34bbd2aSEdward Tomasz Napierala {
1080c34bbd2aSEdward Tomasz Napierala 
10814b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
10824b5c9cf6SEdward Tomasz Napierala 
10834c230cdaSEdward Tomasz Napierala 	RACCT_LOCK();
1084c34bbd2aSEdward Tomasz Napierala 	racct_add_racct(dest, src);
1085c34bbd2aSEdward Tomasz Napierala 	racct_sub_racct(src, src);
10864c230cdaSEdward Tomasz Napierala 	RACCT_UNLOCK();
1087c34bbd2aSEdward Tomasz Napierala }
1088c34bbd2aSEdward Tomasz Napierala 
1089ae34b6ffSEdward Tomasz Napierala /*
1090ae34b6ffSEdward Tomasz Napierala  * Make the process sleep in userret() for 'timeout' ticks.  Setting
1091ae34b6ffSEdward Tomasz Napierala  * timeout to -1 makes it sleep until woken up by racct_proc_wakeup().
1092ae34b6ffSEdward Tomasz Napierala  */
1093ae34b6ffSEdward Tomasz Napierala void
1094ae34b6ffSEdward Tomasz Napierala racct_proc_throttle(struct proc *p, int timeout)
109536af9869SEdward Tomasz Napierala {
109636af9869SEdward Tomasz Napierala 	struct thread *td;
109736af9869SEdward Tomasz Napierala #ifdef SMP
109836af9869SEdward Tomasz Napierala 	int cpuid;
109936af9869SEdward Tomasz Napierala #endif
110036af9869SEdward Tomasz Napierala 
1101ae34b6ffSEdward Tomasz Napierala 	KASSERT(timeout != 0, ("timeout %d", timeout));
11024b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
110336af9869SEdward Tomasz Napierala 	PROC_LOCK_ASSERT(p, MA_OWNED);
110436af9869SEdward Tomasz Napierala 
110536af9869SEdward Tomasz Napierala 	/*
110636af9869SEdward Tomasz Napierala 	 * Do not block kernel processes.  Also do not block processes with
110736af9869SEdward Tomasz Napierala 	 * low %cpu utilization to improve interactivity.
110836af9869SEdward Tomasz Napierala 	 */
1109ae34b6ffSEdward Tomasz Napierala 	if ((p->p_flag & (P_SYSTEM | P_KPROC)) != 0)
111036af9869SEdward Tomasz Napierala 		return;
1111ae34b6ffSEdward Tomasz Napierala 
1112ae34b6ffSEdward Tomasz Napierala 	if (p->p_throttled < 0 || (timeout > 0 && p->p_throttled > timeout))
1113ae34b6ffSEdward Tomasz Napierala 		return;
1114ae34b6ffSEdward Tomasz Napierala 
1115ae34b6ffSEdward Tomasz Napierala 	p->p_throttled = timeout;
111636af9869SEdward Tomasz Napierala 
111736af9869SEdward Tomasz Napierala 	FOREACH_THREAD_IN_PROC(p, td) {
111816befafdSEdward Tomasz Napierala 		thread_lock(td);
111936af9869SEdward Tomasz Napierala 		switch (td->td_state) {
112036af9869SEdward Tomasz Napierala 		case TDS_RUNQ:
112136af9869SEdward Tomasz Napierala 			/*
112236af9869SEdward Tomasz Napierala 			 * If the thread is on the scheduler run-queue, we can
112336af9869SEdward Tomasz Napierala 			 * not just remove it from there.  So we set the flag
112436af9869SEdward Tomasz Napierala 			 * TDF_NEEDRESCHED for the thread, so that once it is
112536af9869SEdward Tomasz Napierala 			 * running, it is taken off the cpu as soon as possible.
112636af9869SEdward Tomasz Napierala 			 */
112736af9869SEdward Tomasz Napierala 			td->td_flags |= TDF_NEEDRESCHED;
112836af9869SEdward Tomasz Napierala 			break;
112936af9869SEdward Tomasz Napierala 		case TDS_RUNNING:
113036af9869SEdward Tomasz Napierala 			/*
113136af9869SEdward Tomasz Napierala 			 * If the thread is running, we request a context
113236af9869SEdward Tomasz Napierala 			 * switch for it by setting the TDF_NEEDRESCHED flag.
113336af9869SEdward Tomasz Napierala 			 */
113436af9869SEdward Tomasz Napierala 			td->td_flags |= TDF_NEEDRESCHED;
113536af9869SEdward Tomasz Napierala #ifdef SMP
113636af9869SEdward Tomasz Napierala 			cpuid = td->td_oncpu;
113736af9869SEdward Tomasz Napierala 			if ((cpuid != NOCPU) && (td != curthread))
113836af9869SEdward Tomasz Napierala 				ipi_cpu(cpuid, IPI_AST);
113936af9869SEdward Tomasz Napierala #endif
114036af9869SEdward Tomasz Napierala 			break;
114136af9869SEdward Tomasz Napierala 		default:
114236af9869SEdward Tomasz Napierala 			break;
114336af9869SEdward Tomasz Napierala 		}
114416befafdSEdward Tomasz Napierala 		thread_unlock(td);
114536af9869SEdward Tomasz Napierala 	}
114636af9869SEdward Tomasz Napierala }
114736af9869SEdward Tomasz Napierala 
114836af9869SEdward Tomasz Napierala static void
114936af9869SEdward Tomasz Napierala racct_proc_wakeup(struct proc *p)
115036af9869SEdward Tomasz Napierala {
11514b5c9cf6SEdward Tomasz Napierala 
11524b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
11534b5c9cf6SEdward Tomasz Napierala 
115436af9869SEdward Tomasz Napierala 	PROC_LOCK_ASSERT(p, MA_OWNED);
115536af9869SEdward Tomasz Napierala 
1156ae34b6ffSEdward Tomasz Napierala 	if (p->p_throttled != 0) {
115736af9869SEdward Tomasz Napierala 		p->p_throttled = 0;
115836af9869SEdward Tomasz Napierala 		wakeup(p->p_racct);
115936af9869SEdward Tomasz Napierala 	}
116036af9869SEdward Tomasz Napierala }
116136af9869SEdward Tomasz Napierala 
116236af9869SEdward Tomasz Napierala static void
1163862d03fbSEdward Tomasz Napierala racct_decay_callback(struct racct *racct, void *dummy1, void *dummy2)
116436af9869SEdward Tomasz Napierala {
116536af9869SEdward Tomasz Napierala 	int64_t r_old, r_new;
116636af9869SEdward Tomasz Napierala 
11674b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
11684c230cdaSEdward Tomasz Napierala 	RACCT_LOCK_ASSERT();
11694b5c9cf6SEdward Tomasz Napierala 
1170ae34b6ffSEdward Tomasz Napierala #ifdef RCTL
1171ae34b6ffSEdward Tomasz Napierala 	rctl_throttle_decay(racct, RACCT_READBPS);
1172ae34b6ffSEdward Tomasz Napierala 	rctl_throttle_decay(racct, RACCT_WRITEBPS);
1173ae34b6ffSEdward Tomasz Napierala 	rctl_throttle_decay(racct, RACCT_READIOPS);
1174ae34b6ffSEdward Tomasz Napierala 	rctl_throttle_decay(racct, RACCT_WRITEIOPS);
1175ae34b6ffSEdward Tomasz Napierala #endif
1176ae34b6ffSEdward Tomasz Napierala 
1177862d03fbSEdward Tomasz Napierala 	r_old = racct->r_resources[RACCT_PCTCPU];
117836af9869SEdward Tomasz Napierala 
117936af9869SEdward Tomasz Napierala 	/* If there is nothing to decay, just exit. */
118036af9869SEdward Tomasz Napierala 	if (r_old <= 0)
118136af9869SEdward Tomasz Napierala 		return;
118236af9869SEdward Tomasz Napierala 
118336af9869SEdward Tomasz Napierala 	r_new = r_old * RACCT_DECAY_FACTOR / FSCALE;
1184862d03fbSEdward Tomasz Napierala 	racct->r_resources[RACCT_PCTCPU] = r_new;
118515db3c07SEdward Tomasz Napierala }
118615db3c07SEdward Tomasz Napierala 
118715db3c07SEdward Tomasz Napierala static void
118815db3c07SEdward Tomasz Napierala racct_decay_pre(void)
118915db3c07SEdward Tomasz Napierala {
119015db3c07SEdward Tomasz Napierala 
11914c230cdaSEdward Tomasz Napierala 	RACCT_LOCK();
119215db3c07SEdward Tomasz Napierala }
119315db3c07SEdward Tomasz Napierala 
119415db3c07SEdward Tomasz Napierala static void
119515db3c07SEdward Tomasz Napierala racct_decay_post(void)
119615db3c07SEdward Tomasz Napierala {
119715db3c07SEdward Tomasz Napierala 
11984c230cdaSEdward Tomasz Napierala 	RACCT_UNLOCK();
119936af9869SEdward Tomasz Napierala }
120036af9869SEdward Tomasz Napierala 
120136af9869SEdward Tomasz Napierala static void
1202097e0da7SEdward Tomasz Napierala racct_decay(void)
120336af9869SEdward Tomasz Napierala {
12044b5c9cf6SEdward Tomasz Napierala 
12054b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
12064b5c9cf6SEdward Tomasz Napierala 
1207862d03fbSEdward Tomasz Napierala 	ui_racct_foreach(racct_decay_callback, racct_decay_pre,
1208862d03fbSEdward Tomasz Napierala 	    racct_decay_post, NULL, NULL);
1209862d03fbSEdward Tomasz Napierala 	loginclass_racct_foreach(racct_decay_callback, racct_decay_pre,
1210862d03fbSEdward Tomasz Napierala 	    racct_decay_post, NULL, NULL);
1211862d03fbSEdward Tomasz Napierala 	prison_racct_foreach(racct_decay_callback, racct_decay_pre,
1212862d03fbSEdward Tomasz Napierala 	    racct_decay_post, NULL, NULL);
121336af9869SEdward Tomasz Napierala }
121436af9869SEdward Tomasz Napierala 
121536af9869SEdward Tomasz Napierala static void
1216097055e2SEdward Tomasz Napierala racctd(void)
1217097055e2SEdward Tomasz Napierala {
1218097055e2SEdward Tomasz Napierala 	struct thread *td;
1219097055e2SEdward Tomasz Napierala 	struct proc *p;
1220097055e2SEdward Tomasz Napierala 	struct timeval wallclock;
1221c1a43e73SEdward Tomasz Napierala 	uint64_t pct, pct_estimate, runtime;
1222097055e2SEdward Tomasz Napierala 
12234b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
12244b5c9cf6SEdward Tomasz Napierala 
1225097055e2SEdward Tomasz Napierala 	for (;;) {
1226862d03fbSEdward Tomasz Napierala 		racct_decay();
122736af9869SEdward Tomasz Napierala 
1228097055e2SEdward Tomasz Napierala 		sx_slock(&allproc_lock);
1229097055e2SEdward Tomasz Napierala 
123036af9869SEdward Tomasz Napierala 		LIST_FOREACH(p, &zombproc, p_list) {
123136af9869SEdward Tomasz Napierala 			PROC_LOCK(p);
123236af9869SEdward Tomasz Napierala 			racct_set(p, RACCT_PCTCPU, 0);
123336af9869SEdward Tomasz Napierala 			PROC_UNLOCK(p);
123436af9869SEdward Tomasz Napierala 		}
123536af9869SEdward Tomasz Napierala 
1236097055e2SEdward Tomasz Napierala 		FOREACH_PROC_IN_SYSTEM(p) {
123736af9869SEdward Tomasz Napierala 			PROC_LOCK(p);
123836af9869SEdward Tomasz Napierala 			if (p->p_state != PRS_NORMAL) {
123936af9869SEdward Tomasz Napierala 				PROC_UNLOCK(p);
1240097055e2SEdward Tomasz Napierala 				continue;
124136af9869SEdward Tomasz Napierala 			}
1242097055e2SEdward Tomasz Napierala 
1243097055e2SEdward Tomasz Napierala 			microuptime(&wallclock);
1244097055e2SEdward Tomasz Napierala 			timevalsub(&wallclock, &p->p_stats->p_start);
12455c7bebf9SKonstantin Belousov 			PROC_STATLOCK(p);
12460a53cd57SEdward Tomasz Napierala 			FOREACH_THREAD_IN_PROC(p, td)
1247097055e2SEdward Tomasz Napierala 				ruxagg(p, td);
1248097055e2SEdward Tomasz Napierala 			runtime = cputick2usec(p->p_rux.rux_runtime);
12495c7bebf9SKonstantin Belousov 			PROC_STATUNLOCK(p);
1250097055e2SEdward Tomasz Napierala #ifdef notyet
1251097055e2SEdward Tomasz Napierala 			KASSERT(runtime >= p->p_prev_runtime,
1252097055e2SEdward Tomasz Napierala 			    ("runtime < p_prev_runtime"));
1253097055e2SEdward Tomasz Napierala #else
1254097055e2SEdward Tomasz Napierala 			if (runtime < p->p_prev_runtime)
1255097055e2SEdward Tomasz Napierala 				runtime = p->p_prev_runtime;
1256097055e2SEdward Tomasz Napierala #endif
1257097055e2SEdward Tomasz Napierala 			p->p_prev_runtime = runtime;
125884590fd8SEdward Tomasz Napierala 			if (wallclock.tv_sec > 0 || wallclock.tv_usec > 0) {
125936af9869SEdward Tomasz Napierala 				pct_estimate = (1000000 * runtime * 100) /
126036af9869SEdward Tomasz Napierala 				    ((uint64_t)wallclock.tv_sec * 1000000 +
126136af9869SEdward Tomasz Napierala 				    wallclock.tv_usec);
126284590fd8SEdward Tomasz Napierala 			} else
126384590fd8SEdward Tomasz Napierala 				pct_estimate = 0;
126436af9869SEdward Tomasz Napierala 			pct = racct_getpcpu(p, pct_estimate);
12654c230cdaSEdward Tomasz Napierala 			RACCT_LOCK();
1266ae34b6ffSEdward Tomasz Napierala #ifdef RCTL
1267ae34b6ffSEdward Tomasz Napierala 			rctl_throttle_decay(p->p_racct, RACCT_READBPS);
1268ae34b6ffSEdward Tomasz Napierala 			rctl_throttle_decay(p->p_racct, RACCT_WRITEBPS);
1269ae34b6ffSEdward Tomasz Napierala 			rctl_throttle_decay(p->p_racct, RACCT_READIOPS);
1270ae34b6ffSEdward Tomasz Napierala 			rctl_throttle_decay(p->p_racct, RACCT_WRITEIOPS);
1271ae34b6ffSEdward Tomasz Napierala #endif
127210287198SEdward Tomasz Napierala 			racct_set_locked(p, RACCT_PCTCPU, pct, 1);
127310287198SEdward Tomasz Napierala 			racct_set_locked(p, RACCT_CPU, runtime, 0);
1274097055e2SEdward Tomasz Napierala 			racct_set_locked(p, RACCT_WALLCLOCK,
127559f513cdSJaakko Heinonen 			    (uint64_t)wallclock.tv_sec * 1000000 +
127610287198SEdward Tomasz Napierala 			    wallclock.tv_usec, 0);
12774c230cdaSEdward Tomasz Napierala 			RACCT_UNLOCK();
1278097055e2SEdward Tomasz Napierala 			PROC_UNLOCK(p);
1279097055e2SEdward Tomasz Napierala 		}
128036af9869SEdward Tomasz Napierala 
128136af9869SEdward Tomasz Napierala 		/*
128236af9869SEdward Tomasz Napierala 		 * To ensure that processes are throttled in a fair way, we need
128336af9869SEdward Tomasz Napierala 		 * to iterate over all processes again and check the limits
128436af9869SEdward Tomasz Napierala 		 * for %cpu resource only after ucred racct containers have been
128536af9869SEdward Tomasz Napierala 		 * properly filled.
128636af9869SEdward Tomasz Napierala 		 */
128736af9869SEdward Tomasz Napierala 		FOREACH_PROC_IN_SYSTEM(p) {
128836af9869SEdward Tomasz Napierala 			PROC_LOCK(p);
128936af9869SEdward Tomasz Napierala 			if (p->p_state != PRS_NORMAL) {
129036af9869SEdward Tomasz Napierala 				PROC_UNLOCK(p);
129136af9869SEdward Tomasz Napierala 				continue;
129236af9869SEdward Tomasz Napierala 			}
129336af9869SEdward Tomasz Napierala 
1294ae34b6ffSEdward Tomasz Napierala 			if (racct_pcpu_available(p) <= 0) {
1295ae34b6ffSEdward Tomasz Napierala 				if (p->p_racct->r_resources[RACCT_PCTCPU] >
1296ae34b6ffSEdward Tomasz Napierala 				    pcpu_threshold)
1297ae34b6ffSEdward Tomasz Napierala 					racct_proc_throttle(p, -1);
1298ae34b6ffSEdward Tomasz Napierala 			} else if (p->p_throttled == -1) {
129936af9869SEdward Tomasz Napierala 				racct_proc_wakeup(p);
1300ae34b6ffSEdward Tomasz Napierala 			}
130136af9869SEdward Tomasz Napierala 			PROC_UNLOCK(p);
130236af9869SEdward Tomasz Napierala 		}
1303097055e2SEdward Tomasz Napierala 		sx_sunlock(&allproc_lock);
1304097055e2SEdward Tomasz Napierala 		pause("-", hz);
1305097055e2SEdward Tomasz Napierala 	}
1306097055e2SEdward Tomasz Napierala }
1307097055e2SEdward Tomasz Napierala 
1308097055e2SEdward Tomasz Napierala static struct kproc_desc racctd_kp = {
1309097055e2SEdward Tomasz Napierala 	"racctd",
1310097055e2SEdward Tomasz Napierala 	racctd,
1311097055e2SEdward Tomasz Napierala 	NULL
1312097055e2SEdward Tomasz Napierala };
13134b5c9cf6SEdward Tomasz Napierala 
13144b5c9cf6SEdward Tomasz Napierala static void
13154b5c9cf6SEdward Tomasz Napierala racctd_init(void)
13164b5c9cf6SEdward Tomasz Napierala {
13174b5c9cf6SEdward Tomasz Napierala 	if (!racct_enable)
13184b5c9cf6SEdward Tomasz Napierala 		return;
13194b5c9cf6SEdward Tomasz Napierala 
13204b5c9cf6SEdward Tomasz Napierala 	kproc_start(&racctd_kp);
13214b5c9cf6SEdward Tomasz Napierala }
13224b5c9cf6SEdward Tomasz Napierala SYSINIT(racctd, SI_SUB_RACCTD, SI_ORDER_FIRST, racctd_init, NULL);
1323097055e2SEdward Tomasz Napierala 
1324097055e2SEdward Tomasz Napierala static void
1325097055e2SEdward Tomasz Napierala racct_init(void)
1326097055e2SEdward Tomasz Napierala {
13274b5c9cf6SEdward Tomasz Napierala 	if (!racct_enable)
13284b5c9cf6SEdward Tomasz Napierala 		return;
1329097055e2SEdward Tomasz Napierala 
1330097055e2SEdward Tomasz Napierala 	racct_zone = uma_zcreate("racct", sizeof(struct racct),
133123e6fff2SEdward Tomasz Napierala 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1332097055e2SEdward Tomasz Napierala 	/*
1333097055e2SEdward Tomasz Napierala 	 * XXX: Move this somewhere.
1334097055e2SEdward Tomasz Napierala 	 */
1335a7ad07bfSEdward Tomasz Napierala 	prison0.pr_prison_racct = prison_racct_find("0");
1336097055e2SEdward Tomasz Napierala }
1337097055e2SEdward Tomasz Napierala SYSINIT(racct, SI_SUB_RACCT, SI_ORDER_FIRST, racct_init, NULL);
1338097055e2SEdward Tomasz Napierala 
1339097055e2SEdward Tomasz Napierala #endif /* !RACCT */
1340