Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / quic / congestion_control / cubic_bytes.cc
blob40259ceae614c0b79b92b46946b30fae1b30663a
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
3 // found in the LICENSE file.
5 #include "net/quic/congestion_control/cubic_bytes.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_protocol.h"
14 #include "net/quic/quic_time.h"
16 using std::max;
18 namespace net {
20 namespace {
22 // Constants based on TCP defaults.
23 // The following constants are in 2^10 fractions of a second instead of ms to
24 // allow a 10 shift right to divide.
25 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
27 // round trip time.
28 const int kCubeCongestionWindowScale = 410;
29 // The cube factor for packets in bytes.
30 const uint64 kCubeFactor = (UINT64_C(1) << kCubeScale) /
31 kCubeCongestionWindowScale / kDefaultTCPMSS;
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 CubicBytes::CubicBytes(const QuicClock* clock)
43 : clock_(clock),
44 num_connections_(kDefaultNumConnections),
45 epoch_(QuicTime::Zero()),
46 last_update_time_(QuicTime::Zero()) {
47 Reset();
50 void CubicBytes::SetNumConnections(int num_connections) {
51 num_connections_ = num_connections;
54 float CubicBytes::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 CubicBytes::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 CubicBytes::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_bytes_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 QuicByteCount CubicBytes::CongestionWindowAfterPacketLoss(
83 QuicByteCount 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 QuicByteCount CubicBytes::CongestionWindowAfterAck(
97 QuicByteCount acked_bytes,
98 QuicByteCount current_congestion_window,
99 QuicTime::Delta delay_min) {
100 acked_bytes_count_ += acked_bytes;
101 QuicTime current_time = clock_->ApproximateNow();
103 // Cubic is "independent" of RTT, the update is limited by the time elapsed.
104 if (last_congestion_window_ == current_congestion_window &&
105 (current_time.Subtract(last_update_time_) <= MaxCubicTimeInterval())) {
106 return max(last_target_congestion_window_,
107 estimated_tcp_congestion_window_);
109 last_congestion_window_ = current_congestion_window;
110 last_update_time_ = current_time;
112 if (!epoch_.IsInitialized()) {
113 // First ACK after a loss event.
114 DVLOG(1) << "Start of epoch";
115 epoch_ = current_time; // Start of epoch.
116 acked_bytes_count_ = acked_bytes; // Reset count.
117 // Reset estimated_tcp_congestion_window_ to be in sync with cubic.
118 estimated_tcp_congestion_window_ = current_congestion_window;
119 if (last_max_congestion_window_ <= current_congestion_window) {
120 time_to_origin_point_ = 0;
121 origin_point_congestion_window_ = current_congestion_window;
122 } else {
123 time_to_origin_point_ =
124 static_cast<uint32>(cbrt(kCubeFactor * (last_max_congestion_window_ -
125 current_congestion_window)));
126 origin_point_congestion_window_ = last_max_congestion_window_;
129 // Change the time unit from microseconds to 2^10 fractions per second. Take
130 // the round trip time in account. This is done to allow us to use shift as a
131 // divide operator.
132 int64 elapsed_time =
133 (current_time.Add(delay_min).Subtract(epoch_).ToMicroseconds() << 10) /
134 kNumMicrosPerSecond;
136 int64 offset = time_to_origin_point_ - elapsed_time;
137 QuicByteCount delta_congestion_window =
138 ((kCubeCongestionWindowScale * offset * offset * offset) >> kCubeScale) *
139 kDefaultTCPMSS;
141 QuicByteCount target_congestion_window =
142 origin_point_congestion_window_ - delta_congestion_window;
144 DCHECK_LT(0u, estimated_tcp_congestion_window_);
145 // Increase the window by Alpha * 1 MSS of bytes every time we ack an
146 // estimated tcp window of bytes.
147 estimated_tcp_congestion_window_ += acked_bytes_count_ *
148 (Alpha() * kDefaultTCPMSS) /
149 estimated_tcp_congestion_window_;
150 acked_bytes_count_ = 0;
152 // We have a new cubic congestion window.
153 last_target_congestion_window_ = target_congestion_window;
155 // Compute target congestion_window based on cubic target and estimated TCP
156 // congestion_window, use highest (fastest).
157 if (target_congestion_window < estimated_tcp_congestion_window_) {
158 target_congestion_window = estimated_tcp_congestion_window_;
161 DVLOG(1) << "Target congestion_window: " << target_congestion_window;
162 return target_congestion_window;
165 } // namespace net