The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/sched_ule.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2002-2007, Jeffrey Roberson <jeff@freebsd.org>
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice unmodified, this list of conditions, and the following
   10  *    disclaimer.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  *
   15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   25  */
   26 
   27 /*
   28  * This file implements the ULE scheduler.  ULE supports independent CPU
   29  * run queues and fine grain locking.  It has superior interactive
   30  * performance under load even on uni-processor systems.
   31  *
   32  * etymology:
   33  *   ULE is the last three letters in schedule.  It owes its name to a
   34  * generic user created for a scheduling system by Paul Mikesell at
   35  * Isilon Systems and a general lack of creativity on the part of the author.
   36  */
   37 
   38 #include <sys/cdefs.h>
   39 __FBSDID("$FreeBSD: releng/7.3/sys/kern/sched_ule.c 203704 2010-02-09 14:56:10Z attilio $");
   40 
   41 #include "opt_hwpmc_hooks.h"
   42 #include "opt_kdtrace.h"
   43 #include "opt_sched.h"
   44 
   45 #include <sys/param.h>
   46 #include <sys/systm.h>
   47 #include <sys/kdb.h>
   48 #include <sys/kernel.h>
   49 #include <sys/ktr.h>
   50 #include <sys/lock.h>
   51 #include <sys/mutex.h>
   52 #include <sys/proc.h>
   53 #include <sys/resource.h>
   54 #include <sys/resourcevar.h>
   55 #include <sys/sched.h>
   56 #include <sys/smp.h>
   57 #include <sys/sx.h>
   58 #include <sys/sysctl.h>
   59 #include <sys/sysproto.h>
   60 #include <sys/turnstile.h>
   61 #include <sys/umtx.h>
   62 #include <sys/vmmeter.h>
   63 #include <sys/cpuset.h>
   64 #ifdef KTRACE
   65 #include <sys/uio.h>
   66 #include <sys/ktrace.h>
   67 #endif
   68 
   69 #ifdef HWPMC_HOOKS
   70 #include <sys/pmckern.h>
   71 #endif
   72 
   73 #ifdef KDTRACE_HOOKS
   74 #include <sys/dtrace_bsd.h>
   75 int                             dtrace_vtime_active;
   76 dtrace_vtime_switch_func_t      dtrace_vtime_switch_func;
   77 #endif
   78 
   79 #include <machine/cpu.h>
   80 #include <machine/smp.h>
   81 
   82 #if !defined(__i386__) && !defined(__amd64__) && !defined(__arm__)
   83 #error "This architecture is not currently compatible with ULE"
   84 #endif
   85 
   86 #define KTR_ULE 0
   87 
   88 /*
   89  * Thread scheduler specific section.  All fields are protected
   90  * by the thread lock.
   91  */
   92 struct td_sched {       
   93         TAILQ_ENTRY(td_sched) ts_procq; /* Run queue. */
   94         struct thread   *ts_thread;     /* Active associated thread. */
   95         struct runq     *ts_runq;       /* Run-queue we're queued on. */
   96         short           ts_flags;       /* TSF_* flags. */
   97         u_char          ts_rqindex;     /* Run queue index. */
   98         u_char          ts_cpu;         /* CPU that we have affinity for. */
   99         int             ts_slice;       /* Ticks of slice remaining. */
  100         u_int           ts_slptime;     /* Number of ticks we vol. slept */
  101         u_int           ts_runtime;     /* Number of ticks we were running */
  102         /* The following variables are only used for pctcpu calculation */
  103         int             ts_ltick;       /* Last tick that we were running on */
  104         int             ts_incrtick;    /* Last tick that we incremented on */
  105         int             ts_ftick;       /* First tick that we were running on */
  106         int             ts_ticks;       /* Tick count */
  107 #ifdef SMP
  108         int             ts_rltick;      /* Real last tick, for affinity. */
  109 #endif
  110 };
  111 /* flags kept in ts_flags */
  112 #define TSF_BOUND       0x0001          /* Thread can not migrate. */
  113 #define TSF_XFERABLE    0x0002          /* Thread was added as transferable. */
  114 
  115 static struct td_sched td_sched0;
  116 
  117 #define THREAD_CAN_MIGRATE(td)  ((td)->td_pinned == 0)
  118 #define THREAD_CAN_SCHED(td, cpu)       \
  119     CPU_ISSET((cpu), &(td)->td_cpuset->cs_mask)
  120 
  121 /*
  122  * Cpu percentage computation macros and defines.
  123  *
  124  * SCHED_TICK_SECS:     Number of seconds to average the cpu usage across.
  125  * SCHED_TICK_TARG:     Number of hz ticks to average the cpu usage across.
  126  * SCHED_TICK_MAX:      Maximum number of ticks before scaling back.
  127  * SCHED_TICK_SHIFT:    Shift factor to avoid rounding away results.
  128  * SCHED_TICK_HZ:       Compute the number of hz ticks for a given ticks count.
  129  * SCHED_TICK_TOTAL:    Gives the amount of time we've been recording ticks.
  130  */
  131 #define SCHED_TICK_SECS         10
  132 #define SCHED_TICK_TARG         (hz * SCHED_TICK_SECS)
  133 #define SCHED_TICK_MAX          (SCHED_TICK_TARG + hz)
  134 #define SCHED_TICK_SHIFT        10
  135 #define SCHED_TICK_HZ(ts)       ((ts)->ts_ticks >> SCHED_TICK_SHIFT)
  136 #define SCHED_TICK_TOTAL(ts)    (max((ts)->ts_ltick - (ts)->ts_ftick, hz))
  137 
  138 /*
  139  * These macros determine priorities for non-interactive threads.  They are
  140  * assigned a priority based on their recent cpu utilization as expressed
  141  * by the ratio of ticks to the tick total.  NHALF priorities at the start
  142  * and end of the MIN to MAX timeshare range are only reachable with negative
  143  * or positive nice respectively.
  144  *
  145  * PRI_RANGE:   Priority range for utilization dependent priorities.
  146  * PRI_NRESV:   Number of nice values.
  147  * PRI_TICKS:   Compute a priority in PRI_RANGE from the ticks count and total.
  148  * PRI_NICE:    Determines the part of the priority inherited from nice.
  149  */
  150 #define SCHED_PRI_NRESV         (PRIO_MAX - PRIO_MIN)
  151 #define SCHED_PRI_NHALF         (SCHED_PRI_NRESV / 2)
  152 #define SCHED_PRI_MIN           (PRI_MIN_TIMESHARE + SCHED_PRI_NHALF)
  153 #define SCHED_PRI_MAX           (PRI_MAX_TIMESHARE - SCHED_PRI_NHALF)
  154 #define SCHED_PRI_RANGE         (SCHED_PRI_MAX - SCHED_PRI_MIN)
  155 #define SCHED_PRI_TICKS(ts)                                             \
  156     (SCHED_TICK_HZ((ts)) /                                              \
  157     (roundup(SCHED_TICK_TOTAL((ts)), SCHED_PRI_RANGE) / SCHED_PRI_RANGE))
  158 #define SCHED_PRI_NICE(nice)    (nice)
  159 
  160 /*
  161  * These determine the interactivity of a process.  Interactivity differs from
  162  * cpu utilization in that it expresses the voluntary time slept vs time ran
  163  * while cpu utilization includes all time not running.  This more accurately
  164  * models the intent of the thread.
  165  *
  166  * SLP_RUN_MAX: Maximum amount of sleep time + run time we'll accumulate
  167  *              before throttling back.
  168  * SLP_RUN_FORK:        Maximum slp+run time to inherit at fork time.
  169  * INTERACT_MAX:        Maximum interactivity value.  Smaller is better.
  170  * INTERACT_THRESH:     Threshhold for placement on the current runq.
  171  */
  172 #define SCHED_SLP_RUN_MAX       ((hz * 5) << SCHED_TICK_SHIFT)
  173 #define SCHED_SLP_RUN_FORK      ((hz / 2) << SCHED_TICK_SHIFT)
  174 #define SCHED_INTERACT_MAX      (100)
  175 #define SCHED_INTERACT_HALF     (SCHED_INTERACT_MAX / 2)
  176 #define SCHED_INTERACT_THRESH   (30)
  177 
  178 /*
  179  * tickincr:            Converts a stathz tick into a hz domain scaled by
  180  *                      the shift factor.  Without the shift the error rate
  181  *                      due to rounding would be unacceptably high.
  182  * realstathz:          stathz is sometimes 0 and run off of hz.
  183  * sched_slice:         Runtime of each thread before rescheduling.
  184  * preempt_thresh:      Priority threshold for preemption and remote IPIs.
  185  */
  186 static int sched_interact = SCHED_INTERACT_THRESH;
  187 static int realstathz;
  188 static int tickincr;
  189 static int sched_slice;
  190 #ifdef PREEMPTION
  191 #ifdef FULL_PREEMPTION
  192 static int preempt_thresh = PRI_MAX_IDLE;
  193 #else
  194 static int preempt_thresh = PRI_MIN_KERN;
  195 #endif
  196 #else 
  197 static int preempt_thresh = 0;
  198 #endif
  199 
  200 /*
  201  * tdq - per processor runqs and statistics.  All fields are protected by the
  202  * tdq_lock.  The load and lowpri may be accessed without to avoid excess
  203  * locking in sched_pickcpu();
  204  */
  205 struct tdq {
  206         struct mtx      *tdq_lock;              /* Pointer to group lock. */
  207         struct runq     tdq_realtime;           /* real-time run queue. */
  208         struct runq     tdq_timeshare;          /* timeshare run queue. */
  209         struct runq     tdq_idle;               /* Queue of IDLE threads. */
  210         int             tdq_load;               /* Aggregate load. */
  211         u_char          tdq_idx;                /* Current insert index. */
  212         u_char          tdq_ridx;               /* Current removal index. */
  213 #ifdef SMP
  214         u_char          tdq_lowpri;             /* Lowest priority thread. */
  215         int             tdq_transferable;       /* Transferable thread count. */
  216         LIST_ENTRY(tdq) tdq_siblings;           /* Next in tdq group. */
  217         struct tdq_group *tdq_group;            /* Our processor group. */
  218 #else
  219         int             tdq_sysload;            /* For loadavg, !ITHD load. */
  220 #endif
  221 } __aligned(64);
  222 
  223 
  224 #ifdef SMP
  225 /*
  226  * tdq groups are groups of processors which can cheaply share threads.  When
  227  * one processor in the group goes idle it will check the runqs of the other
  228  * processors in its group prior to halting and waiting for an interrupt.
  229  * These groups are suitable for SMT (Symetric Multi-Threading) and not NUMA.
  230  * In a numa environment we'd want an idle bitmap per group and a two tiered
  231  * load balancer.
  232  */
  233 struct tdq_group {
  234         struct mtx      tdg_lock;       /* Protects all fields below. */
  235         int             tdg_cpus;       /* Count of CPUs in this tdq group. */
  236         cpumask_t       tdg_cpumask;    /* Mask of cpus in this group. */
  237         cpumask_t       tdg_idlemask;   /* Idle cpus in this group. */
  238         cpumask_t       tdg_mask;       /* Bit mask for first cpu. */
  239         int             tdg_load;       /* Total load of this group. */
  240         int     tdg_transferable;       /* Transferable load of this group. */
  241         LIST_HEAD(, tdq) tdg_members;   /* Linked list of all members. */
  242         char            tdg_name[16];   /* lock name. */
  243 } __aligned(64);
  244 
  245 #define SCHED_AFFINITY_DEFAULT  (max(1, hz / 300))
  246 #define SCHED_AFFINITY(ts)      ((ts)->ts_rltick > ticks - affinity)
  247 
  248 /*
  249  * Run-time tunables.
  250  */
  251 static int rebalance = 1;
  252 static int balance_interval = 128;      /* Default set in sched_initticks(). */
  253 static int pick_pri = 1;
  254 static int affinity;
  255 static int tryself = 1;
  256 static int steal_htt = 1;
  257 static int steal_idle = 1;
  258 static int steal_thresh = 2;
  259 static int topology = 0;
  260 
  261 /*
  262  * One thread queue per processor.
  263  */
  264 static volatile cpumask_t tdq_idle;
  265 static int tdg_maxid;
  266 static struct tdq       tdq_cpu[MAXCPU];
  267 static struct tdq_group tdq_groups[MAXCPU];
  268 static struct tdq       *balance_tdq;
  269 static int balance_group_ticks;
  270 static int balance_ticks;
  271 
  272 #define TDQ_SELF()      (&tdq_cpu[PCPU_GET(cpuid)])
  273 #define TDQ_CPU(x)      (&tdq_cpu[(x)])
  274 #define TDQ_ID(x)       ((int)((x) - tdq_cpu))
  275 #define TDQ_GROUP(x)    (&tdq_groups[(x)])
  276 #define TDG_ID(x)       ((int)((x) - tdq_groups))
  277 #else   /* !SMP */
  278 static struct tdq       tdq_cpu;
  279 static struct mtx       tdq_lock;
  280 
  281 #define TDQ_ID(x)       (0)
  282 #define TDQ_SELF()      (&tdq_cpu)
  283 #define TDQ_CPU(x)      (&tdq_cpu)
  284 #endif
  285 
  286 #define TDQ_LOCK_ASSERT(t, type)        mtx_assert(TDQ_LOCKPTR((t)), (type))
  287 #define TDQ_LOCK(t)             mtx_lock_spin(TDQ_LOCKPTR((t)))
  288 #define TDQ_LOCK_FLAGS(t, f)    mtx_lock_spin_flags(TDQ_LOCKPTR((t)), (f))
  289 #define TDQ_UNLOCK(t)           mtx_unlock_spin(TDQ_LOCKPTR((t)))
  290 #define TDQ_LOCKPTR(t)          ((t)->tdq_lock)
  291 
  292 static void sched_priority(struct thread *);
  293 static void sched_thread_priority(struct thread *, u_char);
  294 static int sched_interact_score(struct thread *);
  295 static void sched_interact_update(struct thread *);
  296 static void sched_interact_fork(struct thread *);
  297 static void sched_pctcpu_update(struct td_sched *);
  298 
  299 /* Operations on per processor queues */
  300 static struct td_sched * tdq_choose(struct tdq *);
  301 static void tdq_setup(struct tdq *);
  302 static void tdq_load_add(struct tdq *, struct td_sched *);
  303 static void tdq_load_rem(struct tdq *, struct td_sched *);
  304 static __inline void tdq_runq_add(struct tdq *, struct td_sched *, int);
  305 static __inline void tdq_runq_rem(struct tdq *, struct td_sched *);
  306 void tdq_print(int cpu);
  307 static void runq_print(struct runq *rq);
  308 static void tdq_add(struct tdq *, struct thread *, int);
  309 #ifdef SMP
  310 static void tdq_move(struct tdq *, struct tdq *);
  311 static int tdq_idled(struct tdq *);
  312 static void tdq_notify(struct td_sched *);
  313 static struct td_sched *tdq_steal(struct tdq *, int);
  314 static struct td_sched *runq_steal(struct runq *, int);
  315 static int sched_pickcpu(struct thread *, int);
  316 static void sched_balance(void);
  317 static void sched_balance_groups(void);
  318 static void sched_balance_group(struct tdq_group *);
  319 static void sched_balance_pair(struct tdq *, struct tdq *);
  320 static inline struct tdq *sched_setcpu(struct td_sched *, int, int);
  321 static inline void thread_unblock_switch(struct thread *, struct mtx *);
  322 static struct mtx *sched_switch_migrate(struct tdq *, struct thread *, int);
  323 #endif
  324 
  325 static void sched_setup(void *dummy);
  326 SYSINIT(sched_setup, SI_SUB_RUN_QUEUE, SI_ORDER_FIRST, sched_setup, NULL);
  327 
  328 static void sched_initticks(void *dummy);
  329 SYSINIT(sched_initticks, SI_SUB_CLOCKS, SI_ORDER_THIRD, sched_initticks,
  330     NULL);
  331 
  332 /*
  333  * Print the threads waiting on a run-queue.
  334  */
  335 static void
  336 runq_print(struct runq *rq)
  337 {
  338         struct rqhead *rqh;
  339         struct td_sched *ts;
  340         int pri;
  341         int j;
  342         int i;
  343 
  344         for (i = 0; i < RQB_LEN; i++) {
  345                 printf("\t\trunq bits %d 0x%zx\n",
  346                     i, rq->rq_status.rqb_bits[i]);
  347                 for (j = 0; j < RQB_BPW; j++)
  348                         if (rq->rq_status.rqb_bits[i] & (1ul << j)) {
  349                                 pri = j + (i << RQB_L2BPW);
  350                                 rqh = &rq->rq_queues[pri];
  351                                 TAILQ_FOREACH(ts, rqh, ts_procq) {
  352                                         printf("\t\t\ttd %p(%s) priority %d rqindex %d pri %d\n",
  353                                             ts->ts_thread, ts->ts_thread->td_proc->p_comm, ts->ts_thread->td_priority, ts->ts_rqindex, pri);
  354                                 }
  355                         }
  356         }
  357 }
  358 
  359 /*
  360  * Print the status of a per-cpu thread queue.  Should be a ddb show cmd.
  361  */
  362 void
  363 tdq_print(int cpu)
  364 {
  365         struct tdq *tdq;
  366 
  367         tdq = TDQ_CPU(cpu);
  368 
  369         printf("tdq %d:\n", TDQ_ID(tdq));
  370         printf("\tlockptr         %p\n", TDQ_LOCKPTR(tdq));
  371         printf("\tload:           %d\n", tdq->tdq_load);
  372         printf("\ttimeshare idx:  %d\n", tdq->tdq_idx);
  373         printf("\ttimeshare ridx: %d\n", tdq->tdq_ridx);
  374         printf("\trealtime runq:\n");
  375         runq_print(&tdq->tdq_realtime);
  376         printf("\ttimeshare runq:\n");
  377         runq_print(&tdq->tdq_timeshare);
  378         printf("\tidle runq:\n");
  379         runq_print(&tdq->tdq_idle);
  380 #ifdef SMP
  381         printf("\tload transferable: %d\n", tdq->tdq_transferable);
  382         printf("\tlowest priority:   %d\n", tdq->tdq_lowpri);
  383         printf("\tgroup:             %d\n", TDG_ID(tdq->tdq_group));
  384         printf("\tLock name:         %s\n", tdq->tdq_group->tdg_name);
  385 #endif
  386 }
  387 
  388 #define TS_RQ_PPQ       (((PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE) + 1) / RQ_NQS)
  389 /*
  390  * Add a thread to the actual run-queue.  Keeps transferable counts up to
  391  * date with what is actually on the run-queue.  Selects the correct
  392  * queue position for timeshare threads.
  393  */
  394 static __inline void
  395 tdq_runq_add(struct tdq *tdq, struct td_sched *ts, int flags)
  396 {
  397         TDQ_LOCK_ASSERT(tdq, MA_OWNED);
  398         THREAD_LOCK_ASSERT(ts->ts_thread, MA_OWNED);
  399 #ifdef SMP
  400         if (THREAD_CAN_MIGRATE(ts->ts_thread)) {
  401                 tdq->tdq_transferable++;
  402                 tdq->tdq_group->tdg_transferable++;
  403                 ts->ts_flags |= TSF_XFERABLE;
  404         }
  405 #endif
  406         if (ts->ts_runq == &tdq->tdq_timeshare) {
  407                 u_char pri;
  408 
  409                 pri = ts->ts_thread->td_priority;
  410                 KASSERT(pri <= PRI_MAX_TIMESHARE && pri >= PRI_MIN_TIMESHARE,
  411                         ("Invalid priority %d on timeshare runq", pri));
  412                 /*
  413                  * This queue contains only priorities between MIN and MAX
  414                  * realtime.  Use the whole queue to represent these values.
  415                  */
  416                 if ((flags & (SRQ_BORROWING|SRQ_PREEMPTED)) == 0) {
  417                         pri = (pri - PRI_MIN_TIMESHARE) / TS_RQ_PPQ;
  418                         pri = (pri + tdq->tdq_idx) % RQ_NQS;
  419                         /*
  420                          * This effectively shortens the queue by one so we
  421                          * can have a one slot difference between idx and
  422                          * ridx while we wait for threads to drain.
  423                          */
  424                         if (tdq->tdq_ridx != tdq->tdq_idx &&
  425                             pri == tdq->tdq_ridx)
  426                                 pri = (unsigned char)(pri - 1) % RQ_NQS;
  427                 } else
  428                         pri = tdq->tdq_ridx;
  429                 runq_add_pri(ts->ts_runq, ts, pri, flags);
  430         } else
  431                 runq_add(ts->ts_runq, ts, flags);
  432 }
  433 
  434 /* 
  435  * Remove a thread from a run-queue.  This typically happens when a thread
  436  * is selected to run.  Running threads are not on the queue and the
  437  * transferable count does not reflect them.
  438  */
  439 static __inline void
  440 tdq_runq_rem(struct tdq *tdq, struct td_sched *ts)
  441 {
  442         TDQ_LOCK_ASSERT(tdq, MA_OWNED);
  443         KASSERT(ts->ts_runq != NULL,
  444             ("tdq_runq_remove: thread %p null ts_runq", ts->ts_thread));
  445 #ifdef SMP
  446         if (ts->ts_flags & TSF_XFERABLE) {
  447                 tdq->tdq_transferable--;
  448                 tdq->tdq_group->tdg_transferable--;
  449                 ts->ts_flags &= ~TSF_XFERABLE;
  450         }
  451 #endif
  452         if (ts->ts_runq == &tdq->tdq_timeshare) {
  453                 if (tdq->tdq_idx != tdq->tdq_ridx)
  454                         runq_remove_idx(ts->ts_runq, ts, &tdq->tdq_ridx);
  455                 else
  456                         runq_remove_idx(ts->ts_runq, ts, NULL);
  457                 /*
  458                  * For timeshare threads we update the priority here so
  459                  * the priority reflects the time we've been sleeping.
  460                  */
  461                 ts->ts_ltick = ticks;
  462                 sched_pctcpu_update(ts);
  463                 sched_priority(ts->ts_thread);
  464         } else
  465                 runq_remove(ts->ts_runq, ts);
  466 }
  467 
  468 /*
  469  * Load is maintained for all threads RUNNING and ON_RUNQ.  Add the load
  470  * for this thread to the referenced thread queue.
  471  */
  472 static void
  473 tdq_load_add(struct tdq *tdq, struct td_sched *ts)
  474 {
  475         int class;
  476 
  477         TDQ_LOCK_ASSERT(tdq, MA_OWNED);
  478         THREAD_LOCK_ASSERT(ts->ts_thread, MA_OWNED);
  479         class = PRI_BASE(ts->ts_thread->td_pri_class);
  480         tdq->tdq_load++;
  481         CTR2(KTR_SCHED, "cpu %d load: %d", TDQ_ID(tdq), tdq->tdq_load);
  482         if (class != PRI_ITHD &&
  483             (ts->ts_thread->td_proc->p_flag & P_NOLOAD) == 0)
  484 #ifdef SMP
  485                 tdq->tdq_group->tdg_load++;
  486 #else
  487                 tdq->tdq_sysload++;
  488 #endif
  489 }
  490 
  491 /*
  492  * Remove the load from a thread that is transitioning to a sleep state or
  493  * exiting.
  494  */
  495 static void
  496 tdq_load_rem(struct tdq *tdq, struct td_sched *ts)
  497 {
  498         int class;
  499 
  500         THREAD_LOCK_ASSERT(ts->ts_thread, MA_OWNED);
  501         TDQ_LOCK_ASSERT(tdq, MA_OWNED);
  502         class = PRI_BASE(ts->ts_thread->td_pri_class);
  503         if (class != PRI_ITHD &&
  504             (ts->ts_thread->td_proc->p_flag & P_NOLOAD) == 0)
  505 #ifdef SMP
  506                 tdq->tdq_group->tdg_load--;
  507 #else
  508                 tdq->tdq_sysload--;
  509 #endif
  510         KASSERT(tdq->tdq_load != 0,
  511             ("tdq_load_rem: Removing with 0 load on queue %d", TDQ_ID(tdq)));
  512         tdq->tdq_load--;
  513         CTR1(KTR_SCHED, "load: %d", tdq->tdq_load);
  514         ts->ts_runq = NULL;
  515 }
  516 
  517 #ifdef SMP
  518 /*
  519  * sched_balance is a simple CPU load balancing algorithm.  It operates by
  520  * finding the least loaded and most loaded cpu and equalizing their load
  521  * by migrating some processes.
  522  *
  523  * Dealing only with two CPUs at a time has two advantages.  Firstly, most
  524  * installations will only have 2 cpus.  Secondly, load balancing too much at
  525  * once can have an unpleasant effect on the system.  The scheduler rarely has
  526  * enough information to make perfect decisions.  So this algorithm chooses
  527  * simplicity and more gradual effects on load in larger systems.
  528  *
  529  */
  530 static void
  531 sched_balance()
  532 {
  533         struct tdq_group *high;
  534         struct tdq_group *low;
  535         struct tdq_group *tdg;
  536         struct tdq *tdq;
  537         int cnt;
  538         int i;
  539 
  540         /*
  541          * Select a random time between .5 * balance_interval and
  542          * 1.5 * balance_interval.
  543          */
  544         balance_ticks = max(balance_interval / 2, 1);
  545         balance_ticks += random() % balance_interval;
  546         if (smp_started == 0 || rebalance == 0)
  547                 return;
  548         tdq = TDQ_SELF();
  549         TDQ_UNLOCK(tdq);
  550         low = high = NULL;
  551         i = random() % (tdg_maxid + 1);
  552         for (cnt = 0; cnt <= tdg_maxid; cnt++) {
  553                 tdg = TDQ_GROUP(i);
  554                 /*
  555                  * Find the CPU with the highest load that has some
  556                  * threads to transfer.
  557                  */
  558                 if ((high == NULL || tdg->tdg_load > high->tdg_load)
  559                     && tdg->tdg_transferable)
  560                         high = tdg;
  561                 if (low == NULL || tdg->tdg_load < low->tdg_load)
  562                         low = tdg;
  563                 if (++i > tdg_maxid)
  564                         i = 0;
  565         }
  566         if (low != NULL && high != NULL && high != low)
  567                 sched_balance_pair(LIST_FIRST(&high->tdg_members),
  568                     LIST_FIRST(&low->tdg_members));
  569         TDQ_LOCK(tdq);
  570 }
  571 
  572 /*
  573  * Balance load between CPUs in a group.  Will only migrate within the group.
  574  */
  575 static void
  576 sched_balance_groups()
  577 {
  578         struct tdq *tdq;
  579         int i;
  580 
  581         /*
  582          * Select a random time between .5 * balance_interval and
  583          * 1.5 * balance_interval.
  584          */
  585         balance_group_ticks = max(balance_interval / 2, 1);
  586         balance_group_ticks += random() % balance_interval;
  587         if (smp_started == 0 || rebalance == 0)
  588                 return;
  589         tdq = TDQ_SELF();
  590         TDQ_UNLOCK(tdq);
  591         for (i = 0; i <= tdg_maxid; i++)
  592                 sched_balance_group(TDQ_GROUP(i));
  593         TDQ_LOCK(tdq);
  594 }
  595 
  596 /*
  597  * Finds the greatest imbalance between two tdqs in a group.
  598  */
  599 static void
  600 sched_balance_group(struct tdq_group *tdg)
  601 {
  602         struct tdq *tdq;
  603         struct tdq *high;
  604         struct tdq *low;
  605         int load;
  606 
  607         if (tdg->tdg_transferable == 0)
  608                 return;
  609         low = NULL;
  610         high = NULL;
  611         LIST_FOREACH(tdq, &tdg->tdg_members, tdq_siblings) {
  612                 load = tdq->tdq_load;
  613                 if (high == NULL || load > high->tdq_load)
  614                         high = tdq;
  615                 if (low == NULL || load < low->tdq_load)
  616                         low = tdq;
  617         }
  618         if (high != NULL && low != NULL && high != low)
  619                 sched_balance_pair(high, low);
  620 }
  621 
  622 /*
  623  * Lock two thread queues using their address to maintain lock order.
  624  */
  625 static void
  626 tdq_lock_pair(struct tdq *one, struct tdq *two)
  627 {
  628         if (one < two) {
  629                 TDQ_LOCK(one);
  630                 TDQ_LOCK_FLAGS(two, MTX_DUPOK);
  631         } else {
  632                 TDQ_LOCK(two);
  633                 TDQ_LOCK_FLAGS(one, MTX_DUPOK);
  634         }
  635 }
  636 
  637 /*
  638  * Unlock two thread queues.  Order is not important here.
  639  */
  640 static void
  641 tdq_unlock_pair(struct tdq *one, struct tdq *two)
  642 {
  643         TDQ_UNLOCK(one);
  644         TDQ_UNLOCK(two);
  645 }
  646 
  647 /*
  648  * Transfer load between two imbalanced thread queues.
  649  */
  650 static void
  651 sched_balance_pair(struct tdq *high, struct tdq *low)
  652 {
  653         int transferable;
  654         int high_load;
  655         int low_load;
  656         int move;
  657         int diff;
  658         int i;
  659 
  660         tdq_lock_pair(high, low);
  661         /*
  662          * If we're transfering within a group we have to use this specific
  663          * tdq's transferable count, otherwise we can steal from other members
  664          * of the group.
  665          */
  666         if (high->tdq_group == low->tdq_group) {
  667                 transferable = high->tdq_transferable;
  668                 high_load = high->tdq_load;
  669                 low_load = low->tdq_load;
  670         } else {
  671                 transferable = high->tdq_group->tdg_transferable;
  672                 high_load = high->tdq_group->tdg_load;
  673                 low_load = low->tdq_group->tdg_load;
  674         }
  675         /*
  676          * Determine what the imbalance is and then adjust that to how many
  677          * threads we actually have to give up (transferable).
  678          */
  679         if (transferable != 0) {
  680                 diff = high_load - low_load;
  681                 move = diff / 2;
  682                 if (diff & 0x1)
  683                         move++;
  684                 move = min(move, transferable);
  685                 for (i = 0; i < move; i++)
  686                         tdq_move(high, low);
  687                 /*
  688                  * IPI the target cpu to force it to reschedule with the new
  689                  * workload.
  690                  */
  691                 ipi_selected(1 << TDQ_ID(low), IPI_PREEMPT);
  692         }
  693         tdq_unlock_pair(high, low);
  694         return;
  695 }
  696 
  697 /*
  698  * Move a thread from one thread queue to another.
  699  */
  700 static void
  701 tdq_move(struct tdq *from, struct tdq *to)
  702 {
  703         struct td_sched *ts;
  704         struct thread *td;
  705         struct tdq *tdq;
  706         int cpu;
  707 
  708         TDQ_LOCK_ASSERT(from, MA_OWNED);
  709         TDQ_LOCK_ASSERT(to, MA_OWNED);
  710 
  711         tdq = from;
  712         cpu = TDQ_ID(to);
  713         ts = tdq_steal(tdq, cpu);
  714         if (ts == NULL) {
  715                 struct tdq_group *tdg;
  716 
  717                 tdg = tdq->tdq_group;
  718                 LIST_FOREACH(tdq, &tdg->tdg_members, tdq_siblings) {
  719                         if (tdq == from || tdq->tdq_transferable == 0)
  720                                 continue;
  721                         ts = tdq_steal(tdq, cpu);
  722                         break;
  723                 }
  724                 if (ts == NULL)
  725                         return;
  726         }
  727         if (tdq == to)
  728                 return;
  729         td = ts->ts_thread;
  730         /*
  731          * Although the run queue is locked the thread may be blocked.  Lock
  732          * it to clear this and acquire the run-queue lock.
  733          */
  734         thread_lock(td);
  735         /* Drop recursive lock on from acquired via thread_lock(). */
  736         TDQ_UNLOCK(from);
  737         sched_rem(td);
  738         ts->ts_cpu = cpu;
  739         td->td_lock = TDQ_LOCKPTR(to);
  740         tdq_add(to, td, SRQ_YIELDING);
  741 }
  742 
  743 /*
  744  * This tdq has idled.  Try to steal a thread from another cpu and switch
  745  * to it.
  746  */
  747 static int
  748 tdq_idled(struct tdq *tdq)
  749 {
  750         struct tdq_group *tdg;
  751         struct tdq *steal;
  752         int highload;
  753         int highcpu;
  754         int cpu;
  755 
  756         if (smp_started == 0 || steal_idle == 0)
  757                 return (1);
  758         /* We don't want to be preempted while we're iterating over tdqs */
  759         spinlock_enter();
  760         tdg = tdq->tdq_group;
  761         /*
  762          * If we're in a cpu group, try and steal threads from another cpu in
  763          * the group before idling.  In a HTT group all cpus share the same
  764          * run-queue lock, however, we still need a recursive lock to
  765          * call tdq_move().
  766          */
  767         if (steal_htt && tdg->tdg_cpus > 1 && tdg->tdg_transferable) {
  768                 TDQ_LOCK(tdq);
  769                 LIST_FOREACH(steal, &tdg->tdg_members, tdq_siblings) {
  770                         if (steal == tdq || steal->tdq_transferable == 0)
  771                                 continue;
  772                         TDQ_LOCK(steal);
  773                         goto steal;
  774                 }
  775                 TDQ_UNLOCK(tdq);
  776         }
  777         /*
  778          * Find the least loaded CPU with a transferable thread and attempt
  779          * to steal it.  We make a lockless pass and then verify that the
  780          * thread is still available after locking.
  781          */
  782         for (;;) {
  783                 highcpu = 0;
  784                 highload = 0;
  785                 for (cpu = 0; cpu <= mp_maxid; cpu++) {
  786                         if (CPU_ABSENT(cpu))
  787                                 continue;
  788                         steal = TDQ_CPU(cpu);
  789                         if (steal->tdq_transferable == 0)
  790                                 continue;
  791                         if (steal->tdq_load < highload)
  792                                 continue;
  793                         highload = steal->tdq_load;
  794                         highcpu = cpu;
  795                 }
  796                 if (highload < steal_thresh)
  797                         break;
  798                 steal = TDQ_CPU(highcpu);
  799                 if (steal == tdq)
  800                         break;
  801                 tdq_lock_pair(tdq, steal);
  802                 if (steal->tdq_load >= steal_thresh && steal->tdq_transferable)
  803                         goto steal;
  804                 tdq_unlock_pair(tdq, steal);
  805         }
  806         spinlock_exit();
  807         return (1);
  808 steal:
  809         spinlock_exit();
  810         tdq_move(steal, tdq);
  811         TDQ_UNLOCK(steal);
  812         mi_switch(SW_VOL, NULL);
  813         thread_unlock(curthread);
  814 
  815         return (0);
  816 }
  817 
  818 /*
  819  * Notify a remote cpu of new work.  Sends an IPI if criteria are met.
  820  */
  821 static void
  822 tdq_notify(struct td_sched *ts)
  823 {
  824         struct thread *ctd;
  825         struct pcpu *pcpu;
  826         int cpri;
  827         int pri;
  828         int cpu;
  829 
  830         cpu = ts->ts_cpu;
  831         pri = ts->ts_thread->td_priority;
  832         pcpu = pcpu_find(cpu);
  833         ctd = pcpu->pc_curthread;
  834         cpri = ctd->td_priority;
  835 
  836         /*
  837          * If our priority is not better than the current priority there is
  838          * nothing to do.
  839          */
  840         if (pri > cpri)
  841                 return;
  842         /*
  843          * Always IPI idle.
  844          */
  845         if (cpri > PRI_MIN_IDLE)
  846                 goto sendipi;
  847         /*
  848          * If we're realtime or better and there is timeshare or worse running
  849          * send an IPI.
  850          */
  851         if (pri < PRI_MAX_REALTIME && cpri > PRI_MAX_REALTIME)
  852                 goto sendipi;
  853         /*
  854          * Otherwise only IPI if we exceed the threshold.
  855          */
  856         if (pri > preempt_thresh)
  857                 return;
  858 sendipi:
  859         ipi_selected(1 << cpu, IPI_PREEMPT);
  860 }
  861 
  862 /*
  863  * Steals load from a timeshare queue.  Honors the rotating queue head
  864  * index.
  865  */
  866 static struct td_sched *
  867 runq_steal_from(struct runq *rq, int cpu, u_char start)
  868 {
  869         struct td_sched *ts;
  870         struct rqbits *rqb;
  871         struct rqhead *rqh;
  872         int first;
  873         int bit;
  874         int pri;
  875         int i;
  876 
  877         rqb = &rq->rq_status;
  878         bit = start & (RQB_BPW -1);
  879         pri = 0;
  880         first = 0;
  881 again:
  882         for (i = RQB_WORD(start); i < RQB_LEN; bit = 0, i++) {
  883                 if (rqb->rqb_bits[i] == 0)
  884                         continue;
  885                 if (bit != 0) {
  886                         for (pri = bit; pri < RQB_BPW; pri++)
  887                                 if (rqb->rqb_bits[i] & (1ul << pri))
  888                                         break;
  889                         if (pri >= RQB_BPW)
  890                                 continue;
  891                 } else
  892                         pri = RQB_FFS(rqb->rqb_bits[i]);
  893                 pri += (i << RQB_L2BPW);
  894                 rqh = &rq->rq_queues[pri];
  895                 TAILQ_FOREACH(ts, rqh, ts_procq) {
  896                         if (first && THREAD_CAN_MIGRATE(ts->ts_thread) &&
  897                             THREAD_CAN_SCHED(ts->ts_thread, cpu))
  898                                 return (ts);
  899                         first = 1;
  900                 }
  901         }
  902         if (start != 0) {
  903                 start = 0;
  904                 goto again;
  905         }
  906 
  907         return (NULL);
  908 }
  909 
  910 /*
  911  * Steals load from a standard linear queue.
  912  */
  913 static struct td_sched *
  914 runq_steal(struct runq *rq, int cpu)
  915 {
  916         struct rqhead *rqh;
  917         struct rqbits *rqb;
  918         struct td_sched *ts;
  919         int word;
  920         int bit;
  921 
  922         rqb = &rq->rq_status;
  923         for (word = 0; word < RQB_LEN; word++) {
  924                 if (rqb->rqb_bits[word] == 0)
  925                         continue;
  926                 for (bit = 0; bit < RQB_BPW; bit++) {
  927                         if ((rqb->rqb_bits[word] & (1ul << bit)) == 0)
  928                                 continue;
  929                         rqh = &rq->rq_queues[bit + (word << RQB_L2BPW)];
  930                         TAILQ_FOREACH(ts, rqh, ts_procq)
  931                                 if (THREAD_CAN_MIGRATE(ts->ts_thread) &&
  932                                     THREAD_CAN_SCHED(ts->ts_thread, cpu))
  933                                         return (ts);
  934                 }
  935         }
  936         return (NULL);
  937 }
  938 
  939 /*
  940  * Attempt to steal a thread in priority order from a thread queue.
  941  */
  942 static struct td_sched *
  943 tdq_steal(struct tdq *tdq, int cpu)
  944 {
  945         struct td_sched *ts;
  946 
  947         TDQ_LOCK_ASSERT(tdq, MA_OWNED);
  948         if ((ts = runq_steal(&tdq->tdq_realtime, cpu)) != NULL)
  949                 return (ts);
  950         if ((ts = runq_steal_from(&tdq->tdq_timeshare,
  951             cpu, tdq->tdq_ridx)) != NULL)
  952                 return (ts);
  953         return (runq_steal(&tdq->tdq_idle, cpu));
  954 }
  955 
  956 /*
  957  * Sets the thread lock and ts_cpu to match the requested cpu.  Unlocks the
  958  * current lock and returns with the assigned queue locked.
  959  */
  960 static inline struct tdq *
  961 sched_setcpu(struct td_sched *ts, int cpu, int flags)
  962 {
  963         struct thread *td;
  964         struct tdq *tdq;
  965 
  966         THREAD_LOCK_ASSERT(ts->ts_thread, MA_OWNED);
  967 
  968         tdq = TDQ_CPU(cpu);
  969         td = ts->ts_thread;
  970         ts->ts_cpu = cpu;
  971 
  972         /* If the lock matches just return the queue. */
  973         if (td->td_lock == TDQ_LOCKPTR(tdq))
  974                 return (tdq);
  975 #ifdef notyet
  976         /*
  977          * If the thread isn't running its lockptr is a
  978          * turnstile or a sleepqueue.  We can just lock_set without
  979          * blocking.
  980          */
  981         if (TD_CAN_RUN(td)) {
  982                 TDQ_LOCK(tdq);
  983                 thread_lock_set(td, TDQ_LOCKPTR(tdq));
  984                 return (tdq);
  985         }
  986 #endif
  987         /*
  988          * The hard case, migration, we need to block the thread first to
  989          * prevent order reversals with other cpus locks.
  990          */
  991         spinlock_enter();
  992         thread_lock_block(td);
  993         TDQ_LOCK(tdq);
  994         thread_lock_unblock(td, TDQ_LOCKPTR(tdq));
  995         spinlock_exit();
  996         return (tdq);
  997 }
  998 
  999 /*
 1000  * Find the thread queue running the lowest priority thread.
 1001  */
 1002 static int
 1003 tdq_lowestpri(struct thread *td)
 1004 {
 1005         struct tdq *tdq;
 1006         int lowpri;
 1007         int lowcpu;
 1008         int lowload;
 1009         int load;
 1010         int cpu;
 1011         int pri;
 1012 
 1013         lowload = 0;
 1014         lowpri = lowcpu = 0;
 1015         for (cpu = 0; cpu <= mp_maxid; cpu++) {
 1016                 if (CPU_ABSENT(cpu))
 1017                         continue;
 1018                 if (!THREAD_CAN_SCHED(td, cpu))
 1019                         continue;
 1020                 tdq = TDQ_CPU(cpu);
 1021                 pri = tdq->tdq_lowpri;
 1022                 load = TDQ_CPU(cpu)->tdq_load;
 1023                 CTR4(KTR_ULE,
 1024                     "cpu %d pri %d lowcpu %d lowpri %d",
 1025                     cpu, pri, lowcpu, lowpri);
 1026                 if (pri < lowpri)
 1027                         continue;
 1028                 if (lowpri && lowpri == pri && load > lowload)
 1029                         continue;
 1030                 lowpri = pri;
 1031                 lowcpu = cpu;
 1032                 lowload = load;
 1033         }
 1034 
 1035         return (lowcpu);
 1036 }
 1037 
 1038 /*
 1039  * Find the thread queue with the least load.
 1040  */
 1041 static int
 1042 tdq_lowestload(struct thread *td)
 1043 {
 1044         struct tdq *tdq;
 1045         int lowload;
 1046         int lowpri;
 1047         int lowcpu;
 1048         int load;
 1049         int cpu;
 1050         int pri;
 1051 
 1052         lowcpu = 0;
 1053         lowload = TDQ_CPU(0)->tdq_load;
 1054         lowpri = TDQ_CPU(0)->tdq_lowpri;
 1055         for (cpu = 1; cpu <= mp_maxid; cpu++) {
 1056                 if (CPU_ABSENT(cpu))
 1057                         continue;
 1058                 if (!THREAD_CAN_SCHED(td, cpu))
 1059                         continue;
 1060                 tdq = TDQ_CPU(cpu);
 1061                 load = tdq->tdq_load;
 1062                 pri = tdq->tdq_lowpri;
 1063                 CTR4(KTR_ULE, "cpu %d load %d lowcpu %d lowload %d",
 1064                     cpu, load, lowcpu, lowload);
 1065                 if (load > lowload)
 1066                         continue;
 1067                 if (load == lowload && pri < lowpri)
 1068                         continue;
 1069                 lowcpu = cpu;
 1070                 lowload = load;
 1071                 lowpri = pri;
 1072         }
 1073 
 1074         return (lowcpu);
 1075 }
 1076 
 1077 /*
 1078  * Pick the destination cpu for sched_add().  Respects affinity and makes
 1079  * a determination based on load or priority of available processors.
 1080  */
 1081 static int
 1082 sched_pickcpu(struct thread *td, int flags)
 1083 {
 1084         struct tdq *tdq;
 1085         struct td_sched *ts;
 1086         cpumask_t mask;
 1087         int self;
 1088         int pri;
 1089         int cpu;
 1090 
 1091         self = PCPU_GET(cpuid);
 1092         ts = td->td_sched;
 1093         if (smp_started == 0)
 1094                 return (self);
 1095         /*
 1096          * Don't migrate a running thread from sched_switch().
 1097          */
 1098         if (flags & SRQ_OURSELF) {
 1099                 CTR1(KTR_ULE, "YIELDING %d",
 1100                     curthread->td_priority);
 1101                 return (self);
 1102         }
 1103         pri = ts->ts_thread->td_priority;
 1104         cpu = ts->ts_cpu;
 1105         if (THREAD_CAN_SCHED(td, cpu)) {
 1106                 /*
 1107                  * Regardless of affinity, if the last cpu is idle
 1108                  * send it there.
 1109                  */
 1110                 tdq = TDQ_CPU(cpu);
 1111                 if (tdq->tdq_lowpri > PRI_MIN_IDLE) {
 1112                         CTR5(KTR_ULE,
 1113                     "ts_cpu %d idle, ltick %d ticks %d pri %d curthread %d",
 1114                             ts->ts_cpu, ts->ts_rltick, ticks, pri,
 1115                             tdq->tdq_lowpri);
 1116                         return (ts->ts_cpu);
 1117                 }
 1118                 /*
 1119                  * If we have affinity, try to place it on the cpu we
 1120                  * last ran on.
 1121                  */
 1122                 if (SCHED_AFFINITY(ts) && tdq->tdq_lowpri > pri) {
 1123                         CTR5(KTR_ULE,
 1124                     "affinity for %d, ltick %d ticks %d pri %d curthread %d",
 1125                             ts->ts_cpu, ts->ts_rltick, ticks, pri,
 1126                             tdq->tdq_lowpri);
 1127                         return (ts->ts_cpu);
 1128                 }
 1129         }
 1130 
 1131         /*
 1132          * Look for an idle group.
 1133          */
 1134         CTR1(KTR_ULE, "tdq_idle %X", tdq_idle);
 1135         mask = tdq_idle;
 1136         while ((cpu = ffs(mask)) != 0) {
 1137                 --cpu;
 1138                 if (THREAD_CAN_SCHED(td, cpu))
 1139                         return (cpu);
 1140                 mask &= ~(1 << cpu);
 1141         }
 1142         /*
 1143          * If there are no idle cores see if we can run the thread locally.
 1144          * This may improve locality among sleepers and wakers when there
 1145          * is shared data.
 1146          */
 1147         if (tryself && THREAD_CAN_SCHED(td, self) &&
 1148             pri < curthread->td_priority) {
 1149                 CTR1(KTR_ULE, "tryself %d",
 1150                     curthread->td_priority);
 1151                 return (self);
 1152         }
 1153         /*
 1154          * Now search for the cpu running the lowest priority thread with
 1155          * the least load.
 1156          */
 1157         if (pick_pri)
 1158                 cpu = tdq_lowestpri(td);
 1159         else
 1160                 cpu = tdq_lowestload(td);
 1161         return (cpu);
 1162 }
 1163 
 1164 #endif  /* SMP */
 1165 
 1166 /*
 1167  * Pick the highest priority task we have and return it.
 1168  */
 1169 static struct td_sched *
 1170 tdq_choose(struct tdq *tdq)
 1171 {
 1172         struct td_sched *ts;
 1173 
 1174         TDQ_LOCK_ASSERT(tdq, MA_OWNED);
 1175         ts = runq_choose(&tdq->tdq_realtime);
 1176         if (ts != NULL)
 1177                 return (ts);
 1178         ts = runq_choose_from(&tdq->tdq_timeshare, tdq->tdq_ridx);
 1179         if (ts != NULL) {
 1180                 KASSERT(ts->ts_thread->td_priority >= PRI_MIN_TIMESHARE,
 1181                     ("tdq_choose: Invalid priority on timeshare queue %d",
 1182                     ts->ts_thread->td_priority));
 1183                 return (ts);
 1184         }
 1185 
 1186         ts = runq_choose(&tdq->tdq_idle);
 1187         if (ts != NULL) {
 1188                 KASSERT(ts->ts_thread->td_priority >= PRI_MIN_IDLE,
 1189                     ("tdq_choose: Invalid priority on idle queue %d",
 1190                     ts->ts_thread->td_priority));
 1191                 return (ts);
 1192         }
 1193 
 1194         return (NULL);
 1195 }
 1196 
 1197 /*
 1198  * Initialize a thread queue.
 1199  */
 1200 static void
 1201 tdq_setup(struct tdq *tdq)
 1202 {
 1203 
 1204         if (bootverbose)
 1205                 printf("ULE: setup cpu %d\n", TDQ_ID(tdq));
 1206         runq_init(&tdq->tdq_realtime);
 1207         runq_init(&tdq->tdq_timeshare);
 1208         runq_init(&tdq->tdq_idle);
 1209         tdq->tdq_load = 0;
 1210 }
 1211 
 1212 #ifdef SMP
 1213 static void
 1214 tdg_setup(struct tdq_group *tdg)
 1215 {
 1216         if (bootverbose)
 1217                 printf("ULE: setup cpu group %d\n", TDG_ID(tdg));
 1218         snprintf(tdg->tdg_name, sizeof(tdg->tdg_name),
 1219             "sched lock %d", (int)TDG_ID(tdg));
 1220         mtx_init(&tdg->tdg_lock, tdg->tdg_name, "sched lock",
 1221             MTX_SPIN | MTX_RECURSE);
 1222         LIST_INIT(&tdg->tdg_members);
 1223         tdg->tdg_load = 0;
 1224         tdg->tdg_transferable = 0;
 1225         tdg->tdg_cpus = 0;
 1226         tdg->tdg_mask = 0;
 1227         tdg->tdg_cpumask = 0;
 1228         tdg->tdg_idlemask = 0;
 1229 }
 1230 
 1231 static void
 1232 tdg_add(struct tdq_group *tdg, struct tdq *tdq)
 1233 {
 1234         if (tdg->tdg_mask == 0)
 1235                 tdg->tdg_mask |= 1 << TDQ_ID(tdq);
 1236         tdg->tdg_cpumask |= 1 << TDQ_ID(tdq);
 1237         tdg->tdg_cpus++;
 1238         tdq->tdq_group = tdg;
 1239         tdq->tdq_lock = &tdg->tdg_lock;
 1240         LIST_INSERT_HEAD(&tdg->tdg_members, tdq, tdq_siblings);
 1241         if (bootverbose)
 1242                 printf("ULE: adding cpu %d to group %d: cpus %d mask 0x%X\n",
 1243                     TDQ_ID(tdq), TDG_ID(tdg), tdg->tdg_cpus, tdg->tdg_cpumask);
 1244 }
 1245 
 1246 static void
 1247 sched_setup_topology(void)
 1248 {
 1249         struct tdq_group *tdg;
 1250         struct cpu_group *cg;
 1251         int balance_groups;
 1252         struct tdq *tdq;
 1253         int i;
 1254         int j;
 1255 
 1256         topology = 1;
 1257         balance_groups = 0;
 1258         for (i = 0; i < smp_topology->ct_count; i++) {
 1259                 cg = &smp_topology->ct_group[i];
 1260                 tdg = &tdq_groups[i];
 1261                 /*
 1262                  * Initialize the group.
 1263                  */
 1264                 tdg_setup(tdg);
 1265                 /*
 1266                  * Find all of the group members and add them.
 1267                  */
 1268                 for (j = 0; j < MAXCPU; j++) { 
 1269                         if ((cg->cg_mask & (1 << j)) != 0) {
 1270                                 tdq = TDQ_CPU(j);
 1271                                 tdq_setup(tdq);
 1272                                 tdg_add(tdg, tdq);
 1273                         }
 1274                 }
 1275                 if (tdg->tdg_cpus > 1)
 1276                         balance_groups = 1;
 1277         }
 1278         tdg_maxid = smp_topology->ct_count - 1;
 1279         if (balance_groups)
 1280                 sched_balance_groups();
 1281 }
 1282 
 1283 static void
 1284 sched_setup_smp(void)
 1285 {
 1286         struct tdq_group *tdg;
 1287         struct tdq *tdq;
 1288         int cpus;
 1289         int i;
 1290 
 1291         for (cpus = 0, i = 0; i < MAXCPU; i++) {
 1292                 if (CPU_ABSENT(i))
 1293                         continue;
 1294                 tdq = &tdq_cpu[i];
 1295                 tdg = &tdq_groups[i];
 1296                 /*
 1297                  * Setup a tdq group with one member.
 1298                  */
 1299                 tdg_setup(tdg);
 1300                 tdq_setup(tdq);
 1301                 tdg_add(tdg, tdq);
 1302                 cpus++;
 1303         }
 1304         tdg_maxid = cpus - 1;
 1305 }
 1306 
 1307 /*
 1308  * Fake a topology with one group containing all CPUs.
 1309  */
 1310 static void
 1311 sched_fake_topo(void)
 1312 {
 1313 #ifdef SCHED_FAKE_TOPOLOGY
 1314         static struct cpu_top top;
 1315         static struct cpu_group group;
 1316 
 1317         top.ct_count = 1;
 1318         top.ct_group = &group;
 1319         group.cg_mask = all_cpus;
 1320         group.cg_count = mp_ncpus;
 1321         group.cg_children = 0;
 1322         smp_topology = &top;
 1323 #endif
 1324 }
 1325 #endif
 1326 
 1327 /*
 1328  * Setup the thread queues and initialize the topology based on MD
 1329  * information.
 1330  */
 1331 static void
 1332 sched_setup(void *dummy)
 1333 {
 1334         struct tdq *tdq;
 1335 
 1336         tdq = TDQ_SELF();
 1337 #ifdef SMP
 1338         sched_fake_topo();
 1339         /*
 1340          * Setup tdqs based on a topology configuration or vanilla SMP based
 1341          * on mp_maxid.
 1342          */
 1343         if (smp_topology == NULL)
 1344                 sched_setup_smp();
 1345         else 
 1346                 sched_setup_topology();
 1347         balance_tdq = tdq;
 1348         sched_balance();
 1349 #else
 1350         tdq_setup(tdq);
 1351         mtx_init(&tdq_lock, "sched lock", "sched lock", MTX_SPIN | MTX_RECURSE);
 1352         tdq->tdq_lock = &tdq_lock;
 1353 #endif
 1354         /*
 1355          * To avoid divide-by-zero, we set realstathz a dummy value
 1356          * in case which sched_clock() called before sched_initticks().
 1357          */
 1358         realstathz = hz;
 1359         sched_slice = (realstathz/10);  /* ~100ms */
 1360         tickincr = 1 << SCHED_TICK_SHIFT;
 1361 
 1362         /* Add thread0's load since it's running. */
 1363         TDQ_LOCK(tdq);
 1364         thread0.td_lock = TDQ_LOCKPTR(TDQ_SELF());
 1365         tdq_load_add(tdq, &td_sched0);
 1366         TDQ_UNLOCK(tdq);
 1367 }
 1368 
 1369 /*
 1370  * This routine determines the tickincr after stathz and hz are setup.
 1371  */
 1372 /* ARGSUSED */
 1373 static void
 1374 sched_initticks(void *dummy)
 1375 {
 1376         int incr;
 1377 
 1378         realstathz = stathz ? stathz : hz;
 1379         sched_slice = (realstathz/10);  /* ~100ms */
 1380 
 1381         /*
 1382          * tickincr is shifted out by 10 to avoid rounding errors due to
 1383          * hz not being evenly divisible by stathz on all platforms.
 1384          */
 1385         incr = (hz << SCHED_TICK_SHIFT) / realstathz;
 1386         /*
 1387          * This does not work for values of stathz that are more than
 1388          * 1 << SCHED_TICK_SHIFT * hz.  In practice this does not happen.
 1389          */
 1390         if (incr == 0)
 1391                 incr = 1;
 1392         tickincr = incr;
 1393 #ifdef SMP
 1394         /*
 1395          * Set the default balance interval now that we know
 1396          * what realstathz is.
 1397          */
 1398         balance_interval = realstathz;
 1399         /*
 1400          * Set steal thresh to roughly log2(mp_ncpu) but no greater than 4. 
 1401          * This prevents excess thrashing on large machines and excess idle 
 1402          * on smaller machines.
 1403          */
 1404         steal_thresh = min(fls(mp_ncpus) - 1, 3);
 1405         affinity = SCHED_AFFINITY_DEFAULT;
 1406 #endif
 1407 }
 1408 
 1409 
 1410 /*
 1411  * This is the core of the interactivity algorithm.  Determines a score based
 1412  * on past behavior.  It is the ratio of sleep time to run time scaled to
 1413  * a [0, 100] integer.  This is the voluntary sleep time of a process, which
 1414  * differs from the cpu usage because it does not account for time spent
 1415  * waiting on a run-queue.  Would be prettier if we had floating point.
 1416  */
 1417 static int
 1418 sched_interact_score(struct thread *td)
 1419 {
 1420         struct td_sched *ts;
 1421         int div;
 1422 
 1423         ts = td->td_sched;
 1424         /*
 1425          * The score is only needed if this is likely to be an interactive
 1426          * task.  Don't go through the expense of computing it if there's
 1427          * no chance.
 1428          */
 1429         if (sched_interact <= SCHED_INTERACT_HALF &&
 1430                 ts->ts_runtime >= ts->ts_slptime)
 1431                         return (SCHED_INTERACT_HALF);
 1432 
 1433         if (ts->ts_runtime > ts->ts_slptime) {
 1434                 div = max(1, ts->ts_runtime / SCHED_INTERACT_HALF);
 1435                 return (SCHED_INTERACT_HALF +
 1436                     (SCHED_INTERACT_HALF - (ts->ts_slptime / div)));
 1437         }
 1438         if (ts->ts_slptime > ts->ts_runtime) {
 1439                 div = max(1, ts->ts_slptime / SCHED_INTERACT_HALF);
 1440                 return (ts->ts_runtime / div);
 1441         }
 1442         /* runtime == slptime */
 1443         if (ts->ts_runtime)
 1444                 return (SCHED_INTERACT_HALF);
 1445 
 1446         /*
 1447          * This can happen if slptime and runtime are 0.
 1448          */
 1449         return (0);
 1450 
 1451 }
 1452 
 1453 /*
 1454  * Scale the scheduling priority according to the "interactivity" of this
 1455  * process.
 1456  */
 1457 static void
 1458 sched_priority(struct thread *td)
 1459 {
 1460         int score;
 1461         int pri;
 1462 
 1463         if (td->td_pri_class != PRI_TIMESHARE)
 1464                 return;
 1465         /*
 1466          * If the score is interactive we place the thread in the realtime
 1467          * queue with a priority that is less than kernel and interrupt
 1468          * priorities.  These threads are not subject to nice restrictions.
 1469          *
 1470          * Scores greater than this are placed on the normal timeshare queue
 1471          * where the priority is partially decided by the most recent cpu
 1472          * utilization and the rest is decided by nice value.
 1473          *
 1474          * The nice value of the process has a linear effect on the calculated
 1475          * score.  Negative nice values make it easier for a thread to be
 1476          * considered interactive.
 1477          */
 1478         score = imax(0, sched_interact_score(td) + td->td_proc->p_nice);
 1479         if (score < sched_interact) {
 1480                 pri = PRI_MIN_REALTIME;
 1481                 pri += ((PRI_MAX_REALTIME - PRI_MIN_REALTIME) / sched_interact)
 1482                     * score;
 1483                 KASSERT(pri >= PRI_MIN_REALTIME && pri <= PRI_MAX_REALTIME,
 1484                     ("sched_priority: invalid interactive priority %d score %d",
 1485                     pri, score));
 1486         } else {
 1487                 pri = SCHED_PRI_MIN;
 1488                 if (td->td_sched->ts_ticks)
 1489                         pri += SCHED_PRI_TICKS(td->td_sched);
 1490                 pri += SCHED_PRI_NICE(td->td_proc->p_nice);
 1491                 KASSERT(pri >= PRI_MIN_TIMESHARE && pri <= PRI_MAX_TIMESHARE,
 1492                     ("sched_priority: invalid priority %d: nice %d, " 
 1493                     "ticks %d ftick %d ltick %d tick pri %d",
 1494                     pri, td->td_proc->p_nice, td->td_sched->ts_ticks,
 1495                     td->td_sched->ts_ftick, td->td_sched->ts_ltick,
 1496                     SCHED_PRI_TICKS(td->td_sched)));
 1497         }
 1498         sched_user_prio(td, pri);
 1499 
 1500         return;
 1501 }
 1502 
 1503 /*
 1504  * This routine enforces a maximum limit on the amount of scheduling history
 1505  * kept.  It is called after either the slptime or runtime is adjusted.  This
 1506  * function is ugly due to integer math.
 1507  */
 1508 static void
 1509 sched_interact_update(struct thread *td)
 1510 {
 1511         struct td_sched *ts;
 1512         u_int sum;
 1513 
 1514         ts = td->td_sched;
 1515         sum = ts->ts_runtime + ts->ts_slptime;
 1516         if (sum < SCHED_SLP_RUN_MAX)
 1517                 return;
 1518         /*
 1519          * This only happens from two places:
 1520          * 1) We have added an unusual amount of run time from fork_exit.
 1521          * 2) We have added an unusual amount of sleep time from sched_sleep().
 1522          */
 1523         if (sum > SCHED_SLP_RUN_MAX * 2) {
 1524                 if (ts->ts_runtime > ts->ts_slptime) {
 1525                         ts->ts_runtime = SCHED_SLP_RUN_MAX;
 1526                         ts->ts_slptime = 1;
 1527                 } else {
 1528                         ts->ts_slptime = SCHED_SLP_RUN_MAX;
 1529                         ts->ts_runtime = 1;
 1530                 }
 1531                 return;
 1532         }
 1533         /*
 1534          * If we have exceeded by more than 1/5th then the algorithm below
 1535          * will not bring us back into range.  Dividing by two here forces
 1536          * us into the range of [4/5 * SCHED_INTERACT_MAX, SCHED_INTERACT_MAX]
 1537          */
 1538         if (sum > (SCHED_SLP_RUN_MAX / 5) * 6) {
 1539                 ts->ts_runtime /= 2;
 1540                 ts->ts_slptime /= 2;
 1541                 return;
 1542         }
 1543         ts->ts_runtime = (ts->ts_runtime / 5) * 4;
 1544         ts->ts_slptime = (ts->ts_slptime / 5) * 4;
 1545 }
 1546 
 1547 /*
 1548  * Scale back the interactivity history when a child thread is created.  The
 1549  * history is inherited from the parent but the thread may behave totally
 1550  * differently.  For example, a shell spawning a compiler process.  We want
 1551  * to learn that the compiler is behaving badly very quickly.
 1552  */
 1553 static void
 1554 sched_interact_fork(struct thread *td)
 1555 {
 1556         int ratio;
 1557         int sum;
 1558 
 1559         sum = td->td_sched->ts_runtime + td->td_sched->ts_slptime;
 1560         if (sum > SCHED_SLP_RUN_FORK) {
 1561                 ratio = sum / SCHED_SLP_RUN_FORK;
 1562                 td->td_sched->ts_runtime /= ratio;
 1563                 td->td_sched->ts_slptime /= ratio;
 1564         }
 1565 }
 1566 
 1567 /*
 1568  * Called from proc0_init() to setup the scheduler fields.
 1569  */
 1570 void
 1571 schedinit(void)
 1572 {
 1573 
 1574         /*
 1575          * Set up the scheduler specific parts of proc0.
 1576          */
 1577         proc0.p_sched = NULL; /* XXX */
 1578         thread0.td_sched = &td_sched0;
 1579         td_sched0.ts_ltick = ticks;
 1580         td_sched0.ts_ftick = ticks;
 1581         td_sched0.ts_thread = &thread0;
 1582 }
 1583 
 1584 /*
 1585  * This is only somewhat accurate since given many processes of the same
 1586  * priority they will switch when their slices run out, which will be
 1587  * at most sched_slice stathz ticks.
 1588  */
 1589 int
 1590 sched_rr_interval(void)
 1591 {
 1592 
 1593         /* Convert sched_slice to hz */
 1594         return (hz/(realstathz/sched_slice));
 1595 }
 1596 
 1597 /*
 1598  * Update the percent cpu tracking information when it is requested or
 1599  * the total history exceeds the maximum.  We keep a sliding history of
 1600  * tick counts that slowly decays.  This is less precise than the 4BSD
 1601  * mechanism since it happens with less regular and frequent events.
 1602  */
 1603 static void
 1604 sched_pctcpu_update(struct td_sched *ts)
 1605 {
 1606 
 1607         if (ts->ts_ticks == 0)
 1608                 return;
 1609         if (ticks - (hz / 10) < ts->ts_ltick &&
 1610             SCHED_TICK_TOTAL(ts) < SCHED_TICK_MAX)
 1611                 return;
 1612         /*
 1613          * Adjust counters and watermark for pctcpu calc.
 1614          */
 1615         if (ts->ts_ltick > ticks - SCHED_TICK_TARG)
 1616                 ts->ts_ticks = (ts->ts_ticks / (ticks - ts->ts_ftick)) *
 1617                             SCHED_TICK_TARG;
 1618         else
 1619                 ts->ts_ticks = 0;
 1620         ts->ts_ltick = ticks;
 1621         ts->ts_ftick = ts->ts_ltick - SCHED_TICK_TARG;
 1622 }
 1623 
 1624 /*
 1625  * Adjust the priority of a thread.  Move it to the appropriate run-queue
 1626  * if necessary.  This is the back-end for several priority related
 1627  * functions.
 1628  */
 1629 static void
 1630 sched_thread_priority(struct thread *td, u_char prio)
 1631 {
 1632         struct td_sched *ts;
 1633 
 1634         CTR6(KTR_SCHED, "sched_prio: %p(%s) prio %d newprio %d by %p(%s)",
 1635             td, td->td_proc->p_comm, td->td_priority, prio, curthread,
 1636             curthread->td_proc->p_comm);
 1637         ts = td->td_sched;
 1638         THREAD_LOCK_ASSERT(td, MA_OWNED);
 1639         if (td->td_priority == prio)
 1640                 return;
 1641 
 1642         if (TD_ON_RUNQ(td) && prio < td->td_priority) {
 1643                 /*
 1644                  * If the priority has been elevated due to priority
 1645                  * propagation, we may have to move ourselves to a new
 1646                  * queue.  This could be optimized to not re-add in some
 1647                  * cases.
 1648                  */
 1649                 sched_rem(td);
 1650                 td->td_priority = prio;
 1651                 sched_add(td, SRQ_BORROWING);
 1652         } else {
 1653 #ifdef SMP
 1654                 struct tdq *tdq;
 1655 
 1656                 tdq = TDQ_CPU(ts->ts_cpu);
 1657                 if (prio < tdq->tdq_lowpri)
 1658                         tdq->tdq_lowpri = prio;
 1659 #endif
 1660                 td->td_priority = prio;
 1661         }
 1662 }
 1663 
 1664 /*
 1665  * Update a thread's priority when it is lent another thread's
 1666  * priority.
 1667  */
 1668 void
 1669 sched_lend_prio(struct thread *td, u_char prio)
 1670 {
 1671 
 1672         td->td_flags |= TDF_BORROWING;
 1673         sched_thread_priority(td, prio);
 1674 }
 1675 
 1676 /*
 1677  * Restore a thread's priority when priority propagation is
 1678  * over.  The prio argument is the minimum priority the thread
 1679  * needs to have to satisfy other possible priority lending
 1680  * requests.  If the thread's regular priority is less
 1681  * important than prio, the thread will keep a priority boost
 1682  * of prio.
 1683  */
 1684 void
 1685 sched_unlend_prio(struct thread *td, u_char prio)
 1686 {
 1687         u_char base_pri;
 1688 
 1689         if (td->td_base_pri >= PRI_MIN_TIMESHARE &&
 1690             td->td_base_pri <= PRI_MAX_TIMESHARE)
 1691                 base_pri = td->td_user_pri;
 1692         else
 1693                 base_pri = td->td_base_pri;
 1694         if (prio >= base_pri) {
 1695                 td->td_flags &= ~TDF_BORROWING;
 1696                 sched_thread_priority(td, base_pri);
 1697         } else
 1698                 sched_lend_prio(td, prio);
 1699 }
 1700 
 1701 /*
 1702  * Standard entry for setting the priority to an absolute value.
 1703  */
 1704 void
 1705 sched_prio(struct thread *td, u_char prio)
 1706 {
 1707         u_char oldprio;
 1708 
 1709         /* First, update the base priority. */
 1710         td->td_base_pri = prio;
 1711 
 1712         /*
 1713          * If the thread is borrowing another thread's priority, don't
 1714          * ever lower the priority.
 1715          */
 1716         if (td->td_flags & TDF_BORROWING && td->td_priority < prio)
 1717                 return;
 1718 
 1719         /* Change the real priority. */
 1720         oldprio = td->td_priority;
 1721         sched_thread_priority(td, prio);
 1722 
 1723         /*
 1724          * If the thread is on a turnstile, then let the turnstile update
 1725          * its state.
 1726          */
 1727         if (TD_ON_LOCK(td) && oldprio != prio)
 1728                 turnstile_adjust(td, oldprio);
 1729 }
 1730 
 1731 /*
 1732  * Set the base user priority, does not effect current running priority.
 1733  */
 1734 void
 1735 sched_user_prio(struct thread *td, u_char prio)
 1736 {
 1737         u_char oldprio;
 1738 
 1739         td->td_base_user_pri = prio;
 1740         if (td->td_flags & TDF_UBORROWING && td->td_user_pri <= prio)
 1741                 return;
 1742         oldprio = td->td_user_pri;
 1743         td->td_user_pri = prio;
 1744 }
 1745 
 1746 void
 1747 sched_lend_user_prio(struct thread *td, u_char prio)
 1748 {
 1749         u_char oldprio;
 1750 
 1751         THREAD_LOCK_ASSERT(td, MA_OWNED);
 1752         td->td_flags |= TDF_UBORROWING;
 1753         oldprio = td->td_user_pri;
 1754         td->td_user_pri = prio;
 1755 }
 1756 
 1757 void
 1758 sched_unlend_user_prio(struct thread *td, u_char prio)
 1759 {
 1760         u_char base_pri;
 1761 
 1762         THREAD_LOCK_ASSERT(td, MA_OWNED);
 1763         base_pri = td->td_base_user_pri;
 1764         if (prio >= base_pri) {
 1765                 td->td_flags &= ~TDF_UBORROWING;
 1766                 sched_user_prio(td, base_pri);
 1767         } else {
 1768                 sched_lend_user_prio(td, prio);
 1769         }
 1770 }
 1771 
 1772 /*
 1773  * Add the thread passed as 'newtd' to the run queue before selecting
 1774  * the next thread to run.  This is only used for KSE.
 1775  */
 1776 static void
 1777 sched_switchin(struct tdq *tdq, struct thread *td)
 1778 {
 1779 #ifdef SMP
 1780         spinlock_enter();
 1781         TDQ_UNLOCK(tdq);
 1782         thread_lock(td);
 1783         spinlock_exit();
 1784         sched_setcpu(td->td_sched, TDQ_ID(tdq), SRQ_YIELDING);
 1785 #else
 1786         td->td_lock = TDQ_LOCKPTR(tdq);
 1787 #endif
 1788         tdq_add(tdq, td, SRQ_YIELDING);
 1789         MPASS(td->td_lock == TDQ_LOCKPTR(tdq));
 1790 }
 1791 
 1792 /*
 1793  * Handle migration from sched_switch().  This happens only for
 1794  * cpu binding.
 1795  */
 1796 static struct mtx *
 1797 sched_switch_migrate(struct tdq *tdq, struct thread *td, int flags)
 1798 {
 1799         struct tdq *tdn;
 1800 
 1801         tdn = TDQ_CPU(td->td_sched->ts_cpu);
 1802 #ifdef SMP
 1803         /*
 1804          * Do the lock dance required to avoid LOR.  We grab an extra
 1805          * spinlock nesting to prevent preemption while we're
 1806          * not holding either run-queue lock.
 1807          */
 1808         spinlock_enter();
 1809         thread_lock_block(td);  /* This releases the lock on tdq. */
 1810 
 1811         /*
 1812          * Acquire both run-queue locks before placing the thread on the new
 1813          * run-queue to avoid deadlocks created by placing a thread with a
 1814          * blocked lock on the run-queue of a remote processor.  The deadlock
 1815          * occurs when a third processor attempts to lock the two queues in
 1816          * question while the target processor is spinning with its own
 1817          * run-queue lock held while waiting for the blocked lock to clear.
 1818          */
 1819         if (TDQ_LOCKPTR(tdn) == TDQ_LOCKPTR(tdq)) {
 1820                 TDQ_LOCK(tdq);
 1821                 tdq_add(tdn, td, flags);
 1822                 tdq_notify(td->td_sched);
 1823         } else {
 1824                 tdq_lock_pair(tdn, tdq);
 1825                 tdq_add(tdn, td, flags);
 1826                 tdq_notify(td->td_sched);
 1827                 TDQ_UNLOCK(tdn);
 1828         }
 1829         spinlock_exit();
 1830 #endif
 1831         return (TDQ_LOCKPTR(tdn));
 1832 }
 1833 
 1834 /*
 1835  * Variadic version of thread_lock_unblock() that does not assume td_lock
 1836  * is blocked.
 1837  */
 1838 static inline void
 1839 thread_unblock_switch(struct thread *td, struct mtx *mtx)
 1840 {
 1841         atomic_store_rel_ptr((volatile uintptr_t *)&td->td_lock,
 1842             (uintptr_t)mtx);
 1843 }
 1844 
 1845 /*
 1846  * Switch threads.  This function has to handle threads coming in while
 1847  * blocked for some reason, running, or idle.  It also must deal with
 1848  * migrating a thread from one queue to another as running threads may
 1849  * be assigned elsewhere via binding.
 1850  */
 1851 void
 1852 sched_switch(struct thread *td, struct thread *newtd, int flags)
 1853 {
 1854         struct tdq *tdq;
 1855         struct td_sched *ts;
 1856         struct mtx *mtx;
 1857         int srqflag;
 1858         int cpuid;
 1859 
 1860         THREAD_LOCK_ASSERT(td, MA_OWNED);
 1861 
 1862         cpuid = PCPU_GET(cpuid);
 1863         tdq = TDQ_CPU(cpuid);
 1864         ts = td->td_sched;
 1865         mtx = td->td_lock;
 1866 #ifdef SMP
 1867         ts->ts_rltick = ticks;
 1868         if (newtd && newtd->td_priority < tdq->tdq_lowpri)
 1869                 tdq->tdq_lowpri = newtd->td_priority;
 1870 #endif
 1871         td->td_lastcpu = td->td_oncpu;
 1872         td->td_oncpu = NOCPU;
 1873         td->td_flags &= ~TDF_NEEDRESCHED;
 1874         td->td_owepreempt = 0;
 1875         /*
 1876          * The lock pointer in an idle thread should never change.  Reset it
 1877          * to CAN_RUN as well.
 1878          */
 1879         if (TD_IS_IDLETHREAD(td)) {
 1880                 MPASS(td->td_lock == TDQ_LOCKPTR(tdq));
 1881                 TD_SET_CAN_RUN(td);
 1882         } else if (TD_IS_RUNNING(td)) {
 1883                 MPASS(td->td_lock == TDQ_LOCKPTR(tdq));
 1884                 tdq_load_rem(tdq, ts);
 1885                 srqflag = (flags & SW_PREEMPT) ?
 1886                     SRQ_OURSELF|SRQ_YIELDING|SRQ_PREEMPTED :
 1887                     SRQ_OURSELF|SRQ_YIELDING;
 1888                 if (ts->ts_cpu == cpuid)
 1889                         tdq_add(tdq, td, srqflag);
 1890                 else
 1891                         mtx = sched_switch_migrate(tdq, td, srqflag);
 1892         } else {
 1893                 /* This thread must be going to sleep. */
 1894                 TDQ_LOCK(tdq);
 1895                 mtx = thread_lock_block(td);
 1896                 tdq_load_rem(tdq, ts);
 1897         }
 1898         /*
 1899          * We enter here with the thread blocked and assigned to the
 1900          * appropriate cpu run-queue or sleep-queue and with the current
 1901          * thread-queue locked.
 1902          */
 1903         TDQ_LOCK_ASSERT(tdq, MA_OWNED | MA_NOTRECURSED);
 1904         /*
 1905          * If KSE assigned a new thread just add it here and let choosethread
 1906          * select the best one.
 1907          */
 1908         if (newtd != NULL)
 1909                 sched_switchin(tdq, newtd);
 1910         newtd = choosethread();
 1911         /*
 1912          * Call the MD code to switch contexts if necessary.
 1913          */
 1914         if (td != newtd) {
 1915 #ifdef  HWPMC_HOOKS
 1916                 if (PMC_PROC_IS_USING_PMCS(td->td_proc))
 1917                         PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_OUT);
 1918 #endif
 1919                 TDQ_LOCKPTR(tdq)->mtx_lock = (uintptr_t)newtd;
 1920 
 1921 #ifdef KDTRACE_HOOKS
 1922                 /*
 1923                  * If DTrace has set the active vtime enum to anything
 1924                  * other than INACTIVE (0), then it should have set the
 1925                  * function to call.
 1926                  */
 1927                 if (dtrace_vtime_active)
 1928                         (*dtrace_vtime_switch_func)(newtd);
 1929 #endif
 1930                 cpu_switch(td, newtd, mtx);
 1931                 /*
 1932                  * We may return from cpu_switch on a different cpu.  However,
 1933                  * we always return with td_lock pointing to the current cpu's
 1934                  * run queue lock.
 1935                  */
 1936                 cpuid = PCPU_GET(cpuid);
 1937                 tdq = TDQ_CPU(cpuid);
 1938 #ifdef  HWPMC_HOOKS
 1939                 if (PMC_PROC_IS_USING_PMCS(td->td_proc))
 1940                         PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_IN);
 1941 #endif
 1942         } else
 1943                 thread_unblock_switch(td, mtx);
 1944         /*
 1945          * Assert that all went well and return.
 1946          */
 1947 #ifdef SMP
 1948         /* We should always get here with the lowest priority td possible */
 1949         tdq->tdq_lowpri = td->td_priority;
 1950 #endif
 1951         TDQ_LOCK_ASSERT(tdq, MA_OWNED|MA_NOTRECURSED);
 1952         MPASS(td->td_lock == TDQ_LOCKPTR(tdq));
 1953         td->td_oncpu = cpuid;
 1954 }
 1955 
 1956 /*
 1957  * Adjust thread priorities as a result of a nice request.
 1958  */
 1959 void
 1960 sched_nice(struct proc *p, int nice)
 1961 {
 1962         struct thread *td;
 1963 
 1964         PROC_LOCK_ASSERT(p, MA_OWNED);
 1965         PROC_SLOCK_ASSERT(p, MA_OWNED);
 1966 
 1967         p->p_nice = nice;
 1968         FOREACH_THREAD_IN_PROC(p, td) {
 1969                 thread_lock(td);
 1970                 sched_priority(td);
 1971                 sched_prio(td, td->td_base_user_pri);
 1972                 thread_unlock(td);
 1973         }
 1974 }
 1975 
 1976 /*
 1977  * Record the sleep time for the interactivity scorer.
 1978  */
 1979 void
 1980 sched_sleep(struct thread *td)
 1981 {
 1982 
 1983         THREAD_LOCK_ASSERT(td, MA_OWNED);
 1984 
 1985         td->td_slptick = ticks;
 1986 }
 1987 
 1988 /*
 1989  * Schedule a thread to resume execution and record how long it voluntarily
 1990  * slept.  We also update the pctcpu, interactivity, and priority.
 1991  */
 1992 void
 1993 sched_wakeup(struct thread *td)
 1994 {
 1995         struct td_sched *ts;
 1996         int slptick;
 1997 
 1998         THREAD_LOCK_ASSERT(td, MA_OWNED);
 1999         ts = td->td_sched;
 2000         /*
 2001          * If we slept for more than a tick update our interactivity and
 2002          * priority.
 2003          */
 2004         slptick = td->td_slptick;
 2005         td->td_slptick = 0;
 2006         if (slptick && slptick != ticks) {
 2007                 u_int hzticks;
 2008 
 2009                 hzticks = (ticks - slptick) << SCHED_TICK_SHIFT;
 2010                 ts->ts_slptime += hzticks;
 2011                 sched_interact_update(td);
 2012                 sched_pctcpu_update(ts);
 2013                 sched_priority(td);
 2014         }
 2015         /* Reset the slice value after we sleep. */
 2016         ts->ts_slice = sched_slice;
 2017         sched_add(td, SRQ_BORING);
 2018 }
 2019 
 2020 /*
 2021  * Penalize the parent for creating a new child and initialize the child's
 2022  * priority.
 2023  */
 2024 void
 2025 sched_fork(struct thread *td, struct thread *child)
 2026 {
 2027         THREAD_LOCK_ASSERT(td, MA_OWNED);
 2028         sched_fork_thread(td, child);
 2029         /*
 2030          * Penalize the parent and child for forking.
 2031          */
 2032         sched_interact_fork(child);
 2033         sched_priority(child);
 2034         td->td_sched->ts_runtime += tickincr;
 2035         sched_interact_update(td);
 2036         sched_priority(td);
 2037 }
 2038 
 2039 /*
 2040  * Fork a new thread, may be within the same process.
 2041  */
 2042 void
 2043 sched_fork_thread(struct thread *td, struct thread *child)
 2044 {
 2045         struct td_sched *ts;
 2046         struct td_sched *ts2;
 2047 
 2048         /*
 2049          * Initialize child.
 2050          */
 2051         THREAD_LOCK_ASSERT(td, MA_OWNED);
 2052         sched_newthread(child);
 2053         child->td_lock = TDQ_LOCKPTR(TDQ_SELF());
 2054         child->td_cpuset = cpuset_ref(td->td_cpuset);
 2055         ts = td->td_sched;
 2056         ts2 = child->td_sched;
 2057         ts2->ts_cpu = ts->ts_cpu;
 2058         ts2->ts_runq = NULL;
 2059         /*
 2060          * Grab our parents cpu estimation information and priority.
 2061          */
 2062         ts2->ts_ticks = ts->ts_ticks;
 2063         ts2->ts_ltick = ts->ts_ltick;
 2064         ts2->ts_incrtick = ts->ts_incrtick;
 2065         ts2->ts_ftick = ts->ts_ftick;
 2066         child->td_user_pri = td->td_user_pri;
 2067         child->td_base_user_pri = td->td_base_user_pri;
 2068         /*
 2069          * And update interactivity score.
 2070          */
 2071         ts2->ts_slptime = ts->ts_slptime;
 2072         ts2->ts_runtime = ts->ts_runtime;
 2073         ts2->ts_slice = 1;      /* Attempt to quickly learn interactivity. */
 2074 }
 2075 
 2076 /*
 2077  * Adjust the priority class of a thread.
 2078  */
 2079 void
 2080 sched_class(struct thread *td, int class)
 2081 {
 2082 
 2083         THREAD_LOCK_ASSERT(td, MA_OWNED);
 2084         if (td->td_pri_class == class)
 2085                 return;
 2086 
 2087 #ifdef SMP
 2088         /*
 2089          * On SMP if we're on the RUNQ we must adjust the transferable
 2090          * count because could be changing to or from an interrupt
 2091          * class.
 2092          */
 2093         if (TD_ON_RUNQ(td)) {
 2094                 struct tdq *tdq;
 2095 
 2096                 tdq = TDQ_CPU(td->td_sched->ts_cpu);
 2097                 if (THREAD_CAN_MIGRATE(td)) {
 2098                         tdq->tdq_transferable--;
 2099                         tdq->tdq_group->tdg_transferable--;
 2100                 }
 2101                 td->td_pri_class = class;
 2102                 if (THREAD_CAN_MIGRATE(td)) {
 2103                         tdq->tdq_transferable++;
 2104                         tdq->tdq_group->tdg_transferable++;
 2105                 }
 2106         }
 2107 #endif
 2108         td->td_pri_class = class;
 2109 }
 2110 
 2111 /*
 2112  * Return some of the child's priority and interactivity to the parent.
 2113  */
 2114 void
 2115 sched_exit(struct proc *p, struct thread *child)
 2116 {
 2117         struct thread *td;
 2118         
 2119         CTR3(KTR_SCHED, "sched_exit: %p(%s) prio %d",
 2120             child, child->td_proc->p_comm, child->td_priority);
 2121 
 2122         PROC_SLOCK_ASSERT(p, MA_OWNED);
 2123         td = FIRST_THREAD_IN_PROC(p);
 2124         sched_exit_thread(td, child);
 2125 }
 2126 
 2127 /*
 2128  * Penalize another thread for the time spent on this one.  This helps to
 2129  * worsen the priority and interactivity of processes which schedule batch
 2130  * jobs such as make.  This has little effect on the make process itself but
 2131  * causes new processes spawned by it to receive worse scores immediately.
 2132  */
 2133 void
 2134 sched_exit_thread(struct thread *td, struct thread *child)
 2135 {
 2136 
 2137         CTR3(KTR_SCHED, "sched_exit_thread: %p(%s) prio %d",
 2138             child, child->td_proc->p_comm, child->td_priority);
 2139 
 2140 #ifdef KSE
 2141         /*
 2142          * KSE forks and exits so often that this penalty causes short-lived
 2143          * threads to always be non-interactive.  This causes mozilla to
 2144          * crawl under load.
 2145          */
 2146         if ((td->td_pflags & TDP_SA) && td->td_proc == child->td_proc)
 2147                 return;
 2148 #endif
 2149         /*
 2150          * Give the child's runtime to the parent without returning the
 2151          * sleep time as a penalty to the parent.  This causes shells that
 2152          * launch expensive things to mark their children as expensive.
 2153          */
 2154         thread_lock(td);
 2155         td->td_sched->ts_runtime += child->td_sched->ts_runtime;
 2156         sched_interact_update(td);
 2157         sched_priority(td);
 2158         thread_unlock(td);
 2159 }
 2160 
 2161 /*
 2162  * Fix priorities on return to user-space.  Priorities may be elevated due
 2163  * to static priorities in msleep() or similar.
 2164  */
 2165 void
 2166 sched_userret(struct thread *td)
 2167 {
 2168         /*
 2169          * XXX we cheat slightly on the locking here to avoid locking in  
 2170          * the usual case.  Setting td_priority here is essentially an
 2171          * incomplete workaround for not setting it properly elsewhere.
 2172          * Now that some interrupt handlers are threads, not setting it
 2173          * properly elsewhere can clobber it in the window between setting
 2174          * it here and returning to user mode, so don't waste time setting
 2175          * it perfectly here.
 2176          */
 2177         KASSERT((td->td_flags & TDF_BORROWING) == 0,
 2178             ("thread with borrowed priority returning to userland"));
 2179         if (td->td_priority != td->td_user_pri) {
 2180                 thread_lock(td);
 2181                 td->td_priority = td->td_user_pri;
 2182                 td->td_base_pri = td->td_user_pri;
 2183                 thread_unlock(td);
 2184         }
 2185 }
 2186 
 2187 /*
 2188  * Handle a stathz tick.  This is really only relevant for timeshare
 2189  * threads.
 2190  */
 2191 void
 2192 sched_clock(struct thread *td)
 2193 {
 2194         struct tdq *tdq;
 2195         struct td_sched *ts;
 2196 
 2197         THREAD_LOCK_ASSERT(td, MA_OWNED);
 2198         tdq = TDQ_SELF();
 2199 #ifdef SMP
 2200         /*
 2201          * We run the long term load balancer infrequently on the first cpu.
 2202          */
 2203         if (balance_tdq == tdq) {
 2204                 if (balance_ticks && --balance_ticks == 0)
 2205                         sched_balance();
 2206                 if (balance_group_ticks && --balance_group_ticks == 0)
 2207                         sched_balance_groups();
 2208         }
 2209 #endif
 2210         /*
 2211          * Advance the insert index once for each tick to ensure that all
 2212          * threads get a chance to run.
 2213          */
 2214         if (tdq->tdq_idx == tdq->tdq_ridx) {
 2215                 tdq->tdq_idx = (tdq->tdq_idx + 1) % RQ_NQS;
 2216                 if (TAILQ_EMPTY(&tdq->tdq_timeshare.rq_queues[tdq->tdq_ridx]))
 2217                         tdq->tdq_ridx = tdq->tdq_idx;
 2218         }
 2219         ts = td->td_sched;
 2220         if (td->td_pri_class & PRI_FIFO_BIT)
 2221                 return;
 2222         if (td->td_pri_class == PRI_TIMESHARE) {
 2223                 /*
 2224                  * We used a tick; charge it to the thread so
 2225                  * that we can compute our interactivity.
 2226                  */
 2227                 td->td_sched->ts_runtime += tickincr;
 2228                 sched_interact_update(td);
 2229         }
 2230         /*
 2231          * We used up one time slice.
 2232          */
 2233         if (--ts->ts_slice > 0)
 2234                 return;
 2235         /*
 2236          * We're out of time, recompute priorities and requeue.
 2237          */
 2238         sched_priority(td);
 2239         td->td_flags |= TDF_NEEDRESCHED;
 2240 }
 2241 
 2242 /*
 2243  * Called once per hz tick.  Used for cpu utilization information.  This
 2244  * is easier than trying to scale based on stathz.
 2245  */
 2246 void
 2247 sched_tick(void)
 2248 {
 2249         struct td_sched *ts;
 2250 
 2251         ts = curthread->td_sched;
 2252         /*
 2253          * Ticks is updated asynchronously on a single cpu.  Check here to
 2254          * avoid incrementing ts_ticks multiple times in a single tick.
 2255          */
 2256         if (ts->ts_incrtick == ticks)
 2257                 return;
 2258         /* Adjust ticks for pctcpu */
 2259         ts->ts_ticks += 1 << SCHED_TICK_SHIFT;
 2260         ts->ts_incrtick = ticks;
 2261         ts->ts_ltick = ticks;
 2262         /*
 2263          * Update if we've exceeded our desired tick threshhold by over one
 2264          * second.
 2265          */
 2266         if (ts->ts_ftick + SCHED_TICK_MAX < ts->ts_ltick)
 2267                 sched_pctcpu_update(ts);
 2268 }
 2269 
 2270 /*
 2271  * Return whether the current CPU has runnable tasks.  Used for in-kernel
 2272  * cooperative idle threads.
 2273  */
 2274 int
 2275 sched_runnable(void)
 2276 {
 2277         struct tdq *tdq;
 2278         int load;
 2279 
 2280         load = 1;
 2281 
 2282         tdq = TDQ_SELF();
 2283         if ((curthread->td_flags & TDF_IDLETD) != 0) {
 2284                 if (tdq->tdq_load > 0)
 2285                         goto out;
 2286         } else
 2287                 if (tdq->tdq_load - 1 > 0)
 2288                         goto out;
 2289         load = 0;
 2290 out:
 2291         return (load);
 2292 }
 2293 
 2294 /*
 2295  * Choose the highest priority thread to run.  The thread is removed from
 2296  * the run-queue while running however the load remains.  For SMP we set
 2297  * the tdq in the global idle bitmask if it idles here.
 2298  */
 2299 struct thread *
 2300 sched_choose(void)
 2301 {
 2302 #ifdef SMP
 2303         struct tdq_group *tdg;
 2304 #endif
 2305         struct td_sched *ts;
 2306         struct tdq *tdq;
 2307 
 2308         tdq = TDQ_SELF();
 2309         TDQ_LOCK_ASSERT(tdq, MA_OWNED);
 2310         ts = tdq_choose(tdq);
 2311         if (ts) {
 2312                 tdq_runq_rem(tdq, ts);
 2313                 return (ts->ts_thread);
 2314         }
 2315 #ifdef SMP
 2316         /*
 2317          * We only set the idled bit when all of the cpus in the group are
 2318          * idle.  Otherwise we could get into a situation where a thread bounces
 2319          * back and forth between two idle cores on seperate physical CPUs.
 2320          */
 2321         tdg = tdq->tdq_group;
 2322         tdg->tdg_idlemask |= PCPU_GET(cpumask);
 2323         if (tdg->tdg_idlemask == tdg->tdg_cpumask)
 2324                 atomic_set_int(&tdq_idle, tdg->tdg_mask);
 2325         tdq->tdq_lowpri = PRI_MAX_IDLE;
 2326 #endif
 2327         return (PCPU_GET(idlethread));
 2328 }
 2329 
 2330 /*
 2331  * Set owepreempt if necessary.  Preemption never happens directly in ULE,
 2332  * we always request it once we exit a critical section.
 2333  */
 2334 static inline void
 2335 sched_setpreempt(struct thread *td)
 2336 {
 2337         struct thread *ctd;
 2338         int cpri;
 2339         int pri;
 2340 
 2341         ctd = curthread;
 2342         pri = td->td_priority;
 2343         cpri = ctd->td_priority;
 2344         if (td->td_priority < ctd->td_priority)
 2345                 curthread->td_flags |= TDF_NEEDRESCHED;
 2346         if (panicstr != NULL || pri >= cpri || cold || TD_IS_INHIBITED(ctd))
 2347                 return;
 2348         /*
 2349          * Always preempt IDLE threads.  Otherwise only if the preempting
 2350          * thread is an ithread.
 2351          */
 2352         if (pri > preempt_thresh && cpri < PRI_MIN_IDLE)
 2353                 return;
 2354         ctd->td_owepreempt = 1;
 2355         return;
 2356 }
 2357 
 2358 /*
 2359  * Add a thread to a thread queue.  Initializes priority, slice, runq, and
 2360  * add it to the appropriate queue.  This is the internal function called
 2361  * when the tdq is predetermined.
 2362  */
 2363 void
 2364 tdq_add(struct tdq *tdq, struct thread *td, int flags)
 2365 {
 2366         struct td_sched *ts;
 2367         int class;
 2368 #ifdef SMP
 2369         int cpumask;
 2370 #endif
 2371 
 2372         TDQ_LOCK_ASSERT(tdq, MA_OWNED);
 2373         KASSERT((td->td_inhibitors == 0),
 2374             ("sched_add: trying to run inhibited thread"));
 2375         KASSERT((TD_CAN_RUN(td) || TD_IS_RUNNING(td)),
 2376             ("sched_add: bad thread state"));
 2377         KASSERT(td->td_flags & TDF_INMEM,
 2378             ("sched_add: thread swapped out"));
 2379 
 2380         ts = td->td_sched;
 2381         class = PRI_BASE(td->td_pri_class);
 2382         TD_SET_RUNQ(td);
 2383         if (ts->ts_slice == 0)
 2384                 ts->ts_slice = sched_slice;
 2385         /*
 2386          * Pick the run queue based on priority.
 2387          */
 2388         if (td->td_priority <= PRI_MAX_REALTIME)
 2389                 ts->ts_runq = &tdq->tdq_realtime;
 2390         else if (td->td_priority <= PRI_MAX_TIMESHARE)
 2391                 ts->ts_runq = &tdq->tdq_timeshare;
 2392         else
 2393                 ts->ts_runq = &tdq->tdq_idle;
 2394 #ifdef SMP
 2395         cpumask = 1 << ts->ts_cpu;
 2396         /*
 2397          * If we had been idle, clear our bit in the group and potentially
 2398          * the global bitmap.
 2399          */
 2400         if ((class != PRI_IDLE && class != PRI_ITHD) &&
 2401             (tdq->tdq_group->tdg_idlemask & cpumask) != 0) {
 2402                 /*
 2403                  * Check to see if our group is unidling, and if so, remove it
 2404                  * from the global idle mask.
 2405                  */
 2406                 if (tdq->tdq_group->tdg_idlemask ==
 2407                     tdq->tdq_group->tdg_cpumask)
 2408                         atomic_clear_int(&tdq_idle, tdq->tdq_group->tdg_mask);
 2409                 /*
 2410                  * Now remove ourselves from the group specific idle mask.
 2411                  */
 2412                 tdq->tdq_group->tdg_idlemask &= ~cpumask;
 2413         }
 2414         if (td->td_priority < tdq->tdq_lowpri)
 2415                 tdq->tdq_lowpri = td->td_priority;
 2416 #endif
 2417         tdq_runq_add(tdq, ts, flags);
 2418         tdq_load_add(tdq, ts);
 2419 }
 2420 
 2421 /*
 2422  * Select the target thread queue and add a thread to it.  Request
 2423  * preemption or IPI a remote processor if required.
 2424  */
 2425 void
 2426 sched_add(struct thread *td, int flags)
 2427 {
 2428         struct td_sched *ts;
 2429         struct tdq *tdq;
 2430 #ifdef SMP
 2431         int cpuid;
 2432         int cpu;
 2433 #endif
 2434         CTR5(KTR_SCHED, "sched_add: %p(%s) prio %d by %p(%s)",
 2435             td, td->td_proc->p_comm, td->td_priority, curthread,
 2436             curthread->td_proc->p_comm);
 2437         THREAD_LOCK_ASSERT(td, MA_OWNED);
 2438         ts = td->td_sched;
 2439         /*
 2440          * Recalculate the priority before we select the target cpu or
 2441          * run-queue.
 2442          */
 2443         if (PRI_BASE(td->td_pri_class) == PRI_TIMESHARE)
 2444                 sched_priority(td);
 2445 #ifdef SMP
 2446         cpuid = PCPU_GET(cpuid);
 2447         /*
 2448          * Pick the destination cpu and if it isn't ours transfer to the
 2449          * target cpu.
 2450          */
 2451         if (td->td_priority <= PRI_MAX_ITHD && THREAD_CAN_MIGRATE(td) &&
 2452             curthread->td_intr_nesting_level)
 2453                 ts->ts_cpu = cpuid;
 2454         if (!THREAD_CAN_MIGRATE(td))
 2455                 cpu = ts->ts_cpu;
 2456         else
 2457                 cpu = sched_pickcpu(td, flags);
 2458         tdq = sched_setcpu(ts, cpu, flags);
 2459         tdq_add(tdq, td, flags);
 2460         if (cpu != cpuid) {
 2461                 tdq_notify(ts);
 2462                 return;
 2463         }
 2464 #else
 2465         tdq = TDQ_SELF();
 2466         TDQ_LOCK(tdq);
 2467         /*
 2468          * Now that the thread is moving to the run-queue, set the lock
 2469          * to the scheduler's lock.
 2470          */
 2471         thread_lock_set(td, TDQ_LOCKPTR(tdq));
 2472         tdq_add(tdq, td, flags);
 2473 #endif
 2474         if (!(flags & SRQ_YIELDING))
 2475                 sched_setpreempt(td);
 2476 }
 2477 
 2478 /*
 2479  * Remove a thread from a run-queue without running it.  This is used
 2480  * when we're stealing a thread from a remote queue.  Otherwise all threads
 2481  * exit by calling sched_exit_thread() and sched_throw() themselves.
 2482  */
 2483 void
 2484 sched_rem(struct thread *td)
 2485 {
 2486         struct tdq *tdq;
 2487         struct td_sched *ts;
 2488 
 2489         CTR5(KTR_SCHED, "sched_rem: %p(%s) prio %d by %p(%s)",
 2490             td, td->td_proc->p_comm, td->td_priority, curthread,
 2491             curthread->td_proc->p_comm);
 2492         ts = td->td_sched;
 2493         tdq = TDQ_CPU(ts->ts_cpu);
 2494         TDQ_LOCK_ASSERT(tdq, MA_OWNED);
 2495         MPASS(td->td_lock == TDQ_LOCKPTR(tdq));
 2496         KASSERT(TD_ON_RUNQ(td),
 2497             ("sched_rem: thread not on run queue"));
 2498         tdq_runq_rem(tdq, ts);
 2499         tdq_load_rem(tdq, ts);
 2500         TD_SET_CAN_RUN(td);
 2501 }
 2502 
 2503 /*
 2504  * Fetch cpu utilization information.  Updates on demand.
 2505  */
 2506 fixpt_t
 2507 sched_pctcpu(struct thread *td)
 2508 {
 2509         fixpt_t pctcpu;
 2510         struct td_sched *ts;
 2511 
 2512         pctcpu = 0;
 2513         ts = td->td_sched;
 2514         if (ts == NULL)
 2515                 return (0);
 2516 
 2517         thread_lock(td);
 2518         if (ts->ts_ticks) {
 2519                 int rtick;
 2520 
 2521                 sched_pctcpu_update(ts);
 2522                 /* How many rtick per second ? */
 2523                 rtick = min(SCHED_TICK_HZ(ts) / SCHED_TICK_SECS, hz);
 2524                 pctcpu = (FSCALE * ((FSCALE * rtick)/hz)) >> FSHIFT;
 2525         }
 2526         thread_unlock(td);
 2527 
 2528         return (pctcpu);
 2529 }
 2530 
 2531 /*
 2532  * Enforce affinity settings for a thread.  Called after adjustments to
 2533  * cpumask.
 2534  */
 2535 void
 2536 sched_affinity(struct thread *td)
 2537 {
 2538 #ifdef SMP
 2539         struct td_sched *ts;
 2540         int cpu;
 2541 
 2542         THREAD_LOCK_ASSERT(td, MA_OWNED);
 2543         ts = td->td_sched;
 2544         if (THREAD_CAN_SCHED(td, ts->ts_cpu))
 2545                 return;
 2546         if (TD_ON_RUNQ(td)) {
 2547                 sched_rem(td);
 2548                 sched_add(td, SRQ_BORING);
 2549                 return;
 2550         }
 2551         if (!TD_IS_RUNNING(td))
 2552                 return;
 2553         td->td_flags |= TDF_NEEDRESCHED;
 2554         if (!THREAD_CAN_MIGRATE(td))
 2555                 return;
 2556         /*
 2557          * Assign the new cpu and force a switch before returning to
 2558          * userspace.  If the target thread is not running locally send
 2559          * an ipi to force the issue.
 2560          */
 2561         cpu = ts->ts_cpu;
 2562         ts->ts_cpu = sched_pickcpu(td, 0);
 2563         if (cpu != PCPU_GET(cpuid))
 2564                 ipi_selected(1 << cpu, IPI_PREEMPT);
 2565 #endif
 2566 }
 2567 
 2568 /*
 2569  * Bind a thread to a target cpu.
 2570  */
 2571 void
 2572 sched_bind(struct thread *td, int cpu)
 2573 {
 2574         struct td_sched *ts;
 2575 
 2576         THREAD_LOCK_ASSERT(td, MA_OWNED|MA_NOTRECURSED);
 2577         ts = td->td_sched;
 2578         if (ts->ts_flags & TSF_BOUND)
 2579                 sched_unbind(td);
 2580         ts->ts_flags |= TSF_BOUND;
 2581 #ifdef SMP
 2582         sched_pin();
 2583         if (PCPU_GET(cpuid) == cpu)
 2584                 return;
 2585         ts->ts_cpu = cpu;
 2586         /* When we return from mi_switch we'll be on the correct cpu. */
 2587         mi_switch(SW_VOL, NULL);
 2588 #endif
 2589 }
 2590 
 2591 /*
 2592  * Release a bound thread.
 2593  */
 2594 void
 2595 sched_unbind(struct thread *td)
 2596 {
 2597         struct td_sched *ts;
 2598 
 2599         THREAD_LOCK_ASSERT(td, MA_OWNED);
 2600         ts = td->td_sched;
 2601         if ((ts->ts_flags & TSF_BOUND) == 0)
 2602                 return;
 2603         ts->ts_flags &= ~TSF_BOUND;
 2604 #ifdef SMP
 2605         sched_unpin();
 2606 #endif
 2607 }
 2608 
 2609 int
 2610 sched_is_bound(struct thread *td)
 2611 {
 2612         THREAD_LOCK_ASSERT(td, MA_OWNED);
 2613         return (td->td_sched->ts_flags & TSF_BOUND);
 2614 }
 2615 
 2616 /*
 2617  * Basic yield call.
 2618  */
 2619 void
 2620 sched_relinquish(struct thread *td)
 2621 {
 2622         thread_lock(td);
 2623         SCHED_STAT_INC(switch_relinquish);
 2624         mi_switch(SW_VOL, NULL);
 2625         thread_unlock(td);
 2626 }
 2627 
 2628 /*
 2629  * Return the total system load.
 2630  */
 2631 int
 2632 sched_load(void)
 2633 {
 2634 #ifdef SMP
 2635         int total;
 2636         int i;
 2637 
 2638         total = 0;
 2639         for (i = 0; i <= tdg_maxid; i++)
 2640                 total += TDQ_GROUP(i)->tdg_load;
 2641         return (total);
 2642 #else
 2643         return (TDQ_SELF()->tdq_sysload);
 2644 #endif
 2645 }
 2646 
 2647 int
 2648 sched_sizeof_proc(void)
 2649 {
 2650         return (sizeof(struct proc));
 2651 }
 2652 
 2653 int
 2654 sched_sizeof_thread(void)
 2655 {
 2656         return (sizeof(struct thread) + sizeof(struct td_sched));
 2657 }
 2658 
 2659 /*
 2660  * The actual idle process.
 2661  */
 2662 void
 2663 sched_idletd(void *dummy)
 2664 {
 2665         struct thread *td;
 2666         struct tdq *tdq;
 2667 
 2668         td = curthread;
 2669         tdq = TDQ_SELF();
 2670         mtx_assert(&Giant, MA_NOTOWNED);
 2671         /* ULE relies on preemption for idle interruption. */
 2672         for (;;) {
 2673 #ifdef SMP
 2674                 if (tdq_idled(tdq))
 2675                         cpu_idle();
 2676 #else
 2677                 cpu_idle();
 2678 #endif
 2679         }
 2680 }
 2681 
 2682 /*
 2683  * A CPU is entering for the first time or a thread is exiting.
 2684  */
 2685 void
 2686 sched_throw(struct thread *td)
 2687 {
 2688         struct thread *newtd;
 2689         struct tdq *tdq;
 2690 
 2691         tdq = TDQ_SELF();
 2692         if (td == NULL) {
 2693                 /* Correct spinlock nesting and acquire the correct lock. */
 2694                 TDQ_LOCK(tdq);
 2695                 spinlock_exit();
 2696         } else {
 2697                 MPASS(td->td_lock == TDQ_LOCKPTR(tdq));
 2698                 tdq_load_rem(tdq, td->td_sched);
 2699         }
 2700         KASSERT(curthread->td_md.md_spinlock_count == 1, ("invalid count"));
 2701         newtd = choosethread();
 2702         TDQ_LOCKPTR(tdq)->mtx_lock = (uintptr_t)newtd;
 2703         PCPU_SET(switchtime, cpu_ticks());
 2704         PCPU_SET(switchticks, ticks);
 2705         cpu_throw(td, newtd);           /* doesn't return */
 2706 }
 2707 
 2708 /*
 2709  * This is called from fork_exit().  Just acquire the correct locks and
 2710  * let fork do the rest of the work.
 2711  */
 2712 void
 2713 sched_fork_exit(struct thread *td)
 2714 {
 2715         struct td_sched *ts;
 2716         struct tdq *tdq;
 2717         int cpuid;
 2718 
 2719         /*
 2720          * Finish setting up thread glue so that it begins execution in a
 2721          * non-nested critical section with the scheduler lock held.
 2722          */
 2723         cpuid = PCPU_GET(cpuid);
 2724         tdq = TDQ_CPU(cpuid);
 2725         ts = td->td_sched;
 2726         if (TD_IS_IDLETHREAD(td))
 2727                 td->td_lock = TDQ_LOCKPTR(tdq);
 2728         MPASS(td->td_lock == TDQ_LOCKPTR(tdq));
 2729         td->td_oncpu = cpuid;
 2730         TDQ_LOCK_ASSERT(tdq, MA_OWNED | MA_NOTRECURSED);
 2731 }
 2732 
 2733 static SYSCTL_NODE(_kern, OID_AUTO, sched, CTLFLAG_RW, 0,
 2734     "Scheduler");
 2735 SYSCTL_STRING(_kern_sched, OID_AUTO, name, CTLFLAG_RD, "ULE", 0,
 2736     "Scheduler name");
 2737 SYSCTL_INT(_kern_sched, OID_AUTO, slice, CTLFLAG_RW, &sched_slice, 0,
 2738     "Slice size for timeshare threads");
 2739 SYSCTL_INT(_kern_sched, OID_AUTO, interact, CTLFLAG_RW, &sched_interact, 0,
 2740      "Interactivity score threshold");
 2741 SYSCTL_INT(_kern_sched, OID_AUTO, preempt_thresh, CTLFLAG_RW, &preempt_thresh,
 2742      0,"Min priority for preemption, lower priorities have greater precedence");
 2743 #ifdef SMP
 2744 SYSCTL_INT(_kern_sched, OID_AUTO, pick_pri, CTLFLAG_RW, &pick_pri, 0,
 2745     "Pick the target cpu based on priority rather than load.");
 2746 SYSCTL_INT(_kern_sched, OID_AUTO, affinity, CTLFLAG_RW, &affinity, 0,
 2747     "Number of hz ticks to keep thread affinity for");
 2748 SYSCTL_INT(_kern_sched, OID_AUTO, tryself, CTLFLAG_RW, &tryself, 0, "");
 2749 SYSCTL_INT(_kern_sched, OID_AUTO, balance, CTLFLAG_RW, &rebalance, 0,
 2750     "Enables the long-term load balancer");
 2751 SYSCTL_INT(_kern_sched, OID_AUTO, balance_interval, CTLFLAG_RW,
 2752     &balance_interval, 0,
 2753     "Average frequency in stathz ticks to run the long-term balancer");
 2754 SYSCTL_INT(_kern_sched, OID_AUTO, steal_htt, CTLFLAG_RW, &steal_htt, 0,
 2755     "Steals work from another hyper-threaded core on idle");
 2756 SYSCTL_INT(_kern_sched, OID_AUTO, steal_idle, CTLFLAG_RW, &steal_idle, 0,
 2757     "Attempts to steal work from other cores before idling");
 2758 SYSCTL_INT(_kern_sched, OID_AUTO, steal_thresh, CTLFLAG_RW, &steal_thresh, 0,
 2759     "Minimum load on remote cpu before we'll steal");
 2760 SYSCTL_INT(_kern_sched, OID_AUTO, topology, CTLFLAG_RD, &topology, 0,
 2761     "True when a topology has been specified by the MD code.");
 2762 #endif
 2763 
 2764 /* ps compat.  All cpu percentages from ULE are weighted. */
 2765 static int ccpu = 0;
 2766 SYSCTL_INT(_kern, OID_AUTO, ccpu, CTLFLAG_RD, &ccpu, 0, "");
 2767 
 2768 
 2769 #define KERN_SWITCH_INCLUDE 1
 2770 #include "kern/kern_switch.c"

Cache object: 73c34b357f3c5a32b2096b46ad73a528


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.