Deleted Added
sdiff udiff text old ( 82711 ) new ( 83366 )
full compact
1/*-
2 * Copyright (c) 1982, 1986, 1990, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.

--- 22 unchanged lines hidden (view full) ---

31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)kern_synch.c 8.9 (Berkeley) 5/19/95
39 * $FreeBSD: head/sys/kern/kern_synch.c 82711 2001-09-01 03:54:09Z dillon $
40 */
41
42#include "opt_ddb.h"
43#include "opt_ktrace.h"
44
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/condvar.h>

--- 54 unchanged lines hidden (view full) ---

102SYSCTL_PROC(_kern, OID_AUTO, quantum, CTLTYPE_INT|CTLFLAG_RW,
103 0, sizeof sched_quantum, sysctl_kern_quantum, "I", "");
104
105/*
106 * Arrange to reschedule if necessary, taking the priorities and
107 * schedulers into account.
108 */
109void
110maybe_resched(p)
111 struct proc *p;
112{
113
114 mtx_assert(&sched_lock, MA_OWNED);
115 if (p->p_pri.pri_level < curproc->p_pri.pri_level)
116 curproc->p_sflag |= PS_NEEDRESCHED;
117}
118
119int
120roundrobin_interval(void)
121{
122 return (sched_quantum);
123}
124

--- 115 unchanged lines hidden (view full) ---

240 */
241/* ARGSUSED */
242static void
243schedcpu(arg)
244 void *arg;
245{
246 register fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
247 register struct proc *p;
248 register int realstathz;
249
250 realstathz = stathz ? stathz : hz;
251 sx_slock(&allproc_lock);
252 LIST_FOREACH(p, &allproc, p_list) {
253 /*
254 * Increment time in/out of memory and sleep time
255 * (if sleeping). We ignore overflow; with 16-bit int's
256 * (remember them?) overflow takes 45 days.
257 */
258 mtx_lock_spin(&sched_lock);
259 p->p_swtime++;
260 if (p->p_stat == SSLEEP || p->p_stat == SSTOP)
261 p->p_slptime++;
262 p->p_pctcpu = (p->p_pctcpu * ccpu) >> FSHIFT;
263 /*
264 * If the process has slept the entire second,
265 * stop recalculating its priority until it wakes up.
266 */
267 if (p->p_slptime > 1) {
268 mtx_unlock_spin(&sched_lock);
269 continue;
270 }
271
272 /*
273 * p_pctcpu is only for ps.
274 */
275#if (FSHIFT >= CCPU_SHIFT)
276 p->p_pctcpu += (realstathz == 100)?
277 ((fixpt_t) p->p_cpticks) << (FSHIFT - CCPU_SHIFT):
278 100 * (((fixpt_t) p->p_cpticks)
279 << (FSHIFT - CCPU_SHIFT)) / realstathz;
280#else
281 p->p_pctcpu += ((FSCALE - ccpu) *
282 (p->p_cpticks * FSCALE / realstathz)) >> FSHIFT;
283#endif
284 p->p_cpticks = 0;
285 p->p_estcpu = decay_cpu(loadfac, p->p_estcpu);
286 resetpriority(p);
287 if (p->p_pri.pri_level >= PUSER) {
288 if (p->p_oncpu == NOCPU && /* idle */
289 p->p_stat == SRUN &&
290 (p->p_sflag & PS_INMEM) &&
291 (p->p_pri.pri_level / RQ_PPQ) !=
292 (p->p_pri.pri_user / RQ_PPQ)) {
293 remrunqueue(p);
294 p->p_pri.pri_level = p->p_pri.pri_user;
295 setrunqueue(p);
296 } else
297 p->p_pri.pri_level = p->p_pri.pri_user;
298 }
299 mtx_unlock_spin(&sched_lock);
300 }
301 sx_sunlock(&allproc_lock);
302 vmmeter();
303 wakeup((caddr_t)&lbolt);
304 callout_reset(&schedcpu_callout, hz, schedcpu, NULL);
305}
306
307/*
308 * Recalculate the priority of a process after it has slept for a while.
309 * For all load averages >= 1 and max p_estcpu of 255, sleeping for at
310 * least six times the loadfactor will decay p_estcpu to zero.
311 */
312void
313updatepri(p)
314 register struct proc *p;
315{
316 register unsigned int newcpu = p->p_estcpu;
317 register fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
318
319 if (p->p_slptime > 5 * loadfac)
320 p->p_estcpu = 0;
321 else {
322 p->p_slptime--; /* the first time was done in schedcpu */
323 while (newcpu && --p->p_slptime)
324 newcpu = decay_cpu(loadfac, newcpu);
325 p->p_estcpu = newcpu;
326 }
327 resetpriority(p);
328}
329
330/*
331 * We're only looking at 7 bits of the address; everything is
332 * aligned to 4, lots of things are aligned to greater powers
333 * of 2. Shift right by 8, i.e. drop the bottom 256 worth.
334 */
335#define TABLESIZE 128
336static TAILQ_HEAD(slpquehead, proc) slpque[TABLESIZE];
337#define LOOKUP(x) (((intptr_t)(x) >> 8) & (TABLESIZE - 1))
338
339void
340sleepinit(void)
341{
342 int i;
343
344 sched_quantum = hz/10;

--- 20 unchanged lines hidden (view full) ---

365int
366msleep(ident, mtx, priority, wmesg, timo)
367 void *ident;
368 struct mtx *mtx;
369 int priority, timo;
370 const char *wmesg;
371{
372 struct proc *p = curproc;
373 int sig, catch = priority & PCATCH;
374 int rval = 0;
375 WITNESS_SAVE_DECL(mtx);
376
377#ifdef KTRACE
378 if (p && KTRPOINT(p, KTR_CSW))
379 ktrcsw(p->p_tracep, 1, 0);
380#endif

--- 20 unchanged lines hidden (view full) ---

401 mtx_assert(mtx, MA_OWNED | MA_NOTRECURSED);
402 WITNESS_SAVE(&mtx->mtx_object, mtx);
403 mtx_unlock_flags(mtx, MTX_NOSWITCH);
404 if (priority & PDROP)
405 mtx = NULL;
406 }
407
408 KASSERT(p != NULL, ("msleep1"));
409 KASSERT(ident != NULL && p->p_stat == SRUN, ("msleep"));
410
411 p->p_wchan = ident;
412 p->p_wmesg = wmesg;
413 p->p_slptime = 0;
414 p->p_pri.pri_level = priority & PRIMASK;
415 CTR5(KTR_PROC, "msleep: proc %p (pid %d, %s) on %s (%p)", p, p->p_pid,
416 p->p_comm, wmesg, ident);
417 TAILQ_INSERT_TAIL(&slpque[LOOKUP(ident)], p, p_slpq);
418 if (timo)
419 callout_reset(&p->p_slpcallout, timo, endtsleep, p);
420 /*
421 * We put ourselves on the sleep queue and start our timeout
422 * before calling CURSIG, as we could stop there, and a wakeup
423 * or a SIGCONT (or both) could occur while we were stopped.
424 * A SIGCONT would cause us to be marked as SSLEEP
425 * without resuming us, thus we must be ready for sleep
426 * when CURSIG is called. If the wakeup happens while we're
427 * stopped, p->p_wchan will be 0 upon return from CURSIG.
428 */
429 if (catch) {
430 CTR3(KTR_PROC, "msleep caught: proc %p (pid %d, %s)", p,
431 p->p_pid, p->p_comm);
432 p->p_sflag |= PS_SINTR;
433 mtx_unlock_spin(&sched_lock);
434 PROC_LOCK(p);
435 sig = CURSIG(p);
436 mtx_lock_spin(&sched_lock);
437 PROC_UNLOCK_NOSWITCH(p);
438 if (sig != 0) {
439 if (p->p_wchan != NULL)
440 unsleep(p);
441 } else if (p->p_wchan == NULL)
442 catch = 0;
443 } else
444 sig = 0;
445 if (p->p_wchan != NULL) {
446 p->p_stat = SSLEEP;
447 p->p_stats->p_ru.ru_nvcsw++;
448 mi_switch();
449 }
450 CTR3(KTR_PROC, "msleep resume: proc %p (pid %d, %s)", p, p->p_pid,
451 p->p_comm);
452 KASSERT(p->p_stat == SRUN, ("running but not SRUN"));
453 p->p_sflag &= ~PS_SINTR;
454 if (p->p_sflag & PS_TIMEOUT) {
455 p->p_sflag &= ~PS_TIMEOUT;
456 if (sig == 0)
457 rval = EWOULDBLOCK;
458 } else if (p->p_sflag & PS_TIMOFAIL)
459 p->p_sflag &= ~PS_TIMOFAIL;
460 else if (timo && callout_stop(&p->p_slpcallout) == 0) {
461 /*
462 * This isn't supposed to be pretty. If we are here, then
463 * the endtsleep() callout is currently executing on another
464 * CPU and is either spinning on the sched_lock or will be
465 * soon. If we don't synchronize here, there is a chance
466 * that this process may msleep() again before the callout
467 * has a chance to run and the callout may end up waking up
468 * the wrong msleep(). Yuck.
469 */
470 p->p_sflag |= PS_TIMEOUT;
471 p->p_stats->p_ru.ru_nivcsw++;
472 mi_switch();
473 }
474 mtx_unlock_spin(&sched_lock);
475
476 if (rval == 0 && catch) {
477 PROC_LOCK(p);
478 /* XXX: shouldn't we always be calling CURSIG() */

--- 26 unchanged lines hidden (view full) ---

505 * set timeout flag and undo the sleep. If proc
506 * is stopped, just unsleep so it will remain stopped.
507 * MP-safe, called without the Giant mutex.
508 */
509static void
510endtsleep(arg)
511 void *arg;
512{
513 register struct proc *p;
514
515 p = (struct proc *)arg;
516 CTR3(KTR_PROC, "endtsleep: proc %p (pid %d, %s)", p, p->p_pid,
517 p->p_comm);
518 mtx_lock_spin(&sched_lock);
519 /*
520 * This is the other half of the synchronization with msleep()
521 * described above. If the PS_TIMEOUT flag is set, we lost the
522 * race and just need to put the process back on the runqueue.
523 */
524 if ((p->p_sflag & PS_TIMEOUT) != 0) {
525 p->p_sflag &= ~PS_TIMEOUT;
526 setrunqueue(p);
527 } else if (p->p_wchan != NULL) {
528 if (p->p_stat == SSLEEP)
529 setrunnable(p);
530 else
531 unsleep(p);
532 p->p_sflag |= PS_TIMEOUT;
533 } else
534 p->p_sflag |= PS_TIMOFAIL;
535 mtx_unlock_spin(&sched_lock);
536}
537
538/*
539 * Remove a process from its wait queue
540 */
541void
542unsleep(p)
543 register struct proc *p;
544{
545
546 mtx_lock_spin(&sched_lock);
547 if (p->p_wchan != NULL) {
548 TAILQ_REMOVE(&slpque[LOOKUP(p->p_wchan)], p, p_slpq);
549 p->p_wchan = NULL;
550 }
551 mtx_unlock_spin(&sched_lock);
552}
553
554/*
555 * Make all processes sleeping on the specified identifier runnable.
556 */
557void
558wakeup(ident)
559 register void *ident;
560{
561 register struct slpquehead *qp;
562 register struct proc *p;
563
564 mtx_lock_spin(&sched_lock);
565 qp = &slpque[LOOKUP(ident)];
566restart:
567 TAILQ_FOREACH(p, qp, p_slpq) {
568 if (p->p_wchan == ident) {
569 TAILQ_REMOVE(qp, p, p_slpq);
570 p->p_wchan = NULL;
571 if (p->p_stat == SSLEEP) {
572 /* OPTIMIZED EXPANSION OF setrunnable(p); */
573 CTR3(KTR_PROC, "wakeup: proc %p (pid %d, %s)",
574 p, p->p_pid, p->p_comm);
575 if (p->p_slptime > 1)
576 updatepri(p);
577 p->p_slptime = 0;
578 p->p_stat = SRUN;
579 if (p->p_sflag & PS_INMEM) {
580 setrunqueue(p);
581 maybe_resched(p);
582 } else {
583 p->p_sflag |= PS_SWAPINREQ;
584 wakeup((caddr_t)&proc0);
585 }
586 /* END INLINE EXPANSION */
587 goto restart;
588 }
589 }

--- 6 unchanged lines hidden (view full) ---

596 * May wake more than one process if a target process is currently
597 * swapped out.
598 */
599void
600wakeup_one(ident)
601 register void *ident;
602{
603 register struct slpquehead *qp;
604 register struct proc *p;
605
606 mtx_lock_spin(&sched_lock);
607 qp = &slpque[LOOKUP(ident)];
608
609 TAILQ_FOREACH(p, qp, p_slpq) {
610 if (p->p_wchan == ident) {
611 TAILQ_REMOVE(qp, p, p_slpq);
612 p->p_wchan = NULL;
613 if (p->p_stat == SSLEEP) {
614 /* OPTIMIZED EXPANSION OF setrunnable(p); */
615 CTR3(KTR_PROC, "wakeup1: proc %p (pid %d, %s)",
616 p, p->p_pid, p->p_comm);
617 if (p->p_slptime > 1)
618 updatepri(p);
619 p->p_slptime = 0;
620 p->p_stat = SRUN;
621 if (p->p_sflag & PS_INMEM) {
622 setrunqueue(p);
623 maybe_resched(p);
624 break;
625 } else {
626 p->p_sflag |= PS_SWAPINREQ;
627 wakeup((caddr_t)&proc0);
628 }
629 /* END INLINE EXPANSION */
630 }
631 }
632 }
633 mtx_unlock_spin(&sched_lock);
634}
635
636/*
637 * The machine independent parts of mi_switch().
638 */
639void
640mi_switch()
641{
642 struct timeval new_switchtime;
643 register struct proc *p = curproc; /* XXX */
644#if 0
645 register struct rlimit *rlim;
646#endif
647 critical_t sched_crit;
648 u_int sched_nest;
649
650 mtx_assert(&sched_lock, MA_OWNED | MA_NOTRECURSED);
651

--- 60 unchanged lines hidden (view full) ---

712 * Pick a new current process and record its start time.
713 */
714 cnt.v_swtch++;
715 PCPU_SET(switchtime, new_switchtime);
716 CTR3(KTR_PROC, "mi_switch: old proc %p (pid %d, %s)", p, p->p_pid,
717 p->p_comm);
718 sched_crit = sched_lock.mtx_savecrit;
719 sched_nest = sched_lock.mtx_recurse;
720 p->p_lastcpu = p->p_oncpu;
721 p->p_oncpu = NOCPU;
722 p->p_sflag &= ~PS_NEEDRESCHED;
723 cpu_switch();
724 p->p_oncpu = PCPU_GET(cpuid);
725 sched_lock.mtx_savecrit = sched_crit;
726 sched_lock.mtx_recurse = sched_nest;
727 sched_lock.mtx_lock = (uintptr_t)p;
728 CTR3(KTR_PROC, "mi_switch: new proc %p (pid %d, %s)", p, p->p_pid,
729 p->p_comm);
730 if (PCPU_GET(switchtime.tv_sec) == 0)
731 microuptime(PCPU_PTR(switchtime));
732 PCPU_SET(switchticks, ticks);
733}
734
735/*
736 * Change process state to be runnable,
737 * placing it on the run queue if it is in memory,
738 * and awakening the swapper if it isn't in memory.
739 */
740void
741setrunnable(p)
742 register struct proc *p;
743{
744
745 mtx_lock_spin(&sched_lock);
746 switch (p->p_stat) {
747 case 0:
748 case SRUN:
749 case SZOMB:
750 case SWAIT:
751 default:
752 panic("setrunnable");
753 case SSTOP:
754 case SSLEEP: /* e.g. when sending signals */
755 if (p->p_sflag & PS_CVWAITQ)
756 cv_waitq_remove(p);
757 else
758 unsleep(p);
759 break;
760
761 case SIDL:
762 break;
763 }
764 p->p_stat = SRUN;
765 if (p->p_slptime > 1)
766 updatepri(p);
767 p->p_slptime = 0;
768 if ((p->p_sflag & PS_INMEM) == 0) {
769 p->p_sflag |= PS_SWAPINREQ;
770 wakeup((caddr_t)&proc0);
771 } else {
772 setrunqueue(p);
773 maybe_resched(p);
774 }
775 mtx_unlock_spin(&sched_lock);
776}
777
778/*
779 * Compute the priority of a process when running in user mode.
780 * Arrange to reschedule if the resulting priority is better
781 * than that of the current process.
782 */
783void
784resetpriority(p)
785 register struct proc *p;
786{
787 register unsigned int newpriority;
788
789 mtx_lock_spin(&sched_lock);
790 if (p->p_pri.pri_class == PRI_TIMESHARE) {
791 newpriority = PUSER + p->p_estcpu / INVERSE_ESTCPU_WEIGHT +
792 NICE_WEIGHT * (p->p_nice - PRIO_MIN);
793 newpriority = min(max(newpriority, PRI_MIN_TIMESHARE),
794 PRI_MAX_TIMESHARE);
795 p->p_pri.pri_user = newpriority;
796 }
797 maybe_resched(p);
798 mtx_unlock_spin(&sched_lock);
799}
800
801/* ARGSUSED */
802static void
803sched_setup(dummy)
804 void *dummy;
805{

--- 16 unchanged lines hidden (view full) ---

822 * quite quickly when the process is running (linearly), and decays
823 * away exponentially, at a rate which is proportionally slower when
824 * the system is busy. The basic principle is that the system will
825 * 90% forget that the process used a lot of CPU time in 5 * loadav
826 * seconds. This causes the system to favor processes which haven't
827 * run much recently, and to round-robin among other processes.
828 */
829void
830schedclock(p)
831 struct proc *p;
832{
833
834 p->p_cpticks++;
835 p->p_estcpu = ESTCPULIM(p->p_estcpu + 1);
836 if ((p->p_estcpu % INVERSE_ESTCPU_WEIGHT) == 0) {
837 resetpriority(p);
838 if (p->p_pri.pri_level >= PUSER)
839 p->p_pri.pri_level = p->p_pri.pri_user;
840 }
841}
842
843/*
844 * General purpose yield system call
845 */
846int
847yield(struct proc *p, struct yield_args *uap)
848{
849 p->p_retval[0] = 0;
850
851 mtx_lock_spin(&sched_lock);
852 mtx_assert(&Giant, MA_NOTOWNED);
853#if 0
854 DROP_GIANT_NOSWITCH();
855#endif
856 p->p_pri.pri_level = PRI_MAX_TIMESHARE;
857 setrunqueue(p);
858 p->p_stats->p_ru.ru_nvcsw++;
859 mi_switch();
860 mtx_unlock_spin(&sched_lock);
861#if 0
862 PICKUP_GIANT();
863#endif
864
865 return (0);
866}
867