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