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