kern_racct.c revision 242957
1220137Strasz/*-
2220137Strasz * Copyright (c) 2010 The FreeBSD Foundation
3220137Strasz * All rights reserved.
4220137Strasz *
5220137Strasz * This software was developed by Edward Tomasz Napierala under sponsorship
6220137Strasz * from the FreeBSD Foundation.
7220137Strasz *
8220137Strasz * Redistribution and use in source and binary forms, with or without
9220137Strasz * modification, are permitted provided that the following conditions
10220137Strasz * are met:
11220137Strasz * 1. Redistributions of source code must retain the above copyright
12220137Strasz *    notice, this list of conditions and the following disclaimer.
13220137Strasz * 2. Redistributions in binary form must reproduce the above copyright
14220137Strasz *    notice, this list of conditions and the following disclaimer in the
15220137Strasz *    documentation and/or other materials provided with the distribution.
16220137Strasz *
17220137Strasz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18220137Strasz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19220137Strasz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20220137Strasz * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21220137Strasz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22220137Strasz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23220137Strasz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24220137Strasz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25220137Strasz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26220137Strasz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27220137Strasz * SUCH DAMAGE.
28220137Strasz *
29220137Strasz * $FreeBSD: head/sys/kern/kern_racct.c 242957 2012-11-13 11:29:08Z trasz $
30220137Strasz */
31220137Strasz
32220137Strasz#include <sys/cdefs.h>
33220137Strasz__FBSDID("$FreeBSD: head/sys/kern/kern_racct.c 242957 2012-11-13 11:29:08Z trasz $");
34220137Strasz
35220137Strasz#include "opt_kdtrace.h"
36242139Strasz#include "opt_sched.h"
37220137Strasz
38220137Strasz#include <sys/param.h>
39228430Savg#include <sys/systm.h>
40220137Strasz#include <sys/eventhandler.h>
41220137Strasz#include <sys/jail.h>
42220137Strasz#include <sys/kernel.h>
43220137Strasz#include <sys/kthread.h>
44220137Strasz#include <sys/lock.h>
45220137Strasz#include <sys/loginclass.h>
46220137Strasz#include <sys/malloc.h>
47220137Strasz#include <sys/mutex.h>
48220137Strasz#include <sys/proc.h>
49220137Strasz#include <sys/racct.h>
50220137Strasz#include <sys/resourcevar.h>
51220137Strasz#include <sys/sbuf.h>
52220137Strasz#include <sys/sched.h>
53220137Strasz#include <sys/sdt.h>
54242139Strasz#include <sys/smp.h>
55220137Strasz#include <sys/sx.h>
56242139Strasz#include <sys/sysctl.h>
57220137Strasz#include <sys/sysent.h>
58220137Strasz#include <sys/sysproto.h>
59220137Strasz#include <sys/umtx.h>
60242139Strasz#include <machine/smp.h>
61220137Strasz
62220137Strasz#ifdef RCTL
63220137Strasz#include <sys/rctl.h>
64220137Strasz#endif
65220137Strasz
66220137Strasz#ifdef RACCT
67220137Strasz
68220137StraszFEATURE(racct, "Resource Accounting");
69220137Strasz
70242139Strasz/*
71242139Strasz * Do not block processes that have their %cpu usage <= pcpu_threshold.
72242139Strasz */
73242139Straszstatic int pcpu_threshold = 1;
74242139Strasz
75242139StraszSYSCTL_NODE(_kern, OID_AUTO, racct, CTLFLAG_RW, 0, "Resource Accounting");
76242139StraszSYSCTL_UINT(_kern_racct, OID_AUTO, pcpu_threshold, CTLFLAG_RW, &pcpu_threshold,
77242139Strasz    0, "Processes with higher %cpu usage than this value can be throttled.");
78242139Strasz
79242139Strasz/*
80242139Strasz * How many seconds it takes to use the scheduler %cpu calculations.  When a
81242139Strasz * process starts, we compute its %cpu usage by dividing its runtime by the
82242139Strasz * process wall clock time.  After RACCT_PCPU_SECS pass, we use the value
83242139Strasz * provided by the scheduler.
84242139Strasz */
85242139Strasz#define RACCT_PCPU_SECS		3
86242139Strasz
87220137Straszstatic struct mtx racct_lock;
88220137StraszMTX_SYSINIT(racct_lock, &racct_lock, "racct lock", MTX_DEF);
89220137Strasz
90220137Straszstatic uma_zone_t racct_zone;
91220137Strasz
92220137Straszstatic void racct_sub_racct(struct racct *dest, const struct racct *src);
93220137Straszstatic void racct_sub_cred_locked(struct ucred *cred, int resource,
94220137Strasz		uint64_t amount);
95220137Straszstatic void racct_add_cred_locked(struct ucred *cred, int resource,
96220137Strasz		uint64_t amount);
97220137Strasz
98220137StraszSDT_PROVIDER_DEFINE(racct);
99220137StraszSDT_PROBE_DEFINE3(racct, kernel, rusage, add, add, "struct proc *", "int",
100220137Strasz    "uint64_t");
101220137StraszSDT_PROBE_DEFINE3(racct, kernel, rusage, add_failure, add-failure,
102220137Strasz    "struct proc *", "int", "uint64_t");
103220137StraszSDT_PROBE_DEFINE3(racct, kernel, rusage, add_cred, add-cred, "struct ucred *",
104220137Strasz    "int", "uint64_t");
105220137StraszSDT_PROBE_DEFINE3(racct, kernel, rusage, add_force, add-force, "struct proc *",
106220137Strasz    "int", "uint64_t");
107220137StraszSDT_PROBE_DEFINE3(racct, kernel, rusage, set, set, "struct proc *", "int",
108220137Strasz    "uint64_t");
109220137StraszSDT_PROBE_DEFINE3(racct, kernel, rusage, set_failure, set-failure,
110220137Strasz    "struct proc *", "int", "uint64_t");
111220137StraszSDT_PROBE_DEFINE3(racct, kernel, rusage, sub, sub, "struct proc *", "int",
112220137Strasz    "uint64_t");
113220137StraszSDT_PROBE_DEFINE3(racct, kernel, rusage, sub_cred, sub-cred, "struct ucred *",
114220137Strasz    "int", "uint64_t");
115220137StraszSDT_PROBE_DEFINE1(racct, kernel, racct, create, create, "struct racct *");
116220137StraszSDT_PROBE_DEFINE1(racct, kernel, racct, destroy, destroy, "struct racct *");
117220137StraszSDT_PROBE_DEFINE2(racct, kernel, racct, join, join, "struct racct *",
118220137Strasz    "struct racct *");
119220137StraszSDT_PROBE_DEFINE2(racct, kernel, racct, join_failure, join-failure,
120220137Strasz    "struct racct *", "struct racct *");
121220137StraszSDT_PROBE_DEFINE2(racct, kernel, racct, leave, leave, "struct racct *",
122220137Strasz    "struct racct *");
123220137Strasz
124220137Straszint racct_types[] = {
125220137Strasz	[RACCT_CPU] =
126224036Strasz		RACCT_IN_MILLIONS,
127220137Strasz	[RACCT_DATA] =
128220137Strasz		RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
129220137Strasz	[RACCT_STACK] =
130220137Strasz		RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
131220137Strasz	[RACCT_CORE] =
132220137Strasz		RACCT_DENIABLE,
133220137Strasz	[RACCT_RSS] =
134220137Strasz		RACCT_RECLAIMABLE,
135220137Strasz	[RACCT_MEMLOCK] =
136220137Strasz		RACCT_RECLAIMABLE | RACCT_DENIABLE,
137220137Strasz	[RACCT_NPROC] =
138220137Strasz		RACCT_RECLAIMABLE | RACCT_DENIABLE,
139220137Strasz	[RACCT_NOFILE] =
140220137Strasz		RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
141220137Strasz	[RACCT_VMEM] =
142220137Strasz		RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
143220137Strasz	[RACCT_NPTS] =
144220137Strasz		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
145220137Strasz	[RACCT_SWAP] =
146220137Strasz		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
147220137Strasz	[RACCT_NTHR] =
148220137Strasz		RACCT_RECLAIMABLE | RACCT_DENIABLE,
149220137Strasz	[RACCT_MSGQQUEUED] =
150220137Strasz		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
151220137Strasz	[RACCT_MSGQSIZE] =
152220137Strasz		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
153220137Strasz	[RACCT_NMSGQ] =
154220137Strasz		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
155220137Strasz	[RACCT_NSEM] =
156220137Strasz		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
157220137Strasz	[RACCT_NSEMOP] =
158220137Strasz		RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
159220137Strasz	[RACCT_NSHM] =
160220137Strasz		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
161220137Strasz	[RACCT_SHMSIZE] =
162220137Strasz		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
163220137Strasz	[RACCT_WALLCLOCK] =
164242139Strasz		RACCT_IN_MILLIONS,
165242139Strasz	[RACCT_PCTCPU] =
166242139Strasz		RACCT_DECAYING | RACCT_DENIABLE | RACCT_IN_MILLIONS };
167220137Strasz
168242139Straszstatic const fixpt_t RACCT_DECAY_FACTOR = 0.3 * FSCALE;
169242139Strasz
170242139Strasz#ifdef SCHED_4BSD
171242139Strasz/*
172242139Strasz * Contains intermediate values for %cpu calculations to avoid using floating
173242139Strasz * point in the kernel.
174242139Strasz * ccpu_exp[k] = FSCALE * (ccpu/FSCALE)^k = FSCALE * exp(-k/20)
175242139Strasz * It is needed only for the 4BSD scheduler, because in ULE, the ccpu equals to
176242139Strasz * zero so the calculations are more straightforward.
177242139Strasz */
178242139Straszfixpt_t ccpu_exp[] = {
179242139Strasz	[0] = FSCALE * 1,
180242139Strasz	[1] = FSCALE * 0.95122942450071400909,
181242139Strasz	[2] = FSCALE * 0.90483741803595957316,
182242139Strasz	[3] = FSCALE * 0.86070797642505780722,
183242139Strasz	[4] = FSCALE * 0.81873075307798185866,
184242139Strasz	[5] = FSCALE * 0.77880078307140486824,
185242139Strasz	[6] = FSCALE * 0.74081822068171786606,
186242139Strasz	[7] = FSCALE * 0.70468808971871343435,
187242139Strasz	[8] = FSCALE * 0.67032004603563930074,
188242139Strasz	[9] = FSCALE * 0.63762815162177329314,
189242139Strasz	[10] = FSCALE * 0.60653065971263342360,
190242139Strasz	[11] = FSCALE * 0.57694981038048669531,
191242139Strasz	[12] = FSCALE * 0.54881163609402643262,
192242139Strasz	[13] = FSCALE * 0.52204577676101604789,
193242139Strasz	[14] = FSCALE * 0.49658530379140951470,
194242139Strasz	[15] = FSCALE * 0.47236655274101470713,
195242139Strasz	[16] = FSCALE * 0.44932896411722159143,
196242139Strasz	[17] = FSCALE * 0.42741493194872666992,
197242139Strasz	[18] = FSCALE * 0.40656965974059911188,
198242139Strasz	[19] = FSCALE * 0.38674102345450120691,
199242139Strasz	[20] = FSCALE * 0.36787944117144232159,
200242139Strasz	[21] = FSCALE * 0.34993774911115535467,
201242139Strasz	[22] = FSCALE * 0.33287108369807955328,
202242139Strasz	[23] = FSCALE * 0.31663676937905321821,
203242139Strasz	[24] = FSCALE * 0.30119421191220209664,
204242139Strasz	[25] = FSCALE * 0.28650479686019010032,
205242139Strasz	[26] = FSCALE * 0.27253179303401260312,
206242139Strasz	[27] = FSCALE * 0.25924026064589150757,
207242139Strasz	[28] = FSCALE * 0.24659696394160647693,
208242139Strasz	[29] = FSCALE * 0.23457028809379765313,
209242139Strasz	[30] = FSCALE * 0.22313016014842982893,
210242139Strasz	[31] = FSCALE * 0.21224797382674305771,
211242139Strasz	[32] = FSCALE * 0.20189651799465540848,
212242139Strasz	[33] = FSCALE * 0.19204990862075411423,
213242139Strasz	[34] = FSCALE * 0.18268352405273465022,
214242139Strasz	[35] = FSCALE * 0.17377394345044512668,
215242139Strasz	[36] = FSCALE * 0.16529888822158653829,
216242139Strasz	[37] = FSCALE * 0.15723716631362761621,
217242139Strasz	[38] = FSCALE * 0.14956861922263505264,
218242139Strasz	[39] = FSCALE * 0.14227407158651357185,
219242139Strasz	[40] = FSCALE * 0.13533528323661269189,
220242139Strasz	[41] = FSCALE * 0.12873490358780421886,
221242139Strasz	[42] = FSCALE * 0.12245642825298191021,
222242139Strasz	[43] = FSCALE * 0.11648415777349695786,
223242139Strasz	[44] = FSCALE * 0.11080315836233388333,
224242139Strasz	[45] = FSCALE * 0.10539922456186433678,
225242139Strasz	[46] = FSCALE * 0.10025884372280373372,
226242139Strasz	[47] = FSCALE * 0.09536916221554961888,
227242139Strasz	[48] = FSCALE * 0.09071795328941250337,
228242139Strasz	[49] = FSCALE * 0.08629358649937051097,
229242139Strasz	[50] = FSCALE * 0.08208499862389879516,
230242139Strasz	[51] = FSCALE * 0.07808166600115315231,
231242139Strasz	[52] = FSCALE * 0.07427357821433388042,
232242139Strasz	[53] = FSCALE * 0.07065121306042958674,
233242139Strasz	[54] = FSCALE * 0.06720551273974976512,
234242139Strasz	[55] = FSCALE * 0.06392786120670757270,
235242139Strasz	[56] = FSCALE * 0.06081006262521796499,
236242139Strasz	[57] = FSCALE * 0.05784432087483846296,
237242139Strasz	[58] = FSCALE * 0.05502322005640722902,
238242139Strasz	[59] = FSCALE * 0.05233970594843239308,
239242139Strasz	[60] = FSCALE * 0.04978706836786394297,
240242139Strasz	[61] = FSCALE * 0.04735892439114092119,
241242139Strasz	[62] = FSCALE * 0.04504920239355780606,
242242139Strasz	[63] = FSCALE * 0.04285212686704017991,
243242139Strasz	[64] = FSCALE * 0.04076220397836621516,
244242139Strasz	[65] = FSCALE * 0.03877420783172200988,
245242139Strasz	[66] = FSCALE * 0.03688316740124000544,
246242139Strasz	[67] = FSCALE * 0.03508435410084502588,
247242139Strasz	[68] = FSCALE * 0.03337326996032607948,
248242139Strasz	[69] = FSCALE * 0.03174563637806794323,
249242139Strasz	[70] = FSCALE * 0.03019738342231850073,
250242139Strasz	[71] = FSCALE * 0.02872463965423942912,
251242139Strasz	[72] = FSCALE * 0.02732372244729256080,
252242139Strasz	[73] = FSCALE * 0.02599112877875534358,
253242139Strasz	[74] = FSCALE * 0.02472352647033939120,
254242139Strasz	[75] = FSCALE * 0.02351774585600910823,
255242139Strasz	[76] = FSCALE * 0.02237077185616559577,
256242139Strasz	[77] = FSCALE * 0.02127973643837716938,
257242139Strasz	[78] = FSCALE * 0.02024191144580438847,
258242139Strasz	[79] = FSCALE * 0.01925470177538692429,
259242139Strasz	[80] = FSCALE * 0.01831563888873418029,
260242139Strasz	[81] = FSCALE * 0.01742237463949351138,
261242139Strasz	[82] = FSCALE * 0.01657267540176124754,
262242139Strasz	[83] = FSCALE * 0.01576441648485449082,
263242139Strasz	[84] = FSCALE * 0.01499557682047770621,
264242139Strasz	[85] = FSCALE * 0.01426423390899925527,
265242139Strasz	[86] = FSCALE * 0.01356855901220093175,
266242139Strasz	[87] = FSCALE * 0.01290681258047986886,
267242139Strasz	[88] = FSCALE * 0.01227733990306844117,
268242139Strasz	[89] = FSCALE * 0.01167856697039544521,
269242139Strasz	[90] = FSCALE * 0.01110899653824230649,
270242139Strasz	[91] = FSCALE * 0.01056720438385265337,
271242139Strasz	[92] = FSCALE * 0.01005183574463358164,
272242139Strasz	[93] = FSCALE * 0.00956160193054350793,
273242139Strasz	[94] = FSCALE * 0.00909527710169581709,
274242139Strasz	[95] = FSCALE * 0.00865169520312063417,
275242139Strasz	[96] = FSCALE * 0.00822974704902002884,
276242139Strasz	[97] = FSCALE * 0.00782837754922577143,
277242139Strasz	[98] = FSCALE * 0.00744658307092434051,
278242139Strasz	[99] = FSCALE * 0.00708340892905212004,
279242139Strasz	[100] = FSCALE * 0.00673794699908546709,
280242139Strasz	[101] = FSCALE * 0.00640933344625638184,
281242139Strasz	[102] = FSCALE * 0.00609674656551563610,
282242139Strasz	[103] = FSCALE * 0.00579940472684214321,
283242139Strasz	[104] = FSCALE * 0.00551656442076077241,
284242139Strasz	[105] = FSCALE * 0.00524751839918138427,
285242139Strasz	[106] = FSCALE * 0.00499159390691021621,
286242139Strasz	[107] = FSCALE * 0.00474815099941147558,
287242139Strasz	[108] = FSCALE * 0.00451658094261266798,
288242139Strasz	[109] = FSCALE * 0.00429630469075234057,
289242139Strasz	[110] = FSCALE * 0.00408677143846406699,
290242139Strasz};
291242139Strasz#endif
292242139Strasz
293242139Strasz#define	CCPU_EXP_MAX	110
294242139Strasz
295242139Strasz/*
296242139Strasz * This function is analogical to the getpcpu() function in the ps(1) command.
297242139Strasz * They should both calculate in the same way so that the racct %cpu
298242139Strasz * calculations are consistent with the values showed by the ps(1) tool.
299242139Strasz * The calculations are more complex in the 4BSD scheduler because of the value
300242139Strasz * of the ccpu variable.  In ULE it is defined to be zero which saves us some
301242139Strasz * work.
302242139Strasz */
303242139Straszstatic uint64_t
304242139Straszracct_getpcpu(struct proc *p, u_int pcpu)
305242139Strasz{
306242139Strasz	u_int swtime;
307242139Strasz#ifdef SCHED_4BSD
308242139Strasz	fixpt_t pctcpu, pctcpu_next;
309242139Strasz#endif
310242139Strasz#ifdef SMP
311242139Strasz	struct pcpu *pc;
312242139Strasz	int found;
313242139Strasz#endif
314242139Strasz	fixpt_t p_pctcpu;
315242139Strasz	struct thread *td;
316242139Strasz
317242139Strasz	/*
318242139Strasz	 * If the process is swapped out, we count its %cpu usage as zero.
319242139Strasz	 * This behaviour is consistent with the userland ps(1) tool.
320242139Strasz	 */
321242139Strasz	if ((p->p_flag & P_INMEM) == 0)
322242139Strasz		return (0);
323242139Strasz	swtime = (ticks - p->p_swtick) / hz;
324242139Strasz
325242139Strasz	/*
326242139Strasz	 * For short-lived processes, the sched_pctcpu() returns small
327242139Strasz	 * values even for cpu intensive processes.  Therefore we use
328242139Strasz	 * our own estimate in this case.
329242139Strasz	 */
330242139Strasz	if (swtime < RACCT_PCPU_SECS)
331242139Strasz		return (pcpu);
332242139Strasz
333242139Strasz	p_pctcpu = 0;
334242139Strasz	FOREACH_THREAD_IN_PROC(p, td) {
335242139Strasz		if (td == PCPU_GET(idlethread))
336242139Strasz			continue;
337242139Strasz#ifdef SMP
338242139Strasz		found = 0;
339242139Strasz		STAILQ_FOREACH(pc, &cpuhead, pc_allcpu) {
340242139Strasz			if (td == pc->pc_idlethread) {
341242139Strasz				found = 1;
342242139Strasz				break;
343242139Strasz			}
344242139Strasz		}
345242139Strasz		if (found)
346242139Strasz			continue;
347242139Strasz#endif
348242139Strasz		thread_lock(td);
349242139Strasz#ifdef SCHED_4BSD
350242139Strasz		pctcpu = sched_pctcpu(td);
351242139Strasz		/* Count also the yet unfinished second. */
352242139Strasz		pctcpu_next = (pctcpu * ccpu_exp[1]) >> FSHIFT;
353242139Strasz		pctcpu_next += sched_pctcpu_delta(td);
354242139Strasz		p_pctcpu += max(pctcpu, pctcpu_next);
355242139Strasz#else
356242139Strasz		/*
357242139Strasz		 * In ULE the %cpu statistics are updated on every
358242139Strasz		 * sched_pctcpu() call.  So special calculations to
359242139Strasz		 * account for the latest (unfinished) second are
360242139Strasz		 * not needed.
361242139Strasz		 */
362242139Strasz		p_pctcpu += sched_pctcpu(td);
363242139Strasz#endif
364242139Strasz		thread_unlock(td);
365242139Strasz	}
366242139Strasz
367242139Strasz#ifdef SCHED_4BSD
368242139Strasz	if (swtime <= CCPU_EXP_MAX)
369242139Strasz		return ((100 * (uint64_t)p_pctcpu * 1000000) /
370242139Strasz		    (FSCALE - ccpu_exp[swtime]));
371242139Strasz#endif
372242139Strasz
373242139Strasz	return ((100 * (uint64_t)p_pctcpu * 1000000) / FSCALE);
374242139Strasz}
375242139Strasz
376220137Straszstatic void
377220137Straszracct_add_racct(struct racct *dest, const struct racct *src)
378220137Strasz{
379220137Strasz	int i;
380220137Strasz
381220137Strasz	mtx_assert(&racct_lock, MA_OWNED);
382220137Strasz
383220137Strasz	/*
384220137Strasz	 * Update resource usage in dest.
385220137Strasz	 */
386220137Strasz	for (i = 0; i <= RACCT_MAX; i++) {
387220137Strasz		KASSERT(dest->r_resources[i] >= 0,
388220137Strasz		    ("racct propagation meltdown: dest < 0"));
389220137Strasz		KASSERT(src->r_resources[i] >= 0,
390220137Strasz		    ("racct propagation meltdown: src < 0"));
391220137Strasz		dest->r_resources[i] += src->r_resources[i];
392220137Strasz	}
393220137Strasz}
394220137Strasz
395220137Straszstatic void
396220137Straszracct_sub_racct(struct racct *dest, const struct racct *src)
397220137Strasz{
398220137Strasz	int i;
399220137Strasz
400220137Strasz	mtx_assert(&racct_lock, MA_OWNED);
401220137Strasz
402220137Strasz	/*
403220137Strasz	 * Update resource usage in dest.
404220137Strasz	 */
405220137Strasz	for (i = 0; i <= RACCT_MAX; i++) {
406223844Strasz		if (!RACCT_IS_SLOPPY(i)) {
407220137Strasz			KASSERT(dest->r_resources[i] >= 0,
408220137Strasz			    ("racct propagation meltdown: dest < 0"));
409220137Strasz			KASSERT(src->r_resources[i] >= 0,
410220137Strasz			    ("racct propagation meltdown: src < 0"));
411220137Strasz			KASSERT(src->r_resources[i] <= dest->r_resources[i],
412220137Strasz			    ("racct propagation meltdown: src > dest"));
413220137Strasz		}
414242139Strasz		if (RACCT_CAN_DROP(i)) {
415220137Strasz			dest->r_resources[i] -= src->r_resources[i];
416220137Strasz			if (dest->r_resources[i] < 0) {
417223844Strasz				KASSERT(RACCT_IS_SLOPPY(i),
418220137Strasz				    ("racct_sub_racct: usage < 0"));
419220137Strasz				dest->r_resources[i] = 0;
420220137Strasz			}
421220137Strasz		}
422220137Strasz	}
423220137Strasz}
424220137Strasz
425220137Straszvoid
426220137Straszracct_create(struct racct **racctp)
427220137Strasz{
428220137Strasz
429220137Strasz	SDT_PROBE(racct, kernel, racct, create, racctp, 0, 0, 0, 0);
430220137Strasz
431220137Strasz	KASSERT(*racctp == NULL, ("racct already allocated"));
432220137Strasz
433220137Strasz	*racctp = uma_zalloc(racct_zone, M_WAITOK | M_ZERO);
434220137Strasz}
435220137Strasz
436220137Straszstatic void
437220137Straszracct_destroy_locked(struct racct **racctp)
438220137Strasz{
439220137Strasz	int i;
440220137Strasz	struct racct *racct;
441220137Strasz
442220137Strasz	SDT_PROBE(racct, kernel, racct, destroy, racctp, 0, 0, 0, 0);
443220137Strasz
444220137Strasz	mtx_assert(&racct_lock, MA_OWNED);
445220137Strasz	KASSERT(racctp != NULL, ("NULL racctp"));
446220137Strasz	KASSERT(*racctp != NULL, ("NULL racct"));
447220137Strasz
448220137Strasz	racct = *racctp;
449220137Strasz
450220137Strasz	for (i = 0; i <= RACCT_MAX; i++) {
451223844Strasz		if (RACCT_IS_SLOPPY(i))
452220137Strasz			continue;
453223844Strasz		if (!RACCT_IS_RECLAIMABLE(i))
454220137Strasz			continue;
455220137Strasz		KASSERT(racct->r_resources[i] == 0,
456220137Strasz		    ("destroying non-empty racct: "
457220137Strasz		    "%ju allocated for resource %d\n",
458220137Strasz		    racct->r_resources[i], i));
459220137Strasz	}
460220137Strasz	uma_zfree(racct_zone, racct);
461220137Strasz	*racctp = NULL;
462220137Strasz}
463220137Strasz
464220137Straszvoid
465220137Straszracct_destroy(struct racct **racct)
466220137Strasz{
467220137Strasz
468220137Strasz	mtx_lock(&racct_lock);
469220137Strasz	racct_destroy_locked(racct);
470220137Strasz	mtx_unlock(&racct_lock);
471220137Strasz}
472220137Strasz
473220137Strasz/*
474220137Strasz * Increase consumption of 'resource' by 'amount' for 'racct'
475220137Strasz * and all its parents.  Differently from other cases, 'amount' here
476220137Strasz * may be less than zero.
477220137Strasz */
478220137Straszstatic void
479220137Straszracct_alloc_resource(struct racct *racct, int resource,
480220137Strasz    uint64_t amount)
481220137Strasz{
482220137Strasz
483220137Strasz	mtx_assert(&racct_lock, MA_OWNED);
484220137Strasz	KASSERT(racct != NULL, ("NULL racct"));
485220137Strasz
486220137Strasz	racct->r_resources[resource] += amount;
487220137Strasz	if (racct->r_resources[resource] < 0) {
488242139Strasz		KASSERT(RACCT_IS_SLOPPY(resource) || RACCT_IS_DECAYING(resource),
489220137Strasz		    ("racct_alloc_resource: usage < 0"));
490220137Strasz		racct->r_resources[resource] = 0;
491220137Strasz	}
492242139Strasz
493242139Strasz	/*
494242139Strasz	 * There are some cases where the racct %cpu resource would grow
495242139Strasz	 * beyond 100%.
496242139Strasz	 * For example in racct_proc_exit() we add the process %cpu usage
497242139Strasz	 * to the ucred racct containers.  If too many processes terminated
498242139Strasz	 * in a short time span, the ucred %cpu resource could grow too much.
499242139Strasz	 * Also, the 4BSD scheduler sometimes returns for a thread more than
500242139Strasz	 * 100% cpu usage.  So we set a boundary here to 100%.
501242139Strasz	 */
502242139Strasz	if ((resource == RACCT_PCTCPU) &&
503242139Strasz	    (racct->r_resources[RACCT_PCTCPU] > 100 * 1000000))
504242139Strasz		racct->r_resources[RACCT_PCTCPU] = 100 * 1000000;
505220137Strasz}
506220137Strasz
507225944Straszstatic int
508225944Straszracct_add_locked(struct proc *p, int resource, uint64_t amount)
509220137Strasz{
510220137Strasz#ifdef RCTL
511220137Strasz	int error;
512220137Strasz#endif
513220137Strasz
514220137Strasz	SDT_PROBE(racct, kernel, rusage, add, p, resource, amount, 0, 0);
515220137Strasz
516220137Strasz	/*
517220137Strasz	 * We need proc lock to dereference p->p_ucred.
518220137Strasz	 */
519220137Strasz	PROC_LOCK_ASSERT(p, MA_OWNED);
520220137Strasz
521220137Strasz#ifdef RCTL
522220137Strasz	error = rctl_enforce(p, resource, amount);
523223844Strasz	if (error && RACCT_IS_DENIABLE(resource)) {
524220137Strasz		SDT_PROBE(racct, kernel, rusage, add_failure, p, resource,
525220137Strasz		    amount, 0, 0);
526220137Strasz		return (error);
527220137Strasz	}
528220137Strasz#endif
529220137Strasz	racct_alloc_resource(p->p_racct, resource, amount);
530220137Strasz	racct_add_cred_locked(p->p_ucred, resource, amount);
531220137Strasz
532220137Strasz	return (0);
533220137Strasz}
534220137Strasz
535225944Strasz/*
536225944Strasz * Increase allocation of 'resource' by 'amount' for process 'p'.
537225944Strasz * Return 0 if it's below limits, or errno, if it's not.
538225944Strasz */
539225944Straszint
540225944Straszracct_add(struct proc *p, int resource, uint64_t amount)
541225944Strasz{
542225944Strasz	int error;
543225944Strasz
544225944Strasz	mtx_lock(&racct_lock);
545225944Strasz	error = racct_add_locked(p, resource, amount);
546225944Strasz	mtx_unlock(&racct_lock);
547225944Strasz	return (error);
548225944Strasz}
549225944Strasz
550220137Straszstatic void
551220137Straszracct_add_cred_locked(struct ucred *cred, int resource, uint64_t amount)
552220137Strasz{
553220137Strasz	struct prison *pr;
554220137Strasz
555220137Strasz	SDT_PROBE(racct, kernel, rusage, add_cred, cred, resource, amount,
556220137Strasz	    0, 0);
557220137Strasz
558220137Strasz	racct_alloc_resource(cred->cr_ruidinfo->ui_racct, resource, amount);
559220137Strasz	for (pr = cred->cr_prison; pr != NULL; pr = pr->pr_parent)
560221362Strasz		racct_alloc_resource(pr->pr_prison_racct->prr_racct, resource,
561221362Strasz		    amount);
562220137Strasz	racct_alloc_resource(cred->cr_loginclass->lc_racct, resource, amount);
563220137Strasz}
564220137Strasz
565220137Strasz/*
566220137Strasz * Increase allocation of 'resource' by 'amount' for credential 'cred'.
567220137Strasz * Doesn't check for limits and never fails.
568220137Strasz *
569220137Strasz * XXX: Shouldn't this ever return an error?
570220137Strasz */
571220137Straszvoid
572220137Straszracct_add_cred(struct ucred *cred, int resource, uint64_t amount)
573220137Strasz{
574220137Strasz
575220137Strasz	mtx_lock(&racct_lock);
576220137Strasz	racct_add_cred_locked(cred, resource, amount);
577220137Strasz	mtx_unlock(&racct_lock);
578220137Strasz}
579220137Strasz
580220137Strasz/*
581220137Strasz * Increase allocation of 'resource' by 'amount' for process 'p'.
582220137Strasz * Doesn't check for limits and never fails.
583220137Strasz */
584220137Straszvoid
585220137Straszracct_add_force(struct proc *p, int resource, uint64_t amount)
586220137Strasz{
587220137Strasz
588220137Strasz	SDT_PROBE(racct, kernel, rusage, add_force, p, resource, amount, 0, 0);
589220137Strasz
590220137Strasz	/*
591220137Strasz	 * We need proc lock to dereference p->p_ucred.
592220137Strasz	 */
593220137Strasz	PROC_LOCK_ASSERT(p, MA_OWNED);
594220137Strasz
595220137Strasz	mtx_lock(&racct_lock);
596220137Strasz	racct_alloc_resource(p->p_racct, resource, amount);
597220137Strasz	mtx_unlock(&racct_lock);
598220137Strasz	racct_add_cred(p->p_ucred, resource, amount);
599220137Strasz}
600220137Strasz
601220137Straszstatic int
602220137Straszracct_set_locked(struct proc *p, int resource, uint64_t amount)
603220137Strasz{
604242139Strasz	int64_t old_amount, decayed_amount;
605242139Strasz	int64_t diff_proc, diff_cred;
606220137Strasz#ifdef RCTL
607220137Strasz	int error;
608220137Strasz#endif
609220137Strasz
610220137Strasz	SDT_PROBE(racct, kernel, rusage, set, p, resource, amount, 0, 0);
611220137Strasz
612220137Strasz	/*
613220137Strasz	 * We need proc lock to dereference p->p_ucred.
614220137Strasz	 */
615220137Strasz	PROC_LOCK_ASSERT(p, MA_OWNED);
616220137Strasz
617242139Strasz	old_amount = p->p_racct->r_resources[resource];
618242139Strasz	/*
619242139Strasz	 * The diffs may be negative.
620242139Strasz	 */
621242139Strasz	diff_proc = amount - old_amount;
622242139Strasz	if (RACCT_IS_DECAYING(resource)) {
623242139Strasz		/*
624242139Strasz		 * Resources in per-credential racct containers may decay.
625242139Strasz		 * If this is the case, we need to calculate the difference
626242139Strasz		 * between the new amount and the proportional value of the
627242139Strasz		 * old amount that has decayed in the ucred racct containers.
628242139Strasz		 */
629242139Strasz		decayed_amount = old_amount * RACCT_DECAY_FACTOR / FSCALE;
630242139Strasz		diff_cred = amount - decayed_amount;
631242139Strasz	} else
632242139Strasz		diff_cred = diff_proc;
633220137Strasz#ifdef notyet
634242139Strasz	KASSERT(diff_proc >= 0 || RACCT_CAN_DROP(resource),
635242139Strasz	    ("racct_set: usage of non-droppable resource %d dropping",
636220137Strasz	     resource));
637220137Strasz#endif
638220137Strasz#ifdef RCTL
639242139Strasz	if (diff_proc > 0) {
640242139Strasz		error = rctl_enforce(p, resource, diff_proc);
641223844Strasz		if (error && RACCT_IS_DENIABLE(resource)) {
642220137Strasz			SDT_PROBE(racct, kernel, rusage, set_failure, p,
643220137Strasz			    resource, amount, 0, 0);
644220137Strasz			return (error);
645220137Strasz		}
646220137Strasz	}
647220137Strasz#endif
648242139Strasz	racct_alloc_resource(p->p_racct, resource, diff_proc);
649242139Strasz	if (diff_cred > 0)
650242139Strasz		racct_add_cred_locked(p->p_ucred, resource, diff_cred);
651242139Strasz	else if (diff_cred < 0)
652242139Strasz		racct_sub_cred_locked(p->p_ucred, resource, -diff_cred);
653220137Strasz
654220137Strasz	return (0);
655220137Strasz}
656220137Strasz
657220137Strasz/*
658220137Strasz * Set allocation of 'resource' to 'amount' for process 'p'.
659220137Strasz * Return 0 if it's below limits, or errno, if it's not.
660220137Strasz *
661220137Strasz * Note that decreasing the allocation always returns 0,
662220137Strasz * even if it's above the limit.
663220137Strasz */
664220137Straszint
665220137Straszracct_set(struct proc *p, int resource, uint64_t amount)
666220137Strasz{
667220137Strasz	int error;
668220137Strasz
669220137Strasz	mtx_lock(&racct_lock);
670220137Strasz	error = racct_set_locked(p, resource, amount);
671220137Strasz	mtx_unlock(&racct_lock);
672220137Strasz	return (error);
673220137Strasz}
674220137Strasz
675242139Straszstatic void
676242139Straszracct_set_force_locked(struct proc *p, int resource, uint64_t amount)
677220137Strasz{
678242139Strasz	int64_t old_amount, decayed_amount;
679242139Strasz	int64_t diff_proc, diff_cred;
680220137Strasz
681220137Strasz	SDT_PROBE(racct, kernel, rusage, set, p, resource, amount, 0, 0);
682220137Strasz
683220137Strasz	/*
684220137Strasz	 * We need proc lock to dereference p->p_ucred.
685220137Strasz	 */
686220137Strasz	PROC_LOCK_ASSERT(p, MA_OWNED);
687220137Strasz
688242139Strasz	old_amount = p->p_racct->r_resources[resource];
689242139Strasz	/*
690242139Strasz	 * The diffs may be negative.
691242139Strasz	 */
692242139Strasz	diff_proc = amount - old_amount;
693242139Strasz	if (RACCT_IS_DECAYING(resource)) {
694242139Strasz		/*
695242139Strasz		 * Resources in per-credential racct containers may decay.
696242139Strasz		 * If this is the case, we need to calculate the difference
697242139Strasz		 * between the new amount and the proportional value of the
698242139Strasz		 * old amount that has decayed in the ucred racct containers.
699242139Strasz		 */
700242139Strasz		decayed_amount = old_amount * RACCT_DECAY_FACTOR / FSCALE;
701242139Strasz		diff_cred = amount - decayed_amount;
702242139Strasz	} else
703242139Strasz		diff_cred = diff_proc;
704242139Strasz
705242139Strasz	racct_alloc_resource(p->p_racct, resource, diff_proc);
706242139Strasz	if (diff_cred > 0)
707242139Strasz		racct_add_cred_locked(p->p_ucred, resource, diff_cred);
708242139Strasz	else if (diff_cred < 0)
709242139Strasz		racct_sub_cred_locked(p->p_ucred, resource, -diff_cred);
710242139Strasz}
711242139Strasz
712242139Straszvoid
713242139Straszracct_set_force(struct proc *p, int resource, uint64_t amount)
714242139Strasz{
715220137Strasz	mtx_lock(&racct_lock);
716242139Strasz	racct_set_force_locked(p, resource, amount);
717220137Strasz	mtx_unlock(&racct_lock);
718220137Strasz}
719220137Strasz
720220137Strasz/*
721220137Strasz * Returns amount of 'resource' the process 'p' can keep allocated.
722220137Strasz * Allocating more than that would be denied, unless the resource
723220137Strasz * is marked undeniable.  Amount of already allocated resource does
724220137Strasz * not matter.
725220137Strasz */
726220137Straszuint64_t
727220137Straszracct_get_limit(struct proc *p, int resource)
728220137Strasz{
729220137Strasz
730220137Strasz#ifdef RCTL
731220137Strasz	return (rctl_get_limit(p, resource));
732220137Strasz#else
733220137Strasz	return (UINT64_MAX);
734220137Strasz#endif
735220137Strasz}
736220137Strasz
737220137Strasz/*
738220137Strasz * Returns amount of 'resource' the process 'p' can keep allocated.
739220137Strasz * Allocating more than that would be denied, unless the resource
740220137Strasz * is marked undeniable.  Amount of already allocated resource does
741220137Strasz * matter.
742220137Strasz */
743220137Straszuint64_t
744220137Straszracct_get_available(struct proc *p, int resource)
745220137Strasz{
746220137Strasz
747220137Strasz#ifdef RCTL
748220137Strasz	return (rctl_get_available(p, resource));
749220137Strasz#else
750220137Strasz	return (UINT64_MAX);
751220137Strasz#endif
752220137Strasz}
753220137Strasz
754220137Strasz/*
755242139Strasz * Returns amount of the %cpu resource that process 'p' can add to its %cpu
756242139Strasz * utilization.  Adding more than that would lead to the process being
757242139Strasz * throttled.
758242139Strasz */
759242139Straszstatic int64_t
760242139Straszracct_pcpu_available(struct proc *p)
761242139Strasz{
762242139Strasz
763242139Strasz#ifdef RCTL
764242139Strasz	return (rctl_pcpu_available(p));
765242139Strasz#else
766242139Strasz	return (INT64_MAX);
767242139Strasz#endif
768242139Strasz}
769242139Strasz
770242139Strasz/*
771220137Strasz * Decrease allocation of 'resource' by 'amount' for process 'p'.
772220137Strasz */
773220137Straszvoid
774220137Straszracct_sub(struct proc *p, int resource, uint64_t amount)
775220137Strasz{
776220137Strasz
777220137Strasz	SDT_PROBE(racct, kernel, rusage, sub, p, resource, amount, 0, 0);
778220137Strasz
779220137Strasz	/*
780220137Strasz	 * We need proc lock to dereference p->p_ucred.
781220137Strasz	 */
782220137Strasz	PROC_LOCK_ASSERT(p, MA_OWNED);
783242139Strasz	KASSERT(RACCT_CAN_DROP(resource),
784242139Strasz	    ("racct_sub: called for non-droppable resource %d", resource));
785220137Strasz
786220137Strasz	mtx_lock(&racct_lock);
787220137Strasz	KASSERT(amount <= p->p_racct->r_resources[resource],
788220137Strasz	    ("racct_sub: freeing %ju of resource %d, which is more "
789220137Strasz	     "than allocated %jd for %s (pid %d)", amount, resource,
790220137Strasz	    (intmax_t)p->p_racct->r_resources[resource], p->p_comm, p->p_pid));
791220137Strasz
792220137Strasz	racct_alloc_resource(p->p_racct, resource, -amount);
793220137Strasz	racct_sub_cred_locked(p->p_ucred, resource, amount);
794220137Strasz	mtx_unlock(&racct_lock);
795220137Strasz}
796220137Strasz
797220137Straszstatic void
798220137Straszracct_sub_cred_locked(struct ucred *cred, int resource, uint64_t amount)
799220137Strasz{
800220137Strasz	struct prison *pr;
801220137Strasz
802220137Strasz	SDT_PROBE(racct, kernel, rusage, sub_cred, cred, resource, amount,
803220137Strasz	    0, 0);
804220137Strasz
805220137Strasz#ifdef notyet
806242139Strasz	KASSERT(RACCT_CAN_DROP(resource),
807242139Strasz	    ("racct_sub_cred: called for resource %d which can not drop",
808220137Strasz	     resource));
809220137Strasz#endif
810220137Strasz
811220137Strasz	racct_alloc_resource(cred->cr_ruidinfo->ui_racct, resource, -amount);
812220137Strasz	for (pr = cred->cr_prison; pr != NULL; pr = pr->pr_parent)
813221362Strasz		racct_alloc_resource(pr->pr_prison_racct->prr_racct, resource,
814221362Strasz		    -amount);
815220137Strasz	racct_alloc_resource(cred->cr_loginclass->lc_racct, resource, -amount);
816220137Strasz}
817220137Strasz
818220137Strasz/*
819220137Strasz * Decrease allocation of 'resource' by 'amount' for credential 'cred'.
820220137Strasz */
821220137Straszvoid
822220137Straszracct_sub_cred(struct ucred *cred, int resource, uint64_t amount)
823220137Strasz{
824220137Strasz
825220137Strasz	mtx_lock(&racct_lock);
826220137Strasz	racct_sub_cred_locked(cred, resource, amount);
827220137Strasz	mtx_unlock(&racct_lock);
828220137Strasz}
829220137Strasz
830220137Strasz/*
831220137Strasz * Inherit resource usage information from the parent process.
832220137Strasz */
833220137Straszint
834220137Straszracct_proc_fork(struct proc *parent, struct proc *child)
835220137Strasz{
836220137Strasz	int i, error = 0;
837220137Strasz
838220137Strasz	/*
839220137Strasz	 * Create racct for the child process.
840220137Strasz	 */
841220137Strasz	racct_create(&child->p_racct);
842220137Strasz
843220137Strasz	PROC_LOCK(parent);
844220137Strasz	PROC_LOCK(child);
845220137Strasz	mtx_lock(&racct_lock);
846220137Strasz
847225981Strasz#ifdef RCTL
848225981Strasz	error = rctl_proc_fork(parent, child);
849225981Strasz	if (error != 0)
850225981Strasz		goto out;
851225981Strasz#endif
852225981Strasz
853242139Strasz	/* Init process cpu time. */
854242139Strasz	child->p_prev_runtime = 0;
855242139Strasz	child->p_throttled = 0;
856242139Strasz
857220137Strasz	/*
858220137Strasz	 * Inherit resource usage.
859220137Strasz	 */
860220137Strasz	for (i = 0; i <= RACCT_MAX; i++) {
861220137Strasz		if (parent->p_racct->r_resources[i] == 0 ||
862223844Strasz		    !RACCT_IS_INHERITABLE(i))
863220137Strasz			continue;
864220137Strasz
865220137Strasz		error = racct_set_locked(child, i,
866220137Strasz		    parent->p_racct->r_resources[i]);
867225938Strasz		if (error != 0)
868220137Strasz			goto out;
869220137Strasz	}
870220137Strasz
871225944Strasz	error = racct_add_locked(child, RACCT_NPROC, 1);
872225944Strasz	error += racct_add_locked(child, RACCT_NTHR, 1);
873225944Strasz
874220137Straszout:
875220137Strasz	mtx_unlock(&racct_lock);
876220137Strasz	PROC_UNLOCK(child);
877220137Strasz	PROC_UNLOCK(parent);
878220137Strasz
879235787Strasz	if (error != 0)
880235787Strasz		racct_proc_exit(child);
881235787Strasz
882220137Strasz	return (error);
883220137Strasz}
884220137Strasz
885225940Strasz/*
886225940Strasz * Called at the end of fork1(), to handle rules that require the process
887225940Strasz * to be fully initialized.
888225940Strasz */
889220137Straszvoid
890225940Straszracct_proc_fork_done(struct proc *child)
891225940Strasz{
892225940Strasz
893225940Strasz#ifdef RCTL
894225940Strasz	PROC_LOCK(child);
895225940Strasz	mtx_lock(&racct_lock);
896225940Strasz	rctl_enforce(child, RACCT_NPROC, 0);
897225940Strasz	rctl_enforce(child, RACCT_NTHR, 0);
898225940Strasz	mtx_unlock(&racct_lock);
899225940Strasz	PROC_UNLOCK(child);
900225940Strasz#endif
901225940Strasz}
902225940Strasz
903225940Straszvoid
904220137Straszracct_proc_exit(struct proc *p)
905220137Strasz{
906225364Strasz	int i;
907220137Strasz	uint64_t runtime;
908242139Strasz	struct timeval wallclock;
909242139Strasz	uint64_t pct_estimate, pct;
910220137Strasz
911220137Strasz	PROC_LOCK(p);
912220137Strasz	/*
913220137Strasz	 * We don't need to calculate rux, proc_reap() has already done this.
914220137Strasz	 */
915220137Strasz	runtime = cputick2usec(p->p_rux.rux_runtime);
916220137Strasz#ifdef notyet
917220137Strasz	KASSERT(runtime >= p->p_prev_runtime, ("runtime < p_prev_runtime"));
918220137Strasz#else
919220137Strasz	if (runtime < p->p_prev_runtime)
920220137Strasz		runtime = p->p_prev_runtime;
921220137Strasz#endif
922242139Strasz	microuptime(&wallclock);
923242139Strasz	timevalsub(&wallclock, &p->p_stats->p_start);
924242957Strasz	if (wallclock.tv_sec > 0 || wallclock.tv_usec > 0) {
925242957Strasz		pct_estimate = (1000000 * runtime * 100) /
926242957Strasz		    ((uint64_t)wallclock.tv_sec * 1000000 +
927242957Strasz		    wallclock.tv_usec);
928242957Strasz	} else
929242957Strasz		pct_estimate = 0;
930242139Strasz	pct = racct_getpcpu(p, pct_estimate);
931242139Strasz
932225364Strasz	mtx_lock(&racct_lock);
933225364Strasz	racct_set_locked(p, RACCT_CPU, runtime);
934242139Strasz	racct_add_cred_locked(p->p_ucred, RACCT_PCTCPU, pct);
935220137Strasz
936225364Strasz	for (i = 0; i <= RACCT_MAX; i++) {
937225364Strasz		if (p->p_racct->r_resources[i] == 0)
938225364Strasz			continue;
939225364Strasz	    	if (!RACCT_IS_RECLAIMABLE(i))
940225364Strasz			continue;
941225364Strasz		racct_set_locked(p, i, 0);
942225364Strasz	}
943225364Strasz
944225364Strasz	mtx_unlock(&racct_lock);
945220137Strasz	PROC_UNLOCK(p);
946220137Strasz
947220137Strasz#ifdef RCTL
948220137Strasz	rctl_racct_release(p->p_racct);
949220137Strasz#endif
950220137Strasz	racct_destroy(&p->p_racct);
951220137Strasz}
952220137Strasz
953220137Strasz/*
954220137Strasz * Called after credentials change, to move resource utilisation
955220137Strasz * between raccts.
956220137Strasz */
957220137Straszvoid
958220137Straszracct_proc_ucred_changed(struct proc *p, struct ucred *oldcred,
959220137Strasz    struct ucred *newcred)
960220137Strasz{
961220137Strasz	struct uidinfo *olduip, *newuip;
962220137Strasz	struct loginclass *oldlc, *newlc;
963220137Strasz	struct prison *oldpr, *newpr, *pr;
964220137Strasz
965220137Strasz	PROC_LOCK_ASSERT(p, MA_NOTOWNED);
966220137Strasz
967220137Strasz	newuip = newcred->cr_ruidinfo;
968220137Strasz	olduip = oldcred->cr_ruidinfo;
969220137Strasz	newlc = newcred->cr_loginclass;
970220137Strasz	oldlc = oldcred->cr_loginclass;
971220137Strasz	newpr = newcred->cr_prison;
972220137Strasz	oldpr = oldcred->cr_prison;
973220137Strasz
974220137Strasz	mtx_lock(&racct_lock);
975220137Strasz	if (newuip != olduip) {
976220137Strasz		racct_sub_racct(olduip->ui_racct, p->p_racct);
977220137Strasz		racct_add_racct(newuip->ui_racct, p->p_racct);
978220137Strasz	}
979220137Strasz	if (newlc != oldlc) {
980220137Strasz		racct_sub_racct(oldlc->lc_racct, p->p_racct);
981220137Strasz		racct_add_racct(newlc->lc_racct, p->p_racct);
982220137Strasz	}
983220137Strasz	if (newpr != oldpr) {
984220137Strasz		for (pr = oldpr; pr != NULL; pr = pr->pr_parent)
985221362Strasz			racct_sub_racct(pr->pr_prison_racct->prr_racct,
986221362Strasz			    p->p_racct);
987220137Strasz		for (pr = newpr; pr != NULL; pr = pr->pr_parent)
988221362Strasz			racct_add_racct(pr->pr_prison_racct->prr_racct,
989221362Strasz			    p->p_racct);
990220137Strasz	}
991220137Strasz	mtx_unlock(&racct_lock);
992220137Strasz
993220137Strasz#ifdef RCTL
994220137Strasz	rctl_proc_ucred_changed(p, newcred);
995220137Strasz#endif
996220137Strasz}
997220137Strasz
998232598Straszvoid
999232598Straszracct_move(struct racct *dest, struct racct *src)
1000232598Strasz{
1001232598Strasz
1002232598Strasz	mtx_lock(&racct_lock);
1003232598Strasz
1004232598Strasz	racct_add_racct(dest, src);
1005232598Strasz	racct_sub_racct(src, src);
1006232598Strasz
1007232598Strasz	mtx_unlock(&racct_lock);
1008232598Strasz}
1009232598Strasz
1010220137Straszstatic void
1011242139Straszracct_proc_throttle(struct proc *p)
1012242139Strasz{
1013242139Strasz	struct thread *td;
1014242139Strasz#ifdef SMP
1015242139Strasz	int cpuid;
1016242139Strasz#endif
1017242139Strasz
1018242139Strasz	PROC_LOCK_ASSERT(p, MA_OWNED);
1019242139Strasz
1020242139Strasz	/*
1021242139Strasz	 * Do not block kernel processes.  Also do not block processes with
1022242139Strasz	 * low %cpu utilization to improve interactivity.
1023242139Strasz	 */
1024242139Strasz	if (((p->p_flag & (P_SYSTEM | P_KTHREAD)) != 0) ||
1025242139Strasz	    (p->p_racct->r_resources[RACCT_PCTCPU] <= pcpu_threshold))
1026242139Strasz		return;
1027242139Strasz	p->p_throttled = 1;
1028242139Strasz
1029242139Strasz	FOREACH_THREAD_IN_PROC(p, td) {
1030242139Strasz		switch (td->td_state) {
1031242139Strasz		case TDS_RUNQ:
1032242139Strasz			/*
1033242139Strasz			 * If the thread is on the scheduler run-queue, we can
1034242139Strasz			 * not just remove it from there.  So we set the flag
1035242139Strasz			 * TDF_NEEDRESCHED for the thread, so that once it is
1036242139Strasz			 * running, it is taken off the cpu as soon as possible.
1037242139Strasz			 */
1038242139Strasz			thread_lock(td);
1039242139Strasz			td->td_flags |= TDF_NEEDRESCHED;
1040242139Strasz			thread_unlock(td);
1041242139Strasz			break;
1042242139Strasz		case TDS_RUNNING:
1043242139Strasz			/*
1044242139Strasz			 * If the thread is running, we request a context
1045242139Strasz			 * switch for it by setting the TDF_NEEDRESCHED flag.
1046242139Strasz			 */
1047242139Strasz			thread_lock(td);
1048242139Strasz			td->td_flags |= TDF_NEEDRESCHED;
1049242139Strasz#ifdef SMP
1050242139Strasz			cpuid = td->td_oncpu;
1051242139Strasz			if ((cpuid != NOCPU) && (td != curthread))
1052242139Strasz				ipi_cpu(cpuid, IPI_AST);
1053242139Strasz#endif
1054242139Strasz			thread_unlock(td);
1055242139Strasz			break;
1056242139Strasz		default:
1057242139Strasz			break;
1058242139Strasz		}
1059242139Strasz	}
1060242139Strasz}
1061242139Strasz
1062242139Straszstatic void
1063242139Straszracct_proc_wakeup(struct proc *p)
1064242139Strasz{
1065242139Strasz	PROC_LOCK_ASSERT(p, MA_OWNED);
1066242139Strasz
1067242139Strasz	if (p->p_throttled) {
1068242139Strasz		p->p_throttled = 0;
1069242139Strasz		wakeup(p->p_racct);
1070242139Strasz	}
1071242139Strasz}
1072242139Strasz
1073242139Straszstatic void
1074242139Straszracct_decay_resource(struct racct *racct, void * res, void* dummy)
1075242139Strasz{
1076242139Strasz	int resource;
1077242139Strasz	int64_t r_old, r_new;
1078242139Strasz
1079242139Strasz	resource = *(int *)res;
1080242139Strasz	r_old = racct->r_resources[resource];
1081242139Strasz
1082242139Strasz	/* If there is nothing to decay, just exit. */
1083242139Strasz	if (r_old <= 0)
1084242139Strasz		return;
1085242139Strasz
1086242139Strasz	mtx_lock(&racct_lock);
1087242139Strasz	r_new = r_old * RACCT_DECAY_FACTOR / FSCALE;
1088242139Strasz	racct->r_resources[resource] = r_new;
1089242139Strasz	mtx_unlock(&racct_lock);
1090242139Strasz}
1091242139Strasz
1092242139Straszstatic void
1093242139Straszracct_decay(int resource)
1094242139Strasz{
1095242139Strasz	ui_racct_foreach(racct_decay_resource, &resource, NULL);
1096242139Strasz	loginclass_racct_foreach(racct_decay_resource, &resource, NULL);
1097242139Strasz	prison_racct_foreach(racct_decay_resource, &resource, NULL);
1098242139Strasz}
1099242139Strasz
1100242139Straszstatic void
1101220137Straszracctd(void)
1102220137Strasz{
1103220137Strasz	struct thread *td;
1104220137Strasz	struct proc *p;
1105220137Strasz	struct timeval wallclock;
1106220137Strasz	uint64_t runtime;
1107242139Strasz	uint64_t pct, pct_estimate;
1108220137Strasz
1109220137Strasz	for (;;) {
1110242139Strasz		racct_decay(RACCT_PCTCPU);
1111242139Strasz
1112220137Strasz		sx_slock(&allproc_lock);
1113220137Strasz
1114242139Strasz		LIST_FOREACH(p, &zombproc, p_list) {
1115242139Strasz			PROC_LOCK(p);
1116242139Strasz			racct_set(p, RACCT_PCTCPU, 0);
1117242139Strasz			PROC_UNLOCK(p);
1118242139Strasz		}
1119242139Strasz
1120220137Strasz		FOREACH_PROC_IN_SYSTEM(p) {
1121242139Strasz			PROC_LOCK(p);
1122242139Strasz			if (p->p_state != PRS_NORMAL) {
1123242139Strasz				PROC_UNLOCK(p);
1124220137Strasz				continue;
1125242139Strasz			}
1126220137Strasz
1127220137Strasz			microuptime(&wallclock);
1128220137Strasz			timevalsub(&wallclock, &p->p_stats->p_start);
1129220137Strasz			PROC_SLOCK(p);
1130232782Strasz			FOREACH_THREAD_IN_PROC(p, td)
1131220137Strasz				ruxagg(p, td);
1132220137Strasz			runtime = cputick2usec(p->p_rux.rux_runtime);
1133220137Strasz			PROC_SUNLOCK(p);
1134220137Strasz#ifdef notyet
1135220137Strasz			KASSERT(runtime >= p->p_prev_runtime,
1136220137Strasz			    ("runtime < p_prev_runtime"));
1137220137Strasz#else
1138220137Strasz			if (runtime < p->p_prev_runtime)
1139220137Strasz				runtime = p->p_prev_runtime;
1140220137Strasz#endif
1141220137Strasz			p->p_prev_runtime = runtime;
1142242957Strasz			if (wallclock.tv_sec > 0 || wallclock.tv_usec > 0) {
1143242957Strasz				pct_estimate = (1000000 * runtime * 100) /
1144242957Strasz				    ((uint64_t)wallclock.tv_sec * 1000000 +
1145242957Strasz				    wallclock.tv_usec);
1146242957Strasz			} else
1147242957Strasz				pct_estimate = 0;
1148242139Strasz			pct = racct_getpcpu(p, pct_estimate);
1149220137Strasz			mtx_lock(&racct_lock);
1150242139Strasz			racct_set_force_locked(p, RACCT_PCTCPU, pct);
1151220137Strasz			racct_set_locked(p, RACCT_CPU, runtime);
1152220137Strasz			racct_set_locked(p, RACCT_WALLCLOCK,
1153233126Sjh			    (uint64_t)wallclock.tv_sec * 1000000 +
1154233126Sjh			    wallclock.tv_usec);
1155220137Strasz			mtx_unlock(&racct_lock);
1156220137Strasz			PROC_UNLOCK(p);
1157220137Strasz		}
1158242139Strasz
1159242139Strasz		/*
1160242139Strasz		 * To ensure that processes are throttled in a fair way, we need
1161242139Strasz		 * to iterate over all processes again and check the limits
1162242139Strasz		 * for %cpu resource only after ucred racct containers have been
1163242139Strasz		 * properly filled.
1164242139Strasz		 */
1165242139Strasz		FOREACH_PROC_IN_SYSTEM(p) {
1166242139Strasz			PROC_LOCK(p);
1167242139Strasz			if (p->p_state != PRS_NORMAL) {
1168242139Strasz				PROC_UNLOCK(p);
1169242139Strasz				continue;
1170242139Strasz			}
1171242139Strasz
1172242139Strasz			if (racct_pcpu_available(p) <= 0)
1173242139Strasz				racct_proc_throttle(p);
1174242139Strasz			else if (p->p_throttled)
1175242139Strasz				racct_proc_wakeup(p);
1176242139Strasz			PROC_UNLOCK(p);
1177242139Strasz		}
1178220137Strasz		sx_sunlock(&allproc_lock);
1179220137Strasz		pause("-", hz);
1180220137Strasz	}
1181220137Strasz}
1182220137Strasz
1183220137Straszstatic struct kproc_desc racctd_kp = {
1184220137Strasz	"racctd",
1185220137Strasz	racctd,
1186220137Strasz	NULL
1187220137Strasz};
1188220137StraszSYSINIT(racctd, SI_SUB_RACCTD, SI_ORDER_FIRST, kproc_start, &racctd_kp);
1189220137Strasz
1190220137Straszstatic void
1191220137Straszracct_init(void)
1192220137Strasz{
1193220137Strasz
1194220137Strasz	racct_zone = uma_zcreate("racct", sizeof(struct racct),
1195220137Strasz	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
1196220137Strasz	/*
1197220137Strasz	 * XXX: Move this somewhere.
1198220137Strasz	 */
1199221362Strasz	prison0.pr_prison_racct = prison_racct_find("0");
1200220137Strasz}
1201220137StraszSYSINIT(racct, SI_SUB_RACCT, SI_ORDER_FIRST, racct_init, NULL);
1202220137Strasz
1203220137Strasz#else /* !RACCT */
1204220137Strasz
1205220137Straszint
1206220137Straszracct_add(struct proc *p, int resource, uint64_t amount)
1207220137Strasz{
1208220137Strasz
1209220137Strasz	return (0);
1210220137Strasz}
1211220137Strasz
1212220137Straszvoid
1213220137Straszracct_add_cred(struct ucred *cred, int resource, uint64_t amount)
1214220137Strasz{
1215220137Strasz}
1216220137Strasz
1217220137Straszvoid
1218220137Straszracct_add_force(struct proc *p, int resource, uint64_t amount)
1219220137Strasz{
1220220137Strasz
1221220137Strasz	return;
1222220137Strasz}
1223220137Strasz
1224220137Straszint
1225220137Straszracct_set(struct proc *p, int resource, uint64_t amount)
1226220137Strasz{
1227220137Strasz
1228220137Strasz	return (0);
1229220137Strasz}
1230220137Strasz
1231220137Straszvoid
1232220372Straszracct_set_force(struct proc *p, int resource, uint64_t amount)
1233220372Strasz{
1234220372Strasz}
1235220372Strasz
1236220372Straszvoid
1237220137Straszracct_sub(struct proc *p, int resource, uint64_t amount)
1238220137Strasz{
1239220137Strasz}
1240220137Strasz
1241220137Straszvoid
1242220137Straszracct_sub_cred(struct ucred *cred, int resource, uint64_t amount)
1243220137Strasz{
1244220137Strasz}
1245220137Strasz
1246220137Straszuint64_t
1247220137Straszracct_get_limit(struct proc *p, int resource)
1248220137Strasz{
1249220137Strasz
1250220137Strasz	return (UINT64_MAX);
1251220137Strasz}
1252220137Strasz
1253220372Straszuint64_t
1254220372Straszracct_get_available(struct proc *p, int resource)
1255220372Strasz{
1256220372Strasz
1257220372Strasz	return (UINT64_MAX);
1258220372Strasz}
1259220372Strasz
1260220137Straszvoid
1261220137Straszracct_create(struct racct **racctp)
1262220137Strasz{
1263220137Strasz}
1264220137Strasz
1265220137Straszvoid
1266220137Straszracct_destroy(struct racct **racctp)
1267220137Strasz{
1268220137Strasz}
1269220137Strasz
1270220137Straszint
1271220137Straszracct_proc_fork(struct proc *parent, struct proc *child)
1272220137Strasz{
1273220137Strasz
1274220137Strasz	return (0);
1275220137Strasz}
1276220137Strasz
1277220137Straszvoid
1278225940Straszracct_proc_fork_done(struct proc *child)
1279225940Strasz{
1280225940Strasz}
1281225940Strasz
1282225940Straszvoid
1283220137Straszracct_proc_exit(struct proc *p)
1284220137Strasz{
1285220137Strasz}
1286220137Strasz
1287220137Strasz#endif /* !RACCT */
1288