]>
git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/sched_average.c
bfa1d2ef0d3a4068dd9ea0c095b6e75b7b5250a3
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
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
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
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.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
34 * Mach Operating System
35 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
36 * All Rights Reserved.
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.
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.
48 * Carnegie Mellon requests users of this software to return to
50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
55 * any improvements or extensions that they make and grant Carnegie Mellon
56 * the rights to redistribute these changes.
61 * Author: Avadis Tevanian, Jr.
64 * Compute various averages.
67 #include <mach/mach_types.h>
69 #include <kern/sched.h>
70 #include <kern/assert.h>
71 #include <kern/processor.h>
72 #include <kern/thread.h>
74 uint32_t avenrun
[3] = {0, 0, 0};
75 uint32_t mach_factor
[3] = {0, 0, 0};
78 * Values are scaled by LOAD_SCALE, defined in processor_info.h
80 #define base(n) ((n) << SCHED_TICK_SHIFT)
81 #define frac(n) (((base(n) - 1) * LOAD_SCALE) / base(n))
83 static uint32_t fract
[3] = {
84 frac(5), /* 5 second average */
85 frac(30), /* 30 second average */
86 frac(60), /* 1 minute average */
92 static unsigned int sched_nrun
;
94 typedef void (*sched_avg_comp_t
)(
97 #define SCHED_AVG_SECS(n) ((n) << SCHED_TICK_SHIFT)
99 static struct sched_average
{
100 sched_avg_comp_t comp
;
104 } sched_average
[] = {
105 { compute_averunnable
, &sched_nrun
, SCHED_AVG_SECS(5), 0 },
106 { compute_stack_target
, NULL
, SCHED_AVG_SECS(5), 1 },
110 typedef struct sched_average
*sched_average_t
;
113 compute_averages(void)
115 register processor_set_t pset
= &default_pset
;
117 register int nthreads
, nshared
;
119 register uint32_t factor_now
= 0;
120 register uint32_t average_now
= 0;
121 register uint32_t load_now
= 0;
123 if ((ncpus
= pset
->processor_count
) > 0) {
125 * Retrieve counts, ignoring
126 * the current thread.
128 nthreads
= pset
->run_count
- 1;
129 nshared
= pset
->share_count
;
132 * Load average and mach factor calculations for
133 * those which ask about these things.
135 average_now
= nthreads
* LOAD_SCALE
;
137 if (nthreads
> ncpus
)
138 factor_now
= (ncpus
* LOAD_SCALE
) / (nthreads
+ 1);
140 factor_now
= (ncpus
- nthreads
) * LOAD_SCALE
;
142 pset
->mach_factor
= ((pset
->mach_factor
<< 2) + factor_now
) / 5;
143 pset
->load_average
= ((pset
->load_average
<< 2) + average_now
) / 5;
146 * Compute the timeshare priority
147 * conversion factor based on loading.
149 if (nshared
> nthreads
)
152 if (nshared
> ncpus
) {
154 load_now
= nshared
/ ncpus
;
158 if (load_now
> NRQS
- 1)
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.
168 * Zero loading results in a out of range
169 * shift count. Accumulated usage is ignored
170 * during conversion and new usage deltas
173 pset
->pri_shift
= sched_pri_shift
- sched_load_shifts
[load_now
];
176 pset
->mach_factor
= pset
->load_average
= 0;
177 pset
->pri_shift
= INT8_MAX
;
178 nthreads
= pset
->run_count
;
182 * Sample total running threads.
184 sched_nrun
= nthreads
;
187 * Compute old-style Mach load averages.
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
;
196 avenrun
[i
] = ((avenrun
[i
] * fract
[i
]) +
197 (average_now
* (LOAD_SCALE
- fract
[i
]))) / LOAD_SCALE
;
202 * Compute averages in other components.
204 for (avg
= sched_average
; avg
->comp
!= NULL
; ++avg
) {
205 if (++avg
->tick
>= avg
->period
) {
206 (*avg
->comp
)(avg
->param
);