]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/sched_average.c
xnu-4903.231.4.tar.gz
[apple/xnu.git] / osfmk / kern / sched_average.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2007 Apple Computer, Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * @OSF_COPYRIGHT@
30 */
31/*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
35 *
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
41 *
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56/*
57 */
58/*
1c79356b
A
59 * Author: Avadis Tevanian, Jr.
60 * Date: 1986
61 *
91447636 62 * Compute various averages.
1c79356b
A
63 */
64
91447636 65#include <mach/mach_types.h>
1c79356b 66
1c79356b
A
67#include <kern/sched.h>
68#include <kern/assert.h>
69#include <kern/processor.h>
70#include <kern/thread.h>
39236c6e
A
71#if CONFIG_TELEMETRY
72#include <kern/telemetry.h>
73#endif
490019cf
A
74
75#include <sys/kdebug.h>
76
9bccf70c
A
77uint32_t avenrun[3] = {0, 0, 0};
78uint32_t mach_factor[3] = {0, 0, 0};
1c79356b 79
39037602
A
80uint32_t sched_load_average, sched_mach_factor;
81
3e170ce0 82#if defined(CONFIG_SCHED_TIMESHARE_CORE)
1c79356b
A
83/*
84 * Values are scaled by LOAD_SCALE, defined in processor_info.h
85 */
0b4e3aa0
A
86#define base(n) ((n) << SCHED_TICK_SHIFT)
87#define frac(n) (((base(n) - 1) * LOAD_SCALE) / base(n))
88
9bccf70c 89static uint32_t fract[3] = {
0b4e3aa0
A
90 frac(5), /* 5 second average */
91 frac(30), /* 30 second average */
92 frac(60), /* 1 minute average */
1c79356b 93};
9bccf70c 94
0b4e3aa0
A
95#undef base
96#undef frac
1c79356b 97
3e170ce0 98#endif /* CONFIG_SCHED_TIMESHARE_CORE */
6d2010ae 99
91447636
A
100static unsigned int sched_nrun;
101
102typedef void (*sched_avg_comp_t)(
103 void *param);
104
91447636
A
105static struct sched_average {
106 sched_avg_comp_t comp;
39236c6e
A
107 void *param;
108 int period; /* in seconds */
109 uint64_t deadline;
91447636 110} sched_average[] = {
6d2010ae
A
111 { compute_averunnable, &sched_nrun, 5, 0 },
112 { compute_stack_target, NULL, 5, 1 },
316670eb 113 { compute_pageout_gc_throttle, NULL, 1, 0 },
6d2010ae 114 { compute_pmap_gc_throttle, NULL, 60, 0 },
39236c6e
A
115#if CONFIG_TELEMETRY
116 { compute_telemetry, NULL, 1, 0 },
117#endif
91447636
A
118 { NULL, NULL, 0, 0 }
119};
120
121typedef struct sched_average *sched_average_t;
122
d9a64523
A
123/*
124 * Scheduler load calculation algorithm
125 *
126 * The scheduler load values provide an estimate of the number of runnable
127 * timeshare threads in the system at various priority bands. The load
128 * ultimately affects the priority shifts applied to all threads in a band
129 * causing them to timeshare with other threads in the system. The load is
130 * maintained in buckets, with each bucket corresponding to a priority band.
131 *
132 * Each runnable thread on the system contributes its load to its priority
133 * band and to the bands above it. The contribution of a thread to the bands
134 * above it is not strictly 1:1 and is weighted based on the priority band
135 * of the thread. The rules of thread load contribution to each of its higher
136 * bands are as follows:
137 *
138 * - DF threads: Upto (2 * NCPUs) threads
139 * - UT threads: Upto NCPUs threads
140 * - BG threads: Upto 1 thread
141 *
142 * To calculate the load values, the various run buckets are sampled (every
143 * sched_load_compute_interval_abs) and the weighted contributions of the the
144 * lower bucket threads are added. The resultant value is plugged into an
145 * exponentially weighted moving average formula:
146 * new-load = alpha * old-load + (1 - alpha) * run-bucket-sample-count
147 * (where, alpha < 1)
148 * The calculations for the scheduler load are done using fixpoint math with
149 * a scale factor of 16 to avoid expensive divides and floating point
150 * operations. The final load values are a smooth curve representative of
151 * the actual number of runnable threads in a priority band.
152 */
153
154/* Maintains the current (scaled for fixpoint) load in various buckets */
155uint32_t sched_load[TH_BUCKET_MAX];
39037602 156
d9a64523
A
157/*
158 * Alpha factor for the EWMA alogrithm. The current values are chosen as
159 * 6:10 ("old load":"new samples") to make sure the scheduler reacts fast
160 * enough to changing system load but does not see too many spikes from bursty
161 * activity. The current values ensure that the scheduler would converge
162 * to the latest load in 2-3 sched_load_compute_interval_abs intervals
163 * (which amounts to ~30-45ms with current values).
39236c6e 164 */
d9a64523
A
165#define SCHED_LOAD_EWMA_ALPHA_OLD 6
166#define SCHED_LOAD_EWMA_ALPHA_NEW 10
167#define SCHED_LOAD_EWMA_ALPHA_SHIFT 4
168static_assert((SCHED_LOAD_EWMA_ALPHA_OLD + SCHED_LOAD_EWMA_ALPHA_NEW) == (1ul << SCHED_LOAD_EWMA_ALPHA_SHIFT));
169
170/* For fixpoint EWMA, roundup the load to make it converge */
171#define SCHED_LOAD_EWMA_ROUNDUP(load) (((load) & (1ul << (SCHED_LOAD_EWMA_ALPHA_SHIFT - 1))) != 0)
172
173/* Macro to convert scaled sched load to a real load value */
174#define SCHED_LOAD_EWMA_UNSCALE(load) (((load) >> SCHED_LOAD_EWMA_ALPHA_SHIFT) + SCHED_LOAD_EWMA_ROUNDUP(load))
39236c6e 175
d9a64523
A
176/*
177 * Routine to capture the latest runnable counts and update sched_load */
1c79356b 178void
d9a64523 179compute_sched_load(void)
1c79356b 180{
2d21ac55 181 /*
39037602
A
182 * Retrieve a snapshot of the current run counts.
183 *
184 * Why not a bcopy()? Because we need atomic word-sized reads of sched_run_buckets,
185 * not byte-by-byte copy.
2d21ac55 186 */
39037602 187 uint32_t ncpus = processor_avail_count;
d9a64523 188 uint32_t load_now[TH_BUCKET_MAX];
2d21ac55 189
39037602
A
190 load_now[TH_BUCKET_RUN] = sched_run_buckets[TH_BUCKET_RUN];
191 load_now[TH_BUCKET_FIXPRI] = sched_run_buckets[TH_BUCKET_FIXPRI];
192 load_now[TH_BUCKET_SHARE_FG] = sched_run_buckets[TH_BUCKET_SHARE_FG];
d9a64523 193 load_now[TH_BUCKET_SHARE_DF] = sched_run_buckets[TH_BUCKET_SHARE_DF];
39037602
A
194 load_now[TH_BUCKET_SHARE_UT] = sched_run_buckets[TH_BUCKET_SHARE_UT];
195 load_now[TH_BUCKET_SHARE_BG] = sched_run_buckets[TH_BUCKET_SHARE_BG];
2d21ac55 196
39037602
A
197 assert(load_now[TH_BUCKET_RUN] >= 0);
198 assert(load_now[TH_BUCKET_FIXPRI] >= 0);
199
d9a64523
A
200 uint32_t nthreads = load_now[TH_BUCKET_RUN];
201 uint32_t nfixpri = load_now[TH_BUCKET_FIXPRI];
39037602
A
202
203 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
204 MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_LOAD) | DBG_FUNC_NONE,
d9a64523 205 load_now[TH_BUCKET_FIXPRI], (load_now[TH_BUCKET_SHARE_FG] + load_now[TH_BUCKET_SHARE_DF]),
39037602 206 load_now[TH_BUCKET_SHARE_BG], load_now[TH_BUCKET_SHARE_UT], 0);
2d21ac55 207
2d21ac55 208 /*
39236c6e
A
209 * Compute the timeshare priority conversion factor based on loading.
210 * Because our counters may be incremented and accessed
211 * concurrently with respect to each other, we may have
d9a64523 212 * windows where the invariant (nthreads - nfixpri) == (fg + df + bg + ut)
39236c6e 213 * is broken, so truncate values in these cases.
2d21ac55 214 */
39037602 215 uint32_t timeshare_threads = (nthreads - nfixpri);
39037602
A
216 for (uint32_t i = TH_BUCKET_SHARE_FG; i <= TH_BUCKET_SHARE_BG ; i++) {
217 if (load_now[i] > timeshare_threads)
218 load_now[i] = timeshare_threads;
219 }
39236c6e 220
d9a64523
A
221 /*
222 * Default threads contribute up to (NCPUS * 2) of load to FG threads
223 */
224 if (load_now[TH_BUCKET_SHARE_DF] <= (ncpus * 2)) {
225 load_now[TH_BUCKET_SHARE_FG] += load_now[TH_BUCKET_SHARE_DF];
226 } else {
227 load_now[TH_BUCKET_SHARE_FG] += (ncpus * 2);
228 }
229
39037602 230 /*
d9a64523 231 * Utility threads contribute up to NCPUS of load to FG & DF threads
39037602
A
232 */
233 if (load_now[TH_BUCKET_SHARE_UT] <= ncpus) {
234 load_now[TH_BUCKET_SHARE_FG] += load_now[TH_BUCKET_SHARE_UT];
d9a64523 235 load_now[TH_BUCKET_SHARE_DF] += load_now[TH_BUCKET_SHARE_UT];
39037602
A
236 } else {
237 load_now[TH_BUCKET_SHARE_FG] += ncpus;
d9a64523 238 load_now[TH_BUCKET_SHARE_DF] += ncpus;
39037602 239 }
91447636 240
39037602 241 /*
d9a64523 242 * BG threads contribute up to 1 thread worth of load to FG, DF and UT threads
39037602
A
243 */
244 if (load_now[TH_BUCKET_SHARE_BG] > 0) {
245 load_now[TH_BUCKET_SHARE_FG] += 1;
d9a64523 246 load_now[TH_BUCKET_SHARE_DF] += 1;
39037602 247 load_now[TH_BUCKET_SHARE_UT] += 1;
1c79356b
A
248 }
249
39037602
A
250 /*
251 * The conversion factor consists of two components:
252 * a fixed value based on the absolute time unit (sched_fixed_shift),
253 * and a dynamic portion based on load (sched_load_shifts).
254 *
255 * Zero load results in a out of range shift count.
256 */
39236c6e 257
39037602
A
258 for (uint32_t i = TH_BUCKET_SHARE_FG; i <= TH_BUCKET_SHARE_BG ; i++) {
259 uint32_t bucket_load = 0;
39236c6e 260
39037602 261 if (load_now[i] > ncpus) {
d9a64523 262 /* Normalize the load to number of CPUs */
39037602
A
263 if (ncpus > 1)
264 bucket_load = load_now[i] / ncpus;
265 else
266 bucket_load = load_now[i];
39236c6e 267
39037602
A
268 if (bucket_load > MAX_LOAD)
269 bucket_load = MAX_LOAD;
270 }
d9a64523
A
271 /* Plug the load values into the EWMA algorithm to calculate (scaled for fixpoint) sched_load */
272 sched_load[i] = (sched_load[i] * SCHED_LOAD_EWMA_ALPHA_OLD) + ((bucket_load << SCHED_LOAD_EWMA_ALPHA_SHIFT) * SCHED_LOAD_EWMA_ALPHA_NEW);
273 sched_load[i] = sched_load[i] >> SCHED_LOAD_EWMA_ALPHA_SHIFT;
274 }
39236c6e 275
d9a64523
A
276 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
277 MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_LOAD_EFFECTIVE) | DBG_FUNC_NONE,
278 SCHED_LOAD_EWMA_UNSCALE(sched_load[TH_BUCKET_SHARE_FG]), SCHED_LOAD_EWMA_UNSCALE(sched_load[TH_BUCKET_SHARE_DF]),
279 SCHED_LOAD_EWMA_UNSCALE(sched_load[TH_BUCKET_SHARE_UT]), SCHED_LOAD_EWMA_UNSCALE(sched_load[TH_BUCKET_SHARE_BG]), 0);
280}
281
282void
283compute_averages(uint64_t stdelta)
284{
285
286 uint32_t nthreads = sched_run_buckets[TH_BUCKET_RUN] - 1;
287 uint32_t ncpus = processor_avail_count;
288
289 /* Update the global pri_shifts based on the latest values */
290 for (uint32_t i = TH_BUCKET_SHARE_FG; i <= TH_BUCKET_SHARE_BG ; i++) {
291 uint32_t bucket_load = SCHED_LOAD_EWMA_UNSCALE(sched_load[i]);
39037602
A
292 sched_pri_shifts[i] = sched_fixed_shift - sched_load_shifts[bucket_load];
293 }
490019cf 294
6d2010ae 295 /*
39037602 296 * Sample total running threads for the load average calculation.
6d2010ae
A
297 */
298 sched_nrun = nthreads;
6d2010ae 299
2d21ac55 300 /*
39037602
A
301 * Load average and mach factor calculations for
302 * those which ask about these things.
2d21ac55 303 */
39037602
A
304 uint32_t average_now = nthreads * LOAD_SCALE;
305 uint32_t factor_now;
306
307 if (nthreads > ncpus)
308 factor_now = (ncpus * LOAD_SCALE) / (nthreads + 1);
309 else
310 factor_now = (ncpus - nthreads) * LOAD_SCALE;
2d21ac55 311
1c79356b 312 /*
39037602
A
313 * For those statistics that formerly relied on being recomputed
314 * on timer ticks, advance by the approximate number of corresponding
315 * elapsed intervals, thus compensating for potential idle intervals.
1c79356b 316 */
39037602
A
317 for (uint32_t index = 0; index < stdelta; index++) {
318 sched_mach_factor = ((sched_mach_factor << 2) + factor_now) / 5;
319 sched_load_average = ((sched_load_average << 2) + average_now) / 5;
320 }
39236c6e 321
39037602
A
322 /*
323 * Compute old-style Mach load averages.
324 */
325 for (uint32_t index = 0; index < stdelta; index++) {
326 for (uint32_t i = 0; i < 3; i++) {
1c79356b
A
327 mach_factor[i] = ((mach_factor[i] * fract[i]) +
328 (factor_now * (LOAD_SCALE - fract[i]))) / LOAD_SCALE;
329
330 avenrun[i] = ((avenrun[i] * fract[i]) +
331 (average_now * (LOAD_SCALE - fract[i]))) / LOAD_SCALE;
332 }
333 }
9bccf70c
A
334
335 /*
39037602 336 * Compute averages in other components.
9bccf70c 337 */
39037602
A
338 uint64_t abstime = mach_absolute_time();
339
340 for (sched_average_t avg = sched_average; avg->comp != NULL; ++avg) {
6d2010ae 341 if (abstime >= avg->deadline) {
39236c6e
A
342 uint64_t period_abs = (avg->period * sched_one_second_interval);
343 uint64_t ninvokes = 1;
344
345 ninvokes += (abstime - avg->deadline) / period_abs;
346 ninvokes = MIN(ninvokes, SCHED_TICK_MAX_DELTA);
347
39037602 348 for (uint32_t index = 0; index < ninvokes; index++) {
39236c6e
A
349 (*avg->comp)(avg->param);
350 }
351 avg->deadline = abstime + period_abs;
91447636 352 }
9bccf70c 353 }
1c79356b 354}