]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/sched_ule.c
In some particular cases (like in pccard and pccbb), the real device
[FreeBSD/FreeBSD.git] / sys / kern / sched_ule.c
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 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include "opt_hwpmc_hooks.h"
31 #include "opt_sched.h"
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kdb.h>
36 #include <sys/kernel.h>
37 #include <sys/ktr.h>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 #include <sys/proc.h>
41 #include <sys/resource.h>
42 #include <sys/resourcevar.h>
43 #include <sys/sched.h>
44 #include <sys/smp.h>
45 #include <sys/sx.h>
46 #include <sys/sysctl.h>
47 #include <sys/sysproto.h>
48 #include <sys/turnstile.h>
49 #include <sys/umtx.h>
50 #include <sys/vmmeter.h>
51 #ifdef KTRACE
52 #include <sys/uio.h>
53 #include <sys/ktrace.h>
54 #endif
55
56 #ifdef HWPMC_HOOKS
57 #include <sys/pmckern.h>
58 #endif
59
60 #include <machine/cpu.h>
61 #include <machine/smp.h>
62
63 #ifndef PREEMPTION
64 #error  "SCHED_ULE requires options PREEMPTION"
65 #endif
66
67 /*
68  * TODO:
69  *      Pick idle from affinity group or self group first.
70  *      Implement pick_score.
71  */
72
73 #define KTR_ULE 0x0             /* Enable for pickpri debugging. */
74
75 /*
76  * Thread scheduler specific section.
77  */
78 struct td_sched {       
79         TAILQ_ENTRY(td_sched) ts_procq; /* (j/z) Run queue. */
80         int             ts_flags;       /* (j) TSF_* flags. */
81         struct thread   *ts_thread;     /* (*) Active associated thread. */
82         u_char          ts_rqindex;     /* (j) Run queue index. */
83         int             ts_slptime;
84         int             ts_slice;
85         struct runq     *ts_runq;
86         u_char          ts_cpu;         /* CPU that we have affinity for. */
87         /* The following variables are only used for pctcpu calculation */
88         int             ts_ltick;       /* Last tick that we were running on */
89         int             ts_ftick;       /* First tick that we were running on */
90         int             ts_ticks;       /* Tick count */
91 #ifdef SMP
92         int             ts_rltick;      /* Real last tick, for affinity. */
93 #endif
94
95         /* originally from kg_sched */
96         u_int   skg_slptime;            /* Number of ticks we vol. slept */
97         u_int   skg_runtime;            /* Number of ticks we were running */
98 };
99 /* flags kept in ts_flags */
100 #define TSF_BOUND       0x0001          /* Thread can not migrate. */
101 #define TSF_XFERABLE    0x0002          /* Thread was added as transferable. */
102
103 static struct td_sched td_sched0;
104
105 /*
106  * Cpu percentage computation macros and defines.
107  *
108  * SCHED_TICK_SECS:     Number of seconds to average the cpu usage across.
109  * SCHED_TICK_TARG:     Number of hz ticks to average the cpu usage across.
110  * SCHED_TICK_MAX:      Maximum number of ticks before scaling back.
111  * SCHED_TICK_SHIFT:    Shift factor to avoid rounding away results.
112  * SCHED_TICK_HZ:       Compute the number of hz ticks for a given ticks count.
113  * SCHED_TICK_TOTAL:    Gives the amount of time we've been recording ticks.
114  */
115 #define SCHED_TICK_SECS         10
116 #define SCHED_TICK_TARG         (hz * SCHED_TICK_SECS)
117 #define SCHED_TICK_MAX          (SCHED_TICK_TARG + hz)
118 #define SCHED_TICK_SHIFT        10
119 #define SCHED_TICK_HZ(ts)       ((ts)->ts_ticks >> SCHED_TICK_SHIFT)
120 #define SCHED_TICK_TOTAL(ts)    (max((ts)->ts_ltick - (ts)->ts_ftick, hz))
121
122 /*
123  * These macros determine priorities for non-interactive threads.  They are
124  * assigned a priority based on their recent cpu utilization as expressed
125  * by the ratio of ticks to the tick total.  NHALF priorities at the start
126  * and end of the MIN to MAX timeshare range are only reachable with negative
127  * or positive nice respectively.
128  *
129  * PRI_RANGE:   Priority range for utilization dependent priorities.
130  * PRI_NRESV:   Number of nice values.
131  * PRI_TICKS:   Compute a priority in PRI_RANGE from the ticks count and total.
132  * PRI_NICE:    Determines the part of the priority inherited from nice.
133  */
134 #define SCHED_PRI_NRESV         (PRIO_MAX - PRIO_MIN)
135 #define SCHED_PRI_NHALF         (SCHED_PRI_NRESV / 2)
136 #define SCHED_PRI_MIN           (PRI_MIN_TIMESHARE + SCHED_PRI_NHALF)
137 #define SCHED_PRI_MAX           (PRI_MAX_TIMESHARE - SCHED_PRI_NHALF)
138 #define SCHED_PRI_RANGE         (SCHED_PRI_MAX - SCHED_PRI_MIN + 1)
139 #define SCHED_PRI_TICKS(ts)                                             \
140     (SCHED_TICK_HZ((ts)) /                                              \
141     (roundup(SCHED_TICK_TOTAL((ts)), SCHED_PRI_RANGE) / SCHED_PRI_RANGE))
142 #define SCHED_PRI_NICE(nice)    (nice)
143
144 /*
145  * These determine the interactivity of a process.  Interactivity differs from
146  * cpu utilization in that it expresses the voluntary time slept vs time ran
147  * while cpu utilization includes all time not running.  This more accurately
148  * models the intent of the thread.
149  *
150  * SLP_RUN_MAX: Maximum amount of sleep time + run time we'll accumulate
151  *              before throttling back.
152  * SLP_RUN_FORK:        Maximum slp+run time to inherit at fork time.
153  * INTERACT_MAX:        Maximum interactivity value.  Smaller is better.
154  * INTERACT_THRESH:     Threshhold for placement on the current runq.
155  */
156 #define SCHED_SLP_RUN_MAX       ((hz * 5) << SCHED_TICK_SHIFT)
157 #define SCHED_SLP_RUN_FORK      ((hz / 2) << SCHED_TICK_SHIFT)
158 #define SCHED_INTERACT_MAX      (100)
159 #define SCHED_INTERACT_HALF     (SCHED_INTERACT_MAX / 2)
160 #define SCHED_INTERACT_THRESH   (30)
161
162 /*
163  * tickincr:            Converts a stathz tick into a hz domain scaled by
164  *                      the shift factor.  Without the shift the error rate
165  *                      due to rounding would be unacceptably high.
166  * realstathz:          stathz is sometimes 0 and run off of hz.
167  * sched_slice:         Runtime of each thread before rescheduling.
168  */
169 static int sched_interact = SCHED_INTERACT_THRESH;
170 static int realstathz;
171 static int tickincr;
172 static int sched_slice;
173
174 /*
175  * tdq - per processor runqs and statistics.
176  */
177 struct tdq {
178         struct runq     tdq_idle;               /* Queue of IDLE threads. */
179         struct runq     tdq_timeshare;          /* timeshare run queue. */
180         struct runq     tdq_realtime;           /* real-time run queue. */
181         u_char          tdq_idx;                /* Current insert index. */
182         u_char          tdq_ridx;               /* Current removal index. */
183         short           tdq_flags;              /* Thread queue flags */
184         int             tdq_load;               /* Aggregate load. */
185 #ifdef SMP
186         int             tdq_transferable;
187         LIST_ENTRY(tdq) tdq_siblings;           /* Next in tdq group. */
188         struct tdq_group *tdq_group;            /* Our processor group. */
189 #else
190         int             tdq_sysload;            /* For loadavg, !ITHD load. */
191 #endif
192 };
193
194 #define TDQF_BUSY       0x0001                  /* Queue is marked as busy */
195
196 #ifdef SMP
197 /*
198  * tdq groups are groups of processors which can cheaply share threads.  When
199  * one processor in the group goes idle it will check the runqs of the other
200  * processors in its group prior to halting and waiting for an interrupt.
201  * These groups are suitable for SMT (Symetric Multi-Threading) and not NUMA.
202  * In a numa environment we'd want an idle bitmap per group and a two tiered
203  * load balancer.
204  */
205 struct tdq_group {
206         int     tdg_cpus;               /* Count of CPUs in this tdq group. */
207         cpumask_t tdg_cpumask;          /* Mask of cpus in this group. */
208         cpumask_t tdg_idlemask;         /* Idle cpus in this group. */
209         cpumask_t tdg_mask;             /* Bit mask for first cpu. */
210         int     tdg_load;               /* Total load of this group. */
211         int     tdg_transferable;       /* Transferable load of this group. */
212         LIST_HEAD(, tdq) tdg_members;   /* Linked list of all members. */
213 };
214
215 #define SCHED_AFFINITY_DEFAULT  (hz / 100)
216 #define SCHED_AFFINITY(ts)      ((ts)->ts_rltick > ticks - affinity)
217
218 /*
219  * Run-time tunables.
220  */
221 static int rebalance = 0;
222 static int pick_pri = 1;
223 static int affinity;
224 static int tryself = 1;
225 static int tryselfidle = 1;
226 static int ipi_ast = 0;
227 static int ipi_preempt = 1;
228 static int ipi_thresh = PRI_MIN_KERN;
229 static int steal_htt = 1;
230 static int steal_busy = 1;
231 static int busy_thresh = 4;
232
233 /*
234  * One thread queue per processor.
235  */
236 static volatile cpumask_t tdq_idle;
237 static volatile cpumask_t tdq_busy;
238 static int tdg_maxid;
239 static struct tdq       tdq_cpu[MAXCPU];
240 static struct tdq_group tdq_groups[MAXCPU];
241 static int bal_tick;
242 static int gbal_tick;
243 static int balance_groups;
244
245 #define TDQ_SELF()      (&tdq_cpu[PCPU_GET(cpuid)])
246 #define TDQ_CPU(x)      (&tdq_cpu[(x)])
247 #define TDQ_ID(x)       ((x) - tdq_cpu)
248 #define TDQ_GROUP(x)    (&tdq_groups[(x)])
249 #else   /* !SMP */
250 static struct tdq       tdq_cpu;
251
252 #define TDQ_SELF()      (&tdq_cpu)
253 #define TDQ_CPU(x)      (&tdq_cpu)
254 #endif
255
256 static void sched_priority(struct thread *);
257 static void sched_thread_priority(struct thread *, u_char);
258 static int sched_interact_score(struct thread *);
259 static void sched_interact_update(struct thread *);
260 static void sched_interact_fork(struct thread *);
261 static void sched_pctcpu_update(struct td_sched *);
262 static inline void sched_pin_td(struct thread *td);
263 static inline void sched_unpin_td(struct thread *td);
264
265 /* Operations on per processor queues */
266 static struct td_sched * tdq_choose(struct tdq *);
267 static void tdq_setup(struct tdq *);
268 static void tdq_load_add(struct tdq *, struct td_sched *);
269 static void tdq_load_rem(struct tdq *, struct td_sched *);
270 static __inline void tdq_runq_add(struct tdq *, struct td_sched *, int);
271 static __inline void tdq_runq_rem(struct tdq *, struct td_sched *);
272 void tdq_print(int cpu);
273 static void runq_print(struct runq *rq);
274 #ifdef SMP
275 static int tdq_pickidle(struct tdq *, struct td_sched *);
276 static int tdq_pickpri(struct tdq *, struct td_sched *, int);
277 static struct td_sched *runq_steal(struct runq *);
278 static void sched_balance(void);
279 static void sched_balance_groups(void);
280 static void sched_balance_group(struct tdq_group *);
281 static void sched_balance_pair(struct tdq *, struct tdq *);
282 static void sched_smp_tick(struct thread *);
283 static void tdq_move(struct tdq *, int);
284 static int tdq_idled(struct tdq *);
285 static void tdq_notify(struct td_sched *);
286 static struct td_sched *tdq_steal(struct tdq *, int);
287
288 #define THREAD_CAN_MIGRATE(td)   ((td)->td_pinned == 0)
289 #endif
290
291 static void sched_setup(void *dummy);
292 SYSINIT(sched_setup, SI_SUB_RUN_QUEUE, SI_ORDER_FIRST, sched_setup, NULL)
293
294 static void sched_initticks(void *dummy);
295 SYSINIT(sched_initticks, SI_SUB_CLOCKS, SI_ORDER_THIRD, sched_initticks, NULL)
296
297 static inline void
298 sched_pin_td(struct thread *td)
299 {
300         td->td_pinned++;
301 }
302
303 static inline void
304 sched_unpin_td(struct thread *td)
305 {
306         td->td_pinned--;
307 }
308
309 static void
310 runq_print(struct runq *rq)
311 {
312         struct rqhead *rqh;
313         struct td_sched *ts;
314         int pri;
315         int j;
316         int i;
317
318         for (i = 0; i < RQB_LEN; i++) {
319                 printf("\t\trunq bits %d 0x%zx\n",
320                     i, rq->rq_status.rqb_bits[i]);
321                 for (j = 0; j < RQB_BPW; j++)
322                         if (rq->rq_status.rqb_bits[i] & (1ul << j)) {
323                                 pri = j + (i << RQB_L2BPW);
324                                 rqh = &rq->rq_queues[pri];
325                                 TAILQ_FOREACH(ts, rqh, ts_procq) {
326                                         printf("\t\t\ttd %p(%s) priority %d rqindex %d pri %d\n",
327                                             ts->ts_thread, ts->ts_thread->td_proc->p_comm, ts->ts_thread->td_priority, ts->ts_rqindex, pri);
328                                 }
329                         }
330         }
331 }
332
333 void
334 tdq_print(int cpu)
335 {
336         struct tdq *tdq;
337
338         tdq = TDQ_CPU(cpu);
339
340         printf("tdq:\n");
341         printf("\tload:           %d\n", tdq->tdq_load);
342         printf("\ttimeshare idx: %d\n", tdq->tdq_idx);
343         printf("\ttimeshare ridx: %d\n", tdq->tdq_ridx);
344         printf("\trealtime runq:\n");
345         runq_print(&tdq->tdq_realtime);
346         printf("\ttimeshare runq:\n");
347         runq_print(&tdq->tdq_timeshare);
348         printf("\tidle runq:\n");
349         runq_print(&tdq->tdq_idle);
350 #ifdef SMP
351         printf("\tload transferable: %d\n", tdq->tdq_transferable);
352 #endif
353 }
354
355 static __inline void
356 tdq_runq_add(struct tdq *tdq, struct td_sched *ts, int flags)
357 {
358 #ifdef SMP
359         if (THREAD_CAN_MIGRATE(ts->ts_thread)) {
360                 tdq->tdq_transferable++;
361                 tdq->tdq_group->tdg_transferable++;
362                 ts->ts_flags |= TSF_XFERABLE;
363                 if (tdq->tdq_transferable >= busy_thresh &&
364                     (tdq->tdq_flags & TDQF_BUSY) == 0) {
365                         tdq->tdq_flags |= TDQF_BUSY;
366                         atomic_set_int(&tdq_busy, 1 << TDQ_ID(tdq));
367                 }
368         }
369 #endif
370         if (ts->ts_runq == &tdq->tdq_timeshare) {
371                 u_char pri;
372
373                 pri = ts->ts_thread->td_priority;
374                 KASSERT(pri <= PRI_MAX_TIMESHARE && pri >= PRI_MIN_TIMESHARE,
375                         ("Invalid priority %d on timeshare runq", pri));
376                 /*
377                  * This queue contains only priorities between MIN and MAX
378                  * realtime.  Use the whole queue to represent these values.
379                  */
380 #define TS_RQ_PPQ       (((PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE) + 1) / RQ_NQS)
381                 if ((flags & SRQ_BORROWING) == 0) {
382                         pri = (pri - PRI_MIN_TIMESHARE) / TS_RQ_PPQ;
383                         pri = (pri + tdq->tdq_idx) % RQ_NQS;
384                         /*
385                          * This effectively shortens the queue by one so we
386                          * can have a one slot difference between idx and
387                          * ridx while we wait for threads to drain.
388                          */
389                         if (tdq->tdq_ridx != tdq->tdq_idx &&
390                             pri == tdq->tdq_ridx)
391                                 pri = (unsigned char)(pri - 1) % RQ_NQS;
392                 } else
393                         pri = tdq->tdq_ridx;
394                 runq_add_pri(ts->ts_runq, ts, pri, flags);
395         } else
396                 runq_add(ts->ts_runq, ts, flags);
397 }
398
399 static __inline void
400 tdq_runq_rem(struct tdq *tdq, struct td_sched *ts)
401 {
402 #ifdef SMP
403         if (ts->ts_flags & TSF_XFERABLE) {
404                 tdq->tdq_transferable--;
405                 tdq->tdq_group->tdg_transferable--;
406                 ts->ts_flags &= ~TSF_XFERABLE;
407                 if (tdq->tdq_transferable < busy_thresh && 
408                     (tdq->tdq_flags & TDQF_BUSY)) {
409                         atomic_clear_int(&tdq_busy, 1 << TDQ_ID(tdq));
410                         tdq->tdq_flags &= ~TDQF_BUSY;
411                 }
412         }
413 #endif
414         if (ts->ts_runq == &tdq->tdq_timeshare) {
415                 if (tdq->tdq_idx != tdq->tdq_ridx)
416                         runq_remove_idx(ts->ts_runq, ts, &tdq->tdq_ridx);
417                 else
418                         runq_remove_idx(ts->ts_runq, ts, NULL);
419                 /*
420                  * For timeshare threads we update the priority here so
421                  * the priority reflects the time we've been sleeping.
422                  */
423                 ts->ts_ltick = ticks;
424                 sched_pctcpu_update(ts);
425                 sched_priority(ts->ts_thread);
426         } else
427                 runq_remove(ts->ts_runq, ts);
428 }
429
430 static void
431 tdq_load_add(struct tdq *tdq, struct td_sched *ts)
432 {
433         int class;
434         mtx_assert(&sched_lock, MA_OWNED);
435         class = PRI_BASE(ts->ts_thread->td_pri_class);
436         tdq->tdq_load++;
437         CTR1(KTR_SCHED, "load: %d", tdq->tdq_load);
438         if (class != PRI_ITHD &&
439             (ts->ts_thread->td_proc->p_flag & P_NOLOAD) == 0)
440 #ifdef SMP
441                 tdq->tdq_group->tdg_load++;
442 #else
443                 tdq->tdq_sysload++;
444 #endif
445 }
446
447 static void
448 tdq_load_rem(struct tdq *tdq, struct td_sched *ts)
449 {
450         int class;
451         mtx_assert(&sched_lock, MA_OWNED);
452         class = PRI_BASE(ts->ts_thread->td_pri_class);
453         if (class != PRI_ITHD &&
454             (ts->ts_thread->td_proc->p_flag & P_NOLOAD) == 0)
455 #ifdef SMP
456                 tdq->tdq_group->tdg_load--;
457 #else
458                 tdq->tdq_sysload--;
459 #endif
460         tdq->tdq_load--;
461         CTR1(KTR_SCHED, "load: %d", tdq->tdq_load);
462         ts->ts_runq = NULL;
463 }
464
465 #ifdef SMP
466 static void
467 sched_smp_tick(struct thread *td)
468 {
469         struct tdq *tdq;
470
471         tdq = TDQ_SELF();
472         if (rebalance) {
473                 if (ticks >= bal_tick)
474                         sched_balance();
475                 if (ticks >= gbal_tick && balance_groups)
476                         sched_balance_groups();
477         }
478         td->td_sched->ts_rltick = ticks;
479 }
480
481 /*
482  * sched_balance is a simple CPU load balancing algorithm.  It operates by
483  * finding the least loaded and most loaded cpu and equalizing their load
484  * by migrating some processes.
485  *
486  * Dealing only with two CPUs at a time has two advantages.  Firstly, most
487  * installations will only have 2 cpus.  Secondly, load balancing too much at
488  * once can have an unpleasant effect on the system.  The scheduler rarely has
489  * enough information to make perfect decisions.  So this algorithm chooses
490  * algorithm simplicity and more gradual effects on load in larger systems.
491  *
492  * It could be improved by considering the priorities and slices assigned to
493  * each task prior to balancing them.  There are many pathological cases with
494  * any approach and so the semi random algorithm below may work as well as any.
495  *
496  */
497 static void
498 sched_balance(void)
499 {
500         struct tdq_group *high;
501         struct tdq_group *low;
502         struct tdq_group *tdg;
503         int cnt;
504         int i;
505
506         bal_tick = ticks + (random() % (hz * 2));
507         if (smp_started == 0)
508                 return;
509         low = high = NULL;
510         i = random() % (tdg_maxid + 1);
511         for (cnt = 0; cnt <= tdg_maxid; cnt++) {
512                 tdg = TDQ_GROUP(i);
513                 /*
514                  * Find the CPU with the highest load that has some
515                  * threads to transfer.
516                  */
517                 if ((high == NULL || tdg->tdg_load > high->tdg_load)
518                     && tdg->tdg_transferable)
519                         high = tdg;
520                 if (low == NULL || tdg->tdg_load < low->tdg_load)
521                         low = tdg;
522                 if (++i > tdg_maxid)
523                         i = 0;
524         }
525         if (low != NULL && high != NULL && high != low)
526                 sched_balance_pair(LIST_FIRST(&high->tdg_members),
527                     LIST_FIRST(&low->tdg_members));
528 }
529
530 static void
531 sched_balance_groups(void)
532 {
533         int i;
534
535         gbal_tick = ticks + (random() % (hz * 2));
536         mtx_assert(&sched_lock, MA_OWNED);
537         if (smp_started)
538                 for (i = 0; i <= tdg_maxid; i++)
539                         sched_balance_group(TDQ_GROUP(i));
540 }
541
542 static void
543 sched_balance_group(struct tdq_group *tdg)
544 {
545         struct tdq *tdq;
546         struct tdq *high;
547         struct tdq *low;
548         int load;
549
550         if (tdg->tdg_transferable == 0)
551                 return;
552         low = NULL;
553         high = NULL;
554         LIST_FOREACH(tdq, &tdg->tdg_members, tdq_siblings) {
555                 load = tdq->tdq_load;
556                 if (high == NULL || load > high->tdq_load)
557                         high = tdq;
558                 if (low == NULL || load < low->tdq_load)
559                         low = tdq;
560         }
561         if (high != NULL && low != NULL && high != low)
562                 sched_balance_pair(high, low);
563 }
564
565 static void
566 sched_balance_pair(struct tdq *high, struct tdq *low)
567 {
568         int transferable;
569         int high_load;
570         int low_load;
571         int move;
572         int diff;
573         int i;
574
575         /*
576          * If we're transfering within a group we have to use this specific
577          * tdq's transferable count, otherwise we can steal from other members
578          * of the group.
579          */
580         if (high->tdq_group == low->tdq_group) {
581                 transferable = high->tdq_transferable;
582                 high_load = high->tdq_load;
583                 low_load = low->tdq_load;
584         } else {
585                 transferable = high->tdq_group->tdg_transferable;
586                 high_load = high->tdq_group->tdg_load;
587                 low_load = low->tdq_group->tdg_load;
588         }
589         if (transferable == 0)
590                 return;
591         /*
592          * Determine what the imbalance is and then adjust that to how many
593          * threads we actually have to give up (transferable).
594          */
595         diff = high_load - low_load;
596         move = diff / 2;
597         if (diff & 0x1)
598                 move++;
599         move = min(move, transferable);
600         for (i = 0; i < move; i++)
601                 tdq_move(high, TDQ_ID(low));
602         return;
603 }
604
605 static void
606 tdq_move(struct tdq *from, int cpu)
607 {
608         struct tdq *tdq;
609         struct tdq *to;
610         struct td_sched *ts;
611
612         tdq = from;
613         to = TDQ_CPU(cpu);
614         ts = tdq_steal(tdq, 1);
615         if (ts == NULL) {
616                 struct tdq_group *tdg;
617
618                 tdg = tdq->tdq_group;
619                 LIST_FOREACH(tdq, &tdg->tdg_members, tdq_siblings) {
620                         if (tdq == from || tdq->tdq_transferable == 0)
621                                 continue;
622                         ts = tdq_steal(tdq, 1);
623                         break;
624                 }
625                 if (ts == NULL)
626                         panic("tdq_move: No threads available with a "
627                             "transferable count of %d\n", 
628                             tdg->tdg_transferable);
629         }
630         if (tdq == to)
631                 return;
632         sched_rem(ts->ts_thread);
633         ts->ts_cpu = cpu;
634         sched_pin_td(ts->ts_thread);
635         sched_add(ts->ts_thread, SRQ_YIELDING);
636         sched_unpin_td(ts->ts_thread);
637 }
638
639 static int
640 tdq_idled(struct tdq *tdq)
641 {
642         struct tdq_group *tdg;
643         struct tdq *steal;
644         struct td_sched *ts;
645
646         tdg = tdq->tdq_group;
647         /*
648          * If we're in a cpu group, try and steal threads from another cpu in
649          * the group before idling.
650          */
651         if (steal_htt && tdg->tdg_cpus > 1 && tdg->tdg_transferable) {
652                 LIST_FOREACH(steal, &tdg->tdg_members, tdq_siblings) {
653                         if (steal == tdq || steal->tdq_transferable == 0)
654                                 continue;
655                         ts = tdq_steal(steal, 0);
656                         if (ts)
657                                 goto steal;
658                 }
659         }
660         if (steal_busy) {
661                 while (tdq_busy) {
662                         int cpu;
663
664                         cpu = ffs(tdq_busy);
665                         if (cpu == 0)
666                                 break;
667                         cpu--;
668                         steal = TDQ_CPU(cpu);
669                         if (steal->tdq_transferable == 0)
670                                 continue;
671                         ts = tdq_steal(steal, 1);
672                         if (ts == NULL)
673                                 continue;
674                         CTR5(KTR_ULE,
675                             "tdq_idled: stealing td %p(%s) pri %d from %d busy 0x%X",
676                             ts->ts_thread, ts->ts_thread->td_proc->p_comm,
677                             ts->ts_thread->td_priority, cpu, tdq_busy);
678                         goto steal;
679                 }
680         }
681         /*
682          * We only set the idled bit when all of the cpus in the group are
683          * idle.  Otherwise we could get into a situation where a thread bounces
684          * back and forth between two idle cores on seperate physical CPUs.
685          */
686         tdg->tdg_idlemask |= PCPU_GET(cpumask);
687         if (tdg->tdg_idlemask == tdg->tdg_cpumask)
688                 atomic_set_int(&tdq_idle, tdg->tdg_mask);
689         return (1);
690 steal:
691         sched_rem(ts->ts_thread);
692         ts->ts_cpu = PCPU_GET(cpuid);
693         sched_pin_td(ts->ts_thread);
694         sched_add(ts->ts_thread, SRQ_YIELDING);
695         sched_unpin_td(ts->ts_thread);
696
697         return (0);
698 }
699
700 static void
701 tdq_notify(struct td_sched *ts)
702 {
703         struct thread *ctd;
704         struct pcpu *pcpu;
705         int cpri;
706         int pri;
707         int cpu;
708
709         cpu = ts->ts_cpu;
710         pri = ts->ts_thread->td_priority;
711         pcpu = pcpu_find(cpu);
712         ctd = pcpu->pc_curthread;
713         cpri = ctd->td_priority;
714
715         /*
716          * If our priority is not better than the current priority there is
717          * nothing to do.
718          */
719         if (pri > cpri)
720                 return;
721         /*
722          * Always IPI idle.
723          */
724         if (cpri > PRI_MIN_IDLE)
725                 goto sendipi;
726         /*
727          * If we're realtime or better and there is timeshare or worse running
728          * send an IPI.
729          */
730         if (pri < PRI_MAX_REALTIME && cpri > PRI_MAX_REALTIME)
731                 goto sendipi;
732         /*
733          * Otherwise only IPI if we exceed the threshold.
734          */
735         if (pri > ipi_thresh)
736                 return;
737 sendipi:
738         ctd->td_flags |= TDF_NEEDRESCHED;
739         if (cpri < PRI_MIN_IDLE) {
740                 if (ipi_ast)
741                         ipi_selected(1 << cpu, IPI_AST);
742                 else if (ipi_preempt)
743                         ipi_selected(1 << cpu, IPI_PREEMPT);
744         } else 
745                 ipi_selected(1 << cpu, IPI_PREEMPT);
746 }
747
748 static struct td_sched *
749 runq_steal(struct runq *rq)
750 {
751         struct rqhead *rqh;
752         struct rqbits *rqb;
753         struct td_sched *ts;
754         int word;
755         int bit;
756
757         mtx_assert(&sched_lock, MA_OWNED);
758         rqb = &rq->rq_status;
759         for (word = 0; word < RQB_LEN; word++) {
760                 if (rqb->rqb_bits[word] == 0)
761                         continue;
762                 for (bit = 0; bit < RQB_BPW; bit++) {
763                         if ((rqb->rqb_bits[word] & (1ul << bit)) == 0)
764                                 continue;
765                         rqh = &rq->rq_queues[bit + (word << RQB_L2BPW)];
766                         TAILQ_FOREACH(ts, rqh, ts_procq) {
767                                 if (THREAD_CAN_MIGRATE(ts->ts_thread))
768                                         return (ts);
769                         }
770                 }
771         }
772         return (NULL);
773 }
774
775 static struct td_sched *
776 tdq_steal(struct tdq *tdq, int stealidle)
777 {
778         struct td_sched *ts;
779
780         /*
781          * Steal from next first to try to get a non-interactive task that
782          * may not have run for a while.
783          * XXX Need to effect steal order for timeshare threads.
784          */
785         if ((ts = runq_steal(&tdq->tdq_realtime)) != NULL)
786                 return (ts);
787         if ((ts = runq_steal(&tdq->tdq_timeshare)) != NULL)
788                 return (ts);
789         if (stealidle)
790                 return (runq_steal(&tdq->tdq_idle));
791         return (NULL);
792 }
793
794 int
795 tdq_pickidle(struct tdq *tdq, struct td_sched *ts)
796 {
797         struct tdq_group *tdg;
798         int self;
799         int cpu;
800
801         self = PCPU_GET(cpuid);
802         if (smp_started == 0)
803                 return (self);
804         /*
805          * If the current CPU has idled, just run it here.
806          */
807         if ((tdq->tdq_group->tdg_idlemask & PCPU_GET(cpumask)) != 0)
808                 return (self);
809         /*
810          * Try the last group we ran on.
811          */
812         tdg = TDQ_CPU(ts->ts_cpu)->tdq_group;
813         cpu = ffs(tdg->tdg_idlemask);
814         if (cpu)
815                 return (cpu - 1);
816         /*
817          * Search for an idle group.
818          */
819         cpu = ffs(tdq_idle);
820         if (cpu) 
821                 return (cpu - 1);
822         /*
823          * XXX If there are no idle groups, check for an idle core.
824          */
825         /*
826          * No idle CPUs?
827          */
828         return (self);
829 }
830
831 static int
832 tdq_pickpri(struct tdq *tdq, struct td_sched *ts, int flags)
833 {
834         struct pcpu *pcpu;
835         int lowpri;
836         int lowcpu;
837         int lowload;
838         int load;
839         int self;
840         int pri;
841         int cpu;
842
843         self = PCPU_GET(cpuid);
844         if (smp_started == 0)
845                 return (self);
846
847         pri = ts->ts_thread->td_priority;
848         /*
849          * Regardless of affinity, if the last cpu is idle send it there.
850          */
851         pcpu = pcpu_find(ts->ts_cpu);
852         if (pcpu->pc_curthread->td_priority > PRI_MIN_IDLE) {
853                 CTR5(KTR_ULE,
854                     "ts_cpu %d idle, ltick %d ticks %d pri %d curthread %d",
855                     ts->ts_cpu, ts->ts_rltick, ticks, pri,
856                     pcpu->pc_curthread->td_priority);
857                 return (ts->ts_cpu);
858         }
859         /*
860          * If we have affinity, try to place it on the cpu we last ran on.
861          */
862         if (SCHED_AFFINITY(ts) && pcpu->pc_curthread->td_priority > pri) {
863                 CTR5(KTR_ULE,
864                     "affinity for %d, ltick %d ticks %d pri %d curthread %d",
865                     ts->ts_cpu, ts->ts_rltick, ticks, pri,
866                     pcpu->pc_curthread->td_priority);
867                 return (ts->ts_cpu);
868         }
869         /*
870          * Try ourself first; If we're running something lower priority this
871          * may have some locality with the waking thread and execute faster
872          * here.
873          */
874         if (tryself) {
875                 /*
876                  * If we're being awoken by an interrupt thread or the waker
877                  * is going right to sleep run here as well.
878                  */
879                 if ((TDQ_SELF()->tdq_load == 1) && (flags & SRQ_YIELDING ||
880                     curthread->td_pri_class == PRI_ITHD)) {
881                         CTR2(KTR_ULE, "tryself load %d flags %d",
882                             TDQ_SELF()->tdq_load, flags);
883                         return (self);
884                 }
885         }
886         /*
887          * Look for an idle group.
888          */
889         CTR1(KTR_ULE, "tdq_idle %X", tdq_idle);
890         cpu = ffs(tdq_idle);
891         if (cpu)
892                 return (cpu - 1);
893         if (tryselfidle && pri < curthread->td_priority) {
894                 CTR1(KTR_ULE, "tryself %d",
895                     curthread->td_priority);
896                 return (self);
897         }
898         /*
899          * Now search for the cpu running the lowest priority thread with
900          * the least load.
901          */
902         lowload = 0;
903         lowpri = lowcpu = 0;
904         for (cpu = 0; cpu <= mp_maxid; cpu++) {
905                 if (CPU_ABSENT(cpu))
906                         continue;
907                 pcpu = pcpu_find(cpu);
908                 pri = pcpu->pc_curthread->td_priority;
909                 CTR4(KTR_ULE,
910                     "cpu %d pri %d lowcpu %d lowpri %d",
911                     cpu, pri, lowcpu, lowpri);
912                 if (pri < lowpri)
913                         continue;
914                 load = TDQ_CPU(cpu)->tdq_load;
915                 if (lowpri && lowpri == pri && load > lowload)
916                         continue;
917                 lowpri = pri;
918                 lowcpu = cpu;
919                 lowload = load;
920         }
921
922         return (lowcpu);
923 }
924
925 #endif  /* SMP */
926
927 /*
928  * Pick the highest priority task we have and return it.
929  */
930
931 static struct td_sched *
932 tdq_choose(struct tdq *tdq)
933 {
934         struct td_sched *ts;
935
936         mtx_assert(&sched_lock, MA_OWNED);
937
938         ts = runq_choose(&tdq->tdq_realtime);
939         if (ts != NULL) {
940                 KASSERT(ts->ts_thread->td_priority <= PRI_MAX_REALTIME,
941                     ("tdq_choose: Invalid priority on realtime queue %d",
942                     ts->ts_thread->td_priority));
943                 return (ts);
944         }
945         ts = runq_choose_from(&tdq->tdq_timeshare, tdq->tdq_ridx);
946         if (ts != NULL) {
947                 KASSERT(ts->ts_thread->td_priority <= PRI_MAX_TIMESHARE &&
948                     ts->ts_thread->td_priority >= PRI_MIN_TIMESHARE,
949                     ("tdq_choose: Invalid priority on timeshare queue %d",
950                     ts->ts_thread->td_priority));
951                 return (ts);
952         }
953
954         ts = runq_choose(&tdq->tdq_idle);
955         if (ts != NULL) {
956                 KASSERT(ts->ts_thread->td_priority >= PRI_MIN_IDLE,
957                     ("tdq_choose: Invalid priority on idle queue %d",
958                     ts->ts_thread->td_priority));
959                 return (ts);
960         }
961
962         return (NULL);
963 }
964
965 static void
966 tdq_setup(struct tdq *tdq)
967 {
968         runq_init(&tdq->tdq_realtime);
969         runq_init(&tdq->tdq_timeshare);
970         runq_init(&tdq->tdq_idle);
971         tdq->tdq_load = 0;
972 }
973
974 static void
975 sched_setup(void *dummy)
976 {
977 #ifdef SMP
978         int i;
979 #endif
980
981         /*
982          * To avoid divide-by-zero, we set realstathz a dummy value
983          * in case which sched_clock() called before sched_initticks().
984          */
985         realstathz = hz;
986         sched_slice = (realstathz/10);  /* ~100ms */
987         tickincr = 1 << SCHED_TICK_SHIFT;
988
989 #ifdef SMP
990         balance_groups = 0;
991         /*
992          * Initialize the tdqs.
993          */
994         for (i = 0; i < MAXCPU; i++) {
995                 struct tdq *tdq;
996
997                 tdq = &tdq_cpu[i];
998                 tdq_setup(&tdq_cpu[i]);
999         }
1000         if (1) {
1001                 struct tdq_group *tdg;
1002                 struct tdq *tdq;
1003                 int cpus;
1004
1005                 for (cpus = 0, i = 0; i < MAXCPU; i++) {
1006                         if (CPU_ABSENT(i))
1007                                 continue;
1008                         tdq = &tdq_cpu[i];
1009                         tdg = &tdq_groups[cpus];
1010                         /*
1011                          * Setup a tdq group with one member.
1012                          */
1013                         tdq->tdq_transferable = 0;
1014                         tdq->tdq_group = tdg;
1015                         tdg->tdg_cpus = 1;
1016                         tdg->tdg_idlemask = 0;
1017                         tdg->tdg_cpumask = tdg->tdg_mask = 1 << i;
1018                         tdg->tdg_load = 0;
1019                         tdg->tdg_transferable = 0;
1020                         LIST_INIT(&tdg->tdg_members);
1021                         LIST_INSERT_HEAD(&tdg->tdg_members, tdq, tdq_siblings);
1022                         cpus++;
1023                 }
1024                 tdg_maxid = cpus - 1;
1025         } else {
1026                 struct tdq_group *tdg;
1027                 struct cpu_group *cg;
1028                 int j;
1029
1030                 for (i = 0; i < smp_topology->ct_count; i++) {
1031                         cg = &smp_topology->ct_group[i];
1032                         tdg = &tdq_groups[i];
1033                         /*
1034                          * Initialize the group.
1035                          */
1036                         tdg->tdg_idlemask = 0;
1037                         tdg->tdg_load = 0;
1038                         tdg->tdg_transferable = 0;
1039                         tdg->tdg_cpus = cg->cg_count;
1040                         tdg->tdg_cpumask = cg->cg_mask;
1041                         LIST_INIT(&tdg->tdg_members);
1042                         /*
1043                          * Find all of the group members and add them.
1044                          */
1045                         for (j = 0; j < MAXCPU; j++) {
1046                                 if ((cg->cg_mask & (1 << j)) != 0) {
1047                                         if (tdg->tdg_mask == 0)
1048                                                 tdg->tdg_mask = 1 << j;
1049                                         tdq_cpu[j].tdq_transferable = 0;
1050                                         tdq_cpu[j].tdq_group = tdg;
1051                                         LIST_INSERT_HEAD(&tdg->tdg_members,
1052                                             &tdq_cpu[j], tdq_siblings);
1053                                 }
1054                         }
1055                         if (tdg->tdg_cpus > 1)
1056                                 balance_groups = 1;
1057                 }
1058                 tdg_maxid = smp_topology->ct_count - 1;
1059         }
1060         /*
1061          * Stagger the group and global load balancer so they do not
1062          * interfere with each other.
1063          */
1064         bal_tick = ticks + hz;
1065         if (balance_groups)
1066                 gbal_tick = ticks + (hz / 2);
1067 #else
1068         tdq_setup(TDQ_SELF());
1069 #endif
1070         mtx_lock_spin(&sched_lock);
1071         tdq_load_add(TDQ_SELF(), &td_sched0);
1072         mtx_unlock_spin(&sched_lock);
1073 }
1074
1075 /* ARGSUSED */
1076 static void
1077 sched_initticks(void *dummy)
1078 {
1079         mtx_lock_spin(&sched_lock);
1080         realstathz = stathz ? stathz : hz;
1081         sched_slice = (realstathz/10);  /* ~100ms */
1082
1083         /*
1084          * tickincr is shifted out by 10 to avoid rounding errors due to
1085          * hz not being evenly divisible by stathz on all platforms.
1086          */
1087         tickincr = (hz << SCHED_TICK_SHIFT) / realstathz;
1088         /*
1089          * This does not work for values of stathz that are more than
1090          * 1 << SCHED_TICK_SHIFT * hz.  In practice this does not happen.
1091          */
1092         if (tickincr == 0)
1093                 tickincr = 1;
1094 #ifdef SMP
1095         affinity = SCHED_AFFINITY_DEFAULT;
1096 #endif
1097         mtx_unlock_spin(&sched_lock);
1098 }
1099
1100
1101 /*
1102  * Scale the scheduling priority according to the "interactivity" of this
1103  * process.
1104  */
1105 static void
1106 sched_priority(struct thread *td)
1107 {
1108         int score;
1109         int pri;
1110
1111         if (td->td_pri_class != PRI_TIMESHARE)
1112                 return;
1113         /*
1114          * If the score is interactive we place the thread in the realtime
1115          * queue with a priority that is less than kernel and interrupt
1116          * priorities.  These threads are not subject to nice restrictions.
1117          *
1118          * Scores greater than this are placed on the normal realtime queue
1119          * where the priority is partially decided by the most recent cpu
1120          * utilization and the rest is decided by nice value.
1121          */
1122         score = sched_interact_score(td);
1123         if (score < sched_interact) {
1124                 pri = PRI_MIN_REALTIME;
1125                 pri += ((PRI_MAX_REALTIME - PRI_MIN_REALTIME) / sched_interact)
1126                     * score;
1127                 KASSERT(pri >= PRI_MIN_REALTIME && pri <= PRI_MAX_REALTIME,
1128                     ("sched_priority: invalid interactive priority %d score %d",
1129                     pri, score));
1130         } else {
1131                 pri = SCHED_PRI_MIN;
1132                 if (td->td_sched->ts_ticks)
1133                         pri += SCHED_PRI_TICKS(td->td_sched);
1134                 pri += SCHED_PRI_NICE(td->td_proc->p_nice);
1135                 if (!(pri >= PRI_MIN_TIMESHARE && pri <= PRI_MAX_TIMESHARE)) {
1136                         static int once = 1;
1137                         if (once) {
1138                                 printf("sched_priority: invalid priority %d",
1139                                     pri);
1140                                 printf("nice %d, ticks %d ftick %d ltick %d tick pri %d\n",
1141                                     td->td_proc->p_nice,
1142                                     td->td_sched->ts_ticks,
1143                                     td->td_sched->ts_ftick,
1144                                     td->td_sched->ts_ltick,
1145                                     SCHED_PRI_TICKS(td->td_sched));
1146                                 once = 0;
1147                         }
1148                         pri = min(max(pri, PRI_MIN_TIMESHARE),
1149                             PRI_MAX_TIMESHARE);
1150                 }
1151         }
1152         sched_user_prio(td, pri);
1153
1154         return;
1155 }
1156
1157 /*
1158  * This routine enforces a maximum limit on the amount of scheduling history
1159  * kept.  It is called after either the slptime or runtime is adjusted.
1160  */
1161 static void
1162 sched_interact_update(struct thread *td)
1163 {
1164         struct td_sched *ts;
1165         u_int sum;
1166
1167         ts = td->td_sched;
1168         sum = ts->skg_runtime + ts->skg_slptime;
1169         if (sum < SCHED_SLP_RUN_MAX)
1170                 return;
1171         /*
1172          * This only happens from two places:
1173          * 1) We have added an unusual amount of run time from fork_exit.
1174          * 2) We have added an unusual amount of sleep time from sched_sleep().
1175          */
1176         if (sum > SCHED_SLP_RUN_MAX * 2) {
1177                 if (ts->skg_runtime > ts->skg_slptime) {
1178                         ts->skg_runtime = SCHED_SLP_RUN_MAX;
1179                         ts->skg_slptime = 1;
1180                 } else {
1181                         ts->skg_slptime = SCHED_SLP_RUN_MAX;
1182                         ts->skg_runtime = 1;
1183                 }
1184                 return;
1185         }
1186         /*
1187          * If we have exceeded by more than 1/5th then the algorithm below
1188          * will not bring us back into range.  Dividing by two here forces
1189          * us into the range of [4/5 * SCHED_INTERACT_MAX, SCHED_INTERACT_MAX]
1190          */
1191         if (sum > (SCHED_SLP_RUN_MAX / 5) * 6) {
1192                 ts->skg_runtime /= 2;
1193                 ts->skg_slptime /= 2;
1194                 return;
1195         }
1196         ts->skg_runtime = (ts->skg_runtime / 5) * 4;
1197         ts->skg_slptime = (ts->skg_slptime / 5) * 4;
1198 }
1199
1200 static void
1201 sched_interact_fork(struct thread *td)
1202 {
1203         int ratio;
1204         int sum;
1205
1206         sum = td->td_sched->skg_runtime + td->td_sched->skg_slptime;
1207         if (sum > SCHED_SLP_RUN_FORK) {
1208                 ratio = sum / SCHED_SLP_RUN_FORK;
1209                 td->td_sched->skg_runtime /= ratio;
1210                 td->td_sched->skg_slptime /= ratio;
1211         }
1212 }
1213
1214 static int
1215 sched_interact_score(struct thread *td)
1216 {
1217         int div;
1218
1219         if (td->td_sched->skg_runtime > td->td_sched->skg_slptime) {
1220                 div = max(1, td->td_sched->skg_runtime / SCHED_INTERACT_HALF);
1221                 return (SCHED_INTERACT_HALF +
1222                     (SCHED_INTERACT_HALF - (td->td_sched->skg_slptime / div)));
1223         }
1224         if (td->td_sched->skg_slptime > td->td_sched->skg_runtime) {
1225                 div = max(1, td->td_sched->skg_slptime / SCHED_INTERACT_HALF);
1226                 return (td->td_sched->skg_runtime / div);
1227         }
1228         /* runtime == slptime */
1229         if (td->td_sched->skg_runtime)
1230                 return (SCHED_INTERACT_HALF);
1231
1232         /*
1233          * This can happen if slptime and runtime are 0.
1234          */
1235         return (0);
1236
1237 }
1238
1239 /*
1240  * Called from proc0_init() to bootstrap the scheduler.
1241  */
1242 void
1243 schedinit(void)
1244 {
1245
1246         /*
1247          * Set up the scheduler specific parts of proc0.
1248          */
1249         proc0.p_sched = NULL; /* XXX */
1250         thread0.td_sched = &td_sched0;
1251         td_sched0.ts_ltick = ticks;
1252         td_sched0.ts_ftick = ticks;
1253         td_sched0.ts_thread = &thread0;
1254 }
1255
1256 /*
1257  * This is only somewhat accurate since given many processes of the same
1258  * priority they will switch when their slices run out, which will be
1259  * at most sched_slice stathz ticks.
1260  */
1261 int
1262 sched_rr_interval(void)
1263 {
1264
1265         /* Convert sched_slice to hz */
1266         return (hz/(realstathz/sched_slice));
1267 }
1268
1269 static void
1270 sched_pctcpu_update(struct td_sched *ts)
1271 {
1272
1273         if (ts->ts_ticks == 0)
1274                 return;
1275         if (ticks - (hz / 10) < ts->ts_ltick &&
1276             SCHED_TICK_TOTAL(ts) < SCHED_TICK_MAX)
1277                 return;
1278         /*
1279          * Adjust counters and watermark for pctcpu calc.
1280          */
1281         if (ts->ts_ltick > ticks - SCHED_TICK_TARG)
1282                 ts->ts_ticks = (ts->ts_ticks / (ticks - ts->ts_ftick)) *
1283                             SCHED_TICK_TARG;
1284         else
1285                 ts->ts_ticks = 0;
1286         ts->ts_ltick = ticks;
1287         ts->ts_ftick = ts->ts_ltick - SCHED_TICK_TARG;
1288 }
1289
1290 static void
1291 sched_thread_priority(struct thread *td, u_char prio)
1292 {
1293         struct td_sched *ts;
1294
1295         CTR6(KTR_SCHED, "sched_prio: %p(%s) prio %d newprio %d by %p(%s)",
1296             td, td->td_proc->p_comm, td->td_priority, prio, curthread,
1297             curthread->td_proc->p_comm);
1298         ts = td->td_sched;
1299         mtx_assert(&sched_lock, MA_OWNED);
1300         if (td->td_priority == prio)
1301                 return;
1302
1303         if (TD_ON_RUNQ(td) && prio < td->td_priority) {
1304                 /*
1305                  * If the priority has been elevated due to priority
1306                  * propagation, we may have to move ourselves to a new
1307                  * queue.  This could be optimized to not re-add in some
1308                  * cases.
1309                  */
1310                 sched_rem(td);
1311                 td->td_priority = prio;
1312                 sched_add(td, SRQ_BORROWING);
1313         } else
1314                 td->td_priority = prio;
1315 }
1316
1317 /*
1318  * Update a thread's priority when it is lent another thread's
1319  * priority.
1320  */
1321 void
1322 sched_lend_prio(struct thread *td, u_char prio)
1323 {
1324
1325         td->td_flags |= TDF_BORROWING;
1326         sched_thread_priority(td, prio);
1327 }
1328
1329 /*
1330  * Restore a thread's priority when priority propagation is
1331  * over.  The prio argument is the minimum priority the thread
1332  * needs to have to satisfy other possible priority lending
1333  * requests.  If the thread's regular priority is less
1334  * important than prio, the thread will keep a priority boost
1335  * of prio.
1336  */
1337 void
1338 sched_unlend_prio(struct thread *td, u_char prio)
1339 {
1340         u_char base_pri;
1341
1342         if (td->td_base_pri >= PRI_MIN_TIMESHARE &&
1343             td->td_base_pri <= PRI_MAX_TIMESHARE)
1344                 base_pri = td->td_user_pri;
1345         else
1346                 base_pri = td->td_base_pri;
1347         if (prio >= base_pri) {
1348                 td->td_flags &= ~TDF_BORROWING;
1349                 sched_thread_priority(td, base_pri);
1350         } else
1351                 sched_lend_prio(td, prio);
1352 }
1353
1354 void
1355 sched_prio(struct thread *td, u_char prio)
1356 {
1357         u_char oldprio;
1358
1359         /* First, update the base priority. */
1360         td->td_base_pri = prio;
1361
1362         /*
1363          * If the thread is borrowing another thread's priority, don't
1364          * ever lower the priority.
1365          */
1366         if (td->td_flags & TDF_BORROWING && td->td_priority < prio)
1367                 return;
1368
1369         /* Change the real priority. */
1370         oldprio = td->td_priority;
1371         sched_thread_priority(td, prio);
1372
1373         /*
1374          * If the thread is on a turnstile, then let the turnstile update
1375          * its state.
1376          */
1377         if (TD_ON_LOCK(td) && oldprio != prio)
1378                 turnstile_adjust(td, oldprio);
1379 }
1380
1381 void
1382 sched_user_prio(struct thread *td, u_char prio)
1383 {
1384         u_char oldprio;
1385
1386         td->td_base_user_pri = prio;
1387         if (td->td_flags & TDF_UBORROWING && td->td_user_pri <= prio)
1388                 return;
1389         oldprio = td->td_user_pri;
1390         td->td_user_pri = prio;
1391
1392         if (TD_ON_UPILOCK(td) && oldprio != prio)
1393                 umtx_pi_adjust(td, oldprio);
1394 }
1395
1396 void
1397 sched_lend_user_prio(struct thread *td, u_char prio)
1398 {
1399         u_char oldprio;
1400
1401         td->td_flags |= TDF_UBORROWING;
1402
1403         oldprio = td->td_user_pri;
1404         td->td_user_pri = prio;
1405
1406         if (TD_ON_UPILOCK(td) && oldprio != prio)
1407                 umtx_pi_adjust(td, oldprio);
1408 }
1409
1410 void
1411 sched_unlend_user_prio(struct thread *td, u_char prio)
1412 {
1413         u_char base_pri;
1414
1415         base_pri = td->td_base_user_pri;
1416         if (prio >= base_pri) {
1417                 td->td_flags &= ~TDF_UBORROWING;
1418                 sched_user_prio(td, base_pri);
1419         } else
1420                 sched_lend_user_prio(td, prio);
1421 }
1422
1423 void
1424 sched_switch(struct thread *td, struct thread *newtd, int flags)
1425 {
1426         struct tdq *tdq;
1427         struct td_sched *ts;
1428         int preempt;
1429
1430         mtx_assert(&sched_lock, MA_OWNED);
1431
1432         preempt = flags & SW_PREEMPT;
1433         tdq = TDQ_SELF();
1434         ts = td->td_sched;
1435         td->td_lastcpu = td->td_oncpu;
1436         td->td_oncpu = NOCPU;
1437         td->td_flags &= ~TDF_NEEDRESCHED;
1438         td->td_owepreempt = 0;
1439         /*
1440          * If the thread has been assigned it may be in the process of switching
1441          * to the new cpu.  This is the case in sched_bind().
1442          */
1443         if (TD_IS_IDLETHREAD(td)) {
1444                 TD_SET_CAN_RUN(td);
1445         } else {
1446                 tdq_load_rem(tdq, ts);
1447                 if (TD_IS_RUNNING(td)) {
1448                         /*
1449                          * Don't allow the thread to migrate
1450                          * from a preemption.
1451                          */
1452                         if (preempt)
1453                                 sched_pin_td(td);
1454                         sched_add(td, preempt ?
1455                             SRQ_OURSELF|SRQ_YIELDING|SRQ_PREEMPTED :
1456                             SRQ_OURSELF|SRQ_YIELDING);
1457                         if (preempt)
1458                                 sched_unpin_td(td);
1459                 }
1460         }
1461         if (newtd != NULL) {
1462                 /*
1463                  * If we bring in a thread account for it as if it had been
1464                  * added to the run queue and then chosen.
1465                  */
1466                 TD_SET_RUNNING(newtd);
1467                 tdq_load_add(TDQ_SELF(), newtd->td_sched);
1468         } else
1469                 newtd = choosethread();
1470         if (td != newtd) {
1471 #ifdef  HWPMC_HOOKS
1472                 if (PMC_PROC_IS_USING_PMCS(td->td_proc))
1473                         PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_OUT);
1474 #endif
1475
1476                 cpu_switch(td, newtd);
1477 #ifdef  HWPMC_HOOKS
1478                 if (PMC_PROC_IS_USING_PMCS(td->td_proc))
1479                         PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_IN);
1480 #endif
1481         }
1482         sched_lock.mtx_lock = (uintptr_t)td;
1483         td->td_oncpu = PCPU_GET(cpuid);
1484 }
1485
1486 void
1487 sched_nice(struct proc *p, int nice)
1488 {
1489         struct thread *td;
1490
1491         PROC_LOCK_ASSERT(p, MA_OWNED);
1492         mtx_assert(&sched_lock, MA_OWNED);
1493
1494         p->p_nice = nice;
1495         FOREACH_THREAD_IN_PROC(p, td) {
1496                 sched_priority(td);
1497                 sched_prio(td, td->td_base_user_pri);
1498         }
1499 }
1500
1501 void
1502 sched_sleep(struct thread *td)
1503 {
1504
1505         mtx_assert(&sched_lock, MA_OWNED);
1506
1507         td->td_sched->ts_slptime = ticks;
1508 }
1509
1510 void
1511 sched_wakeup(struct thread *td)
1512 {
1513         struct td_sched *ts;
1514         int slptime;
1515
1516         mtx_assert(&sched_lock, MA_OWNED);
1517         ts = td->td_sched;
1518         /*
1519          * If we slept for more than a tick update our interactivity and
1520          * priority.
1521          */
1522         slptime = ts->ts_slptime;
1523         ts->ts_slptime = 0;
1524         if (slptime && slptime != ticks) {
1525                 u_int hzticks;
1526
1527                 hzticks = (ticks - slptime) << SCHED_TICK_SHIFT;
1528                 ts->skg_slptime += hzticks;
1529                 sched_interact_update(td);
1530                 sched_pctcpu_update(ts);
1531                 sched_priority(td);
1532         }
1533         /* Reset the slice value after we sleep. */
1534         ts->ts_slice = sched_slice;
1535         sched_add(td, SRQ_BORING);
1536 }
1537
1538 /*
1539  * Penalize the parent for creating a new child and initialize the child's
1540  * priority.
1541  */
1542 void
1543 sched_fork(struct thread *td, struct thread *child)
1544 {
1545         mtx_assert(&sched_lock, MA_OWNED);
1546         sched_fork_thread(td, child);
1547         /*
1548          * Penalize the parent and child for forking.
1549          */
1550         sched_interact_fork(child);
1551         sched_priority(child);
1552         td->td_sched->skg_runtime += tickincr;
1553         sched_interact_update(td);
1554         sched_priority(td);
1555 }
1556
1557 void
1558 sched_fork_thread(struct thread *td, struct thread *child)
1559 {
1560         struct td_sched *ts;
1561         struct td_sched *ts2;
1562
1563         /*
1564          * Initialize child.
1565          */
1566         sched_newthread(child);
1567         ts = td->td_sched;
1568         ts2 = child->td_sched;
1569         ts2->ts_cpu = ts->ts_cpu;
1570         ts2->ts_runq = NULL;
1571         /*
1572          * Grab our parents cpu estimation information and priority.
1573          */
1574         ts2->ts_ticks = ts->ts_ticks;
1575         ts2->ts_ltick = ts->ts_ltick;
1576         ts2->ts_ftick = ts->ts_ftick;
1577         child->td_user_pri = td->td_user_pri;
1578         child->td_base_user_pri = td->td_base_user_pri;
1579         /*
1580          * And update interactivity score.
1581          */
1582         ts2->skg_slptime = ts->skg_slptime;
1583         ts2->skg_runtime = ts->skg_runtime;
1584         ts2->ts_slice = 1;      /* Attempt to quickly learn interactivity. */
1585 }
1586
1587 void
1588 sched_class(struct thread *td, int class)
1589 {
1590
1591         mtx_assert(&sched_lock, MA_OWNED);
1592         if (td->td_pri_class == class)
1593                 return;
1594
1595 #ifdef SMP
1596         /*
1597          * On SMP if we're on the RUNQ we must adjust the transferable
1598          * count because could be changing to or from an interrupt
1599          * class.
1600          */
1601         if (TD_ON_RUNQ(td)) {
1602                 struct tdq *tdq;
1603
1604                 tdq = TDQ_CPU(td->td_sched->ts_cpu);
1605                 if (THREAD_CAN_MIGRATE(td)) {
1606                         tdq->tdq_transferable--;
1607                         tdq->tdq_group->tdg_transferable--;
1608                 }
1609                 td->td_pri_class = class;
1610                 if (THREAD_CAN_MIGRATE(td)) {
1611                         tdq->tdq_transferable++;
1612                         tdq->tdq_group->tdg_transferable++;
1613                 }
1614         }
1615 #endif
1616         td->td_pri_class = class;
1617 }
1618
1619 /*
1620  * Return some of the child's priority and interactivity to the parent.
1621  */
1622 void
1623 sched_exit(struct proc *p, struct thread *child)
1624 {
1625         struct thread *td;
1626         
1627         CTR3(KTR_SCHED, "sched_exit: %p(%s) prio %d",
1628             child, child->td_proc->p_comm, child->td_priority);
1629
1630         td = FIRST_THREAD_IN_PROC(p);
1631         sched_exit_thread(td, child);
1632 }
1633
1634 void
1635 sched_exit_thread(struct thread *td, struct thread *child)
1636 {
1637
1638         CTR3(KTR_SCHED, "sched_exit_thread: %p(%s) prio %d",
1639             child, child->td_proc->p_comm, child->td_priority);
1640
1641         tdq_load_rem(TDQ_CPU(child->td_sched->ts_cpu), child->td_sched);
1642 #ifdef KSE
1643         /*
1644          * KSE forks and exits so often that this penalty causes short-lived
1645          * threads to always be non-interactive.  This causes mozilla to
1646          * crawl under load.
1647          */
1648         if ((td->td_pflags & TDP_SA) && td->td_proc == child->td_proc)
1649                 return;
1650 #endif
1651         /*
1652          * Give the child's runtime to the parent without returning the
1653          * sleep time as a penalty to the parent.  This causes shells that
1654          * launch expensive things to mark their children as expensive.
1655          */
1656         td->td_sched->skg_runtime += child->td_sched->skg_runtime;
1657         sched_interact_update(td);
1658         sched_priority(td);
1659 }
1660
1661 void
1662 sched_userret(struct thread *td)
1663 {
1664         /*
1665          * XXX we cheat slightly on the locking here to avoid locking in  
1666          * the usual case.  Setting td_priority here is essentially an
1667          * incomplete workaround for not setting it properly elsewhere.
1668          * Now that some interrupt handlers are threads, not setting it
1669          * properly elsewhere can clobber it in the window between setting
1670          * it here and returning to user mode, so don't waste time setting
1671          * it perfectly here.
1672          */
1673         KASSERT((td->td_flags & TDF_BORROWING) == 0,
1674             ("thread with borrowed priority returning to userland"));
1675         if (td->td_priority != td->td_user_pri) {
1676                 mtx_lock_spin(&sched_lock);
1677                 td->td_priority = td->td_user_pri;
1678                 td->td_base_pri = td->td_user_pri;
1679                 mtx_unlock_spin(&sched_lock);
1680         }
1681 }
1682
1683 void
1684 sched_clock(struct thread *td)
1685 {
1686         struct tdq *tdq;
1687         struct td_sched *ts;
1688
1689         mtx_assert(&sched_lock, MA_OWNED);
1690 #ifdef SMP
1691         sched_smp_tick(td);
1692 #endif
1693         tdq = TDQ_SELF();
1694         /*
1695          * Advance the insert index once for each tick to ensure that all
1696          * threads get a chance to run.
1697          */
1698         if (tdq->tdq_idx == tdq->tdq_ridx) {
1699                 tdq->tdq_idx = (tdq->tdq_idx + 1) % RQ_NQS;
1700                 if (TAILQ_EMPTY(&tdq->tdq_timeshare.rq_queues[tdq->tdq_ridx]))
1701                         tdq->tdq_ridx = tdq->tdq_idx;
1702         }
1703         ts = td->td_sched;
1704         /*
1705          * We only do slicing code for TIMESHARE threads.
1706          */
1707         if (td->td_pri_class != PRI_TIMESHARE)
1708                 return;
1709         /*
1710          * We used a tick; charge it to the thread so that we can compute our
1711          * interactivity.
1712          */
1713         td->td_sched->skg_runtime += tickincr;
1714         sched_interact_update(td);
1715         /*
1716          * We used up one time slice.
1717          */
1718         if (--ts->ts_slice > 0)
1719                 return;
1720         /*
1721          * We're out of time, recompute priorities and requeue.
1722          */
1723         sched_priority(td);
1724         td->td_flags |= TDF_NEEDRESCHED;
1725 }
1726
1727 int
1728 sched_runnable(void)
1729 {
1730         struct tdq *tdq;
1731         int load;
1732
1733         load = 1;
1734
1735         tdq = TDQ_SELF();
1736 #ifdef SMP
1737         if (tdq_busy)
1738                 goto out;
1739 #endif
1740         if ((curthread->td_flags & TDF_IDLETD) != 0) {
1741                 if (tdq->tdq_load > 0)
1742                         goto out;
1743         } else
1744                 if (tdq->tdq_load - 1 > 0)
1745                         goto out;
1746         load = 0;
1747 out:
1748         return (load);
1749 }
1750
1751 struct thread *
1752 sched_choose(void)
1753 {
1754         struct tdq *tdq;
1755         struct td_sched *ts;
1756
1757         mtx_assert(&sched_lock, MA_OWNED);
1758         tdq = TDQ_SELF();
1759 #ifdef SMP
1760 restart:
1761 #endif
1762         ts = tdq_choose(tdq);
1763         if (ts) {
1764 #ifdef SMP
1765                 if (ts->ts_thread->td_priority > PRI_MIN_IDLE)
1766                         if (tdq_idled(tdq) == 0)
1767                                 goto restart;
1768 #endif
1769                 tdq_runq_rem(tdq, ts);
1770                 return (ts->ts_thread);
1771         }
1772 #ifdef SMP
1773         if (tdq_idled(tdq) == 0)
1774                 goto restart;
1775 #endif
1776         return (PCPU_GET(idlethread));
1777 }
1778
1779 static int
1780 sched_preempt(struct thread *td)
1781 {
1782         struct thread *ctd;
1783         int cpri;
1784         int pri;
1785
1786         ctd = curthread;
1787         pri = td->td_priority;
1788         cpri = ctd->td_priority;
1789         if (panicstr != NULL || pri >= cpri || cold || TD_IS_INHIBITED(ctd))
1790                 return (0);
1791         /*
1792          * Always preempt IDLE threads.  Otherwise only if the preempting
1793          * thread is an ithread.
1794          */
1795         if (pri > PRI_MAX_ITHD && cpri < PRI_MIN_IDLE)
1796                 return (0);
1797         if (ctd->td_critnest > 1) {
1798                 CTR1(KTR_PROC, "sched_preempt: in critical section %d",
1799                     ctd->td_critnest);
1800                 ctd->td_owepreempt = 1;
1801                 return (0);
1802         }
1803         /*
1804          * Thread is runnable but not yet put on system run queue.
1805          */
1806         MPASS(TD_ON_RUNQ(td));
1807         TD_SET_RUNNING(td);
1808         CTR3(KTR_PROC, "preempting to thread %p (pid %d, %s)\n", td,
1809             td->td_proc->p_pid, td->td_proc->p_comm);
1810         mi_switch(SW_INVOL|SW_PREEMPT, td);
1811         return (1);
1812 }
1813
1814 void
1815 sched_add(struct thread *td, int flags)
1816 {
1817         struct tdq *tdq;
1818         struct td_sched *ts;
1819         int preemptive;
1820         int class;
1821 #ifdef SMP
1822         int cpuid;
1823         int cpumask;
1824 #endif
1825         ts = td->td_sched;
1826
1827         mtx_assert(&sched_lock, MA_OWNED);
1828         CTR5(KTR_SCHED, "sched_add: %p(%s) prio %d by %p(%s)",
1829             td, td->td_proc->p_comm, td->td_priority, curthread,
1830             curthread->td_proc->p_comm);
1831         KASSERT((td->td_inhibitors == 0),
1832             ("sched_add: trying to run inhibited thread"));
1833         KASSERT((TD_CAN_RUN(td) || TD_IS_RUNNING(td)),
1834             ("sched_add: bad thread state"));
1835         KASSERT(td->td_proc->p_sflag & PS_INMEM,
1836             ("sched_add: process swapped out"));
1837         KASSERT(ts->ts_runq == NULL,
1838             ("sched_add: thread %p is still assigned to a run queue", td));
1839         TD_SET_RUNQ(td);
1840         tdq = TDQ_SELF();
1841         class = PRI_BASE(td->td_pri_class);
1842         preemptive = !(flags & SRQ_YIELDING);
1843         /*
1844          * Recalculate the priority before we select the target cpu or
1845          * run-queue.
1846          */
1847         if (class == PRI_TIMESHARE)
1848                 sched_priority(td);
1849         if (ts->ts_slice == 0)
1850                 ts->ts_slice = sched_slice;
1851 #ifdef SMP
1852         cpuid = PCPU_GET(cpuid);
1853         /*
1854          * Pick the destination cpu and if it isn't ours transfer to the
1855          * target cpu.
1856          */
1857         if (THREAD_CAN_MIGRATE(td)) {
1858                 if (td->td_priority <= PRI_MAX_ITHD) {
1859                         CTR2(KTR_ULE, "ithd %d < %d",
1860                             td->td_priority, PRI_MAX_ITHD);
1861                         ts->ts_cpu = cpuid;
1862                 } else if (pick_pri)
1863                         ts->ts_cpu = tdq_pickpri(tdq, ts, flags);
1864                 else
1865                         ts->ts_cpu = tdq_pickidle(tdq, ts);
1866         } else
1867                 CTR1(KTR_ULE, "pinned %d", td->td_pinned);
1868         if (ts->ts_cpu != cpuid)
1869                 preemptive = 0;
1870         tdq = TDQ_CPU(ts->ts_cpu);
1871         cpumask = 1 << ts->ts_cpu;
1872         /*
1873          * If we had been idle, clear our bit in the group and potentially
1874          * the global bitmap.
1875          */
1876         if ((class != PRI_IDLE && class != PRI_ITHD) &&
1877             (tdq->tdq_group->tdg_idlemask & cpumask) != 0) {
1878                 /*
1879                  * Check to see if our group is unidling, and if so, remove it
1880                  * from the global idle mask.
1881                  */
1882                 if (tdq->tdq_group->tdg_idlemask ==
1883                     tdq->tdq_group->tdg_cpumask)
1884                         atomic_clear_int(&tdq_idle, tdq->tdq_group->tdg_mask);
1885                 /*
1886                  * Now remove ourselves from the group specific idle mask.
1887                  */
1888                 tdq->tdq_group->tdg_idlemask &= ~cpumask;
1889         }
1890 #endif
1891         /*
1892          * Pick the run queue based on priority.
1893          */
1894         if (td->td_priority <= PRI_MAX_REALTIME)
1895                 ts->ts_runq = &tdq->tdq_realtime;
1896         else if (td->td_priority <= PRI_MAX_TIMESHARE)
1897                 ts->ts_runq = &tdq->tdq_timeshare;
1898         else
1899                 ts->ts_runq = &tdq->tdq_idle;
1900         if (preemptive && sched_preempt(td))
1901                 return;
1902         tdq_runq_add(tdq, ts, flags);
1903         tdq_load_add(tdq, ts);
1904 #ifdef SMP
1905         if (ts->ts_cpu != cpuid) {
1906                 tdq_notify(ts);
1907                 return;
1908         }
1909 #endif
1910         if (td->td_priority < curthread->td_priority)
1911                 curthread->td_flags |= TDF_NEEDRESCHED;
1912 }
1913
1914 void
1915 sched_rem(struct thread *td)
1916 {
1917         struct tdq *tdq;
1918         struct td_sched *ts;
1919
1920         CTR5(KTR_SCHED, "sched_rem: %p(%s) prio %d by %p(%s)",
1921             td, td->td_proc->p_comm, td->td_priority, curthread,
1922             curthread->td_proc->p_comm);
1923         mtx_assert(&sched_lock, MA_OWNED);
1924         ts = td->td_sched;
1925         KASSERT(TD_ON_RUNQ(td),
1926             ("sched_rem: thread not on run queue"));
1927
1928         tdq = TDQ_CPU(ts->ts_cpu);
1929         tdq_runq_rem(tdq, ts);
1930         tdq_load_rem(tdq, ts);
1931         TD_SET_CAN_RUN(td);
1932 }
1933
1934 fixpt_t
1935 sched_pctcpu(struct thread *td)
1936 {
1937         fixpt_t pctcpu;
1938         struct td_sched *ts;
1939
1940         pctcpu = 0;
1941         ts = td->td_sched;
1942         if (ts == NULL)
1943                 return (0);
1944
1945         mtx_lock_spin(&sched_lock);
1946         if (ts->ts_ticks) {
1947                 int rtick;
1948
1949                 sched_pctcpu_update(ts);
1950                 /* How many rtick per second ? */
1951                 rtick = min(SCHED_TICK_HZ(ts) / SCHED_TICK_SECS, hz);
1952                 pctcpu = (FSCALE * ((FSCALE * rtick)/hz)) >> FSHIFT;
1953         }
1954         td->td_proc->p_swtime = ts->ts_ltick - ts->ts_ftick;
1955         mtx_unlock_spin(&sched_lock);
1956
1957         return (pctcpu);
1958 }
1959
1960 void
1961 sched_bind(struct thread *td, int cpu)
1962 {
1963         struct td_sched *ts;
1964
1965         mtx_assert(&sched_lock, MA_OWNED);
1966         ts = td->td_sched;
1967         if (ts->ts_flags & TSF_BOUND)
1968                 sched_unbind(td);
1969         ts->ts_flags |= TSF_BOUND;
1970 #ifdef SMP
1971         sched_pin();
1972         if (PCPU_GET(cpuid) == cpu)
1973                 return;
1974         ts->ts_cpu = cpu;
1975         /* When we return from mi_switch we'll be on the correct cpu. */
1976         mi_switch(SW_VOL, NULL);
1977 #endif
1978 }
1979
1980 void
1981 sched_unbind(struct thread *td)
1982 {
1983         struct td_sched *ts;
1984
1985         mtx_assert(&sched_lock, MA_OWNED);
1986         ts = td->td_sched;
1987         if ((ts->ts_flags & TSF_BOUND) == 0)
1988                 return;
1989         ts->ts_flags &= ~TSF_BOUND;
1990 #ifdef SMP
1991         sched_unpin();
1992 #endif
1993 }
1994
1995 int
1996 sched_is_bound(struct thread *td)
1997 {
1998         mtx_assert(&sched_lock, MA_OWNED);
1999         return (td->td_sched->ts_flags & TSF_BOUND);
2000 }
2001
2002 void
2003 sched_relinquish(struct thread *td)
2004 {
2005         mtx_lock_spin(&sched_lock);
2006         if (td->td_pri_class == PRI_TIMESHARE)
2007                 sched_prio(td, PRI_MAX_TIMESHARE);
2008         mi_switch(SW_VOL, NULL);
2009         mtx_unlock_spin(&sched_lock);
2010 }
2011
2012 int
2013 sched_load(void)
2014 {
2015 #ifdef SMP
2016         int total;
2017         int i;
2018
2019         total = 0;
2020         for (i = 0; i <= tdg_maxid; i++)
2021                 total += TDQ_GROUP(i)->tdg_load;
2022         return (total);
2023 #else
2024         return (TDQ_SELF()->tdq_sysload);
2025 #endif
2026 }
2027
2028 int
2029 sched_sizeof_proc(void)
2030 {
2031         return (sizeof(struct proc));
2032 }
2033
2034 int
2035 sched_sizeof_thread(void)
2036 {
2037         return (sizeof(struct thread) + sizeof(struct td_sched));
2038 }
2039
2040 void
2041 sched_tick(void)
2042 {
2043         struct td_sched *ts;
2044
2045         ts = curthread->td_sched;
2046         /* Adjust ticks for pctcpu */
2047         ts->ts_ticks += 1 << SCHED_TICK_SHIFT;
2048         ts->ts_ltick = ticks;
2049         /*
2050          * Update if we've exceeded our desired tick threshhold by over one
2051          * second.
2052          */
2053         if (ts->ts_ftick + SCHED_TICK_MAX < ts->ts_ltick)
2054                 sched_pctcpu_update(ts);
2055 }
2056
2057 /*
2058  * The actual idle process.
2059  */
2060 void
2061 sched_idletd(void *dummy)
2062 {
2063         struct proc *p;
2064         struct thread *td;
2065
2066         td = curthread;
2067         p = td->td_proc;
2068         mtx_assert(&Giant, MA_NOTOWNED);
2069         /* ULE Relies on preemption for idle interruption. */
2070         for (;;)
2071                 cpu_idle();
2072 }
2073
2074 static SYSCTL_NODE(_kern, OID_AUTO, sched, CTLFLAG_RW, 0, "Scheduler");
2075 SYSCTL_STRING(_kern_sched, OID_AUTO, name, CTLFLAG_RD, "ule", 0,
2076     "Scheduler name");
2077 SYSCTL_INT(_kern_sched, OID_AUTO, slice, CTLFLAG_RW, &sched_slice, 0, "");
2078 SYSCTL_INT(_kern_sched, OID_AUTO, interact, CTLFLAG_RW, &sched_interact, 0, "");
2079 SYSCTL_INT(_kern_sched, OID_AUTO, tickincr, CTLFLAG_RD, &tickincr, 0, "");
2080 SYSCTL_INT(_kern_sched, OID_AUTO, realstathz, CTLFLAG_RD, &realstathz, 0, "");
2081 #ifdef SMP
2082 SYSCTL_INT(_kern_sched, OID_AUTO, pick_pri, CTLFLAG_RW, &pick_pri, 0, "");
2083 SYSCTL_INT(_kern_sched, OID_AUTO, pick_pri_affinity, CTLFLAG_RW,
2084     &affinity, 0, "");
2085 SYSCTL_INT(_kern_sched, OID_AUTO, pick_pri_tryself, CTLFLAG_RW,
2086     &tryself, 0, "");
2087 SYSCTL_INT(_kern_sched, OID_AUTO, pick_pri_tryselfidle, CTLFLAG_RW,
2088     &tryselfidle, 0, "");
2089 SYSCTL_INT(_kern_sched, OID_AUTO, balance, CTLFLAG_RW, &rebalance, 0, "");
2090 SYSCTL_INT(_kern_sched, OID_AUTO, ipi_preempt, CTLFLAG_RW, &ipi_preempt, 0, "");
2091 SYSCTL_INT(_kern_sched, OID_AUTO, ipi_ast, CTLFLAG_RW, &ipi_ast, 0, "");
2092 SYSCTL_INT(_kern_sched, OID_AUTO, ipi_thresh, CTLFLAG_RW, &ipi_thresh, 0, "");
2093 SYSCTL_INT(_kern_sched, OID_AUTO, steal_htt, CTLFLAG_RW, &steal_htt, 0, "");
2094 SYSCTL_INT(_kern_sched, OID_AUTO, steal_busy, CTLFLAG_RW, &steal_busy, 0, "");
2095 SYSCTL_INT(_kern_sched, OID_AUTO, busy_thresh, CTLFLAG_RW, &busy_thresh, 0, "");
2096 #endif
2097
2098 /* ps compat */
2099 static fixpt_t  ccpu = 0.95122942450071400909 * FSCALE; /* exp(-1/20) */
2100 SYSCTL_INT(_kern, OID_AUTO, ccpu, CTLFLAG_RD, &ccpu, 0, "");
2101
2102
2103 #define KERN_SWITCH_INCLUDE 1
2104 #include "kern/kern_switch.c"