syscall-counts-by-pid.py 2.0 KB

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