]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/sched_average.c
xnu-792.6.22.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_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * @OSF_COPYRIGHT@
24 */
25 /*
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
28 * All Rights Reserved.
29 *
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
35 *
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
39 *
40 * Carnegie Mellon requests users of this software to return to
41 *
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
46 *
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
49 */
50 /*
51 */
52 /*
53 * Author: Avadis Tevanian, Jr.
54 * Date: 1986
55 *
56 * Compute various averages.
57 */
58
59 #include <mach/mach_types.h>
60
61 #include <kern/sched.h>
62 #include <kern/assert.h>
63 #include <kern/processor.h>
64 #include <kern/thread.h>
65
66 uint32_t avenrun[3] = {0, 0, 0};
67 uint32_t mach_factor[3] = {0, 0, 0};
68
69 /*
70 * Values are scaled by LOAD_SCALE, defined in processor_info.h
71 */
72 #define base(n) ((n) << SCHED_TICK_SHIFT)
73 #define frac(n) (((base(n) - 1) * LOAD_SCALE) / base(n))
74
75 static uint32_t fract[3] = {
76 frac(5), /* 5 second average */
77 frac(30), /* 30 second average */
78 frac(60), /* 1 minute average */
79 };
80
81 #undef base
82 #undef frac
83
84 static unsigned int sched_nrun;
85
86 typedef void (*sched_avg_comp_t)(
87 void *param);
88
89 #define SCHED_AVG_SECS(n) ((n) << SCHED_TICK_SHIFT)
90
91 static struct sched_average {
92 sched_avg_comp_t comp;
93 void *param;
94 int period;
95 int tick;
96 } sched_average[] = {
97 { compute_averunnable, &sched_nrun, SCHED_AVG_SECS(5), 0 },
98 { compute_stack_target, NULL, SCHED_AVG_SECS(5), 1 },
99 { NULL, NULL, 0, 0 }
100 };
101
102 typedef struct sched_average *sched_average_t;
103
104 void
105 compute_averages(void)
106 {
107 register processor_set_t pset = &default_pset;
108 register int ncpus;
109 register int nthreads, nshared;
110 sched_average_t avg;
111 register uint32_t factor_now = 0;
112 register uint32_t average_now = 0;
113 register uint32_t load_now = 0;
114
115 if ((ncpus = pset->processor_count) > 0) {
116 /*
117 * Retrieve counts, ignoring
118 * the current thread.
119 */
120 nthreads = pset->run_count - 1;
121 nshared = pset->share_count;
122
123 /*
124 * Load average and mach factor calculations for
125 * those which ask about these things.
126 */
127 average_now = nthreads * LOAD_SCALE;
128
129 if (nthreads > ncpus)
130 factor_now = (ncpus * LOAD_SCALE) / (nthreads + 1);
131 else
132 factor_now = (ncpus - nthreads) * LOAD_SCALE;
133
134 pset->mach_factor = ((pset->mach_factor << 2) + factor_now) / 5;
135 pset->load_average = ((pset->load_average << 2) + average_now) / 5;
136
137 /*
138 * Compute the timeshare priority
139 * conversion factor based on loading.
140 */
141 if (nshared > nthreads)
142 nshared = nthreads;
143
144 if (nshared > ncpus) {
145 if (ncpus > 1)
146 load_now = nshared / ncpus;
147 else
148 load_now = nshared;
149
150 if (load_now > NRQS - 1)
151 load_now = NRQS - 1;
152 }
153
154 /*
155 * The conversion factor consists of
156 * two components: a fixed value based
157 * on the absolute time unit, and a
158 * dynamic portion based on loading.
159 *
160 * Zero loading results in a out of range
161 * shift count. Accumulated usage is ignored
162 * during conversion and new usage deltas
163 * are discarded.
164 */
165 pset->pri_shift = sched_pri_shift - sched_load_shifts[load_now];
166 }
167 else {
168 pset->mach_factor = pset->load_average = 0;
169 pset->pri_shift = INT8_MAX;
170 nthreads = pset->run_count;
171 }
172
173 /*
174 * Sample total running threads.
175 */
176 sched_nrun = nthreads;
177
178 /*
179 * Compute old-style Mach load averages.
180 */
181 {
182 register int i;
183
184 for (i = 0; i < 3; i++) {
185 mach_factor[i] = ((mach_factor[i] * fract[i]) +
186 (factor_now * (LOAD_SCALE - fract[i]))) / LOAD_SCALE;
187
188 avenrun[i] = ((avenrun[i] * fract[i]) +
189 (average_now * (LOAD_SCALE - fract[i]))) / LOAD_SCALE;
190 }
191 }
192
193 /*
194 * Compute averages in other components.
195 */
196 for (avg = sched_average; avg->comp != NULL; ++avg) {
197 if (++avg->tick >= avg->period) {
198 (*avg->comp)(avg->param);
199 avg->tick = 0;
200 }
201 }
202 }