syscall-counts.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # system call counts
  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. from Util import syscall_name
  14. usage = "perf script -s syscall-counts.py [comm]\n";
  15. for_comm = None
  16. if len(sys.argv) > 2:
  17. sys.exit(usage)
  18. if len(sys.argv) > 1:
  19. for_comm = sys.argv[1]
  20. syscalls = autodict()
  21. def trace_begin():
  22. print "Press control+C to stop and show the summary"
  23. def trace_end():
  24. print_syscall_totals()
  25. def raw_syscalls__sys_enter(event_name, context, common_cpu,
  26. common_secs, common_nsecs, common_pid, common_comm,
  27. common_callchain, id, args):
  28. if for_comm is not None:
  29. if common_comm != for_comm:
  30. return
  31. try:
  32. syscalls[id] += 1
  33. except TypeError:
  34. syscalls[id] = 1
  35. def syscalls__sys_enter(event_name, context, common_cpu,
  36. common_secs, common_nsecs, common_pid, common_comm,
  37. id, args):
  38. raw_syscalls__sys_enter(**locals())
  39. def print_syscall_totals():
  40. if for_comm is not None:
  41. print "\nsyscall events for %s:\n\n" % (for_comm),
  42. else:
  43. print "\nsyscall events:\n\n",
  44. print "%-40s %10s\n" % ("event", "count"),
  45. print "%-40s %10s\n" % ("----------------------------------------", \
  46. "-----------"),
  47. for id, val in sorted(syscalls.iteritems(), key = lambda(k, v): (v, k), \
  48. reverse = True):
  49. print "%-40s %10d\n" % (syscall_name(id), val),