aboutsummaryrefslogtreecommitdiffstats
path: root/src/scripts/host-stack/cc_plots.py
blob: 20f5bd6a3f15dece3598eed1bac0c9386aab4e4e (plain)
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
#!/usr/bin/env python3

import sys
import re
import argparse
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D

class Point():
    "CC event"
    def __init__(self, x, y):
        self.x = x
        self.y = y

def listx(points):
  return list(map(lambda pt: pt.x, points))

def listy(points):
  return list(map(lambda pt: pt.y, points))

def plot_data(d):

  plt.figure(1)

  cwndx = listx(d["cwnd"])
  cwndy = listy(d["cwnd"])
  congx = listx(d["congestion"])
  congy = listy(d["congestion"])
  rcvrdx = listx(d["recovered"])
  rcvrdy = listy(d["recovered"])
  rxttx = listx(d["rxtTimeout"])
  rxtty = listy(d["rxtTimeout"])

  # cwnd/ssthresh/cc events
  plt.subplot(311)
  plt.title("cwnd/ssthresh")
  pcwnd = plt.plot(cwndx, cwndy, 'r')
  psst = plt.plot(cwndx, d["ssthresh"], 'y-')
  pcong = plt.plot(congx, congy,'yo')
  precov = plt.plot(rcvrdx, rcvrdy,'co')
  prxtt = plt.plot(rxttx, rxtty,'mo')

  marker1 = Line2D(range(1), range(1), color="r")
  marker2 = Line2D(range(1), range(1), color="y")
  marker3 = Line2D(range(1), range(1), color="w", marker="o", markerfacecolor="y")
  marker4 = Line2D(range(1), range(1), color="w", marker="o", markerfacecolor="c")
  marker5 = Line2D(range(1), range(1), color="w", marker="o", markerfacecolor="m")
  plt.legend((marker1, marker2, marker3, marker4, marker5),
             ('cwnd', 'ssthresh', 'congestion', 'recovered', 'rxt-timeout'),
             loc=4)
  axes = plt.gca()
  axes.set_ylim([-20e4, max(cwndy) + 20e4])

  # snd variables
  plt.subplot(312)
  plt.title("cc variables")
  plt.plot(cwndx, d["space"], 'g-', markersize=1)
  plt.plot(cwndx, d["flight"], 'b-', markersize=1)
  plt.plot(cwndx, d["sacked"], 'm:', markersize=1)
  plt.plot(cwndx, d["lost"], 'y:', markersize=1)
  plt.plot(cwndx, d["cc-space"], 'k:', markersize=1)
  plt.plot(cwndx, cwndy, 'ro', markersize=2)

  plt.plot(congx, congy, 'y^', markersize=10, markerfacecolor="y")
  plt.plot(rcvrdx, rcvrdy, 'c^', markersize=10, markerfacecolor="c")
  plt.plot(rxttx, rxtty, 'm^', markersize=10, markerfacecolor="m")

  #plt.plot(cwndx, d["snd_wnd"], 'ko', markersize=1)
  plt.legend(("snd-space", "flight", "sacked", "lost", "cc-space", "cwnd",
              "congestion", "recovered", "rxt-timeout"),
             loc=1)

  # rto/srrt/rttvar
  plt.subplot(313)
  plt.title("rtt")
  plt.plot(cwndx, d["srtt"], 'g-')
  plt.plot(cwndx, [x/1000 for x in d["mrtt-us"]], 'r-')
  plt.plot(cwndx, d["rttvar"], 'b-')
  plt.legend(["srtt", "mrtt-us", "rttvar"])
  axes = plt.gca()
  #plt.plot(cwndx, rto, 'r-')
  #axes.set_ylim([0, int(max(rto[2:len(rto)])) + 50])

  # show
  plt.show()

def find_pattern(file_path,session_idx):
    is_active_open = 1
    listener_pattern = "l\[\d\]"
    if (is_active_open):
      initial_pattern = "\[\d\](\.\d+:\d+\->\.\d+:\d+)\s+open:\s"
    else:
      initial_pattern = "\[\d\](\.\d+:\d+\->\.\d+:\d+)\s"
    idx = 0
    f = open(file_path, 'r')
    for line in f:
      # skip listener lines (server)
      if (re.search(listener_pattern, line) != None):
        continue
      match = re.search(initial_pattern, line)
      if (match == None):
        continue
      if (idx < session_idx):
        idx += 1
        continue
      filter_pattern = str(match.group(1)) + "\s+(.+)"
      print ("pattern is %s" % filter_pattern)
      f.close()
      return filter_pattern
    raise Exception ("Could not find initial pattern")

def compute_time(min, sec, msec):
  return int(min)*60 + int(sec) + int(msec)/1000.0

def run(file_path, session_idx):
    filter_sessions = 1
    filter_pattern = ""

    patterns = {
      "time"      : "^\d+:(\d+):(\d+):(\d+):\d+",
      "listener"  : "l\[\d\]",
      "cc"        : "cwnd (\d+) flight (\d+) space (\d+) ssthresh (\d+) snd_wnd (\d+)",
      "cc-snd"    : "cc_space (\d+) sacked (\d+) lost (\d+)",
      "rtt"       : "rto (\d+) srtt (\d+) mrtt-us (\d+) rttvar (\d+)",
      "rxtt"      : "rxt-timeout",
      "congestion": "congestion",
      "recovered" : "recovered",
    }
    d = {
      "cwnd"        : [],
      "space"       : [],
      "flight"      : [],
      "ssthresh"    : [],
      "snd_wnd"     : [],
      "cc-space"    : [],
      "lost"        : [],
      "sacked"      : [],
      "rto"         : [],
      "srtt"        : [],
      "mrtt-us"     : [],
      "rttvar"      : [],
      "rxtTimeout"  : [],
      "congestion"  : [],
      "recovered"   : [],
    }

    if (filter_sessions):
        filter_pattern = find_pattern(file_path, session_idx)
    f = open(file_path, 'r')

    stats_index = 0
    start_time = 0

    for line in f:
        # skip listener lines (server)
        if (re.search(patterns["listener"], line) != None):
            continue
        # filter sessions
        if (filter_sessions):
            match = re.search(filter_pattern, line)
            if (match == None):
                continue

        original_line = line
        line = match.group(1)
        match = re.search (patterns["time"], original_line)
        if (match == None):
          print "something went wrong! no time!"
          continue
        time = compute_time (match.group(1), match.group(2), match.group(3))
        if (start_time == 0):
          start_time = time

        time = time - start_time
        match = re.search(patterns["cc"], line)
        if (match != None):
          d["cwnd"].append(Point(time, int(match.group(1))))
          d["flight"].append(int(match.group(2)))
          d["space"].append(int(match.group(3)))
          d["ssthresh"].append(int(match.group(4)))
          d["snd_wnd"].append(int(match.group(5)))
          stats_index += 1
          continue
        match = re.search(patterns["cc-snd"], line)
        if (match != None):
          d["cc-space"].append(int(match.group(1)))
          d["sacked"].append(int(match.group(2)))
          d["lost"].append(int(match.group(3)))
        match = re.search(patterns["rtt"], line)
        if (match != None):
           d["rto"].append(int(match.group(1)))
           d["srtt"].append(int(match.group(2)))
           d["mrtt-us"].append(int(match.group(3)))
           d["rttvar"].append(int(match.group(4)))
        if (stats_index == 0):
           continue
        match = re.search(patterns["rxtt"], line)
        if (match != None):
          d["rxtTimeout"].append(Point(time, d["cwnd"][stats_index - 1].y + 1e4))
          continue
        match = re.search(patterns["congestion"], line)
        if (match != None):
          d["congestion"].append(Point(time, d["cwnd"][stats_index - 1].y - 1e4))
          continue
        match = re.search(patterns["recovered"], line)
        if (match != None):
          d["recovered"].append(Point(time, d["cwnd"][stats_index - 1].y))
          continue

    plot_data(d)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Plot tcp cc logs")
    parser.add_argument('-f', action='store', dest='file', required=True,
                        help="elog file in txt format")
    parser.add_argument('-s', action='store', dest='session_index', default=0,
                        help="session index for which to plot cc logs" )
    results = parser.parse_args()
    run(results.file, int(results.session_index))