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