Back to home page

OSCL-LXR

 
 

    


0001 # futex contention
0002 # (c) 2010, Arnaldo Carvalho de Melo <acme@redhat.com>
0003 # Licensed under the terms of the GNU GPL License version 2
0004 #
0005 # Translation of:
0006 #
0007 # http://sourceware.org/systemtap/wiki/WSFutexContention
0008 #
0009 # to perf python scripting.
0010 #
0011 # Measures futex contention
0012 
0013 from __future__ import print_function
0014 
0015 import os
0016 import sys
0017 sys.path.append(os.environ['PERF_EXEC_PATH'] +
0018                 '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
0019 from Util import *
0020 
0021 process_names = {}
0022 thread_thislock = {}
0023 thread_blocktime = {}
0024 
0025 lock_waits = {}  # long-lived stats on (tid,lock) blockage elapsed time
0026 process_names = {}  # long-lived pid-to-execname mapping
0027 
0028 
0029 def syscalls__sys_enter_futex(event, ctxt, cpu, s, ns, tid, comm, callchain,
0030                               nr, uaddr, op, val, utime, uaddr2, val3):
0031     cmd = op & FUTEX_CMD_MASK
0032     if cmd != FUTEX_WAIT:
0033         return  # we don't care about originators of WAKE events
0034 
0035     process_names[tid] = comm
0036     thread_thislock[tid] = uaddr
0037     thread_blocktime[tid] = nsecs(s, ns)
0038 
0039 
0040 def syscalls__sys_exit_futex(event, ctxt, cpu, s, ns, tid, comm, callchain,
0041                              nr, ret):
0042     if tid in thread_blocktime:
0043         elapsed = nsecs(s, ns) - thread_blocktime[tid]
0044         add_stats(lock_waits, (tid, thread_thislock[tid]), elapsed)
0045         del thread_blocktime[tid]
0046         del thread_thislock[tid]
0047 
0048 
0049 def trace_begin():
0050     print("Press control+C to stop and show the summary")
0051 
0052 
0053 def trace_end():
0054     for (tid, lock) in lock_waits:
0055         min, max, avg, count = lock_waits[tid, lock]
0056         print("%s[%d] lock %x contended %d times, %d avg ns [max: %d ns, min %d ns]" %
0057               (process_names[tid], tid, lock, count, avg, max, min))