2 * Copyright (c) 2013 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <mach/mach_types.h>
30 #include <mach/machine.h>
32 #include <machine/machine_routines.h>
33 #include <machine/sched_param.h>
34 #include <machine/machine_cpu.h>
36 #include <kern/kern_types.h>
37 #include <kern/debug.h>
38 #include <kern/machine.h>
39 #include <kern/misc_protos.h>
40 #include <kern/processor.h>
41 #include <kern/queue.h>
42 #include <kern/sched.h>
43 #include <kern/sched_prim.h>
44 #include <kern/task.h>
45 #include <kern/thread.h>
47 #include <sys/kdebug.h>
50 sched_dualq_init(void);
53 sched_dualq_steal_thread(processor_set_t pset
);
56 sched_dualq_thread_update_scan(sched_update_scan_context_t scan_context
);
59 sched_dualq_processor_enqueue(processor_t processor
, thread_t thread
, integer_t options
);
62 sched_dualq_processor_queue_remove(processor_t processor
, thread_t thread
);
65 sched_dualq_processor_csw_check(processor_t processor
);
68 sched_dualq_processor_queue_has_priority(processor_t processor
, int priority
, boolean_t gte
);
71 sched_dualq_runq_count(processor_t processor
);
74 sched_dualq_processor_queue_empty(processor_t processor
);
77 sched_dualq_runq_stats_count_sum(processor_t processor
);
80 sched_dualq_processor_bound_count(processor_t processor
);
83 sched_dualq_pset_init(processor_set_t pset
);
86 sched_dualq_processor_init(processor_t processor
);
89 sched_dualq_choose_thread(processor_t processor
, int priority
, ast_t reason
);
92 sched_dualq_processor_queue_shutdown(processor_t processor
);
95 sched_dualq_initial_thread_sched_mode(task_t parent_task
);
98 sched_dualq_thread_avoid_processor(processor_t processor
, thread_t thread
);
100 const struct sched_dispatch_table sched_dualq_dispatch
= {
101 .sched_name
= "dualq",
102 .init
= sched_dualq_init
,
103 .timebase_init
= sched_timeshare_timebase_init
,
104 .processor_init
= sched_dualq_processor_init
,
105 .pset_init
= sched_dualq_pset_init
,
106 .maintenance_continuation
= sched_timeshare_maintenance_continue
,
107 .choose_thread
= sched_dualq_choose_thread
,
108 .steal_thread_enabled
= sched_steal_thread_enabled
,
109 .steal_thread
= sched_dualq_steal_thread
,
110 .compute_timeshare_priority
= sched_compute_timeshare_priority
,
111 .choose_processor
= choose_processor
,
112 .processor_enqueue
= sched_dualq_processor_enqueue
,
113 .processor_queue_shutdown
= sched_dualq_processor_queue_shutdown
,
114 .processor_queue_remove
= sched_dualq_processor_queue_remove
,
115 .processor_queue_empty
= sched_dualq_processor_queue_empty
,
116 .priority_is_urgent
= priority_is_urgent
,
117 .processor_csw_check
= sched_dualq_processor_csw_check
,
118 .processor_queue_has_priority
= sched_dualq_processor_queue_has_priority
,
119 .initial_quantum_size
= sched_timeshare_initial_quantum_size
,
120 .initial_thread_sched_mode
= sched_dualq_initial_thread_sched_mode
,
121 .can_update_priority
= can_update_priority
,
122 .update_priority
= update_priority
,
123 .lightweight_update_priority
= lightweight_update_priority
,
124 .quantum_expire
= sched_default_quantum_expire
,
125 .processor_runq_count
= sched_dualq_runq_count
,
126 .processor_runq_stats_count_sum
= sched_dualq_runq_stats_count_sum
,
127 .processor_bound_count
= sched_dualq_processor_bound_count
,
128 .thread_update_scan
= sched_dualq_thread_update_scan
,
129 .direct_dispatch_to_idle_processors
= FALSE
,
130 .multiple_psets_enabled
= TRUE
,
131 .sched_groups_enabled
= FALSE
,
132 .avoid_processor_enabled
= TRUE
,
133 .thread_avoid_processor
= sched_dualq_thread_avoid_processor
,
134 .processor_balance
= sched_SMT_balance
,
136 .rt_runq
= sched_rtglobal_runq
,
137 .rt_init
= sched_rtglobal_init
,
138 .rt_queue_shutdown
= sched_rtglobal_queue_shutdown
,
139 .rt_runq_scan
= sched_rtglobal_runq_scan
,
140 .rt_runq_count_sum
= sched_rtglobal_runq_count_sum
,
142 .qos_max_parallelism
= sched_qos_max_parallelism
,
143 .check_spill
= sched_check_spill
,
144 .ipi_policy
= sched_ipi_policy
,
145 .thread_should_yield
= sched_thread_should_yield
,
148 __attribute__((always_inline
))
149 static inline run_queue_t
150 dualq_main_runq(processor_t processor
)
152 return &processor
->processor_set
->pset_runq
;
155 __attribute__((always_inline
))
156 static inline run_queue_t
157 dualq_bound_runq(processor_t processor
)
159 return &processor
->runq
;
162 __attribute__((always_inline
))
163 static inline run_queue_t
164 dualq_runq_for_thread(processor_t processor
, thread_t thread
)
166 if (thread
->bound_processor
== PROCESSOR_NULL
) {
167 return dualq_main_runq(processor
);
169 assert(thread
->bound_processor
== processor
);
170 return dualq_bound_runq(processor
);
175 sched_dualq_initial_thread_sched_mode(task_t parent_task
)
177 if (parent_task
== kernel_task
) {
178 return TH_MODE_FIXED
;
180 return TH_MODE_TIMESHARE
;
185 sched_dualq_processor_init(processor_t processor
)
187 run_queue_init(&processor
->runq
);
191 sched_dualq_pset_init(processor_set_t pset
)
193 run_queue_init(&pset
->pset_runq
);
196 extern int sched_allow_NO_SMT_threads
;
198 sched_dualq_init(void)
200 sched_timeshare_init();
202 if (PE_parse_boot_argn("disable_NO_SMT_threads", NULL
, 0)) {
203 sched_allow_NO_SMT_threads
= 0;
208 sched_dualq_choose_thread(
209 processor_t processor
,
211 __unused ast_t reason
)
213 run_queue_t main_runq
= dualq_main_runq(processor
);
214 run_queue_t bound_runq
= dualq_bound_runq(processor
);
215 run_queue_t chosen_runq
;
217 if (bound_runq
->highq
< priority
&&
218 main_runq
->highq
< priority
) {
222 if (bound_runq
->count
&& main_runq
->count
) {
223 if (bound_runq
->highq
>= main_runq
->highq
) {
224 chosen_runq
= bound_runq
;
226 chosen_runq
= main_runq
;
228 } else if (bound_runq
->count
) {
229 chosen_runq
= bound_runq
;
230 } else if (main_runq
->count
) {
231 chosen_runq
= main_runq
;
236 if (chosen_runq
== bound_runq
) {
237 return run_queue_dequeue(chosen_runq
, SCHED_HEADQ
);
240 if (processor
->is_SMT
) {
241 thread_t potential_thread
= run_queue_dequeue(chosen_runq
, SCHED_PEEK
| SCHED_HEADQ
);
242 if (potential_thread
== THREAD_NULL
) {
245 if (processor
->processor_primary
!= processor
) {
247 * Secondary processor may not run a NO_SMT thread,
248 * nor any thread if the primary is running a NO_SMT thread.
250 if (thread_no_smt(potential_thread
)) {
251 processor
->must_idle
= true;
254 processor_t primary
= processor
->processor_primary
;
255 if (primary
->state
== PROCESSOR_RUNNING
) {
256 if (processor_active_thread_no_smt(primary
)) {
257 processor
->must_idle
= true;
261 } else if (processor
->processor_secondary
!= PROCESSOR_NULL
) {
262 processor_t secondary
= processor
->processor_secondary
;
264 * Primary processor may not run a NO_SMT thread if
265 * its secondary is running a bound thread.
267 if (secondary
->state
== PROCESSOR_RUNNING
) {
268 if (thread_no_smt(potential_thread
) && secondary
->current_is_bound
) {
269 processor
->must_idle
= true;
276 return run_queue_dequeue(chosen_runq
, SCHED_HEADQ
);
280 sched_dualq_processor_enqueue(
281 processor_t processor
,
285 run_queue_t rq
= dualq_runq_for_thread(processor
, thread
);
288 result
= run_queue_enqueue(rq
, thread
, options
);
289 thread
->runq
= processor
;
295 sched_dualq_processor_queue_empty(processor_t processor
)
297 return dualq_main_runq(processor
)->count
== 0 &&
298 dualq_bound_runq(processor
)->count
== 0;
302 sched_dualq_processor_csw_check(processor_t processor
)
304 boolean_t has_higher
;
307 if (sched_dualq_thread_avoid_processor(processor
, current_thread())) {
308 return AST_PREEMPT
| AST_URGENT
;
311 run_queue_t main_runq
= dualq_main_runq(processor
);
312 run_queue_t bound_runq
= dualq_bound_runq(processor
);
314 assert(processor
->active_thread
!= NULL
);
316 pri
= MAX(main_runq
->highq
, bound_runq
->highq
);
318 if (processor
->first_timeslice
) {
319 has_higher
= (pri
> processor
->current_pri
);
321 has_higher
= (pri
>= processor
->current_pri
);
325 if (main_runq
->urgency
> 0) {
326 return AST_PREEMPT
| AST_URGENT
;
329 if (bound_runq
->urgency
> 0) {
330 return AST_PREEMPT
| AST_URGENT
;
340 sched_dualq_processor_queue_has_priority(processor_t processor
,
344 run_queue_t main_runq
= dualq_main_runq(processor
);
345 run_queue_t bound_runq
= dualq_bound_runq(processor
);
347 int qpri
= MAX(main_runq
->highq
, bound_runq
->highq
);
350 return qpri
>= priority
;
352 return qpri
> priority
;
357 sched_dualq_runq_count(processor_t processor
)
359 return dualq_main_runq(processor
)->count
+ dualq_bound_runq(processor
)->count
;
363 sched_dualq_runq_stats_count_sum(processor_t processor
)
365 uint64_t bound_sum
= dualq_bound_runq(processor
)->runq_stats
.count_sum
;
367 if (processor
->cpu_id
== processor
->processor_set
->cpu_set_low
) {
368 return bound_sum
+ dualq_main_runq(processor
)->runq_stats
.count_sum
;
374 sched_dualq_processor_bound_count(processor_t processor
)
376 return dualq_bound_runq(processor
)->count
;
380 sched_dualq_processor_queue_shutdown(processor_t processor
)
382 processor_set_t pset
= processor
->processor_set
;
383 run_queue_t rq
= dualq_main_runq(processor
);
387 /* We only need to migrate threads if this is the last active processor in the pset */
388 if (pset
->online_processor_count
> 0) {
395 while (rq
->count
> 0) {
396 thread
= run_queue_dequeue(rq
, SCHED_HEADQ
);
397 enqueue_tail(&tqueue
, &thread
->runq_links
);
402 qe_foreach_element_safe(thread
, &tqueue
, runq_links
) {
403 remqueue(&thread
->runq_links
);
407 thread_setrun(thread
, SCHED_TAILQ
);
409 thread_unlock(thread
);
414 sched_dualq_processor_queue_remove(
415 processor_t processor
,
419 processor_set_t pset
= processor
->processor_set
;
423 rq
= dualq_runq_for_thread(processor
, thread
);
425 if (processor
== thread
->runq
) {
427 * Thread is on a run queue and we have a lock on
430 run_queue_remove(rq
, thread
);
433 * The thread left the run queue before we could
434 * lock the run queue.
436 assert(thread
->runq
== PROCESSOR_NULL
);
437 processor
= PROCESSOR_NULL
;
442 return processor
!= PROCESSOR_NULL
;
446 sched_dualq_steal_thread(processor_set_t pset
)
448 processor_set_t cset
= pset
;
449 processor_set_t nset
= next_pset(cset
);
452 while (nset
!= pset
) {
457 if (cset
->pset_runq
.count
> 0) {
458 /* Need task_restrict logic here */
459 thread
= run_queue_dequeue(&cset
->pset_runq
, SCHED_HEADQ
);
464 nset
= next_pset(cset
);
473 sched_dualq_thread_update_scan(sched_update_scan_context_t scan_context
)
475 boolean_t restart_needed
= FALSE
;
476 processor_t processor
= processor_list
;
477 processor_set_t pset
;
482 * We update the threads associated with each processor (bound and idle threads)
483 * and then update the threads in each pset runqueue.
488 pset
= processor
->processor_set
;
493 restart_needed
= runq_scan(dualq_bound_runq(processor
), scan_context
);
498 if (restart_needed
) {
502 thread
= processor
->idle_thread
;
503 if (thread
!= THREAD_NULL
&& thread
->sched_stamp
!= sched_tick
) {
504 if (thread_update_add_thread(thread
) == FALSE
) {
505 restart_needed
= TRUE
;
509 } while ((processor
= processor
->processor_list
) != NULL
);
511 /* Ok, we now have a collection of candidates -- fix them. */
512 thread_update_process_threads();
513 } while (restart_needed
);
522 restart_needed
= runq_scan(&pset
->pset_runq
, scan_context
);
527 if (restart_needed
) {
530 } while ((pset
= pset
->pset_list
) != NULL
);
532 /* Ok, we now have a collection of candidates -- fix them. */
533 thread_update_process_threads();
534 } while (restart_needed
);
537 extern int sched_allow_rt_smt
;
539 /* Return true if this thread should not continue running on this processor */
541 sched_dualq_thread_avoid_processor(processor_t processor
, thread_t thread
)
543 if (thread
->bound_processor
== processor
) {
544 /* Thread is bound here */
548 if (processor
->processor_primary
!= processor
) {
550 * This is a secondary SMT processor. If the primary is running
551 * a realtime thread, only allow realtime threads on the secondary.
553 processor_t primary
= processor
->processor_primary
;
554 if ((primary
->current_pri
>= BASEPRI_RTQUEUES
) && ((thread
->sched_pri
< BASEPRI_RTQUEUES
) || !sched_allow_rt_smt
)) {
558 /* NO_SMT threads are not allowed on secondary processors */
559 if (thread_no_smt(thread
)) {
563 if (primary
->state
== PROCESSOR_RUNNING
) {
564 if (processor_active_thread_no_smt(primary
)) {
565 /* No threads allowed on secondary if primary has NO_SMT */
571 if (processor
->processor_secondary
!= PROCESSOR_NULL
) {
573 * This is a primary SMT processor. If the secondary is running
574 * a bound thread, the primary may not run a NO_SMT thread.
576 processor_t secondary
= processor
->processor_secondary
;
578 if (secondary
->state
== PROCESSOR_RUNNING
) {
579 if (secondary
->current_is_bound
&& thread_no_smt(thread
)) {