| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 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/congestion_control/cubic.h" | 5 #include "net/quic/congestion_control/cubic_bytes.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cmath> | 8 #include <cmath> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/time/time.h" | |
| 13 #include "net/quic/quic_flags.h" | |
| 14 #include "net/quic/quic_protocol.h" | 12 #include "net/quic/quic_protocol.h" |
| 15 | 13 |
| 16 using std::max; | 14 using std::max; |
| 17 | 15 |
| 18 namespace net { | 16 namespace net { |
| 19 | 17 |
| 20 namespace { | 18 namespace { |
| 21 | 19 |
| 22 // Constants based on TCP defaults. | 20 // Constants based on TCP defaults. |
| 23 // The following constants are in 2^10 fractions of a second instead of ms to | 21 // The following constants are in 2^10 fractions of a second instead of ms to |
| 24 // allow a 10 shift right to divide. | 22 // allow a 10 shift right to divide. |
| 25 const int kCubeScale = 40; // 1024*1024^3 (first 1024 is from 0.100^3) | 23 const int kCubeScale = 40; // 1024*1024^3 (first 1024 is from 0.100^3) |
| 26 // where 0.100 is 100 ms which is the scaling | 24 // where 0.100 is 100 ms which is the scaling |
| 27 // round trip time. | 25 // round trip time. |
| 28 const int kCubeCongestionWindowScale = 410; | 26 const int kCubeCongestionWindowScale = 410; |
| 27 // The cube factor for packets in bytes. |
| 29 const uint64 kCubeFactor = (GG_UINT64_C(1) << kCubeScale) / | 28 const uint64 kCubeFactor = (GG_UINT64_C(1) << kCubeScale) / |
| 30 kCubeCongestionWindowScale; | 29 kCubeCongestionWindowScale / kDefaultTCPMSS; |
| 31 | 30 |
| 32 const uint32 kDefaultNumConnections = 2; | 31 const uint32 kDefaultNumConnections = 2; |
| 33 const float kBeta = 0.7f; // Default Cubic backoff factor. | 32 const float kBeta = 0.7f; // Default Cubic backoff factor. |
| 34 // Additional backoff factor when loss occurs in the concave part of the Cubic | 33 // Additional backoff factor when loss occurs in the concave part of the Cubic |
| 35 // curve. This additional backoff factor is expected to give up bandwidth to | 34 // curve. This additional backoff factor is expected to give up bandwidth to |
| 36 // new concurrent flows and speed up convergence. | 35 // new concurrent flows and speed up convergence. |
| 37 const float kBetaLastMax = 0.85f; | 36 const float kBetaLastMax = 0.85f; |
| 38 | 37 |
| 39 } // namespace | 38 } // namespace |
| 40 | 39 |
| 41 Cubic::Cubic(const QuicClock* clock) | 40 CubicBytes::CubicBytes(const QuicClock* clock) |
| 42 : clock_(clock), | 41 : clock_(clock), |
| 43 num_connections_(kDefaultNumConnections), | 42 num_connections_(kDefaultNumConnections), |
| 44 epoch_(QuicTime::Zero()), | 43 epoch_(QuicTime::Zero()), |
| 45 last_update_time_(QuicTime::Zero()) { | 44 last_update_time_(QuicTime::Zero()) { |
| 46 Reset(); | 45 Reset(); |
| 47 } | 46 } |
| 48 | 47 |
| 49 void Cubic::SetNumConnections(int num_connections) { | 48 void CubicBytes::SetNumConnections(int num_connections) { |
| 50 num_connections_ = num_connections; | 49 num_connections_ = num_connections; |
| 51 } | 50 } |
| 52 | 51 |
| 53 float Cubic::Alpha() const { | 52 float CubicBytes::Alpha() const { |
| 54 // TCPFriendly alpha is described in Section 3.3 of the CUBIC paper. Note that | 53 // TCPFriendly alpha is described in Section 3.3 of the CUBIC paper. Note that |
| 55 // beta here is a cwnd multiplier, and is equal to 1-beta from the paper. | 54 // beta here is a cwnd multiplier, and is equal to 1-beta from the paper. |
| 56 // We derive the equivalent alpha for an N-connection emulation as: | 55 // We derive the equivalent alpha for an N-connection emulation as: |
| 57 const float beta = Beta(); | 56 const float beta = Beta(); |
| 58 return 3 * num_connections_ * num_connections_ * (1 - beta) / (1 + beta); | 57 return 3 * num_connections_ * num_connections_ * (1 - beta) / (1 + beta); |
| 59 } | 58 } |
| 60 | 59 |
| 61 float Cubic::Beta() const { | 60 float CubicBytes::Beta() const { |
| 62 // kNConnectionBeta is the backoff factor after loss for our N-connection | 61 // kNConnectionBeta is the backoff factor after loss for our N-connection |
| 63 // emulation, which emulates the effective backoff of an ensemble of N | 62 // emulation, which emulates the effective backoff of an ensemble of N |
| 64 // TCP-Reno connections on a single loss event. The effective multiplier is | 63 // TCP-Reno connections on a single loss event. The effective multiplier is |
| 65 // computed as: | 64 // computed as: |
| 66 return (num_connections_ - 1 + kBeta) / num_connections_; | 65 return (num_connections_ - 1 + kBeta) / num_connections_; |
| 67 } | 66 } |
| 68 | 67 |
| 69 void Cubic::Reset() { | 68 void CubicBytes::Reset() { |
| 70 epoch_ = QuicTime::Zero(); // Reset time. | 69 epoch_ = QuicTime::Zero(); // Reset time. |
| 71 last_update_time_ = QuicTime::Zero(); // Reset time. | 70 last_update_time_ = QuicTime::Zero(); // Reset time. |
| 72 last_congestion_window_ = 0; | 71 last_congestion_window_ = 0; |
| 73 last_max_congestion_window_ = 0; | 72 last_max_congestion_window_ = 0; |
| 74 acked_packets_count_ = 0; | 73 acked_bytes_count_ = 0; |
| 75 estimated_tcp_congestion_window_ = 0; | 74 estimated_tcp_congestion_window_ = 0; |
| 76 origin_point_congestion_window_ = 0; | 75 origin_point_congestion_window_ = 0; |
| 77 time_to_origin_point_ = 0; | 76 time_to_origin_point_ = 0; |
| 78 last_target_congestion_window_ = 0; | 77 last_target_congestion_window_ = 0; |
| 79 } | 78 } |
| 80 | 79 |
| 81 QuicPacketCount Cubic::CongestionWindowAfterPacketLoss( | 80 QuicByteCount CubicBytes::CongestionWindowAfterPacketLoss( |
| 82 QuicPacketCount current_congestion_window) { | 81 QuicByteCount current_congestion_window) { |
| 83 if (current_congestion_window < last_max_congestion_window_) { | 82 if (current_congestion_window < last_max_congestion_window_) { |
| 84 // We never reached the old max, so assume we are competing with another | 83 // We never reached the old max, so assume we are competing with another |
| 85 // flow. Use our extra back off factor to allow the other flow to go up. | 84 // flow. Use our extra back off factor to allow the other flow to go up. |
| 86 last_max_congestion_window_ = | 85 last_max_congestion_window_ = |
| 87 static_cast<int>(kBetaLastMax * current_congestion_window); | 86 static_cast<int>(kBetaLastMax * current_congestion_window); |
| 88 } else { | 87 } else { |
| 89 last_max_congestion_window_ = current_congestion_window; | 88 last_max_congestion_window_ = current_congestion_window; |
| 90 } | 89 } |
| 91 epoch_ = QuicTime::Zero(); // Reset time. | 90 epoch_ = QuicTime::Zero(); // Reset time. |
| 92 return static_cast<int>(current_congestion_window * Beta()); | 91 return static_cast<int>(current_congestion_window * Beta()); |
| 93 } | 92 } |
| 94 | 93 |
| 95 QuicPacketCount Cubic::CongestionWindowAfterAck( | 94 QuicByteCount CubicBytes::CongestionWindowAfterAck( |
| 96 QuicPacketCount current_congestion_window, | 95 QuicByteCount acked_bytes, |
| 96 QuicByteCount current_congestion_window, |
| 97 QuicTime::Delta delay_min) { | 97 QuicTime::Delta delay_min) { |
| 98 acked_packets_count_ += 1; // Packets acked. | 98 acked_bytes_count_ += acked_bytes; |
| 99 QuicTime current_time = clock_->ApproximateNow(); | 99 QuicTime current_time = clock_->ApproximateNow(); |
| 100 | 100 |
| 101 // Cubic is "independent" of RTT, the update is limited by the time elapsed. | 101 // Cubic is "independent" of RTT, the update is limited by the time elapsed. |
| 102 if (last_congestion_window_ == current_congestion_window && | 102 if (last_congestion_window_ == current_congestion_window && |
| 103 (current_time.Subtract(last_update_time_) <= MaxCubicTimeInterval())) { | 103 (current_time.Subtract(last_update_time_) <= MaxCubicTimeInterval())) { |
| 104 return max(last_target_congestion_window_, | 104 return max(last_target_congestion_window_, |
| 105 estimated_tcp_congestion_window_); | 105 estimated_tcp_congestion_window_); |
| 106 } | 106 } |
| 107 last_congestion_window_ = current_congestion_window; | 107 last_congestion_window_ = current_congestion_window; |
| 108 last_update_time_ = current_time; | 108 last_update_time_ = current_time; |
| 109 | 109 |
| 110 if (!epoch_.IsInitialized()) { | 110 if (!epoch_.IsInitialized()) { |
| 111 // First ACK after a loss event. | 111 // First ACK after a loss event. |
| 112 DVLOG(1) << "Start of epoch"; | 112 DVLOG(1) << "Start of epoch"; |
| 113 epoch_ = current_time; // Start of epoch. | 113 epoch_ = current_time; // Start of epoch. |
| 114 acked_packets_count_ = 1; // Reset count. | 114 acked_bytes_count_ = acked_bytes; // Reset count. |
| 115 // Reset estimated_tcp_congestion_window_ to be in sync with cubic. | 115 // Reset estimated_tcp_congestion_window_ to be in sync with cubic. |
| 116 estimated_tcp_congestion_window_ = current_congestion_window; | 116 estimated_tcp_congestion_window_ = current_congestion_window; |
| 117 if (last_max_congestion_window_ <= current_congestion_window) { | 117 if (last_max_congestion_window_ <= current_congestion_window) { |
| 118 time_to_origin_point_ = 0; | 118 time_to_origin_point_ = 0; |
| 119 origin_point_congestion_window_ = current_congestion_window; | 119 origin_point_congestion_window_ = current_congestion_window; |
| 120 } else { | 120 } else { |
| 121 time_to_origin_point_ = | 121 time_to_origin_point_ = |
| 122 static_cast<uint32>(cbrt(kCubeFactor * (last_max_congestion_window_ - | 122 static_cast<uint32>(cbrt(kCubeFactor * (last_max_congestion_window_ - |
| 123 current_congestion_window))); | 123 current_congestion_window))); |
| 124 origin_point_congestion_window_ = | 124 origin_point_congestion_window_ = last_max_congestion_window_; |
| 125 last_max_congestion_window_; | |
| 126 } | 125 } |
| 127 } | 126 } |
| 128 // Change the time unit from microseconds to 2^10 fractions per second. Take | 127 // Change the time unit from microseconds to 2^10 fractions per second. Take |
| 129 // the round trip time in account. This is done to allow us to use shift as a | 128 // the round trip time in account. This is done to allow us to use shift as a |
| 130 // divide operator. | 129 // divide operator. |
| 131 int64 elapsed_time = | 130 int64 elapsed_time = |
| 132 (current_time.Add(delay_min).Subtract(epoch_).ToMicroseconds() << 10) / | 131 (current_time.Add(delay_min).Subtract(epoch_).ToMicroseconds() << 10) / |
| 133 base::Time::kMicrosecondsPerSecond; | 132 kNumMicrosPerSecond; |
| 134 | 133 |
| 135 int64 offset = time_to_origin_point_ - elapsed_time; | 134 int64 offset = time_to_origin_point_ - elapsed_time; |
| 136 QuicPacketCount delta_congestion_window = (kCubeCongestionWindowScale | 135 QuicByteCount delta_congestion_window = |
| 137 * offset * offset * offset) >> kCubeScale; | 136 ((kCubeCongestionWindowScale * offset * offset * offset) >> kCubeScale) * |
| 137 kDefaultTCPMSS; |
| 138 | 138 |
| 139 QuicPacketCount target_congestion_window = | 139 QuicByteCount target_congestion_window = |
| 140 origin_point_congestion_window_ - delta_congestion_window; | 140 origin_point_congestion_window_ - delta_congestion_window; |
| 141 | 141 |
| 142 DCHECK_LT(0u, estimated_tcp_congestion_window_); | 142 DCHECK_LT(0u, estimated_tcp_congestion_window_); |
| 143 // With dynamic beta/alpha based on number of active streams, it is possible | 143 // Increase the window by Alpha * 1 MSS of bytes every time we ack an |
| 144 // for the required_ack_count to become much lower than acked_packets_count_ | 144 // estimated tcp window of bytes. |
| 145 // suddenly, leading to more than one iteration through the following loop. | 145 estimated_tcp_congestion_window_ += acked_bytes_count_ * |
| 146 while (true) { | 146 (Alpha() * kDefaultTCPMSS) / |
| 147 // Update estimated TCP congestion_window. | 147 estimated_tcp_congestion_window_; |
| 148 QuicPacketCount required_ack_count = static_cast<QuicPacketCount>( | 148 acked_bytes_count_ = 0; |
| 149 estimated_tcp_congestion_window_ / Alpha()); | |
| 150 if (acked_packets_count_ < required_ack_count) { | |
| 151 break; | |
| 152 } | |
| 153 acked_packets_count_ -= required_ack_count; | |
| 154 estimated_tcp_congestion_window_++; | |
| 155 } | |
| 156 | 149 |
| 157 // We have a new cubic congestion window. | 150 // We have a new cubic congestion window. |
| 158 last_target_congestion_window_ = target_congestion_window; | 151 last_target_congestion_window_ = target_congestion_window; |
| 159 | 152 |
| 160 // Compute target congestion_window based on cubic target and estimated TCP | 153 // Compute target congestion_window based on cubic target and estimated TCP |
| 161 // congestion_window, use highest (fastest). | 154 // congestion_window, use highest (fastest). |
| 162 if (target_congestion_window < estimated_tcp_congestion_window_) { | 155 if (target_congestion_window < estimated_tcp_congestion_window_) { |
| 163 target_congestion_window = estimated_tcp_congestion_window_; | 156 target_congestion_window = estimated_tcp_congestion_window_; |
| 164 } | 157 } |
| 165 | 158 |
| 166 DVLOG(1) << "Target congestion_window: " << target_congestion_window; | 159 DVLOG(1) << "Target congestion_window: " << target_congestion_window; |
| 167 return target_congestion_window; | 160 return target_congestion_window; |
| 168 } | 161 } |
| 169 | 162 |
| 170 } // namespace net | 163 } // namespace net |
| OLD | NEW |