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@
30 * Called from a trigger. Actually takes the data from the different
31 * modules and puts them in a buffer
34 #include <mach/mach_types.h>
35 #include <machine/machine_routines.h>
36 #include <kern/kalloc.h>
37 #include <kern/debug.h> /* panic */
38 #include <kern/thread.h>
39 #include <sys/errno.h>
42 #include <kperf/action.h>
43 #include <kperf/ast.h>
44 #include <kperf/buffer.h>
45 #include <kperf/callstack.h>
46 #include <kperf/context.h>
47 #include <kperf/kdebug_trigger.h>
48 #include <kperf/kperf.h>
49 #include <kperf/kperf_kpc.h>
50 #include <kperf/kperf_timer.h>
51 #include <kperf/pet.h>
52 #include <kperf/sample.h>
53 #include <kperf/thread_samplers.h>
55 #define ACTION_MAX (32)
57 /* the list of different actions to take */
61 uint32_t ucallstack_depth
;
62 uint32_t kcallstack_depth
;
67 /* the list of actions */
68 static unsigned actionc
= 0;
69 static struct action
*actionv
= NULL
;
71 /* should emit tracepoint on context switch */
72 int kperf_kdebug_cswitch
= 0;
75 kperf_sample_internal(struct kperf_sample
*sbuf
,
76 struct kperf_context
*context
,
77 unsigned sample_what
, unsigned sample_flags
,
78 unsigned actionid
, uint32_t ucallstack_depth
)
80 int pended_ucallstack
= 0;
81 int pended_th_dispatch
= 0;
83 /* not much point continuing here, but what to do ? return
84 * Shutdown? cut a tracepoint and continue?
86 if (sample_what
== 0) {
87 return SAMPLE_CONTINUE
;
90 /* callstacks should be explicitly ignored */
91 if (sample_flags
& SAMPLE_FLAG_EMPTY_CALLSTACK
) {
92 sample_what
&= ~(SAMPLER_KSTACK
| SAMPLER_USTACK
);
95 context
->cur_thread
->kperf_pet_gen
= kperf_pet_gen
;
96 boolean_t is_kernel
= (context
->cur_pid
== 0);
98 if (actionid
&& actionid
<= actionc
) {
99 sbuf
->kcallstack
.nframes
= actionv
[actionid
- 1].kcallstack_depth
;
101 sbuf
->kcallstack
.nframes
= MAX_CALLSTACK_FRAMES
;
104 if (ucallstack_depth
) {
105 sbuf
->ucallstack
.nframes
= ucallstack_depth
;
107 sbuf
->ucallstack
.nframes
= MAX_CALLSTACK_FRAMES
;
110 sbuf
->kcallstack
.flags
= CALLSTACK_VALID
;
111 sbuf
->ucallstack
.flags
= CALLSTACK_VALID
;
113 /* an event occurred. Sample everything and dump it in a
117 /* collect data from samplers */
118 if (sample_what
& SAMPLER_TH_INFO
) {
119 kperf_thread_info_sample(&sbuf
->th_info
, context
);
121 /* See if we should drop idle thread samples */
122 if (!(sample_flags
& SAMPLE_FLAG_IDLE_THREADS
)) {
123 if (sbuf
->th_info
.kpthi_runmode
& 0x40) {
124 return SAMPLE_CONTINUE
;
129 if (sample_what
& SAMPLER_TH_SNAPSHOT
) {
130 kperf_thread_snapshot_sample(&(sbuf
->th_snapshot
), context
);
132 if (sample_what
& SAMPLER_TH_SCHEDULING
) {
133 kperf_thread_scheduling_sample(&(sbuf
->th_scheduling
), context
);
135 if (sample_what
& SAMPLER_KSTACK
) {
136 if (sample_flags
& SAMPLE_FLAG_CONTINUATION
) {
137 kperf_continuation_sample(&(sbuf
->kcallstack
), context
);
138 /* outside of interrupt context, backtrace the current thread */
139 } else if (sample_flags
& SAMPLE_FLAG_NON_INTERRUPT
) {
140 kperf_backtrace_sample(&(sbuf
->kcallstack
), context
);
142 kperf_kcallstack_sample(&(sbuf
->kcallstack
), context
);
145 if (sample_what
& SAMPLER_TK_SNAPSHOT
) {
146 kperf_task_snapshot_sample(&(sbuf
->tk_snapshot
), context
);
151 if (sample_what
& SAMPLER_MEMINFO
) {
152 kperf_meminfo_sample(&(sbuf
->meminfo
), context
);
155 if (sample_flags
& SAMPLE_FLAG_PEND_USER
) {
156 if (sample_what
& SAMPLER_USTACK
) {
157 pended_ucallstack
= kperf_ucallstack_pend(context
, sbuf
->ucallstack
.nframes
);
160 if (sample_what
& SAMPLER_TH_DISPATCH
) {
161 pended_th_dispatch
= kperf_thread_dispatch_pend(context
);
164 if (sample_what
& SAMPLER_USTACK
) {
165 kperf_ucallstack_sample(&(sbuf
->ucallstack
), context
);
168 if (sample_what
& SAMPLER_TH_DISPATCH
) {
169 kperf_thread_dispatch_sample(&(sbuf
->th_dispatch
), context
);
174 if (sample_what
& SAMPLER_PMC_THREAD
) {
175 kperf_kpc_thread_sample(&(sbuf
->kpcdata
), sample_what
);
176 } else if (sample_what
& SAMPLER_PMC_CPU
) {
177 kperf_kpc_cpu_sample(&(sbuf
->kpcdata
), sample_what
);
180 /* lookup the user tag, if any */
182 if (actionid
&& (actionid
<= actionc
)) {
183 userdata
= actionv
[actionid
- 1].userdata
;
188 /* avoid logging if this sample only pended samples */
189 if (sample_flags
& SAMPLE_FLAG_PEND_USER
&&
190 !(sample_what
& ~(SAMPLER_USTACK
| SAMPLER_TH_DISPATCH
)))
192 return SAMPLE_CONTINUE
;
195 /* stash the data into the buffer
196 * interrupts off to ensure we don't get split
198 boolean_t enabled
= ml_set_interrupts_enabled(FALSE
);
200 BUF_DATA(PERF_GEN_EVENT
| DBG_FUNC_START
, sample_what
,
201 actionid
, userdata
, sample_flags
);
203 if (sample_what
& SAMPLER_TH_INFO
) {
204 kperf_thread_info_log(&sbuf
->th_info
);
206 if (sample_what
& SAMPLER_TH_SCHEDULING
) {
207 kperf_thread_scheduling_log(&(sbuf
->th_scheduling
));
209 if (sample_what
& SAMPLER_TH_SNAPSHOT
) {
210 kperf_thread_snapshot_log(&(sbuf
->th_snapshot
));
212 if (sample_what
& SAMPLER_KSTACK
) {
213 kperf_kcallstack_log(&sbuf
->kcallstack
);
215 if (sample_what
& SAMPLER_TK_SNAPSHOT
) {
216 kperf_task_snapshot_log(&(sbuf
->tk_snapshot
));
219 /* dump user stuff */
222 if (sample_what
& SAMPLER_MEMINFO
) {
223 kperf_meminfo_log(&(sbuf
->meminfo
));
226 if (sample_flags
& SAMPLE_FLAG_PEND_USER
) {
227 if (pended_ucallstack
) {
228 BUF_INFO(PERF_CS_UPEND
);
231 if (pended_th_dispatch
) {
232 BUF_INFO(PERF_TI_DISPPEND
);
235 if (sample_what
& SAMPLER_USTACK
) {
236 kperf_ucallstack_log(&(sbuf
->ucallstack
));
239 if (sample_what
& SAMPLER_TH_DISPATCH
) {
240 kperf_thread_dispatch_log(&(sbuf
->th_dispatch
));
245 if (sample_what
& SAMPLER_PMC_THREAD
) {
246 kperf_kpc_thread_log(&(sbuf
->kpcdata
));
247 } else if (sample_what
& SAMPLER_PMC_CPU
) {
248 kperf_kpc_cpu_log(&(sbuf
->kpcdata
));
251 BUF_DATA(PERF_GEN_EVENT
| DBG_FUNC_END
, sample_what
);
254 ml_set_interrupts_enabled(enabled
);
256 return SAMPLE_CONTINUE
;
259 /* Translate actionid into sample bits and take a sample */
261 kperf_sample(struct kperf_sample
*sbuf
,
262 struct kperf_context
*context
,
263 unsigned actionid
, unsigned sample_flags
)
265 /* work out what to sample, if anything */
266 if ((actionid
> actionc
) || (actionid
== 0)) {
267 return SAMPLE_SHUTDOWN
;
270 /* check the pid filter against the context's current pid.
271 * filter pid == -1 means any pid
273 int pid_filter
= actionv
[actionid
- 1].pid_filter
;
274 if ((pid_filter
!= -1) && (pid_filter
!= context
->cur_pid
)) {
275 return SAMPLE_CONTINUE
;
278 /* the samplers to run */
279 unsigned int sample_what
= actionv
[actionid
- 1].sample
;
281 /* do the actual sample operation */
282 return kperf_sample_internal(sbuf
, context
, sample_what
,
283 sample_flags
, actionid
,
284 actionv
[actionid
- 1].ucallstack_depth
);
288 kperf_kdebug_handler(uint32_t debugid
, uintptr_t *starting_fp
)
290 uint32_t sample_flags
= SAMPLE_FLAG_PEND_USER
;
291 struct kperf_context ctx
;
292 struct kperf_sample
*sample
= NULL
;
293 kern_return_t kr
= KERN_SUCCESS
;
296 if (!kperf_kdebug_should_trigger(debugid
)) {
300 BUF_VERB(PERF_KDBG_HNDLR
| DBG_FUNC_START
, debugid
);
302 ctx
.cur_thread
= current_thread();
303 ctx
.cur_pid
= task_pid(get_threadtask(ctx
.cur_thread
));
304 ctx
.trigger_type
= TRIGGER_TYPE_KDEBUG
;
307 s
= ml_set_interrupts_enabled(0);
309 sample
= kperf_intr_sample_buffer();
311 if (!ml_at_interrupt_context()) {
312 sample_flags
|= SAMPLE_FLAG_NON_INTERRUPT
;
313 ctx
.starting_fp
= starting_fp
;
316 kr
= kperf_sample(sample
, &ctx
, kperf_kdebug_get_action(), sample_flags
);
318 ml_set_interrupts_enabled(s
);
319 BUF_VERB(PERF_KDBG_HNDLR
| DBG_FUNC_END
, kr
);
323 * This function allocates >2.3KB of the stack. Prevent the compiler from
324 * inlining this function into ast_taken and ensure the stack memory is only
325 * allocated for the kperf AST.
327 __attribute__((noinline
))
329 kperf_thread_ast_handler(thread_t thread
)
331 BUF_INFO(PERF_AST_HNDLR
| DBG_FUNC_START
, thread
, kperf_get_thread_flags(thread
));
333 /* ~2KB of the stack for the sample since this is called from AST */
334 struct kperf_sample sbuf
;
335 memset(&sbuf
, 0, sizeof(struct kperf_sample
));
337 task_t task
= get_threadtask(thread
);
339 /* make a context, take a sample */
340 struct kperf_context ctx
;
341 ctx
.cur_thread
= thread
;
342 ctx
.cur_pid
= task_pid(task
);
344 /* decode the flags to determine what to sample */
345 unsigned int sample_what
= 0;
346 uint32_t flags
= kperf_get_thread_flags(thread
);
348 if (flags
& T_KPERF_AST_DISPATCH
) {
349 sample_what
|= SAMPLER_TH_DISPATCH
;
351 if (flags
& T_KPERF_AST_CALLSTACK
) {
352 sample_what
|= SAMPLER_USTACK
;
353 sample_what
|= SAMPLER_TH_INFO
;
356 uint32_t ucallstack_depth
= T_KPERF_GET_CALLSTACK_DEPTH(flags
);
358 int r
= kperf_sample_internal(&sbuf
, &ctx
, sample_what
, 0, 0, ucallstack_depth
);
360 BUF_INFO(PERF_AST_HNDLR
| DBG_FUNC_END
, r
);
363 /* register AST bits */
365 kperf_ast_pend(thread_t thread
, uint32_t set_flags
)
367 /* can only pend on the current thread */
368 if (thread
!= current_thread()) {
369 panic("pending to non-current thread");
372 /* get our current bits */
373 uint32_t flags
= kperf_get_thread_flags(thread
);
375 /* see if it's already been done or pended */
376 if (!(flags
& set_flags
)) {
377 /* set the bit on the thread */
379 kperf_set_thread_flags(thread
, flags
);
381 /* set the actual AST */
382 act_set_kperf(thread
);
390 kperf_ast_set_callstack_depth(thread_t thread
, uint32_t depth
)
392 uint32_t ast_flags
= kperf_get_thread_flags(thread
);
393 uint32_t existing_callstack_depth
= T_KPERF_GET_CALLSTACK_DEPTH(ast_flags
);
395 if (existing_callstack_depth
!= depth
) {
396 ast_flags
&= ~T_KPERF_SET_CALLSTACK_DEPTH(depth
);
397 ast_flags
|= T_KPERF_SET_CALLSTACK_DEPTH(depth
);
399 kperf_set_thread_flags(thread
, ast_flags
);
404 kperf_kdbg_cswitch_get(void)
406 return kperf_kdebug_cswitch
;
410 kperf_kdbg_cswitch_set(int newval
)
412 kperf_kdebug_cswitch
= newval
;
413 kperf_on_cpu_update();
419 * Action configuration
422 kperf_action_get_count(void)
428 kperf_action_set_samplers(unsigned actionid
, uint32_t samplers
)
430 if ((actionid
> actionc
) || (actionid
== 0)) {
434 /* disallow both CPU and thread counters to be sampled in the same
436 if ((samplers
& SAMPLER_PMC_THREAD
) && (samplers
& SAMPLER_PMC_CPU
)) {
440 actionv
[actionid
- 1].sample
= samplers
;
446 kperf_action_get_samplers(unsigned actionid
, uint32_t *samplers_out
)
448 if ((actionid
> actionc
)) {
453 *samplers_out
= 0; /* "NULL" action */
455 *samplers_out
= actionv
[actionid
- 1].sample
;
462 kperf_action_set_userdata(unsigned actionid
, uint32_t userdata
)
464 if ((actionid
> actionc
) || (actionid
== 0)) {
468 actionv
[actionid
- 1].userdata
= userdata
;
474 kperf_action_get_userdata(unsigned actionid
, uint32_t *userdata_out
)
476 if ((actionid
> actionc
)) {
481 *userdata_out
= 0; /* "NULL" action */
483 *userdata_out
= actionv
[actionid
- 1].userdata
;
490 kperf_action_set_filter(unsigned actionid
, int pid
)
492 if ((actionid
> actionc
) || (actionid
== 0)) {
496 actionv
[actionid
- 1].pid_filter
= pid
;
502 kperf_action_get_filter(unsigned actionid
, int *pid_out
)
504 if ((actionid
> actionc
)) {
509 *pid_out
= -1; /* "NULL" action */
511 *pid_out
= actionv
[actionid
- 1].pid_filter
;
518 kperf_action_reset(void)
520 for (unsigned int i
= 0; i
< actionc
; i
++) {
521 kperf_action_set_samplers(i
+ 1, 0);
522 kperf_action_set_userdata(i
+ 1, 0);
523 kperf_action_set_filter(i
+ 1, -1);
524 kperf_action_set_ucallstack_depth(i
+ 1, MAX_CALLSTACK_FRAMES
);
525 kperf_action_set_kcallstack_depth(i
+ 1, MAX_CALLSTACK_FRAMES
);
530 kperf_action_set_count(unsigned count
)
532 struct action
*new_actionv
= NULL
, *old_actionv
= NULL
;
536 if (count
== actionc
) {
540 /* TODO: allow shrinking? */
541 if (count
< actionc
) {
545 /* cap it for good measure */
546 if (count
> ACTION_MAX
) {
550 /* creating the action arror for the first time. create a few
555 if ((r
= kperf_init())) {
560 /* create a new array */
561 new_actionv
= kalloc_tag(count
* sizeof(*new_actionv
), VM_KERN_MEMORY_DIAG
);
562 if (new_actionv
== NULL
) {
566 old_actionv
= actionv
;
569 if (old_actionv
!= NULL
) {
570 memcpy(new_actionv
, actionv
, actionc
* sizeof(*actionv
));
573 memset(&(new_actionv
[actionc
]), 0, (count
- old_count
) * sizeof(*actionv
));
575 for (unsigned int i
= old_count
; i
< count
; i
++) {
576 new_actionv
[i
].pid_filter
= -1;
577 new_actionv
[i
].ucallstack_depth
= MAX_CALLSTACK_FRAMES
;
578 new_actionv
[i
].kcallstack_depth
= MAX_CALLSTACK_FRAMES
;
581 actionv
= new_actionv
;
584 if (old_actionv
!= NULL
) {
585 kfree(old_actionv
, old_count
* sizeof(*actionv
));
592 kperf_action_set_ucallstack_depth(unsigned action_id
, uint32_t depth
)
594 if ((action_id
> actionc
) || (action_id
== 0)) {
598 if (depth
> MAX_CALLSTACK_FRAMES
) {
602 actionv
[action_id
- 1].ucallstack_depth
= depth
;
608 kperf_action_set_kcallstack_depth(unsigned action_id
, uint32_t depth
)
610 if ((action_id
> actionc
) || (action_id
== 0)) {
614 if (depth
> MAX_CALLSTACK_FRAMES
) {
618 actionv
[action_id
- 1].kcallstack_depth
= depth
;
624 kperf_action_get_ucallstack_depth(unsigned action_id
, uint32_t * depth_out
)
626 if ((action_id
> actionc
)) {
632 if (action_id
== 0) {
633 *depth_out
= MAX_CALLSTACK_FRAMES
;
635 *depth_out
= actionv
[action_id
- 1].ucallstack_depth
;
642 kperf_action_get_kcallstack_depth(unsigned action_id
, uint32_t * depth_out
)
644 if ((action_id
> actionc
)) {
650 if (action_id
== 0) {
651 *depth_out
= MAX_CALLSTACK_FRAMES
;
653 *depth_out
= actionv
[action_id
- 1].kcallstack_depth
;