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
|
// Copyright (c) 2012 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 "net/quic/congestion_control/quic_send_scheduler.h"
#include <algorithm>
#include <cmath>
#include <map>
#include "base/stl_util.h"
#include "base/time.h"
#include "net/quic/congestion_control/send_algorithm_interface.h"
//#include "util/gtl/map-util.h"
using std::map;
using std::max;
namespace net {
const int64 kNumMicrosPerSecond = base::Time::kMicrosecondsPerSecond;
QuicSendScheduler::QuicSendScheduler(
const QuicClock* clock,
CongestionFeedbackType type)
: clock_(clock),
current_estimated_bandwidth_(-1),
max_estimated_bandwidth_(-1),
last_sent_packet_(QuicTime::FromMicroseconds(0)),
current_packet_bucket_(-1),
first_packet_bucket_(-1),
send_algorithm_(SendAlgorithmInterface::Create(clock, type)) {
memset(packet_history_, 0, sizeof(packet_history_));
}
QuicSendScheduler::~QuicSendScheduler() {
STLDeleteContainerPairSecondPointers(pending_packets_.begin(),
pending_packets_.end());
}
int QuicSendScheduler::UpdatePacketHistory() {
int timestamp_scaled = clock_->Now().ToMicroseconds() /
kBitrateSmoothingPeriod;
int bucket = timestamp_scaled % kBitrateSmoothingBuckets;
if (!HasSentPacket()) {
// First packet.
current_packet_bucket_ = bucket;
first_packet_bucket_ = bucket;
}
if (current_packet_bucket_ != bucket) {
// We need to make sure to zero out any skipped buckets.
// Max loop count is kBitrateSmoothingBuckets.
do {
current_packet_bucket_ =
(1 + current_packet_bucket_) % kBitrateSmoothingBuckets;
packet_history_[current_packet_bucket_] = 0;
if (first_packet_bucket_ == current_packet_bucket_) {
// We have filled the whole window, no need to keep track of first
// bucket.
first_packet_bucket_ = -1;
}
} while (current_packet_bucket_ != bucket);
}
return bucket;
}
void QuicSendScheduler::SentPacket(QuicPacketSequenceNumber sequence_number,
size_t bytes,
bool retransmit) {
int bucket = UpdatePacketHistory();
packet_history_[bucket] += bytes;
send_algorithm_->SentPacket(sequence_number, bytes, retransmit);
if (!retransmit) {
pending_packets_[sequence_number] =
new PendingPacket(bytes, clock_->Now());
}
DLOG(INFO) << "Sent sequence number:" << sequence_number;
}
void QuicSendScheduler::OnIncomingQuicCongestionFeedbackFrame(
const QuicCongestionFeedbackFrame& congestion_feedback_frame) {
send_algorithm_->OnIncomingQuicCongestionFeedbackFrame(
congestion_feedback_frame);
}
void QuicSendScheduler::OnIncomingAckFrame(const QuicAckFrame& ack_frame) {
// We want to.
// * Get all packets lower(including) than largest_observed
// from pending_packets_.
// * Remove all missing packets.
// * Send each ACK in the list to send_algorithm_.
QuicTime last_timestamp(QuicTime::FromMicroseconds(0));
map<QuicPacketSequenceNumber, size_t> acked_packets;
PendingPacketsMap::iterator it, it_upper;
it = pending_packets_.begin();
it_upper = pending_packets_.upper_bound(
ack_frame.received_info.largest_observed);
while (it != it_upper) {
QuicPacketSequenceNumber sequence_number = it->first;
if (!ack_frame.received_info.IsAwaitingPacket(sequence_number)) {
// Not missing, hence implicitly acked.
scoped_ptr<PendingPacket> pending_packet_cleaner(it->second);
acked_packets[sequence_number] = pending_packet_cleaner->BytesSent();
last_timestamp = pending_packet_cleaner->SendTimestamp();
pending_packets_.erase(it++); // Must be incremented post to work.
} else {
++it;
}
}
// We calculate the RTT based on the highest ACKed sequence number, the lower
// sequence numbers will include the ACK aggregation delay.
QuicTime::Delta rtt = clock_->Now().Subtract(last_timestamp);
map<QuicPacketSequenceNumber, size_t>::iterator it_acked_packets;
for (it_acked_packets = acked_packets.begin();
it_acked_packets != acked_packets.end();
++it_acked_packets) {
send_algorithm_->OnIncomingAck(it_acked_packets->first,
it_acked_packets->second,
rtt);
DLOG(INFO) << "ACKed sequence number:" << it_acked_packets->first;
}
}
QuicTime::Delta QuicSendScheduler::TimeUntilSend(bool retransmit) {
return send_algorithm_->TimeUntilSend(retransmit);
}
size_t QuicSendScheduler::AvailableCongestionWindow() {
return send_algorithm_->AvailableCongestionWindow();
}
int QuicSendScheduler::BandwidthEstimate() {
int bandwidth_estimate = send_algorithm_->BandwidthEstimate();
if (bandwidth_estimate == kNoValidEstimate) {
// If we don't have a valid estimate use the send rate.
return SentBandwidth();
}
return bandwidth_estimate;
}
bool QuicSendScheduler::HasSentPacket() {
return (current_packet_bucket_ != -1);
}
// TODO(pwestin) add a timer to make this accurate even if not called.
int QuicSendScheduler::SentBandwidth() {
UpdatePacketHistory();
if (first_packet_bucket_ != -1) {
// We don't have a full set of data.
int number_of_buckets = (current_packet_bucket_ - first_packet_bucket_) + 1;
if (number_of_buckets < 0) {
// We have a wrap in bucket index.
number_of_buckets = kBitrateSmoothingBuckets + number_of_buckets;
}
int64 sum = 0;
int bucket = first_packet_bucket_;
for (int n = 0; n < number_of_buckets; bucket++, n++) {
bucket = bucket % kBitrateSmoothingBuckets;
sum += packet_history_[bucket];
}
current_estimated_bandwidth_ = (sum *
(kNumMicrosPerSecond / kBitrateSmoothingPeriod)) / number_of_buckets;
} else {
int64 sum = 0;
for (uint32 bucket = 0; bucket < kBitrateSmoothingBuckets; ++bucket) {
sum += packet_history_[bucket];
}
current_estimated_bandwidth_ = (sum * (kNumMicrosPerSecond /
kBitrateSmoothingPeriod)) / kBitrateSmoothingBuckets;
}
max_estimated_bandwidth_ = max(max_estimated_bandwidth_,
current_estimated_bandwidth_);
return current_estimated_bandwidth_;
}
int QuicSendScheduler::PeakSustainedBandwidth() {
// To make sure that we get the latest estimate we call SentBandwidth.
if (HasSentPacket()) {
SentBandwidth();
}
return max_estimated_bandwidth_;
}
} // namespace net
|