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