]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/mach_clock.c
c0b234c63cf281df69b767ae1245ef3b627cf0aa
[apple/xnu.git] / osfmk / kern / mach_clock.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
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
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
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
102 boolean_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 */
111 void
112 hertz_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
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 */
147 if (thread == THREAD_NULL)
148 return;
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);
167 if (thread->priority < BASEPRI_DEFAULT)
168 state = CPU_STATE_NICE;
169 else
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 {
177 switch(processor_ptr[my_cpu]->state) {
178
179 case PROCESSOR_IDLE:
180 TICKBUMP(&thread->system_timer);
181 state = CPU_STATE_IDLE;
182 break;
183
184 default:
185 TICKBUMP(&thread->system_timer);
186 state = CPU_STATE_SYSTEM;
187 break;
188 }
189 #if GPROF
190 if (pv->active) {
191 if (state == CPU_STATE_SYSTEM)
192 PROF_CNT_INC(pv->stats.kernel_ticks);
193 else
194 PROF_CNT_INC(pv->stats.idle_ticks);
195
196 if ((prof_uptrint_t)pc < _profile_vars.profil_info.lowpc)
197 PROF_CNT_INC(pv->stats.too_low);
198 else {
199 s = (prof_uptrint_t)pc - _profile_vars.profil_info.lowpc;
200 if (s < pv->profil_info.text_len) {
201 LHISTCOUNTER *ptr = (LHISTCOUNTER *) pv->profil_buf;
202 LPROF_CNT_INC(ptr[s / HISTFRACTION]);
203 }
204 else
205 PROF_CNT_INC(pv->stats.too_high);
206 }
207 }
208 #endif
209 }
210
211 machine_slot[my_cpu].cpu_ticks[state]++;
212
213 /*
214 * Hertz processing performed by the master-cpu
215 * exclusively.
216 */
217 if (my_cpu == master_cpu) {
218 #ifdef MACH_BSD
219 {
220 extern void bsd_hardclock(
221 boolean_t usermode,
222 natural_t pc,
223 int ticks);
224
225 bsd_hardclock(usermode, pc, 1);
226 }
227 #endif /* MACH_BSD */
228 }
229
230 #if MACH_PROF
231 thr_act = thread->top_act;
232 if (thr_act->act_profiled) {
233 if (inkernel && thr_act->map != kernel_map) {
234 /*
235 * Non-kernel thread running in kernel
236 * Register user pc (mach_msg, vm_allocate ...)
237 */
238 if (profile_kernel_services)
239 profile(user_pc(thr_act), thr_act->profil_buffer);
240 }
241 else
242 /*
243 * User thread and user mode or
244 * user (server) thread in kernel-loaded server or
245 * kernel thread and kernel mode
246 * register interrupted pc
247 */
248 profile(pc, thr_act->profil_buffer);
249 }
250 if (kernel_task->task_profiled) {
251 if (inkernel && thr_act->map != kernel_map)
252 /*
253 * User thread not profiled in kernel mode,
254 * kernel task profiled, register kernel pc
255 * for kernel task
256 */
257 profile(pc, kernel_task->profil_buffer);
258 }
259 #endif /* MACH_PROF */
260 }