1/*
2 * Copyright (c) 2000-2007 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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/*
59 *	Author:	Avadis Tevanian, Jr.
60 *	Date:	1986
61 *
62 *	Compute various averages.
63 */
64
65#include <mach/mach_types.h>
66
67#include <kern/sched.h>
68#include <kern/assert.h>
69#include <kern/processor.h>
70#include <kern/thread.h>
71
72uint32_t	avenrun[3] = {0, 0, 0};
73uint32_t	mach_factor[3] = {0, 0, 0};
74
75#if defined(CONFIG_SCHED_TRADITIONAL)
76/*
77 * Values are scaled by LOAD_SCALE, defined in processor_info.h
78 */
79#define base(n)		((n) << SCHED_TICK_SHIFT)
80#define frac(n)		(((base(n) - 1) * LOAD_SCALE) /	base(n))
81
82static uint32_t		fract[3] = {
83	frac(5),		/* 5 second average */
84	frac(30),		/* 30 second average */
85	frac(60),		/* 1 minute average */
86};
87
88#undef base
89#undef frac
90
91#endif /* CONFIG_SCHED_TRADITIONAL */
92
93static unsigned int		sched_nrun;
94
95typedef void	(*sched_avg_comp_t)(
96					void			*param);
97
98static struct sched_average {
99	sched_avg_comp_t	comp;
100	void				*param;
101	int					period; /* in seconds */
102	uint64_t			deadline;
103} sched_average[] = {
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, 60, 0 },
108	{ compute_pageout_gc_throttle, NULL, 1, 0 },
109	{ compute_pmap_gc_throttle, NULL, 60, 0 },
110	{ NULL, NULL, 0, 0 }
111};
112
113typedef struct sched_average	*sched_average_t;
114
115void
116compute_averages(void)
117{
118	int					ncpus, nthreads, nshared;
119	uint32_t			factor_now, average_now, load_now = 0;
120	sched_average_t		avg;
121	uint64_t			abstime;
122
123	/*
124	 *	Retrieve counts, ignoring
125	 *	the current thread.
126	 */
127	ncpus = processor_avail_count;
128	nthreads = sched_run_count - 1;
129	nshared = sched_share_count;
130
131	/*
132	 *	Load average and mach factor calculations for
133	 *	those which ask about these things.
134	 */
135	average_now = nthreads * LOAD_SCALE;
136
137	if (nthreads > ncpus)
138		factor_now = (ncpus * LOAD_SCALE) / (nthreads + 1);
139	else
140		factor_now = (ncpus - nthreads) * LOAD_SCALE;
141
142	sched_mach_factor =	((sched_mach_factor << 2) + factor_now) / 5;
143	sched_load_average = ((sched_load_average << 2) + average_now) / 5;
144
145	/*
146	 *	Compute the timeshare priority
147	 *	conversion factor based on loading.
148	 */
149	if (nshared > nthreads)
150		nshared = nthreads;
151
152	if (nshared > ncpus) {
153		if (ncpus > 1)
154			load_now = nshared / ncpus;
155		else
156			load_now = nshared;
157
158		if (load_now > NRQS - 1)
159			load_now = NRQS - 1;
160	}
161
162	/*
163	 *	Sample total running threads.
164	 */
165	sched_nrun = nthreads;
166
167#if defined(CONFIG_SCHED_TRADITIONAL)
168
169	/*
170	 *	The conversion factor consists of
171	 *	two components: a fixed value based
172	 *	on the absolute time unit, and a
173	 *	dynamic portion based on loading.
174	 *
175	 *	Zero loading results in a out of range
176	 *	shift count.  Accumulated usage is ignored
177	 *	during conversion and new usage deltas
178	 *	are discarded.
179	 */
180	sched_pri_shift = sched_fixed_shift - sched_load_shifts[load_now];
181
182	/*
183	 * Compute old-style Mach load averages.
184	 */
185	{
186		register int		i;
187
188		for (i = 0; i < 3; i++) {
189			mach_factor[i] = ((mach_factor[i] * fract[i]) +
190						(factor_now * (LOAD_SCALE - fract[i]))) / LOAD_SCALE;
191
192			avenrun[i] = ((avenrun[i] * fract[i]) +
193						(average_now * (LOAD_SCALE - fract[i]))) / LOAD_SCALE;
194		}
195	}
196#endif /* CONFIG_SCHED_TRADITIONAL */
197
198	/*
199	 *	Compute averages in other components.
200	 */
201	abstime = mach_absolute_time();
202	for (avg = sched_average; avg->comp != NULL; ++avg) {
203		if (abstime >= avg->deadline) {
204			(*avg->comp)(avg->param);
205			avg->deadline = abstime + avg->period * sched_one_second_interval;
206		}
207	}
208}
209