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
,
60 sched_options_t options
);
63 sched_dualq_processor_queue_remove(processor_t processor
, thread_t thread
);
66 sched_dualq_processor_csw_check(processor_t processor
);
69 sched_dualq_processor_queue_has_priority(processor_t processor
, int priority
, boolean_t gte
);
72 sched_dualq_runq_count(processor_t processor
);
75 sched_dualq_processor_queue_empty(processor_t processor
);
78 sched_dualq_runq_stats_count_sum(processor_t processor
);
81 sched_dualq_processor_bound_count(processor_t processor
);
84 sched_dualq_pset_init(processor_set_t pset
);
87 sched_dualq_processor_init(processor_t processor
);
90 sched_dualq_choose_thread(processor_t processor
, int priority
, ast_t reason
);
93 sched_dualq_processor_queue_shutdown(processor_t processor
);
96 sched_dualq_initial_thread_sched_mode(task_t parent_task
);
99 sched_dualq_thread_avoid_processor(processor_t processor
, thread_t thread
);
101 const struct sched_dispatch_table sched_dualq_dispatch
= {
102 .sched_name
= "dualq",
103 .init
= sched_dualq_init
,
104 .timebase_init
= sched_timeshare_timebase_init
,
105 .processor_init
= sched_dualq_processor_init
,
106 .pset_init
= sched_dualq_pset_init
,
107 .maintenance_continuation
= sched_timeshare_maintenance_continue
,
108 .choose_thread
= sched_dualq_choose_thread
,
109 .steal_thread_enabled
= sched_steal_thread_enabled
,
110 .steal_thread
= sched_dualq_steal_thread
,
111 .compute_timeshare_priority
= sched_compute_timeshare_priority
,
112 .choose_processor
= choose_processor
,
113 .processor_enqueue
= sched_dualq_processor_enqueue
,
114 .processor_queue_shutdown
= sched_dualq_processor_queue_shutdown
,
115 .processor_queue_remove
= sched_dualq_processor_queue_remove
,
116 .processor_queue_empty
= sched_dualq_processor_queue_empty
,
117 .priority_is_urgent
= priority_is_urgent
,
118 .processor_csw_check
= sched_dualq_processor_csw_check
,
119 .processor_queue_has_priority
= sched_dualq_processor_queue_has_priority
,
120 .initial_quantum_size
= sched_timeshare_initial_quantum_size
,
121 .initial_thread_sched_mode
= sched_dualq_initial_thread_sched_mode
,
122 .can_update_priority
= can_update_priority
,
123 .update_priority
= update_priority
,
124 .lightweight_update_priority
= lightweight_update_priority
,
125 .quantum_expire
= sched_default_quantum_expire
,
126 .processor_runq_count
= sched_dualq_runq_count
,
127 .processor_runq_stats_count_sum
= sched_dualq_runq_stats_count_sum
,
128 .processor_bound_count
= sched_dualq_processor_bound_count
,
129 .thread_update_scan
= sched_dualq_thread_update_scan
,
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
,
146 .run_count_incr
= sched_run_incr
,
147 .run_count_decr
= sched_run_decr
,
148 .update_thread_bucket
= sched_update_thread_bucket
,
149 .pset_made_schedulable
= sched_pset_made_schedulable
,
152 __attribute__((always_inline
))
153 static inline run_queue_t
154 dualq_main_runq(processor_t processor
)
156 return &processor
->processor_set
->pset_runq
;
159 __attribute__((always_inline
))
160 static inline run_queue_t
161 dualq_bound_runq(processor_t processor
)
163 return &processor
->runq
;
166 __attribute__((always_inline
))
167 static inline run_queue_t
168 dualq_runq_for_thread(processor_t processor
, thread_t thread
)
170 if (thread
->bound_processor
== PROCESSOR_NULL
) {
171 return dualq_main_runq(processor
);
173 assert(thread
->bound_processor
== processor
);
174 return dualq_bound_runq(processor
);
179 sched_dualq_initial_thread_sched_mode(task_t parent_task
)
181 if (parent_task
== kernel_task
) {
182 return TH_MODE_FIXED
;
184 return TH_MODE_TIMESHARE
;
189 sched_dualq_processor_init(processor_t processor
)
191 run_queue_init(&processor
->runq
);
195 sched_dualq_pset_init(processor_set_t pset
)
197 run_queue_init(&pset
->pset_runq
);
200 extern int sched_allow_NO_SMT_threads
;
202 sched_dualq_init(void)
204 sched_timeshare_init();
206 if (PE_parse_boot_argn("disable_NO_SMT_threads", NULL
, 0)) {
207 sched_allow_NO_SMT_threads
= 0;
212 sched_dualq_choose_thread(
213 processor_t processor
,
215 __unused ast_t reason
)
217 run_queue_t main_runq
= dualq_main_runq(processor
);
218 run_queue_t bound_runq
= dualq_bound_runq(processor
);
219 run_queue_t chosen_runq
;
221 if (bound_runq
->highq
< priority
&&
222 main_runq
->highq
< priority
) {
226 if (bound_runq
->count
&& main_runq
->count
) {
227 if (bound_runq
->highq
>= main_runq
->highq
) {
228 chosen_runq
= bound_runq
;
230 chosen_runq
= main_runq
;
232 } else if (bound_runq
->count
) {
233 chosen_runq
= bound_runq
;
234 } else if (main_runq
->count
) {
235 chosen_runq
= main_runq
;
240 if (chosen_runq
== bound_runq
) {
241 return run_queue_dequeue(chosen_runq
, SCHED_HEADQ
);
244 if (processor
->is_SMT
) {
245 thread_t potential_thread
= run_queue_peek(chosen_runq
);
246 if (potential_thread
== THREAD_NULL
) {
249 if (processor
->processor_primary
!= processor
) {
251 * Secondary processor may not run a NO_SMT thread,
252 * nor any thread if the primary is running a NO_SMT thread.
254 if (thread_no_smt(potential_thread
)) {
255 processor
->must_idle
= true;
258 processor_t primary
= processor
->processor_primary
;
259 if (primary
->state
== PROCESSOR_RUNNING
) {
260 if (processor_active_thread_no_smt(primary
)) {
261 processor
->must_idle
= true;
265 } else if (processor
->processor_secondary
!= PROCESSOR_NULL
) {
266 processor_t secondary
= processor
->processor_secondary
;
268 * Primary processor may not run a NO_SMT thread if
269 * its secondary is running a bound thread.
271 if (secondary
->state
== PROCESSOR_RUNNING
) {
272 if (thread_no_smt(potential_thread
) && secondary
->current_is_bound
) {
273 processor
->must_idle
= true;
280 return run_queue_dequeue(chosen_runq
, SCHED_HEADQ
);
284 sched_dualq_processor_enqueue(
285 processor_t processor
,
287 sched_options_t options
)
289 run_queue_t rq
= dualq_runq_for_thread(processor
, thread
);
292 result
= run_queue_enqueue(rq
, thread
, options
);
293 thread
->runq
= processor
;
299 sched_dualq_processor_queue_empty(processor_t processor
)
301 return dualq_main_runq(processor
)->count
== 0 &&
302 dualq_bound_runq(processor
)->count
== 0;
306 sched_dualq_processor_csw_check(processor_t processor
)
308 boolean_t has_higher
;
311 if (sched_dualq_thread_avoid_processor(processor
, current_thread())) {
312 return AST_PREEMPT
| AST_URGENT
;
315 run_queue_t main_runq
= dualq_main_runq(processor
);
316 run_queue_t bound_runq
= dualq_bound_runq(processor
);
318 assert(processor
->active_thread
!= NULL
);
320 pri
= MAX(main_runq
->highq
, bound_runq
->highq
);
322 if (processor
->first_timeslice
) {
323 has_higher
= (pri
> processor
->current_pri
);
325 has_higher
= (pri
>= processor
->current_pri
);
329 if (main_runq
->urgency
> 0) {
330 return AST_PREEMPT
| AST_URGENT
;
333 if (bound_runq
->urgency
> 0) {
334 return AST_PREEMPT
| AST_URGENT
;
344 sched_dualq_processor_queue_has_priority(processor_t processor
,
348 run_queue_t main_runq
= dualq_main_runq(processor
);
349 run_queue_t bound_runq
= dualq_bound_runq(processor
);
351 int qpri
= MAX(main_runq
->highq
, bound_runq
->highq
);
354 return qpri
>= priority
;
356 return qpri
> priority
;
361 sched_dualq_runq_count(processor_t processor
)
363 return dualq_main_runq(processor
)->count
+ dualq_bound_runq(processor
)->count
;
367 sched_dualq_runq_stats_count_sum(processor_t processor
)
369 uint64_t bound_sum
= dualq_bound_runq(processor
)->runq_stats
.count_sum
;
371 if (processor
->cpu_id
== processor
->processor_set
->cpu_set_low
) {
372 return bound_sum
+ dualq_main_runq(processor
)->runq_stats
.count_sum
;
378 sched_dualq_processor_bound_count(processor_t processor
)
380 return dualq_bound_runq(processor
)->count
;
384 sched_dualq_processor_queue_shutdown(processor_t processor
)
386 processor_set_t pset
= processor
->processor_set
;
387 run_queue_t rq
= dualq_main_runq(processor
);
391 /* We only need to migrate threads if this is the last active processor in the pset */
392 if (pset
->online_processor_count
> 0) {
399 while (rq
->count
> 0) {
400 thread
= run_queue_dequeue(rq
, SCHED_HEADQ
);
401 enqueue_tail(&tqueue
, &thread
->runq_links
);
406 qe_foreach_element_safe(thread
, &tqueue
, runq_links
) {
407 remqueue(&thread
->runq_links
);
411 thread_setrun(thread
, SCHED_TAILQ
);
413 thread_unlock(thread
);
418 sched_dualq_processor_queue_remove(
419 processor_t processor
,
423 processor_set_t pset
= processor
->processor_set
;
427 rq
= dualq_runq_for_thread(processor
, thread
);
429 if (processor
== thread
->runq
) {
431 * Thread is on a run queue and we have a lock on
434 run_queue_remove(rq
, thread
);
437 * The thread left the run queue before we could
438 * lock the run queue.
440 assert(thread
->runq
== PROCESSOR_NULL
);
441 processor
= PROCESSOR_NULL
;
446 return processor
!= PROCESSOR_NULL
;
450 sched_dualq_steal_thread(processor_set_t pset
)
452 processor_set_t cset
= pset
;
453 processor_set_t nset
= next_pset(cset
);
456 while (nset
!= pset
) {
461 if (cset
->pset_runq
.count
> 0) {
462 /* Need task_restrict logic here */
463 thread
= run_queue_dequeue(&cset
->pset_runq
, SCHED_HEADQ
);
468 nset
= next_pset(cset
);
477 sched_dualq_thread_update_scan(sched_update_scan_context_t scan_context
)
479 boolean_t restart_needed
= FALSE
;
480 processor_t processor
= processor_list
;
481 processor_set_t pset
;
486 * We update the threads associated with each processor (bound and idle threads)
487 * and then update the threads in each pset runqueue.
492 pset
= processor
->processor_set
;
497 restart_needed
= runq_scan(dualq_bound_runq(processor
), scan_context
);
502 if (restart_needed
) {
506 thread
= processor
->idle_thread
;
507 if (thread
!= THREAD_NULL
&& thread
->sched_stamp
!= sched_tick
) {
508 if (thread_update_add_thread(thread
) == FALSE
) {
509 restart_needed
= TRUE
;
513 } while ((processor
= processor
->processor_list
) != NULL
);
515 /* Ok, we now have a collection of candidates -- fix them. */
516 thread_update_process_threads();
517 } while (restart_needed
);
526 restart_needed
= runq_scan(&pset
->pset_runq
, scan_context
);
531 if (restart_needed
) {
534 } while ((pset
= pset
->pset_list
) != NULL
);
536 /* Ok, we now have a collection of candidates -- fix them. */
537 thread_update_process_threads();
538 } while (restart_needed
);
541 extern int sched_allow_rt_smt
;
543 /* Return true if this thread should not continue running on this processor */
545 sched_dualq_thread_avoid_processor(processor_t processor
, thread_t thread
)
547 if (thread
->bound_processor
== processor
) {
548 /* Thread is bound here */
552 if (processor
->processor_primary
!= processor
) {
554 * This is a secondary SMT processor. If the primary is running
555 * a realtime thread, only allow realtime threads on the secondary.
557 processor_t primary
= processor
->processor_primary
;
558 if ((primary
->current_pri
>= BASEPRI_RTQUEUES
) && ((thread
->sched_pri
< BASEPRI_RTQUEUES
) || !sched_allow_rt_smt
)) {
562 /* NO_SMT threads are not allowed on secondary processors */
563 if (thread_no_smt(thread
)) {
567 if (primary
->state
== PROCESSOR_RUNNING
) {
568 if (processor_active_thread_no_smt(primary
)) {
569 /* No threads allowed on secondary if primary has NO_SMT */
575 if (processor
->processor_secondary
!= PROCESSOR_NULL
) {
577 * This is a primary SMT processor. If the secondary is running
578 * a bound thread, the primary may not run a NO_SMT thread.
580 processor_t secondary
= processor
->processor_secondary
;
582 if (secondary
->state
== PROCESSOR_RUNNING
) {
583 if (secondary
->current_is_bound
&& thread_no_smt(thread
)) {