1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1982, 1986, 1990, 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * (c) UNIX System Laboratories, Inc. 7 * All or some portions of this file are derived from material licensed 8 * to the University of California by American Telephone and Telegraph 9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 10 * the permission of UNIX System Laboratories, Inc. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #include <sys/cdefs.h> 38 #include "opt_ktrace.h" 39 #include "opt_sched.h" 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/blockcount.h> 44 #include <sys/condvar.h> 45 #include <sys/kdb.h> 46 #include <sys/kernel.h> 47 #include <sys/ktr.h> 48 #include <sys/ktrace.h> 49 #include <sys/lock.h> 50 #include <sys/mutex.h> 51 #include <sys/proc.h> 52 #include <sys/resourcevar.h> 53 #include <sys/sched.h> 54 #include <sys/sdt.h> 55 #include <sys/signalvar.h> 56 #include <sys/sleepqueue.h> 57 #include <sys/smp.h> 58 #include <sys/sx.h> 59 #include <sys/sysctl.h> 60 #include <sys/sysproto.h> 61 #include <sys/vmmeter.h> 62 #ifdef KTRACE 63 #include <sys/uio.h> 64 #endif 65 #ifdef EPOCH_TRACE 66 #include <sys/epoch.h> 67 #endif 68 69 #include <machine/cpu.h> 70 71 static void synch_setup(void *dummy); 72 SYSINIT(synch_setup, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, synch_setup, 73 NULL); 74 75 int hogticks; 76 static const char pause_wchan[MAXCPU]; 77 78 static struct callout loadav_callout; 79 80 struct loadavg averunnable = 81 { {0, 0, 0}, FSCALE }; /* load average, of runnable procs */ 82 /* 83 * Constants for averages over 1, 5, and 15 minutes 84 * when sampling at 5 second intervals. 85 */ 86 static uint64_t cexp[3] = { 87 0.9200444146293232 * FSCALE, /* exp(-1/12) */ 88 0.9834714538216174 * FSCALE, /* exp(-1/60) */ 89 0.9944598480048967 * FSCALE, /* exp(-1/180) */ 90 }; 91 92 /* kernel uses `FSCALE', userland (SHOULD) use kern.fscale */ 93 SYSCTL_INT(_kern, OID_AUTO, fscale, CTLFLAG_RD, SYSCTL_NULL_INT_PTR, FSCALE, 94 "Fixed-point scale factor used for calculating load average values"); 95 96 static void loadav(void *arg); 97 98 SDT_PROVIDER_DECLARE(sched); 99 SDT_PROBE_DEFINE(sched, , , preempt); 100 101 static void 102 sleepinit(void *unused) 103 { 104 105 hogticks = (hz / 10) * 2; /* Default only. */ 106 init_sleepqueues(); 107 } 108 109 /* 110 * vmem tries to lock the sleepq mutexes when free'ing kva, so make sure 111 * it is available. 112 */ 113 SYSINIT(sleepinit, SI_SUB_KMEM, SI_ORDER_ANY, sleepinit, NULL); 114 115 /* 116 * General sleep call. Suspends the current thread until a wakeup is 117 * performed on the specified identifier. The thread will then be made 118 * runnable with the specified priority. Sleeps at most sbt units of time 119 * (0 means no timeout). If pri includes the PCATCH flag, let signals 120 * interrupt the sleep, otherwise ignore them while sleeping. Returns 0 if 121 * awakened, EWOULDBLOCK if the timeout expires. If PCATCH is set and a 122 * signal becomes pending, ERESTART is returned if the current system 123 * call should be restarted if possible, and EINTR is returned if the system 124 * call should be interrupted by the signal (return EINTR). 125 * 126 * The lock argument is unlocked before the caller is suspended, and 127 * re-locked before _sleep() returns. If priority includes the PDROP 128 * flag the lock is not re-locked before returning. 129 */ 130 int 131 _sleep(const void *ident, struct lock_object *lock, int priority, 132 const char *wmesg, sbintime_t sbt, sbintime_t pr, int flags) 133 { 134 struct thread *td __ktrace_used; 135 struct lock_class *class; 136 struct timespec sw_out_tv __ktrace_used; 137 uintptr_t lock_state; 138 int catch, pri, rval, sleepq_flags; 139 WITNESS_SAVE_DECL(lock_witness); 140 141 TSENTER(); 142 td = curthread; 143 #ifdef KTRACE 144 if (KTRPOINT(td, KTR_CSW)) 145 nanotime(&sw_out_tv); 146 #endif 147 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock, 148 "Sleeping on \"%s\"", wmesg); 149 KASSERT(sbt != 0 || mtx_owned(&Giant) || lock != NULL || 150 (priority & PNOLOCK) != 0, 151 ("sleeping without a lock")); 152 KASSERT(ident != NULL, ("_sleep: NULL ident")); 153 KASSERT(TD_IS_RUNNING(td), ("_sleep: curthread not running")); 154 if (priority & PDROP) 155 KASSERT(lock != NULL && lock != &Giant.lock_object, 156 ("PDROP requires a non-Giant lock")); 157 if (lock != NULL) 158 class = LOCK_CLASS(lock); 159 else 160 class = NULL; 161 162 if (SCHEDULER_STOPPED()) { 163 if (lock != NULL && priority & PDROP) 164 class->lc_unlock(lock); 165 return (0); 166 } 167 catch = priority & PCATCH; 168 pri = priority & PRIMASK; 169 170 KASSERT(!TD_ON_SLEEPQ(td), ("recursive sleep")); 171 172 if ((uintptr_t)ident >= (uintptr_t)&pause_wchan[0] && 173 (uintptr_t)ident <= (uintptr_t)&pause_wchan[MAXCPU - 1]) 174 sleepq_flags = SLEEPQ_PAUSE; 175 else 176 sleepq_flags = SLEEPQ_SLEEP; 177 if (catch) 178 sleepq_flags |= SLEEPQ_INTERRUPTIBLE; 179 180 sleepq_lock(ident); 181 CTR5(KTR_PROC, "sleep: thread %ld (pid %ld, %s) on %s (%p)", 182 td->td_tid, td->td_proc->p_pid, td->td_name, wmesg, ident); 183 184 if (lock == &Giant.lock_object) 185 mtx_assert(&Giant, MA_OWNED); 186 DROP_GIANT(); 187 if (lock != NULL && lock != &Giant.lock_object && 188 !(class->lc_flags & LC_SLEEPABLE)) { 189 KASSERT(!(class->lc_flags & LC_SPINLOCK), 190 ("spin locks can only use msleep_spin")); 191 WITNESS_SAVE(lock, lock_witness); 192 lock_state = class->lc_unlock(lock); 193 } else 194 /* GCC needs to follow the Yellow Brick Road */ 195 lock_state = -1; 196 197 /* 198 * We put ourselves on the sleep queue and start our timeout 199 * before calling thread_suspend_check, as we could stop there, 200 * and a wakeup or a SIGCONT (or both) could occur while we were 201 * stopped without resuming us. Thus, we must be ready for sleep 202 * when cursig() is called. If the wakeup happens while we're 203 * stopped, then td will no longer be on a sleep queue upon 204 * return from cursig(). 205 */ 206 sleepq_add(ident, lock, wmesg, sleepq_flags, 0); 207 if (sbt != 0) 208 sleepq_set_timeout_sbt(ident, sbt, pr, flags); 209 if (lock != NULL && class->lc_flags & LC_SLEEPABLE) { 210 sleepq_release(ident); 211 WITNESS_SAVE(lock, lock_witness); 212 lock_state = class->lc_unlock(lock); 213 sleepq_lock(ident); 214 } 215 if (sbt != 0 && catch) 216 rval = sleepq_timedwait_sig(ident, pri); 217 else if (sbt != 0) 218 rval = sleepq_timedwait(ident, pri); 219 else if (catch) 220 rval = sleepq_wait_sig(ident, pri); 221 else { 222 sleepq_wait(ident, pri); 223 rval = 0; 224 } 225 #ifdef KTRACE 226 if (KTRPOINT(td, KTR_CSW)) { 227 ktrcsw_out(&sw_out_tv, wmesg); 228 ktrcsw(0, 0, wmesg); 229 } 230 #endif 231 PICKUP_GIANT(); 232 if (lock != NULL && lock != &Giant.lock_object && !(priority & PDROP)) { 233 class->lc_lock(lock, lock_state); 234 WITNESS_RESTORE(lock, lock_witness); 235 } 236 TSEXIT(); 237 return (rval); 238 } 239 240 int 241 msleep_spin_sbt(const void *ident, struct mtx *mtx, const char *wmesg, 242 sbintime_t sbt, sbintime_t pr, int flags) 243 { 244 struct thread *td __ktrace_used; 245 struct timespec sw_out_tv __ktrace_used; 246 int rval; 247 WITNESS_SAVE_DECL(mtx); 248 249 td = curthread; 250 KASSERT(mtx != NULL, ("sleeping without a mutex")); 251 KASSERT(ident != NULL, ("msleep_spin_sbt: NULL ident")); 252 KASSERT(TD_IS_RUNNING(td), ("msleep_spin_sbt: curthread not running")); 253 254 if (SCHEDULER_STOPPED()) 255 return (0); 256 257 sleepq_lock(ident); 258 CTR5(KTR_PROC, "msleep_spin: thread %ld (pid %ld, %s) on %s (%p)", 259 td->td_tid, td->td_proc->p_pid, td->td_name, wmesg, ident); 260 261 DROP_GIANT(); 262 mtx_assert(mtx, MA_OWNED | MA_NOTRECURSED); 263 WITNESS_SAVE(&mtx->lock_object, mtx); 264 mtx_unlock_spin(mtx); 265 266 /* 267 * We put ourselves on the sleep queue and start our timeout. 268 */ 269 sleepq_add(ident, &mtx->lock_object, wmesg, SLEEPQ_SLEEP, 0); 270 if (sbt != 0) 271 sleepq_set_timeout_sbt(ident, sbt, pr, flags); 272 273 #ifdef KTRACE 274 if (KTRPOINT(td, KTR_CSW)) 275 nanotime(&sw_out_tv); 276 #endif 277 #ifdef WITNESS 278 sleepq_release(ident); 279 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "Sleeping on \"%s\"", 280 wmesg); 281 sleepq_lock(ident); 282 #endif 283 if (sbt != 0) 284 rval = sleepq_timedwait(ident, 0); 285 else { 286 sleepq_wait(ident, 0); 287 rval = 0; 288 } 289 #ifdef KTRACE 290 if (KTRPOINT(td, KTR_CSW)) { 291 ktrcsw_out(&sw_out_tv, wmesg); 292 ktrcsw(0, 0, wmesg); 293 } 294 #endif 295 PICKUP_GIANT(); 296 mtx_lock_spin(mtx); 297 WITNESS_RESTORE(&mtx->lock_object, mtx); 298 return (rval); 299 } 300 301 /* 302 * pause_sbt() delays the calling thread by the given signed binary 303 * time. During cold bootup, pause_sbt() uses the DELAY() function 304 * instead of the _sleep() function to do the waiting. The "sbt" 305 * argument must be greater than or equal to zero. A "sbt" value of 306 * zero is equivalent to a "sbt" value of one tick. 307 */ 308 int 309 pause_sbt(const char *wmesg, sbintime_t sbt, sbintime_t pr, int flags) 310 { 311 KASSERT(sbt >= 0, ("pause_sbt: timeout must be >= 0")); 312 313 /* silently convert invalid timeouts */ 314 if (sbt == 0) 315 sbt = tick_sbt; 316 317 if ((cold && curthread == &thread0) || kdb_active || 318 SCHEDULER_STOPPED()) { 319 /* 320 * We delay one second at a time to avoid overflowing the 321 * system specific DELAY() function(s): 322 */ 323 while (sbt >= SBT_1S) { 324 DELAY(1000000); 325 sbt -= SBT_1S; 326 } 327 /* Do the delay remainder, if any */ 328 sbt = howmany(sbt, SBT_1US); 329 if (sbt > 0) 330 DELAY(sbt); 331 return (EWOULDBLOCK); 332 } 333 return (_sleep(&pause_wchan[curcpu], NULL, 334 (flags & C_CATCH) ? PCATCH : 0, wmesg, sbt, pr, flags)); 335 } 336 337 /* 338 * Make all threads sleeping on the specified identifier runnable. 339 */ 340 void 341 wakeup(const void *ident) 342 { 343 sleepq_lock(ident); 344 sleepq_broadcast(ident, SLEEPQ_SLEEP, 0, 0); 345 sleepq_release(ident); 346 } 347 348 /* 349 * Make a thread sleeping on the specified identifier runnable. 350 * May wake more than one thread if a target thread is currently 351 * swapped out. 352 */ 353 void 354 wakeup_one(const void *ident) 355 { 356 sleepq_lock(ident); 357 sleepq_signal(ident, SLEEPQ_SLEEP | SLEEPQ_DROP, 0, 0); 358 } 359 360 void 361 wakeup_any(const void *ident) 362 { 363 sleepq_lock(ident); 364 sleepq_signal(ident, SLEEPQ_SLEEP | SLEEPQ_UNFAIR | SLEEPQ_DROP, 0, 0); 365 } 366 367 /* 368 * Signal sleeping waiters after the counter has reached zero. 369 */ 370 void 371 _blockcount_wakeup(blockcount_t *bc, u_int old) 372 { 373 374 KASSERT(_BLOCKCOUNT_WAITERS(old), 375 ("%s: no waiters on %p", __func__, bc)); 376 377 if (atomic_cmpset_int(&bc->__count, _BLOCKCOUNT_WAITERS_FLAG, 0)) 378 wakeup(bc); 379 } 380 381 /* 382 * Wait for a wakeup or a signal. This does not guarantee that the count is 383 * still zero on return. Callers wanting a precise answer should use 384 * blockcount_wait() with an interlock. 385 * 386 * If there is no work to wait for, return 0. If the sleep was interrupted by a 387 * signal, return EINTR or ERESTART, and return EAGAIN otherwise. 388 */ 389 int 390 _blockcount_sleep(blockcount_t *bc, struct lock_object *lock, const char *wmesg, 391 int prio) 392 { 393 void *wchan; 394 uintptr_t lock_state; 395 u_int old; 396 int ret; 397 bool catch, drop; 398 399 KASSERT(lock != &Giant.lock_object, 400 ("%s: cannot use Giant as the interlock", __func__)); 401 402 catch = (prio & PCATCH) != 0; 403 drop = (prio & PDROP) != 0; 404 prio &= PRIMASK; 405 406 /* 407 * Synchronize with the fence in blockcount_release(). If we end up 408 * waiting, the sleepqueue lock acquisition will provide the required 409 * side effects. 410 * 411 * If there is no work to wait for, but waiters are present, try to put 412 * ourselves to sleep to avoid jumping ahead. 413 */ 414 if (atomic_load_acq_int(&bc->__count) == 0) { 415 if (lock != NULL && drop) 416 LOCK_CLASS(lock)->lc_unlock(lock); 417 return (0); 418 } 419 lock_state = 0; 420 wchan = bc; 421 sleepq_lock(wchan); 422 DROP_GIANT(); 423 if (lock != NULL) 424 lock_state = LOCK_CLASS(lock)->lc_unlock(lock); 425 old = blockcount_read(bc); 426 ret = 0; 427 do { 428 if (_BLOCKCOUNT_COUNT(old) == 0) { 429 sleepq_release(wchan); 430 goto out; 431 } 432 if (_BLOCKCOUNT_WAITERS(old)) 433 break; 434 } while (!atomic_fcmpset_int(&bc->__count, &old, 435 old | _BLOCKCOUNT_WAITERS_FLAG)); 436 sleepq_add(wchan, NULL, wmesg, catch ? SLEEPQ_INTERRUPTIBLE : 0, 0); 437 if (catch) 438 ret = sleepq_wait_sig(wchan, prio); 439 else 440 sleepq_wait(wchan, prio); 441 if (ret == 0) 442 ret = EAGAIN; 443 444 out: 445 PICKUP_GIANT(); 446 if (lock != NULL && !drop) 447 LOCK_CLASS(lock)->lc_lock(lock, lock_state); 448 449 return (ret); 450 } 451 452 static void 453 kdb_switch(void) 454 { 455 thread_unlock(curthread); 456 kdb_backtrace(); 457 kdb_reenter(); 458 panic("%s: did not reenter debugger", __func__); 459 } 460 461 /* 462 * mi_switch(9): The machine-independent parts of context switching. 463 * 464 * The thread lock is required on entry and is no longer held on return. 465 */ 466 void 467 mi_switch(int flags) 468 { 469 uint64_t runtime, new_switchtime; 470 struct thread *td; 471 472 td = curthread; /* XXX */ 473 THREAD_LOCK_ASSERT(td, MA_OWNED | MA_NOTRECURSED); 474 KASSERT(!TD_ON_RUNQ(td), ("mi_switch: called by old code")); 475 #ifdef INVARIANTS 476 if (!TD_ON_LOCK(td) && !TD_IS_RUNNING(td)) 477 mtx_assert(&Giant, MA_NOTOWNED); 478 #endif 479 /* thread_lock() performs spinlock_enter(). */ 480 KASSERT(td->td_critnest == 1 || KERNEL_PANICKED(), 481 ("mi_switch: switch in a critical section")); 482 KASSERT((flags & (SW_INVOL | SW_VOL)) != 0, 483 ("mi_switch: switch must be voluntary or involuntary")); 484 KASSERT((flags & SW_TYPE_MASK) != 0, 485 ("mi_switch: a switch reason (type) must be specified")); 486 KASSERT((flags & SW_TYPE_MASK) < SWT_COUNT, 487 ("mi_switch: invalid switch reason %d", (flags & SW_TYPE_MASK))); 488 489 /* 490 * Don't perform context switches from the debugger. 491 */ 492 if (kdb_active) 493 kdb_switch(); 494 if (SCHEDULER_STOPPED()) 495 return; 496 if (flags & SW_VOL) { 497 td->td_ru.ru_nvcsw++; 498 td->td_swvoltick = ticks; 499 } else { 500 td->td_ru.ru_nivcsw++; 501 td->td_swinvoltick = ticks; 502 } 503 #ifdef SCHED_STATS 504 SCHED_STAT_INC(sched_switch_stats[flags & SW_TYPE_MASK]); 505 #endif 506 /* 507 * Compute the amount of time during which the current 508 * thread was running, and add that to its total so far. 509 */ 510 new_switchtime = cpu_ticks(); 511 runtime = new_switchtime - PCPU_GET(switchtime); 512 td->td_runtime += runtime; 513 td->td_incruntime += runtime; 514 PCPU_SET(switchtime, new_switchtime); 515 td->td_generation++; /* bump preempt-detect counter */ 516 VM_CNT_INC(v_swtch); 517 PCPU_SET(switchticks, ticks); 518 CTR4(KTR_PROC, "mi_switch: old thread %ld (td_sched %p, pid %ld, %s)", 519 td->td_tid, td_get_sched(td), td->td_proc->p_pid, td->td_name); 520 #ifdef KDTRACE_HOOKS 521 if (SDT_PROBES_ENABLED() && 522 ((flags & SW_PREEMPT) != 0 || ((flags & SW_INVOL) != 0 && 523 (flags & SW_TYPE_MASK) == SWT_NEEDRESCHED))) 524 SDT_PROBE0(sched, , , preempt); 525 #endif 526 sched_switch(td, flags); 527 CTR4(KTR_PROC, "mi_switch: new thread %ld (td_sched %p, pid %ld, %s)", 528 td->td_tid, td_get_sched(td), td->td_proc->p_pid, td->td_name); 529 530 /* 531 * If the last thread was exiting, finish cleaning it up. 532 */ 533 if ((td = PCPU_GET(deadthread))) { 534 PCPU_SET(deadthread, NULL); 535 thread_stash(td); 536 } 537 spinlock_exit(); 538 } 539 540 /* 541 * Change thread state to be runnable, placing it on the run queue. 542 * 543 * Requires the thread lock on entry, drops on exit. 544 */ 545 void 546 setrunnable(struct thread *td, int srqflags) 547 { 548 THREAD_LOCK_ASSERT(td, MA_OWNED); 549 KASSERT(td->td_proc->p_state != PRS_ZOMBIE, 550 ("setrunnable: pid %d is a zombie", td->td_proc->p_pid)); 551 552 switch (TD_GET_STATE(td)) { 553 case TDS_RUNNING: 554 case TDS_RUNQ: 555 case TDS_INHIBITED: 556 if ((srqflags & (SRQ_HOLD | SRQ_HOLDTD)) == 0) 557 thread_unlock(td); 558 break; 559 case TDS_CAN_RUN: 560 KASSERT((td->td_flags & TDF_INMEM) != 0, 561 ("setrunnable: td %p not in mem, flags 0x%X inhibit 0x%X", 562 td, td->td_flags, td->td_inhibitors)); 563 /* unlocks thread lock according to flags */ 564 sched_wakeup(td, srqflags); 565 break; 566 default: 567 panic("setrunnable: state 0x%x", TD_GET_STATE(td)); 568 } 569 } 570 571 /* 572 * Compute a tenex style load average of a quantity on 573 * 1, 5 and 15 minute intervals. 574 */ 575 static void 576 loadav(void *arg) 577 { 578 int i; 579 uint64_t nrun; 580 struct loadavg *avg; 581 582 nrun = (uint64_t)sched_load(); 583 avg = &averunnable; 584 585 for (i = 0; i < 3; i++) 586 avg->ldavg[i] = (cexp[i] * (uint64_t)avg->ldavg[i] + 587 nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT; 588 589 /* 590 * Schedule the next update to occur after 5 seconds, but add a 591 * random variation to avoid synchronisation with processes that 592 * run at regular intervals. 593 */ 594 callout_reset_sbt(&loadav_callout, 595 SBT_1US * (4000000 + (int)(random() % 2000001)), SBT_1US, 596 loadav, NULL, C_DIRECT_EXEC | C_PREL(32)); 597 } 598 599 void 600 ast_scheduler(struct thread *td, int tda __unused) 601 { 602 #ifdef KTRACE 603 if (KTRPOINT(td, KTR_CSW)) 604 ktrcsw(1, 1, __func__); 605 #endif 606 thread_lock(td); 607 sched_prio(td, td->td_user_pri); 608 mi_switch(SW_INVOL | SWT_NEEDRESCHED); 609 #ifdef KTRACE 610 if (KTRPOINT(td, KTR_CSW)) 611 ktrcsw(0, 1, __func__); 612 #endif 613 } 614 615 static void 616 synch_setup(void *dummy __unused) 617 { 618 callout_init(&loadav_callout, 1); 619 ast_register(TDA_SCHED, ASTR_ASTF_REQUIRED, 0, ast_scheduler); 620 621 /* Kick off timeout driven events by calling first time. */ 622 loadav(NULL); 623 } 624 625 bool 626 should_yield(void) 627 { 628 629 return ((u_int)ticks - (u_int)curthread->td_swvoltick >= hogticks); 630 } 631 632 void 633 maybe_yield(void) 634 { 635 636 if (should_yield()) 637 kern_yield(PRI_USER); 638 } 639 640 void 641 kern_yield(int prio) 642 { 643 struct thread *td; 644 645 td = curthread; 646 DROP_GIANT(); 647 thread_lock(td); 648 if (prio == PRI_USER) 649 prio = td->td_user_pri; 650 if (prio >= 0) 651 sched_prio(td, prio); 652 mi_switch(SW_VOL | SWT_RELINQUISH); 653 PICKUP_GIANT(); 654 } 655 656 /* 657 * General purpose yield system call. 658 */ 659 int 660 sys_yield(struct thread *td, struct yield_args *uap) 661 { 662 663 thread_lock(td); 664 if (PRI_BASE(td->td_pri_class) == PRI_TIMESHARE) 665 sched_prio(td, PRI_MAX_TIMESHARE); 666 mi_switch(SW_VOL | SWT_RELINQUISH); 667 td->td_retval[0] = 0; 668 return (0); 669 } 670 671 int 672 sys_sched_getcpu(struct thread *td, struct sched_getcpu_args *uap) 673 { 674 td->td_retval[0] = td->td_oncpu; 675 return (0); 676 } 677