| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 |
|
| 14 | from __future__ import print_function
|
| 15 |
|
| 16 | import os
|
| 17 | import sys
|
| 18 | import struct
|
| 19 | import argparse
|
| 20 |
|
| 21 | from libxed import LibXED
|
| 22 | from ctypes import create_string_buffer, addressof
|
| 23 |
|
| 24 | sys.path.append(os.environ['PERF_EXEC_PATH'] + \
|
| 25 | '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
|
| 26 |
|
| 27 | from perf_trace_context import perf_set_itrace_options, \
|
| 28 | perf_sample_insn, perf_sample_srccode
|
| 29 |
|
| 30 | try:
|
| 31 | broken_pipe_exception = BrokenPipeError
|
| 32 | except:
|
| 33 | broken_pipe_exception = IOError
|
| 34 |
|
| 35 | glb_switch_str = {}
|
| 36 | glb_insn = False
|
| 37 | glb_disassembler = None
|
| 38 | glb_src = False
|
| 39 | glb_source_file_name = None
|
| 40 | glb_line_number = None
|
| 41 | glb_dso = None
|
| 42 |
|
| 43 | def get_optional_null(perf_dict, field):
|
| 44 | if field in perf_dict:
|
| 45 | return perf_dict[field]
|
| 46 | return ""
|
| 47 |
|
| 48 | def get_optional_zero(perf_dict, field):
|
| 49 | if field in perf_dict:
|
| 50 | return perf_dict[field]
|
| 51 | return 0
|
| 52 |
|
| 53 | def get_optional_bytes(perf_dict, field):
|
| 54 | if field in perf_dict:
|
| 55 | return perf_dict[field]
|
| 56 | return bytes()
|
| 57 |
|
| 58 | def get_optional(perf_dict, field):
|
| 59 | if field in perf_dict:
|
| 60 | return perf_dict[field]
|
| 61 | return "[unknown]"
|
| 62 |
|
| 63 | def get_offset(perf_dict, field):
|
| 64 | if field in perf_dict:
|
| 65 | return "+%#x" % perf_dict[field]
|
| 66 | return ""
|
| 67 |
|
| 68 | def trace_begin():
|
| 69 | ap = argparse.ArgumentParser(usage = "", add_help = False)
|
| 70 | ap.add_argument("--insn-trace", action='store_true')
|
| 71 | ap.add_argument("--src-trace", action='store_true')
|
| 72 | ap.add_argument("--all-switch-events", action='store_true')
|
| 73 | global glb_args
|
| 74 | global glb_insn
|
| 75 | global glb_src
|
| 76 | glb_args = ap.parse_args()
|
| 77 | if glb_args.insn_trace:
|
| 78 | print("Intel PT Instruction Trace")
|
| 79 | itrace = "i0nsepwxI"
|
| 80 | glb_insn = True
|
| 81 | elif glb_args.src_trace:
|
| 82 | print("Intel PT Source Trace")
|
| 83 | itrace = "i0nsepwxI"
|
| 84 | glb_insn = True
|
| 85 | glb_src = True
|
| 86 | else:
|
| 87 | print("Intel PT Branch Trace, Power Events, Event Trace and PTWRITE")
|
| 88 | itrace = "bepwxI"
|
| 89 | global glb_disassembler
|
| 90 | try:
|
| 91 | glb_disassembler = LibXED()
|
| 92 | except:
|
| 93 | glb_disassembler = None
|
| 94 | perf_set_itrace_options(perf_script_context, itrace)
|
| 95 |
|
| 96 | def trace_end():
|
| 97 | print("End")
|
| 98 |
|
| 99 | def trace_unhandled(event_name, context, event_fields_dict):
|
| 100 | print(' '.join(['%s=%s'%(k,str(v))for k,v in sorted(event_fields_dict.items())]))
|
| 101 |
|
| 102 | def print_ptwrite(raw_buf):
|
| 103 | data = struct.unpack_from("<IQ", raw_buf)
|
| 104 | flags = data[0]
|
| 105 | payload = data[1]
|
| 106 | exact_ip = flags & 1
|
| 107 | print("IP: %u payload: %#x" % (exact_ip, payload), end=' ')
|
| 108 |
|
| 109 | def print_cbr(raw_buf):
|
| 110 | data = struct.unpack_from("<BBBBII", raw_buf)
|
| 111 | cbr = data[0]
|
| 112 | f = (data[4] + 500) / 1000
|
| 113 | p = ((cbr * 1000 / data[2]) + 5) / 10
|
| 114 | print("%3u freq: %4u MHz (%3u%%)" % (cbr, f, p), end=' ')
|
| 115 |
|
| 116 | def print_mwait(raw_buf):
|
| 117 | data = struct.unpack_from("<IQ", raw_buf)
|
| 118 | payload = data[1]
|
| 119 | hints = payload & 0xff
|
| 120 | extensions = (payload >> 32) & 0x3
|
| 121 | print("hints: %#x extensions: %#x" % (hints, extensions), end=' ')
|
| 122 |
|
| 123 | def print_pwre(raw_buf):
|
| 124 | data = struct.unpack_from("<IQ", raw_buf)
|
| 125 | payload = data[1]
|
| 126 | hw = (payload >> 7) & 1
|
| 127 | cstate = (payload >> 12) & 0xf
|
| 128 | subcstate = (payload >> 8) & 0xf
|
| 129 | print("hw: %u cstate: %u sub-cstate: %u" % (hw, cstate, subcstate),
|
| 130 | end=' ')
|
| 131 |
|
| 132 | def print_exstop(raw_buf):
|
| 133 | data = struct.unpack_from("<I", raw_buf)
|
| 134 | flags = data[0]
|
| 135 | exact_ip = flags & 1
|
| 136 | print("IP: %u" % (exact_ip), end=' ')
|
| 137 |
|
| 138 | def print_pwrx(raw_buf):
|
| 139 | data = struct.unpack_from("<IQ", raw_buf)
|
| 140 | payload = data[1]
|
| 141 | deepest_cstate = payload & 0xf
|
| 142 | last_cstate = (payload >> 4) & 0xf
|
| 143 | wake_reason = (payload >> 8) & 0xf
|
| 144 | print("deepest cstate: %u last cstate: %u wake reason: %#x" %
|
| 145 | (deepest_cstate, last_cstate, wake_reason), end=' ')
|
| 146 |
|
| 147 | def print_psb(raw_buf):
|
| 148 | data = struct.unpack_from("<IQ", raw_buf)
|
| 149 | offset = data[1]
|
| 150 | print("offset: %#x" % (offset), end=' ')
|
| 151 |
|
| 152 | glb_cfe = ["", "INTR", "IRET", "SMI", "RSM", "SIPI", "INIT", "VMENTRY", "VMEXIT",
|
| 153 | "VMEXIT_INTR", "SHUTDOWN", "", "UINT", "UIRET"] + [""] * 18
|
| 154 | glb_evd = ["", "PFA", "VMXQ", "VMXR"] + [""] * 60
|
| 155 |
|
| 156 | def print_evt(raw_buf):
|
| 157 | data = struct.unpack_from("<BBH", raw_buf)
|
| 158 | typ = data[0] & 0x1f
|
| 159 | ip_flag = (data[0] & 0x80) >> 7
|
| 160 | vector = data[1]
|
| 161 | evd_cnt = data[2]
|
| 162 | s = glb_cfe[typ]
|
| 163 | if s:
|
| 164 | print(" cfe: %s IP: %u vector: %u" % (s, ip_flag, vector), end=' ')
|
| 165 | else:
|
| 166 | print(" cfe: %u IP: %u vector: %u" % (typ, ip_flag, vector), end=' ')
|
| 167 | pos = 4
|
| 168 | for i in range(evd_cnt):
|
| 169 | data = struct.unpack_from("<QQ", raw_buf)
|
| 170 | et = data[0] & 0x3f
|
| 171 | s = glb_evd[et]
|
| 172 | if s:
|
| 173 | print("%s: %#x" % (s, data[1]), end=' ')
|
| 174 | else:
|
| 175 | print("EVD_%u: %#x" % (et, data[1]), end=' ')
|
| 176 |
|
| 177 | def print_iflag(raw_buf):
|
| 178 | data = struct.unpack_from("<IQ", raw_buf)
|
| 179 | iflag = data[0] & 1
|
| 180 | old_iflag = iflag ^ 1
|
| 181 | via_branch = data[0] & 2
|
| 182 | branch_ip = data[1]
|
| 183 | if via_branch:
|
| 184 | s = "via"
|
| 185 | else:
|
| 186 | s = "non"
|
| 187 | print("IFLAG: %u->%u %s branch" % (old_iflag, iflag, s), end=' ')
|
| 188 |
|
| 189 | def common_start_str(comm, sample):
|
| 190 | ts = sample["time"]
|
| 191 | cpu = sample["cpu"]
|
| 192 | pid = sample["pid"]
|
| 193 | tid = sample["tid"]
|
| 194 | return "%16s %5u/%-5u [%03u] %9u.%09u " % (comm, pid, tid, cpu, ts / 1000000000, ts %1000000000)
|
| 195 |
|
| 196 | def print_common_start(comm, sample, name):
|
| 197 | flags_disp = get_optional_null(sample, "flags_disp")
|
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | print(common_start_str(comm, sample) + "%8s %21s" % (name, flags_disp), end=' ')
|
| 205 |
|
| 206 | def print_instructions_start(comm, sample):
|
| 207 | if "x" in get_optional_null(sample, "flags"):
|
| 208 | print(common_start_str(comm, sample) + "x", end=' ')
|
| 209 | else:
|
| 210 | print(common_start_str(comm, sample), end=' ')
|
| 211 |
|
| 212 | def disassem(insn, ip):
|
| 213 | inst = glb_disassembler.Instruction()
|
| 214 | glb_disassembler.SetMode(inst, 0) |
| 215 | buf = create_string_buffer(64)
|
| 216 | buf.value = insn
|
| 217 | return glb_disassembler.DisassembleOne(inst, addressof(buf), len(insn), ip)
|
| 218 |
|
| 219 | def print_common_ip(param_dict, sample, symbol, dso):
|
| 220 | ip = sample["ip"]
|
| 221 | offs = get_offset(param_dict, "symoff")
|
| 222 | if "cyc_cnt" in sample:
|
| 223 | cyc_cnt = sample["cyc_cnt"]
|
| 224 | insn_cnt = get_optional_zero(sample, "insn_cnt")
|
| 225 | ipc_str = " IPC: %#.2f (%u/%u)" % (insn_cnt / cyc_cnt, insn_cnt, cyc_cnt)
|
| 226 | else:
|
| 227 | ipc_str = ""
|
| 228 | if glb_insn and glb_disassembler is not None:
|
| 229 | insn = perf_sample_insn(perf_script_context)
|
| 230 | if insn and len(insn):
|
| 231 | cnt, text = disassem(insn, ip)
|
| 232 | byte_str = ("%x" % ip).rjust(16)
|
| 233 | if sys.version_info.major >= 3:
|
| 234 | for k in range(cnt):
|
| 235 | byte_str += " %02x" % insn[k]
|
| 236 | else:
|
| 237 | for k in xrange(cnt):
|
| 238 | byte_str += " %02x" % ord(insn[k])
|
| 239 | print("%-40s %-30s" % (byte_str, text), end=' ')
|
| 240 | print("%s%s (%s)" % (symbol, offs, dso), end=' ')
|
| 241 | else:
|
| 242 | print("%16x %s%s (%s)" % (ip, symbol, offs, dso), end=' ')
|
| 243 | if "addr_correlates_sym" in sample:
|
| 244 | addr = sample["addr"]
|
| 245 | dso = get_optional(sample, "addr_dso")
|
| 246 | symbol = get_optional(sample, "addr_symbol")
|
| 247 | offs = get_offset(sample, "addr_symoff")
|
| 248 | print("=> %x %s%s (%s)%s" % (addr, symbol, offs, dso, ipc_str))
|
| 249 | else:
|
| 250 | print(ipc_str)
|
| 251 |
|
| 252 | def print_srccode(comm, param_dict, sample, symbol, dso, with_insn):
|
| 253 | ip = sample["ip"]
|
| 254 | if symbol == "[unknown]":
|
| 255 | start_str = common_start_str(comm, sample) + ("%x" % ip).rjust(16).ljust(40)
|
| 256 | else:
|
| 257 | offs = get_offset(param_dict, "symoff")
|
| 258 | start_str = common_start_str(comm, sample) + (symbol + offs).ljust(40)
|
| 259 |
|
| 260 | if with_insn and glb_insn and glb_disassembler is not None:
|
| 261 | insn = perf_sample_insn(perf_script_context)
|
| 262 | if insn and len(insn):
|
| 263 | cnt, text = disassem(insn, ip)
|
| 264 | start_str += text.ljust(30)
|
| 265 |
|
| 266 | global glb_source_file_name
|
| 267 | global glb_line_number
|
| 268 | global glb_dso
|
| 269 |
|
| 270 | source_file_name, line_number, source_line = perf_sample_srccode(perf_script_context)
|
| 271 | if source_file_name:
|
| 272 | if glb_line_number == line_number and glb_source_file_name == source_file_name:
|
| 273 | src_str = ""
|
| 274 | else:
|
| 275 | if len(source_file_name) > 40:
|
| 276 | src_file = ("..." + source_file_name[-37:]) + " "
|
| 277 | else:
|
| 278 | src_file = source_file_name.ljust(41)
|
| 279 | if source_line is None:
|
| 280 | src_str = src_file + str(line_number).rjust(4) + " <source not found>"
|
| 281 | else:
|
| 282 | src_str = src_file + str(line_number).rjust(4) + " " + source_line
|
| 283 | glb_dso = None
|
| 284 | elif dso == glb_dso:
|
| 285 | src_str = ""
|
| 286 | else:
|
| 287 | src_str = dso
|
| 288 | glb_dso = dso
|
| 289 |
|
| 290 | glb_line_number = line_number
|
| 291 | glb_source_file_name = source_file_name
|
| 292 |
|
| 293 | print(start_str, src_str)
|
| 294 |
|
| 295 | def do_process_event(param_dict):
|
| 296 | event_attr = param_dict["attr"]
|
| 297 | sample = param_dict["sample"]
|
| 298 | raw_buf = param_dict["raw_buf"]
|
| 299 | comm = param_dict["comm"]
|
| 300 | name = param_dict["ev_name"]
|
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 |
|
| 306 | |
| 307 | dso = get_optional(param_dict, "dso")
|
| 308 | symbol = get_optional(param_dict, "symbol")
|
| 309 |
|
| 310 | cpu = sample["cpu"]
|
| 311 | if cpu in glb_switch_str:
|
| 312 | print(glb_switch_str[cpu])
|
| 313 | del glb_switch_str[cpu]
|
| 314 |
|
| 315 | if name[0:12] == "instructions":
|
| 316 | if glb_src:
|
| 317 | print_srccode(comm, param_dict, sample, symbol, dso, True)
|
| 318 | else:
|
| 319 | print_instructions_start(comm, sample)
|
| 320 | print_common_ip(param_dict, sample, symbol, dso)
|
| 321 | elif name[0:8] == "branches":
|
| 322 | if glb_src:
|
| 323 | print_srccode(comm, param_dict, sample, symbol, dso, False)
|
| 324 | else:
|
| 325 | print_common_start(comm, sample, name)
|
| 326 | print_common_ip(param_dict, sample, symbol, dso)
|
| 327 | elif name == "ptwrite":
|
| 328 | print_common_start(comm, sample, name)
|
| 329 | print_ptwrite(raw_buf)
|
| 330 | print_common_ip(param_dict, sample, symbol, dso)
|
| 331 | elif name == "cbr":
|
| 332 | print_common_start(comm, sample, name)
|
| 333 | print_cbr(raw_buf)
|
| 334 | print_common_ip(param_dict, sample, symbol, dso)
|
| 335 | elif name == "mwait":
|
| 336 | print_common_start(comm, sample, name)
|
| 337 | print_mwait(raw_buf)
|
| 338 | print_common_ip(param_dict, sample, symbol, dso)
|
| 339 | elif name == "pwre":
|
| 340 | print_common_start(comm, sample, name)
|
| 341 | print_pwre(raw_buf)
|
| 342 | print_common_ip(param_dict, sample, symbol, dso)
|
| 343 | elif name == "exstop":
|
| 344 | print_common_start(comm, sample, name)
|
| 345 | print_exstop(raw_buf)
|
| 346 | print_common_ip(param_dict, sample, symbol, dso)
|
| 347 | elif name == "pwrx":
|
| 348 | print_common_start(comm, sample, name)
|
| 349 | print_pwrx(raw_buf)
|
| 350 | print_common_ip(param_dict, sample, symbol, dso)
|
| 351 | elif name == "psb":
|
| 352 | print_common_start(comm, sample, name)
|
| 353 | print_psb(raw_buf)
|
| 354 | print_common_ip(param_dict, sample, symbol, dso)
|
| 355 | elif name == "evt":
|
| 356 | print_common_start(comm, sample, name)
|
| 357 | print_evt(raw_buf)
|
| 358 | print_common_ip(param_dict, sample, symbol, dso)
|
| 359 | elif name == "iflag":
|
| 360 | print_common_start(comm, sample, name)
|
| 361 | print_iflag(raw_buf)
|
| 362 | print_common_ip(param_dict, sample, symbol, dso)
|
| 363 | else:
|
| 364 | print_common_start(comm, sample, name)
|
| 365 | print_common_ip(param_dict, sample, symbol, dso)
|
| 366 |
|
| 367 | def process_event(param_dict):
|
| 368 | try:
|
| 369 | do_process_event(param_dict)
|
| 370 | except broken_pipe_exception:
|
| 371 | |
| 372 | sys.stdout = open(os.devnull, 'w')
|
| 373 | sys.exit(1)
|
| 374 |
|
| 375 | def auxtrace_error(typ, code, cpu, pid, tid, ip, ts, msg, cpumode, *x):
|
| 376 | try:
|
| 377 | print("%16s %5u/%-5u [%03u] %9u.%09u error type %u code %u: %s ip 0x%16x" %
|
| 378 | ("Trace error", pid, tid, cpu, ts / 1000000000, ts %1000000000, typ, code, msg, ip))
|
| 379 | except broken_pipe_exception:
|
| 380 | |
| 381 | sys.stdout = open(os.devnull, 'w')
|
| 382 | sys.exit(1)
|
| 383 |
|
| 384 | def context_switch(ts, cpu, pid, tid, np_pid, np_tid, machine_pid, out, out_preempt, *x):
|
| 385 | if out:
|
| 386 | out_str = "Switch out "
|
| 387 | else:
|
| 388 | out_str = "Switch In "
|
| 389 | if out_preempt:
|
| 390 | preempt_str = "preempt"
|
| 391 | else:
|
| 392 | preempt_str = ""
|
| 393 | if machine_pid == -1:
|
| 394 | machine_str = ""
|
| 395 | else:
|
| 396 | machine_str = "machine PID %d" % machine_pid
|
| 397 | switch_str = "%16s %5d/%-5d [%03u] %9u.%09u %5d/%-5d %s %s" % \
|
| 398 | (out_str, pid, tid, cpu, ts / 1000000000, ts %1000000000, np_pid, np_tid, machine_str, preempt_str)
|
| 399 | if glb_args.all_switch_events:
|
| 400 | print(switch_str);
|
| 401 | else:
|
| 402 | global glb_switch_str
|
| 403 | glb_switch_str[cpu] = switch_str
|