2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
29 * Mach Operating System
30 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
31 * All Rights Reserved.
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
43 * Carnegie Mellon requests users of this software to return to
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
57 * Author: Avadis Tevanian, Jr.
63 #include <stat_time.h>
64 #include <mach_prof.h>
67 #include <mach/boolean.h>
68 #include <mach/machine.h>
69 #include <mach/time_value.h>
70 #include <mach/vm_param.h>
71 #include <mach/vm_prot.h>
72 #include <kern/clock.h>
73 #include <kern/counters.h>
74 #include <kern/cpu_number.h>
75 #include <kern/host.h>
76 #include <kern/lock.h>
77 #include <kern/mach_param.h>
78 #include <kern/misc_protos.h>
79 #include <kern/processor.h>
80 #include <kern/profile.h>
81 #include <kern/sched.h>
82 #include <kern/sched_prim.h>
84 #include <kern/thread.h>
85 #include <kern/thread_swap.h>
86 #include <kern/time_out.h>
87 #include <vm/vm_kern.h> /* kernel_map */
88 #include <machine/mach_param.h> /* HZ */
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>
97 #define TICKBUMP(t) timer_bump(t, (1000000/HZ))
102 boolean_t profile_kernel_services
= TRUE
; /* Indicates wether or not we
103 * account kernel services
104 * samples for user task */
107 * Hertz rate clock interrupt servicing. Primarily used to
108 * update CPU statistics, recompute thread priority, and to
113 boolean_t usermode
, /* executing user code */
116 thread_act_t thr_act
;
118 register thread_t thread
= current_thread();
129 #endif /* MACH_PROF */
131 struct profile_vars
*pv
;
139 my_cpu
= cpu_number();
142 * The system startup sequence initializes the clock
143 * before kicking off threads. So it's possible,
144 * especially when debugging, to wind up here with
145 * no thread to bill against. So ignore the tick.
147 if (thread
== THREAD_NULL
)
151 inkernel
= !usermode
&& (pc
< (unsigned int)ETEXT
);
152 #endif /* MACH_PROF */
155 * Hertz processing performed by all processors
156 * includes statistics gathering, state tracking,
157 * and quantum updating.
159 counter(c_clock_ticks
++);
162 pv
= PROFILE_VARS(my_cpu
);
166 TICKBUMP(&thread
->user_timer
);
167 if (thread
->priority
< BASEPRI_DEFAULT
)
168 state
= CPU_STATE_NICE
;
170 state
= CPU_STATE_USER
;
173 PROF_CNT_INC(pv
->stats
.user_ticks
);
177 switch(processor_ptr
[my_cpu
]->state
) {
180 TICKBUMP(&thread
->system_timer
);
181 state
= CPU_STATE_IDLE
;
185 TICKBUMP(&thread
->system_timer
);
186 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 machine_slot
[my_cpu
].cpu_ticks
[state
]++;
214 * Hertz processing performed by the master-cpu
217 if (my_cpu
== master_cpu
) {
220 extern void bsd_hardclock(
225 bsd_hardclock(usermode
, pc
, 1);
227 #endif /* MACH_BSD */
231 thr_act
= thread
->top_act
;
232 if (thr_act
->act_profiled
) {
233 if (inkernel
&& thr_act
->map
!= kernel_map
) {
235 * Non-kernel thread running in kernel
236 * Register user pc (mach_msg, vm_allocate ...)
238 if (profile_kernel_services
)
239 profile(user_pc(thr_act
), thr_act
->profil_buffer
);
243 * User thread and user mode or
244 * user (server) thread in kernel-loaded server or
245 * kernel thread and kernel mode
246 * register interrupted pc
248 profile(pc
, thr_act
->profil_buffer
);
250 if (kernel_task
->task_profiled
) {
251 if (inkernel
&& thr_act
->map
!= kernel_map
)
253 * User thread not profiled in kernel mode,
254 * kernel task profiled, register kernel pc
257 profile(pc
, kernel_task
->profil_buffer
);
259 #endif /* MACH_PROF */