41#include <sys/eventhandler.h>
43#include <sys/kernel.h>
44#include <sys/kthread.h>
46#include <sys/loginclass.h>
47#include <sys/malloc.h>
51#include <sys/resourcevar.h>
57#include <sys/sysctl.h>
58#include <sys/sysent.h>
59#include <sys/sysproto.h>
60#include <sys/umtxvar.h>
61#include <machine/smp.h>
69FEATURE(racct,
"Resource Accounting");
74static int pcpu_threshold = 1;
75#ifdef RACCT_DEFAULT_TO_DISABLED
76bool __read_frequently racct_enable =
false;
78bool __read_frequently racct_enable =
true;
81SYSCTL_NODE(_kern, OID_AUTO, racct, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
82 "Resource Accounting");
83SYSCTL_BOOL(_kern_racct, OID_AUTO, enable, CTLFLAG_RDTUN, &racct_enable,
84 0,
"Enable RACCT/RCTL");
85SYSCTL_UINT(_kern_racct, OID_AUTO, pcpu_threshold, CTLFLAG_RW, &pcpu_threshold,
86 0,
"Processes with higher %cpu usage than this value can be throttled.");
94#define RACCT_PCPU_SECS 3
97MTX_SYSINIT(racct_lock, &racct_lock,
"racct lock", MTX_DEF);
99static uma_zone_t racct_zone;
101static void racct_sub_racct(
struct racct *dest,
const struct racct *
src);
102static void racct_sub_cred_locked(
struct ucred *cred,
int resource,
104static void racct_add_cred_locked(
struct ucred *cred,
int resource,
109 "struct proc *",
"int",
"uint64_t");
111 "struct proc *",
"int",
"uint64_t");
113 "struct proc *",
"const struct buf *",
"int");
115 "struct ucred *",
"int",
"uint64_t");
117 "struct proc *",
"int",
"uint64_t");
119 "struct proc *",
"int",
"uint64_t");
121 "struct proc *",
"int",
"uint64_t");
123 "struct proc *",
"int",
"uint64_t");
125 "struct proc *",
"int",
"uint64_t");
127 "struct ucred *",
"int",
"uint64_t");
133 "struct racct *",
"struct racct *");
135 "struct racct *",
"struct racct *");
137 "struct racct *",
"struct racct *");
143 RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
145 RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
151 RACCT_RECLAIMABLE | RACCT_DENIABLE,
153 RACCT_RECLAIMABLE | RACCT_DENIABLE,
155 RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
157 RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
159 RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
161 RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
163 RACCT_RECLAIMABLE | RACCT_DENIABLE,
165 RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
167 RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
169 RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
171 RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
173 RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
175 RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
177 RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
181 RACCT_DECAYING | RACCT_DENIABLE | RACCT_IN_MILLIONS,
191static const fixpt_t RACCT_DECAY_FACTOR = 0.3 * FSCALE;
201fixpt_t ccpu_exp[] = {
203 [1] = FSCALE * 0.95122942450071400909,
204 [2] = FSCALE * 0.90483741803595957316,
205 [3] = FSCALE * 0.86070797642505780722,
206 [4] = FSCALE * 0.81873075307798185866,
207 [5] = FSCALE * 0.77880078307140486824,
208 [6] = FSCALE * 0.74081822068171786606,
209 [7] = FSCALE * 0.70468808971871343435,
210 [8] = FSCALE * 0.67032004603563930074,
211 [9] = FSCALE * 0.63762815162177329314,
212 [10] = FSCALE * 0.60653065971263342360,
213 [11] = FSCALE * 0.57694981038048669531,
214 [12] = FSCALE * 0.54881163609402643262,
215 [13] = FSCALE * 0.52204577676101604789,
216 [14] = FSCALE * 0.49658530379140951470,
217 [15] = FSCALE * 0.47236655274101470713,
218 [16] = FSCALE * 0.44932896411722159143,
219 [17] = FSCALE * 0.42741493194872666992,
220 [18] = FSCALE * 0.40656965974059911188,
221 [19] = FSCALE * 0.38674102345450120691,
222 [20] = FSCALE * 0.36787944117144232159,
223 [21] = FSCALE * 0.34993774911115535467,
224 [22] = FSCALE * 0.33287108369807955328,
225 [23] = FSCALE * 0.31663676937905321821,
226 [24] = FSCALE * 0.30119421191220209664,
227 [25] = FSCALE * 0.28650479686019010032,
228 [26] = FSCALE * 0.27253179303401260312,
229 [27] = FSCALE * 0.25924026064589150757,
230 [28] = FSCALE * 0.24659696394160647693,
231 [29] = FSCALE * 0.23457028809379765313,
232 [30] = FSCALE * 0.22313016014842982893,
233 [31] = FSCALE * 0.21224797382674305771,
234 [32] = FSCALE * 0.20189651799465540848,
235 [33] = FSCALE * 0.19204990862075411423,
236 [34] = FSCALE * 0.18268352405273465022,
237 [35] = FSCALE * 0.17377394345044512668,
238 [36] = FSCALE * 0.16529888822158653829,
239 [37] = FSCALE * 0.15723716631362761621,
240 [38] = FSCALE * 0.14956861922263505264,
241 [39] = FSCALE * 0.14227407158651357185,
242 [40] = FSCALE * 0.13533528323661269189,
243 [41] = FSCALE * 0.12873490358780421886,
244 [42] = FSCALE * 0.12245642825298191021,
245 [43] = FSCALE * 0.11648415777349695786,
246 [44] = FSCALE * 0.11080315836233388333,
247 [45] = FSCALE * 0.10539922456186433678,
248 [46] = FSCALE * 0.10025884372280373372,
249 [47] = FSCALE * 0.09536916221554961888,
250 [48] = FSCALE * 0.09071795328941250337,
251 [49] = FSCALE * 0.08629358649937051097,
252 [50] = FSCALE * 0.08208499862389879516,
253 [51] = FSCALE * 0.07808166600115315231,
254 [52] = FSCALE * 0.07427357821433388042,
255 [53] = FSCALE * 0.07065121306042958674,
256 [54] = FSCALE * 0.06720551273974976512,
257 [55] = FSCALE * 0.06392786120670757270,
258 [56] = FSCALE * 0.06081006262521796499,
259 [57] = FSCALE * 0.05784432087483846296,
260 [58] = FSCALE * 0.05502322005640722902,
261 [59] = FSCALE * 0.05233970594843239308,
262 [60] = FSCALE * 0.04978706836786394297,
263 [61] = FSCALE * 0.04735892439114092119,
264 [62] = FSCALE * 0.04504920239355780606,
265 [63] = FSCALE * 0.04285212686704017991,
266 [64] = FSCALE * 0.04076220397836621516,
267 [65] = FSCALE * 0.03877420783172200988,
268 [66] = FSCALE * 0.03688316740124000544,
269 [67] = FSCALE * 0.03508435410084502588,
270 [68] = FSCALE * 0.03337326996032607948,
271 [69] = FSCALE * 0.03174563637806794323,
272 [70] = FSCALE * 0.03019738342231850073,
273 [71] = FSCALE * 0.02872463965423942912,
274 [72] = FSCALE * 0.02732372244729256080,
275 [73] = FSCALE * 0.02599112877875534358,
276 [74] = FSCALE * 0.02472352647033939120,
277 [75] = FSCALE * 0.02351774585600910823,
278 [76] = FSCALE * 0.02237077185616559577,
279 [77] = FSCALE * 0.02127973643837716938,
280 [78] = FSCALE * 0.02024191144580438847,
281 [79] = FSCALE * 0.01925470177538692429,
282 [80] = FSCALE * 0.01831563888873418029,
283 [81] = FSCALE * 0.01742237463949351138,
284 [82] = FSCALE * 0.01657267540176124754,
285 [83] = FSCALE * 0.01576441648485449082,
286 [84] = FSCALE * 0.01499557682047770621,
287 [85] = FSCALE * 0.01426423390899925527,
288 [86] = FSCALE * 0.01356855901220093175,
289 [87] = FSCALE * 0.01290681258047986886,
290 [88] = FSCALE * 0.01227733990306844117,
291 [89] = FSCALE * 0.01167856697039544521,
292 [90] = FSCALE * 0.01110899653824230649,
293 [91] = FSCALE * 0.01056720438385265337,
294 [92] = FSCALE * 0.01005183574463358164,
295 [93] = FSCALE * 0.00956160193054350793,
296 [94] = FSCALE * 0.00909527710169581709,
297 [95] = FSCALE * 0.00865169520312063417,
298 [96] = FSCALE * 0.00822974704902002884,
299 [97] = FSCALE * 0.00782837754922577143,
300 [98] = FSCALE * 0.00744658307092434051,
301 [99] = FSCALE * 0.00708340892905212004,
302 [100] = FSCALE * 0.00673794699908546709,
303 [101] = FSCALE * 0.00640933344625638184,
304 [102] = FSCALE * 0.00609674656551563610,
305 [103] = FSCALE * 0.00579940472684214321,
306 [104] = FSCALE * 0.00551656442076077241,
307 [105] = FSCALE * 0.00524751839918138427,
308 [106] = FSCALE * 0.00499159390691021621,
309 [107] = FSCALE * 0.00474815099941147558,
310 [108] = FSCALE * 0.00451658094261266798,
311 [109] = FSCALE * 0.00429630469075234057,
312 [110] = FSCALE * 0.00408677143846406699,
316#define CCPU_EXP_MAX 110
327racct_getpcpu(
struct proc *p, u_int pcpu)
331 fixpt_t pctcpu, pctcpu_next;
340 ASSERT_RACCT_ENABLED();
346 if ((p->p_flag & P_INMEM) == 0)
348 swtime = (
ticks - p->p_swtick) /
hz;
355 if (swtime < RACCT_PCPU_SECS)
359 FOREACH_THREAD_IN_PROC(p, td) {
360 if (td == PCPU_GET(idlethread))
364 STAILQ_FOREACH(pc, &cpuhead, pc_allcpu) {
365 if (td == pc->pc_idlethread) {
377 pctcpu_next = (pctcpu * ccpu_exp[1]) >> FSHIFT;
378 pctcpu_next += sched_pctcpu_delta(td);
379 p_pctcpu += max(pctcpu, pctcpu_next);
393 if (swtime <= CCPU_EXP_MAX)
394 return ((100 * (uint64_t)p_pctcpu * 1000000) /
395 (FSCALE - ccpu_exp[swtime]));
398 return ((100 * (uint64_t)p_pctcpu * 1000000) / FSCALE);
402racct_add_racct(
struct racct *dest,
const struct racct *
src)
406 ASSERT_RACCT_ENABLED();
412 for (i = 0; i <= RACCT_MAX; i++) {
413 KASSERT(dest->r_resources[i] >= 0,
414 (
"%s: resource %d propagation meltdown: dest < 0",
416 KASSERT(
src->r_resources[i] >= 0,
417 (
"%s: resource %d propagation meltdown: src < 0",
419 dest->r_resources[i] +=
src->r_resources[i];
424racct_sub_racct(
struct racct *dest,
const struct racct *
src)
428 ASSERT_RACCT_ENABLED();
434 for (i = 0; i <= RACCT_MAX; i++) {
435 if (!RACCT_IS_SLOPPY(i) && !RACCT_IS_DECAYING(i)) {
436 KASSERT(dest->r_resources[i] >= 0,
437 (
"%s: resource %d propagation meltdown: dest < 0",
439 KASSERT(
src->r_resources[i] >= 0,
440 (
"%s: resource %d propagation meltdown: src < 0",
442 KASSERT(
src->r_resources[i] <= dest->r_resources[i],
443 (
"%s: resource %d propagation meltdown: src > dest",
446 if (RACCT_CAN_DROP(i)) {
447 dest->r_resources[i] -=
src->r_resources[i];
448 if (dest->r_resources[i] < 0)
449 dest->r_resources[i] = 0;
455racct_create(
struct racct **racctp)
461 SDT_PROBE1(racct, , racct, create, racctp);
463 KASSERT(*racctp == NULL, (
"racct already allocated"));
465 *racctp = uma_zalloc(racct_zone, M_WAITOK | M_ZERO);
469racct_destroy_locked(
struct racct **racctp)
474 ASSERT_RACCT_ENABLED();
476 SDT_PROBE1(racct, , racct, destroy, racctp);
479 KASSERT(racctp != NULL, (
"NULL racctp"));
480 KASSERT(*racctp != NULL, (
"NULL racct"));
484 for (i = 0; i <= RACCT_MAX; i++) {
485 if (RACCT_IS_SLOPPY(i))
487 if (!RACCT_IS_RECLAIMABLE(i))
489 KASSERT(racct->r_resources[i] == 0,
490 (
"destroying non-empty racct: "
491 "%ju allocated for resource %d\n",
492 racct->r_resources[i], i));
494 uma_zfree(racct_zone, racct);
499racct_destroy(
struct racct **racct)
506 racct_destroy_locked(racct);
516racct_adjust_resource(
struct racct *racct,
int resource,
520 ASSERT_RACCT_ENABLED();
522 KASSERT(racct != NULL, (
"NULL racct"));
524 racct->r_resources[resource] += amount;
525 if (racct->r_resources[resource] < 0) {
526 KASSERT(RACCT_IS_SLOPPY(resource) || RACCT_IS_DECAYING(resource),
527 (
"%s: resource %d usage < 0", __func__, resource));
528 racct->r_resources[resource] = 0;
540 if ((resource == RACCT_PCTCPU) &&
541 (racct->r_resources[RACCT_PCTCPU] > 100 * 1000000 * (int64_t)MAXCPU))
542 racct->r_resources[RACCT_PCTCPU] = 100 * 1000000 * (int64_t)MAXCPU;
546racct_add_locked(
struct proc *p,
int resource, uint64_t amount,
int force)
552 ASSERT_RACCT_ENABLED();
557 PROC_LOCK_ASSERT(p, MA_OWNED);
560 error = rctl_enforce(p, resource, amount);
561 if (error && !force && RACCT_IS_DENIABLE(resource)) {
562 SDT_PROBE3(racct, , rusage, add__failure, p, resource, amount);
566 racct_adjust_resource(p->p_racct, resource, amount);
567 racct_add_cred_locked(p->p_ucred, resource, amount);
577racct_add(
struct proc *p,
int resource, uint64_t amount)
584 SDT_PROBE3(racct, , rusage, add, p, resource, amount);
587 error = racct_add_locked(p, resource, amount, 0);
597racct_add_force(
struct proc *p,
int resource, uint64_t amount)
603 SDT_PROBE3(racct, , rusage, add__force, p, resource, amount);
606 racct_add_locked(p, resource, amount, 1);
611racct_add_cred_locked(
struct ucred *cred,
int resource, uint64_t amount)
615 ASSERT_RACCT_ENABLED();
617 racct_adjust_resource(cred->cr_ruidinfo->ui_racct, resource, amount);
618 for (
pr = cred->cr_prison;
pr != NULL;
pr =
pr->pr_parent)
619 racct_adjust_resource(
pr->pr_prison_racct->prr_racct, resource,
621 racct_adjust_resource(cred->cr_loginclass->lc_racct, resource, amount);
629racct_add_cred(
struct ucred *cred,
int resource, uint64_t amount)
635 SDT_PROBE3(racct, , rusage, add__cred, cred, resource, amount);
638 racct_add_cred_locked(cred, resource, amount);
647racct_add_buf(
struct proc *p,
const struct buf *bp,
int is_write)
650 ASSERT_RACCT_ENABLED();
651 PROC_LOCK_ASSERT(p, MA_OWNED);
653 SDT_PROBE3(racct, , rusage, add__buf, p, bp, is_write);
657 racct_add_locked(curproc, RACCT_WRITEBPS, bp->b_bcount, 1);
658 racct_add_locked(curproc, RACCT_WRITEIOPS, 1, 1);
660 racct_add_locked(curproc, RACCT_READBPS, bp->b_bcount, 1);
661 racct_add_locked(curproc, RACCT_READIOPS, 1, 1);
667racct_set_locked(
struct proc *p,
int resource, uint64_t amount,
int force)
669 int64_t old_amount, decayed_amount, diff_proc, diff_cred;
674 ASSERT_RACCT_ENABLED();
679 PROC_LOCK_ASSERT(p, MA_OWNED);
681 old_amount = p->p_racct->r_resources[resource];
685 diff_proc = amount - old_amount;
686 if (resource == RACCT_PCTCPU) {
693 decayed_amount = old_amount * RACCT_DECAY_FACTOR / FSCALE;
694 diff_cred = amount - decayed_amount;
696 diff_cred = diff_proc;
698 KASSERT(diff_proc >= 0 || RACCT_CAN_DROP(resource),
699 (
"%s: usage of non-droppable resource %d dropping", __func__,
704 error = rctl_enforce(p, resource, diff_proc);
705 if (error && !force && RACCT_IS_DENIABLE(resource)) {
706 SDT_PROBE3(racct, , rusage, set__failure, p, resource,
712 racct_adjust_resource(p->p_racct, resource, diff_proc);
714 racct_add_cred_locked(p->p_ucred, resource, diff_cred);
715 else if (diff_cred < 0)
716 racct_sub_cred_locked(p->p_ucred, resource, -diff_cred);
729racct_set_unlocked(
struct proc *p,
int resource, uint64_t amount)
733 ASSERT_RACCT_ENABLED();
735 error = racct_set(p, resource, amount);
741racct_set(
struct proc *p,
int resource, uint64_t amount)
748 SDT_PROBE3(racct, , rusage, set__force, p, resource, amount);
751 error = racct_set_locked(p, resource, amount, 0);
757racct_set_force(
struct proc *p,
int resource, uint64_t amount)
763 SDT_PROBE3(racct, , rusage,
set, p, resource, amount);
766 racct_set_locked(p, resource, amount, 1);
777racct_get_limit(
struct proc *p,
int resource)
786 available = rctl_get_limit(p, resource);
803racct_get_available(
struct proc *p,
int resource)
812 available = rctl_get_available(p, resource);
828racct_pcpu_available(
struct proc *p)
833 ASSERT_RACCT_ENABLED();
836 available = rctl_pcpu_available(p);
850racct_sub(
struct proc *p,
int resource, uint64_t amount)
856 SDT_PROBE3(racct, , rusage, sub, p, resource, amount);
861 PROC_LOCK_ASSERT(p, MA_OWNED);
862 KASSERT(RACCT_CAN_DROP(resource),
863 (
"%s: called for non-droppable resource %d", __func__, resource));
866 KASSERT(amount <= p->p_racct->r_resources[resource],
867 (
"%s: freeing %ju of resource %d, which is more "
868 "than allocated %jd for %s (pid %d)", __func__, amount, resource,
869 (intmax_t)p->p_racct->r_resources[resource], p->p_comm, p->p_pid));
871 racct_adjust_resource(p->p_racct, resource, -amount);
872 racct_sub_cred_locked(p->p_ucred, resource, amount);
877racct_sub_cred_locked(
struct ucred *cred,
int resource, uint64_t amount)
881 ASSERT_RACCT_ENABLED();
883 racct_adjust_resource(cred->cr_ruidinfo->ui_racct, resource, -amount);
884 for (
pr = cred->cr_prison;
pr != NULL;
pr =
pr->pr_parent)
885 racct_adjust_resource(
pr->pr_prison_racct->prr_racct, resource,
887 racct_adjust_resource(cred->cr_loginclass->lc_racct, resource, -amount);
894racct_sub_cred(
struct ucred *cred,
int resource, uint64_t amount)
900 SDT_PROBE3(racct, , rusage, sub__cred, cred, resource, amount);
903 KASSERT(RACCT_CAN_DROP(resource),
904 (
"%s: called for resource %d which can not drop", __func__,
909 racct_sub_cred_locked(cred, resource, amount);
917racct_proc_fork(
struct proc *
parent,
struct proc *
child)
927 racct_create(&
child->p_racct);
940 child->p_prev_runtime = 0;
941 child->p_throttled = 0;
946 for (i = 0; i <= RACCT_MAX; i++) {
947 if (
parent->p_racct->r_resources[i] == 0 ||
948 !RACCT_IS_INHERITABLE(i))
951 error = racct_set_locked(
child, i,
952 parent->p_racct->r_resources[i], 0);
957 error = racct_add_locked(
child, RACCT_NPROC, 1, 0);
958 error += racct_add_locked(
child, RACCT_NTHR, 1, 0);
966 racct_proc_exit(
child);
976racct_proc_fork_done(
struct proc *
child)
985 rctl_enforce(
child, RACCT_NPROC, 0);
986 rctl_enforce(
child, RACCT_NTHR, 0);
993racct_proc_exit(
struct proc *p)
995 struct timeval wallclock;
996 uint64_t pct_estimate, pct, runtime;
1008 KASSERT(runtime >= p->p_prev_runtime, (
"runtime < p_prev_runtime"));
1010 if (runtime < p->p_prev_runtime)
1011 runtime = p->p_prev_runtime;
1014 timevalsub(&wallclock, &p->p_stats->p_start);
1015 if (wallclock.tv_sec > 0 || wallclock.tv_usec > 0) {
1016 pct_estimate = (1000000 * runtime * 100) /
1017 ((uint64_t)wallclock.tv_sec * 1000000 +
1021 pct = racct_getpcpu(p, pct_estimate);
1024 racct_set_locked(p, RACCT_CPU, runtime, 0);
1025 racct_add_cred_locked(p->p_ucred, RACCT_PCTCPU, pct);
1027 KASSERT(p->p_racct->r_resources[RACCT_RSS] == 0,
1028 (
"process reaped with %ju allocated for RSS\n",
1029 p->p_racct->r_resources[RACCT_RSS]));
1030 for (i = 0; i <= RACCT_MAX; i++) {
1031 if (p->p_racct->r_resources[i] == 0)
1033 if (!RACCT_IS_RECLAIMABLE(i))
1035 racct_set_locked(p, i, 0, 0);
1039 rctl_racct_release(p->p_racct);
1041 racct_destroy_locked(&p->p_racct);
1051racct_proc_ucred_changed(
struct proc *p,
struct ucred *oldcred,
1052 struct ucred *newcred)
1054 struct uidinfo *olduip, *newuip;
1055 struct loginclass *oldlc, *newlc;
1056 struct prison *oldpr, *newpr, *
pr;
1061 PROC_LOCK_ASSERT(p, MA_OWNED);
1063 newuip = newcred->cr_ruidinfo;
1064 olduip = oldcred->cr_ruidinfo;
1065 newlc = newcred->cr_loginclass;
1066 oldlc = oldcred->cr_loginclass;
1067 newpr = newcred->cr_prison;
1068 oldpr = oldcred->cr_prison;
1071 if (newuip != olduip) {
1072 racct_sub_racct(olduip->ui_racct, p->p_racct);
1073 racct_add_racct(newuip->ui_racct, p->p_racct);
1075 if (newlc != oldlc) {
1076 racct_sub_racct(oldlc->lc_racct, p->p_racct);
1077 racct_add_racct(newlc->lc_racct, p->p_racct);
1079 if (newpr != oldpr) {
1080 for (
pr = oldpr;
pr != NULL;
pr =
pr->pr_parent)
1081 racct_sub_racct(
pr->pr_prison_racct->prr_racct,
1083 for (
pr = newpr;
pr != NULL;
pr =
pr->pr_parent)
1084 racct_add_racct(
pr->pr_prison_racct->prr_racct,
1091racct_move(
struct racct *dest,
struct racct *
src)
1094 ASSERT_RACCT_ENABLED();
1097 racct_add_racct(dest,
src);
1098 racct_sub_racct(
src,
src);
1103racct_proc_throttled(
struct proc *p)
1106 ASSERT_RACCT_ENABLED();
1109 while (p->p_throttled != 0) {
1110 msleep(p->p_racct, &p->p_mtx, 0,
"racct",
1111 p->p_throttled < 0 ? 0 : p->p_throttled);
1112 if (p->p_throttled > 0)
1123racct_proc_throttle(
struct proc *p,
int timeout)
1130 KASSERT(timeout != 0, (
"timeout %d", timeout));
1131 ASSERT_RACCT_ENABLED();
1132 PROC_LOCK_ASSERT(p, MA_OWNED);
1138 if ((p->p_flag & (P_SYSTEM | P_KPROC)) != 0)
1141 if (p->p_throttled < 0 || (timeout > 0 && p->p_throttled > timeout))
1144 p->p_throttled = timeout;
1146 FOREACH_THREAD_IN_PROC(p, td) {
1148 td->td_flags |= TDF_ASTPENDING;
1150 switch (TD_GET_STATE(td)) {
1158 td->td_flags |= TDF_NEEDRESCHED;
1165 td->td_flags |= TDF_NEEDRESCHED;
1167 cpuid = td->td_oncpu;
1168 if ((cpuid != NOCPU) && (td != curthread))
1169 ipi_cpu(cpuid, IPI_AST);
1180racct_proc_wakeup(
struct proc *p)
1183 ASSERT_RACCT_ENABLED();
1185 PROC_LOCK_ASSERT(p, MA_OWNED);
1187 if (p->p_throttled != 0) {
1194racct_decay_callback(
struct racct *racct,
void *dummy1,
void *dummy2)
1196 int64_t r_old, r_new;
1198 ASSERT_RACCT_ENABLED();
1199 RACCT_LOCK_ASSERT();
1202 rctl_throttle_decay(racct, RACCT_READBPS);
1203 rctl_throttle_decay(racct, RACCT_WRITEBPS);
1204 rctl_throttle_decay(racct, RACCT_READIOPS);
1205 rctl_throttle_decay(racct, RACCT_WRITEIOPS);
1208 r_old = racct->r_resources[RACCT_PCTCPU];
1214 r_new = r_old * RACCT_DECAY_FACTOR / FSCALE;
1215 racct->r_resources[RACCT_PCTCPU] = r_new;
1219racct_decay_pre(
void)
1226racct_decay_post(
void)
1236 ASSERT_RACCT_ENABLED();
1238 ui_racct_foreach(racct_decay_callback, racct_decay_pre,
1239 racct_decay_post, NULL, NULL);
1241 racct_decay_post, NULL, NULL);
1242 prison_racct_foreach(racct_decay_callback, racct_decay_pre,
1243 racct_decay_post, NULL, NULL);
1251 struct timeval wallclock;
1252 uint64_t pct, pct_estimate, runtime;
1254 ASSERT_RACCT_ENABLED();
1261 FOREACH_PROC_IN_SYSTEM(p) {
1263 if (p->p_state != PRS_NORMAL) {
1264 if (p->p_state == PRS_ZOMBIE)
1265 racct_set(p, RACCT_PCTCPU, 0);
1271 timevalsub(&wallclock, &p->p_stats->p_start);
1273 FOREACH_THREAD_IN_PROC(p, td)
1278 KASSERT(runtime >= p->p_prev_runtime,
1279 (
"runtime < p_prev_runtime"));
1281 if (runtime < p->p_prev_runtime)
1282 runtime = p->p_prev_runtime;
1284 p->p_prev_runtime = runtime;
1285 if (wallclock.tv_sec > 0 || wallclock.tv_usec > 0) {
1286 pct_estimate = (1000000 * runtime * 100) /
1287 ((uint64_t)wallclock.tv_sec * 1000000 +
1291 pct = racct_getpcpu(p, pct_estimate);
1294 rctl_throttle_decay(p->p_racct, RACCT_READBPS);
1295 rctl_throttle_decay(p->p_racct, RACCT_WRITEBPS);
1296 rctl_throttle_decay(p->p_racct, RACCT_READIOPS);
1297 rctl_throttle_decay(p->p_racct, RACCT_WRITEIOPS);
1299 racct_set_locked(p, RACCT_PCTCPU, pct, 1);
1300 racct_set_locked(p, RACCT_CPU, runtime, 0);
1301 racct_set_locked(p, RACCT_WALLCLOCK,
1302 (uint64_t)wallclock.tv_sec * 1000000 +
1303 wallclock.tv_usec, 0);
1314 FOREACH_PROC_IN_SYSTEM(p) {
1316 if (p->p_state != PRS_NORMAL) {
1321 if (racct_pcpu_available(p) <= 0) {
1322 if (p->p_racct->r_resources[RACCT_PCTCPU] >
1324 racct_proc_throttle(p, -1);
1325 }
else if (p->p_throttled == -1) {
1326 racct_proc_wakeup(p);
1335static struct kproc_desc racctd_kp = {
1349SYSINIT(racctd, SI_SUB_RACCTD, SI_ORDER_FIRST, racctd_init, NULL);
1357 racct_zone = uma_zcreate(
"racct",
sizeof(
struct racct),
1358 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1362 prison0.pr_prison_racct = prison_racct_find(
"0");
1364SYSINIT(racct, SI_SUB_RACCT, SI_ORDER_FIRST, racct_init, NULL);
SYSCTL_NODE(_kern, OID_AUTO, binmisc, CTLFLAG_RW|CTLFLAG_MPSAFE, 0, "Image activator for miscellaneous binaries")
SYSINIT(imgact_binmisc, SI_SUB_EXEC, SI_ORDER_MIDDLE, imgact_binmisc_init, NULL)
SYSCTL_BOOL(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO, allow_wx, CTLFLAG_RWTUN, &__elfN(allow_wx), 0, "Allow pages to be mapped simultaneously writable and executable")
SDT_PROBE_DEFINE2(sched,,, tick, "struct thread *", "struct proc *")
SYSCTL_UINT(_kern_eventtimer, OID_AUTO, idletick, CTLFLAG_RWTUN, &idletick, 0, "Run periodic events when idle")
FEATURE(kdtrace_hooks, "Kernel DTrace hooks which are required to load DTrace kernel modules")
MTX_SYSINIT(et_eventtimers_init, &et_eventtimers_mtx, "et_mtx", MTX_DEF)
SDT_PROBE_DEFINE1(proc,,, exec, "char *")
SDT_PROBE_DEFINE3(proc,,, create, "struct proc *", "struct proc *", "int")
void kproc_start(const void *udata)
SDT_PROVIDER_DEFINE(lockstat)
void loginclass_racct_foreach(void(*callback)(struct racct *racct, void *arg2, void *arg3), void(*pre)(void), void(*post)(void), void *arg2, void *arg3)
static struct pollrec pr[POLL_LIST_LEN]
struct sx __exclusive_cache_line allproc_lock
void ruxagg(struct proc *p, struct thread *td)
void wakeup(const void *ident)
void microuptime(struct timeval *tvp)
uint64_t cputick2usec(uint64_t tick)
void timevalsub(struct timeval *t1, const struct timeval *t2)
struct intr_irqsrc ** src
fixpt_t sched_pctcpu(struct thread *td)