Skip to content

Commit 968ce9a

Browse files
HybridDognerzhul
authored andcommittedJun 23, 2018
RTT fixes (#7428)
* Few code updates * Do not show average RTT before timing out * Fix unwanted integer division in RTTStatistics * Fix float format, prettier jitter calculation * Use +=, 0.1f -> 100.0f for stronger average updates
1 parent 07b1743 commit 968ce9a

File tree

5 files changed

+34
-39
lines changed

5 files changed

+34
-39
lines changed
 

‎src/client.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ void Client::step(float dtime)
437437
counter = 0.0;
438438
// connectedAndInitialized() is true, peer exists.
439439
float avg_rtt = getRTT();
440-
infostream << "Client: avg_rtt=" << avg_rtt << std::endl;
440+
infostream << "Client: average rtt: " << avg_rtt << std::endl;
441441
}
442442

443443
/*
@@ -1706,9 +1706,17 @@ void Client::afterContentReceived()
17061706
delete[] text;
17071707
}
17081708

1709+
// returns the Round Trip Time
1710+
// if the RTT did not become updated within 2 seconds, e.g. before timing out,
1711+
// it returns the expired time instead
17091712
float Client::getRTT()
17101713
{
1711-
return m_con->getPeerStat(PEER_ID_SERVER,con::AVG_RTT);
1714+
float avg_rtt = m_con->getPeerStat(PEER_ID_SERVER, con::AVG_RTT);
1715+
float time_from_last_rtt =
1716+
m_con->getPeerStat(PEER_ID_SERVER, con::TIMEOUT_COUNTER);
1717+
if (avg_rtt + 2.0f > time_from_last_rtt)
1718+
return avg_rtt;
1719+
return time_from_last_rtt;
17121720
}
17131721

17141722
float Client::getCurRate()

‎src/network/connection.cpp

+12-23
Original file line numberDiff line numberDiff line change
@@ -825,8 +825,9 @@ void Peer::DecUseCount()
825825
delete this;
826826
}
827827

828-
void Peer::RTTStatistics(float rtt, const std::string &profiler_id,
829-
unsigned int num_samples) {
828+
void Peer::RTTStatistics(float rtt, const std::string &profiler_id)
829+
{
830+
static const float avg_factor = 100.0f / MAX_RELIABLE_WINDOW_SIZE;
830831

831832
if (m_last_rtt > 0) {
832833
/* set min max values */
@@ -837,32 +838,24 @@ void Peer::RTTStatistics(float rtt, const std::string &profiler_id,
837838

838839
/* do average calculation */
839840
if (m_rtt.avg_rtt < 0.0)
840-
m_rtt.avg_rtt = rtt;
841+
m_rtt.avg_rtt = rtt;
841842
else
842-
m_rtt.avg_rtt = m_rtt.avg_rtt * (num_samples/(num_samples-1)) +
843-
rtt * (1/num_samples);
843+
m_rtt.avg_rtt += (rtt - m_rtt.avg_rtt) * avg_factor;
844844

845845
/* do jitter calculation */
846846

847847
//just use some neutral value at beginning
848-
float jitter = m_rtt.jitter_min;
849-
850-
if (rtt > m_last_rtt)
851-
jitter = rtt-m_last_rtt;
852-
853-
if (rtt <= m_last_rtt)
854-
jitter = m_last_rtt - rtt;
848+
float jitter = std::fabs(rtt - m_last_rtt);
855849

856850
if (jitter < m_rtt.jitter_min)
857851
m_rtt.jitter_min = jitter;
858852
if (jitter >= m_rtt.jitter_max)
859853
m_rtt.jitter_max = jitter;
860854

861855
if (m_rtt.jitter_avg < 0.0)
862-
m_rtt.jitter_avg = jitter;
856+
m_rtt.jitter_avg = jitter;
863857
else
864-
m_rtt.jitter_avg = m_rtt.jitter_avg * (num_samples/(num_samples-1)) +
865-
jitter * (1/num_samples);
858+
m_rtt.jitter_avg += (jitter - m_rtt.jitter_avg) * avg_factor;
866859

867860
if (!profiler_id.empty()) {
868861
g_profiler->graphAdd(profiler_id + "_rtt", rtt);
@@ -934,16 +927,12 @@ void UDPPeer::setNonLegacyPeer()
934927

935928
void UDPPeer::reportRTT(float rtt)
936929
{
937-
if (rtt < 0.0) {
938-
return;
939-
}
940-
RTTStatistics(rtt,"rudp",MAX_RELIABLE_WINDOW_SIZE*10);
930+
assert(rtt >= 0.0f);
931+
932+
RTTStatistics(rtt, "rudp");
941933

942934
float timeout = getStat(AVG_RTT) * RESEND_TIMEOUT_FACTOR;
943-
if (timeout < RESEND_TIMEOUT_MIN)
944-
timeout = RESEND_TIMEOUT_MIN;
945-
if (timeout > RESEND_TIMEOUT_MAX)
946-
timeout = RESEND_TIMEOUT_MAX;
935+
timeout = rangelim(timeout, RESEND_TIMEOUT_MIN, RESEND_TIMEOUT_MAX);
947936

948937
MutexAutoLock usage_lock(m_exclusive_access_mutex);
949938
resend_timeout = timeout;

‎src/network/connection.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -593,15 +593,15 @@ class Peer {
593593
return m_rtt.jitter_max;
594594
case AVG_JITTER:
595595
return m_rtt.jitter_avg;
596+
case TIMEOUT_COUNTER:
597+
return m_timeout_counter;
596598
}
597599
return -1;
598600
}
599601
protected:
600602
virtual void reportRTT(float rtt) {};
601603

602-
void RTTStatistics(float rtt,
603-
const std::string &profiler_id = "",
604-
unsigned int num_samples = 1000);
604+
void RTTStatistics(float rtt, const std::string &profiler_id = "");
605605

606606
bool IncUseCount();
607607
void DecUseCount();

‎src/network/connectionthreads.cpp

+7-10
Original file line numberDiff line numberDiff line change
@@ -1167,19 +1167,16 @@ SharedBuffer<u8> ConnectionReceiveThread::handlePacketType_Control(Channel *chan
11671167

11681168
// a overflow is quite unlikely but as it'd result in major
11691169
// rtt miscalculation we handle it here
1170+
float rtt;
11701171
if (current_time > p.absolute_send_time) {
1171-
float rtt = (current_time - p.absolute_send_time) / 1000.0;
1172-
1173-
// Let peer calculate stuff according to it
1174-
// (avg_rtt and resend_timeout)
1175-
dynamic_cast<UDPPeer *>(peer)->reportRTT(rtt);
1172+
rtt = (current_time - p.absolute_send_time) / 1000.0f;
11761173
} else if (p.totaltime > 0) {
1177-
float rtt = p.totaltime;
1178-
1179-
// Let peer calculate stuff according to it
1180-
// (avg_rtt and resend_timeout)
1181-
dynamic_cast<UDPPeer *>(peer)->reportRTT(rtt);
1174+
rtt = p.totaltime;
11821175
}
1176+
1177+
// Let peer calculate stuff according to it
1178+
// (avg_rtt and resend_timeout)
1179+
dynamic_cast<UDPPeer *>(peer)->reportRTT(rtt);
11831180
}
11841181
// put bytes for max bandwidth calculation
11851182
channel->UpdateBytesSent(p.data.getSize(), 1);

‎src/network/peerhandler.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ typedef enum {
3030
AVG_RTT,
3131
MIN_JITTER,
3232
MAX_JITTER,
33-
AVG_JITTER
33+
AVG_JITTER,
34+
TIMEOUT_COUNTER
3435
} rtt_stat_type;
3536

3637
class Peer;

0 commit comments

Comments
 (0)
Please sign in to comment.