futex-contention.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # futex contention
  2. # (c) 2010, Arnaldo Carvalho de Melo <acme@redhat.com>
  3. # Licensed under the terms of the GNU GPL License version 2
  4. #
  5. # Translation of:
  6. #
  7. # http://sourceware.org/systemtap/wiki/WSFutexContention
  8. #
  9. # to perf python scripting.
  10. #
  11. # Measures futex contention
  12. from __future__ import print_function
  13. import os, sys
  14. sys.path.append(os.environ['PERF_EXEC_PATH'] + '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
  15. from Util import *
  16. process_names = {}
  17. thread_thislock = {}
  18. thread_blocktime = {}
  19. lock_waits = {} # long-lived stats on (tid,lock) blockage elapsed time
  20. process_names = {} # long-lived pid-to-execname mapping
  21. def syscalls__sys_enter_futex(event, ctxt, cpu, s, ns, tid, comm, callchain,
  22. nr, uaddr, op, val, utime, uaddr2, val3):
  23. cmd = op & FUTEX_CMD_MASK
  24. if cmd != FUTEX_WAIT:
  25. return # we don't care about originators of WAKE events
  26. process_names[tid] = comm
  27. thread_thislock[tid] = uaddr
  28. thread_blocktime[tid] = nsecs(s, ns)
  29. def syscalls__sys_exit_futex(event, ctxt, cpu, s, ns, tid, comm, callchain,
  30. nr, ret):
  31. if tid in thread_blocktime:
  32. elapsed = nsecs(s, ns) - thread_blocktime[tid]
  33. add_stats(lock_waits, (tid, thread_thislock[tid]), elapsed)
  34. del thread_blocktime[tid]
  35. del thread_thislock[tid]
  36. def trace_begin():
  37. print("Press control+C to stop and show the summary")
  38. def trace_end():
  39. for (tid, lock) in lock_waits:
  40. min, max, avg, count = lock_waits[tid, lock]
  41. print("%s[%d] lock %x contended %d times, %d avg ns" %
  42. (process_names[tid], tid, lock, count, avg))