1235063Snetchild#!/usr/sbin/dtrace -qs 2235063Snetchild 3235063Snetchild/*- 4235063Snetchild * Copyright (c) 2011-2012 Alexander Leidinger <netchild@FreeBSD.org> 5235063Snetchild * All rights reserved. 6235063Snetchild * 7235063Snetchild * Redistribution and use in source and binary forms, with or without 8235063Snetchild * modification, are permitted provided that the following conditions 9235063Snetchild * are met: 10235063Snetchild * 1. Redistributions of source code must retain the above copyright 11235063Snetchild * notice, this list of conditions and the following disclaimer 12235063Snetchild * in this position and unchanged. 13235063Snetchild * 2. Redistributions in binary form must reproduce the above copyright 14235063Snetchild * notice, this list of conditions and the following disclaimer in the 15235063Snetchild * documentation and/or other materials provided with the distribution. 16235063Snetchild * 17235063Snetchild * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18235063Snetchild * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19235063Snetchild * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20235063Snetchild * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21235063Snetchild * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22235063Snetchild * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23235063Snetchild * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24235063Snetchild * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25235063Snetchild * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26235063Snetchild * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27235063Snetchild * 28235063Snetchild * $FreeBSD: releng/10.2/sys/compat/linux/trace_futexes.d 235063 2012-05-05 19:42:38Z netchild $ 29235063Snetchild */ 30235063Snetchild 31235063Snetchild/** 32235063Snetchild * Trace futex operations: 33235063Snetchild * - internal locks 34235063Snetchild * - size of the futex list 35235063Snetchild * - report error conditions (emulation errors, kernel errors, 36235063Snetchild * programming errors) 37235063Snetchild * - execution time (wallclock) of futex related functions 38235063Snetchild */ 39235063Snetchild 40235063Snetchild#pragma D option specsize=32m 41235063Snetchild 42235063Snetchild/* Error conditions */ 43235063Snetchildlinuxulator*:futex:futex_get:error, 44235063Snetchildlinuxulator*:futex:futex_sleep:requeue_error, 45235063Snetchildlinuxulator*:futex:futex_sleep:sleep_error, 46235063Snetchildlinuxulator*:futex:futex_wait:copyin_error, 47235063Snetchildlinuxulator*:futex:futex_wait:itimerfix_error, 48235063Snetchildlinuxulator*:futex:futex_wait:sleep_error, 49235063Snetchildlinuxulator*:futex:futex_atomic_op:missing_access_check, 50235063Snetchildlinuxulator*:futex:futex_atomic_op:unimplemented_op, 51235063Snetchildlinuxulator*:futex:futex_atomic_op:unimplemented_cmp, 52235063Snetchildlinuxulator*:futex:linux_sys_futex:unimplemented_clockswitch, 53235063Snetchildlinuxulator*:futex:linux_sys_futex:copyin_error, 54235063Snetchildlinuxulator*:futex:linux_sys_futex:unhandled_efault, 55235063Snetchildlinuxulator*:futex:linux_sys_futex:unimplemented_lock_pi, 56235063Snetchildlinuxulator*:futex:linux_sys_futex:unimplemented_unlock_pi, 57235063Snetchildlinuxulator*:futex:linux_sys_futex:unimplemented_trylock_pi, 58235063Snetchildlinuxulator*:futex:linux_sys_futex:unimplemented_wait_requeue_pi, 59235063Snetchildlinuxulator*:futex:linux_sys_futex:unimplemented_cmp_requeue_pi, 60235063Snetchildlinuxulator*:futex:linux_sys_futex:unknown_operation, 61235063Snetchildlinuxulator*:futex:linux_get_robust_list:copyout_error, 62235063Snetchildlinuxulator*:futex:handle_futex_death:copyin_error, 63235063Snetchildlinuxulator*:futex:fetch_robust_entry:copyin_error, 64235063Snetchildlinuxulator*:futex:release_futexes:copyin_error 65235063Snetchild{ 66235063Snetchild printf("ERROR: %s in %s:%s:%s\n", probename, probeprov, probemod, 67235063Snetchild probefunc); 68235063Snetchild stack(); 69235063Snetchild ustack(); 70235063Snetchild} 71235063Snetchild 72235063Snetchildlinuxulator*:futex:linux_sys_futex:invalid_cmp_requeue_use, 73235063Snetchildlinuxulator*:futex:linux_sys_futex:deprecated_requeue, 74235063Snetchildlinuxulator*:futex:linux_set_robust_list:size_error 75235063Snetchild{ 76235063Snetchild printf("WARNING: %s:%s:%s:%s in application %s, maybe an application error?\n", 77235063Snetchild probename, probeprov, probemod, probefunc, execname); 78235063Snetchild stack(); 79235063Snetchild ustack(); 80235063Snetchild} 81235063Snetchild 82235063Snetchild 83235063Snetchild/* Per futex checks/statistics */ 84235063Snetchild 85235063Snetchildlinuxulator*:futex:futex:create 86235063Snetchild{ 87235063Snetchild ++futex_count; 88235063Snetchild @max_futexes = max(futex_count); 89235063Snetchild} 90235063Snetchild 91235063Snetchildlinuxulator*:futex:futex:destroy 92235063Snetchild/futex_count == 0/ 93235063Snetchild{ 94235063Snetchild printf("ERROR: Request to destroy a futex which was not created,\n"); 95235063Snetchild printf(" or this script was started after some futexes where\n"); 96235063Snetchild printf(" created. Stack trace:\n"); 97235063Snetchild stack(); 98235063Snetchild ustack(); 99235063Snetchild} 100235063Snetchild 101235063Snetchildlinuxulator*:futex:futex:destroy 102235063Snetchild{ 103235063Snetchild --futex_count; 104235063Snetchild} 105235063Snetchild 106235063Snetchild 107235063Snetchild/* Internal locks */ 108235063Snetchild 109235063Snetchildlinuxulator*:locks:futex_mtx:locked 110235063Snetchild{ 111235063Snetchild ++check[probefunc, arg0]; 112235063Snetchild @stats[probefunc] = count(); 113235063Snetchild 114235063Snetchild ts[probefunc] = timestamp; 115235063Snetchild spec[probefunc] = speculation(); 116235063Snetchild printf("Stacktrace of last lock operation of the %s:\n", probefunc); 117235063Snetchild stack(); 118235063Snetchild} 119235063Snetchild 120235063Snetchildlinuxulator*:locks:futex_mtx:unlock 121235063Snetchild/check[probefunc, arg0] == 0/ 122235063Snetchild{ 123235063Snetchild printf("ERROR: unlock attemt of unlocked %s (%p),", probefunc, arg0); 124235063Snetchild printf(" missing SDT probe in kernel, or dtrace program started"); 125235063Snetchild printf(" while the %s was already held (race condition).", probefunc); 126235063Snetchild printf(" Stack trace follows:"); 127235063Snetchild stack(); 128235063Snetchild} 129235063Snetchild 130235063Snetchildlinuxulator*:locks:futex_mtx:unlock 131235063Snetchild{ 132235063Snetchild discard(spec[probefunc]); 133235063Snetchild spec[probefunc] = 0; 134235063Snetchild --check[probefunc, arg0]; 135235063Snetchild} 136235063Snetchild 137235063Snetchild/* Timeout handling for internal locks */ 138235063Snetchild 139235063Snetchildtick-10s 140235063Snetchild/spec["futex_mtx"] != 0 && timestamp - ts["futex_mtx"] >= 9999999000/ 141235063Snetchild{ 142235063Snetchild commit(spec["futex_mtx"]); 143235063Snetchild spec["futex_mtx"] = 0; 144235063Snetchild} 145235063Snetchild 146235063Snetchild 147235063Snetchild/* Timing statistings */ 148235063Snetchild 149235063Snetchildlinuxulator*:futex::entry 150235063Snetchild{ 151235063Snetchild self->time[probefunc] = timestamp; 152235063Snetchild @calls[probeprov, execname, probefunc] = count(); 153235063Snetchild} 154235063Snetchild 155235063Snetchildlinuxulator*:futex::return 156235063Snetchild/self->time[probefunc] != 0/ 157235063Snetchild{ 158235063Snetchild this->timediff = self->time[probefunc] - timestamp; 159235063Snetchild 160235063Snetchild @timestats[probeprov, execname, probefunc] = quantize(this->timediff); 161235063Snetchild @longest[probeprov, probefunc] = max(this->timediff); 162235063Snetchild 163235063Snetchild self->time[probefunc] = 0; 164235063Snetchild} 165235063Snetchild 166235063Snetchild 167235063Snetchild/* Statistics */ 168235063Snetchild 169235063SnetchildEND 170235063Snetchild{ 171235063Snetchild printf("Number of locks per type:"); 172235063Snetchild printa(@stats); 173235063Snetchild printf("Number of maximum number of futexes in the futex list:"); 174235063Snetchild printa(@max_futexes); 175235063Snetchild printf("Number of futexes still existing: %d", futex_count); 176235063Snetchild printf("Number of calls per provider/application/kernel function:"); 177235063Snetchild printa(@calls); 178235063Snetchild printf("Wallclock-timing statistics per provider/application/kernel function (in ns):"); 179235063Snetchild printa(@timestats); 180235063Snetchild printf("Longest running (wallclock!) functions per provider (in ns):"); 181235063Snetchild printa(@longest); 182235063Snetchild} 183