tests docs: update python3 venv packages
[vpp.git] / src / scripts / host-stack / cc_plots.py
1 #!/usr/bin/env python3
2
3 import sys
4 import re
5 import argparse
6 import matplotlib.pyplot as plt
7 from matplotlib.lines import Line2D
8
9
10 class Point:
11     "CC event"
12
13     def __init__(self, x, y):
14         self.x = x
15         self.y = y
16
17
18 def listx(points):
19     return list(map(lambda pt: pt.x, points))
20
21
22 def listy(points):
23     return list(map(lambda pt: pt.y, points))
24
25
26 def plot_data(d):
27     plt.figure(1)
28
29     cwndx = listx(d["cwnd"])
30     cwndy = listy(d["cwnd"])
31     congx = listx(d["congestion"])
32     congy = listy(d["congestion"])
33     rcvrdx = listx(d["recovered"])
34     rcvrdy = listy(d["recovered"])
35     rxttx = listx(d["rxtTimeout"])
36     rxtty = listy(d["rxtTimeout"])
37
38     # cwnd/ssthresh/cc events
39     plt.subplot(311)
40     plt.title("cwnd/ssthresh")
41     pcwnd = plt.plot(cwndx, cwndy, "r")
42     psst = plt.plot(cwndx, d["ssthresh"], "y-")
43     pcong = plt.plot(congx, congy, "yo")
44     precov = plt.plot(rcvrdx, rcvrdy, "co")
45     prxtt = plt.plot(rxttx, rxtty, "mo")
46
47     marker1 = Line2D(range(1), range(1), color="r")
48     marker2 = Line2D(range(1), range(1), color="y")
49     marker3 = Line2D(range(1), range(1), color="w", marker="o", markerfacecolor="y")
50     marker4 = Line2D(range(1), range(1), color="w", marker="o", markerfacecolor="c")
51     marker5 = Line2D(range(1), range(1), color="w", marker="o", markerfacecolor="m")
52     plt.legend(
53         (marker1, marker2, marker3, marker4, marker5),
54         ("cwnd", "ssthresh", "congestion", "recovered", "rxt-timeout"),
55         loc=4,
56     )
57     axes = plt.gca()
58     axes.set_ylim([-20e4, max(cwndy) + 20e4])
59
60     # snd variables
61     plt.subplot(312)
62     plt.title("cc variables")
63     plt.plot(cwndx, d["space"], "g-", markersize=1)
64     plt.plot(cwndx, d["flight"], "b-", markersize=1)
65     plt.plot(cwndx, d["sacked"], "m:", markersize=1)
66     plt.plot(cwndx, d["lost"], "y:", markersize=1)
67     plt.plot(cwndx, d["cc-space"], "k:", markersize=1)
68     plt.plot(cwndx, cwndy, "ro", markersize=2)
69
70     plt.plot(congx, congy, "y^", markersize=10, markerfacecolor="y")
71     plt.plot(rcvrdx, rcvrdy, "c^", markersize=10, markerfacecolor="c")
72     plt.plot(rxttx, rxtty, "m^", markersize=10, markerfacecolor="m")
73
74     # plt.plot(cwndx, d["snd_wnd"], 'ko', markersize=1)
75     plt.legend(
76         (
77             "snd-space",
78             "flight",
79             "sacked",
80             "lost",
81             "cc-space",
82             "cwnd",
83             "congestion",
84             "recovered",
85             "rxt-timeout",
86         ),
87         loc=1,
88     )
89
90     # rto/srrt/rttvar
91     plt.subplot(313)
92     plt.title("rtt")
93     plt.plot(cwndx, d["srtt"], "g-")
94     plt.plot(cwndx, [x / 1000 for x in d["mrtt-us"]], "r-")
95     plt.plot(cwndx, d["rttvar"], "b-")
96     plt.legend(["srtt", "mrtt-us", "rttvar"])
97     axes = plt.gca()
98     # plt.plot(cwndx, rto, 'r-')
99     # axes.set_ylim([0, int(max(rto[2:len(rto)])) + 50])
100
101     # show
102     plt.show()
103
104
105 def find_pattern(file_path, session_idx):
106     is_active_open = 1
107     listener_pattern = "l\[\d\]"
108     if is_active_open:
109         initial_pattern = "\[\d\](\.\d+:\d+\->\.\d+:\d+)\s+open:\s"
110     else:
111         initial_pattern = "\[\d\](\.\d+:\d+\->\.\d+:\d+)\s"
112     idx = 0
113     f = open(file_path, "r")
114     for line in f:
115         # skip listener lines (server)
116         if re.search(listener_pattern, line) != None:
117             continue
118         match = re.search(initial_pattern, line)
119         if match == None:
120             continue
121         if idx < session_idx:
122             idx += 1
123             continue
124         filter_pattern = str(match.group(1)) + "\s+(.+)"
125         print("pattern is %s" % filter_pattern)
126         f.close()
127         return filter_pattern
128     raise Exception("Could not find initial pattern")
129
130
131 def compute_time(min, sec, msec):
132     return int(min) * 60 + int(sec) + int(msec) / 1000.0
133
134
135 def run(file_path, session_idx):
136     filter_sessions = 1
137     filter_pattern = ""
138
139     patterns = {
140         "time": "^\d+:(\d+):(\d+):(\d+):\d+",
141         "listener": "l\[\d\]",
142         "cc": "cwnd (\d+) flight (\d+) space (\d+) ssthresh (\d+) snd_wnd (\d+)",
143         "cc-snd": "cc_space (\d+) sacked (\d+) lost (\d+)",
144         "rtt": "rto (\d+) srtt (\d+) mrtt-us (\d+) rttvar (\d+)",
145         "rxtt": "rxt-timeout",
146         "congestion": "congestion",
147         "recovered": "recovered",
148     }
149     d = {
150         "cwnd": [],
151         "space": [],
152         "flight": [],
153         "ssthresh": [],
154         "snd_wnd": [],
155         "cc-space": [],
156         "lost": [],
157         "sacked": [],
158         "rto": [],
159         "srtt": [],
160         "mrtt-us": [],
161         "rttvar": [],
162         "rxtTimeout": [],
163         "congestion": [],
164         "recovered": [],
165     }
166
167     if filter_sessions:
168         filter_pattern = find_pattern(file_path, session_idx)
169     f = open(file_path, "r")
170
171     stats_index = 0
172     start_time = 0
173
174     for line in f:
175         # skip listener lines (server)
176         if re.search(patterns["listener"], line) != None:
177             continue
178         # filter sessions
179         if filter_sessions:
180             match = re.search(filter_pattern, line)
181             if match == None:
182                 continue
183
184         original_line = line
185         line = match.group(1)
186         match = re.search(patterns["time"], original_line)
187         if match == None:
188             print("something went wrong! no time!")
189             continue
190         time = compute_time(match.group(1), match.group(2), match.group(3))
191         if start_time == 0:
192             start_time = time
193
194         time = time - start_time
195         match = re.search(patterns["cc"], line)
196         if match != None:
197             d["cwnd"].append(Point(time, int(match.group(1))))
198             d["flight"].append(int(match.group(2)))
199             d["space"].append(int(match.group(3)))
200             d["ssthresh"].append(int(match.group(4)))
201             d["snd_wnd"].append(int(match.group(5)))
202             stats_index += 1
203             continue
204         match = re.search(patterns["cc-snd"], line)
205         if match != None:
206             d["cc-space"].append(int(match.group(1)))
207             d["sacked"].append(int(match.group(2)))
208             d["lost"].append(int(match.group(3)))
209         match = re.search(patterns["rtt"], line)
210         if match != None:
211             d["rto"].append(int(match.group(1)))
212             d["srtt"].append(int(match.group(2)))
213             d["mrtt-us"].append(int(match.group(3)))
214             d["rttvar"].append(int(match.group(4)))
215         if stats_index == 0:
216             continue
217         match = re.search(patterns["rxtt"], line)
218         if match != None:
219             d["rxtTimeout"].append(Point(time, d["cwnd"][stats_index - 1].y + 1e4))
220             continue
221         match = re.search(patterns["congestion"], line)
222         if match != None:
223             d["congestion"].append(Point(time, d["cwnd"][stats_index - 1].y - 1e4))
224             continue
225         match = re.search(patterns["recovered"], line)
226         if match != None:
227             d["recovered"].append(Point(time, d["cwnd"][stats_index - 1].y))
228             continue
229
230     plot_data(d)
231
232
233 if __name__ == "__main__":
234     parser = argparse.ArgumentParser(description="Plot tcp cc logs")
235     parser.add_argument(
236         "-f", action="store", dest="file", required=True, help="elog file in txt format"
237     )
238     parser.add_argument(
239         "-s",
240         action="store",
241         dest="session_index",
242         default=0,
243         help="session index for which to plot cc logs",
244     )
245     results = parser.parse_args()
246     run(results.file, int(results.session_index))