futex-contention.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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
  14. import sys
  15. sys.path.append(os.environ['PERF_EXEC_PATH'] +
  16. '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
  17. from Util import *
  18. process_names = {}
  19. thread_thislock = {}
  20. thread_blocktime = {}
  21. lock_waits = {} # long-lived stats on (tid,lock) blockage elapsed time
  22. process_names = {} # long-lived pid-to-execname mapping
  23. def syscalls__sys_enter_futex(event, ctxt, cpu, s, ns, tid, comm, callchain,
  24. nr, uaddr, op, val, utime, uaddr2, val3):
  25. cmd = op & FUTEX_CMD_MASK
  26. if cmd != FUTEX_WAIT:
  27. return # we don't care about originators of WAKE events
  28. process_names[tid] = comm
  29. thread_thislock[tid] = uaddr
  30. thread_blocktime[tid] = nsecs(s, ns)
  31. def syscalls__sys_exit_futex(event, ctxt, cpu, s, ns, tid, comm, callchain,
  32. nr, ret):
  33. if tid in thread_blocktime:
  34. elapsed = nsecs(s, ns) - thread_blocktime[tid]
  35. add_stats(lock_waits, (tid, thread_thislock[tid]), elapsed)
  36. del thread_blocktime[tid]
  37. del thread_thislock[tid]
  38. def trace_begin():
  39. print("Press control+C to stop and show the summary")
  40. def trace_end():
  41. for (tid, lock) in lock_waits:
  42. min, max, avg, count = lock_waits[tid, lock]
  43. print("%s[%d] lock %x contended %d times, %d avg ns [max: %d ns, min %d ns]" %
  44. (process_names[tid], tid, lock, count, avg, max, min))