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