1#!/usr/sbin/dtrace -s
2
3/*
4 * Copyright (c) 2012-2013 Apple Inc. All rights reserved.
5 *
6 * @APPLE_APACHE_LICENSE_HEADER_START@
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 *     http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 * @APPLE_APACHE_LICENSE_HEADER_END@
21 */
22
23/*
24 * Usage: dispatch_timers.d -p [pid]
25 *        traced process must have been executed with
26 *        DYLD_LIBRARY_PATH=/usr/lib/system/introspection or with
27 *        DYLD_IMAGE_SUFFIX=_profile or DYLD_IMAGE_SUFFIX=_debug
28 */
29
30#pragma D option quiet
31#pragma D option zdefs
32
33typedef struct dispatch_trace_timer_params_s {
34	int64_t deadline, interval, leeway;
35} *dispatch_trace_timer_params_t;
36
37dispatch$target:libdispatch*.dylib::timer-configure,
38dispatch$target:libdispatch*.dylib::timer-program,
39dispatch$target:libdispatch*.dylib::timer-wake,
40dispatch$target:libdispatch*.dylib::timer-fire /!start/ {
41	start = walltimestamp;
42}
43
44/*
45 * Trace dispatch timer configuration and programming:
46 * Timer configuration indicates that dispatch_source_set_timer() was called.
47 * Timer programming indicates that the dispatch manager is about to sleep
48 * for 'deadline' ns (but may wake up earlier if non-timer events occur).
49 * Time parameters are in nanoseconds, a value of -1 means "forever".
50 *
51 * probe timer-configure/-program(dispatch_source_t source,
52 *         dispatch_function_t function, dispatch_trace_timer_params_t params)
53 */
54dispatch$target:libdispatch*.dylib::timer-configure,
55dispatch$target:libdispatch*.dylib::timer-program {
56	this->p = (dispatch_trace_timer_params_t)copyin(arg2,
57			sizeof(struct dispatch_trace_timer_params_s));
58	printf("%8dus %-15s: 0x%0?p deadline: %11dns interval: %11dns leeway: %11dns",
59			(walltimestamp-start)/1000, probename, arg0,
60			this->p ? this->p->deadline : 0, this->p ? this->p->interval : 0,
61			this->p ? this->p->leeway : 0);
62	usym(arg1);
63	printf("\n");
64}
65dispatch$target:libdispatch*.dylib::timer-configure {
66	printf("              / --- Begin ustack");
67	ustack();
68	printf("              \ --- End ustack\n");
69}
70
71/*
72 * Trace dispatch timer wakes and fires:
73 * Timer wakes indicate that the dispatch manager woke up due to expiry of the
74 * deadline for the specified timer.
75 * Timer fires indicate that that the dispatch manager scheduled the event
76 * handler of the specified timer for asynchronous execution (may occur without
77 * a corresponding timer wake if the manager was awake processing other events
78 * when the timer deadline expired).
79 *
80 * probe timer-wake/-fire(dispatch_source_t source,
81 *         dispatch_function_t function)
82 */
83dispatch$target:libdispatch*.dylib::timer-wake,
84dispatch$target:libdispatch*.dylib::timer-fire {
85	printf("%8dus %-15s: 0x%0?p%-70s", (walltimestamp-start)/1000, probename,
86			arg0, "");
87	usym(arg1);
88	printf("\n");
89}
90