syscall-counts-by-pid.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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, sys
  8. sys.path.append(os.environ['PERF_EXEC_PATH'] + \
  9. '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
  10. from perf_trace_context import *
  11. from Core import *
  12. from Util import syscall_name
  13. usage = "perf script -s syscall-counts-by-pid.py [comm]\n";
  14. for_comm = None
  15. for_pid = None
  16. if len(sys.argv) > 2:
  17. sys.exit(usage)
  18. if len(sys.argv) > 1:
  19. try:
  20. for_pid = int(sys.argv[1])
  21. except:
  22. for_comm = sys.argv[1]
  23. syscalls = autodict()
  24. def trace_begin():
  25. print "Press control+C to stop and show the summary"
  26. def trace_end():
  27. print_syscall_totals()
  28. def raw_syscalls__sys_enter(event_name, context, common_cpu,
  29. common_secs, common_nsecs, common_pid, common_comm,
  30. common_callchain, id, args):
  31. if (for_comm and common_comm != for_comm) or \
  32. (for_pid and common_pid != for_pid ):
  33. return
  34. try:
  35. syscalls[common_comm][common_pid][id] += 1
  36. except TypeError:
  37. syscalls[common_comm][common_pid][id] = 1
  38. def syscalls__sys_enter(event_name, context, common_cpu,
  39. common_secs, common_nsecs, common_pid, common_comm,
  40. id, args):
  41. raw_syscalls__sys_enter(**locals())
  42. def print_syscall_totals():
  43. if for_comm is not None:
  44. print "\nsyscall events for %s:\n\n" % (for_comm),
  45. else:
  46. print "\nsyscall events by comm/pid:\n\n",
  47. print "%-40s %10s\n" % ("comm [pid]/syscalls", "count"),
  48. print "%-40s %10s\n" % ("----------------------------------------", \
  49. "----------"),
  50. comm_keys = syscalls.keys()
  51. for comm in comm_keys:
  52. pid_keys = syscalls[comm].keys()
  53. for pid in pid_keys:
  54. print "\n%s [%d]\n" % (comm, pid),
  55. id_keys = syscalls[comm][pid].keys()
  56. for id, val in sorted(syscalls[comm][pid].iteritems(), \
  57. key = lambda(k, v): (v, k), reverse = True):
  58. print " %-38s %10d\n" % (syscall_name(id), val),