From 89e32e4a6beddd0ffee571d8fd465e42f56646f9 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 29 Jul 2015 11:11:06 -0700 Subject: [PATCH] some initial cleanup in CongestionControl --- .../networking/src/udt/CongestionControl.cpp | 41 ++++++++++--------- .../networking/src/udt/CongestionControl.h | 15 +++---- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/libraries/networking/src/udt/CongestionControl.cpp b/libraries/networking/src/udt/CongestionControl.cpp index a7cd45cfe7..969909e956 100644 --- a/libraries/networking/src/udt/CongestionControl.cpp +++ b/libraries/networking/src/udt/CongestionControl.cpp @@ -11,16 +11,17 @@ #include "CongestionControl.h" -#include +#include using namespace udt; +using namespace std::chrono; void DefaultCC::init() { - _lastRCTime = usecTimestampNow(); + _lastRCTime = high_resolution_clock::now(); setAckTimer(synInterval()); _lastAck = _sendCurrSeqNum; - _lastDecSeq = SequenceNumber{ SequenceNumber::MAX }; + _lastDecreaseMaxSeq = SequenceNumber { SequenceNumber::MAX }; _congestionWindowSize = 16.0; _packetSendPeriod = 1.0; @@ -35,12 +36,12 @@ void DefaultCC::onACK(SequenceNumber ackNum) { // for long time. const double min_inc = 0.01; - uint64_t currtime = usecTimestampNow(); - if (currtime - _lastRCTime < (uint64_t)synInterval()) { + auto now = high_resolution_clock::now(); + if (duration_cast(now - _lastRCTime).count() < synInterval()) { return; } - _lastRCTime = currtime; + _lastRCTime = now; if (_slowStart) { _congestionWindowSize += seqlen(_lastAck, ackNum); @@ -48,14 +49,14 @@ void DefaultCC::onACK(SequenceNumber ackNum) { if (_congestionWindowSize > _maxCongestionWindowSize) { _slowStart = false; - if (_recvieveRate > 0) { - _packetSendPeriod = 1000000.0 / _recvieveRate; + if (_receiveRate > 0) { + _packetSendPeriod = USECS_PER_SECOND / _receiveRate; } else { _packetSendPeriod = (_rtt + synInterval()) / _congestionWindowSize; } } } else { - _congestionWindowSize = _recvieveRate / 1000000.0 * (_rtt + synInterval()) + 16; + _congestionWindowSize = _receiveRate / USECS_PER_SECOND * (_rtt + synInterval()) + 16; } // During Slow Start, no rate increase @@ -69,7 +70,7 @@ void DefaultCC::onACK(SequenceNumber ackNum) { } B = (int64_t)(_bandwidth - 1000000.0 / _packetSendPeriod); - if ((_packetSendPeriod > _lastDecPeriod) && ((_bandwidth / 9) < B)) { + if ((_packetSendPeriod > _lastDecreasePeriod) && ((_bandwidth / 9) < B)) { B = _bandwidth / 9; } if (B <= 0) { @@ -92,9 +93,9 @@ void DefaultCC::onLoss(SequenceNumber rangeStart, SequenceNumber rangeEnd) { //Slow Start stopped, if it hasn't yet if (_slowStart) { _slowStart = false; - if (_recvieveRate > 0) { + if (_receiveRate > 0) { // Set the sending rate to the receiving rate. - _packetSendPeriod = 1000000.0 / _recvieveRate; + _packetSendPeriod = USECS_PER_SECOND / _receiveRate; return; } // If no receiving rate is observed, we have to compute the sending @@ -105,33 +106,33 @@ void DefaultCC::onLoss(SequenceNumber rangeStart, SequenceNumber rangeEnd) { _loss = true; - if (rangeStart > _lastDecSeq) { - _lastDecPeriod = _packetSendPeriod; + if (rangeStart > _lastDecreaseMaxSeq) { + _lastDecreasePeriod = _packetSendPeriod; _packetSendPeriod = ceil(_packetSendPeriod * 1.125); _avgNAKNum = (int)ceil(_avgNAKNum * 0.875 + _nakCount * 0.125); _nakCount = 1; _decCount = 1; - _lastDecSeq = _sendCurrSeqNum; + _lastDecreaseMaxSeq = _sendCurrSeqNum; // remove global synchronization using randomization - srand((uint32_t)_lastDecSeq); + srand((uint32_t)_lastDecreaseMaxSeq); _decRandom = (int)ceil(_avgNAKNum * (double(rand()) / RAND_MAX)); if (_decRandom < 1) _decRandom = 1; - } else if ((_decCount ++ < 5) && (0 == (++ _nakCount % _decRandom))) { + } else if ((_decCount++ < 5) && (0 == (++_nakCount % _decRandom))) { // 0.875^5 = 0.51, rate should not be decreased by more than half within a congestion period _packetSendPeriod = ceil(_packetSendPeriod * 1.125); - _lastDecSeq = _sendCurrSeqNum; + _lastDecreaseMaxSeq = _sendCurrSeqNum; } } void DefaultCC::onTimeout() { if (_slowStart) { _slowStart = false; - if (_recvieveRate > 0) { - _packetSendPeriod = 1000000.0 / _recvieveRate; + if (_receiveRate > 0) { + _packetSendPeriod = USECS_PER_SECOND / _receiveRate; } else { _packetSendPeriod = _congestionWindowSize / (_rtt + synInterval()); } diff --git a/libraries/networking/src/udt/CongestionControl.h b/libraries/networking/src/udt/CongestionControl.h index 93e842a21a..83959f034f 100644 --- a/libraries/networking/src/udt/CongestionControl.h +++ b/libraries/networking/src/udt/CongestionControl.h @@ -12,6 +12,7 @@ #ifndef hifi_CongestionControl_h #define hifi_CongestionControl_h +#include #include #include "LossList.h" @@ -52,7 +53,7 @@ protected: int _mss = 0; // Maximum Packet Size, including all packet headers SequenceNumber _sendCurrSeqNum; // current maximum seq num sent out - int _recvieveRate = 0; // packet arrive rate at receiver side, packets per second + int _receiveRate = 0; // packet arrive rate at receiver side, packets per second int _rtt = 0; // current estimated RTT, microsecond private: @@ -62,9 +63,9 @@ private: void setMss(int mss) { _mss = mss; } void setMaxCongestionWindowSize(int window) { _maxCongestionWindowSize = window; } void setBandwidth(int bandwidth) { _bandwidth = bandwidth; } - void setSndCurrSeqNum(SequenceNumber seqNum) { _sendCurrSeqNum = seqNum; } - void setRcvRate(int rate) { _recvieveRate = rate; } - void setRtt(int rtt) { _rtt = rtt; } + void setSendCurrentSequenceNumber(SequenceNumber seqNum) { _sendCurrSeqNum = seqNum; } + void setReceiveRate(int rate) { _receiveRate = rate; } + void setRTT(int rtt) { _rtt = rtt; } int _ackPeriod = 0; // Periodical timer to send an ACK, in milliseconds int _ackInterval = 0; // How many packets to send one ACK, in packets @@ -103,12 +104,12 @@ public: virtual void onTimeout(); private: - uint64_t _lastRCTime = 0; // last rate increase time + std::chrono::high_resolution_clock::time_point _lastRCTime; // last rate increase time bool _slowStart = true; // if in slow start phase SequenceNumber _lastAck; // last ACKed seq num bool _loss = false; // if loss happened since last rate increase - SequenceNumber _lastDecSeq; // max pkt seq num sent out when last decrease happened - double _lastDecPeriod = 1; // value of pktsndperiod when last decrease happened + SequenceNumber _lastDecreaseMaxSeq; // max pkt seq num sent out when last decrease happened + double _lastDecreasePeriod = 1; // value of _packetSendPeriod when last decrease happened int _nakCount = 0; // NAK counter int _decRandom = 1; // random threshold on decrease by number of loss events int _avgNAKNum = 0; // average number of NAKs per congestion