futex-contention.py 1.5 KB

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