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

3
4
5
6
7
8
# TODO:
# * dynamic flow detection
# * flowIdentifer i/o "port"
# * fix socket-input
# * dynamic value detection

9
import matplotlib
10
matplotlib.use('Qt4Agg')
11
12
import matplotlib.pyplot as plt
import matplotlib.animation as animation
13
from matplotlib.widgets import Button, RadioButtons
14
15
import numpy as np

16
17
import math
import sys
18
import time
19
import threading
20
21
22
23
from collections import deque

from .gui_base import GuiBase

24
VALUES_TO_PLOT = ['cwnd', 'sst', 'rtt', 'smoothedThroughput'] # (only values for Y-axis)
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
VALUES_TO_PROCESS = ['time']  + VALUES_TO_PLOT #helper to init all data structures

# Strings for UI-elements
FIGURE_TITLE = "TCPplot"
PLOT_TITLE = "Data from"
PAUSE = "Pause"
QUIT = "Quit"

# 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
41
42
43
44
45
        self.__stopped = threading.Event()
        self.timestampOfLastGuiRefresh = 0

    def setConnectionBuffer(self, connectionBuffer):
        self.__connectionBuffer = connectionBuffer
46
47

    def tearDown(self):
48
49
        plt.close()
        sys.exit(0)
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
        pass

    def startUp(self):
        pass

    def startupCheck(self):
        pass

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

    def toggleVisibility(self, lineID):
        """Toggles visibility for given line."""
        for port in self.options.filterPorts:
            self.__plotLineConfigs[port][lineID] ^= True
            self.__plotLines[port][lineID].set_visible(self.__plotLineConfigs[port][lineID])
        self.drawPlotLegend()

70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

    def updateValueVisibility(self, label):
        for port in self.options.filterPorts:
            for i in range(1, len(VALUES_TO_PLOT)+1):
                self.__plotLineConfigs[port][VALUES_TO_PLOT[(i-1)]] = False
                self.__plotLines[port][(VALUES_TO_PLOT[(i-1)])].set_visible(self.__plotLineConfigs[port][(VALUES_TO_PLOT[(i-1)])])
        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

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
    def drawPlotLegend(self):
        """(Re)draws legend with visible lines."""
        labelObjs  = []
        labelTexts = []
        for port in self.options.filterPorts:
            for val in VALUES_TO_PLOT:
                if(self.__plotLineConfigs[port][val]):
                    labelObjs.append(self.__plotLines[port][val])
                    labelTexts.append(self.__plotLines[port][val].get_label())
        if(len(labelObjs) > 0):
            self.__ax.legend(labelObjs, labelTexts, fontsize='small')
        else:
            self.__ax.legend_.remove()

    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()

    def plotGraph(self):
        """Initializes plot configuration and starts the plotting."""
        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()
        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 = {}
142
143
        self.__plotValuesMin = {}
        self.__plotValuesMax = {}
144
145
146
147
        self.__plotLineConfigs = {}
        for port in self.options.filterPorts:
            self.__plotLines[port] = {}
            self.__plotValues[port] = {}
148
149
            self.__plotValuesMin[port] = {}
            self.__plotValuesMax[port] = {}
150
151
152
            self.__plotLineConfigs[port] = {}
            self.__plotLineConfigs[port]['lastTimestamp'] = 0
            for val in VALUES_TO_PROCESS:
153
154
                self.__plotValuesMin[port][val] = math.inf
                self.__plotValuesMax[port][val] = -math.inf
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
                self.__plotValues[port][val] = deque(maxlen=(int(self.options.xDelta / self.options.plotResolution * 10)))
            index = 1
            for val in VALUES_TO_PLOT:
                self.__plotLines[port][val], = self.__ax.plot([])
                self.__plotLines[port][val].set_label("[" + str(index) + "] " + val + " - " + str(port) + "")
                self.__plotLineConfigs[port][val] = True
                self.__plotLines[port][val].set_visible(True)
                index += 1
        self.drawPlotLegend()

        # 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)

175
176
177
178
179
180
181
182
183

        # 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)

184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
        if(self.options.preloadBuffer > 0):
            self.__preloading = True
        else:
            self.__preloading = False

        self.__lastPlotTimestamp = {}
        for port in self.options.filterPorts:
            self.__lastPlotTimestamp[port] = 0
        self.__lastDrawTimestamp = 0
        self.__initRealtimeTimestamp = 0
        self.__initSampletimeTimestamp = -1

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

        # call update-routine
201
202
203
204
205
206
207
208
209
210
211
        print("foo")
        # 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")
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

    def returnAllLines(self):
        """Macro to return all lines as they are."""
        allPlotLines = []
        for port in self.options.filterPorts:
            for val in VALUES_TO_PLOT:
                allPlotLines.append(self.__plotLines[port][val])
        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."""
        if(self.__initSampletimeTimestamp == -1):
            self.__initSampletimeTimestamp = 0
            return self.returnAllLines()
        elif(self.__initSampletimeTimestamp == 0):
            self.calculateSampleTimeOffset()
            return self.returnAllLines()

        # fill playback-buffer
239
        if(False and self.__preloading):
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
            bufferLength = -1
            for port in self.options.filterPorts:
                bufferLength = max(bufferLength, len(self.__connectionBuffer[port]))

            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
                self.__ax.set_xlim(newXmin, newXmax)

271
272
                maxYval = -math.inf
                minYval = math.inf
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
                connectionsData = {}

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

                for port in self.options.filterPorts:
                    connectionsData[port] = deque()
                    whileRun = True
                    while(len(self.__connectionBuffer[port]) > 0 and whileRun):
                        try:
                            data = self.__connectionBuffer[port].popleft()
                        except IndexError:
                            whileRun = False
                            pass
                        else:
289
                            lineTime = self.__initRealtimeTimestamp  + (float(data['time']) - self.__initSampletimeTimestamp)
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
                            # time in past
                            if(lineTime < newXmin):
                                continue
                            # time older than newst timestamp
                            elif(lineTime < self.__lastPlotTimestamp[port]):
                                continue
                            # skip this sample due plot plotResolution
                            elif((lineTime - self.__lastPlotTimestamp[port]) < self.options.plotResolution):
                                continue
                            else:
                                if(self.__lastPlotTimestamp[port] > 0 and ((lineTime - self.__lastPlotTimestamp[port]) > CLEAR_GAP)):
                                    self.__lastPlotTimestamp[port] = lineTime
                                    nanSample = self.returnNanSample(lineTime)
                                    connectionsData[port].append(nanSample)
                                infinityReached = False
                                for val in VALUES_TO_PLOT:
306
307
308
                                    try:
                                        convertedValue = float(data[val])
                                    except ValueError:
309
                                        data[val] = np.nan
310
311
312
                                    else:
                                        if(convertedValue > INFINITY_THRESHOLD):
                                            data[val] = np.nan
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
                                        # nanSample = self.returnNanSample(lineTime)
                                        # connectionsData[port].append(nanSample)
                                        # infinityReached = True

                                if(not infinityReached):
                                    self.__lastPlotTimestamp[port] = lineTime
                                    connectionsData[port].append(data)

                data = 0
                for port in connectionsData:
                    if(len(connectionsData[port]) > 0):
                        data += 1

                for port in self.options.filterPorts:
                    if(data < 1 and currentTimestamp > self.__lastPlotTimestamp[port] ):
                        if(self.options.debug):
                            print("No data for any connection.")
                        if(self.options.interimBuffering):
                            self.__preloading = True
                        return self.returnAllLines()



336
                # copy raw-value into corresponding lists
337
338
339
340
                for connection in connectionsData:
                    while(len(connectionsData[connection]) > 0):
                        data = connectionsData[connection].popleft()

341
342
                        lineTime = self.__initRealtimeTimestamp  + (float(data['time']) - self.__initSampletimeTimestamp)
                        self.__plotLineConfigs[connection]['lastTimestamp'] = float(data['time'])
343
344
345
346
347

                        for val in VALUES_TO_PROCESS:
                            if(val == 'time'):
                                self.__plotValues[connection][val].append(lineTime)
                            else:
348
349
350
351
352
353
354
355
                                try:
                                    currentVal = float(data[val])
                                except ValueError:
                                    pass
                                else:
                                    self.__plotValues[connection][val].append(currentVal)

                    # update axis (xy-tuple) with data from lists
356
                    for val in VALUES_TO_PLOT:
357
358
                        x, y = self.__plotValues[connection]['time'], self.__plotValues[connection][val]
                        self.__plotLines[connection][val].set_data(x, y)
359
360


361
                self.__lastDrawTimestamp = time.perf_counter()
362

363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
                # y-scaling
                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)
381
382
383

                return self.returnAllLines()

384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
    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

399
400
401
402
403
404
405
406
407
408
    def plotInit(self):
        """Helper to initialize plot."""
        for port in self.options.filterPorts:
            for val in VALUES_TO_PLOT:
                self.__plotLines[port][val].set_data([], [])

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

409
410
        # if(self.options.debug):
        #     print("Plot init done.")
411
412
413
414
415
416
417
418
419
420

        return self.returnAllLines()

    def calculateSampleTimeOffset(self):
        """Calculate SampleTime difference at start"""
        for port in self.options.filterPorts:
            try:
                data = self.__connectionBuffer[port].popleft()
            except IndexError:
                pass
421
422
            except KeyError:
                pass
423
            else:
424
                # print(data)
425
426
427
428
429
                #re-add first sample (to head of dequeue)
                self.__connectionBuffer[port].appendleft(data)
                self.__initSampletimeTimestamp = float(data['time'])
                return