]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/mach_clock.c
xnu-792.17.14.tar.gz
[apple/xnu.git] / osfmk / kern / mach_clock.c
CommitLineData
1c79356b 1/*
91447636 2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
1c79356b 3 *
8f6c56a5 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
8f6c56a5
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.
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
8ad349bb 24 * limitations under the License.
8f6c56a5
A
25 *
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 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 * File: clock_prim.c
60 * Author: Avadis Tevanian, Jr.
61 * Date: 1986
62 *
63 * Clock primitives.
64 */
1c79356b
A
65#include <mach_prof.h>
66#include <gprof.h>
67
68#include <mach/boolean.h>
69#include <mach/machine.h>
70#include <mach/time_value.h>
71#include <mach/vm_param.h>
72#include <mach/vm_prot.h>
73#include <kern/clock.h>
74#include <kern/counters.h>
75#include <kern/cpu_number.h>
76#include <kern/host.h>
77#include <kern/lock.h>
78#include <kern/mach_param.h>
79#include <kern/misc_protos.h>
80#include <kern/processor.h>
81#include <kern/profile.h>
82#include <kern/sched.h>
83#include <kern/sched_prim.h>
84#include <kern/spl.h>
85#include <kern/thread.h>
1c79356b 86#include <vm/vm_kern.h> /* kernel_map */
1c79356b
A
87
88#include <mach/clock_server.h>
89#include <mach/clock_priv_server.h>
90#include <mach/mach_host_server.h>
91
92#include <profiling/profile-mk.h>
93
1c79356b
A
94boolean_t profile_kernel_services = TRUE; /* Indicates wether or not we
95 * account kernel services
91447636 96
1c79356b 97 * samples for user task */
91447636
A
98#ifdef MACH_BSD
99extern void bsd_hardclock(
100 boolean_t usermode,
101 natural_t pc,
102 int numticks);
103#endif /* MACH_BSD */
1c79356b
A
104
105/*
106 * Hertz rate clock interrupt servicing. Primarily used to
107 * update CPU statistics, recompute thread priority, and to
108 * do profiling
109 */
110void
111hertz_tick(
91447636
A
112#if STAT_TIME
113 natural_t ticks,
114#endif /* STAT_TIME */
115 boolean_t usermode,
116 natural_t pc)
1c79356b 117{
91447636
A
118 processor_t processor = current_processor();
119 thread_t thread = current_thread();
120 int state;
1c79356b
A
121#if MACH_PROF
122#ifdef __MACHO__
91447636
A
123#define ETEXT etext
124 extern long etext;
1c79356b 125#else
91447636
A
126#define ETEXT &etext
127 extern char etext;
1c79356b 128#endif
91447636 129 boolean_t inkernel;
1c79356b
A
130#endif /* MACH_PROF */
131#if GPROF
132 struct profile_vars *pv;
133 prof_uptrint_t s;
134#endif
135
136#ifdef lint
137 pc++;
138#endif /* lint */
139
1c79356b
A
140 /*
141 * The system startup sequence initializes the clock
142 * before kicking off threads. So it's possible,
143 * especially when debugging, to wind up here with
144 * no thread to bill against. So ignore the tick.
145 */
0b4e3aa0 146 if (thread == THREAD_NULL)
1c79356b 147 return;
1c79356b
A
148
149#if MACH_PROF
150 inkernel = !usermode && (pc < (unsigned int)ETEXT);
151#endif /* MACH_PROF */
152
153 /*
154 * Hertz processing performed by all processors
155 * includes statistics gathering, state tracking,
156 * and quantum updating.
157 */
158 counter(c_clock_ticks++);
159
160#if GPROF
91447636 161 pv = PROFILE_VARS(cpu_number());
1c79356b
A
162#endif
163
164 if (usermode) {
91447636 165 TIMER_BUMP(&thread->user_timer, ticks);
1c79356b
A
166 if (thread->priority < BASEPRI_DEFAULT)
167 state = CPU_STATE_NICE;
168 else
1c79356b
A
169 state = CPU_STATE_USER;
170#if GPROF
171 if (pv->active)
172 PROF_CNT_INC(pv->stats.user_ticks);
173#endif
174 }
175 else {
91447636 176 TIMER_BUMP(&thread->system_timer, ticks);
1c79356b 177
91447636 178 state = processor->state;
55e303ae 179 if ( state == PROCESSOR_IDLE ||
91447636
A
180 state == PROCESSOR_DISPATCHING)
181 state = CPU_STATE_IDLE;
182 else
183 if (thread->options & TH_OPT_DELAYIDLE)
1c79356b 184 state = CPU_STATE_IDLE;
55e303ae 185 else
1c79356b 186 state = CPU_STATE_SYSTEM;
1c79356b
A
187#if GPROF
188 if (pv->active) {
189 if (state == CPU_STATE_SYSTEM)
190 PROF_CNT_INC(pv->stats.kernel_ticks);
191 else
192 PROF_CNT_INC(pv->stats.idle_ticks);
193
194 if ((prof_uptrint_t)pc < _profile_vars.profil_info.lowpc)
195 PROF_CNT_INC(pv->stats.too_low);
196 else {
197 s = (prof_uptrint_t)pc - _profile_vars.profil_info.lowpc;
198 if (s < pv->profil_info.text_len) {
199 LHISTCOUNTER *ptr = (LHISTCOUNTER *) pv->profil_buf;
200 LPROF_CNT_INC(ptr[s / HISTFRACTION]);
201 }
202 else
203 PROF_CNT_INC(pv->stats.too_high);
204 }
205 }
206#endif
207 }
208
91447636 209 PROCESSOR_DATA(processor, cpu_ticks[state]++);
1c79356b 210
1c79356b 211#ifdef MACH_BSD
91447636
A
212 /*XXX*/
213 if (processor == master_processor) {
214 bsd_hardclock(usermode, pc, 1);
1c79356b 215 }
91447636
A
216 /*XXX*/
217#endif /* MACH_BSD */
1c79356b
A
218
219#if MACH_PROF
91447636
A
220 if (thread->act_profiled) {
221 if (inkernel && thread->map != kernel_map) {
1c79356b
A
222 /*
223 * Non-kernel thread running in kernel
224 * Register user pc (mach_msg, vm_allocate ...)
225 */
226 if (profile_kernel_services)
91447636 227 profile(user_pc(thread), thread->profil_buffer);
1c79356b
A
228 }
229 else
230 /*
231 * User thread and user mode or
232 * user (server) thread in kernel-loaded server or
233 * kernel thread and kernel mode
234 * register interrupted pc
235 */
91447636 236 profile(pc, thread->profil_buffer);
1c79356b
A
237 }
238 if (kernel_task->task_profiled) {
91447636 239 if (inkernel && thread->map != kernel_map)
1c79356b
A
240 /*
241 * User thread not profiled in kernel mode,
242 * kernel task profiled, register kernel pc
243 * for kernel task
244 */
245 profile(pc, kernel_task->profil_buffer);
246 }
247#endif /* MACH_PROF */
1c79356b 248}