sctop.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # system call top
  2. # (c) 2010, Tom Zanussi <tzanussi@gmail.com>
  3. # Licensed under the terms of the GNU GPL License version 2
  4. #
  5. # Periodically displays system-wide system call totals, broken down by
  6. # syscall. If a [comm] arg is specified, only syscalls called by
  7. # [comm] are displayed. If an [interval] arg is specified, the display
  8. # will be refreshed every [interval] seconds. The default interval is
  9. # 3 seconds.
  10. from __future__ import print_function
  11. import os, sys, time
  12. try:
  13. import thread
  14. except ImportError:
  15. import _thread as thread
  16. sys.path.append(os.environ['PERF_EXEC_PATH'] + \
  17. '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
  18. from perf_trace_context import *
  19. from Core import *
  20. from Util import *
  21. usage = "perf script -s sctop.py [comm] [interval]\n";
  22. for_comm = None
  23. default_interval = 3
  24. interval = default_interval
  25. if len(sys.argv) > 3:
  26. sys.exit(usage)
  27. if len(sys.argv) > 2:
  28. for_comm = sys.argv[1]
  29. interval = int(sys.argv[2])
  30. elif len(sys.argv) > 1:
  31. try:
  32. interval = int(sys.argv[1])
  33. except ValueError:
  34. for_comm = sys.argv[1]
  35. interval = default_interval
  36. syscalls = autodict()
  37. def trace_begin():
  38. thread.start_new_thread(print_syscall_totals, (interval,))
  39. pass
  40. def raw_syscalls__sys_enter(event_name, context, common_cpu,
  41. common_secs, common_nsecs, common_pid, common_comm,
  42. common_callchain, id, args):
  43. if for_comm is not None:
  44. if common_comm != for_comm:
  45. return
  46. try:
  47. syscalls[id] += 1
  48. except TypeError:
  49. syscalls[id] = 1
  50. def syscalls__sys_enter(event_name, context, common_cpu,
  51. common_secs, common_nsecs, common_pid, common_comm,
  52. id, args):
  53. raw_syscalls__sys_enter(**locals())
  54. def print_syscall_totals(interval):
  55. while 1:
  56. clear_term()
  57. if for_comm is not None:
  58. print("\nsyscall events for %s:\n" % (for_comm))
  59. else:
  60. print("\nsyscall events:\n")
  61. print("%-40s %10s" % ("event", "count"))
  62. print("%-40s %10s" %
  63. ("----------------------------------------",
  64. "----------"))
  65. for id, val in sorted(syscalls.items(),
  66. key = lambda kv: (kv[1], kv[0]),
  67. reverse = True):
  68. try:
  69. print("%-40s %10d" % (syscall_name(id), val))
  70. except TypeError:
  71. pass
  72. syscalls.clear()
  73. time.sleep(interval)