/
parser_av.py
executable file
·445 lines (317 loc) · 12.5 KB
/
parser_av.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# Copyright 2012 IKT Leibniz Universitaet Hannover
# GPL2
# Zdravko Bozakov (zb@ikt.uni-hannover.de)
import time
import threading
import warnings
from collections import deque
try:
import numpy as np
except ImportError:
print __name__ + ": please make sure the following packages are installed:"
print "\tpython-numpy"
exit(1)
try:
# import cython functions if available
import hpfast
import types # for cython bindings
except (ImportError, ValueError) as e:
pass
import hphelper
import hplotting
warnings.simplefilter('ignore', np.RankWarning)
np_append = np.append
options = hphelper.options
DEBUG = hphelper.DEBUG
ERROR = hphelper.err
class var_est(object):
"""An online variance estimator. The object is fed new samples
using it's step method and calulates the variance on the fly.
"""
N_MIN = 25 # minimum number of samples required to
# return a variance estimate
def __init__(self,m):
self.M = m # aggregation level in seconds: only used for printing
self.n = 0
self.mean = 0.0
self.M2 = 0.0
def step(self,x):
"""Receive a single sample and update the variance"""
self.n += 1
delta = x - self.mean
self.mean += delta/self.n
self.M2 += delta*(x - self.mean)
def var(self):
"""Returns the variance estimate for the current aggregation
level. Returns NaN if less than N_MIN samples were available for
calculating the variance"""
if self.n<self.N_MIN: return np.nan
return self.M2/(self.n - 1)
def freeze(self):
"""Reset sample counter for numeric stability"""
self.M2 = self.M2/self.n
self.n = 1
def __str__(self):
return '%f\t%.6f\t%d\t%.6f\n' % (self.M, self.var(), self.n, self.mean)
def __repr__(self):
return 'M%d samples: %d\tvar: %.6f' % (self.M, self.n, self.var())
try:
var_est = hpfast.var_est
DEBUG('using hpfast.var_est', __name__)
except (NameError, AttributeError) as e:
pass
class AggVarEstimator(threading.Thread):
def __init__(self, buf, slots, M=None):
self.buf = buf
self.slots = slots
if not M:
#M = range(options.M[0], options.M[1], 300)
M = 10**np.linspace(np.log10(options.M[0]),np.log10(options.M[1]),200)
M = np.unique(np.floor(M)).astype(int)
self.M = M
# sliding window to store arriving values
self.win = np.zeros(np.max(M)).astype(bool)
# avars stores an estimator for each aggregation level in M
self.avars = dict.fromkeys(M,0)
# ensure that we calculate the variance at the smallest
# aggregate level, even when it was not specified by the user
self.avars[1] = 0
for m in self.avars.iterkeys():
self.avars[m] = var_est(m)
self.probe_count = 0
self.slot_count = 0
self.stats = hphelper.stats_stats()
self.mean_a = options.rate
self.var_a = self.mean_a - self.mean_a**2
self.va = self.var_a*np.ones(len(self.M))/self.M
# start progress bar thread
hphelper.bar_init(options, self.stats)
threading.Thread.__init__(self)
def run(self):
stats = self.stats
last_seq = -1 # store maximum sequence number received until now
last_slot = 0
if options.min_rtt == -1.0:
min_rtt = np.inf
else:
min_rtt = options.min_rtt
while 1:
try:
(seq, slot, rtt) = self.buf.popleft()
except IndexError:
continue
if seq == -2: break
stats.update(seq, rtt, slot)
if seq!=last_seq+1:
# unexpected sequence number
seq_delta = seq-last_seq-1
if seq_delta<0:
# discard probe if it was received out of order
# seq_delta == -1 --> duplicate packet
stats.rx_out_of_order += 1
continue
# all intermediate packets were missing
stats.rcv_err += seq_delta
## packet was not sent correctly!
#if slot == -1.0:
# stats.snd_err += 1
# continue
## each dropped probe indicates a full queue append, a
## 1 to the covariance vector
#while seq!=last_seq+1:
# last_seq += 1
# next_slot = slots[last_seq]
# if next_slot==-1: # slottimes vector might be incomplete
# continue
# slot_delta = next_slot - last_slot
# last_slot = next_slot
# stats.rcv_err += 1 # increment dropped packets counter
# self.append_fast(True, slot_delta-1)
last_seq = seq
slot_delta = slot - last_slot
last_slot = slot
# update the minimum RTT on the fly. Only update if the
# RTT was not specified as an option
#if options.min_rtt == -1.0:
# min_rtt = min(rtt, min_rtt)
# check if probe saw a busy period (True/False)
probe = rtt > min_rtt
self.append_fast(probe, slot_delta-1)
def get_avars(self):
"""Returns the variances estimated so far for all aggregation
levels stored in M"""
return [self.avars[m].var() for m in self.M]
def get_avars_corrected(self):
"""Returns the variances for all aggregation levels, corrected
to account for the geometric sampling process"""
var_w = self.avars[1].var()
vw = self.get_avars()
mean_y_hat = self.mean()/self.mean_a
var_y_hat = (var_w - mean_y_hat**2*self.var_a)/(self.var_a + self.mean_a**2);
return (vw - mean_y_hat**2*self.va - self.var_a*var_y_hat/self.M)/self.mean_a**2;
def fit(self):
"""Performs a linear fit on the aggregated variance estimates"""
logy = np.log10(self.get_avars_corrected())
logx = np.log10(self.M)
try:
(d,y0) = np.polyfit(logx[~np.isnan(logy)], logy[~np.isnan(logy)],1)
return (d,10**y0)
except Exception as e:
return (-1, -1)
def hurst(self, d=None):
''' return the hurst parameter estimate'''
if not d:
(d,y0) = self.fit()
return (d+2)/2
def getdata_str(self):
variances = self.get_avars_corrected()
if any(variances):
return '\n'.join([' '.join([str(m*options.delta),str(v)]) for m,v in zip(self.M, variances)])
else:
return None
def append_fast(self, probe, zcount=0):
''' Append the latest received probe to a sliding
window. Update the aggregate variances for each variance
block '''
self.probe_count += probe
z=[False]*zcount
z.append(bool(probe))
for x in z:
# speed: np.r_ < np.roll < np.concatenate < np.append
self.win = np_append(self.win[1:], x)
self.slot_count += 1
[var.step(np.mean(self.win[-m:])) for m,var in self.avars.iteritems() if not self.slot_count % m]
def mean(self):
''' return the mean of the observation vector mu_w '''
try:
return self.probe_count*1.0/self.slot_count
except:
return np.nan
try:
# try to bind cython methods
# http://wiki.cython.org/FAQ#HowdoIimplementasingleclassmethodinaCythonmodule.3F
AggVarEstimator.append_fast = types.MethodType(hpfast.av_append_f, None, AggVarEstimator)
AggVarEstimator.get_avars_corrected = types.MethodType(hpfast.get_avars_corrected_f, None, AggVarEstimator)
min = hpfast.min
max = hpfast.max
DEBUG('cython methods bounded', __name__)
except (NameError, AttributeError) as e:
pass
def avparser(pipe, ns, ST=None):
if not options.start_time:
options.start_time = time.time()
if not options.savefile:
# default save name is destination + YYMMDD + HHMM
options.savefile = options.DST + time.strftime("_%Y%m%d_%H%M",
time.localtime(options.start_time))
options.savefile += options.tag
options.savefile += '_av'
rcv_buf = deque() # TODO
# init estimator thread
av = AggVarEstimator(rcv_buf, ST)
av.daemon = True
# init plotter thread
avplotter_thread = threading.Thread(target=avplotter, args=(av,))
avplotter_thread.daemon = True
#block until sender + receiver say they are ready
while not all([ns.RCV_READY,ns.SND_READY]):
time.sleep(0.1)
av.stats.run_start = time.time()
# start threads
avplotter_thread.start()
av.start()
data = None
try:
while 1: # faster than while True
data = pipe.recv()
(seq, snd_time, rtt) = data
rcv_buf.append((seq, snd_time, rtt)) # receive (seq, snd_time, rtt) from rcvloop process
except (KeyboardInterrupt):
rcv_buf.append((-2,-2,-2))
print '\n\nparse loop interrupted...'
except (ValueError) as e:
rcv_buf.append((-2,-2,-2))
print '\a', # received all packets
try:
av.join()
avplotter_thread.join()
except KeyboardInterrupt:
pass
av.stats.run_end = time.time()
av.stats.pprint()
print
print "\tH=%.2f" % (av.hurst(),)
print
fname = options.savefile + '.dat'
print "saving variances to " + fname + " ..."
try:
fs = open(fname, mode='w')
fs.write('% ' + options.IPDST + ' ' + str(options))
for m,v in zip(av.get_avars_corrected(), av.M):
fs.write("%e\t%d\n" % (m,v))
fs.close()
except IOError:
ERROR('could not write to file')
return
except KeyboardInterrupt:
print 'canceled saving.'
DEBUG('done', __name__)
def avplotter(av):
if not options.plot: return
gp = hplotting.gp_plotter()
if not gp.gp: return
getdata_str = av.getdata_str
gp_cmd = gp.cmd
fps = 1.0/options.fps
# use these to plot axis ranges
min_x = options.M[0]*options.delta
max_x = options.M[1]*options.delta
min_y, max_y = (1e-5,1e-0)
# set plot options
gp.setup(xlabel='log_{10}(M) [s]',
ylabel='log_{10}(aggregate variance)',
xrange=(min_x, max_x),
yrange=(min_y, max_y),
)
# draw auxiliarry lines
#y0=1e-0
#gp.arrow(min_x,y0, max_x,y0/(max_x/options.delta))
#gp.arrow(min_x,y0, y0/0.1,1e-4)
i = 0
try:
while av.is_alive():
#if not gp.gp: break
i += 1
if i%10==0: # calculate and plot hurst fit every 10 frames
(d,y0) = av.fit()
if y0!=-1:
# plot H linear fit and label it
gp.arrow(min_x, y0*(min_x/options.delta)**d, max_x, y0*(max_x/options.delta)**d,'2')
# sleep before redrawing
time.sleep(fps)
data = getdata_str()
if data:
gp_cmd("plot '-' with points ls 4\n %s\n e\n" % (data), flush=True)
except ValueError as e:
print e # gnuplot error
return
# replot with label
(d,y0) = av.fit()
if y0!=-1:
# plot H linear fit and label it
gp.label('H=%.2f' % (av.hurst(d)), min_x*5, 2*y0*(min_x*5/options.delta)**d, '2')
gp.arrow(min_x, y0*(min_x/options.delta)**d, max_x, y0*(max_x/options.delta)**d,'2')
data = getdata_str()
if data:
gp_cmd("plot '-' with points ls 4\n %s\n e\n" % (data), flush=True)
# save plot to EPS
gp.set_term_eps(options.savefile)
# we must replot everything to save it to the file
(d,y0) = av.fit()
if y0!=-1:
# plot H linear fit and label it
gp.label('H=%.2f' % (av.hurst(d)), min_x*5, 2*y0*(min_x*5/options.delta)**d, '2')
gp.arrow(min_x, y0*(min_x/options.delta)**d, max_x, y0*(max_x/options.delta)**d,'2')
data = getdata_str()
gp_cmd("plot '-' with points ls 4\n %s\n e\n" % (data), flush=True)
gp.quit()