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