]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/mach_factor.c
xnu-344.49.tar.gz
[apple/xnu.git] / osfmk / kern / mach_factor.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
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
13 * file.
14 *
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.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * @OSF_COPYRIGHT@
27 */
28 /*
29 * Mach Operating System
30 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
31 * All Rights Reserved.
32 *
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.
38 *
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.
42 *
43 * Carnegie Mellon requests users of this software to return to
44 *
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
49 *
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
52 */
53 /*
54 */
55 /*
56 * File: kern/mach_factor.c
57 * Author: Avadis Tevanian, Jr.
58 * Date: 1986
59 *
60 * Compute the Mach Factor.
61 */
62
63 #include <cpus.h>
64
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>
71 #if MACH_KERNEL
72 #include <mach/kern_return.h>
73 #include <mach/port.h>
74 #endif /* MACH_KERNEL */
75
76 uint32_t avenrun[3] = {0, 0, 0};
77 uint32_t mach_factor[3] = {0, 0, 0};
78
79 /*
80 * Values are scaled by LOAD_SCALE, defined in processor_info.h
81 */
82 #define base(n) ((n) << SCHED_TICK_SHIFT)
83 #define frac(n) (((base(n) - 1) * LOAD_SCALE) / base(n))
84
85 static uint32_t fract[3] = {
86 frac(5), /* 5 second average */
87 frac(30), /* 30 second average */
88 frac(60), /* 1 minute average */
89 };
90
91 #undef base
92 #undef frac
93
94 void
95 compute_mach_factor(void)
96 {
97 register processor_set_t pset;
98 register int ncpus;
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;
103
104 pset = &default_pset;
105 if ((ncpus = pset->processor_count) > 0) {
106 /*
107 * Number of threads running in pset.
108 */
109 nthreads = pset->run_count;
110
111 /*
112 * The current thread (running this calculation)
113 * doesn't count; it's always in the default pset.
114 */
115 if (pset == &default_pset)
116 nthreads -= 1;
117
118 if (nthreads > ncpus) {
119 factor_now = (ncpus * LOAD_SCALE) / (nthreads + 1);
120 load_now = (nthreads << SCHED_SHIFT) / ncpus;
121 }
122 else
123 factor_now = (ncpus - nthreads) * LOAD_SCALE;
124
125 /*
126 * Load average and mach factor calculations for
127 * those that ask about these things.
128 */
129 average_now = nthreads * LOAD_SCALE;
130
131 pset->mach_factor = ((pset->mach_factor << 2) + factor_now) / 5;
132 pset->load_average = ((pset->load_average << 2) + average_now) / 5;
133
134 /*
135 * sched_load is used by the timesharing algorithm.
136 */
137 pset->sched_load = (pset->sched_load + load_now) >> 1;
138 }
139 else {
140 pset->mach_factor = pset->load_average = 0;
141 pset->sched_load = 0;
142 }
143
144 /*
145 * Compute old-style Mach load averages.
146 */
147 {
148 register int i;
149
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;
153
154 avenrun[i] = ((avenrun[i] * fract[i]) +
155 (average_now * (LOAD_SCALE - fract[i]))) / LOAD_SCALE;
156 }
157 }
158
159 /*
160 * Call out to BSD for averunnable.
161 */
162 {
163 #define AVGTICK_PERIOD (5 << SCHED_TICK_SHIFT)
164 static uint32_t avgtick_count;
165 extern void compute_averunnable(
166 int nrun);
167
168 if (++avgtick_count == 1)
169 compute_averunnable(nthreads);
170 else
171 if (avgtick_count >= AVGTICK_PERIOD)
172 avgtick_count = 0;
173 }
174 }