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>
41 #include <vm/vm_object.h>
42 #include <vm/vm_page.h>
43 #include <vm/vm_pageout.h>
45 #include <kperf/action.h>
46 #include <kperf/ast.h>
47 #include <kperf/buffer.h>
48 #include <kperf/callstack.h>
49 #include <kperf/context.h>
50 #include <kperf/kdebug_trigger.h>
51 #include <kperf/kperf.h>
52 #include <kperf/kperf_kpc.h>
53 #include <kperf/kperf_timer.h>
54 #include <kperf/pet.h>
55 #include <kperf/sample.h>
56 #include <kperf/thread_samplers.h>
58 #define ACTION_MAX (32)
60 /* the list of different actions to take */
63 uint32_t ucallstack_depth
;
64 uint32_t kcallstack_depth
;
69 /* the list of actions */
70 static unsigned int actionc
= 0;
71 static struct action
*actionv
= NULL
;
73 /* should emit tracepoint on context switch */
74 int kperf_kdebug_cswitch
= 0;
77 kperf_action_has_non_system(unsigned int actionid
)
79 if (actionid
> actionc
) {
83 if (actionv
[actionid
- 1].sample
& ~SAMPLER_SYS_MEM
) {
91 kperf_action_has_task(unsigned int actionid
)
93 if (actionid
> actionc
) {
97 return actionv
[actionid
- 1].sample
& SAMPLER_TASK_MASK
;
101 kperf_action_has_thread(unsigned int actionid
)
103 if (actionid
> actionc
) {
107 return actionv
[actionid
- 1].sample
& SAMPLER_THREAD_MASK
;
111 kperf_system_memory_log(void)
113 BUF_DATA(PERF_MI_SYS_DATA
, (uintptr_t)vm_page_free_count
,
114 (uintptr_t)vm_page_wire_count
, (uintptr_t)vm_page_external_count
,
115 (uintptr_t)(vm_page_active_count
+ vm_page_inactive_count
+
116 vm_page_speculative_count
));
117 BUF_DATA(PERF_MI_SYS_DATA_2
, (uintptr_t)vm_page_anonymous_count
,
118 (uintptr_t)vm_page_internal_count
,
119 (uintptr_t)vm_pageout_vminfo
.vm_pageout_compressions
,
120 (uintptr_t)VM_PAGE_COMPRESSOR_COUNT
);
124 kperf_sample_internal(struct kperf_sample
*sbuf
,
125 struct kperf_context
*context
,
126 unsigned sample_what
, unsigned sample_flags
,
127 unsigned actionid
, uint32_t ucallstack_depth
)
129 int pended_ucallstack
= 0;
130 int pended_th_dispatch
= 0;
131 bool on_idle_thread
= false;
132 uint32_t userdata
= actionid
;
133 bool task_only
= false;
135 /* not much point continuing here, but what to do ? return
136 * Shutdown? cut a tracepoint and continue?
138 if (sample_what
== 0) {
139 return SAMPLE_CONTINUE
;
142 /* callstacks should be explicitly ignored */
143 if (sample_flags
& SAMPLE_FLAG_EMPTY_CALLSTACK
) {
144 sample_what
&= ~(SAMPLER_KSTACK
| SAMPLER_USTACK
);
147 if (sample_flags
& SAMPLE_FLAG_ONLY_SYSTEM
) {
148 sample_what
&= SAMPLER_SYS_MEM
;
151 assert((sample_flags
& (SAMPLE_FLAG_THREAD_ONLY
| SAMPLE_FLAG_TASK_ONLY
))
152 != (SAMPLE_FLAG_THREAD_ONLY
| SAMPLE_FLAG_TASK_ONLY
));
153 if (sample_flags
& SAMPLE_FLAG_THREAD_ONLY
) {
154 sample_what
&= SAMPLER_THREAD_MASK
;
156 if (sample_flags
& SAMPLE_FLAG_TASK_ONLY
) {
158 sample_what
&= SAMPLER_TASK_MASK
;
162 context
->cur_thread
->kperf_pet_gen
= kperf_pet_gen
;
164 bool is_kernel
= (context
->cur_pid
== 0);
166 if (actionid
&& actionid
<= actionc
) {
167 sbuf
->kcallstack
.nframes
= actionv
[actionid
- 1].kcallstack_depth
;
169 sbuf
->kcallstack
.nframes
= MAX_CALLSTACK_FRAMES
;
172 if (ucallstack_depth
) {
173 sbuf
->ucallstack
.nframes
= ucallstack_depth
;
175 sbuf
->ucallstack
.nframes
= MAX_CALLSTACK_FRAMES
;
178 sbuf
->kcallstack
.flags
= CALLSTACK_VALID
;
179 sbuf
->ucallstack
.flags
= CALLSTACK_VALID
;
181 /* an event occurred. Sample everything and dump it in a
185 /* collect data from samplers */
186 if (sample_what
& SAMPLER_TH_INFO
) {
187 kperf_thread_info_sample(&sbuf
->th_info
, context
);
189 /* See if we should drop idle thread samples */
190 if (!(sample_flags
& SAMPLE_FLAG_IDLE_THREADS
)) {
191 if (sbuf
->th_info
.kpthi_runmode
& 0x40) {
192 on_idle_thread
= true;
198 if (sample_what
& SAMPLER_TH_SNAPSHOT
) {
199 kperf_thread_snapshot_sample(&(sbuf
->th_snapshot
), context
);
201 if (sample_what
& SAMPLER_TH_SCHEDULING
) {
202 kperf_thread_scheduling_sample(&(sbuf
->th_scheduling
), context
);
204 if (sample_what
& SAMPLER_KSTACK
) {
205 if (sample_flags
& SAMPLE_FLAG_CONTINUATION
) {
206 kperf_continuation_sample(&(sbuf
->kcallstack
), context
);
207 /* outside of interrupt context, backtrace the current thread */
208 } else if (sample_flags
& SAMPLE_FLAG_NON_INTERRUPT
) {
209 kperf_backtrace_sample(&(sbuf
->kcallstack
), context
);
211 kperf_kcallstack_sample(&(sbuf
->kcallstack
), context
);
214 if (sample_what
& SAMPLER_TK_SNAPSHOT
) {
215 kperf_task_snapshot_sample(context
->cur_task
, &(sbuf
->tk_snapshot
));
220 if (sample_what
& SAMPLER_MEMINFO
) {
221 kperf_meminfo_sample(context
->cur_task
, &(sbuf
->meminfo
));
224 if (sample_flags
& SAMPLE_FLAG_PEND_USER
) {
225 if (sample_what
& SAMPLER_USTACK
) {
226 pended_ucallstack
= kperf_ucallstack_pend(context
, sbuf
->ucallstack
.nframes
);
229 if (sample_what
& SAMPLER_TH_DISPATCH
) {
230 pended_th_dispatch
= kperf_thread_dispatch_pend(context
);
233 if (sample_what
& SAMPLER_USTACK
) {
234 kperf_ucallstack_sample(&(sbuf
->ucallstack
), context
);
237 if (sample_what
& SAMPLER_TH_DISPATCH
) {
238 kperf_thread_dispatch_sample(&(sbuf
->th_dispatch
), context
);
243 if (sample_what
& SAMPLER_PMC_THREAD
) {
244 kperf_kpc_thread_sample(&(sbuf
->kpcdata
), sample_what
);
245 } else if (sample_what
& SAMPLER_PMC_CPU
) {
246 kperf_kpc_cpu_sample(&(sbuf
->kpcdata
), sample_what
);
250 /* lookup the user tag, if any */
251 if (actionid
&& (actionid
<= actionc
)) {
252 userdata
= actionv
[actionid
- 1].userdata
;
255 /* avoid logging if this sample only pended samples */
256 if (sample_flags
& SAMPLE_FLAG_PEND_USER
&&
257 !(sample_what
& ~(SAMPLER_USTACK
| SAMPLER_TH_DISPATCH
))) {
258 return SAMPLE_CONTINUE
;
261 /* stash the data into the buffer
262 * interrupts off to ensure we don't get split
264 boolean_t enabled
= ml_set_interrupts_enabled(FALSE
);
266 BUF_DATA(PERF_GEN_EVENT
| DBG_FUNC_START
, sample_what
,
267 actionid
, userdata
, sample_flags
);
269 if (sample_flags
& SAMPLE_FLAG_SYSTEM
) {
270 if (sample_what
& SAMPLER_SYS_MEM
) {
271 kperf_system_memory_log();
274 if (on_idle_thread
) {
278 if (sample_what
& SAMPLER_TH_INFO
) {
279 kperf_thread_info_log(&sbuf
->th_info
);
281 if (sample_what
& SAMPLER_TH_SCHEDULING
) {
282 kperf_thread_scheduling_log(&(sbuf
->th_scheduling
));
284 if (sample_what
& SAMPLER_TH_SNAPSHOT
) {
285 kperf_thread_snapshot_log(&(sbuf
->th_snapshot
));
287 if (sample_what
& SAMPLER_KSTACK
) {
288 kperf_kcallstack_log(&sbuf
->kcallstack
);
290 if (sample_what
& SAMPLER_TH_INSCYC
) {
291 kperf_thread_inscyc_log(context
);
293 if (sample_what
& SAMPLER_TK_SNAPSHOT
) {
294 kperf_task_snapshot_log(&(sbuf
->tk_snapshot
));
296 if (sample_what
& SAMPLER_TK_INFO
) {
297 kperf_task_info_log(context
);
300 /* dump user stuff */
303 if (sample_what
& SAMPLER_MEMINFO
) {
304 kperf_meminfo_log(&(sbuf
->meminfo
));
307 if (sample_flags
& SAMPLE_FLAG_PEND_USER
) {
308 if (pended_ucallstack
) {
309 BUF_INFO(PERF_CS_UPEND
);
312 if (pended_th_dispatch
) {
313 BUF_INFO(PERF_TI_DISPPEND
);
316 if (sample_what
& SAMPLER_USTACK
) {
317 kperf_ucallstack_log(&(sbuf
->ucallstack
));
320 if (sample_what
& SAMPLER_TH_DISPATCH
) {
321 kperf_thread_dispatch_log(&(sbuf
->th_dispatch
));
326 if (sample_what
& SAMPLER_PMC_THREAD
) {
327 kperf_kpc_thread_log(&(sbuf
->kpcdata
));
328 } else if (sample_what
& SAMPLER_PMC_CPU
) {
329 kperf_kpc_cpu_log(&(sbuf
->kpcdata
));
333 BUF_DATA(PERF_GEN_EVENT
| DBG_FUNC_END
, sample_what
, on_idle_thread
? 1 : 0);
336 ml_set_interrupts_enabled(enabled
);
338 return SAMPLE_CONTINUE
;
341 /* Translate actionid into sample bits and take a sample */
343 kperf_sample(struct kperf_sample
*sbuf
,
344 struct kperf_context
*context
,
345 unsigned actionid
, unsigned sample_flags
)
347 /* work out what to sample, if anything */
348 if ((actionid
> actionc
) || (actionid
== 0)) {
349 return SAMPLE_SHUTDOWN
;
352 /* check the pid filter against the context's current pid.
353 * filter pid == -1 means any pid
355 int pid_filter
= actionv
[actionid
- 1].pid_filter
;
356 if ((pid_filter
!= -1) && (pid_filter
!= context
->cur_pid
)) {
357 return SAMPLE_CONTINUE
;
360 /* the samplers to run */
361 unsigned int sample_what
= actionv
[actionid
- 1].sample
;
363 /* do the actual sample operation */
364 return kperf_sample_internal(sbuf
, context
, sample_what
,
365 sample_flags
, actionid
,
366 actionv
[actionid
- 1].ucallstack_depth
);
370 kperf_kdebug_handler(uint32_t debugid
, uintptr_t *starting_fp
)
372 uint32_t sample_flags
= SAMPLE_FLAG_PEND_USER
;
373 struct kperf_sample
*sample
= NULL
;
374 kern_return_t kr
= KERN_SUCCESS
;
377 if (!kperf_kdebug_should_trigger(debugid
)) {
381 BUF_VERB(PERF_KDBG_HNDLR
| DBG_FUNC_START
, debugid
);
383 thread_t thread
= current_thread();
384 task_t task
= get_threadtask(thread
);
385 struct kperf_context ctx
= {
386 .cur_thread
= thread
,
388 .cur_pid
= task_pid(task
),
389 .trigger_type
= TRIGGER_TYPE_KDEBUG
,
393 s
= ml_set_interrupts_enabled(0);
395 sample
= kperf_intr_sample_buffer();
397 if (!ml_at_interrupt_context()) {
398 sample_flags
|= SAMPLE_FLAG_NON_INTERRUPT
;
399 ctx
.starting_fp
= starting_fp
;
402 kr
= kperf_sample(sample
, &ctx
, kperf_kdebug_get_action(), sample_flags
);
404 ml_set_interrupts_enabled(s
);
405 BUF_VERB(PERF_KDBG_HNDLR
| DBG_FUNC_END
, kr
);
409 * This function allocates >2.3KB of the stack. Prevent the compiler from
410 * inlining this function into ast_taken and ensure the stack memory is only
411 * allocated for the kperf AST.
413 __attribute__((noinline
))
415 kperf_thread_ast_handler(thread_t thread
)
417 BUF_INFO(PERF_AST_HNDLR
| DBG_FUNC_START
, thread
, kperf_get_thread_flags(thread
));
419 /* ~2KB of the stack for the sample since this is called from AST */
420 struct kperf_sample sbuf
;
421 memset(&sbuf
, 0, sizeof(struct kperf_sample
));
423 task_t task
= get_threadtask(thread
);
425 if (task_did_exec(task
) || task_is_exec_copy(task
)) {
426 BUF_INFO(PERF_AST_HNDLR
| DBG_FUNC_END
, SAMPLE_CONTINUE
);
430 /* make a context, take a sample */
431 struct kperf_context ctx
= {
432 .cur_thread
= thread
,
434 .cur_pid
= task_pid(task
),
437 /* decode the flags to determine what to sample */
438 unsigned int sample_what
= 0;
439 uint32_t flags
= kperf_get_thread_flags(thread
);
441 if (flags
& T_KPERF_AST_DISPATCH
) {
442 sample_what
|= SAMPLER_TH_DISPATCH
;
444 if (flags
& T_KPERF_AST_CALLSTACK
) {
445 sample_what
|= SAMPLER_USTACK
;
446 sample_what
|= SAMPLER_TH_INFO
;
449 uint32_t ucallstack_depth
= T_KPERF_GET_CALLSTACK_DEPTH(flags
);
451 int r
= kperf_sample_internal(&sbuf
, &ctx
, sample_what
, 0, 0, ucallstack_depth
);
453 BUF_INFO(PERF_AST_HNDLR
| DBG_FUNC_END
, r
);
456 /* register AST bits */
458 kperf_ast_pend(thread_t thread
, uint32_t set_flags
)
460 /* can only pend on the current thread */
461 if (thread
!= current_thread()) {
462 panic("pending to non-current thread");
465 /* get our current bits */
466 uint32_t flags
= kperf_get_thread_flags(thread
);
468 /* see if it's already been done or pended */
469 if (!(flags
& set_flags
)) {
470 /* set the bit on the thread */
472 kperf_set_thread_flags(thread
, flags
);
474 /* set the actual AST */
475 act_set_kperf(thread
);
483 kperf_ast_set_callstack_depth(thread_t thread
, uint32_t depth
)
485 uint32_t ast_flags
= kperf_get_thread_flags(thread
);
486 uint32_t existing_callstack_depth
= T_KPERF_GET_CALLSTACK_DEPTH(ast_flags
);
488 if (existing_callstack_depth
!= depth
) {
489 ast_flags
&= ~T_KPERF_SET_CALLSTACK_DEPTH(depth
);
490 ast_flags
|= T_KPERF_SET_CALLSTACK_DEPTH(depth
);
492 kperf_set_thread_flags(thread
, ast_flags
);
497 kperf_kdbg_cswitch_get(void)
499 return kperf_kdebug_cswitch
;
503 kperf_kdbg_cswitch_set(int newval
)
505 kperf_kdebug_cswitch
= newval
;
506 kperf_on_cpu_update();
512 * Action configuration
515 kperf_action_get_count(void)
521 kperf_action_set_samplers(unsigned actionid
, uint32_t samplers
)
523 if ((actionid
> actionc
) || (actionid
== 0)) {
527 /* disallow both CPU and thread counters to be sampled in the same
529 if ((samplers
& SAMPLER_PMC_THREAD
) && (samplers
& SAMPLER_PMC_CPU
)) {
533 actionv
[actionid
- 1].sample
= samplers
;
539 kperf_action_get_samplers(unsigned actionid
, uint32_t *samplers_out
)
541 if ((actionid
> actionc
)) {
546 *samplers_out
= 0; /* "NULL" action */
548 *samplers_out
= actionv
[actionid
- 1].sample
;
555 kperf_action_set_userdata(unsigned actionid
, uint32_t userdata
)
557 if ((actionid
> actionc
) || (actionid
== 0)) {
561 actionv
[actionid
- 1].userdata
= userdata
;
567 kperf_action_get_userdata(unsigned actionid
, uint32_t *userdata_out
)
569 if ((actionid
> actionc
)) {
574 *userdata_out
= 0; /* "NULL" action */
576 *userdata_out
= actionv
[actionid
- 1].userdata
;
583 kperf_action_set_filter(unsigned actionid
, int pid
)
585 if ((actionid
> actionc
) || (actionid
== 0)) {
589 actionv
[actionid
- 1].pid_filter
= pid
;
595 kperf_action_get_filter(unsigned actionid
, int *pid_out
)
597 if ((actionid
> actionc
)) {
602 *pid_out
= -1; /* "NULL" action */
604 *pid_out
= actionv
[actionid
- 1].pid_filter
;
611 kperf_action_reset(void)
613 for (unsigned int i
= 0; i
< actionc
; i
++) {
614 kperf_action_set_samplers(i
+ 1, 0);
615 kperf_action_set_userdata(i
+ 1, 0);
616 kperf_action_set_filter(i
+ 1, -1);
617 kperf_action_set_ucallstack_depth(i
+ 1, MAX_CALLSTACK_FRAMES
);
618 kperf_action_set_kcallstack_depth(i
+ 1, MAX_CALLSTACK_FRAMES
);
623 kperf_action_set_count(unsigned count
)
625 struct action
*new_actionv
= NULL
, *old_actionv
= NULL
;
629 if (count
== actionc
) {
633 /* TODO: allow shrinking? */
634 if (count
< actionc
) {
638 /* cap it for good measure */
639 if (count
> ACTION_MAX
) {
643 /* creating the action arror for the first time. create a few
648 if ((r
= kperf_init())) {
653 /* create a new array */
654 new_actionv
= kalloc_tag(count
* sizeof(*new_actionv
), VM_KERN_MEMORY_DIAG
);
655 if (new_actionv
== NULL
) {
659 old_actionv
= actionv
;
662 if (old_actionv
!= NULL
) {
663 memcpy(new_actionv
, actionv
, actionc
* sizeof(*actionv
));
666 memset(&(new_actionv
[actionc
]), 0, (count
- old_count
) * sizeof(*actionv
));
668 for (unsigned int i
= old_count
; i
< count
; i
++) {
669 new_actionv
[i
].pid_filter
= -1;
670 new_actionv
[i
].ucallstack_depth
= MAX_CALLSTACK_FRAMES
;
671 new_actionv
[i
].kcallstack_depth
= MAX_CALLSTACK_FRAMES
;
674 actionv
= new_actionv
;
677 if (old_actionv
!= NULL
) {
678 kfree(old_actionv
, old_count
* sizeof(*actionv
));
685 kperf_action_set_ucallstack_depth(unsigned action_id
, uint32_t depth
)
687 if ((action_id
> actionc
) || (action_id
== 0)) {
691 if (depth
> MAX_CALLSTACK_FRAMES
) {
695 actionv
[action_id
- 1].ucallstack_depth
= depth
;
701 kperf_action_set_kcallstack_depth(unsigned action_id
, uint32_t depth
)
703 if ((action_id
> actionc
) || (action_id
== 0)) {
707 if (depth
> MAX_CALLSTACK_FRAMES
) {
711 actionv
[action_id
- 1].kcallstack_depth
= depth
;
717 kperf_action_get_ucallstack_depth(unsigned action_id
, uint32_t * depth_out
)
719 if ((action_id
> actionc
)) {
725 if (action_id
== 0) {
726 *depth_out
= MAX_CALLSTACK_FRAMES
;
728 *depth_out
= actionv
[action_id
- 1].ucallstack_depth
;
735 kperf_action_get_kcallstack_depth(unsigned action_id
, uint32_t * depth_out
)
737 if ((action_id
> actionc
)) {
743 if (action_id
== 0) {
744 *depth_out
= MAX_CALLSTACK_FRAMES
;
746 *depth_out
= actionv
[action_id
- 1].kcallstack_depth
;