]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/mach_clock.c
xnu-792.6.56.tar.gz
[apple/xnu.git] / osfmk / kern / mach_clock.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*
24 * @OSF_COPYRIGHT@
25 */
26 /*
27 * Mach Operating System
28 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
29 * All Rights Reserved.
30 *
31 * Permission to use, copy, modify and distribute this software and its
32 * documentation is hereby granted, provided that both the copyright
33 * notice and this permission notice appear in all copies of the
34 * software, derivative works or modified versions, and any portions
35 * thereof, and that both notices appear in supporting documentation.
36 *
37 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
38 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
39 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
40 *
41 * Carnegie Mellon requests users of this software to return to
42 *
43 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
44 * School of Computer Science
45 * Carnegie Mellon University
46 * Pittsburgh PA 15213-3890
47 *
48 * any improvements or extensions that they make and grant Carnegie Mellon
49 * the rights to redistribute these changes.
50 */
51 /*
52 */
53 /*
54 * File: clock_prim.c
55 * Author: Avadis Tevanian, Jr.
56 * Date: 1986
57 *
58 * Clock primitives.
59 */
60 #include <mach_prof.h>
61 #include <gprof.h>
62
63 #include <mach/boolean.h>
64 #include <mach/machine.h>
65 #include <mach/time_value.h>
66 #include <mach/vm_param.h>
67 #include <mach/vm_prot.h>
68 #include <kern/clock.h>
69 #include <kern/counters.h>
70 #include <kern/cpu_number.h>
71 #include <kern/host.h>
72 #include <kern/lock.h>
73 #include <kern/mach_param.h>
74 #include <kern/misc_protos.h>
75 #include <kern/processor.h>
76 #include <kern/profile.h>
77 #include <kern/sched.h>
78 #include <kern/sched_prim.h>
79 #include <kern/spl.h>
80 #include <kern/thread.h>
81 #include <vm/vm_kern.h> /* kernel_map */
82
83 #include <mach/clock_server.h>
84 #include <mach/clock_priv_server.h>
85 #include <mach/mach_host_server.h>
86
87 #include <profiling/profile-mk.h>
88
89 boolean_t profile_kernel_services = TRUE; /* Indicates wether or not we
90 * account kernel services
91
92 * samples for user task */
93 #ifdef MACH_BSD
94 extern void bsd_hardclock(
95 boolean_t usermode,
96 natural_t pc,
97 int numticks);
98 #endif /* MACH_BSD */
99
100 /*
101 * Hertz rate clock interrupt servicing. Primarily used to
102 * update CPU statistics, recompute thread priority, and to
103 * do profiling
104 */
105 void
106 hertz_tick(
107 #if STAT_TIME
108 natural_t ticks,
109 #endif /* STAT_TIME */
110 boolean_t usermode,
111 natural_t pc)
112 {
113 processor_t processor = current_processor();
114 thread_t thread = current_thread();
115 int state;
116 #if MACH_PROF
117 #ifdef __MACHO__
118 #define ETEXT etext
119 extern long etext;
120 #else
121 #define ETEXT &etext
122 extern char etext;
123 #endif
124 boolean_t inkernel;
125 #endif /* MACH_PROF */
126 #if GPROF
127 struct profile_vars *pv;
128 prof_uptrint_t s;
129 #endif
130
131 #ifdef lint
132 pc++;
133 #endif /* lint */
134
135 /*
136 * The system startup sequence initializes the clock
137 * before kicking off threads. So it's possible,
138 * especially when debugging, to wind up here with
139 * no thread to bill against. So ignore the tick.
140 */
141 if (thread == THREAD_NULL)
142 return;
143
144 #if MACH_PROF
145 inkernel = !usermode && (pc < (unsigned int)ETEXT);
146 #endif /* MACH_PROF */
147
148 /*
149 * Hertz processing performed by all processors
150 * includes statistics gathering, state tracking,
151 * and quantum updating.
152 */
153 counter(c_clock_ticks++);
154
155 #if GPROF
156 pv = PROFILE_VARS(cpu_number());
157 #endif
158
159 if (usermode) {
160 TIMER_BUMP(&thread->user_timer, ticks);
161 if (thread->priority < BASEPRI_DEFAULT)
162 state = CPU_STATE_NICE;
163 else
164 state = CPU_STATE_USER;
165 #if GPROF
166 if (pv->active)
167 PROF_CNT_INC(pv->stats.user_ticks);
168 #endif
169 }
170 else {
171 TIMER_BUMP(&thread->system_timer, ticks);
172
173 state = processor->state;
174 if ( state == PROCESSOR_IDLE ||
175 state == PROCESSOR_DISPATCHING)
176 state = CPU_STATE_IDLE;
177 else
178 if (thread->options & TH_OPT_DELAYIDLE)
179 state = CPU_STATE_IDLE;
180 else
181 state = CPU_STATE_SYSTEM;
182 #if GPROF
183 if (pv->active) {
184 if (state == CPU_STATE_SYSTEM)
185 PROF_CNT_INC(pv->stats.kernel_ticks);
186 else
187 PROF_CNT_INC(pv->stats.idle_ticks);
188
189 if ((prof_uptrint_t)pc < _profile_vars.profil_info.lowpc)
190 PROF_CNT_INC(pv->stats.too_low);
191 else {
192 s = (prof_uptrint_t)pc - _profile_vars.profil_info.lowpc;
193 if (s < pv->profil_info.text_len) {
194 LHISTCOUNTER *ptr = (LHISTCOUNTER *) pv->profil_buf;
195 LPROF_CNT_INC(ptr[s / HISTFRACTION]);
196 }
197 else
198 PROF_CNT_INC(pv->stats.too_high);
199 }
200 }
201 #endif
202 }
203
204 PROCESSOR_DATA(processor, cpu_ticks[state]++);
205
206 #ifdef MACH_BSD
207 /*XXX*/
208 if (processor == master_processor) {
209 bsd_hardclock(usermode, pc, 1);
210 }
211 /*XXX*/
212 #endif /* MACH_BSD */
213
214 #if MACH_PROF
215 if (thread->act_profiled) {
216 if (inkernel && thread->map != kernel_map) {
217 /*
218 * Non-kernel thread running in kernel
219 * Register user pc (mach_msg, vm_allocate ...)
220 */
221 if (profile_kernel_services)
222 profile(user_pc(thread), thread->profil_buffer);
223 }
224 else
225 /*
226 * User thread and user mode or
227 * user (server) thread in kernel-loaded server or
228 * kernel thread and kernel mode
229 * register interrupted pc
230 */
231 profile(pc, thread->profil_buffer);
232 }
233 if (kernel_task->task_profiled) {
234 if (inkernel && thread->map != kernel_map)
235 /*
236 * User thread not profiled in kernel mode,
237 * kernel task profiled, register kernel pc
238 * for kernel task
239 */
240 profile(pc, kernel_task->profil_buffer);
241 }
242 #endif /* MACH_PROF */
243 }