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