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