Lots of random cleanups, mostly for native_theme_win.cc:
[chromium-blink-merge.git] / net / quic / congestion_control / rtt_stats.cc
blob3225d9f3afc2b0817d23623773ae1aa8e2b93c11
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 using std::max;
9 namespace net {
11 namespace {
13 // Default initial rtt used before any samples are received.
14 const int kInitialRttMs = 100;
15 const float kAlpha = 0.125f;
16 const float kOneMinusAlpha = (1 - kAlpha);
17 const float kBeta = 0.25f;
18 const float kOneMinusBeta = (1 - kBeta);
19 const float kHalfWindow = 0.5f;
20 const float kQuarterWindow = 0.25f;
22 } // namespace
24 RttStats::RttStats()
25 : latest_rtt_(QuicTime::Delta::Zero()),
26 min_rtt_(QuicTime::Delta::Zero()),
27 smoothed_rtt_(QuicTime::Delta::Zero()),
28 mean_deviation_(QuicTime::Delta::Zero()),
29 initial_rtt_us_(kInitialRttMs * base::Time::kMicrosecondsPerMillisecond),
30 num_min_rtt_samples_remaining_(0),
31 recent_min_rtt_window_(QuicTime::Delta::Infinite()) {}
33 bool RttStats::HasUpdates() const {
34 return !smoothed_rtt_.IsZero();
37 void RttStats::SampleNewRecentMinRtt(uint32 num_samples) {
38 num_min_rtt_samples_remaining_ = num_samples;
39 new_min_rtt_ = RttSample();
42 void RttStats::ExpireSmoothedMetrics() {
43 mean_deviation_ = max(mean_deviation_, latest_rtt_.Subtract(smoothed_rtt_));
44 smoothed_rtt_ = max(smoothed_rtt_, latest_rtt_);
47 // Updates the RTT based on a new sample.
48 void RttStats::UpdateRtt(QuicTime::Delta send_delta,
49 QuicTime::Delta ack_delay,
50 QuicTime now) {
51 QuicTime::Delta rtt_sample(QuicTime::Delta::Zero());
52 if (send_delta > ack_delay) {
53 rtt_sample = send_delta.Subtract(ack_delay);
54 } else if (!HasUpdates()) {
55 // Even though we received information from the peer suggesting
56 // an invalid (negative) RTT, we can use the send delta as an
57 // approximation until we get a better estimate.
58 rtt_sample = send_delta;
61 if (rtt_sample.IsInfinite() || rtt_sample.IsZero()) {
62 DVLOG(1) << "Ignoring rtt, because it's "
63 << (rtt_sample.IsZero() ? "Zero" : "Infinite");
64 return;
66 // RTT can't be negative.
67 DCHECK_LT(0, rtt_sample.ToMicroseconds());
69 latest_rtt_ = rtt_sample;
70 // First time call or link delay decreases.
71 if (min_rtt_.IsZero() || min_rtt_ > rtt_sample) {
72 min_rtt_ = rtt_sample;
74 UpdateRecentMinRtt(rtt_sample, now);
75 // First time call.
76 if (!HasUpdates()) {
77 smoothed_rtt_ = rtt_sample;
78 mean_deviation_ = QuicTime::Delta::FromMicroseconds(
79 rtt_sample.ToMicroseconds() / 2);
80 } else {
81 mean_deviation_ = QuicTime::Delta::FromMicroseconds(
82 kOneMinusBeta * mean_deviation_.ToMicroseconds() +
83 kBeta * std::abs(smoothed_rtt_.Subtract(rtt_sample).ToMicroseconds()));
84 smoothed_rtt_ = smoothed_rtt_.Multiply(kOneMinusAlpha).Add(
85 rtt_sample.Multiply(kAlpha));
86 DVLOG(1) << "Cubic; smoothed_rtt(us):" << smoothed_rtt_.ToMicroseconds()
87 << " mean_deviation(us):" << mean_deviation_.ToMicroseconds();
91 void RttStats::UpdateRecentMinRtt(QuicTime::Delta rtt_sample, QuicTime now) {
92 // Recent min_rtt update.
93 if (num_min_rtt_samples_remaining_ > 0) {
94 --num_min_rtt_samples_remaining_;
95 if (new_min_rtt_.rtt.IsZero() || rtt_sample <= new_min_rtt_.rtt) {
96 new_min_rtt_ = RttSample(rtt_sample, now);
98 if (num_min_rtt_samples_remaining_ == 0) {
99 quarter_window_rtt_ = half_window_rtt_ = recent_min_rtt_ = new_min_rtt_;
103 // Update the three recent rtt samples.
104 if (recent_min_rtt_.rtt.IsZero() || rtt_sample <= recent_min_rtt_.rtt) {
105 recent_min_rtt_ = RttSample(rtt_sample, now);
106 quarter_window_rtt_ = half_window_rtt_ = recent_min_rtt_;
107 } else if (rtt_sample <= half_window_rtt_.rtt) {
108 half_window_rtt_ = RttSample(rtt_sample, now);
109 quarter_window_rtt_ = half_window_rtt_;
110 } else if (rtt_sample <= quarter_window_rtt_.rtt) {
111 quarter_window_rtt_ = RttSample(rtt_sample, now);
114 // Expire old min rtt samples.
115 if (recent_min_rtt_.time < now.Subtract(recent_min_rtt_window_)) {
116 recent_min_rtt_ = half_window_rtt_;
117 half_window_rtt_ = quarter_window_rtt_;
118 quarter_window_rtt_ = RttSample(rtt_sample, now);
119 } else if (half_window_rtt_.time <
120 now.Subtract(recent_min_rtt_window_.Multiply(kHalfWindow))) {
121 half_window_rtt_ = quarter_window_rtt_;
122 quarter_window_rtt_ = RttSample(rtt_sample, now);
123 } else if (quarter_window_rtt_.time <
124 now.Subtract(recent_min_rtt_window_.Multiply(kQuarterWindow))) {
125 quarter_window_rtt_ = RttSample(rtt_sample, now);
129 QuicTime::Delta RttStats::SmoothedRtt() const {
130 if (!HasUpdates()) {
131 return QuicTime::Delta::FromMicroseconds(initial_rtt_us_);
133 return smoothed_rtt_;
136 } // namespace net