]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/kern/priority.c
xnu-1699.22.73.tar.gz
[apple/xnu.git] / osfmk / kern / priority.c
index 9ff146229eb2bc79bafdfa5f5da50cb18da95102..74b90dfa8d41418faa9beee90d17b4cae3dce471 100644 (file)
@@ -1,23 +1,29 @@
 /*
- * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
  *
- * @APPLE_LICENSE_HEADER_START@
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  * 
- * The contents of this file constitute Original Code as defined in and
- * are subject to the Apple Public Source License Version 1.1 (the
- * "License").  You may not use this file except in compliance with the
- * License.  Please obtain a copy of the License at
- * http://www.apple.com/publicsource and read it before using this file.
+ * This file contains Original Code and/or Modifications of Original Code
+ * as defined in and that are subject to the Apple Public Source License
+ * Version 2.0 (the 'License'). You may not use this file except in
+ * compliance with the License. The rights granted to you under the License
+ * may not be used to create, or enable the creation or redistribution of,
+ * unlawful or unlicensed copies of an Apple operating system, or to
+ * circumvent, violate, or enable the circumvention or violation of, any
+ * terms of an Apple operating system software license agreement.
  * 
- * This Original Code and all software distributed under the License are
- * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * Please obtain a copy of the License at
+ * http://www.opensource.apple.com/apsl/ and read it before using this file.
+ * 
+ * The Original Code and all software distributed under the License are
+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
- * License for the specific language governing rights and limitations
- * under the License.
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
+ * Please see the License for the specific language governing rights and
+ * limitations under the License.
  * 
- * @APPLE_LICENSE_HEADER_END@
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
  */
 /*
  * @OSF_COPYRIGHT@
@@ -63,6 +69,7 @@
 #include <kern/host.h>
 #include <kern/mach_param.h>
 #include <kern/sched.h>
+#include <sys/kdebug.h>
 #include <kern/spl.h>
 #include <kern/thread.h>
 #include <kern/processor.h>
@@ -72,6 +79,8 @@
  *     thread_quantum_expire:
  *
  *     Recalculate the quantum and priority for a thread.
+ *
+ *     Called at splsched.
  */
 
 void
@@ -79,50 +88,116 @@ thread_quantum_expire(
        timer_call_param_t      p0,
        timer_call_param_t      p1)
 {
-       register processor_t            myprocessor = p0;
-       register thread_t                       thread = p1;
-       spl_t                                           s;
+       processor_t                     processor = p0;
+       thread_t                        thread = p1;
+       ast_t                           preempt;
 
-       s = splsched();
        thread_lock(thread);
 
+       /*
+        * We've run up until our quantum expiration, and will (potentially)
+        * continue without re-entering the scheduler, so update this now.
+        */
+       thread->last_run_time = processor->quantum_end;
+       
        /*
         *      Check for fail-safe trip.
         */
-       if (!(thread->sched_mode & TH_MODE_TIMESHARE)) {
-               uint64_t                        new_computation;
+       if ((thread->sched_mode == TH_MODE_REALTIME || thread->sched_mode == TH_MODE_FIXED) && 
+           !(thread->sched_flags & TH_SFLAG_PROMOTED) &&
+           !(thread->options & TH_OPT_SYSTEM_CRITICAL)) {
+               uint64_t new_computation;
 
-               new_computation = myprocessor->quantum_end;
-               new_computation -= thread->computation_epoch;
-               if (new_computation + thread->computation_metered >
-                                                                                       max_unsafe_computation) {
+               new_computation = processor->quantum_end - thread->computation_epoch;
+               new_computation += thread->computation_metered;
+               if (new_computation > max_unsafe_computation) {
 
-                       if (thread->sched_mode & TH_MODE_REALTIME) {
-                               thread->priority = DEPRESSPRI;
+                       KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED, MACH_FAILSAFE)|DBG_FUNC_NONE,
+                                       (uintptr_t)thread->sched_pri, (uintptr_t)thread->sched_mode, 0, 0, 0);
 
-                               thread->safe_mode |= TH_MODE_REALTIME;
-                               thread->sched_mode &= ~TH_MODE_REALTIME;
+                       if (thread->sched_mode == TH_MODE_REALTIME) {
+                               thread->priority = DEPRESSPRI;
+                       }
+                       
+                       thread->saved_mode = thread->sched_mode;
+
+                       if (SCHED(supports_timeshare_mode)) {
+                               sched_share_incr();
+                               thread->sched_mode = TH_MODE_TIMESHARE;
+                       } else {
+                               /* XXX handle fixed->fixed case */
+                               thread->sched_mode = TH_MODE_FIXED;
                        }
 
-                       pset_share_incr(thread->processor_set);
-
-                       thread->safe_release = sched_tick + sched_safe_duration;
-                       thread->sched_mode |= (TH_MODE_FAILSAFE|TH_MODE_TIMESHARE);
-                       thread->sched_mode &= ~TH_MODE_PREEMPT;
+                       thread->safe_release = processor->quantum_end + sched_safe_duration;
+                       thread->sched_flags |= TH_SFLAG_FAILSAFE;
                }
        }
                
        /*
         *      Recompute scheduled priority if appropriate.
         */
-       if (thread->sched_stamp != sched_tick)
-               update_priority(thread);
+       if (SCHED(can_update_priority)(thread))
+               SCHED(update_priority)(thread);
        else
-       if (thread->sched_mode & TH_MODE_TIMESHARE) {
-               register uint32_t       delta;
+               SCHED(lightweight_update_priority)(thread);
 
-               thread_timer_delta(thread, delta);
+       SCHED(quantum_expire)(thread);
+       
+       processor->current_pri = thread->sched_pri;
+       processor->current_thmode = thread->sched_mode;
+
+       /*
+        *      This quantum is up, give this thread another.
+        */
+       if (first_timeslice(processor))
+               processor->timeslice--;
+
+       thread_quantum_init(thread);
+       thread->last_quantum_refill_time = processor->quantum_end;
+
+       processor->quantum_end += thread->current_quantum;
+       timer_call_enter1(&processor->quantum_timer,
+                                                       thread, processor->quantum_end, 0);
+
+       /*
+        *      Context switch check.
+        */
+       if ((preempt = csw_check(processor)) != AST_NONE)
+               ast_on(preempt);
+       else {
+               processor_set_t         pset = processor->processor_set;
 
+               pset_lock(pset);
+
+               pset_pri_hint(pset, processor, processor->current_pri);
+               pset_count_hint(pset, processor, SCHED(processor_runq_count)(processor));
+
+               pset_unlock(pset);
+       }
+
+       thread_unlock(thread);
+}
+
+#if defined(CONFIG_SCHED_TRADITIONAL)
+
+void
+sched_traditional_quantum_expire(thread_t      thread __unused)
+{
+       /*
+        * No special behavior when a timeshare, fixed, or realtime thread
+        * uses up its entire quantum
+        */
+}
+
+void
+lightweight_update_priority(thread_t thread)
+{
+       if (thread->sched_mode == TH_MODE_TIMESHARE) {
+               register uint32_t       delta;
+               
+               thread_timer_delta(thread, delta);
+               
                /*
                 *      Accumulate timesharing usage only
                 *      during contention for processor
@@ -130,38 +205,18 @@ thread_quantum_expire(
                 */
                if (thread->pri_shift < INT8_MAX)
                        thread->sched_usage += delta;
-
+               
                thread->cpu_delta += delta;
-
+               
                /*
                 * Adjust the scheduled priority if
                 * the thread has not been promoted
                 * and is not depressed.
                 */
-               if (    !(thread->sched_mode & TH_MODE_PROMOTED)        &&
-                               !(thread->sched_mode & TH_MODE_ISDEPRESSED)             )
+               if (    !(thread->sched_flags & TH_SFLAG_PROMOTED)      &&
+                       !(thread->sched_flags & TH_SFLAG_DEPRESSED_MASK)                )
                        compute_my_priority(thread);
-       }
-
-       /*
-        *      This quantum is up, give this thread another.
-        */
-       if (first_timeslice(myprocessor))
-               myprocessor->timeslice--;
-
-       thread_quantum_init(thread);
-       myprocessor->quantum_end += thread->current_quantum;
-       timer_call_enter1(&myprocessor->quantum_timer,
-                                                       thread, myprocessor->quantum_end);
-
-       thread_unlock(thread);
-
-       /*
-        * Check for and schedule ast if needed.
-        */
-       ast_check(myprocessor);
-
-       splx(s);
+       }       
 }
 
 /*
@@ -189,6 +244,25 @@ static struct shift_data   sched_decay_shifts[SCHED_DECAY_TICKS] = {
  *
  *     Calculate the timesharing priority based upon usage and load.
  */
+#ifdef CONFIG_EMBEDDED
+
+#define do_priority_computation(thread, pri)                                                   \
+       MACRO_BEGIN                                                                                                                     \
+       (pri) = (thread)->priority              /* start with base priority */          \
+           - ((thread)->sched_usage >> (thread)->pri_shift);                           \
+       if ((pri) < MAXPRI_THROTTLE) {                                                                          \
+               if ((thread)->task->max_priority > MAXPRI_THROTTLE)                             \
+                       (pri) = MAXPRI_THROTTLE;                                                                        \
+               else                                                                                                                    \
+                       if ((pri) < MINPRI_USER)                                                                        \
+                               (pri) = MINPRI_USER;                                                                    \
+       } else                                                                                                                          \
+       if ((pri) > MAXPRI_KERNEL)                                                                                      \
+               (pri) = MAXPRI_KERNEL;                                                                                  \
+       MACRO_END
+
+#else
+
 #define do_priority_computation(thread, pri)                                                   \
        MACRO_BEGIN                                                                                                                     \
        (pri) = (thread)->priority              /* start with base priority */          \
@@ -200,6 +274,10 @@ static struct shift_data   sched_decay_shifts[SCHED_DECAY_TICKS] = {
                (pri) = MAXPRI_KERNEL;                                                                                  \
        MACRO_END
 
+#endif /* defined(CONFIG_SCHED_TRADITIONAL) */
+
+#endif
+
 /*
  *     set_priority:
  *
@@ -214,9 +292,11 @@ set_priority(
        register int            priority)
 {
        thread->priority = priority;
-       compute_priority(thread, FALSE);
+       SCHED(compute_priority)(thread, FALSE);
 }
 
+#if defined(CONFIG_SCHED_TRADITIONAL)
+
 /*
  *     compute_priority:
  *
@@ -233,10 +313,10 @@ compute_priority(
 {
        register int            priority;
 
-       if (    !(thread->sched_mode & TH_MODE_PROMOTED)                        &&
-                       (!(thread->sched_mode & TH_MODE_ISDEPRESSED)    ||
+       if (    !(thread->sched_flags & TH_SFLAG_PROMOTED)                      &&
+                       (!(thread->sched_flags & TH_SFLAG_DEPRESSED_MASK)       ||
                                 override_depress                                                       )               ) {
-               if (thread->sched_mode & TH_MODE_TIMESHARE)
+               if (thread->sched_mode == TH_MODE_TIMESHARE)
                        do_priority_computation(thread, priority);
                else
                        priority = thread->priority;
@@ -263,10 +343,27 @@ compute_my_priority(
        register int            priority;
 
        do_priority_computation(thread, priority);
-       assert(thread->runq == RUN_QUEUE_NULL);
+       assert(thread->runq == PROCESSOR_NULL);
        thread->sched_pri = priority;
 }
 
+/*
+ *     can_update_priority
+ *
+ *     Make sure we don't do re-dispatches more frequently than a scheduler tick.
+ *
+ *     Called with the thread locked.
+ */
+boolean_t
+can_update_priority(
+                                       thread_t        thread)
+{
+       if (sched_tick == thread->sched_stamp)
+               return (FALSE);
+       else
+               return (TRUE);
+}
+
 /*
  *     update_priority
  *
@@ -284,7 +381,7 @@ update_priority(
        ticks = sched_tick - thread->sched_stamp;
        assert(ticks != 0);
        thread->sched_stamp += ticks;
-       thread->pri_shift = thread->processor_set->pri_shift;
+       thread->pri_shift = sched_pri_shift;
 
        /*
         *      Gather cpu usage data.
@@ -330,44 +427,45 @@ update_priority(
        /*
         *      Check for fail-safe release.
         */
-       if (    (thread->sched_mode & TH_MODE_FAILSAFE)         &&
-                       thread->sched_stamp >= thread->safe_release             ) {
-               if (!(thread->safe_mode & TH_MODE_TIMESHARE)) {
-                       if (thread->safe_mode & TH_MODE_REALTIME) {
+       if (    (thread->sched_flags & TH_SFLAG_FAILSAFE)               &&
+                       mach_absolute_time() >= thread->safe_release            ) {
+               if (thread->saved_mode != TH_MODE_TIMESHARE) {
+                       if (thread->saved_mode == TH_MODE_REALTIME) {
                                thread->priority = BASEPRI_RTQUEUES;
-
-                               thread->sched_mode |= TH_MODE_REALTIME;
                        }
 
-                       thread->sched_mode &= ~TH_MODE_TIMESHARE;
+                       thread->sched_mode = thread->saved_mode;
+                       thread->saved_mode = TH_MODE_NONE;
 
-                       if (thread->state & TH_RUN)
-                               pset_share_decr(thread->processor_set);
+                       if ((thread->state & (TH_RUN|TH_IDLE)) == TH_RUN)
+                               sched_share_decr();
 
-                       if (!(thread->sched_mode & TH_MODE_ISDEPRESSED))
+                       if (!(thread->sched_flags & TH_SFLAG_DEPRESSED_MASK))
                                set_sched_pri(thread, thread->priority);
                }
 
-               thread->safe_mode = 0;
-               thread->sched_mode &= ~TH_MODE_FAILSAFE;
+               thread->sched_flags &= ~TH_SFLAG_FAILSAFE;
        }
 
        /*
         *      Recompute scheduled priority if appropriate.
         */
-       if (    (thread->sched_mode & TH_MODE_TIMESHARE)        &&
-                       !(thread->sched_mode & TH_MODE_PROMOTED)        &&
-                       !(thread->sched_mode & TH_MODE_ISDEPRESSED)             ) {
+       if (    (thread->sched_mode == TH_MODE_TIMESHARE)       &&
+                       !(thread->sched_flags & TH_SFLAG_PROMOTED)      &&
+                       !(thread->sched_flags & TH_SFLAG_DEPRESSED_MASK)                ) {
                register int            new_pri;
 
                do_priority_computation(thread, new_pri);
                if (new_pri != thread->sched_pri) {
-                       run_queue_t             runq;
+                       boolean_t               removed = thread_run_queue_remove(thread);
 
-                       runq = run_queue_remove(thread);
                        thread->sched_pri = new_pri;
-                       if (runq != RUN_QUEUE_NULL)
+                       if (removed)
                                thread_setrun(thread, SCHED_TAILQ);
                }
        }
+       
+       return;
 }
+
+#endif /* CONFIG_SCHED_TRADITIONAL */