Home | History | Annotate | Line # | Download | only in kern
sched_m2.c revision 1.5.2.2
      1      1.4    yamt /*	$NetBSD: sched_m2.c,v 1.5.2.2 2007/11/13 16:02:16 bouyer Exp $	*/
      2      1.1   rmind 
      3      1.1   rmind /*
      4      1.1   rmind  * Copyright (c) 2007, Mindaugas Rasiukevicius
      5      1.1   rmind  *
      6      1.1   rmind  * Redistribution and use in source and binary forms, with or without
      7      1.1   rmind  * modification, are permitted provided that the following conditions
      8      1.1   rmind  * are met:
      9      1.1   rmind  * 1. Redistributions of source code must retain the above copyright
     10      1.1   rmind  *    notice, this list of conditions and the following disclaimer.
     11      1.1   rmind  * 2. Redistributions in binary form must reproduce the above copyright
     12      1.1   rmind  *    notice, this list of conditions and the following disclaimer in the
     13      1.1   rmind  *    documentation and/or other materials provided with the distribution.
     14      1.1   rmind  *
     15      1.1   rmind  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     16      1.1   rmind  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     17      1.1   rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     18      1.1   rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     19      1.1   rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     20      1.1   rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     21      1.1   rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     22      1.1   rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     23      1.1   rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     24      1.1   rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25      1.1   rmind  * POSSIBILITY OF SUCH DAMAGE.
     26      1.1   rmind  */
     27      1.1   rmind 
     28      1.1   rmind /*
     29      1.1   rmind  * TODO:
     30      1.1   rmind  *  - Implementation of fair share queue;
     31      1.1   rmind  *  - Support for NUMA;
     32      1.1   rmind  */
     33      1.1   rmind 
     34      1.1   rmind #include <sys/cdefs.h>
     35      1.4    yamt __KERNEL_RCSID(0, "$NetBSD: sched_m2.c,v 1.5.2.2 2007/11/13 16:02:16 bouyer Exp $");
     36      1.1   rmind 
     37      1.1   rmind #include <sys/param.h>
     38      1.1   rmind 
     39  1.5.2.2  bouyer #include <sys/bitops.h>
     40      1.1   rmind #include <sys/cpu.h>
     41      1.1   rmind #include <sys/callout.h>
     42      1.1   rmind #include <sys/errno.h>
     43      1.1   rmind #include <sys/kernel.h>
     44      1.1   rmind #include <sys/kmem.h>
     45      1.1   rmind #include <sys/lwp.h>
     46      1.1   rmind #include <sys/mutex.h>
     47      1.1   rmind #include <sys/pool.h>
     48      1.1   rmind #include <sys/proc.h>
     49      1.1   rmind #include <sys/resource.h>
     50      1.1   rmind #include <sys/resourcevar.h>
     51      1.1   rmind #include <sys/sched.h>
     52      1.1   rmind #include <sys/syscallargs.h>
     53      1.1   rmind #include <sys/sysctl.h>
     54      1.1   rmind #include <sys/types.h>
     55      1.1   rmind 
     56      1.1   rmind /*
     57  1.5.2.2  bouyer  * Priority related defintions.
     58      1.1   rmind  */
     59  1.5.2.2  bouyer #define	PRI_TS_COUNT	(NPRI_USER)
     60  1.5.2.2  bouyer #define	PRI_RT_COUNT	(PRI_COUNT - PRI_TS_COUNT)
     61  1.5.2.2  bouyer #define	PRI_HTS_RANGE	(PRI_TS_COUNT / 10)
     62  1.5.2.2  bouyer 
     63  1.5.2.2  bouyer #define	PRI_HIGHEST_TS	(MAXPRI_USER)
     64  1.5.2.2  bouyer #define	PRI_DEFAULT	(NPRI_USER >> 1)
     65  1.5.2.2  bouyer 
     66  1.5.2.2  bouyer const int schedppq = 1;
     67      1.1   rmind 
     68      1.1   rmind /*
     69      1.1   rmind  * Bits per map.
     70      1.1   rmind  */
     71  1.5.2.2  bouyer #define	BITMAP_BITS	(32)
     72  1.5.2.2  bouyer #define	BITMAP_SHIFT	(5)
     73  1.5.2.2  bouyer #define	BITMAP_MSB	(0x80000000U)
     74  1.5.2.2  bouyer #define	BITMAP_MASK	(BITMAP_BITS - 1)
     75      1.1   rmind 
     76      1.1   rmind /*
     77      1.1   rmind  * Time-slices and priorities.
     78      1.1   rmind  */
     79      1.1   rmind static u_int	min_ts;			/* Minimal time-slice */
     80      1.1   rmind static u_int	max_ts;			/* Maximal time-slice */
     81      1.1   rmind static u_int	rt_ts;			/* Real-time time-slice */
     82      1.1   rmind static u_int	ts_map[PRI_COUNT];	/* Map of time-slices */
     83      1.1   rmind static pri_t	high_pri[PRI_COUNT];	/* Map for priority increase */
     84      1.1   rmind 
     85      1.1   rmind /*
     86      1.1   rmind  * Migration and balancing.
     87      1.1   rmind  */
     88      1.1   rmind #ifdef MULTIPROCESSOR
     89      1.1   rmind static u_int	cacheht_time;		/* Cache hotness time */
     90      1.1   rmind static u_int	min_catch;		/* Minimal LWP count for catching */
     91      1.1   rmind 
     92      1.1   rmind static u_int		balance_period;	/* Balance period */
     93      1.1   rmind static struct callout	balance_ch;	/* Callout of balancer */
     94      1.1   rmind 
     95      1.1   rmind static struct cpu_info * volatile worker_ci;
     96      1.1   rmind 
     97      1.1   rmind #define CACHE_HOT(sil)		(sil->sl_lrtime && \
     98      1.1   rmind     (hardclock_ticks - sil->sl_lrtime < cacheht_time))
     99      1.1   rmind 
    100      1.1   rmind #endif
    101      1.1   rmind 
    102      1.1   rmind /*
    103      1.1   rmind  * Structures, runqueue.
    104      1.1   rmind  */
    105      1.1   rmind 
    106      1.1   rmind typedef struct {
    107      1.1   rmind 	TAILQ_HEAD(, lwp) q_head;
    108      1.1   rmind } queue_t;
    109      1.1   rmind 
    110      1.1   rmind typedef struct {
    111      1.1   rmind 	/* Lock and bitmap */
    112      1.1   rmind 	kmutex_t	r_rq_mutex;
    113  1.5.2.2  bouyer 	uint32_t	r_bitmap[PRI_COUNT >> BITMAP_SHIFT];
    114      1.1   rmind 	/* Counters */
    115      1.1   rmind 	u_int		r_count;	/* Count of the threads */
    116      1.1   rmind 	pri_t		r_highest_pri;	/* Highest priority */
    117      1.1   rmind 	u_int		r_avgcount;	/* Average count of threads */
    118      1.1   rmind 	u_int		r_mcount;	/* Count of migratable threads */
    119      1.1   rmind 	/* Runqueues */
    120      1.1   rmind 	queue_t		r_rt_queue[PRI_RT_COUNT];
    121      1.1   rmind 	queue_t		r_ts_queue[PRI_TS_COUNT];
    122      1.1   rmind } runqueue_t;
    123      1.1   rmind 
    124      1.1   rmind typedef struct {
    125      1.1   rmind 	u_int		sl_flags;
    126      1.1   rmind 	u_int		sl_timeslice;	/* Time-slice of thread */
    127      1.1   rmind 	u_int		sl_slept;	/* Saved sleep time for sleep sum */
    128      1.1   rmind 	u_int		sl_slpsum;	/* Sum of sleep time */
    129      1.1   rmind 	u_int		sl_rtime;	/* Saved start time of run */
    130      1.1   rmind 	u_int		sl_rtsum;	/* Sum of the run time */
    131      1.1   rmind 	u_int		sl_lrtime;	/* Last run time */
    132      1.1   rmind } sched_info_lwp_t;
    133      1.1   rmind 
    134      1.1   rmind /* Flags */
    135      1.1   rmind #define	SL_BATCH	0x01
    136      1.1   rmind 
    137      1.1   rmind /* Pool of the scheduler-specific structures for threads */
    138      1.1   rmind static struct pool	sil_pool;
    139      1.1   rmind 
    140      1.1   rmind /*
    141      1.1   rmind  * Prototypes.
    142      1.1   rmind  */
    143      1.1   rmind 
    144      1.1   rmind static inline void *	sched_getrq(runqueue_t *, const pri_t);
    145      1.1   rmind static inline void	sched_newts(struct lwp *);
    146      1.1   rmind static void		sched_precalcts(void);
    147      1.1   rmind 
    148      1.1   rmind #ifdef MULTIPROCESSOR
    149      1.1   rmind static struct lwp *	sched_catchlwp(void);
    150      1.1   rmind static void		sched_balance(void *);
    151      1.1   rmind #endif
    152      1.1   rmind 
    153      1.1   rmind /*
    154      1.1   rmind  * Initialization and setup.
    155      1.1   rmind  */
    156      1.1   rmind 
    157      1.1   rmind void
    158      1.1   rmind sched_rqinit(void)
    159      1.1   rmind {
    160      1.1   rmind 	struct cpu_info *ci = curcpu();
    161      1.1   rmind 
    162      1.1   rmind 	if (hz < 100) {
    163      1.1   rmind 		panic("sched_rqinit: value of HZ is too low\n");
    164      1.1   rmind 	}
    165      1.1   rmind 
    166      1.1   rmind 	/* Default timing ranges */
    167      1.1   rmind 	min_ts = mstohz(50);			/* ~50ms  */
    168      1.1   rmind 	max_ts = mstohz(150);			/* ~150ms */
    169      1.1   rmind 	rt_ts = mstohz(100);			/* ~100ms */
    170      1.1   rmind 	sched_precalcts();
    171      1.1   rmind 
    172      1.1   rmind #ifdef MULTIPROCESSOR
    173      1.1   rmind 	/* Balancing */
    174      1.1   rmind 	worker_ci = ci;
    175      1.1   rmind 	cacheht_time = mstohz(5);		/* ~5 ms  */
    176      1.1   rmind 	balance_period = mstohz(300);		/* ~300ms */
    177      1.1   rmind 	min_catch = ~0;
    178      1.1   rmind #endif
    179      1.1   rmind 
    180      1.1   rmind 	/* Pool of the scheduler-specific structures */
    181      1.1   rmind 	pool_init(&sil_pool, sizeof(sched_info_lwp_t), 0, 0, 0,
    182      1.1   rmind 	    "lwpsd", &pool_allocator_nointr, IPL_NONE);
    183      1.1   rmind 
    184      1.1   rmind 	/* Attach the primary CPU here */
    185      1.1   rmind 	sched_cpuattach(ci);
    186      1.1   rmind 
    187      1.1   rmind 	/* Initialize the scheduler structure of the primary LWP */
    188      1.1   rmind 	lwp0.l_mutex = &ci->ci_schedstate.spc_lwplock;
    189  1.5.2.2  bouyer 	sched_lwp_fork(NULL, &lwp0);
    190      1.1   rmind 	sched_newts(&lwp0);
    191      1.1   rmind }
    192      1.1   rmind 
    193      1.1   rmind void
    194      1.1   rmind sched_setup(void)
    195      1.1   rmind {
    196      1.1   rmind 
    197      1.1   rmind #ifdef MULTIPROCESSOR
    198      1.1   rmind 	/* Minimal count of LWPs for catching: log2(count of CPUs) */
    199  1.5.2.2  bouyer 	min_catch = min(ilog2(ncpu), 4);
    200      1.1   rmind 
    201      1.1   rmind 	/* Initialize balancing callout and run it */
    202      1.1   rmind 	callout_init(&balance_ch, CALLOUT_MPSAFE);
    203      1.1   rmind 	callout_setfunc(&balance_ch, sched_balance, NULL);
    204      1.1   rmind 	callout_schedule(&balance_ch, balance_period);
    205      1.1   rmind #endif
    206      1.1   rmind }
    207      1.1   rmind 
    208      1.1   rmind void
    209      1.1   rmind sched_cpuattach(struct cpu_info *ci)
    210      1.1   rmind {
    211      1.1   rmind 	runqueue_t *ci_rq;
    212      1.1   rmind 	void *rq_ptr;
    213      1.1   rmind 	u_int i, size;
    214      1.1   rmind 
    215      1.1   rmind 	/*
    216      1.1   rmind 	 * Allocate the run queue.
    217      1.1   rmind 	 * XXX: Estimate cache behaviour more..
    218      1.1   rmind 	 */
    219      1.1   rmind 	size = roundup(sizeof(runqueue_t), CACHE_LINE_SIZE) + CACHE_LINE_SIZE;
    220      1.1   rmind 	rq_ptr = kmem_zalloc(size, KM_NOSLEEP);
    221      1.1   rmind 	if (rq_ptr == NULL) {
    222      1.1   rmind 		panic("scheduler: could not allocate the runqueue");
    223      1.1   rmind 	}
    224      1.1   rmind 	/* XXX: Save the original pointer for future.. */
    225      1.1   rmind 	ci_rq = (void *)(roundup((intptr_t)(rq_ptr), CACHE_LINE_SIZE));
    226      1.1   rmind 
    227      1.1   rmind 	/* Initialize run queues */
    228      1.1   rmind 	mutex_init(&ci_rq->r_rq_mutex, MUTEX_SPIN, IPL_SCHED);
    229      1.1   rmind 	for (i = 0; i < PRI_RT_COUNT; i++)
    230      1.1   rmind 		TAILQ_INIT(&ci_rq->r_rt_queue[i].q_head);
    231      1.1   rmind 	for (i = 0; i < PRI_TS_COUNT; i++)
    232      1.1   rmind 		TAILQ_INIT(&ci_rq->r_ts_queue[i].q_head);
    233  1.5.2.2  bouyer 	ci_rq->r_highest_pri = 0;
    234      1.1   rmind 
    235      1.1   rmind 	ci->ci_schedstate.spc_sched_info = ci_rq;
    236      1.1   rmind 	ci->ci_schedstate.spc_mutex = &ci_rq->r_rq_mutex;
    237      1.1   rmind }
    238      1.1   rmind 
    239      1.1   rmind /* Pre-calculate the time-slices for the priorities */
    240      1.1   rmind static void
    241      1.1   rmind sched_precalcts(void)
    242      1.1   rmind {
    243      1.1   rmind 	pri_t p;
    244      1.1   rmind 
    245  1.5.2.2  bouyer 	/* Time-sharing range */
    246  1.5.2.2  bouyer 	for (p = 0; p <= PRI_HIGHEST_TS; p++) {
    247  1.5.2.2  bouyer 		ts_map[p] = max_ts -
    248  1.5.2.2  bouyer 		    (p * 100 / (PRI_TS_COUNT - 1) * (max_ts - min_ts) / 100);
    249  1.5.2.2  bouyer 		high_pri[p] = (PRI_HIGHEST_TS - PRI_HTS_RANGE) +
    250  1.5.2.2  bouyer 		    ((p * PRI_HTS_RANGE) / (PRI_TS_COUNT - 1));
    251      1.1   rmind 	}
    252      1.1   rmind 
    253  1.5.2.2  bouyer 	/* Real-time range */
    254  1.5.2.2  bouyer 	for (p = (PRI_HIGHEST_TS + 1); p < PRI_COUNT; p++) {
    255  1.5.2.2  bouyer 		ts_map[p] = rt_ts;
    256  1.5.2.2  bouyer 		high_pri[p] = p;
    257      1.1   rmind 	}
    258      1.1   rmind }
    259      1.1   rmind 
    260      1.1   rmind /*
    261      1.1   rmind  * Hooks.
    262      1.1   rmind  */
    263      1.1   rmind 
    264      1.1   rmind void
    265      1.1   rmind sched_proc_fork(struct proc *parent, struct proc *child)
    266      1.1   rmind {
    267      1.1   rmind 	struct lwp *l;
    268      1.1   rmind 
    269      1.1   rmind 	LIST_FOREACH(l, &child->p_lwps, l_sibling) {
    270      1.1   rmind 		lwp_lock(l);
    271      1.1   rmind 		sched_newts(l);
    272      1.1   rmind 		lwp_unlock(l);
    273      1.1   rmind 	}
    274      1.1   rmind }
    275      1.1   rmind 
    276      1.1   rmind void
    277      1.1   rmind sched_proc_exit(struct proc *child, struct proc *parent)
    278      1.1   rmind {
    279      1.1   rmind 
    280      1.1   rmind 	/* Dummy */
    281      1.1   rmind }
    282      1.1   rmind 
    283      1.1   rmind void
    284  1.5.2.2  bouyer sched_lwp_fork(struct lwp *l1, struct lwp *l2)
    285      1.1   rmind {
    286      1.1   rmind 
    287  1.5.2.2  bouyer 	KASSERT(l2->l_sched_info == NULL);
    288  1.5.2.2  bouyer 	l2->l_sched_info = pool_get(&sil_pool, PR_WAITOK);
    289  1.5.2.2  bouyer 	memset(l2->l_sched_info, 0, sizeof(sched_info_lwp_t));
    290  1.5.2.2  bouyer 	if (l2->l_priority <= PRI_HIGHEST_TS) /* XXX: For now only.. */
    291  1.5.2.2  bouyer 		l2->l_priority = PRI_DEFAULT;
    292      1.1   rmind }
    293      1.1   rmind 
    294      1.1   rmind void
    295      1.1   rmind sched_lwp_exit(struct lwp *l)
    296      1.1   rmind {
    297      1.1   rmind 
    298      1.1   rmind 	KASSERT(l->l_sched_info != NULL);
    299      1.1   rmind 	pool_put(&sil_pool, l->l_sched_info);
    300      1.1   rmind 	l->l_sched_info = NULL;
    301      1.1   rmind }
    302      1.1   rmind 
    303      1.1   rmind void
    304  1.5.2.2  bouyer sched_lwp_collect(struct lwp *l)
    305  1.5.2.2  bouyer {
    306  1.5.2.2  bouyer 
    307  1.5.2.2  bouyer }
    308  1.5.2.2  bouyer 
    309  1.5.2.2  bouyer void
    310      1.1   rmind sched_setrunnable(struct lwp *l)
    311      1.1   rmind {
    312      1.1   rmind 
    313      1.1   rmind 	/* Dummy */
    314      1.1   rmind }
    315      1.1   rmind 
    316      1.1   rmind void
    317      1.1   rmind sched_schedclock(struct lwp *l)
    318      1.1   rmind {
    319      1.1   rmind 
    320      1.1   rmind 	/* Dummy */
    321      1.1   rmind }
    322      1.1   rmind 
    323      1.1   rmind /*
    324      1.1   rmind  * Priorities and time-slice.
    325      1.1   rmind  */
    326      1.1   rmind 
    327      1.1   rmind void
    328      1.1   rmind sched_nice(struct proc *p, int prio)
    329      1.1   rmind {
    330      1.1   rmind 	int nprio;
    331      1.1   rmind 	struct lwp *l;
    332      1.1   rmind 
    333  1.5.2.2  bouyer 	KASSERT(mutex_owned(&p->p_smutex));
    334      1.1   rmind 
    335      1.1   rmind 	p->p_nice = prio;
    336  1.5.2.2  bouyer 	nprio = max(min(PRI_DEFAULT + p->p_nice, PRI_HIGHEST_TS), 0);
    337      1.1   rmind 
    338      1.1   rmind 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
    339      1.1   rmind 		lwp_lock(l);
    340      1.1   rmind 		lwp_changepri(l, nprio);
    341      1.1   rmind 		lwp_unlock(l);
    342      1.1   rmind 	}
    343      1.1   rmind }
    344      1.1   rmind 
    345      1.1   rmind /* Recalculate the time-slice */
    346      1.1   rmind static inline void
    347      1.1   rmind sched_newts(struct lwp *l)
    348      1.1   rmind {
    349      1.1   rmind 	sched_info_lwp_t *sil = l->l_sched_info;
    350      1.1   rmind 
    351      1.1   rmind 	sil->sl_timeslice = ts_map[lwp_eprio(l)];
    352      1.1   rmind }
    353      1.1   rmind 
    354      1.1   rmind /*
    355      1.1   rmind  * Control of the runqueue.
    356      1.1   rmind  */
    357      1.1   rmind 
    358      1.1   rmind static inline void *
    359      1.1   rmind sched_getrq(runqueue_t *ci_rq, const pri_t prio)
    360      1.1   rmind {
    361      1.1   rmind 
    362      1.1   rmind 	KASSERT(prio < PRI_COUNT);
    363  1.5.2.2  bouyer 	return (prio <= PRI_HIGHEST_TS) ?
    364  1.5.2.2  bouyer 	    &ci_rq->r_ts_queue[prio].q_head :
    365  1.5.2.2  bouyer 	    &ci_rq->r_rt_queue[prio - PRI_HIGHEST_TS - 1].q_head;
    366      1.1   rmind }
    367      1.1   rmind 
    368      1.1   rmind void
    369      1.1   rmind sched_enqueue(struct lwp *l, bool swtch)
    370      1.1   rmind {
    371      1.1   rmind 	runqueue_t *ci_rq;
    372      1.1   rmind 	sched_info_lwp_t *sil = l->l_sched_info;
    373      1.1   rmind 	TAILQ_HEAD(, lwp) *q_head;
    374      1.1   rmind 	const pri_t eprio = lwp_eprio(l);
    375      1.1   rmind 
    376      1.1   rmind 	ci_rq = l->l_cpu->ci_schedstate.spc_sched_info;
    377      1.1   rmind 	KASSERT(lwp_locked(l, l->l_cpu->ci_schedstate.spc_mutex));
    378      1.1   rmind 
    379      1.1   rmind 	/* Update the last run time on switch */
    380  1.5.2.2  bouyer 	if (__predict_true(swtch == true)) {
    381      1.1   rmind 		sil->sl_lrtime = hardclock_ticks;
    382      1.1   rmind 		sil->sl_rtsum += (hardclock_ticks - sil->sl_rtime);
    383  1.5.2.2  bouyer 	} else if (sil->sl_lrtime == 0)
    384  1.5.2.2  bouyer 		sil->sl_lrtime = hardclock_ticks;
    385      1.1   rmind 
    386      1.1   rmind 	/* Enqueue the thread */
    387      1.1   rmind 	q_head = sched_getrq(ci_rq, eprio);
    388      1.1   rmind 	if (TAILQ_EMPTY(q_head)) {
    389      1.1   rmind 		u_int i;
    390      1.1   rmind 		uint32_t q;
    391      1.1   rmind 
    392      1.1   rmind 		/* Mark bit */
    393      1.1   rmind 		i = eprio >> BITMAP_SHIFT;
    394  1.5.2.2  bouyer 		q = BITMAP_MSB >> (eprio & BITMAP_MASK);
    395  1.5.2.2  bouyer 		KASSERT((ci_rq->r_bitmap[i] & q) == 0);
    396  1.5.2.2  bouyer 		ci_rq->r_bitmap[i] |= q;
    397      1.1   rmind 	}
    398      1.1   rmind 	TAILQ_INSERT_TAIL(q_head, l, l_runq);
    399      1.1   rmind 	ci_rq->r_count++;
    400      1.1   rmind 	if ((l->l_flag & LW_BOUND) == 0)
    401      1.1   rmind 		ci_rq->r_mcount++;
    402      1.1   rmind 
    403      1.1   rmind 	/*
    404      1.1   rmind 	 * Update the value of highest priority in the runqueue,
    405      1.1   rmind 	 * if priority of this thread is higher.
    406      1.1   rmind 	 */
    407  1.5.2.2  bouyer 	if (eprio > ci_rq->r_highest_pri)
    408      1.1   rmind 		ci_rq->r_highest_pri = eprio;
    409      1.1   rmind 
    410      1.1   rmind 	sched_newts(l);
    411      1.1   rmind }
    412      1.1   rmind 
    413      1.1   rmind void
    414      1.1   rmind sched_dequeue(struct lwp *l)
    415      1.1   rmind {
    416      1.1   rmind 	runqueue_t *ci_rq;
    417      1.1   rmind 	TAILQ_HEAD(, lwp) *q_head;
    418      1.1   rmind 	const pri_t eprio = lwp_eprio(l);
    419      1.1   rmind 
    420      1.1   rmind 	ci_rq = l->l_cpu->ci_schedstate.spc_sched_info;
    421      1.1   rmind 	KASSERT(lwp_locked(l, l->l_cpu->ci_schedstate.spc_mutex));
    422  1.5.2.2  bouyer 	KASSERT(eprio <= ci_rq->r_highest_pri);
    423      1.1   rmind 	KASSERT(ci_rq->r_bitmap[eprio >> BITMAP_SHIFT] != 0);
    424      1.1   rmind 	KASSERT(ci_rq->r_count > 0);
    425      1.1   rmind 
    426      1.1   rmind 	ci_rq->r_count--;
    427      1.1   rmind 	if ((l->l_flag & LW_BOUND) == 0)
    428      1.1   rmind 		ci_rq->r_mcount--;
    429      1.1   rmind 
    430      1.1   rmind 	q_head = sched_getrq(ci_rq, eprio);
    431      1.1   rmind 	TAILQ_REMOVE(q_head, l, l_runq);
    432      1.1   rmind 	if (TAILQ_EMPTY(q_head)) {
    433      1.1   rmind 		u_int i;
    434      1.1   rmind 		uint32_t q;
    435      1.1   rmind 
    436      1.1   rmind 		/* Unmark bit */
    437      1.1   rmind 		i = eprio >> BITMAP_SHIFT;
    438  1.5.2.2  bouyer 		q = BITMAP_MSB >> (eprio & BITMAP_MASK);
    439  1.5.2.2  bouyer 		KASSERT((ci_rq->r_bitmap[i] & q) != 0);
    440  1.5.2.2  bouyer 		ci_rq->r_bitmap[i] &= ~q;
    441      1.1   rmind 
    442      1.1   rmind 		/*
    443      1.1   rmind 		 * Update the value of highest priority in the runqueue, in a
    444      1.1   rmind 		 * case it was a last thread in the queue of highest priority.
    445      1.1   rmind 		 */
    446      1.1   rmind 		if (eprio != ci_rq->r_highest_pri)
    447      1.1   rmind 			return;
    448      1.1   rmind 
    449      1.1   rmind 		do {
    450      1.1   rmind 			q = ffs(ci_rq->r_bitmap[i]);
    451      1.1   rmind 			if (q) {
    452      1.1   rmind 				ci_rq->r_highest_pri =
    453  1.5.2.2  bouyer 				    (i << BITMAP_SHIFT) + (BITMAP_BITS - q);
    454      1.1   rmind 				return;
    455      1.1   rmind 			}
    456  1.5.2.2  bouyer 		} while (i--);
    457      1.1   rmind 
    458  1.5.2.2  bouyer 		/* If not found - set the lowest value */
    459  1.5.2.2  bouyer 		ci_rq->r_highest_pri = 0;
    460      1.1   rmind 	}
    461      1.1   rmind }
    462      1.1   rmind 
    463      1.1   rmind void
    464      1.1   rmind sched_slept(struct lwp *l)
    465      1.1   rmind {
    466      1.1   rmind 	sched_info_lwp_t *sil = l->l_sched_info;
    467      1.1   rmind 
    468      1.1   rmind 	/* Save the time when thread has slept */
    469      1.1   rmind 	sil->sl_slept = hardclock_ticks;
    470      1.1   rmind 
    471      1.1   rmind 	/*
    472  1.5.2.2  bouyer 	 * If thread is in time-sharing queue and batch flag is not marked,
    473  1.5.2.2  bouyer 	 * increase the the priority, and run with the lower time-quantum.
    474      1.1   rmind 	 */
    475  1.5.2.2  bouyer 	if (l->l_priority < PRI_HIGHEST_TS && (sil->sl_flags & SL_BATCH) == 0) {
    476  1.5.2.2  bouyer 		KASSERT(l->l_class == SCHED_OTHER);
    477  1.5.2.2  bouyer 		l->l_priority++;
    478  1.5.2.2  bouyer 	}
    479      1.1   rmind }
    480      1.1   rmind 
    481      1.1   rmind void
    482      1.1   rmind sched_wakeup(struct lwp *l)
    483      1.1   rmind {
    484      1.1   rmind 	sched_info_lwp_t *sil = l->l_sched_info;
    485      1.1   rmind 
    486      1.1   rmind 	/* Update sleep time delta */
    487      1.1   rmind 	sil->sl_slpsum += (l->l_slptime == 0) ?
    488      1.1   rmind 	    (hardclock_ticks - sil->sl_slept) : hz;
    489      1.1   rmind 
    490      1.1   rmind 	/* If thread was sleeping a second or more - set a high priority */
    491      1.1   rmind 	if (l->l_slptime > 1 || (hardclock_ticks - sil->sl_slept) >= hz)
    492  1.5.2.2  bouyer 		l->l_priority = high_pri[l->l_priority];
    493      1.1   rmind 
    494      1.1   rmind 	/* Also, consider looking for a better CPU to wake up */
    495      1.1   rmind 	if ((l->l_flag & (LW_BOUND | LW_SYSTEM)) == 0)
    496      1.1   rmind 		l->l_cpu = sched_takecpu(l);
    497      1.1   rmind }
    498      1.1   rmind 
    499      1.1   rmind void
    500      1.1   rmind sched_pstats_hook(struct lwp *l)
    501      1.1   rmind {
    502      1.1   rmind 	sched_info_lwp_t *sil = l->l_sched_info;
    503  1.5.2.2  bouyer 	pri_t prio;
    504  1.5.2.2  bouyer 	bool batch;
    505  1.5.2.2  bouyer 
    506  1.5.2.2  bouyer 	if (l->l_stat == LSSLEEP || l->l_stat == LSSTOP ||
    507  1.5.2.2  bouyer 	    l->l_stat == LSSUSPENDED)
    508  1.5.2.2  bouyer 		l->l_slptime++;
    509      1.1   rmind 
    510      1.1   rmind 	/*
    511      1.1   rmind 	 * Set that thread is more CPU-bound, if sum of run time exceeds the
    512  1.5.2.2  bouyer 	 * sum of sleep time.  Check if thread is CPU-bound a first time.
    513      1.1   rmind 	 */
    514  1.5.2.2  bouyer 	batch = (sil->sl_rtsum > sil->sl_slpsum);
    515  1.5.2.2  bouyer 	if (batch) {
    516  1.5.2.2  bouyer 		if ((sil->sl_flags & SL_BATCH) == 0)
    517  1.5.2.2  bouyer 			batch = false;
    518      1.1   rmind 		sil->sl_flags |= SL_BATCH;
    519  1.5.2.2  bouyer 	} else
    520      1.1   rmind 		sil->sl_flags &= ~SL_BATCH;
    521  1.5.2.2  bouyer 
    522  1.5.2.2  bouyer 	/* Reset the time sums */
    523      1.1   rmind 	sil->sl_slpsum = 0;
    524      1.1   rmind 	sil->sl_rtsum = 0;
    525      1.1   rmind 
    526  1.5.2.2  bouyer 	/* Estimate threads on time-sharing queue only */
    527  1.5.2.2  bouyer 	if (l->l_priority >= PRI_HIGHEST_TS)
    528      1.1   rmind 		return;
    529      1.1   rmind 
    530  1.5.2.2  bouyer 	/* If it is CPU-bound not a first time - decrease the priority */
    531  1.5.2.2  bouyer 	prio = l->l_priority;
    532  1.5.2.2  bouyer 	if (batch && prio != 0)
    533  1.5.2.2  bouyer 		prio--;
    534  1.5.2.2  bouyer 
    535      1.1   rmind 	/* If thread was not ran a second or more - set a high priority */
    536  1.5.2.2  bouyer 	if (l->l_stat == LSRUN) {
    537  1.5.2.2  bouyer 		if (sil->sl_lrtime && (hardclock_ticks - sil->sl_lrtime >= hz))
    538  1.5.2.2  bouyer 			prio = high_pri[prio];
    539  1.5.2.2  bouyer 		/* Re-enqueue the thread if priority has changed */
    540  1.5.2.2  bouyer 		if (prio != l->l_priority)
    541  1.5.2.2  bouyer 			lwp_changepri(l, prio);
    542  1.5.2.2  bouyer 	} else {
    543  1.5.2.2  bouyer 		/* In other states, change the priority directly */
    544  1.5.2.2  bouyer 		l->l_priority = prio;
    545  1.5.2.2  bouyer 	}
    546      1.1   rmind }
    547      1.1   rmind 
    548      1.1   rmind /*
    549      1.1   rmind  * Migration and balancing.
    550      1.1   rmind  */
    551      1.1   rmind 
    552      1.1   rmind #ifdef MULTIPROCESSOR
    553      1.1   rmind 
    554      1.1   rmind /* Check if LWP can migrate to the chosen CPU */
    555      1.1   rmind static inline bool
    556      1.1   rmind sched_migratable(const struct lwp *l, const struct cpu_info *ci)
    557      1.1   rmind {
    558      1.1   rmind 
    559      1.1   rmind 	if (ci->ci_schedstate.spc_flags & SPCF_OFFLINE)
    560      1.1   rmind 		return false;
    561      1.1   rmind 
    562      1.1   rmind 	if ((l->l_flag & LW_BOUND) == 0)
    563      1.1   rmind 		return true;
    564      1.1   rmind #if 0
    565      1.1   rmind 	return cpu_in_pset(ci, l->l_psid);
    566      1.1   rmind #else
    567      1.1   rmind 	return false;
    568      1.1   rmind #endif
    569      1.1   rmind }
    570      1.1   rmind 
    571      1.1   rmind /*
    572      1.1   rmind  * Estimate the migration of LWP to the other CPU.
    573      1.1   rmind  * Take and return the CPU, if migration is needed.
    574      1.1   rmind  */
    575      1.1   rmind struct cpu_info *
    576      1.1   rmind sched_takecpu(struct lwp *l)
    577      1.1   rmind {
    578      1.1   rmind 	struct cpu_info *ci, *tci = NULL;
    579      1.1   rmind 	struct schedstate_percpu *spc;
    580      1.1   rmind 	runqueue_t *ci_rq;
    581      1.1   rmind 	sched_info_lwp_t *sil;
    582      1.1   rmind 	CPU_INFO_ITERATOR cii;
    583      1.1   rmind 	pri_t eprio, lpri;
    584      1.1   rmind 
    585      1.1   rmind 	ci = l->l_cpu;
    586      1.1   rmind 	spc = &ci->ci_schedstate;
    587      1.1   rmind 	ci_rq = spc->spc_sched_info;
    588      1.1   rmind 
    589      1.1   rmind 	/* CPU of this thread is idling - run there */
    590      1.1   rmind 	if (ci_rq->r_count == 0)
    591      1.1   rmind 		return ci;
    592      1.1   rmind 
    593      1.1   rmind 	eprio = lwp_eprio(l);
    594      1.1   rmind 	sil = l->l_sched_info;
    595      1.1   rmind 
    596      1.1   rmind 	/* Stay if thread is cache-hot */
    597      1.1   rmind 	if (l->l_stat == LSSLEEP && l->l_slptime <= 1 &&
    598  1.5.2.2  bouyer 	    CACHE_HOT(sil) && eprio >= spc->spc_curpriority)
    599      1.1   rmind 		return ci;
    600      1.1   rmind 
    601      1.1   rmind 	/* Run on current CPU if priority of thread is higher */
    602      1.1   rmind 	ci = curcpu();
    603      1.1   rmind 	spc = &ci->ci_schedstate;
    604  1.5.2.2  bouyer 	if (eprio > spc->spc_curpriority && sched_migratable(l, ci))
    605      1.1   rmind 		return ci;
    606      1.1   rmind 
    607      1.1   rmind 	/*
    608      1.1   rmind 	 * Look for the CPU with the lowest priority thread.  In case of
    609      1.1   rmind 	 * equal the priority - check the lower count of the threads.
    610      1.1   rmind 	 */
    611  1.5.2.2  bouyer 	lpri = PRI_COUNT;
    612      1.1   rmind 	for (CPU_INFO_FOREACH(cii, ci)) {
    613      1.1   rmind 		runqueue_t *ici_rq;
    614      1.1   rmind 		pri_t pri;
    615      1.1   rmind 
    616      1.1   rmind 		spc = &ci->ci_schedstate;
    617      1.1   rmind 		ici_rq = spc->spc_sched_info;
    618  1.5.2.2  bouyer 		pri = max(spc->spc_curpriority, ici_rq->r_highest_pri);
    619  1.5.2.2  bouyer 		if (pri > lpri)
    620      1.1   rmind 			continue;
    621      1.1   rmind 
    622  1.5.2.2  bouyer 		if (pri == lpri && tci && ci_rq->r_count < ici_rq->r_count)
    623      1.1   rmind 			continue;
    624      1.1   rmind 
    625      1.1   rmind 		if (sched_migratable(l, ci) == false)
    626      1.1   rmind 			continue;
    627      1.1   rmind 
    628      1.1   rmind 		lpri = pri;
    629      1.1   rmind 		tci = ci;
    630      1.1   rmind 		ci_rq = ici_rq;
    631      1.1   rmind 	}
    632      1.1   rmind 
    633  1.5.2.2  bouyer 	KASSERT(tci != NULL);
    634      1.1   rmind 	return tci;
    635      1.1   rmind }
    636      1.1   rmind 
    637      1.1   rmind /*
    638      1.1   rmind  * Tries to catch an LWP from the runqueue of other CPU.
    639      1.1   rmind  */
    640      1.1   rmind static struct lwp *
    641      1.1   rmind sched_catchlwp(void)
    642      1.1   rmind {
    643      1.1   rmind 	struct cpu_info *curci = curcpu(), *ci = worker_ci;
    644      1.1   rmind 	TAILQ_HEAD(, lwp) *q_head;
    645      1.1   rmind 	runqueue_t *ci_rq;
    646      1.1   rmind 	struct lwp *l;
    647      1.1   rmind 
    648      1.1   rmind 	if (curci == ci)
    649      1.1   rmind 		return NULL;
    650      1.1   rmind 
    651      1.1   rmind 	/* Lockless check */
    652      1.1   rmind 	ci_rq = ci->ci_schedstate.spc_sched_info;
    653      1.1   rmind 	if (ci_rq->r_count < min_catch)
    654      1.1   rmind 		return NULL;
    655      1.1   rmind 
    656      1.1   rmind 	/*
    657      1.1   rmind 	 * Double-lock the runqueues.
    658      1.1   rmind 	 */
    659      1.3   rmind 	if (curci < ci) {
    660      1.1   rmind 		spc_lock(ci);
    661      1.1   rmind 	} else if (!mutex_tryenter(ci->ci_schedstate.spc_mutex)) {
    662      1.1   rmind 		const runqueue_t *cur_rq = curci->ci_schedstate.spc_sched_info;
    663      1.1   rmind 
    664      1.1   rmind 		spc_unlock(curci);
    665      1.1   rmind 		spc_lock(ci);
    666      1.1   rmind 		spc_lock(curci);
    667      1.1   rmind 
    668      1.1   rmind 		if (cur_rq->r_count) {
    669      1.1   rmind 			spc_unlock(ci);
    670      1.1   rmind 			return NULL;
    671      1.1   rmind 		}
    672      1.1   rmind 	}
    673      1.1   rmind 
    674      1.1   rmind 	if (ci_rq->r_count < min_catch) {
    675      1.1   rmind 		spc_unlock(ci);
    676      1.1   rmind 		return NULL;
    677      1.1   rmind 	}
    678      1.1   rmind 
    679      1.1   rmind 	/* Take the highest priority thread */
    680      1.1   rmind 	q_head = sched_getrq(ci_rq, ci_rq->r_highest_pri);
    681      1.1   rmind 	l = TAILQ_FIRST(q_head);
    682      1.1   rmind 
    683      1.1   rmind 	for (;;) {
    684      1.1   rmind 		sched_info_lwp_t *sil;
    685      1.1   rmind 
    686      1.1   rmind 		/* Check the first and next result from the queue */
    687      1.1   rmind 		if (l == NULL)
    688      1.1   rmind 			break;
    689      1.1   rmind 
    690      1.1   rmind 		/* Look for threads, whose are allowed to migrate */
    691      1.1   rmind 		sil = l->l_sched_info;
    692      1.1   rmind 		if ((l->l_flag & LW_SYSTEM) || CACHE_HOT(sil) ||
    693      1.1   rmind 		    sched_migratable(l, curci) == false) {
    694      1.1   rmind 			l = TAILQ_NEXT(l, l_runq);
    695      1.1   rmind 			continue;
    696      1.1   rmind 		}
    697      1.1   rmind 		/* Recheck if chosen thread is still on the runqueue */
    698      1.1   rmind 		if (l->l_stat == LSRUN && (l->l_flag & LW_INMEM)) {
    699      1.1   rmind 			sched_dequeue(l);
    700      1.1   rmind 			l->l_cpu = curci;
    701      1.1   rmind 			lwp_setlock(l, curci->ci_schedstate.spc_mutex);
    702      1.1   rmind 			sched_enqueue(l, false);
    703      1.1   rmind 			break;
    704      1.1   rmind 		}
    705      1.1   rmind 		l = TAILQ_NEXT(l, l_runq);
    706      1.1   rmind 	}
    707      1.1   rmind 	spc_unlock(ci);
    708      1.1   rmind 
    709      1.1   rmind 	return l;
    710      1.1   rmind }
    711      1.1   rmind 
    712      1.1   rmind /*
    713      1.1   rmind  * Periodical calculations for balancing.
    714      1.1   rmind  */
    715      1.1   rmind static void
    716      1.1   rmind sched_balance(void *nocallout)
    717      1.1   rmind {
    718      1.1   rmind 	struct cpu_info *ci, *hci;
    719      1.1   rmind 	runqueue_t *ci_rq;
    720      1.1   rmind 	CPU_INFO_ITERATOR cii;
    721      1.1   rmind 	u_int highest;
    722      1.1   rmind 
    723      1.1   rmind 	hci = curcpu();
    724      1.1   rmind 	highest = 0;
    725      1.1   rmind 
    726      1.1   rmind 	/* Make lockless countings */
    727      1.1   rmind 	for (CPU_INFO_FOREACH(cii, ci)) {
    728      1.1   rmind 		ci_rq = ci->ci_schedstate.spc_sched_info;
    729      1.1   rmind 
    730      1.1   rmind 		/* Average count of the threads */
    731      1.1   rmind 		ci_rq->r_avgcount = (ci_rq->r_avgcount + ci_rq->r_mcount) >> 1;
    732      1.1   rmind 
    733      1.1   rmind 		/* Look for CPU with the highest average */
    734      1.1   rmind 		if (ci_rq->r_avgcount > highest) {
    735      1.1   rmind 			hci = ci;
    736      1.1   rmind 			highest = ci_rq->r_avgcount;
    737      1.1   rmind 		}
    738      1.1   rmind 	}
    739      1.1   rmind 
    740      1.1   rmind 	/* Update the worker */
    741      1.1   rmind 	worker_ci = hci;
    742      1.1   rmind 
    743      1.1   rmind 	if (nocallout == NULL)
    744      1.1   rmind 		callout_schedule(&balance_ch, balance_period);
    745      1.1   rmind }
    746      1.1   rmind 
    747      1.1   rmind #else
    748      1.1   rmind 
    749      1.1   rmind struct cpu_info *
    750      1.1   rmind sched_takecpu(struct lwp *l)
    751      1.1   rmind {
    752      1.1   rmind 
    753      1.1   rmind 	return l->l_cpu;
    754      1.1   rmind }
    755      1.1   rmind 
    756      1.1   rmind #endif	/* MULTIPROCESSOR */
    757      1.1   rmind 
    758      1.1   rmind /*
    759      1.1   rmind  * Scheduler mill.
    760      1.1   rmind  */
    761      1.1   rmind struct lwp *
    762      1.1   rmind sched_nextlwp(void)
    763      1.1   rmind {
    764      1.1   rmind 	struct cpu_info *ci = curcpu();
    765      1.1   rmind 	struct schedstate_percpu *spc;
    766      1.1   rmind 	TAILQ_HEAD(, lwp) *q_head;
    767      1.1   rmind 	sched_info_lwp_t *sil;
    768      1.1   rmind 	runqueue_t *ci_rq;
    769      1.1   rmind 	struct lwp *l;
    770      1.1   rmind 
    771      1.1   rmind 	spc = &ci->ci_schedstate;
    772      1.1   rmind 	ci_rq = ci->ci_schedstate.spc_sched_info;
    773      1.1   rmind 
    774      1.1   rmind #ifdef MULTIPROCESSOR
    775      1.1   rmind 	/* If runqueue is empty, try to catch some thread from other CPU */
    776  1.5.2.2  bouyer 	if (__predict_false(spc->spc_flags & SPCF_OFFLINE)) {
    777  1.5.2.2  bouyer 		if ((ci_rq->r_count - ci_rq->r_mcount) == 0)
    778      1.1   rmind 			return NULL;
    779      1.1   rmind 	} else if (ci_rq->r_count == 0) {
    780      1.1   rmind 		/* Reset the counter, and call the balancer */
    781      1.1   rmind 		ci_rq->r_avgcount = 0;
    782      1.1   rmind 		sched_balance(ci);
    783      1.1   rmind 
    784      1.1   rmind 		/* The re-locking will be done inside */
    785      1.1   rmind 		return sched_catchlwp();
    786      1.1   rmind 	}
    787      1.1   rmind #else
    788      1.1   rmind 	if (ci_rq->r_count == 0)
    789      1.1   rmind 		return NULL;
    790      1.1   rmind #endif
    791      1.1   rmind 
    792      1.1   rmind 	/* Take the highest priority thread */
    793      1.1   rmind 	KASSERT(ci_rq->r_bitmap[ci_rq->r_highest_pri >> BITMAP_SHIFT]);
    794      1.1   rmind 	q_head = sched_getrq(ci_rq, ci_rq->r_highest_pri);
    795      1.1   rmind 	l = TAILQ_FIRST(q_head);
    796      1.1   rmind 	KASSERT(l != NULL);
    797      1.1   rmind 
    798      1.1   rmind 	/* Update the counters */
    799      1.1   rmind 	sil = l->l_sched_info;
    800      1.1   rmind 	KASSERT(sil->sl_timeslice >= min_ts);
    801      1.1   rmind 	KASSERT(sil->sl_timeslice <= max_ts);
    802      1.1   rmind 	spc->spc_ticks = sil->sl_timeslice;
    803      1.1   rmind 	sil->sl_rtime = hardclock_ticks;
    804      1.1   rmind 
    805      1.1   rmind 	return l;
    806      1.1   rmind }
    807      1.1   rmind 
    808      1.1   rmind bool
    809      1.1   rmind sched_curcpu_runnable_p(void)
    810      1.1   rmind {
    811      1.1   rmind 	const struct cpu_info *ci = curcpu();
    812      1.1   rmind 	const runqueue_t *ci_rq = ci->ci_schedstate.spc_sched_info;
    813      1.1   rmind 
    814  1.5.2.2  bouyer #ifndef __HAVE_FAST_SOFTINTS
    815  1.5.2.2  bouyer 	if (ci->ci_data.cpu_softints)
    816  1.5.2.2  bouyer 		return true;
    817  1.5.2.2  bouyer #endif
    818  1.5.2.2  bouyer 
    819      1.1   rmind 	if (ci->ci_schedstate.spc_flags & SPCF_OFFLINE)
    820  1.5.2.2  bouyer 		return (ci_rq->r_count - ci_rq->r_mcount);
    821      1.1   rmind 
    822      1.1   rmind 	return ci_rq->r_count;
    823      1.1   rmind }
    824      1.1   rmind 
    825      1.1   rmind /*
    826      1.1   rmind  * Time-driven events.
    827      1.1   rmind  */
    828      1.1   rmind 
    829      1.1   rmind /*
    830      1.1   rmind  * Called once per time-quantum.  This routine is CPU-local and runs at
    831      1.1   rmind  * IPL_SCHED, thus the locking is not needed.
    832      1.1   rmind  */
    833      1.1   rmind void
    834      1.1   rmind sched_tick(struct cpu_info *ci)
    835      1.1   rmind {
    836      1.1   rmind 	const runqueue_t *ci_rq = ci->ci_schedstate.spc_sched_info;
    837      1.1   rmind 	struct schedstate_percpu *spc = &ci->ci_schedstate;
    838      1.1   rmind 	struct lwp *l = curlwp;
    839      1.1   rmind 	sched_info_lwp_t *sil = l->l_sched_info;
    840      1.1   rmind 
    841      1.2   rmind 	if (CURCPU_IDLE_P())
    842      1.2   rmind 		return;
    843      1.1   rmind 
    844  1.5.2.2  bouyer 	switch (l->l_class) {
    845      1.2   rmind 	case SCHED_FIFO:
    846      1.2   rmind 		/*
    847      1.2   rmind 		 * Update the time-quantum, and continue running,
    848      1.2   rmind 		 * if thread runs on FIFO real-time policy.
    849      1.2   rmind 		 */
    850      1.1   rmind 		spc->spc_ticks = sil->sl_timeslice;
    851      1.1   rmind 		return;
    852      1.2   rmind 	case SCHED_OTHER:
    853  1.5.2.2  bouyer 		/*
    854  1.5.2.2  bouyer 		 * If thread is in time-sharing queue, decrease the priority,
    855  1.5.2.2  bouyer 		 * and run with a higher time-quantum.
    856  1.5.2.2  bouyer 		 */
    857  1.5.2.2  bouyer 		if (l->l_priority > PRI_HIGHEST_TS)
    858      1.2   rmind 			break;
    859  1.5.2.2  bouyer 		if (l->l_priority != 0)
    860  1.5.2.2  bouyer 			l->l_priority--;
    861      1.2   rmind 		break;
    862      1.1   rmind 	}
    863      1.1   rmind 
    864      1.1   rmind 	/*
    865      1.2   rmind 	 * If there are higher priority threads or threads in the same queue,
    866      1.2   rmind 	 * mark that thread should yield, otherwise, continue running.
    867      1.1   rmind 	 */
    868  1.5.2.2  bouyer 	if (lwp_eprio(l) <= ci_rq->r_highest_pri) {
    869      1.1   rmind 		spc->spc_flags |= SPCF_SHOULDYIELD;
    870      1.1   rmind 		cpu_need_resched(ci, 0);
    871      1.1   rmind 	} else
    872      1.1   rmind 		spc->spc_ticks = sil->sl_timeslice;
    873      1.1   rmind }
    874      1.1   rmind 
    875      1.1   rmind /*
    876      1.1   rmind  * Sysctl nodes and initialization.
    877      1.1   rmind  */
    878      1.1   rmind 
    879      1.1   rmind static int
    880      1.1   rmind sysctl_sched_mints(SYSCTLFN_ARGS)
    881      1.1   rmind {
    882      1.1   rmind 	struct sysctlnode node;
    883      1.1   rmind 	struct cpu_info *ci;
    884      1.1   rmind 	int error, newsize;
    885      1.1   rmind 	CPU_INFO_ITERATOR cii;
    886      1.1   rmind 
    887      1.1   rmind 	node = *rnode;
    888      1.1   rmind 	node.sysctl_data = &newsize;
    889      1.1   rmind 
    890      1.1   rmind 	newsize = hztoms(min_ts);
    891      1.1   rmind 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    892      1.1   rmind 	if (error || newp == NULL)
    893      1.1   rmind 		return error;
    894      1.1   rmind 
    895  1.5.2.2  bouyer 	newsize = mstohz(newsize);
    896      1.1   rmind 	if (newsize < 1 || newsize > hz || newsize >= max_ts)
    897      1.1   rmind 		return EINVAL;
    898      1.1   rmind 
    899      1.1   rmind 	/* It is safe to do this in such order */
    900      1.1   rmind 	for (CPU_INFO_FOREACH(cii, ci))
    901      1.1   rmind 		spc_lock(ci);
    902      1.1   rmind 
    903  1.5.2.2  bouyer 	min_ts = newsize;
    904      1.1   rmind 	sched_precalcts();
    905      1.1   rmind 
    906      1.1   rmind 	for (CPU_INFO_FOREACH(cii, ci))
    907      1.1   rmind 		spc_unlock(ci);
    908      1.1   rmind 
    909      1.1   rmind 	return 0;
    910      1.1   rmind }
    911      1.1   rmind 
    912      1.1   rmind static int
    913      1.1   rmind sysctl_sched_maxts(SYSCTLFN_ARGS)
    914      1.1   rmind {
    915      1.1   rmind 	struct sysctlnode node;
    916      1.1   rmind 	struct cpu_info *ci;
    917      1.1   rmind 	int error, newsize;
    918      1.1   rmind 	CPU_INFO_ITERATOR cii;
    919      1.1   rmind 
    920      1.1   rmind 	node = *rnode;
    921      1.1   rmind 	node.sysctl_data = &newsize;
    922      1.1   rmind 
    923      1.1   rmind 	newsize = hztoms(max_ts);
    924      1.1   rmind 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    925      1.1   rmind 	if (error || newp == NULL)
    926      1.1   rmind 		return error;
    927      1.1   rmind 
    928  1.5.2.2  bouyer 	newsize = mstohz(newsize);
    929      1.1   rmind 	if (newsize < 10 || newsize > hz || newsize <= min_ts)
    930      1.1   rmind 		return EINVAL;
    931      1.1   rmind 
    932      1.1   rmind 	/* It is safe to do this in such order */
    933      1.1   rmind 	for (CPU_INFO_FOREACH(cii, ci))
    934      1.1   rmind 		spc_lock(ci);
    935      1.1   rmind 
    936  1.5.2.2  bouyer 	max_ts = newsize;
    937      1.1   rmind 	sched_precalcts();
    938      1.1   rmind 
    939      1.1   rmind 	for (CPU_INFO_FOREACH(cii, ci))
    940      1.1   rmind 		spc_unlock(ci);
    941      1.1   rmind 
    942      1.1   rmind 	return 0;
    943      1.1   rmind }
    944      1.1   rmind 
    945      1.1   rmind SYSCTL_SETUP(sysctl_sched_setup, "sysctl kern.sched subtree setup")
    946      1.1   rmind {
    947      1.1   rmind 	const struct sysctlnode *node = NULL;
    948      1.1   rmind 
    949      1.1   rmind 	sysctl_createv(clog, 0, NULL, NULL,
    950      1.1   rmind 		CTLFLAG_PERMANENT,
    951      1.1   rmind 		CTLTYPE_NODE, "kern", NULL,
    952      1.1   rmind 		NULL, 0, NULL, 0,
    953      1.1   rmind 		CTL_KERN, CTL_EOL);
    954      1.1   rmind 	sysctl_createv(clog, 0, NULL, &node,
    955      1.1   rmind 		CTLFLAG_PERMANENT,
    956      1.1   rmind 		CTLTYPE_NODE, "sched",
    957      1.1   rmind 		SYSCTL_DESCR("Scheduler options"),
    958      1.1   rmind 		NULL, 0, NULL, 0,
    959      1.1   rmind 		CTL_KERN, CTL_CREATE, CTL_EOL);
    960      1.1   rmind 
    961      1.1   rmind 	if (node == NULL)
    962      1.1   rmind 		return;
    963      1.1   rmind 
    964      1.1   rmind 	sysctl_createv(clog, 0, &node, NULL,
    965      1.1   rmind 		CTLFLAG_PERMANENT,
    966      1.1   rmind 		CTLTYPE_STRING, "name", NULL,
    967      1.1   rmind 		NULL, 0, __UNCONST("M2"), 0,
    968      1.1   rmind 		CTL_CREATE, CTL_EOL);
    969      1.1   rmind 	sysctl_createv(clog, 0, &node, NULL,
    970      1.1   rmind 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    971      1.1   rmind 		CTLTYPE_INT, "maxts",
    972  1.5.2.2  bouyer 		SYSCTL_DESCR("Maximal time quantum (in miliseconds)"),
    973      1.1   rmind 		sysctl_sched_maxts, 0, &max_ts, 0,
    974      1.1   rmind 		CTL_CREATE, CTL_EOL);
    975      1.1   rmind 	sysctl_createv(clog, 0, &node, NULL,
    976      1.1   rmind 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    977      1.1   rmind 		CTLTYPE_INT, "mints",
    978  1.5.2.2  bouyer 		SYSCTL_DESCR("Minimal time quantum (in miliseconds)"),
    979      1.1   rmind 		sysctl_sched_mints, 0, &min_ts, 0,
    980      1.1   rmind 		CTL_CREATE, CTL_EOL);
    981      1.1   rmind 
    982      1.1   rmind #ifdef MULTIPROCESSOR
    983      1.1   rmind 	sysctl_createv(clog, 0, &node, NULL,
    984      1.1   rmind 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    985      1.1   rmind 		CTLTYPE_INT, "cacheht_time",
    986  1.5.2.2  bouyer 		SYSCTL_DESCR("Cache hotness time (in ticks)"),
    987      1.1   rmind 		NULL, 0, &cacheht_time, 0,
    988      1.1   rmind 		CTL_CREATE, CTL_EOL);
    989      1.1   rmind 	sysctl_createv(clog, 0, &node, NULL,
    990      1.1   rmind 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    991      1.1   rmind 		CTLTYPE_INT, "balance_period",
    992  1.5.2.2  bouyer 		SYSCTL_DESCR("Balance period (in ticks)"),
    993      1.1   rmind 		NULL, 0, &balance_period, 0,
    994      1.1   rmind 		CTL_CREATE, CTL_EOL);
    995      1.1   rmind 	sysctl_createv(clog, 0, &node, NULL,
    996      1.1   rmind 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    997      1.1   rmind 		CTLTYPE_INT, "min_catch",
    998  1.5.2.2  bouyer 		SYSCTL_DESCR("Minimal count of the threads for catching"),
    999      1.1   rmind 		NULL, 0, &min_catch, 0,
   1000      1.1   rmind 		CTL_CREATE, CTL_EOL);
   1001      1.1   rmind #endif
   1002      1.1   rmind }
   1003      1.1   rmind 
   1004      1.1   rmind /*
   1005      1.1   rmind  * Debugging.
   1006      1.1   rmind  */
   1007      1.1   rmind 
   1008      1.1   rmind #ifdef DDB
   1009      1.1   rmind 
   1010      1.1   rmind void
   1011      1.1   rmind sched_print_runqueue(void (*pr)(const char *, ...))
   1012      1.1   rmind {
   1013      1.1   rmind 	runqueue_t *ci_rq;
   1014      1.1   rmind 	sched_info_lwp_t *sil;
   1015      1.1   rmind 	struct lwp *l;
   1016      1.1   rmind 	struct proc *p;
   1017      1.1   rmind 	int i;
   1018      1.1   rmind 
   1019      1.1   rmind 	struct cpu_info *ci;
   1020      1.1   rmind 	CPU_INFO_ITERATOR cii;
   1021      1.1   rmind 
   1022      1.1   rmind 	for (CPU_INFO_FOREACH(cii, ci)) {
   1023      1.1   rmind 		ci_rq = ci->ci_schedstate.spc_sched_info;
   1024      1.1   rmind 
   1025      1.1   rmind 		(*pr)("Run-queue (CPU = %d):\n", ci->ci_cpuid);
   1026      1.1   rmind 		(*pr)(" pid.lid = %d.%d, threads count = %u, "
   1027      1.1   rmind 		    "avgcount = %u, highest pri = %d\n",
   1028      1.1   rmind 		    ci->ci_curlwp->l_proc->p_pid, ci->ci_curlwp->l_lid,
   1029      1.1   rmind 		    ci_rq->r_count, ci_rq->r_avgcount, ci_rq->r_highest_pri);
   1030  1.5.2.2  bouyer 		i = (PRI_COUNT >> BITMAP_SHIFT) - 1;
   1031      1.1   rmind 		do {
   1032  1.5.2.2  bouyer 			uint32_t q;
   1033  1.5.2.2  bouyer 			q = ci_rq->r_bitmap[i];
   1034  1.5.2.2  bouyer 			(*pr)(" bitmap[%d] => [ %d (0x%x) ]\n", i, ffs(q), q);
   1035  1.5.2.2  bouyer 		} while (i--);
   1036      1.1   rmind 	}
   1037      1.1   rmind 
   1038      1.1   rmind 	(*pr)("   %5s %4s %4s %10s %3s %4s %11s %3s %s\n",
   1039  1.5.2.2  bouyer 	    "LID", "PRI", "EPRI", "FL", "ST", "TS", "LWP", "CPU", "LRTIME");
   1040      1.1   rmind 
   1041      1.1   rmind 	PROCLIST_FOREACH(p, &allproc) {
   1042      1.1   rmind 		(*pr)(" /- %d (%s)\n", (int)p->p_pid, p->p_comm);
   1043      1.1   rmind 		LIST_FOREACH(l, &p->p_lwps, l_sibling) {
   1044      1.1   rmind 			sil = l->l_sched_info;
   1045      1.1   rmind 			ci = l->l_cpu;
   1046      1.1   rmind 			(*pr)(" | %5d %4u %4u 0x%8.8x %3s %4u %11p %3d "
   1047      1.1   rmind 			    "%u ST=%d RT=%d %d\n",
   1048  1.5.2.2  bouyer 			    (int)l->l_lid, l->l_priority, lwp_eprio(l),
   1049      1.1   rmind 			    l->l_flag, l->l_stat == LSRUN ? "RQ" :
   1050      1.1   rmind 			    (l->l_stat == LSSLEEP ? "SQ" : "-"),
   1051      1.1   rmind 			    sil->sl_timeslice, l, ci->ci_cpuid,
   1052      1.1   rmind 			    (u_int)(hardclock_ticks - sil->sl_lrtime),
   1053      1.1   rmind 			    sil->sl_slpsum, sil->sl_rtsum, sil->sl_flags);
   1054      1.1   rmind 		}
   1055      1.1   rmind 	}
   1056      1.1   rmind }
   1057      1.1   rmind 
   1058      1.1   rmind #endif /* defined(DDB) */
   1059