]>
git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/sched_average.c
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
28 * All Rights Reserved.
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.
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.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
53 * Author: Avadis Tevanian, Jr.
56 * Compute various averages.
59 #include <mach/mach_types.h>
61 #include <kern/sched.h>
62 #include <kern/assert.h>
63 #include <kern/processor.h>
64 #include <kern/thread.h>
66 uint32_t avenrun
[3] = {0, 0, 0};
67 uint32_t mach_factor
[3] = {0, 0, 0};
70 * Values are scaled by LOAD_SCALE, defined in processor_info.h
72 #define base(n) ((n) << SCHED_TICK_SHIFT)
73 #define frac(n) (((base(n) - 1) * LOAD_SCALE) / base(n))
75 static uint32_t fract
[3] = {
76 frac(5), /* 5 second average */
77 frac(30), /* 30 second average */
78 frac(60), /* 1 minute average */
84 static unsigned int sched_nrun
;
86 typedef void (*sched_avg_comp_t
)(
89 #define SCHED_AVG_SECS(n) ((n) << SCHED_TICK_SHIFT)
91 static struct sched_average
{
92 sched_avg_comp_t comp
;
97 { compute_averunnable
, &sched_nrun
, SCHED_AVG_SECS(5), 0 },
98 { compute_stack_target
, NULL
, SCHED_AVG_SECS(5), 1 },
102 typedef struct sched_average
*sched_average_t
;
105 compute_averages(void)
107 register processor_set_t pset
= &default_pset
;
109 register int nthreads
, nshared
;
111 register uint32_t factor_now
= 0;
112 register uint32_t average_now
= 0;
113 register uint32_t load_now
= 0;
115 if ((ncpus
= pset
->processor_count
) > 0) {
117 * Retrieve counts, ignoring
118 * the current thread.
120 nthreads
= pset
->run_count
- 1;
121 nshared
= pset
->share_count
;
124 * Load average and mach factor calculations for
125 * those which ask about these things.
127 average_now
= nthreads
* LOAD_SCALE
;
129 if (nthreads
> ncpus
)
130 factor_now
= (ncpus
* LOAD_SCALE
) / (nthreads
+ 1);
132 factor_now
= (ncpus
- nthreads
) * LOAD_SCALE
;
134 pset
->mach_factor
= ((pset
->mach_factor
<< 2) + factor_now
) / 5;
135 pset
->load_average
= ((pset
->load_average
<< 2) + average_now
) / 5;
138 * Compute the timeshare priority
139 * conversion factor based on loading.
141 if (nshared
> nthreads
)
144 if (nshared
> ncpus
) {
146 load_now
= nshared
/ ncpus
;
150 if (load_now
> NRQS
- 1)
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.
160 * Zero loading results in a out of range
161 * shift count. Accumulated usage is ignored
162 * during conversion and new usage deltas
165 pset
->pri_shift
= sched_pri_shift
- sched_load_shifts
[load_now
];
168 pset
->mach_factor
= pset
->load_average
= 0;
169 pset
->pri_shift
= INT8_MAX
;
170 nthreads
= pset
->run_count
;
174 * Sample total running threads.
176 sched_nrun
= nthreads
;
179 * Compute old-style Mach load averages.
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
;
188 avenrun
[i
] = ((avenrun
[i
] * fract
[i
]) +
189 (average_now
* (LOAD_SCALE
- fract
[i
]))) / LOAD_SCALE
;
194 * Compute averages in other components.
196 for (avg
= sched_average
; avg
->comp
!= NULL
; ++avg
) {
197 if (++avg
->tick
>= avg
->period
) {
198 (*avg
->comp
)(avg
->param
);