| 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 |
/* Bits indicating sharing in futex_key.both.offset */ |
| 7 |
global FUT_OFF_INODE = 1 /* We set bit 0 if key has a reference on inode */ |
| 8 |
global FUT_OFF_MMSHARED = 2 /* We set bit 1 if key has a reference on mm */ |
| 9 |
|
| 10 |
global FUTEX_WAIT = 0 /*, FUTEX_WAKE = 1 */ |
| 11 |
global FUTEX_PRIVATE_FLAG = 128 /* linux 2.6.22+ */ |
| 12 |
global FUTEX_CLOCK_REALTIME = 256 /* linux 2.6.29+ */ |
| 13 |
|
| 14 |
global wait_keys # per-thread key strings for waiting futexes |
| 15 |
|
| 16 |
global lock_waits # long-lived stats on (tid,lock) blockage elapsed time |
| 17 |
global process_names # long-lived pid-to-execname mapping |
| 18 |
|
| 19 |
@define KEY %( @cast(key, "union futex_key") %) |
| 20 |
probe kernel.function("futex_wait_setup").return { |
| 21 |
if ($return != 0) next |
| 22 |
|
| 23 |
key = @entry(&$q->key) |
| 24 |
|
| 25 |
offset = @KEY->both->offset |
| 26 |
masked_offset = offset & ~(FUT_OFF_INODE | FUT_OFF_MMSHARED) |
| 27 |
|
| 28 |
if (offset & FUT_OFF_INODE) { |
| 29 |
address = @KEY->shared->pgoff * mem_page_size() + masked_offset |
| 30 |
path = inode_path(@KEY->shared->inode) |
| 31 |
wait_keys[tid()] = sprintf("INODE:%s+%p", path, address) |
| 32 |
} else if (offset & FUT_OFF_MMSHARED) { |
| 33 |
address = @KEY->private->address + masked_offset |
| 34 |
wait_keys[tid()] = sprintf("MMSHARED:%p", address) |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
global entry_times% |
| 39 |
|
| 40 |
probe syscall.futex { |
| 41 |
if ((op & ~(FUTEX_PRIVATE_FLAG|FUTEX_CLOCK_REALTIME)) != FUTEX_WAIT) next |
| 42 |
entry_times[tid()] = gettimeofday_us() |
| 43 |
} |
| 44 |
|
| 45 |
probe syscall.futex.return { |
| 46 |
if (!(tid() in wait_keys)) next |
| 47 |
key = wait_keys[tid()] |
| 48 |
delete wait_keys[tid()] |
| 49 |
|
| 50 |
if (!(tid() in entry_times)) next |
| 51 |
|
| 52 |
elapsed = gettimeofday_us() - entry_times[tid()] |
| 53 |
lock_waits[pid(), key] <<< elapsed |
| 54 |
delete entry_times[tid()] |
| 55 |
if (!(pid() in process_names)) |
| 56 |
process_names[pid()] = execname() |
| 57 |
} |
| 58 |
|
| 59 |
probe end { |
| 60 |
foreach ([pid+, lock] in lock_waits) |
| 61 |
printf ("%s[%d] lock %s contended %d times, %d avg us\n", |
| 62 |
process_names[pid], pid, lock, @count(lock_waits[pid,lock]), |
| 63 |
@avg(lock_waits[pid,lock])) |
| 64 |
} |