-
Notifications
You must be signed in to change notification settings - Fork 14
/
ft-compute-stats
executable file
·277 lines (230 loc) · 8.16 KB
/
ft-compute-stats
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env python
from __future__ import division
import numpy
import optparse
import sys
import os
from math import ceil
from os.path import splitext
import itertools as it
def decode_key_value_filename(name):
"Map key=value_otherkey=other-value names to proper dictionary."
params = {}
parts = name.split('_')
for p in parts:
kv = p.split('=')
k = kv[0]
v = kv[1] if len(kv) > 1 else None
params[k] = v
return params
def print_cols(cols, first_row_prefix='# ', other_rows_prefix=' '):
col_widths = [max((len(str(x)) for x in col)) for col in cols]
prefs = it.chain([first_row_prefix], it.repeat(other_rows_prefix))
for prefix, row in it.izip(prefs, range(0, max((len(c) for c in cols)))):
reached_end = True
data = [col[row] if row < len(col) else '' for col in cols]
print '%s%s' % (prefix,
", ".join([' ' * (c - len(str(f))) + str(f)
for (c, f) in it.izip(col_widths, data)]))
def print_rows(rows, first_row_prefix='# ', other_rows_prefix=' '):
col = 0
col_widths = []
field_widths = True
while field_widths:
field_widths = [len(str(row[col])) for row in rows if len(row) > col]
if field_widths:
col_widths.append(max(field_widths))
col += 1
prefs = it.chain([first_row_prefix], it.repeat(other_rows_prefix))
for prefix, row in it.izip(prefs, rows):
print '%s%s' % (prefix,
", ".join([' ' * (c - len(str(f))) + str(f)
for (c, f) in it.izip(col_widths, row)]))
def stats_for_file(fname, scale):
n = 0
max = 0
p95 = 0
p99 = 0
p999 = 0
min = 0
med = 0
avg = 0
std = 0
var = 0
size = os.stat(fname).st_size
if size:
samples = numpy.memmap(fname, dtype='float32', mode='c')
n = len(samples)
if n > 0:
samples *= scale
max = numpy.amax(samples)
p95 = numpy.percentile(samples, 95.0)
p99 = numpy.percentile(samples, 99.0)
p999 = numpy.percentile(samples, 99.9)
med = numpy.median(samples)
avg = numpy.mean(samples)
min = numpy.amin(samples)
std = numpy.std(samples, ddof=1)
var = numpy.var(samples)
return [n, max, p999, p99, p95, avg, med, min, std, var]
o = optparse.make_option
opts = [
o('-p', '--cycles-per-usec', action='store', dest='cycles', type='float',
help='how many cycles per usec'),
o(None, '--hist', action='store_true', dest='want_hist',
help='generate a histogram'),
o('-b', '--bin-size', action='store', dest='bin_size', type='float',
help='size of each bin in histogram'),
o('-n', '--normalize-counts', action='store_true', dest='normalize',
help='give relative frequency, not absolute sample counts'),
o('-c', '--cumulative', action='store_true', dest='cumulative',
help='report cumulative counts (i.e., CDF)'),
o(None, '--percentiles', action='store_true', dest='want_percentiles',
help='produce table of percentiles'),
o('-r', '--resolution', action='store', dest='resolution', type='float',
help='set resolution of percentiles table'),
o(None, '--percent', action='store_true', dest='want_percent',
help='give relative frequency as a percentage'),
]
defaults = {
'cycles' : None,
'want_hist' : False,
'bin_size' : 1000,
'normalize' : False,
'want_percent' : False,
'cumulative' : False,
'want_percentiles' : False,
'resolution' : 0.1,
}
options = None
def to_str(x):
if type(x) == str:
return x
if type(x) == int:
return "%d" % x
else:
return "%.5f" % x
STATS_HEADERS = [
"Plugin", "#cores", "Overhead", 'Unit', 'Scale', "#tasks",
"#samples",
"max", "99.9th perc.", "99th perc.", "95th perc.",
"avg", "med", "min", "std", "var",
"file"
]
def get_stats(fname):
name, ext = splitext(fname)
conf = decode_key_value_filename(name)
if 'overhead' in conf and conf['overhead'].rfind('-LATENCY') != -1:
# latency is stored in nanoseconds, not cycles
scale = 1 / 1000 # convert from nanoseconds
unit = 'microseconds'
scale_desc = '1/1000.00'
elif options.cycles is None:
scale = 1
unit = 'cycles'
scale_desc = '1'
else:
# convert from cycles to usec
scale = 1 / options.cycles
unit = 'microseconds'
scale_desc = '1/%.2f' % options.cycles
stats = stats_for_file(fname, scale)
if 'locks' in conf:
sched = '%s_locks=%s' % (conf['scheduler'], conf['locks'])
elif 'scheduler' in conf:
sched = conf['scheduler']
else:
sched = 'UNKNOWN'
ohead = conf['overhead'] if 'overhead' in conf else 'UNKNOWN'
n = conf['n'] if 'n' in conf else '*'
m = conf['m'] if 'm' in conf else '*'
info = [sched, m, ohead, unit, scale_desc, n]
finfo = [fname]
return [to_str(x) for x in info + stats + finfo]
def make_bins(max_val):
num_bins = int(ceil(max_val / options.bin_size)) + 1
return [x * options.bin_size for x in range(0, num_bins)]
def iter_percentiles(resolution):
percentile = 0.0
while percentile < 100:
yield percentile
percentile += resolution
yield 100.0
def make_percentiles(resolution):
return list(iter_percentiles(resolution))
def hist_file(fname, scale):
max_val = 0
hist = []
size = os.stat(fname).st_size
if size:
samples = numpy.memmap(fname, dtype='float32', mode='c')
n = len(samples)
if n > 0:
samples *= scale
max_val = numpy.amax(samples)
bins = make_bins(max_val)
hist, _ = numpy.histogram(samples, bins)
if options.cumulative:
hist = numpy.cumsum(hist)
if options.normalize:
hist = [h/n * (100 if options.want_percent else 1) for h in hist]
return (max_val, hist)
def percentiles_file(fname, percentiles):
percs = []
size = os.stat(fname).st_size
if size:
samples = numpy.memmap(fname, dtype='float32', mode='c')
n = len(samples)
if n > 0:
percs = numpy.percentile(samples, percentiles)
return percs
if __name__ == '__main__':
# FIXME: would be nicer with argparse
parser = optparse.OptionParser(option_list=opts)
parser.set_defaults(**defaults)
(options, files) = parser.parse_args()
try:
if options.want_hist:
cols = []
max_val = 0
col_names = []
for i, f in enumerate(files):
try:
(max_in_file, hist) = hist_file(f, 1)
cols.append([i + 1] + [to_str(x) for x in hist])
max_val = max(max_val, max_in_file)
col_names.append(f)
except IOError, msg:
print >> sys.stderr, msg
bins = ['Bin'] + make_bins(max_val)
print_cols([bins] + cols)
print '# Columns:'
for i, f in enumerate(col_names):
print '# (%d) %s' % (i + 1, f)
elif options.want_percentiles:
cols = []
percentiles = make_percentiles(options.resolution)
col_names = []
for i, f in enumerate(files):
try:
percs = percentiles_file(f, percentiles)
cols.append([i + 1] + [to_str(x) for x in percs])
col_names.append(f)
except IOError, msg:
print >> sys.stderr, msg
perc = ['Percentile'] + [to_str(x) for x in percentiles]
print_cols([perc] + cols)
print '# Columns:'
for i, f in enumerate(col_names):
print '# (%d) %s' % (i + 1, f)
else:
rows = []
rows.append(STATS_HEADERS)
for f in files:
try:
rows.append(get_stats(f))
except IOError, msg:
print >> sys.stderr, msg
print_rows(rows)
except KeyboardInterrupt:
pass