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