intel-pt-events.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. import os
  13. import sys
  14. import struct
  15. sys.path.append(os.environ['PERF_EXEC_PATH'] + \
  16. '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
  17. # These perf imports are not used at present
  18. #from perf_trace_context import *
  19. #from Core import *
  20. def trace_begin():
  21. print "Intel PT Power Events and PTWRITE"
  22. def trace_end():
  23. print "End"
  24. def trace_unhandled(event_name, context, event_fields_dict):
  25. print ' '.join(['%s=%s'%(k,str(v))for k,v in sorted(event_fields_dict.items())])
  26. def print_ptwrite(raw_buf):
  27. data = struct.unpack_from("<IQ", raw_buf)
  28. flags = data[0]
  29. payload = data[1]
  30. exact_ip = flags & 1
  31. print "IP: %u payload: %#x" % (exact_ip, payload),
  32. def print_cbr(raw_buf):
  33. data = struct.unpack_from("<BBBBII", raw_buf)
  34. cbr = data[0]
  35. f = (data[4] + 500) / 1000
  36. p = ((cbr * 1000 / data[2]) + 5) / 10
  37. print "%3u freq: %4u MHz (%3u%%)" % (cbr, f, p),
  38. def print_mwait(raw_buf):
  39. data = struct.unpack_from("<IQ", raw_buf)
  40. payload = data[1]
  41. hints = payload & 0xff
  42. extensions = (payload >> 32) & 0x3
  43. print "hints: %#x extensions: %#x" % (hints, extensions),
  44. def print_pwre(raw_buf):
  45. data = struct.unpack_from("<IQ", raw_buf)
  46. payload = data[1]
  47. hw = (payload >> 7) & 1
  48. cstate = (payload >> 12) & 0xf
  49. subcstate = (payload >> 8) & 0xf
  50. print "hw: %u cstate: %u sub-cstate: %u" % (hw, cstate, subcstate),
  51. def print_exstop(raw_buf):
  52. data = struct.unpack_from("<I", raw_buf)
  53. flags = data[0]
  54. exact_ip = flags & 1
  55. print "IP: %u" % (exact_ip),
  56. def print_pwrx(raw_buf):
  57. data = struct.unpack_from("<IQ", raw_buf)
  58. payload = data[1]
  59. deepest_cstate = payload & 0xf
  60. last_cstate = (payload >> 4) & 0xf
  61. wake_reason = (payload >> 8) & 0xf
  62. print "deepest cstate: %u last cstate: %u wake reason: %#x" % (deepest_cstate, last_cstate, wake_reason),
  63. def print_common_start(comm, sample, name):
  64. ts = sample["time"]
  65. cpu = sample["cpu"]
  66. pid = sample["pid"]
  67. tid = sample["tid"]
  68. print "%16s %5u/%-5u [%03u] %9u.%09u %7s:" % (comm, pid, tid, cpu, ts / 1000000000, ts %1000000000, name),
  69. def print_common_ip(sample, symbol, dso):
  70. ip = sample["ip"]
  71. print "%16x %s (%s)" % (ip, symbol, dso)
  72. def process_event(param_dict):
  73. event_attr = param_dict["attr"]
  74. sample = param_dict["sample"]
  75. raw_buf = param_dict["raw_buf"]
  76. comm = param_dict["comm"]
  77. name = param_dict["ev_name"]
  78. # Symbol and dso info are not always resolved
  79. if (param_dict.has_key("dso")):
  80. dso = param_dict["dso"]
  81. else:
  82. dso = "[unknown]"
  83. if (param_dict.has_key("symbol")):
  84. symbol = param_dict["symbol"]
  85. else:
  86. symbol = "[unknown]"
  87. if name == "ptwrite":
  88. print_common_start(comm, sample, name)
  89. print_ptwrite(raw_buf)
  90. print_common_ip(sample, symbol, dso)
  91. elif name == "cbr":
  92. print_common_start(comm, sample, name)
  93. print_cbr(raw_buf)
  94. print_common_ip(sample, symbol, dso)
  95. elif name == "mwait":
  96. print_common_start(comm, sample, name)
  97. print_mwait(raw_buf)
  98. print_common_ip(sample, symbol, dso)
  99. elif name == "pwre":
  100. print_common_start(comm, sample, name)
  101. print_pwre(raw_buf)
  102. print_common_ip(sample, symbol, dso)
  103. elif name == "exstop":
  104. print_common_start(comm, sample, name)
  105. print_exstop(raw_buf)
  106. print_common_ip(sample, symbol, dso)
  107. elif name == "pwrx":
  108. print_common_start(comm, sample, name)
  109. print_pwrx(raw_buf)
  110. print_common_ip(sample, symbol, dso)