diff --git a/assignment-client/src/assets/AssetServer.cpp b/assignment-client/src/assets/AssetServer.cpp index fad8ece7bf..0bf59eb8ad 100644 --- a/assignment-client/src/assets/AssetServer.cpp +++ b/assignment-client/src/assets/AssetServer.cpp @@ -82,11 +82,11 @@ void AssetServer::completeSetup() { auto maxBandwidthFloat = maxBandwidthValue.toDouble(-1); if (maxBandwidthFloat > 0.0) { - const int BYTES_PER_MEGABITS = (1024 * 1024) / 8; - int maxBandwidth = maxBandwidthFloat * BYTES_PER_MEGABITS; + const int BITS_PER_MEGABITS = 1000 * 1000; + int maxBandwidth = maxBandwidthFloat * BITS_PER_MEGABITS; nodeList->setConnectionMaxBandwidth(maxBandwidth); qInfo() << "Set maximum bandwith per connection to" << maxBandwidthFloat << "Mb/s." - " (" << maxBandwidth << "bytes/sec)"; + " (" << maxBandwidth << "bits/s)"; } // get the path to the asset folder from the domain server settings diff --git a/libraries/networking/src/udt/CongestionControl.cpp b/libraries/networking/src/udt/CongestionControl.cpp index d30be2c139..5826bfa11c 100644 --- a/libraries/networking/src/udt/CongestionControl.cpp +++ b/libraries/networking/src/udt/CongestionControl.cpp @@ -19,6 +19,7 @@ using namespace udt; using namespace std::chrono; static const double USECS_PER_SECOND = 1000000.0; +static const int BITS_PER_BYTE = 8; void CongestionControl::setMaxBandwidth(int maxBandwidth) { _maxBandwidth = maxBandwidth; @@ -28,11 +29,11 @@ void CongestionControl::setMaxBandwidth(int maxBandwidth) { void CongestionControl::setPacketSendPeriod(double newSendPeriod) { Q_ASSERT_X(newSendPeriod >= 0, "CongestionControl::setPacketPeriod", "Can not set a negative packet send period"); - auto maxBandwidth = _maxBandwidth.load(); - if (maxBandwidth > 0) { + auto packetsPerSecond = (double)_maxBandwidth / (BITS_PER_BYTE * _mss); + if (packetsPerSecond > 0.0) { // anytime the packet send period is about to be increased, make sure it stays below the minimum period, // calculated based on the maximum desired bandwidth - double minPacketSendPeriod = USECS_PER_SECOND / (((double) maxBandwidth) / _mss); + double minPacketSendPeriod = USECS_PER_SECOND / packetsPerSecond; _packetSendPeriod = std::max(newSendPeriod, minPacketSendPeriod); } else { _packetSendPeriod = newSendPeriod; @@ -145,17 +146,23 @@ void DefaultCC::onLoss(SequenceNumber rangeStart, SequenceNumber rangeEnd) { } _loss = true; - + ++_nakCount; + static const double INTER_PACKET_ARRIVAL_INCREASE = 1.125; static const int MAX_DECREASES_PER_CONGESTION_EPOCH = 5; // check if this NAK starts a new congestion period - this will be the case if the // NAK received occured for a packet sent after the last decrease if (rangeStart > _lastDecreaseMaxSeq) { + _delayedDecrease = (rangeStart == rangeEnd); _lastDecreasePeriod = _packetSendPeriod; - - setPacketSendPeriod(ceil(_packetSendPeriod * INTER_PACKET_ARRIVAL_INCREASE)); + + if (!_delayedDecrease) { + setPacketSendPeriod(ceil(_packetSendPeriod * INTER_PACKET_ARRIVAL_INCREASE)); + } else { + _loss = false; + } // use EWMA to update the average number of NAKs per congestion static const double NAK_EWMA_ALPHA = 0.125; @@ -177,10 +184,12 @@ void DefaultCC::onLoss(SequenceNumber rangeStart, SequenceNumber rangeEnd) { _randomDecreaseThreshold = distribution(generator); } - } else if ((_decreaseCount++ < MAX_DECREASES_PER_CONGESTION_EPOCH) && ((++_nakCount % _randomDecreaseThreshold) == 0)) { + } else if (_delayedDecrease && _nakCount == 2) { + setPacketSendPeriod(ceil(_packetSendPeriod * INTER_PACKET_ARRIVAL_INCREASE)); + } else if ((_decreaseCount++ < MAX_DECREASES_PER_CONGESTION_EPOCH) && ((_nakCount % _randomDecreaseThreshold) == 0)) { // there have been fewer than MAX_DECREASES_PER_CONGESTION_EPOCH AND this NAK matches the random count at which we // decided we would decrease the packet send period - + setPacketSendPeriod(ceil(_packetSendPeriod * INTER_PACKET_ARRIVAL_INCREASE)); _lastDecreaseMaxSeq = _sendCurrSeqNum; } diff --git a/libraries/networking/src/udt/CongestionControl.h b/libraries/networking/src/udt/CongestionControl.h index 8297b5f6bd..cb99e6c8d4 100644 --- a/libraries/networking/src/udt/CongestionControl.h +++ b/libraries/networking/src/udt/CongestionControl.h @@ -15,7 +15,6 @@ #include #include #include -#include #include @@ -61,7 +60,7 @@ protected: double _congestionWindowSize { 16.0 }; // Congestion window size, in packets int _bandwidth { 0 }; // estimated bandwidth, packets per second - std::atomic _maxBandwidth { -1 }; // Maximum desired bandwidth, bytes per second + std::atomic _maxBandwidth { -1 }; // Maximum desired bandwidth, bits per second double _maxCongestionWindowSize { 0.0 }; // maximum cwnd size, in packets int _mss { 0 }; // Maximum Packet Size, including all packet headers @@ -124,6 +123,7 @@ private: int _randomDecreaseThreshold { 1 }; // random threshold on decrease by number of loss events int _avgNAKNum { 0 }; // average number of NAKs per congestion int _decreaseCount { 0 }; // number of decreases in a congestion epoch + bool _delayedDecrease { false }; }; } diff --git a/libraries/networking/src/udt/Socket.cpp b/libraries/networking/src/udt/Socket.cpp index e9af1577fb..0695a65e1f 100644 --- a/libraries/networking/src/udt/Socket.cpp +++ b/libraries/networking/src/udt/Socket.cpp @@ -354,7 +354,7 @@ void Socket::setCongestionControlFactory(std::unique_ptr message) { void UDTTest::sampleStats() { static bool first = true; static const double USECS_PER_MSEC = 1000.0; - + static const double MEGABITS_PER_BYTE = 8.0 / 1000000.0; + static const double MS_PER_SECOND = 1000.0; + static const double PPS_TO_MBPS = 1500.0 * MEGABITS_PER_BYTE; + + if (!_target.isNull()) { if (first) { // output the headers for stats for our table @@ -378,8 +382,8 @@ void UDTTest::sampleStats() { // setup a list of left justified values QStringList values { - QString::number(stats.sendRate).rightJustified(CLIENT_STATS_TABLE_HEADERS[++headerIndex].size()), - QString::number(stats.estimatedBandwith).rightJustified(CLIENT_STATS_TABLE_HEADERS[++headerIndex].size()), + QString::number(stats.sendRate * PPS_TO_MBPS).rightJustified(CLIENT_STATS_TABLE_HEADERS[++headerIndex].size()), + QString::number(stats.estimatedBandwith * PPS_TO_MBPS).rightJustified(CLIENT_STATS_TABLE_HEADERS[++headerIndex].size()), QString::number(stats.rtt / USECS_PER_MSEC, 'f', 2).rightJustified(CLIENT_STATS_TABLE_HEADERS[++headerIndex].size()), QString::number(stats.congestionWindowSize).rightJustified(CLIENT_STATS_TABLE_HEADERS[++headerIndex].size()), QString::number(stats.packetSendPeriod).rightJustified(CLIENT_STATS_TABLE_HEADERS[++headerIndex].size()), @@ -408,16 +412,13 @@ void UDTTest::sampleStats() { int headerIndex = -1; - static const double MEGABITS_PER_BYTE = 8.0 / 1000000.0; - static const double MS_PER_SECOND = 1000.0; - double megabitsPerSecond = (stats.receivedBytes * MEGABITS_PER_BYTE * MS_PER_SECOND) / _statsInterval; // setup a list of left justified values QStringList values { QString::number(megabitsPerSecond, 'f', 2).rightJustified(SERVER_STATS_TABLE_HEADERS[++headerIndex].size()), - QString::number(stats.receiveRate).rightJustified(SERVER_STATS_TABLE_HEADERS[++headerIndex].size()), - QString::number(stats.estimatedBandwith).rightJustified(SERVER_STATS_TABLE_HEADERS[++headerIndex].size()), + QString::number(stats.receiveRate * PPS_TO_MBPS).rightJustified(SERVER_STATS_TABLE_HEADERS[++headerIndex].size()), + QString::number(stats.estimatedBandwith * PPS_TO_MBPS).rightJustified(SERVER_STATS_TABLE_HEADERS[++headerIndex].size()), QString::number(stats.rtt / USECS_PER_MSEC, 'f', 2).rightJustified(SERVER_STATS_TABLE_HEADERS[++headerIndex].size()), QString::number(stats.congestionWindowSize).rightJustified(SERVER_STATS_TABLE_HEADERS[++headerIndex].size()), QString::number(stats.events[udt::ConnectionStats::Stats::SentACK]).rightJustified(SERVER_STATS_TABLE_HEADERS[++headerIndex].size()),