]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/kern/kern_racct.c
MFC r368207,368607:
[FreeBSD/stable/10.git] / sys / kern / kern_racct.c
1 /*-
2  * Copyright (c) 2010 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Edward Tomasz Napierala under sponsorship
6  * from the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include "opt_kdtrace.h"
36 #include "opt_sched.h"
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/eventhandler.h>
41 #include <sys/jail.h>
42 #include <sys/kernel.h>
43 #include <sys/kthread.h>
44 #include <sys/lock.h>
45 #include <sys/loginclass.h>
46 #include <sys/malloc.h>
47 #include <sys/mutex.h>
48 #include <sys/proc.h>
49 #include <sys/racct.h>
50 #include <sys/resourcevar.h>
51 #include <sys/sbuf.h>
52 #include <sys/sched.h>
53 #include <sys/sdt.h>
54 #include <sys/smp.h>
55 #include <sys/sx.h>
56 #include <sys/sysctl.h>
57 #include <sys/sysent.h>
58 #include <sys/sysproto.h>
59 #include <sys/umtx.h>
60 #include <machine/smp.h>
61
62 #ifdef RCTL
63 #include <sys/rctl.h>
64 #endif
65
66 #ifdef RACCT
67
68 FEATURE(racct, "Resource Accounting");
69
70 /*
71  * Do not block processes that have their %cpu usage <= pcpu_threshold.
72  */
73 static int pcpu_threshold = 1;
74 #ifdef RACCT_DEFAULT_TO_DISABLED
75 int racct_enable = 0;
76 #else
77 int racct_enable = 1;
78 #endif
79
80 SYSCTL_NODE(_kern, OID_AUTO, racct, CTLFLAG_RW, 0, "Resource Accounting");
81 TUNABLE_INT("kern.racct.enable", &racct_enable);
82 SYSCTL_UINT(_kern_racct, OID_AUTO, enable, CTLFLAG_RDTUN, &racct_enable,
83     0, "Enable RACCT/RCTL");
84 SYSCTL_UINT(_kern_racct, OID_AUTO, pcpu_threshold, CTLFLAG_RW, &pcpu_threshold,
85     0, "Processes with higher %cpu usage than this value can be throttled.");
86
87 /*
88  * How many seconds it takes to use the scheduler %cpu calculations.  When a
89  * process starts, we compute its %cpu usage by dividing its runtime by the
90  * process wall clock time.  After RACCT_PCPU_SECS pass, we use the value
91  * provided by the scheduler.
92  */
93 #define RACCT_PCPU_SECS         3
94
95 static struct mtx racct_lock;
96 MTX_SYSINIT(racct_lock, &racct_lock, "racct lock", MTX_DEF);
97
98 static uma_zone_t racct_zone;
99
100 static void racct_sub_racct(struct racct *dest, const struct racct *src);
101 static void racct_sub_cred_locked(struct ucred *cred, int resource,
102                 uint64_t amount);
103 static void racct_add_cred_locked(struct ucred *cred, int resource,
104                 uint64_t amount);
105
106 SDT_PROVIDER_DEFINE(racct);
107 SDT_PROBE_DEFINE3(racct, , rusage, add,
108     "struct proc *", "int", "uint64_t");
109 SDT_PROBE_DEFINE3(racct, , rusage, add__failure,
110     "struct proc *", "int", "uint64_t");
111 SDT_PROBE_DEFINE3(racct, , rusage, add__cred,
112     "struct ucred *", "int", "uint64_t");
113 SDT_PROBE_DEFINE3(racct, , rusage, add__force,
114     "struct proc *", "int", "uint64_t");
115 SDT_PROBE_DEFINE3(racct, , rusage, set,
116     "struct proc *", "int", "uint64_t");
117 SDT_PROBE_DEFINE3(racct, , rusage, set__failure,
118     "struct proc *", "int", "uint64_t");
119 SDT_PROBE_DEFINE3(racct, , rusage, sub,
120     "struct proc *", "int", "uint64_t");
121 SDT_PROBE_DEFINE3(racct, , rusage, sub__cred,
122     "struct ucred *", "int", "uint64_t");
123 SDT_PROBE_DEFINE1(racct, , racct, create,
124     "struct racct *");
125 SDT_PROBE_DEFINE1(racct, , racct, destroy,
126     "struct racct *");
127 SDT_PROBE_DEFINE2(racct, , racct, join,
128     "struct racct *", "struct racct *");
129 SDT_PROBE_DEFINE2(racct, , racct, join__failure,
130     "struct racct *", "struct racct *");
131 SDT_PROBE_DEFINE2(racct, , racct, leave,
132     "struct racct *", "struct racct *");
133
134 int racct_types[] = {
135         [RACCT_CPU] =
136                 RACCT_IN_MILLIONS,
137         [RACCT_DATA] =
138                 RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
139         [RACCT_STACK] =
140                 RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
141         [RACCT_CORE] =
142                 RACCT_DENIABLE,
143         [RACCT_RSS] =
144                 RACCT_RECLAIMABLE,
145         [RACCT_MEMLOCK] =
146                 RACCT_RECLAIMABLE | RACCT_DENIABLE,
147         [RACCT_NPROC] =
148                 RACCT_RECLAIMABLE | RACCT_DENIABLE,
149         [RACCT_NOFILE] =
150                 RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
151         [RACCT_VMEM] =
152                 RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
153         [RACCT_NPTS] =
154                 RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
155         [RACCT_SWAP] =
156                 RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
157         [RACCT_NTHR] =
158                 RACCT_RECLAIMABLE | RACCT_DENIABLE,
159         [RACCT_MSGQQUEUED] =
160                 RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
161         [RACCT_MSGQSIZE] =
162                 RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
163         [RACCT_NMSGQ] =
164                 RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
165         [RACCT_NSEM] =
166                 RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
167         [RACCT_NSEMOP] =
168                 RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
169         [RACCT_NSHM] =
170                 RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
171         [RACCT_SHMSIZE] =
172                 RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
173         [RACCT_WALLCLOCK] =
174                 RACCT_IN_MILLIONS,
175         [RACCT_PCTCPU] =
176                 RACCT_DECAYING | RACCT_DENIABLE | RACCT_IN_MILLIONS };
177
178 static const fixpt_t RACCT_DECAY_FACTOR = 0.3 * FSCALE;
179
180 #ifdef SCHED_4BSD
181 /*
182  * Contains intermediate values for %cpu calculations to avoid using floating
183  * point in the kernel.
184  * ccpu_exp[k] = FSCALE * (ccpu/FSCALE)^k = FSCALE * exp(-k/20)
185  * It is needed only for the 4BSD scheduler, because in ULE, the ccpu equals to
186  * zero so the calculations are more straightforward.
187  */
188 fixpt_t ccpu_exp[] = {
189         [0] = FSCALE * 1,
190         [1] = FSCALE * 0.95122942450071400909,
191         [2] = FSCALE * 0.90483741803595957316,
192         [3] = FSCALE * 0.86070797642505780722,
193         [4] = FSCALE * 0.81873075307798185866,
194         [5] = FSCALE * 0.77880078307140486824,
195         [6] = FSCALE * 0.74081822068171786606,
196         [7] = FSCALE * 0.70468808971871343435,
197         [8] = FSCALE * 0.67032004603563930074,
198         [9] = FSCALE * 0.63762815162177329314,
199         [10] = FSCALE * 0.60653065971263342360,
200         [11] = FSCALE * 0.57694981038048669531,
201         [12] = FSCALE * 0.54881163609402643262,
202         [13] = FSCALE * 0.52204577676101604789,
203         [14] = FSCALE * 0.49658530379140951470,
204         [15] = FSCALE * 0.47236655274101470713,
205         [16] = FSCALE * 0.44932896411722159143,
206         [17] = FSCALE * 0.42741493194872666992,
207         [18] = FSCALE * 0.40656965974059911188,
208         [19] = FSCALE * 0.38674102345450120691,
209         [20] = FSCALE * 0.36787944117144232159,
210         [21] = FSCALE * 0.34993774911115535467,
211         [22] = FSCALE * 0.33287108369807955328,
212         [23] = FSCALE * 0.31663676937905321821,
213         [24] = FSCALE * 0.30119421191220209664,
214         [25] = FSCALE * 0.28650479686019010032,
215         [26] = FSCALE * 0.27253179303401260312,
216         [27] = FSCALE * 0.25924026064589150757,
217         [28] = FSCALE * 0.24659696394160647693,
218         [29] = FSCALE * 0.23457028809379765313,
219         [30] = FSCALE * 0.22313016014842982893,
220         [31] = FSCALE * 0.21224797382674305771,
221         [32] = FSCALE * 0.20189651799465540848,
222         [33] = FSCALE * 0.19204990862075411423,
223         [34] = FSCALE * 0.18268352405273465022,
224         [35] = FSCALE * 0.17377394345044512668,
225         [36] = FSCALE * 0.16529888822158653829,
226         [37] = FSCALE * 0.15723716631362761621,
227         [38] = FSCALE * 0.14956861922263505264,
228         [39] = FSCALE * 0.14227407158651357185,
229         [40] = FSCALE * 0.13533528323661269189,
230         [41] = FSCALE * 0.12873490358780421886,
231         [42] = FSCALE * 0.12245642825298191021,
232         [43] = FSCALE * 0.11648415777349695786,
233         [44] = FSCALE * 0.11080315836233388333,
234         [45] = FSCALE * 0.10539922456186433678,
235         [46] = FSCALE * 0.10025884372280373372,
236         [47] = FSCALE * 0.09536916221554961888,
237         [48] = FSCALE * 0.09071795328941250337,
238         [49] = FSCALE * 0.08629358649937051097,
239         [50] = FSCALE * 0.08208499862389879516,
240         [51] = FSCALE * 0.07808166600115315231,
241         [52] = FSCALE * 0.07427357821433388042,
242         [53] = FSCALE * 0.07065121306042958674,
243         [54] = FSCALE * 0.06720551273974976512,
244         [55] = FSCALE * 0.06392786120670757270,
245         [56] = FSCALE * 0.06081006262521796499,
246         [57] = FSCALE * 0.05784432087483846296,
247         [58] = FSCALE * 0.05502322005640722902,
248         [59] = FSCALE * 0.05233970594843239308,
249         [60] = FSCALE * 0.04978706836786394297,
250         [61] = FSCALE * 0.04735892439114092119,
251         [62] = FSCALE * 0.04504920239355780606,
252         [63] = FSCALE * 0.04285212686704017991,
253         [64] = FSCALE * 0.04076220397836621516,
254         [65] = FSCALE * 0.03877420783172200988,
255         [66] = FSCALE * 0.03688316740124000544,
256         [67] = FSCALE * 0.03508435410084502588,
257         [68] = FSCALE * 0.03337326996032607948,
258         [69] = FSCALE * 0.03174563637806794323,
259         [70] = FSCALE * 0.03019738342231850073,
260         [71] = FSCALE * 0.02872463965423942912,
261         [72] = FSCALE * 0.02732372244729256080,
262         [73] = FSCALE * 0.02599112877875534358,
263         [74] = FSCALE * 0.02472352647033939120,
264         [75] = FSCALE * 0.02351774585600910823,
265         [76] = FSCALE * 0.02237077185616559577,
266         [77] = FSCALE * 0.02127973643837716938,
267         [78] = FSCALE * 0.02024191144580438847,
268         [79] = FSCALE * 0.01925470177538692429,
269         [80] = FSCALE * 0.01831563888873418029,
270         [81] = FSCALE * 0.01742237463949351138,
271         [82] = FSCALE * 0.01657267540176124754,
272         [83] = FSCALE * 0.01576441648485449082,
273         [84] = FSCALE * 0.01499557682047770621,
274         [85] = FSCALE * 0.01426423390899925527,
275         [86] = FSCALE * 0.01356855901220093175,
276         [87] = FSCALE * 0.01290681258047986886,
277         [88] = FSCALE * 0.01227733990306844117,
278         [89] = FSCALE * 0.01167856697039544521,
279         [90] = FSCALE * 0.01110899653824230649,
280         [91] = FSCALE * 0.01056720438385265337,
281         [92] = FSCALE * 0.01005183574463358164,
282         [93] = FSCALE * 0.00956160193054350793,
283         [94] = FSCALE * 0.00909527710169581709,
284         [95] = FSCALE * 0.00865169520312063417,
285         [96] = FSCALE * 0.00822974704902002884,
286         [97] = FSCALE * 0.00782837754922577143,
287         [98] = FSCALE * 0.00744658307092434051,
288         [99] = FSCALE * 0.00708340892905212004,
289         [100] = FSCALE * 0.00673794699908546709,
290         [101] = FSCALE * 0.00640933344625638184,
291         [102] = FSCALE * 0.00609674656551563610,
292         [103] = FSCALE * 0.00579940472684214321,
293         [104] = FSCALE * 0.00551656442076077241,
294         [105] = FSCALE * 0.00524751839918138427,
295         [106] = FSCALE * 0.00499159390691021621,
296         [107] = FSCALE * 0.00474815099941147558,
297         [108] = FSCALE * 0.00451658094261266798,
298         [109] = FSCALE * 0.00429630469075234057,
299         [110] = FSCALE * 0.00408677143846406699,
300 };
301 #endif
302
303 #define CCPU_EXP_MAX    110
304
305 /*
306  * This function is analogical to the getpcpu() function in the ps(1) command.
307  * They should both calculate in the same way so that the racct %cpu
308  * calculations are consistent with the values showed by the ps(1) tool.
309  * The calculations are more complex in the 4BSD scheduler because of the value
310  * of the ccpu variable.  In ULE it is defined to be zero which saves us some
311  * work.
312  */
313 static uint64_t
314 racct_getpcpu(struct proc *p, u_int pcpu)
315 {
316         u_int swtime;
317 #ifdef SCHED_4BSD
318         fixpt_t pctcpu, pctcpu_next;
319 #endif
320 #ifdef SMP
321         struct pcpu *pc;
322         int found;
323 #endif
324         fixpt_t p_pctcpu;
325         struct thread *td;
326
327         ASSERT_RACCT_ENABLED();
328
329         /*
330          * If the process is swapped out, we count its %cpu usage as zero.
331          * This behaviour is consistent with the userland ps(1) tool.
332          */
333         if ((p->p_flag & P_INMEM) == 0)
334                 return (0);
335         swtime = (ticks - p->p_swtick) / hz;
336
337         /*
338          * For short-lived processes, the sched_pctcpu() returns small
339          * values even for cpu intensive processes.  Therefore we use
340          * our own estimate in this case.
341          */
342         if (swtime < RACCT_PCPU_SECS)
343                 return (pcpu);
344
345         p_pctcpu = 0;
346         FOREACH_THREAD_IN_PROC(p, td) {
347                 if (td == PCPU_GET(idlethread))
348                         continue;
349 #ifdef SMP
350                 found = 0;
351                 STAILQ_FOREACH(pc, &cpuhead, pc_allcpu) {
352                         if (td == pc->pc_idlethread) {
353                                 found = 1;
354                                 break;
355                         }
356                 }
357                 if (found)
358                         continue;
359 #endif
360                 thread_lock(td);
361 #ifdef SCHED_4BSD
362                 pctcpu = sched_pctcpu(td);
363                 /* Count also the yet unfinished second. */
364                 pctcpu_next = (pctcpu * ccpu_exp[1]) >> FSHIFT;
365                 pctcpu_next += sched_pctcpu_delta(td);
366                 p_pctcpu += max(pctcpu, pctcpu_next);
367 #else
368                 /*
369                  * In ULE the %cpu statistics are updated on every
370                  * sched_pctcpu() call.  So special calculations to
371                  * account for the latest (unfinished) second are
372                  * not needed.
373                  */
374                 p_pctcpu += sched_pctcpu(td);
375 #endif
376                 thread_unlock(td);
377         }
378
379 #ifdef SCHED_4BSD
380         if (swtime <= CCPU_EXP_MAX)
381                 return ((100 * (uint64_t)p_pctcpu * 1000000) /
382                     (FSCALE - ccpu_exp[swtime]));
383 #endif
384
385         return ((100 * (uint64_t)p_pctcpu * 1000000) / FSCALE);
386 }
387
388 static void
389 racct_add_racct(struct racct *dest, const struct racct *src)
390 {
391         int i;
392
393         ASSERT_RACCT_ENABLED();
394         mtx_assert(&racct_lock, MA_OWNED);
395
396         /*
397          * Update resource usage in dest.
398          */
399         for (i = 0; i <= RACCT_MAX; i++) {
400                 KASSERT(dest->r_resources[i] >= 0,
401                     ("%s: resource %d propagation meltdown: dest < 0",
402                     __func__, i));
403                 KASSERT(src->r_resources[i] >= 0,
404                     ("%s: resource %d propagation meltdown: src < 0",
405                     __func__, i));
406                 dest->r_resources[i] += src->r_resources[i];
407         }
408 }
409
410 static void
411 racct_sub_racct(struct racct *dest, const struct racct *src)
412 {
413         int i;
414
415         ASSERT_RACCT_ENABLED();
416         mtx_assert(&racct_lock, MA_OWNED);
417
418         /*
419          * Update resource usage in dest.
420          */
421         for (i = 0; i <= RACCT_MAX; i++) {
422                 if (!RACCT_IS_SLOPPY(i) && !RACCT_IS_DECAYING(i)) {
423                         KASSERT(dest->r_resources[i] >= 0,
424                             ("%s: resource %d propagation meltdown: dest < 0",
425                             __func__, i));
426                         KASSERT(src->r_resources[i] >= 0,
427                             ("%s: resource %d propagation meltdown: src < 0",
428                             __func__, i));
429                         KASSERT(src->r_resources[i] <= dest->r_resources[i],
430                             ("%s: resource %d propagation meltdown: src > dest",
431                             __func__, i));
432                 }
433                 if (RACCT_CAN_DROP(i)) {
434                         dest->r_resources[i] -= src->r_resources[i];
435                         if (dest->r_resources[i] < 0) {
436                                 KASSERT(RACCT_IS_SLOPPY(i) ||
437                                     RACCT_IS_DECAYING(i),
438                                     ("%s: resource %d usage < 0", __func__, i));
439                                 dest->r_resources[i] = 0;
440                         }
441                 }
442         }
443 }
444
445 void
446 racct_create(struct racct **racctp)
447 {
448
449         if (!racct_enable)
450                 return;
451
452         SDT_PROBE1(racct, , racct, create, racctp);
453
454         KASSERT(*racctp == NULL, ("racct already allocated"));
455
456         *racctp = uma_zalloc(racct_zone, M_WAITOK | M_ZERO);
457 }
458
459 static void
460 racct_destroy_locked(struct racct **racctp)
461 {
462         int i;
463         struct racct *racct;
464
465         ASSERT_RACCT_ENABLED();
466
467         SDT_PROBE1(racct, , racct, destroy, racctp);
468
469         mtx_assert(&racct_lock, MA_OWNED);
470         KASSERT(racctp != NULL, ("NULL racctp"));
471         KASSERT(*racctp != NULL, ("NULL racct"));
472
473         racct = *racctp;
474
475         for (i = 0; i <= RACCT_MAX; i++) {
476                 if (RACCT_IS_SLOPPY(i))
477                         continue;
478                 if (!RACCT_IS_RECLAIMABLE(i))
479                         continue;
480                 KASSERT(racct->r_resources[i] == 0,
481                     ("destroying non-empty racct: "
482                     "%ju allocated for resource %d\n",
483                     racct->r_resources[i], i));
484         }
485         uma_zfree(racct_zone, racct);
486         *racctp = NULL;
487 }
488
489 void
490 racct_destroy(struct racct **racct)
491 {
492
493         if (!racct_enable)
494                 return;
495
496         mtx_lock(&racct_lock);
497         racct_destroy_locked(racct);
498         mtx_unlock(&racct_lock);
499 }
500
501 /*
502  * Increase consumption of 'resource' by 'amount' for 'racct'
503  * and all its parents.  Differently from other cases, 'amount' here
504  * may be less than zero.
505  */
506 static void
507 racct_adjust_resource(struct racct *racct, int resource,
508     uint64_t amount)
509 {
510
511         ASSERT_RACCT_ENABLED();
512         mtx_assert(&racct_lock, MA_OWNED);
513         KASSERT(racct != NULL, ("NULL racct"));
514
515         racct->r_resources[resource] += amount;
516         if (racct->r_resources[resource] < 0) {
517                 KASSERT(RACCT_IS_SLOPPY(resource) || RACCT_IS_DECAYING(resource),
518                     ("%s: resource %d usage < 0", __func__, resource));
519                 racct->r_resources[resource] = 0;
520         }
521         
522         /*
523          * There are some cases where the racct %cpu resource would grow
524          * beyond 100% per core.  For example in racct_proc_exit() we add
525          * the process %cpu usage to the ucred racct containers.  If too
526          * many processes terminated in a short time span, the ucred %cpu
527          * resource could grow too much.  Also, the 4BSD scheduler sometimes
528          * returns for a thread more than 100% cpu usage. So we set a sane
529          * boundary here to 100% * the maxumum number of CPUs.
530          */
531         if ((resource == RACCT_PCTCPU) &&
532             (racct->r_resources[RACCT_PCTCPU] > 100 * 1000000 * (int64_t)MAXCPU))
533                 racct->r_resources[RACCT_PCTCPU] = 100 * 1000000 * (int64_t)MAXCPU;
534 }
535
536 static int
537 racct_add_locked(struct proc *p, int resource, uint64_t amount)
538 {
539 #ifdef RCTL
540         int error;
541 #endif
542
543         ASSERT_RACCT_ENABLED();
544
545         SDT_PROBE3(racct, , rusage, add, p, resource, amount);
546
547         /*
548          * We need proc lock to dereference p->p_ucred.
549          */
550         PROC_LOCK_ASSERT(p, MA_OWNED);
551
552 #ifdef RCTL
553         error = rctl_enforce(p, resource, amount);
554         if (error && RACCT_IS_DENIABLE(resource)) {
555                 SDT_PROBE3(racct, , rusage, add__failure, p, resource, amount);
556                 return (error);
557         }
558 #endif
559         racct_adjust_resource(p->p_racct, resource, amount);
560         racct_add_cred_locked(p->p_ucred, resource, amount);
561
562         return (0);
563 }
564
565 /*
566  * Increase allocation of 'resource' by 'amount' for process 'p'.
567  * Return 0 if it's below limits, or errno, if it's not.
568  */
569 int
570 racct_add(struct proc *p, int resource, uint64_t amount)
571 {
572         int error;
573
574         if (!racct_enable)
575                 return (0);
576
577         mtx_lock(&racct_lock);
578         error = racct_add_locked(p, resource, amount);
579         mtx_unlock(&racct_lock);
580         return (error);
581 }
582
583 static void
584 racct_add_cred_locked(struct ucred *cred, int resource, uint64_t amount)
585 {
586         struct prison *pr;
587
588         ASSERT_RACCT_ENABLED();
589
590         SDT_PROBE3(racct, , rusage, add__cred, cred, resource, amount);
591
592         racct_adjust_resource(cred->cr_ruidinfo->ui_racct, resource, amount);
593         for (pr = cred->cr_prison; pr != NULL; pr = pr->pr_parent)
594                 racct_adjust_resource(pr->pr_prison_racct->prr_racct, resource,
595                     amount);
596         racct_adjust_resource(cred->cr_loginclass->lc_racct, resource, amount);
597 }
598
599 /*
600  * Increase allocation of 'resource' by 'amount' for credential 'cred'.
601  * Doesn't check for limits and never fails.
602  *
603  * XXX: Shouldn't this ever return an error?
604  */
605 void
606 racct_add_cred(struct ucred *cred, int resource, uint64_t amount)
607 {
608
609         if (!racct_enable)
610                 return;
611
612         mtx_lock(&racct_lock);
613         racct_add_cred_locked(cred, resource, amount);
614         mtx_unlock(&racct_lock);
615 }
616
617 /*
618  * Increase allocation of 'resource' by 'amount' for process 'p'.
619  * Doesn't check for limits and never fails.
620  */
621 void
622 racct_add_force(struct proc *p, int resource, uint64_t amount)
623 {
624
625         if (!racct_enable)
626                 return;
627
628         SDT_PROBE3(racct, , rusage, add__force, p, resource, amount);
629
630         /*
631          * We need proc lock to dereference p->p_ucred.
632          */
633         PROC_LOCK_ASSERT(p, MA_OWNED);
634
635         mtx_lock(&racct_lock);
636         racct_adjust_resource(p->p_racct, resource, amount);
637         mtx_unlock(&racct_lock);
638         racct_add_cred(p->p_ucred, resource, amount);
639 }
640
641 static int
642 racct_set_locked(struct proc *p, int resource, uint64_t amount)
643 {
644         int64_t old_amount, decayed_amount;
645         int64_t diff_proc, diff_cred;
646 #ifdef RCTL
647         int error;
648 #endif
649
650         ASSERT_RACCT_ENABLED();
651
652         SDT_PROBE3(racct, , rusage, set, p, resource, amount);
653
654         /*
655          * We need proc lock to dereference p->p_ucred.
656          */
657         PROC_LOCK_ASSERT(p, MA_OWNED);
658
659         old_amount = p->p_racct->r_resources[resource];
660         /*
661          * The diffs may be negative.
662          */
663         diff_proc = amount - old_amount;
664         if (RACCT_IS_DECAYING(resource)) {
665                 /*
666                  * Resources in per-credential racct containers may decay.
667                  * If this is the case, we need to calculate the difference
668                  * between the new amount and the proportional value of the
669                  * old amount that has decayed in the ucred racct containers.
670                  */
671                 decayed_amount = old_amount * RACCT_DECAY_FACTOR / FSCALE;
672                 diff_cred = amount - decayed_amount;
673         } else
674                 diff_cred = diff_proc;
675 #ifdef notyet
676         KASSERT(diff_proc >= 0 || RACCT_CAN_DROP(resource),
677             ("%s: usage of non-droppable resource %d dropping", __func__,
678              resource));
679 #endif
680 #ifdef RCTL
681         if (diff_proc > 0) {
682                 error = rctl_enforce(p, resource, diff_proc);
683                 if (error && RACCT_IS_DENIABLE(resource)) {
684                         SDT_PROBE3(racct, , rusage, set__failure, p, resource,
685                             amount);
686                         return (error);
687                 }
688         }
689 #endif
690         racct_adjust_resource(p->p_racct, resource, diff_proc);
691         if (diff_cred > 0)
692                 racct_add_cred_locked(p->p_ucred, resource, diff_cred);
693         else if (diff_cred < 0)
694                 racct_sub_cred_locked(p->p_ucred, resource, -diff_cred);
695
696         return (0);
697 }
698
699 /*
700  * Set allocation of 'resource' to 'amount' for process 'p'.
701  * Return 0 if it's below limits, or errno, if it's not.
702  *
703  * Note that decreasing the allocation always returns 0,
704  * even if it's above the limit.
705  */
706 int
707 racct_set(struct proc *p, int resource, uint64_t amount)
708 {
709         int error;
710
711         if (!racct_enable)
712                 return (0);
713
714         mtx_lock(&racct_lock);
715         error = racct_set_locked(p, resource, amount);
716         mtx_unlock(&racct_lock);
717         return (error);
718 }
719
720 static void
721 racct_set_force_locked(struct proc *p, int resource, uint64_t amount)
722 {
723         int64_t old_amount, decayed_amount;
724         int64_t diff_proc, diff_cred;
725
726         ASSERT_RACCT_ENABLED();
727
728         SDT_PROBE3(racct, , rusage, set, p, resource, amount);
729
730         /*
731          * We need proc lock to dereference p->p_ucred.
732          */
733         PROC_LOCK_ASSERT(p, MA_OWNED);
734
735         old_amount = p->p_racct->r_resources[resource];
736         /*
737          * The diffs may be negative.
738          */
739         diff_proc = amount - old_amount;
740         if (RACCT_IS_DECAYING(resource)) {
741                 /*
742                  * Resources in per-credential racct containers may decay.
743                  * If this is the case, we need to calculate the difference
744                  * between the new amount and the proportional value of the
745                  * old amount that has decayed in the ucred racct containers.
746                  */
747                 decayed_amount = old_amount * RACCT_DECAY_FACTOR / FSCALE;
748                 diff_cred = amount - decayed_amount;
749         } else
750                 diff_cred = diff_proc;
751
752         racct_adjust_resource(p->p_racct, resource, diff_proc);
753         if (diff_cred > 0)
754                 racct_add_cred_locked(p->p_ucred, resource, diff_cred);
755         else if (diff_cred < 0)
756                 racct_sub_cred_locked(p->p_ucred, resource, -diff_cred);
757 }
758
759 void
760 racct_set_force(struct proc *p, int resource, uint64_t amount)
761 {
762
763         if (!racct_enable)
764                 return;
765
766         mtx_lock(&racct_lock);
767         racct_set_force_locked(p, resource, amount);
768         mtx_unlock(&racct_lock);
769 }
770
771 /*
772  * Returns amount of 'resource' the process 'p' can keep allocated.
773  * Allocating more than that would be denied, unless the resource
774  * is marked undeniable.  Amount of already allocated resource does
775  * not matter.
776  */
777 uint64_t
778 racct_get_limit(struct proc *p, int resource)
779 {
780
781         if (!racct_enable)
782                 return (UINT64_MAX);
783
784 #ifdef RCTL
785         return (rctl_get_limit(p, resource));
786 #else
787         return (UINT64_MAX);
788 #endif
789 }
790
791 /*
792  * Returns amount of 'resource' the process 'p' can keep allocated.
793  * Allocating more than that would be denied, unless the resource
794  * is marked undeniable.  Amount of already allocated resource does
795  * matter.
796  */
797 uint64_t
798 racct_get_available(struct proc *p, int resource)
799 {
800
801         if (!racct_enable)
802                 return (UINT64_MAX);
803
804 #ifdef RCTL
805         return (rctl_get_available(p, resource));
806 #else
807         return (UINT64_MAX);
808 #endif
809 }
810
811 /*
812  * Returns amount of the %cpu resource that process 'p' can add to its %cpu
813  * utilization.  Adding more than that would lead to the process being
814  * throttled.
815  */
816 static int64_t
817 racct_pcpu_available(struct proc *p)
818 {
819
820         ASSERT_RACCT_ENABLED();
821
822 #ifdef RCTL
823         return (rctl_pcpu_available(p));
824 #else
825         return (INT64_MAX);
826 #endif
827 }
828
829 /*
830  * Decrease allocation of 'resource' by 'amount' for process 'p'.
831  */
832 void
833 racct_sub(struct proc *p, int resource, uint64_t amount)
834 {
835
836         if (!racct_enable)
837                 return;
838
839         SDT_PROBE3(racct, , rusage, sub, p, resource, amount);
840
841         /*
842          * We need proc lock to dereference p->p_ucred.
843          */
844         PROC_LOCK_ASSERT(p, MA_OWNED);
845         KASSERT(RACCT_CAN_DROP(resource),
846             ("%s: called for non-droppable resource %d", __func__, resource));
847
848         mtx_lock(&racct_lock);
849         KASSERT(amount <= p->p_racct->r_resources[resource],
850             ("%s: freeing %ju of resource %d, which is more "
851              "than allocated %jd for %s (pid %d)", __func__, amount, resource,
852             (intmax_t)p->p_racct->r_resources[resource], p->p_comm, p->p_pid));
853
854         racct_adjust_resource(p->p_racct, resource, -amount);
855         racct_sub_cred_locked(p->p_ucred, resource, amount);
856         mtx_unlock(&racct_lock);
857 }
858
859 static void
860 racct_sub_cred_locked(struct ucred *cred, int resource, uint64_t amount)
861 {
862         struct prison *pr;
863
864         ASSERT_RACCT_ENABLED();
865
866         SDT_PROBE3(racct, , rusage, sub__cred, cred, resource, amount);
867
868 #ifdef notyet
869         KASSERT(RACCT_CAN_DROP(resource),
870             ("%s: called for resource %d which can not drop", __func__,
871              resource));
872 #endif
873
874         racct_adjust_resource(cred->cr_ruidinfo->ui_racct, resource, -amount);
875         for (pr = cred->cr_prison; pr != NULL; pr = pr->pr_parent)
876                 racct_adjust_resource(pr->pr_prison_racct->prr_racct, resource,
877                     -amount);
878         racct_adjust_resource(cred->cr_loginclass->lc_racct, resource, -amount);
879 }
880
881 /*
882  * Decrease allocation of 'resource' by 'amount' for credential 'cred'.
883  */
884 void
885 racct_sub_cred(struct ucred *cred, int resource, uint64_t amount)
886 {
887
888         if (!racct_enable)
889                 return;
890
891         mtx_lock(&racct_lock);
892         racct_sub_cred_locked(cred, resource, amount);
893         mtx_unlock(&racct_lock);
894 }
895
896 /*
897  * Inherit resource usage information from the parent process.
898  */
899 int
900 racct_proc_fork(struct proc *parent, struct proc *child)
901 {
902         int i, error = 0;
903
904         if (!racct_enable)
905                 return (0);
906
907         /*
908          * Create racct for the child process.
909          */
910         racct_create(&child->p_racct);
911
912         PROC_LOCK(parent);
913         PROC_LOCK(child);
914         mtx_lock(&racct_lock);
915
916 #ifdef RCTL
917         error = rctl_proc_fork(parent, child);
918         if (error != 0)
919                 goto out;
920 #endif
921
922         /* Init process cpu time. */
923         child->p_prev_runtime = 0;
924         child->p_throttled = 0;
925
926         /*
927          * Inherit resource usage.
928          */
929         for (i = 0; i <= RACCT_MAX; i++) {
930                 if (parent->p_racct->r_resources[i] == 0 ||
931                     !RACCT_IS_INHERITABLE(i))
932                         continue;
933
934                 error = racct_set_locked(child, i,
935                     parent->p_racct->r_resources[i]);
936                 if (error != 0)
937                         goto out;
938         }
939
940         error = racct_add_locked(child, RACCT_NPROC, 1);
941         error += racct_add_locked(child, RACCT_NTHR, 1);
942
943 out:
944         mtx_unlock(&racct_lock);
945         PROC_UNLOCK(child);
946         PROC_UNLOCK(parent);
947
948         if (error != 0)
949                 racct_proc_exit(child);
950
951         return (error);
952 }
953
954 /*
955  * Called at the end of fork1(), to handle rules that require the process
956  * to be fully initialized.
957  */
958 void
959 racct_proc_fork_done(struct proc *child)
960 {
961
962 #ifdef RCTL
963         if (!racct_enable)
964                 return;
965
966         PROC_LOCK(child);
967         mtx_lock(&racct_lock);
968         rctl_enforce(child, RACCT_NPROC, 0);
969         rctl_enforce(child, RACCT_NTHR, 0);
970         mtx_unlock(&racct_lock);
971         PROC_UNLOCK(child);
972 #endif
973 }
974
975 void
976 racct_proc_exit(struct proc *p)
977 {
978         int i;
979         uint64_t runtime;
980         struct timeval wallclock;
981         uint64_t pct_estimate, pct;
982
983         if (!racct_enable)
984                 return;
985
986         PROC_LOCK(p);
987         /*
988          * We don't need to calculate rux, proc_reap() has already done this.
989          */
990         runtime = cputick2usec(p->p_rux.rux_runtime);
991 #ifdef notyet
992         KASSERT(runtime >= p->p_prev_runtime, ("runtime < p_prev_runtime"));
993 #else
994         if (runtime < p->p_prev_runtime)
995                 runtime = p->p_prev_runtime;
996 #endif
997         microuptime(&wallclock);
998         timevalsub(&wallclock, &p->p_stats->p_start);
999         if (wallclock.tv_sec > 0 || wallclock.tv_usec > 0) {
1000                 pct_estimate = (1000000 * runtime * 100) /
1001                     ((uint64_t)wallclock.tv_sec * 1000000 +
1002                     wallclock.tv_usec);
1003         } else
1004                 pct_estimate = 0;
1005         pct = racct_getpcpu(p, pct_estimate);
1006
1007         mtx_lock(&racct_lock);
1008         racct_set_locked(p, RACCT_CPU, runtime);
1009         racct_add_cred_locked(p->p_ucred, RACCT_PCTCPU, pct);
1010
1011         KASSERT(p->p_racct->r_resources[RACCT_RSS] == 0,
1012             ("process reaped with %ju allocated for RSS\n",
1013             p->p_racct->r_resources[RACCT_RSS]));
1014         for (i = 0; i <= RACCT_MAX; i++) {
1015                 if (p->p_racct->r_resources[i] == 0)
1016                         continue;
1017                 if (!RACCT_IS_RECLAIMABLE(i))
1018                         continue;
1019                 racct_set_locked(p, i, 0);
1020         }
1021
1022         mtx_unlock(&racct_lock);
1023         PROC_UNLOCK(p);
1024
1025 #ifdef RCTL
1026         rctl_racct_release(p->p_racct);
1027 #endif
1028         racct_destroy(&p->p_racct);
1029 }
1030
1031 /*
1032  * Called after credentials change, to move resource utilisation
1033  * between raccts.
1034  */
1035 void
1036 racct_proc_ucred_changed(struct proc *p, struct ucred *oldcred,
1037     struct ucred *newcred)
1038 {
1039         struct uidinfo *olduip, *newuip;
1040         struct loginclass *oldlc, *newlc;
1041         struct prison *oldpr, *newpr, *pr;
1042
1043         if (!racct_enable)
1044                 return;
1045
1046         PROC_LOCK_ASSERT(p, MA_NOTOWNED);
1047
1048         newuip = newcred->cr_ruidinfo;
1049         olduip = oldcred->cr_ruidinfo;
1050         newlc = newcred->cr_loginclass;
1051         oldlc = oldcred->cr_loginclass;
1052         newpr = newcred->cr_prison;
1053         oldpr = oldcred->cr_prison;
1054
1055         mtx_lock(&racct_lock);
1056         if (newuip != olduip) {
1057                 racct_sub_racct(olduip->ui_racct, p->p_racct);
1058                 racct_add_racct(newuip->ui_racct, p->p_racct);
1059         }
1060         if (newlc != oldlc) {
1061                 racct_sub_racct(oldlc->lc_racct, p->p_racct);
1062                 racct_add_racct(newlc->lc_racct, p->p_racct);
1063         }
1064         if (newpr != oldpr) {
1065                 for (pr = oldpr; pr != NULL; pr = pr->pr_parent)
1066                         racct_sub_racct(pr->pr_prison_racct->prr_racct,
1067                             p->p_racct);
1068                 for (pr = newpr; pr != NULL; pr = pr->pr_parent)
1069                         racct_add_racct(pr->pr_prison_racct->prr_racct,
1070                             p->p_racct);
1071         }
1072         mtx_unlock(&racct_lock);
1073
1074 #ifdef RCTL
1075         rctl_proc_ucred_changed(p, newcred);
1076 #endif
1077 }
1078
1079 void
1080 racct_move(struct racct *dest, struct racct *src)
1081 {
1082
1083         ASSERT_RACCT_ENABLED();
1084
1085         mtx_lock(&racct_lock);
1086
1087         racct_add_racct(dest, src);
1088         racct_sub_racct(src, src);
1089
1090         mtx_unlock(&racct_lock);
1091 }
1092
1093 static void
1094 racct_proc_throttle(struct proc *p)
1095 {
1096         struct thread *td;
1097 #ifdef SMP
1098         int cpuid;
1099 #endif
1100
1101         ASSERT_RACCT_ENABLED();
1102         PROC_LOCK_ASSERT(p, MA_OWNED);
1103
1104         /*
1105          * Do not block kernel processes.  Also do not block processes with
1106          * low %cpu utilization to improve interactivity.
1107          */
1108         if (((p->p_flag & (P_SYSTEM | P_KTHREAD)) != 0) ||
1109             (p->p_racct->r_resources[RACCT_PCTCPU] <= pcpu_threshold))
1110                 return;
1111         p->p_throttled = 1;
1112
1113         FOREACH_THREAD_IN_PROC(p, td) {
1114                 thread_lock(td);
1115                 switch (td->td_state) {
1116                 case TDS_RUNQ:
1117                         /*
1118                          * If the thread is on the scheduler run-queue, we can
1119                          * not just remove it from there.  So we set the flag
1120                          * TDF_NEEDRESCHED for the thread, so that once it is
1121                          * running, it is taken off the cpu as soon as possible.
1122                          */
1123                         td->td_flags |= TDF_NEEDRESCHED;
1124                         break;
1125                 case TDS_RUNNING:
1126                         /*
1127                          * If the thread is running, we request a context
1128                          * switch for it by setting the TDF_NEEDRESCHED flag.
1129                          */
1130                         td->td_flags |= TDF_NEEDRESCHED;
1131 #ifdef SMP
1132                         cpuid = td->td_oncpu;
1133                         if ((cpuid != NOCPU) && (td != curthread))
1134                                 ipi_cpu(cpuid, IPI_AST);
1135 #endif
1136                         break;
1137                 default:
1138                         break;
1139                 }
1140                 thread_unlock(td);
1141         }
1142 }
1143
1144 static void
1145 racct_proc_wakeup(struct proc *p)
1146 {
1147
1148         ASSERT_RACCT_ENABLED();
1149
1150         PROC_LOCK_ASSERT(p, MA_OWNED);
1151
1152         if (p->p_throttled) {
1153                 p->p_throttled = 0;
1154                 wakeup(p->p_racct);
1155         }
1156 }
1157
1158 static void
1159 racct_decay_resource(struct racct *racct, void * res, void* dummy)
1160 {
1161         int resource;
1162         int64_t r_old, r_new;
1163
1164         ASSERT_RACCT_ENABLED();
1165
1166         resource = *(int *)res;
1167         r_old = racct->r_resources[resource];
1168
1169         /* If there is nothing to decay, just exit. */
1170         if (r_old <= 0)
1171                 return;
1172
1173         mtx_lock(&racct_lock);
1174         r_new = r_old * RACCT_DECAY_FACTOR / FSCALE;
1175         racct->r_resources[resource] = r_new;
1176         mtx_unlock(&racct_lock);
1177 }
1178
1179 static void
1180 racct_decay(int resource)
1181 {
1182
1183         ASSERT_RACCT_ENABLED();
1184
1185         ui_racct_foreach(racct_decay_resource, &resource, NULL);
1186         loginclass_racct_foreach(racct_decay_resource, &resource, NULL);
1187         prison_racct_foreach(racct_decay_resource, &resource, NULL);
1188 }
1189
1190 static void
1191 racctd(void)
1192 {
1193         struct thread *td;
1194         struct proc *p;
1195         struct timeval wallclock;
1196         uint64_t runtime;
1197         uint64_t pct, pct_estimate;
1198
1199         ASSERT_RACCT_ENABLED();
1200
1201         for (;;) {
1202                 racct_decay(RACCT_PCTCPU);
1203
1204                 sx_slock(&allproc_lock);
1205
1206                 LIST_FOREACH(p, &zombproc, p_list) {
1207                         PROC_LOCK(p);
1208                         racct_set(p, RACCT_PCTCPU, 0);
1209                         PROC_UNLOCK(p);
1210                 }
1211
1212                 FOREACH_PROC_IN_SYSTEM(p) {
1213                         PROC_LOCK(p);
1214                         if (p->p_state != PRS_NORMAL) {
1215                                 PROC_UNLOCK(p);
1216                                 continue;
1217                         }
1218
1219                         microuptime(&wallclock);
1220                         timevalsub(&wallclock, &p->p_stats->p_start);
1221                         PROC_STATLOCK(p);
1222                         FOREACH_THREAD_IN_PROC(p, td)
1223                                 ruxagg(p, td);
1224                         runtime = cputick2usec(p->p_rux.rux_runtime);
1225                         PROC_STATUNLOCK(p);
1226 #ifdef notyet
1227                         KASSERT(runtime >= p->p_prev_runtime,
1228                             ("runtime < p_prev_runtime"));
1229 #else
1230                         if (runtime < p->p_prev_runtime)
1231                                 runtime = p->p_prev_runtime;
1232 #endif
1233                         p->p_prev_runtime = runtime;
1234                         if (wallclock.tv_sec > 0 || wallclock.tv_usec > 0) {
1235                                 pct_estimate = (1000000 * runtime * 100) /
1236                                     ((uint64_t)wallclock.tv_sec * 1000000 +
1237                                     wallclock.tv_usec);
1238                         } else
1239                                 pct_estimate = 0;
1240                         pct = racct_getpcpu(p, pct_estimate);
1241                         mtx_lock(&racct_lock);
1242                         racct_set_force_locked(p, RACCT_PCTCPU, pct);
1243                         racct_set_locked(p, RACCT_CPU, runtime);
1244                         racct_set_locked(p, RACCT_WALLCLOCK,
1245                             (uint64_t)wallclock.tv_sec * 1000000 +
1246                             wallclock.tv_usec);
1247                         mtx_unlock(&racct_lock);
1248                         PROC_UNLOCK(p);
1249                 }
1250
1251                 /*
1252                  * To ensure that processes are throttled in a fair way, we need
1253                  * to iterate over all processes again and check the limits
1254                  * for %cpu resource only after ucred racct containers have been
1255                  * properly filled.
1256                  */
1257                 FOREACH_PROC_IN_SYSTEM(p) {
1258                         PROC_LOCK(p);
1259                         if (p->p_state != PRS_NORMAL) {
1260                                 PROC_UNLOCK(p);
1261                                 continue;
1262                         }
1263
1264                         if (racct_pcpu_available(p) <= 0)
1265                                 racct_proc_throttle(p);
1266                         else if (p->p_throttled)
1267                                 racct_proc_wakeup(p);
1268                         PROC_UNLOCK(p);
1269                 }
1270                 sx_sunlock(&allproc_lock);
1271                 pause("-", hz);
1272         }
1273 }
1274
1275 static struct kproc_desc racctd_kp = {
1276         "racctd",
1277         racctd,
1278         NULL
1279 };
1280
1281 static void
1282 racctd_init(void)
1283 {
1284         if (!racct_enable)
1285                 return;
1286
1287         kproc_start(&racctd_kp);
1288 }
1289 SYSINIT(racctd, SI_SUB_RACCTD, SI_ORDER_FIRST, racctd_init, NULL);
1290
1291 static void
1292 racct_init(void)
1293 {
1294         if (!racct_enable)
1295                 return;
1296
1297         racct_zone = uma_zcreate("racct", sizeof(struct racct),
1298             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
1299         /*
1300          * XXX: Move this somewhere.
1301          */
1302         prison0.pr_prison_racct = prison_racct_find("0");
1303 }
1304 SYSINIT(racct, SI_SUB_RACCT, SI_ORDER_FIRST, racct_init, NULL);
1305
1306 #else /* !RACCT */
1307
1308 int
1309 racct_add(struct proc *p, int resource, uint64_t amount)
1310 {
1311
1312         return (0);
1313 }
1314
1315 void
1316 racct_add_cred(struct ucred *cred, int resource, uint64_t amount)
1317 {
1318 }
1319
1320 void
1321 racct_add_force(struct proc *p, int resource, uint64_t amount)
1322 {
1323
1324         return;
1325 }
1326
1327 int
1328 racct_set(struct proc *p, int resource, uint64_t amount)
1329 {
1330
1331         return (0);
1332 }
1333
1334 void
1335 racct_set_force(struct proc *p, int resource, uint64_t amount)
1336 {
1337 }
1338
1339 void
1340 racct_sub(struct proc *p, int resource, uint64_t amount)
1341 {
1342 }
1343
1344 void
1345 racct_sub_cred(struct ucred *cred, int resource, uint64_t amount)
1346 {
1347 }
1348
1349 uint64_t
1350 racct_get_limit(struct proc *p, int resource)
1351 {
1352
1353         return (UINT64_MAX);
1354 }
1355
1356 uint64_t
1357 racct_get_available(struct proc *p, int resource)
1358 {
1359
1360         return (UINT64_MAX);
1361 }
1362
1363 void
1364 racct_create(struct racct **racctp)
1365 {
1366 }
1367
1368 void
1369 racct_destroy(struct racct **racctp)
1370 {
1371 }
1372
1373 int
1374 racct_proc_fork(struct proc *parent, struct proc *child)
1375 {
1376
1377         return (0);
1378 }
1379
1380 void
1381 racct_proc_fork_done(struct proc *child)
1382 {
1383 }
1384
1385 void
1386 racct_proc_exit(struct proc *p)
1387 {
1388 }
1389
1390 #endif /* !RACCT */