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