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