Merge tag 'ntb-5.11' of git://github.com/jonmason/ntb
[linux/fpc-iii.git] / tools / perf / scripts / python / Perf-Trace-Util / lib / Perf / Trace / Util.py
blob7384dcb628c4326c3d98dca18639b8439fc5c8ac
1 # Util.py - Python extension for perf script, miscellaneous utility code
3 # Copyright (C) 2010 by Tom Zanussi <tzanussi@gmail.com>
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.
8 from __future__ import print_function
10 import errno, os
12 FUTEX_WAIT = 0
13 FUTEX_WAKE = 1
14 FUTEX_PRIVATE_FLAG = 128
15 FUTEX_CLOCK_REALTIME = 256
16 FUTEX_CMD_MASK = ~(FUTEX_PRIVATE_FLAG | FUTEX_CLOCK_REALTIME)
18 NSECS_PER_SEC = 1000000000
20 def avg(total, n):
21 return total / n
23 def nsecs(secs, nsecs):
24 return secs * NSECS_PER_SEC + nsecs
26 def nsecs_secs(nsecs):
27 return nsecs / NSECS_PER_SEC
29 def nsecs_nsecs(nsecs):
30 return nsecs % NSECS_PER_SEC
32 def nsecs_str(nsecs):
33 str = "%5u.%09u" % (nsecs_secs(nsecs), nsecs_nsecs(nsecs)),
34 return str
36 def 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)
48 def clear_term():
49 print("\x1b[H\x1b[2J")
51 audit_package_warned = False
53 try:
54 import audit
55 machine_to_id = {
56 'x86_64': audit.MACH_86_64,
57 'alpha' : audit.MACH_ALPHA,
58 'ia64' : audit.MACH_IA64,
59 'ppc' : audit.MACH_PPC,
60 'ppc64' : audit.MACH_PPC64,
61 'ppc64le' : audit.MACH_PPC64LE,
62 's390' : audit.MACH_S390,
63 's390x' : audit.MACH_S390X,
64 'i386' : audit.MACH_X86,
65 'i586' : audit.MACH_X86,
66 'i686' : audit.MACH_X86,
68 try:
69 machine_to_id['armeb'] = audit.MACH_ARMEB
70 except:
71 pass
72 machine_id = machine_to_id[os.uname()[4]]
73 except:
74 if not audit_package_warned:
75 audit_package_warned = True
76 print("Install the audit-libs-python package to get syscall names.\n"
77 "For example:\n # apt-get install python-audit (Ubuntu)"
78 "\n # yum install audit-libs-python (Fedora)"
79 "\n etc.\n")
81 def syscall_name(id):
82 try:
83 return audit.audit_syscall_to_name(id, machine_id)
84 except:
85 return str(id)
87 def strerror(nr):
88 try:
89 return errno.errorcode[abs(nr)]
90 except:
91 return "Unknown %d errno" % nr