syscall-counts-by-pid.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # system call counts, by pid
  2. # (c) 2010, Tom Zanussi <tzanussi@gmail.com>
  3. # Licensed under the terms of the GNU GPL License version 2
  4. #
  5. # Displays system-wide system call totals, broken down by syscall.
  6. # If a [comm] arg is specified, only syscalls called by [comm] are displayed.
  7. import os
  8. import sys
  9. sys.path.append(os.environ['PERF_EXEC_PATH'] + \
  10. '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
  11. from perf_trace_context import *
  12. from Core import *
  13. usage = "perf trace -s syscall-counts-by-pid.py [comm]\n";
  14. for_comm = None
  15. if len(sys.argv) > 2:
  16. sys.exit(usage)
  17. if len(sys.argv) > 1:
  18. for_comm = sys.argv[1]
  19. syscalls = autodict()
  20. def trace_begin():
  21. pass
  22. def trace_end():
  23. print_syscall_totals()
  24. def raw_syscalls__sys_enter(event_name, context, common_cpu,
  25. common_secs, common_nsecs, common_pid, common_comm,
  26. id, args):
  27. if for_comm is not None:
  28. if common_comm != for_comm:
  29. return
  30. try:
  31. syscalls[common_comm][common_pid][id] += 1
  32. except TypeError:
  33. syscalls[common_comm][common_pid][id] = 1
  34. def print_syscall_totals():
  35. if for_comm is not None:
  36. print "\nsyscall events for %s:\n\n" % (for_comm),
  37. else:
  38. print "\nsyscall events by comm/pid:\n\n",
  39. print "%-40s %10s\n" % ("comm [pid]/syscalls", "count"),
  40. print "%-40s %10s\n" % ("----------------------------------------", \
  41. "----------"),
  42. comm_keys = syscalls.keys()
  43. for comm in comm_keys:
  44. pid_keys = syscalls[comm].keys()
  45. for pid in pid_keys:
  46. print "\n%s [%d]\n" % (comm, pid),
  47. id_keys = syscalls[comm][pid].keys()
  48. for id, val in sorted(syscalls[comm][pid].iteritems(), \
  49. key = lambda(k, v): (v, k), reverse = True):
  50. print " %-38d %10d\n" % (id, val),