| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "net/quic/congestion_control/rtt_stats.h" | |
| 6 | |
| 7 #include <cstdlib> // std::abs | |
| 8 | |
| 9 #include "net/quic/quic_flags.h" | |
| 10 | |
| 11 using std::max; | |
| 12 | |
| 13 namespace net { | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 // Default initial rtt used before any samples are received. | |
| 18 const int kInitialRttMs = 100; | |
| 19 const float kAlpha = 0.125f; | |
| 20 const float kOneMinusAlpha = (1 - kAlpha); | |
| 21 const float kBeta = 0.25f; | |
| 22 const float kOneMinusBeta = (1 - kBeta); | |
| 23 // 10-second window length for windowed min RTT. | |
| 24 const int kMinRttWindowLengthMs = 10000; | |
| 25 | |
| 26 } // namespace | |
| 27 | |
| 28 RttStats::RttStats() | |
| 29 : latest_rtt_(QuicTime::Delta::Zero()), | |
| 30 min_rtt_(QuicTime::Delta::Zero()), | |
| 31 smoothed_rtt_(QuicTime::Delta::Zero()), | |
| 32 previous_srtt_(QuicTime::Delta::Zero()), | |
| 33 mean_deviation_(QuicTime::Delta::Zero()), | |
| 34 initial_rtt_us_(kInitialRttMs * kNumMicrosPerMilli), | |
| 35 forced_windowed_min_rtt_(QuicTime::Delta::Zero()), | |
| 36 forced_windowed_min_rtt_time_(QuicTime::Zero()), | |
| 37 num_samples_for_forced_min_(0), | |
| 38 windowed_min_rtt_( | |
| 39 QuicTime::Delta::FromMilliseconds(kMinRttWindowLengthMs), | |
| 40 QuicTime::Delta::Zero(), | |
| 41 QuicTime::Zero()) {} | |
| 42 | |
| 43 void RttStats::SampleNewWindowedMinRtt(uint32_t num_samples) { | |
| 44 num_samples_for_forced_min_ = num_samples; | |
| 45 forced_windowed_min_rtt_ = QuicTime::Delta::Zero(); | |
| 46 forced_windowed_min_rtt_time_ = QuicTime::Zero(); | |
| 47 } | |
| 48 | |
| 49 void RttStats::ExpireSmoothedMetrics() { | |
| 50 mean_deviation_ = max(mean_deviation_, | |
| 51 QuicTime::Delta::FromMicroseconds(std::abs( | |
| 52 (smoothed_rtt_ - latest_rtt_).ToMicroseconds()))); | |
| 53 smoothed_rtt_ = max(smoothed_rtt_, latest_rtt_); | |
| 54 } | |
| 55 | |
| 56 // Updates the RTT based on a new sample. | |
| 57 void RttStats::UpdateRtt(QuicTime::Delta send_delta, | |
| 58 QuicTime::Delta ack_delay, | |
| 59 QuicTime now) { | |
| 60 if (send_delta.IsInfinite() || send_delta <= QuicTime::Delta::Zero()) { | |
| 61 LOG(WARNING) << "Ignoring measured send_delta, because it's is " | |
| 62 << "either infinite, zero, or negative. send_delta = " | |
| 63 << send_delta.ToMicroseconds(); | |
| 64 return; | |
| 65 } | |
| 66 | |
| 67 // Update min_rtt_ first. min_rtt_ does not use an rtt_sample corrected for | |
| 68 // ack_delay but the raw observed send_delta, since poor clock granularity at | |
| 69 // the client may cause a high ack_delay to result in underestimation of the | |
| 70 // min_rtt_. | |
| 71 if (min_rtt_.IsZero() || min_rtt_ > send_delta) { | |
| 72 min_rtt_ = send_delta; | |
| 73 } | |
| 74 UpdateWindowedMinRtt(send_delta, now); | |
| 75 | |
| 76 // Correct for ack_delay if information received from the peer results in a | |
| 77 // positive RTT sample. Otherwise, we use the send_delta as a reasonable | |
| 78 // measure for smoothed_rtt. | |
| 79 QuicTime::Delta rtt_sample(send_delta); | |
| 80 previous_srtt_ = smoothed_rtt_; | |
| 81 | |
| 82 if (rtt_sample > ack_delay) { | |
| 83 rtt_sample = rtt_sample - ack_delay; | |
| 84 } | |
| 85 latest_rtt_ = rtt_sample; | |
| 86 // First time call. | |
| 87 if (smoothed_rtt_.IsZero()) { | |
| 88 smoothed_rtt_ = rtt_sample; | |
| 89 mean_deviation_ = | |
| 90 QuicTime::Delta::FromMicroseconds(rtt_sample.ToMicroseconds() / 2); | |
| 91 } else { | |
| 92 mean_deviation_ = QuicTime::Delta::FromMicroseconds(static_cast<int64_t>( | |
| 93 kOneMinusBeta * mean_deviation_.ToMicroseconds() + | |
| 94 kBeta * std::abs((smoothed_rtt_ - rtt_sample).ToMicroseconds()))); | |
| 95 smoothed_rtt_ = kOneMinusAlpha * smoothed_rtt_ + kAlpha * rtt_sample; | |
| 96 DVLOG(1) << " smoothed_rtt(us):" << smoothed_rtt_.ToMicroseconds() | |
| 97 << " mean_deviation(us):" << mean_deviation_.ToMicroseconds(); | |
| 98 } | |
| 99 } | |
| 100 | |
| 101 void RttStats::UpdateWindowedMinRtt(QuicTime::Delta rtt_sample, QuicTime now) { | |
| 102 // Update windowed_min_rtt. | |
| 103 windowed_min_rtt_.Update(rtt_sample, now); | |
| 104 if (num_samples_for_forced_min_ <= 0) { | |
| 105 return; | |
| 106 } | |
| 107 // Reset windowed_min_rtt to the min of num_samples_for_forced_min_ samples. | |
| 108 if (forced_windowed_min_rtt_.IsZero() || | |
| 109 rtt_sample <= forced_windowed_min_rtt_) { | |
| 110 forced_windowed_min_rtt_ = rtt_sample; | |
| 111 forced_windowed_min_rtt_time_ = now; | |
| 112 } | |
| 113 if (num_samples_for_forced_min_ == 1) { | |
| 114 windowed_min_rtt_.Reset(forced_windowed_min_rtt_, | |
| 115 forced_windowed_min_rtt_time_); | |
| 116 } | |
| 117 --num_samples_for_forced_min_; | |
| 118 } | |
| 119 | |
| 120 void RttStats::OnConnectionMigration() { | |
| 121 latest_rtt_ = QuicTime::Delta::Zero(); | |
| 122 min_rtt_ = QuicTime::Delta::Zero(); | |
| 123 smoothed_rtt_ = QuicTime::Delta::Zero(); | |
| 124 mean_deviation_ = QuicTime::Delta::Zero(); | |
| 125 initial_rtt_us_ = kInitialRttMs * kNumMicrosPerMilli; | |
| 126 num_samples_for_forced_min_ = 0; | |
| 127 windowed_min_rtt_.Reset(QuicTime::Delta::Zero(), QuicTime::Zero()); | |
| 128 } | |
| 129 | |
| 130 } // namespace net | |
| OLD | NEW |