Linux Perf
Util.py
Go to the documentation of this file.
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.
8 
9 import errno, os
10 
11 FUTEX_WAIT = 0
12 FUTEX_WAKE = 1
13 FUTEX_PRIVATE_FLAG = 128
14 FUTEX_CLOCK_REALTIME = 256
15 FUTEX_CMD_MASK = ~(FUTEX_PRIVATE_FLAG | FUTEX_CLOCK_REALTIME)
16 
17 NSECS_PER_SEC = 1000000000
18 
19 def avg(total, n):
20  return total / n
21 
22 def nsecs(secs, nsecs):
23  return secs * NSECS_PER_SEC + nsecs
24 
25 def nsecs_secs(nsecs):
26  return nsecs / NSECS_PER_SEC
27 
28 def nsecs_nsecs(nsecs):
29  return nsecs % NSECS_PER_SEC
30 
31 def nsecs_str(nsecs):
32  str = "%5u.%09u" % (nsecs_secs(nsecs), nsecs_nsecs(nsecs)),
33  return str
34 
35 def add_stats(dict, key, value):
36  if not dict.has_key(key):
37  dict[key] = (value, value, value, 1)
38  else:
39  min, max, avg, count = dict[key]
40  if value < min:
41  min = value
42  if value > max:
43  max = value
44  avg = (avg + value) / 2
45  dict[key] = (min, max, avg, count + 1)
46 
47 def clear_term():
48  print("\x1b[H\x1b[2J")
49 
50 audit_package_warned = False
51 
52 try:
53  import audit
54  machine_to_id = {
55  'x86_64': audit.MACH_86_64,
56  'alpha' : audit.MACH_ALPHA,
57  'ia64' : audit.MACH_IA64,
58  'ppc' : audit.MACH_PPC,
59  'ppc64' : audit.MACH_PPC64,
60  'ppc64le' : audit.MACH_PPC64LE,
61  's390' : audit.MACH_S390,
62  's390x' : audit.MACH_S390X,
63  'i386' : audit.MACH_X86,
64  'i586' : audit.MACH_X86,
65  'i686' : audit.MACH_X86,
66  }
67  try:
68  machine_to_id['armeb'] = audit.MACH_ARMEB
69  except:
70  pass
71  machine_id = machine_to_id[os.uname()[4]]
72 except:
73  if not audit_package_warned:
74  audit_package_warned = True
75  print "Install the audit-libs-python package to get syscall names.\n" \
76  "For example:\n # apt-get install python-audit (Ubuntu)" \
77  "\n # yum install audit-libs-python (Fedora)" \
78  "\n etc.\n"
79 
80 def syscall_name(id):
81  try:
82  return audit.audit_syscall_to_name(id, machine_id)
83  except:
84  return str(id)
85 
86 def strerror(nr):
87  try:
88  return errno.errorcode[abs(nr)]
89  except:
90  return "Unknown %d errno" % nr
def avg(total, n)
Definition: Util.py:19
def nsecs(secs, nsecs)
Definition: Util.py:22
def strerror(nr)
Definition: Util.py:86
def clear_term()
Definition: Util.py:47
def syscall_name(id)
Definition: Util.py:80
static int str(yyscan_t scanner, int token)
def nsecs_secs(nsecs)
Definition: Util.py:25
def nsecs_str(nsecs)
Definition: Util.py:31
def nsecs_nsecs(nsecs)
Definition: Util.py:28
def add_stats(dict, key, value)
Definition: Util.py:35