2 * Copyright (c) 2011 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@
28 #include <kern/ipc_tt.h> /* port_name_to_task */
29 #include <kern/thread.h>
30 #include <kern/machine.h>
31 #include <kern/kalloc.h>
32 #include <mach/mach_types.h>
33 #include <sys/errno.h>
34 #include <sys/ktrace.h>
36 #include <kperf/action.h>
37 #include <kperf/buffer.h>
38 #include <kperf/kdebug_trigger.h>
39 #include <kperf/kperf.h>
40 #include <kperf/kperf_timer.h>
41 #include <kperf/pet.h>
42 #include <kperf/sample.h>
44 /* from libkern/libkern.h */
45 extern uint64_t strtouq(const char *, char **, int);
47 lck_grp_t kperf_lck_grp
;
49 /* IDs of threads on CPUs before starting the PET thread */
50 uint64_t *kperf_tid_on_cpus
= NULL
;
52 /* one wired sample buffer per CPU */
53 static struct kperf_sample
*intr_samplev
;
54 static unsigned int intr_samplec
= 0;
56 /* current sampling status */
57 static unsigned sampling_status
= KPERF_SAMPLING_OFF
;
60 static boolean_t kperf_initted
= FALSE
;
62 /* whether or not to callback to kperf on context switch */
63 boolean_t kperf_on_cpu_active
= FALSE
;
66 kperf_intr_sample_buffer(void)
68 unsigned ncpu
= cpu_number();
70 assert(ml_get_interrupts_enabled() == FALSE
);
71 assert(ncpu
< intr_samplec
);
73 return &(intr_samplev
[ncpu
]);
76 /* setup interrupt sample buffers */
80 static lck_grp_attr_t lck_grp_attr
;
89 lck_grp_attr_setdefault(&lck_grp_attr
);
90 lck_grp_init(&kperf_lck_grp
, "kperf", &lck_grp_attr
);
92 ncpus
= machine_info
.logical_cpu_max
;
94 /* create buffers to remember which threads don't need to be sampled by PET */
95 kperf_tid_on_cpus
= kalloc_tag(ncpus
* sizeof(*kperf_tid_on_cpus
),
97 if (kperf_tid_on_cpus
== NULL
) {
101 bzero(kperf_tid_on_cpus
, ncpus
* sizeof(*kperf_tid_on_cpus
));
103 /* create the interrupt buffers */
104 intr_samplec
= ncpus
;
105 intr_samplev
= kalloc_tag(ncpus
* sizeof(*intr_samplev
),
106 VM_KERN_MEMORY_DIAG
);
107 if (intr_samplev
== NULL
) {
111 bzero(intr_samplev
, ncpus
* sizeof(*intr_samplev
));
113 /* create kdebug trigger filter buffers */
114 if ((err
= kperf_kdebug_init())) {
118 kperf_initted
= TRUE
;
123 kfree(intr_samplev
, ncpus
* sizeof(*intr_samplev
));
128 if (kperf_tid_on_cpus
) {
129 kfree(kperf_tid_on_cpus
, ncpus
* sizeof(*kperf_tid_on_cpus
));
130 kperf_tid_on_cpus
= NULL
;
139 /* turn off sampling first */
140 (void)kperf_sampling_disable();
142 /* cleanup miscellaneous configuration first */
143 (void)kperf_kdbg_cswitch_set(0);
144 (void)kperf_set_lightweight_pet(0);
145 kperf_kdebug_reset();
147 /* timers, which require actions, first */
149 kperf_action_reset();
153 kperf_kernel_configure(const char *config
)
159 assert(config
!= NULL
);
161 ktrace_start_single_threaded();
163 ktrace_kernel_configure(KTRACE_KPERF
);
165 if (config
[0] == 'p') {
171 uint32_t action_samplers
;
172 uint64_t timer_period
;
175 kperf_action_set_count(pairs
);
176 kperf_timer_set_count(pairs
);
178 action_samplers
= (uint32_t)strtouq(config
, &end
, 0);
180 kprintf("kperf: unable to parse '%s' as action sampler\n", config
);
185 kperf_action_set_samplers(pairs
, action_samplers
);
187 if (config
[0] == '\0') {
188 kprintf("kperf: missing timer period in config\n");
193 timer_period
= strtouq(config
, &end
, 0);
195 kprintf("kperf: unable to parse '%s' as timer period\n", config
);
200 kperf_timer_set_period(pairs
- 1, timer_period
);
201 kperf_timer_set_action(pairs
- 1, pairs
);
204 kperf_timer_set_petid(pairs
- 1);
205 kperf_set_lightweight_pet(1);
208 } while (*(config
++) == ',');
210 kperf_sampling_enable();
213 ktrace_end_single_threaded();
217 kperf_on_cpu_internal(thread_t thread
, thread_continue_t continuation
,
218 uintptr_t *starting_fp
)
220 if (kperf_kdebug_cswitch
) {
221 /* trace the new thread's PID for Instruments */
222 int pid
= task_pid(get_threadtask(thread
));
224 BUF_DATA(PERF_TI_CSWITCH
, thread_tid(thread
), pid
);
226 if (kperf_lightweight_pet_active
) {
227 kperf_pet_on_cpu(thread
, continuation
, starting_fp
);
232 kperf_on_cpu_update(void)
234 kperf_on_cpu_active
= kperf_kdebug_cswitch
||
235 kperf_lightweight_pet_active
;
238 /* random misc-ish functions */
240 kperf_get_thread_flags(thread_t thread
)
242 return thread
->kperf_flags
;
246 kperf_set_thread_flags(thread_t thread
, uint32_t flags
)
248 thread
->kperf_flags
= flags
;
252 kperf_sampling_status(void)
254 return sampling_status
;
258 kperf_sampling_enable(void)
260 if (sampling_status
== KPERF_SAMPLING_ON
) {
264 if (sampling_status
!= KPERF_SAMPLING_OFF
) {
265 panic("kperf: sampling was %d when asked to enable", sampling_status
);
268 /* make sure interrupt tables and actions are initted */
269 if (!kperf_initted
|| (kperf_action_get_count() == 0)) {
273 /* mark as running */
274 sampling_status
= KPERF_SAMPLING_ON
;
275 kperf_lightweight_pet_active_update();
277 /* tell timers to enable */
284 kperf_sampling_disable(void)
286 if (sampling_status
!= KPERF_SAMPLING_ON
) {
290 /* mark a shutting down */
291 sampling_status
= KPERF_SAMPLING_SHUTDOWN
;
293 /* tell timers to disable */
297 sampling_status
= KPERF_SAMPLING_OFF
;
298 kperf_lightweight_pet_active_update();
304 kperf_thread_get_dirty(thread_t thread
)
306 return (thread
->c_switch
!= thread
->kperf_c_switch
);
310 kperf_thread_set_dirty(thread_t thread
, boolean_t dirty
)
313 thread
->kperf_c_switch
= thread
->c_switch
- 1;
315 thread
->kperf_c_switch
= thread
->c_switch
;
320 kperf_port_to_pid(mach_port_name_t portname
)
325 if (!MACH_PORT_VALID(portname
)) {
329 task
= port_name_to_task(portname
);
331 if (task
== TASK_NULL
) {
335 pid
= task_pid(task
);
337 task_deallocate_internal(task
);