timertop.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #!/usr/bin/env python
  2. import sys, os, subprocess, addr2line
  3. def ex(cmd):
  4. return subprocess.Popen([ 'bash', '-c', cmd ], stdout = subprocess.PIPE).communicate()[0]
  5. if len(sys.argv) > 1:
  6. if sys.argv[1] == '-':
  7. data = sys.stdin.readlines()
  8. else:
  9. data = file(sys.argv[1]).readlines()
  10. else:
  11. data = file('sim_timers.out').readlines()
  12. addr2line.set_rdtsc(long(data[0]))
  13. data = [ map(long, line.split()[:4]) + [ map(long, line.split()[4:10]), line.split(' ', 10)[10].strip() ] for line in data[1:] ]
  14. data.sort(key = lambda line: line[0], reverse = True)
  15. height, width = ex('stty size').split()
  16. width = int(width)
  17. if width < 120:
  18. width = 2*width # if we're line-wrapping anyway: use 2 full lines
  19. for line in data[:15]:
  20. total, n, max, n_switched, trace, name = line
  21. result = '%6.1f s, %8u calls, avg %5.0f us/call, max %5.1f ms, switched = %.3f%% (%3u) ' % \
  22. (total / 1e9, n, total / (n or 1) / 1e3, max / 1e6, 100. * n_switched / (n or 1), n_switched)
  23. print '%-85s %s' % (result, name)
  24. for a in trace[1:6]:
  25. if a:
  26. (file, function, line) = addr2line.addr2line(a)
  27. function = function[:(width - 86 - len(file) - 1 - 1 - len(line))]
  28. print ' '*85, ':'.join((file, function, line)).strip()