- register processor_set_t pset = &default_pset;
- register int ncpus;
- register int nthreads, nshared;
- sched_average_t avg;
- register uint32_t factor_now = 0;
- register uint32_t average_now = 0;
- register uint32_t load_now = 0;
-
- if ((ncpus = pset->processor_count) > 0) {
- /*
- * Retrieve counts, ignoring
- * the current thread.
- */
- nthreads = pset->run_count - 1;
- nshared = pset->share_count;
-
- /*
- * Load average and mach factor calculations for
- * those which ask about these things.
- */
- average_now = nthreads * LOAD_SCALE;
-
- if (nthreads > ncpus)
- factor_now = (ncpus * LOAD_SCALE) / (nthreads + 1);
- else
- factor_now = (ncpus - nthreads) * LOAD_SCALE;
-
- pset->mach_factor = ((pset->mach_factor << 2) + factor_now) / 5;
- pset->load_average = ((pset->load_average << 2) + average_now) / 5;
-
- /*
- * Compute the timeshare priority
- * conversion factor based on loading.
- */
- if (nshared > nthreads)
- nshared = nthreads;
-
- if (nshared > ncpus) {
+ /*
+ * Retrieve a snapshot of the current run counts.
+ *
+ * Why not a bcopy()? Because we need atomic word-sized reads of sched_run_buckets,
+ * not byte-by-byte copy.
+ */
+ uint32_t ncpus = processor_avail_count;
+
+ load_now[TH_BUCKET_RUN] = sched_run_buckets[TH_BUCKET_RUN];
+ load_now[TH_BUCKET_FIXPRI] = sched_run_buckets[TH_BUCKET_FIXPRI];
+ load_now[TH_BUCKET_SHARE_FG] = sched_run_buckets[TH_BUCKET_SHARE_FG];
+ load_now[TH_BUCKET_SHARE_UT] = sched_run_buckets[TH_BUCKET_SHARE_UT];
+ load_now[TH_BUCKET_SHARE_BG] = sched_run_buckets[TH_BUCKET_SHARE_BG];
+
+ assert(load_now[TH_BUCKET_RUN] >= 0);
+ assert(load_now[TH_BUCKET_FIXPRI] >= 0);
+
+ /* Ignore the current thread, which is a running fixpri thread */
+
+ uint32_t nthreads = load_now[TH_BUCKET_RUN] - 1;
+ uint32_t nfixpri = load_now[TH_BUCKET_FIXPRI] - 1;
+
+ KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
+ MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_LOAD) | DBG_FUNC_NONE,
+ load_now[TH_BUCKET_FIXPRI] - 1, load_now[TH_BUCKET_SHARE_FG],
+ load_now[TH_BUCKET_SHARE_BG], load_now[TH_BUCKET_SHARE_UT], 0);
+
+ /*
+ * Compute the timeshare priority conversion factor based on loading.
+ * Because our counters may be incremented and accessed
+ * concurrently with respect to each other, we may have
+ * windows where the invariant (nthreads - nfixpri) == (fg + bg + ut)
+ * is broken, so truncate values in these cases.
+ */
+
+ uint32_t timeshare_threads = (nthreads - nfixpri);
+
+ for (uint32_t i = TH_BUCKET_SHARE_FG; i <= TH_BUCKET_SHARE_BG ; i++) {
+ if (load_now[i] > timeshare_threads)
+ load_now[i] = timeshare_threads;
+ }
+
+ /*
+ * Utility threads contribute up to NCPUS of load to FG threads
+ */
+ if (load_now[TH_BUCKET_SHARE_UT] <= ncpus) {
+ load_now[TH_BUCKET_SHARE_FG] += load_now[TH_BUCKET_SHARE_UT];
+ } else {
+ load_now[TH_BUCKET_SHARE_FG] += ncpus;
+ }
+
+ /*
+ * FG and UT should notice there's one thread of competition from BG,
+ * but no more.
+ */
+ if (load_now[TH_BUCKET_SHARE_BG] > 0) {
+ load_now[TH_BUCKET_SHARE_FG] += 1;
+ load_now[TH_BUCKET_SHARE_UT] += 1;
+ }
+
+ /*
+ * The conversion factor consists of two components:
+ * a fixed value based on the absolute time unit (sched_fixed_shift),
+ * and a dynamic portion based on load (sched_load_shifts).
+ *
+ * Zero load results in a out of range shift count.
+ */
+
+ for (uint32_t i = TH_BUCKET_SHARE_FG; i <= TH_BUCKET_SHARE_BG ; i++) {
+ uint32_t bucket_load = 0;
+
+ if (load_now[i] > ncpus) {