1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/quic/congestion_control/rtt_stats.h"
7 #include <complex> // std::abs
15 // Default initial rtt used before any samples are received.
16 const int kInitialRttMs
= 100;
17 const float kAlpha
= 0.125f
;
18 const float kOneMinusAlpha
= (1 - kAlpha
);
19 const float kBeta
= 0.25f
;
20 const float kOneMinusBeta
= (1 - kBeta
);
21 const float kHalfWindow
= 0.5f
;
22 const float kQuarterWindow
= 0.25f
;
27 : latest_rtt_(QuicTime::Delta::Zero()),
28 min_rtt_(QuicTime::Delta::Zero()),
29 smoothed_rtt_(QuicTime::Delta::Zero()),
30 mean_deviation_(QuicTime::Delta::Zero()),
31 initial_rtt_us_(kInitialRttMs
* kNumMicrosPerMilli
),
32 num_min_rtt_samples_remaining_(0),
33 recent_min_rtt_window_(QuicTime::Delta::Infinite()) {}
35 void RttStats::SampleNewRecentMinRtt(uint32 num_samples
) {
36 num_min_rtt_samples_remaining_
= num_samples
;
37 new_min_rtt_
= RttSample();
40 void RttStats::ExpireSmoothedMetrics() {
43 QuicTime::Delta::FromMicroseconds(
44 std::abs(smoothed_rtt_
.Subtract(latest_rtt_
).ToMicroseconds())));
45 smoothed_rtt_
= max(smoothed_rtt_
, latest_rtt_
);
48 // Updates the RTT based on a new sample.
49 void RttStats::UpdateRtt(QuicTime::Delta send_delta
,
50 QuicTime::Delta ack_delay
,
52 if (send_delta
.IsInfinite() || send_delta
<= QuicTime::Delta::Zero()) {
53 LOG(WARNING
) << "Ignoring measured send_delta, because it's is "
54 << "either infinite, zero, or negative. send_delta = "
55 << send_delta
.ToMicroseconds();
59 // Update min_rtt_ first. min_rtt_ does not use an rtt_sample corrected for
60 // ack_delay but the raw observed send_delta, since poor clock granularity at
61 // the client may cause a high ack_delay to result in underestimation of the
63 if (min_rtt_
.IsZero() || min_rtt_
> send_delta
) {
64 min_rtt_
= send_delta
;
66 UpdateRecentMinRtt(send_delta
, now
);
68 // Correct for ack_delay if information received from the peer results in a
69 // positive RTT sample. Otherwise, we use the send_delta as a reasonable
70 // measure for smoothed_rtt.
71 QuicTime::Delta
rtt_sample(send_delta
);
72 if (rtt_sample
> ack_delay
) {
73 rtt_sample
= rtt_sample
.Subtract(ack_delay
);
75 latest_rtt_
= rtt_sample
;
77 if (smoothed_rtt_
.IsZero()) {
78 smoothed_rtt_
= rtt_sample
;
79 mean_deviation_
= QuicTime::Delta::FromMicroseconds(
80 rtt_sample
.ToMicroseconds() / 2);
82 mean_deviation_
= QuicTime::Delta::FromMicroseconds(static_cast<int64
>(
83 kOneMinusBeta
* mean_deviation_
.ToMicroseconds() +
84 kBeta
* std::abs(smoothed_rtt_
.Subtract(rtt_sample
).ToMicroseconds())));
85 smoothed_rtt_
= smoothed_rtt_
.Multiply(kOneMinusAlpha
).Add(
86 rtt_sample
.Multiply(kAlpha
));
87 DVLOG(1) << " smoothed_rtt(us):" << smoothed_rtt_
.ToMicroseconds()
88 << " mean_deviation(us):" << mean_deviation_
.ToMicroseconds();
92 void RttStats::UpdateRecentMinRtt(QuicTime::Delta rtt_sample
, QuicTime now
) {
93 // Recent min_rtt update.
94 if (num_min_rtt_samples_remaining_
> 0) {
95 --num_min_rtt_samples_remaining_
;
96 if (new_min_rtt_
.rtt
.IsZero() || rtt_sample
<= new_min_rtt_
.rtt
) {
97 new_min_rtt_
= RttSample(rtt_sample
, now
);
99 if (num_min_rtt_samples_remaining_
== 0) {
100 quarter_window_rtt_
= half_window_rtt_
= recent_min_rtt_
= new_min_rtt_
;
104 // Update the three recent rtt samples.
105 if (recent_min_rtt_
.rtt
.IsZero() || rtt_sample
<= recent_min_rtt_
.rtt
) {
106 recent_min_rtt_
= RttSample(rtt_sample
, now
);
107 quarter_window_rtt_
= half_window_rtt_
= recent_min_rtt_
;
108 } else if (rtt_sample
<= half_window_rtt_
.rtt
) {
109 half_window_rtt_
= RttSample(rtt_sample
, now
);
110 quarter_window_rtt_
= half_window_rtt_
;
111 } else if (rtt_sample
<= quarter_window_rtt_
.rtt
) {
112 quarter_window_rtt_
= RttSample(rtt_sample
, now
);
115 // Expire old min rtt samples.
116 if (recent_min_rtt_
.time
< now
.Subtract(recent_min_rtt_window_
)) {
117 recent_min_rtt_
= half_window_rtt_
;
118 half_window_rtt_
= quarter_window_rtt_
;
119 quarter_window_rtt_
= RttSample(rtt_sample
, now
);
120 } else if (half_window_rtt_
.time
<
121 now
.Subtract(recent_min_rtt_window_
.Multiply(kHalfWindow
))) {
122 half_window_rtt_
= quarter_window_rtt_
;
123 quarter_window_rtt_
= RttSample(rtt_sample
, now
);
124 } else if (quarter_window_rtt_
.time
<
125 now
.Subtract(recent_min_rtt_window_
.Multiply(kQuarterWindow
))) {
126 quarter_window_rtt_
= RttSample(rtt_sample
, now
);