intel-pt-events.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # intel-pt-events.py: Print Intel PT Power Events and PTWRITE
  2. # Copyright (c) 2017, Intel Corporation.
  3. #
  4. # This program is free software; you can redistribute it and/or modify it
  5. # under the terms and conditions of the GNU General Public License,
  6. # version 2, as published by the Free Software Foundation.
  7. #
  8. # This program is distributed in the hope it will be useful, but WITHOUT
  9. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. # more details.
  12. from __future__ import print_function
  13. import os
  14. import sys
  15. import struct
  16. sys.path.append(os.environ['PERF_EXEC_PATH'] + \
  17. '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
  18. # These perf imports are not used at present
  19. #from perf_trace_context import *
  20. #from Core import *
  21. def trace_begin():
  22. print("Intel PT Power Events and PTWRITE")
  23. def trace_end():
  24. print("End")
  25. def trace_unhandled(event_name, context, event_fields_dict):
  26. print(' '.join(['%s=%s'%(k,str(v))for k,v in sorted(event_fields_dict.items())]))
  27. def print_ptwrite(raw_buf):
  28. data = struct.unpack_from("<IQ", raw_buf)
  29. flags = data[0]
  30. payload = data[1]
  31. exact_ip = flags & 1
  32. print("IP: %u payload: %#x" % (exact_ip, payload), end=' ')
  33. def print_cbr(raw_buf):
  34. data = struct.unpack_from("<BBBBII", raw_buf)
  35. cbr = data[0]
  36. f = (data[4] + 500) / 1000
  37. p = ((cbr * 1000 / data[2]) + 5) / 10
  38. print("%3u freq: %4u MHz (%3u%%)" % (cbr, f, p), end=' ')
  39. def print_mwait(raw_buf):
  40. data = struct.unpack_from("<IQ", raw_buf)
  41. payload = data[1]
  42. hints = payload & 0xff
  43. extensions = (payload >> 32) & 0x3
  44. print("hints: %#x extensions: %#x" % (hints, extensions), end=' ')
  45. def print_pwre(raw_buf):
  46. data = struct.unpack_from("<IQ", raw_buf)
  47. payload = data[1]
  48. hw = (payload >> 7) & 1
  49. cstate = (payload >> 12) & 0xf
  50. subcstate = (payload >> 8) & 0xf
  51. print("hw: %u cstate: %u sub-cstate: %u" % (hw, cstate, subcstate),
  52. end=' ')
  53. def print_exstop(raw_buf):
  54. data = struct.unpack_from("<I", raw_buf)
  55. flags = data[0]
  56. exact_ip = flags & 1
  57. print("IP: %u" % (exact_ip), end=' ')
  58. def print_pwrx(raw_buf):
  59. data = struct.unpack_from("<IQ", raw_buf)
  60. payload = data[1]
  61. deepest_cstate = payload & 0xf
  62. last_cstate = (payload >> 4) & 0xf
  63. wake_reason = (payload >> 8) & 0xf
  64. print("deepest cstate: %u last cstate: %u wake reason: %#x" %
  65. (deepest_cstate, last_cstate, wake_reason), end=' ')
  66. def print_common_start(comm, sample, name):
  67. ts = sample["time"]
  68. cpu = sample["cpu"]
  69. pid = sample["pid"]
  70. tid = sample["tid"]
  71. print("%16s %5u/%-5u [%03u] %9u.%09u %7s:" %
  72. (comm, pid, tid, cpu, ts / 1000000000, ts %1000000000, name),
  73. end=' ')
  74. def print_common_ip(sample, symbol, dso):
  75. ip = sample["ip"]
  76. print("%16x %s (%s)" % (ip, symbol, dso))
  77. def process_event(param_dict):
  78. event_attr = param_dict["attr"]
  79. sample = param_dict["sample"]
  80. raw_buf = param_dict["raw_buf"]
  81. comm = param_dict["comm"]
  82. name = param_dict["ev_name"]
  83. # Symbol and dso info are not always resolved
  84. if "dso" in param_dict:
  85. dso = param_dict["dso"]
  86. else:
  87. dso = "[unknown]"
  88. if "symbol" in param_dict:
  89. symbol = param_dict["symbol"]
  90. else:
  91. symbol = "[unknown]"
  92. if name == "ptwrite":
  93. print_common_start(comm, sample, name)
  94. print_ptwrite(raw_buf)
  95. print_common_ip(sample, symbol, dso)
  96. elif name == "cbr":
  97. print_common_start(comm, sample, name)
  98. print_cbr(raw_buf)
  99. print_common_ip(sample, symbol, dso)
  100. elif name == "mwait":
  101. print_common_start(comm, sample, name)
  102. print_mwait(raw_buf)
  103. print_common_ip(sample, symbol, dso)
  104. elif name == "pwre":
  105. print_common_start(comm, sample, name)
  106. print_pwre(raw_buf)
  107. print_common_ip(sample, symbol, dso)
  108. elif name == "exstop":
  109. print_common_start(comm, sample, name)
  110. print_exstop(raw_buf)
  111. print_common_ip(sample, symbol, dso)
  112. elif name == "pwrx":
  113. print_common_start(comm, sample, name)
  114. print_pwrx(raw_buf)
  115. print_common_ip(sample, symbol, dso)