syscall-counts.py 1.6 KB

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