xref: /linux/tools/perf/scripts/python/Perf-Trace-Util/lib/Perf/Trace/Util.py (revision b58b13f156c00c2457035b7071eaaac105fe6836)
1# Util.py - Python extension for perf script, miscellaneous utility code
2#
3# Copyright (C) 2010 by Tom Zanussi <tzanussi@gmail.com>
4#
5# This software may be distributed under the terms of the GNU General
6# Public License ("GPL") version 2 as published by the Free Software
7# Foundation.
8from __future__ import print_function
9
10import errno, os
11
12FUTEX_WAIT = 0
13FUTEX_WAKE = 1
14FUTEX_PRIVATE_FLAG = 128
15FUTEX_CLOCK_REALTIME = 256
16FUTEX_CMD_MASK = ~(FUTEX_PRIVATE_FLAG | FUTEX_CLOCK_REALTIME)
17
18NSECS_PER_SEC    = 1000000000
19
20def avg(total, n):
21    return total / n
22
23def nsecs(secs, nsecs):
24    return secs * NSECS_PER_SEC + nsecs
25
26def nsecs_secs(nsecs):
27    return nsecs / NSECS_PER_SEC
28
29def nsecs_nsecs(nsecs):
30    return nsecs % NSECS_PER_SEC
31
32def nsecs_str(nsecs):
33    str = "%5u.%09u" % (nsecs_secs(nsecs), nsecs_nsecs(nsecs)),
34    return str
35
36def add_stats(dict, key, value):
37	if key not in dict:
38		dict[key] = (value, value, value, 1)
39	else:
40		min, max, avg, count = dict[key]
41		if value < min:
42			min = value
43		if value > max:
44			max = value
45		avg = (avg + value) / 2
46		dict[key] = (min, max, avg, count + 1)
47
48def clear_term():
49    print("\x1b[H\x1b[2J")
50
51audit_package_warned = False
52
53try:
54	import audit
55	machine_to_id = {
56		'x86_64': audit.MACH_86_64,
57		'aarch64': audit.MACH_AARCH64,
58		'alpha'	: audit.MACH_ALPHA,
59		'ia64'	: audit.MACH_IA64,
60		'ppc'	: audit.MACH_PPC,
61		'ppc64'	: audit.MACH_PPC64,
62		'ppc64le' : audit.MACH_PPC64LE,
63		's390'	: audit.MACH_S390,
64		's390x'	: audit.MACH_S390X,
65		'i386'	: audit.MACH_X86,
66		'i586'	: audit.MACH_X86,
67		'i686'	: audit.MACH_X86,
68	}
69	try:
70		machine_to_id['armeb'] = audit.MACH_ARMEB
71	except:
72		pass
73	machine_id = machine_to_id[os.uname()[4]]
74except:
75	if not audit_package_warned:
76		audit_package_warned = True
77		print("Install the python-audit package to get syscall names.\n"
78                    "For example:\n  # apt-get install python3-audit (Ubuntu)"
79                    "\n  # yum install python3-audit (Fedora)"
80                    "\n  etc.\n")
81
82def syscall_name(id):
83	try:
84		return audit.audit_syscall_to_name(id, machine_id)
85	except:
86		return str(id)
87
88def strerror(nr):
89	try:
90		return errno.errorcode[abs(nr)]
91	except:
92		return "Unknown %d errno" % nr
93