xref: /freebsd/sys/compat/linux/trace_futexes.d (revision a64729f5077d77e13b9497cb33ecb3c82e606ee8)
1 #!/usr/sbin/dtrace -qs
2 
3 /*-
4  * Copyright (c) 2011-2012 Alexander Leidinger <netchild@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer
12  *    in this position and unchanged.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /**
30  * Trace futex operations:
31  *  - internal locks
32  *  - size of the futex list
33  *  - report error conditions (emulation errors, kernel errors,
34  *    programming errors)
35  *  - execution time (wallclock) of futex related functions
36  */
37 
38 #pragma D option specsize=32m
39 
40 /* Error conditions */
41 linuxulator*:futex:futex_get:error,
42 linuxulator*:futex:futex_sleep:requeue_error,
43 linuxulator*:futex:futex_sleep:sleep_error,
44 linuxulator*:futex:futex_wait:copyin_error,
45 linuxulator*:futex:futex_wait:itimerfix_error,
46 linuxulator*:futex:futex_wait:sleep_error,
47 linuxulator*:futex:futex_atomic_op:missing_access_check,
48 linuxulator*:futex:futex_atomic_op:unimplemented_op,
49 linuxulator*:futex:futex_atomic_op:unimplemented_cmp,
50 linuxulator*:futex:linux_sys_futex:unimplemented_clockswitch,
51 linuxulator*:futex:linux_sys_futex:copyin_error,
52 linuxulator*:futex:linux_sys_futex:unhandled_efault,
53 linuxulator*:futex:linux_sys_futex:unimplemented_lock_pi,
54 linuxulator*:futex:linux_sys_futex:unimplemented_unlock_pi,
55 linuxulator*:futex:linux_sys_futex:unimplemented_trylock_pi,
56 linuxulator*:futex:linux_sys_futex:unimplemented_wait_requeue_pi,
57 linuxulator*:futex:linux_sys_futex:unimplemented_cmp_requeue_pi,
58 linuxulator*:futex:linux_sys_futex:unknown_operation,
59 linuxulator*:futex:linux_get_robust_list:copyout_error,
60 linuxulator*:futex:handle_futex_death:copyin_error,
61 linuxulator*:futex:fetch_robust_entry:copyin_error,
62 linuxulator*:futex:release_futexes:copyin_error
63 {
64 	printf("ERROR: %s in %s:%s:%s\n", probename, probeprov, probemod,
65 	    probefunc);
66 	stack();
67 	ustack();
68 }
69 
70 linuxulator*:futex:linux_sys_futex:invalid_cmp_requeue_use,
71 linuxulator*:futex:linux_sys_futex:deprecated_requeue,
72 linuxulator*:futex:linux_set_robust_list:size_error
73 {
74 	printf("WARNING: %s:%s:%s:%s in application %s, maybe an application error?\n",
75 	    probename, probeprov, probemod, probefunc, execname);
76 	stack();
77 	ustack();
78 }
79 
80 
81 /* Per futex checks/statistics */
82 
83 linuxulator*:futex:futex:create
84 {
85 	++futex_count;
86 	@max_futexes = max(futex_count);
87 }
88 
89 linuxulator*:futex:futex:destroy
90 /futex_count == 0/
91 {
92 	printf("ERROR: Request to destroy a futex which was not created,\n");
93 	printf("       or this script was started after some futexes where\n");
94 	printf("       created. Stack trace:\n");
95 	stack();
96 	ustack();
97 }
98 
99 linuxulator*:futex:futex:destroy
100 {
101 	--futex_count;
102 }
103 
104 
105 /* Internal locks */
106 
107 linuxulator*:locks:futex_mtx:locked
108 {
109 	++check[probefunc, arg0];
110 	@stats[probefunc] = count();
111 
112 	ts[probefunc] = timestamp;
113 	spec[probefunc] = speculation();
114 	printf("Stacktrace of last lock operation of the %s:\n", probefunc);
115 	stack();
116 }
117 
118 linuxulator*:locks:futex_mtx:unlock
119 /check[probefunc, arg0] == 0/
120 {
121 	printf("ERROR: unlock attempt of unlocked %s (%p),", probefunc, arg0);
122 	printf("       missing SDT probe in kernel, or dtrace program started");
123 	printf("       while the %s was already held (race condition).", probefunc);
124 	printf("       Stack trace follows:");
125 	stack();
126 }
127 
128 linuxulator*:locks:futex_mtx:unlock
129 {
130 	discard(spec[probefunc]);
131 	spec[probefunc] = 0;
132 	--check[probefunc, arg0];
133 }
134 
135 /* Timeout handling for internal locks */
136 
137 tick-10s
138 /spec["futex_mtx"] != 0 && timestamp - ts["futex_mtx"] >= 9999999000/
139 {
140 	commit(spec["futex_mtx"]);
141 	spec["futex_mtx"] = 0;
142 }
143 
144 
145 /* Timing statistings */
146 
147 linuxulator*:futex::entry
148 {
149 	self->time[probefunc] = timestamp;
150 	@calls[probeprov, execname, probefunc] = count();
151 }
152 
153 linuxulator*:futex::return
154 /self->time[probefunc] != 0/
155 {
156 	this->timediff = self->time[probefunc] - timestamp;
157 
158 	@timestats[probeprov, execname, probefunc] = quantize(this->timediff);
159 	@longest[probeprov, probefunc] = max(this->timediff);
160 
161 	self->time[probefunc] = 0;
162 }
163 
164 
165 /* Statistics */
166 
167 END
168 {
169 	printf("Number of locks per type:");
170 	printa(@stats);
171 	printf("Number of maximum number of futexes in the futex list:");
172 	printa(@max_futexes);
173 	printf("Number of futexes still existing: %d", futex_count);
174 	printf("Number of calls per provider/application/kernel function:");
175 	printa(@calls);
176 	printf("Wallclock-timing statistics per provider/application/kernel function (in ns):");
177 	printa(@timestats);
178 	printf("Longest running (wallclock!) functions per provider (in ns):");
179 	printa(@longest);
180 }
181