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
);
97 const struct sched_dispatch_table sched_dualq_dispatch
= {
98 .sched_name
= "dualq",
99 .init
= sched_dualq_init
,
100 .timebase_init
= sched_timeshare_timebase_init
,
101 .processor_init
= sched_dualq_processor_init
,
102 .pset_init
= sched_dualq_pset_init
,
103 .maintenance_continuation
= sched_timeshare_maintenance_continue
,
104 .choose_thread
= sched_dualq_choose_thread
,
105 .steal_thread_enabled
= TRUE
,
106 .steal_thread
= sched_dualq_steal_thread
,
107 .compute_timeshare_priority
= sched_compute_timeshare_priority
,
108 .choose_processor
= choose_processor
,
109 .processor_enqueue
= sched_dualq_processor_enqueue
,
110 .processor_queue_shutdown
= sched_dualq_processor_queue_shutdown
,
111 .processor_queue_remove
= sched_dualq_processor_queue_remove
,
112 .processor_queue_empty
= sched_dualq_processor_queue_empty
,
113 .priority_is_urgent
= priority_is_urgent
,
114 .processor_csw_check
= sched_dualq_processor_csw_check
,
115 .processor_queue_has_priority
= sched_dualq_processor_queue_has_priority
,
116 .initial_quantum_size
= sched_timeshare_initial_quantum_size
,
117 .initial_thread_sched_mode
= sched_dualq_initial_thread_sched_mode
,
118 .can_update_priority
= can_update_priority
,
119 .update_priority
= update_priority
,
120 .lightweight_update_priority
= lightweight_update_priority
,
121 .quantum_expire
= sched_default_quantum_expire
,
122 .processor_runq_count
= sched_dualq_runq_count
,
123 .processor_runq_stats_count_sum
= sched_dualq_runq_stats_count_sum
,
124 .processor_bound_count
= sched_dualq_processor_bound_count
,
125 .thread_update_scan
= sched_dualq_thread_update_scan
,
126 .direct_dispatch_to_idle_processors
= FALSE
,
127 .multiple_psets_enabled
= TRUE
,
128 .sched_groups_enabled
= FALSE
,
131 __attribute__((always_inline
))
132 static inline run_queue_t
dualq_main_runq(processor_t processor
)
134 return &processor
->processor_set
->pset_runq
;
137 __attribute__((always_inline
))
138 static inline run_queue_t
dualq_bound_runq(processor_t processor
)
140 return &processor
->runq
;
143 __attribute__((always_inline
))
144 static inline run_queue_t
dualq_runq_for_thread(processor_t processor
, thread_t thread
)
146 if (thread
->bound_processor
== PROCESSOR_NULL
) {
147 return dualq_main_runq(processor
);
149 assert(thread
->bound_processor
== processor
);
150 return dualq_bound_runq(processor
);
155 sched_dualq_initial_thread_sched_mode(task_t parent_task
)
157 if (parent_task
== kernel_task
)
158 return TH_MODE_FIXED
;
160 return TH_MODE_TIMESHARE
;
164 sched_dualq_processor_init(processor_t processor
)
166 run_queue_init(&processor
->runq
);
170 sched_dualq_pset_init(processor_set_t pset
)
172 run_queue_init(&pset
->pset_runq
);
176 sched_dualq_init(void)
178 sched_timeshare_init();
182 sched_dualq_choose_thread(
183 processor_t processor
,
185 __unused ast_t reason
)
187 run_queue_t main_runq
= dualq_main_runq(processor
);
188 run_queue_t bound_runq
= dualq_bound_runq(processor
);
189 run_queue_t chosen_runq
;
191 if (bound_runq
->highq
< priority
&&
192 main_runq
->highq
< priority
)
195 if (bound_runq
->count
&& main_runq
->count
) {
196 if (bound_runq
->highq
>= main_runq
->highq
) {
197 chosen_runq
= bound_runq
;
199 chosen_runq
= main_runq
;
201 } else if (bound_runq
->count
) {
202 chosen_runq
= bound_runq
;
203 } else if (main_runq
->count
) {
204 chosen_runq
= main_runq
;
206 return (THREAD_NULL
);
209 return run_queue_dequeue(chosen_runq
, SCHED_HEADQ
);
213 sched_dualq_processor_enqueue(
214 processor_t processor
,
218 run_queue_t rq
= dualq_runq_for_thread(processor
, thread
);
221 result
= run_queue_enqueue(rq
, thread
, options
);
222 thread
->runq
= processor
;
228 sched_dualq_processor_queue_empty(processor_t processor
)
230 return dualq_main_runq(processor
)->count
== 0 &&
231 dualq_bound_runq(processor
)->count
== 0;
235 sched_dualq_processor_csw_check(processor_t processor
)
237 boolean_t has_higher
;
240 run_queue_t main_runq
= dualq_main_runq(processor
);
241 run_queue_t bound_runq
= dualq_bound_runq(processor
);
243 assert(processor
->active_thread
!= NULL
);
245 pri
= MAX(main_runq
->highq
, bound_runq
->highq
);
247 if (processor
->first_timeslice
) {
248 has_higher
= (pri
> processor
->current_pri
);
250 has_higher
= (pri
>= processor
->current_pri
);
254 if (main_runq
->urgency
> 0)
255 return (AST_PREEMPT
| AST_URGENT
);
257 if (bound_runq
->urgency
> 0)
258 return (AST_PREEMPT
| AST_URGENT
);
267 sched_dualq_processor_queue_has_priority(processor_t processor
,
271 run_queue_t main_runq
= dualq_main_runq(processor
);
272 run_queue_t bound_runq
= dualq_bound_runq(processor
);
274 if (main_runq
->count
== 0 && bound_runq
->count
== 0)
277 int qpri
= MAX(main_runq
->highq
, bound_runq
->highq
);
280 return qpri
>= priority
;
282 return qpri
> priority
;
286 sched_dualq_runq_count(processor_t processor
)
288 return dualq_main_runq(processor
)->count
+ dualq_bound_runq(processor
)->count
;
292 sched_dualq_runq_stats_count_sum(processor_t processor
)
294 uint64_t bound_sum
= dualq_bound_runq(processor
)->runq_stats
.count_sum
;
296 if (processor
->cpu_id
== processor
->processor_set
->cpu_set_low
)
297 return bound_sum
+ dualq_main_runq(processor
)->runq_stats
.count_sum
;
302 sched_dualq_processor_bound_count(processor_t processor
)
304 return dualq_bound_runq(processor
)->count
;
308 sched_dualq_processor_queue_shutdown(processor_t processor
)
310 processor_set_t pset
= processor
->processor_set
;
311 run_queue_t rq
= dualq_main_runq(processor
);
315 /* We only need to migrate threads if this is the last active processor in the pset */
316 if (pset
->online_processor_count
> 0) {
323 while (rq
->count
> 0) {
324 thread
= run_queue_dequeue(rq
, SCHED_HEADQ
);
325 enqueue_tail(&tqueue
, &thread
->runq_links
);
330 qe_foreach_element_safe(thread
, &tqueue
, runq_links
) {
332 remqueue(&thread
->runq_links
);
336 thread_setrun(thread
, SCHED_TAILQ
);
338 thread_unlock(thread
);
343 sched_dualq_processor_queue_remove(
344 processor_t processor
,
348 processor_set_t pset
= processor
->processor_set
;
352 rq
= dualq_runq_for_thread(processor
, thread
);
354 if (processor
== thread
->runq
) {
356 * Thread is on a run queue and we have a lock on
359 run_queue_remove(rq
, thread
);
363 * The thread left the run queue before we could
364 * lock the run queue.
366 assert(thread
->runq
== PROCESSOR_NULL
);
367 processor
= PROCESSOR_NULL
;
372 return (processor
!= PROCESSOR_NULL
);
376 sched_dualq_steal_thread(processor_set_t pset
)
378 processor_set_t nset
, cset
= pset
;
382 if (cset
->pset_runq
.count
> 0) {
383 thread
= run_queue_dequeue(&cset
->pset_runq
, SCHED_HEADQ
);
388 nset
= next_pset(cset
);
396 } while (nset
!= pset
);
400 return (THREAD_NULL
);
404 sched_dualq_thread_update_scan(sched_update_scan_context_t scan_context
)
406 boolean_t restart_needed
= FALSE
;
407 processor_t processor
= processor_list
;
408 processor_set_t pset
;
413 * We update the threads associated with each processor (bound and idle threads)
414 * and then update the threads in each pset runqueue.
419 pset
= processor
->processor_set
;
424 restart_needed
= runq_scan(dualq_bound_runq(processor
), scan_context
);
432 thread
= processor
->idle_thread
;
433 if (thread
!= THREAD_NULL
&& thread
->sched_stamp
!= sched_tick
) {
434 if (thread_update_add_thread(thread
) == FALSE
) {
435 restart_needed
= TRUE
;
439 } while ((processor
= processor
->processor_list
) != NULL
);
441 /* Ok, we now have a collection of candidates -- fix them. */
442 thread_update_process_threads();
444 } while (restart_needed
);
453 restart_needed
= runq_scan(&pset
->pset_runq
, scan_context
);
460 } while ((pset
= pset
->pset_list
) != NULL
);
462 /* Ok, we now have a collection of candidates -- fix them. */
463 thread_update_process_threads();
465 } while (restart_needed
);