]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
91447636 | 2 | * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved. |
1c79356b A |
3 | * |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
e5568f75 A |
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. | |
1c79356b | 11 | * |
e5568f75 A |
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 | |
1c79356b A |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
e5568f75 A |
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. | |
1c79356b A |
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 | /* | |
1c79356b A |
53 | * Author: Avadis Tevanian, Jr. |
54 | * Date: 1986 | |
55 | * | |
91447636 | 56 | * Compute various averages. |
1c79356b A |
57 | */ |
58 | ||
91447636 | 59 | #include <mach/mach_types.h> |
1c79356b | 60 | |
1c79356b A |
61 | #include <kern/sched.h> |
62 | #include <kern/assert.h> | |
63 | #include <kern/processor.h> | |
64 | #include <kern/thread.h> | |
91447636 | 65 | |
9bccf70c A |
66 | uint32_t avenrun[3] = {0, 0, 0}; |
67 | uint32_t mach_factor[3] = {0, 0, 0}; | |
1c79356b A |
68 | |
69 | /* | |
70 | * Values are scaled by LOAD_SCALE, defined in processor_info.h | |
71 | */ | |
0b4e3aa0 A |
72 | #define base(n) ((n) << SCHED_TICK_SHIFT) |
73 | #define frac(n) (((base(n) - 1) * LOAD_SCALE) / base(n)) | |
74 | ||
9bccf70c | 75 | static uint32_t fract[3] = { |
0b4e3aa0 A |
76 | frac(5), /* 5 second average */ |
77 | frac(30), /* 30 second average */ | |
78 | frac(60), /* 1 minute average */ | |
1c79356b | 79 | }; |
9bccf70c | 80 | |
0b4e3aa0 A |
81 | #undef base |
82 | #undef frac | |
1c79356b | 83 | |
91447636 A |
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 | ||
1c79356b | 104 | void |
91447636 | 105 | compute_averages(void) |
1c79356b | 106 | { |
55e303ae | 107 | register processor_set_t pset = &default_pset; |
1c79356b | 108 | register int ncpus; |
55e303ae | 109 | register int nthreads, nshared; |
91447636 | 110 | sched_average_t avg; |
9bccf70c A |
111 | register uint32_t factor_now = 0; |
112 | register uint32_t average_now = 0; | |
113 | register uint32_t load_now = 0; | |
1c79356b | 114 | |
1c79356b A |
115 | if ((ncpus = pset->processor_count) > 0) { |
116 | /* | |
91447636 A |
117 | * Retrieve counts, ignoring |
118 | * the current thread. | |
1c79356b | 119 | */ |
91447636 | 120 | nthreads = pset->run_count - 1; |
55e303ae | 121 | nshared = pset->share_count; |
1c79356b | 122 | |
1c79356b A |
123 | /* |
124 | * Load average and mach factor calculations for | |
55e303ae | 125 | * those which ask about these things. |
1c79356b | 126 | */ |
9bccf70c | 127 | average_now = nthreads * LOAD_SCALE; |
1c79356b | 128 | |
55e303ae A |
129 | if (nthreads > ncpus) |
130 | factor_now = (ncpus * LOAD_SCALE) / (nthreads + 1); | |
131 | else | |
132 | factor_now = (ncpus - nthreads) * LOAD_SCALE; | |
133 | ||
1c79356b A |
134 | pset->mach_factor = ((pset->mach_factor << 2) + factor_now) / 5; |
135 | pset->load_average = ((pset->load_average << 2) + average_now) / 5; | |
136 | ||
137 | /* | |
91447636 A |
138 | * Compute the timeshare priority |
139 | * conversion factor based on loading. | |
1c79356b | 140 | */ |
55e303ae A |
141 | if (nshared > nthreads) |
142 | nshared = nthreads; | |
143 | ||
91447636 A |
144 | if (nshared > ncpus) { |
145 | if (ncpus > 1) | |
146 | load_now = nshared / ncpus; | |
147 | else | |
148 | load_now = nshared; | |
55e303ae | 149 | |
91447636 A |
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]; | |
1c79356b A |
166 | } |
167 | else { | |
168 | pset->mach_factor = pset->load_average = 0; | |
91447636 A |
169 | pset->pri_shift = INT8_MAX; |
170 | nthreads = pset->run_count; | |
1c79356b A |
171 | } |
172 | ||
91447636 A |
173 | /* |
174 | * Sample total running threads. | |
175 | */ | |
176 | sched_nrun = nthreads; | |
177 | ||
1c79356b | 178 | /* |
9bccf70c | 179 | * Compute old-style Mach load averages. |
1c79356b A |
180 | */ |
181 | { | |
9bccf70c | 182 | register int i; |
1c79356b A |
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 | } | |
9bccf70c A |
192 | |
193 | /* | |
91447636 | 194 | * Compute averages in other components. |
9bccf70c | 195 | */ |
91447636 A |
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 | } | |
9bccf70c | 201 | } |
1c79356b | 202 | } |