-
Notifications
You must be signed in to change notification settings - Fork 1
/
mutrace_aggregate.py
executable file
·194 lines (152 loc) · 7.03 KB
/
mutrace_aggregate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/python
# parse output of mutrace, and aggregate data for multiple instances of mutexes
import re, sys
# Mutex # Locked Changed Cont. tot.Time[ms] cont.Time[ms] avg.Time[ms] Flags
# 1390 3730 2754 1406 1.472 0.000 0.004 M-.--.
# 1971 8261 7344 1150 20.597 0.002 0.065 Mx.--.
# ....
#
# Cond # Waits Signals Cont. tot.Time[ms] cont.Time[ms] avg.Time[ms] Flags
# 33 101 1 99 0.476 0.000 0.000 -.
# 8 100 0 99 0.000 0.000 0.000 -.
# Type information
mutexInfo = { 'type': 'mutex',
'headers_int': ['Locked', 'Changed', 'Cont'],
'headers_dbl': ['tot', 'cont', 'avg'],
'item_regex': re.compile("^Mutex #(\d+)", re.IGNORECASE)
}
mutexInfo['table_regex'] = re.compile("^" \
+ "\s+(?P<%s>\d+)" % 'mutex' \
+ "".join([ "\s+(?P<%s>\d+)" % s for s in mutexInfo['headers_int']]) \
+ "".join([ "\s+(?P<%s>\d+\.\d+)" % s for s in mutexInfo['headers_dbl']]),
re.IGNORECASE)
###############################################################################
condvarInfo = { 'type': 'condvar',
'headers_int': ['Waits', 'Signals', 'Cont'],
'headers_dbl': ['tot', 'cont', 'avg'],
'item_regex': re.compile("^Condvar #(\d+)")
}
condvarInfo['table_regex'] = re.compile("^" \
+ "\s+(?P<%s>\d+)" % 'condvar' \
+ "".join([ "\s+(?P<%s>\d+)" % s for s in condvarInfo['headers_int']]) \
+ "".join([ "\s+(?P<%s>\d+\.\d+)" % s for s in condvarInfo['headers_dbl']]))
###############################################################################
sig_line = re.compile("^\t")#.*\[0x[0-9a-f]+\]")
###############################################################################
class MutraceInfo(object):
def __init__(self, typeInfo):
self.backtraces = {}
self.indices = {}
self.nums = {}
self.stats = {}
self.aggregated = []
self.typeInfo = typeInfo
def aggregate(self, sortKey="Cont"):
"""Aggregate the traces and sort"""
self.indices = dict(zip(self.backtraces, range(len(self.backtraces))))
zeroes = [ 0 for _ in self.typeInfo['headers_int'] ]
zeroes += [ 0.0 for _ in self.typeInfo['headers_dbl'] ]
for sig in self.backtraces:
s = zeroes
for item in self.nums[sig]:
if item in self.stats:
s = map(sum, zip(s, self.stats[item]))
d = dict(zip(self.typeInfo['headers_int'] + self.typeInfo['headers_dbl'], s))
d['Cont_p'] = 0.0
if d.get('Locked',0) != 0:
d['avg'] = d['cont'] / d['Locked']
d['Cont_p'] = 100 * float(d['Cont']) / d['Locked']
elif d.get("Waits",0) != 0:
d['avg'] = d['cont'] / d['Waits']
d['Cont_p'] = 100 * float(d['Cont']) / d['Waits']
d['index'] = self.indices[sig]
d['sig'] = sig
self.aggregated += [d]
self.aggregated.sort(key=lambda d: d[sortKey], reverse=True)
def display(self, firstN=20):
if firstN == None:
firstN = len(self.aggregated)
for stat in self.aggregated[:firstN]:
sig = stat['sig']
if sig == '':
continue
l = self.nums[sig]
if len(l) == 0:
continue
l.sort()
l = map(str, l)
print("#%s %d: (count: %d)" % (self.typeInfo['type'], self.indices[sig], len(l)))
print(sig)
print("------ Aggregated results -----")
print(" ".join([' % 7s' % x for x in [self.typeInfo['type']] + self.typeInfo['headers_int'] ] \
+ [' % 7s' % 'Cont_p'] \
+ [' % 11s[ms]' % x for x in self.typeInfo['headers_dbl'] ]))
for d in self.aggregated[:firstN]:
if d.get('Locked',0) != 0 or d.get('Waits',0) != 0:
print " ".join([' % 7d' % d[x] for x in ['index'] \
+ self.typeInfo['headers_int'] ] \
+ [' % 3.2f' % d['Cont_p']] \
+ [' % 15.3f' % d[x] for x in self.typeInfo['headers_dbl'] ])
print("\n\n")
###############################################################################
###############################################################################
def addBacktrace(sig, n, bt, mutrace):
if sig != '':
if sig not in mutrace.backtraces:
mutrace.backtraces[sig] = bt
mutrace.nums[sig] = [n]
else:
mutrace.nums[sig] += [n]
def parseItem(line, (n,sig,bt,lastMatch), mutrace):
typeInfo = mutrace.typeInfo
varType = typeInfo['type']
if varType == lastMatch: #shared regex for the tables, distinction based on state
m = typeInfo['table_regex'].match(line)
if m:
addBacktrace(sig, n, bt, mutrace)
d = m.groupdict()
item = int(d[typeInfo['type']])
mutrace.stats[item] = \
[ int(d[s]) for s in typeInfo['headers_int'] ] \
+ [ float(d[s]) for s in typeInfo['headers_dbl'] ]
return (0,'',[],typeInfo['type'])
m = typeInfo['item_regex'].match(line)
if m:
addBacktrace(sig, n, bt, mutrace)
n = int(m.group(1))
return (n,'',[],typeInfo['type'])
# Failed to match item
return (n,sig,bt,'')
def parse(fileHandler, mutraceInfos):
"""Parse a file to gather mutex and/or condvar information"""
parseMutex = "mutex" in mutraceInfos.keys()
parseCondvar = "condvar" in mutraceInfos.keys()
n, sig, bt = 0, '', []
lastMatch = ""
for line in fileHandler:
# if looking for mutexes
if parseMutex:
(n, sig, bt, matched) = parseItem(line, (n,sig,bt, lastMatch), mutraceInfos['mutex'])
if matched != '':
lastMatch = matched
continue
if parseCondvar:
(n, sig, bt, matched) = parseItem(line, (n,sig,bt, lastMatch), mutraceInfos['condvar'])
if matched != '':
lastMatch = matched
continue
m = sig_line.match(line)
if m:
sig += line
bt += [line]
###############################################################################
###############################################################################
if __name__ == "__main__":
mutraceInfos = {"mutex": MutraceInfo(mutexInfo),
"condvar": MutraceInfo(condvarInfo)}
for filename in sys.argv:
with open(filename, 'r') as fileHandler:
parse(fileHandler, mutraceInfos)
for info in mutraceInfos.values():
info.aggregate("Cont_p")
info.display(None)