2 * Copyright (c) 2012-2013 Apple 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@
28 #include <mach/host_priv.h>
29 #include <mach/host_special_ports.h>
30 #include <mach/mach_types.h>
31 #include <mach/telemetry_notification_server.h>
33 #include <kern/assert.h>
34 #include <kern/clock.h>
35 #include <kern/debug.h>
36 #include <kern/host.h>
37 #include <kern/kalloc.h>
38 #include <kern/kern_types.h>
39 #include <kern/locks.h>
40 #include <kern/misc_protos.h>
41 #include <kern/sched.h>
42 #include <kern/sched_prim.h>
43 #include <kern/telemetry.h>
44 #include <kern/timer_call.h>
46 #include <pexpert/pexpert.h>
48 #include <vm/vm_kern.h>
49 #include <vm/vm_shared_region.h>
51 #include <kperf/kperf.h>
52 #include <kperf/context.h>
53 #include <kperf/callstack.h>
55 #include <sys/kdebug.h>
56 #include <uuid/uuid.h>
57 #include <kdp/kdp_dyld.h>
59 #define TELEMETRY_DEBUG 0
61 extern int proc_pid(void *);
62 extern char *proc_name_address(void *p
);
63 extern uint64_t proc_uniqueid(void *p
);
64 extern uint64_t proc_was_throttled(void *p
);
65 extern uint64_t proc_did_throttle(void *p
);
66 extern uint64_t get_dispatchqueue_serialno_offset_from_proc(void *p
);
67 extern int proc_selfpid(void);
69 struct micro_snapshot_buffer
{
72 uint32_t current_position
;
76 void telemetry_take_sample(thread_t thread
, uint8_t microsnapshot_flags
, struct micro_snapshot_buffer
* current_buffer
);
77 int telemetry_buffer_gather(user_addr_t buffer
, uint32_t *length
, boolean_t mark
, struct micro_snapshot_buffer
* current_buffer
);
79 #define TELEMETRY_DEFAULT_SAMPLE_RATE (1) /* 1 sample every 1 second */
80 #define TELEMETRY_DEFAULT_WINDOW_BUFFER_SIZE (512*1024) /* Should hopefully provide 10 seconds worth of samples */
81 #define TELEMETRY_DEFAULT_BUFFER_SIZE (16*1024)
82 #define TELEMETRY_MAX_BUFFER_SIZE (64*1024)
84 #define TELEMETRY_DEFAULT_NOTIFY_LEEWAY (4*1024) // Userland gets 4k of leeway to collect data after notification
85 #define TELEMETRY_MAX_UUID_COUNT (128) // Max of 128 non-shared-cache UUIDs to log for symbolication
87 uint32_t telemetry_sample_rate
= 0;
88 volatile boolean_t telemetry_needs_record
= FALSE
;
89 volatile boolean_t telemetry_windowed_record
= FALSE
;
90 volatile boolean_t telemetry_needs_timer_arming_record
= FALSE
;
93 * Tells the scheduler that we want it to invoke
94 * compute_telemetry_windowed(); it is still our responsibility
95 * to ensure that we do not panic if someone disables the window
96 * buffer immediately after the scheduler does so.
98 volatile boolean_t telemetry_window_enabled
= FALSE
;
101 * If TRUE, record micro-stackshot samples for all tasks.
102 * If FALSE, only sample tasks which are marked for telemetry.
104 boolean_t telemetry_sample_all_tasks
= FALSE
;
105 uint32_t telemetry_active_tasks
= 0; // Number of tasks opted into telemetry
107 uint32_t telemetry_timestamp
= 0;
110 * We have two buffers. The telemetry_buffer is responsible
111 * for timer samples and interrupt samples that are driven by
112 * compute_averages(). It will notify its client (if one
113 * exists) when it has enough data to be worth flushing.
115 * The window_buffer contains only interrupt_samples that are
116 * driven by the scheduler. Its intent is to provide a
117 * window of recent activity on the cpu(s).
119 struct micro_snapshot_buffer telemetry_buffer
= {0, 0, 0, 0};
120 struct micro_snapshot_buffer window_buffer
= {0, 0, 0, 0};
122 int telemetry_bytes_since_last_mark
= -1; // How much data since buf was last marked?
123 int telemetry_buffer_notify_at
= 0;
125 lck_grp_t telemetry_lck_grp
;
126 lck_mtx_t telemetry_mtx
;
128 #define TELEMETRY_LOCK() do { lck_mtx_lock(&telemetry_mtx); } while(0)
129 #define TELEMETRY_TRY_SPIN_LOCK() lck_mtx_try_lock_spin(&telemetry_mtx)
130 #define TELEMETRY_UNLOCK() do { lck_mtx_unlock(&telemetry_mtx); } while(0)
132 void telemetry_init(void)
135 uint32_t telemetry_notification_leeway
;
137 lck_grp_init(&telemetry_lck_grp
, "telemetry group", LCK_GRP_ATTR_NULL
);
138 lck_mtx_init(&telemetry_mtx
, &telemetry_lck_grp
, LCK_ATTR_NULL
);
140 if (!PE_parse_boot_argn("telemetry_buffer_size", &telemetry_buffer
.size
, sizeof(telemetry_buffer
.size
))) {
141 telemetry_buffer
.size
= TELEMETRY_DEFAULT_BUFFER_SIZE
;
144 if (telemetry_buffer
.size
> TELEMETRY_MAX_BUFFER_SIZE
)
145 telemetry_buffer
.size
= TELEMETRY_MAX_BUFFER_SIZE
;
147 ret
= kmem_alloc(kernel_map
, &telemetry_buffer
.buffer
, telemetry_buffer
.size
);
148 if (ret
!= KERN_SUCCESS
) {
149 kprintf("Telemetry: Allocation failed: %d\n", ret
);
152 bzero((void *) telemetry_buffer
.buffer
, telemetry_buffer
.size
);
154 if (!PE_parse_boot_argn("telemetry_notification_leeway", &telemetry_notification_leeway
, sizeof(telemetry_notification_leeway
))) {
156 * By default, notify the user to collect the buffer when there is this much space left in the buffer.
158 telemetry_notification_leeway
= TELEMETRY_DEFAULT_NOTIFY_LEEWAY
;
160 if (telemetry_notification_leeway
>= telemetry_buffer
.size
) {
161 printf("telemetry: nonsensical telemetry_notification_leeway boot-arg %d changed to %d\n",
162 telemetry_notification_leeway
, TELEMETRY_DEFAULT_NOTIFY_LEEWAY
);
163 telemetry_notification_leeway
= TELEMETRY_DEFAULT_NOTIFY_LEEWAY
;
165 telemetry_buffer_notify_at
= telemetry_buffer
.size
- telemetry_notification_leeway
;
167 if (!PE_parse_boot_argn("telemetry_sample_rate", &telemetry_sample_rate
, sizeof(telemetry_sample_rate
))) {
168 telemetry_sample_rate
= TELEMETRY_DEFAULT_SAMPLE_RATE
;
172 * To enable telemetry for all tasks, include "telemetry_sample_all_tasks=1" in boot-args.
174 if (!PE_parse_boot_argn("telemetry_sample_all_tasks", &telemetry_sample_all_tasks
, sizeof(telemetry_sample_all_tasks
))) {
176 telemetry_sample_all_tasks
= TRUE
;
180 kprintf("Telemetry: Sampling %stasks once per %u second%s\n",
181 (telemetry_sample_all_tasks
) ? "all " : "",
182 telemetry_sample_rate
, telemetry_sample_rate
== 1 ? "" : "s");
186 * Enable or disable global microstackshots (ie telemetry_sample_all_tasks).
188 * enable_disable == 1: turn it on
189 * enable_disable == 0: turn it off
192 telemetry_global_ctl(int enable_disable
)
194 if (enable_disable
== 1) {
195 telemetry_sample_all_tasks
= TRUE
;
197 telemetry_sample_all_tasks
= FALSE
;
202 * Opt the given task into or out of the telemetry stream.
204 * Supported reasons (callers may use any or all of):
208 * enable_disable == 1: turn it on
209 * enable_disable == 0: turn it off
212 telemetry_task_ctl(task_t task
, uint32_t reasons
, int enable_disable
)
215 telemetry_task_ctl_locked(task
, reasons
, enable_disable
);
220 telemetry_task_ctl_locked(task_t task
, uint32_t reasons
, int enable_disable
)
224 assert((reasons
!= 0) && ((reasons
| TF_TELEMETRY
) == TF_TELEMETRY
));
226 task_lock_assert_owned(task
);
228 origflags
= task
->t_flags
;
230 if (enable_disable
== 1) {
231 task
->t_flags
|= reasons
;
232 if ((origflags
& TF_TELEMETRY
) == 0) {
233 OSIncrementAtomic(&telemetry_active_tasks
);
235 printf("%s: telemetry OFF -> ON (%d active)\n", proc_name_address(task
->bsd_info
), telemetry_active_tasks
);
239 task
->t_flags
&= ~reasons
;
240 if (((origflags
& TF_TELEMETRY
) != 0) && ((task
->t_flags
& TF_TELEMETRY
) == 0)) {
242 * If this task went from having at least one telemetry bit to having none,
243 * the net change was to disable telemetry for the task.
245 OSDecrementAtomic(&telemetry_active_tasks
);
247 printf("%s: telemetry ON -> OFF (%d active)\n", proc_name_address(task
->bsd_info
), telemetry_active_tasks
);
254 * Enable the window_buffer, and do any associated setup.
257 telemetry_enable_window(void)
259 kern_return_t ret
= KERN_SUCCESS
;
260 vm_offset_t kern_buffer
= 0;
261 vm_size_t kern_buffer_size
= TELEMETRY_DEFAULT_WINDOW_BUFFER_SIZE
;
264 * We have no guarantee we won't allocate the buffer, take
265 * the lock, and then discover someone beat us to the punch,
266 * but we would prefer to avoid blocking while holding the
269 ret
= kmem_alloc(kernel_map
, &kern_buffer
, kern_buffer_size
);
273 if (!window_buffer
.buffer
) {
274 if (ret
== KERN_SUCCESS
) {
275 /* No existing buffer was found, so... */
276 window_buffer
.end_point
= 0;
277 window_buffer
.current_position
= 0;
279 /* Hand off the buffer, and... */
280 window_buffer
.size
= (uint32_t) kern_buffer_size
;
281 window_buffer
.buffer
= kern_buffer
;
283 kern_buffer_size
= 0;
284 bzero((void *) window_buffer
.buffer
, window_buffer
.size
);
286 /* Let the scheduler know it should drive windowed samples */
287 telemetry_window_enabled
= TRUE
;
290 /* We already have a buffer, so we have "succeeded" */
297 kmem_free(kernel_map
, kern_buffer
, kern_buffer_size
);
303 * Disable the window_buffer, and do any associated teardown.
306 telemetry_disable_window(void)
308 vm_offset_t kern_buffer
= 0;
309 vm_size_t kern_buffer_size
= 0;
313 if (window_buffer
.buffer
) {
314 /* We have a window buffer, so tear it down */
315 telemetry_window_enabled
= FALSE
;
316 kern_buffer
= window_buffer
.buffer
;
317 kern_buffer_size
= window_buffer
.size
;
318 window_buffer
.buffer
= 0;
319 window_buffer
.size
= 0;
320 window_buffer
.current_position
= 0;
321 window_buffer
.end_point
= 0;
327 kmem_free(kernel_map
, kern_buffer
, kern_buffer_size
);
331 * Determine if the current thread is eligible for telemetry:
333 * telemetry_sample_all_tasks: All threads are eligible. This takes precedence.
334 * telemetry_active_tasks: Count of tasks opted in.
335 * task->t_flags & TF_TELEMETRY: This task is opted in.
338 telemetry_is_active(thread_t thread
)
340 if (telemetry_sample_all_tasks
== TRUE
) {
344 if ((telemetry_active_tasks
> 0) && ((thread
->task
->t_flags
& TF_TELEMETRY
) != 0)) {
352 * Userland is arming a timer. If we are eligible for such a record,
353 * sample now. No need to do this one at the AST because we're already at
354 * a safe place in this system call.
356 int telemetry_timer_event(__unused
uint64_t deadline
, __unused
uint64_t interval
, __unused
uint64_t leeway
)
358 if (telemetry_needs_timer_arming_record
== TRUE
) {
359 telemetry_needs_timer_arming_record
= FALSE
;
360 telemetry_take_sample(current_thread(), kTimerArmingRecord
| kUserMode
, &telemetry_buffer
);
367 * Mark the current thread for an interrupt-based
368 * telemetry record, to be sampled at the next AST boundary.
370 void telemetry_mark_curthread(boolean_t interrupted_userspace
)
372 uint32_t ast_bits
= 0;
373 thread_t thread
= current_thread();
376 * If telemetry isn't active for this thread, return and try
379 if (telemetry_is_active(thread
) == FALSE
) {
383 ast_bits
|= (interrupted_userspace
? AST_TELEMETRY_USER
: AST_TELEMETRY_KERNEL
);
385 if (telemetry_windowed_record
) {
386 ast_bits
|= AST_TELEMETRY_WINDOWED
;
389 telemetry_windowed_record
= FALSE
;
390 telemetry_needs_record
= FALSE
;
391 thread_ast_set(thread
, ast_bits
);
392 ast_propagate(thread
->ast
);
395 void compute_telemetry(void *arg __unused
)
397 if (telemetry_sample_all_tasks
|| (telemetry_active_tasks
> 0)) {
398 if ((++telemetry_timestamp
) % telemetry_sample_rate
== 0) {
399 telemetry_needs_record
= TRUE
;
400 telemetry_needs_timer_arming_record
= TRUE
;
405 void compute_telemetry_windowed(void)
407 if (telemetry_sample_all_tasks
|| (telemetry_active_tasks
> 0)) {
409 * Due to the relationship between the two fields here,
410 * a request for a windowed record will "squash" a
411 * request for a regular interrupt record. We hedge
412 * against this by doing a quick check for an existing
413 * request. compute_telemetry doesn't hedge because
414 * a regular request cannot squash a windowed request
415 * (due to the implementation).
417 * If we really want to do this properly, we could make
418 * telemetry_needs_record a bitfield, and process one
419 * request per telemetry_mark_curthread... but that
420 * would be more expensive (atomics). This should be
421 * robust enough for now (although it biases in favor
422 * of the regular records).
424 if (!telemetry_needs_record
) {
425 telemetry_needs_record
= TRUE
;
426 telemetry_windowed_record
= TRUE
;
432 * If userland has registered a port for telemetry notifications, send one now.
435 telemetry_notify_user(void)
437 mach_port_t user_port
;
441 error
= host_get_telemetry_port(host_priv_self(), &user_port
);
442 if ((error
!= KERN_SUCCESS
) || !IPC_PORT_VALID(user_port
)) {
446 telemetry_notification(user_port
, flags
);
449 void telemetry_ast(thread_t thread
, boolean_t interrupted_userspace
, boolean_t is_windowed
)
451 uint8_t microsnapshot_flags
= kInterruptRecord
;
453 if (interrupted_userspace
)
454 microsnapshot_flags
|= kUserMode
;
457 telemetry_take_sample(thread
, microsnapshot_flags
, &window_buffer
);
459 telemetry_take_sample(thread
, microsnapshot_flags
, &telemetry_buffer
);
463 void telemetry_take_sample(thread_t thread
, uint8_t microsnapshot_flags
, struct micro_snapshot_buffer
* current_buffer
)
467 struct kperf_context ctx
;
469 uint32_t btcount
, bti
;
470 struct micro_snapshot
*msnap
;
471 struct task_snapshot
*tsnap
;
472 struct thread_snapshot
*thsnap
;
476 uint32_t current_record_start
;
478 boolean_t notify
= FALSE
;
480 if (thread
== THREAD_NULL
)
484 if ((task
== TASK_NULL
) || (task
== kernel_task
))
488 * To avoid overloading the system with telemetry requests, make
489 * sure we don't add more requests while existing ones are
490 * in-flight. Attempt this by checking if we can grab the lock.
492 * This concerns me a little; this working as intended is
493 * contingent on the workload being done in the context of the
494 * telemetry lock being the expensive part of telemetry. This
495 * includes populating the buffer and the client gathering it,
496 * but excludes the copyin overhead.
498 if (!TELEMETRY_TRY_SPIN_LOCK())
503 /* telemetry_XXX accessed outside of lock for instrumentation only */
505 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_STACKSHOT
, MICROSTACKSHOT_RECORD
) | DBG_FUNC_START
, microsnapshot_flags
, telemetry_bytes_since_last_mark
, 0, 0, (&telemetry_buffer
!= current_buffer
));
507 p
= get_bsdtask_info(task
);
509 ctx
.cur_thread
= thread
;
510 ctx
.cur_pid
= proc_pid(p
);
513 * Gather up the data we'll need for this sample. The sample is written into the kernel
514 * buffer with the global telemetry lock held -- so we must do our (possibly faulting)
515 * copies from userland here, before taking the lock.
517 kperf_ucallstack_sample(&cs
, &ctx
);
518 if (!(cs
.flags
& CALLSTACK_VALID
))
522 * Find the actual [slid] address of the shared cache's UUID, and copy it in from userland.
524 int shared_cache_uuid_valid
= 0;
525 uint64_t shared_cache_base_address
;
526 struct _dyld_cache_header shared_cache_header
;
527 uint64_t shared_cache_slide
;
530 * Don't copy in the entire shared cache header; we only need the UUID. Calculate the
531 * offset of that one field.
533 int sc_header_uuid_offset
= (char *)&shared_cache_header
.uuid
- (char *)&shared_cache_header
;
534 vm_shared_region_t sr
= vm_shared_region_get(task
);
536 if ((vm_shared_region_start_address(sr
, &shared_cache_base_address
) == KERN_SUCCESS
) &&
537 (copyin(shared_cache_base_address
+ sc_header_uuid_offset
, (char *)&shared_cache_header
.uuid
,
538 sizeof (shared_cache_header
.uuid
)) == 0)) {
539 shared_cache_uuid_valid
= 1;
540 shared_cache_slide
= vm_shared_region_get_slide(sr
);
542 // vm_shared_region_get() gave us a reference on the shared region.
543 vm_shared_region_deallocate(sr
);
547 * Retrieve the array of UUID's for binaries used by this task.
548 * We reach down into DYLD's data structures to find the array.
550 * XXX - make this common with kdp?
552 uint32_t uuid_info_count
= 0;
553 mach_vm_address_t uuid_info_addr
= 0;
554 if (task_has_64BitAddr(task
)) {
555 struct user64_dyld_all_image_infos task_image_infos
;
556 if (copyin(task
->all_image_info_addr
, (char *)&task_image_infos
, sizeof(task_image_infos
)) == 0) {
557 uuid_info_count
= (uint32_t)task_image_infos
.uuidArrayCount
;
558 uuid_info_addr
= task_image_infos
.uuidArray
;
561 struct user32_dyld_all_image_infos task_image_infos
;
562 if (copyin(task
->all_image_info_addr
, (char *)&task_image_infos
, sizeof(task_image_infos
)) == 0) {
563 uuid_info_count
= task_image_infos
.uuidArrayCount
;
564 uuid_info_addr
= task_image_infos
.uuidArray
;
569 * If we get a NULL uuid_info_addr (which can happen when we catch dyld in the middle of updating
570 * this data structure), we zero the uuid_info_count so that we won't even try to save load info
573 if (!uuid_info_addr
) {
578 * Don't copy in an unbounded amount of memory. The main binary and interesting
579 * non-shared-cache libraries should be in the first few images.
581 if (uuid_info_count
> TELEMETRY_MAX_UUID_COUNT
) {
582 uuid_info_count
= TELEMETRY_MAX_UUID_COUNT
;
585 uint32_t uuid_info_size
= (uint32_t)(task_has_64BitAddr(thread
->task
) ? sizeof(struct user64_dyld_uuid_info
) : sizeof(struct user32_dyld_uuid_info
));
586 uint32_t uuid_info_array_size
= uuid_info_count
* uuid_info_size
;
587 char *uuid_info_array
= NULL
;
589 if (uuid_info_count
> 0) {
590 if ((uuid_info_array
= (char *)kalloc(uuid_info_array_size
)) == NULL
) {
595 * Copy in the UUID info array.
596 * It may be nonresident, in which case just fix up nloadinfos to 0 in the task snapshot.
598 if (copyin(uuid_info_addr
, uuid_info_array
, uuid_info_array_size
) != 0) {
599 kfree(uuid_info_array
, uuid_info_array_size
);
600 uuid_info_array
= NULL
;
601 uuid_info_array_size
= 0;
606 * Look for a dispatch queue serial number, and copy it in from userland if present.
608 uint64_t dqserialnum
= 0;
609 int dqserialnum_valid
= 0;
611 uint64_t dqkeyaddr
= thread_dispatchqaddr(thread
);
612 if (dqkeyaddr
!= 0) {
614 uint64_t dq_serialno_offset
= get_dispatchqueue_serialno_offset_from_proc(task
->bsd_info
);
615 if ((copyin(dqkeyaddr
, (char *)&dqaddr
, (task_has_64BitAddr(task
) ? 8 : 4)) == 0) &&
616 (dqaddr
!= 0) && (dq_serialno_offset
!= 0)) {
617 uint64_t dqserialnumaddr
= dqaddr
+ dq_serialno_offset
;
618 if (copyin(dqserialnumaddr
, (char *)&dqserialnum
, (task_has_64BitAddr(task
) ? 8 : 4)) == 0) {
619 dqserialnum_valid
= 1;
624 clock_get_calendar_microtime(&secs
, &usecs
);
629 * For the benefit of the window buffer; if our buffer is not backed by anything,
630 * then we cannot take the sample. Meant to allow us to deallocate the window
631 * buffer if it is disabled.
633 if (!current_buffer
->buffer
)
637 * We do the bulk of the operation under the telemetry lock, on assumption that
638 * any page faults during execution will not cause another AST_TELEMETRY_ALL
639 * to deadlock; they will just block until we finish. This makes it easier
640 * to copy into the buffer directly. As soon as we unlock, userspace can copy
646 current_record_start
= current_buffer
->current_position
;
648 if ((current_buffer
->size
- current_buffer
->current_position
) < sizeof(struct micro_snapshot
)) {
650 * We can't fit a record in the space available, so wrap around to the beginning.
651 * Save the current position as the known end point of valid data.
653 current_buffer
->end_point
= current_record_start
;
654 current_buffer
->current_position
= 0;
655 if (current_record_start
== 0) {
656 /* This sample is too large to fit in the buffer even when we started at 0, so skip it */
662 msnap
= (struct micro_snapshot
*)(uintptr_t)(current_buffer
->buffer
+ current_buffer
->current_position
);
663 msnap
->snapshot_magic
= STACKSHOT_MICRO_SNAPSHOT_MAGIC
;
664 msnap
->ms_flags
= microsnapshot_flags
;
665 msnap
->ms_opaque_flags
= 0; /* namespace managed by userspace */
666 msnap
->ms_cpu
= 0; /* XXX - does this field make sense for a micro-stackshot? */
667 msnap
->ms_time
= secs
;
668 msnap
->ms_time_microsecs
= usecs
;
670 current_buffer
->current_position
+= sizeof(struct micro_snapshot
);
672 if ((current_buffer
->size
- current_buffer
->current_position
) < sizeof(struct task_snapshot
)) {
673 current_buffer
->end_point
= current_record_start
;
674 current_buffer
->current_position
= 0;
675 if (current_record_start
== 0) {
676 /* This sample is too large to fit in the buffer even when we started at 0, so skip it */
682 tsnap
= (struct task_snapshot
*)(uintptr_t)(current_buffer
->buffer
+ current_buffer
->current_position
);
683 bzero(tsnap
, sizeof(*tsnap
));
684 tsnap
->snapshot_magic
= STACKSHOT_TASK_SNAPSHOT_MAGIC
;
685 tsnap
->pid
= proc_pid(p
);
686 tsnap
->uniqueid
= proc_uniqueid(p
);
687 tsnap
->user_time_in_terminated_threads
= task
->total_user_time
;
688 tsnap
->system_time_in_terminated_threads
= task
->total_system_time
;
689 tsnap
->suspend_count
= task
->suspend_count
;
690 tsnap
->task_size
= pmap_resident_count(task
->map
->pmap
);
691 tsnap
->faults
= task
->faults
;
692 tsnap
->pageins
= task
->pageins
;
693 tsnap
->cow_faults
= task
->cow_faults
;
695 * The throttling counters are maintained as 64-bit counters in the proc
696 * structure. However, we reserve 32-bits (each) for them in the task_snapshot
697 * struct to save space and since we do not expect them to overflow 32-bits. If we
698 * find these values overflowing in the future, the fix would be to simply
699 * upgrade these counters to 64-bit in the task_snapshot struct
701 tsnap
->was_throttled
= (uint32_t) proc_was_throttled(p
);
702 tsnap
->did_throttle
= (uint32_t) proc_did_throttle(p
);
704 if (task
->t_flags
& TF_TELEMETRY
) {
705 tsnap
->ss_flags
|= kTaskRsrcFlagged
;
708 if (task
->effective_policy
.darwinbg
== 1) {
709 tsnap
->ss_flags
|= kTaskDarwinBG
;
712 proc_get_darwinbgstate(task
, &tmp
);
714 if (task
->requested_policy
.t_role
== TASK_FOREGROUND_APPLICATION
) {
715 tsnap
->ss_flags
|= kTaskIsForeground
;
718 if (tmp
& PROC_FLAG_ADAPTIVE_IMPORTANT
) {
719 tsnap
->ss_flags
|= kTaskIsBoosted
;
722 if (tmp
& PROC_FLAG_SUPPRESSED
) {
723 tsnap
->ss_flags
|= kTaskIsSuppressed
;
726 tsnap
->latency_qos
= task_grab_latency_qos(task
);
728 strlcpy(tsnap
->p_comm
, proc_name_address(p
), sizeof(tsnap
->p_comm
));
729 if (task_has_64BitAddr(thread
->task
)) {
730 tsnap
->ss_flags
|= kUser64_p
;
733 if (shared_cache_uuid_valid
) {
734 tsnap
->shared_cache_slide
= shared_cache_slide
;
735 bcopy(shared_cache_header
.uuid
, tsnap
->shared_cache_identifier
, sizeof (shared_cache_header
.uuid
));
738 current_buffer
->current_position
+= sizeof(struct task_snapshot
);
741 * Directly after the task snapshot, place the array of UUID's corresponding to the binaries
744 if ((current_buffer
->size
- current_buffer
->current_position
) < uuid_info_array_size
) {
745 current_buffer
->end_point
= current_record_start
;
746 current_buffer
->current_position
= 0;
747 if (current_record_start
== 0) {
748 /* This sample is too large to fit in the buffer even when we started at 0, so skip it */
755 * Copy the UUID info array into our sample.
757 if (uuid_info_array_size
> 0) {
758 bcopy(uuid_info_array
, (char *)(current_buffer
->buffer
+ current_buffer
->current_position
), uuid_info_array_size
);
759 tsnap
->nloadinfos
= uuid_info_count
;
762 current_buffer
->current_position
+= uuid_info_array_size
;
765 * After the task snapshot & list of binary UUIDs, we place a thread snapshot.
768 if ((current_buffer
->size
- current_buffer
->current_position
) < sizeof(struct thread_snapshot
)) {
769 /* wrap and overwrite */
770 current_buffer
->end_point
= current_record_start
;
771 current_buffer
->current_position
= 0;
772 if (current_record_start
== 0) {
773 /* This sample is too large to fit in the buffer even when we started at 0, so skip it */
779 thsnap
= (struct thread_snapshot
*)(uintptr_t)(current_buffer
->buffer
+ current_buffer
->current_position
);
780 bzero(thsnap
, sizeof(*thsnap
));
782 thsnap
->snapshot_magic
= STACKSHOT_THREAD_SNAPSHOT_MAGIC
;
783 thsnap
->thread_id
= thread_tid(thread
);
784 thsnap
->state
= thread
->state
;
785 thsnap
->priority
= thread
->priority
;
786 thsnap
->sched_pri
= thread
->sched_pri
;
787 thsnap
->sched_flags
= thread
->sched_flags
;
788 thsnap
->ss_flags
|= kStacksPCOnly
;
789 thsnap
->ts_qos
= thread
->effective_policy
.thep_qos
;
791 if (thread
->effective_policy
.darwinbg
) {
792 thsnap
->ss_flags
|= kThreadDarwinBG
;
795 thsnap
->user_time
= timer_grab(&thread
->user_timer
);
797 uint64_t tval
= timer_grab(&thread
->system_timer
);
799 if (thread
->precise_user_kernel_time
) {
800 thsnap
->system_time
= tval
;
802 thsnap
->user_time
+= tval
;
803 thsnap
->system_time
= 0;
806 current_buffer
->current_position
+= sizeof(struct thread_snapshot
);
809 * If this thread has a dispatch queue serial number, include it here.
811 if (dqserialnum_valid
) {
812 if ((current_buffer
->size
- current_buffer
->current_position
) < sizeof(dqserialnum
)) {
813 /* wrap and overwrite */
814 current_buffer
->end_point
= current_record_start
;
815 current_buffer
->current_position
= 0;
816 if (current_record_start
== 0) {
817 /* This sample is too large to fit in the buffer even when we started at 0, so skip it */
823 thsnap
->ss_flags
|= kHasDispatchSerial
;
824 bcopy(&dqserialnum
, (char *)current_buffer
->buffer
+ current_buffer
->current_position
, sizeof (dqserialnum
));
825 current_buffer
->current_position
+= sizeof (dqserialnum
);
828 if (task_has_64BitAddr(task
)) {
830 thsnap
->ss_flags
|= kUser64_p
;
835 btcount
= cs
.nframes
;
838 * If we can't fit this entire stacktrace then cancel this record, wrap to the beginning,
839 * and start again there so that we always store a full record.
841 if ((current_buffer
->size
- current_buffer
->current_position
)/framesize
< btcount
) {
842 current_buffer
->end_point
= current_record_start
;
843 current_buffer
->current_position
= 0;
844 if (current_record_start
== 0) {
845 /* This sample is too large to fit in the buffer even when we started at 0, so skip it */
851 for (bti
=0; bti
< btcount
; bti
++, current_buffer
->current_position
+= framesize
) {
852 if (framesize
== 8) {
853 *(uint64_t *)(uintptr_t)(current_buffer
->buffer
+ current_buffer
->current_position
) = cs
.frames
[bti
];
855 *(uint32_t *)(uintptr_t)(current_buffer
->buffer
+ current_buffer
->current_position
) = (uint32_t)cs
.frames
[bti
];
859 if (current_buffer
->end_point
< current_buffer
->current_position
) {
861 * Each time the cursor wraps around to the beginning, we leave a
862 * differing amount of unused space at the end of the buffer. Make
863 * sure the cursor pushes the end point in case we're making use of
864 * more of the buffer than we did the last time we wrapped.
866 current_buffer
->end_point
= current_buffer
->current_position
;
869 thsnap
->nuser_frames
= btcount
;
872 * Now THIS is a hack.
874 if (current_buffer
== &telemetry_buffer
) {
875 telemetry_bytes_since_last_mark
+= (current_buffer
->current_position
- current_record_start
);
876 if (telemetry_bytes_since_last_mark
> telemetry_buffer_notify_at
) {
886 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_STACKSHOT
, MICROSTACKSHOT_RECORD
) | DBG_FUNC_END
, notify
, telemetry_bytes_since_last_mark
, current_buffer
->current_position
, current_buffer
->end_point
, (&telemetry_buffer
!= current_buffer
));
889 telemetry_notify_user();
892 if (uuid_info_array
!= NULL
) {
893 kfree(uuid_info_array
, uuid_info_array_size
);
899 log_telemetry_output(vm_offset_t buf
, uint32_t pos
, uint32_t sz
)
901 struct micro_snapshot
*p
;
904 printf("Copying out %d bytes of telemetry at offset %d\n", sz
, pos
);
909 * Find and log each timestamp in this chunk of buffer.
911 for (offset
= 0; offset
< sz
; offset
++) {
912 p
= (struct micro_snapshot
*)(buf
+ offset
);
913 if (p
->snapshot_magic
== STACKSHOT_MICRO_SNAPSHOT_MAGIC
) {
914 printf("telemetry timestamp: %lld\n", p
->ms_time
);
920 int telemetry_gather(user_addr_t buffer
, uint32_t *length
, boolean_t mark
)
922 return telemetry_buffer_gather(buffer
, length
, mark
, &telemetry_buffer
);
925 int telemetry_gather_windowed(user_addr_t buffer
, uint32_t *length
)
927 return telemetry_buffer_gather(buffer
, length
, 0, &window_buffer
);
930 int telemetry_buffer_gather(user_addr_t buffer
, uint32_t *length
, boolean_t mark
, struct micro_snapshot_buffer
* current_buffer
)
933 uint32_t oldest_record_offset
;
936 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_STACKSHOT
, MICROSTACKSHOT_GATHER
) | DBG_FUNC_START
, mark
, telemetry_bytes_since_last_mark
, 0, 0, (&telemetry_buffer
!= current_buffer
));
940 if (current_buffer
->buffer
== 0) {
945 if (*length
< current_buffer
->size
) {
946 result
= KERN_NO_SPACE
;
951 * Copy the ring buffer out to userland in order sorted by time: least recent to most recent.
952 * First, we need to search forward from the cursor to find the oldest record in our buffer.
954 oldest_record_offset
= current_buffer
->current_position
;
956 if (((oldest_record_offset
+ sizeof(uint32_t)) > current_buffer
->size
) ||
957 ((oldest_record_offset
+ sizeof(uint32_t)) > current_buffer
->end_point
)) {
959 if (*(uint32_t *)(uintptr_t)(current_buffer
->buffer
) == 0) {
961 * There is no magic number at the start of the buffer, which means
962 * it's empty; nothing to see here yet.
968 * We've looked through the end of the active buffer without finding a valid
969 * record; that means all valid records are in a single chunk, beginning at
970 * the very start of the buffer.
973 oldest_record_offset
= 0;
974 assert(*(uint32_t *)(uintptr_t)(current_buffer
->buffer
) == STACKSHOT_MICRO_SNAPSHOT_MAGIC
);
978 if (*(uint32_t *)(uintptr_t)(current_buffer
->buffer
+ oldest_record_offset
) == STACKSHOT_MICRO_SNAPSHOT_MAGIC
)
982 * There are no alignment guarantees for micro-stackshot records, so we must search at each
985 oldest_record_offset
++;
986 } while (oldest_record_offset
!= current_buffer
->current_position
);
989 * If needed, copyout in two chunks: from the oldest record to the end of the buffer, and then
990 * from the beginning of the buffer up to the current position.
992 if (oldest_record_offset
!= 0) {
994 log_telemetry_output(current_buffer
->buffer
, oldest_record_offset
,
995 current_buffer
->end_point
- oldest_record_offset
);
997 if ((result
= copyout((void *)(current_buffer
->buffer
+ oldest_record_offset
), buffer
,
998 current_buffer
->end_point
- oldest_record_offset
)) != 0) {
1002 *length
= current_buffer
->end_point
- oldest_record_offset
;
1008 log_telemetry_output(current_buffer
->buffer
, 0, current_buffer
->current_position
);
1010 if ((result
= copyout((void *)current_buffer
->buffer
, buffer
+ *length
,
1011 current_buffer
->current_position
)) != 0) {
1015 *length
+= (uint32_t)current_buffer
->current_position
;
1019 if (mark
&& (*length
> 0)) {
1020 telemetry_bytes_since_last_mark
= 0;
1025 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_STACKSHOT
, MICROSTACKSHOT_GATHER
) | DBG_FUNC_END
, current_buffer
->current_position
, *length
, current_buffer
->end_point
, 0, (&telemetry_buffer
!= current_buffer
));
1030 /************************/
1031 /* BOOT PROFILE SUPPORT */
1032 /************************/
1036 * The boot-profiling support is a mechanism to sample activity happening on the
1037 * system during boot. This mechanism sets up a periodic timer and on every timer fire,
1038 * captures a full backtrace into the boot profiling buffer. This buffer can be pulled
1039 * out and analyzed from user-space. It is turned on using the following boot-args:
1040 * "bootprofile_buffer_size" specifies the size of the boot profile buffer
1041 * "bootprofile_interval_ms" specifies the interval for the profiling timer
1043 * Process Specific Boot Profiling
1045 * The boot-arg "bootprofile_proc_name" can be used to specify a certain
1046 * process that needs to profiled during boot. Setting this boot-arg changes
1047 * the way stackshots are captured. At every timer fire, the code looks at the
1048 * currently running process and takes a stackshot only if the requested process
1049 * is on-core (which makes it unsuitable for MP systems).
1053 * The boot-arg "bootprofile_type=boot" starts the timer during early boot. Using
1054 * "wake" starts the timer at AP wake from suspend-to-RAM.
1057 #define BOOTPROFILE_MAX_BUFFER_SIZE (64*1024*1024) /* see also COPYSIZELIMIT_PANIC */
1059 vm_offset_t bootprofile_buffer
= 0;
1060 uint32_t bootprofile_buffer_size
= 0;
1061 uint32_t bootprofile_buffer_current_position
= 0;
1062 uint32_t bootprofile_interval_ms
= 0;
1063 uint64_t bootprofile_interval_abs
= 0;
1064 uint64_t bootprofile_next_deadline
= 0;
1065 uint32_t bootprofile_all_procs
= 0;
1066 char bootprofile_proc_name
[17];
1068 lck_grp_t bootprofile_lck_grp
;
1069 lck_mtx_t bootprofile_mtx
;
1072 kBootProfileDisabled
= 0,
1073 kBootProfileStartTimerAtBoot
,
1074 kBootProfileStartTimerAtWake
1075 } bootprofile_type
= kBootProfileDisabled
;
1078 static timer_call_data_t bootprofile_timer_call_entry
;
1080 #define BOOTPROFILE_LOCK() do { lck_mtx_lock(&bootprofile_mtx); } while(0)
1081 #define BOOTPROFILE_TRY_SPIN_LOCK() lck_mtx_try_lock_spin(&bootprofile_mtx)
1082 #define BOOTPROFILE_UNLOCK() do { lck_mtx_unlock(&bootprofile_mtx); } while(0)
1084 static void bootprofile_timer_call(
1085 timer_call_param_t param0
,
1086 timer_call_param_t param1
);
1089 stack_snapshot_from_kernel(int pid
, void *buf
, uint32_t size
, uint32_t flags
, unsigned *retbytes
);
1091 void bootprofile_init(void)
1096 lck_grp_init(&bootprofile_lck_grp
, "bootprofile group", LCK_GRP_ATTR_NULL
);
1097 lck_mtx_init(&bootprofile_mtx
, &bootprofile_lck_grp
, LCK_ATTR_NULL
);
1099 if (!PE_parse_boot_argn("bootprofile_buffer_size", &bootprofile_buffer_size
, sizeof(bootprofile_buffer_size
))) {
1100 bootprofile_buffer_size
= 0;
1103 if (bootprofile_buffer_size
> BOOTPROFILE_MAX_BUFFER_SIZE
)
1104 bootprofile_buffer_size
= BOOTPROFILE_MAX_BUFFER_SIZE
;
1106 if (!PE_parse_boot_argn("bootprofile_interval_ms", &bootprofile_interval_ms
, sizeof(bootprofile_interval_ms
))) {
1107 bootprofile_interval_ms
= 0;
1110 if (!PE_parse_boot_argn("bootprofile_proc_name", &bootprofile_proc_name
, sizeof(bootprofile_proc_name
))) {
1111 bootprofile_all_procs
= 1;
1112 bootprofile_proc_name
[0] = '\0';
1115 if (PE_parse_boot_argn("bootprofile_type", type
, sizeof(type
))) {
1116 if (0 == strcmp(type
, "boot")) {
1117 bootprofile_type
= kBootProfileStartTimerAtBoot
;
1118 } else if (0 == strcmp(type
, "wake")) {
1119 bootprofile_type
= kBootProfileStartTimerAtWake
;
1121 bootprofile_type
= kBootProfileDisabled
;
1124 bootprofile_type
= kBootProfileDisabled
;
1127 clock_interval_to_absolutetime_interval(bootprofile_interval_ms
, NSEC_PER_MSEC
, &bootprofile_interval_abs
);
1129 /* Both boot args must be set to enable */
1130 if ((bootprofile_type
== kBootProfileDisabled
) || (bootprofile_buffer_size
== 0) || (bootprofile_interval_abs
== 0)) {
1134 ret
= kmem_alloc(kernel_map
, &bootprofile_buffer
, bootprofile_buffer_size
);
1135 if (ret
!= KERN_SUCCESS
) {
1136 kprintf("Boot profile: Allocation failed: %d\n", ret
);
1139 bzero((void *) bootprofile_buffer
, bootprofile_buffer_size
);
1141 kprintf("Boot profile: Sampling %s once per %u ms at %s\n", bootprofile_all_procs
? "all procs" : bootprofile_proc_name
, bootprofile_interval_ms
,
1142 bootprofile_type
== kBootProfileStartTimerAtBoot
? "boot" : (bootprofile_type
== kBootProfileStartTimerAtWake
? "wake" : "unknown"));
1144 timer_call_setup(&bootprofile_timer_call_entry
,
1145 bootprofile_timer_call
,
1148 if (bootprofile_type
== kBootProfileStartTimerAtBoot
) {
1149 bootprofile_next_deadline
= mach_absolute_time() + bootprofile_interval_abs
;
1150 timer_call_enter_with_leeway(&bootprofile_timer_call_entry
,
1152 bootprofile_next_deadline
,
1154 TIMER_CALL_SYS_NORMAL
,
1160 bootprofile_wake_from_sleep(void)
1162 if (bootprofile_type
== kBootProfileStartTimerAtWake
) {
1163 bootprofile_next_deadline
= mach_absolute_time() + bootprofile_interval_abs
;
1164 timer_call_enter_with_leeway(&bootprofile_timer_call_entry
,
1166 bootprofile_next_deadline
,
1168 TIMER_CALL_SYS_NORMAL
,
1174 static void bootprofile_timer_call(
1175 timer_call_param_t param0 __unused
,
1176 timer_call_param_t param1 __unused
)
1178 unsigned retbytes
= 0;
1179 int pid_to_profile
= -1;
1181 if (!BOOTPROFILE_TRY_SPIN_LOCK()) {
1185 /* Check if process-specific boot profiling is turned on */
1186 if (!bootprofile_all_procs
) {
1188 * Since boot profiling initializes really early in boot, it is
1189 * possible that at this point, the task/proc is not initialized.
1190 * Nothing to do in that case.
1193 if ((current_task() != NULL
) && (current_task()->bsd_info
!= NULL
) &&
1194 (0 == strncmp(bootprofile_proc_name
, proc_name_address(current_task()->bsd_info
), 17))) {
1195 pid_to_profile
= proc_selfpid();
1199 * Process-specific boot profiling requested but the on-core process is
1200 * something else. Nothing to do here.
1202 BOOTPROFILE_UNLOCK();
1207 /* initiate a stackshot with whatever portion of the buffer is left */
1208 if (bootprofile_buffer_current_position
< bootprofile_buffer_size
) {
1209 stack_snapshot_from_kernel(
1211 (void *)(bootprofile_buffer
+ bootprofile_buffer_current_position
),
1212 bootprofile_buffer_size
- bootprofile_buffer_current_position
,
1213 STACKSHOT_SAVE_LOADINFO
| STACKSHOT_SAVE_KEXT_LOADINFO
| STACKSHOT_GET_GLOBAL_MEM_STATS
,
1217 bootprofile_buffer_current_position
+= retbytes
;
1220 BOOTPROFILE_UNLOCK();
1222 /* If we didn't get any data or have run out of buffer space, stop profiling */
1223 if ((retbytes
== 0) || (bootprofile_buffer_current_position
== bootprofile_buffer_size
)) {
1229 /* If the user gathered the buffer, no need to keep profiling */
1230 if (bootprofile_interval_abs
== 0) {
1234 clock_deadline_for_periodic_event(bootprofile_interval_abs
,
1235 mach_absolute_time(),
1236 &bootprofile_next_deadline
);
1237 timer_call_enter_with_leeway(&bootprofile_timer_call_entry
,
1239 bootprofile_next_deadline
,
1241 TIMER_CALL_SYS_NORMAL
,
1245 int bootprofile_gather(user_addr_t buffer
, uint32_t *length
)
1251 if (bootprofile_buffer
== 0) {
1256 if (*length
< bootprofile_buffer_current_position
) {
1257 result
= KERN_NO_SPACE
;
1261 if ((result
= copyout((void *)bootprofile_buffer
, buffer
,
1262 bootprofile_buffer_current_position
)) != 0) {
1266 *length
= bootprofile_buffer_current_position
;
1268 /* cancel future timers */
1269 bootprofile_interval_abs
= 0;
1273 BOOTPROFILE_UNLOCK();