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