2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
60 * Author: Avadis Tevanian, Jr.
65 #include <mach_prof.h>
68 #include <mach/boolean.h>
69 #include <mach/machine.h>
70 #include <mach/time_value.h>
71 #include <mach/vm_param.h>
72 #include <mach/vm_prot.h>
73 #include <kern/clock.h>
74 #include <kern/counters.h>
75 #include <kern/cpu_number.h>
76 #include <kern/host.h>
77 #include <kern/lock.h>
78 #include <kern/mach_param.h>
79 #include <kern/misc_protos.h>
80 #include <kern/processor.h>
81 #include <kern/profile.h>
82 #include <kern/sched.h>
83 #include <kern/sched_prim.h>
85 #include <kern/thread.h>
86 #include <vm/vm_kern.h> /* kernel_map */
88 #include <mach/clock_server.h>
89 #include <mach/clock_priv_server.h>
90 #include <mach/mach_host_server.h>
92 #include <profiling/profile-mk.h>
94 boolean_t profile_kernel_services
= TRUE
; /* Indicates wether or not we
95 * account kernel services
97 * samples for user task */
99 extern void bsd_hardclock(
103 #endif /* MACH_BSD */
106 * Hertz rate clock interrupt servicing. Primarily used to
107 * update CPU statistics, recompute thread priority, and to
114 #endif /* STAT_TIME */
118 processor_t processor
= current_processor();
119 thread_t thread
= current_thread();
130 #endif /* MACH_PROF */
132 struct profile_vars
*pv
;
141 * The system startup sequence initializes the clock
142 * before kicking off threads. So it's possible,
143 * especially when debugging, to wind up here with
144 * no thread to bill against. So ignore the tick.
146 if (thread
== THREAD_NULL
)
150 inkernel
= !usermode
&& (pc
< (unsigned int)ETEXT
);
151 #endif /* MACH_PROF */
154 * Hertz processing performed by all processors
155 * includes statistics gathering, state tracking,
156 * and quantum updating.
158 counter(c_clock_ticks
++);
161 pv
= PROFILE_VARS(cpu_number());
165 TIMER_BUMP(&thread
->user_timer
, ticks
);
166 if (thread
->priority
< BASEPRI_DEFAULT
)
167 state
= CPU_STATE_NICE
;
169 state
= CPU_STATE_USER
;
172 PROF_CNT_INC(pv
->stats
.user_ticks
);
176 TIMER_BUMP(&thread
->system_timer
, ticks
);
178 state
= processor
->state
;
179 if ( state
== PROCESSOR_IDLE
||
180 state
== PROCESSOR_DISPATCHING
)
181 state
= CPU_STATE_IDLE
;
183 if (thread
->options
& TH_OPT_DELAYIDLE
)
184 state
= CPU_STATE_IDLE
;
186 state
= CPU_STATE_SYSTEM
;
189 if (state
== CPU_STATE_SYSTEM
)
190 PROF_CNT_INC(pv
->stats
.kernel_ticks
);
192 PROF_CNT_INC(pv
->stats
.idle_ticks
);
194 if ((prof_uptrint_t
)pc
< _profile_vars
.profil_info
.lowpc
)
195 PROF_CNT_INC(pv
->stats
.too_low
);
197 s
= (prof_uptrint_t
)pc
- _profile_vars
.profil_info
.lowpc
;
198 if (s
< pv
->profil_info
.text_len
) {
199 LHISTCOUNTER
*ptr
= (LHISTCOUNTER
*) pv
->profil_buf
;
200 LPROF_CNT_INC(ptr
[s
/ HISTFRACTION
]);
203 PROF_CNT_INC(pv
->stats
.too_high
);
209 PROCESSOR_DATA(processor
, cpu_ticks
[state
]++);
213 if (processor
== master_processor
) {
214 bsd_hardclock(usermode
, pc
, 1);
217 #endif /* MACH_BSD */
220 if (thread
->act_profiled
) {
221 if (inkernel
&& thread
->map
!= kernel_map
) {
223 * Non-kernel thread running in kernel
224 * Register user pc (mach_msg, vm_allocate ...)
226 if (profile_kernel_services
)
227 profile(user_pc(thread
), thread
->profil_buffer
);
231 * User thread and user mode or
232 * user (server) thread in kernel-loaded server or
233 * kernel thread and kernel mode
234 * register interrupted pc
236 profile(pc
, thread
->profil_buffer
);
238 if (kernel_task
->task_profiled
) {
239 if (inkernel
&& thread
->map
!= kernel_map
)
241 * User thread not profiled in kernel mode,
242 * kernel task profiled, register kernel pc
245 profile(pc
, kernel_task
->profil_buffer
);
247 #endif /* MACH_PROF */