- 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);
+ int ncpus, nthreads, nshared, nbackground, nshared_non_bg;
+ uint32_t factor_now, average_now, load_now = 0, background_load_now = 0, combined_fgbg_load_now = 0;
+ sched_average_t avg;
+ uint64_t abstime, index;
+
+ /*
+ * Retrieve counts, ignoring
+ * the current thread.
+ */
+ ncpus = processor_avail_count;
+ nthreads = sched_run_count - 1;
+ nshared = sched_share_count;
+ nbackground = sched_background_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;
+
+ /* For those statistics that formerly relied on being recomputed
+ * on timer ticks, advance by the approximate number of corresponding
+ * elapsed intervals, thus compensating for potential idle intervals.
+ */
+ for (index = 0; index < stdelta; index++) {
+ sched_mach_factor = ((sched_mach_factor << 2) + factor_now) / 5;
+ sched_load_average = ((sched_load_average << 2) + average_now) / 5;
+ }
+ /*
+ * 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 >= nshared >= nbackground
+ * is broken, so truncate values in these cases.
+ */
+
+ if (nshared > nthreads)
+ nshared = nthreads;
+
+ if (nbackground > nshared)
+ nbackground = nshared;
+
+ nshared_non_bg = nshared - nbackground;
+
+ if (nshared_non_bg > ncpus) {
+ if (ncpus > 1)
+ load_now = nshared_non_bg / ncpus;