]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/sched_average.c
xnu-792.17.14.tar.gz
[apple/xnu.git] / osfmk / kern / sched_average.c
1 /*
2 * Copyright (c) 2000-2004 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
72 uint32_t avenrun[3] = {0, 0, 0};
73 uint32_t mach_factor[3] = {0, 0, 0};
74
75 /*
76 * Values are scaled by LOAD_SCALE, defined in processor_info.h
77 */
78 #define base(n) ((n) << SCHED_TICK_SHIFT)
79 #define frac(n) (((base(n) - 1) * LOAD_SCALE) / base(n))
80
81 static uint32_t fract[3] = {
82 frac(5), /* 5 second average */
83 frac(30), /* 30 second average */
84 frac(60), /* 1 minute average */
85 };
86
87 #undef base
88 #undef frac
89
90 static unsigned int sched_nrun;
91
92 typedef void (*sched_avg_comp_t)(
93 void *param);
94
95 #define SCHED_AVG_SECS(n) ((n) << SCHED_TICK_SHIFT)
96
97 static struct sched_average {
98 sched_avg_comp_t comp;
99 void *param;
100 int period;
101 int tick;
102 } sched_average[] = {
103 { compute_averunnable, &sched_nrun, SCHED_AVG_SECS(5), 0 },
104 { compute_stack_target, NULL, SCHED_AVG_SECS(5), 1 },
105 { NULL, NULL, 0, 0 }
106 };
107
108 typedef struct sched_average *sched_average_t;
109
110 void
111 compute_averages(void)
112 {
113 register processor_set_t pset = &default_pset;
114 register int ncpus;
115 register int nthreads, nshared;
116 sched_average_t avg;
117 register uint32_t factor_now = 0;
118 register uint32_t average_now = 0;
119 register uint32_t load_now = 0;
120
121 if ((ncpus = pset->processor_count) > 0) {
122 /*
123 * Retrieve counts, ignoring
124 * the current thread.
125 */
126 nthreads = pset->run_count - 1;
127 nshared = pset->share_count;
128
129 /*
130 * Load average and mach factor calculations for
131 * those which ask about these things.
132 */
133 average_now = nthreads * LOAD_SCALE;
134
135 if (nthreads > ncpus)
136 factor_now = (ncpus * LOAD_SCALE) / (nthreads + 1);
137 else
138 factor_now = (ncpus - nthreads) * LOAD_SCALE;
139
140 pset->mach_factor = ((pset->mach_factor << 2) + factor_now) / 5;
141 pset->load_average = ((pset->load_average << 2) + average_now) / 5;
142
143 /*
144 * Compute the timeshare priority
145 * conversion factor based on loading.
146 */
147 if (nshared > nthreads)
148 nshared = nthreads;
149
150 if (nshared > ncpus) {
151 if (ncpus > 1)
152 load_now = nshared / ncpus;
153 else
154 load_now = nshared;
155
156 if (load_now > NRQS - 1)
157 load_now = NRQS - 1;
158 }
159
160 /*
161 * The conversion factor consists of
162 * two components: a fixed value based
163 * on the absolute time unit, and a
164 * dynamic portion based on loading.
165 *
166 * Zero loading results in a out of range
167 * shift count. Accumulated usage is ignored
168 * during conversion and new usage deltas
169 * are discarded.
170 */
171 pset->pri_shift = sched_pri_shift - sched_load_shifts[load_now];
172 }
173 else {
174 pset->mach_factor = pset->load_average = 0;
175 pset->pri_shift = INT8_MAX;
176 nthreads = pset->run_count;
177 }
178
179 /*
180 * Sample total running threads.
181 */
182 sched_nrun = nthreads;
183
184 /*
185 * Compute old-style Mach load averages.
186 */
187 {
188 register int i;
189
190 for (i = 0; i < 3; i++) {
191 mach_factor[i] = ((mach_factor[i] * fract[i]) +
192 (factor_now * (LOAD_SCALE - fract[i]))) / LOAD_SCALE;
193
194 avenrun[i] = ((avenrun[i] * fract[i]) +
195 (average_now * (LOAD_SCALE - fract[i]))) / LOAD_SCALE;
196 }
197 }
198
199 /*
200 * Compute averages in other components.
201 */
202 for (avg = sched_average; avg->comp != NULL; ++avg) {
203 if (++avg->tick >= avg->period) {
204 (*avg->comp)(avg->param);
205 avg->tick = 0;
206 }
207 }
208 }