summaryrefslogtreecommitdiffstats
path: root/net/curvecp/rtt_and_send_rate_calculator.cc
blob: da2be78b857b1418ab21eabe8f5e8d2e38cbd4b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stdlib.h>

#include "net/curvecp/rtt_and_send_rate_calculator.h"

namespace net {

RttAndSendRateCalculator::RttAndSendRateCalculator()
    : rtt_latest_(0),
      rtt_average_(0),
      rtt_deviation_(0),
      rtt_lowwater_(0),
      rtt_highwater_(0),
      rtt_timeout_(base::Time::kMicrosecondsPerSecond),
      send_rate_(1000000),
      rtt_seenrecenthigh_(0),
      rtt_seenrecentlow_(0),
      rtt_seenolderhigh_(0),
      rtt_seenolderlow_(0),
      rtt_phase_(false) {
}

void RttAndSendRateCalculator::OnMessage(int32 rtt_us) {
  UpdateRTT(rtt_us);
  AdjustSendRate();
}

void RttAndSendRateCalculator::OnTimeout() {
  base::TimeDelta time_since_last_loss = last_sample_time_ - last_loss_time_;
  if (time_since_last_loss.InMilliseconds() < 4 * rtt_timeout_)
    return;
  send_rate_ *= 2;
  last_loss_time_ = last_sample_time_;
  last_edge_time_ = last_sample_time_;
}

// Updates RTT
void RttAndSendRateCalculator::UpdateRTT(int32 rtt_us) {
  rtt_latest_ = rtt_us;
  last_sample_time_ = base::TimeTicks::Now();

  // Initialize for the first sample.
  if (!rtt_average_) {
    rtt_average_ = rtt_latest_;
    rtt_deviation_ = rtt_latest_;
    rtt_highwater_ = rtt_latest_;
    rtt_lowwater_ = rtt_latest_;
  }

  // Jacobson's retransmission timeout calculation.
  int32 rtt_delta = rtt_latest_ - rtt_average_;
  rtt_average_ += rtt_delta / 8;
  if (rtt_delta < 0)
    rtt_delta = -rtt_delta;
  rtt_delta -= rtt_deviation_;
  rtt_deviation_ += rtt_delta / 4;
  rtt_timeout_ = rtt_average_ + (4 * rtt_deviation_);

  // Adjust for delayed acks with anti-spiking.
  // TODO(mbelshe): this seems high.
  rtt_timeout_ += 8 * send_rate_;

  // Recognize the top and bottom of the congestion cycle.
  rtt_delta = rtt_latest_ - rtt_highwater_;
  rtt_highwater_ += rtt_delta / 1024;

  rtt_delta = rtt_latest_ - rtt_lowwater_;
  if (rtt_delta > 0)
    rtt_lowwater_ += rtt_delta / 8192;
  else
    rtt_lowwater_ += rtt_delta / 256;
}

void RttAndSendRateCalculator::AdjustSendRate() {
  last_sample_time_ = base::TimeTicks::Now();

  base::TimeDelta time = last_sample_time_ - last_send_adjust_time_;

  // We only adjust the send rate approximately every 16 samples.
  if (time.InMicroseconds() < 16 * send_rate_)
    return;

  if (rtt_average_ >
      rtt_highwater_ + (5 * base::Time::kMicrosecondsPerMillisecond))
    rtt_seenrecenthigh_ = true;
  else if (rtt_average_ < rtt_lowwater_)
    rtt_seenrecentlow_ = true;

  last_send_adjust_time_ = last_sample_time_;

  // If too much time has elapsed, re-initialize the send_rate.
  if (time.InMicroseconds() >  10 * base::Time::kMicrosecondsPerSecond) {
    send_rate_ =  base::Time::kMicrosecondsPerSecond;  // restart.
    send_rate_ += randommod(send_rate_ / 8);
  }

  // TODO(mbelshe):  Why is 128us a lower bound?
  if (send_rate_ >= 128) {
    // Additive increase:  adjust 1/N by a constant c.
    // rtt-fair additive increase: adjust 1/N by a constant c every nanosec.
    // approximation: adjust 1/N by cN every N nanoseconds.

    // TODO(mbelshe): he used c == 2^-51. for nanosecs.
    //                I use c ==  2^31, for microsecs.

    // i.e. N <- 1/(1/N + cN) = N/(1 + cN^2) every N nanosec.
    if (false && send_rate_ < 16384) {
      // N/(1+cN^2) approx N - cN^3
      // TODO(mbelshe):  note that he is using (cN)^3 here, not what he wrote.
      int32 msec = send_rate_ / 128;
      send_rate_ -= msec * msec * msec;
    } else {
      double d = send_rate_;
      send_rate_ = d / (1 + d * d / 2147483648.0);  // 2^31
    }
  }

  if (rtt_phase_ == false) {
    if (rtt_seenolderhigh_) {
      rtt_phase_ = true;
      last_edge_time_ = last_sample_time_;
      send_rate_ += randommod(send_rate_ / 4);
    }
  } else {
    if (rtt_seenolderlow_) {
      rtt_phase_ = false;
    }
  }

  rtt_seenolderhigh_ = rtt_seenrecenthigh_;
  rtt_seenolderlow_ = rtt_seenrecentlow_;
  rtt_seenrecenthigh_ = false;
  rtt_seenrecentlow_ = false;

  AttemptToDoubleSendRate();
}

void RttAndSendRateCalculator::AttemptToDoubleSendRate() {
  base::TimeDelta time_since_edge = last_sample_time_ - last_edge_time_;
  base::TimeDelta time_since_doubling =
      last_sample_time_ - last_doubling_time_;


  int32 threshold = 0;
  if (time_since_edge.InMicroseconds() <
      base::Time::kMicrosecondsPerSecond * 60) {
    threshold = (4 * send_rate_) +
                (64 * rtt_timeout_) +
                (5 * base::Time::kMicrosecondsPerSecond);
  } else {
    threshold = (4 * send_rate_) +
                (2 * rtt_timeout_);
  }
  if (time_since_doubling.InMicroseconds() < threshold)
    return;

  if (send_rate_ < 64)
    return;

  send_rate_ /= 2;
  last_doubling_time_ = last_sample_time_;
}

// returns a random number from 0 to val-1.
int32 RttAndSendRateCalculator::randommod(int32 val) {
  return rand() % val;
}

}  // namespace net