]>
git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/mach_factor.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
29 * Mach Operating System
30 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
31 * All Rights Reserved.
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
43 * Carnegie Mellon requests users of this software to return to
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
56 * File: kern/mach_factor.c
57 * Author: Avadis Tevanian, Jr.
60 * Compute the Mach Factor.
65 #include <mach/machine.h>
66 #include <mach/processor_info.h>
67 #include <kern/sched.h>
68 #include <kern/assert.h>
69 #include <kern/processor.h>
70 #include <kern/thread.h>
72 #include <mach/kern_return.h>
73 #include <mach/port.h>
74 #endif /* MACH_KERNEL */
76 uint32_t avenrun
[3] = {0, 0, 0};
77 uint32_t mach_factor
[3] = {0, 0, 0};
80 * Values are scaled by LOAD_SCALE, defined in processor_info.h
82 #define base(n) ((n) << SCHED_TICK_SHIFT)
83 #define frac(n) (((base(n) - 1) * LOAD_SCALE) / base(n))
85 static uint32_t fract
[3] = {
86 frac(5), /* 5 second average */
87 frac(30), /* 30 second average */
88 frac(60), /* 1 minute average */
95 compute_mach_factor(void)
97 register processor_set_t pset
;
99 register int nthreads
;
100 register uint32_t factor_now
= 0;
101 register uint32_t average_now
= 0;
102 register uint32_t load_now
= 0;
104 pset
= &default_pset
;
105 if ((ncpus
= pset
->processor_count
) > 0) {
107 * Number of threads running in pset.
109 nthreads
= pset
->run_count
;
112 * The current thread (running this calculation)
113 * doesn't count; it's always in the default pset.
115 if (pset
== &default_pset
)
118 if (nthreads
> ncpus
) {
119 factor_now
= (ncpus
* LOAD_SCALE
) / (nthreads
+ 1);
120 load_now
= (nthreads
<< SCHED_SHIFT
) / ncpus
;
123 factor_now
= (ncpus
- nthreads
) * LOAD_SCALE
;
126 * Load average and mach factor calculations for
127 * those that ask about these things.
129 average_now
= nthreads
* LOAD_SCALE
;
131 pset
->mach_factor
= ((pset
->mach_factor
<< 2) + factor_now
) / 5;
132 pset
->load_average
= ((pset
->load_average
<< 2) + average_now
) / 5;
135 * sched_load is used by the timesharing algorithm.
137 pset
->sched_load
= (pset
->sched_load
+ load_now
) >> 1;
140 pset
->mach_factor
= pset
->load_average
= 0;
141 pset
->sched_load
= 0;
145 * Compute old-style Mach load averages.
150 for (i
= 0; i
< 3; i
++) {
151 mach_factor
[i
] = ((mach_factor
[i
] * fract
[i
]) +
152 (factor_now
* (LOAD_SCALE
- fract
[i
]))) / LOAD_SCALE
;
154 avenrun
[i
] = ((avenrun
[i
] * fract
[i
]) +
155 (average_now
* (LOAD_SCALE
- fract
[i
]))) / LOAD_SCALE
;
160 * Call out to BSD for averunnable.
163 #define AVGTICK_PERIOD (5 << SCHED_TICK_SHIFT)
164 static uint32_t avgtick_count
;
165 extern void compute_averunnable(
168 if (++avgtick_count
== 1)
169 compute_averunnable(nthreads
);
171 if (avgtick_count
>= AVGTICK_PERIOD
)