+
+/*
+ * thread_recompute_sched_pri:
+ *
+ * Reset the scheduled priority of the thread
+ * according to its base priority if the
+ * thread has not been promoted or depressed.
+ *
+ * This is the only way to push base_pri changes into sched_pri,
+ * or to recalculate the appropriate sched_pri after changing
+ * a promotion or depression.
+ *
+ * Called at splsched with the thread locked.
+ *
+ * TODO: Add an 'update urgency' flag to avoid urgency callouts on every rwlock operation
+ */
+void
+thread_recompute_sched_pri(thread_t thread, set_sched_pri_options_t options)
+{
+ uint32_t sched_flags = thread->sched_flags;
+ sched_mode_t sched_mode = thread->sched_mode;
+
+ int priority = thread->base_pri;
+
+ if (sched_mode == TH_MODE_TIMESHARE) {
+ priority = SCHED(compute_timeshare_priority)(thread);
+ }
+
+ if (sched_flags & TH_SFLAG_DEPRESS) {
+ /* thread_yield_internal overrides kernel mutex promotion */
+ priority = DEPRESSPRI;
+ } else {
+ /* poll-depress is overridden by mutex promotion and promote-reasons */
+ if ((sched_flags & TH_SFLAG_POLLDEPRESS)) {
+ priority = DEPRESSPRI;
+ }
+
+ if (thread->kern_promotion_schedpri > 0) {
+ priority = MAX(priority, thread->kern_promotion_schedpri);
+
+ if (sched_mode != TH_MODE_REALTIME) {
+ priority = MIN(priority, MAXPRI_PROMOTE);
+ }
+ }
+
+ if (sched_flags & TH_SFLAG_PROMOTED) {
+ priority = MAX(priority, thread->promotion_priority);
+
+ if (sched_mode != TH_MODE_REALTIME) {
+ priority = MIN(priority, MAXPRI_PROMOTE);
+ }
+ }
+
+ if (sched_flags & TH_SFLAG_PROMOTE_REASON_MASK) {
+ if (sched_flags & TH_SFLAG_RW_PROMOTED) {
+ priority = MAX(priority, MINPRI_RWLOCK);
+ }
+
+ if (sched_flags & TH_SFLAG_WAITQ_PROMOTED) {
+ priority = MAX(priority, MINPRI_WAITQ);
+ }
+
+ if (sched_flags & TH_SFLAG_EXEC_PROMOTED) {
+ priority = MAX(priority, MINPRI_EXEC);
+ }
+ }
+ }
+
+ set_sched_pri(thread, priority, options);
+}
+
+void
+sched_default_quantum_expire(thread_t thread __unused)
+{
+ /*
+ * No special behavior when a timeshare, fixed, or realtime thread
+ * uses up its entire quantum
+ */
+}
+
+#if defined(CONFIG_SCHED_TIMESHARE_CORE)
+
+/*
+ * lightweight_update_priority:
+ *
+ * Update the scheduled priority for
+ * a timesharing thread.
+ *
+ * Only for use on the current thread.
+ *
+ * Called with the thread locked.
+ */
+void
+lightweight_update_priority(thread_t thread)
+{
+ assert(thread->runq == PROCESSOR_NULL);
+ assert(thread == current_thread());
+
+ if (thread->sched_mode == TH_MODE_TIMESHARE) {
+ int priority;
+ uint32_t delta;
+
+ thread_timer_delta(thread, delta);
+
+ /*
+ * Accumulate timesharing usage only
+ * during contention for processor
+ * resources.
+ */
+ if (thread->pri_shift < INT8_MAX) {
+ thread->sched_usage += delta;
+ }
+
+ thread->cpu_delta += delta;
+
+#if CONFIG_SCHED_CLUTCH
+ /*
+ * Update the CPU usage for the thread group to which the thread belongs.
+ * The implementation assumes that the thread ran for the entire delta
+ * as part of the same thread group.
+ */
+ sched_clutch_cpu_usage_update(thread, delta);
+#endif /* CONFIG_SCHED_CLUTCH */
+
+ priority = sched_compute_timeshare_priority(thread);
+
+ if (priority != thread->sched_pri) {
+ thread_recompute_sched_pri(thread, SETPRI_LAZY);
+ }
+ }
+}
+
+/*
+ * Define shifts for simulating (5/8) ** n
+ *
+ * Shift structures for holding update shifts. Actual computation
+ * is usage = (usage >> shift1) +/- (usage >> abs(shift2)) where the
+ * +/- is determined by the sign of shift 2.
+ */
+
+const struct shift_data sched_decay_shifts[SCHED_DECAY_TICKS] = {
+ { .shift1 = 1, .shift2 = 1 },
+ { .shift1 = 1, .shift2 = 3 },
+ { .shift1 = 1, .shift2 = -3 },
+ { .shift1 = 2, .shift2 = -7 },
+ { .shift1 = 3, .shift2 = 5 },
+ { .shift1 = 3, .shift2 = -5 },
+ { .shift1 = 4, .shift2 = -8 },
+ { .shift1 = 5, .shift2 = 7 },
+ { .shift1 = 5, .shift2 = -7 },
+ { .shift1 = 6, .shift2 = -10 },
+ { .shift1 = 7, .shift2 = 10 },
+ { .shift1 = 7, .shift2 = -9 },
+ { .shift1 = 8, .shift2 = -11 },
+ { .shift1 = 9, .shift2 = 12 },
+ { .shift1 = 9, .shift2 = -11 },
+ { .shift1 = 10, .shift2 = -13 },
+ { .shift1 = 11, .shift2 = 14 },
+ { .shift1 = 11, .shift2 = -13 },
+ { .shift1 = 12, .shift2 = -15 },
+ { .shift1 = 13, .shift2 = 17 },
+ { .shift1 = 13, .shift2 = -15 },
+ { .shift1 = 14, .shift2 = -17 },
+ { .shift1 = 15, .shift2 = 19 },
+ { .shift1 = 16, .shift2 = 18 },
+ { .shift1 = 16, .shift2 = -19 },
+ { .shift1 = 17, .shift2 = 22 },
+ { .shift1 = 18, .shift2 = 20 },
+ { .shift1 = 18, .shift2 = -20 },
+ { .shift1 = 19, .shift2 = 26 },
+ { .shift1 = 20, .shift2 = 22 },
+ { .shift1 = 20, .shift2 = -22 },
+ { .shift1 = 21, .shift2 = -27 }
+};
+
+/*
+ * sched_compute_timeshare_priority:
+ *
+ * Calculate the timesharing priority based upon usage and load.
+ */
+extern int sched_pri_decay_band_limit;
+
+
+/* Only use the decay floor logic on embedded non-clutch schedulers */
+#if CONFIG_EMBEDDED && !CONFIG_SCHED_CLUTCH
+
+int
+sched_compute_timeshare_priority(thread_t thread)
+{
+ int decay_amount = (thread->sched_usage >> thread->pri_shift);
+ int decay_limit = sched_pri_decay_band_limit;
+
+ if (thread->base_pri > BASEPRI_FOREGROUND) {
+ decay_limit += (thread->base_pri - BASEPRI_FOREGROUND);
+ }
+
+ if (decay_amount > decay_limit) {
+ decay_amount = decay_limit;
+ }
+
+ /* start with base priority */
+ int priority = thread->base_pri - decay_amount;
+
+ if (priority < MAXPRI_THROTTLE) {
+ if (thread->task->max_priority > MAXPRI_THROTTLE) {
+ priority = MAXPRI_THROTTLE;
+ } else if (priority < MINPRI_USER) {
+ priority = MINPRI_USER;
+ }
+ } else if (priority > MAXPRI_KERNEL) {
+ priority = MAXPRI_KERNEL;
+ }
+
+ return priority;
+}
+
+#else /* CONFIG_EMBEDDED && !CONFIG_SCHED_CLUTCH */
+
+int
+sched_compute_timeshare_priority(thread_t thread)
+{
+ /* start with base priority */
+ int priority = thread->base_pri - (thread->sched_usage >> thread->pri_shift);
+
+ if (priority < MINPRI_USER) {
+ priority = MINPRI_USER;
+ } else if (priority > MAXPRI_KERNEL) {
+ priority = MAXPRI_KERNEL;
+ }
+
+ return priority;
+}
+
+#endif /* CONFIG_EMBEDDED && !CONFIG_SCHED_CLUTCH */
+
+/*
+ * 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
+ *
+ * Perform housekeeping operations driven by scheduler tick.
+ *
+ * Called with the thread locked.
+ */
+void
+update_priority(
+ thread_t thread)
+{
+ uint32_t ticks, delta;
+
+ ticks = sched_tick - thread->sched_stamp;
+ assert(ticks != 0);
+
+ thread->sched_stamp += ticks;
+
+ /* If requested, accelerate aging of sched_usage */
+ if (sched_decay_usage_age_factor > 1) {
+ ticks *= sched_decay_usage_age_factor;
+ }
+
+ /*
+ * Gather cpu usage data.
+ */
+ thread_timer_delta(thread, delta);
+ if (ticks < SCHED_DECAY_TICKS) {
+ /*
+ * Accumulate timesharing usage only during contention for processor
+ * resources. Use the pri_shift from the previous tick window to
+ * determine if the system was in a contended state.
+ */
+ if (thread->pri_shift < INT8_MAX) {
+ thread->sched_usage += delta;
+ }
+
+ thread->cpu_usage += delta + thread->cpu_delta;
+ thread->cpu_delta = 0;
+
+#if CONFIG_SCHED_CLUTCH
+ /*
+ * Update the CPU usage for the thread group to which the thread belongs.
+ * The implementation assumes that the thread ran for the entire delta
+ * as part of the same thread group.
+ */
+ sched_clutch_cpu_usage_update(thread, delta);
+#endif /* CONFIG_SCHED_CLUTCH */
+
+ const struct shift_data *shiftp = &sched_decay_shifts[ticks];
+
+ if (shiftp->shift2 > 0) {
+ thread->cpu_usage = (thread->cpu_usage >> shiftp->shift1) +
+ (thread->cpu_usage >> shiftp->shift2);
+ thread->sched_usage = (thread->sched_usage >> shiftp->shift1) +
+ (thread->sched_usage >> shiftp->shift2);
+ } else {
+ thread->cpu_usage = (thread->cpu_usage >> shiftp->shift1) -
+ (thread->cpu_usage >> -(shiftp->shift2));
+ thread->sched_usage = (thread->sched_usage >> shiftp->shift1) -
+ (thread->sched_usage >> -(shiftp->shift2));
+ }
+ } else {
+ thread->cpu_usage = thread->cpu_delta = 0;
+ thread->sched_usage = 0;
+ }
+
+ /*
+ * Check for fail-safe release.
+ */
+ if ((thread->sched_flags & TH_SFLAG_FAILSAFE) &&
+ mach_absolute_time() >= thread->safe_release) {
+ sched_thread_mode_undemote(thread, TH_SFLAG_FAILSAFE);
+ }
+
+ /*
+ * Now that the thread's CPU usage has been accumulated and aged
+ * based on contention of the previous tick window, update the
+ * pri_shift of the thread to match the current global load/shift
+ * values. The updated pri_shift would be used to calculate the
+ * new priority of the thread.
+ */
+#if CONFIG_SCHED_CLUTCH
+ thread->pri_shift = sched_clutch_thread_pri_shift(thread, thread->th_sched_bucket);
+#else /* CONFIG_SCHED_CLUTCH */
+ thread->pri_shift = sched_pri_shifts[thread->th_sched_bucket];
+#endif /* CONFIG_SCHED_CLUTCH */
+
+ /* Recompute scheduled priority if appropriate. */
+ if (thread->sched_mode == TH_MODE_TIMESHARE) {
+ thread_recompute_sched_pri(thread, SETPRI_LAZY);
+ }
+}
+
+#endif /* CONFIG_SCHED_TIMESHARE_CORE */
+
+
+/*
+ * TH_BUCKET_RUN is a count of *all* runnable non-idle threads.
+ * Each other bucket is a count of the runnable non-idle threads
+ * with that property. All updates to these counts should be
+ * performed with os_atomic_* operations.
+ *
+ * For the clutch scheduler, this global bucket is used only for
+ * keeping the total global run count.
+ */
+uint32_t sched_run_buckets[TH_BUCKET_MAX];
+
+static void
+sched_incr_bucket(sched_bucket_t bucket)
+{
+ assert(bucket >= TH_BUCKET_FIXPRI &&
+ bucket <= TH_BUCKET_SHARE_BG);
+
+ os_atomic_inc(&sched_run_buckets[bucket], relaxed);
+}
+
+static void
+sched_decr_bucket(sched_bucket_t bucket)
+{
+ assert(bucket >= TH_BUCKET_FIXPRI &&
+ bucket <= TH_BUCKET_SHARE_BG);
+
+ assert(os_atomic_load(&sched_run_buckets[bucket], relaxed) > 0);
+
+ os_atomic_dec(&sched_run_buckets[bucket], relaxed);
+}
+
+uint32_t
+sched_run_incr(thread_t thread)
+{
+ assert((thread->state & (TH_RUN | TH_IDLE)) == TH_RUN);
+
+ uint32_t new_count = os_atomic_inc(&sched_run_buckets[TH_BUCKET_RUN], relaxed);
+
+ sched_incr_bucket(thread->th_sched_bucket);
+
+ return new_count;
+}
+
+uint32_t
+sched_run_decr(thread_t thread)
+{
+ assert((thread->state & (TH_RUN | TH_IDLE)) != TH_RUN);
+
+ sched_decr_bucket(thread->th_sched_bucket);
+
+ uint32_t new_count = os_atomic_dec(&sched_run_buckets[TH_BUCKET_RUN], relaxed);
+
+ return new_count;
+}
+
+void
+sched_update_thread_bucket(thread_t thread)
+{
+ sched_bucket_t old_bucket = thread->th_sched_bucket;
+ sched_bucket_t new_bucket = TH_BUCKET_RUN;
+
+ switch (thread->sched_mode) {
+ case TH_MODE_FIXED:
+ case TH_MODE_REALTIME:
+ new_bucket = TH_BUCKET_FIXPRI;
+ break;
+
+ case TH_MODE_TIMESHARE:
+ if (thread->base_pri > BASEPRI_DEFAULT) {
+ new_bucket = TH_BUCKET_SHARE_FG;
+ } else if (thread->base_pri > BASEPRI_UTILITY) {
+ new_bucket = TH_BUCKET_SHARE_DF;
+ } else if (thread->base_pri > MAXPRI_THROTTLE) {
+ new_bucket = TH_BUCKET_SHARE_UT;
+ } else {
+ new_bucket = TH_BUCKET_SHARE_BG;
+ }
+ break;
+
+ default:
+ panic("unexpected mode: %d", thread->sched_mode);
+ break;
+ }
+
+ if (old_bucket != new_bucket) {
+ thread->th_sched_bucket = new_bucket;
+ thread->pri_shift = sched_pri_shifts[new_bucket];
+
+ if ((thread->state & (TH_RUN | TH_IDLE)) == TH_RUN) {
+ sched_decr_bucket(old_bucket);
+ sched_incr_bucket(new_bucket);
+ }
+ }
+}
+
+/*
+ * Set the thread's true scheduling mode
+ * Called with thread mutex and thread locked
+ * The thread has already been removed from the runqueue.
+ *
+ * (saved_mode is handled before this point)
+ */
+void
+sched_set_thread_mode(thread_t thread, sched_mode_t new_mode)
+{
+ assert(thread->runq == PROCESSOR_NULL);
+
+ switch (new_mode) {
+ case TH_MODE_FIXED:
+ case TH_MODE_REALTIME:
+ case TH_MODE_TIMESHARE:
+ break;
+
+ default:
+ panic("unexpected mode: %d", new_mode);
+ break;
+ }
+
+ thread->sched_mode = new_mode;
+
+ SCHED(update_thread_bucket)(thread);
+}
+
+/*
+ * Demote the true scheduler mode to timeshare (called with the thread locked)
+ */
+void
+sched_thread_mode_demote(thread_t thread, uint32_t reason)
+{
+ assert(reason & TH_SFLAG_DEMOTED_MASK);
+ assert((thread->sched_flags & reason) != reason);
+
+ if (thread->policy_reset) {
+ return;
+ }
+
+ if (thread->sched_flags & TH_SFLAG_DEMOTED_MASK) {
+ /* Another demotion reason is already active */
+ thread->sched_flags |= reason;
+ return;
+ }
+
+ assert(thread->saved_mode == TH_MODE_NONE);
+
+ boolean_t removed = thread_run_queue_remove(thread);
+
+ thread->sched_flags |= reason;
+
+ thread->saved_mode = thread->sched_mode;
+
+ sched_set_thread_mode(thread, TH_MODE_TIMESHARE);
+
+ thread_recompute_priority(thread);
+
+ if (removed) {
+ thread_run_queue_reinsert(thread, SCHED_TAILQ);
+ }
+}
+
+/*
+ * Un-demote the true scheduler mode back to the saved mode (called with the thread locked)
+ */
+void
+sched_thread_mode_undemote(thread_t thread, uint32_t reason)
+{
+ assert(reason & TH_SFLAG_DEMOTED_MASK);
+ assert((thread->sched_flags & reason) == reason);
+ assert(thread->saved_mode != TH_MODE_NONE);
+ assert(thread->sched_mode == TH_MODE_TIMESHARE);
+ assert(thread->policy_reset == 0);
+
+ thread->sched_flags &= ~reason;
+
+ if (thread->sched_flags & TH_SFLAG_DEMOTED_MASK) {
+ /* Another demotion reason is still active */
+ return;
+ }
+
+ boolean_t removed = thread_run_queue_remove(thread);
+
+ sched_set_thread_mode(thread, thread->saved_mode);
+
+ thread->saved_mode = TH_MODE_NONE;
+
+ thread_recompute_priority(thread);
+
+ if (removed) {
+ thread_run_queue_reinsert(thread, SCHED_TAILQ);
+ }
+}
+
+/*
+ * Promote thread to have a sched pri floor for a specific reason
+ *
+ * Promotion must not last past syscall boundary
+ * Clients must always pair promote and demote 1:1,
+ * Handling nesting of the same promote reason is the client's responsibility
+ *
+ * Called at splsched with thread locked
+ */
+void
+sched_thread_promote_reason(thread_t thread,
+ uint32_t reason,
+ __kdebug_only uintptr_t trace_obj /* already unslid */)
+{
+ assert(reason & TH_SFLAG_PROMOTE_REASON_MASK);
+ assert((thread->sched_flags & reason) != reason);
+
+ switch (reason) {
+ case TH_SFLAG_RW_PROMOTED:
+ KDBG(MACHDBG_CODE(DBG_MACH_SCHED, MACH_RW_PROMOTE),
+ thread_tid(thread), thread->sched_pri,
+ thread->base_pri, trace_obj);
+ break;
+ case TH_SFLAG_WAITQ_PROMOTED:
+ KDBG(MACHDBG_CODE(DBG_MACH_SCHED, MACH_WAITQ_PROMOTE),
+ thread_tid(thread), thread->sched_pri,
+ thread->base_pri, trace_obj);
+ break;
+ case TH_SFLAG_EXEC_PROMOTED:
+ KDBG(MACHDBG_CODE(DBG_MACH_SCHED, MACH_EXEC_PROMOTE),
+ thread_tid(thread), thread->sched_pri,
+ thread->base_pri, trace_obj);
+ break;
+ }
+
+ thread->sched_flags |= reason;
+
+ thread_recompute_sched_pri(thread, SETPRI_DEFAULT);
+}
+
+/*
+ * End a specific promotion reason
+ * Demotes a thread back to its expected priority without the promotion in place
+ *
+ * Called at splsched with thread locked
+ */
+void
+sched_thread_unpromote_reason(thread_t thread,
+ uint32_t reason,
+ __kdebug_only uintptr_t trace_obj /* already unslid */)
+{
+ assert(reason & TH_SFLAG_PROMOTE_REASON_MASK);
+ assert((thread->sched_flags & reason) == reason);
+
+ switch (reason) {
+ case TH_SFLAG_RW_PROMOTED:
+ KDBG(MACHDBG_CODE(DBG_MACH_SCHED, MACH_RW_DEMOTE),
+ thread_tid(thread), thread->sched_pri,
+ thread->base_pri, trace_obj);
+ break;
+ case TH_SFLAG_WAITQ_PROMOTED:
+ KDBG(MACHDBG_CODE(DBG_MACH_SCHED, MACH_WAITQ_DEMOTE),
+ thread_tid(thread), thread->sched_pri,
+ thread->base_pri, trace_obj);
+ break;
+ case TH_SFLAG_EXEC_PROMOTED:
+ KDBG(MACHDBG_CODE(DBG_MACH_SCHED, MACH_EXEC_DEMOTE),
+ thread_tid(thread), thread->sched_pri,
+ thread->base_pri, trace_obj);
+ break;
+ }
+
+ thread->sched_flags &= ~reason;
+
+ thread_recompute_sched_pri(thread, SETPRI_DEFAULT);
+}