live.py 20.8 KB
Newer Older
1
2
3
# -*- coding: utf-8 -*-

import numpy as np
4
5
import math
import sys
6
import time
7
import threading
8
9
10
from collections import deque
from .gui_base import GuiBase

11
VALUES_TO_PLOT = ['cwnd', 'sst', 'rtt', 'smoothedThroughput'] # (only values for Y-axis)
12
13
14
VALUES_TO_PROCESS = ['time']  + VALUES_TO_PLOT #helper to init all data structures

# Strings for UI-elements
15
FIGURE_TITLE = "TCPlivePLOT"
16
17
18
19
PLOT_TITLE = "Data from"
PAUSE = "Pause"
QUIT = "Quit"

20
21
22
23
24
25
import matplotlib
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')
import matplotlib.animation as animation
from matplotlib.widgets import Button, RadioButtons

26
27
28
29
30
31
32
33
# constants
CLEAR_GAP = 0.2 # gap in s
INFINITY_THRESHOLD = 1e8

class LiveGui(GuiBase):
    def __init__(self, options, infoRegistry):
        self.options = options
        self.infoRegistry = infoRegistry
34
35
        self.__stopped = threading.Event()
        self.timestampOfLastGuiRefresh = 0
36
37
38
39
40
41
42
43
        self.__lastPlotTimestamp = {}
        self.__lastDrawTimestamp = 0
        self.__initRealtimeTimestamp = 0
        self.__initSampletimeTimestamp = -1
        self.xmin = 0
        self.xmax = self.xmin + self.options.xDelta
        self.flows = []
        self.lineVisibility = {}
44

45
46
47
48
        if(self.options.debug):
            print("matplotlib-version: " +  matplotlib.__version__)
            print("available matplotlib-styles:" + str(plt.style.available))

49
50
51

    def setConnectionBuffer(self, connectionBuffer):
        self.__connectionBuffer = connectionBuffer
52
53

    def tearDown(self):
54
55
        plt.close()
        sys.exit(0)
56
57
58
59
60
61
        pass

    def startUp(self):
        pass

    def startupCheck(self):
62
        for val in VALUES_TO_PLOT:
63
64
65
66
            if((len(self.options.initialLineVisibility) < 1) or (val in self.options.initialLineVisibility)):
                self.lineVisibility[val] = True
            else:
                self.lineVisibility[val] = False
67
68
69
70
71
72
73
74

    def pause(self, event):
        """Toggles pause flag."""
        self.__paused ^= True
        return

    def toggleVisibility(self, lineID):
        """Toggles visibility for given line."""
75
76
        self.lineVisibility[lineID] ^= True
        for flowIdentifier in self.__connectionBuffer:
Michael König (Student)'s avatar
Michael König (Student) committed
77
78
79
            if(flowIdentifier in self.flows and flowIdentifier in self.__plotLineConfigs):
                self.__plotLineConfigs[flowIdentifier][lineID] ^= True
                self.__plotLines[flowIdentifier][lineID].set_visible(self.__plotLineConfigs[flowIdentifier][lineID])
80
81
        self.drawPlotLegend()

82
83

    def updateValueVisibility(self, label):
84
        for flowIdentifier in self.__connectionBuffer:
85
            for i in range(1, len(VALUES_TO_PLOT)+1):
Michael König (Student)'s avatar
Michael König (Student) committed
86
87
88
                if(flowIdentifier in self.flows and flowIdentifier in self.__plotLineConfigs):
                    self.__plotLineConfigs[flowIdentifier][VALUES_TO_PLOT[(i-1)]] = False
                    self.__plotLines[flowIdentifier][(VALUES_TO_PLOT[(i-1)])].set_visible(self.__plotLineConfigs[flowIdentifier][(VALUES_TO_PLOT[(i-1)])])
89
90
91
92
93
94
95
96
97
98
99
        if label == 'cwnd':
            self.toggleVisibility(VALUES_TO_PLOT[0])
        elif label == 'sst':
            self.toggleVisibility(VALUES_TO_PLOT[1])
        elif label == 'rtt':
            self.toggleVisibility(VALUES_TO_PLOT[2])
        elif label == 'bw':
            self.toggleVisibility(VALUES_TO_PLOT[3])
        else:
            pass

100
101
    def drawPlotLegend(self):
        """(Re)draws legend with visible lines."""
102
103
104
105
        if len(self.__connectionBuffer) > 0:
            labelObjs  = []
            labelTexts = []
            for flowIdentifier in self.__connectionBuffer:
Michael König (Student)'s avatar
Michael König (Student) committed
106
                if(flowIdentifier in self.flows and flowIdentifier in self.__plotLineConfigs):
107
108
109
110
111
112
113
114
115
116
117
                    for val in VALUES_TO_PLOT:
                        if(self.__plotLineConfigs[flowIdentifier][val]):
                            labelObjs.append(self.__plotLines[flowIdentifier][val])
                            labelTexts.append(self.__plotLines[flowIdentifier][val].get_label())
            if(len(labelObjs) > 0):
                self.legendVisible = True
                self.__ax.legend(labelObjs, labelTexts, fontsize='small')
            else:
                if self.legendVisible:
                    self.legendVisible = False
                    self.__ax.legend_.remove()
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

    def plotKeyPressCallback(self, event):
        """Callback to handle key presses."""
        if(self.options.debug):
            print("Key pressed: '" + event.key + "'")

        # p pauses
        if(event.key == "p"):
            self.pause(event)
        # ctrl+{c,q,w} quits programm
        elif(event.key == "ctrl+c" or event.key == "ctrl+w" or event.key == "ctrl+q"):
            raise SystemExit
        else:
            try:
                index = int(event.key)
            except ValueError:
                pass
            else:
                # Numbers 1-N toggle visibility of lines
                if index in range(1, len(VALUES_TO_PLOT)+1):
                    self.toggleVisibility(VALUES_TO_PLOT[(index-1)])

    def stopPlotting(self, event):
        """Callback function to stop plotting and the programm."""
        self.__tmpTimestamp = time.perf_counter()
        self.tearDown()

145
    def updateFlowDataStructures(self):
146
147
148
149
        # test all flows if outdated and replace list in-place
        for flowIdentifier in self.flows:
            self.flows[:] = [i for i in self.flows if not self.isFlowOutdated(i)]

150
151
152
        for flowIdentifier in self.__connectionBuffer:
            if(flowIdentifier not in self.flows):
                self.initFlowDataStructures(flowIdentifier)
153

154
155
        self.drawPlotLegend()

156
157
158
159
160
161
162
163
164
165
    def isFlowOutdated(self, flowIdentifier):
        outdated = self.__lastPlotTimestamp[flowIdentifier] < self.xmin
        if(outdated):
            for val in VALUES_TO_PLOT:
                self.__plotLineConfigs[flowIdentifier][val] = False
                self.__plotLines[flowIdentifier][val].set_visible(False)
                self.__plotLines[flowIdentifier][val].set_data([], [])
                self.__plotValues[flowIdentifier][val] = []
            self.drawPlotLegend()

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
    def initFlowDataStructures(self, flowIdentifier):
        splittedFlowidentifier = flowIdentifier.split("-")
        srcPort = splittedFlowidentifier[0]
        dstPort = splittedFlowidentifier[1]
        dstPortCount = 1
        for preexistingFlow in self.flows:
            flowDstPort = preexistingFlow.split("-")[1]
            if flowDstPort == dstPort:
                dstPortCount += 1

        self.flows.append(flowIdentifier)
        self.__lastPlotTimestamp[flowIdentifier] = 0
        self.__plotLines[flowIdentifier] = {}
        self.__plotValues[flowIdentifier] = {}
        self.__plotValuesMin[flowIdentifier] = {}
        self.__plotValuesMax[flowIdentifier] = {}
        self.__plotLineConfigs[flowIdentifier] = {}
        self.__plotLineConfigs[flowIdentifier]['lastTimestamp'] = 0
        for val in VALUES_TO_PROCESS:
            self.__plotValuesMin[flowIdentifier][val] = math.inf
            self.__plotValuesMax[flowIdentifier][val] = -math.inf
            self.__plotValues[flowIdentifier][val] = deque(maxlen=(int(self.options.xDelta / self.options.plotResolution * 10)))
        index = 1
        for val in VALUES_TO_PLOT:
            self.__plotLines[flowIdentifier][val], = self.__ax.plot([])
            self.__plotLines[flowIdentifier][val].set_label("[" + str(index) + "] " + val + " - " + str(dstPort) + " #" + str(dstPortCount) + "")
            self.__plotLineConfigs[flowIdentifier][val] = self.lineVisibility[val]
            self.__plotLines[flowIdentifier][val].set_visible(self.lineVisibility[val])
            self.__plotLines[flowIdentifier][val].set_data([], [])
            index += 1

    def destroyFlowDataStructures(self, flowIdentifier):
        self.flows.remove(flowIdentifier)
199
200
        # del(self.flows[(self.flows.index(flowIdentifier))])
        print("deleting " + str(flowIdentifier))
201

202
203
    def plotGraph(self):
        """Initializes plot configuration and starts the plotting."""
204
205
206
207
208
209
210
211
212
213

        while(len(self.__connectionBuffer) < 1):
            print("waiting for data on filtered flows...")
            time.sleep(0.5)

        if(self.options.debug):
            print("initializing...")
        time.sleep(1)


214
215
216
217
218
219
220
        self.__paused = False
        self.__minVal = 9999999999
        self.__maxVal = 0

        fig = plt.figure(FIGURE_TITLE)
        fig.canvas.mpl_connect('key_press_event', self.plotKeyPressCallback)
        self.__ax = plt.axes()
221
        # self.__ax2 = self.__ax.twinx()
222
223
224
225
226
227
        self.__ax.set_autoscaley_on(False)
        self.__ax.set_xlim(0, self.options.xDelta)
        self.__ax.set_title(PLOT_TITLE + " :" + ', :'.join(map(str, self.options.filterPorts)))

        self.__plotLines = {}
        self.__plotValues = {}
228
229
        self.__plotValuesMin = {}
        self.__plotValuesMax = {}
230
        self.__plotLineConfigs = {}
231
232

        self.updateFlowDataStructures()
233
234
235
236
237
238
239
240
241
242
243

        # pause button
        pauseAx = plt.axes([0.8, 0.025, 0.1, 0.04])
        pauseButton = Button(pauseAx, PAUSE)
        pauseButton.on_clicked(self.pause)

        # quit button
        quitAx = plt.axes([0.125, 0.025, 0.1, 0.04])
        quitButton = Button(quitAx, QUIT)
        quitButton.on_clicked(self.stopPlotting)

244
245
246
247
248
249
250
251
252

        # valueCheckboxesAx = plt.axes([0.05, 0.4, 0.1, 0.15])
        # valueCheckboxes = CheckButtons(valueCheckboxesAx, ('cwnd', 'sst', 'rtt', 'bw'), (True, False, False, False))
        # valueCheckboxes.on_clicked(self.updateValueVisibility)

        # valueRadiobuttonsAx = plt.axes([0.020, 0.025, 0.075, 0.15])
        # valueRadiobuttons = RadioButtons(valueRadiobuttonsAx, ('cwnd', 'sst', 'rtt', 'bw'))
        # valueRadiobuttons.on_clicked(self.updateValueVisibility)

253
254
255
256
257
258
259
260
261
262
263
        if(self.options.preloadBuffer > 0):
            self.__preloading = True
        else:
            self.__preloading = False


        self.__timeOffset = 0
        self.__bufferFactor = 1
        self.__apsFixFactor = 1

        # call update-routine
264
265
266
267
268
269
270
271
272
273
        # self.plotInit()
        # self.__plotLine = self.plotGraphUpdate(0)
        animation.FuncAnimation(fig, self.plotGraphUpdate, init_func=self.plotInit, frames=self.options.drawFps, interval=self.options.drawIntervall, blit=self.options.blitting, repeat=True)
        # if self.__stopped.isSet():
        #     return
        # else:
        # plt.ioff()
        # plt.draw()
        plt.show()
        # print("bar")
274
275
276
277

    def returnAllLines(self):
        """Macro to return all lines as they are."""
        allPlotLines = []
278
        for flowIdentifier in self.__connectionBuffer:
279
            for val in VALUES_TO_PLOT:
280
                allPlotLines.append(self.__plotLines[flowIdentifier][val])
281
282
283
284
285
286
287
288
289
290
291
292
        return tuple(allPlotLines)

    def returnNanSample(self, time):
        """Macro to return NaN-Samples (to fill plot)."""
        data = {}
        data['time'] = time - self.options.plotResolution
        for val in VALUES_TO_PLOT:
            data[val] = np.nan
        return data

    def plotGraphUpdate(self, i):
        """Animation loop - does the actual plot update."""
293
294
295

        self.updateFlowDataStructures()

296
297
298
299
300
301
302
303
        if(self.__initSampletimeTimestamp == -1):
            self.__initSampletimeTimestamp = 0
            return self.returnAllLines()
        elif(self.__initSampletimeTimestamp == 0):
            self.calculateSampleTimeOffset()
            return self.returnAllLines()

        # fill playback-buffer
304
        if(False and self.__preloading):
305
            bufferLength = -1
306
307
            for flowIdentifier in self.__connectionBuffer:
                bufferLength = max(bufferLength, len(self.__connectionBuffer[flowIdentifier]))
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

            if(bufferLength > 0):
                bufferedTime = bufferLength * self.options.plotResolution
                bufferTarget = self.options.preloadBuffer * self.__bufferFactor
                if(bufferedTime >= bufferTarget):
                    self.__preloading = False
                    # reduce buffer-target to half size after initial buffering
                    self.__bufferFactor = 0.5
                    print("Buffer filled.")
            if(self.__preloading):
                print("Buffering... " + str(format(bufferedTime, ".2f")) + "/" + str(format(bufferTarget, ".2f")))
                return self.returnAllLines()

        if(self.__paused == True):
            return self.returnAllLines()
        else:
            while(True):
                currentTimestamp = time.perf_counter()
                if(self.__initRealtimeTimestamp == 0):
                    self.__initRealtimeTimestamp = currentTimestamp
                timestampDelta = (currentTimestamp - self.__lastDrawTimestamp) * self.options.playbackSpeed * self.__apsFixFactor

                currentXmin, currentXmax = self.__ax.get_xlim()
                currentYmin, currentYmax = self.__ax.get_ylim()
                newXmax = currentTimestamp - self.options.preloadBuffer
                newXmin = newXmax - self.options.xDelta
334
335
                self.xmin = newXmin
                self.xmax = newXmax
336
337
                self.__ax.set_xlim(newXmin, newXmax)

338
339
                maxYval = -math.inf
                minYval = math.inf
340
341
342
343
344
345
                connectionsData = {}

                # skip this cycle, plot resolution not yet reached
                if(timestampDelta < self.options.plotResolution):
                    return self.returnAllLines()

346
347
                for flowIdentifier in self.__connectionBuffer:
                    connectionsData[flowIdentifier] = deque()
348
                    whileRun = True
349
                    while(len(self.__connectionBuffer[flowIdentifier]) > 0 and whileRun):
350
                        try:
351
                            data = self.__connectionBuffer[flowIdentifier].popleft()
352
353
354
355
                        except IndexError:
                            whileRun = False
                            pass
                        else:
356
357
358
                            if(flowIdentifier not in self.flows):
                                continue

Michael König (Student)'s avatar
Michael König (Student) committed
359
360
361
362
363
364
365
                            try:
                                dataTime = float(data['time'])
                            except ValueError:
                                continue
                            else:
                                lineTime = self.__initRealtimeTimestamp  + (dataTime - self.__initSampletimeTimestamp)

366
367
368
369
                            # time in past
                            if(lineTime < newXmin):
                                continue
                            # time older than newst timestamp
370
                            elif(lineTime < self.__lastPlotTimestamp[flowIdentifier]):
371
372
                                continue
                            # skip this sample due plot plotResolution
373
                            elif((lineTime - self.__lastPlotTimestamp[flowIdentifier]) < self.options.plotResolution):
374
375
                                continue
                            else:
376
377
                                if(self.__lastPlotTimestamp[flowIdentifier] > 0 and ((lineTime - self.__lastPlotTimestamp[flowIdentifier]) > CLEAR_GAP)):
                                    self.__lastPlotTimestamp[flowIdentifier] = lineTime
378
                                    nanSample = self.returnNanSample(lineTime)
379
                                    connectionsData[flowIdentifier].append(nanSample)
380
381
                                infinityReached = False
                                for val in VALUES_TO_PLOT:
382
383
384
                                    try:
                                        convertedValue = float(data[val])
                                    except ValueError:
385
                                        data[val] = np.nan
386
387
388
                                    else:
                                        if(convertedValue > INFINITY_THRESHOLD):
                                            data[val] = np.nan
389
390
391
392
393
                                        # nanSample = self.returnNanSample(lineTime)
                                        # connectionsData[port].append(nanSample)
                                        # infinityReached = True

                                if(not infinityReached):
394
395
                                    self.__lastPlotTimestamp[flowIdentifier] = lineTime
                                    connectionsData[flowIdentifier].append(data)
396
397

                data = 0
398
                for flowIdentifier in connectionsData:
Michael König (Student)'s avatar
Michael König (Student) committed
399
                    if(len(connectionsData[flowIdentifier]) > 0 and len(self.flows) > 0 and flowIdentifier in self.flows):
400
401
                        data += 1

402
403
                for flowIdentifier in self.__connectionBuffer:
                    if(data < 1 and currentTimestamp > self.__lastPlotTimestamp[flowIdentifier] ):
404
405
406
407
408
409
                        if(self.options.debug):
                            print("No data for any connection.")
                        return self.returnAllLines()



410
                # copy raw-value into corresponding lists
411
412
413
414
                for connection in connectionsData:
                    while(len(connectionsData[connection]) > 0):
                        data = connectionsData[connection].popleft()

Michael König (Student)'s avatar
Michael König (Student) committed
415
416
417
418
419
420
421
422

                        try:
                            dataTime = float(data['time'])
                        except ValueError:
                            continue
                        else:
                            lineTime = self.__initRealtimeTimestamp  + (dataTime - self.__initSampletimeTimestamp)
                            self.__plotLineConfigs[connection]['lastTimestamp'] = dataTime
423
424
425
426
427

                        for val in VALUES_TO_PROCESS:
                            if(val == 'time'):
                                self.__plotValues[connection][val].append(lineTime)
                            else:
428
429
430
431
432
433
434
435
                                try:
                                    currentVal = float(data[val])
                                except ValueError:
                                    pass
                                else:
                                    self.__plotValues[connection][val].append(currentVal)

                    # update axis (xy-tuple) with data from lists
436
                    for val in VALUES_TO_PLOT:
437
438
                        x, y = self.__plotValues[connection]['time'], self.__plotValues[connection][val]
                        self.__plotLines[connection][val].set_data(x, y)
439
440


441
                self.__lastDrawTimestamp = time.perf_counter()
442

443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
                if(self.options.yAxisMax is 0):
                    # y-scaling (autoscaling)
                    lines = self.__ax.get_lines()
                    bot,top = np.inf, -np.inf
                    for line in lines:
                        if(line.get_visible()):
                            new_bot, new_top = self.determineNewYvalues(line)

                            if(new_bot != new_top):
                                if(new_bot < bot):
                                    bot = new_bot
                                if(new_top > top):
                                    top = new_top

                    if(bot != np.inf and top != -np.inf):
                        self.__ax.set_ylim(bot, top)
                    else:
                        # intial y-scale
                        self.__ax.set_ylim(0, 500)
462
                else:
463
464
                    # static y-axis (no autoscaling)
                    self.__ax.set_ylim(0, self.options.yAxisMax)
465
466
467

                return self.returnAllLines()

468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
    def determineNewYvalues(self, line, margin=0.25):
        xLine = line.get_xdata()
        yLine = line.get_ydata()
        xData = np.array(xLine)
        yData = np.array(yLine)
        low,high = self.__ax.get_xlim()
        yVisibleMask = yData[((xData>low) & (xData<high))]
        if(len(yVisibleMask) > 0):
            height = np.max(yVisibleMask) - np.min(yVisibleMask)
            bot = np.min(yVisibleMask) - margin * height
            top = np.max(yVisibleMask) + margin * height
            return bot,top
        else:
            return 0,0

483
484
    def plotInit(self):
        """Helper to initialize plot."""
485
        for flowIdentifier in self.__connectionBuffer:
486
            for val in VALUES_TO_PLOT:
487
488
                self.__plotLines[flowIdentifier][val].set_data([], [])

489
490
491
492
493

        newXmin = 0
        newXmax = newXmin + self.options.xDelta
        self.__ax.set_xlim(newXmin, newXmax)

494
495
        # if(self.options.debug):
        #     print("Plot init done.")
496
497
498
499
500

        return self.returnAllLines()

    def calculateSampleTimeOffset(self):
        """Calculate SampleTime difference at start"""
501
        for flowIdentifier in self.__connectionBuffer:
502
            try:
503
                data = self.__connectionBuffer[flowIdentifier].popleft()
504
505
            except IndexError:
                pass
506
507
            except KeyError:
                pass
508
            else:
509
                # print(data)
510
                #re-add first sample (to head of dequeue)
511
                self.__connectionBuffer[flowIdentifier].appendleft(data)
Michael König (Student)'s avatar
Michael König (Student) committed
512
513
514
515
516
517
                try:
                    dataTime = float(data['time'])
                except ValueError:
                    continue
                else:
                    self.__initSampletimeTimestamp = dataTime
518
519
                return