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$
30220137Strasz */
31220137Strasz
32220137Strasz#include <sys/cdefs.h>
33220137Strasz__FBSDID("$FreeBSD$");
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);
99260817SavgSDT_PROBE_DEFINE3(racct, kernel, rusage, add, "struct proc *", "int",
100220137Strasz    "uint64_t");
101260817SavgSDT_PROBE_DEFINE3(racct, kernel, rusage, add__failure,
102220137Strasz    "struct proc *", "int", "uint64_t");
103260817SavgSDT_PROBE_DEFINE3(racct, kernel, rusage, add__cred, "struct ucred *",
104220137Strasz    "int", "uint64_t");
105260817SavgSDT_PROBE_DEFINE3(racct, kernel, rusage, add__force, "struct proc *",
106220137Strasz    "int", "uint64_t");
107260817SavgSDT_PROBE_DEFINE3(racct, kernel, rusage, set, "struct proc *", "int",
108220137Strasz    "uint64_t");
109260817SavgSDT_PROBE_DEFINE3(racct, kernel, rusage, set__failure,
110220137Strasz    "struct proc *", "int", "uint64_t");
111260817SavgSDT_PROBE_DEFINE3(racct, kernel, rusage, sub, "struct proc *", "int",
112220137Strasz    "uint64_t");
113260817SavgSDT_PROBE_DEFINE3(racct, kernel, rusage, sub__cred, "struct ucred *",
114220137Strasz    "int", "uint64_t");
115260817SavgSDT_PROBE_DEFINE1(racct, kernel, racct, create, "struct racct *");
116260817SavgSDT_PROBE_DEFINE1(racct, kernel, racct, destroy, "struct racct *");
117260817SavgSDT_PROBE_DEFINE2(racct, kernel, racct, join, "struct racct *",
118220137Strasz    "struct racct *");
119260817SavgSDT_PROBE_DEFINE2(racct, kernel, racct, join__failure,
120220137Strasz    "struct racct *", "struct racct *");
121260817SavgSDT_PROBE_DEFINE2(racct, kernel, racct, 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,
388243088Strasz		    ("%s: resource %d propagation meltdown: dest < 0",
389243088Strasz		    __func__, i));
390220137Strasz		KASSERT(src->r_resources[i] >= 0,
391243088Strasz		    ("%s: resource %d propagation meltdown: src < 0",
392243088Strasz		    __func__, i));
393220137Strasz		dest->r_resources[i] += src->r_resources[i];
394220137Strasz	}
395220137Strasz}
396220137Strasz
397220137Straszstatic void
398220137Straszracct_sub_racct(struct racct *dest, const struct racct *src)
399220137Strasz{
400220137Strasz	int i;
401220137Strasz
402220137Strasz	mtx_assert(&racct_lock, MA_OWNED);
403220137Strasz
404220137Strasz	/*
405220137Strasz	 * Update resource usage in dest.
406220137Strasz	 */
407220137Strasz	for (i = 0; i <= RACCT_MAX; i++) {
408243070Strasz		if (!RACCT_IS_SLOPPY(i) && !RACCT_IS_DECAYING(i)) {
409220137Strasz			KASSERT(dest->r_resources[i] >= 0,
410243088Strasz			    ("%s: resource %d propagation meltdown: dest < 0",
411243088Strasz			    __func__, i));
412220137Strasz			KASSERT(src->r_resources[i] >= 0,
413243088Strasz			    ("%s: resource %d propagation meltdown: src < 0",
414243088Strasz			    __func__, i));
415220137Strasz			KASSERT(src->r_resources[i] <= dest->r_resources[i],
416243088Strasz			    ("%s: resource %d propagation meltdown: src > dest",
417243088Strasz			    __func__, i));
418220137Strasz		}
419242139Strasz		if (RACCT_CAN_DROP(i)) {
420220137Strasz			dest->r_resources[i] -= src->r_resources[i];
421220137Strasz			if (dest->r_resources[i] < 0) {
422243070Strasz				KASSERT(RACCT_IS_SLOPPY(i) ||
423243070Strasz				    RACCT_IS_DECAYING(i),
424243088Strasz				    ("%s: resource %d usage < 0", __func__, i));
425220137Strasz				dest->r_resources[i] = 0;
426220137Strasz			}
427220137Strasz		}
428220137Strasz	}
429220137Strasz}
430220137Strasz
431220137Straszvoid
432220137Straszracct_create(struct racct **racctp)
433220137Strasz{
434220137Strasz
435220137Strasz	SDT_PROBE(racct, kernel, racct, create, racctp, 0, 0, 0, 0);
436220137Strasz
437220137Strasz	KASSERT(*racctp == NULL, ("racct already allocated"));
438220137Strasz
439220137Strasz	*racctp = uma_zalloc(racct_zone, M_WAITOK | M_ZERO);
440220137Strasz}
441220137Strasz
442220137Straszstatic void
443220137Straszracct_destroy_locked(struct racct **racctp)
444220137Strasz{
445220137Strasz	int i;
446220137Strasz	struct racct *racct;
447220137Strasz
448220137Strasz	SDT_PROBE(racct, kernel, racct, destroy, racctp, 0, 0, 0, 0);
449220137Strasz
450220137Strasz	mtx_assert(&racct_lock, MA_OWNED);
451220137Strasz	KASSERT(racctp != NULL, ("NULL racctp"));
452220137Strasz	KASSERT(*racctp != NULL, ("NULL racct"));
453220137Strasz
454220137Strasz	racct = *racctp;
455220137Strasz
456220137Strasz	for (i = 0; i <= RACCT_MAX; i++) {
457223844Strasz		if (RACCT_IS_SLOPPY(i))
458220137Strasz			continue;
459223844Strasz		if (!RACCT_IS_RECLAIMABLE(i))
460220137Strasz			continue;
461220137Strasz		KASSERT(racct->r_resources[i] == 0,
462220137Strasz		    ("destroying non-empty racct: "
463220137Strasz		    "%ju allocated for resource %d\n",
464220137Strasz		    racct->r_resources[i], i));
465220137Strasz	}
466220137Strasz	uma_zfree(racct_zone, racct);
467220137Strasz	*racctp = NULL;
468220137Strasz}
469220137Strasz
470220137Straszvoid
471220137Straszracct_destroy(struct racct **racct)
472220137Strasz{
473220137Strasz
474220137Strasz	mtx_lock(&racct_lock);
475220137Strasz	racct_destroy_locked(racct);
476220137Strasz	mtx_unlock(&racct_lock);
477220137Strasz}
478220137Strasz
479220137Strasz/*
480220137Strasz * Increase consumption of 'resource' by 'amount' for 'racct'
481220137Strasz * and all its parents.  Differently from other cases, 'amount' here
482220137Strasz * may be less than zero.
483220137Strasz */
484220137Straszstatic void
485220137Straszracct_alloc_resource(struct racct *racct, int resource,
486220137Strasz    uint64_t amount)
487220137Strasz{
488220137Strasz
489220137Strasz	mtx_assert(&racct_lock, MA_OWNED);
490220137Strasz	KASSERT(racct != NULL, ("NULL racct"));
491220137Strasz
492220137Strasz	racct->r_resources[resource] += amount;
493220137Strasz	if (racct->r_resources[resource] < 0) {
494242139Strasz		KASSERT(RACCT_IS_SLOPPY(resource) || RACCT_IS_DECAYING(resource),
495243088Strasz		    ("%s: resource %d usage < 0", __func__, resource));
496220137Strasz		racct->r_resources[resource] = 0;
497220137Strasz	}
498242139Strasz
499242139Strasz	/*
500242139Strasz	 * There are some cases where the racct %cpu resource would grow
501242139Strasz	 * beyond 100%.
502242139Strasz	 * For example in racct_proc_exit() we add the process %cpu usage
503242139Strasz	 * to the ucred racct containers.  If too many processes terminated
504242139Strasz	 * in a short time span, the ucred %cpu resource could grow too much.
505242139Strasz	 * Also, the 4BSD scheduler sometimes returns for a thread more than
506242139Strasz	 * 100% cpu usage.  So we set a boundary here to 100%.
507242139Strasz	 */
508242139Strasz	if ((resource == RACCT_PCTCPU) &&
509242139Strasz	    (racct->r_resources[RACCT_PCTCPU] > 100 * 1000000))
510242139Strasz		racct->r_resources[RACCT_PCTCPU] = 100 * 1000000;
511220137Strasz}
512220137Strasz
513225944Straszstatic int
514225944Straszracct_add_locked(struct proc *p, int resource, uint64_t amount)
515220137Strasz{
516220137Strasz#ifdef RCTL
517220137Strasz	int error;
518220137Strasz#endif
519220137Strasz
520220137Strasz	SDT_PROBE(racct, kernel, rusage, add, p, resource, amount, 0, 0);
521220137Strasz
522220137Strasz	/*
523220137Strasz	 * We need proc lock to dereference p->p_ucred.
524220137Strasz	 */
525220137Strasz	PROC_LOCK_ASSERT(p, MA_OWNED);
526220137Strasz
527220137Strasz#ifdef RCTL
528220137Strasz	error = rctl_enforce(p, resource, amount);
529223844Strasz	if (error && RACCT_IS_DENIABLE(resource)) {
530260817Savg		SDT_PROBE(racct, kernel, rusage, add__failure, p, resource,
531220137Strasz		    amount, 0, 0);
532220137Strasz		return (error);
533220137Strasz	}
534220137Strasz#endif
535220137Strasz	racct_alloc_resource(p->p_racct, resource, amount);
536220137Strasz	racct_add_cred_locked(p->p_ucred, resource, amount);
537220137Strasz
538220137Strasz	return (0);
539220137Strasz}
540220137Strasz
541225944Strasz/*
542225944Strasz * Increase allocation of 'resource' by 'amount' for process 'p'.
543225944Strasz * Return 0 if it's below limits, or errno, if it's not.
544225944Strasz */
545225944Straszint
546225944Straszracct_add(struct proc *p, int resource, uint64_t amount)
547225944Strasz{
548225944Strasz	int error;
549225944Strasz
550225944Strasz	mtx_lock(&racct_lock);
551225944Strasz	error = racct_add_locked(p, resource, amount);
552225944Strasz	mtx_unlock(&racct_lock);
553225944Strasz	return (error);
554225944Strasz}
555225944Strasz
556220137Straszstatic void
557220137Straszracct_add_cred_locked(struct ucred *cred, int resource, uint64_t amount)
558220137Strasz{
559220137Strasz	struct prison *pr;
560220137Strasz
561260817Savg	SDT_PROBE(racct, kernel, rusage, add__cred, cred, resource, amount,
562220137Strasz	    0, 0);
563220137Strasz
564220137Strasz	racct_alloc_resource(cred->cr_ruidinfo->ui_racct, resource, amount);
565220137Strasz	for (pr = cred->cr_prison; pr != NULL; pr = pr->pr_parent)
566221362Strasz		racct_alloc_resource(pr->pr_prison_racct->prr_racct, resource,
567221362Strasz		    amount);
568220137Strasz	racct_alloc_resource(cred->cr_loginclass->lc_racct, resource, amount);
569220137Strasz}
570220137Strasz
571220137Strasz/*
572220137Strasz * Increase allocation of 'resource' by 'amount' for credential 'cred'.
573220137Strasz * Doesn't check for limits and never fails.
574220137Strasz *
575220137Strasz * XXX: Shouldn't this ever return an error?
576220137Strasz */
577220137Straszvoid
578220137Straszracct_add_cred(struct ucred *cred, int resource, uint64_t amount)
579220137Strasz{
580220137Strasz
581220137Strasz	mtx_lock(&racct_lock);
582220137Strasz	racct_add_cred_locked(cred, resource, amount);
583220137Strasz	mtx_unlock(&racct_lock);
584220137Strasz}
585220137Strasz
586220137Strasz/*
587220137Strasz * Increase allocation of 'resource' by 'amount' for process 'p'.
588220137Strasz * Doesn't check for limits and never fails.
589220137Strasz */
590220137Straszvoid
591220137Straszracct_add_force(struct proc *p, int resource, uint64_t amount)
592220137Strasz{
593220137Strasz
594260817Savg	SDT_PROBE(racct, kernel, rusage, add__force, p, resource, amount, 0, 0);
595220137Strasz
596220137Strasz	/*
597220137Strasz	 * We need proc lock to dereference p->p_ucred.
598220137Strasz	 */
599220137Strasz	PROC_LOCK_ASSERT(p, MA_OWNED);
600220137Strasz
601220137Strasz	mtx_lock(&racct_lock);
602220137Strasz	racct_alloc_resource(p->p_racct, resource, amount);
603220137Strasz	mtx_unlock(&racct_lock);
604220137Strasz	racct_add_cred(p->p_ucred, resource, amount);
605220137Strasz}
606220137Strasz
607220137Straszstatic int
608220137Straszracct_set_locked(struct proc *p, int resource, uint64_t amount)
609220137Strasz{
610242139Strasz	int64_t old_amount, decayed_amount;
611242139Strasz	int64_t diff_proc, diff_cred;
612220137Strasz#ifdef RCTL
613220137Strasz	int error;
614220137Strasz#endif
615220137Strasz
616220137Strasz	SDT_PROBE(racct, kernel, rusage, set, p, resource, amount, 0, 0);
617220137Strasz
618220137Strasz	/*
619220137Strasz	 * We need proc lock to dereference p->p_ucred.
620220137Strasz	 */
621220137Strasz	PROC_LOCK_ASSERT(p, MA_OWNED);
622220137Strasz
623242139Strasz	old_amount = p->p_racct->r_resources[resource];
624242139Strasz	/*
625242139Strasz	 * The diffs may be negative.
626242139Strasz	 */
627242139Strasz	diff_proc = amount - old_amount;
628242139Strasz	if (RACCT_IS_DECAYING(resource)) {
629242139Strasz		/*
630242139Strasz		 * Resources in per-credential racct containers may decay.
631242139Strasz		 * If this is the case, we need to calculate the difference
632242139Strasz		 * between the new amount and the proportional value of the
633242139Strasz		 * old amount that has decayed in the ucred racct containers.
634242139Strasz		 */
635242139Strasz		decayed_amount = old_amount * RACCT_DECAY_FACTOR / FSCALE;
636242139Strasz		diff_cred = amount - decayed_amount;
637242139Strasz	} else
638242139Strasz		diff_cred = diff_proc;
639220137Strasz#ifdef notyet
640242139Strasz	KASSERT(diff_proc >= 0 || RACCT_CAN_DROP(resource),
641243088Strasz	    ("%s: usage of non-droppable resource %d dropping", __func__,
642220137Strasz	     resource));
643220137Strasz#endif
644220137Strasz#ifdef RCTL
645242139Strasz	if (diff_proc > 0) {
646242139Strasz		error = rctl_enforce(p, resource, diff_proc);
647223844Strasz		if (error && RACCT_IS_DENIABLE(resource)) {
648260817Savg			SDT_PROBE(racct, kernel, rusage, set__failure, p,
649220137Strasz			    resource, amount, 0, 0);
650220137Strasz			return (error);
651220137Strasz		}
652220137Strasz	}
653220137Strasz#endif
654242139Strasz	racct_alloc_resource(p->p_racct, resource, diff_proc);
655242139Strasz	if (diff_cred > 0)
656242139Strasz		racct_add_cred_locked(p->p_ucred, resource, diff_cred);
657242139Strasz	else if (diff_cred < 0)
658242139Strasz		racct_sub_cred_locked(p->p_ucred, resource, -diff_cred);
659220137Strasz
660220137Strasz	return (0);
661220137Strasz}
662220137Strasz
663220137Strasz/*
664220137Strasz * Set allocation of 'resource' to 'amount' for process 'p'.
665220137Strasz * Return 0 if it's below limits, or errno, if it's not.
666220137Strasz *
667220137Strasz * Note that decreasing the allocation always returns 0,
668220137Strasz * even if it's above the limit.
669220137Strasz */
670220137Straszint
671220137Straszracct_set(struct proc *p, int resource, uint64_t amount)
672220137Strasz{
673220137Strasz	int error;
674220137Strasz
675220137Strasz	mtx_lock(&racct_lock);
676220137Strasz	error = racct_set_locked(p, resource, amount);
677220137Strasz	mtx_unlock(&racct_lock);
678220137Strasz	return (error);
679220137Strasz}
680220137Strasz
681242139Straszstatic void
682242139Straszracct_set_force_locked(struct proc *p, int resource, uint64_t amount)
683220137Strasz{
684242139Strasz	int64_t old_amount, decayed_amount;
685242139Strasz	int64_t diff_proc, diff_cred;
686220137Strasz
687220137Strasz	SDT_PROBE(racct, kernel, rusage, set, p, resource, amount, 0, 0);
688220137Strasz
689220137Strasz	/*
690220137Strasz	 * We need proc lock to dereference p->p_ucred.
691220137Strasz	 */
692220137Strasz	PROC_LOCK_ASSERT(p, MA_OWNED);
693220137Strasz
694242139Strasz	old_amount = p->p_racct->r_resources[resource];
695242139Strasz	/*
696242139Strasz	 * The diffs may be negative.
697242139Strasz	 */
698242139Strasz	diff_proc = amount - old_amount;
699242139Strasz	if (RACCT_IS_DECAYING(resource)) {
700242139Strasz		/*
701242139Strasz		 * Resources in per-credential racct containers may decay.
702242139Strasz		 * If this is the case, we need to calculate the difference
703242139Strasz		 * between the new amount and the proportional value of the
704242139Strasz		 * old amount that has decayed in the ucred racct containers.
705242139Strasz		 */
706242139Strasz		decayed_amount = old_amount * RACCT_DECAY_FACTOR / FSCALE;
707242139Strasz		diff_cred = amount - decayed_amount;
708242139Strasz	} else
709242139Strasz		diff_cred = diff_proc;
710242139Strasz
711242139Strasz	racct_alloc_resource(p->p_racct, resource, diff_proc);
712242139Strasz	if (diff_cred > 0)
713242139Strasz		racct_add_cred_locked(p->p_ucred, resource, diff_cred);
714242139Strasz	else if (diff_cred < 0)
715242139Strasz		racct_sub_cred_locked(p->p_ucred, resource, -diff_cred);
716242139Strasz}
717242139Strasz
718242139Straszvoid
719242139Straszracct_set_force(struct proc *p, int resource, uint64_t amount)
720242139Strasz{
721220137Strasz	mtx_lock(&racct_lock);
722242139Strasz	racct_set_force_locked(p, resource, amount);
723220137Strasz	mtx_unlock(&racct_lock);
724220137Strasz}
725220137Strasz
726220137Strasz/*
727220137Strasz * Returns amount of 'resource' the process 'p' can keep allocated.
728220137Strasz * Allocating more than that would be denied, unless the resource
729220137Strasz * is marked undeniable.  Amount of already allocated resource does
730220137Strasz * not matter.
731220137Strasz */
732220137Straszuint64_t
733220137Straszracct_get_limit(struct proc *p, int resource)
734220137Strasz{
735220137Strasz
736220137Strasz#ifdef RCTL
737220137Strasz	return (rctl_get_limit(p, resource));
738220137Strasz#else
739220137Strasz	return (UINT64_MAX);
740220137Strasz#endif
741220137Strasz}
742220137Strasz
743220137Strasz/*
744220137Strasz * Returns amount of 'resource' the process 'p' can keep allocated.
745220137Strasz * Allocating more than that would be denied, unless the resource
746220137Strasz * is marked undeniable.  Amount of already allocated resource does
747220137Strasz * matter.
748220137Strasz */
749220137Straszuint64_t
750220137Straszracct_get_available(struct proc *p, int resource)
751220137Strasz{
752220137Strasz
753220137Strasz#ifdef RCTL
754220137Strasz	return (rctl_get_available(p, resource));
755220137Strasz#else
756220137Strasz	return (UINT64_MAX);
757220137Strasz#endif
758220137Strasz}
759220137Strasz
760220137Strasz/*
761242139Strasz * Returns amount of the %cpu resource that process 'p' can add to its %cpu
762242139Strasz * utilization.  Adding more than that would lead to the process being
763242139Strasz * throttled.
764242139Strasz */
765242139Straszstatic int64_t
766242139Straszracct_pcpu_available(struct proc *p)
767242139Strasz{
768242139Strasz
769242139Strasz#ifdef RCTL
770242139Strasz	return (rctl_pcpu_available(p));
771242139Strasz#else
772242139Strasz	return (INT64_MAX);
773242139Strasz#endif
774242139Strasz}
775242139Strasz
776242139Strasz/*
777220137Strasz * Decrease allocation of 'resource' by 'amount' for process 'p'.
778220137Strasz */
779220137Straszvoid
780220137Straszracct_sub(struct proc *p, int resource, uint64_t amount)
781220137Strasz{
782220137Strasz
783220137Strasz	SDT_PROBE(racct, kernel, rusage, sub, p, resource, amount, 0, 0);
784220137Strasz
785220137Strasz	/*
786220137Strasz	 * We need proc lock to dereference p->p_ucred.
787220137Strasz	 */
788220137Strasz	PROC_LOCK_ASSERT(p, MA_OWNED);
789242139Strasz	KASSERT(RACCT_CAN_DROP(resource),
790243088Strasz	    ("%s: called for non-droppable resource %d", __func__, resource));
791220137Strasz
792220137Strasz	mtx_lock(&racct_lock);
793220137Strasz	KASSERT(amount <= p->p_racct->r_resources[resource],
794243088Strasz	    ("%s: freeing %ju of resource %d, which is more "
795243088Strasz	     "than allocated %jd for %s (pid %d)", __func__, amount, resource,
796220137Strasz	    (intmax_t)p->p_racct->r_resources[resource], p->p_comm, p->p_pid));
797220137Strasz
798220137Strasz	racct_alloc_resource(p->p_racct, resource, -amount);
799220137Strasz	racct_sub_cred_locked(p->p_ucred, resource, amount);
800220137Strasz	mtx_unlock(&racct_lock);
801220137Strasz}
802220137Strasz
803220137Straszstatic void
804220137Straszracct_sub_cred_locked(struct ucred *cred, int resource, uint64_t amount)
805220137Strasz{
806220137Strasz	struct prison *pr;
807220137Strasz
808260817Savg	SDT_PROBE(racct, kernel, rusage, sub__cred, cred, resource, amount,
809220137Strasz	    0, 0);
810220137Strasz
811220137Strasz#ifdef notyet
812242139Strasz	KASSERT(RACCT_CAN_DROP(resource),
813243088Strasz	    ("%s: called for resource %d which can not drop", __func__,
814220137Strasz	     resource));
815220137Strasz#endif
816220137Strasz
817220137Strasz	racct_alloc_resource(cred->cr_ruidinfo->ui_racct, resource, -amount);
818220137Strasz	for (pr = cred->cr_prison; pr != NULL; pr = pr->pr_parent)
819221362Strasz		racct_alloc_resource(pr->pr_prison_racct->prr_racct, resource,
820221362Strasz		    -amount);
821220137Strasz	racct_alloc_resource(cred->cr_loginclass->lc_racct, resource, -amount);
822220137Strasz}
823220137Strasz
824220137Strasz/*
825220137Strasz * Decrease allocation of 'resource' by 'amount' for credential 'cred'.
826220137Strasz */
827220137Straszvoid
828220137Straszracct_sub_cred(struct ucred *cred, int resource, uint64_t amount)
829220137Strasz{
830220137Strasz
831220137Strasz	mtx_lock(&racct_lock);
832220137Strasz	racct_sub_cred_locked(cred, resource, amount);
833220137Strasz	mtx_unlock(&racct_lock);
834220137Strasz}
835220137Strasz
836220137Strasz/*
837220137Strasz * Inherit resource usage information from the parent process.
838220137Strasz */
839220137Straszint
840220137Straszracct_proc_fork(struct proc *parent, struct proc *child)
841220137Strasz{
842220137Strasz	int i, error = 0;
843220137Strasz
844220137Strasz	/*
845220137Strasz	 * Create racct for the child process.
846220137Strasz	 */
847220137Strasz	racct_create(&child->p_racct);
848220137Strasz
849220137Strasz	PROC_LOCK(parent);
850220137Strasz	PROC_LOCK(child);
851220137Strasz	mtx_lock(&racct_lock);
852220137Strasz
853225981Strasz#ifdef RCTL
854225981Strasz	error = rctl_proc_fork(parent, child);
855225981Strasz	if (error != 0)
856225981Strasz		goto out;
857225981Strasz#endif
858225981Strasz
859242139Strasz	/* Init process cpu time. */
860242139Strasz	child->p_prev_runtime = 0;
861242139Strasz	child->p_throttled = 0;
862242139Strasz
863220137Strasz	/*
864220137Strasz	 * Inherit resource usage.
865220137Strasz	 */
866220137Strasz	for (i = 0; i <= RACCT_MAX; i++) {
867220137Strasz		if (parent->p_racct->r_resources[i] == 0 ||
868223844Strasz		    !RACCT_IS_INHERITABLE(i))
869220137Strasz			continue;
870220137Strasz
871220137Strasz		error = racct_set_locked(child, i,
872220137Strasz		    parent->p_racct->r_resources[i]);
873225938Strasz		if (error != 0)
874220137Strasz			goto out;
875220137Strasz	}
876220137Strasz
877225944Strasz	error = racct_add_locked(child, RACCT_NPROC, 1);
878225944Strasz	error += racct_add_locked(child, RACCT_NTHR, 1);
879225944Strasz
880220137Straszout:
881220137Strasz	mtx_unlock(&racct_lock);
882220137Strasz	PROC_UNLOCK(child);
883220137Strasz	PROC_UNLOCK(parent);
884220137Strasz
885235787Strasz	if (error != 0)
886235787Strasz		racct_proc_exit(child);
887235787Strasz
888220137Strasz	return (error);
889220137Strasz}
890220137Strasz
891225940Strasz/*
892225940Strasz * Called at the end of fork1(), to handle rules that require the process
893225940Strasz * to be fully initialized.
894225940Strasz */
895220137Straszvoid
896225940Straszracct_proc_fork_done(struct proc *child)
897225940Strasz{
898225940Strasz
899225940Strasz#ifdef RCTL
900225940Strasz	PROC_LOCK(child);
901225940Strasz	mtx_lock(&racct_lock);
902225940Strasz	rctl_enforce(child, RACCT_NPROC, 0);
903225940Strasz	rctl_enforce(child, RACCT_NTHR, 0);
904225940Strasz	mtx_unlock(&racct_lock);
905225940Strasz	PROC_UNLOCK(child);
906225940Strasz#endif
907225940Strasz}
908225940Strasz
909225940Straszvoid
910220137Straszracct_proc_exit(struct proc *p)
911220137Strasz{
912225364Strasz	int i;
913220137Strasz	uint64_t runtime;
914242139Strasz	struct timeval wallclock;
915242139Strasz	uint64_t pct_estimate, pct;
916220137Strasz
917220137Strasz	PROC_LOCK(p);
918220137Strasz	/*
919220137Strasz	 * We don't need to calculate rux, proc_reap() has already done this.
920220137Strasz	 */
921220137Strasz	runtime = cputick2usec(p->p_rux.rux_runtime);
922220137Strasz#ifdef notyet
923220137Strasz	KASSERT(runtime >= p->p_prev_runtime, ("runtime < p_prev_runtime"));
924220137Strasz#else
925220137Strasz	if (runtime < p->p_prev_runtime)
926220137Strasz		runtime = p->p_prev_runtime;
927220137Strasz#endif
928242139Strasz	microuptime(&wallclock);
929242139Strasz	timevalsub(&wallclock, &p->p_stats->p_start);
930242957Strasz	if (wallclock.tv_sec > 0 || wallclock.tv_usec > 0) {
931242957Strasz		pct_estimate = (1000000 * runtime * 100) /
932242957Strasz		    ((uint64_t)wallclock.tv_sec * 1000000 +
933242957Strasz		    wallclock.tv_usec);
934242957Strasz	} else
935242957Strasz		pct_estimate = 0;
936242139Strasz	pct = racct_getpcpu(p, pct_estimate);
937242139Strasz
938225364Strasz	mtx_lock(&racct_lock);
939225364Strasz	racct_set_locked(p, RACCT_CPU, runtime);
940242139Strasz	racct_add_cred_locked(p->p_ucred, RACCT_PCTCPU, pct);
941220137Strasz
942225364Strasz	for (i = 0; i <= RACCT_MAX; i++) {
943225364Strasz		if (p->p_racct->r_resources[i] == 0)
944225364Strasz			continue;
945225364Strasz	    	if (!RACCT_IS_RECLAIMABLE(i))
946225364Strasz			continue;
947225364Strasz		racct_set_locked(p, i, 0);
948225364Strasz	}
949225364Strasz
950225364Strasz	mtx_unlock(&racct_lock);
951220137Strasz	PROC_UNLOCK(p);
952220137Strasz
953220137Strasz#ifdef RCTL
954220137Strasz	rctl_racct_release(p->p_racct);
955220137Strasz#endif
956220137Strasz	racct_destroy(&p->p_racct);
957220137Strasz}
958220137Strasz
959220137Strasz/*
960220137Strasz * Called after credentials change, to move resource utilisation
961220137Strasz * between raccts.
962220137Strasz */
963220137Straszvoid
964220137Straszracct_proc_ucred_changed(struct proc *p, struct ucred *oldcred,
965220137Strasz    struct ucred *newcred)
966220137Strasz{
967220137Strasz	struct uidinfo *olduip, *newuip;
968220137Strasz	struct loginclass *oldlc, *newlc;
969220137Strasz	struct prison *oldpr, *newpr, *pr;
970220137Strasz
971220137Strasz	PROC_LOCK_ASSERT(p, MA_NOTOWNED);
972220137Strasz
973220137Strasz	newuip = newcred->cr_ruidinfo;
974220137Strasz	olduip = oldcred->cr_ruidinfo;
975220137Strasz	newlc = newcred->cr_loginclass;
976220137Strasz	oldlc = oldcred->cr_loginclass;
977220137Strasz	newpr = newcred->cr_prison;
978220137Strasz	oldpr = oldcred->cr_prison;
979220137Strasz
980220137Strasz	mtx_lock(&racct_lock);
981220137Strasz	if (newuip != olduip) {
982220137Strasz		racct_sub_racct(olduip->ui_racct, p->p_racct);
983220137Strasz		racct_add_racct(newuip->ui_racct, p->p_racct);
984220137Strasz	}
985220137Strasz	if (newlc != oldlc) {
986220137Strasz		racct_sub_racct(oldlc->lc_racct, p->p_racct);
987220137Strasz		racct_add_racct(newlc->lc_racct, p->p_racct);
988220137Strasz	}
989220137Strasz	if (newpr != oldpr) {
990220137Strasz		for (pr = oldpr; pr != NULL; pr = pr->pr_parent)
991221362Strasz			racct_sub_racct(pr->pr_prison_racct->prr_racct,
992221362Strasz			    p->p_racct);
993220137Strasz		for (pr = newpr; pr != NULL; pr = pr->pr_parent)
994221362Strasz			racct_add_racct(pr->pr_prison_racct->prr_racct,
995221362Strasz			    p->p_racct);
996220137Strasz	}
997220137Strasz	mtx_unlock(&racct_lock);
998220137Strasz
999220137Strasz#ifdef RCTL
1000220137Strasz	rctl_proc_ucred_changed(p, newcred);
1001220137Strasz#endif
1002220137Strasz}
1003220137Strasz
1004232598Straszvoid
1005232598Straszracct_move(struct racct *dest, struct racct *src)
1006232598Strasz{
1007232598Strasz
1008232598Strasz	mtx_lock(&racct_lock);
1009232598Strasz
1010232598Strasz	racct_add_racct(dest, src);
1011232598Strasz	racct_sub_racct(src, src);
1012232598Strasz
1013232598Strasz	mtx_unlock(&racct_lock);
1014232598Strasz}
1015232598Strasz
1016220137Straszstatic void
1017242139Straszracct_proc_throttle(struct proc *p)
1018242139Strasz{
1019242139Strasz	struct thread *td;
1020242139Strasz#ifdef SMP
1021242139Strasz	int cpuid;
1022242139Strasz#endif
1023242139Strasz
1024242139Strasz	PROC_LOCK_ASSERT(p, MA_OWNED);
1025242139Strasz
1026242139Strasz	/*
1027242139Strasz	 * Do not block kernel processes.  Also do not block processes with
1028242139Strasz	 * low %cpu utilization to improve interactivity.
1029242139Strasz	 */
1030242139Strasz	if (((p->p_flag & (P_SYSTEM | P_KTHREAD)) != 0) ||
1031242139Strasz	    (p->p_racct->r_resources[RACCT_PCTCPU] <= pcpu_threshold))
1032242139Strasz		return;
1033242139Strasz	p->p_throttled = 1;
1034242139Strasz
1035242139Strasz	FOREACH_THREAD_IN_PROC(p, td) {
1036248298Strasz		thread_lock(td);
1037242139Strasz		switch (td->td_state) {
1038242139Strasz		case TDS_RUNQ:
1039242139Strasz			/*
1040242139Strasz			 * If the thread is on the scheduler run-queue, we can
1041242139Strasz			 * not just remove it from there.  So we set the flag
1042242139Strasz			 * TDF_NEEDRESCHED for the thread, so that once it is
1043242139Strasz			 * running, it is taken off the cpu as soon as possible.
1044242139Strasz			 */
1045242139Strasz			td->td_flags |= TDF_NEEDRESCHED;
1046242139Strasz			break;
1047242139Strasz		case TDS_RUNNING:
1048242139Strasz			/*
1049242139Strasz			 * If the thread is running, we request a context
1050242139Strasz			 * switch for it by setting the TDF_NEEDRESCHED flag.
1051242139Strasz			 */
1052242139Strasz			td->td_flags |= TDF_NEEDRESCHED;
1053242139Strasz#ifdef SMP
1054242139Strasz			cpuid = td->td_oncpu;
1055242139Strasz			if ((cpuid != NOCPU) && (td != curthread))
1056242139Strasz				ipi_cpu(cpuid, IPI_AST);
1057242139Strasz#endif
1058242139Strasz			break;
1059242139Strasz		default:
1060242139Strasz			break;
1061242139Strasz		}
1062248298Strasz		thread_unlock(td);
1063242139Strasz	}
1064242139Strasz}
1065242139Strasz
1066242139Straszstatic void
1067242139Straszracct_proc_wakeup(struct proc *p)
1068242139Strasz{
1069242139Strasz	PROC_LOCK_ASSERT(p, MA_OWNED);
1070242139Strasz
1071242139Strasz	if (p->p_throttled) {
1072242139Strasz		p->p_throttled = 0;
1073242139Strasz		wakeup(p->p_racct);
1074242139Strasz	}
1075242139Strasz}
1076242139Strasz
1077242139Straszstatic void
1078242139Straszracct_decay_resource(struct racct *racct, void * res, void* dummy)
1079242139Strasz{
1080242139Strasz	int resource;
1081242139Strasz	int64_t r_old, r_new;
1082242139Strasz
1083242139Strasz	resource = *(int *)res;
1084242139Strasz	r_old = racct->r_resources[resource];
1085242139Strasz
1086242139Strasz	/* If there is nothing to decay, just exit. */
1087242139Strasz	if (r_old <= 0)
1088242139Strasz		return;
1089242139Strasz
1090242139Strasz	mtx_lock(&racct_lock);
1091242139Strasz	r_new = r_old * RACCT_DECAY_FACTOR / FSCALE;
1092242139Strasz	racct->r_resources[resource] = r_new;
1093242139Strasz	mtx_unlock(&racct_lock);
1094242139Strasz}
1095242139Strasz
1096242139Straszstatic void
1097242139Straszracct_decay(int resource)
1098242139Strasz{
1099242139Strasz	ui_racct_foreach(racct_decay_resource, &resource, NULL);
1100242139Strasz	loginclass_racct_foreach(racct_decay_resource, &resource, NULL);
1101242139Strasz	prison_racct_foreach(racct_decay_resource, &resource, NULL);
1102242139Strasz}
1103242139Strasz
1104242139Straszstatic void
1105220137Straszracctd(void)
1106220137Strasz{
1107220137Strasz	struct thread *td;
1108220137Strasz	struct proc *p;
1109220137Strasz	struct timeval wallclock;
1110220137Strasz	uint64_t runtime;
1111242139Strasz	uint64_t pct, pct_estimate;
1112220137Strasz
1113220137Strasz	for (;;) {
1114242139Strasz		racct_decay(RACCT_PCTCPU);
1115242139Strasz
1116220137Strasz		sx_slock(&allproc_lock);
1117220137Strasz
1118242139Strasz		LIST_FOREACH(p, &zombproc, p_list) {
1119242139Strasz			PROC_LOCK(p);
1120242139Strasz			racct_set(p, RACCT_PCTCPU, 0);
1121242139Strasz			PROC_UNLOCK(p);
1122242139Strasz		}
1123242139Strasz
1124220137Strasz		FOREACH_PROC_IN_SYSTEM(p) {
1125242139Strasz			PROC_LOCK(p);
1126242139Strasz			if (p->p_state != PRS_NORMAL) {
1127242139Strasz				PROC_UNLOCK(p);
1128220137Strasz				continue;
1129242139Strasz			}
1130220137Strasz
1131220137Strasz			microuptime(&wallclock);
1132220137Strasz			timevalsub(&wallclock, &p->p_stats->p_start);
1133220137Strasz			PROC_SLOCK(p);
1134232782Strasz			FOREACH_THREAD_IN_PROC(p, td)
1135220137Strasz				ruxagg(p, td);
1136220137Strasz			runtime = cputick2usec(p->p_rux.rux_runtime);
1137220137Strasz			PROC_SUNLOCK(p);
1138220137Strasz#ifdef notyet
1139220137Strasz			KASSERT(runtime >= p->p_prev_runtime,
1140220137Strasz			    ("runtime < p_prev_runtime"));
1141220137Strasz#else
1142220137Strasz			if (runtime < p->p_prev_runtime)
1143220137Strasz				runtime = p->p_prev_runtime;
1144220137Strasz#endif
1145220137Strasz			p->p_prev_runtime = runtime;
1146242957Strasz			if (wallclock.tv_sec > 0 || wallclock.tv_usec > 0) {
1147242957Strasz				pct_estimate = (1000000 * runtime * 100) /
1148242957Strasz				    ((uint64_t)wallclock.tv_sec * 1000000 +
1149242957Strasz				    wallclock.tv_usec);
1150242957Strasz			} else
1151242957Strasz				pct_estimate = 0;
1152242139Strasz			pct = racct_getpcpu(p, pct_estimate);
1153220137Strasz			mtx_lock(&racct_lock);
1154242139Strasz			racct_set_force_locked(p, RACCT_PCTCPU, pct);
1155220137Strasz			racct_set_locked(p, RACCT_CPU, runtime);
1156220137Strasz			racct_set_locked(p, RACCT_WALLCLOCK,
1157233126Sjh			    (uint64_t)wallclock.tv_sec * 1000000 +
1158233126Sjh			    wallclock.tv_usec);
1159220137Strasz			mtx_unlock(&racct_lock);
1160220137Strasz			PROC_UNLOCK(p);
1161220137Strasz		}
1162242139Strasz
1163242139Strasz		/*
1164242139Strasz		 * To ensure that processes are throttled in a fair way, we need
1165242139Strasz		 * to iterate over all processes again and check the limits
1166242139Strasz		 * for %cpu resource only after ucred racct containers have been
1167242139Strasz		 * properly filled.
1168242139Strasz		 */
1169242139Strasz		FOREACH_PROC_IN_SYSTEM(p) {
1170242139Strasz			PROC_LOCK(p);
1171242139Strasz			if (p->p_state != PRS_NORMAL) {
1172242139Strasz				PROC_UNLOCK(p);
1173242139Strasz				continue;
1174242139Strasz			}
1175242139Strasz
1176242139Strasz			if (racct_pcpu_available(p) <= 0)
1177242139Strasz				racct_proc_throttle(p);
1178242139Strasz			else if (p->p_throttled)
1179242139Strasz				racct_proc_wakeup(p);
1180242139Strasz			PROC_UNLOCK(p);
1181242139Strasz		}
1182220137Strasz		sx_sunlock(&allproc_lock);
1183220137Strasz		pause("-", hz);
1184220137Strasz	}
1185220137Strasz}
1186220137Strasz
1187220137Straszstatic struct kproc_desc racctd_kp = {
1188220137Strasz	"racctd",
1189220137Strasz	racctd,
1190220137Strasz	NULL
1191220137Strasz};
1192220137StraszSYSINIT(racctd, SI_SUB_RACCTD, SI_ORDER_FIRST, kproc_start, &racctd_kp);
1193220137Strasz
1194220137Straszstatic void
1195220137Straszracct_init(void)
1196220137Strasz{
1197220137Strasz
1198220137Strasz	racct_zone = uma_zcreate("racct", sizeof(struct racct),
1199220137Strasz	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
1200220137Strasz	/*
1201220137Strasz	 * XXX: Move this somewhere.
1202220137Strasz	 */
1203221362Strasz	prison0.pr_prison_racct = prison_racct_find("0");
1204220137Strasz}
1205220137StraszSYSINIT(racct, SI_SUB_RACCT, SI_ORDER_FIRST, racct_init, NULL);
1206220137Strasz
1207220137Strasz#else /* !RACCT */
1208220137Strasz
1209220137Straszint
1210220137Straszracct_add(struct proc *p, int resource, uint64_t amount)
1211220137Strasz{
1212220137Strasz
1213220137Strasz	return (0);
1214220137Strasz}
1215220137Strasz
1216220137Straszvoid
1217220137Straszracct_add_cred(struct ucred *cred, int resource, uint64_t amount)
1218220137Strasz{
1219220137Strasz}
1220220137Strasz
1221220137Straszvoid
1222220137Straszracct_add_force(struct proc *p, int resource, uint64_t amount)
1223220137Strasz{
1224220137Strasz
1225220137Strasz	return;
1226220137Strasz}
1227220137Strasz
1228220137Straszint
1229220137Straszracct_set(struct proc *p, int resource, uint64_t amount)
1230220137Strasz{
1231220137Strasz
1232220137Strasz	return (0);
1233220137Strasz}
1234220137Strasz
1235220137Straszvoid
1236220372Straszracct_set_force(struct proc *p, int resource, uint64_t amount)
1237220372Strasz{
1238220372Strasz}
1239220372Strasz
1240220372Straszvoid
1241220137Straszracct_sub(struct proc *p, int resource, uint64_t amount)
1242220137Strasz{
1243220137Strasz}
1244220137Strasz
1245220137Straszvoid
1246220137Straszracct_sub_cred(struct ucred *cred, int resource, uint64_t amount)
1247220137Strasz{
1248220137Strasz}
1249220137Strasz
1250220137Straszuint64_t
1251220137Straszracct_get_limit(struct proc *p, int resource)
1252220137Strasz{
1253220137Strasz
1254220137Strasz	return (UINT64_MAX);
1255220137Strasz}
1256220137Strasz
1257220372Straszuint64_t
1258220372Straszracct_get_available(struct proc *p, int resource)
1259220372Strasz{
1260220372Strasz
1261220372Strasz	return (UINT64_MAX);
1262220372Strasz}
1263220372Strasz
1264220137Straszvoid
1265220137Straszracct_create(struct racct **racctp)
1266220137Strasz{
1267220137Strasz}
1268220137Strasz
1269220137Straszvoid
1270220137Straszracct_destroy(struct racct **racctp)
1271220137Strasz{
1272220137Strasz}
1273220137Strasz
1274220137Straszint
1275220137Straszracct_proc_fork(struct proc *parent, struct proc *child)
1276220137Strasz{
1277220137Strasz
1278220137Strasz	return (0);
1279220137Strasz}
1280220137Strasz
1281220137Straszvoid
1282225940Straszracct_proc_fork_done(struct proc *child)
1283225940Strasz{
1284225940Strasz}
1285225940Strasz
1286225940Straszvoid
1287220137Straszracct_proc_exit(struct proc *p)
1288220137Strasz{
1289220137Strasz}
1290220137Strasz
1291220137Strasz#endif /* !RACCT */
1292