]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kperf/pet.c
xnu-6153.61.1.tar.gz
[apple/xnu.git] / osfmk / kperf / pet.c
CommitLineData
316670eb 1/*
39037602 2 * Copyright (c) 2011-2016 Apple Computer, Inc. All rights reserved.
316670eb
A
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
39037602 5 *
316670eb
A
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.
39037602 14 *
316670eb
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
39037602 17 *
316670eb
A
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.
39037602 25 *
316670eb
A
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29/* all thread states code */
30#include <mach/mach_types.h>
316670eb
A
31#include <sys/errno.h>
32
39037602 33#include <kperf/kperf.h>
316670eb
A
34#include <kperf/buffer.h>
35#include <kperf/sample.h>
36#include <kperf/context.h>
37#include <kperf/action.h>
316670eb 38#include <kperf/pet.h>
39037602 39#include <kperf/kperf_timer.h>
316670eb 40
3e170ce0 41#include <kern/task.h>
39037602
A
42#include <kern/kalloc.h>
43
44/* action ID to call for each sample
45 *
46 * Address is used as the sync point for waiting.
47 */
48static unsigned int pet_action_id = 0;
49
50static lck_mtx_t *pet_lock;
51static boolean_t pet_initted = FALSE;
52static boolean_t pet_running = FALSE;
fe8ab488 53
39037602
A
54/* number of callstack samples to skip for idle threads */
55static uint32_t pet_idle_rate = KPERF_PET_DEFAULT_IDLE_RATE;
316670eb 56
39037602
A
57/*
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.
63 *
64 * | |
65 * thread A +--+---------|
66 * | |
67 * thread B |--+---------------|
68 * | |
69 * thread C | | |-------------------------------------
70 * | | |
71 * thread D | | | |-------------------------------
72 * | | | |
73 * +--+---------+-----+--------------------------------> time
74 * | │ |
75 * | +-----+--- threads sampled when they come on-core in
76 * | kperf_pet_switch_context
77 * |
78 * +--- PET timer fire, sample on-core threads A and B,
79 * increment kperf_pet_gen
316670eb 80 */
39037602 81static boolean_t lightweight_pet = FALSE;
316670eb 82
39037602
A
83/*
84 * Whether or not lightweight PET and sampling is active.
85 */
86boolean_t kperf_lightweight_pet_active = FALSE;
316670eb 87
39037602 88uint32_t kperf_pet_gen = 0;
316670eb 89
39037602 90static struct kperf_sample *pet_sample;
316670eb 91
39037602 92/* thread lifecycle */
39236c6e 93
39037602
A
94static kern_return_t pet_init(void);
95static void pet_start(void);
96static void pet_stop(void);
316670eb 97
39037602 98/* PET thread-only */
316670eb 99
39037602
A
100static void pet_thread_loop(void *param, wait_result_t wr);
101static void pet_thread_idle(void);
102static void pet_thread_work_unit(void);
316670eb 103
39037602 104/* listing things to sample */
39236c6e 105
39037602
A
106static task_array_t pet_tasks = NULL;
107static vm_size_t pet_tasks_size = 0;
108static vm_size_t pet_tasks_count = 0;
39236c6e 109
39037602
A
110static thread_array_t pet_threads = NULL;
111static vm_size_t pet_threads_size = 0;
112static vm_size_t pet_threads_count = 0;
39236c6e 113
39037602
A
114static kern_return_t pet_tasks_prepare(void);
115static kern_return_t pet_tasks_prepare_internal(void);
316670eb 116
39037602 117static kern_return_t pet_threads_prepare(task_t task);
316670eb 118
39037602 119/* sampling */
316670eb 120
39037602
A
121static void pet_sample_all_tasks(uint32_t idle_rate);
122static void pet_sample_task(task_t task, uint32_t idle_rate);
d9a64523 123static void pet_sample_thread(int pid, task_t task, thread_t thread,
0a7de745 124 uint32_t idle_rate);
316670eb 125
39037602 126/* functions called by other areas of kperf */
39236c6e 127
39037602
A
128void
129kperf_pet_fire_before(void)
130{
131 if (!pet_initted || !pet_running) {
132 return;
133 }
134
135 if (lightweight_pet) {
136 BUF_INFO(PERF_PET_SAMPLE);
137 OSIncrementAtomic(&kperf_pet_gen);
316670eb
A
138 }
139}
140
39037602
A
141void
142kperf_pet_fire_after(void)
316670eb 143{
39037602 144 if (!pet_initted || !pet_running) {
316670eb
A
145 return;
146 }
147
39037602
A
148 if (lightweight_pet) {
149 kperf_timer_pet_rearm(0);
150 } else {
151 thread_wakeup(&pet_action_id);
152 }
316670eb
A
153}
154
39037602
A
155void
156kperf_pet_on_cpu(thread_t thread, thread_continue_t continuation,
0a7de745 157 uintptr_t *starting_fp)
316670eb 158{
39037602
A
159 assert(thread != NULL);
160 assert(ml_get_interrupts_enabled() == FALSE);
161
0a7de745
A
162 uint32_t actionid = pet_action_id;
163 if (actionid == 0) {
164 return;
165 }
166
39037602
A
167 if (thread->kperf_pet_gen != kperf_pet_gen) {
168 BUF_VERB(PERF_PET_SAMPLE_THREAD | DBG_FUNC_START, kperf_pet_gen, thread->kperf_pet_gen);
169
d9a64523 170 task_t task = get_threadtask(thread);
39037602
A
171 struct kperf_context ctx = {
172 .cur_thread = thread,
d9a64523
A
173 .cur_task = task,
174 .cur_pid = task_pid(task),
39037602
A
175 .starting_fp = starting_fp,
176 };
177 /*
178 * Use a per-CPU interrupt buffer, since this is only called
179 * while interrupts are disabled, from the scheduler.
316670eb 180 */
39037602
A
181 struct kperf_sample *sample = kperf_intr_sample_buffer();
182 if (!sample) {
183 BUF_VERB(PERF_PET_SAMPLE_THREAD | DBG_FUNC_END, 1);
184 return;
316670eb 185 }
39236c6e 186
39037602
A
187 unsigned int flags = SAMPLE_FLAG_NON_INTERRUPT | SAMPLE_FLAG_PEND_USER;
188 if (continuation != NULL) {
189 flags |= SAMPLE_FLAG_CONTINUATION;
316670eb 190 }
0a7de745 191 kperf_sample(sample, &ctx, actionid, flags);
39236c6e 192
39037602
A
193 BUF_VERB(PERF_PET_SAMPLE_THREAD | DBG_FUNC_END);
194 } else {
195 BUF_VERB(PERF_PET_SAMPLE_THREAD, kperf_pet_gen, thread->kperf_pet_gen);
196 }
197}
316670eb 198
39037602
A
199void
200kperf_pet_config(unsigned int action_id)
201{
0a7de745
A
202 if (action_id == 0 && !pet_initted) {
203 return;
204 }
205
39037602
A
206 kern_return_t kr = pet_init();
207 if (kr != KERN_SUCCESS) {
208 return;
316670eb 209 }
39037602
A
210
211 lck_mtx_lock(pet_lock);
212
213 BUF_INFO(PERF_PET_THREAD, 3, action_id);
214
215 if (action_id == 0) {
216 pet_stop();
217 } else {
218 pet_start();
219 }
220
221 pet_action_id = action_id;
222
223 lck_mtx_unlock(pet_lock);
316670eb
A
224}
225
39037602
A
226/* handle resource allocation */
227
228void
229pet_start(void)
316670eb 230{
39037602 231 lck_mtx_assert(pet_lock, LCK_MTX_ASSERT_OWNED);
316670eb 232
39037602
A
233 if (pet_running) {
234 return;
235 }
316670eb 236
39037602
A
237 pet_sample = kalloc(sizeof(struct kperf_sample));
238 if (!pet_sample) {
316670eb
A
239 return;
240 }
241
39037602 242 pet_running = TRUE;
316670eb
A
243}
244
39037602
A
245void
246pet_stop(void)
316670eb 247{
39037602
A
248 lck_mtx_assert(pet_lock, LCK_MTX_ASSERT_OWNED);
249
250 if (!pet_initted) {
316670eb
A
251 return;
252 }
253
39037602
A
254 if (pet_tasks != NULL) {
255 assert(pet_tasks_size != 0);
256 kfree(pet_tasks, pet_tasks_size);
316670eb 257
39037602
A
258 pet_tasks = NULL;
259 pet_tasks_size = 0;
260 pet_tasks_count = 0;
316670eb
A
261 }
262
39037602
A
263 if (pet_threads != NULL) {
264 assert(pet_threads_size != 0);
265 kfree(pet_threads, pet_threads_size);
266
267 pet_threads = NULL;
268 pet_threads_size = 0;
269 pet_threads_count = 0;
270 }
316670eb 271
39037602
A
272 if (pet_sample != NULL) {
273 kfree(pet_sample, sizeof(struct kperf_sample));
274 pet_sample = NULL;
275 }
316670eb 276
39037602 277 pet_running = FALSE;
316670eb
A
278}
279
39037602
A
280/*
281 * Lazily initialize PET. The PET thread never exits once PET has been used
282 * once.
283 */
284static kern_return_t
285pet_init(void)
316670eb 286{
39037602
A
287 if (pet_initted) {
288 return KERN_SUCCESS;
289 }
316670eb 290
39037602
A
291 /* make the sync point */
292 pet_lock = lck_mtx_alloc_init(&kperf_lck_grp, NULL);
0a7de745 293 assert(pet_lock != NULL);
316670eb 294
39037602
A
295 /* create the thread */
296
297 BUF_INFO(PERF_PET_THREAD, 0);
298 thread_t t;
299 kern_return_t kr = kernel_thread_start(pet_thread_loop, NULL, &t);
300 if (kr != KERN_SUCCESS) {
301 lck_mtx_free(pet_lock, &kperf_lck_grp);
302 return kr;
316670eb
A
303 }
304
39037602
A
305 thread_set_thread_name(t, "kperf sampling");
306 /* let the thread hold the only reference */
307 thread_deallocate(t);
308
309 pet_initted = TRUE;
316670eb 310
39037602 311 return KERN_SUCCESS;
316670eb
A
312}
313
39037602
A
314/* called by PET thread only */
315
316static void
317pet_thread_work_unit(void)
316670eb 318{
39037602 319 pet_sample_all_tasks(pet_idle_rate);
316670eb
A
320}
321
316670eb 322static void
39037602 323pet_thread_idle(void)
316670eb 324{
39037602
A
325 lck_mtx_assert(pet_lock, LCK_MTX_ASSERT_OWNED);
326
0a7de745
A
327 do {
328 (void)lck_mtx_sleep(pet_lock, LCK_SLEEP_DEFAULT, &pet_action_id,
329 THREAD_UNINT);
330 } while (pet_action_id == 0);
39037602
A
331}
332
333__attribute__((noreturn))
334static void
335pet_thread_loop(void *param, wait_result_t wr)
336{
337#pragma unused(param, wr)
39236c6e
A
338 uint64_t work_unit_ticks;
339
39037602 340 BUF_INFO(PERF_PET_THREAD, 1);
316670eb 341
39037602
A
342 lck_mtx_lock(pet_lock);
343 for (;;) {
344 BUF_INFO(PERF_PET_IDLE);
345 pet_thread_idle();
316670eb 346
39037602 347 BUF_INFO(PERF_PET_RUN);
39236c6e
A
348
349 /* measure how long the work unit takes */
350 work_unit_ticks = mach_absolute_time();
39037602 351 pet_thread_work_unit();
39236c6e 352 work_unit_ticks = mach_absolute_time() - work_unit_ticks;
316670eb
A
353
354 /* re-program the timer */
39037602
A
355 kperf_timer_pet_rearm(work_unit_ticks);
356 }
357}
316670eb 358
39037602
A
359/* sampling */
360
361static void
d9a64523 362pet_sample_thread(int pid, task_t task, thread_t thread, uint32_t idle_rate)
39037602
A
363{
364 lck_mtx_assert(pet_lock, LCK_MTX_ASSERT_OWNED);
365
94ff46dc
A
366 uint32_t sample_flags = SAMPLE_FLAG_IDLE_THREADS |
367 SAMPLE_FLAG_THREAD_ONLY;
39037602
A
368
369 BUF_VERB(PERF_PET_SAMPLE_THREAD | DBG_FUNC_START);
370
371 /* work out the context */
372 struct kperf_context ctx = {
373 .cur_thread = thread,
d9a64523 374 .cur_task = task,
39037602
A
375 .cur_pid = pid,
376 };
377
378 boolean_t thread_dirty = kperf_thread_get_dirty(thread);
379
380 /*
381 * Clean a dirty thread and skip callstack sample if the thread was not
382 * dirty and thread has skipped less than pet_idle_rate samples.
383 */
384 if (thread_dirty) {
385 kperf_thread_set_dirty(thread, FALSE);
386 } else if ((thread->kperf_pet_cnt % idle_rate) != 0) {
387 sample_flags |= SAMPLE_FLAG_EMPTY_CALLSTACK;
316670eb 388 }
39037602
A
389 thread->kperf_pet_cnt++;
390
391 kperf_sample(pet_sample, &ctx, pet_action_id, sample_flags);
94ff46dc
A
392 kperf_sample_user(&pet_sample->usample, &ctx, pet_action_id,
393 sample_flags);
39037602
A
394
395 BUF_VERB(PERF_PET_SAMPLE_THREAD | DBG_FUNC_END);
316670eb
A
396}
397
39037602
A
398static kern_return_t
399pet_threads_prepare(task_t task)
316670eb 400{
39037602
A
401 lck_mtx_assert(pet_lock, LCK_MTX_ASSERT_OWNED);
402
403 vm_size_t threads_size_needed;
404
405 if (task == TASK_NULL) {
406 return KERN_INVALID_ARGUMENT;
407 }
408
409 for (;;) {
410 task_lock(task);
411
412 if (!task->active) {
413 task_unlock(task);
39236c6e 414
39037602
A
415 return KERN_FAILURE;
416 }
417
418 /* do we have the memory we need? */
419 threads_size_needed = task->thread_count * sizeof(thread_t);
420 if (threads_size_needed <= pet_threads_size) {
421 break;
422 }
316670eb 423
39037602
A
424 /* not enough memory, unlock the task and increase allocation */
425 task_unlock(task);
316670eb 426
39037602
A
427 if (pet_threads_size != 0) {
428 kfree(pet_threads, pet_threads_size);
429 }
430
431 assert(threads_size_needed > 0);
432 pet_threads_size = threads_size_needed;
316670eb 433
39037602
A
434 pet_threads = kalloc(pet_threads_size);
435 if (pet_threads == NULL) {
436 pet_threads_size = 0;
437 return KERN_RESOURCE_SHORTAGE;
438 }
439 }
440
441 /* have memory and the task is locked and active */
442 thread_t thread;
443 pet_threads_count = 0;
444 queue_iterate(&(task->threads), thread, thread_t, task_threads) {
445 thread_reference_internal(thread);
446 pet_threads[pet_threads_count++] = thread;
447 }
448
449 /* can unlock task now that threads are referenced */
450 task_unlock(task);
451
452 return (pet_threads_count == 0) ? KERN_FAILURE : KERN_SUCCESS;
316670eb
A
453}
454
39037602
A
455static void
456pet_sample_task(task_t task, uint32_t idle_rate)
316670eb 457{
39037602
A
458 lck_mtx_assert(pet_lock, LCK_MTX_ASSERT_OWNED);
459
460 BUF_VERB(PERF_PET_SAMPLE_TASK | DBG_FUNC_START);
461
d9a64523
A
462 int pid = task_pid(task);
463 if (kperf_action_has_task(pet_action_id)) {
464 struct kperf_context ctx = {
465 .cur_task = task,
466 .cur_pid = pid,
467 };
468
469 kperf_sample(pet_sample, &ctx, pet_action_id, SAMPLE_FLAG_TASK_ONLY);
470 }
471
472 if (!kperf_action_has_thread(pet_action_id)) {
473 BUF_VERB(PERF_PET_SAMPLE_TASK | DBG_FUNC_END);
39236c6e 474 return;
39037602
A
475 }
476
d9a64523
A
477 kern_return_t kr = KERN_SUCCESS;
478
479 /*
480 * Suspend the task to see an atomic snapshot of all its threads. This
481 * is expensive, and disruptive.
482 */
483 bool needs_suspend = task != kernel_task;
484 if (needs_suspend) {
485 kr = task_suspend_internal(task);
486 if (kr != KERN_SUCCESS) {
487 BUF_VERB(PERF_PET_SAMPLE_TASK | DBG_FUNC_END, 1);
488 return;
489 }
490 needs_suspend = true;
491 }
492
493 kr = pet_threads_prepare(task);
494 if (kr != KERN_SUCCESS) {
495 BUF_INFO(PERF_PET_ERROR, ERR_THREAD, kr);
496 goto out;
497 }
39236c6e 498
39037602
A
499 for (unsigned int i = 0; i < pet_threads_count; i++) {
500 thread_t thread = pet_threads[i];
d9a64523 501 assert(thread != THREAD_NULL);
39037602 502
d9a64523
A
503 /*
504 * Do not sample the thread if it was on a CPU when the timer fired.
505 */
506 int cpu = 0;
39037602 507 for (cpu = 0; cpu < machine_info.logical_cpu_max; cpu++) {
5ba3f43e 508 if (kperf_tid_on_cpus[cpu] == thread_tid(thread)) {
39037602
A
509 break;
510 }
511 }
512
513 /* the thread was not on a CPU */
514 if (cpu == machine_info.logical_cpu_max) {
d9a64523 515 pet_sample_thread(pid, task, thread, idle_rate);
39037602
A
516 }
517
518 thread_deallocate(pet_threads[i]);
519 }
520
d9a64523
A
521out:
522 if (needs_suspend) {
523 task_resume_internal(task);
524 }
525
39037602 526 BUF_VERB(PERF_PET_SAMPLE_TASK | DBG_FUNC_END, pet_threads_count);
316670eb
A
527}
528
39037602
A
529static kern_return_t
530pet_tasks_prepare_internal(void)
531{
532 lck_mtx_assert(pet_lock, LCK_MTX_ASSERT_OWNED);
316670eb 533
39037602
A
534 vm_size_t tasks_size_needed = 0;
535
536 for (;;) {
537 lck_mtx_lock(&tasks_threads_lock);
538
539 /* do we have the memory we need? */
540 tasks_size_needed = tasks_count * sizeof(task_t);
541 if (tasks_size_needed <= pet_tasks_size) {
542 break;
543 }
544
545 /* unlock and allocate more memory */
546 lck_mtx_unlock(&tasks_threads_lock);
547
548 /* grow task array */
549 if (tasks_size_needed > pet_tasks_size) {
550 if (pet_tasks_size != 0) {
551 kfree(pet_tasks, pet_tasks_size);
552 }
553
554 assert(tasks_size_needed > 0);
555 pet_tasks_size = tasks_size_needed;
556
557 pet_tasks = (task_array_t)kalloc(pet_tasks_size);
558 if (pet_tasks == NULL) {
559 pet_tasks_size = 0;
560 return KERN_RESOURCE_SHORTAGE;
561 }
562 }
563 }
564
565 return KERN_SUCCESS;
566}
567
568static kern_return_t
569pet_tasks_prepare(void)
316670eb 570{
39037602
A
571 lck_mtx_assert(pet_lock, LCK_MTX_ASSERT_OWNED);
572
573 /* allocate space and take the tasks_threads_lock */
574 kern_return_t kr = pet_tasks_prepare_internal();
575 if (KERN_SUCCESS != kr) {
576 return kr;
577 }
578 lck_mtx_assert(&tasks_threads_lock, LCK_MTX_ASSERT_OWNED);
579
580 /* make sure the tasks are not deallocated after dropping the lock */
581 task_t task;
582 pet_tasks_count = 0;
583 queue_iterate(&tasks, task, task_t, tasks) {
584 if (task != kernel_task) {
585 task_reference_internal(task);
586 pet_tasks[pet_tasks_count++] = task;
587 }
588 }
589
590 lck_mtx_unlock(&tasks_threads_lock);
39236c6e 591
39037602 592 return KERN_SUCCESS;
316670eb
A
593}
594
39037602
A
595static void
596pet_sample_all_tasks(uint32_t idle_rate)
316670eb 597{
39037602 598 lck_mtx_assert(pet_lock, LCK_MTX_ASSERT_OWNED);
0a7de745 599 assert(pet_action_id > 0);
316670eb 600
39037602 601 BUF_INFO(PERF_PET_SAMPLE | DBG_FUNC_START);
316670eb 602
39037602
A
603 kern_return_t kr = pet_tasks_prepare();
604 if (kr != KERN_SUCCESS) {
605 BUF_INFO(PERF_PET_ERROR, ERR_TASK, kr);
606 BUF_INFO(PERF_PET_SAMPLE | DBG_FUNC_END, 0);
607 return;
608 }
316670eb 609
39037602
A
610 for (unsigned int i = 0; i < pet_tasks_count; i++) {
611 task_t task = pet_tasks[i];
612
39037602 613 pet_sample_task(task, idle_rate);
316670eb
A
614 }
615
0a7de745 616 for (unsigned int i = 0; i < pet_tasks_count; i++) {
39037602
A
617 task_deallocate(pet_tasks[i]);
618 }
619
620 BUF_INFO(PERF_PET_SAMPLE | DBG_FUNC_END, pet_tasks_count);
316670eb 621}
39236c6e 622
39037602
A
623/* support sysctls */
624
39236c6e 625int
39037602 626kperf_get_pet_idle_rate(void)
39236c6e
A
627{
628 return pet_idle_rate;
629}
630
39037602
A
631int
632kperf_set_pet_idle_rate(int val)
39236c6e
A
633{
634 pet_idle_rate = val;
39037602
A
635
636 return 0;
637}
638
639int
640kperf_get_lightweight_pet(void)
641{
642 return lightweight_pet;
643}
644
645int
646kperf_set_lightweight_pet(int val)
647{
648 if (kperf_sampling_status() == KPERF_SAMPLING_ON) {
649 return EBUSY;
650 }
651
652 lightweight_pet = (val == 1);
653 kperf_lightweight_pet_active_update();
654
655 return 0;
656}
657
658void
659kperf_lightweight_pet_active_update(void)
660{
661 kperf_lightweight_pet_active = (kperf_sampling_status() && lightweight_pet);
662 kperf_on_cpu_update();
39236c6e 663}