forked from Haivision/srt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
statswriter.cpp
351 lines (294 loc) · 11 KB
/
statswriter.cpp
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
/*
* SRT - Secure, Reliable, Transport
* Copyright (c) 2018 Haivision Systems Inc.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
*/
#include <string>
#include <chrono>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <utility>
#include <memory>
#include "statswriter.hpp"
#include "netinet_any.h"
#include "srt_compat.h"
using namespace std;
template <class TYPE>
inline SrtStatData* make_stat(SrtStatCat cat, const string& name, const string& longname,
TYPE CBytePerfMon::*field)
{
return new SrtStatDataType<TYPE>(cat, name, longname, field);
}
#define STATX(catsuf, sname, lname, field) s.emplace_back(make_stat(SSC_##catsuf, #sname, #lname, &CBytePerfMon:: field))
#define STAT(catsuf, sname, field) STATX(catsuf, sname, field, field)
vector<unique_ptr<SrtStatData>> g_SrtStatsTable;
struct SrtStatsTableInit
{
SrtStatsTableInit(vector<unique_ptr<SrtStatData>>& s)
{
STATX(GEN, time, Time, msTimeStamp);
STAT(WINDOW, flow, pktFlowWindow);
STAT(WINDOW, congestion, pktCongestionWindow);
STAT(WINDOW, flight, pktFlightSize);
STAT(LINK, rtt, msRTT);
STAT(LINK, bandwidth, mbpsBandwidth);
STAT(LINK, maxBandwidth, mbpsMaxBW);
STAT(SEND, packets, pktSent);
STAT(SEND, packetsUnique, pktSentUnique);
STAT(SEND, packetsLost, pktSndLoss);
STAT(SEND, packetsDropped, pktSndDrop);
STAT(SEND, packetsRetransmitted, pktRetrans);
STAT(SEND, packetsFilterExtra, pktSndFilterExtra);
STAT(SEND, bytes, byteSent);
STAT(SEND, bytesUnique, byteSentUnique);
STAT(SEND, bytesDropped, byteSndDrop);
STAT(SEND, byteAvailBuf, byteAvailSndBuf);
STAT(SEND, msBuf, msSndBuf);
STAT(SEND, mbitRate, mbpsSendRate);
STAT(SEND, sendPeriod, usPktSndPeriod);
STAT(RECV, packets, pktRecv);
STAT(RECV, packetsUnique, pktRecvUnique);
STAT(RECV, packetsLost, pktRcvLoss);
STAT(RECV, packetsDropped, pktRcvDrop);
STAT(RECV, packetsRetransmitted, pktRcvRetrans);
STAT(RECV, packetsBelated, pktRcvBelated);
STAT(RECV, packetsFilterExtra, pktRcvFilterExtra);
STAT(RECV, packetsFilterSupply, pktRcvFilterSupply);
STAT(RECV, packetsFilterLoss, pktRcvFilterLoss);
STAT(RECV, bytes, byteRecv);
STAT(RECV, bytesUnique, byteRecvUnique);
STAT(RECV, bytesLost, byteRcvLoss);
STAT(RECV, bytesDropped, byteRcvDrop);
STAT(RECV, byteAvailBuf, byteAvailRcvBuf);
STAT(RECV, msBuf, msRcvBuf);
STAT(RECV, mbitRate, mbpsRecvRate);
STAT(RECV, msTsbPdDelay, msRcvTsbPdDelay);
}
} g_SrtStatsTableInit (g_SrtStatsTable);
#undef STAT
#undef STATX
string srt_json_cat_names [] = {
"",
"window",
"link",
"send",
"recv"
};
#ifdef HAVE_CXX_STD_PUT_TIME
// Follows ISO 8601
std::string SrtStatsWriter::print_timestamp()
{
using namespace std;
using namespace std::chrono;
const auto systime_now = system_clock::now();
const time_t time_now = system_clock::to_time_t(systime_now);
std::ostringstream output;
// SysLocalTime returns zeroed tm_now on failure, which is ok for put_time.
const tm tm_now = SysLocalTime(time_now);
output << std::put_time(&tm_now, "%FT%T.") << std::setfill('0') << std::setw(6);
const auto since_epoch = systime_now.time_since_epoch();
const seconds s = duration_cast<seconds>(since_epoch);
output << duration_cast<microseconds>(since_epoch - s).count();
output << std::put_time(&tm_now, "%z");
return output.str();
}
#else
// This is a stub. The error when not defining it would be too
// misleading, so this stub will work if someone mistakenly adds
// the item to the output format without checking that HAVE_CXX_STD_PUT_TIME
string SrtStatsWriter::print_timestamp()
{ return "<NOT IMPLEMENTED>"; }
#endif // HAVE_CXX_STD_PUT_TIME
class SrtStatsJson : public SrtStatsWriter
{
static string quotekey(const string& name)
{
if (name == "")
return "";
return R"(")" + name + R"(":)";
}
static string quote(const string& name)
{
if (name == "")
return "";
return R"(")" + name + R"(")";
}
public:
string WriteStats(int sid, const CBytePerfMon& mon) override
{
std::ostringstream output;
string pretty_cr, pretty_tab;
if (Option("pretty"))
{
pretty_cr = "\n";
pretty_tab = "\t";
}
SrtStatCat cat = SSC_GEN;
// Do general manually
output << quotekey(srt_json_cat_names[cat]) << "{" << pretty_cr;
// SID is displayed manually
output << pretty_tab << quotekey("sid") << sid;
// Extra Timepoint is also displayed manually
#ifdef HAVE_CXX_STD_PUT_TIME
// NOTE: still assumed SSC_GEN category
output << "," << pretty_cr << pretty_tab
<< quotekey("timepoint") << quote(print_timestamp());
#endif
// Now continue with fields as specified in the table
for (auto& i: g_SrtStatsTable)
{
if (i->category == cat)
{
output << ","; // next item in same cat
output << pretty_cr;
output << pretty_tab;
if (cat != SSC_GEN)
output << pretty_tab;
}
else
{
if (cat != SSC_GEN)
{
// DO NOT close if general category, just
// enter the depth.
output << pretty_cr << pretty_tab << "}";
}
cat = i->category;
output << ",";
output << pretty_cr;
if (cat != SSC_GEN)
output << pretty_tab;
output << quotekey(srt_json_cat_names[cat]) << "{" << pretty_cr << pretty_tab;
if (cat != SSC_GEN)
output << pretty_tab;
}
// Print the current field
output << quotekey(i->name);
i->PrintValue(output, mon);
}
// Close the previous subcategory
if (cat != SSC_GEN)
{
output << pretty_cr << pretty_tab << "}" << pretty_cr;
}
// Close the general category entity
output << "}" << pretty_cr << endl;
return output.str();
}
string WriteBandwidth(double mbpsBandwidth) override
{
std::ostringstream output;
output << "{\"bandwidth\":" << mbpsBandwidth << '}' << endl;
return output.str();
}
};
class SrtStatsCsv : public SrtStatsWriter
{
private:
bool first_line_printed;
public:
SrtStatsCsv() : first_line_printed(false) {}
string WriteStats(int sid, const CBytePerfMon& mon) override
{
std::ostringstream output;
// Header
if (!first_line_printed)
{
#ifdef HAVE_CXX_STD_PUT_TIME
output << "Timepoint,";
#endif
output << "Time,SocketID";
for (auto& i: g_SrtStatsTable)
{
output << "," << i->longname;
}
output << endl;
first_line_printed = true;
}
// Values
#ifdef HAVE_CXX_STD_PUT_TIME
// HDR: Timepoint
output << print_timestamp() << ",";
#endif // HAVE_CXX_STD_PUT_TIME
// HDR: Time,SocketID
output << mon.msTimeStamp << "," << sid;
// HDR: the loop of all values in g_SrtStatsTable
for (auto& i: g_SrtStatsTable)
{
output << ",";
i->PrintValue(output, mon);
}
output << endl;
return output.str();
}
string WriteBandwidth(double mbpsBandwidth) override
{
std::ostringstream output;
output << "+++/+++SRT BANDWIDTH: " << mbpsBandwidth << endl;
return output.str();
}
};
class SrtStatsCols : public SrtStatsWriter
{
public:
string WriteStats(int sid, const CBytePerfMon& mon) override
{
std::ostringstream output;
output << "======= SRT STATS: sid=" << sid << endl;
output << "PACKETS SENT: " << setw(11) << mon.pktSent << " RECEIVED: " << setw(11) << mon.pktRecv << endl;
output << "LOST PKT SENT: " << setw(11) << mon.pktSndLoss << " RECEIVED: " << setw(11) << mon.pktRcvLoss << endl;
output << "REXMIT SENT: " << setw(11) << mon.pktRetrans << " RECEIVED: " << setw(11) << mon.pktRcvRetrans << endl;
output << "DROP PKT SENT: " << setw(11) << mon.pktSndDrop << " RECEIVED: " << setw(11) << mon.pktRcvDrop << endl;
output << "FILTER EXTRA TX: " << setw(11) << mon.pktSndFilterExtra << " RX: " << setw(11) << mon.pktRcvFilterExtra << endl;
output << "FILTER RX SUPPL: " << setw(11) << mon.pktRcvFilterSupply << " RX LOSS: " << setw(11) << mon.pktRcvFilterLoss << endl;
output << "RATE SENDING: " << setw(11) << mon.mbpsSendRate << " RECEIVING: " << setw(11) << mon.mbpsRecvRate << endl;
output << "BELATED RECEIVED: " << setw(11) << mon.pktRcvBelated << " AVG TIME: " << setw(11) << mon.pktRcvAvgBelatedTime << endl;
output << "REORDER DISTANCE: " << setw(11) << mon.pktReorderDistance << endl;
output << "WINDOW FLOW: " << setw(11) << mon.pktFlowWindow << " CONGESTION: " << setw(11) << mon.pktCongestionWindow << " FLIGHT: " << setw(11) << mon.pktFlightSize << endl;
output << "LINK RTT: " << setw(9) << mon.msRTT << "ms BANDWIDTH: " << setw(7) << mon.mbpsBandwidth << "Mb/s " << endl;
output << "BUFFERLEFT: SND: " << setw(11) << mon.byteAvailSndBuf << " RCV: " << setw(11) << mon.byteAvailRcvBuf << endl;
return output.str();
}
string WriteBandwidth(double mbpsBandwidth) override
{
std::ostringstream output;
output << "+++/+++SRT BANDWIDTH: " << mbpsBandwidth << endl;
return output.str();
}
};
shared_ptr<SrtStatsWriter> SrtStatsWriterFactory(SrtStatsPrintFormat printformat)
{
switch (printformat)
{
case SRTSTATS_PROFMAT_JSON:
return make_shared<SrtStatsJson>();
case SRTSTATS_PROFMAT_CSV:
return make_shared<SrtStatsCsv>();
case SRTSTATS_PROFMAT_2COLS:
return make_shared<SrtStatsCols>();
default:
break;
}
return nullptr;
}
SrtStatsPrintFormat ParsePrintFormat(string pf, string& w_extras)
{
size_t havecomma = pf.find(',');
if (havecomma != string::npos)
{
w_extras = pf.substr(havecomma+1);
pf = pf.substr(0, havecomma);
}
if (pf == "default")
return SRTSTATS_PROFMAT_2COLS;
if (pf == "json")
return SRTSTATS_PROFMAT_JSON;
if (pf == "csv")
return SRTSTATS_PROFMAT_CSV;
return SRTSTATS_PROFMAT_INVALID;
}