2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
27 * Mach Operating System
28 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
29 * All Rights Reserved.
31 * Permission to use, copy, modify and distribute this software and its
32 * documentation is hereby granted, provided that both the copyright
33 * notice and this permission notice appear in all copies of the
34 * software, derivative works or modified versions, and any portions
35 * thereof, and that both notices appear in supporting documentation.
37 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
38 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
39 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
41 * Carnegie Mellon requests users of this software to return to
43 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
44 * School of Computer Science
45 * Carnegie Mellon University
46 * Pittsburgh PA 15213-3890
48 * any improvements or extensions that they make and grant Carnegie Mellon
49 * the rights to redistribute these changes.
55 * Author: Avadis Tevanian, Jr.
60 #include <mach_prof.h>
63 #include <mach/boolean.h>
64 #include <mach/machine.h>
65 #include <mach/time_value.h>
66 #include <mach/vm_param.h>
67 #include <mach/vm_prot.h>
68 #include <kern/clock.h>
69 #include <kern/counters.h>
70 #include <kern/cpu_number.h>
71 #include <kern/host.h>
72 #include <kern/lock.h>
73 #include <kern/mach_param.h>
74 #include <kern/misc_protos.h>
75 #include <kern/processor.h>
76 #include <kern/profile.h>
77 #include <kern/sched.h>
78 #include <kern/sched_prim.h>
80 #include <kern/thread.h>
81 #include <vm/vm_kern.h> /* kernel_map */
83 #include <mach/clock_server.h>
84 #include <mach/clock_priv_server.h>
85 #include <mach/mach_host_server.h>
87 #include <profiling/profile-mk.h>
89 boolean_t profile_kernel_services
= TRUE
; /* Indicates wether or not we
90 * account kernel services
92 * samples for user task */
94 extern void bsd_hardclock(
101 * Hertz rate clock interrupt servicing. Primarily used to
102 * update CPU statistics, recompute thread priority, and to
109 #endif /* STAT_TIME */
113 processor_t processor
= current_processor();
114 thread_t thread
= current_thread();
125 #endif /* MACH_PROF */
127 struct profile_vars
*pv
;
136 * The system startup sequence initializes the clock
137 * before kicking off threads. So it's possible,
138 * especially when debugging, to wind up here with
139 * no thread to bill against. So ignore the tick.
141 if (thread
== THREAD_NULL
)
145 inkernel
= !usermode
&& (pc
< (unsigned int)ETEXT
);
146 #endif /* MACH_PROF */
149 * Hertz processing performed by all processors
150 * includes statistics gathering, state tracking,
151 * and quantum updating.
153 counter(c_clock_ticks
++);
156 pv
= PROFILE_VARS(cpu_number());
160 TIMER_BUMP(&thread
->user_timer
, ticks
);
161 if (thread
->priority
< BASEPRI_DEFAULT
)
162 state
= CPU_STATE_NICE
;
164 state
= CPU_STATE_USER
;
167 PROF_CNT_INC(pv
->stats
.user_ticks
);
171 TIMER_BUMP(&thread
->system_timer
, ticks
);
173 state
= processor
->state
;
174 if ( state
== PROCESSOR_IDLE
||
175 state
== PROCESSOR_DISPATCHING
)
176 state
= CPU_STATE_IDLE
;
178 if (thread
->options
& TH_OPT_DELAYIDLE
)
179 state
= CPU_STATE_IDLE
;
181 state
= CPU_STATE_SYSTEM
;
184 if (state
== CPU_STATE_SYSTEM
)
185 PROF_CNT_INC(pv
->stats
.kernel_ticks
);
187 PROF_CNT_INC(pv
->stats
.idle_ticks
);
189 if ((prof_uptrint_t
)pc
< _profile_vars
.profil_info
.lowpc
)
190 PROF_CNT_INC(pv
->stats
.too_low
);
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
]);
198 PROF_CNT_INC(pv
->stats
.too_high
);
204 PROCESSOR_DATA(processor
, cpu_ticks
[state
]++);
208 if (processor
== master_processor
) {
209 bsd_hardclock(usermode
, pc
, 1);
212 #endif /* MACH_BSD */
215 if (thread
->act_profiled
) {
216 if (inkernel
&& thread
->map
!= kernel_map
) {
218 * Non-kernel thread running in kernel
219 * Register user pc (mach_msg, vm_allocate ...)
221 if (profile_kernel_services
)
222 profile(user_pc(thread
), thread
->profil_buffer
);
226 * User thread and user mode or
227 * user (server) thread in kernel-loaded server or
228 * kernel thread and kernel mode
229 * register interrupted pc
231 profile(pc
, thread
->profil_buffer
);
233 if (kernel_task
->task_profiled
) {
234 if (inkernel
&& thread
->map
!= kernel_map
)
236 * User thread not profiled in kernel mode,
237 * kernel task profiled, register kernel pc
240 profile(pc
, kernel_task
->profil_buffer
);
242 #endif /* MACH_PROF */