usched: Implement LWP lazy migration support.
[dragonfly.git] / sys / kern / usched_bsd4.c
bloba9b03cd145c42facab35d0a6a9ee67877fedfdc3
1 /*
2 * Copyright (c) 2012 The DragonFly Project. All rights reserved.
3 * Copyright (c) 1999 Peter Wemm <peter@FreeBSD.org>. All rights reserved.
5 * This code is derived from software contributed to The DragonFly Project
6 * by Matthew Dillon <dillon@backplane.com>,
7 * by Mihai Carabas <mihai.carabas@gmail.com>
8 * and many others.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/lock.h>
36 #include <sys/queue.h>
37 #include <sys/proc.h>
38 #include <sys/rtprio.h>
39 #include <sys/uio.h>
40 #include <sys/sysctl.h>
41 #include <sys/resourcevar.h>
42 #include <sys/spinlock.h>
43 #include <sys/cpu_topology.h>
44 #include <sys/thread2.h>
45 #include <sys/spinlock2.h>
46 #include <sys/mplock2.h>
48 #include <sys/ktr.h>
50 #include <machine/cpu.h>
51 #include <machine/smp.h>
54 * Priorities. Note that with 32 run queues per scheduler each queue
55 * represents four priority levels.
58 #define MAXPRI 128
59 #define PRIMASK (MAXPRI - 1)
60 #define PRIBASE_REALTIME 0
61 #define PRIBASE_NORMAL MAXPRI
62 #define PRIBASE_IDLE (MAXPRI * 2)
63 #define PRIBASE_THREAD (MAXPRI * 3)
64 #define PRIBASE_NULL (MAXPRI * 4)
66 #define NQS 32 /* 32 run queues. */
67 #define PPQ (MAXPRI / NQS) /* priorities per queue */
68 #define PPQMASK (PPQ - 1)
71 * NICEPPQ - number of nice units per priority queue
73 * ESTCPUPPQ - number of estcpu units per priority queue
74 * ESTCPUMAX - number of estcpu units
76 #define NICEPPQ 2
77 #define ESTCPUPPQ 512
78 #define ESTCPUMAX (ESTCPUPPQ * NQS)
79 #define BATCHMAX (ESTCPUFREQ * 30)
80 #define PRIO_RANGE (PRIO_MAX - PRIO_MIN + 1)
82 #define ESTCPULIM(v) min((v), ESTCPUMAX)
84 TAILQ_HEAD(rq, lwp);
86 #define lwp_priority lwp_usdata.bsd4.priority
87 #define lwp_rqindex lwp_usdata.bsd4.rqindex
88 #define lwp_estcpu lwp_usdata.bsd4.estcpu
89 #define lwp_batch lwp_usdata.bsd4.batch
90 #define lwp_rqtype lwp_usdata.bsd4.rqtype
92 static void bsd4_acquire_curproc(struct lwp *lp);
93 static void bsd4_release_curproc(struct lwp *lp);
94 static void bsd4_select_curproc(globaldata_t gd);
95 static void bsd4_setrunqueue(struct lwp *lp);
96 static void bsd4_schedulerclock(struct lwp *lp, sysclock_t period,
97 sysclock_t cpstamp);
98 static void bsd4_recalculate_estcpu(struct lwp *lp);
99 static void bsd4_resetpriority(struct lwp *lp);
100 static void bsd4_forking(struct lwp *plp, struct lwp *lp);
101 static void bsd4_exiting(struct lwp *lp, struct proc *);
102 static void bsd4_uload_update(struct lwp *lp);
103 static void bsd4_yield(struct lwp *lp);
104 static void bsd4_need_user_resched_remote(void *dummy);
105 static int bsd4_batchy_looser_pri_test(struct lwp* lp);
106 static struct lwp *bsd4_chooseproc_locked_cache_coherent(struct lwp *chklp);
107 static void bsd4_kick_helper(struct lwp *lp);
108 static struct lwp *bsd4_chooseproc_locked(struct lwp *chklp);
109 static void bsd4_remrunqueue_locked(struct lwp *lp);
110 static void bsd4_setrunqueue_locked(struct lwp *lp);
111 static void bsd4_changedcpu(struct lwp *lp);
113 struct usched usched_bsd4 = {
114 { NULL },
115 "bsd4", "Original DragonFly Scheduler",
116 NULL, /* default registration */
117 NULL, /* default deregistration */
118 bsd4_acquire_curproc,
119 bsd4_release_curproc,
120 bsd4_setrunqueue,
121 bsd4_schedulerclock,
122 bsd4_recalculate_estcpu,
123 bsd4_resetpriority,
124 bsd4_forking,
125 bsd4_exiting,
126 bsd4_uload_update,
127 NULL, /* setcpumask not supported */
128 bsd4_yield,
129 bsd4_changedcpu
132 struct usched_bsd4_pcpu {
133 struct thread *helper_thread;
134 short rrcount;
135 short upri;
136 struct lwp *uschedcp;
137 struct lwp *old_uschedcp;
138 cpu_node_t *cpunode;
141 typedef struct usched_bsd4_pcpu *bsd4_pcpu_t;
144 * We have NQS (32) run queues per scheduling class. For the normal
145 * class, there are 128 priorities scaled onto these 32 queues. New
146 * processes are added to the last entry in each queue, and processes
147 * are selected for running by taking them from the head and maintaining
148 * a simple FIFO arrangement. Realtime and Idle priority processes have
149 * and explicit 0-31 priority which maps directly onto their class queue
150 * index. When a queue has something in it, the corresponding bit is
151 * set in the queuebits variable, allowing a single read to determine
152 * the state of all 32 queues and then a ffs() to find the first busy
153 * queue.
155 static struct rq bsd4_queues[NQS];
156 static struct rq bsd4_rtqueues[NQS];
157 static struct rq bsd4_idqueues[NQS];
158 static u_int32_t bsd4_queuebits;
159 static u_int32_t bsd4_rtqueuebits;
160 static u_int32_t bsd4_idqueuebits;
161 /* currently running a user process */
162 static cpumask_t bsd4_curprocmask = CPUMASK_INITIALIZER_ALLONES;
163 /* ready to accept a user process */
164 static cpumask_t bsd4_rdyprocmask;
165 static int bsd4_runqcount;
166 static volatile int bsd4_scancpu;
167 static struct spinlock bsd4_spin;
168 static struct usched_bsd4_pcpu bsd4_pcpu[MAXCPU];
169 static struct sysctl_ctx_list usched_bsd4_sysctl_ctx;
170 static struct sysctl_oid *usched_bsd4_sysctl_tree;
172 /* Debug info exposed through debug.* sysctl */
174 SYSCTL_INT(_debug, OID_AUTO, bsd4_runqcount, CTLFLAG_RD,
175 &bsd4_runqcount, 0,
176 "Number of run queues");
178 static int usched_bsd4_debug = -1;
179 SYSCTL_INT(_debug, OID_AUTO, bsd4_scdebug, CTLFLAG_RW,
180 &usched_bsd4_debug, 0,
181 "Print debug information for this pid");
183 static int usched_bsd4_pid_debug = -1;
184 SYSCTL_INT(_debug, OID_AUTO, bsd4_pid_debug, CTLFLAG_RW,
185 &usched_bsd4_pid_debug, 0,
186 "Print KTR debug information for this pid");
188 /* Tunning usched_bsd4 - configurable through kern.usched_bsd4.* */
189 static int usched_bsd4_smt = 0;
190 static int usched_bsd4_cache_coherent = 0;
191 static int usched_bsd4_upri_affinity = 16; /* 32 queues - half-way */
192 static int usched_bsd4_queue_checks = 5;
193 static int usched_bsd4_stick_to_level = 0;
194 static long usched_bsd4_kicks;
195 static int usched_bsd4_rrinterval = (ESTCPUFREQ + 9) / 10;
196 static int usched_bsd4_decay = 8;
197 static int usched_bsd4_batch_time = 10;
199 /* KTR debug printings */
201 KTR_INFO_MASTER_EXTERN(usched);
203 #if !defined(KTR_USCHED_BSD4)
204 #define KTR_USCHED_BSD4 KTR_ALL
205 #endif
207 KTR_INFO(KTR_USCHED_BSD4, usched, bsd4_acquire_curproc_urw, 0,
208 "USCHED_BSD4(bsd4_acquire_curproc in user_reseched_wanted "
209 "after release: pid %d, cpuid %d, curr_cpuid %d)",
210 pid_t pid, int cpuid, int curr);
211 KTR_INFO(KTR_USCHED_BSD4, usched, bsd4_acquire_curproc_before_loop, 0,
212 "USCHED_BSD4(bsd4_acquire_curproc before loop: pid %d, cpuid %d, "
213 "curr_cpuid %d)",
214 pid_t pid, int cpuid, int curr);
215 KTR_INFO(KTR_USCHED_BSD4, usched, bsd4_acquire_curproc_not, 0,
216 "USCHED_BSD4(bsd4_acquire_curproc couldn't acquire after "
217 "bsd4_setrunqueue: pid %d, cpuid %d, curr_lp pid %d, curr_cpuid %d)",
218 pid_t pid, int cpuid, pid_t curr_pid, int curr_cpuid);
219 KTR_INFO(KTR_USCHED_BSD4, usched, bsd4_acquire_curproc_switch, 0,
220 "USCHED_BSD4(bsd4_acquire_curproc after lwkt_switch: pid %d, "
221 "cpuid %d, curr_cpuid %d)",
222 pid_t pid, int cpuid, int curr);
224 KTR_INFO(KTR_USCHED_BSD4, usched, bsd4_release_curproc, 0,
225 "USCHED_BSD4(bsd4_release_curproc before select: pid %d, "
226 "cpuid %d, curr_cpuid %d)",
227 pid_t pid, int cpuid, int curr);
229 KTR_INFO(KTR_USCHED_BSD4, usched, bsd4_select_curproc, 0,
230 "USCHED_BSD4(bsd4_release_curproc before select: pid %d, "
231 "cpuid %d, old_pid %d, old_cpuid %d, curr_cpuid %d)",
232 pid_t pid, int cpuid, pid_t old_pid, int old_cpuid, int curr);
234 KTR_INFO(KTR_USCHED_BSD4, usched, batchy_test_false, 0,
235 "USCHED_BSD4(batchy_looser_pri_test false: pid %d, "
236 "cpuid %d, verify_mask %lu)",
237 pid_t pid, int cpuid, unsigned long mask);
238 KTR_INFO(KTR_USCHED_BSD4, usched, batchy_test_true, 0,
239 "USCHED_BSD4(batchy_looser_pri_test true: pid %d, "
240 "cpuid %d, verify_mask %lu)",
241 pid_t pid, int cpuid, unsigned long mask);
243 KTR_INFO(KTR_USCHED_BSD4, usched, bsd4_setrunqueue_fc_smt, 0,
244 "USCHED_BSD4(bsd4_setrunqueue free cpus smt: pid %d, cpuid %d, "
245 "mask %lu, curr_cpuid %d)",
246 pid_t pid, int cpuid, unsigned long mask, int curr);
247 KTR_INFO(KTR_USCHED_BSD4, usched, bsd4_setrunqueue_fc_non_smt, 0,
248 "USCHED_BSD4(bsd4_setrunqueue free cpus check non_smt: pid %d, "
249 "cpuid %d, mask %lu, curr_cpuid %d)",
250 pid_t pid, int cpuid, unsigned long mask, int curr);
251 KTR_INFO(KTR_USCHED_BSD4, usched, bsd4_setrunqueue_rc, 0,
252 "USCHED_BSD4(bsd4_setrunqueue running cpus check: pid %d, "
253 "cpuid %d, mask %lu, curr_cpuid %d)",
254 pid_t pid, int cpuid, unsigned long mask, int curr);
255 KTR_INFO(KTR_USCHED_BSD4, usched, bsd4_setrunqueue_found, 0,
256 "USCHED_BSD4(bsd4_setrunqueue found cpu: pid %d, cpuid %d, "
257 "mask %lu, found_cpuid %d, curr_cpuid %d)",
258 pid_t pid, int cpuid, unsigned long mask, int found_cpuid, int curr);
259 KTR_INFO(KTR_USCHED_BSD4, usched, bsd4_setrunqueue_not_found, 0,
260 "USCHED_BSD4(bsd4_setrunqueue not found cpu: pid %d, cpuid %d, "
261 "try_cpuid %d, curr_cpuid %d)",
262 pid_t pid, int cpuid, int try_cpuid, int curr);
263 KTR_INFO(KTR_USCHED_BSD4, usched, bsd4_setrunqueue_found_best_cpuid, 0,
264 "USCHED_BSD4(bsd4_setrunqueue found cpu: pid %d, cpuid %d, "
265 "mask %lu, found_cpuid %d, curr_cpuid %d)",
266 pid_t pid, int cpuid, unsigned long mask, int found_cpuid, int curr);
268 KTR_INFO(KTR_USCHED_BSD4, usched, bsd4_chooseproc, 0,
269 "USCHED_BSD4(chooseproc: pid %d, old_cpuid %d, curr_cpuid %d)",
270 pid_t pid, int old_cpuid, int curr);
271 KTR_INFO(KTR_USCHED_BSD4, usched, chooseproc_cc, 0,
272 "USCHED_BSD4(chooseproc_cc: pid %d, old_cpuid %d, curr_cpuid %d)",
273 pid_t pid, int old_cpuid, int curr);
274 KTR_INFO(KTR_USCHED_BSD4, usched, chooseproc_cc_not_good, 0,
275 "USCHED_BSD4(chooseproc_cc not good: pid %d, old_cpumask %lu, "
276 "sibling_mask %lu, curr_cpumask %lu)",
277 pid_t pid, unsigned long old_cpumask, unsigned long sibling_mask, unsigned long curr);
278 KTR_INFO(KTR_USCHED_BSD4, usched, chooseproc_cc_elected, 0,
279 "USCHED_BSD4(chooseproc_cc elected: pid %d, old_cpumask %lu, "
280 "sibling_mask %lu, curr_cpumask: %lu)",
281 pid_t pid, unsigned long old_cpumask, unsigned long sibling_mask, unsigned long curr);
283 KTR_INFO(KTR_USCHED_BSD4, usched, sched_thread_no_process, 0,
284 "USCHED_BSD4(sched_thread %d no process scheduled: pid %d, old_cpuid %d)",
285 int id, pid_t pid, int cpuid);
286 KTR_INFO(KTR_USCHED_BSD4, usched, sched_thread_process, 0,
287 "USCHED_BSD4(sched_thread %d process scheduled: pid %d, old_cpuid %d)",
288 int id, pid_t pid, int cpuid);
289 KTR_INFO(KTR_USCHED_BSD4, usched, sched_thread_no_process_found, 0,
290 "USCHED_BSD4(sched_thread %d no process found; tmpmask %lu)",
291 int id, unsigned long tmpmask);
294 * Initialize the run queues at boot time.
296 static void
297 bsd4_rqinit(void *dummy)
299 int i;
301 spin_init(&bsd4_spin, "bsd4rq");
302 for (i = 0; i < NQS; i++) {
303 TAILQ_INIT(&bsd4_queues[i]);
304 TAILQ_INIT(&bsd4_rtqueues[i]);
305 TAILQ_INIT(&bsd4_idqueues[i]);
307 ATOMIC_CPUMASK_NANDBIT(bsd4_curprocmask, 0);
309 SYSINIT(runqueue, SI_BOOT2_USCHED, SI_ORDER_FIRST, bsd4_rqinit, NULL);
312 * BSD4_ACQUIRE_CURPROC
314 * This function is called when the kernel intends to return to userland.
315 * It is responsible for making the thread the current designated userland
316 * thread for this cpu, blocking if necessary.
318 * The kernel will not depress our LWKT priority until after we return,
319 * in case we have to shove over to another cpu.
321 * We must determine our thread's disposition before we switch away. This
322 * is very sensitive code.
324 * WARNING! THIS FUNCTION IS ALLOWED TO CAUSE THE CURRENT THREAD TO MIGRATE
325 * TO ANOTHER CPU! Because most of the kernel assumes that no migration will
326 * occur, this function is called only under very controlled circumstances.
328 * MPSAFE
330 static void
331 bsd4_acquire_curproc(struct lwp *lp)
333 globaldata_t gd;
334 bsd4_pcpu_t dd;
335 thread_t td;
336 #if 0
337 struct lwp *olp;
338 #endif
341 * Make sure we aren't sitting on a tsleep queue.
343 td = lp->lwp_thread;
344 crit_enter_quick(td);
345 if (td->td_flags & TDF_TSLEEPQ)
346 tsleep_remove(td);
347 bsd4_recalculate_estcpu(lp);
350 * If a reschedule was requested give another thread the
351 * driver's seat.
353 if (user_resched_wanted()) {
354 clear_user_resched();
355 bsd4_release_curproc(lp);
357 KTR_COND_LOG(usched_bsd4_acquire_curproc_urw,
358 lp->lwp_proc->p_pid == usched_bsd4_pid_debug,
359 lp->lwp_proc->p_pid,
360 lp->lwp_thread->td_gd->gd_cpuid,
361 mycpu->gd_cpuid);
365 * Loop until we are the current user thread
367 gd = mycpu;
368 dd = &bsd4_pcpu[gd->gd_cpuid];
370 KTR_COND_LOG(usched_bsd4_acquire_curproc_before_loop,
371 lp->lwp_proc->p_pid == usched_bsd4_pid_debug,
372 lp->lwp_proc->p_pid,
373 lp->lwp_thread->td_gd->gd_cpuid,
374 gd->gd_cpuid);
376 do {
378 * Process any pending events and higher priority threads.
380 lwkt_yield();
382 /* This lwp is an outcast; force reschedule. */
383 if (__predict_false(
384 CPUMASK_TESTBIT(lp->lwp_cpumask, gd->gd_cpuid) == 0)) {
385 bsd4_release_curproc(lp);
386 goto resched;
390 * Become the currently scheduled user thread for this cpu
391 * if we can do so trivially.
393 * We can steal another thread's current thread designation
394 * on this cpu since if we are running that other thread
395 * must not be, so we can safely deschedule it.
397 if (dd->uschedcp == lp) {
399 * We are already the current lwp (hot path).
401 dd->upri = lp->lwp_priority;
402 } else if (dd->uschedcp == NULL) {
404 * We can trivially become the current lwp.
406 ATOMIC_CPUMASK_ORBIT(bsd4_curprocmask, gd->gd_cpuid);
407 dd->uschedcp = lp;
408 dd->upri = lp->lwp_priority;
409 } else if (dd->upri > lp->lwp_priority) {
411 * We can steal the current cpu's lwp designation
412 * away simply by replacing it. The other thread
413 * will stall when it tries to return to userland.
415 dd->uschedcp = lp;
416 dd->upri = lp->lwp_priority;
418 lwkt_deschedule(olp->lwp_thread);
419 bsd4_setrunqueue(olp);
421 } else {
422 resched:
424 * We cannot become the current lwp, place the lp
425 * on the bsd4 run-queue and deschedule ourselves.
427 * When we are reactivated we will have another
428 * chance.
430 lwkt_deschedule(lp->lwp_thread);
432 bsd4_setrunqueue(lp);
434 KTR_COND_LOG(usched_bsd4_acquire_curproc_not,
435 lp->lwp_proc->p_pid == usched_bsd4_pid_debug,
436 lp->lwp_proc->p_pid,
437 lp->lwp_thread->td_gd->gd_cpuid,
438 dd->uschedcp->lwp_proc->p_pid,
439 gd->gd_cpuid);
442 lwkt_switch();
445 * Reload after a switch or setrunqueue/switch possibly
446 * moved us to another cpu.
448 gd = mycpu;
449 dd = &bsd4_pcpu[gd->gd_cpuid];
451 KTR_COND_LOG(usched_bsd4_acquire_curproc_switch,
452 lp->lwp_proc->p_pid == usched_bsd4_pid_debug,
453 lp->lwp_proc->p_pid,
454 lp->lwp_thread->td_gd->gd_cpuid,
455 gd->gd_cpuid);
457 } while (dd->uschedcp != lp);
459 crit_exit_quick(td);
460 KKASSERT((lp->lwp_mpflags & LWP_MP_ONRUNQ) == 0);
464 * BSD4_RELEASE_CURPROC
466 * This routine detaches the current thread from the userland scheduler,
467 * usually because the thread needs to run or block in the kernel (at
468 * kernel priority) for a while.
470 * This routine is also responsible for selecting a new thread to
471 * make the current thread.
473 * NOTE: This implementation differs from the dummy example in that
474 * bsd4_select_curproc() is able to select the current process, whereas
475 * dummy_select_curproc() is not able to select the current process.
476 * This means we have to NULL out uschedcp.
478 * Additionally, note that we may already be on a run queue if releasing
479 * via the lwkt_switch() in bsd4_setrunqueue().
481 * MPSAFE
484 static void
485 bsd4_release_curproc(struct lwp *lp)
487 globaldata_t gd = mycpu;
488 bsd4_pcpu_t dd = &bsd4_pcpu[gd->gd_cpuid];
490 if (dd->uschedcp == lp) {
491 crit_enter();
492 KKASSERT((lp->lwp_mpflags & LWP_MP_ONRUNQ) == 0);
494 KTR_COND_LOG(usched_bsd4_release_curproc,
495 lp->lwp_proc->p_pid == usched_bsd4_pid_debug,
496 lp->lwp_proc->p_pid,
497 lp->lwp_thread->td_gd->gd_cpuid,
498 gd->gd_cpuid);
500 dd->uschedcp = NULL; /* don't let lp be selected */
501 dd->upri = PRIBASE_NULL;
502 ATOMIC_CPUMASK_NANDBIT(bsd4_curprocmask, gd->gd_cpuid);
503 dd->old_uschedcp = lp; /* used only for KTR debug prints */
504 bsd4_select_curproc(gd);
505 crit_exit();
510 * BSD4_SELECT_CURPROC
512 * Select a new current process for this cpu and clear any pending user
513 * reschedule request. The cpu currently has no current process.
515 * This routine is also responsible for equal-priority round-robining,
516 * typically triggered from bsd4_schedulerclock(). In our dummy example
517 * all the 'user' threads are LWKT scheduled all at once and we just
518 * call lwkt_switch().
520 * The calling process is not on the queue and cannot be selected.
522 * MPSAFE
524 static
525 void
526 bsd4_select_curproc(globaldata_t gd)
528 bsd4_pcpu_t dd = &bsd4_pcpu[gd->gd_cpuid];
529 struct lwp *nlp;
530 int cpuid = gd->gd_cpuid;
532 crit_enter_gd(gd);
534 spin_lock(&bsd4_spin);
535 if(usched_bsd4_cache_coherent)
536 nlp = bsd4_chooseproc_locked_cache_coherent(dd->uschedcp);
537 else
538 nlp = bsd4_chooseproc_locked(dd->uschedcp);
540 if (nlp) {
542 KTR_COND_LOG(usched_bsd4_select_curproc,
543 nlp->lwp_proc->p_pid == usched_bsd4_pid_debug,
544 nlp->lwp_proc->p_pid,
545 nlp->lwp_thread->td_gd->gd_cpuid,
546 dd->old_uschedcp->lwp_proc->p_pid,
547 dd->old_uschedcp->lwp_thread->td_gd->gd_cpuid,
548 gd->gd_cpuid);
550 ATOMIC_CPUMASK_ORBIT(bsd4_curprocmask, cpuid);
551 dd->upri = nlp->lwp_priority;
552 dd->uschedcp = nlp;
553 dd->rrcount = 0; /* reset round robin */
554 spin_unlock(&bsd4_spin);
555 lwkt_acquire(nlp->lwp_thread);
556 lwkt_schedule(nlp->lwp_thread);
557 } else {
558 spin_unlock(&bsd4_spin);
561 #if 0
562 } else if (bsd4_runqcount && CPUMASK_TESTBIT(bsd4_rdyprocmask, cpuid)) {
563 ATOMIC_CPUMASK_NANDBIT(bsd4_rdyprocmask, cpuid);
564 spin_unlock(&bsd4_spin);
565 lwkt_schedule(dd->helper_thread);
566 } else {
567 spin_unlock(&bsd4_spin);
569 #endif
570 crit_exit_gd(gd);
574 * batchy_looser_pri_test() - determine if a process is batchy or not
575 * relative to the other processes running in the system
577 static int
578 bsd4_batchy_looser_pri_test(struct lwp* lp)
580 cpumask_t mask;
581 bsd4_pcpu_t other_dd;
582 int cpu;
584 /* Current running processes */
585 mask = bsd4_curprocmask;
586 CPUMASK_ANDMASK(mask, smp_active_mask);
587 CPUMASK_ANDMASK(mask, usched_global_cpumask);
589 while (CPUMASK_TESTNZERO(mask)) {
590 cpu = BSFCPUMASK(mask);
591 other_dd = &bsd4_pcpu[cpu];
592 if (other_dd->upri - lp->lwp_priority > usched_bsd4_upri_affinity * PPQ) {
594 KTR_COND_LOG(usched_batchy_test_false,
595 lp->lwp_proc->p_pid == usched_bsd4_pid_debug,
596 lp->lwp_proc->p_pid,
597 lp->lwp_thread->td_gd->gd_cpuid,
598 (unsigned long)CPUMASK_LOWMASK(mask));
600 return 0;
602 CPUMASK_NANDBIT(mask, cpu);
605 KTR_COND_LOG(usched_batchy_test_true,
606 lp->lwp_proc->p_pid == usched_bsd4_pid_debug,
607 lp->lwp_proc->p_pid,
608 lp->lwp_thread->td_gd->gd_cpuid,
609 (unsigned long)CPUMASK_LOWMASK(mask));
611 return 1;
616 * BSD4_SETRUNQUEUE
618 * Place the specified lwp on the user scheduler's run queue. This routine
619 * must be called with the thread descheduled. The lwp must be runnable.
621 * The thread may be the current thread as a special case.
623 * MPSAFE
625 static void
626 bsd4_setrunqueue(struct lwp *lp)
628 globaldata_t gd;
629 bsd4_pcpu_t dd;
630 int cpuid;
631 cpumask_t mask;
632 cpumask_t tmpmask;
635 * First validate the process state relative to the current cpu.
636 * We don't need the spinlock for this, just a critical section.
637 * We are in control of the process.
639 crit_enter();
640 KASSERT(lp->lwp_stat == LSRUN, ("setrunqueue: lwp not LSRUN"));
641 KASSERT((lp->lwp_mpflags & LWP_MP_ONRUNQ) == 0,
642 ("lwp %d/%d already on runq! flag %08x/%08x", lp->lwp_proc->p_pid,
643 lp->lwp_tid, lp->lwp_proc->p_flags, lp->lwp_flags));
644 KKASSERT((lp->lwp_thread->td_flags & TDF_RUNQ) == 0);
647 * Note: gd and dd are relative to the target thread's last cpu,
648 * NOT our current cpu.
650 gd = lp->lwp_thread->td_gd;
651 dd = &bsd4_pcpu[gd->gd_cpuid];
654 * This process is not supposed to be scheduled anywhere or assigned
655 * as the current process anywhere. Assert the condition.
657 KKASSERT(dd->uschedcp != lp);
660 * XXX fixme. Could be part of a remrunqueue/setrunqueue
661 * operation when the priority is recalculated, so TDF_MIGRATING
662 * may already be set.
664 if ((lp->lwp_thread->td_flags & TDF_MIGRATING) == 0)
665 lwkt_giveaway(lp->lwp_thread);
668 * We lose control of lp the moment we release the spinlock after
669 * having placed lp on the queue. i.e. another cpu could pick it
670 * up and it could exit, or its priority could be further adjusted,
671 * or something like that.
673 spin_lock(&bsd4_spin);
674 bsd4_setrunqueue_locked(lp);
675 lp->lwp_rebal_ticks = sched_ticks;
678 * Kick the scheduler helper on one of the other cpu's
679 * and request a reschedule if appropriate.
681 * NOTE: We check all cpus whos rdyprocmask is set. First we
682 * look for cpus without designated lps, then we look for
683 * cpus with designated lps with a worse priority than our
684 * process.
686 ++bsd4_scancpu;
688 if (usched_bsd4_smt) {
691 * SMT heuristic - Try to schedule on a free physical core.
692 * If no physical core found than choose the one that has
693 * an interactive thread.
696 int best_cpuid = -1;
697 int min_prio = MAXPRI * MAXPRI;
698 int sibling;
700 cpuid = (bsd4_scancpu & 0xFFFF) % ncpus;
701 mask = bsd4_rdyprocmask;
702 CPUMASK_NANDMASK(mask, bsd4_curprocmask);
703 CPUMASK_ANDMASK(mask, lp->lwp_cpumask);
704 CPUMASK_ANDMASK(mask, smp_active_mask);
705 CPUMASK_ANDMASK(mask, usched_global_cpumask);
707 KTR_COND_LOG(usched_bsd4_setrunqueue_fc_smt,
708 lp->lwp_proc->p_pid == usched_bsd4_pid_debug,
709 lp->lwp_proc->p_pid,
710 lp->lwp_thread->td_gd->gd_cpuid,
711 (unsigned long)CPUMASK_LOWMASK(mask),
712 mycpu->gd_cpuid);
714 while (CPUMASK_TESTNZERO(mask)) {
715 CPUMASK_ASSNBMASK(tmpmask, cpuid);
716 if (CPUMASK_TESTMASK(tmpmask, mask)) {
717 CPUMASK_ANDMASK(tmpmask, mask);
718 cpuid = BSFCPUMASK(tmpmask);
719 } else {
720 cpuid = BSFCPUMASK(mask);
722 gd = globaldata_find(cpuid);
723 dd = &bsd4_pcpu[cpuid];
725 if ((dd->upri & ~PPQMASK) >= (lp->lwp_priority & ~PPQMASK)) {
726 tmpmask = dd->cpunode->parent_node->members;
727 CPUMASK_NANDMASK(tmpmask, dd->cpunode->members);
728 CPUMASK_ANDMASK(tmpmask, mask);
729 if (CPUMASK_TESTNZERO(tmpmask)) {
730 KTR_COND_LOG(usched_bsd4_setrunqueue_found,
731 lp->lwp_proc->p_pid == usched_bsd4_pid_debug,
732 lp->lwp_proc->p_pid,
733 lp->lwp_thread->td_gd->gd_cpuid,
734 (unsigned long)CPUMASK_LOWMASK(mask),
735 cpuid,
736 mycpu->gd_cpuid);
738 goto found;
739 } else {
740 tmpmask =
741 dd->cpunode->parent_node->members;
742 CPUMASK_NANDMASK(tmpmask,
743 dd->cpunode->members);
744 sibling = BSFCPUMASK(tmpmask);
745 if (min_prio >
746 bsd4_pcpu[sibling].upri) {
747 min_prio =
748 bsd4_pcpu[sibling].upri;
749 best_cpuid = cpuid;
753 CPUMASK_NANDBIT(mask, cpuid);
756 if (best_cpuid != -1) {
757 cpuid = best_cpuid;
758 gd = globaldata_find(cpuid);
759 dd = &bsd4_pcpu[cpuid];
761 KTR_COND_LOG(usched_bsd4_setrunqueue_found_best_cpuid,
762 lp->lwp_proc->p_pid == usched_bsd4_pid_debug,
763 lp->lwp_proc->p_pid,
764 lp->lwp_thread->td_gd->gd_cpuid,
765 (unsigned long)CPUMASK_LOWMASK(mask),
766 cpuid,
767 mycpu->gd_cpuid);
769 goto found;
771 } else {
772 /* Fallback to the original heuristic */
773 cpuid = (bsd4_scancpu & 0xFFFF) % ncpus;
774 mask = bsd4_rdyprocmask;
775 CPUMASK_NANDMASK(mask, bsd4_curprocmask);
776 CPUMASK_ANDMASK(mask, lp->lwp_cpumask);
777 CPUMASK_ANDMASK(mask, smp_active_mask);
778 CPUMASK_ANDMASK(mask, usched_global_cpumask);
780 KTR_COND_LOG(usched_bsd4_setrunqueue_fc_non_smt,
781 lp->lwp_proc->p_pid == usched_bsd4_pid_debug,
782 lp->lwp_proc->p_pid,
783 lp->lwp_thread->td_gd->gd_cpuid,
784 (unsigned long)CPUMASK_LOWMASK(mask),
785 mycpu->gd_cpuid);
787 while (CPUMASK_TESTNZERO(mask)) {
788 CPUMASK_ASSNBMASK(tmpmask, cpuid);
789 if (CPUMASK_TESTMASK(tmpmask, mask)) {
790 CPUMASK_ANDMASK(tmpmask, mask);
791 cpuid = BSFCPUMASK(tmpmask);
792 } else {
793 cpuid = BSFCPUMASK(mask);
795 gd = globaldata_find(cpuid);
796 dd = &bsd4_pcpu[cpuid];
798 if ((dd->upri & ~PPQMASK) >=
799 (lp->lwp_priority & ~PPQMASK)) {
800 KTR_COND_LOG(usched_bsd4_setrunqueue_found,
801 lp->lwp_proc->p_pid == usched_bsd4_pid_debug,
802 lp->lwp_proc->p_pid,
803 lp->lwp_thread->td_gd->gd_cpuid,
804 (unsigned long)CPUMASK_LOWMASK(mask),
805 cpuid,
806 mycpu->gd_cpuid);
808 goto found;
810 CPUMASK_NANDBIT(mask, cpuid);
815 * Then cpus which might have a currently running lp
817 mask = bsd4_curprocmask;
818 CPUMASK_ANDMASK(mask, bsd4_rdyprocmask);
819 CPUMASK_ANDMASK(mask, lp->lwp_cpumask);
820 CPUMASK_ANDMASK(mask, smp_active_mask);
821 CPUMASK_ANDMASK(mask, usched_global_cpumask);
823 KTR_COND_LOG(usched_bsd4_setrunqueue_rc,
824 lp->lwp_proc->p_pid == usched_bsd4_pid_debug,
825 lp->lwp_proc->p_pid,
826 lp->lwp_thread->td_gd->gd_cpuid,
827 (unsigned long)CPUMASK_LOWMASK(mask),
828 mycpu->gd_cpuid);
830 while (CPUMASK_TESTNZERO(mask)) {
831 CPUMASK_ASSNBMASK(tmpmask, cpuid);
832 if (CPUMASK_TESTMASK(tmpmask, mask)) {
833 CPUMASK_ANDMASK(tmpmask, mask);
834 cpuid = BSFCPUMASK(tmpmask);
835 } else {
836 cpuid = BSFCPUMASK(mask);
838 gd = globaldata_find(cpuid);
839 dd = &bsd4_pcpu[cpuid];
841 if ((dd->upri & ~PPQMASK) > (lp->lwp_priority & ~PPQMASK)) {
842 KTR_COND_LOG(usched_bsd4_setrunqueue_found,
843 lp->lwp_proc->p_pid == usched_bsd4_pid_debug,
844 lp->lwp_proc->p_pid,
845 lp->lwp_thread->td_gd->gd_cpuid,
846 (unsigned long)CPUMASK_LOWMASK(mask),
847 cpuid,
848 mycpu->gd_cpuid);
850 goto found;
852 CPUMASK_NANDBIT(mask, cpuid);
856 * If we cannot find a suitable cpu we reload from bsd4_scancpu
857 * and round-robin. Other cpus will pickup as they release their
858 * current lwps or become ready.
860 * Avoid a degenerate system lockup case if usched_global_cpumask
861 * is set to 0 or otherwise does not cover lwp_cpumask.
863 * We only kick the target helper thread in this case, we do not
864 * set the user resched flag because
866 cpuid = (bsd4_scancpu & 0xFFFF) % ncpus;
867 if (CPUMASK_TESTBIT(lp->lwp_cpumask, cpuid) == 0)
868 cpuid = BSFCPUMASK(lp->lwp_cpumask);
869 else if (CPUMASK_TESTBIT(usched_global_cpumask, cpuid) == 0)
870 cpuid = 0;
871 gd = globaldata_find(cpuid);
872 dd = &bsd4_pcpu[cpuid];
874 KTR_COND_LOG(usched_bsd4_setrunqueue_not_found,
875 lp->lwp_proc->p_pid == usched_bsd4_pid_debug,
876 lp->lwp_proc->p_pid,
877 lp->lwp_thread->td_gd->gd_cpuid,
878 cpuid,
879 mycpu->gd_cpuid);
881 found:
882 if (gd == mycpu) {
883 spin_unlock(&bsd4_spin);
884 if ((dd->upri & ~PPQMASK) > (lp->lwp_priority & ~PPQMASK)) {
885 if (dd->uschedcp == NULL) {
886 wakeup_mycpu(dd->helper_thread);
887 } else {
888 need_user_resched();
891 } else {
892 ATOMIC_CPUMASK_NANDBIT(bsd4_rdyprocmask, cpuid);
893 spin_unlock(&bsd4_spin);
894 if ((dd->upri & ~PPQMASK) > (lp->lwp_priority & ~PPQMASK))
895 lwkt_send_ipiq(gd, bsd4_need_user_resched_remote, NULL);
896 else
897 wakeup(dd->helper_thread);
899 crit_exit();
903 * This routine is called from a systimer IPI. It MUST be MP-safe and
904 * the BGL IS NOT HELD ON ENTRY. This routine is called at ESTCPUFREQ on
905 * each cpu.
907 * This routine is called on every sched tick. If the currently running
908 * thread belongs to this scheduler it will be called with a non-NULL lp,
909 * otherwise it will be called with a NULL lp.
911 * MPSAFE
913 static
914 void
915 bsd4_schedulerclock(struct lwp *lp, sysclock_t period, sysclock_t cpstamp)
917 globaldata_t gd = mycpu;
918 bsd4_pcpu_t dd = &bsd4_pcpu[gd->gd_cpuid];
921 * No impl if no lp running.
923 if (lp == NULL)
924 return;
927 * Do we need to round-robin? We round-robin 10 times a second.
928 * This should only occur for cpu-bound batch processes.
930 if (++dd->rrcount >= usched_bsd4_rrinterval) {
931 dd->rrcount = 0;
932 need_user_resched();
936 * Adjust estcpu upward using a real time equivalent calculation.
938 lp->lwp_estcpu = ESTCPULIM(lp->lwp_estcpu + ESTCPUMAX / ESTCPUFREQ + 1);
941 * Spinlocks also hold a critical section so there should not be
942 * any active.
944 KKASSERT(gd->gd_spinlocks == 0);
946 bsd4_resetpriority(lp);
950 * Called from acquire and from kern_synch's one-second timer (one of the
951 * callout helper threads) with a critical section held.
953 * Decay p_estcpu based on the number of ticks we haven't been running
954 * and our p_nice. As the load increases each process observes a larger
955 * number of idle ticks (because other processes are running in them).
956 * This observation leads to a larger correction which tends to make the
957 * system more 'batchy'.
959 * Note that no recalculation occurs for a process which sleeps and wakes
960 * up in the same tick. That is, a system doing thousands of context
961 * switches per second will still only do serious estcpu calculations
962 * ESTCPUFREQ times per second.
964 * MPSAFE
966 static
967 void
968 bsd4_recalculate_estcpu(struct lwp *lp)
970 globaldata_t gd = mycpu;
971 sysclock_t cpbase;
972 sysclock_t ttlticks;
973 int estcpu;
974 int decay_factor;
977 * We have to subtract periodic to get the last schedclock
978 * timeout time, otherwise we would get the upcoming timeout.
979 * Keep in mind that a process can migrate between cpus and
980 * while the scheduler clock should be very close, boundary
981 * conditions could lead to a small negative delta.
983 cpbase = gd->gd_schedclock.time - gd->gd_schedclock.periodic;
985 if (lp->lwp_slptime > 1) {
987 * Too much time has passed, do a coarse correction.
989 lp->lwp_estcpu = lp->lwp_estcpu >> 1;
990 bsd4_resetpriority(lp);
991 lp->lwp_cpbase = cpbase;
992 lp->lwp_cpticks = 0;
993 lp->lwp_batch -= ESTCPUFREQ;
994 if (lp->lwp_batch < 0)
995 lp->lwp_batch = 0;
996 } else if (lp->lwp_cpbase != cpbase) {
998 * Adjust estcpu if we are in a different tick. Don't waste
999 * time if we are in the same tick.
1001 * First calculate the number of ticks in the measurement
1002 * interval. The ttlticks calculation can wind up 0 due to
1003 * a bug in the handling of lwp_slptime (as yet not found),
1004 * so make sure we do not get a divide by 0 panic.
1006 ttlticks = (cpbase - lp->lwp_cpbase) /
1007 gd->gd_schedclock.periodic;
1008 if ((ssysclock_t)ttlticks < 0) {
1009 ttlticks = 0;
1010 lp->lwp_cpbase = cpbase;
1012 if (ttlticks == 0)
1013 return;
1014 updatepcpu(lp, lp->lwp_cpticks, ttlticks);
1017 * Calculate the percentage of one cpu used factoring in ncpus
1018 * and the load and adjust estcpu. Handle degenerate cases
1019 * by adding 1 to bsd4_runqcount.
1021 * estcpu is scaled by ESTCPUMAX.
1023 * bsd4_runqcount is the excess number of user processes
1024 * that cannot be immediately scheduled to cpus. We want
1025 * to count these as running to avoid range compression
1026 * in the base calculation (which is the actual percentage
1027 * of one cpu used).
1029 estcpu = (lp->lwp_cpticks * ESTCPUMAX) *
1030 (bsd4_runqcount + ncpus) / (ncpus * ttlticks);
1033 * If estcpu is > 50% we become more batch-like
1034 * If estcpu is <= 50% we become less batch-like
1036 * It takes 30 cpu seconds to traverse the entire range.
1038 if (estcpu > ESTCPUMAX / 2) {
1039 lp->lwp_batch += ttlticks;
1040 if (lp->lwp_batch > BATCHMAX)
1041 lp->lwp_batch = BATCHMAX;
1042 } else {
1043 lp->lwp_batch -= ttlticks;
1044 if (lp->lwp_batch < 0)
1045 lp->lwp_batch = 0;
1048 if (usched_bsd4_debug == lp->lwp_proc->p_pid) {
1049 kprintf("pid %d lwp %p estcpu %3d %3d bat %d cp %d/%d",
1050 lp->lwp_proc->p_pid, lp,
1051 estcpu, lp->lwp_estcpu,
1052 lp->lwp_batch,
1053 lp->lwp_cpticks, ttlticks);
1057 * Adjust lp->lwp_esetcpu. The decay factor determines how
1058 * quickly lwp_estcpu collapses to its realtime calculation.
1059 * A slower collapse gives us a more accurate number but
1060 * can cause a cpu hog to eat too much cpu before the
1061 * scheduler decides to downgrade it.
1063 * NOTE: p_nice is accounted for in bsd4_resetpriority(),
1064 * and not here, but we must still ensure that a
1065 * cpu-bound nice -20 process does not completely
1066 * override a cpu-bound nice +20 process.
1068 * NOTE: We must use ESTCPULIM() here to deal with any
1069 * overshoot.
1071 decay_factor = usched_bsd4_decay;
1072 if (decay_factor < 1)
1073 decay_factor = 1;
1074 if (decay_factor > 1024)
1075 decay_factor = 1024;
1077 lp->lwp_estcpu = ESTCPULIM(
1078 (lp->lwp_estcpu * decay_factor + estcpu) /
1079 (decay_factor + 1));
1081 if (usched_bsd4_debug == lp->lwp_proc->p_pid)
1082 kprintf(" finalestcpu %d\n", lp->lwp_estcpu);
1083 bsd4_resetpriority(lp);
1084 lp->lwp_cpbase += ttlticks * gd->gd_schedclock.periodic;
1085 lp->lwp_cpticks = 0;
1090 * Compute the priority of a process when running in user mode.
1091 * Arrange to reschedule if the resulting priority is better
1092 * than that of the current process.
1094 * This routine may be called with any process.
1096 * This routine is called by fork1() for initial setup with the process
1097 * of the run queue, and also may be called normally with the process on or
1098 * off the run queue.
1100 * MPSAFE
1102 static void
1103 bsd4_resetpriority(struct lwp *lp)
1105 bsd4_pcpu_t dd;
1106 int newpriority;
1107 u_short newrqtype;
1108 int reschedcpu;
1109 int checkpri;
1110 int estcpu;
1113 * Calculate the new priority and queue type
1115 crit_enter();
1116 spin_lock(&bsd4_spin);
1118 newrqtype = lp->lwp_rtprio.type;
1120 switch(newrqtype) {
1121 case RTP_PRIO_REALTIME:
1122 case RTP_PRIO_FIFO:
1123 newpriority = PRIBASE_REALTIME +
1124 (lp->lwp_rtprio.prio & PRIMASK);
1125 break;
1126 case RTP_PRIO_NORMAL:
1128 * Detune estcpu based on batchiness. lwp_batch ranges
1129 * from 0 to BATCHMAX. Limit estcpu for the sake of
1130 * the priority calculation to between 50% and 100%.
1132 estcpu = lp->lwp_estcpu * (lp->lwp_batch + BATCHMAX) /
1133 (BATCHMAX * 2);
1136 * p_nice piece Adds (0-40) * 2 0-80
1137 * estcpu Adds 16384 * 4 / 512 0-128
1139 newpriority = (lp->lwp_proc->p_nice - PRIO_MIN) * PPQ / NICEPPQ;
1140 newpriority += estcpu * PPQ / ESTCPUPPQ;
1141 newpriority = newpriority * MAXPRI / (PRIO_RANGE * PPQ /
1142 NICEPPQ + ESTCPUMAX * PPQ / ESTCPUPPQ);
1143 newpriority = PRIBASE_NORMAL + (newpriority & PRIMASK);
1144 break;
1145 case RTP_PRIO_IDLE:
1146 newpriority = PRIBASE_IDLE + (lp->lwp_rtprio.prio & PRIMASK);
1147 break;
1148 case RTP_PRIO_THREAD:
1149 newpriority = PRIBASE_THREAD + (lp->lwp_rtprio.prio & PRIMASK);
1150 break;
1151 default:
1152 panic("Bad RTP_PRIO %d", newrqtype);
1153 /* NOT REACHED */
1157 * The newpriority incorporates the queue type so do a simple masked
1158 * check to determine if the process has moved to another queue. If
1159 * it has, and it is currently on a run queue, then move it.
1161 * td_upri has normal sense (higher values are more desireable), so
1162 * negate it.
1164 lp->lwp_thread->td_upri = -(newpriority & ~PPQMASK);
1165 if ((lp->lwp_priority ^ newpriority) & ~PPQMASK) {
1166 lp->lwp_priority = newpriority;
1167 if (lp->lwp_mpflags & LWP_MP_ONRUNQ) {
1168 bsd4_remrunqueue_locked(lp);
1169 lp->lwp_rqtype = newrqtype;
1170 lp->lwp_rqindex = (newpriority & PRIMASK) / PPQ;
1171 bsd4_setrunqueue_locked(lp);
1172 checkpri = 1;
1173 } else {
1174 lp->lwp_rqtype = newrqtype;
1175 lp->lwp_rqindex = (newpriority & PRIMASK) / PPQ;
1176 checkpri = 0;
1178 reschedcpu = lp->lwp_thread->td_gd->gd_cpuid;
1179 } else {
1180 lp->lwp_priority = newpriority;
1181 reschedcpu = -1;
1182 checkpri = 1;
1186 * Determine if we need to reschedule the target cpu. This only
1187 * occurs if the LWP is already on a scheduler queue, which means
1188 * that idle cpu notification has already occured. At most we
1189 * need only issue a need_user_resched() on the appropriate cpu.
1191 * The LWP may be owned by a CPU different from the current one,
1192 * in which case dd->uschedcp may be modified without an MP lock
1193 * or a spinlock held. The worst that happens is that the code
1194 * below causes a spurious need_user_resched() on the target CPU
1195 * and dd->pri to be wrong for a short period of time, both of
1196 * which are harmless.
1198 * If checkpri is 0 we are adjusting the priority of the current
1199 * process, possibly higher (less desireable), so ignore the upri
1200 * check which will fail in that case.
1202 if (reschedcpu >= 0) {
1203 dd = &bsd4_pcpu[reschedcpu];
1204 if (CPUMASK_TESTBIT(bsd4_rdyprocmask, reschedcpu) &&
1205 (checkpri == 0 ||
1206 (dd->upri & ~PRIMASK) > (lp->lwp_priority & ~PRIMASK))) {
1207 if (reschedcpu == mycpu->gd_cpuid) {
1208 spin_unlock(&bsd4_spin);
1209 need_user_resched();
1210 } else {
1211 spin_unlock(&bsd4_spin);
1212 ATOMIC_CPUMASK_NANDBIT(bsd4_rdyprocmask,
1213 reschedcpu);
1214 lwkt_send_ipiq(lp->lwp_thread->td_gd,
1215 bsd4_need_user_resched_remote,
1216 NULL);
1218 } else {
1219 spin_unlock(&bsd4_spin);
1221 } else {
1222 spin_unlock(&bsd4_spin);
1224 crit_exit();
1228 * MPSAFE
1230 static
1231 void
1232 bsd4_yield(struct lwp *lp)
1234 #if 0
1235 /* FUTURE (or something similar) */
1236 switch(lp->lwp_rqtype) {
1237 case RTP_PRIO_NORMAL:
1238 lp->lwp_estcpu = ESTCPULIM(lp->lwp_estcpu + ESTCPUINCR);
1239 break;
1240 default:
1241 break;
1243 #endif
1244 need_user_resched();
1247 static
1248 void
1249 bsd4_changedcpu(struct lwp *lp __unused)
1254 * Called from fork1() when a new child process is being created.
1256 * Give the child process an initial estcpu that is more batch then
1257 * its parent and dock the parent for the fork (but do not
1258 * reschedule the parent). This comprises the main part of our batch
1259 * detection heuristic for both parallel forking and sequential execs.
1261 * XXX lwp should be "spawning" instead of "forking"
1263 * MPSAFE
1265 static void
1266 bsd4_forking(struct lwp *plp, struct lwp *lp)
1269 * Put the child 4 queue slots (out of 32) higher than the parent
1270 * (less desireable than the parent).
1272 lp->lwp_estcpu = ESTCPULIM(plp->lwp_estcpu + ESTCPUPPQ * 4);
1275 * The batch status of children always starts out centerline
1276 * and will inch-up or inch-down as appropriate. It takes roughly
1277 * ~15 seconds of >50% cpu to hit the limit.
1279 lp->lwp_batch = BATCHMAX / 2;
1282 * Dock the parent a cost for the fork, protecting us from fork
1283 * bombs. If the parent is forking quickly make the child more
1284 * batchy.
1286 plp->lwp_estcpu = ESTCPULIM(plp->lwp_estcpu + ESTCPUPPQ / 16);
1290 * Called when a lwp is being removed from this scheduler, typically
1291 * during lwp_exit().
1293 static void
1294 bsd4_exiting(struct lwp *lp, struct proc *child_proc)
1298 static void
1299 bsd4_uload_update(struct lwp *lp)
1304 * chooseproc() is called when a cpu needs a user process to LWKT schedule,
1305 * it selects a user process and returns it. If chklp is non-NULL and chklp
1306 * has a better or equal priority then the process that would otherwise be
1307 * chosen, NULL is returned.
1309 * Until we fix the RUNQ code the chklp test has to be strict or we may
1310 * bounce between processes trying to acquire the current process designation.
1312 * MPSAFE - must be called with bsd4_spin exclusive held. The spinlock is
1313 * left intact through the entire routine.
1315 static
1316 struct lwp *
1317 bsd4_chooseproc_locked(struct lwp *chklp)
1319 struct lwp *lp;
1320 struct rq *q;
1321 u_int32_t *which, *which2;
1322 u_int32_t pri;
1323 u_int32_t rtqbits;
1324 u_int32_t tsqbits;
1325 u_int32_t idqbits;
1326 cpumask_t cpumask;
1328 rtqbits = bsd4_rtqueuebits;
1329 tsqbits = bsd4_queuebits;
1330 idqbits = bsd4_idqueuebits;
1331 cpumask = mycpu->gd_cpumask;
1334 again:
1335 if (rtqbits) {
1336 pri = bsfl(rtqbits);
1337 q = &bsd4_rtqueues[pri];
1338 which = &bsd4_rtqueuebits;
1339 which2 = &rtqbits;
1340 } else if (tsqbits) {
1341 pri = bsfl(tsqbits);
1342 q = &bsd4_queues[pri];
1343 which = &bsd4_queuebits;
1344 which2 = &tsqbits;
1345 } else if (idqbits) {
1346 pri = bsfl(idqbits);
1347 q = &bsd4_idqueues[pri];
1348 which = &bsd4_idqueuebits;
1349 which2 = &idqbits;
1350 } else {
1351 return NULL;
1353 lp = TAILQ_FIRST(q);
1354 KASSERT(lp, ("chooseproc: no lwp on busy queue"));
1356 while (CPUMASK_TESTMASK(lp->lwp_cpumask, cpumask) == 0) {
1357 lp = TAILQ_NEXT(lp, lwp_procq);
1358 if (lp == NULL) {
1359 *which2 &= ~(1 << pri);
1360 goto again;
1365 * If the passed lwp <chklp> is reasonably close to the selected
1366 * lwp <lp>, return NULL (indicating that <chklp> should be kept).
1368 * Note that we must error on the side of <chklp> to avoid bouncing
1369 * between threads in the acquire code.
1371 if (chklp) {
1372 if (chklp->lwp_priority < lp->lwp_priority + PPQ)
1373 return(NULL);
1377 * If the chosen lwp does not reside on this cpu spend a few
1378 * cycles looking for a better candidate at the same priority level.
1379 * This is a fallback check, setrunqueue() tries to wakeup the
1380 * correct cpu and is our front-line affinity.
1382 if (lp->lwp_thread->td_gd != mycpu &&
1383 (chklp = TAILQ_NEXT(lp, lwp_procq)) != NULL
1385 if (chklp->lwp_thread->td_gd == mycpu) {
1386 lp = chklp;
1390 KTR_COND_LOG(usched_bsd4_chooseproc,
1391 lp->lwp_proc->p_pid == usched_bsd4_pid_debug,
1392 lp->lwp_proc->p_pid,
1393 lp->lwp_thread->td_gd->gd_cpuid,
1394 mycpu->gd_cpuid);
1396 TAILQ_REMOVE(q, lp, lwp_procq);
1397 --bsd4_runqcount;
1398 if (TAILQ_EMPTY(q))
1399 *which &= ~(1 << pri);
1400 KASSERT((lp->lwp_mpflags & LWP_MP_ONRUNQ) != 0, ("not on runq6!"));
1401 atomic_clear_int(&lp->lwp_mpflags, LWP_MP_ONRUNQ);
1403 return lp;
1407 * chooseproc() - with a cache coherence heuristic. Try to pull a process that
1408 * has its home on the current CPU> If the process doesn't have its home here
1409 * and is a batchy one (see batcy_looser_pri_test), we can wait for a
1410 * sched_tick, may be its home will become free and pull it in. Anyway,
1411 * we can't wait more than one tick. If that tick expired, we pull in that
1412 * process, no matter what.
1414 static
1415 struct lwp *
1416 bsd4_chooseproc_locked_cache_coherent(struct lwp *chklp)
1418 struct lwp *lp;
1419 struct rq *q;
1420 u_int32_t *which, *which2;
1421 u_int32_t pri;
1422 u_int32_t checks;
1423 u_int32_t rtqbits;
1424 u_int32_t tsqbits;
1425 u_int32_t idqbits;
1426 cpumask_t cpumask;
1428 struct lwp * min_level_lwp = NULL;
1429 struct rq *min_q = NULL;
1430 cpumask_t siblings;
1431 cpu_node_t* cpunode = NULL;
1432 u_int32_t min_level = MAXCPU; /* number of levels < MAXCPU */
1433 u_int32_t *min_which = NULL;
1434 u_int32_t min_pri = 0;
1435 u_int32_t level = 0;
1437 rtqbits = bsd4_rtqueuebits;
1438 tsqbits = bsd4_queuebits;
1439 idqbits = bsd4_idqueuebits;
1440 cpumask = mycpu->gd_cpumask;
1442 /* Get the mask coresponding to the sysctl configured level */
1443 cpunode = bsd4_pcpu[mycpu->gd_cpuid].cpunode;
1444 level = usched_bsd4_stick_to_level;
1445 while (level) {
1446 cpunode = cpunode->parent_node;
1447 level--;
1449 /* The cpus which can ellect a process */
1450 siblings = cpunode->members;
1451 checks = 0;
1453 again:
1454 if (rtqbits) {
1455 pri = bsfl(rtqbits);
1456 q = &bsd4_rtqueues[pri];
1457 which = &bsd4_rtqueuebits;
1458 which2 = &rtqbits;
1459 } else if (tsqbits) {
1460 pri = bsfl(tsqbits);
1461 q = &bsd4_queues[pri];
1462 which = &bsd4_queuebits;
1463 which2 = &tsqbits;
1464 } else if (idqbits) {
1465 pri = bsfl(idqbits);
1466 q = &bsd4_idqueues[pri];
1467 which = &bsd4_idqueuebits;
1468 which2 = &idqbits;
1469 } else {
1471 * No more left and we didn't reach the checks limit.
1473 bsd4_kick_helper(min_level_lwp);
1474 return NULL;
1476 lp = TAILQ_FIRST(q);
1477 KASSERT(lp, ("chooseproc: no lwp on busy queue"));
1480 * Limit the number of checks/queue to a configurable value to
1481 * minimize the contention (we are in a locked region
1483 while (checks < usched_bsd4_queue_checks) {
1484 if (CPUMASK_TESTMASK(lp->lwp_cpumask, cpumask) == 0 ||
1485 (CPUMASK_TESTMASK(siblings,
1486 lp->lwp_thread->td_gd->gd_cpumask) == 0 &&
1487 (lp->lwp_rebal_ticks == sched_ticks ||
1488 lp->lwp_rebal_ticks == (int)(sched_ticks - 1)) &&
1489 bsd4_batchy_looser_pri_test(lp))) {
1491 KTR_COND_LOG(usched_chooseproc_cc_not_good,
1492 lp->lwp_proc->p_pid == usched_bsd4_pid_debug,
1493 lp->lwp_proc->p_pid,
1494 (unsigned long)CPUMASK_LOWMASK(
1495 lp->lwp_thread->td_gd->gd_cpumask),
1496 (unsigned long)CPUMASK_LOWMASK(siblings),
1497 (unsigned long)CPUMASK_LOWMASK(cpumask));
1499 cpunode = bsd4_pcpu[lp->lwp_thread->td_gd->gd_cpuid].cpunode;
1500 level = 0;
1501 while (cpunode) {
1502 if (CPUMASK_TESTMASK(cpunode->members,
1503 cpumask)) {
1504 break;
1506 cpunode = cpunode->parent_node;
1507 level++;
1509 if (level < min_level ||
1510 (level == min_level && min_level_lwp &&
1511 lp->lwp_priority < min_level_lwp->lwp_priority)) {
1512 bsd4_kick_helper(min_level_lwp);
1513 min_level_lwp = lp;
1514 min_level = level;
1515 min_q = q;
1516 min_which = which;
1517 min_pri = pri;
1518 } else {
1519 bsd4_kick_helper(lp);
1521 lp = TAILQ_NEXT(lp, lwp_procq);
1522 if (lp == NULL) {
1523 *which2 &= ~(1 << pri);
1524 goto again;
1526 } else {
1527 KTR_COND_LOG(usched_chooseproc_cc_elected,
1528 lp->lwp_proc->p_pid == usched_bsd4_pid_debug,
1529 lp->lwp_proc->p_pid,
1530 (unsigned long)CPUMASK_LOWMASK(
1531 lp->lwp_thread->td_gd->gd_cpumask),
1532 (unsigned long)CPUMASK_LOWMASK(siblings),
1533 (unsigned long)CPUMASK_LOWMASK(cpumask));
1535 goto found;
1537 ++checks;
1541 * Checks exhausted, we tried to defer too many threads, so schedule
1542 * the best of the worst.
1544 lp = min_level_lwp;
1545 q = min_q;
1546 which = min_which;
1547 pri = min_pri;
1548 KASSERT(lp, ("chooseproc: at least the first lp was good"));
1550 found:
1553 * If the passed lwp <chklp> is reasonably close to the selected
1554 * lwp <lp>, return NULL (indicating that <chklp> should be kept).
1556 * Note that we must error on the side of <chklp> to avoid bouncing
1557 * between threads in the acquire code.
1559 if (chklp) {
1560 if (chklp->lwp_priority < lp->lwp_priority + PPQ) {
1561 bsd4_kick_helper(lp);
1562 return(NULL);
1566 KTR_COND_LOG(usched_chooseproc_cc,
1567 lp->lwp_proc->p_pid == usched_bsd4_pid_debug,
1568 lp->lwp_proc->p_pid,
1569 lp->lwp_thread->td_gd->gd_cpuid,
1570 mycpu->gd_cpuid);
1572 TAILQ_REMOVE(q, lp, lwp_procq);
1573 --bsd4_runqcount;
1574 if (TAILQ_EMPTY(q))
1575 *which &= ~(1 << pri);
1576 KASSERT((lp->lwp_mpflags & LWP_MP_ONRUNQ) != 0, ("not on runq6!"));
1577 atomic_clear_int(&lp->lwp_mpflags, LWP_MP_ONRUNQ);
1579 return lp;
1583 * If we aren't willing to schedule a ready process on our cpu, give it's
1584 * target cpu a kick rather than wait for the next tick.
1586 * Called with bsd4_spin held.
1588 static
1589 void
1590 bsd4_kick_helper(struct lwp *lp)
1592 globaldata_t gd;
1593 bsd4_pcpu_t dd;
1594 cpumask_t tmpmask;
1596 if (lp == NULL)
1597 return;
1598 gd = lp->lwp_thread->td_gd;
1599 dd = &bsd4_pcpu[gd->gd_cpuid];
1601 tmpmask = smp_active_mask;
1602 CPUMASK_ANDMASK(tmpmask, usched_global_cpumask);
1603 CPUMASK_ANDMASK(tmpmask, bsd4_rdyprocmask);
1604 CPUMASK_ANDMASK(tmpmask, gd->gd_cpumask);
1605 if (CPUMASK_TESTZERO(tmpmask))
1606 return;
1608 ++usched_bsd4_kicks;
1609 ATOMIC_CPUMASK_NANDBIT(bsd4_rdyprocmask, gd->gd_cpuid);
1610 if ((dd->upri & ~PPQMASK) > (lp->lwp_priority & ~PPQMASK)) {
1611 lwkt_send_ipiq(gd, bsd4_need_user_resched_remote, NULL);
1612 } else {
1613 wakeup(dd->helper_thread);
1617 static
1618 void
1619 bsd4_need_user_resched_remote(void *dummy)
1621 globaldata_t gd = mycpu;
1622 bsd4_pcpu_t dd = &bsd4_pcpu[gd->gd_cpuid];
1624 need_user_resched();
1626 /* Call wakeup_mycpu to avoid sending IPIs to other CPUs */
1627 wakeup_mycpu(dd->helper_thread);
1631 * bsd4_remrunqueue_locked() removes a given process from the run queue
1632 * that it is on, clearing the queue busy bit if it becomes empty.
1634 * Note that user process scheduler is different from the LWKT schedule.
1635 * The user process scheduler only manages user processes but it uses LWKT
1636 * underneath, and a user process operating in the kernel will often be
1637 * 'released' from our management.
1639 * MPSAFE - bsd4_spin must be held exclusively on call
1641 static void
1642 bsd4_remrunqueue_locked(struct lwp *lp)
1644 struct rq *q;
1645 u_int32_t *which;
1646 u_int8_t pri;
1648 KKASSERT(lp->lwp_mpflags & LWP_MP_ONRUNQ);
1649 atomic_clear_int(&lp->lwp_mpflags, LWP_MP_ONRUNQ);
1650 --bsd4_runqcount;
1651 KKASSERT(bsd4_runqcount >= 0);
1653 pri = lp->lwp_rqindex;
1654 switch(lp->lwp_rqtype) {
1655 case RTP_PRIO_NORMAL:
1656 q = &bsd4_queues[pri];
1657 which = &bsd4_queuebits;
1658 break;
1659 case RTP_PRIO_REALTIME:
1660 case RTP_PRIO_FIFO:
1661 q = &bsd4_rtqueues[pri];
1662 which = &bsd4_rtqueuebits;
1663 break;
1664 case RTP_PRIO_IDLE:
1665 q = &bsd4_idqueues[pri];
1666 which = &bsd4_idqueuebits;
1667 break;
1668 default:
1669 panic("remrunqueue: invalid rtprio type");
1670 /* NOT REACHED */
1672 TAILQ_REMOVE(q, lp, lwp_procq);
1673 if (TAILQ_EMPTY(q)) {
1674 KASSERT((*which & (1 << pri)) != 0,
1675 ("remrunqueue: remove from empty queue"));
1676 *which &= ~(1 << pri);
1681 * bsd4_setrunqueue_locked()
1683 * Add a process whos rqtype and rqindex had previously been calculated
1684 * onto the appropriate run queue. Determine if the addition requires
1685 * a reschedule on a cpu and return the cpuid or -1.
1687 * NOTE: Lower priorities are better priorities.
1689 * MPSAFE - bsd4_spin must be held exclusively on call
1691 static void
1692 bsd4_setrunqueue_locked(struct lwp *lp)
1694 struct rq *q;
1695 u_int32_t *which;
1696 int pri;
1698 KKASSERT((lp->lwp_mpflags & LWP_MP_ONRUNQ) == 0);
1699 atomic_set_int(&lp->lwp_mpflags, LWP_MP_ONRUNQ);
1700 ++bsd4_runqcount;
1702 pri = lp->lwp_rqindex;
1704 switch(lp->lwp_rqtype) {
1705 case RTP_PRIO_NORMAL:
1706 q = &bsd4_queues[pri];
1707 which = &bsd4_queuebits;
1708 break;
1709 case RTP_PRIO_REALTIME:
1710 case RTP_PRIO_FIFO:
1711 q = &bsd4_rtqueues[pri];
1712 which = &bsd4_rtqueuebits;
1713 break;
1714 case RTP_PRIO_IDLE:
1715 q = &bsd4_idqueues[pri];
1716 which = &bsd4_idqueuebits;
1717 break;
1718 default:
1719 panic("remrunqueue: invalid rtprio type");
1720 /* NOT REACHED */
1724 * Add to the correct queue and set the appropriate bit. If no
1725 * lower priority (i.e. better) processes are in the queue then
1726 * we want a reschedule, calculate the best cpu for the job.
1728 * Always run reschedules on the LWPs original cpu.
1730 TAILQ_INSERT_TAIL(q, lp, lwp_procq);
1731 *which |= 1 << pri;
1735 * For SMP systems a user scheduler helper thread is created for each
1736 * cpu and is used to allow one cpu to wakeup another for the purposes of
1737 * scheduling userland threads from setrunqueue().
1739 * UP systems do not need the helper since there is only one cpu.
1741 * We can't use the idle thread for this because we might block.
1742 * Additionally, doing things this way allows us to HLT idle cpus
1743 * on MP systems.
1745 * MPSAFE
1747 static void
1748 sched_thread(void *dummy)
1750 globaldata_t gd;
1751 bsd4_pcpu_t dd;
1752 bsd4_pcpu_t tmpdd;
1753 struct lwp *nlp;
1754 cpumask_t mask;
1755 int cpuid;
1756 cpumask_t tmpmask;
1757 int tmpid;
1759 gd = mycpu;
1760 cpuid = gd->gd_cpuid; /* doesn't change */
1761 mask = gd->gd_cpumask; /* doesn't change */
1762 dd = &bsd4_pcpu[cpuid];
1765 * Since we are woken up only when no user processes are scheduled
1766 * on a cpu, we can run at an ultra low priority.
1768 lwkt_setpri_self(TDPRI_USER_SCHEDULER);
1770 tsleep(dd->helper_thread, 0, "sched_thread_sleep", 0);
1772 for (;;) {
1774 * We use the LWKT deschedule-interlock trick to avoid racing
1775 * bsd4_rdyprocmask. This means we cannot block through to the
1776 * manual lwkt_switch() call we make below.
1778 crit_enter_gd(gd);
1779 tsleep_interlock(dd->helper_thread, 0);
1780 spin_lock(&bsd4_spin);
1781 ATOMIC_CPUMASK_ORMASK(bsd4_rdyprocmask, mask);
1783 clear_user_resched(); /* This satisfied the reschedule request */
1784 dd->rrcount = 0; /* Reset the round-robin counter */
1786 if (CPUMASK_TESTMASK(bsd4_curprocmask, mask) == 0) {
1788 * No thread is currently scheduled.
1790 KKASSERT(dd->uschedcp == NULL);
1791 if ((nlp = bsd4_chooseproc_locked(NULL)) != NULL) {
1792 KTR_COND_LOG(usched_sched_thread_no_process,
1793 nlp->lwp_proc->p_pid == usched_bsd4_pid_debug,
1794 gd->gd_cpuid,
1795 nlp->lwp_proc->p_pid,
1796 nlp->lwp_thread->td_gd->gd_cpuid);
1798 ATOMIC_CPUMASK_ORMASK(bsd4_curprocmask, mask);
1799 dd->upri = nlp->lwp_priority;
1800 dd->uschedcp = nlp;
1801 dd->rrcount = 0; /* reset round robin */
1802 spin_unlock(&bsd4_spin);
1803 lwkt_acquire(nlp->lwp_thread);
1804 lwkt_schedule(nlp->lwp_thread);
1805 } else {
1806 spin_unlock(&bsd4_spin);
1808 } else if (bsd4_runqcount) {
1809 if ((nlp = bsd4_chooseproc_locked(dd->uschedcp)) != NULL) {
1810 KTR_COND_LOG(usched_sched_thread_process,
1811 nlp->lwp_proc->p_pid == usched_bsd4_pid_debug,
1812 gd->gd_cpuid,
1813 nlp->lwp_proc->p_pid,
1814 nlp->lwp_thread->td_gd->gd_cpuid);
1816 dd->upri = nlp->lwp_priority;
1817 dd->uschedcp = nlp;
1818 dd->rrcount = 0; /* reset round robin */
1819 spin_unlock(&bsd4_spin);
1820 lwkt_acquire(nlp->lwp_thread);
1821 lwkt_schedule(nlp->lwp_thread);
1822 } else {
1824 * CHAINING CONDITION TRAIN
1826 * We could not deal with the scheduler wakeup
1827 * request on this cpu, locate a ready scheduler
1828 * with no current lp assignment and chain to it.
1830 * This ensures that a wakeup race which fails due
1831 * to priority test does not leave other unscheduled
1832 * cpus idle when the runqueue is not empty.
1834 tmpmask = bsd4_rdyprocmask;
1835 CPUMASK_NANDMASK(tmpmask, bsd4_curprocmask);
1836 CPUMASK_ANDMASK(tmpmask, smp_active_mask);
1837 if (CPUMASK_TESTNZERO(tmpmask)) {
1838 tmpid = BSFCPUMASK(tmpmask);
1839 tmpdd = &bsd4_pcpu[tmpid];
1840 ATOMIC_CPUMASK_NANDBIT(bsd4_rdyprocmask, tmpid);
1841 spin_unlock(&bsd4_spin);
1842 wakeup(tmpdd->helper_thread);
1843 } else {
1844 spin_unlock(&bsd4_spin);
1847 KTR_LOG(usched_sched_thread_no_process_found,
1848 gd->gd_cpuid, (unsigned long)CPUMASK_LOWMASK(tmpmask));
1850 } else {
1852 * The runq is empty.
1854 spin_unlock(&bsd4_spin);
1858 * We're descheduled unless someone scheduled us. Switch away.
1859 * Exiting the critical section will cause splz() to be called
1860 * for us if interrupts and such are pending.
1862 crit_exit_gd(gd);
1863 tsleep(dd->helper_thread, PINTERLOCKED, "schslp", 0);
1867 /* sysctl stick_to_level parameter */
1868 static int
1869 sysctl_usched_bsd4_stick_to_level(SYSCTL_HANDLER_ARGS)
1871 int error, new_val;
1873 new_val = usched_bsd4_stick_to_level;
1875 error = sysctl_handle_int(oidp, &new_val, 0, req);
1876 if (error != 0 || req->newptr == NULL)
1877 return (error);
1878 if (new_val > cpu_topology_levels_number - 1 || new_val < 0)
1879 return (EINVAL);
1880 usched_bsd4_stick_to_level = new_val;
1881 return (0);
1885 * Setup our scheduler helpers. Note that curprocmask bit 0 has already
1886 * been cleared by rqinit() and we should not mess with it further.
1888 static void
1889 sched_thread_cpu_init(void)
1891 int i;
1892 int smt_not_supported = 0;
1893 int cache_coherent_not_supported = 0;
1895 if (bootverbose)
1896 kprintf("Start usched_bsd4 helpers on cpus:\n");
1898 sysctl_ctx_init(&usched_bsd4_sysctl_ctx);
1899 usched_bsd4_sysctl_tree =
1900 SYSCTL_ADD_NODE(&usched_bsd4_sysctl_ctx,
1901 SYSCTL_STATIC_CHILDREN(_kern), OID_AUTO,
1902 "usched_bsd4", CTLFLAG_RD, 0, "");
1904 for (i = 0; i < ncpus; ++i) {
1905 bsd4_pcpu_t dd = &bsd4_pcpu[i];
1906 cpumask_t mask;
1908 CPUMASK_ASSBIT(mask, i);
1910 if (CPUMASK_TESTMASK(mask, smp_active_mask) == 0)
1911 continue;
1913 dd->cpunode = get_cpu_node_by_cpuid(i);
1915 if (dd->cpunode == NULL) {
1916 smt_not_supported = 1;
1917 cache_coherent_not_supported = 1;
1918 if (bootverbose)
1919 kprintf (" cpu%d - WARNING: No CPU NODE "
1920 "found for cpu\n", i);
1921 } else {
1922 switch (dd->cpunode->type) {
1923 case THREAD_LEVEL:
1924 if (bootverbose)
1925 kprintf (" cpu%d - HyperThreading "
1926 "available. Core siblings: ",
1928 break;
1929 case CORE_LEVEL:
1930 smt_not_supported = 1;
1932 if (bootverbose)
1933 kprintf (" cpu%d - No HT available, "
1934 "multi-core/physical "
1935 "cpu. Physical siblings: ",
1937 break;
1938 case CHIP_LEVEL:
1939 smt_not_supported = 1;
1941 if (bootverbose)
1942 kprintf (" cpu%d - No HT available, "
1943 "single-core/physical cpu. "
1944 "Package Siblings: ",
1946 break;
1947 default:
1948 /* Let's go for safe defaults here */
1949 smt_not_supported = 1;
1950 cache_coherent_not_supported = 1;
1951 if (bootverbose)
1952 kprintf (" cpu%d - Unknown cpunode->"
1953 "type=%u. Siblings: ",
1955 (u_int)dd->cpunode->type);
1956 break;
1959 if (bootverbose) {
1960 if (dd->cpunode->parent_node != NULL) {
1961 kprint_cpuset(&dd->cpunode->
1962 parent_node->members);
1963 kprintf("\n");
1964 } else {
1965 kprintf(" no siblings\n");
1970 lwkt_create(sched_thread, NULL, &dd->helper_thread, NULL,
1971 0, i, "usched %d", i);
1974 * Allow user scheduling on the target cpu. cpu #0 has already
1975 * been enabled in rqinit().
1977 if (i)
1978 ATOMIC_CPUMASK_NANDMASK(bsd4_curprocmask, mask);
1979 ATOMIC_CPUMASK_ORMASK(bsd4_rdyprocmask, mask);
1980 dd->upri = PRIBASE_NULL;
1984 /* usched_bsd4 sysctl configurable parameters */
1986 SYSCTL_ADD_INT(&usched_bsd4_sysctl_ctx,
1987 SYSCTL_CHILDREN(usched_bsd4_sysctl_tree),
1988 OID_AUTO, "rrinterval", CTLFLAG_RW,
1989 &usched_bsd4_rrinterval, 0, "");
1990 SYSCTL_ADD_INT(&usched_bsd4_sysctl_ctx,
1991 SYSCTL_CHILDREN(usched_bsd4_sysctl_tree),
1992 OID_AUTO, "decay", CTLFLAG_RW,
1993 &usched_bsd4_decay, 0, "Extra decay when not running");
1994 SYSCTL_ADD_INT(&usched_bsd4_sysctl_ctx,
1995 SYSCTL_CHILDREN(usched_bsd4_sysctl_tree),
1996 OID_AUTO, "batch_time", CTLFLAG_RW,
1997 &usched_bsd4_batch_time, 0, "Min batch counter value");
1998 SYSCTL_ADD_LONG(&usched_bsd4_sysctl_ctx,
1999 SYSCTL_CHILDREN(usched_bsd4_sysctl_tree),
2000 OID_AUTO, "kicks", CTLFLAG_RW,
2001 &usched_bsd4_kicks, "Number of kickstarts");
2003 /* Add enable/disable option for SMT scheduling if supported */
2004 if (smt_not_supported) {
2005 usched_bsd4_smt = 0;
2006 SYSCTL_ADD_STRING(&usched_bsd4_sysctl_ctx,
2007 SYSCTL_CHILDREN(usched_bsd4_sysctl_tree),
2008 OID_AUTO, "smt", CTLFLAG_RD,
2009 "NOT SUPPORTED", 0, "SMT NOT SUPPORTED");
2010 } else {
2011 usched_bsd4_smt = 1;
2012 SYSCTL_ADD_INT(&usched_bsd4_sysctl_ctx,
2013 SYSCTL_CHILDREN(usched_bsd4_sysctl_tree),
2014 OID_AUTO, "smt", CTLFLAG_RW,
2015 &usched_bsd4_smt, 0, "Enable SMT scheduling");
2019 * Add enable/disable option for cache coherent scheduling
2020 * if supported
2022 if (cache_coherent_not_supported) {
2023 usched_bsd4_cache_coherent = 0;
2024 SYSCTL_ADD_STRING(&usched_bsd4_sysctl_ctx,
2025 SYSCTL_CHILDREN(usched_bsd4_sysctl_tree),
2026 OID_AUTO, "cache_coherent", CTLFLAG_RD,
2027 "NOT SUPPORTED", 0,
2028 "Cache coherence NOT SUPPORTED");
2029 } else {
2030 usched_bsd4_cache_coherent = 1;
2031 SYSCTL_ADD_INT(&usched_bsd4_sysctl_ctx,
2032 SYSCTL_CHILDREN(usched_bsd4_sysctl_tree),
2033 OID_AUTO, "cache_coherent", CTLFLAG_RW,
2034 &usched_bsd4_cache_coherent, 0,
2035 "Enable/Disable cache coherent scheduling");
2037 SYSCTL_ADD_INT(&usched_bsd4_sysctl_ctx,
2038 SYSCTL_CHILDREN(usched_bsd4_sysctl_tree),
2039 OID_AUTO, "upri_affinity", CTLFLAG_RW,
2040 &usched_bsd4_upri_affinity, 1,
2041 "Number of PPQs in user priority check");
2043 SYSCTL_ADD_INT(&usched_bsd4_sysctl_ctx,
2044 SYSCTL_CHILDREN(usched_bsd4_sysctl_tree),
2045 OID_AUTO, "queue_checks", CTLFLAG_RW,
2046 &usched_bsd4_queue_checks, 5,
2047 "LWPs to check from a queue before giving up");
2049 SYSCTL_ADD_PROC(&usched_bsd4_sysctl_ctx,
2050 SYSCTL_CHILDREN(usched_bsd4_sysctl_tree),
2051 OID_AUTO, "stick_to_level",
2052 CTLTYPE_INT | CTLFLAG_RW,
2053 NULL, sizeof usched_bsd4_stick_to_level,
2054 sysctl_usched_bsd4_stick_to_level, "I",
2055 "Stick a process to this level. See sysctl"
2056 "paremter hw.cpu_topology.level_description");
2059 SYSINIT(uschedtd, SI_BOOT2_USCHED, SI_ORDER_SECOND,
2060 sched_thread_cpu_init, NULL);