History

perf scripting python: intel-pt-events.py: Add --insn-trace and --src-trace

Add an instruction trace and a source trace to the intel-pt-events.py script.

Adrian Hunter committed 5 years ago

Commit a483e641 parent 2b873862 files · +163 −19Browse files

Showing the changes to tools/perf/scripts/python (1 of 2 files). Show all

Unified · Split

tools/perf/scripts/python/intel-pt-events.py+158−18
@@ -16,21 +16,30 @@ from __future__ import print_function · 15 unmodified lines
1616 import os
1717 import sys
1818 import struct
19+import argparse
20+
21+from libxed import LibXED
22+from ctypes import create_string_buffer, addressof
1923
2024 sys.path.append(os.environ['PERF_EXEC_PATH'] + \
2125 '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
2226
23−# These perf imports are not used at present
24−#from perf_trace_context import *
25−#from Core import *
27+from perf_trace_context import perf_set_itrace_options, \
28+ perf_sample_insn, perf_sample_srccode
2629
2730 try:
2831 broken_pipe_exception = BrokenPipeError
2932 except:
3033 broken_pipe_exception = IOError
3134
32−glb_switch_str = None
33−glb_switch_printed = True
35+glb_switch_str = None
36+glb_switch_printed = True
37+glb_insn = False
38+glb_disassembler = None
39+glb_src = False
40+glb_source_file_name = None
41+glb_line_number = None
42+glb_dso = None
3443
3544 def get_optional_null(perf_dict, field):
3645 if field in perf_dict:
@@ -42,6 +51,11 @@ def get_optional_zero(perf_dict, field): · 5 unmodified lines
4251 return perf_dict[field]
4352 return 0
4453
54+def get_optional_bytes(perf_dict, field):
55+ if field in perf_dict:
56+ return perf_dict[field]
57+ return bytes()
58+
4559 def get_optional(perf_dict, field):
4660 if field in perf_dict:
4761 return perf_dict[field]
@@ -53,7 +67,31 @@ def get_offset(perf_dict, field): · 5 unmodified lines
5367 return ""
5468
5569 def trace_begin():
56− print("Intel PT Branch Trace, Power Events and PTWRITE")
70+ ap = argparse.ArgumentParser(usage = "", add_help = False)
71+ ap.add_argument("--insn-trace", action='store_true')
72+ ap.add_argument("--src-trace", 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 = "i0nsepwx"
80+ glb_insn = True
81+ elif glb_args.src_trace:
82+ print("Intel PT Source Trace")
83+ itrace = "i0nsepwx"
84+ glb_insn = True
85+ glb_src = True
86+ else:
87+ print("Intel PT Branch Trace, Power Events and PTWRITE")
88+ itrace = "bepwx"
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)
5795
5896 def trace_end():
5997 print("End")
@@ -111,11 +149,14 @@ def print_psb(raw_buf): · 51 unmodified lines
111149 offset = data[1]
112150 print("offset: %#x" % (offset), end=' ')
113151
114−def print_common_start(comm, sample, name):
152+def common_start_str(comm, sample):
115153 ts = sample["time"]
116154 cpu = sample["cpu"]
117155 pid = sample["pid"]
118156 tid = sample["tid"]
157+ return "%16s %5u/%-5u [%03u] %9u.%09u " % (comm, pid, tid, cpu, ts / 1000000000, ts %1000000000)
158+
159+def print_common_start(comm, sample, name):
119160 flags_disp = get_optional_null(sample, "flags_disp")
120161 # Unused fields:
121162 # period = sample["period"]
@@ -123,22 +164,96 @@ def print_common_start(comm, sample, name): · 1 unmodified line
123164 # weight = sample["weight"]
124165 # transaction = sample["transaction"]
125166 # cpumode = get_optional_zero(sample, "cpumode")
126− print("%16s %5u/%-5u [%03u] %9u.%09u %7s %19s" %
127− (comm, pid, tid, cpu, ts / 1000000000, ts %1000000000, name, flags_disp),
128− end=' ')
167+ print(common_start_str(comm, sample) + "%7s %19s" % (name, flags_disp), end=' ')
168+
169+def print_instructions_start(comm, sample):
170+ if "x" in get_optional_null(sample, "flags"):
171+ print(common_start_str(comm, sample) + "x", end=' ')
172+ else:
173+ print(common_start_str(comm, sample), end=' ')
174+
175+def disassem(insn, ip):
176+ inst = glb_disassembler.Instruction()
177+ glb_disassembler.SetMode(inst, 0) # Assume 64-bit
178+ buf = create_string_buffer(64)
179+ buf.value = insn
180+ return glb_disassembler.DisassembleOne(inst, addressof(buf), len(insn), ip)
129181
130182 def print_common_ip(param_dict, sample, symbol, dso):
131183 ip = sample["ip"]
132184 offs = get_offset(param_dict, "symoff")
133− print("%16x %s%s (%s)" % (ip, symbol, offs, dso), end=' ')
185+ if "cyc_cnt" in sample:
186+ cyc_cnt = sample["cyc_cnt"]
187+ insn_cnt = get_optional_zero(sample, "insn_cnt")
188+ ipc_str = " IPC: %#.2f (%u/%u)" % (insn_cnt / cyc_cnt, insn_cnt, cyc_cnt)
189+ else:
190+ ipc_str = ""
191+ if glb_insn and glb_disassembler is not None:
192+ insn = perf_sample_insn(perf_script_context)
193+ if insn and len(insn):
194+ cnt, text = disassem(insn, ip)
195+ byte_str = ("%x" % ip).rjust(16)
196+ if sys.version_info.major >= 3:
197+ for k in range(cnt):
198+ byte_str += " %02x" % insn[k]
199+ else:
200+ for k in xrange(cnt):
201+ byte_str += " %02x" % ord(insn[k])
202+ print("%-40s %-30s" % (byte_str, text), end=' ')
203+ print("%s%s (%s)" % (symbol, offs, dso), end=' ')
204+ else:
205+ print("%16x %s%s (%s)" % (ip, symbol, offs, dso), end=' ')
134206 if "addr_correlates_sym" in sample:
135207 addr = sample["addr"]
136208 dso = get_optional(sample, "addr_dso")
137209 symbol = get_optional(sample, "addr_symbol")
138210 offs = get_offset(sample, "addr_symoff")
139− print("=> %x %s%s (%s)" % (addr, symbol, offs, dso))
211+ print("=> %x %s%s (%s)%s" % (addr, symbol, offs, dso, ipc_str))
212+ else:
213+ print(ipc_str)
214+
215+def print_srccode(comm, param_dict, sample, symbol, dso, with_insn):
216+ ip = sample["ip"]
217+ if symbol == "[unknown]":
218+ start_str = common_start_str(comm, sample) + ("%x" % ip).rjust(16).ljust(40)
140219 else:
141− print()
220+ offs = get_offset(param_dict, "symoff")
221+ start_str = common_start_str(comm, sample) + (symbol + offs).ljust(40)
222+
223+ if with_insn and glb_insn and glb_disassembler is not None:
224+ insn = perf_sample_insn(perf_script_context)
225+ if insn and len(insn):
226+ cnt, text = disassem(insn, ip)
227+ start_str += text.ljust(30)
228+
229+ global glb_source_file_name
230+ global glb_line_number
231+ global glb_dso
232+
233+ source_file_name, line_number, source_line = perf_sample_srccode(perf_script_context)
234+ if source_file_name:
235+ if glb_line_number == line_number and glb_source_file_name == source_file_name:
236+ src_str = ""
237+ else:
238+ if len(source_file_name) > 40:
239+ src_file = ("..." + source_file_name[-37:]) + " "
240+ else:
241+ src_file = source_file_name.ljust(41)
242+ if source_line is None:
243+ src_str = src_file + str(line_number).rjust(4) + " <source not found>"
244+ else:
245+ src_str = src_file + str(line_number).rjust(4) + " " + source_line
246+ glb_dso = None
247+ elif dso == glb_dso:
248+ src_str = ""
249+ else:
250+ src_str = dso
251+ glb_dso = dso
252+
253+ glb_line_number = line_number
254+ glb_source_file_name = source_file_name
255+
256+ print(start_str, src_str)
142257
143258 def do_process_event(param_dict):
144259 global glb_switch_printed
@@ -159,24 +274,49 @@ def do_process_event(param_dict): · 14 unmodified lines
159274 dso = get_optional(param_dict, "dso")
160275 symbol = get_optional(param_dict, "symbol")
161276
162− print_common_start(comm, sample, name)
163−
164− if name == "ptwrite":
277+ if name[0:12] == "instructions":
278+ if glb_src:
279+ print_srccode(comm, param_dict, sample, symbol, dso, True)
280+ else:
281+ print_instructions_start(comm, sample)
282+ print_common_ip(param_dict, sample, symbol, dso)
283+ elif name[0:8] == "branches":
284+ if glb_src:
285+ print_srccode(comm, param_dict, sample, symbol, dso, False)
286+ else:
287+ print_common_start(comm, sample, name)
288+ print_common_ip(param_dict, sample, symbol, dso)
289+ elif name == "ptwrite":
290+ print_common_start(comm, sample, name)
165291 print_ptwrite(raw_buf)
292+ print_common_ip(param_dict, sample, symbol, dso)
166293 elif name == "cbr":
294+ print_common_start(comm, sample, name)
167295 print_cbr(raw_buf)
296+ print_common_ip(param_dict, sample, symbol, dso)
168297 elif name == "mwait":
298+ print_common_start(comm, sample, name)
169299 print_mwait(raw_buf)
300+ print_common_ip(param_dict, sample, symbol, dso)
170301 elif name == "pwre":
302+ print_common_start(comm, sample, name)
171303 print_pwre(raw_buf)
304+ print_common_ip(param_dict, sample, symbol, dso)
172305 elif name == "exstop":
306+ print_common_start(comm, sample, name)
173307 print_exstop(raw_buf)
308+ print_common_ip(param_dict, sample, symbol, dso)
174309 elif name == "pwrx":
310+ print_common_start(comm, sample, name)
175311 print_pwrx(raw_buf)
312+ print_common_ip(param_dict, sample, symbol, dso)
176313 elif name == "psb":
314+ print_common_start(comm, sample, name)
177315 print_psb(raw_buf)
178−
179− print_common_ip(param_dict, sample, symbol, dso)
316+ print_common_ip(param_dict, sample, symbol, dso)
317+ else:
318+ print_common_start(comm, sample, name)
319+ print_common_ip(param_dict, sample, symbol, dso)
180320
181321 def process_event(param_dict):
182322 try:

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t