-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbandwidth_sampler.cpp
308 lines (269 loc) · 10.1 KB
/
bandwidth_sampler.cpp
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include <bandwidth_sampler.h>
namespace bbr
{
uint64_t MaxAckHeightTracker::update(common::BandWidth bw,
int round_trip_count, time::Timestamp ack_time, uint64_t bytes_acked)
{
if (!aggregation_epoch_start_time_.is_valid()) {
aggregation_epoch_bytes_ = bytes_acked;
aggregation_epoch_start_time_ = ack_time;
++num_ack_aggregation_epochs_;
return 0;
}
// Compute how many bytes are expected to be delivered, assuming bw is correct.
size_t expected_bytes_acked = bw * (ack_time - aggregation_epoch_start_time_) / 8;
// Reset the current aggregation epoch as soon as the ack arrival rate is less
// than or equal to the max bandwidth.
if (aggregation_epoch_bytes_ <= threshold_ * expected_bytes_acked) {
// Reset to start measuring a new aggregation epoch.
aggregation_epoch_bytes_ = bytes_acked;
aggregation_epoch_start_time_ = ack_time;
++num_ack_aggregation_epochs_;
return 0;
}
aggregation_epoch_bytes_ += bytes_acked;
// Compute how many extra bytes were delivered vs max bandwidth.
size_t extra_bytes_acked = aggregation_epoch_bytes_ - expected_bytes_acked;
max_ack_height_filter_.update(extra_bytes_acked, round_trip_count);
return extra_bytes_acked;
}
BandwidthSampler::BandwidthSampler(int ack_track_win)
:max_ack_height_tracker_(ack_track_win)
{
max_ack_height_tracker_.set_threshold(2.0);
}
void BandwidthSampler::on_packet_sent(uint64_t seq_no, size_t bytes,
size_t bytes_in_flight, time::Timestamp at_time, bool need_retransmite)
{
last_sent_packet_ = seq_no;
if(!need_retransmite) {
return ;
}
total_bytes_sent_ += bytes;
if(bytes_in_flight == 0) {
//assume we received last ack at this moment
last_acked_packet_ack_time_ = at_time;
total_bytes_sent_at_last_acked_packet_ = total_bytes_sent_;
// In this situation ack compression is not a concern, set send rate to
// effectively infinite.
last_acked_packet_sent_time_ = at_time;
ack_points_.clear();
ack_points_.update(at_time, total_bytes_acked_);
a0_candidates_.clear();
a0_candidates_.push_back(ack_points_.recent_point());
}
SendTimeState cur_state {
false, //invalid
is_app_limited_,
total_bytes_sent_,
total_bytes_acked_,
total_bytes_lost_,
bytes + bytes_in_flight};
state_map_.insert(std::make_pair(seq_no,
ConnectionStateOnSentPacket{
bytes,
at_time,
total_bytes_sent_at_last_acked_packet_,
last_acked_packet_sent_time_,
last_acked_packet_ack_time_,
cur_state}));
//TODO: warn when state_map_ contain too much tracked packet
}
CongestionEventSample BandwidthSampler::on_congestion_event(time::Timestamp ack_time,
const std::vector<internal::AckedPacket>& acked_pkts,
const std::vector<internal::LostPacket>& lost_pkts,
common::BandWidth max_bw,
common::BandWidth estimated_bw_upper_bound,
int round_count)
{
CongestionEventSample event_sample;
SendTimeState last_lost_packet_send_state;
for (const auto& pkt : lost_pkts) {
SendTimeState send_state = on_pkt_lost(pkt.seq_no, pkt.bytes);
if (send_state.is_valid) {
last_lost_packet_send_state = send_state;
}
}
if (acked_pkts.empty()) {
// Only populate send state for a loss-only event.
event_sample.last_packet_send_state = last_lost_packet_send_state;
return event_sample;
}
SendTimeState last_acked_packet_send_state;
for (const auto& pkt : acked_pkts) {
BandwidthSample sample = on_pkt_acked( pkt.seq_no, ack_time);
if (!sample.state_at_send.is_valid) {
continue;
}
last_acked_packet_send_state = sample.state_at_send;
//get minimum rtt
if (sample.rtt.is_valid()) {
event_sample.sample_rtt = std::min(event_sample.sample_rtt, sample.rtt);
}
//get maximum bandwidth
if (sample.bandwidth.is_valid() &&
sample.bandwidth > event_sample.sample_max_bandwidth) {
event_sample.sample_max_bandwidth = sample.bandwidth;
event_sample.sample_is_app_limited = sample.state_at_send.is_app_limited;
}
//get maximum inflight
size_t inflight_sample = total_bytes_acked_ - last_acked_packet_send_state.total_bytes_acked;
if (inflight_sample > event_sample.sample_max_inflight) {
event_sample.sample_max_inflight = inflight_sample;
}
}
if (!last_lost_packet_send_state.is_valid)
{
event_sample.last_packet_send_state = last_acked_packet_send_state;
}
else if (!last_acked_packet_send_state.is_valid)
{
event_sample.last_packet_send_state = last_lost_packet_send_state;
}
else
{
// If two packets are inflight and an alarm is armed to lose a packet and it
// wakes up late, then the first of two in flight packets could have been
// acknowledged before the wakeup, which re-evaluates loss detection, and
// could declare the later of the two lost.
event_sample.last_packet_send_state = lost_pkts.back().seq_no > acked_pkts.back().seq_no
? last_lost_packet_send_state : last_acked_packet_send_state;
}
max_bw = std::max(max_bw, event_sample.sample_max_bandwidth);
max_bw = std::min(max_bw, estimated_bw_upper_bound);
event_sample.extra_acked = extra_acked(max_bw, round_count);
return event_sample;
}
SendTimeState BandwidthSampler::on_pkt_lost(uint64_t seq_no, size_t bytes)
{
SendTimeState state;
total_bytes_lost_ += bytes;
auto iter = state_map_.find(seq_no);
if(iter != state_map_.end())
{
connection_state_to_sent_state(iter->second, state);
}
return state;
}
BandwidthSample BandwidthSampler::on_pkt_acked(uint64_t seq_no, time::Timestamp ack_time)
{
BandwidthSample sample;
auto iter = state_map_.find(seq_no);
if(iter == state_map_.end()) {
return sample;
}
ConnectionStateOnSentPacket& sent_pkt = iter->second;
total_bytes_acked_ += sent_pkt.bytes;
total_bytes_sent_at_last_acked_packet_ = sent_pkt.state.total_bytes_sent;
last_acked_packet_sent_time_ = sent_pkt.sent_time;
last_acked_packet_ack_time_ = ack_time;
ack_points_.update(ack_time, total_bytes_acked_);
if(is_app_limited_) {
// Exit app-limited phase in two cases:
// (1) end_of_app_limited_phase_ is not initialized, i.e., so far all
// packets are sent while there are buffered packets or pending data.
// (2) The current acked packet is after the sent packet marked as the end
// of the app limit phase.
if (end_of_app_limited_phase_ == std::numeric_limits<uint64_t>::max()
|| seq_no > end_of_app_limited_phase_)
{
is_app_limited_ = false;
}
}
// There might have been no packets acknowledged at the moment when the
// current packet was sent. In that case, there is no bandwidth sample to
// make.
if(!sent_pkt.last_acked_pkt_sent_time.is_valid()) {
return sample;
}
common::BitRate send_rate;
if (sent_pkt.sent_time > sent_pkt.last_acked_pkt_sent_time) {
send_rate = (sent_pkt.state.total_bytes_sent - sent_pkt.total_sent_bytes_at_last_acked_pkt) /
(sent_pkt.sent_time - sent_pkt.last_acked_pkt_sent_time);
}
AckPoint a0;
if(!choose_a0(sent_pkt.state.total_bytes_acked, a0)) {
a0.ack_time = sent_pkt.last_acked_pkt_ack_time;
a0.total_bytes_acked = sent_pkt.state.total_bytes_acked;
}
assert(a0.ack_time < ack_time);
common::BitRate ack_rate = (total_bytes_acked_ - a0.total_bytes_acked) / (ack_time - a0.ack_time);
sample.bandwidth = std::min(send_rate, ack_rate);
sample.rtt = ack_time - sent_pkt.sent_time;
connection_state_to_sent_state(sent_pkt, sample.state_at_send);
return sample;
}
size_t BandwidthSampler::extra_acked(common::BandWidth max_bw, int round_count)
{
size_t newly_acked_bytes = total_bytes_acked_ -
total_bytes_acked_after_last_ack_event_;
if (newly_acked_bytes == 0) {
return 0;
}
total_bytes_acked_after_last_ack_event_ = total_bytes_acked_;
size_t extra_acked = max_ack_height_tracker_.update(max_bw, round_count,
last_acked_packet_ack_time_, newly_acked_bytes);
if(extra_acked == 0) {
a0_candidates_.push_back(ack_points_.less_recent_point());
}
return extra_acked;
}
bool BandwidthSampler::choose_a0(size_t total_bytes_acked, AckPoint& point)
{
if (a0_candidates_.empty()) {
return false;
}
if (a0_candidates_.size() == 1) {
point = a0_candidates_.front();
return true;
}
for (size_t i = 1; i < a0_candidates_.size(); ++i) {
if (a0_candidates_[i].total_bytes_acked > total_bytes_acked) {
point = a0_candidates_[i-1];
while (i > 1) {
a0_candidates_.pop_front();
i--;
}
return true;
}
}
// All candidates' total_bytes_acked is <= |total_bytes_acked|.
point = a0_candidates_.back();
while(a0_candidates_.size() > 1) {
a0_candidates_.pop_front();
}
return true;
}
void BandwidthSampler::on_pkt_neutered(uint64_t seq_no)
{
auto iter = state_map_.find(seq_no);
if(iter == state_map_.end()) {
return;
}
total_bytes_neutered_ += iter->second.bytes;
state_map_.erase(iter);
}
//[0, up_to)
void BandwidthSampler::remove_obsolete_pkts(uint64_t up_to)
{
auto iter = state_map_.lower_bound(up_to);
if(iter != state_map_.end()) {
state_map_.erase(state_map_.begin(), iter);
}
}
void BandwidthSampler::on_app_limited()
{
is_app_limited_ = true;
end_of_app_limited_phase_ = last_sent_packet_;
}
bool BandwidthSampler::is_app_limited()
{
return is_app_limited_;
}
void BandwidthSampler::connection_state_to_sent_state(
const ConnectionStateOnSentPacket& s1, SendTimeState& s2)
{
s2 = s1.state;
s2.is_valid = true;
}
}