2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
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
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
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.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
34 * Mach Operating System
35 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
36 * All Rights Reserved.
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.
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.
48 * Carnegie Mellon requests users of this software to return to
50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
55 * any improvements or extensions that they make and grant Carnegie Mellon
56 * the rights to redistribute these changes.
62 * Author: Avadis Tevanian, Jr.
67 #include <mach_prof.h>
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>
87 #include <kern/thread.h>
88 #include <vm/vm_kern.h> /* kernel_map */
90 #include <mach/clock_server.h>
91 #include <mach/clock_priv_server.h>
92 #include <mach/mach_host_server.h>
94 #include <profiling/profile-mk.h>
96 boolean_t profile_kernel_services
= TRUE
; /* Indicates wether or not we
97 * account kernel services
99 * samples for user task */
101 extern void bsd_hardclock(
105 #endif /* MACH_BSD */
108 * Hertz rate clock interrupt servicing. Primarily used to
109 * update CPU statistics, recompute thread priority, and to
116 #endif /* STAT_TIME */
120 processor_t processor
= current_processor();
121 thread_t thread
= current_thread();
132 #endif /* MACH_PROF */
134 struct profile_vars
*pv
;
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.
148 if (thread
== THREAD_NULL
)
152 inkernel
= !usermode
&& (pc
< (unsigned int)ETEXT
);
153 #endif /* MACH_PROF */
156 * Hertz processing performed by all processors
157 * includes statistics gathering, state tracking,
158 * and quantum updating.
160 counter(c_clock_ticks
++);
163 pv
= PROFILE_VARS(cpu_number());
167 TIMER_BUMP(&thread
->user_timer
, ticks
);
168 if (thread
->priority
< BASEPRI_DEFAULT
)
169 state
= CPU_STATE_NICE
;
171 state
= CPU_STATE_USER
;
174 PROF_CNT_INC(pv
->stats
.user_ticks
);
178 TIMER_BUMP(&thread
->system_timer
, ticks
);
180 state
= processor
->state
;
181 if ( state
== PROCESSOR_IDLE
||
182 state
== PROCESSOR_DISPATCHING
)
183 state
= CPU_STATE_IDLE
;
185 if (thread
->options
& TH_OPT_DELAYIDLE
)
186 state
= CPU_STATE_IDLE
;
188 state
= CPU_STATE_SYSTEM
;
191 if (state
== CPU_STATE_SYSTEM
)
192 PROF_CNT_INC(pv
->stats
.kernel_ticks
);
194 PROF_CNT_INC(pv
->stats
.idle_ticks
);
196 if ((prof_uptrint_t
)pc
< _profile_vars
.profil_info
.lowpc
)
197 PROF_CNT_INC(pv
->stats
.too_low
);
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
]);
205 PROF_CNT_INC(pv
->stats
.too_high
);
211 PROCESSOR_DATA(processor
, cpu_ticks
[state
]++);
215 if (processor
== master_processor
) {
216 bsd_hardclock(usermode
, pc
, 1);
219 #endif /* MACH_BSD */
222 if (thread
->act_profiled
) {
223 if (inkernel
&& thread
->map
!= kernel_map
) {
225 * Non-kernel thread running in kernel
226 * Register user pc (mach_msg, vm_allocate ...)
228 if (profile_kernel_services
)
229 profile(user_pc(thread
), thread
->profil_buffer
);
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
238 profile(pc
, thread
->profil_buffer
);
240 if (kernel_task
->task_profiled
) {
241 if (inkernel
&& thread
->map
!= kernel_map
)
243 * User thread not profiled in kernel mode,
244 * kernel task profiled, register kernel pc
247 profile(pc
, kernel_task
->profil_buffer
);
249 #endif /* MACH_PROF */