]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
91447636 | 2 | * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved. |
1c79356b | 3 | * |
8ad349bb | 4 | * @APPLE_LICENSE_OSREFERENCE_HEADER_START@ |
1c79356b | 5 | * |
8ad349bb 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 | |
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@ | |
1c79356b A |
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 | /* | |
1c79356b A |
61 | * Author: Avadis Tevanian, Jr. |
62 | * Date: 1986 | |
63 | * | |
91447636 | 64 | * Compute various averages. |
1c79356b A |
65 | */ |
66 | ||
91447636 | 67 | #include <mach/mach_types.h> |
1c79356b | 68 | |
1c79356b A |
69 | #include <kern/sched.h> |
70 | #include <kern/assert.h> | |
71 | #include <kern/processor.h> | |
72 | #include <kern/thread.h> | |
91447636 | 73 | |
9bccf70c A |
74 | uint32_t avenrun[3] = {0, 0, 0}; |
75 | uint32_t mach_factor[3] = {0, 0, 0}; | |
1c79356b A |
76 | |
77 | /* | |
78 | * Values are scaled by LOAD_SCALE, defined in processor_info.h | |
79 | */ | |
0b4e3aa0 A |
80 | #define base(n) ((n) << SCHED_TICK_SHIFT) |
81 | #define frac(n) (((base(n) - 1) * LOAD_SCALE) / base(n)) | |
82 | ||
9bccf70c | 83 | static uint32_t fract[3] = { |
0b4e3aa0 A |
84 | frac(5), /* 5 second average */ |
85 | frac(30), /* 30 second average */ | |
86 | frac(60), /* 1 minute average */ | |
1c79356b | 87 | }; |
9bccf70c | 88 | |
0b4e3aa0 A |
89 | #undef base |
90 | #undef frac | |
1c79356b | 91 | |
91447636 A |
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 | ||
1c79356b | 112 | void |
91447636 | 113 | compute_averages(void) |
1c79356b | 114 | { |
55e303ae | 115 | register processor_set_t pset = &default_pset; |
1c79356b | 116 | register int ncpus; |
55e303ae | 117 | register int nthreads, nshared; |
91447636 | 118 | sched_average_t avg; |
9bccf70c A |
119 | register uint32_t factor_now = 0; |
120 | register uint32_t average_now = 0; | |
121 | register uint32_t load_now = 0; | |
1c79356b | 122 | |
1c79356b A |
123 | if ((ncpus = pset->processor_count) > 0) { |
124 | /* | |
91447636 A |
125 | * Retrieve counts, ignoring |
126 | * the current thread. | |
1c79356b | 127 | */ |
91447636 | 128 | nthreads = pset->run_count - 1; |
55e303ae | 129 | nshared = pset->share_count; |
1c79356b | 130 | |
1c79356b A |
131 | /* |
132 | * Load average and mach factor calculations for | |
55e303ae | 133 | * those which ask about these things. |
1c79356b | 134 | */ |
9bccf70c | 135 | average_now = nthreads * LOAD_SCALE; |
1c79356b | 136 | |
55e303ae A |
137 | if (nthreads > ncpus) |
138 | factor_now = (ncpus * LOAD_SCALE) / (nthreads + 1); | |
139 | else | |
140 | factor_now = (ncpus - nthreads) * LOAD_SCALE; | |
141 | ||
1c79356b A |
142 | pset->mach_factor = ((pset->mach_factor << 2) + factor_now) / 5; |
143 | pset->load_average = ((pset->load_average << 2) + average_now) / 5; | |
144 | ||
145 | /* | |
91447636 A |
146 | * Compute the timeshare priority |
147 | * conversion factor based on loading. | |
1c79356b | 148 | */ |
55e303ae A |
149 | if (nshared > nthreads) |
150 | nshared = nthreads; | |
151 | ||
91447636 A |
152 | if (nshared > ncpus) { |
153 | if (ncpus > 1) | |
154 | load_now = nshared / ncpus; | |
155 | else | |
156 | load_now = nshared; | |
55e303ae | 157 | |
91447636 A |
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]; | |
1c79356b A |
174 | } |
175 | else { | |
176 | pset->mach_factor = pset->load_average = 0; | |
91447636 A |
177 | pset->pri_shift = INT8_MAX; |
178 | nthreads = pset->run_count; | |
1c79356b A |
179 | } |
180 | ||
91447636 A |
181 | /* |
182 | * Sample total running threads. | |
183 | */ | |
184 | sched_nrun = nthreads; | |
185 | ||
1c79356b | 186 | /* |
9bccf70c | 187 | * Compute old-style Mach load averages. |
1c79356b A |
188 | */ |
189 | { | |
9bccf70c | 190 | register int i; |
1c79356b A |
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 | } | |
9bccf70c A |
200 | |
201 | /* | |
91447636 | 202 | * Compute averages in other components. |
9bccf70c | 203 | */ |
91447636 A |
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 | } | |
9bccf70c | 209 | } |
1c79356b | 210 | } |