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