2 * Copyright (c) 2011-2016 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 /* all thread states code */
30 #include <mach/mach_types.h>
31 #include <sys/errno.h>
33 #include <kperf/kperf.h>
34 #include <kperf/buffer.h>
35 #include <kperf/sample.h>
36 #include <kperf/context.h>
37 #include <kperf/action.h>
38 #include <kperf/pet.h>
39 #include <kperf/kperf_timer.h>
41 #include <kern/task.h>
42 #include <kern/kalloc.h>
44 /* action ID to call for each sample
46 * Address is used as the sync point for waiting.
48 static unsigned int pet_action_id
= 0;
50 static lck_mtx_t
*pet_lock
;
51 static boolean_t pet_initted
= FALSE
;
52 static boolean_t pet_running
= FALSE
;
54 /* number of callstack samples to skip for idle threads */
55 static uint32_t pet_idle_rate
= KPERF_PET_DEFAULT_IDLE_RATE
;
58 * Lightweight PET mode samples the system less-intrusively than normal PET
59 * mode. Instead of iterating tasks and threads on each sample, it increments
60 * a global generation count, kperf_pet_gen, which is checked as threads are
61 * context switched on-core. If the thread's local generation count is older
62 * than the global generation, the thread samples itself.
65 * thread A +--+---------|
67 * thread B |--+---------------|
69 * thread C | | |-------------------------------------
71 * thread D | | | |-------------------------------
73 * +--+---------+-----+--------------------------------> time
75 * | +-----+--- threads sampled when they come on-core in
76 * | kperf_pet_switch_context
78 * +--- PET timer fire, sample on-core threads A and B,
79 * increment kperf_pet_gen
81 static boolean_t lightweight_pet
= FALSE
;
84 * Whether or not lightweight PET and sampling is active.
86 boolean_t kperf_lightweight_pet_active
= FALSE
;
88 uint32_t kperf_pet_gen
= 0;
90 static struct kperf_sample
*pet_sample
;
92 /* thread lifecycle */
94 static kern_return_t
pet_init(void);
95 static void pet_start(void);
96 static void pet_stop(void);
100 static void pet_thread_loop(void *param
, wait_result_t wr
);
101 static void pet_thread_idle(void);
102 static void pet_thread_work_unit(void);
104 /* listing things to sample */
106 static task_array_t pet_tasks
= NULL
;
107 static vm_size_t pet_tasks_size
= 0;
108 static vm_size_t pet_tasks_count
= 0;
110 static thread_array_t pet_threads
= NULL
;
111 static vm_size_t pet_threads_size
= 0;
112 static vm_size_t pet_threads_count
= 0;
114 static kern_return_t
pet_tasks_prepare(void);
115 static kern_return_t
pet_tasks_prepare_internal(void);
117 static kern_return_t
pet_threads_prepare(task_t task
);
121 static void pet_sample_all_tasks(uint32_t idle_rate
);
122 static void pet_sample_task(task_t task
, uint32_t idle_rate
);
123 static void pet_sample_thread(int pid
, thread_t thread
, uint32_t idle_rate
);
125 /* functions called by other areas of kperf */
128 kperf_pet_fire_before(void)
130 if (!pet_initted
|| !pet_running
) {
134 if (lightweight_pet
) {
135 BUF_INFO(PERF_PET_SAMPLE
);
136 OSIncrementAtomic(&kperf_pet_gen
);
141 kperf_pet_fire_after(void)
143 if (!pet_initted
|| !pet_running
) {
147 if (lightweight_pet
) {
148 kperf_timer_pet_rearm(0);
150 thread_wakeup(&pet_action_id
);
155 kperf_pet_on_cpu(thread_t thread
, thread_continue_t continuation
,
156 uintptr_t *starting_fp
)
158 assert(thread
!= NULL
);
159 assert(ml_get_interrupts_enabled() == FALSE
);
161 if (thread
->kperf_pet_gen
!= kperf_pet_gen
) {
162 BUF_VERB(PERF_PET_SAMPLE_THREAD
| DBG_FUNC_START
, kperf_pet_gen
, thread
->kperf_pet_gen
);
164 struct kperf_context ctx
= {
165 .cur_thread
= thread
,
166 .cur_pid
= task_pid(get_threadtask(thread
)),
167 .starting_fp
= starting_fp
,
170 * Use a per-CPU interrupt buffer, since this is only called
171 * while interrupts are disabled, from the scheduler.
173 struct kperf_sample
*sample
= kperf_intr_sample_buffer();
175 BUF_VERB(PERF_PET_SAMPLE_THREAD
| DBG_FUNC_END
, 1);
179 unsigned int flags
= SAMPLE_FLAG_NON_INTERRUPT
| SAMPLE_FLAG_PEND_USER
;
180 if (continuation
!= NULL
) {
181 flags
|= SAMPLE_FLAG_CONTINUATION
;
183 kperf_sample(sample
, &ctx
, pet_action_id
, flags
);
185 BUF_VERB(PERF_PET_SAMPLE_THREAD
| DBG_FUNC_END
);
187 BUF_VERB(PERF_PET_SAMPLE_THREAD
, kperf_pet_gen
, thread
->kperf_pet_gen
);
192 kperf_pet_config(unsigned int action_id
)
194 kern_return_t kr
= pet_init();
195 if (kr
!= KERN_SUCCESS
) {
199 lck_mtx_lock(pet_lock
);
201 BUF_INFO(PERF_PET_THREAD
, 3, action_id
);
203 if (action_id
== 0) {
209 pet_action_id
= action_id
;
211 lck_mtx_unlock(pet_lock
);
214 /* handle resource allocation */
219 lck_mtx_assert(pet_lock
, LCK_MTX_ASSERT_OWNED
);
225 pet_sample
= kalloc(sizeof(struct kperf_sample
));
236 lck_mtx_assert(pet_lock
, LCK_MTX_ASSERT_OWNED
);
242 if (pet_tasks
!= NULL
) {
243 assert(pet_tasks_size
!= 0);
244 kfree(pet_tasks
, pet_tasks_size
);
251 if (pet_threads
!= NULL
) {
252 assert(pet_threads_size
!= 0);
253 kfree(pet_threads
, pet_threads_size
);
256 pet_threads_size
= 0;
257 pet_threads_count
= 0;
260 if (pet_sample
!= NULL
) {
261 kfree(pet_sample
, sizeof(struct kperf_sample
));
269 * Lazily initialize PET. The PET thread never exits once PET has been used
279 /* make the sync point */
280 pet_lock
= lck_mtx_alloc_init(&kperf_lck_grp
, NULL
);
283 /* create the thread */
285 BUF_INFO(PERF_PET_THREAD
, 0);
287 kern_return_t kr
= kernel_thread_start(pet_thread_loop
, NULL
, &t
);
288 if (kr
!= KERN_SUCCESS
) {
289 lck_mtx_free(pet_lock
, &kperf_lck_grp
);
293 thread_set_thread_name(t
, "kperf sampling");
294 /* let the thread hold the only reference */
295 thread_deallocate(t
);
302 /* called by PET thread only */
305 pet_thread_work_unit(void)
307 pet_sample_all_tasks(pet_idle_rate
);
311 pet_thread_idle(void)
313 lck_mtx_assert(pet_lock
, LCK_MTX_ASSERT_OWNED
);
315 (void)lck_mtx_sleep(pet_lock
, LCK_SLEEP_DEFAULT
, &pet_action_id
,
319 __attribute__((noreturn
))
321 pet_thread_loop(void *param
, wait_result_t wr
)
323 #pragma unused(param, wr)
324 uint64_t work_unit_ticks
;
326 BUF_INFO(PERF_PET_THREAD
, 1);
328 lck_mtx_lock(pet_lock
);
330 BUF_INFO(PERF_PET_IDLE
);
333 BUF_INFO(PERF_PET_RUN
);
335 /* measure how long the work unit takes */
336 work_unit_ticks
= mach_absolute_time();
337 pet_thread_work_unit();
338 work_unit_ticks
= mach_absolute_time() - work_unit_ticks
;
340 /* re-program the timer */
341 kperf_timer_pet_rearm(work_unit_ticks
);
348 pet_sample_thread(int pid
, thread_t thread
, uint32_t idle_rate
)
350 lck_mtx_assert(pet_lock
, LCK_MTX_ASSERT_OWNED
);
352 uint32_t sample_flags
= SAMPLE_FLAG_IDLE_THREADS
;
354 BUF_VERB(PERF_PET_SAMPLE_THREAD
| DBG_FUNC_START
);
356 /* work out the context */
357 struct kperf_context ctx
= {
358 .cur_thread
= thread
,
362 boolean_t thread_dirty
= kperf_thread_get_dirty(thread
);
365 * Clean a dirty thread and skip callstack sample if the thread was not
366 * dirty and thread has skipped less than pet_idle_rate samples.
369 kperf_thread_set_dirty(thread
, FALSE
);
370 } else if ((thread
->kperf_pet_cnt
% idle_rate
) != 0) {
371 sample_flags
|= SAMPLE_FLAG_EMPTY_CALLSTACK
;
373 thread
->kperf_pet_cnt
++;
375 kperf_sample(pet_sample
, &ctx
, pet_action_id
, sample_flags
);
377 BUF_VERB(PERF_PET_SAMPLE_THREAD
| DBG_FUNC_END
);
381 pet_threads_prepare(task_t task
)
383 lck_mtx_assert(pet_lock
, LCK_MTX_ASSERT_OWNED
);
385 vm_size_t threads_size_needed
;
387 if (task
== TASK_NULL
) {
388 return KERN_INVALID_ARGUMENT
;
400 /* do we have the memory we need? */
401 threads_size_needed
= task
->thread_count
* sizeof(thread_t
);
402 if (threads_size_needed
<= pet_threads_size
) {
406 /* not enough memory, unlock the task and increase allocation */
409 if (pet_threads_size
!= 0) {
410 kfree(pet_threads
, pet_threads_size
);
413 assert(threads_size_needed
> 0);
414 pet_threads_size
= threads_size_needed
;
416 pet_threads
= kalloc(pet_threads_size
);
417 if (pet_threads
== NULL
) {
418 pet_threads_size
= 0;
419 return KERN_RESOURCE_SHORTAGE
;
423 /* have memory and the task is locked and active */
425 pet_threads_count
= 0;
426 queue_iterate(&(task
->threads
), thread
, thread_t
, task_threads
) {
427 thread_reference_internal(thread
);
428 pet_threads
[pet_threads_count
++] = thread
;
431 /* can unlock task now that threads are referenced */
434 return (pet_threads_count
== 0) ? KERN_FAILURE
: KERN_SUCCESS
;
438 pet_sample_task(task_t task
, uint32_t idle_rate
)
440 lck_mtx_assert(pet_lock
, LCK_MTX_ASSERT_OWNED
);
442 BUF_VERB(PERF_PET_SAMPLE_TASK
| DBG_FUNC_START
);
444 kern_return_t kr
= pet_threads_prepare(task
);
445 if (kr
!= KERN_SUCCESS
) {
446 BUF_INFO(PERF_PET_ERROR
, ERR_THREAD
, kr
);
447 BUF_VERB(PERF_PET_SAMPLE_TASK
| DBG_FUNC_END
, 1);
451 int pid
= task_pid(task
);
453 for (unsigned int i
= 0; i
< pet_threads_count
; i
++) {
454 thread_t thread
= pet_threads
[i
];
458 /* do not sample the thread if it was on a CPU during the IPI. */
459 for (cpu
= 0; cpu
< machine_info
.logical_cpu_max
; cpu
++) {
460 if (kperf_tid_on_cpus
[cpu
] == thread_tid(thread
)) {
465 /* the thread was not on a CPU */
466 if (cpu
== machine_info
.logical_cpu_max
) {
467 pet_sample_thread(pid
, thread
, idle_rate
);
470 thread_deallocate(pet_threads
[i
]);
473 BUF_VERB(PERF_PET_SAMPLE_TASK
| DBG_FUNC_END
, pet_threads_count
);
477 pet_tasks_prepare_internal(void)
479 lck_mtx_assert(pet_lock
, LCK_MTX_ASSERT_OWNED
);
481 vm_size_t tasks_size_needed
= 0;
484 lck_mtx_lock(&tasks_threads_lock
);
486 /* do we have the memory we need? */
487 tasks_size_needed
= tasks_count
* sizeof(task_t
);
488 if (tasks_size_needed
<= pet_tasks_size
) {
492 /* unlock and allocate more memory */
493 lck_mtx_unlock(&tasks_threads_lock
);
495 /* grow task array */
496 if (tasks_size_needed
> pet_tasks_size
) {
497 if (pet_tasks_size
!= 0) {
498 kfree(pet_tasks
, pet_tasks_size
);
501 assert(tasks_size_needed
> 0);
502 pet_tasks_size
= tasks_size_needed
;
504 pet_tasks
= (task_array_t
)kalloc(pet_tasks_size
);
505 if (pet_tasks
== NULL
) {
507 return KERN_RESOURCE_SHORTAGE
;
516 pet_tasks_prepare(void)
518 lck_mtx_assert(pet_lock
, LCK_MTX_ASSERT_OWNED
);
520 /* allocate space and take the tasks_threads_lock */
521 kern_return_t kr
= pet_tasks_prepare_internal();
522 if (KERN_SUCCESS
!= kr
) {
525 lck_mtx_assert(&tasks_threads_lock
, LCK_MTX_ASSERT_OWNED
);
527 /* make sure the tasks are not deallocated after dropping the lock */
530 queue_iterate(&tasks
, task
, task_t
, tasks
) {
531 if (task
!= kernel_task
) {
532 task_reference_internal(task
);
533 pet_tasks
[pet_tasks_count
++] = task
;
537 lck_mtx_unlock(&tasks_threads_lock
);
543 pet_sample_all_tasks(uint32_t idle_rate
)
545 lck_mtx_assert(pet_lock
, LCK_MTX_ASSERT_OWNED
);
547 BUF_INFO(PERF_PET_SAMPLE
| DBG_FUNC_START
);
549 kern_return_t kr
= pet_tasks_prepare();
550 if (kr
!= KERN_SUCCESS
) {
551 BUF_INFO(PERF_PET_ERROR
, ERR_TASK
, kr
);
552 BUF_INFO(PERF_PET_SAMPLE
| DBG_FUNC_END
, 0);
556 for (unsigned int i
= 0; i
< pet_tasks_count
; i
++) {
557 task_t task
= pet_tasks
[i
];
559 if (task
!= kernel_task
) {
560 kr
= task_suspend_internal(task
);
561 if (kr
!= KERN_SUCCESS
) {
566 pet_sample_task(task
, idle_rate
);
568 if (task
!= kernel_task
) {
569 task_resume_internal(task
);
573 for(unsigned int i
= 0; i
< pet_tasks_count
; i
++) {
574 task_deallocate(pet_tasks
[i
]);
577 BUF_INFO(PERF_PET_SAMPLE
| DBG_FUNC_END
, pet_tasks_count
);
580 /* support sysctls */
583 kperf_get_pet_idle_rate(void)
585 return pet_idle_rate
;
589 kperf_set_pet_idle_rate(int val
)
597 kperf_get_lightweight_pet(void)
599 return lightweight_pet
;
603 kperf_set_lightweight_pet(int val
)
605 if (kperf_sampling_status() == KPERF_SAMPLING_ON
) {
609 lightweight_pet
= (val
== 1);
610 kperf_lightweight_pet_active_update();
616 kperf_lightweight_pet_active_update(void)
618 kperf_lightweight_pet_active
= (kperf_sampling_status() && lightweight_pet
);
619 kperf_on_cpu_update();