1 /*- 2 * Copyright (c) 2002-2007, Jeffrey Roberson <jeff@freebsd.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice unmodified, this list of conditions, and the following 10 * disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include "opt_hwpmc_hooks.h" 31 #include "opt_sched.h" 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/kdb.h> 36 #include <sys/kernel.h> 37 #include <sys/ktr.h> 38 #include <sys/lock.h> 39 #include <sys/mutex.h> 40 #include <sys/proc.h> 41 #include <sys/resource.h> 42 #include <sys/resourcevar.h> 43 #include <sys/sched.h> 44 #include <sys/smp.h> 45 #include <sys/sx.h> 46 #include <sys/sysctl.h> 47 #include <sys/sysproto.h> 48 #include <sys/turnstile.h> 49 #include <sys/umtx.h> 50 #include <sys/vmmeter.h> 51 #ifdef KTRACE 52 #include <sys/uio.h> 53 #include <sys/ktrace.h> 54 #endif 55 56 #ifdef HWPMC_HOOKS 57 #include <sys/pmckern.h> 58 #endif 59 60 #include <machine/cpu.h> 61 #include <machine/smp.h> 62 63 /* 64 * TODO: 65 * Pick idle from affinity group or self group first. 66 * Implement pick_score. 67 */ 68 69 /* 70 * Thread scheduler specific section. 71 */ 72 struct td_sched { 73 TAILQ_ENTRY(td_sched) ts_procq; /* (j/z) Run queue. */ 74 int ts_flags; /* (j) TSF_* flags. */ 75 struct thread *ts_thread; /* (*) Active associated thread. */ 76 u_char ts_rqindex; /* (j) Run queue index. */ 77 enum { 78 TSS_THREAD, 79 TSS_ONRUNQ 80 } ts_state; /* (j) thread sched specific status. */ 81 int ts_slptime; 82 int ts_slice; 83 struct runq *ts_runq; 84 u_char ts_cpu; /* CPU that we have affinity for. */ 85 /* The following variables are only used for pctcpu calculation */ 86 int ts_ltick; /* Last tick that we were running on */ 87 int ts_ftick; /* First tick that we were running on */ 88 int ts_ticks; /* Tick count */ 89 #ifdef SMP 90 int ts_rltick; /* Real last tick, for affinity. */ 91 #endif 92 93 /* originally from kg_sched */ 94 int skg_slptime; /* Number of ticks we vol. slept */ 95 int skg_runtime; /* Number of ticks we were running */ 96 }; 97 /* flags kept in ts_flags */ 98 #define TSF_BOUND 0x0001 /* Thread can not migrate. */ 99 #define TSF_XFERABLE 0x0002 /* Thread was added as transferable. */ 100 #define TSF_DIDRUN 0x2000 /* Thread actually ran. */ 101 102 static struct td_sched td_sched0; 103 104 /* 105 * Cpu percentage computation macros and defines. 106 * 107 * SCHED_TICK_SECS: Number of seconds to average the cpu usage across. 108 * SCHED_TICK_TARG: Number of hz ticks to average the cpu usage across. 109 * SCHED_TICK_MAX: Maximum number of ticks before scaling back. 110 * SCHED_TICK_SHIFT: Shift factor to avoid rounding away results. 111 * SCHED_TICK_HZ: Compute the number of hz ticks for a given ticks count. 112 * SCHED_TICK_TOTAL: Gives the amount of time we've been recording ticks. 113 */ 114 #define SCHED_TICK_SECS 10 115 #define SCHED_TICK_TARG (hz * SCHED_TICK_SECS) 116 #define SCHED_TICK_MAX (SCHED_TICK_TARG + hz) 117 #define SCHED_TICK_SHIFT 10 118 #define SCHED_TICK_HZ(ts) ((ts)->ts_ticks >> SCHED_TICK_SHIFT) 119 #define SCHED_TICK_TOTAL(ts) (max((ts)->ts_ltick - (ts)->ts_ftick, hz)) 120 121 /* 122 * These macros determine priorities for non-interactive threads. They are 123 * assigned a priority based on their recent cpu utilization as expressed 124 * by the ratio of ticks to the tick total. NHALF priorities at the start 125 * and end of the MIN to MAX timeshare range are only reachable with negative 126 * or positive nice respectively. 127 * 128 * PRI_RANGE: Priority range for utilization dependent priorities. 129 * PRI_NRESV: Number of nice values. 130 * PRI_TICKS: Compute a priority in PRI_RANGE from the ticks count and total. 131 * PRI_NICE: Determines the part of the priority inherited from nice. 132 */ 133 #define SCHED_PRI_NRESV (PRIO_MAX - PRIO_MIN) 134 #define SCHED_PRI_NHALF (SCHED_PRI_NRESV / 2) 135 #define SCHED_PRI_MIN (PRI_MIN_TIMESHARE + SCHED_PRI_NHALF) 136 #define SCHED_PRI_MAX (PRI_MAX_TIMESHARE - SCHED_PRI_NHALF) 137 #define SCHED_PRI_RANGE (SCHED_PRI_MAX - SCHED_PRI_MIN + 1) 138 #define SCHED_PRI_TICKS(ts) \ 139 (SCHED_TICK_HZ((ts)) / \ 140 (roundup(SCHED_TICK_TOTAL((ts)), SCHED_PRI_RANGE) / SCHED_PRI_RANGE)) 141 #define SCHED_PRI_NICE(nice) (nice) 142 143 /* 144 * These determine the interactivity of a process. Interactivity differs from 145 * cpu utilization in that it expresses the voluntary time slept vs time ran 146 * while cpu utilization includes all time not running. This more accurately 147 * models the intent of the thread. 148 * 149 * SLP_RUN_MAX: Maximum amount of sleep time + run time we'll accumulate 150 * before throttling back. 151 * SLP_RUN_FORK: Maximum slp+run time to inherit at fork time. 152 * INTERACT_MAX: Maximum interactivity value. Smaller is better. 153 * INTERACT_THRESH: Threshhold for placement on the current runq. 154 */ 155 #define SCHED_SLP_RUN_MAX ((hz * 5) << SCHED_TICK_SHIFT) 156 #define SCHED_SLP_RUN_FORK ((hz / 2) << SCHED_TICK_SHIFT) 157 #define SCHED_INTERACT_MAX (100) 158 #define SCHED_INTERACT_HALF (SCHED_INTERACT_MAX / 2) 159 #define SCHED_INTERACT_THRESH (30) 160 161 /* 162 * tickincr: Converts a stathz tick into a hz domain scaled by 163 * the shift factor. Without the shift the error rate 164 * due to rounding would be unacceptably high. 165 * realstathz: stathz is sometimes 0 and run off of hz. 166 * sched_slice: Runtime of each thread before rescheduling. 167 */ 168 static int sched_interact = SCHED_INTERACT_THRESH; 169 static int realstathz; 170 static int tickincr; 171 static int sched_slice; 172 173 /* 174 * tdq - per processor runqs and statistics. 175 */ 176 struct tdq { 177 struct runq tdq_idle; /* Queue of IDLE threads. */ 178 struct runq tdq_timeshare; /* timeshare run queue. */ 179 struct runq tdq_realtime; /* real-time run queue. */ 180 int tdq_idx; /* Current insert index. */ 181 int tdq_ridx; /* Current removal index. */ 182 int tdq_load; /* Aggregate load. */ 183 int tdq_flags; /* Thread queue flags */ 184 #ifdef SMP 185 int tdq_transferable; 186 LIST_ENTRY(tdq) tdq_siblings; /* Next in tdq group. */ 187 struct tdq_group *tdq_group; /* Our processor group. */ 188 #else 189 int tdq_sysload; /* For loadavg, !ITHD load. */ 190 #endif 191 }; 192 193 #define TDQF_BUSY 0x0001 /* Queue is marked as busy */ 194 195 #ifdef SMP 196 /* 197 * tdq groups are groups of processors which can cheaply share threads. When 198 * one processor in the group goes idle it will check the runqs of the other 199 * processors in its group prior to halting and waiting for an interrupt. 200 * These groups are suitable for SMT (Symetric Multi-Threading) and not NUMA. 201 * In a numa environment we'd want an idle bitmap per group and a two tiered 202 * load balancer. 203 */ 204 struct tdq_group { 205 int tdg_cpus; /* Count of CPUs in this tdq group. */ 206 cpumask_t tdg_cpumask; /* Mask of cpus in this group. */ 207 cpumask_t tdg_idlemask; /* Idle cpus in this group. */ 208 cpumask_t tdg_mask; /* Bit mask for first cpu. */ 209 int tdg_load; /* Total load of this group. */ 210 int tdg_transferable; /* Transferable load of this group. */ 211 LIST_HEAD(, tdq) tdg_members; /* Linked list of all members. */ 212 }; 213 214 #define SCHED_AFFINITY_DEFAULT (hz / 100) 215 #define SCHED_AFFINITY(ts) ((ts)->ts_rltick > ticks - affinity) 216 217 /* 218 * Run-time tunables. 219 */ 220 static int rebalance = 0; 221 static int pick_pri = 1; 222 static int affinity; 223 static int tryself = 1; 224 static int tryselfidle = 1; 225 static int ipi_ast = 0; 226 static int ipi_preempt = 1; 227 static int ipi_thresh = PRI_MIN_KERN; 228 static int steal_htt = 1; 229 static int steal_busy = 1; 230 static int busy_thresh = 4; 231 232 /* 233 * One thread queue per processor. 234 */ 235 static volatile cpumask_t tdq_idle; 236 static volatile cpumask_t tdq_busy; 237 static int tdg_maxid; 238 static struct tdq tdq_cpu[MAXCPU]; 239 static struct tdq_group tdq_groups[MAXCPU]; 240 static int bal_tick; 241 static int gbal_tick; 242 static int balance_groups; 243 244 #define TDQ_SELF() (&tdq_cpu[PCPU_GET(cpuid)]) 245 #define TDQ_CPU(x) (&tdq_cpu[(x)]) 246 #define TDQ_ID(x) ((x) - tdq_cpu) 247 #define TDQ_GROUP(x) (&tdq_groups[(x)]) 248 #else /* !SMP */ 249 static struct tdq tdq_cpu; 250 251 #define TDQ_SELF() (&tdq_cpu) 252 #define TDQ_CPU(x) (&tdq_cpu) 253 #endif 254 255 static struct td_sched *sched_choose(void); /* XXX Should be thread * */ 256 static void sched_priority(struct thread *); 257 static void sched_thread_priority(struct thread *, u_char); 258 static int sched_interact_score(struct thread *); 259 static void sched_interact_update(struct thread *); 260 static void sched_interact_fork(struct thread *); 261 static void sched_pctcpu_update(struct td_sched *); 262 static inline void sched_pin_td(struct thread *td); 263 static inline void sched_unpin_td(struct thread *td); 264 265 /* Operations on per processor queues */ 266 static struct td_sched * tdq_choose(struct tdq *); 267 static void tdq_setup(struct tdq *); 268 static void tdq_load_add(struct tdq *, struct td_sched *); 269 static void tdq_load_rem(struct tdq *, struct td_sched *); 270 static __inline void tdq_runq_add(struct tdq *, struct td_sched *, int); 271 static __inline void tdq_runq_rem(struct tdq *, struct td_sched *); 272 void tdq_print(int cpu); 273 static void runq_print(struct runq *rq); 274 #ifdef SMP 275 static int tdq_pickidle(struct tdq *, struct td_sched *); 276 static int tdq_pickpri(struct tdq *, struct td_sched *, int); 277 static struct td_sched *runq_steal(struct runq *); 278 static void sched_balance(void); 279 static void sched_balance_groups(void); 280 static void sched_balance_group(struct tdq_group *); 281 static void sched_balance_pair(struct tdq *, struct tdq *); 282 static void sched_smp_tick(struct thread *); 283 static void tdq_move(struct tdq *, int); 284 static int tdq_idled(struct tdq *); 285 static void tdq_notify(struct td_sched *); 286 static struct td_sched *tdq_steal(struct tdq *, int); 287 288 #define THREAD_CAN_MIGRATE(td) ((td)->td_pinned == 0) 289 #endif 290 291 static void sched_setup(void *dummy); 292 SYSINIT(sched_setup, SI_SUB_RUN_QUEUE, SI_ORDER_FIRST, sched_setup, NULL) 293 294 static void sched_initticks(void *dummy); 295 SYSINIT(sched_initticks, SI_SUB_CLOCKS, SI_ORDER_THIRD, sched_initticks, NULL) 296 297 static inline void 298 sched_pin_td(struct thread *td) 299 { 300 td->td_pinned++; 301 } 302 303 static inline void 304 sched_unpin_td(struct thread *td) 305 { 306 td->td_pinned--; 307 } 308 309 static void 310 runq_print(struct runq *rq) 311 { 312 struct rqhead *rqh; 313 struct td_sched *ts; 314 int pri; 315 int j; 316 int i; 317 318 for (i = 0; i < RQB_LEN; i++) { 319 printf("\t\trunq bits %d 0x%zx\n", 320 i, rq->rq_status.rqb_bits[i]); 321 for (j = 0; j < RQB_BPW; j++) 322 if (rq->rq_status.rqb_bits[i] & (1ul << j)) { 323 pri = j + (i << RQB_L2BPW); 324 rqh = &rq->rq_queues[pri]; 325 TAILQ_FOREACH(ts, rqh, ts_procq) { 326 printf("\t\t\ttd %p(%s) priority %d rqindex %d pri %d\n", 327 ts->ts_thread, ts->ts_thread->td_proc->p_comm, ts->ts_thread->td_priority, ts->ts_rqindex, pri); 328 } 329 } 330 } 331 } 332 333 void 334 tdq_print(int cpu) 335 { 336 struct tdq *tdq; 337 338 tdq = TDQ_CPU(cpu); 339 340 printf("tdq:\n"); 341 printf("\tload: %d\n", tdq->tdq_load); 342 printf("\ttimeshare idx: %d\n", tdq->tdq_idx); 343 printf("\ttimeshare ridx: %d\n", tdq->tdq_ridx); 344 printf("\trealtime runq:\n"); 345 runq_print(&tdq->tdq_realtime); 346 printf("\ttimeshare runq:\n"); 347 runq_print(&tdq->tdq_timeshare); 348 printf("\tidle runq:\n"); 349 runq_print(&tdq->tdq_idle); 350 #ifdef SMP 351 printf("\tload transferable: %d\n", tdq->tdq_transferable); 352 #endif 353 } 354 355 static __inline void 356 tdq_runq_add(struct tdq *tdq, struct td_sched *ts, int flags) 357 { 358 #ifdef SMP 359 if (THREAD_CAN_MIGRATE(ts->ts_thread)) { 360 tdq->tdq_transferable++; 361 tdq->tdq_group->tdg_transferable++; 362 ts->ts_flags |= TSF_XFERABLE; 363 if (tdq->tdq_transferable >= busy_thresh && 364 (tdq->tdq_flags & TDQF_BUSY) == 0) { 365 tdq->tdq_flags |= TDQF_BUSY; 366 atomic_set_int(&tdq_busy, 1 << TDQ_ID(tdq)); 367 } 368 } 369 #endif 370 if (ts->ts_runq == &tdq->tdq_timeshare) { 371 int pri; 372 373 pri = ts->ts_thread->td_priority; 374 KASSERT(pri <= PRI_MAX_TIMESHARE && pri >= PRI_MIN_TIMESHARE, 375 ("Invalid priority %d on timeshare runq", pri)); 376 /* 377 * This queue contains only priorities between MIN and MAX 378 * realtime. Use the whole queue to represent these values. 379 */ 380 #define TS_RQ_PPQ (((PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE) + 1) / RQ_NQS) 381 if ((flags & SRQ_BORROWING) == 0) { 382 pri = (pri - PRI_MIN_TIMESHARE) / TS_RQ_PPQ; 383 pri = (pri + tdq->tdq_idx) % RQ_NQS; 384 /* 385 * This effectively shortens the queue by one so we 386 * can have a one slot difference between idx and 387 * ridx while we wait for threads to drain. 388 */ 389 if (tdq->tdq_ridx != tdq->tdq_idx && 390 pri == tdq->tdq_ridx) 391 pri = (pri - 1) % RQ_NQS; 392 } else 393 pri = tdq->tdq_ridx; 394 runq_add_pri(ts->ts_runq, ts, pri, flags); 395 } else 396 runq_add(ts->ts_runq, ts, flags); 397 } 398 399 static __inline void 400 tdq_runq_rem(struct tdq *tdq, struct td_sched *ts) 401 { 402 #ifdef SMP 403 if (ts->ts_flags & TSF_XFERABLE) { 404 tdq->tdq_transferable--; 405 tdq->tdq_group->tdg_transferable--; 406 ts->ts_flags &= ~TSF_XFERABLE; 407 if (tdq->tdq_transferable < busy_thresh && 408 (tdq->tdq_flags & TDQF_BUSY)) { 409 atomic_clear_int(&tdq_busy, 1 << TDQ_ID(tdq)); 410 tdq->tdq_flags &= ~TDQF_BUSY; 411 } 412 } 413 #endif 414 if (ts->ts_runq == &tdq->tdq_timeshare) { 415 if (tdq->tdq_idx != tdq->tdq_ridx) 416 runq_remove_idx(ts->ts_runq, ts, &tdq->tdq_ridx); 417 else 418 runq_remove_idx(ts->ts_runq, ts, NULL); 419 /* 420 * For timeshare threads we update the priority here so 421 * the priority reflects the time we've been sleeping. 422 */ 423 ts->ts_ltick = ticks; 424 sched_pctcpu_update(ts); 425 sched_priority(ts->ts_thread); 426 } else 427 runq_remove(ts->ts_runq, ts); 428 } 429 430 static void 431 tdq_load_add(struct tdq *tdq, struct td_sched *ts) 432 { 433 int class; 434 mtx_assert(&sched_lock, MA_OWNED); 435 class = PRI_BASE(ts->ts_thread->td_pri_class); 436 tdq->tdq_load++; 437 CTR1(KTR_SCHED, "load: %d", tdq->tdq_load); 438 if (class != PRI_ITHD && 439 (ts->ts_thread->td_proc->p_flag & P_NOLOAD) == 0) 440 #ifdef SMP 441 tdq->tdq_group->tdg_load++; 442 #else 443 tdq->tdq_sysload++; 444 #endif 445 } 446 447 static void 448 tdq_load_rem(struct tdq *tdq, struct td_sched *ts) 449 { 450 int class; 451 mtx_assert(&sched_lock, MA_OWNED); 452 class = PRI_BASE(ts->ts_thread->td_pri_class); 453 if (class != PRI_ITHD && 454 (ts->ts_thread->td_proc->p_flag & P_NOLOAD) == 0) 455 #ifdef SMP 456 tdq->tdq_group->tdg_load--; 457 #else 458 tdq->tdq_sysload--; 459 #endif 460 tdq->tdq_load--; 461 CTR1(KTR_SCHED, "load: %d", tdq->tdq_load); 462 ts->ts_runq = NULL; 463 } 464 465 #ifdef SMP 466 static void 467 sched_smp_tick(struct thread *td) 468 { 469 struct tdq *tdq; 470 471 tdq = TDQ_SELF(); 472 if (rebalance) { 473 if (ticks >= bal_tick) 474 sched_balance(); 475 if (ticks >= gbal_tick && balance_groups) 476 sched_balance_groups(); 477 } 478 td->td_sched->ts_rltick = ticks; 479 } 480 481 /* 482 * sched_balance is a simple CPU load balancing algorithm. It operates by 483 * finding the least loaded and most loaded cpu and equalizing their load 484 * by migrating some processes. 485 * 486 * Dealing only with two CPUs at a time has two advantages. Firstly, most 487 * installations will only have 2 cpus. Secondly, load balancing too much at 488 * once can have an unpleasant effect on the system. The scheduler rarely has 489 * enough information to make perfect decisions. So this algorithm chooses 490 * algorithm simplicity and more gradual effects on load in larger systems. 491 * 492 * It could be improved by considering the priorities and slices assigned to 493 * each task prior to balancing them. There are many pathological cases with 494 * any approach and so the semi random algorithm below may work as well as any. 495 * 496 */ 497 static void 498 sched_balance(void) 499 { 500 struct tdq_group *high; 501 struct tdq_group *low; 502 struct tdq_group *tdg; 503 int cnt; 504 int i; 505 506 bal_tick = ticks + (random() % (hz * 2)); 507 if (smp_started == 0) 508 return; 509 low = high = NULL; 510 i = random() % (tdg_maxid + 1); 511 for (cnt = 0; cnt <= tdg_maxid; cnt++) { 512 tdg = TDQ_GROUP(i); 513 /* 514 * Find the CPU with the highest load that has some 515 * threads to transfer. 516 */ 517 if ((high == NULL || tdg->tdg_load > high->tdg_load) 518 && tdg->tdg_transferable) 519 high = tdg; 520 if (low == NULL || tdg->tdg_load < low->tdg_load) 521 low = tdg; 522 if (++i > tdg_maxid) 523 i = 0; 524 } 525 if (low != NULL && high != NULL && high != low) 526 sched_balance_pair(LIST_FIRST(&high->tdg_members), 527 LIST_FIRST(&low->tdg_members)); 528 } 529 530 static void 531 sched_balance_groups(void) 532 { 533 int i; 534 535 gbal_tick = ticks + (random() % (hz * 2)); 536 mtx_assert(&sched_lock, MA_OWNED); 537 if (smp_started) 538 for (i = 0; i <= tdg_maxid; i++) 539 sched_balance_group(TDQ_GROUP(i)); 540 } 541 542 static void 543 sched_balance_group(struct tdq_group *tdg) 544 { 545 struct tdq *tdq; 546 struct tdq *high; 547 struct tdq *low; 548 int load; 549 550 if (tdg->tdg_transferable == 0) 551 return; 552 low = NULL; 553 high = NULL; 554 LIST_FOREACH(tdq, &tdg->tdg_members, tdq_siblings) { 555 load = tdq->tdq_load; 556 if (high == NULL || load > high->tdq_load) 557 high = tdq; 558 if (low == NULL || load < low->tdq_load) 559 low = tdq; 560 } 561 if (high != NULL && low != NULL && high != low) 562 sched_balance_pair(high, low); 563 } 564 565 static void 566 sched_balance_pair(struct tdq *high, struct tdq *low) 567 { 568 int transferable; 569 int high_load; 570 int low_load; 571 int move; 572 int diff; 573 int i; 574 575 /* 576 * If we're transfering within a group we have to use this specific 577 * tdq's transferable count, otherwise we can steal from other members 578 * of the group. 579 */ 580 if (high->tdq_group == low->tdq_group) { 581 transferable = high->tdq_transferable; 582 high_load = high->tdq_load; 583 low_load = low->tdq_load; 584 } else { 585 transferable = high->tdq_group->tdg_transferable; 586 high_load = high->tdq_group->tdg_load; 587 low_load = low->tdq_group->tdg_load; 588 } 589 if (transferable == 0) 590 return; 591 /* 592 * Determine what the imbalance is and then adjust that to how many 593 * threads we actually have to give up (transferable). 594 */ 595 diff = high_load - low_load; 596 move = diff / 2; 597 if (diff & 0x1) 598 move++; 599 move = min(move, transferable); 600 for (i = 0; i < move; i++) 601 tdq_move(high, TDQ_ID(low)); 602 return; 603 } 604 605 static void 606 tdq_move(struct tdq *from, int cpu) 607 { 608 struct tdq *tdq; 609 struct tdq *to; 610 struct td_sched *ts; 611 612 tdq = from; 613 to = TDQ_CPU(cpu); 614 ts = tdq_steal(tdq, 1); 615 if (ts == NULL) { 616 struct tdq_group *tdg; 617 618 tdg = tdq->tdq_group; 619 LIST_FOREACH(tdq, &tdg->tdg_members, tdq_siblings) { 620 if (tdq == from || tdq->tdq_transferable == 0) 621 continue; 622 ts = tdq_steal(tdq, 1); 623 break; 624 } 625 if (ts == NULL) 626 panic("tdq_move: No threads available with a " 627 "transferable count of %d\n", 628 tdg->tdg_transferable); 629 } 630 if (tdq == to) 631 return; 632 sched_rem(ts->ts_thread); 633 ts->ts_cpu = cpu; 634 sched_pin_td(ts->ts_thread); 635 sched_add(ts->ts_thread, SRQ_YIELDING); 636 sched_unpin_td(ts->ts_thread); 637 } 638 639 static int 640 tdq_idled(struct tdq *tdq) 641 { 642 struct tdq_group *tdg; 643 struct tdq *steal; 644 struct td_sched *ts; 645 646 tdg = tdq->tdq_group; 647 /* 648 * If we're in a cpu group, try and steal threads from another cpu in 649 * the group before idling. 650 */ 651 if (steal_htt && tdg->tdg_cpus > 1 && tdg->tdg_transferable) { 652 LIST_FOREACH(steal, &tdg->tdg_members, tdq_siblings) { 653 if (steal == tdq || steal->tdq_transferable == 0) 654 continue; 655 ts = tdq_steal(steal, 0); 656 if (ts) 657 goto steal; 658 } 659 } 660 if (steal_busy) { 661 while (tdq_busy) { 662 int cpu; 663 664 cpu = ffs(tdq_busy); 665 if (cpu == 0) 666 break; 667 cpu--; 668 steal = TDQ_CPU(cpu); 669 if (steal->tdq_transferable == 0) 670 continue; 671 ts = tdq_steal(steal, 1); 672 if (ts == NULL) 673 continue; 674 CTR5(KTR_SCHED, 675 "tdq_idled: stealing td %p(%s) pri %d from %d busy 0x%X", 676 ts->ts_thread, ts->ts_thread->td_proc->p_comm, 677 ts->ts_thread->td_priority, cpu, tdq_busy); 678 goto steal; 679 } 680 } 681 /* 682 * We only set the idled bit when all of the cpus in the group are 683 * idle. Otherwise we could get into a situation where a thread bounces 684 * back and forth between two idle cores on seperate physical CPUs. 685 */ 686 tdg->tdg_idlemask |= PCPU_GET(cpumask); 687 if (tdg->tdg_idlemask == tdg->tdg_cpumask) 688 atomic_set_int(&tdq_idle, tdg->tdg_mask); 689 return (1); 690 steal: 691 sched_rem(ts->ts_thread); 692 ts->ts_cpu = PCPU_GET(cpuid); 693 sched_pin_td(ts->ts_thread); 694 sched_add(ts->ts_thread, SRQ_YIELDING); 695 sched_unpin_td(ts->ts_thread); 696 697 return (0); 698 } 699 700 static void 701 tdq_notify(struct td_sched *ts) 702 { 703 struct thread *td; 704 struct pcpu *pcpu; 705 int prio; 706 int cpu; 707 708 prio = ts->ts_thread->td_priority; 709 cpu = ts->ts_cpu; 710 pcpu = pcpu_find(cpu); 711 td = pcpu->pc_curthread; 712 713 /* 714 * If our priority is not better than the current priority there is 715 * nothing to do. 716 */ 717 if (prio > td->td_priority) 718 return; 719 /* Always set NEEDRESCHED. */ 720 td->td_flags |= TDF_NEEDRESCHED; 721 /* 722 * IPI if we exceed the threshold or if the target cpu is running an 723 * idle thread. 724 */ 725 if (prio > ipi_thresh && td->td_priority < PRI_MIN_IDLE) 726 return; 727 if (ipi_ast) 728 ipi_selected(1 << cpu, IPI_AST); 729 else if (ipi_preempt) 730 ipi_selected(1 << cpu, IPI_PREEMPT); 731 } 732 733 static struct td_sched * 734 runq_steal(struct runq *rq) 735 { 736 struct rqhead *rqh; 737 struct rqbits *rqb; 738 struct td_sched *ts; 739 int word; 740 int bit; 741 742 mtx_assert(&sched_lock, MA_OWNED); 743 rqb = &rq->rq_status; 744 for (word = 0; word < RQB_LEN; word++) { 745 if (rqb->rqb_bits[word] == 0) 746 continue; 747 for (bit = 0; bit < RQB_BPW; bit++) { 748 if ((rqb->rqb_bits[word] & (1ul << bit)) == 0) 749 continue; 750 rqh = &rq->rq_queues[bit + (word << RQB_L2BPW)]; 751 TAILQ_FOREACH(ts, rqh, ts_procq) { 752 if (THREAD_CAN_MIGRATE(ts->ts_thread)) 753 return (ts); 754 } 755 } 756 } 757 return (NULL); 758 } 759 760 static struct td_sched * 761 tdq_steal(struct tdq *tdq, int stealidle) 762 { 763 struct td_sched *ts; 764 765 /* 766 * Steal from next first to try to get a non-interactive task that 767 * may not have run for a while. 768 * XXX Need to effect steal order for timeshare threads. 769 */ 770 if ((ts = runq_steal(&tdq->tdq_realtime)) != NULL) 771 return (ts); 772 if ((ts = runq_steal(&tdq->tdq_timeshare)) != NULL) 773 return (ts); 774 if (stealidle) 775 return (runq_steal(&tdq->tdq_idle)); 776 return (NULL); 777 } 778 779 int 780 tdq_pickidle(struct tdq *tdq, struct td_sched *ts) 781 { 782 struct tdq_group *tdg; 783 int self; 784 int cpu; 785 786 self = PCPU_GET(cpuid); 787 if (smp_started == 0) 788 return (self); 789 /* 790 * If the current CPU has idled, just run it here. 791 */ 792 if ((tdq->tdq_group->tdg_idlemask & PCPU_GET(cpumask)) != 0) 793 return (self); 794 /* 795 * Try the last group we ran on. 796 */ 797 tdg = TDQ_CPU(ts->ts_cpu)->tdq_group; 798 cpu = ffs(tdg->tdg_idlemask); 799 if (cpu) 800 return (cpu - 1); 801 /* 802 * Search for an idle group. 803 */ 804 cpu = ffs(tdq_idle); 805 if (cpu) 806 return (cpu - 1); 807 /* 808 * XXX If there are no idle groups, check for an idle core. 809 */ 810 /* 811 * No idle CPUs? 812 */ 813 return (self); 814 } 815 816 static int 817 tdq_pickpri(struct tdq *tdq, struct td_sched *ts, int flags) 818 { 819 struct pcpu *pcpu; 820 int lowpri; 821 int lowcpu; 822 int lowload; 823 int load; 824 int self; 825 int pri; 826 int cpu; 827 828 self = PCPU_GET(cpuid); 829 if (smp_started == 0) 830 return (self); 831 832 pri = ts->ts_thread->td_priority; 833 /* 834 * Regardless of affinity, if the last cpu is idle send it there. 835 */ 836 pcpu = pcpu_find(ts->ts_cpu); 837 if (pcpu->pc_curthread->td_priority > PRI_MIN_IDLE) { 838 CTR5(KTR_SCHED, 839 "ts_cpu %d idle, ltick %d ticks %d pri %d curthread %d", 840 ts->ts_cpu, ts->ts_rltick, ticks, pri, 841 pcpu->pc_curthread->td_priority); 842 return (ts->ts_cpu); 843 } 844 /* 845 * If we have affinity, try to place it on the cpu we last ran on. 846 */ 847 if (SCHED_AFFINITY(ts) && pcpu->pc_curthread->td_priority > pri) { 848 CTR5(KTR_SCHED, 849 "affinity for %d, ltick %d ticks %d pri %d curthread %d", 850 ts->ts_cpu, ts->ts_rltick, ticks, pri, 851 pcpu->pc_curthread->td_priority); 852 return (ts->ts_cpu); 853 } 854 /* 855 * Try ourself first; If we're running something lower priority this 856 * may have some locality with the waking thread and execute faster 857 * here. 858 */ 859 if (tryself) { 860 /* 861 * If we're being awoken by an interrupt thread or the waker 862 * is going right to sleep run here as well. 863 */ 864 if ((TDQ_SELF()->tdq_load == 1) && (flags & SRQ_YIELDING || 865 curthread->td_pri_class == PRI_ITHD)) { 866 CTR2(KTR_SCHED, "tryself load %d flags %d", 867 TDQ_SELF()->tdq_load, flags); 868 return (self); 869 } 870 } 871 /* 872 * Look for an idle group. 873 */ 874 CTR1(KTR_SCHED, "tdq_idle %X", tdq_idle); 875 cpu = ffs(tdq_idle); 876 if (cpu) 877 return (cpu - 1); 878 if (tryselfidle && pri < curthread->td_priority) { 879 CTR1(KTR_SCHED, "tryself %d", 880 curthread->td_priority); 881 return (self); 882 } 883 /* 884 * Now search for the cpu running the lowest priority thread with 885 * the least load. 886 */ 887 lowload = 0; 888 lowpri = lowcpu = 0; 889 for (cpu = 0; cpu <= mp_maxid; cpu++) { 890 if (CPU_ABSENT(cpu)) 891 continue; 892 pcpu = pcpu_find(cpu); 893 pri = pcpu->pc_curthread->td_priority; 894 CTR4(KTR_SCHED, 895 "cpu %d pri %d lowcpu %d lowpri %d", 896 cpu, pri, lowcpu, lowpri); 897 if (pri < lowpri) 898 continue; 899 load = TDQ_CPU(cpu)->tdq_load; 900 if (lowpri && lowpri == pri && load > lowload) 901 continue; 902 lowpri = pri; 903 lowcpu = cpu; 904 lowload = load; 905 } 906 907 return (lowcpu); 908 } 909 910 #endif /* SMP */ 911 912 /* 913 * Pick the highest priority task we have and return it. 914 */ 915 916 static struct td_sched * 917 tdq_choose(struct tdq *tdq) 918 { 919 struct td_sched *ts; 920 921 mtx_assert(&sched_lock, MA_OWNED); 922 923 ts = runq_choose(&tdq->tdq_realtime); 924 if (ts != NULL) { 925 KASSERT(ts->ts_thread->td_priority <= PRI_MAX_REALTIME, 926 ("tdq_choose: Invalid priority on realtime queue %d", 927 ts->ts_thread->td_priority)); 928 return (ts); 929 } 930 ts = runq_choose_from(&tdq->tdq_timeshare, tdq->tdq_ridx); 931 if (ts != NULL) { 932 KASSERT(ts->ts_thread->td_priority <= PRI_MAX_TIMESHARE && 933 ts->ts_thread->td_priority >= PRI_MIN_TIMESHARE, 934 ("tdq_choose: Invalid priority on timeshare queue %d", 935 ts->ts_thread->td_priority)); 936 return (ts); 937 } 938 939 ts = runq_choose(&tdq->tdq_idle); 940 if (ts != NULL) { 941 KASSERT(ts->ts_thread->td_priority >= PRI_MIN_IDLE, 942 ("tdq_choose: Invalid priority on idle queue %d", 943 ts->ts_thread->td_priority)); 944 return (ts); 945 } 946 947 return (NULL); 948 } 949 950 static void 951 tdq_setup(struct tdq *tdq) 952 { 953 runq_init(&tdq->tdq_realtime); 954 runq_init(&tdq->tdq_timeshare); 955 runq_init(&tdq->tdq_idle); 956 tdq->tdq_load = 0; 957 } 958 959 static void 960 sched_setup(void *dummy) 961 { 962 #ifdef SMP 963 int i; 964 #endif 965 966 /* 967 * To avoid divide-by-zero, we set realstathz a dummy value 968 * in case which sched_clock() called before sched_initticks(). 969 */ 970 realstathz = hz; 971 sched_slice = (realstathz/7); /* 140ms */ 972 tickincr = 1 << SCHED_TICK_SHIFT; 973 974 #ifdef SMP 975 balance_groups = 0; 976 /* 977 * Initialize the tdqs. 978 */ 979 for (i = 0; i < MAXCPU; i++) { 980 struct tdq *tdq; 981 982 tdq = &tdq_cpu[i]; 983 tdq_setup(&tdq_cpu[i]); 984 } 985 if (smp_topology == NULL) { 986 struct tdq_group *tdg; 987 struct tdq *tdq; 988 int cpus; 989 990 for (cpus = 0, i = 0; i < MAXCPU; i++) { 991 if (CPU_ABSENT(i)) 992 continue; 993 tdq = &tdq_cpu[i]; 994 tdg = &tdq_groups[cpus]; 995 /* 996 * Setup a tdq group with one member. 997 */ 998 tdq->tdq_transferable = 0; 999 tdq->tdq_group = tdg; 1000 tdg->tdg_cpus = 1; 1001 tdg->tdg_idlemask = 0; 1002 tdg->tdg_cpumask = tdg->tdg_mask = 1 << i; 1003 tdg->tdg_load = 0; 1004 tdg->tdg_transferable = 0; 1005 LIST_INIT(&tdg->tdg_members); 1006 LIST_INSERT_HEAD(&tdg->tdg_members, tdq, tdq_siblings); 1007 cpus++; 1008 } 1009 tdg_maxid = cpus - 1; 1010 } else { 1011 struct tdq_group *tdg; 1012 struct cpu_group *cg; 1013 int j; 1014 1015 for (i = 0; i < smp_topology->ct_count; i++) { 1016 cg = &smp_topology->ct_group[i]; 1017 tdg = &tdq_groups[i]; 1018 /* 1019 * Initialize the group. 1020 */ 1021 tdg->tdg_idlemask = 0; 1022 tdg->tdg_load = 0; 1023 tdg->tdg_transferable = 0; 1024 tdg->tdg_cpus = cg->cg_count; 1025 tdg->tdg_cpumask = cg->cg_mask; 1026 LIST_INIT(&tdg->tdg_members); 1027 /* 1028 * Find all of the group members and add them. 1029 */ 1030 for (j = 0; j < MAXCPU; j++) { 1031 if ((cg->cg_mask & (1 << j)) != 0) { 1032 if (tdg->tdg_mask == 0) 1033 tdg->tdg_mask = 1 << j; 1034 tdq_cpu[j].tdq_transferable = 0; 1035 tdq_cpu[j].tdq_group = tdg; 1036 LIST_INSERT_HEAD(&tdg->tdg_members, 1037 &tdq_cpu[j], tdq_siblings); 1038 } 1039 } 1040 if (tdg->tdg_cpus > 1) 1041 balance_groups = 1; 1042 } 1043 tdg_maxid = smp_topology->ct_count - 1; 1044 } 1045 /* 1046 * Stagger the group and global load balancer so they do not 1047 * interfere with each other. 1048 */ 1049 bal_tick = ticks + hz; 1050 if (balance_groups) 1051 gbal_tick = ticks + (hz / 2); 1052 #else 1053 tdq_setup(TDQ_SELF()); 1054 #endif 1055 mtx_lock_spin(&sched_lock); 1056 tdq_load_add(TDQ_SELF(), &td_sched0); 1057 mtx_unlock_spin(&sched_lock); 1058 } 1059 1060 /* ARGSUSED */ 1061 static void 1062 sched_initticks(void *dummy) 1063 { 1064 mtx_lock_spin(&sched_lock); 1065 realstathz = stathz ? stathz : hz; 1066 sched_slice = (realstathz/7); /* ~140ms */ 1067 1068 /* 1069 * tickincr is shifted out by 10 to avoid rounding errors due to 1070 * hz not being evenly divisible by stathz on all platforms. 1071 */ 1072 tickincr = (hz << SCHED_TICK_SHIFT) / realstathz; 1073 /* 1074 * This does not work for values of stathz that are more than 1075 * 1 << SCHED_TICK_SHIFT * hz. In practice this does not happen. 1076 */ 1077 if (tickincr == 0) 1078 tickincr = 1; 1079 #ifdef SMP 1080 affinity = SCHED_AFFINITY_DEFAULT; 1081 #endif 1082 mtx_unlock_spin(&sched_lock); 1083 } 1084 1085 1086 /* 1087 * Scale the scheduling priority according to the "interactivity" of this 1088 * process. 1089 */ 1090 static void 1091 sched_priority(struct thread *td) 1092 { 1093 int score; 1094 int pri; 1095 1096 if (td->td_pri_class != PRI_TIMESHARE) 1097 return; 1098 /* 1099 * If the score is interactive we place the thread in the realtime 1100 * queue with a priority that is less than kernel and interrupt 1101 * priorities. These threads are not subject to nice restrictions. 1102 * 1103 * Scores greater than this are placed on the normal realtime queue 1104 * where the priority is partially decided by the most recent cpu 1105 * utilization and the rest is decided by nice value. 1106 */ 1107 score = sched_interact_score(td); 1108 if (score < sched_interact) { 1109 pri = PRI_MIN_REALTIME; 1110 pri += ((PRI_MAX_REALTIME - PRI_MIN_REALTIME) / sched_interact) 1111 * score; 1112 KASSERT(pri >= PRI_MIN_REALTIME && pri <= PRI_MAX_REALTIME, 1113 ("sched_priority: invalid interactive priority %d", pri)); 1114 } else { 1115 pri = SCHED_PRI_MIN; 1116 if (td->td_sched->ts_ticks) 1117 pri += SCHED_PRI_TICKS(td->td_sched); 1118 pri += SCHED_PRI_NICE(td->td_proc->p_nice); 1119 if (!(pri >= PRI_MIN_TIMESHARE && pri <= PRI_MAX_TIMESHARE)) { 1120 static int once = 1; 1121 if (once) { 1122 printf("sched_priority: invalid priority %d", 1123 pri); 1124 printf("nice %d, ticks %d ftick %d ltick %d tick pri %d\n", 1125 td->td_proc->p_nice, 1126 td->td_sched->ts_ticks, 1127 td->td_sched->ts_ftick, 1128 td->td_sched->ts_ltick, 1129 SCHED_PRI_TICKS(td->td_sched)); 1130 once = 0; 1131 } 1132 pri = min(max(pri, PRI_MIN_TIMESHARE), 1133 PRI_MAX_TIMESHARE); 1134 } 1135 } 1136 sched_user_prio(td, pri); 1137 1138 return; 1139 } 1140 1141 /* 1142 * This routine enforces a maximum limit on the amount of scheduling history 1143 * kept. It is called after either the slptime or runtime is adjusted. 1144 */ 1145 static void 1146 sched_interact_update(struct thread *td) 1147 { 1148 struct td_sched *ts; 1149 int sum; 1150 1151 ts = td->td_sched; 1152 sum = ts->skg_runtime + ts->skg_slptime; 1153 if (sum < SCHED_SLP_RUN_MAX) 1154 return; 1155 /* 1156 * This only happens from two places: 1157 * 1) We have added an unusual amount of run time from fork_exit. 1158 * 2) We have added an unusual amount of sleep time from sched_sleep(). 1159 */ 1160 if (sum > SCHED_SLP_RUN_MAX * 2) { 1161 if (ts->skg_runtime > ts->skg_slptime) { 1162 ts->skg_runtime = SCHED_SLP_RUN_MAX; 1163 ts->skg_slptime = 1; 1164 } else { 1165 ts->skg_slptime = SCHED_SLP_RUN_MAX; 1166 ts->skg_runtime = 1; 1167 } 1168 return; 1169 } 1170 /* 1171 * If we have exceeded by more than 1/5th then the algorithm below 1172 * will not bring us back into range. Dividing by two here forces 1173 * us into the range of [4/5 * SCHED_INTERACT_MAX, SCHED_INTERACT_MAX] 1174 */ 1175 if (sum > (SCHED_SLP_RUN_MAX / 5) * 6) { 1176 ts->skg_runtime /= 2; 1177 ts->skg_slptime /= 2; 1178 return; 1179 } 1180 ts->skg_runtime = (ts->skg_runtime / 5) * 4; 1181 ts->skg_slptime = (ts->skg_slptime / 5) * 4; 1182 } 1183 1184 static void 1185 sched_interact_fork(struct thread *td) 1186 { 1187 int ratio; 1188 int sum; 1189 1190 sum = td->td_sched->skg_runtime + td->td_sched->skg_slptime; 1191 if (sum > SCHED_SLP_RUN_FORK) { 1192 ratio = sum / SCHED_SLP_RUN_FORK; 1193 td->td_sched->skg_runtime /= ratio; 1194 td->td_sched->skg_slptime /= ratio; 1195 } 1196 } 1197 1198 static int 1199 sched_interact_score(struct thread *td) 1200 { 1201 int div; 1202 1203 if (td->td_sched->skg_runtime > td->td_sched->skg_slptime) { 1204 div = max(1, td->td_sched->skg_runtime / SCHED_INTERACT_HALF); 1205 return (SCHED_INTERACT_HALF + 1206 (SCHED_INTERACT_HALF - (td->td_sched->skg_slptime / div))); 1207 } if (td->td_sched->skg_slptime > td->td_sched->skg_runtime) { 1208 div = max(1, td->td_sched->skg_slptime / SCHED_INTERACT_HALF); 1209 return (td->td_sched->skg_runtime / div); 1210 } 1211 1212 /* 1213 * This can happen if slptime and runtime are 0. 1214 */ 1215 return (0); 1216 1217 } 1218 1219 /* 1220 * Called from proc0_init() to bootstrap the scheduler. 1221 */ 1222 void 1223 schedinit(void) 1224 { 1225 1226 /* 1227 * Set up the scheduler specific parts of proc0. 1228 */ 1229 proc0.p_sched = NULL; /* XXX */ 1230 thread0.td_sched = &td_sched0; 1231 td_sched0.ts_ltick = ticks; 1232 td_sched0.ts_ftick = ticks; 1233 td_sched0.ts_thread = &thread0; 1234 td_sched0.ts_state = TSS_THREAD; 1235 } 1236 1237 /* 1238 * This is only somewhat accurate since given many processes of the same 1239 * priority they will switch when their slices run out, which will be 1240 * at most sched_slice stathz ticks. 1241 */ 1242 int 1243 sched_rr_interval(void) 1244 { 1245 1246 /* Convert sched_slice to hz */ 1247 return (hz/(realstathz/sched_slice)); 1248 } 1249 1250 static void 1251 sched_pctcpu_update(struct td_sched *ts) 1252 { 1253 1254 if (ts->ts_ticks == 0) 1255 return; 1256 if (ticks - (hz / 10) < ts->ts_ltick && 1257 SCHED_TICK_TOTAL(ts) < SCHED_TICK_MAX) 1258 return; 1259 /* 1260 * Adjust counters and watermark for pctcpu calc. 1261 */ 1262 if (ts->ts_ltick > ticks - SCHED_TICK_TARG) 1263 ts->ts_ticks = (ts->ts_ticks / (ticks - ts->ts_ftick)) * 1264 SCHED_TICK_TARG; 1265 else 1266 ts->ts_ticks = 0; 1267 ts->ts_ltick = ticks; 1268 ts->ts_ftick = ts->ts_ltick - SCHED_TICK_TARG; 1269 } 1270 1271 static void 1272 sched_thread_priority(struct thread *td, u_char prio) 1273 { 1274 struct td_sched *ts; 1275 1276 CTR6(KTR_SCHED, "sched_prio: %p(%s) prio %d newprio %d by %p(%s)", 1277 td, td->td_proc->p_comm, td->td_priority, prio, curthread, 1278 curthread->td_proc->p_comm); 1279 ts = td->td_sched; 1280 mtx_assert(&sched_lock, MA_OWNED); 1281 if (td->td_priority == prio) 1282 return; 1283 1284 if (TD_ON_RUNQ(td) && prio < td->td_priority) { 1285 /* 1286 * If the priority has been elevated due to priority 1287 * propagation, we may have to move ourselves to a new 1288 * queue. This could be optimized to not re-add in some 1289 * cases. 1290 */ 1291 sched_rem(td); 1292 td->td_priority = prio; 1293 sched_add(td, SRQ_BORROWING); 1294 } else 1295 td->td_priority = prio; 1296 } 1297 1298 /* 1299 * Update a thread's priority when it is lent another thread's 1300 * priority. 1301 */ 1302 void 1303 sched_lend_prio(struct thread *td, u_char prio) 1304 { 1305 1306 td->td_flags |= TDF_BORROWING; 1307 sched_thread_priority(td, prio); 1308 } 1309 1310 /* 1311 * Restore a thread's priority when priority propagation is 1312 * over. The prio argument is the minimum priority the thread 1313 * needs to have to satisfy other possible priority lending 1314 * requests. If the thread's regular priority is less 1315 * important than prio, the thread will keep a priority boost 1316 * of prio. 1317 */ 1318 void 1319 sched_unlend_prio(struct thread *td, u_char prio) 1320 { 1321 u_char base_pri; 1322 1323 if (td->td_base_pri >= PRI_MIN_TIMESHARE && 1324 td->td_base_pri <= PRI_MAX_TIMESHARE) 1325 base_pri = td->td_user_pri; 1326 else 1327 base_pri = td->td_base_pri; 1328 if (prio >= base_pri) { 1329 td->td_flags &= ~TDF_BORROWING; 1330 sched_thread_priority(td, base_pri); 1331 } else 1332 sched_lend_prio(td, prio); 1333 } 1334 1335 void 1336 sched_prio(struct thread *td, u_char prio) 1337 { 1338 u_char oldprio; 1339 1340 /* First, update the base priority. */ 1341 td->td_base_pri = prio; 1342 1343 /* 1344 * If the thread is borrowing another thread's priority, don't 1345 * ever lower the priority. 1346 */ 1347 if (td->td_flags & TDF_BORROWING && td->td_priority < prio) 1348 return; 1349 1350 /* Change the real priority. */ 1351 oldprio = td->td_priority; 1352 sched_thread_priority(td, prio); 1353 1354 /* 1355 * If the thread is on a turnstile, then let the turnstile update 1356 * its state. 1357 */ 1358 if (TD_ON_LOCK(td) && oldprio != prio) 1359 turnstile_adjust(td, oldprio); 1360 } 1361 1362 void 1363 sched_user_prio(struct thread *td, u_char prio) 1364 { 1365 u_char oldprio; 1366 1367 td->td_base_user_pri = prio; 1368 if (td->td_flags & TDF_UBORROWING && td->td_user_pri <= prio) 1369 return; 1370 oldprio = td->td_user_pri; 1371 td->td_user_pri = prio; 1372 1373 if (TD_ON_UPILOCK(td) && oldprio != prio) 1374 umtx_pi_adjust(td, oldprio); 1375 } 1376 1377 void 1378 sched_lend_user_prio(struct thread *td, u_char prio) 1379 { 1380 u_char oldprio; 1381 1382 td->td_flags |= TDF_UBORROWING; 1383 1384 oldprio = td->td_user_pri; 1385 td->td_user_pri = prio; 1386 1387 if (TD_ON_UPILOCK(td) && oldprio != prio) 1388 umtx_pi_adjust(td, oldprio); 1389 } 1390 1391 void 1392 sched_unlend_user_prio(struct thread *td, u_char prio) 1393 { 1394 u_char base_pri; 1395 1396 base_pri = td->td_base_user_pri; 1397 if (prio >= base_pri) { 1398 td->td_flags &= ~TDF_UBORROWING; 1399 sched_user_prio(td, base_pri); 1400 } else 1401 sched_lend_user_prio(td, prio); 1402 } 1403 1404 void 1405 sched_switch(struct thread *td, struct thread *newtd, int flags) 1406 { 1407 struct tdq *tdq; 1408 struct td_sched *ts; 1409 int preempt; 1410 1411 mtx_assert(&sched_lock, MA_OWNED); 1412 1413 preempt = flags & SW_PREEMPT; 1414 tdq = TDQ_SELF(); 1415 ts = td->td_sched; 1416 td->td_lastcpu = td->td_oncpu; 1417 td->td_oncpu = NOCPU; 1418 td->td_flags &= ~TDF_NEEDRESCHED; 1419 td->td_owepreempt = 0; 1420 /* 1421 * If the thread has been assigned it may be in the process of switching 1422 * to the new cpu. This is the case in sched_bind(). 1423 */ 1424 if (td == PCPU_GET(idlethread)) { 1425 TD_SET_CAN_RUN(td); 1426 } else { 1427 tdq_load_rem(tdq, ts); 1428 if (TD_IS_RUNNING(td)) { 1429 /* 1430 * Don't allow the thread to migrate 1431 * from a preemption. 1432 */ 1433 if (preempt) 1434 sched_pin_td(td); 1435 setrunqueue(td, preempt ? 1436 SRQ_OURSELF|SRQ_YIELDING|SRQ_PREEMPTED : 1437 SRQ_OURSELF|SRQ_YIELDING); 1438 if (preempt) 1439 sched_unpin_td(td); 1440 } 1441 } 1442 if (newtd != NULL) { 1443 /* 1444 * If we bring in a thread account for it as if it had been 1445 * added to the run queue and then chosen. 1446 */ 1447 newtd->td_sched->ts_flags |= TSF_DIDRUN; 1448 TD_SET_RUNNING(newtd); 1449 tdq_load_add(TDQ_SELF(), newtd->td_sched); 1450 } else 1451 newtd = choosethread(); 1452 if (td != newtd) { 1453 #ifdef HWPMC_HOOKS 1454 if (PMC_PROC_IS_USING_PMCS(td->td_proc)) 1455 PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_OUT); 1456 #endif 1457 1458 cpu_switch(td, newtd); 1459 #ifdef HWPMC_HOOKS 1460 if (PMC_PROC_IS_USING_PMCS(td->td_proc)) 1461 PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_IN); 1462 #endif 1463 } 1464 sched_lock.mtx_lock = (uintptr_t)td; 1465 td->td_oncpu = PCPU_GET(cpuid); 1466 } 1467 1468 void 1469 sched_nice(struct proc *p, int nice) 1470 { 1471 struct thread *td; 1472 1473 PROC_LOCK_ASSERT(p, MA_OWNED); 1474 mtx_assert(&sched_lock, MA_OWNED); 1475 1476 p->p_nice = nice; 1477 FOREACH_THREAD_IN_PROC(p, td) { 1478 sched_priority(td); 1479 sched_prio(td, td->td_base_user_pri); 1480 } 1481 } 1482 1483 void 1484 sched_sleep(struct thread *td) 1485 { 1486 1487 mtx_assert(&sched_lock, MA_OWNED); 1488 1489 td->td_sched->ts_slptime = ticks; 1490 } 1491 1492 void 1493 sched_wakeup(struct thread *td) 1494 { 1495 int slptime; 1496 1497 mtx_assert(&sched_lock, MA_OWNED); 1498 1499 /* 1500 * If we slept for more than a tick update our interactivity and 1501 * priority. 1502 */ 1503 slptime = td->td_sched->ts_slptime; 1504 td->td_sched->ts_slptime = 0; 1505 if (slptime && slptime != ticks) { 1506 int hzticks; 1507 1508 hzticks = (ticks - slptime) << SCHED_TICK_SHIFT; 1509 td->td_sched->skg_slptime += hzticks; 1510 sched_interact_update(td); 1511 sched_pctcpu_update(td->td_sched); 1512 sched_priority(td); 1513 } 1514 setrunqueue(td, SRQ_BORING); 1515 } 1516 1517 /* 1518 * Penalize the parent for creating a new child and initialize the child's 1519 * priority. 1520 */ 1521 void 1522 sched_fork(struct thread *td, struct thread *child) 1523 { 1524 mtx_assert(&sched_lock, MA_OWNED); 1525 sched_fork_thread(td, child); 1526 /* 1527 * Penalize the parent and child for forking. 1528 */ 1529 sched_interact_fork(child); 1530 sched_priority(child); 1531 td->td_sched->skg_runtime += tickincr; 1532 sched_interact_update(td); 1533 sched_priority(td); 1534 } 1535 1536 void 1537 sched_fork_thread(struct thread *td, struct thread *child) 1538 { 1539 struct td_sched *ts; 1540 struct td_sched *ts2; 1541 1542 /* 1543 * Initialize child. 1544 */ 1545 sched_newthread(child); 1546 ts = td->td_sched; 1547 ts2 = child->td_sched; 1548 ts2->ts_cpu = ts->ts_cpu; 1549 ts2->ts_runq = NULL; 1550 /* 1551 * Grab our parents cpu estimation information and priority. 1552 */ 1553 ts2->ts_ticks = ts->ts_ticks; 1554 ts2->ts_ltick = ts->ts_ltick; 1555 ts2->ts_ftick = ts->ts_ftick; 1556 child->td_user_pri = td->td_user_pri; 1557 child->td_base_user_pri = td->td_base_user_pri; 1558 /* 1559 * And update interactivity score. 1560 */ 1561 ts2->skg_slptime = ts->skg_slptime; 1562 ts2->skg_runtime = ts->skg_runtime; 1563 ts2->ts_slice = 1; /* Attempt to quickly learn interactivity. */ 1564 } 1565 1566 void 1567 sched_class(struct thread *td, int class) 1568 { 1569 1570 mtx_assert(&sched_lock, MA_OWNED); 1571 if (td->td_pri_class == class) 1572 return; 1573 1574 #ifdef SMP 1575 /* 1576 * On SMP if we're on the RUNQ we must adjust the transferable 1577 * count because could be changing to or from an interrupt 1578 * class. 1579 */ 1580 if (td->td_sched->ts_state == TSS_ONRUNQ) { 1581 struct tdq *tdq; 1582 1583 tdq = TDQ_CPU(td->td_sched->ts_cpu); 1584 if (THREAD_CAN_MIGRATE(td)) { 1585 tdq->tdq_transferable--; 1586 tdq->tdq_group->tdg_transferable--; 1587 } 1588 td->td_pri_class = class; 1589 if (THREAD_CAN_MIGRATE(td)) { 1590 tdq->tdq_transferable++; 1591 tdq->tdq_group->tdg_transferable++; 1592 } 1593 } 1594 #endif 1595 td->td_pri_class = class; 1596 } 1597 1598 /* 1599 * Return some of the child's priority and interactivity to the parent. 1600 */ 1601 void 1602 sched_exit(struct proc *p, struct thread *child) 1603 { 1604 struct thread *td; 1605 1606 CTR3(KTR_SCHED, "sched_exit: %p(%s) prio %d", 1607 child, child->td_proc->p_comm, child->td_priority); 1608 1609 td = FIRST_THREAD_IN_PROC(p); 1610 sched_exit_thread(td, child); 1611 } 1612 1613 void 1614 sched_exit_thread(struct thread *td, struct thread *child) 1615 { 1616 1617 CTR3(KTR_SCHED, "sched_exit_thread: %p(%s) prio %d", 1618 child, child->td_proc->p_comm, child->td_priority); 1619 1620 tdq_load_rem(TDQ_CPU(child->td_sched->ts_cpu), child->td_sched); 1621 #ifdef KSE 1622 /* 1623 * KSE forks and exits so often that this penalty causes short-lived 1624 * threads to always be non-interactive. This causes mozilla to 1625 * crawl under load. 1626 */ 1627 if ((td->td_pflags & TDP_SA) && td->td_proc == child->td_proc) 1628 return; 1629 #endif 1630 /* 1631 * Give the child's runtime to the parent without returning the 1632 * sleep time as a penalty to the parent. This causes shells that 1633 * launch expensive things to mark their children as expensive. 1634 */ 1635 td->td_sched->skg_runtime += child->td_sched->skg_runtime; 1636 sched_interact_update(td); 1637 sched_priority(td); 1638 } 1639 1640 void 1641 sched_userret(struct thread *td) 1642 { 1643 /* 1644 * XXX we cheat slightly on the locking here to avoid locking in 1645 * the usual case. Setting td_priority here is essentially an 1646 * incomplete workaround for not setting it properly elsewhere. 1647 * Now that some interrupt handlers are threads, not setting it 1648 * properly elsewhere can clobber it in the window between setting 1649 * it here and returning to user mode, so don't waste time setting 1650 * it perfectly here. 1651 */ 1652 KASSERT((td->td_flags & TDF_BORROWING) == 0, 1653 ("thread with borrowed priority returning to userland")); 1654 if (td->td_priority != td->td_user_pri) { 1655 mtx_lock_spin(&sched_lock); 1656 td->td_priority = td->td_user_pri; 1657 td->td_base_pri = td->td_user_pri; 1658 mtx_unlock_spin(&sched_lock); 1659 } 1660 } 1661 1662 void 1663 sched_clock(struct thread *td) 1664 { 1665 struct tdq *tdq; 1666 struct td_sched *ts; 1667 1668 mtx_assert(&sched_lock, MA_OWNED); 1669 #ifdef SMP 1670 sched_smp_tick(td); 1671 #endif 1672 tdq = TDQ_SELF(); 1673 /* 1674 * Advance the insert index once for each tick to ensure that all 1675 * threads get a chance to run. 1676 */ 1677 if (tdq->tdq_idx == tdq->tdq_ridx) { 1678 tdq->tdq_idx = (tdq->tdq_idx + 1) % RQ_NQS; 1679 if (TAILQ_EMPTY(&tdq->tdq_timeshare.rq_queues[tdq->tdq_ridx])) 1680 tdq->tdq_ridx = tdq->tdq_idx; 1681 } 1682 /* Adjust ticks for pctcpu */ 1683 ts = td->td_sched; 1684 ts->ts_ticks += tickincr; 1685 ts->ts_ltick = ticks; 1686 /* 1687 * Update if we've exceeded our desired tick threshhold by over one 1688 * second. 1689 */ 1690 if (ts->ts_ftick + SCHED_TICK_MAX < ts->ts_ltick) 1691 sched_pctcpu_update(ts); 1692 /* 1693 * We only do slicing code for TIMESHARE threads. 1694 */ 1695 if (td->td_pri_class != PRI_TIMESHARE) 1696 return; 1697 /* 1698 * We used a tick; charge it to the thread so that we can compute our 1699 * interactivity. 1700 */ 1701 td->td_sched->skg_runtime += tickincr; 1702 sched_interact_update(td); 1703 /* 1704 * We used up one time slice. 1705 */ 1706 if (--ts->ts_slice > 0) 1707 return; 1708 /* 1709 * We're out of time, recompute priorities and requeue. 1710 */ 1711 sched_priority(td); 1712 td->td_flags |= TDF_NEEDRESCHED; 1713 } 1714 1715 int 1716 sched_runnable(void) 1717 { 1718 struct tdq *tdq; 1719 int load; 1720 1721 load = 1; 1722 1723 tdq = TDQ_SELF(); 1724 #ifdef SMP 1725 if (tdq_busy) 1726 goto out; 1727 #endif 1728 if ((curthread->td_flags & TDF_IDLETD) != 0) { 1729 if (tdq->tdq_load > 0) 1730 goto out; 1731 } else 1732 if (tdq->tdq_load - 1 > 0) 1733 goto out; 1734 load = 0; 1735 out: 1736 return (load); 1737 } 1738 1739 struct td_sched * 1740 sched_choose(void) 1741 { 1742 struct tdq *tdq; 1743 struct td_sched *ts; 1744 1745 mtx_assert(&sched_lock, MA_OWNED); 1746 tdq = TDQ_SELF(); 1747 #ifdef SMP 1748 restart: 1749 #endif 1750 ts = tdq_choose(tdq); 1751 if (ts) { 1752 #ifdef SMP 1753 if (ts->ts_thread->td_priority > PRI_MIN_IDLE) 1754 if (tdq_idled(tdq) == 0) 1755 goto restart; 1756 #endif 1757 tdq_runq_rem(tdq, ts); 1758 ts->ts_state = TSS_THREAD; 1759 return (ts); 1760 } 1761 #ifdef SMP 1762 if (tdq_idled(tdq) == 0) 1763 goto restart; 1764 #endif 1765 return (NULL); 1766 } 1767 1768 void 1769 sched_add(struct thread *td, int flags) 1770 { 1771 struct tdq *tdq; 1772 struct td_sched *ts; 1773 int preemptive; 1774 int class; 1775 #ifdef SMP 1776 int cpuid; 1777 int cpumask; 1778 #endif 1779 1780 CTR5(KTR_SCHED, "sched_add: %p(%s) prio %d by %p(%s)", 1781 td, td->td_proc->p_comm, td->td_priority, curthread, 1782 curthread->td_proc->p_comm); 1783 mtx_assert(&sched_lock, MA_OWNED); 1784 tdq = TDQ_SELF(); 1785 ts = td->td_sched; 1786 class = PRI_BASE(td->td_pri_class); 1787 preemptive = !(flags & SRQ_YIELDING); 1788 KASSERT(ts->ts_state != TSS_ONRUNQ, 1789 ("sched_add: thread %p (%s) already in run queue", td, 1790 td->td_proc->p_comm)); 1791 KASSERT(td->td_proc->p_sflag & PS_INMEM, 1792 ("sched_add: process swapped out")); 1793 KASSERT(ts->ts_runq == NULL, 1794 ("sched_add: thread %p is still assigned to a run queue", td)); 1795 /* 1796 * Recalculate the priority before we select the target cpu or 1797 * run-queue. 1798 */ 1799 if (class == PRI_TIMESHARE) 1800 sched_priority(td); 1801 #ifdef SMP 1802 cpuid = PCPU_GET(cpuid); 1803 /* 1804 * Pick the destination cpu and if it isn't ours transfer to the 1805 * target cpu. 1806 */ 1807 if (THREAD_CAN_MIGRATE(td)) { 1808 if (td->td_priority <= PRI_MAX_ITHD) { 1809 CTR2(KTR_SCHED, "ithd %d < %d", td->td_priority, PRI_MAX_ITHD); 1810 ts->ts_cpu = cpuid; 1811 } 1812 if (pick_pri) 1813 ts->ts_cpu = tdq_pickpri(tdq, ts, flags); 1814 else 1815 ts->ts_cpu = tdq_pickidle(tdq, ts); 1816 } else 1817 CTR1(KTR_SCHED, "pinned %d", td->td_pinned); 1818 if (ts->ts_cpu != cpuid) 1819 preemptive = 0; 1820 tdq = TDQ_CPU(ts->ts_cpu); 1821 cpumask = 1 << ts->ts_cpu; 1822 /* 1823 * If we had been idle, clear our bit in the group and potentially 1824 * the global bitmap. 1825 */ 1826 if ((class != PRI_IDLE && class != PRI_ITHD) && 1827 (tdq->tdq_group->tdg_idlemask & cpumask) != 0) { 1828 /* 1829 * Check to see if our group is unidling, and if so, remove it 1830 * from the global idle mask. 1831 */ 1832 if (tdq->tdq_group->tdg_idlemask == 1833 tdq->tdq_group->tdg_cpumask) 1834 atomic_clear_int(&tdq_idle, tdq->tdq_group->tdg_mask); 1835 /* 1836 * Now remove ourselves from the group specific idle mask. 1837 */ 1838 tdq->tdq_group->tdg_idlemask &= ~cpumask; 1839 } 1840 #endif 1841 /* 1842 * Set the slice and pick the run queue. 1843 */ 1844 if (ts->ts_slice == 0) 1845 ts->ts_slice = sched_slice; 1846 if (td->td_priority <= PRI_MAX_REALTIME) 1847 ts->ts_runq = &tdq->tdq_realtime; 1848 else if (td->td_priority <= PRI_MAX_TIMESHARE) 1849 ts->ts_runq = &tdq->tdq_timeshare; 1850 else 1851 ts->ts_runq = &tdq->tdq_idle; 1852 if (preemptive && maybe_preempt(td)) 1853 return; 1854 ts->ts_state = TSS_ONRUNQ; 1855 1856 tdq_runq_add(tdq, ts, flags); 1857 tdq_load_add(tdq, ts); 1858 #ifdef SMP 1859 if (ts->ts_cpu != cpuid) { 1860 tdq_notify(ts); 1861 return; 1862 } 1863 #endif 1864 if (td->td_priority < curthread->td_priority) 1865 curthread->td_flags |= TDF_NEEDRESCHED; 1866 } 1867 1868 void 1869 sched_rem(struct thread *td) 1870 { 1871 struct tdq *tdq; 1872 struct td_sched *ts; 1873 1874 CTR5(KTR_SCHED, "sched_rem: %p(%s) prio %d by %p(%s)", 1875 td, td->td_proc->p_comm, td->td_priority, curthread, 1876 curthread->td_proc->p_comm); 1877 mtx_assert(&sched_lock, MA_OWNED); 1878 ts = td->td_sched; 1879 KASSERT((ts->ts_state == TSS_ONRUNQ), 1880 ("sched_rem: thread not on run queue")); 1881 1882 ts->ts_state = TSS_THREAD; 1883 tdq = TDQ_CPU(ts->ts_cpu); 1884 tdq_runq_rem(tdq, ts); 1885 tdq_load_rem(tdq, ts); 1886 } 1887 1888 fixpt_t 1889 sched_pctcpu(struct thread *td) 1890 { 1891 fixpt_t pctcpu; 1892 struct td_sched *ts; 1893 1894 pctcpu = 0; 1895 ts = td->td_sched; 1896 if (ts == NULL) 1897 return (0); 1898 1899 mtx_lock_spin(&sched_lock); 1900 if (ts->ts_ticks) { 1901 int rtick; 1902 1903 sched_pctcpu_update(ts); 1904 /* How many rtick per second ? */ 1905 rtick = min(SCHED_TICK_HZ(ts) / SCHED_TICK_SECS, hz); 1906 pctcpu = (FSCALE * ((FSCALE * rtick)/hz)) >> FSHIFT; 1907 } 1908 td->td_proc->p_swtime = ts->ts_ltick - ts->ts_ftick; 1909 mtx_unlock_spin(&sched_lock); 1910 1911 return (pctcpu); 1912 } 1913 1914 void 1915 sched_bind(struct thread *td, int cpu) 1916 { 1917 struct td_sched *ts; 1918 1919 mtx_assert(&sched_lock, MA_OWNED); 1920 ts = td->td_sched; 1921 if (ts->ts_flags & TSF_BOUND) 1922 sched_unbind(td); 1923 ts->ts_flags |= TSF_BOUND; 1924 #ifdef SMP 1925 sched_pin(); 1926 if (PCPU_GET(cpuid) == cpu) 1927 return; 1928 ts->ts_cpu = cpu; 1929 ts->ts_state = TSS_THREAD; 1930 /* When we return from mi_switch we'll be on the correct cpu. */ 1931 mi_switch(SW_VOL, NULL); 1932 #endif 1933 } 1934 1935 void 1936 sched_unbind(struct thread *td) 1937 { 1938 struct td_sched *ts; 1939 1940 mtx_assert(&sched_lock, MA_OWNED); 1941 ts = td->td_sched; 1942 if ((ts->ts_flags & TSF_BOUND) == 0) 1943 return; 1944 ts->ts_flags &= ~TSF_BOUND; 1945 #ifdef SMP 1946 sched_unpin(); 1947 #endif 1948 } 1949 1950 int 1951 sched_is_bound(struct thread *td) 1952 { 1953 mtx_assert(&sched_lock, MA_OWNED); 1954 return (td->td_sched->ts_flags & TSF_BOUND); 1955 } 1956 1957 void 1958 sched_relinquish(struct thread *td) 1959 { 1960 mtx_lock_spin(&sched_lock); 1961 if (td->td_pri_class == PRI_TIMESHARE) 1962 sched_prio(td, PRI_MAX_TIMESHARE); 1963 mi_switch(SW_VOL, NULL); 1964 mtx_unlock_spin(&sched_lock); 1965 } 1966 1967 int 1968 sched_load(void) 1969 { 1970 #ifdef SMP 1971 int total; 1972 int i; 1973 1974 total = 0; 1975 for (i = 0; i <= tdg_maxid; i++) 1976 total += TDQ_GROUP(i)->tdg_load; 1977 return (total); 1978 #else 1979 return (TDQ_SELF()->tdq_sysload); 1980 #endif 1981 } 1982 1983 int 1984 sched_sizeof_proc(void) 1985 { 1986 return (sizeof(struct proc)); 1987 } 1988 1989 int 1990 sched_sizeof_thread(void) 1991 { 1992 return (sizeof(struct thread) + sizeof(struct td_sched)); 1993 } 1994 1995 void 1996 sched_tick(void) 1997 { 1998 } 1999 2000 static SYSCTL_NODE(_kern, OID_AUTO, sched, CTLFLAG_RW, 0, "Scheduler"); 2001 SYSCTL_STRING(_kern_sched, OID_AUTO, name, CTLFLAG_RD, "ule", 0, 2002 "Scheduler name"); 2003 SYSCTL_INT(_kern_sched, OID_AUTO, slice, CTLFLAG_RW, &sched_slice, 0, ""); 2004 SYSCTL_INT(_kern_sched, OID_AUTO, interact, CTLFLAG_RW, &sched_interact, 0, ""); 2005 SYSCTL_INT(_kern_sched, OID_AUTO, tickincr, CTLFLAG_RD, &tickincr, 0, ""); 2006 SYSCTL_INT(_kern_sched, OID_AUTO, realstathz, CTLFLAG_RD, &realstathz, 0, ""); 2007 #ifdef SMP 2008 SYSCTL_INT(_kern_sched, OID_AUTO, pick_pri, CTLFLAG_RW, &pick_pri, 0, ""); 2009 SYSCTL_INT(_kern_sched, OID_AUTO, pick_pri_affinity, CTLFLAG_RW, 2010 &affinity, 0, ""); 2011 SYSCTL_INT(_kern_sched, OID_AUTO, pick_pri_tryself, CTLFLAG_RW, 2012 &tryself, 0, ""); 2013 SYSCTL_INT(_kern_sched, OID_AUTO, pick_pri_tryselfidle, CTLFLAG_RW, 2014 &tryselfidle, 0, ""); 2015 SYSCTL_INT(_kern_sched, OID_AUTO, balance, CTLFLAG_RW, &rebalance, 0, ""); 2016 SYSCTL_INT(_kern_sched, OID_AUTO, ipi_preempt, CTLFLAG_RW, &ipi_preempt, 0, ""); 2017 SYSCTL_INT(_kern_sched, OID_AUTO, ipi_ast, CTLFLAG_RW, &ipi_ast, 0, ""); 2018 SYSCTL_INT(_kern_sched, OID_AUTO, ipi_thresh, CTLFLAG_RW, &ipi_thresh, 0, ""); 2019 SYSCTL_INT(_kern_sched, OID_AUTO, steal_htt, CTLFLAG_RW, &steal_htt, 0, ""); 2020 SYSCTL_INT(_kern_sched, OID_AUTO, steal_busy, CTLFLAG_RW, &steal_busy, 0, ""); 2021 SYSCTL_INT(_kern_sched, OID_AUTO, busy_thresh, CTLFLAG_RW, &busy_thresh, 0, ""); 2022 #endif 2023 2024 /* ps compat */ 2025 static fixpt_t ccpu = 0.95122942450071400909 * FSCALE; /* exp(-1/20) */ 2026 SYSCTL_INT(_kern, OID_AUTO, ccpu, CTLFLAG_RD, &ccpu, 0, ""); 2027 2028 2029 #define KERN_SWITCH_INCLUDE 1 2030 #include "kern/kern_switch.c" 2031