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