2 * Copyright (c) 2011-2018 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@
29 #include <kern/ipc_tt.h> /* port_name_to_task */
30 #include <kern/thread.h>
31 #include <kern/machine.h>
32 #include <kern/kalloc.h>
33 #include <mach/mach_types.h>
34 #include <sys/errno.h>
35 #include <sys/ktrace.h>
37 #include <kperf/action.h>
38 #include <kperf/buffer.h>
39 #include <kperf/kdebug_trigger.h>
40 #include <kperf/kperf.h>
41 #include <kperf/kptimer.h>
42 #include <kperf/lazy.h>
43 #include <kperf/pet.h>
44 #include <kperf/sample.h>
46 /* from libkern/libkern.h */
47 extern uint64_t strtouq(const char *, char **, int);
49 LCK_GRP_DECLARE(kperf_lck_grp
, "kperf");
51 /* one wired sample buffer per CPU */
52 static struct kperf_sample
*intr_samplev
;
53 static unsigned int intr_samplec
= 0;
55 /* current sampling status */
56 enum kperf_sampling kperf_status
= KPERF_SAMPLING_OFF
;
59 * Only set up kperf once.
61 static bool kperf_is_setup
= false;
63 /* whether or not to callback to kperf on context switch */
64 boolean_t kperf_on_cpu_active
= FALSE
;
66 unsigned int kperf_thread_blocked_action
;
67 unsigned int kperf_cpu_sample_action
;
70 kperf_intr_sample_buffer(void)
72 unsigned ncpu
= cpu_number();
74 assert(ml_get_interrupts_enabled() == FALSE
);
75 assert(ncpu
< intr_samplec
);
77 return &(intr_samplev
[ncpu
]);
81 kperf_init_early(void)
84 * kperf allocates based on the number of CPUs and requires them to all be
89 boolean_t found_kperf
= FALSE
;
90 char kperf_config_str
[64];
91 found_kperf
= PE_parse_boot_arg_str("kperf", kperf_config_str
, sizeof(kperf_config_str
));
92 if (found_kperf
&& kperf_config_str
[0] != '\0') {
93 kperf_kernel_configure(kperf_config_str
);
106 if (kperf_is_setup
) {
110 intr_samplec
= machine_info
.logical_cpu_max
;
111 size_t intr_samplev_size
= intr_samplec
* sizeof(*intr_samplev
);
112 intr_samplev
= kalloc_tag(intr_samplev_size
, VM_KERN_MEMORY_DIAG
);
113 memset(intr_samplev
, 0, intr_samplev_size
);
115 kperf_kdebug_setup();
117 kperf_is_setup
= true;
124 * Make sure samples aren't being taken before tearing everything down.
126 (void)kperf_disable_sampling();
129 (void)kperf_kdbg_cswitch_set(0);
130 kperf_kdebug_reset();
135 * Most of the other systems call into actions, so reset them last.
137 kperf_action_reset();
141 kperf_kernel_configure(const char *config
)
147 assert(config
!= NULL
);
149 ktrace_start_single_threaded();
151 ktrace_kernel_configure(KTRACE_KPERF
);
153 if (config
[0] == 'p') {
159 uint32_t action_samplers
;
160 uint64_t timer_period_ns
;
161 uint64_t timer_period
;
164 kperf_action_set_count(pairs
);
165 kptimer_set_count(pairs
);
167 action_samplers
= (uint32_t)strtouq(config
, &end
, 0);
169 kprintf("kperf: unable to parse '%s' as action sampler\n", config
);
174 kperf_action_set_samplers(pairs
, action_samplers
);
176 if (config
[0] == '\0') {
177 kprintf("kperf: missing timer period in config\n");
182 timer_period_ns
= strtouq(config
, &end
, 0);
184 kprintf("kperf: unable to parse '%s' as timer period\n", config
);
187 nanoseconds_to_absolutetime(timer_period_ns
, &timer_period
);
190 kptimer_set_period(pairs
- 1, timer_period
);
191 kptimer_set_action(pairs
- 1, pairs
);
194 kptimer_set_pet_timerid(pairs
- 1);
195 kppet_set_lightweight_pet(1);
198 } while (*(config
++) == ',');
200 int error
= kperf_enable_sampling();
202 printf("kperf: cannot enable sampling at boot: %d\n", error
);
206 ktrace_end_single_threaded();
209 void kperf_on_cpu_internal(thread_t thread
, thread_continue_t continuation
,
210 uintptr_t *starting_fp
);
212 kperf_on_cpu_internal(thread_t thread
, thread_continue_t continuation
,
213 uintptr_t *starting_fp
)
215 if (kperf_kdebug_cswitch
) {
216 /* trace the new thread's PID for Instruments */
217 int pid
= task_pid(get_threadtask(thread
));
218 BUF_DATA(PERF_TI_CSWITCH
, thread_tid(thread
), pid
);
220 if (kppet_lightweight_active
) {
221 kppet_on_cpu(thread
, continuation
, starting_fp
);
223 if (kperf_lazy_wait_action
!= 0) {
224 kperf_lazy_wait_sample(thread
, continuation
, starting_fp
);
229 kperf_on_cpu_update(void)
231 kperf_on_cpu_active
= kperf_kdebug_cswitch
||
232 kppet_lightweight_active
||
233 kperf_lazy_wait_action
!= 0;
237 kperf_is_sampling(void)
239 return kperf_status
== KPERF_SAMPLING_ON
;
243 kperf_enable_sampling(void)
245 if (kperf_status
== KPERF_SAMPLING_ON
) {
249 if (kperf_status
!= KPERF_SAMPLING_OFF
) {
250 panic("kperf: sampling was %d when asked to enable", kperf_status
);
253 /* make sure interrupt tables and actions are initted */
254 if (!kperf_is_setup
|| (kperf_action_get_count() == 0)) {
258 kperf_status
= KPERF_SAMPLING_ON
;
259 kppet_lightweight_active_update();
266 kperf_disable_sampling(void)
268 if (kperf_status
!= KPERF_SAMPLING_ON
) {
272 /* mark a shutting down */
273 kperf_status
= KPERF_SAMPLING_SHUTDOWN
;
275 /* tell timers to disable */
279 kperf_status
= KPERF_SAMPLING_OFF
;
280 kppet_lightweight_active_update();
286 kperf_timer_expire(void *param0
, void * __unused param1
)
288 processor_t processor
= param0
;
289 int cpuid
= processor
->cpu_id
;
291 kptimer_expire(processor
, cpuid
, mach_absolute_time());
295 kperf_thread_get_dirty(thread_t thread
)
297 return thread
->c_switch
!= thread
->kperf_c_switch
;
301 kperf_thread_set_dirty(thread_t thread
, boolean_t dirty
)
304 thread
->kperf_c_switch
= thread
->c_switch
- 1;
306 thread
->kperf_c_switch
= thread
->c_switch
;
311 kperf_port_to_pid(mach_port_name_t portname
)
313 if (!MACH_PORT_VALID(portname
)) {
317 task_t task
= port_name_to_task(portname
);
318 if (task
== TASK_NULL
) {
322 pid_t pid
= task_pid(task
);
324 os_ref_count_t __assert_only count
= task_deallocate_internal(task
);