1*a92bf335SAdrian Hunter# SPDX-License-Identifier: GPL-2.0 2*a92bf335SAdrian Hunter# intel-pt-events.py: Print Intel PT Events including Power Events and PTWRITE 3*a92bf335SAdrian Hunter# Copyright (c) 2017-2021, Intel Corporation. 4cc892720SAdrian Hunter# 5cc892720SAdrian Hunter# This program is free software; you can redistribute it and/or modify it 6cc892720SAdrian Hunter# under the terms and conditions of the GNU General Public License, 7cc892720SAdrian Hunter# version 2, as published by the Free Software Foundation. 8cc892720SAdrian Hunter# 9cc892720SAdrian Hunter# This program is distributed in the hope it will be useful, but WITHOUT 10cc892720SAdrian Hunter# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11cc892720SAdrian Hunter# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12cc892720SAdrian Hunter# more details. 13cc892720SAdrian Hunter 14fdf2460cSTony Jonesfrom __future__ import print_function 15fdf2460cSTony Jones 16cc892720SAdrian Hunterimport os 17cc892720SAdrian Hunterimport sys 18cc892720SAdrian Hunterimport struct 19cc892720SAdrian Hunter 20cc892720SAdrian Huntersys.path.append(os.environ['PERF_EXEC_PATH'] + \ 21cc892720SAdrian Hunter '/scripts/python/Perf-Trace-Util/lib/Perf/Trace') 22cc892720SAdrian Hunter 23cc892720SAdrian Hunter# These perf imports are not used at present 24cc892720SAdrian Hunter#from perf_trace_context import * 25cc892720SAdrian Hunter#from Core import * 26cc892720SAdrian Hunter 27*a92bf335SAdrian Huntertry: 28*a92bf335SAdrian Hunter broken_pipe_exception = BrokenPipeError 29*a92bf335SAdrian Hunterexcept: 30*a92bf335SAdrian Hunter broken_pipe_exception = IOError 31*a92bf335SAdrian Hunter 32*a92bf335SAdrian Hunterglb_switch_str = None 33*a92bf335SAdrian Hunterglb_switch_printed = True 34*a92bf335SAdrian Hunter 35*a92bf335SAdrian Hunterdef get_optional_null(perf_dict, field): 36*a92bf335SAdrian Hunter if field in perf_dict: 37*a92bf335SAdrian Hunter return perf_dict[field] 38*a92bf335SAdrian Hunter return "" 39*a92bf335SAdrian Hunter 40*a92bf335SAdrian Hunterdef get_optional_zero(perf_dict, field): 41*a92bf335SAdrian Hunter if field in perf_dict: 42*a92bf335SAdrian Hunter return perf_dict[field] 43*a92bf335SAdrian Hunter return 0 44*a92bf335SAdrian Hunter 45*a92bf335SAdrian Hunterdef get_optional(perf_dict, field): 46*a92bf335SAdrian Hunter if field in perf_dict: 47*a92bf335SAdrian Hunter return perf_dict[field] 48*a92bf335SAdrian Hunter return "[unknown]" 49*a92bf335SAdrian Hunter 50*a92bf335SAdrian Hunterdef get_offset(perf_dict, field): 51*a92bf335SAdrian Hunter if field in perf_dict: 52*a92bf335SAdrian Hunter return "+%#x" % perf_dict[field] 53*a92bf335SAdrian Hunter return "" 54*a92bf335SAdrian Hunter 55cc892720SAdrian Hunterdef trace_begin(): 56*a92bf335SAdrian Hunter print("Intel PT Branch Trace, Power Events and PTWRITE") 57cc892720SAdrian Hunter 58cc892720SAdrian Hunterdef trace_end(): 59fdf2460cSTony Jones print("End") 60cc892720SAdrian Hunter 61cc892720SAdrian Hunterdef trace_unhandled(event_name, context, event_fields_dict): 62fdf2460cSTony Jones print(' '.join(['%s=%s'%(k,str(v))for k,v in sorted(event_fields_dict.items())])) 63cc892720SAdrian Hunter 64cc892720SAdrian Hunterdef print_ptwrite(raw_buf): 65cc892720SAdrian Hunter data = struct.unpack_from("<IQ", raw_buf) 66cc892720SAdrian Hunter flags = data[0] 67cc892720SAdrian Hunter payload = data[1] 68cc892720SAdrian Hunter exact_ip = flags & 1 69fdf2460cSTony Jones print("IP: %u payload: %#x" % (exact_ip, payload), end=' ') 70cc892720SAdrian Hunter 71cc892720SAdrian Hunterdef print_cbr(raw_buf): 72cc892720SAdrian Hunter data = struct.unpack_from("<BBBBII", raw_buf) 73cc892720SAdrian Hunter cbr = data[0] 74cc892720SAdrian Hunter f = (data[4] + 500) / 1000 75cc892720SAdrian Hunter p = ((cbr * 1000 / data[2]) + 5) / 10 76fdf2460cSTony Jones print("%3u freq: %4u MHz (%3u%%)" % (cbr, f, p), end=' ') 77cc892720SAdrian Hunter 78cc892720SAdrian Hunterdef print_mwait(raw_buf): 79cc892720SAdrian Hunter data = struct.unpack_from("<IQ", raw_buf) 80cc892720SAdrian Hunter payload = data[1] 81cc892720SAdrian Hunter hints = payload & 0xff 82cc892720SAdrian Hunter extensions = (payload >> 32) & 0x3 83fdf2460cSTony Jones print("hints: %#x extensions: %#x" % (hints, extensions), end=' ') 84cc892720SAdrian Hunter 85cc892720SAdrian Hunterdef print_pwre(raw_buf): 86cc892720SAdrian Hunter data = struct.unpack_from("<IQ", raw_buf) 87cc892720SAdrian Hunter payload = data[1] 88cc892720SAdrian Hunter hw = (payload >> 7) & 1 89cc892720SAdrian Hunter cstate = (payload >> 12) & 0xf 90cc892720SAdrian Hunter subcstate = (payload >> 8) & 0xf 91fdf2460cSTony Jones print("hw: %u cstate: %u sub-cstate: %u" % (hw, cstate, subcstate), 92fdf2460cSTony Jones end=' ') 93cc892720SAdrian Hunter 94cc892720SAdrian Hunterdef print_exstop(raw_buf): 95cc892720SAdrian Hunter data = struct.unpack_from("<I", raw_buf) 96cc892720SAdrian Hunter flags = data[0] 97cc892720SAdrian Hunter exact_ip = flags & 1 98fdf2460cSTony Jones print("IP: %u" % (exact_ip), end=' ') 99cc892720SAdrian Hunter 100cc892720SAdrian Hunterdef print_pwrx(raw_buf): 101cc892720SAdrian Hunter data = struct.unpack_from("<IQ", raw_buf) 102cc892720SAdrian Hunter payload = data[1] 103cc892720SAdrian Hunter deepest_cstate = payload & 0xf 104cc892720SAdrian Hunter last_cstate = (payload >> 4) & 0xf 105cc892720SAdrian Hunter wake_reason = (payload >> 8) & 0xf 106fdf2460cSTony Jones print("deepest cstate: %u last cstate: %u wake reason: %#x" % 107fdf2460cSTony Jones (deepest_cstate, last_cstate, wake_reason), end=' ') 108cc892720SAdrian Hunter 109*a92bf335SAdrian Hunterdef print_psb(raw_buf): 110*a92bf335SAdrian Hunter data = struct.unpack_from("<IQ", raw_buf) 111*a92bf335SAdrian Hunter offset = data[1] 112*a92bf335SAdrian Hunter print("offset: %#x" % (offset), end=' ') 113*a92bf335SAdrian Hunter 114cc892720SAdrian Hunterdef print_common_start(comm, sample, name): 115cc892720SAdrian Hunter ts = sample["time"] 116cc892720SAdrian Hunter cpu = sample["cpu"] 117cc892720SAdrian Hunter pid = sample["pid"] 118cc892720SAdrian Hunter tid = sample["tid"] 119*a92bf335SAdrian Hunter flags_disp = get_optional_null(sample, "flags_disp") 120*a92bf335SAdrian Hunter # Unused fields: 121*a92bf335SAdrian Hunter # period = sample["period"] 122*a92bf335SAdrian Hunter # phys_addr = sample["phys_addr"] 123*a92bf335SAdrian Hunter # weight = sample["weight"] 124*a92bf335SAdrian Hunter # transaction = sample["transaction"] 125*a92bf335SAdrian Hunter # cpumode = get_optional_zero(sample, "cpumode") 126*a92bf335SAdrian Hunter print("%16s %5u/%-5u [%03u] %9u.%09u %7s %19s" % 127*a92bf335SAdrian Hunter (comm, pid, tid, cpu, ts / 1000000000, ts %1000000000, name, flags_disp), 128fdf2460cSTony Jones end=' ') 129cc892720SAdrian Hunter 130*a92bf335SAdrian Hunterdef print_common_ip(param_dict, sample, symbol, dso): 131cc892720SAdrian Hunter ip = sample["ip"] 132*a92bf335SAdrian Hunter offs = get_offset(param_dict, "symoff") 133*a92bf335SAdrian Hunter print("%16x %s%s (%s)" % (ip, symbol, offs, dso), end=' ') 134*a92bf335SAdrian Hunter if "addr_correlates_sym" in sample: 135*a92bf335SAdrian Hunter addr = sample["addr"] 136*a92bf335SAdrian Hunter dso = get_optional(sample, "addr_dso") 137*a92bf335SAdrian Hunter symbol = get_optional(sample, "addr_symbol") 138*a92bf335SAdrian Hunter offs = get_offset(sample, "addr_symoff") 139*a92bf335SAdrian Hunter print("=> %x %s%s (%s)" % (addr, symbol, offs, dso)) 140*a92bf335SAdrian Hunter else: 141*a92bf335SAdrian Hunter print() 142cc892720SAdrian Hunter 143*a92bf335SAdrian Hunterdef do_process_event(param_dict): 144*a92bf335SAdrian Hunter global glb_switch_printed 145*a92bf335SAdrian Hunter if not glb_switch_printed: 146*a92bf335SAdrian Hunter print(glb_switch_str) 147*a92bf335SAdrian Hunter glb_switch_printed = True 148cc892720SAdrian Hunter event_attr = param_dict["attr"] 149cc892720SAdrian Hunter sample = param_dict["sample"] 150cc892720SAdrian Hunter raw_buf = param_dict["raw_buf"] 151cc892720SAdrian Hunter comm = param_dict["comm"] 152cc892720SAdrian Hunter name = param_dict["ev_name"] 153*a92bf335SAdrian Hunter # Unused fields: 154*a92bf335SAdrian Hunter # callchain = param_dict["callchain"] 155*a92bf335SAdrian Hunter # brstack = param_dict["brstack"] 156*a92bf335SAdrian Hunter # brstacksym = param_dict["brstacksym"] 157cc892720SAdrian Hunter 158cc892720SAdrian Hunter # Symbol and dso info are not always resolved 159*a92bf335SAdrian Hunter dso = get_optional(param_dict, "dso") 160*a92bf335SAdrian Hunter symbol = get_optional(param_dict, "symbol") 161cc892720SAdrian Hunter 162*a92bf335SAdrian Hunter print_common_start(comm, sample, name) 163cc892720SAdrian Hunter 164cc892720SAdrian Hunter if name == "ptwrite": 165cc892720SAdrian Hunter print_ptwrite(raw_buf) 166cc892720SAdrian Hunter elif name == "cbr": 167cc892720SAdrian Hunter print_cbr(raw_buf) 168cc892720SAdrian Hunter elif name == "mwait": 169cc892720SAdrian Hunter print_mwait(raw_buf) 170cc892720SAdrian Hunter elif name == "pwre": 171cc892720SAdrian Hunter print_pwre(raw_buf) 172cc892720SAdrian Hunter elif name == "exstop": 173cc892720SAdrian Hunter print_exstop(raw_buf) 174cc892720SAdrian Hunter elif name == "pwrx": 175cc892720SAdrian Hunter print_pwrx(raw_buf) 176*a92bf335SAdrian Hunter elif name == "psb": 177*a92bf335SAdrian Hunter print_psb(raw_buf) 178*a92bf335SAdrian Hunter 179*a92bf335SAdrian Hunter print_common_ip(param_dict, sample, symbol, dso) 180*a92bf335SAdrian Hunter 181*a92bf335SAdrian Hunterdef process_event(param_dict): 182*a92bf335SAdrian Hunter try: 183*a92bf335SAdrian Hunter do_process_event(param_dict) 184*a92bf335SAdrian Hunter except broken_pipe_exception: 185*a92bf335SAdrian Hunter # Stop python printing broken pipe errors and traceback 186*a92bf335SAdrian Hunter sys.stdout = open(os.devnull, 'w') 187*a92bf335SAdrian Hunter sys.exit(1) 188*a92bf335SAdrian Hunter 189*a92bf335SAdrian Hunterdef auxtrace_error(typ, code, cpu, pid, tid, ip, ts, msg, cpumode, *x): 190*a92bf335SAdrian Hunter try: 191*a92bf335SAdrian Hunter print("%16s %5u/%-5u [%03u] %9u.%09u error type %u code %u: %s ip 0x%16x" % 192*a92bf335SAdrian Hunter ("Trace error", pid, tid, cpu, ts / 1000000000, ts %1000000000, typ, code, msg, ip)) 193*a92bf335SAdrian Hunter except broken_pipe_exception: 194*a92bf335SAdrian Hunter # Stop python printing broken pipe errors and traceback 195*a92bf335SAdrian Hunter sys.stdout = open(os.devnull, 'w') 196*a92bf335SAdrian Hunter sys.exit(1) 197*a92bf335SAdrian Hunter 198*a92bf335SAdrian Hunterdef context_switch(ts, cpu, pid, tid, np_pid, np_tid, machine_pid, out, out_preempt, *x): 199*a92bf335SAdrian Hunter global glb_switch_printed 200*a92bf335SAdrian Hunter global glb_switch_str 201*a92bf335SAdrian Hunter if out: 202*a92bf335SAdrian Hunter out_str = "Switch out " 203*a92bf335SAdrian Hunter else: 204*a92bf335SAdrian Hunter out_str = "Switch In " 205*a92bf335SAdrian Hunter if out_preempt: 206*a92bf335SAdrian Hunter preempt_str = "preempt" 207*a92bf335SAdrian Hunter else: 208*a92bf335SAdrian Hunter preempt_str = "" 209*a92bf335SAdrian Hunter if machine_pid == -1: 210*a92bf335SAdrian Hunter machine_str = "" 211*a92bf335SAdrian Hunter else: 212*a92bf335SAdrian Hunter machine_str = "machine PID %d" % machine_pid 213*a92bf335SAdrian Hunter glb_switch_str = "%16s %5d/%-5d [%03u] %9u.%09u %5d/%-5d %s %s" % \ 214*a92bf335SAdrian Hunter (out_str, pid, tid, cpu, ts / 1000000000, ts %1000000000, np_pid, np_tid, machine_str, preempt_str) 215*a92bf335SAdrian Hunter glb_switch_printed = False 216