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 */
41linuxulator*:futex:futex_get:error,
42linuxulator*:futex:futex_sleep:requeue_error,
43linuxulator*:futex:futex_sleep:sleep_error,
44linuxulator*:futex:futex_wait:copyin_error,
45linuxulator*:futex:futex_wait:itimerfix_error,
46linuxulator*:futex:futex_wait:sleep_error,
47linuxulator*:futex:futex_atomic_op:missing_access_check,
48linuxulator*:futex:futex_atomic_op:unimplemented_op,
49linuxulator*:futex:futex_atomic_op:unimplemented_cmp,
50linuxulator*:futex:linux_sys_futex:unimplemented_clockswitch,
51linuxulator*:futex:linux_sys_futex:copyin_error,
52linuxulator*:futex:linux_sys_futex:unhandled_efault,
53linuxulator*:futex:linux_sys_futex:unimplemented_lock_pi,
54linuxulator*:futex:linux_sys_futex:unimplemented_unlock_pi,
55linuxulator*:futex:linux_sys_futex:unimplemented_trylock_pi,
56linuxulator*:futex:linux_sys_futex:unimplemented_wait_requeue_pi,
57linuxulator*:futex:linux_sys_futex:unimplemented_cmp_requeue_pi,
58linuxulator*:futex:linux_sys_futex:unknown_operation,
59linuxulator*:futex:linux_get_robust_list:copyout_error,
60linuxulator*:futex:handle_futex_death:copyin_error,
61linuxulator*:futex:fetch_robust_entry:copyin_error,
62linuxulator*: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
70linuxulator*:futex:linux_sys_futex:invalid_cmp_requeue_use,
71linuxulator*:futex:linux_sys_futex:deprecated_requeue,
72linuxulator*: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
83linuxulator*:futex:futex:create
84{
85	++futex_count;
86	@max_futexes = max(futex_count);
87}
88
89linuxulator*: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
99linuxulator*:futex:futex:destroy
100{
101	--futex_count;
102}
103
104
105/* Internal locks */
106
107linuxulator*: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
118linuxulator*: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
128linuxulator*: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
137tick-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
147linuxulator*:futex::entry
148{
149	self->time[probefunc] = timestamp;
150	@calls[probeprov, execname, probefunc] = count();
151}
152
153linuxulator*: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
167END
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