32 #include <sys/cdefs.h>
35 #include "opt_kdtrace.h"
36 #include "opt_sched.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/eventhandler.h>
42 #include <sys/kernel.h>
43 #include <sys/kthread.h>
45 #include <sys/loginclass.h>
46 #include <sys/malloc.h>
47 #include <sys/mutex.h>
49 #include <sys/racct.h>
50 #include <sys/resourcevar.h>
52 #include <sys/sched.h>
56 #include <sys/sysctl.h>
57 #include <sys/sysent.h>
58 #include <sys/sysproto.h>
60 #include <machine/smp.h>
68 FEATURE(racct,
"Resource Accounting");
73 static int pcpu_threshold = 1;
75 SYSCTL_NODE(_kern, OID_AUTO, racct, CTLFLAG_RW, 0,
"Resource Accounting");
76 SYSCTL_UINT(_kern_racct, OID_AUTO, pcpu_threshold, CTLFLAG_RW, &pcpu_threshold,
77 0,
"Processes with higher %cpu usage than this value can be throttled.");
85 #define RACCT_PCPU_SECS 3
87 static struct mtx racct_lock;
88 MTX_SYSINIT(racct_lock, &racct_lock,
"racct lock", MTX_DEF);
90 static uma_zone_t racct_zone;
92 static void racct_sub_racct(
struct racct *dest,
const struct racct *src);
93 static void racct_sub_cred_locked(
struct ucred *cred,
int resource,
95 static void racct_add_cred_locked(
struct ucred *cred,
int resource,
102 "struct proc *",
"int",
"uint64_t");
110 "struct proc *",
"int",
"uint64_t");
120 "struct racct *",
"struct racct *");
124 int racct_types[] = {
128 RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
130 RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
136 RACCT_RECLAIMABLE | RACCT_DENIABLE,
138 RACCT_RECLAIMABLE | RACCT_DENIABLE,
140 RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
142 RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
144 RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
146 RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
148 RACCT_RECLAIMABLE | RACCT_DENIABLE,
150 RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
152 RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
154 RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
156 RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
158 RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
160 RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
162 RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
166 RACCT_DECAYING | RACCT_DENIABLE | RACCT_IN_MILLIONS };
168 static const fixpt_t RACCT_DECAY_FACTOR = 0.3 * FSCALE;
178 fixpt_t ccpu_exp[] = {
180 [1] = FSCALE * 0.95122942450071400909,
181 [2] = FSCALE * 0.90483741803595957316,
182 [3] = FSCALE * 0.86070797642505780722,
183 [4] = FSCALE * 0.81873075307798185866,
184 [5] = FSCALE * 0.77880078307140486824,
185 [6] = FSCALE * 0.74081822068171786606,
186 [7] = FSCALE * 0.70468808971871343435,
187 [8] = FSCALE * 0.67032004603563930074,
188 [9] = FSCALE * 0.63762815162177329314,
189 [10] = FSCALE * 0.60653065971263342360,
190 [11] = FSCALE * 0.57694981038048669531,
191 [12] = FSCALE * 0.54881163609402643262,
192 [13] = FSCALE * 0.52204577676101604789,
193 [14] = FSCALE * 0.49658530379140951470,
194 [15] = FSCALE * 0.47236655274101470713,
195 [16] = FSCALE * 0.44932896411722159143,
196 [17] = FSCALE * 0.42741493194872666992,
197 [18] = FSCALE * 0.40656965974059911188,
198 [19] = FSCALE * 0.38674102345450120691,
199 [20] = FSCALE * 0.36787944117144232159,
200 [21] = FSCALE * 0.34993774911115535467,
201 [22] = FSCALE * 0.33287108369807955328,
202 [23] = FSCALE * 0.31663676937905321821,
203 [24] = FSCALE * 0.30119421191220209664,
204 [25] = FSCALE * 0.28650479686019010032,
205 [26] = FSCALE * 0.27253179303401260312,
206 [27] = FSCALE * 0.25924026064589150757,
207 [28] = FSCALE * 0.24659696394160647693,
208 [29] = FSCALE * 0.23457028809379765313,
209 [30] = FSCALE * 0.22313016014842982893,
210 [31] = FSCALE * 0.21224797382674305771,
211 [32] = FSCALE * 0.20189651799465540848,
212 [33] = FSCALE * 0.19204990862075411423,
213 [34] = FSCALE * 0.18268352405273465022,
214 [35] = FSCALE * 0.17377394345044512668,
215 [36] = FSCALE * 0.16529888822158653829,
216 [37] = FSCALE * 0.15723716631362761621,
217 [38] = FSCALE * 0.14956861922263505264,
218 [39] = FSCALE * 0.14227407158651357185,
219 [40] = FSCALE * 0.13533528323661269189,
220 [41] = FSCALE * 0.12873490358780421886,
221 [42] = FSCALE * 0.12245642825298191021,
222 [43] = FSCALE * 0.11648415777349695786,
223 [44] = FSCALE * 0.11080315836233388333,
224 [45] = FSCALE * 0.10539922456186433678,
225 [46] = FSCALE * 0.10025884372280373372,
226 [47] = FSCALE * 0.09536916221554961888,
227 [48] = FSCALE * 0.09071795328941250337,
228 [49] = FSCALE * 0.08629358649937051097,
229 [50] = FSCALE * 0.08208499862389879516,
230 [51] = FSCALE * 0.07808166600115315231,
231 [52] = FSCALE * 0.07427357821433388042,
232 [53] = FSCALE * 0.07065121306042958674,
233 [54] = FSCALE * 0.06720551273974976512,
234 [55] = FSCALE * 0.06392786120670757270,
235 [56] = FSCALE * 0.06081006262521796499,
236 [57] = FSCALE * 0.05784432087483846296,
237 [58] = FSCALE * 0.05502322005640722902,
238 [59] = FSCALE * 0.05233970594843239308,
239 [60] = FSCALE * 0.04978706836786394297,
240 [61] = FSCALE * 0.04735892439114092119,
241 [62] = FSCALE * 0.04504920239355780606,
242 [63] = FSCALE * 0.04285212686704017991,
243 [64] = FSCALE * 0.04076220397836621516,
244 [65] = FSCALE * 0.03877420783172200988,
245 [66] = FSCALE * 0.03688316740124000544,
246 [67] = FSCALE * 0.03508435410084502588,
247 [68] = FSCALE * 0.03337326996032607948,
248 [69] = FSCALE * 0.03174563637806794323,
249 [70] = FSCALE * 0.03019738342231850073,
250 [71] = FSCALE * 0.02872463965423942912,
251 [72] = FSCALE * 0.02732372244729256080,
252 [73] = FSCALE * 0.02599112877875534358,
253 [74] = FSCALE * 0.02472352647033939120,
254 [75] = FSCALE * 0.02351774585600910823,
255 [76] = FSCALE * 0.02237077185616559577,
256 [77] = FSCALE * 0.02127973643837716938,
257 [78] = FSCALE * 0.02024191144580438847,
258 [79] = FSCALE * 0.01925470177538692429,
259 [80] = FSCALE * 0.01831563888873418029,
260 [81] = FSCALE * 0.01742237463949351138,
261 [82] = FSCALE * 0.01657267540176124754,
262 [83] = FSCALE * 0.01576441648485449082,
263 [84] = FSCALE * 0.01499557682047770621,
264 [85] = FSCALE * 0.01426423390899925527,
265 [86] = FSCALE * 0.01356855901220093175,
266 [87] = FSCALE * 0.01290681258047986886,
267 [88] = FSCALE * 0.01227733990306844117,
268 [89] = FSCALE * 0.01167856697039544521,
269 [90] = FSCALE * 0.01110899653824230649,
270 [91] = FSCALE * 0.01056720438385265337,
271 [92] = FSCALE * 0.01005183574463358164,
272 [93] = FSCALE * 0.00956160193054350793,
273 [94] = FSCALE * 0.00909527710169581709,
274 [95] = FSCALE * 0.00865169520312063417,
275 [96] = FSCALE * 0.00822974704902002884,
276 [97] = FSCALE * 0.00782837754922577143,
277 [98] = FSCALE * 0.00744658307092434051,
278 [99] = FSCALE * 0.00708340892905212004,
279 [100] = FSCALE * 0.00673794699908546709,
280 [101] = FSCALE * 0.00640933344625638184,
281 [102] = FSCALE * 0.00609674656551563610,
282 [103] = FSCALE * 0.00579940472684214321,
283 [104] = FSCALE * 0.00551656442076077241,
284 [105] = FSCALE * 0.00524751839918138427,
285 [106] = FSCALE * 0.00499159390691021621,
286 [107] = FSCALE * 0.00474815099941147558,
287 [108] = FSCALE * 0.00451658094261266798,
288 [109] = FSCALE * 0.00429630469075234057,
289 [110] = FSCALE * 0.00408677143846406699,
293 #define CCPU_EXP_MAX 110
304 racct_getpcpu(
struct proc *p, u_int pcpu)
308 fixpt_t pctcpu, pctcpu_next;
321 if ((p->p_flag & P_INMEM) == 0)
323 swtime = (
ticks - p->p_swtick) /
hz;
330 if (swtime < RACCT_PCPU_SECS)
334 FOREACH_THREAD_IN_PROC(p, td) {
335 if (td == PCPU_GET(idlethread))
339 STAILQ_FOREACH(pc, &cpuhead, pc_allcpu) {
340 if (td == pc->pc_idlethread) {
352 pctcpu_next = (pctcpu * ccpu_exp[1]) >> FSHIFT;
353 pctcpu_next += sched_pctcpu_delta(td);
354 p_pctcpu += max(pctcpu, pctcpu_next);
368 if (swtime <= CCPU_EXP_MAX)
369 return ((100 * (uint64_t)p_pctcpu * 1000000) /
370 (FSCALE - ccpu_exp[swtime]));
373 return ((100 * (uint64_t)p_pctcpu * 1000000) / FSCALE);
377 racct_add_racct(
struct racct *dest,
const struct racct *src)
381 mtx_assert(&racct_lock, MA_OWNED);
386 for (i = 0; i <= RACCT_MAX; i++) {
387 KASSERT(dest->r_resources[i] >= 0,
388 (
"%s: resource %d propagation meltdown: dest < 0",
390 KASSERT(src->r_resources[i] >= 0,
391 (
"%s: resource %d propagation meltdown: src < 0",
393 dest->r_resources[i] += src->r_resources[i];
398 racct_sub_racct(
struct racct *dest,
const struct racct *src)
402 mtx_assert(&racct_lock, MA_OWNED);
407 for (i = 0; i <= RACCT_MAX; i++) {
408 if (!RACCT_IS_SLOPPY(i) && !RACCT_IS_DECAYING(i)) {
409 KASSERT(dest->r_resources[i] >= 0,
410 (
"%s: resource %d propagation meltdown: dest < 0",
412 KASSERT(src->r_resources[i] >= 0,
413 (
"%s: resource %d propagation meltdown: src < 0",
415 KASSERT(src->r_resources[i] <= dest->r_resources[i],
416 (
"%s: resource %d propagation meltdown: src > dest",
419 if (RACCT_CAN_DROP(i)) {
420 dest->r_resources[i] -= src->r_resources[i];
421 if (dest->r_resources[i] < 0) {
422 KASSERT(RACCT_IS_SLOPPY(i) ||
423 RACCT_IS_DECAYING(i),
424 (
"%s: resource %d usage < 0", __func__, i));
425 dest->r_resources[i] = 0;
435 SDT_PROBE1(racct, kernel, racct, create, racctp);
437 KASSERT(*racctp == NULL, (
"racct already allocated"));
439 *racctp = uma_zalloc(racct_zone, M_WAITOK | M_ZERO);
443 racct_destroy_locked(
struct racct **racctp)
448 SDT_PROBE1(racct, kernel, racct,
destroy, racctp);
450 mtx_assert(&racct_lock, MA_OWNED);
451 KASSERT(racctp != NULL, (
"NULL racctp"));
452 KASSERT(*racctp != NULL, (
"NULL racct"));
456 for (i = 0; i <= RACCT_MAX; i++) {
457 if (RACCT_IS_SLOPPY(i))
459 if (!RACCT_IS_RECLAIMABLE(i))
461 KASSERT(racct->r_resources[i] == 0,
462 (
"destroying non-empty racct: "
463 "%ju allocated for resource %d\n",
464 racct->r_resources[i], i));
466 uma_zfree(racct_zone, racct);
474 mtx_lock(&racct_lock);
475 racct_destroy_locked(racct);
476 mtx_unlock(&racct_lock);
485 racct_alloc_resource(
struct racct *racct,
int resource,
489 mtx_assert(&racct_lock, MA_OWNED);
490 KASSERT(racct != NULL, (
"NULL racct"));
492 racct->r_resources[resource] += amount;
493 if (racct->r_resources[resource] < 0) {
494 KASSERT(RACCT_IS_SLOPPY(resource) || RACCT_IS_DECAYING(resource),
495 (
"%s: resource %d usage < 0", __func__, resource));
496 racct->r_resources[resource] = 0;
508 if ((resource == RACCT_PCTCPU) &&
509 (racct->r_resources[RACCT_PCTCPU] > 100 * 1000000))
510 racct->r_resources[RACCT_PCTCPU] = 100 * 1000000;
514 racct_add_locked(
struct proc *p,
int resource, uint64_t amount)
520 SDT_PROBE3(racct, kernel, rusage, add, p, resource, amount);
525 PROC_LOCK_ASSERT(p, MA_OWNED);
528 error = rctl_enforce(p, resource, amount);
529 if (error && RACCT_IS_DENIABLE(resource)) {
530 SDT_PROBE3(racct, kernel, rusage, add__failure, p, resource,
535 racct_alloc_resource(p->p_racct, resource, amount);
536 racct_add_cred_locked(p->p_ucred, resource, amount);
546 racct_add(
struct proc *p,
int resource, uint64_t amount)
550 mtx_lock(&racct_lock);
551 error = racct_add_locked(p, resource, amount);
552 mtx_unlock(&racct_lock);
557 racct_add_cred_locked(
struct ucred *cred,
int resource, uint64_t amount)
561 SDT_PROBE3(racct, kernel, rusage, add__cred, cred, resource, amount);
563 racct_alloc_resource(cred->cr_ruidinfo->ui_racct, resource, amount);
564 for (pr = cred->cr_prison; pr != NULL; pr = pr->pr_parent)
565 racct_alloc_resource(pr->pr_prison_racct->prr_racct, resource,
567 racct_alloc_resource(cred->cr_loginclass->lc_racct, resource, amount);
580 mtx_lock(&racct_lock);
581 racct_add_cred_locked(cred, resource, amount);
582 mtx_unlock(&racct_lock);
593 SDT_PROBE3(racct, kernel, rusage, add__force, p, resource, amount);
598 PROC_LOCK_ASSERT(p, MA_OWNED);
600 mtx_lock(&racct_lock);
601 racct_alloc_resource(p->p_racct, resource, amount);
602 mtx_unlock(&racct_lock);
607 racct_set_locked(
struct proc *p,
int resource, uint64_t amount)
609 int64_t old_amount, decayed_amount;
610 int64_t diff_proc, diff_cred;
615 SDT_PROBE3(racct, kernel, rusage,
set, p, resource, amount);
620 PROC_LOCK_ASSERT(p, MA_OWNED);
622 old_amount = p->p_racct->r_resources[resource];
626 diff_proc = amount - old_amount;
627 if (RACCT_IS_DECAYING(resource)) {
634 decayed_amount = old_amount * RACCT_DECAY_FACTOR / FSCALE;
635 diff_cred = amount - decayed_amount;
637 diff_cred = diff_proc;
639 KASSERT(diff_proc >= 0 || RACCT_CAN_DROP(resource),
640 (
"%s: usage of non-droppable resource %d dropping", __func__,
645 error = rctl_enforce(p, resource, diff_proc);
646 if (error && RACCT_IS_DENIABLE(resource)) {
647 SDT_PROBE3(racct, kernel, rusage, set__failure, p,
653 racct_alloc_resource(p->p_racct, resource, diff_proc);
655 racct_add_cred_locked(p->p_ucred, resource, diff_cred);
656 else if (diff_cred < 0)
657 racct_sub_cred_locked(p->p_ucred, resource, -diff_cred);
670 racct_set(
struct proc *p,
int resource, uint64_t amount)
674 mtx_lock(&racct_lock);
675 error = racct_set_locked(p, resource, amount);
676 mtx_unlock(&racct_lock);
681 racct_set_force_locked(
struct proc *p,
int resource, uint64_t amount)
683 int64_t old_amount, decayed_amount;
684 int64_t diff_proc, diff_cred;
686 SDT_PROBE3(racct, kernel, rusage,
set, p, resource, amount);
691 PROC_LOCK_ASSERT(p, MA_OWNED);
693 old_amount = p->p_racct->r_resources[resource];
697 diff_proc = amount - old_amount;
698 if (RACCT_IS_DECAYING(resource)) {
705 decayed_amount = old_amount * RACCT_DECAY_FACTOR / FSCALE;
706 diff_cred = amount - decayed_amount;
708 diff_cred = diff_proc;
710 racct_alloc_resource(p->p_racct, resource, diff_proc);
712 racct_add_cred_locked(p->p_ucred, resource, diff_cred);
713 else if (diff_cred < 0)
714 racct_sub_cred_locked(p->p_ucred, resource, -diff_cred);
720 mtx_lock(&racct_lock);
721 racct_set_force_locked(p, resource, amount);
722 mtx_unlock(&racct_lock);
736 return (rctl_get_limit(p, resource));
753 return (rctl_get_available(p, resource));
765 racct_pcpu_available(
struct proc *p)
769 return (rctl_pcpu_available(p));
779 racct_sub(
struct proc *p,
int resource, uint64_t amount)
782 SDT_PROBE3(racct, kernel, rusage, sub, p, resource, amount);
787 PROC_LOCK_ASSERT(p, MA_OWNED);
788 KASSERT(RACCT_CAN_DROP(resource),
789 (
"%s: called for non-droppable resource %d", __func__, resource));
791 mtx_lock(&racct_lock);
792 KASSERT(amount <= p->p_racct->r_resources[resource],
793 (
"%s: freeing %ju of resource %d, which is more "
794 "than allocated %jd for %s (pid %d)", __func__, amount, resource,
795 (intmax_t)p->p_racct->r_resources[resource], p->p_comm, p->p_pid));
797 racct_alloc_resource(p->p_racct, resource, -amount);
798 racct_sub_cred_locked(p->p_ucred, resource, amount);
799 mtx_unlock(&racct_lock);
803 racct_sub_cred_locked(
struct ucred *cred,
int resource, uint64_t amount)
807 SDT_PROBE3(racct, kernel, rusage, sub__cred, cred, resource, amount);
810 KASSERT(RACCT_CAN_DROP(resource),
811 (
"%s: called for resource %d which can not drop", __func__,
815 racct_alloc_resource(cred->cr_ruidinfo->ui_racct, resource, -amount);
816 for (pr = cred->cr_prison; pr != NULL; pr = pr->pr_parent)
817 racct_alloc_resource(pr->pr_prison_racct->prr_racct, resource,
819 racct_alloc_resource(cred->cr_loginclass->lc_racct, resource, -amount);
829 mtx_lock(&racct_lock);
830 racct_sub_cred_locked(cred, resource, amount);
831 mtx_unlock(&racct_lock);
849 mtx_lock(&racct_lock);
852 error = rctl_proc_fork(parent, child);
858 child->p_prev_runtime = 0;
859 child->p_throttled = 0;
864 for (i = 0; i <= RACCT_MAX; i++) {
865 if (parent->p_racct->r_resources[i] == 0 ||
866 !RACCT_IS_INHERITABLE(i))
869 error = racct_set_locked(child, i,
870 parent->p_racct->r_resources[i]);
875 error = racct_add_locked(child, RACCT_NPROC, 1);
876 error += racct_add_locked(child, RACCT_NTHR, 1);
879 mtx_unlock(&racct_lock);
899 mtx_lock(&racct_lock);
900 rctl_enforce(child, RACCT_NPROC, 0);
901 rctl_enforce(child, RACCT_NTHR, 0);
902 mtx_unlock(&racct_lock);
912 struct timeval wallclock;
913 uint64_t pct_estimate, pct;
921 KASSERT(runtime >= p->p_prev_runtime, (
"runtime < p_prev_runtime"));
923 if (runtime < p->p_prev_runtime)
924 runtime = p->p_prev_runtime;
928 if (wallclock.tv_sec > 0 || wallclock.tv_usec > 0) {
929 pct_estimate = (1000000 * runtime * 100) /
930 ((uint64_t)wallclock.tv_sec * 1000000 +
934 pct = racct_getpcpu(p, pct_estimate);
936 mtx_lock(&racct_lock);
937 racct_set_locked(p, RACCT_CPU, runtime);
938 racct_add_cred_locked(p->p_ucred, RACCT_PCTCPU, pct);
940 for (i = 0; i <= RACCT_MAX; i++) {
941 if (p->p_racct->r_resources[i] == 0)
943 if (!RACCT_IS_RECLAIMABLE(i))
945 racct_set_locked(p, i, 0);
948 mtx_unlock(&racct_lock);
952 rctl_racct_release(p->p_racct);
962 racct_proc_ucred_changed(
struct proc *p,
struct ucred *oldcred,
963 struct ucred *newcred)
965 struct uidinfo *olduip, *newuip;
966 struct loginclass *oldlc, *newlc;
967 struct prison *oldpr, *newpr, *
pr;
969 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
971 newuip = newcred->cr_ruidinfo;
972 olduip = oldcred->cr_ruidinfo;
973 newlc = newcred->cr_loginclass;
974 oldlc = oldcred->cr_loginclass;
975 newpr = newcred->cr_prison;
976 oldpr = oldcred->cr_prison;
978 mtx_lock(&racct_lock);
979 if (newuip != olduip) {
980 racct_sub_racct(olduip->ui_racct, p->p_racct);
981 racct_add_racct(newuip->ui_racct, p->p_racct);
983 if (newlc != oldlc) {
984 racct_sub_racct(oldlc->lc_racct, p->p_racct);
985 racct_add_racct(newlc->lc_racct, p->p_racct);
987 if (newpr != oldpr) {
988 for (pr = oldpr; pr != NULL; pr = pr->pr_parent)
989 racct_sub_racct(pr->pr_prison_racct->prr_racct,
991 for (pr = newpr; pr != NULL; pr = pr->pr_parent)
992 racct_add_racct(pr->pr_prison_racct->prr_racct,
995 mtx_unlock(&racct_lock);
998 rctl_proc_ucred_changed(p, newcred);
1003 racct_move(
struct racct *dest,
struct racct *src)
1006 mtx_lock(&racct_lock);
1008 racct_add_racct(dest, src);
1009 racct_sub_racct(src, src);
1011 mtx_unlock(&racct_lock);
1015 racct_proc_throttle(
struct proc *p)
1022 PROC_LOCK_ASSERT(p, MA_OWNED);
1028 if (((p->p_flag & (P_SYSTEM | P_KTHREAD)) != 0) ||
1029 (p->p_racct->r_resources[RACCT_PCTCPU] <= pcpu_threshold))
1033 FOREACH_THREAD_IN_PROC(p, td) {
1035 switch (td->td_state) {
1043 td->td_flags |= TDF_NEEDRESCHED;
1050 td->td_flags |= TDF_NEEDRESCHED;
1052 cpuid = td->td_oncpu;
1053 if ((cpuid != NOCPU) && (td != curthread))
1054 ipi_cpu(cpuid, IPI_AST);
1065 racct_proc_wakeup(
struct proc *p)
1067 PROC_LOCK_ASSERT(p, MA_OWNED);
1069 if (p->p_throttled) {
1076 racct_decay_resource(
struct racct *racct,
void * res,
void*
dummy)
1079 int64_t r_old, r_new;
1081 resource = *(
int *)res;
1082 r_old = racct->r_resources[resource];
1088 mtx_lock(&racct_lock);
1089 r_new = r_old * RACCT_DECAY_FACTOR / FSCALE;
1090 racct->r_resources[resource] = r_new;
1091 mtx_unlock(&racct_lock);
1095 racct_decay(
int resource)
1107 struct timeval wallclock;
1109 uint64_t pct, pct_estimate;
1112 racct_decay(RACCT_PCTCPU);
1116 LIST_FOREACH(p, &
zombproc, p_list) {
1122 FOREACH_PROC_IN_SYSTEM(p) {
1124 if (p->p_state != PRS_NORMAL) {
1130 timevalsub(&wallclock, &p->p_stats->p_start);
1132 FOREACH_THREAD_IN_PROC(p, td)
1137 KASSERT(runtime >= p->p_prev_runtime,
1138 (
"runtime < p_prev_runtime"));
1140 if (runtime < p->p_prev_runtime)
1141 runtime = p->p_prev_runtime;
1143 p->p_prev_runtime = runtime;
1144 if (wallclock.tv_sec > 0 || wallclock.tv_usec > 0) {
1145 pct_estimate = (1000000 * runtime * 100) /
1146 ((uint64_t)wallclock.tv_sec * 1000000 +
1150 pct = racct_getpcpu(p, pct_estimate);
1151 mtx_lock(&racct_lock);
1152 racct_set_force_locked(p, RACCT_PCTCPU, pct);
1153 racct_set_locked(p, RACCT_CPU, runtime);
1154 racct_set_locked(p, RACCT_WALLCLOCK,
1155 (uint64_t)wallclock.tv_sec * 1000000 +
1157 mtx_unlock(&racct_lock);
1167 FOREACH_PROC_IN_SYSTEM(p) {
1169 if (p->p_state != PRS_NORMAL) {
1174 if (racct_pcpu_available(p) <= 0)
1175 racct_proc_throttle(p);
1176 else if (p->p_throttled)
1177 racct_proc_wakeup(p);
1185 static struct kproc_desc racctd_kp = {
1196 racct_zone = uma_zcreate(
"racct",
sizeof(
struct racct),
1197 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
1203 SYSINIT(racct, SI_SUB_RACCT, SI_ORDER_FIRST, racct_init, NULL);
1252 return (UINT64_MAX);
1259 return (UINT64_MAX);
SDT_PROBE_DEFINE2(sched,,, tick,"struct thread *","struct proc *")
SDT_PROBE_DEFINE1(proc, kernel,, exec,"char *")
int racct_add(struct proc *p, int resource, uint64_t amount)
MTX_SYSINIT(et_eventtimers_init,&et_eventtimers_mtx,"et_mtx", MTX_DEF)
void racct_sub_cred(struct ucred *cred, int resource, uint64_t amount)
static SYSCTL_NODE(_debug, OID_AUTO, cpufreq, CTLFLAG_RD, NULL,"cpufreq debugging")
int racct_set(struct proc *p, int resource, uint64_t amount)
void racct_set_force(struct proc *p, int resource, uint64_t amount)
SYSINIT(placeholder, SI_SUB_DUMMY, SI_ORDER_ANY, NULL, NULL)
local int destroy(gz_stream *s)
int racct_proc_fork(struct proc *parent, struct proc *child)
SDT_PROVIDER_DEFINE(priv)
SYSCTL_UINT(_kern_eventtimer, OID_AUTO, idletick, CTLFLAG_RW,&idletick, 0,"Run periodic events when idle")
void racct_add_cred(struct ucred *cred, int resource, uint64_t amount)
void timevalsub(struct timeval *t1, const struct timeval *t2)
void racct_sub(struct proc *p, int resource, uint64_t amount)
void racct_add_force(struct proc *p, int resource, uint64_t amount)
void racct_proc_fork_done(struct proc *child)
uint64_t cputick2usec(uint64_t tick)
uint64_t racct_get_available(struct proc *p, int resource)
void ruxagg(struct proc *p, struct thread *td)
void racct_destroy(struct racct **racctp)
int pause(const char *wmesg, int timo)
void prison_racct_foreach(void(*callback)(struct racct *racct, void *arg2, void *arg3), void *arg2, void *arg3)
SDT_PROBE_DEFINE3(proc, kernel,, create,"struct proc *","struct proc *","int")
void racct_proc_exit(struct proc *p)
void kproc_start(void *udata) const
void microuptime(struct timeval *tvp)
void loginclass_racct_foreach(void(*callback)(struct racct *racct, void *arg2, void *arg3), void *arg2, void *arg3)
FEATURE(kdtrace_hooks,"Kernel DTrace hooks which are required to load DTrace kernel modules")
void ui_racct_foreach(void(*callback)(struct racct *racct, void *arg2, void *arg3), void *arg2, void *arg3)
struct prison_racct * prison_racct_find(const char *name)
void racct_create(struct racct **racctp)
uint64_t racct_get_limit(struct proc *p, int resource)
static struct pollrec pr[POLL_LIST_LEN]
fixpt_t sched_pctcpu(struct thread *td)