| 1 |
/* |
| 2 |
* SRT - Secure, Reliable, Transport |
| 3 |
* Copyright (c) 2018 Haivision Systems Inc. |
| 4 |
* |
| 5 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 6 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 |
* |
| 9 |
*/ |
| 10 |
|
| 11 |
#include <string> |
| 12 |
#include <chrono> |
| 13 |
#include <iostream> |
| 14 |
#include <iomanip> |
| 15 |
#include <sstream> |
| 16 |
#include <utility> |
| 17 |
#include <memory> |
| 18 |
|
| 19 |
#include "statswriter.hpp" |
| 20 |
#include "netinet_any.h" |
| 21 |
#include "srt_compat.h" |
| 22 |
|
| 23 |
|
| 24 |
using namespace std; |
| 25 |
|
| 26 |
|
| 27 |
template <class TYPE> |
| 28 |
inline SrtStatData* make_stat(SrtStatCat cat, const string& name, const string& longname, |
| 29 |
TYPE CBytePerfMon::*field) |
| 30 |
{ |
| 31 |
return new SrtStatDataType<TYPE>(cat, name, longname, field); |
| 32 |
} |
| 33 |
|
| 34 |
#define STATX(catsuf, sname, lname, field) s.emplace_back(make_stat(SSC_##catsuf, #sname, #lname, &CBytePerfMon:: field)) |
| 35 |
#define STAT(catsuf, sname, field) STATX(catsuf, sname, field, field) |
| 36 |
|
| 37 |
vector<unique_ptr<SrtStatData>> g_SrtStatsTable; |
| 38 |
|
| 39 |
struct SrtStatsTableInit |
| 40 |
{ |
| 41 |
SrtStatsTableInit(vector<unique_ptr<SrtStatData>>& s) |
| 42 |
{ |
| 43 |
STATX(GEN, time, Time, msTimeStamp); |
| 44 |
|
| 45 |
STAT(WINDOW, flow, pktFlowWindow); |
| 46 |
STAT(WINDOW, congestion, pktCongestionWindow); |
| 47 |
STAT(WINDOW, flight, pktFlightSize); |
| 48 |
|
| 49 |
STAT(LINK, rtt, msRTT); |
| 50 |
STAT(LINK, bandwidth, mbpsBandwidth); |
| 51 |
STAT(LINK, maxBandwidth, mbpsMaxBW); |
| 52 |
|
| 53 |
STAT(SEND, packets, pktSent); |
| 54 |
STAT(SEND, packetsUnique, pktSentUnique); |
| 55 |
STAT(SEND, packetsLost, pktSndLoss); |
| 56 |
STAT(SEND, packetsDropped, pktSndDrop); |
| 57 |
STAT(SEND, packetsRetransmitted, pktRetrans); |
| 58 |
STAT(SEND, packetsFilterExtra, pktSndFilterExtra); |
| 59 |
STAT(SEND, bytes, byteSent); |
| 60 |
STAT(SEND, bytesUnique, byteSentUnique); |
| 61 |
STAT(SEND, bytesDropped, byteSndDrop); |
| 62 |
STAT(SEND, byteAvailBuf, byteAvailSndBuf); |
| 63 |
STAT(SEND, msBuf, msSndBuf); |
| 64 |
STAT(SEND, mbitRate, mbpsSendRate); |
| 65 |
STAT(SEND, sendPeriod, usPktSndPeriod); |
| 66 |
|
| 67 |
STAT(RECV, packets, pktRecv); |
| 68 |
STAT(RECV, packetsUnique, pktRecvUnique); |
| 69 |
STAT(RECV, packetsLost, pktRcvLoss); |
| 70 |
STAT(RECV, packetsDropped, pktRcvDrop); |
| 71 |
STAT(RECV, packetsRetransmitted, pktRcvRetrans); |
| 72 |
STAT(RECV, packetsBelated, pktRcvBelated); |
| 73 |
STAT(RECV, packetsFilterExtra, pktRcvFilterExtra); |
| 74 |
STAT(RECV, packetsFilterSupply, pktRcvFilterSupply); |
| 75 |
STAT(RECV, packetsFilterLoss, pktRcvFilterLoss); |
| 76 |
STAT(RECV, bytes, byteRecv); |
| 77 |
STAT(RECV, bytesUnique, byteRecvUnique); |
| 78 |
STAT(RECV, bytesLost, byteRcvLoss); |
| 79 |
STAT(RECV, bytesDropped, byteRcvDrop); |
| 80 |
STAT(RECV, byteAvailBuf, byteAvailRcvBuf); |
| 81 |
STAT(RECV, msBuf, msRcvBuf); |
| 82 |
STAT(RECV, mbitRate, mbpsRecvRate); |
| 83 |
STAT(RECV, msTsbPdDelay, msRcvTsbPdDelay); |
| 84 |
} |
| 85 |
} g_SrtStatsTableInit (g_SrtStatsTable); |
| 86 |
|
| 87 |
|
| 88 |
#undef STAT |
| 89 |
#undef STATX |
| 90 |
|
| 91 |
string srt_json_cat_names [] = { |
| 92 |
"", |
| 93 |
"window", |
| 94 |
"link", |
| 95 |
"send", |
| 96 |
"recv" |
| 97 |
}; |
| 98 |
|
| 99 |
#ifdef HAVE_CXX_STD_PUT_TIME |
| 100 |
// Follows ISO 8601 |
| 101 |
std::string SrtStatsWriter::print_timestamp() |
| 102 |
{ |
| 103 |
using namespace std; |
| 104 |
using namespace std::chrono; |
| 105 |
|
| 106 |
const auto systime_now = system_clock::now(); |
| 107 |
const time_t time_now = system_clock::to_time_t(systime_now); |
| 108 |
|
| 109 |
std::ostringstream output; |
| 110 |
|
| 111 |
// SysLocalTime returns zeroed tm_now on failure, which is ok for put_time. |
| 112 |
const tm tm_now = SysLocalTime(time_now); |
| 113 |
output << std::put_time(&tm_now, "%FT%T.") << std::setfill('0') << std::setw(6); |
| 114 |
const auto since_epoch = systime_now.time_since_epoch(); |
| 115 |
const seconds s = duration_cast<seconds>(since_epoch); |
| 116 |
output << duration_cast<microseconds>(since_epoch - s).count(); |
| 117 |
output << std::put_time(&tm_now, "%z"); |
| 118 |
return output.str(); |
| 119 |
} |
| 120 |
#else |
| 121 |
|
| 122 |
// This is a stub. The error when not defining it would be too |
| 123 |
// misleading, so this stub will work if someone mistakenly adds |
| 124 |
// the item to the output format without checking that HAVE_CXX_STD_PUT_TIME |
| 125 |
string SrtStatsWriter::print_timestamp() |
| 126 |
{ return "<NOT IMPLEMENTED>"; } |
| 127 |
#endif // HAVE_CXX_STD_PUT_TIME |
| 128 |
|
| 129 |
|
| 130 |
class SrtStatsJson : public SrtStatsWriter |
| 131 |
{ |
| 132 |
static string quotekey(const string& name) |
| 133 |
{ |
| 134 |
if (name == "") |
| 135 |
return ""; |
| 136 |
|
| 137 |
return R"(")" + name + R"(":)"; |
| 138 |
} |
| 139 |
|
| 140 |
static string quote(const string& name) |
| 141 |
{ |
| 142 |
if (name == "") |
| 143 |
return ""; |
| 144 |
|
| 145 |
return R"(")" + name + R"(")"; |
| 146 |
} |
| 147 |
|
| 148 |
public: |
| 149 |
string WriteStats(int sid, const CBytePerfMon& mon) override |
| 150 |
{ |
| 151 |
std::ostringstream output; |
| 152 |
|
| 153 |
string pretty_cr, pretty_tab; |
| 154 |
if (Option("pretty")) |
| 155 |
{ |
| 156 |
pretty_cr = "\n"; |
| 157 |
pretty_tab = "\t"; |
| 158 |
} |
| 159 |
|
| 160 |
SrtStatCat cat = SSC_GEN; |
| 161 |
|
| 162 |
// Do general manually |
| 163 |
output << quotekey(srt_json_cat_names[cat]) << "{" << pretty_cr; |
| 164 |
|
| 165 |
// SID is displayed manually |
| 166 |
output << pretty_tab << quotekey("sid") << sid; |
| 167 |
|
| 168 |
// Extra Timepoint is also displayed manually |
| 169 |
#ifdef HAVE_CXX_STD_PUT_TIME |
| 170 |
// NOTE: still assumed SSC_GEN category |
| 171 |
output << "," << pretty_cr << pretty_tab |
| 172 |
<< quotekey("timepoint") << quote(print_timestamp()); |
| 173 |
#endif |
| 174 |
|
| 175 |
// Now continue with fields as specified in the table |
| 176 |
for (auto& i: g_SrtStatsTable) |
| 177 |
{ |
| 178 |
if (i->category == cat) |
| 179 |
{ |
| 180 |
output << ","; // next item in same cat |
| 181 |
output << pretty_cr; |
| 182 |
output << pretty_tab; |
| 183 |
if (cat != SSC_GEN) |
| 184 |
output << pretty_tab; |
| 185 |
} |
| 186 |
else |
| 187 |
{ |
| 188 |
if (cat != SSC_GEN) |
| 189 |
{ |
| 190 |
// DO NOT close if general category, just |
| 191 |
// enter the depth. |
| 192 |
output << pretty_cr << pretty_tab << "}"; |
| 193 |
} |
| 194 |
cat = i->category; |
| 195 |
output << ","; |
| 196 |
output << pretty_cr; |
| 197 |
if (cat != SSC_GEN) |
| 198 |
output << pretty_tab; |
| 199 |
|
| 200 |
output << quotekey(srt_json_cat_names[cat]) << "{" << pretty_cr << pretty_tab; |
| 201 |
if (cat != SSC_GEN) |
| 202 |
output << pretty_tab; |
| 203 |
} |
| 204 |
|
| 205 |
// Print the current field |
| 206 |
output << quotekey(i->name); |
| 207 |
i->PrintValue(output, mon); |
| 208 |
} |
| 209 |
|
| 210 |
// Close the previous subcategory |
| 211 |
if (cat != SSC_GEN) |
| 212 |
{ |
| 213 |
output << pretty_cr << pretty_tab << "}" << pretty_cr; |
| 214 |
} |
| 215 |
|
| 216 |
// Close the general category entity |
| 217 |
output << "}" << pretty_cr << endl; |
| 218 |
|
| 219 |
return output.str(); |
| 220 |
} |
| 221 |
|
| 222 |
string WriteBandwidth(double mbpsBandwidth) override |
| 223 |
{ |
| 224 |
std::ostringstream output; |
| 225 |
output << "{\"bandwidth\":" << mbpsBandwidth << '}' << endl; |
| 226 |
return output.str(); |
| 227 |
} |
| 228 |
}; |
| 229 |
|
| 230 |
class SrtStatsCsv : public SrtStatsWriter |
| 231 |
{ |
| 232 |
private: |
| 233 |
bool first_line_printed; |
| 234 |
|
| 235 |
public: |
| 236 |
SrtStatsCsv() : first_line_printed(false) {} |
| 237 |
|
| 238 |
string WriteStats(int sid, const CBytePerfMon& mon) override |
| 239 |
{ |
| 240 |
std::ostringstream output; |
| 241 |
|
| 242 |
// Header |
| 243 |
if (!first_line_printed) |
| 244 |
{ |
| 245 |
#ifdef HAVE_CXX_STD_PUT_TIME |
| 246 |
output << "Timepoint,"; |
| 247 |
#endif |
| 248 |
output << "Time,SocketID"; |
| 249 |
|
| 250 |
for (auto& i: g_SrtStatsTable) |
| 251 |
{ |
| 252 |
output << "," << i->longname; |
| 253 |
} |
| 254 |
output << endl; |
| 255 |
first_line_printed = true; |
| 256 |
} |
| 257 |
|
| 258 |
// Values |
| 259 |
#ifdef HAVE_CXX_STD_PUT_TIME |
| 260 |
// HDR: Timepoint |
| 261 |
output << print_timestamp() << ","; |
| 262 |
#endif // HAVE_CXX_STD_PUT_TIME |
| 263 |
|
| 264 |
// HDR: Time,SocketID |
| 265 |
output << mon.msTimeStamp << "," << sid; |
| 266 |
|
| 267 |
// HDR: the loop of all values in g_SrtStatsTable |
| 268 |
for (auto& i: g_SrtStatsTable) |
| 269 |
{ |
| 270 |
output << ","; |
| 271 |
i->PrintValue(output, mon); |
| 272 |
} |
| 273 |
|
| 274 |
output << endl; |
| 275 |
return output.str(); |
| 276 |
} |
| 277 |
|
| 278 |
string WriteBandwidth(double mbpsBandwidth) override |
| 279 |
{ |
| 280 |
std::ostringstream output; |
| 281 |
output << "+++/+++SRT BANDWIDTH: " << mbpsBandwidth << endl; |
| 282 |
return output.str(); |
| 283 |
} |
| 284 |
}; |
| 285 |
|
| 286 |
class SrtStatsCols : public SrtStatsWriter |
| 287 |
{ |
| 288 |
public: |
| 289 |
string WriteStats(int sid, const CBytePerfMon& mon) override |
| 290 |
{ |
| 291 |
std::ostringstream output; |
| 292 |
output << "======= SRT STATS: sid=" << sid << endl; |
| 293 |
output << "PACKETS SENT: " << setw(11) << mon.pktSent << " RECEIVED: " << setw(11) << mon.pktRecv << endl; |
| 294 |
output << "LOST PKT SENT: " << setw(11) << mon.pktSndLoss << " RECEIVED: " << setw(11) << mon.pktRcvLoss << endl; |
| 295 |
output << "REXMIT SENT: " << setw(11) << mon.pktRetrans << " RECEIVED: " << setw(11) << mon.pktRcvRetrans << endl; |
| 296 |
output << "DROP PKT SENT: " << setw(11) << mon.pktSndDrop << " RECEIVED: " << setw(11) << mon.pktRcvDrop << endl; |
| 297 |
output << "FILTER EXTRA TX: " << setw(11) << mon.pktSndFilterExtra << " RX: " << setw(11) << mon.pktRcvFilterExtra << endl; |
| 298 |
output << "FILTER RX SUPPL: " << setw(11) << mon.pktRcvFilterSupply << " RX LOSS: " << setw(11) << mon.pktRcvFilterLoss << endl; |
| 299 |
output << "RATE SENDING: " << setw(11) << mon.mbpsSendRate << " RECEIVING: " << setw(11) << mon.mbpsRecvRate << endl; |
| 300 |
output << "BELATED RECEIVED: " << setw(11) << mon.pktRcvBelated << " AVG TIME: " << setw(11) << mon.pktRcvAvgBelatedTime << endl; |
| 301 |
output << "REORDER DISTANCE: " << setw(11) << mon.pktReorderDistance << endl; |
| 302 |
output << "WINDOW FLOW: " << setw(11) << mon.pktFlowWindow << " CONGESTION: " << setw(11) << mon.pktCongestionWindow << " FLIGHT: " << setw(11) << mon.pktFlightSize << endl; |
| 303 |
output << "LINK RTT: " << setw(9) << mon.msRTT << "ms BANDWIDTH: " << setw(7) << mon.mbpsBandwidth << "Mb/s " << endl; |
| 304 |
output << "BUFFERLEFT: SND: " << setw(11) << mon.byteAvailSndBuf << " RCV: " << setw(11) << mon.byteAvailRcvBuf << endl; |
| 305 |
return output.str(); |
| 306 |
} |
| 307 |
|
| 308 |
string WriteBandwidth(double mbpsBandwidth) override |
| 309 |
{ |
| 310 |
std::ostringstream output; |
| 311 |
output << "+++/+++SRT BANDWIDTH: " << mbpsBandwidth << endl; |
| 312 |
return output.str(); |
| 313 |
} |
| 314 |
}; |
| 315 |
|
| 316 |
shared_ptr<SrtStatsWriter> SrtStatsWriterFactory(SrtStatsPrintFormat printformat) |
| 317 |
{ |
| 318 |
switch (printformat) |
| 319 |
{ |
| 320 |
case SRTSTATS_PROFMAT_JSON: |
| 321 |
return make_shared<SrtStatsJson>(); |
| 322 |
case SRTSTATS_PROFMAT_CSV: |
| 323 |
return make_shared<SrtStatsCsv>(); |
| 324 |
case SRTSTATS_PROFMAT_2COLS: |
| 325 |
return make_shared<SrtStatsCols>(); |
| 326 |
default: |
| 327 |
break; |
| 328 |
} |
| 329 |
return nullptr; |
| 330 |
} |
| 331 |
|
| 332 |
SrtStatsPrintFormat ParsePrintFormat(string pf, string& w_extras) |
| 333 |
{ |
| 334 |
size_t havecomma = pf.find(','); |
| 335 |
if (havecomma != string::npos) |
| 336 |
{ |
| 337 |
w_extras = pf.substr(havecomma+1); |
| 338 |
pf = pf.substr(0, havecomma); |
| 339 |
} |
| 340 |
|
| 341 |
if (pf == "default") |
| 342 |
return SRTSTATS_PROFMAT_2COLS; |
| 343 |
|
| 344 |
if (pf == "json") |
| 345 |
return SRTSTATS_PROFMAT_JSON; |
| 346 |
|
| 347 |
if (pf == "csv") |
| 348 |
return SRTSTATS_PROFMAT_CSV; |
| 349 |
|
| 350 |
return SRTSTATS_PROFMAT_INVALID; |
| 351 |
} |