1# SPDX-License-Identifier: GPL-2.0+
2#
3# Copyright (C) 2018 Ravi Bangoria, IBM Corporation
4#
5# Hypervisor call statisics
6
7from __future__ import print_function
8
9import os
10import sys
11
12sys.path.append(os.environ['PERF_EXEC_PATH'] + \
13	'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
14
15from perf_trace_context import *
16from Core import *
17from Util import *
18
19# output: {
20#	opcode: {
21#		'min': minimum time nsec
22#		'max': maximum time nsec
23#		'time': average time nsec
24#		'cnt': counter
25#	} ...
26# }
27output = {}
28
29# d_enter: {
30#	cpu: {
31#		opcode: nsec
32#	} ...
33# }
34d_enter = {}
35
36hcall_table = {
37	4: 'H_REMOVE',
38	8: 'H_ENTER',
39	12: 'H_READ',
40	16: 'H_CLEAR_MOD',
41	20: 'H_CLEAR_REF',
42	24: 'H_PROTECT',
43	28: 'H_GET_TCE',
44	32: 'H_PUT_TCE',
45	36: 'H_SET_SPRG0',
46	40: 'H_SET_DABR',
47	44: 'H_PAGE_INIT',
48	48: 'H_SET_ASR',
49	52: 'H_ASR_ON',
50	56: 'H_ASR_OFF',
51	60: 'H_LOGICAL_CI_LOAD',
52	64: 'H_LOGICAL_CI_STORE',
53	68: 'H_LOGICAL_CACHE_LOAD',
54	72: 'H_LOGICAL_CACHE_STORE',
55	76: 'H_LOGICAL_ICBI',
56	80: 'H_LOGICAL_DCBF',
57	84: 'H_GET_TERM_CHAR',
58	88: 'H_PUT_TERM_CHAR',
59	92: 'H_REAL_TO_LOGICAL',
60	96: 'H_HYPERVISOR_DATA',
61	100: 'H_EOI',
62	104: 'H_CPPR',
63	108: 'H_IPI',
64	112: 'H_IPOLL',
65	116: 'H_XIRR',
66	120: 'H_MIGRATE_DMA',
67	124: 'H_PERFMON',
68	220: 'H_REGISTER_VPA',
69	224: 'H_CEDE',
70	228: 'H_CONFER',
71	232: 'H_PROD',
72	236: 'H_GET_PPP',
73	240: 'H_SET_PPP',
74	244: 'H_PURR',
75	248: 'H_PIC',
76	252: 'H_REG_CRQ',
77	256: 'H_FREE_CRQ',
78	260: 'H_VIO_SIGNAL',
79	264: 'H_SEND_CRQ',
80	272: 'H_COPY_RDMA',
81	276: 'H_REGISTER_LOGICAL_LAN',
82	280: 'H_FREE_LOGICAL_LAN',
83	284: 'H_ADD_LOGICAL_LAN_BUFFER',
84	288: 'H_SEND_LOGICAL_LAN',
85	292: 'H_BULK_REMOVE',
86	304: 'H_MULTICAST_CTRL',
87	308: 'H_SET_XDABR',
88	312: 'H_STUFF_TCE',
89	316: 'H_PUT_TCE_INDIRECT',
90	332: 'H_CHANGE_LOGICAL_LAN_MAC',
91	336: 'H_VTERM_PARTNER_INFO',
92	340: 'H_REGISTER_VTERM',
93	344: 'H_FREE_VTERM',
94	348: 'H_RESET_EVENTS',
95	352: 'H_ALLOC_RESOURCE',
96	356: 'H_FREE_RESOURCE',
97	360: 'H_MODIFY_QP',
98	364: 'H_QUERY_QP',
99	368: 'H_REREGISTER_PMR',
100	372: 'H_REGISTER_SMR',
101	376: 'H_QUERY_MR',
102	380: 'H_QUERY_MW',
103	384: 'H_QUERY_HCA',
104	388: 'H_QUERY_PORT',
105	392: 'H_MODIFY_PORT',
106	396: 'H_DEFINE_AQP1',
107	400: 'H_GET_TRACE_BUFFER',
108	404: 'H_DEFINE_AQP0',
109	408: 'H_RESIZE_MR',
110	412: 'H_ATTACH_MCQP',
111	416: 'H_DETACH_MCQP',
112	420: 'H_CREATE_RPT',
113	424: 'H_REMOVE_RPT',
114	428: 'H_REGISTER_RPAGES',
115	432: 'H_DISABLE_AND_GETC',
116	436: 'H_ERROR_DATA',
117	440: 'H_GET_HCA_INFO',
118	444: 'H_GET_PERF_COUNT',
119	448: 'H_MANAGE_TRACE',
120	468: 'H_FREE_LOGICAL_LAN_BUFFER',
121	472: 'H_POLL_PENDING',
122	484: 'H_QUERY_INT_STATE',
123	580: 'H_ILLAN_ATTRIBUTES',
124	592: 'H_MODIFY_HEA_QP',
125	596: 'H_QUERY_HEA_QP',
126	600: 'H_QUERY_HEA',
127	604: 'H_QUERY_HEA_PORT',
128	608: 'H_MODIFY_HEA_PORT',
129	612: 'H_REG_BCMC',
130	616: 'H_DEREG_BCMC',
131	620: 'H_REGISTER_HEA_RPAGES',
132	624: 'H_DISABLE_AND_GET_HEA',
133	628: 'H_GET_HEA_INFO',
134	632: 'H_ALLOC_HEA_RESOURCE',
135	644: 'H_ADD_CONN',
136	648: 'H_DEL_CONN',
137	664: 'H_JOIN',
138	676: 'H_VASI_STATE',
139	688: 'H_ENABLE_CRQ',
140	696: 'H_GET_EM_PARMS',
141	720: 'H_SET_MPP',
142	724: 'H_GET_MPP',
143	748: 'H_HOME_NODE_ASSOCIATIVITY',
144	756: 'H_BEST_ENERGY',
145	764: 'H_XIRR_X',
146	768: 'H_RANDOM',
147	772: 'H_COP',
148	788: 'H_GET_MPP_X',
149	796: 'H_SET_MODE',
150	61440: 'H_RTAS',
151}
152
153def hcall_table_lookup(opcode):
154	if (opcode in hcall_table):
155		return hcall_table[opcode]
156	else:
157		return opcode
158
159print_ptrn = '%-28s%10s%10s%10s%10s'
160
161def trace_end():
162	print(print_ptrn % ('hcall', 'count', 'min(ns)', 'max(ns)', 'avg(ns)'))
163	print('-' * 68)
164	for opcode in output:
165		h_name = hcall_table_lookup(opcode)
166		time = output[opcode]['time']
167		cnt = output[opcode]['cnt']
168		min_t = output[opcode]['min']
169		max_t = output[opcode]['max']
170
171		print(print_ptrn % (h_name, cnt, min_t, max_t, time//cnt))
172
173def powerpc__hcall_exit(name, context, cpu, sec, nsec, pid, comm, callchain,
174			opcode, retval):
175	if (cpu in d_enter and opcode in d_enter[cpu]):
176		diff = nsecs(sec, nsec) - d_enter[cpu][opcode]
177
178		if (opcode in output):
179			output[opcode]['time'] += diff
180			output[opcode]['cnt'] += 1
181			if (output[opcode]['min'] > diff):
182				output[opcode]['min'] = diff
183			if (output[opcode]['max'] < diff):
184				output[opcode]['max'] = diff
185		else:
186			output[opcode] = {
187				'time': diff,
188				'cnt': 1,
189				'min': diff,
190				'max': diff,
191			}
192
193		del d_enter[cpu][opcode]
194#	else:
195#		print("Can't find matching hcall_enter event. Ignoring sample")
196
197def powerpc__hcall_entry(event_name, context, cpu, sec, nsec, pid, comm,
198			 callchain, opcode):
199		if (cpu in d_enter):
200			d_enter[cpu][opcode] = nsecs(sec, nsec)
201		else:
202			d_enter[cpu] = {opcode: nsecs(sec, nsec)}
203