stats.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * SRT - Secure, Reliable, Transport
  3. * Copyright (c) 2021 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. #ifndef INC_SRT_STATS_H
  11. #define INC_SRT_STATS_H
  12. #include "platform_sys.h"
  13. #include "packet.h"
  14. namespace srt
  15. {
  16. namespace stats
  17. {
  18. class Packets
  19. {
  20. public:
  21. Packets() : m_count(0) {}
  22. Packets(uint32_t num) : m_count(num) {}
  23. void reset()
  24. {
  25. m_count = 0;
  26. }
  27. Packets& operator+= (const Packets& other)
  28. {
  29. m_count += other.m_count;
  30. return *this;
  31. }
  32. uint32_t count() const
  33. {
  34. return m_count;
  35. }
  36. private:
  37. uint32_t m_count;
  38. };
  39. class BytesPackets
  40. {
  41. public:
  42. BytesPackets()
  43. : m_bytes(0)
  44. , m_packets(0)
  45. {}
  46. BytesPackets(uint64_t bytes, uint32_t n = 1)
  47. : m_bytes(bytes)
  48. , m_packets(n)
  49. {}
  50. BytesPackets& operator+= (const BytesPackets& other)
  51. {
  52. m_bytes += other.m_bytes;
  53. m_packets += other.m_packets;
  54. return *this;
  55. }
  56. public:
  57. void reset()
  58. {
  59. m_packets = 0;
  60. m_bytes = 0;
  61. }
  62. void count(uint64_t bytes, size_t n = 1)
  63. {
  64. m_packets += (uint32_t) n;
  65. m_bytes += bytes;
  66. }
  67. uint64_t bytes() const
  68. {
  69. return m_bytes;
  70. }
  71. uint32_t count() const
  72. {
  73. return m_packets;
  74. }
  75. uint64_t bytesWithHdr() const
  76. {
  77. return m_bytes + m_packets * CPacket::SRT_DATA_HDR_SIZE;
  78. }
  79. private:
  80. uint64_t m_bytes;
  81. uint32_t m_packets;
  82. };
  83. template <class METRIC_TYPE>
  84. struct Metric
  85. {
  86. METRIC_TYPE trace;
  87. METRIC_TYPE total;
  88. void count(METRIC_TYPE val)
  89. {
  90. trace += val;
  91. total += val;
  92. }
  93. void reset()
  94. {
  95. trace.reset();
  96. total.reset();
  97. }
  98. void resetTrace()
  99. {
  100. trace.reset();
  101. }
  102. };
  103. /// Sender-side statistics.
  104. struct Sender
  105. {
  106. Metric<BytesPackets> sent;
  107. Metric<BytesPackets> sentUnique;
  108. Metric<BytesPackets> sentRetrans; // The number of data packets retransmitted by the sender.
  109. Metric<Packets> lost; // The number of packets reported lost (including repeated reports) to the sender in NAKs.
  110. Metric<BytesPackets> dropped; // The number of data packets dropped by the sender.
  111. Metric<Packets> sentFilterExtra; // The number of packets generate by the packet filter and sent by the sender.
  112. Metric<Packets> recvdAck; // The number of ACK packets received by the sender.
  113. Metric<Packets> recvdNak; // The number of ACK packets received by the sender.
  114. void reset()
  115. {
  116. sent.reset();
  117. sentUnique.reset();
  118. sentRetrans.reset();
  119. lost.reset();
  120. dropped.reset();
  121. recvdAck.reset();
  122. recvdNak.reset();
  123. sentFilterExtra.reset();
  124. }
  125. void resetTrace()
  126. {
  127. sent.resetTrace();
  128. sentUnique.resetTrace();
  129. sentRetrans.resetTrace();
  130. lost.resetTrace();
  131. dropped.resetTrace();
  132. recvdAck.resetTrace();
  133. recvdNak.resetTrace();
  134. sentFilterExtra.resetTrace();
  135. }
  136. };
  137. /// Receiver-side statistics.
  138. struct Receiver
  139. {
  140. Metric<BytesPackets> recvd;
  141. Metric<BytesPackets> recvdUnique;
  142. Metric<BytesPackets> recvdRetrans; // The number of retransmitted data packets received by the receiver.
  143. Metric<BytesPackets> lost; // The number of packets detected by the receiver as lost.
  144. Metric<BytesPackets> dropped; // The number of packets dropped by the receiver (as too-late to be delivered).
  145. Metric<BytesPackets> recvdBelated; // The number of belated packets received (dropped as too late but eventually received).
  146. Metric<BytesPackets> undecrypted; // The number of packets received by the receiver that failed to be decrypted.
  147. Metric<Packets> recvdFilterExtra; // The number of filter packets (e.g. FEC) received by the receiver.
  148. Metric<Packets> suppliedByFilter; // The number of lost packets got from the packet filter at the receiver side (e.g. loss recovered by FEC).
  149. Metric<Packets> lossFilter; // The number of lost DATA packets not recovered by the packet filter at the receiver side.
  150. Metric<Packets> sentAck; // The number of ACK packets sent by the receiver.
  151. Metric<Packets> sentNak; // The number of NACK packets sent by the receiver.
  152. void reset()
  153. {
  154. recvd.reset();
  155. recvdUnique.reset();
  156. recvdRetrans.reset();
  157. lost.reset();
  158. dropped.reset();
  159. recvdBelated.reset();
  160. undecrypted.reset();
  161. recvdFilterExtra.reset();
  162. suppliedByFilter.reset();
  163. lossFilter.reset();
  164. sentAck.reset();
  165. sentNak.reset();
  166. }
  167. void resetTrace()
  168. {
  169. recvd.resetTrace();
  170. recvdUnique.resetTrace();
  171. recvdRetrans.resetTrace();
  172. lost.resetTrace();
  173. dropped.resetTrace();
  174. recvdBelated.resetTrace();
  175. undecrypted.resetTrace();
  176. recvdFilterExtra.resetTrace();
  177. suppliedByFilter.resetTrace();
  178. lossFilter.resetTrace();
  179. sentAck.resetTrace();
  180. sentNak.resetTrace();
  181. }
  182. };
  183. } // namespace stats
  184. } // namespace srt
  185. #endif // INC_SRT_STATS_H