| 1 |
#! /usr/bin/env stap |
| 2 |
|
| 3 |
# This script tries to identify contended user-space locks by hooking |
| 4 |
# into the futex system call. |
| 5 |
|
| 6 |
global FUTEX_WAIT = 0 /*, FUTEX_WAKE = 1 */ |
| 7 |
global FUTEX_PRIVATE_FLAG = 128 /* linux 2.6.22+ */ |
| 8 |
global FUTEX_CLOCK_REALTIME = 256 /* linux 2.6.29+ */ |
| 9 |
|
| 10 |
global lock_waits # long-lived stats on (tid,lock) blockage elapsed time |
| 11 |
global process_names # long-lived pid-to-execname mapping |
| 12 |
|
| 13 |
global entry_times%, uaddrs% |
| 14 |
|
| 15 |
probe syscall.futex { |
| 16 |
if ((op & ~(FUTEX_PRIVATE_FLAG|FUTEX_CLOCK_REALTIME)) != FUTEX_WAIT) next |
| 17 |
entry_times[tid()] = gettimeofday_us() |
| 18 |
uaddrs[tid()] = futex_uaddr |
| 19 |
} |
| 20 |
|
| 21 |
probe syscall.futex.return { |
| 22 |
if (!(entry_times[tid()])) next |
| 23 |
elapsed = gettimeofday_us() - entry_times[tid()] |
| 24 |
lock_waits[pid(), uaddrs[tid()]] <<< elapsed |
| 25 |
delete entry_times[tid()] |
| 26 |
delete uaddrs[tid()] |
| 27 |
if (!(pid() in process_names)) |
| 28 |
process_names[pid()] = execname() |
| 29 |
} |
| 30 |
|
| 31 |
probe end { |
| 32 |
foreach ([pid+, lock] in lock_waits) |
| 33 |
printf ("%s[%d] lock %p contended %d times, %d avg us\n", |
| 34 |
process_names[pid], pid, lock, @count(lock_waits[pid,lock]), |
| 35 |
@avg(lock_waits[pid,lock])) |
| 36 |
} |