2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * @OSF_FREE_COPYRIGHT@
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
28 * All Rights Reserved.
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
54 * Author: Avadis Tevanian, Jr.
57 * Scheduling primitives
64 #include <simple_clock.h>
65 #include <power_save.h>
66 #include <task_swapper.h>
68 #include <ddb/db_output.h>
69 #include <mach/machine.h>
70 #include <machine/machine_routines.h>
71 #include <machine/sched_param.h>
73 #include <kern/clock.h>
74 #include <kern/counters.h>
75 #include <kern/cpu_number.h>
76 #include <kern/cpu_data.h>
77 #include <kern/etap_macros.h>
78 #include <kern/lock.h>
79 #include <kern/macro_help.h>
80 #include <kern/machine.h>
81 #include <kern/misc_protos.h>
82 #include <kern/processor.h>
83 #include <kern/queue.h>
84 #include <kern/sched.h>
85 #include <kern/sched_prim.h>
86 #include <kern/syscall_subr.h>
87 #include <kern/task.h>
88 #include <kern/thread.h>
89 #include <kern/thread_swap.h>
91 #include <vm/vm_kern.h>
92 #include <vm/vm_map.h>
93 #include <mach/policy.h>
94 #include <mach/sync_policy.h>
95 #include <kern/mk_sp.h> /*** ??? fix so this can be removed ***/
96 #include <sys/kdebug.h>
99 #include <kern/task_swap.h>
100 extern int task_swap_on
;
101 #endif /* TASK_SWAPPER */
105 #define DEFAULT_PREEMPTION_RATE 100 /* (1/s) */
106 int default_preemption_rate
= DEFAULT_PREEMPTION_RATE
;
108 #define MAX_UNSAFE_QUANTA 800
109 int max_unsafe_quanta
= MAX_UNSAFE_QUANTA
;
111 #define MAX_POLL_QUANTA 2
112 int max_poll_quanta
= MAX_POLL_QUANTA
;
114 #define SCHED_POLL_YIELD_SHIFT 4 /* 1/16 */
115 int sched_poll_yield_shift
= SCHED_POLL_YIELD_SHIFT
;
117 uint32_t std_quantum_us
;
123 #endif /* SIMPLE_CLOCK */
126 void wait_queues_init(void);
128 thread_t
choose_pset_thread(
129 processor_t myprocessor
,
130 processor_set_t pset
);
132 thread_t
choose_thread(
133 processor_t myprocessor
);
135 boolean_t
run_queue_enqueue(
140 void do_thread_scan(void);
143 void dump_run_queues(
145 void dump_run_queue_struct(
149 void dump_processor_set(
161 boolean_t
thread_runnable(
170 * states are combinations of:
172 * W waiting (or on wait queue)
173 * N non-interruptible
178 * assert_wait thread_block clear_wait swapout swapin
180 * R RW, RWN R; setrun - -
181 * RN RWN RN; setrun - -
194 * Waiting protocols and implementation:
196 * Each thread may be waiting for exactly one event; this event
197 * is set using assert_wait(). That thread may be awakened either
198 * by performing a thread_wakeup_prim() on its event,
199 * or by directly waking that thread up with clear_wait().
201 * The implementation of wait events uses a hash table. Each
202 * bucket is queue of threads having the same hash function
203 * value; the chain for the queue (linked list) is the run queue
204 * field. [It is not possible to be waiting and runnable at the
207 * Locks on both the thread and on the hash buckets govern the
208 * wait event field and the queue chain field. Because wakeup
209 * operations only have the event as an argument, the event hash
210 * bucket must be locked before any thread.
212 * Scheduling operations may also occur at interrupt level; therefore,
213 * interrupts below splsched() must be prevented when holding
214 * thread or hash bucket locks.
216 * The wait event hash table declarations are as follows:
221 struct wait_queue wait_queues
[NUMQUEUES
];
223 #define wait_hash(event) \
224 ((((int)(event) < 0)? ~(int)(event): (int)(event)) % NUMQUEUES)
230 * Calculate the timeslicing quantum
233 if (default_preemption_rate
< 1)
234 default_preemption_rate
= DEFAULT_PREEMPTION_RATE
;
235 std_quantum_us
= (1000 * 1000) / default_preemption_rate
;
237 printf("standard timeslicing quantum is %d us\n", std_quantum_us
);
240 pset_sys_bootstrap(); /* initialize processor mgmt. */
245 #endif /* SIMPLE_CLOCK */
250 wait_queues_init(void)
254 for (i
= 0; i
< NUMQUEUES
; i
++) {
255 wait_queue_init(&wait_queues
[i
], SYNC_POLICY_FIFO
);
260 * Thread wait timer expiration.
264 timer_call_param_t p0
,
265 timer_call_param_t p1
)
267 thread_t thread
= p0
;
272 if (--thread
->wait_timer_active
== 1) {
273 if (thread
->wait_timer_is_set
) {
274 thread
->wait_timer_is_set
= FALSE
;
277 clear_wait_internal(thread
, THREAD_TIMED_OUT
);
278 thread_unlock(thread
);
282 if (thread
->wait_timer_active
== 0)
283 thread_wakeup_one(&thread
->wait_timer_active
);
291 * Set a timer for the current thread, if the thread
292 * is ready to wait. Must be called between assert_wait()
293 * and thread_block().
298 uint32_t scale_factor
)
300 thread_t thread
= current_thread();
307 if ((thread
->state
& TH_WAIT
) != 0) {
308 clock_interval_to_deadline(interval
, scale_factor
, &deadline
);
309 timer_call_enter(&thread
->wait_timer
, deadline
);
310 assert(!thread
->wait_timer_is_set
);
311 thread
->wait_timer_active
++;
312 thread
->wait_timer_is_set
= TRUE
;
314 thread_unlock(thread
);
320 thread_set_timer_deadline(
323 thread_t thread
= current_thread();
329 if ((thread
->state
& TH_WAIT
) != 0) {
330 timer_call_enter(&thread
->wait_timer
, deadline
);
331 assert(!thread
->wait_timer_is_set
);
332 thread
->wait_timer_active
++;
333 thread
->wait_timer_is_set
= TRUE
;
335 thread_unlock(thread
);
341 thread_cancel_timer(void)
343 thread_t thread
= current_thread();
348 if (thread
->wait_timer_is_set
) {
349 if (timer_call_cancel(&thread
->wait_timer
))
350 thread
->wait_timer_active
--;
351 thread
->wait_timer_is_set
= FALSE
;
358 * Set up thread timeout element when thread is created.
364 extern void thread_depress_expire(
365 timer_call_param_t p0
,
366 timer_call_param_t p1
);
368 timer_call_setup(&thread
->wait_timer
, thread_timer_expire
, thread
);
369 thread
->wait_timer_is_set
= FALSE
;
370 thread
->wait_timer_active
= 1;
372 timer_call_setup(&thread
->depress_timer
, thread_depress_expire
, thread
);
373 thread
->depress_timer_active
= 1;
379 thread_timer_terminate(void)
381 thread_t thread
= current_thread();
387 if (thread
->wait_timer_is_set
) {
388 if (timer_call_cancel(&thread
->wait_timer
))
389 thread
->wait_timer_active
--;
390 thread
->wait_timer_is_set
= FALSE
;
393 thread
->wait_timer_active
--;
395 while (thread
->wait_timer_active
> 0) {
396 res
= assert_wait((event_t
)&thread
->wait_timer_active
, THREAD_UNINT
);
397 assert(res
== THREAD_WAITING
);
401 res
= thread_block(THREAD_CONTINUE_NULL
);
402 assert(res
== THREAD_AWAKENED
);
408 thread
->depress_timer_active
--;
410 while (thread
->depress_timer_active
> 0) {
411 res
= assert_wait((event_t
)&thread
->depress_timer_active
, THREAD_UNINT
);
412 assert(res
== THREAD_WAITING
);
416 res
= thread_block(THREAD_CONTINUE_NULL
);
417 assert(res
== THREAD_AWAKENED
);
426 thread_deallocate(thread
);
430 * Routine: thread_go_locked
432 * Start a thread running.
434 * thread lock held, IPC locks may be held.
435 * thread must have been pulled from wait queue under same lock hold.
437 * KERN_SUCCESS - Thread was set running
438 * KERN_NOT_WAITING - Thread was not waiting
443 wait_result_t result
)
445 assert(thread
->at_safe_point
== FALSE
);
446 assert(thread
->wait_event
== NO_EVENT64
);
447 assert(thread
->wait_queue
== WAIT_QUEUE_NULL
);
449 if ((thread
->state
& (TH_WAIT
|TH_TERMINATE
)) == TH_WAIT
) {
450 thread
->state
&= ~(TH_WAIT
|TH_UNINT
);
451 if (!(thread
->state
& TH_RUN
)) {
452 thread
->state
|= TH_RUN
;
454 if (thread
->active_callout
)
455 call_thread_unblock();
457 if (!(thread
->state
& TH_IDLE
)) {
458 _mk_sp_thread_unblock(thread
);
459 hw_atomic_add(&thread
->processor_set
->run_count
, 1);
463 thread
->wait_result
= result
;
466 return KERN_NOT_WAITING
;
470 * Routine: thread_mark_wait_locked
472 * Mark a thread as waiting. If, given the circumstances,
473 * it doesn't want to wait (i.e. already aborted), then
474 * indicate that in the return value.
476 * at splsched() and thread is locked.
480 thread_mark_wait_locked(
482 wait_interrupt_t interruptible
)
484 wait_result_t wait_result
;
485 boolean_t at_safe_point
;
487 assert(thread
== current_thread());
490 * The thread may have certain types of interrupts/aborts masked
491 * off. Even if the wait location says these types of interrupts
492 * are OK, we have to honor mask settings (outer-scoped code may
493 * not be able to handle aborts at the moment).
495 if (interruptible
> thread
->interrupt_level
)
496 interruptible
= thread
->interrupt_level
;
498 at_safe_point
= (interruptible
== THREAD_ABORTSAFE
);
500 if ((interruptible
== THREAD_UNINT
) ||
501 !(thread
->state
& TH_ABORT
) ||
502 (!at_safe_point
&& (thread
->state
& TH_ABORT_SAFELY
))) {
503 thread
->state
|= (interruptible
) ? TH_WAIT
: (TH_WAIT
| TH_UNINT
);
504 thread
->at_safe_point
= at_safe_point
;
505 thread
->sleep_stamp
= sched_tick
;
506 return (thread
->wait_result
= THREAD_WAITING
);
507 } else if (thread
->state
& TH_ABORT_SAFELY
) {
508 thread
->state
&= ~(TH_ABORT
|TH_ABORT_SAFELY
);
510 return (thread
->wait_result
= THREAD_INTERRUPTED
);
514 * Routine: thread_interrupt_level
516 * Set the maximum interruptible state for the
517 * current thread. The effective value of any
518 * interruptible flag passed into assert_wait
519 * will never exceed this.
521 * Useful for code that must not be interrupted,
522 * but which calls code that doesn't know that.
524 * The old interrupt level for the thread.
528 thread_interrupt_level(
529 wait_interrupt_t new_level
)
531 thread_t thread
= current_thread();
532 wait_interrupt_t result
= thread
->interrupt_level
;
534 thread
->interrupt_level
= new_level
;
539 * Routine: assert_wait_timeout
541 * Assert that the thread intends to block,
542 * waiting for a timeout (no user known event).
544 unsigned int assert_wait_timeout_event
;
548 mach_msg_timeout_t msecs
,
549 wait_interrupt_t interruptible
)
553 res
= assert_wait((event_t
)&assert_wait_timeout_event
, interruptible
);
554 if (res
== THREAD_WAITING
)
555 thread_set_timer(msecs
, 1000*NSEC_PER_USEC
);
560 * Check to see if an assert wait is possible, without actually doing one.
561 * This is used by debug code in locks and elsewhere to verify that it is
562 * always OK to block when trying to take a blocking lock (since waiting
563 * for the actual assert_wait to catch the case may make it hard to detect
567 assert_wait_possible(void)
571 extern unsigned int debug_mode
;
574 if(debug_mode
) return TRUE
; /* Always succeed in debug mode */
577 thread
= current_thread();
579 return (thread
== NULL
|| wait_queue_assert_possible(thread
));
585 * Assert that the current thread is about to go to
586 * sleep until the specified event occurs.
591 wait_interrupt_t interruptible
)
593 register wait_queue_t wq
;
596 assert(event
!= NO_EVENT
);
597 assert(assert_wait_possible());
599 index
= wait_hash(event
);
600 wq
= &wait_queues
[index
];
601 return wait_queue_assert_wait(wq
, event
, interruptible
);
606 * thread_sleep_fast_usimple_lock:
608 * Cause the current thread to wait until the specified event
609 * occurs. The specified simple_lock is unlocked before releasing
610 * the cpu and re-acquired as part of waking up.
612 * This is the simple lock sleep interface for components that use a
613 * faster version of simple_lock() than is provided by usimple_lock().
615 __private_extern__ wait_result_t
616 thread_sleep_fast_usimple_lock(
619 wait_interrupt_t interruptible
)
623 res
= assert_wait(event
, interruptible
);
624 if (res
== THREAD_WAITING
) {
626 res
= thread_block(THREAD_CONTINUE_NULL
);
634 * thread_sleep_usimple_lock:
636 * Cause the current thread to wait until the specified event
637 * occurs. The specified usimple_lock is unlocked before releasing
638 * the cpu and re-acquired as part of waking up.
640 * This is the simple lock sleep interface for components where
641 * simple_lock() is defined in terms of usimple_lock().
644 thread_sleep_usimple_lock(
647 wait_interrupt_t interruptible
)
651 res
= assert_wait(event
, interruptible
);
652 if (res
== THREAD_WAITING
) {
653 usimple_unlock(lock
);
654 res
= thread_block(THREAD_CONTINUE_NULL
);
661 * thread_sleep_mutex:
663 * Cause the current thread to wait until the specified event
664 * occurs. The specified mutex is unlocked before releasing
665 * the cpu. The mutex will be re-acquired before returning.
667 * JMM - Add hint to make sure mutex is available before rousting
673 wait_interrupt_t interruptible
)
677 res
= assert_wait(event
, interruptible
);
678 if (res
== THREAD_WAITING
) {
680 res
= thread_block(THREAD_CONTINUE_NULL
);
687 * thread_sleep_mutex_deadline:
689 * Cause the current thread to wait until the specified event
690 * (or deadline) occurs. The specified mutex is unlocked before
691 * releasing the cpu. The mutex will be re-acquired before returning.
693 * JMM - Add hint to make sure mutex is available before rousting
696 thread_sleep_mutex_deadline(
700 wait_interrupt_t interruptible
)
704 res
= assert_wait(event
, interruptible
);
705 if (res
== THREAD_WAITING
) {
707 thread_set_timer_deadline(deadline
);
708 res
= thread_block(THREAD_CONTINUE_NULL
);
709 if (res
!= THREAD_TIMED_OUT
)
710 thread_cancel_timer();
717 * thread_sleep_lock_write:
719 * Cause the current thread to wait until the specified event
720 * occurs. The specified (write) lock is unlocked before releasing
721 * the cpu. The (write) lock will be re-acquired before returning.
723 * JMM - Add hint to make sure mutex is available before rousting
726 thread_sleep_lock_write(
729 wait_interrupt_t interruptible
)
733 res
= assert_wait(event
, interruptible
);
734 if (res
== THREAD_WAITING
) {
735 lock_write_done(lock
);
736 res
= thread_block(THREAD_CONTINUE_NULL
);
744 * thread_sleep_funnel:
746 * Cause the current thread to wait until the specified event
747 * occurs. If the thread is funnelled, the funnel will be released
748 * before giving up the cpu. The funnel will be re-acquired before returning.
750 * JMM - Right now the funnel is dropped and re-acquired inside
751 * thread_block(). At some point, this may give thread_block() a hint.
756 wait_interrupt_t interruptible
)
760 res
= assert_wait(event
, interruptible
);
761 if (res
== THREAD_WAITING
) {
762 res
= thread_block(THREAD_CONTINUE_NULL
);
768 * thread_[un]stop(thread)
769 * Once a thread has blocked interruptibly (via assert_wait) prevent
770 * it from running until thread_unstop.
772 * If someone else has already stopped the thread, wait for the
773 * stop to be cleared, and then stop it again.
775 * Return FALSE if interrupted.
777 * NOTE: thread_hold/thread_suspend should be called on the activation
778 * before calling thread_stop. TH_SUSP is only recognized when
779 * a thread blocks and only prevents clear_wait/thread_wakeup
780 * from restarting an interruptible wait. The wake_active flag is
781 * used to indicate that someone is waiting on the thread.
787 spl_t s
= splsched();
791 while (thread
->state
& TH_SUSP
) {
792 wait_result_t result
;
794 thread
->wake_active
= TRUE
;
795 result
= assert_wait(&thread
->wake_active
, THREAD_ABORTSAFE
);
799 if (result
== THREAD_WAITING
)
800 result
= thread_block(THREAD_CONTINUE_NULL
);
802 if (result
!= THREAD_AWAKENED
)
810 thread
->state
|= TH_SUSP
;
812 while (thread
->state
& TH_RUN
) {
813 wait_result_t result
;
814 processor_t processor
= thread
->last_processor
;
816 if ( processor
!= PROCESSOR_NULL
&&
817 processor
->state
== PROCESSOR_RUNNING
&&
818 processor
->cpu_data
->active_thread
== thread
)
819 cause_ast_check(processor
);
820 thread_unlock(thread
);
822 thread
->wake_active
= TRUE
;
823 result
= assert_wait(&thread
->wake_active
, THREAD_ABORTSAFE
);
827 if (result
== THREAD_WAITING
)
828 result
= thread_block(THREAD_CONTINUE_NULL
);
830 if (result
!= THREAD_AWAKENED
) {
831 thread_unstop(thread
);
840 thread_unlock(thread
);
848 * Clear TH_SUSP and if the thread has been stopped and is now runnable,
849 * put it back on the run queue.
855 spl_t s
= splsched();
860 if ((thread
->state
& (TH_RUN
|TH_WAIT
|TH_SUSP
)) == TH_SUSP
) {
861 thread
->state
&= ~TH_SUSP
;
862 thread
->state
|= TH_RUN
;
864 assert(!(thread
->state
& TH_IDLE
));
865 _mk_sp_thread_unblock(thread
);
866 hw_atomic_add(&thread
->processor_set
->run_count
, 1);
869 if (thread
->state
& TH_SUSP
) {
870 thread
->state
&= ~TH_SUSP
;
872 if (thread
->wake_active
) {
873 thread
->wake_active
= FALSE
;
874 thread_unlock(thread
);
878 thread_wakeup(&thread
->wake_active
);
883 thread_unlock(thread
);
889 * Wait for the thread's RUN bit to clear
895 spl_t s
= splsched();
900 while (thread
->state
& TH_RUN
) {
901 wait_result_t result
;
902 processor_t processor
= thread
->last_processor
;
904 if ( processor
!= PROCESSOR_NULL
&&
905 processor
->state
== PROCESSOR_RUNNING
&&
906 processor
->cpu_data
->active_thread
== thread
)
907 cause_ast_check(processor
);
908 thread_unlock(thread
);
910 thread
->wake_active
= TRUE
;
911 result
= assert_wait(&thread
->wake_active
, THREAD_ABORTSAFE
);
915 if (result
== THREAD_WAITING
)
916 result
= thread_block(THREAD_CONTINUE_NULL
);
918 if (result
!= THREAD_AWAKENED
)
926 thread_unlock(thread
);
934 * Routine: clear_wait_internal
936 * Clear the wait condition for the specified thread.
937 * Start the thread executing if that is appropriate.
939 * thread thread to awaken
940 * result Wakeup result the thread should see
943 * the thread is locked.
945 * KERN_SUCCESS thread was rousted out a wait
946 * KERN_FAILURE thread was waiting but could not be rousted
947 * KERN_NOT_WAITING thread was not waiting
949 __private_extern__ kern_return_t
952 wait_result_t result
)
954 wait_queue_t wq
= thread
->wait_queue
;
960 if ((result
== THREAD_INTERRUPTED
) && (thread
->state
& TH_UNINT
))
963 if (wq
!= WAIT_QUEUE_NULL
) {
964 if (wait_queue_lock_try(wq
)) {
965 wait_queue_pull_thread_locked(wq
, thread
, TRUE
);
966 /* wait queue unlocked, thread still locked */
968 thread_unlock(thread
);
972 if (wq
!= thread
->wait_queue
) {
973 return KERN_NOT_WAITING
; /* we know it moved */
978 ret
= thread_go_locked(thread
, result
);
980 } while (++loop_count
< LockTimeOut
);
981 panic("clear_wait_internal: deadlock: thread=0x%x, wq=0x%x, cpu=%d\n",
982 thread
, wq
, cpu_number());
990 * Clear the wait condition for the specified thread. Start the thread
991 * executing if that is appropriate.
994 * thread thread to awaken
995 * result Wakeup result the thread should see
1000 wait_result_t result
)
1006 thread_lock(thread
);
1007 ret
= clear_wait_internal(thread
, result
);
1008 thread_unlock(thread
);
1015 * thread_wakeup_prim:
1017 * Common routine for thread_wakeup, thread_wakeup_with_result,
1018 * and thread_wakeup_one.
1024 boolean_t one_thread
,
1025 wait_result_t result
)
1027 register wait_queue_t wq
;
1030 index
= wait_hash(event
);
1031 wq
= &wait_queues
[index
];
1033 return (wait_queue_wakeup_one(wq
, event
, result
));
1035 return (wait_queue_wakeup_all(wq
, event
, result
));
1041 * Force a thread to execute on the specified processor.
1042 * If the thread is currently executing, it may wait until its
1043 * time slice is up before switching onto the specified processor.
1045 * A processor of PROCESSOR_NULL causes the thread to be unbound.
1046 * xxx - DO NOT export this to users.
1050 register thread_t thread
,
1051 processor_t processor
)
1056 thread_lock(thread
);
1057 thread_bind_locked(thread
, processor
);
1058 thread_unlock(thread
);
1063 * Select a thread for this processor (the current processor) to run.
1064 * May select the current thread, which must already be locked.
1068 register processor_t myprocessor
)
1070 register thread_t thread
;
1071 processor_set_t pset
;
1072 register run_queue_t runq
= &myprocessor
->runq
;
1073 boolean_t other_runnable
;
1076 * Check for other non-idle runnable threads.
1078 pset
= myprocessor
->processor_set
;
1079 thread
= myprocessor
->cpu_data
->active_thread
;
1081 /* Update the thread's priority */
1082 if (thread
->sched_stamp
!= sched_tick
)
1083 update_priority(thread
);
1085 myprocessor
->current_pri
= thread
->sched_pri
;
1087 simple_lock(&runq
->lock
);
1088 simple_lock(&pset
->runq
.lock
);
1090 other_runnable
= runq
->count
> 0 || pset
->runq
.count
> 0;
1092 if ( thread
->state
== TH_RUN
&&
1094 (runq
->highq
< thread
->sched_pri
&&
1095 pset
->runq
.highq
< thread
->sched_pri
)) &&
1096 thread
->processor_set
== pset
&&
1097 (thread
->bound_processor
== PROCESSOR_NULL
||
1098 thread
->bound_processor
== myprocessor
) ) {
1100 /* I am the highest priority runnable (non-idle) thread */
1101 simple_unlock(&pset
->runq
.lock
);
1102 simple_unlock(&runq
->lock
);
1104 myprocessor
->slice_quanta
=
1105 (thread
->sched_mode
& TH_MODE_TIMESHARE
)? pset
->set_quanta
: 1;
1109 thread
= choose_thread(myprocessor
);
1111 simple_unlock(&pset
->runq
.lock
);
1112 simple_unlock(&runq
->lock
);
1115 * Nothing is runnable, so set this processor idle if it
1116 * was running. If it was in an assignment or shutdown,
1117 * leave it alone. Return its idle thread.
1119 simple_lock(&pset
->sched_lock
);
1120 if (myprocessor
->state
== PROCESSOR_RUNNING
) {
1121 remqueue(&pset
->active_queue
, (queue_entry_t
)myprocessor
);
1122 myprocessor
->state
= PROCESSOR_IDLE
;
1124 if (myprocessor
== master_processor
)
1125 enqueue_tail(&pset
->idle_queue
, (queue_entry_t
)myprocessor
);
1127 enqueue_head(&pset
->idle_queue
, (queue_entry_t
)myprocessor
);
1131 simple_unlock(&pset
->sched_lock
);
1133 thread
= myprocessor
->idle_thread
;
1141 * Stop running the current thread and start running the new thread.
1142 * If continuation is non-zero, and the current thread is blocked,
1143 * then it will resume by executing continuation on a new stack.
1144 * Returns TRUE if the hand-off succeeds.
1150 __current_thread(void)
1152 return (current_thread());
1157 register thread_t old_thread
,
1158 register thread_t new_thread
,
1160 thread_continue_t old_cont
)
1162 thread_continue_t new_cont
;
1163 processor_t processor
;
1165 if (get_preemption_level() != 0)
1166 panic("thread_invoke: preemption_level %d\n",
1167 get_preemption_level());
1170 * Mark thread interruptible.
1172 thread_lock(new_thread
);
1173 new_thread
->state
&= ~TH_UNINT
;
1175 assert(thread_runnable(new_thread
));
1177 assert(old_thread
->continuation
== NULL
);
1180 * Allow time constraint threads to hang onto
1183 if ( (old_thread
->sched_mode
& TH_MODE_REALTIME
) &&
1184 !old_thread
->stack_privilege
) {
1185 old_thread
->stack_privilege
= old_thread
->kernel_stack
;
1188 if (old_cont
!= NULL
) {
1189 if (new_thread
->state
& TH_STACK_HANDOFF
) {
1191 * If the old thread is using a privileged stack,
1192 * check to see whether we can exchange it with
1193 * that of the new thread.
1195 if ( old_thread
->kernel_stack
== old_thread
->stack_privilege
&&
1196 !new_thread
->stack_privilege
)
1199 new_thread
->state
&= ~TH_STACK_HANDOFF
;
1200 new_cont
= new_thread
->continuation
;
1201 new_thread
->continuation
= NULL
;
1204 * Set up ast context of new thread and switch
1207 processor
= current_processor();
1208 new_thread
->last_processor
= processor
;
1209 processor
->current_pri
= new_thread
->sched_pri
;
1210 ast_context(new_thread
->top_act
, processor
->slot_num
);
1211 timer_switch(&new_thread
->system_timer
);
1212 thread_unlock(new_thread
);
1214 current_task()->csw
++;
1216 old_thread
->reason
= reason
;
1217 old_thread
->continuation
= old_cont
;
1219 _mk_sp_thread_done(old_thread
, new_thread
, processor
);
1221 stack_handoff(old_thread
, new_thread
);
1223 _mk_sp_thread_begin(new_thread
, processor
);
1225 wake_lock(old_thread
);
1226 thread_lock(old_thread
);
1229 * Inline thread_dispatch but
1233 switch (old_thread
->state
& (TH_RUN
|TH_WAIT
|TH_UNINT
|TH_IDLE
)) {
1235 case TH_RUN
| TH_UNINT
:
1238 * Still running, put back
1241 old_thread
->state
|= TH_STACK_HANDOFF
;
1242 _mk_sp_thread_dispatch(old_thread
);
1244 thread_unlock(old_thread
);
1245 wake_unlock(old_thread
);
1248 case TH_RUN
| TH_WAIT
| TH_UNINT
:
1249 case TH_RUN
| TH_WAIT
:
1251 boolean_t reap
, wake
, callblock
;
1256 old_thread
->sleep_stamp
= sched_tick
;
1257 old_thread
->state
|= TH_STACK_HANDOFF
;
1258 old_thread
->state
&= ~TH_RUN
;
1259 hw_atomic_sub(&old_thread
->processor_set
->run_count
, 1);
1260 callblock
= old_thread
->active_callout
;
1261 wake
= old_thread
->wake_active
;
1262 old_thread
->wake_active
= FALSE
;
1263 reap
= (old_thread
->state
& TH_TERMINATE
)? TRUE
: FALSE
;
1265 thread_unlock(old_thread
);
1266 wake_unlock(old_thread
);
1269 call_thread_block();
1272 thread_wakeup((event_t
)&old_thread
->wake_active
);
1275 thread_reaper_enqueue(old_thread
);
1279 case TH_RUN
| TH_IDLE
:
1281 * The idle threads don't go
1284 old_thread
->state
|= TH_STACK_HANDOFF
;
1285 thread_unlock(old_thread
);
1286 wake_unlock(old_thread
);
1290 panic("thread_invoke: state 0x%x\n", old_thread
->state
);
1293 counter_always(c_thread_invoke_hits
++);
1295 if (new_thread
->funnel_state
& TH_FN_REFUNNEL
) {
1296 kern_return_t wait_result
= new_thread
->wait_result
;
1298 new_thread
->funnel_state
= 0;
1299 KERNEL_DEBUG(0x6032428 | DBG_FUNC_NONE
,
1300 new_thread
->funnel_lock
, 2, 0, 0, 0);
1301 funnel_lock(new_thread
->funnel_lock
);
1302 KERNEL_DEBUG(0x6032430 | DBG_FUNC_NONE
,
1303 new_thread
->funnel_lock
, 2, 0, 0, 0);
1304 new_thread
->funnel_state
= TH_FN_OWNED
;
1305 new_thread
->wait_result
= wait_result
;
1310 call_continuation(new_cont
);
1315 if (new_thread
->state
& TH_STACK_ALLOC
) {
1317 * Waiting for a stack
1319 counter_always(c_thread_invoke_misses
++);
1320 thread_unlock(new_thread
);
1324 if (new_thread
== old_thread
) {
1325 /* same thread but with continuation */
1326 counter(++c_thread_invoke_same
);
1327 thread_unlock(new_thread
);
1329 if (new_thread
->funnel_state
& TH_FN_REFUNNEL
) {
1330 kern_return_t wait_result
= new_thread
->wait_result
;
1332 new_thread
->funnel_state
= 0;
1333 KERNEL_DEBUG(0x6032428 | DBG_FUNC_NONE
,
1334 new_thread
->funnel_lock
, 3, 0, 0, 0);
1335 funnel_lock(new_thread
->funnel_lock
);
1336 KERNEL_DEBUG(0x6032430 | DBG_FUNC_NONE
,
1337 new_thread
->funnel_lock
, 3, 0, 0, 0);
1338 new_thread
->funnel_state
= TH_FN_OWNED
;
1339 new_thread
->wait_result
= wait_result
;
1342 call_continuation(old_cont
);
1348 * Check that the new thread has a stack
1350 if (new_thread
->state
& TH_STACK_HANDOFF
) {
1352 if (!stack_alloc_try(new_thread
, thread_continue
)) {
1353 counter_always(c_thread_invoke_misses
++);
1354 thread_swapin(new_thread
);
1358 new_thread
->state
&= ~TH_STACK_HANDOFF
;
1361 if (new_thread
->state
& TH_STACK_ALLOC
) {
1363 * Waiting for a stack
1365 counter_always(c_thread_invoke_misses
++);
1366 thread_unlock(new_thread
);
1370 if (old_thread
== new_thread
) {
1371 counter(++c_thread_invoke_same
);
1372 thread_unlock(new_thread
);
1378 * Set up ast context of new thread and switch to its timer.
1380 processor
= current_processor();
1381 new_thread
->last_processor
= processor
;
1382 processor
->current_pri
= new_thread
->sched_pri
;
1383 ast_context(new_thread
->top_act
, processor
->slot_num
);
1384 timer_switch(&new_thread
->system_timer
);
1385 assert(thread_runnable(new_thread
));
1386 thread_unlock(new_thread
);
1388 counter_always(c_thread_invoke_csw
++);
1389 current_task()->csw
++;
1391 assert(old_thread
->runq
== RUN_QUEUE_NULL
);
1392 old_thread
->reason
= reason
;
1393 old_thread
->continuation
= old_cont
;
1395 _mk_sp_thread_done(old_thread
, new_thread
, processor
);
1398 * switch_context is machine-dependent. It does the
1399 * machine-dependent components of a context-switch, like
1400 * changing address spaces. It updates active_threads.
1402 old_thread
= switch_context(old_thread
, old_cont
, new_thread
);
1404 /* Now on new thread's stack. Set a local variable to refer to it. */
1405 new_thread
= __current_thread();
1406 assert(old_thread
!= new_thread
);
1408 assert(thread_runnable(new_thread
));
1409 _mk_sp_thread_begin(new_thread
, new_thread
->last_processor
);
1412 * We're back. Now old_thread is the thread that resumed
1413 * us, and we have to dispatch it.
1415 thread_dispatch(old_thread
);
1418 if (new_thread
->funnel_state
& TH_FN_REFUNNEL
) {
1419 kern_return_t wait_result
= new_thread
->wait_result
;
1421 new_thread
->funnel_state
= 0;
1422 KERNEL_DEBUG(0x6032428 | DBG_FUNC_NONE
,
1423 new_thread
->funnel_lock
, 3, 0, 0, 0);
1424 funnel_lock(new_thread
->funnel_lock
);
1425 KERNEL_DEBUG(0x6032430 | DBG_FUNC_NONE
,
1426 new_thread
->funnel_lock
, 3, 0, 0, 0);
1427 new_thread
->funnel_state
= TH_FN_OWNED
;
1428 new_thread
->wait_result
= wait_result
;
1431 call_continuation(old_cont
);
1441 * Called when a thread gets a new stack, at splsched();
1445 register thread_t old_thread
)
1447 register thread_t self
= current_thread();
1448 register thread_continue_t continuation
;
1450 continuation
= self
->continuation
;
1451 self
->continuation
= NULL
;
1453 _mk_sp_thread_begin(self
, self
->last_processor
);
1456 * We must dispatch the old thread and then
1457 * call the current thread's continuation.
1458 * There might not be an old thread, if we are
1459 * the first thread to run on this processor.
1461 if (old_thread
!= THREAD_NULL
)
1462 thread_dispatch(old_thread
);
1464 if (self
->funnel_state
& TH_FN_REFUNNEL
) {
1465 kern_return_t wait_result
= self
->wait_result
;
1467 self
->funnel_state
= 0;
1468 KERNEL_DEBUG(0x6032428 | DBG_FUNC_NONE
, self
->funnel_lock
, 4, 0, 0, 0);
1469 funnel_lock(self
->funnel_lock
);
1470 KERNEL_DEBUG(0x6032430 | DBG_FUNC_NONE
, self
->funnel_lock
, 4, 0, 0, 0);
1471 self
->funnel_state
= TH_FN_OWNED
;
1472 self
->wait_result
= wait_result
;
1475 assert(continuation
);
1476 call_continuation(continuation
);
1480 #if MACH_LDEBUG || MACH_KDB
1482 #define THREAD_LOG_SIZE 300
1496 } thread_log
[THREAD_LOG_SIZE
];
1498 int thread_log_index
;
1500 void check_thread_time(long n
);
1503 int check_thread_time_crash
;
1507 check_thread_time(long us
)
1511 if (!check_thread_time_crash
)
1514 temp
= thread_log
[0].stamp
;
1515 cyctm05_diff (&thread_log
[1].stamp
, &thread_log
[0].stamp
, &temp
);
1517 if (temp
.l
>= us
&& thread_log
[1].info
!= 0x49) /* HACK!!! */
1518 panic ("check_thread_time");
1523 log_thread_action(char * action
, long info1
, long info2
, long info3
)
1527 static unsigned int tstamp
;
1531 for (i
= THREAD_LOG_SIZE
-1; i
> 0; i
--) {
1532 thread_log
[i
] = thread_log
[i
-1];
1535 thread_log
[0].stamp
.h
= 0;
1536 thread_log
[0].stamp
.l
= tstamp
++;
1537 thread_log
[0].thread
= current_thread();
1538 thread_log
[0].info1
= info1
;
1539 thread_log
[0].info2
= info2
;
1540 thread_log
[0].info3
= info3
;
1541 thread_log
[0].action
= action
;
1542 /* strcpy (&thread_log[0].action[0], action);*/
1546 #endif /* MACH_LDEBUG || MACH_KDB */
1549 #include <ddb/db_output.h>
1550 void db_show_thread_log(void);
1553 db_show_thread_log(void)
1557 db_printf ("%s %s %s %s %s %s\n", " Thread ", " Info1 ", " Info2 ",
1558 " Info3 ", " Timestamp ", "Action");
1560 for (i
= 0; i
< THREAD_LOG_SIZE
; i
++) {
1561 db_printf ("%08x %08x %08x %08x %08x/%08x %s\n",
1562 thread_log
[i
].thread
,
1563 thread_log
[i
].info1
,
1564 thread_log
[i
].info2
,
1565 thread_log
[i
].info3
,
1566 thread_log
[i
].stamp
.h
,
1567 thread_log
[i
].stamp
.l
,
1568 thread_log
[i
].action
);
1571 #endif /* MACH_KDB */
1574 * thread_block_reason:
1576 * Block the current thread if a wait has been asserted,
1577 * otherwise unconditionally yield the remainder of the
1578 * current quantum unless reason contains AST_BLOCK.
1580 * If a continuation is specified, then thread_block will
1581 * attempt to discard the thread's kernel stack. When the
1582 * thread resumes, it will execute the continuation function
1583 * on a new kernel stack.
1585 counter(mach_counter_t c_thread_block_calls
= 0;)
1588 thread_block_reason(
1589 thread_continue_t continuation
,
1592 register thread_t thread
= current_thread();
1593 register processor_t myprocessor
;
1594 register thread_t new_thread
;
1597 counter(++c_thread_block_calls
);
1599 check_simple_locks();
1601 machine_clock_assist();
1605 if ((thread
->funnel_state
& TH_FN_OWNED
) && !(reason
& AST_PREEMPT
)) {
1606 thread
->funnel_state
= TH_FN_REFUNNEL
;
1608 0x603242c | DBG_FUNC_NONE
, thread
->funnel_lock
, 2, 0, 0, 0);
1609 funnel_unlock(thread
->funnel_lock
);
1612 myprocessor
= current_processor();
1614 /* If we're explicitly yielding, force a subsequent quantum */
1615 if (reason
& AST_YIELD
)
1616 myprocessor
->slice_quanta
= 0;
1618 /* We're handling all scheduling AST's */
1619 ast_off(AST_SCHEDULING
);
1621 thread_lock(thread
);
1622 new_thread
= thread_select(myprocessor
);
1623 assert(new_thread
&& thread_runnable(new_thread
));
1624 thread_unlock(thread
);
1625 while (!thread_invoke(thread
, new_thread
, reason
, continuation
)) {
1626 thread_lock(thread
);
1627 new_thread
= thread_select(myprocessor
);
1628 assert(new_thread
&& thread_runnable(new_thread
));
1629 thread_unlock(thread
);
1632 if (thread
->funnel_state
& TH_FN_REFUNNEL
) {
1633 kern_return_t wait_result
= thread
->wait_result
;
1635 thread
->funnel_state
= 0;
1637 0x6032428 | DBG_FUNC_NONE
, thread
->funnel_lock
, 5, 0, 0, 0);
1638 funnel_lock(thread
->funnel_lock
);
1640 0x6032430 | DBG_FUNC_NONE
, thread
->funnel_lock
, 5, 0, 0, 0);
1641 thread
->funnel_state
= TH_FN_OWNED
;
1642 thread
->wait_result
= wait_result
;
1647 return (thread
->wait_result
);
1653 * Block the current thread if a wait has been asserted.
1657 thread_continue_t continuation
)
1659 return thread_block_reason(continuation
, AST_NONE
);
1665 * Switch directly from the current (old) thread to the
1666 * specified thread, handing off our quantum if possible.
1668 * New thread must be runnable, and not on a run queue.
1675 thread_t old_thread
,
1676 thread_continue_t continuation
,
1677 thread_t new_thread
)
1679 ast_t handoff
= AST_HANDOFF
;
1681 assert(old_thread
== current_thread());
1683 machine_clock_assist();
1685 if (old_thread
->funnel_state
& TH_FN_OWNED
) {
1686 old_thread
->funnel_state
= TH_FN_REFUNNEL
;
1688 0x603242c | DBG_FUNC_NONE
, old_thread
->funnel_lock
, 3, 0, 0, 0);
1689 funnel_unlock(old_thread
->funnel_lock
);
1692 while (!thread_invoke(old_thread
, new_thread
, handoff
, continuation
)) {
1693 register processor_t myprocessor
= current_processor();
1695 thread_lock(old_thread
);
1696 new_thread
= thread_select(myprocessor
);
1697 thread_unlock(old_thread
);
1701 /* if we fell thru */
1702 if (old_thread
->funnel_state
& TH_FN_REFUNNEL
) {
1703 kern_return_t wait_result
= old_thread
->wait_result
;
1705 old_thread
->funnel_state
= 0;
1707 0x6032428 | DBG_FUNC_NONE
, old_thread
->funnel_lock
, 6, 0, 0, 0);
1708 funnel_lock(old_thread
->funnel_lock
);
1710 0x6032430 | DBG_FUNC_NONE
, old_thread
->funnel_lock
, 6, 0, 0, 0);
1711 old_thread
->funnel_state
= TH_FN_OWNED
;
1712 old_thread
->wait_result
= wait_result
;
1715 return (old_thread
->wait_result
);
1719 * Dispatches a running thread that is not on a runq.
1720 * Called at splsched.
1724 register thread_t thread
)
1727 thread_lock(thread
);
1730 * If we are discarding the thread's stack, we must do it
1731 * before the thread has a chance to run.
1734 if (thread
->continuation
!= NULL
) {
1735 assert((thread
->state
& TH_STACK_STATE
) == 0);
1736 thread
->state
|= TH_STACK_HANDOFF
;
1741 switch (thread
->state
& (TH_RUN
|TH_WAIT
|TH_UNINT
|TH_IDLE
)) {
1743 case TH_RUN
| TH_UNINT
:
1746 * No reason to stop. Put back on a run queue.
1748 _mk_sp_thread_dispatch(thread
);
1751 case TH_RUN
| TH_WAIT
| TH_UNINT
:
1752 case TH_RUN
| TH_WAIT
:
1754 boolean_t reap
, wake
, callblock
;
1759 thread
->sleep_stamp
= sched_tick
;
1760 thread
->state
&= ~TH_RUN
;
1761 hw_atomic_sub(&thread
->processor_set
->run_count
, 1);
1762 callblock
= thread
->active_callout
;
1763 wake
= thread
->wake_active
;
1764 thread
->wake_active
= FALSE
;
1765 reap
= (thread
->state
& TH_TERMINATE
)? TRUE
: FALSE
;
1767 thread_unlock(thread
);
1768 wake_unlock(thread
);
1771 call_thread_block();
1774 thread_wakeup((event_t
)&thread
->wake_active
);
1777 thread_reaper_enqueue(thread
);
1782 case TH_RUN
| TH_IDLE
:
1784 * The idle threads don't go
1790 panic("thread_dispatch: bad thread state 0x%x\n", thread
->state
);
1793 thread_unlock(thread
);
1794 wake_unlock(thread
);
1798 * Enqueue thread on run queue. Thread must be locked,
1799 * and not already be on a run queue. Returns TRUE iff
1800 * the particular queue level was empty beforehand.
1804 register run_queue_t rq
,
1805 register thread_t thread
,
1808 register int whichq
= thread
->sched_pri
;
1809 register queue_t queue
= &rq
->queues
[whichq
];
1810 boolean_t result
= FALSE
;
1812 assert(whichq
>= MINPRI
&& whichq
<= MAXPRI
);
1814 simple_lock(&rq
->lock
);
1815 assert(thread
->runq
== RUN_QUEUE_NULL
);
1816 if (queue_empty(queue
)) {
1817 enqueue_tail(queue
, (queue_entry_t
)thread
);
1819 setbit(MAXPRI
- whichq
, rq
->bitmap
);
1820 if (whichq
> rq
->highq
)
1826 enqueue_tail(queue
, (queue_entry_t
)thread
);
1828 enqueue_head(queue
, (queue_entry_t
)thread
);
1831 if (thread
->sched_mode
& TH_MODE_PREEMPT
)
1835 thread_check(thread
, rq
);
1837 simple_unlock(&rq
->lock
);
1843 uint32_t pset_idle_last
,
1856 * Dispatch thread for execution, directly onto an idle
1857 * processor if possible. Else put on appropriate run
1858 * queue. (local if bound, else processor set)
1860 * Thread must be locked.
1862 * The tail parameter indicates the proper placement of
1863 * the thread on a run queue.
1867 register thread_t new_thread
,
1870 register processor_t processor
;
1871 register processor_set_t pset
;
1872 register thread_t thread
;
1873 boolean_t try_preempt
= FALSE
;
1874 ast_t preempt
= AST_BLOCK
;
1876 assert(thread_runnable(new_thread
));
1879 * Update priority if needed.
1881 if (new_thread
->sched_stamp
!= sched_tick
)
1882 update_priority(new_thread
);
1885 * Check for urgent preemption.
1887 if (new_thread
->sched_mode
& TH_MODE_PREEMPT
)
1888 preempt
|= AST_URGENT
;
1890 assert(new_thread
->runq
== RUN_QUEUE_NULL
);
1892 if ((processor
= new_thread
->bound_processor
) == PROCESSOR_NULL
) {
1894 * First try to dispatch on
1895 * the last processor.
1897 pset
= new_thread
->processor_set
;
1898 processor
= new_thread
->last_processor
;
1899 if ( pset
->processor_count
> 1 &&
1900 processor
!= PROCESSOR_NULL
&&
1901 processor
->state
== PROCESSOR_IDLE
) {
1902 simple_lock(&processor
->lock
);
1903 simple_lock(&pset
->sched_lock
);
1904 if ( processor
->processor_set
== pset
&&
1905 processor
->state
== PROCESSOR_IDLE
) {
1906 remqueue(&pset
->idle_queue
, (queue_entry_t
)processor
);
1908 processor
->next_thread
= new_thread
;
1909 processor
->state
= PROCESSOR_DISPATCHING
;
1910 simple_unlock(&pset
->sched_lock
);
1911 simple_unlock(&processor
->lock
);
1912 if (processor
!= current_processor())
1913 machine_signal_idle(processor
);
1914 dispatch_counts
.pset_idle_last
++;
1917 simple_unlock(&processor
->lock
);
1920 simple_lock(&pset
->sched_lock
);
1923 * Next pick any idle processor
1924 * in the processor set.
1926 if (pset
->idle_count
> 0) {
1927 processor
= (processor_t
)dequeue_head(&pset
->idle_queue
);
1929 processor
->next_thread
= new_thread
;
1930 processor
->state
= PROCESSOR_DISPATCHING
;
1931 simple_unlock(&pset
->sched_lock
);
1932 if (processor
!= current_processor())
1933 machine_signal_idle(processor
);
1934 dispatch_counts
.pset_idle_any
++;
1939 * Place thread on run queue.
1941 if (run_queue_enqueue(&pset
->runq
, new_thread
, tail
))
1945 * Update the timesharing quanta.
1947 pset_quanta_update(pset
);
1952 processor
= current_processor();
1953 thread
= processor
->cpu_data
->active_thread
;
1956 * First try the current processor
1957 * if it is a member of the correct
1960 if ( pset
== processor
->processor_set
&&
1961 csw_needed(thread
, processor
) ) {
1962 simple_unlock(&pset
->sched_lock
);
1965 dispatch_counts
.pset_self
++;
1970 * If that failed and we have other
1971 * processors available keep trying.
1973 if ( pset
->processor_count
> 1 ||
1974 pset
!= processor
->processor_set
) {
1975 queue_t active
= &pset
->active_queue
;
1976 processor_t myprocessor
, lastprocessor
;
1980 * Next try the last processor
1983 myprocessor
= processor
;
1984 processor
= new_thread
->last_processor
;
1985 if ( processor
!= myprocessor
&&
1986 processor
!= PROCESSOR_NULL
&&
1987 processor
->processor_set
== pset
&&
1988 processor
->state
== PROCESSOR_RUNNING
&&
1989 new_thread
->sched_pri
> processor
->current_pri
) {
1990 cause_ast_check(processor
);
1991 simple_unlock(&pset
->sched_lock
);
1992 dispatch_counts
.pset_last
++;
1997 * Lastly, pick any other
1998 * available processor.
2000 lastprocessor
= processor
;
2001 processor
= (processor_t
)queue_first(active
);
2002 while (!queue_end(active
, (queue_entry_t
)processor
)) {
2003 next
= queue_next((queue_entry_t
)processor
);
2005 if ( processor
!= myprocessor
&&
2006 processor
!= lastprocessor
&&
2007 new_thread
->sched_pri
> processor
->current_pri
) {
2008 if (!queue_end(active
, next
)) {
2009 remqueue(active
, (queue_entry_t
)processor
);
2010 enqueue_tail(active
, (queue_entry_t
)processor
);
2012 cause_ast_check(processor
);
2013 simple_unlock(&pset
->sched_lock
);
2014 dispatch_counts
.pset_other
++;
2018 processor
= (processor_t
)next
;
2023 simple_unlock(&pset
->sched_lock
);
2027 * Bound, can only run on bound processor. Have to lock
2028 * processor here because it may not be the current one.
2030 if (processor
->state
== PROCESSOR_IDLE
) {
2031 simple_lock(&processor
->lock
);
2032 pset
= processor
->processor_set
;
2033 simple_lock(&pset
->sched_lock
);
2034 if (processor
->state
== PROCESSOR_IDLE
) {
2035 remqueue(&pset
->idle_queue
, (queue_entry_t
)processor
);
2037 processor
->next_thread
= new_thread
;
2038 processor
->state
= PROCESSOR_DISPATCHING
;
2039 simple_unlock(&pset
->sched_lock
);
2040 simple_unlock(&processor
->lock
);
2041 if (processor
!= current_processor())
2042 machine_signal_idle(processor
);
2043 dispatch_counts
.bound_idle
++;
2046 simple_unlock(&pset
->sched_lock
);
2047 simple_unlock(&processor
->lock
);
2050 if (run_queue_enqueue(&processor
->runq
, new_thread
, tail
))
2053 if (processor
== current_processor()) {
2055 thread
= processor
->cpu_data
->active_thread
;
2056 if (csw_needed(thread
, processor
)) {
2058 dispatch_counts
.bound_self
++;
2064 if ( processor
->state
== PROCESSOR_RUNNING
&&
2065 new_thread
->sched_pri
> processor
->current_pri
) {
2066 cause_ast_check(processor
);
2067 dispatch_counts
.bound_other
++;
2072 if (processor
->state
== PROCESSOR_IDLE
) {
2073 machine_signal_idle(processor
);
2074 dispatch_counts
.bound_idle
++;
2081 * Called at splsched by a thread on itself.
2086 processor_t processor
)
2088 int current_pri
= thread
->sched_pri
;
2089 ast_t result
= AST_NONE
;
2092 if (first_quantum(processor
)) {
2093 runq
= &processor
->processor_set
->runq
;
2094 if (runq
->highq
> current_pri
) {
2095 if (runq
->urgency
> 0)
2096 return (AST_BLOCK
| AST_URGENT
);
2098 result
|= AST_BLOCK
;
2101 runq
= &processor
->runq
;
2102 if (runq
->highq
> current_pri
) {
2103 if (runq
->urgency
> 0)
2104 return (AST_BLOCK
| AST_URGENT
);
2106 result
|= AST_BLOCK
;
2110 runq
= &processor
->processor_set
->runq
;
2111 if (runq
->highq
>= current_pri
) {
2112 if (runq
->urgency
> 0)
2113 return (AST_BLOCK
| AST_URGENT
);
2115 result
|= AST_BLOCK
;
2118 runq
= &processor
->runq
;
2119 if (runq
->highq
>= current_pri
) {
2120 if (runq
->urgency
> 0)
2121 return (AST_BLOCK
| AST_URGENT
);
2123 result
|= AST_BLOCK
;
2127 if (result
!= AST_NONE
)
2130 if (thread
->state
& TH_SUSP
)
2131 result
|= AST_BLOCK
;
2139 * Set the current scheduled priority of the specified thread.
2140 * This may cause the thread to change queues.
2142 * The thread *must* be locked by the caller.
2149 register struct run_queue
*rq
= rem_runq(thread
);
2151 if ( !(thread
->sched_mode
& TH_MODE_TIMESHARE
) &&
2152 (priority
>= BASEPRI_PREEMPT
||
2153 (thread
->task_priority
< MINPRI_KERNEL
&&
2154 thread
->task_priority
>= BASEPRI_BACKGROUND
&&
2155 priority
> thread
->task_priority
) ||
2156 (thread
->sched_mode
& TH_MODE_FORCEDPREEMPT
) ) )
2157 thread
->sched_mode
|= TH_MODE_PREEMPT
;
2159 thread
->sched_mode
&= ~TH_MODE_PREEMPT
;
2161 thread
->sched_pri
= priority
;
2162 if (rq
!= RUN_QUEUE_NULL
)
2163 thread_setrun(thread
, TAIL_Q
);
2165 if ((thread
->state
& (TH_RUN
|TH_WAIT
)) == TH_RUN
) {
2166 processor_t processor
= thread
->last_processor
;
2168 if (thread
== current_thread()) {
2169 ast_t preempt
= csw_check(thread
, processor
);
2171 if (preempt
!= AST_NONE
)
2173 processor
->current_pri
= priority
;
2176 if ( processor
!= PROCESSOR_NULL
&&
2177 processor
->cpu_data
->active_thread
== thread
)
2178 cause_ast_check(processor
);
2185 * Remove a thread from its run queue.
2186 * The run queue that the process was on is returned
2187 * (or RUN_QUEUE_NULL if not on a run queue). Thread *must* be locked
2188 * before calling this routine. Unusual locking protocol on runq
2189 * field in thread structure makes this code interesting; see thread.h.
2195 register struct run_queue
*rq
;
2199 * If rq is RUN_QUEUE_NULL, the thread will stay out of the
2200 * run_queues because the caller locked the thread. Otherwise
2201 * the thread is on a runq, but could leave.
2203 if (rq
!= RUN_QUEUE_NULL
) {
2204 simple_lock(&rq
->lock
);
2205 if (rq
== thread
->runq
) {
2207 * Thread is in a runq and we have a lock on
2211 thread_check(thread
, rq
);
2213 remqueue(&rq
->queues
[0], (queue_entry_t
)thread
);
2215 if (thread
->sched_mode
& TH_MODE_PREEMPT
)
2217 assert(rq
->urgency
>= 0);
2219 if (queue_empty(rq
->queues
+ thread
->sched_pri
)) {
2220 /* update run queue status */
2221 if (thread
->sched_pri
!= IDLEPRI
)
2222 clrbit(MAXPRI
- thread
->sched_pri
, rq
->bitmap
);
2223 rq
->highq
= MAXPRI
- ffsbit(rq
->bitmap
);
2225 thread
->runq
= RUN_QUEUE_NULL
;
2226 simple_unlock(&rq
->lock
);
2230 * The thread left the runq before we could
2231 * lock the runq. It is not on a runq now, and
2232 * can't move again because this routine's
2233 * caller locked the thread.
2235 assert(thread
->runq
== RUN_QUEUE_NULL
);
2236 simple_unlock(&rq
->lock
);
2237 rq
= RUN_QUEUE_NULL
;
2247 * Choose a thread to execute. The thread chosen is removed
2248 * from its run queue. Note that this requires only that the runq
2252 * Check processor runq first; if anything found, run it.
2253 * Else check pset runq; if nothing found, return idle thread.
2255 * Second line of strategy is implemented by choose_pset_thread.
2257 * Called with both the local & pset run queues locked, returned
2262 processor_t myprocessor
)
2266 register run_queue_t runq
;
2267 processor_set_t pset
;
2269 runq
= &myprocessor
->runq
;
2270 pset
= myprocessor
->processor_set
;
2272 if (runq
->count
> 0 && runq
->highq
>= pset
->runq
.highq
) {
2273 simple_unlock(&pset
->runq
.lock
);
2274 q
= runq
->queues
+ runq
->highq
;
2276 if (!queue_empty(q
)) {
2277 #endif /*MACH_ASSERT*/
2278 thread
= (thread_t
)q
->next
;
2279 ((queue_entry_t
)thread
)->next
->prev
= q
;
2280 q
->next
= ((queue_entry_t
)thread
)->next
;
2281 thread
->runq
= RUN_QUEUE_NULL
;
2283 if (thread
->sched_mode
& TH_MODE_PREEMPT
)
2285 assert(runq
->urgency
>= 0);
2286 if (queue_empty(q
)) {
2287 if (runq
->highq
!= IDLEPRI
)
2288 clrbit(MAXPRI
- runq
->highq
, runq
->bitmap
);
2289 runq
->highq
= MAXPRI
- ffsbit(runq
->bitmap
);
2291 simple_unlock(&runq
->lock
);
2295 panic("choose_thread");
2296 #endif /*MACH_ASSERT*/
2299 simple_unlock(&myprocessor
->runq
.lock
);
2301 return (choose_pset_thread(myprocessor
, pset
));
2305 * choose_pset_thread: choose a thread from processor_set runq or
2306 * set processor idle and choose its idle thread.
2308 * This routine chooses and removes a thread from the runq if there
2309 * is one (and returns it), else it sets the processor idle and
2310 * returns its idle thread.
2312 * Called with both local & pset run queues locked, returned
2317 register processor_t myprocessor
,
2318 processor_set_t pset
)
2320 register run_queue_t runq
;
2321 register thread_t thread
;
2325 if (runq
->count
> 0) {
2326 q
= runq
->queues
+ runq
->highq
;
2328 if (!queue_empty(q
)) {
2329 #endif /*MACH_ASSERT*/
2330 thread
= (thread_t
)q
->next
;
2331 ((queue_entry_t
)thread
)->next
->prev
= q
;
2332 q
->next
= ((queue_entry_t
)thread
)->next
;
2333 thread
->runq
= RUN_QUEUE_NULL
;
2335 if (thread
->sched_mode
& TH_MODE_PREEMPT
)
2337 assert(runq
->urgency
>= 0);
2338 if (queue_empty(q
)) {
2339 if (runq
->highq
!= IDLEPRI
)
2340 clrbit(MAXPRI
- runq
->highq
, runq
->bitmap
);
2341 runq
->highq
= MAXPRI
- ffsbit(runq
->bitmap
);
2343 pset_quanta_update(pset
);
2344 simple_unlock(&runq
->lock
);
2348 panic("choose_pset_thread");
2349 #endif /*MACH_ASSERT*/
2352 simple_unlock(&runq
->lock
);
2355 * Nothing is runnable, so set this processor idle if it
2356 * was running. If it was in an assignment or shutdown,
2357 * leave it alone. Return its idle thread.
2359 simple_lock(&pset
->sched_lock
);
2360 if (myprocessor
->state
== PROCESSOR_RUNNING
) {
2361 remqueue(&pset
->active_queue
, (queue_entry_t
)myprocessor
);
2362 myprocessor
->state
= PROCESSOR_IDLE
;
2364 if (myprocessor
== master_processor
)
2365 enqueue_tail(&pset
->idle_queue
, (queue_entry_t
)myprocessor
);
2367 enqueue_head(&pset
->idle_queue
, (queue_entry_t
)myprocessor
);
2371 simple_unlock(&pset
->sched_lock
);
2373 return (myprocessor
->idle_thread
);
2377 * no_dispatch_count counts number of times processors go non-idle
2378 * without being dispatched. This should be very rare.
2380 int no_dispatch_count
= 0;
2383 * This is the idle thread, which just looks for other threads
2387 idle_thread_continue(void)
2389 register processor_t myprocessor
;
2390 register volatile thread_t
*threadp
;
2391 register volatile int *gcount
;
2392 register volatile int *lcount
;
2393 register thread_t new_thread
;
2395 register processor_set_t pset
;
2398 mycpu
= cpu_number();
2399 myprocessor
= cpu_to_processor(mycpu
);
2400 threadp
= (volatile thread_t
*) &myprocessor
->next_thread
;
2401 lcount
= (volatile int *) &myprocessor
->runq
.count
;
2404 gcount
= (volatile int *)&myprocessor
->processor_set
->runq
.count
;
2407 while ( (*threadp
== (volatile thread_t
)THREAD_NULL
) &&
2408 (*gcount
== 0) && (*lcount
== 0) ) {
2410 /* check for ASTs while we wait */
2411 if (need_ast
[mycpu
] &~ ( AST_SCHEDULING
| AST_BSD
)) {
2412 /* don't allow scheduling ASTs */
2413 need_ast
[mycpu
] &= ~( AST_SCHEDULING
| AST_BSD
);
2414 ast_taken(AST_ALL
, TRUE
); /* back at spllo */
2422 machine_clock_assist();
2428 * This is not a switch statement to avoid the
2429 * bounds checking code in the common case.
2431 pset
= myprocessor
->processor_set
;
2432 simple_lock(&pset
->sched_lock
);
2434 state
= myprocessor
->state
;
2435 if (state
== PROCESSOR_DISPATCHING
) {
2437 * Commmon case -- cpu dispatched.
2439 new_thread
= *threadp
;
2440 *threadp
= (volatile thread_t
) THREAD_NULL
;
2441 myprocessor
->state
= PROCESSOR_RUNNING
;
2442 enqueue_tail(&pset
->active_queue
, (queue_entry_t
)myprocessor
);
2443 simple_unlock(&pset
->sched_lock
);
2445 if ( myprocessor
->runq
.highq
> new_thread
->sched_pri
||
2446 pset
->runq
.highq
> new_thread
->sched_pri
) {
2447 thread_lock(new_thread
);
2448 thread_setrun(new_thread
, HEAD_Q
);
2449 thread_unlock(new_thread
);
2451 counter(c_idle_thread_block
++);
2452 thread_block(idle_thread_continue
);
2456 counter(c_idle_thread_handoff
++);
2457 thread_run(myprocessor
->idle_thread
,
2458 idle_thread_continue
, new_thread
);
2463 if (state
== PROCESSOR_IDLE
) {
2464 if (myprocessor
->state
!= PROCESSOR_IDLE
) {
2466 * Something happened, try again.
2471 * Processor was not dispatched (Rare).
2472 * Set it running again.
2474 no_dispatch_count
++;
2476 remqueue(&pset
->idle_queue
, (queue_entry_t
)myprocessor
);
2477 myprocessor
->state
= PROCESSOR_RUNNING
;
2478 enqueue_tail(&pset
->active_queue
, (queue_entry_t
)myprocessor
);
2479 simple_unlock(&pset
->sched_lock
);
2481 counter(c_idle_thread_block
++);
2482 thread_block(idle_thread_continue
);
2486 if ( state
== PROCESSOR_ASSIGN
||
2487 state
== PROCESSOR_SHUTDOWN
) {
2489 * Changing processor sets, or going off-line.
2490 * Release next_thread if there is one. Actual
2491 * thread to run is on a runq.
2493 if ((new_thread
= (thread_t
)*threadp
) != THREAD_NULL
) {
2494 *threadp
= (volatile thread_t
) THREAD_NULL
;
2495 simple_unlock(&pset
->sched_lock
);
2497 thread_lock(new_thread
);
2498 thread_setrun(new_thread
, TAIL_Q
);
2499 thread_unlock(new_thread
);
2502 simple_unlock(&pset
->sched_lock
);
2504 counter(c_idle_thread_block
++);
2505 thread_block(idle_thread_continue
);
2509 simple_unlock(&pset
->sched_lock
);
2511 panic("idle_thread: bad processor state %d\n", cpu_state(mycpu
));
2521 thread_t self
= current_thread();
2524 stack_privilege(self
);
2528 self
->priority
= IDLEPRI
;
2529 set_sched_pri(self
, self
->priority
);
2530 thread_unlock(self
);
2533 counter(c_idle_thread_block
++);
2534 thread_block(idle_thread_continue
);
2538 static uint64_t sched_tick_interval
, sched_tick_deadline
;
2540 void sched_tick_thread(void);
2543 sched_tick_init(void)
2545 kernel_thread_with_priority(
2546 kernel_task
, MAXPRI_STANDARD
,
2547 sched_tick_thread
, TRUE
, TRUE
);
2553 * Update the priorities of all threads periodically.
2556 sched_tick_thread_continue(void)
2561 #endif /* SIMPLE_CLOCK */
2563 clock_get_uptime(&abstime
);
2565 sched_tick
++; /* age usage one more time */
2568 * Compensate for clock drift. sched_usec is an
2569 * exponential average of the number of microseconds in
2570 * a second. It decays in the same fashion as cpu_usage.
2572 new_usec
= sched_usec_elapsed();
2573 sched_usec
= (5*sched_usec
+ 3*new_usec
)/8;
2574 #endif /* SIMPLE_CLOCK */
2577 * Compute the scheduler load factors.
2579 compute_mach_factor();
2582 * Scan the run queues for runnable threads that need to
2583 * have their priorities recalculated.
2587 clock_deadline_for_periodic_event(sched_tick_interval
, abstime
,
2588 &sched_tick_deadline
);
2590 assert_wait((event_t
)sched_tick_thread_continue
, THREAD_INTERRUPTIBLE
);
2591 thread_set_timer_deadline(sched_tick_deadline
);
2592 thread_block(sched_tick_thread_continue
);
2597 sched_tick_thread(void)
2599 thread_t self
= current_thread();
2603 stack_privilege(self
);
2605 rate
= (1000 >> SCHED_TICK_SHIFT
);
2606 clock_interval_to_absolutetime_interval(rate
, USEC_PER_SEC
,
2607 &sched_tick_interval
);
2608 clock_get_uptime(&sched_tick_deadline
);
2610 thread_block(sched_tick_thread_continue
);
2614 #define MAX_STUCK_THREADS 128
2617 * do_thread_scan: scan for stuck threads. A thread is stuck if
2618 * it is runnable but its priority is so low that it has not
2619 * run for several seconds. Its priority should be higher, but
2620 * won't be until it runs and calls update_priority. The scanner
2621 * finds these threads and does the updates.
2623 * Scanner runs in two passes. Pass one squirrels likely
2624 * thread ids away in an array (takes out references for them).
2625 * Pass two does the priority updates. This is necessary because
2626 * the run queue lock is required for the candidate scan, but
2627 * cannot be held during updates.
2629 * Array length should be enough so that restart isn't necessary,
2630 * but restart logic is included.
2633 thread_t stuck_threads
[MAX_STUCK_THREADS
];
2634 int stuck_count
= 0;
2637 * do_runq_scan is the guts of pass 1. It scans a runq for
2638 * stuck threads. A boolean is returned indicating whether
2639 * a retry is needed.
2646 register thread_t thread
;
2649 boolean_t result
= FALSE
;
2652 simple_lock(&runq
->lock
);
2653 if ((count
= runq
->count
) > 0) {
2654 q
= runq
->queues
+ runq
->highq
;
2656 queue_iterate(q
, thread
, thread_t
, links
) {
2657 if ( !(thread
->state
& (TH_WAIT
|TH_SUSP
)) &&
2658 (thread
->sched_mode
& TH_MODE_TIMESHARE
) ) {
2659 if (thread
->sched_stamp
!= sched_tick
) {
2661 * Stuck, save its id for later.
2663 if (stuck_count
== MAX_STUCK_THREADS
) {
2665 * !@#$% No more room.
2667 simple_unlock(&runq
->lock
);
2674 * Inline version of thread_reference
2675 * XXX - lock ordering problem here:
2676 * thread locks should be taken before runq
2677 * locks: just try and get the thread's locks
2678 * and ignore this thread if we fail, we might
2679 * have better luck next time.
2681 if (thread_lock_try(thread
)) {
2682 thread
->ref_count
++;
2683 thread_unlock(thread
);
2684 stuck_threads
[stuck_count
++] = thread
;
2697 simple_unlock(&runq
->lock
);
2703 boolean_t thread_scan_enabled
= TRUE
;
2706 do_thread_scan(void)
2708 register boolean_t restart_needed
= FALSE
;
2709 register thread_t thread
;
2710 register processor_set_t pset
= &default_pset
;
2711 register processor_t processor
;
2714 if (!thread_scan_enabled
)
2718 restart_needed
= do_runq_scan(&pset
->runq
);
2719 if (!restart_needed
) {
2720 simple_lock(&pset
->processors_lock
);
2721 processor
= (processor_t
)queue_first(&pset
->processors
);
2722 while (!queue_end(&pset
->processors
, (queue_entry_t
)processor
)) {
2723 if (restart_needed
= do_runq_scan(&processor
->runq
))
2726 thread
= processor
->idle_thread
;
2727 if (thread
->sched_stamp
!= sched_tick
) {
2728 if (stuck_count
== MAX_STUCK_THREADS
) {
2729 restart_needed
= TRUE
;
2733 stuck_threads
[stuck_count
++] = thread
;
2736 processor
= (processor_t
)queue_next(&processor
->processors
);
2738 simple_unlock(&pset
->processors_lock
);
2742 * Ok, we now have a collection of candidates -- fix them.
2744 while (stuck_count
> 0) {
2745 thread
= stuck_threads
[--stuck_count
];
2746 stuck_threads
[stuck_count
] = THREAD_NULL
;
2748 thread_lock(thread
);
2749 if ( (thread
->sched_mode
& TH_MODE_TIMESHARE
) ||
2750 (thread
->state
& TH_IDLE
) ) {
2751 if ( !(thread
->state
& (TH_WAIT
|TH_SUSP
)) &&
2752 thread
->sched_stamp
!= sched_tick
)
2753 update_priority(thread
);
2755 thread_unlock(thread
);
2757 if (!(thread
->state
& TH_IDLE
))
2758 thread_deallocate(thread
);
2764 } while (restart_needed
);
2768 * Just in case someone doesn't use the macro
2770 #undef thread_wakeup
2779 thread_wakeup_with_result(x
, THREAD_AWAKENED
);
2789 return ((thread
->state
& (TH_RUN
|TH_WAIT
)) == TH_RUN
);
2796 printf("processor_set: %08x\n",ps
);
2797 printf("idle_queue: %08x %08x, idle_count: 0x%x\n",
2798 ps
->idle_queue
.next
,ps
->idle_queue
.prev
,ps
->idle_count
);
2799 printf("processors: %08x %08x, processor_count: 0x%x\n",
2800 ps
->processors
.next
,ps
->processors
.prev
,ps
->processor_count
);
2801 printf("tasks: %08x %08x, task_count: 0x%x\n",
2802 ps
->tasks
.next
,ps
->tasks
.prev
,ps
->task_count
);
2803 printf("threads: %08x %08x, thread_count: 0x%x\n",
2804 ps
->threads
.next
,ps
->threads
.prev
,ps
->thread_count
);
2805 printf("ref_count: 0x%x, active: %x\n",
2806 ps
->ref_count
,ps
->active
);
2807 printf("pset_self: %08x, pset_name_self: %08x\n",ps
->pset_self
, ps
->pset_name_self
);
2808 printf("set_quanta: 0x%x\n", ps
->set_quanta
);
2811 #define processor_state(s) (((s)>PROCESSOR_SHUTDOWN)?"*unknown*":states[s])
2817 char *states
[]={"OFF_LINE","RUNNING","IDLE","DISPATCHING",
2818 "ASSIGN","SHUTDOWN"};
2820 printf("processor: %08x\n",p
);
2821 printf("processor_queue: %08x %08x\n",
2822 p
->processor_queue
.next
,p
->processor_queue
.prev
);
2823 printf("state: %8s, next_thread: %08x, idle_thread: %08x\n",
2824 processor_state(p
->state
), p
->next_thread
, p
->idle_thread
);
2825 printf("slice_quanta: %x\n", p
->slice_quanta
);
2826 printf("processor_set: %08x, processor_set_next: %08x\n",
2827 p
->processor_set
, p
->processor_set_next
);
2828 printf("processors: %08x %08x\n", p
->processors
.next
,p
->processors
.prev
);
2829 printf("processor_self: %08x, slot_num: 0x%x\n", p
->processor_self
, p
->slot_num
);
2833 dump_run_queue_struct(
2839 for( i
=0; i
< NRQS
; ) {
2842 printf("%6s",(i
==0)?"runq:":"");
2843 for( j
=0; (j
<8) && (i
< NRQS
); j
++,i
++ ) {
2844 if( rq
->queues
[i
].next
== &rq
->queues
[i
] )
2845 printf( " --------");
2847 printf(" %08x",rq
->queues
[i
].next
);
2851 for( i
=0; i
< NRQBM
; ) {
2852 register unsigned int mask
;
2859 *d
++ = ((rq
->bitmap
[i
]&mask
)?'r':'e');
2863 printf("%8s%s\n",((i
==0)?"bitmap:":""),dump_buf
);
2866 printf("highq: 0x%x, count: %u\n", rq
->highq
, rq
->count
);
2873 register queue_t q1
;
2875 register queue_entry_t e
;
2878 for (i
= 0; i
< NRQS
; i
++) {
2879 if (q1
->next
!= q1
) {
2883 for (t_cnt
=0, e
= q1
->next
; e
!= q1
; e
= e
->next
) {
2884 printf("\t0x%08x",e
);
2885 if( (t_cnt
= ++t_cnt%4
) == 0 )
2892 printf("[%u]\t<empty>\n",i);
2903 register queue_t q1
;
2905 register queue_entry_t e
;
2911 for (i
= MAXPRI
; i
>= 0; i
--) {
2912 if (q1
->next
== q1
) {
2913 if (q1
->prev
!= q1
) {
2914 panic("checkrq: empty at %s", msg
);
2921 for (e
= q1
->next
; e
!= q1
; e
= e
->next
) {
2923 if (e
->next
->prev
!= e
)
2924 panic("checkrq-2 at %s", msg
);
2925 if (e
->prev
->next
!= e
)
2926 panic("checkrq-3 at %s", msg
);
2932 panic("checkrq: count wrong at %s", msg
);
2933 if (rq
->count
!= 0 && highq
> rq
->highq
)
2934 panic("checkrq: highq wrong at %s", msg
);
2939 register thread_t thread
,
2940 register run_queue_t rq
)
2942 register int whichq
= thread
->sched_pri
;
2943 register queue_entry_t queue
, entry
;
2945 if (whichq
< MINPRI
|| whichq
> MAXPRI
)
2946 panic("thread_check: bad pri");
2948 queue
= &rq
->queues
[whichq
];
2949 entry
= queue_first(queue
);
2950 while (!queue_end(queue
, entry
)) {
2951 if (entry
== (queue_entry_t
)thread
)
2954 entry
= queue_next(entry
);
2957 panic("thread_check: not found");
2963 #include <ddb/db_output.h>
2964 #define printf kdbprintf
2965 extern int db_indent
;
2966 void db_sched(void);
2971 iprintf("Scheduling Statistics:\n");
2973 iprintf("Thread invocations: csw %d same %d\n",
2974 c_thread_invoke_csw
, c_thread_invoke_same
);
2976 iprintf("Thread block: calls %d\n",
2977 c_thread_block_calls
);
2978 iprintf("Idle thread:\n\thandoff %d block %d no_dispatch %d\n",
2979 c_idle_thread_handoff
,
2980 c_idle_thread_block
, no_dispatch_count
);
2981 iprintf("Sched thread blocks: %d\n", c_sched_thread_block
);
2982 #endif /* MACH_COUNTERS */
2985 #endif /* MACH_KDB */