]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/mach_factor.c
xnu-517.tar.gz
[apple/xnu.git] / osfmk / kern / mach_factor.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
43866e37 6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
1c79356b 7 *
43866e37
A
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
1c79356b
A
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
43866e37
A
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.
1c79356b
A
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
9bccf70c
A
76uint32_t avenrun[3] = {0, 0, 0};
77uint32_t mach_factor[3] = {0, 0, 0};
1c79356b
A
78
79/*
80 * Values are scaled by LOAD_SCALE, defined in processor_info.h
81 */
0b4e3aa0
A
82#define base(n) ((n) << SCHED_TICK_SHIFT)
83#define frac(n) (((base(n) - 1) * LOAD_SCALE) / base(n))
84
9bccf70c 85static uint32_t fract[3] = {
0b4e3aa0
A
86 frac(5), /* 5 second average */
87 frac(30), /* 30 second average */
88 frac(60), /* 1 minute average */
1c79356b 89};
9bccf70c 90
0b4e3aa0
A
91#undef base
92#undef frac
1c79356b
A
93
94void
95compute_mach_factor(void)
96{
55e303ae 97 register processor_set_t pset = &default_pset;
1c79356b 98 register int ncpus;
55e303ae 99 register int nthreads, nshared;
9bccf70c
A
100 register uint32_t factor_now = 0;
101 register uint32_t average_now = 0;
102 register uint32_t load_now = 0;
1c79356b 103
1c79356b
A
104 if ((ncpus = pset->processor_count) > 0) {
105 /*
55e303ae 106 * Retrieve thread counts.
1c79356b 107 */
9bccf70c 108 nthreads = pset->run_count;
55e303ae 109 nshared = pset->share_count;
1c79356b
A
110
111 /*
55e303ae 112 * Don't include the current thread.
1c79356b 113 */
55e303ae 114 nthreads -= 1;
1c79356b
A
115
116 /*
117 * Load average and mach factor calculations for
55e303ae 118 * those which ask about these things.
1c79356b 119 */
9bccf70c 120 average_now = nthreads * LOAD_SCALE;
1c79356b 121
55e303ae
A
122 if (nthreads > ncpus)
123 factor_now = (ncpus * LOAD_SCALE) / (nthreads + 1);
124 else
125 factor_now = (ncpus - nthreads) * LOAD_SCALE;
126
1c79356b
A
127 pset->mach_factor = ((pset->mach_factor << 2) + factor_now) / 5;
128 pset->load_average = ((pset->load_average << 2) + average_now) / 5;
129
130 /*
55e303ae
A
131 * Compute the load factor used by the timesharing
132 * algorithm.
1c79356b 133 */
55e303ae
A
134 if (nshared > nthreads)
135 nshared = nthreads;
136
137 if (nshared > ncpus)
138 load_now = (nshared << SCHED_SHIFT) / ncpus;
139
1c79356b
A
140 pset->sched_load = (pset->sched_load + load_now) >> 1;
141 }
142 else {
143 pset->mach_factor = pset->load_average = 0;
144 pset->sched_load = 0;
145 }
146
1c79356b 147 /*
9bccf70c 148 * Compute old-style Mach load averages.
1c79356b
A
149 */
150 {
9bccf70c 151 register int i;
1c79356b
A
152
153 for (i = 0; i < 3; i++) {
154 mach_factor[i] = ((mach_factor[i] * fract[i]) +
155 (factor_now * (LOAD_SCALE - fract[i]))) / LOAD_SCALE;
156
157 avenrun[i] = ((avenrun[i] * fract[i]) +
158 (average_now * (LOAD_SCALE - fract[i]))) / LOAD_SCALE;
159 }
160 }
9bccf70c
A
161
162 /*
163 * Call out to BSD for averunnable.
164 */
165 {
166#define AVGTICK_PERIOD (5 << SCHED_TICK_SHIFT)
167 static uint32_t avgtick_count;
168 extern void compute_averunnable(
169 int nrun);
170
171 if (++avgtick_count == 1)
172 compute_averunnable(nthreads);
173 else
174 if (avgtick_count >= AVGTICK_PERIOD)
175 avgtick_count = 0;
176 }
1c79356b 177}