]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/sched_average.c
xnu-2422.1.72.tar.gz
[apple/xnu.git] / osfmk / kern / sched_average.c
1 /*
2 * Copyright (c) 2000-2007 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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 /*
59 * Author: Avadis Tevanian, Jr.
60 * Date: 1986
61 *
62 * Compute various averages.
63 */
64
65 #include <mach/mach_types.h>
66
67 #include <kern/sched.h>
68 #include <kern/assert.h>
69 #include <kern/processor.h>
70 #include <kern/thread.h>
71 #if CONFIG_TELEMETRY
72 #include <kern/telemetry.h>
73 #endif
74
75 uint32_t avenrun[3] = {0, 0, 0};
76 uint32_t mach_factor[3] = {0, 0, 0};
77
78 #if defined(CONFIG_SCHED_TRADITIONAL)
79 /*
80 * Values are scaled by LOAD_SCALE, defined in processor_info.h
81 */
82 #define base(n) ((n) << SCHED_TICK_SHIFT)
83 #define frac(n) (((base(n) - 1) * LOAD_SCALE) / base(n))
84
85 static uint32_t fract[3] = {
86 frac(5), /* 5 second average */
87 frac(30), /* 30 second average */
88 frac(60), /* 1 minute average */
89 };
90
91 #undef base
92 #undef frac
93
94 #endif /* CONFIG_SCHED_TRADITIONAL */
95
96 static unsigned int sched_nrun;
97
98 typedef void (*sched_avg_comp_t)(
99 void *param);
100
101 static struct sched_average {
102 sched_avg_comp_t comp;
103 void *param;
104 int period; /* in seconds */
105 uint64_t deadline;
106 } sched_average[] = {
107 { compute_averunnable, &sched_nrun, 5, 0 },
108 { compute_stack_target, NULL, 5, 1 },
109 { compute_memory_pressure, NULL, 1, 0 },
110 { compute_zone_gc_throttle, NULL, 60, 0 },
111 { compute_pageout_gc_throttle, NULL, 1, 0 },
112 { compute_pmap_gc_throttle, NULL, 60, 0 },
113 #if CONFIG_TELEMETRY
114 { compute_telemetry, NULL, 1, 0 },
115 #endif
116 { NULL, NULL, 0, 0 }
117 };
118
119 typedef struct sched_average *sched_average_t;
120
121 /* The "stdelta" parameter represents the number of scheduler maintenance
122 * "ticks" that have elapsed since the last invocation, subject to
123 * integer division imprecision.
124 */
125
126 void
127 compute_averages(uint64_t stdelta)
128 {
129 int ncpus, nthreads, nshared, nbackground, nshared_non_bg;
130 uint32_t factor_now, average_now, load_now = 0, background_load_now = 0, combined_fgbg_load_now = 0;
131 sched_average_t avg;
132 uint64_t abstime, index;
133
134 /*
135 * Retrieve counts, ignoring
136 * the current thread.
137 */
138 ncpus = processor_avail_count;
139 nthreads = sched_run_count - 1;
140 nshared = sched_share_count;
141 nbackground = sched_background_count;
142
143 /*
144 * Load average and mach factor calculations for
145 * those which ask about these things.
146 */
147 average_now = nthreads * LOAD_SCALE;
148
149 if (nthreads > ncpus)
150 factor_now = (ncpus * LOAD_SCALE) / (nthreads + 1);
151 else
152 factor_now = (ncpus - nthreads) * LOAD_SCALE;
153
154 /* For those statistics that formerly relied on being recomputed
155 * on timer ticks, advance by the approximate number of corresponding
156 * elapsed intervals, thus compensating for potential idle intervals.
157 */
158 for (index = 0; index < stdelta; index++) {
159 sched_mach_factor = ((sched_mach_factor << 2) + factor_now) / 5;
160 sched_load_average = ((sched_load_average << 2) + average_now) / 5;
161 }
162 /*
163 * Compute the timeshare priority conversion factor based on loading.
164 * Because our counters may be incremented and accessed
165 * concurrently with respect to each other, we may have
166 * windows where the invariant nthreads >= nshared >= nbackground
167 * is broken, so truncate values in these cases.
168 */
169
170 if (nshared > nthreads)
171 nshared = nthreads;
172
173 if (nbackground > nshared)
174 nbackground = nshared;
175
176 nshared_non_bg = nshared - nbackground;
177
178 if (nshared_non_bg > ncpus) {
179 if (ncpus > 1)
180 load_now = nshared_non_bg / ncpus;
181 else
182 load_now = nshared_non_bg;
183
184 if (load_now > NRQS - 1)
185 load_now = NRQS - 1;
186 }
187
188 if (nbackground > ncpus) {
189 if (ncpus > 1)
190 background_load_now = nbackground / ncpus;
191 else
192 background_load_now = nbackground;
193
194 if (background_load_now > NRQS - 1)
195 background_load_now = NRQS - 1;
196 }
197
198 if (nshared > ncpus) {
199 if (ncpus > 1)
200 combined_fgbg_load_now = nshared / ncpus;
201 else
202 combined_fgbg_load_now = nshared;
203
204 if (combined_fgbg_load_now > NRQS - 1)
205 combined_fgbg_load_now = NRQS - 1;
206 }
207
208 /*
209 * Sample total running threads.
210 */
211 sched_nrun = nthreads;
212
213 #if defined(CONFIG_SCHED_TRADITIONAL)
214
215 /*
216 * The conversion factor consists of
217 * two components: a fixed value based
218 * on the absolute time unit, and a
219 * dynamic portion based on loading.
220 *
221 * Zero loading results in a out of range
222 * shift count. Accumulated usage is ignored
223 * during conversion and new usage deltas
224 * are discarded.
225 */
226 sched_pri_shift = sched_fixed_shift - sched_load_shifts[load_now];
227 sched_background_pri_shift = sched_fixed_shift - sched_load_shifts[background_load_now];
228 sched_combined_fgbg_pri_shift = sched_fixed_shift - sched_load_shifts[combined_fgbg_load_now];
229
230 /*
231 * Compute old-style Mach load averages.
232 */
233
234 for (index = 0; index < stdelta; index++) {
235 register int i;
236
237 for (i = 0; i < 3; i++) {
238 mach_factor[i] = ((mach_factor[i] * fract[i]) +
239 (factor_now * (LOAD_SCALE - fract[i]))) / LOAD_SCALE;
240
241 avenrun[i] = ((avenrun[i] * fract[i]) +
242 (average_now * (LOAD_SCALE - fract[i]))) / LOAD_SCALE;
243 }
244 }
245 #endif /* CONFIG_SCHED_TRADITIONAL */
246
247 /*
248 * Compute averages in other components.
249 */
250 abstime = mach_absolute_time();
251 for (avg = sched_average; avg->comp != NULL; ++avg) {
252 if (abstime >= avg->deadline) {
253 uint64_t period_abs = (avg->period * sched_one_second_interval);
254 uint64_t ninvokes = 1;
255
256 ninvokes += (abstime - avg->deadline) / period_abs;
257 ninvokes = MIN(ninvokes, SCHED_TICK_MAX_DELTA);
258
259 for (index = 0; index < ninvokes; index++) {
260 (*avg->comp)(avg->param);
261 }
262 avg->deadline = abstime + period_abs;
263 }
264 }
265 }