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