OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // A test only class to enable simulations of send algorithms. | 5 // A test only class to enable simulations of send algorithms. |
6 | 6 |
7 #ifndef NET_QUIC_CONGESTION_CONTROL_SEND_ALGORITHM_SIMULATOR_H_ | 7 #ifndef NET_QUIC_CONGESTION_CONTROL_SEND_ALGORITHM_SIMULATOR_H_ |
8 #define NET_QUIC_CONGESTION_CONTROL_SEND_ALGORITHM_SIMULATOR_H_ | 8 #define NET_QUIC_CONGESTION_CONTROL_SEND_ALGORITHM_SIMULATOR_H_ |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
| 11 #include <vector> |
11 | 12 |
12 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
13 #include "net/quic/congestion_control/send_algorithm_interface.h" | 14 #include "net/quic/congestion_control/send_algorithm_interface.h" |
14 #include "net/quic/quic_protocol.h" | 15 #include "net/quic/quic_protocol.h" |
15 #include "net/quic/quic_time.h" | 16 #include "net/quic/quic_time.h" |
16 #include "net/quic/test_tools/mock_clock.h" | 17 #include "net/quic/test_tools/mock_clock.h" |
17 #include "net/quic/test_tools/quic_test_utils.h" | 18 #include "net/quic/test_tools/quic_test_utils.h" |
18 | 19 |
19 namespace net { | 20 namespace net { |
20 | 21 |
21 class SendAlgorithmSimulator { | 22 class SendAlgorithmSimulator { |
22 public: | 23 public: |
| 24 struct Sender { |
| 25 Sender(SendAlgorithmInterface* send_algorithm, RttStats* rtt_stats); |
| 26 |
| 27 void RecordStats() { |
| 28 QuicByteCount cwnd = send_algorithm->GetCongestionWindow(); |
| 29 max_cwnd = std::max(max_cwnd, cwnd); |
| 30 min_cwnd = std::min(min_cwnd, cwnd); |
| 31 if (last_cwnd > cwnd) { |
| 32 max_cwnd_drop = std::max(max_cwnd_drop, last_cwnd - cwnd); |
| 33 } |
| 34 last_cwnd = cwnd; |
| 35 } |
| 36 |
| 37 SendAlgorithmInterface* send_algorithm; |
| 38 RttStats* rtt_stats; |
| 39 |
| 40 // Last sequence number the sender sent. |
| 41 QuicPacketSequenceNumber last_sent; |
| 42 // Last packet sequence number acked. |
| 43 QuicPacketSequenceNumber last_acked; |
| 44 // Packet sequence number to ack up to. |
| 45 QuicPacketSequenceNumber next_acked; |
| 46 |
| 47 // Stats collected for understanding the congestion control. |
| 48 QuicByteCount max_cwnd; |
| 49 QuicByteCount min_cwnd; |
| 50 QuicByteCount max_cwnd_drop; |
| 51 QuicByteCount last_cwnd; |
| 52 |
| 53 QuicBandwidth last_transfer_bandwidth; |
| 54 }; |
| 55 |
| 56 struct Transfer { |
| 57 Transfer(Sender* sender, QuicByteCount num_bytes, QuicTime start_time) |
| 58 : sender(sender), |
| 59 num_bytes(num_bytes), |
| 60 bytes_acked(0), |
| 61 bytes_in_flight(0), |
| 62 start_time(start_time) {} |
| 63 |
| 64 Sender* sender; |
| 65 QuicByteCount num_bytes; |
| 66 QuicByteCount bytes_acked; |
| 67 QuicByteCount bytes_in_flight; |
| 68 QuicTime start_time; |
| 69 }; |
| 70 |
23 struct SentPacket { | 71 struct SentPacket { |
24 SentPacket(QuicPacketSequenceNumber sequence_number, | 72 SentPacket(QuicPacketSequenceNumber sequence_number, |
25 QuicTime send_time, | 73 QuicTime send_time, |
26 QuicTime ack_time) | 74 QuicTime ack_time, |
| 75 Transfer* transfer) |
27 : sequence_number(sequence_number), | 76 : sequence_number(sequence_number), |
28 send_time(send_time), | 77 send_time(send_time), |
29 ack_time(ack_time) {} | 78 ack_time(ack_time), |
| 79 transfer(transfer) {} |
| 80 |
30 QuicPacketSequenceNumber sequence_number; | 81 QuicPacketSequenceNumber sequence_number; |
31 QuicTime send_time; | 82 QuicTime send_time; |
32 QuicTime ack_time; | 83 QuicTime ack_time; |
| 84 Transfer* transfer; |
33 }; | 85 }; |
34 | 86 |
35 // |rtt_stats| should be the same RttStats used by the |send_algorithm|. | 87 // |rtt_stats| should be the same RttStats used by the |send_algorithm|. |
36 SendAlgorithmSimulator(SendAlgorithmInterface* send_algorithm, | 88 SendAlgorithmSimulator(MockClock* clock_, |
37 MockClock* clock_, | |
38 RttStats* rtt_stats, | |
39 QuicBandwidth bandwidth, | 89 QuicBandwidth bandwidth, |
40 QuicTime::Delta rtt); | 90 QuicTime::Delta rtt); |
41 ~SendAlgorithmSimulator(); | 91 ~SendAlgorithmSimulator(); |
42 | 92 |
| 93 void set_bandwidth(QuicBandwidth bandwidth) { |
| 94 bandwidth_ = bandwidth; |
| 95 } |
| 96 |
43 void set_forward_loss_rate(float loss_rate) { | 97 void set_forward_loss_rate(float loss_rate) { |
44 DCHECK_LT(loss_rate, 1.0f); | 98 DCHECK_LT(loss_rate, 1.0f); |
45 forward_loss_rate_ = loss_rate; | 99 forward_loss_rate_ = loss_rate; |
46 } | 100 } |
47 | 101 |
48 void set_reverse_loss_rate(float loss_rate) { | 102 void set_reverse_loss_rate(float loss_rate) { |
49 DCHECK_LT(loss_rate, 1.0f); | 103 DCHECK_LT(loss_rate, 1.0f); |
50 reverse_loss_rate_ = loss_rate; | 104 reverse_loss_rate_ = loss_rate; |
51 } | 105 } |
52 | 106 |
53 void set_loss_correlation(float loss_correlation) { | 107 void set_loss_correlation(float loss_correlation) { |
54 DCHECK_LT(loss_correlation, 1.0f); | 108 DCHECK_LT(loss_correlation, 1.0f); |
55 loss_correlation_ = loss_correlation; | 109 loss_correlation_ = loss_correlation; |
56 } | 110 } |
57 | 111 |
58 void set_buffer_size(size_t buffer_size_bytes) { | 112 void set_buffer_size(size_t buffer_size_bytes) { |
59 buffer_size_ = buffer_size_bytes; | 113 buffer_size_ = buffer_size_bytes; |
60 } | 114 } |
61 | 115 |
62 // Sends the specified number of bytes as quickly as possible and returns the | |
63 // average bandwidth in bytes per second. The time elapsed is based on | |
64 // waiting for all acks to arrive. | |
65 QuicBandwidth SendBytes(size_t num_bytes); | |
66 | |
67 const RttStats* rtt_stats() const { return rtt_stats_; } | |
68 | |
69 QuicByteCount max_cwnd() const { return max_cwnd_; } | |
70 QuicByteCount min_cwnd() const { return min_cwnd_; } | |
71 QuicByteCount max_cwnd_drop() const { return max_cwnd_drop_; } | |
72 QuicByteCount last_cwnd() const { return last_cwnd_; } | |
73 | |
74 private: | |
75 // NextAckTime takes into account packet loss in both forward and reverse | |
76 // direction, as well as delayed ack behavior. | |
77 QuicTime::Delta NextAckDelta(); | |
78 | |
79 // Whether all packets in sent_packets_ are lost. | |
80 bool AllPacketsLost(); | |
81 | |
82 // Sets the next acked. | |
83 void FindNextAcked(); | |
84 | |
85 // Process all the acks that should have arrived by the current time, and | |
86 // lose any packets that are missing. Returns the number of bytes acked. | |
87 int HandlePendingAck(); | |
88 | |
89 void SendDataNow(); | |
90 void RecordStats(); | |
91 | |
92 // Advance the time by |delta| without sending anything. | 116 // Advance the time by |delta| without sending anything. |
93 void AdvanceTime(QuicTime::Delta delta); | 117 void AdvanceTime(QuicTime::Delta delta); |
94 | 118 |
95 // Elapsed time from the start of the connection. | 119 // Adds a pending sender. The send will run when TransferBytes is called. |
96 QuicTime ElapsedTime(); | 120 // Adding two transfers with the same sender is unsupported. |
| 121 void AddTransfer(Sender* sender, size_t num_bytes); |
97 | 122 |
98 SendAlgorithmInterface* send_algorithm_; | 123 // Adds a pending sending to start at the specified time. |
| 124 void AddTransfer(Sender* sender, size_t num_bytes, QuicTime start_time); |
| 125 |
| 126 // Convenience method to transfer all bytes. |
| 127 void TransferBytes(); |
| 128 |
| 129 // Transfers bytes through the connection until |max_bytes| are reached, |
| 130 // |max_time| is reached, or all senders have finished sending. If max_bytes |
| 131 // is 0, it does not apply, and if |max_time| is Zero, no time limit applies. |
| 132 void TransferBytes(QuicByteCount max_bytes, QuicTime::Delta max_time); |
| 133 |
| 134 private: |
| 135 // A pending packet event, either a send or an ack. |
| 136 struct PacketEvent { |
| 137 PacketEvent(QuicTime::Delta time_delta, Transfer* transfer) |
| 138 : time_delta(time_delta), |
| 139 transfer(transfer) {} |
| 140 |
| 141 QuicTime::Delta time_delta; |
| 142 Transfer* transfer; |
| 143 }; |
| 144 |
| 145 // NextSendTime returns the next time any of the pending transfers send, |
| 146 // and populates transfer if the send time is not infinite. |
| 147 PacketEvent NextSendEvent(); |
| 148 |
| 149 // NextAckTime takes into account packet loss in both forward and reverse |
| 150 // direction, as well as delayed ack behavior. |
| 151 PacketEvent NextAckEvent(); |
| 152 |
| 153 // Sets the next acked. |
| 154 QuicTime::Delta FindNextAcked(Transfer* transfer); |
| 155 |
| 156 // Process all the acks that should have arrived by the current time, and |
| 157 // lose any packets that are missing. Returns the number of bytes acked. |
| 158 void HandlePendingAck(Transfer* transfer); |
| 159 |
| 160 void SendDataNow(Transfer* transfer); |
| 161 |
| 162 // List of all pending transfers waiting to use the connection. |
| 163 std::vector<Transfer> pending_transfers_; |
| 164 |
99 MockClock* clock_; | 165 MockClock* clock_; |
100 RttStats* rtt_stats_; | |
101 // Next packet sequence number to send. | |
102 QuicPacketSequenceNumber next_sent_; | |
103 // Last packet sequence number acked. | |
104 QuicPacketSequenceNumber last_acked_; | |
105 // Packet sequence number to ack up to. | |
106 QuicPacketSequenceNumber next_acked_; | |
107 // Whether the next ack should be lost. | 166 // Whether the next ack should be lost. |
108 bool lose_next_ack_; | 167 bool lose_next_ack_; |
109 QuicByteCount bytes_in_flight_; | |
110 // The times acks are expected, assuming acks are not lost and every packet | 168 // The times acks are expected, assuming acks are not lost and every packet |
111 // is acked. | 169 // is acked. |
112 std::list<SentPacket> sent_packets_; | 170 std::list<SentPacket> sent_packets_; |
113 | 171 |
114 test::SimpleRandom simple_random_; | 172 test::SimpleRandom simple_random_; |
115 float forward_loss_rate_; // Loss rate on the forward path. | 173 float forward_loss_rate_; // Loss rate on the forward path. |
116 float reverse_loss_rate_; // Loss rate on the reverse path. | 174 float reverse_loss_rate_; // Loss rate on the reverse path. |
117 float loss_correlation_; // Likelihood the subsequent packet is lost. | 175 float loss_correlation_; // Likelihood the subsequent packet is lost. |
118 QuicBandwidth bandwidth_; | 176 QuicBandwidth bandwidth_; |
119 QuicTime::Delta rtt_; | 177 QuicTime::Delta rtt_; |
120 size_t buffer_size_; // In bytes. | 178 size_t buffer_size_; // In bytes. |
121 | 179 |
122 // Stats collected for understanding the congestion control. | |
123 QuicByteCount max_cwnd_; | |
124 QuicByteCount min_cwnd_; | |
125 QuicByteCount max_cwnd_drop_; | |
126 QuicByteCount last_cwnd_; | |
127 | |
128 DISALLOW_COPY_AND_ASSIGN(SendAlgorithmSimulator); | 180 DISALLOW_COPY_AND_ASSIGN(SendAlgorithmSimulator); |
129 }; | 181 }; |
130 | 182 |
131 } // namespace net | 183 } // namespace net |
132 | 184 |
133 #endif // NET_QUIC_CONGESTION_CONTROL_SEND_ALGORITHM_SIMULATOR_H_ | 185 #endif // NET_QUIC_CONGESTION_CONTROL_SEND_ALGORITHM_SIMULATOR_H_ |
OLD | NEW |