]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/telemetry.c
xnu-4570.1.46.tar.gz
[apple/xnu.git] / osfmk / kern / telemetry.c
1 /*
2 * Copyright (c) 2012-2013 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
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>
32
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>
45 #include <kern/policy_internal.h>
46 #include <kern/kcdata.h>
47
48 #include <pexpert/pexpert.h>
49
50 #include <vm/vm_kern.h>
51 #include <vm/vm_shared_region.h>
52
53 #include <kperf/kperf.h>
54 #include <kperf/context.h>
55 #include <kperf/callstack.h>
56
57 #include <sys/kdebug.h>
58 #include <uuid/uuid.h>
59 #include <kdp/kdp_dyld.h>
60
61 #define TELEMETRY_DEBUG 0
62
63 extern int proc_pid(void *);
64 extern char *proc_name_address(void *p);
65 extern uint64_t proc_uniqueid(void *p);
66 extern uint64_t proc_was_throttled(void *p);
67 extern uint64_t proc_did_throttle(void *p);
68 extern int proc_selfpid(void);
69 extern boolean_t task_did_exec(task_t task);
70 extern boolean_t task_is_exec_copy(task_t task);
71
72 struct micro_snapshot_buffer {
73 vm_offset_t buffer;
74 uint32_t size;
75 uint32_t current_position;
76 uint32_t end_point;
77 };
78
79 void telemetry_take_sample(thread_t thread, uint8_t microsnapshot_flags, struct micro_snapshot_buffer * current_buffer);
80 int telemetry_buffer_gather(user_addr_t buffer, uint32_t *length, boolean_t mark, struct micro_snapshot_buffer * current_buffer);
81
82 #define TELEMETRY_DEFAULT_SAMPLE_RATE (1) /* 1 sample every 1 second */
83 #define TELEMETRY_DEFAULT_BUFFER_SIZE (16*1024)
84 #define TELEMETRY_MAX_BUFFER_SIZE (64*1024)
85
86 #define TELEMETRY_DEFAULT_NOTIFY_LEEWAY (4*1024) // Userland gets 4k of leeway to collect data after notification
87 #define TELEMETRY_MAX_UUID_COUNT (128) // Max of 128 non-shared-cache UUIDs to log for symbolication
88
89 uint32_t telemetry_sample_rate = 0;
90 volatile boolean_t telemetry_needs_record = FALSE;
91 volatile boolean_t telemetry_needs_timer_arming_record = FALSE;
92
93 /*
94 * If TRUE, record micro-stackshot samples for all tasks.
95 * If FALSE, only sample tasks which are marked for telemetry.
96 */
97 boolean_t telemetry_sample_all_tasks = FALSE;
98 uint32_t telemetry_active_tasks = 0; // Number of tasks opted into telemetry
99
100 uint32_t telemetry_timestamp = 0;
101
102 /*
103 * The telemetry_buffer is responsible
104 * for timer samples and interrupt samples that are driven by
105 * compute_averages(). It will notify its client (if one
106 * exists) when it has enough data to be worth flushing.
107 */
108 struct micro_snapshot_buffer telemetry_buffer = {0, 0, 0, 0};
109
110 int telemetry_bytes_since_last_mark = -1; // How much data since buf was last marked?
111 int telemetry_buffer_notify_at = 0;
112
113 lck_grp_t telemetry_lck_grp;
114 lck_mtx_t telemetry_mtx;
115
116 #define TELEMETRY_LOCK() do { lck_mtx_lock(&telemetry_mtx); } while(0)
117 #define TELEMETRY_TRY_SPIN_LOCK() lck_mtx_try_lock_spin(&telemetry_mtx)
118 #define TELEMETRY_UNLOCK() do { lck_mtx_unlock(&telemetry_mtx); } while(0)
119
120 void telemetry_init(void)
121 {
122 kern_return_t ret;
123 uint32_t telemetry_notification_leeway;
124
125 lck_grp_init(&telemetry_lck_grp, "telemetry group", LCK_GRP_ATTR_NULL);
126 lck_mtx_init(&telemetry_mtx, &telemetry_lck_grp, LCK_ATTR_NULL);
127
128 if (!PE_parse_boot_argn("telemetry_buffer_size", &telemetry_buffer.size, sizeof(telemetry_buffer.size))) {
129 telemetry_buffer.size = TELEMETRY_DEFAULT_BUFFER_SIZE;
130 }
131
132 if (telemetry_buffer.size > TELEMETRY_MAX_BUFFER_SIZE)
133 telemetry_buffer.size = TELEMETRY_MAX_BUFFER_SIZE;
134
135 ret = kmem_alloc(kernel_map, &telemetry_buffer.buffer, telemetry_buffer.size, VM_KERN_MEMORY_DIAG);
136 if (ret != KERN_SUCCESS) {
137 kprintf("Telemetry: Allocation failed: %d\n", ret);
138 return;
139 }
140 bzero((void *) telemetry_buffer.buffer, telemetry_buffer.size);
141
142 if (!PE_parse_boot_argn("telemetry_notification_leeway", &telemetry_notification_leeway, sizeof(telemetry_notification_leeway))) {
143 /*
144 * By default, notify the user to collect the buffer when there is this much space left in the buffer.
145 */
146 telemetry_notification_leeway = TELEMETRY_DEFAULT_NOTIFY_LEEWAY;
147 }
148 if (telemetry_notification_leeway >= telemetry_buffer.size) {
149 printf("telemetry: nonsensical telemetry_notification_leeway boot-arg %d changed to %d\n",
150 telemetry_notification_leeway, TELEMETRY_DEFAULT_NOTIFY_LEEWAY);
151 telemetry_notification_leeway = TELEMETRY_DEFAULT_NOTIFY_LEEWAY;
152 }
153 telemetry_buffer_notify_at = telemetry_buffer.size - telemetry_notification_leeway;
154
155 if (!PE_parse_boot_argn("telemetry_sample_rate", &telemetry_sample_rate, sizeof(telemetry_sample_rate))) {
156 telemetry_sample_rate = TELEMETRY_DEFAULT_SAMPLE_RATE;
157 }
158
159 /*
160 * To enable telemetry for all tasks, include "telemetry_sample_all_tasks=1" in boot-args.
161 */
162 if (!PE_parse_boot_argn("telemetry_sample_all_tasks", &telemetry_sample_all_tasks, sizeof(telemetry_sample_all_tasks))) {
163
164 #if CONFIG_EMBEDDED && !(DEVELOPMENT || DEBUG)
165 telemetry_sample_all_tasks = FALSE;
166 #else
167 telemetry_sample_all_tasks = TRUE;
168 #endif /* CONFIG_EMBEDDED && !(DEVELOPMENT || DEBUG) */
169
170 }
171
172 kprintf("Telemetry: Sampling %stasks once per %u second%s\n",
173 (telemetry_sample_all_tasks) ? "all " : "",
174 telemetry_sample_rate, telemetry_sample_rate == 1 ? "" : "s");
175 }
176
177 /*
178 * Enable or disable global microstackshots (ie telemetry_sample_all_tasks).
179 *
180 * enable_disable == 1: turn it on
181 * enable_disable == 0: turn it off
182 */
183 void
184 telemetry_global_ctl(int enable_disable)
185 {
186 if (enable_disable == 1) {
187 telemetry_sample_all_tasks = TRUE;
188 } else {
189 telemetry_sample_all_tasks = FALSE;
190 }
191 }
192
193 /*
194 * Opt the given task into or out of the telemetry stream.
195 *
196 * Supported reasons (callers may use any or all of):
197 * TF_CPUMON_WARNING
198 * TF_WAKEMON_WARNING
199 *
200 * enable_disable == 1: turn it on
201 * enable_disable == 0: turn it off
202 */
203 void
204 telemetry_task_ctl(task_t task, uint32_t reasons, int enable_disable)
205 {
206 task_lock(task);
207 telemetry_task_ctl_locked(task, reasons, enable_disable);
208 task_unlock(task);
209 }
210
211 void
212 telemetry_task_ctl_locked(task_t task, uint32_t reasons, int enable_disable)
213 {
214 uint32_t origflags;
215
216 assert((reasons != 0) && ((reasons | TF_TELEMETRY) == TF_TELEMETRY));
217
218 task_lock_assert_owned(task);
219
220 origflags = task->t_flags;
221
222 if (enable_disable == 1) {
223 task->t_flags |= reasons;
224 if ((origflags & TF_TELEMETRY) == 0) {
225 OSIncrementAtomic(&telemetry_active_tasks);
226 #if TELEMETRY_DEBUG
227 printf("%s: telemetry OFF -> ON (%d active)\n", proc_name_address(task->bsd_info), telemetry_active_tasks);
228 #endif
229 }
230 } else {
231 task->t_flags &= ~reasons;
232 if (((origflags & TF_TELEMETRY) != 0) && ((task->t_flags & TF_TELEMETRY) == 0)) {
233 /*
234 * If this task went from having at least one telemetry bit to having none,
235 * the net change was to disable telemetry for the task.
236 */
237 OSDecrementAtomic(&telemetry_active_tasks);
238 #if TELEMETRY_DEBUG
239 printf("%s: telemetry ON -> OFF (%d active)\n", proc_name_address(task->bsd_info), telemetry_active_tasks);
240 #endif
241 }
242 }
243 }
244
245 /*
246 * Determine if the current thread is eligible for telemetry:
247 *
248 * telemetry_sample_all_tasks: All threads are eligible. This takes precedence.
249 * telemetry_active_tasks: Count of tasks opted in.
250 * task->t_flags & TF_TELEMETRY: This task is opted in.
251 */
252 static boolean_t
253 telemetry_is_active(thread_t thread)
254 {
255 task_t task = thread->task;
256
257 if (task == kernel_task) {
258 /* Kernel threads never return to an AST boundary, and are ineligible */
259 return FALSE;
260 }
261
262 if (telemetry_sample_all_tasks == TRUE) {
263 return (TRUE);
264 }
265
266 if ((telemetry_active_tasks > 0) && ((thread->task->t_flags & TF_TELEMETRY) != 0)) {
267 return (TRUE);
268 }
269
270 return (FALSE);
271 }
272
273 /*
274 * Userland is arming a timer. If we are eligible for such a record,
275 * sample now. No need to do this one at the AST because we're already at
276 * a safe place in this system call.
277 */
278 int telemetry_timer_event(__unused uint64_t deadline, __unused uint64_t interval, __unused uint64_t leeway)
279 {
280 if (telemetry_needs_timer_arming_record == TRUE) {
281 telemetry_needs_timer_arming_record = FALSE;
282 telemetry_take_sample(current_thread(), kTimerArmingRecord | kUserMode, &telemetry_buffer);
283 }
284
285 return (0);
286 }
287
288 /*
289 * Mark the current thread for an interrupt-based
290 * telemetry record, to be sampled at the next AST boundary.
291 */
292 void telemetry_mark_curthread(boolean_t interrupted_userspace)
293 {
294 uint32_t ast_bits = 0;
295 thread_t thread = current_thread();
296
297 /*
298 * If telemetry isn't active for this thread, return and try
299 * again next time.
300 */
301 if (telemetry_is_active(thread) == FALSE) {
302 return;
303 }
304
305 ast_bits |= (interrupted_userspace ? AST_TELEMETRY_USER : AST_TELEMETRY_KERNEL);
306
307 telemetry_needs_record = FALSE;
308 thread_ast_set(thread, ast_bits);
309 ast_propagate(thread);
310 }
311
312 void compute_telemetry(void *arg __unused)
313 {
314 if (telemetry_sample_all_tasks || (telemetry_active_tasks > 0)) {
315 if ((++telemetry_timestamp) % telemetry_sample_rate == 0) {
316 telemetry_needs_record = TRUE;
317 telemetry_needs_timer_arming_record = TRUE;
318 }
319 }
320 }
321
322 /*
323 * If userland has registered a port for telemetry notifications, send one now.
324 */
325 static void
326 telemetry_notify_user(void)
327 {
328 mach_port_t user_port;
329 uint32_t flags = 0;
330 int error;
331
332 error = host_get_telemetry_port(host_priv_self(), &user_port);
333 if ((error != KERN_SUCCESS) || !IPC_PORT_VALID(user_port)) {
334 return;
335 }
336
337 telemetry_notification(user_port, flags);
338 ipc_port_release_send(user_port);
339 }
340
341 void telemetry_ast(thread_t thread, ast_t reasons)
342 {
343 assert((reasons & AST_TELEMETRY_ALL) != AST_TELEMETRY_ALL); /* only one is valid at a time */
344
345 boolean_t io_telemetry = (reasons & AST_TELEMETRY_IO) ? TRUE : FALSE;
346 boolean_t interrupted_userspace = (reasons & AST_TELEMETRY_USER) ? TRUE : FALSE;
347
348 uint8_t microsnapshot_flags = kInterruptRecord;
349
350 if (io_telemetry == TRUE)
351 microsnapshot_flags = kIORecord;
352
353 if (interrupted_userspace)
354 microsnapshot_flags |= kUserMode;
355
356 telemetry_take_sample(thread, microsnapshot_flags, &telemetry_buffer);
357 }
358
359 void telemetry_take_sample(thread_t thread, uint8_t microsnapshot_flags, struct micro_snapshot_buffer * current_buffer)
360 {
361 task_t task;
362 void *p;
363 struct kperf_context ctx;
364 struct callstack cs;
365 uint32_t btcount, bti;
366 struct micro_snapshot *msnap;
367 struct task_snapshot *tsnap;
368 struct thread_snapshot *thsnap;
369 clock_sec_t secs;
370 clock_usec_t usecs;
371 vm_size_t framesize;
372 uint32_t current_record_start;
373 uint32_t tmp = 0;
374 boolean_t notify = FALSE;
375
376 if (thread == THREAD_NULL)
377 return;
378
379 task = thread->task;
380 if ((task == TASK_NULL) || (task == kernel_task) || task_did_exec(task) || task_is_exec_copy(task))
381 return;
382
383 /*
384 * To avoid overloading the system with telemetry requests, make
385 * sure we don't add more requests while existing ones are
386 * in-flight. Attempt this by checking if we can grab the lock.
387 *
388 * This concerns me a little; this working as intended is
389 * contingent on the workload being done in the context of the
390 * telemetry lock being the expensive part of telemetry. This
391 * includes populating the buffer and the client gathering it,
392 * but excludes the copyin overhead.
393 */
394 if (!TELEMETRY_TRY_SPIN_LOCK())
395 return;
396
397 TELEMETRY_UNLOCK();
398
399 /* telemetry_XXX accessed outside of lock for instrumentation only */
400 /* TODO */
401 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));
402
403 p = get_bsdtask_info(task);
404
405 ctx.cur_thread = thread;
406 ctx.cur_pid = proc_pid(p);
407
408 /*
409 * Gather up the data we'll need for this sample. The sample is written into the kernel
410 * buffer with the global telemetry lock held -- so we must do our (possibly faulting)
411 * copies from userland here, before taking the lock.
412 */
413 cs.nframes = MAX_CALLSTACK_FRAMES;
414 kperf_ucallstack_sample(&cs, &ctx);
415 if (!(cs.flags & CALLSTACK_VALID))
416 return;
417
418 /*
419 * Find the actual [slid] address of the shared cache's UUID, and copy it in from userland.
420 */
421 int shared_cache_uuid_valid = 0;
422 uint64_t shared_cache_base_address;
423 struct _dyld_cache_header shared_cache_header;
424 uint64_t shared_cache_slide;
425
426 /*
427 * Don't copy in the entire shared cache header; we only need the UUID. Calculate the
428 * offset of that one field.
429 */
430 int sc_header_uuid_offset = (char *)&shared_cache_header.uuid - (char *)&shared_cache_header;
431 vm_shared_region_t sr = vm_shared_region_get(task);
432 if (sr != NULL) {
433 if ((vm_shared_region_start_address(sr, &shared_cache_base_address) == KERN_SUCCESS) &&
434 (copyin(shared_cache_base_address + sc_header_uuid_offset, (char *)&shared_cache_header.uuid,
435 sizeof (shared_cache_header.uuid)) == 0)) {
436 shared_cache_uuid_valid = 1;
437 shared_cache_slide = vm_shared_region_get_slide(sr);
438 }
439 // vm_shared_region_get() gave us a reference on the shared region.
440 vm_shared_region_deallocate(sr);
441 }
442
443 /*
444 * Retrieve the array of UUID's for binaries used by this task.
445 * We reach down into DYLD's data structures to find the array.
446 *
447 * XXX - make this common with kdp?
448 */
449 uint32_t uuid_info_count = 0;
450 mach_vm_address_t uuid_info_addr = 0;
451 if (task_has_64BitAddr(task)) {
452 struct user64_dyld_all_image_infos task_image_infos;
453 if (copyin(task->all_image_info_addr, (char *)&task_image_infos, sizeof(task_image_infos)) == 0) {
454 uuid_info_count = (uint32_t)task_image_infos.uuidArrayCount;
455 uuid_info_addr = task_image_infos.uuidArray;
456 }
457 } else {
458 struct user32_dyld_all_image_infos task_image_infos;
459 if (copyin(task->all_image_info_addr, (char *)&task_image_infos, sizeof(task_image_infos)) == 0) {
460 uuid_info_count = task_image_infos.uuidArrayCount;
461 uuid_info_addr = task_image_infos.uuidArray;
462 }
463 }
464
465 /*
466 * If we get a NULL uuid_info_addr (which can happen when we catch dyld in the middle of updating
467 * this data structure), we zero the uuid_info_count so that we won't even try to save load info
468 * for this task.
469 */
470 if (!uuid_info_addr) {
471 uuid_info_count = 0;
472 }
473
474 /*
475 * Don't copy in an unbounded amount of memory. The main binary and interesting
476 * non-shared-cache libraries should be in the first few images.
477 */
478 if (uuid_info_count > TELEMETRY_MAX_UUID_COUNT) {
479 uuid_info_count = TELEMETRY_MAX_UUID_COUNT;
480 }
481
482 uint32_t uuid_info_size = (uint32_t)(task_has_64BitAddr(thread->task) ? sizeof(struct user64_dyld_uuid_info) : sizeof(struct user32_dyld_uuid_info));
483 uint32_t uuid_info_array_size = uuid_info_count * uuid_info_size;
484 char *uuid_info_array = NULL;
485
486 if (uuid_info_count > 0) {
487 if ((uuid_info_array = (char *)kalloc(uuid_info_array_size)) == NULL) {
488 return;
489 }
490
491 /*
492 * Copy in the UUID info array.
493 * It may be nonresident, in which case just fix up nloadinfos to 0 in the task snapshot.
494 */
495 if (copyin(uuid_info_addr, uuid_info_array, uuid_info_array_size) != 0) {
496 kfree(uuid_info_array, uuid_info_array_size);
497 uuid_info_array = NULL;
498 uuid_info_array_size = 0;
499 }
500 }
501
502 /*
503 * Look for a dispatch queue serial number, and copy it in from userland if present.
504 */
505 uint64_t dqserialnum = 0;
506 int dqserialnum_valid = 0;
507
508 uint64_t dqkeyaddr = thread_dispatchqaddr(thread);
509 if (dqkeyaddr != 0) {
510 uint64_t dqaddr = 0;
511 uint64_t dq_serialno_offset = get_task_dispatchqueue_serialno_offset(task);
512 if ((copyin(dqkeyaddr, (char *)&dqaddr, (task_has_64BitAddr(task) ? 8 : 4)) == 0) &&
513 (dqaddr != 0) && (dq_serialno_offset != 0)) {
514 uint64_t dqserialnumaddr = dqaddr + dq_serialno_offset;
515 if (copyin(dqserialnumaddr, (char *)&dqserialnum, (task_has_64BitAddr(task) ? 8 : 4)) == 0) {
516 dqserialnum_valid = 1;
517 }
518 }
519 }
520
521 clock_get_calendar_microtime(&secs, &usecs);
522
523 TELEMETRY_LOCK();
524
525 /*
526 * If our buffer is not backed by anything,
527 * then we cannot take the sample. Meant to allow us to deallocate the window
528 * buffer if it is disabled.
529 */
530 if (!current_buffer->buffer)
531 goto cancel_sample;
532
533 /*
534 * We do the bulk of the operation under the telemetry lock, on assumption that
535 * any page faults during execution will not cause another AST_TELEMETRY_ALL
536 * to deadlock; they will just block until we finish. This makes it easier
537 * to copy into the buffer directly. As soon as we unlock, userspace can copy
538 * out of our buffer.
539 */
540
541 copytobuffer:
542
543 current_record_start = current_buffer->current_position;
544
545 if ((current_buffer->size - current_buffer->current_position) < sizeof(struct micro_snapshot)) {
546 /*
547 * We can't fit a record in the space available, so wrap around to the beginning.
548 * Save the current position as the known end point of valid data.
549 */
550 current_buffer->end_point = current_record_start;
551 current_buffer->current_position = 0;
552 if (current_record_start == 0) {
553 /* This sample is too large to fit in the buffer even when we started at 0, so skip it */
554 goto cancel_sample;
555 }
556 goto copytobuffer;
557 }
558
559 msnap = (struct micro_snapshot *)(uintptr_t)(current_buffer->buffer + current_buffer->current_position);
560 msnap->snapshot_magic = STACKSHOT_MICRO_SNAPSHOT_MAGIC;
561 msnap->ms_flags = microsnapshot_flags;
562 msnap->ms_opaque_flags = 0; /* namespace managed by userspace */
563 msnap->ms_cpu = 0; /* XXX - does this field make sense for a micro-stackshot? */
564 msnap->ms_time = secs;
565 msnap->ms_time_microsecs = usecs;
566
567 current_buffer->current_position += sizeof(struct micro_snapshot);
568
569 if ((current_buffer->size - current_buffer->current_position) < sizeof(struct task_snapshot)) {
570 current_buffer->end_point = current_record_start;
571 current_buffer->current_position = 0;
572 if (current_record_start == 0) {
573 /* This sample is too large to fit in the buffer even when we started at 0, so skip it */
574 goto cancel_sample;
575 }
576 goto copytobuffer;
577 }
578
579 tsnap = (struct task_snapshot *)(uintptr_t)(current_buffer->buffer + current_buffer->current_position);
580 bzero(tsnap, sizeof(*tsnap));
581 tsnap->snapshot_magic = STACKSHOT_TASK_SNAPSHOT_MAGIC;
582 tsnap->pid = proc_pid(p);
583 tsnap->uniqueid = proc_uniqueid(p);
584 tsnap->user_time_in_terminated_threads = task->total_user_time;
585 tsnap->system_time_in_terminated_threads = task->total_system_time;
586 tsnap->suspend_count = task->suspend_count;
587 tsnap->task_size = pmap_resident_count(task->map->pmap);
588 tsnap->faults = task->faults;
589 tsnap->pageins = task->pageins;
590 tsnap->cow_faults = task->cow_faults;
591 /*
592 * The throttling counters are maintained as 64-bit counters in the proc
593 * structure. However, we reserve 32-bits (each) for them in the task_snapshot
594 * struct to save space and since we do not expect them to overflow 32-bits. If we
595 * find these values overflowing in the future, the fix would be to simply
596 * upgrade these counters to 64-bit in the task_snapshot struct
597 */
598 tsnap->was_throttled = (uint32_t) proc_was_throttled(p);
599 tsnap->did_throttle = (uint32_t) proc_did_throttle(p);
600
601 if (task->t_flags & TF_TELEMETRY) {
602 tsnap->ss_flags |= kTaskRsrcFlagged;
603 }
604
605 if (proc_get_effective_task_policy(task, TASK_POLICY_DARWIN_BG)) {
606 tsnap->ss_flags |= kTaskDarwinBG;
607 }
608
609 proc_get_darwinbgstate(task, &tmp);
610
611 if (proc_get_effective_task_policy(task, TASK_POLICY_ROLE) == TASK_FOREGROUND_APPLICATION) {
612 tsnap->ss_flags |= kTaskIsForeground;
613 }
614
615 if (tmp & PROC_FLAG_ADAPTIVE_IMPORTANT) {
616 tsnap->ss_flags |= kTaskIsBoosted;
617 }
618
619 if (tmp & PROC_FLAG_SUPPRESSED) {
620 tsnap->ss_flags |= kTaskIsSuppressed;
621 }
622
623 tsnap->latency_qos = task_grab_latency_qos(task);
624
625 strlcpy(tsnap->p_comm, proc_name_address(p), sizeof(tsnap->p_comm));
626 if (task_has_64BitAddr(thread->task)) {
627 tsnap->ss_flags |= kUser64_p;
628 }
629
630 if (shared_cache_uuid_valid) {
631 tsnap->shared_cache_slide = shared_cache_slide;
632 bcopy(shared_cache_header.uuid, tsnap->shared_cache_identifier, sizeof (shared_cache_header.uuid));
633 }
634
635 current_buffer->current_position += sizeof(struct task_snapshot);
636
637 /*
638 * Directly after the task snapshot, place the array of UUID's corresponding to the binaries
639 * used by this task.
640 */
641 if ((current_buffer->size - current_buffer->current_position) < uuid_info_array_size) {
642 current_buffer->end_point = current_record_start;
643 current_buffer->current_position = 0;
644 if (current_record_start == 0) {
645 /* This sample is too large to fit in the buffer even when we started at 0, so skip it */
646 goto cancel_sample;
647 }
648 goto copytobuffer;
649 }
650
651 /*
652 * Copy the UUID info array into our sample.
653 */
654 if (uuid_info_array_size > 0) {
655 bcopy(uuid_info_array, (char *)(current_buffer->buffer + current_buffer->current_position), uuid_info_array_size);
656 tsnap->nloadinfos = uuid_info_count;
657 }
658
659 current_buffer->current_position += uuid_info_array_size;
660
661 /*
662 * After the task snapshot & list of binary UUIDs, we place a thread snapshot.
663 */
664
665 if ((current_buffer->size - current_buffer->current_position) < sizeof(struct thread_snapshot)) {
666 /* wrap and overwrite */
667 current_buffer->end_point = current_record_start;
668 current_buffer->current_position = 0;
669 if (current_record_start == 0) {
670 /* This sample is too large to fit in the buffer even when we started at 0, so skip it */
671 goto cancel_sample;
672 }
673 goto copytobuffer;
674 }
675
676 thsnap = (struct thread_snapshot *)(uintptr_t)(current_buffer->buffer + current_buffer->current_position);
677 bzero(thsnap, sizeof(*thsnap));
678
679 thsnap->snapshot_magic = STACKSHOT_THREAD_SNAPSHOT_MAGIC;
680 thsnap->thread_id = thread_tid(thread);
681 thsnap->state = thread->state;
682 thsnap->priority = thread->base_pri;
683 thsnap->sched_pri = thread->sched_pri;
684 thsnap->sched_flags = thread->sched_flags;
685 thsnap->ss_flags |= kStacksPCOnly;
686 thsnap->ts_qos = thread->effective_policy.thep_qos;
687 thsnap->ts_rqos = thread->requested_policy.thrp_qos;
688 thsnap->ts_rqos_override = thread->requested_policy.thrp_qos_override;
689
690 if (proc_get_effective_thread_policy(thread, TASK_POLICY_DARWIN_BG)) {
691 thsnap->ss_flags |= kThreadDarwinBG;
692 }
693
694 thsnap->user_time = timer_grab(&thread->user_timer);
695
696 uint64_t tval = timer_grab(&thread->system_timer);
697
698 if (thread->precise_user_kernel_time) {
699 thsnap->system_time = tval;
700 } else {
701 thsnap->user_time += tval;
702 thsnap->system_time = 0;
703 }
704
705 current_buffer->current_position += sizeof(struct thread_snapshot);
706
707 /*
708 * If this thread has a dispatch queue serial number, include it here.
709 */
710 if (dqserialnum_valid) {
711 if ((current_buffer->size - current_buffer->current_position) < sizeof(dqserialnum)) {
712 /* wrap and overwrite */
713 current_buffer->end_point = current_record_start;
714 current_buffer->current_position = 0;
715 if (current_record_start == 0) {
716 /* This sample is too large to fit in the buffer even when we started at 0, so skip it */
717 goto cancel_sample;
718 }
719 goto copytobuffer;
720 }
721
722 thsnap->ss_flags |= kHasDispatchSerial;
723 bcopy(&dqserialnum, (char *)current_buffer->buffer + current_buffer->current_position, sizeof (dqserialnum));
724 current_buffer->current_position += sizeof (dqserialnum);
725 }
726
727 if (task_has_64BitAddr(task)) {
728 framesize = 8;
729 thsnap->ss_flags |= kUser64_p;
730 } else {
731 framesize = 4;
732 }
733
734 btcount = cs.nframes;
735
736 /*
737 * If we can't fit this entire stacktrace then cancel this record, wrap to the beginning,
738 * and start again there so that we always store a full record.
739 */
740 if ((current_buffer->size - current_buffer->current_position)/framesize < btcount) {
741 current_buffer->end_point = current_record_start;
742 current_buffer->current_position = 0;
743 if (current_record_start == 0) {
744 /* This sample is too large to fit in the buffer even when we started at 0, so skip it */
745 goto cancel_sample;
746 }
747 goto copytobuffer;
748 }
749
750 for (bti=0; bti < btcount; bti++, current_buffer->current_position += framesize) {
751 if (framesize == 8) {
752 *(uint64_t *)(uintptr_t)(current_buffer->buffer + current_buffer->current_position) = cs.frames[bti];
753 } else {
754 *(uint32_t *)(uintptr_t)(current_buffer->buffer + current_buffer->current_position) = (uint32_t)cs.frames[bti];
755 }
756 }
757
758 if (current_buffer->end_point < current_buffer->current_position) {
759 /*
760 * Each time the cursor wraps around to the beginning, we leave a
761 * differing amount of unused space at the end of the buffer. Make
762 * sure the cursor pushes the end point in case we're making use of
763 * more of the buffer than we did the last time we wrapped.
764 */
765 current_buffer->end_point = current_buffer->current_position;
766 }
767
768 thsnap->nuser_frames = btcount;
769
770 /*
771 * Now THIS is a hack.
772 */
773 if (current_buffer == &telemetry_buffer) {
774 telemetry_bytes_since_last_mark += (current_buffer->current_position - current_record_start);
775 if (telemetry_bytes_since_last_mark > telemetry_buffer_notify_at) {
776 notify = TRUE;
777 }
778 }
779
780 cancel_sample:
781
782 TELEMETRY_UNLOCK();
783
784 /* TODO */
785 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));
786
787 if (notify) {
788 telemetry_notify_user();
789 }
790
791 if (uuid_info_array != NULL) {
792 kfree(uuid_info_array, uuid_info_array_size);
793 }
794 }
795
796 #if TELEMETRY_DEBUG
797 static void
798 log_telemetry_output(vm_offset_t buf, uint32_t pos, uint32_t sz)
799 {
800 struct micro_snapshot *p;
801 uint32_t offset;
802
803 printf("Copying out %d bytes of telemetry at offset %d\n", sz, pos);
804
805 buf += pos;
806
807 /*
808 * Find and log each timestamp in this chunk of buffer.
809 */
810 for (offset = 0; offset < sz; offset++) {
811 p = (struct micro_snapshot *)(buf + offset);
812 if (p->snapshot_magic == STACKSHOT_MICRO_SNAPSHOT_MAGIC) {
813 printf("telemetry timestamp: %lld\n", p->ms_time);
814 }
815 }
816 }
817 #endif
818
819 int telemetry_gather(user_addr_t buffer, uint32_t *length, boolean_t mark)
820 {
821 return telemetry_buffer_gather(buffer, length, mark, &telemetry_buffer);
822 }
823
824 int telemetry_buffer_gather(user_addr_t buffer, uint32_t *length, boolean_t mark, struct micro_snapshot_buffer * current_buffer)
825 {
826 int result = 0;
827 uint32_t oldest_record_offset;
828
829 /* TODO */
830 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));
831
832 TELEMETRY_LOCK();
833
834 if (current_buffer->buffer == 0) {
835 *length = 0;
836 goto out;
837 }
838
839 if (*length < current_buffer->size) {
840 result = KERN_NO_SPACE;
841 goto out;
842 }
843
844 /*
845 * Copy the ring buffer out to userland in order sorted by time: least recent to most recent.
846 * First, we need to search forward from the cursor to find the oldest record in our buffer.
847 */
848 oldest_record_offset = current_buffer->current_position;
849 do {
850 if (((oldest_record_offset + sizeof(uint32_t)) > current_buffer->size) ||
851 ((oldest_record_offset + sizeof(uint32_t)) > current_buffer->end_point)) {
852
853 if (*(uint32_t *)(uintptr_t)(current_buffer->buffer) == 0) {
854 /*
855 * There is no magic number at the start of the buffer, which means
856 * it's empty; nothing to see here yet.
857 */
858 *length = 0;
859 goto out;
860 }
861 /*
862 * We've looked through the end of the active buffer without finding a valid
863 * record; that means all valid records are in a single chunk, beginning at
864 * the very start of the buffer.
865 */
866
867 oldest_record_offset = 0;
868 assert(*(uint32_t *)(uintptr_t)(current_buffer->buffer) == STACKSHOT_MICRO_SNAPSHOT_MAGIC);
869 break;
870 }
871
872 if (*(uint32_t *)(uintptr_t)(current_buffer->buffer + oldest_record_offset) == STACKSHOT_MICRO_SNAPSHOT_MAGIC)
873 break;
874
875 /*
876 * There are no alignment guarantees for micro-stackshot records, so we must search at each
877 * byte offset.
878 */
879 oldest_record_offset++;
880 } while (oldest_record_offset != current_buffer->current_position);
881
882 /*
883 * If needed, copyout in two chunks: from the oldest record to the end of the buffer, and then
884 * from the beginning of the buffer up to the current position.
885 */
886 if (oldest_record_offset != 0) {
887 #if TELEMETRY_DEBUG
888 log_telemetry_output(current_buffer->buffer, oldest_record_offset,
889 current_buffer->end_point - oldest_record_offset);
890 #endif
891 if ((result = copyout((void *)(current_buffer->buffer + oldest_record_offset), buffer,
892 current_buffer->end_point - oldest_record_offset)) != 0) {
893 *length = 0;
894 goto out;
895 }
896 *length = current_buffer->end_point - oldest_record_offset;
897 } else {
898 *length = 0;
899 }
900
901 #if TELEMETRY_DEBUG
902 log_telemetry_output(current_buffer->buffer, 0, current_buffer->current_position);
903 #endif
904 if ((result = copyout((void *)current_buffer->buffer, buffer + *length,
905 current_buffer->current_position)) != 0) {
906 *length = 0;
907 goto out;
908 }
909 *length += (uint32_t)current_buffer->current_position;
910
911 out:
912
913 if (mark && (*length > 0)) {
914 telemetry_bytes_since_last_mark = 0;
915 }
916
917 TELEMETRY_UNLOCK();
918
919 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));
920
921 return (result);
922 }
923
924 /************************/
925 /* BOOT PROFILE SUPPORT */
926 /************************/
927 /*
928 * Boot Profiling
929 *
930 * The boot-profiling support is a mechanism to sample activity happening on the
931 * system during boot. This mechanism sets up a periodic timer and on every timer fire,
932 * captures a full backtrace into the boot profiling buffer. This buffer can be pulled
933 * out and analyzed from user-space. It is turned on using the following boot-args:
934 * "bootprofile_buffer_size" specifies the size of the boot profile buffer
935 * "bootprofile_interval_ms" specifies the interval for the profiling timer
936 *
937 * Process Specific Boot Profiling
938 *
939 * The boot-arg "bootprofile_proc_name" can be used to specify a certain
940 * process that needs to profiled during boot. Setting this boot-arg changes
941 * the way stackshots are captured. At every timer fire, the code looks at the
942 * currently running process and takes a stackshot only if the requested process
943 * is on-core (which makes it unsuitable for MP systems).
944 *
945 * Trigger Events
946 *
947 * The boot-arg "bootprofile_type=boot" starts the timer during early boot. Using
948 * "wake" starts the timer at AP wake from suspend-to-RAM.
949 */
950
951 #define BOOTPROFILE_MAX_BUFFER_SIZE (64*1024*1024) /* see also COPYSIZELIMIT_PANIC */
952
953 vm_offset_t bootprofile_buffer = 0;
954 uint32_t bootprofile_buffer_size = 0;
955 uint32_t bootprofile_buffer_current_position = 0;
956 uint32_t bootprofile_interval_ms = 0;
957 uint32_t bootprofile_stackshot_flags = 0;
958 uint64_t bootprofile_interval_abs = 0;
959 uint64_t bootprofile_next_deadline = 0;
960 uint32_t bootprofile_all_procs = 0;
961 char bootprofile_proc_name[17];
962 uint64_t bootprofile_delta_since_timestamp = 0;
963 lck_grp_t bootprofile_lck_grp;
964 lck_mtx_t bootprofile_mtx;
965
966
967 enum {
968 kBootProfileDisabled = 0,
969 kBootProfileStartTimerAtBoot,
970 kBootProfileStartTimerAtWake
971 } bootprofile_type = kBootProfileDisabled;
972
973
974 static timer_call_data_t bootprofile_timer_call_entry;
975
976 #define BOOTPROFILE_LOCK() do { lck_mtx_lock(&bootprofile_mtx); } while(0)
977 #define BOOTPROFILE_TRY_SPIN_LOCK() lck_mtx_try_lock_spin(&bootprofile_mtx)
978 #define BOOTPROFILE_UNLOCK() do { lck_mtx_unlock(&bootprofile_mtx); } while(0)
979
980 static void bootprofile_timer_call(
981 timer_call_param_t param0,
982 timer_call_param_t param1);
983
984 void bootprofile_init(void)
985 {
986 kern_return_t ret;
987 char type[32];
988
989 lck_grp_init(&bootprofile_lck_grp, "bootprofile group", LCK_GRP_ATTR_NULL);
990 lck_mtx_init(&bootprofile_mtx, &bootprofile_lck_grp, LCK_ATTR_NULL);
991
992 if (!PE_parse_boot_argn("bootprofile_buffer_size", &bootprofile_buffer_size, sizeof(bootprofile_buffer_size))) {
993 bootprofile_buffer_size = 0;
994 }
995
996 if (bootprofile_buffer_size > BOOTPROFILE_MAX_BUFFER_SIZE)
997 bootprofile_buffer_size = BOOTPROFILE_MAX_BUFFER_SIZE;
998
999 if (!PE_parse_boot_argn("bootprofile_interval_ms", &bootprofile_interval_ms, sizeof(bootprofile_interval_ms))) {
1000 bootprofile_interval_ms = 0;
1001 }
1002
1003 if (!PE_parse_boot_argn("bootprofile_stackshot_flags", &bootprofile_stackshot_flags, sizeof(bootprofile_stackshot_flags))) {
1004 bootprofile_stackshot_flags = 0;
1005 }
1006
1007 if (!PE_parse_boot_argn("bootprofile_proc_name", &bootprofile_proc_name, sizeof(bootprofile_proc_name))) {
1008 bootprofile_all_procs = 1;
1009 bootprofile_proc_name[0] = '\0';
1010 }
1011
1012 if (PE_parse_boot_argn("bootprofile_type", type, sizeof(type))) {
1013 if (0 == strcmp(type, "boot")) {
1014 bootprofile_type = kBootProfileStartTimerAtBoot;
1015 } else if (0 == strcmp(type, "wake")) {
1016 bootprofile_type = kBootProfileStartTimerAtWake;
1017 } else {
1018 bootprofile_type = kBootProfileDisabled;
1019 }
1020 } else {
1021 bootprofile_type = kBootProfileDisabled;
1022 }
1023
1024 clock_interval_to_absolutetime_interval(bootprofile_interval_ms, NSEC_PER_MSEC, &bootprofile_interval_abs);
1025
1026 /* Both boot args must be set to enable */
1027 if ((bootprofile_type == kBootProfileDisabled) || (bootprofile_buffer_size == 0) || (bootprofile_interval_abs == 0)) {
1028 return;
1029 }
1030
1031 ret = kmem_alloc(kernel_map, &bootprofile_buffer, bootprofile_buffer_size, VM_KERN_MEMORY_DIAG);
1032 if (ret != KERN_SUCCESS) {
1033 kprintf("Boot profile: Allocation failed: %d\n", ret);
1034 return;
1035 }
1036 bzero((void *) bootprofile_buffer, bootprofile_buffer_size);
1037
1038 kprintf("Boot profile: Sampling %s once per %u ms at %s\n", bootprofile_all_procs ? "all procs" : bootprofile_proc_name, bootprofile_interval_ms,
1039 bootprofile_type == kBootProfileStartTimerAtBoot ? "boot" : (bootprofile_type == kBootProfileStartTimerAtWake ? "wake" : "unknown"));
1040
1041 timer_call_setup(&bootprofile_timer_call_entry,
1042 bootprofile_timer_call,
1043 NULL);
1044
1045 if (bootprofile_type == kBootProfileStartTimerAtBoot) {
1046 bootprofile_next_deadline = mach_absolute_time() + bootprofile_interval_abs;
1047 timer_call_enter_with_leeway(&bootprofile_timer_call_entry,
1048 NULL,
1049 bootprofile_next_deadline,
1050 0,
1051 TIMER_CALL_SYS_NORMAL,
1052 FALSE);
1053 }
1054 }
1055
1056 void
1057 bootprofile_wake_from_sleep(void)
1058 {
1059 if (bootprofile_type == kBootProfileStartTimerAtWake) {
1060 bootprofile_next_deadline = mach_absolute_time() + bootprofile_interval_abs;
1061 timer_call_enter_with_leeway(&bootprofile_timer_call_entry,
1062 NULL,
1063 bootprofile_next_deadline,
1064 0,
1065 TIMER_CALL_SYS_NORMAL,
1066 FALSE);
1067 }
1068 }
1069
1070
1071 static void
1072 bootprofile_timer_call(
1073 timer_call_param_t param0 __unused,
1074 timer_call_param_t param1 __unused)
1075 {
1076 unsigned retbytes = 0;
1077 int pid_to_profile = -1;
1078
1079 if (!BOOTPROFILE_TRY_SPIN_LOCK()) {
1080 goto reprogram;
1081 }
1082
1083 /* Check if process-specific boot profiling is turned on */
1084 if (!bootprofile_all_procs) {
1085 /*
1086 * Since boot profiling initializes really early in boot, it is
1087 * possible that at this point, the task/proc is not initialized.
1088 * Nothing to do in that case.
1089 */
1090
1091 if ((current_task() != NULL) && (current_task()->bsd_info != NULL) &&
1092 (0 == strncmp(bootprofile_proc_name, proc_name_address(current_task()->bsd_info), 17))) {
1093 pid_to_profile = proc_selfpid();
1094 }
1095 else {
1096 /*
1097 * Process-specific boot profiling requested but the on-core process is
1098 * something else. Nothing to do here.
1099 */
1100 BOOTPROFILE_UNLOCK();
1101 goto reprogram;
1102 }
1103 }
1104
1105 /* initiate a stackshot with whatever portion of the buffer is left */
1106 if (bootprofile_buffer_current_position < bootprofile_buffer_size) {
1107
1108 uint32_t flags = STACKSHOT_KCDATA_FORMAT | STACKSHOT_TRYLOCK | STACKSHOT_SAVE_LOADINFO
1109 | STACKSHOT_GET_GLOBAL_MEM_STATS;
1110 #if __x86_64__
1111 flags |= STACKSHOT_SAVE_KEXT_LOADINFO;
1112 #endif /* __x86_64__ */
1113
1114
1115 /* OR on flags specified in boot-args */
1116 flags |= bootprofile_stackshot_flags;
1117 if ((flags & STACKSHOT_COLLECT_DELTA_SNAPSHOT) && (bootprofile_delta_since_timestamp == 0)) {
1118 /* Can't take deltas until the first one */
1119 flags &= ~ STACKSHOT_COLLECT_DELTA_SNAPSHOT;
1120 }
1121
1122 uint64_t timestamp = 0;
1123 if (bootprofile_stackshot_flags & STACKSHOT_COLLECT_DELTA_SNAPSHOT) {
1124 timestamp = mach_absolute_time();
1125 }
1126
1127 kern_return_t r = stack_snapshot_from_kernel(
1128 pid_to_profile, (void *)(bootprofile_buffer + bootprofile_buffer_current_position),
1129 bootprofile_buffer_size - bootprofile_buffer_current_position,
1130 flags, bootprofile_delta_since_timestamp, &retbytes);
1131
1132 /*
1133 * We call with STACKSHOT_TRYLOCK because the stackshot lock is coarser
1134 * than the bootprofile lock. If someone else has the lock we'll just
1135 * try again later.
1136 */
1137
1138 if (r == KERN_LOCK_OWNED) {
1139 BOOTPROFILE_UNLOCK();
1140 goto reprogram;
1141 }
1142
1143 if (bootprofile_stackshot_flags & STACKSHOT_COLLECT_DELTA_SNAPSHOT &&
1144 r == KERN_SUCCESS) {
1145 bootprofile_delta_since_timestamp = timestamp;
1146 }
1147
1148 bootprofile_buffer_current_position += retbytes;
1149 }
1150
1151 BOOTPROFILE_UNLOCK();
1152
1153 /* If we didn't get any data or have run out of buffer space, stop profiling */
1154 if ((retbytes == 0) || (bootprofile_buffer_current_position == bootprofile_buffer_size)) {
1155 return;
1156 }
1157
1158
1159 reprogram:
1160 /* If the user gathered the buffer, no need to keep profiling */
1161 if (bootprofile_interval_abs == 0) {
1162 return;
1163 }
1164
1165 clock_deadline_for_periodic_event(bootprofile_interval_abs,
1166 mach_absolute_time(),
1167 &bootprofile_next_deadline);
1168 timer_call_enter_with_leeway(&bootprofile_timer_call_entry,
1169 NULL,
1170 bootprofile_next_deadline,
1171 0,
1172 TIMER_CALL_SYS_NORMAL,
1173 FALSE);
1174 }
1175
1176 void bootprofile_get(void **buffer, uint32_t *length)
1177 {
1178 BOOTPROFILE_LOCK();
1179 *buffer = (void*) bootprofile_buffer;
1180 *length = bootprofile_buffer_current_position;
1181 BOOTPROFILE_UNLOCK();
1182 }
1183
1184 int bootprofile_gather(user_addr_t buffer, uint32_t *length)
1185 {
1186 int result = 0;
1187
1188 BOOTPROFILE_LOCK();
1189
1190 if (bootprofile_buffer == 0) {
1191 *length = 0;
1192 goto out;
1193 }
1194
1195 if (*length < bootprofile_buffer_current_position) {
1196 result = KERN_NO_SPACE;
1197 goto out;
1198 }
1199
1200 if ((result = copyout((void *)bootprofile_buffer, buffer,
1201 bootprofile_buffer_current_position)) != 0) {
1202 *length = 0;
1203 goto out;
1204 }
1205 *length = bootprofile_buffer_current_position;
1206
1207 /* cancel future timers */
1208 bootprofile_interval_abs = 0;
1209
1210 out:
1211
1212 BOOTPROFILE_UNLOCK();
1213
1214 return (result);
1215 }