]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/sched_average.c
xnu-1699.32.7.tar.gz
[apple/xnu.git] / osfmk / kern / sched_average.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2007 Apple Computer, Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * @OSF_COPYRIGHT@
30 */
31/*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
35 *
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
41 *
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56/*
57 */
58/*
1c79356b
A
59 * Author: Avadis Tevanian, Jr.
60 * Date: 1986
61 *
91447636 62 * Compute various averages.
1c79356b
A
63 */
64
91447636 65#include <mach/mach_types.h>
1c79356b 66
1c79356b
A
67#include <kern/sched.h>
68#include <kern/assert.h>
69#include <kern/processor.h>
70#include <kern/thread.h>
91447636 71
9bccf70c
A
72uint32_t avenrun[3] = {0, 0, 0};
73uint32_t mach_factor[3] = {0, 0, 0};
1c79356b 74
6d2010ae 75#if defined(CONFIG_SCHED_TRADITIONAL)
1c79356b
A
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 90
6d2010ae
A
91#endif /* CONFIG_SCHED_TRADITIONAL */
92
91447636
A
93static unsigned int sched_nrun;
94
95typedef void (*sched_avg_comp_t)(
96 void *param);
97
91447636
A
98static struct sched_average {
99 sched_avg_comp_t comp;
100 void *param;
6d2010ae
A
101 int period; /* in seconds */
102 uint64_t deadline;
91447636 103} sched_average[] = {
6d2010ae
A
104 { compute_averunnable, &sched_nrun, 5, 0 },
105 { compute_stack_target, NULL, 5, 1 },
106 { compute_memory_pressure, NULL, 1, 0 },
107 { compute_zone_gc_throttle, NULL, 1, 0 },
108 { compute_pmap_gc_throttle, NULL, 60, 0 },
91447636
A
109 { NULL, NULL, 0, 0 }
110};
111
112typedef struct sched_average *sched_average_t;
113
1c79356b 114void
91447636 115compute_averages(void)
1c79356b 116{
2d21ac55
A
117 int ncpus, nthreads, nshared;
118 uint32_t factor_now, average_now, load_now = 0;
119 sched_average_t avg;
6d2010ae
A
120 uint64_t abstime;
121
2d21ac55
A
122 /*
123 * Retrieve counts, ignoring
124 * the current thread.
125 */
126 ncpus = processor_avail_count;
127 nthreads = sched_run_count - 1;
128 nshared = sched_share_count;
129
130 /*
131 * Load average and mach factor calculations for
132 * those which ask about these things.
133 */
134 average_now = nthreads * LOAD_SCALE;
135
136 if (nthreads > ncpus)
137 factor_now = (ncpus * LOAD_SCALE) / (nthreads + 1);
138 else
139 factor_now = (ncpus - nthreads) * LOAD_SCALE;
140
141 sched_mach_factor = ((sched_mach_factor << 2) + factor_now) / 5;
142 sched_load_average = ((sched_load_average << 2) + average_now) / 5;
143
144 /*
145 * Compute the timeshare priority
146 * conversion factor based on loading.
147 */
148 if (nshared > nthreads)
149 nshared = nthreads;
150
151 if (nshared > ncpus) {
152 if (ncpus > 1)
153 load_now = nshared / ncpus;
55e303ae 154 else
2d21ac55 155 load_now = nshared;
91447636 156
2d21ac55
A
157 if (load_now > NRQS - 1)
158 load_now = NRQS - 1;
1c79356b
A
159 }
160
6d2010ae
A
161 /*
162 * Sample total running threads.
163 */
164 sched_nrun = nthreads;
165
166#if defined(CONFIG_SCHED_TRADITIONAL)
167
2d21ac55
A
168 /*
169 * The conversion factor consists of
170 * two components: a fixed value based
171 * on the absolute time unit, and a
172 * dynamic portion based on loading.
173 *
174 * Zero loading results in a out of range
175 * shift count. Accumulated usage is ignored
176 * during conversion and new usage deltas
177 * are discarded.
178 */
179 sched_pri_shift = sched_fixed_shift - sched_load_shifts[load_now];
180
1c79356b 181 /*
9bccf70c 182 * Compute old-style Mach load averages.
1c79356b
A
183 */
184 {
9bccf70c 185 register int i;
1c79356b
A
186
187 for (i = 0; i < 3; i++) {
188 mach_factor[i] = ((mach_factor[i] * fract[i]) +
189 (factor_now * (LOAD_SCALE - fract[i]))) / LOAD_SCALE;
190
191 avenrun[i] = ((avenrun[i] * fract[i]) +
192 (average_now * (LOAD_SCALE - fract[i]))) / LOAD_SCALE;
193 }
194 }
6d2010ae 195#endif /* CONFIG_SCHED_TRADITIONAL */
9bccf70c
A
196
197 /*
91447636 198 * Compute averages in other components.
9bccf70c 199 */
6d2010ae 200 abstime = mach_absolute_time();
91447636 201 for (avg = sched_average; avg->comp != NULL; ++avg) {
6d2010ae 202 if (abstime >= avg->deadline) {
91447636 203 (*avg->comp)(avg->param);
6d2010ae 204 avg->deadline = abstime + avg->period * sched_one_second_interval;
91447636 205 }
9bccf70c 206 }
1c79356b 207}