Land Recent QUIC Changes.
[chromium-blink-merge.git] / net / quic / congestion_control / rtt_stats.cc
blob4161859bb57aa5f0e683c23544e6c5c8e14bcb45
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 namespace net {
9 namespace {
11 // Default initial rtt used before any samples are received.
12 const int kInitialRttMs = 100;
13 const float kAlpha = 0.125f;
14 const float kOneMinusAlpha = (1 - kAlpha);
15 const float kBeta = 0.25f;
16 const float kOneMinusBeta = (1 - kBeta);
20 RttStats::RttStats()
21 : latest_rtt_(QuicTime::Delta::Zero()),
22 min_rtt_(QuicTime::Delta::Zero()),
23 smoothed_rtt_(QuicTime::Delta::Zero()),
24 mean_deviation_(QuicTime::Delta::Zero()),
25 initial_rtt_us_(kInitialRttMs * base::Time::kMicrosecondsPerMillisecond) {
28 bool RttStats::HasUpdates() const {
29 return !smoothed_rtt_.IsZero();
32 // Updates the RTT based on a new sample.
33 void RttStats::UpdateRtt(QuicTime::Delta send_delta,
34 QuicTime::Delta ack_delay) {
35 QuicTime::Delta rtt_sample(QuicTime::Delta::Zero());
36 if (send_delta > ack_delay) {
37 rtt_sample = send_delta.Subtract(ack_delay);
38 } else if (!HasUpdates()) {
39 // Even though we received information from the peer suggesting
40 // an invalid (negative) RTT, we can use the send delta as an
41 // approximation until we get a better estimate.
42 rtt_sample = send_delta;
45 if (rtt_sample.IsInfinite() || rtt_sample.IsZero()) {
46 DVLOG(1) << "Ignoring rtt, because it's "
47 << (rtt_sample.IsZero() ? "Zero" : "Infinite");
48 return;
50 // RTT can't be negative.
51 DCHECK_LT(0, rtt_sample.ToMicroseconds());
53 latest_rtt_ = rtt_sample;
54 // First time call or link delay decreases.
55 if (min_rtt_.IsZero() || min_rtt_ > rtt_sample) {
56 min_rtt_ = rtt_sample;
58 // First time call.
59 if (!HasUpdates()) {
60 smoothed_rtt_ = rtt_sample;
61 mean_deviation_ = QuicTime::Delta::FromMicroseconds(
62 rtt_sample.ToMicroseconds() / 2);
63 } else {
64 mean_deviation_ = QuicTime::Delta::FromMicroseconds(
65 kOneMinusBeta * mean_deviation_.ToMicroseconds() +
66 kBeta * std::abs(smoothed_rtt_.Subtract(rtt_sample).ToMicroseconds()));
67 smoothed_rtt_ = smoothed_rtt_.Multiply(kOneMinusAlpha).Add(
68 rtt_sample.Multiply(kAlpha));
69 DVLOG(1) << "Cubic; smoothed_rtt(us):" << smoothed_rtt_.ToMicroseconds()
70 << " mean_deviation(us):" << mean_deviation_.ToMicroseconds();
74 QuicTime::Delta RttStats::SmoothedRtt() const {
75 if (!HasUpdates()) {
76 return QuicTime::Delta::FromMicroseconds(initial_rtt_us_);
78 return smoothed_rtt_;
81 } // namespace net