2 * Copyright (c) 2009 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>
31 #include <mach/policy.h>
32 #include <mach/sync_policy.h>
33 #include <mach/thread_act.h>
35 #include <machine/machine_routines.h>
36 #include <machine/sched_param.h>
37 #include <machine/machine_cpu.h>
39 #include <kern/kern_types.h>
40 #include <kern/clock.h>
41 #include <kern/counters.h>
42 #include <kern/cpu_number.h>
43 #include <kern/cpu_data.h>
44 #include <kern/debug.h>
45 #include <kern/macro_help.h>
46 #include <kern/machine.h>
47 #include <kern/misc_protos.h>
48 #include <kern/processor.h>
49 #include <kern/queue.h>
50 #include <kern/sched.h>
51 #include <kern/sched_prim.h>
52 #include <kern/syscall_subr.h>
53 #include <kern/task.h>
54 #include <kern/thread.h>
55 #include <kern/wait_queue.h>
58 #include <vm/vm_kern.h>
59 #include <vm/vm_map.h>
63 #include <sys/kdebug.h>
66 sched_proto_init(void);
69 sched_proto_timebase_init(void);
72 sched_proto_processor_init(processor_t processor
);
75 sched_proto_pset_init(processor_set_t pset
);
78 sched_proto_maintenance_continuation(void);
81 sched_proto_choose_thread(processor_t processor
,
86 sched_proto_steal_thread(processor_set_t pset
);
89 sched_proto_compute_priority(thread_t thread
,
90 boolean_t override_depress
);
93 sched_proto_choose_processor( processor_set_t pset
,
94 processor_t processor
,
99 sched_proto_processor_enqueue(
100 processor_t processor
,
105 sched_proto_processor_queue_shutdown(
106 processor_t processor
);
109 sched_proto_processor_queue_remove(
110 processor_t processor
,
114 sched_proto_processor_queue_empty(processor_t processor
);
117 sched_proto_processor_queue_has_priority(processor_t processor
,
122 sched_proto_priority_is_urgent(int priority
);
125 sched_proto_processor_csw_check(processor_t processor
);
128 sched_proto_initial_quantum_size(thread_t thread
);
131 sched_proto_initial_thread_sched_mode(task_t parent_task
);
134 sched_proto_can_update_priority(thread_t thread
);
137 sched_proto_update_priority(thread_t thread
);
140 sched_proto_lightweight_update_priority(thread_t thread
);
143 sched_proto_quantum_expire(thread_t thread
);
146 sched_proto_should_current_thread_rechoose_processor(processor_t processor
);
149 sched_proto_processor_runq_count(processor_t processor
);
152 sched_proto_processor_runq_stats_count_sum(processor_t processor
);
155 sched_proto_processor_bound_count(processor_t processor
);
158 sched_proto_thread_update_scan(void);
161 const struct sched_dispatch_table sched_proto_dispatch
= {
162 .init
= sched_proto_init
,
163 .timebase_init
= sched_proto_timebase_init
,
164 .processor_init
= sched_proto_processor_init
,
165 .pset_init
= sched_proto_pset_init
,
166 .maintenance_continuation
= sched_proto_maintenance_continuation
,
167 .choose_thread
= sched_proto_choose_thread
,
168 .steal_thread
= sched_proto_steal_thread
,
169 .compute_priority
= sched_proto_compute_priority
,
170 .choose_processor
= sched_proto_choose_processor
,
171 .processor_enqueue
= sched_proto_processor_enqueue
,
172 .processor_queue_shutdown
= sched_proto_processor_queue_shutdown
,
173 .processor_queue_remove
= sched_proto_processor_queue_remove
,
174 .processor_queue_empty
= sched_proto_processor_queue_empty
,
175 .priority_is_urgent
= sched_proto_priority_is_urgent
,
176 .processor_csw_check
= sched_proto_processor_csw_check
,
177 .processor_queue_has_priority
= sched_proto_processor_queue_has_priority
,
178 .initial_quantum_size
= sched_proto_initial_quantum_size
,
179 .initial_thread_sched_mode
= sched_proto_initial_thread_sched_mode
,
180 .can_update_priority
= sched_proto_can_update_priority
,
181 .update_priority
= sched_proto_update_priority
,
182 .lightweight_update_priority
= sched_proto_lightweight_update_priority
,
183 .quantum_expire
= sched_proto_quantum_expire
,
184 .should_current_thread_rechoose_processor
= sched_proto_should_current_thread_rechoose_processor
,
185 .processor_runq_count
= sched_proto_processor_runq_count
,
186 .processor_runq_stats_count_sum
= sched_proto_processor_runq_stats_count_sum
,
187 .fairshare_init
= sched_traditional_fairshare_init
,
188 .fairshare_runq_count
= sched_traditional_fairshare_runq_count
,
189 .fairshare_runq_stats_count_sum
= sched_traditional_fairshare_runq_stats_count_sum
,
190 .fairshare_enqueue
= sched_traditional_fairshare_enqueue
,
191 .fairshare_dequeue
= sched_traditional_fairshare_dequeue
,
192 .fairshare_queue_remove
= sched_traditional_fairshare_queue_remove
,
193 .processor_bound_count
= sched_proto_processor_bound_count
,
194 .thread_update_scan
= sched_proto_thread_update_scan
,
195 .direct_dispatch_to_idle_processors
= TRUE
,
198 static struct run_queue
*global_runq
;
199 static struct run_queue global_runq_storage
;
201 #define GLOBAL_RUNQ ((processor_t)-2)
202 decl_simple_lock_data(static,global_runq_lock
);
204 extern int max_unsafe_quanta
;
206 static uint32_t proto_quantum_us
;
207 static uint32_t proto_quantum
;
209 static uint32_t runqueue_generation
;
211 static processor_t proto_processor
;
213 static uint64_t sched_proto_tick_deadline
;
214 static uint32_t sched_proto_tick
;
217 sched_proto_init(void)
219 proto_quantum_us
= 10*1000;
221 printf("standard proto timeslicing quantum is %d us\n", proto_quantum_us
);
223 simple_lock_init(&global_runq_lock
, 0);
224 global_runq
= &global_runq_storage
;
225 run_queue_init(global_runq
);
226 runqueue_generation
= 0;
228 proto_processor
= master_processor
;
232 sched_proto_timebase_init(void)
236 /* standard timeslicing quantum */
237 clock_interval_to_absolutetime_interval(
238 proto_quantum_us
, NSEC_PER_USEC
, &abstime
);
239 assert((abstime
>> 32) == 0 && (uint32_t)abstime
!= 0);
240 proto_quantum
= (uint32_t)abstime
;
242 thread_depress_time
= 1 * proto_quantum
;
243 default_timeshare_computation
= proto_quantum
/ 2;
244 default_timeshare_constraint
= proto_quantum
;
246 max_unsafe_computation
= max_unsafe_quanta
* proto_quantum
;
247 sched_safe_duration
= 2 * max_unsafe_quanta
* proto_quantum
;
252 sched_proto_processor_init(processor_t processor __unused
)
254 /* No per-processor state */
258 sched_proto_pset_init(processor_set_t pset __unused
)
263 sched_proto_maintenance_continuation(void)
265 uint64_t abstime
= mach_absolute_time();
269 /* Every 8 seconds, switch to another processor */
270 if ((sched_proto_tick
& 0x7) == 0) {
271 processor_t new_processor
;
273 new_processor
= proto_processor
->processor_list
;
274 if (new_processor
== PROCESSOR_NULL
)
275 proto_processor
= master_processor
;
277 proto_processor
= new_processor
;
282 * Compute various averages.
286 if (sched_proto_tick_deadline
== 0)
287 sched_proto_tick_deadline
= abstime
;
289 clock_deadline_for_periodic_event(sched_one_second_interval
, abstime
,
290 &sched_proto_tick_deadline
);
292 assert_wait_deadline((event_t
)sched_proto_maintenance_continuation
, THREAD_UNINT
, sched_proto_tick_deadline
);
293 thread_block((thread_continue_t
)sched_proto_maintenance_continuation
);
298 sched_proto_choose_thread(processor_t processor
,
300 ast_t reason __unused
)
302 run_queue_t rq
= global_runq
;
308 simple_lock(&global_runq_lock
);
310 queue
= rq
->queues
+ rq
->highq
;
315 * Since we don't depress priorities, a high priority thread
316 * may get selected over and over again. Put a runqueue
317 * generation number in the thread structure so that we
318 * can ensure that we've cycled through all runnable tasks
319 * before coming back to a high priority thread. This isn't
320 * perfect, especially if the number of runnable threads always
321 * stays high, but is a workable approximation
324 while (count
> 0 && pri
>= priority
) {
325 thread
= (thread_t
)queue_first(queue
);
326 while (!queue_end(queue
, (queue_entry_t
)thread
)) {
327 if ((thread
->bound_processor
== PROCESSOR_NULL
||
328 thread
->bound_processor
== processor
) &&
329 runqueue_generation
!= thread
->runqueue_generation
) {
330 remqueue((queue_entry_t
)thread
);
332 thread
->runq
= PROCESSOR_NULL
;
333 thread
->runqueue_generation
= runqueue_generation
;
334 SCHED_STATS_RUNQ_CHANGE(&rq
->runq_stats
, rq
->count
);
336 if (queue_empty(queue
)) {
338 clrbit(MAXPRI
- pri
, rq
->bitmap
);
339 rq
->highq
= MAXPRI
- ffsbit(rq
->bitmap
);
342 simple_unlock(&global_runq_lock
);
347 thread
= (thread_t
)queue_next((queue_entry_t
)thread
);
353 runqueue_generation
++;
355 simple_unlock(&global_runq_lock
);
356 return (THREAD_NULL
);
360 sched_proto_steal_thread(processor_set_t pset
)
364 return (THREAD_NULL
);
369 sched_proto_compute_priority(thread_t thread
,
370 boolean_t override_depress __unused
)
372 set_sched_pri(thread
, thread
->priority
);
376 sched_proto_choose_processor( processor_set_t pset
,
377 processor_t processor
,
378 thread_t thread __unused
)
380 processor
= proto_processor
;
383 * Check that the correct processor set is
386 if (pset
!= processor
->processor_set
) {
389 pset
= processor
->processor_set
;
397 sched_proto_processor_enqueue(
398 processor_t processor __unused
,
402 run_queue_t rq
= global_runq
;
405 simple_lock(&global_runq_lock
);
406 result
= run_queue_enqueue(rq
, thread
, options
);
407 thread
->runq
= GLOBAL_RUNQ
;
408 simple_unlock(&global_runq_lock
);
414 sched_proto_processor_queue_shutdown(
415 processor_t processor
)
417 /* With a global runqueue, just stop choosing this processor */
422 sched_proto_processor_queue_remove(
423 processor_t processor
,
429 rqlock
= &global_runq_lock
;
433 if (processor
== thread
->runq
) {
435 * Thread is on a run queue and we have a lock on
438 remqueue((queue_entry_t
)thread
);
439 SCHED_STATS_RUNQ_CHANGE(&rq
->runq_stats
, rq
->count
);
441 if (SCHED(priority_is_urgent
)(thread
->sched_pri
)) {
442 rq
->urgency
--; assert(rq
->urgency
>= 0);
445 if (queue_empty(rq
->queues
+ thread
->sched_pri
)) {
446 /* update run queue status */
447 if (thread
->sched_pri
!= IDLEPRI
)
448 clrbit(MAXPRI
- thread
->sched_pri
, rq
->bitmap
);
449 rq
->highq
= MAXPRI
- ffsbit(rq
->bitmap
);
452 thread
->runq
= PROCESSOR_NULL
;
456 * The thread left the run queue before we could
457 * lock the run queue.
459 assert(thread
->runq
== PROCESSOR_NULL
);
460 processor
= PROCESSOR_NULL
;
463 simple_unlock(rqlock
);
465 return (processor
!= PROCESSOR_NULL
);
469 sched_proto_processor_queue_empty(processor_t processor __unused
)
473 result
= (global_runq
->count
== 0);
479 sched_proto_processor_queue_has_priority(processor_t processor __unused
,
485 simple_lock(&global_runq_lock
);
488 result
= global_runq
->highq
>= priority
;
490 result
= global_runq
->highq
>= priority
;
492 simple_unlock(&global_runq_lock
);
497 /* Implement sched_preempt_pri in code */
499 sched_proto_priority_is_urgent(int priority
)
501 if (priority
<= BASEPRI_FOREGROUND
)
504 if (priority
< MINPRI_KERNEL
)
507 if (priority
>= BASEPRI_PREEMPT
)
514 sched_proto_processor_csw_check(processor_t processor __unused
)
521 urgency
= runq
->urgency
;
525 return (AST_PREEMPT
| AST_URGENT
);
534 sched_proto_initial_quantum_size(thread_t thread __unused
)
536 return proto_quantum
;
540 sched_proto_initial_thread_sched_mode(task_t parent_task
)
542 if (parent_task
== kernel_task
)
543 return TH_MODE_FIXED
;
545 return TH_MODE_TIMESHARE
;
549 sched_proto_can_update_priority(thread_t thread __unused
)
555 sched_proto_update_priority(thread_t thread __unused
)
561 sched_proto_lightweight_update_priority(thread_t thread __unused
)
567 sched_proto_quantum_expire(thread_t thread __unused
)
573 sched_proto_should_current_thread_rechoose_processor(processor_t processor
)
575 return (proto_processor
!= processor
);
579 sched_proto_processor_runq_count(processor_t processor
)
581 if (master_processor
== processor
) {
582 return global_runq
->count
;
589 sched_proto_processor_runq_stats_count_sum(processor_t processor
)
591 if (master_processor
== processor
) {
592 return global_runq
->runq_stats
.count_sum
;
599 sched_proto_processor_bound_count(__unused processor_t processor
)
605 sched_proto_thread_update_scan(void)