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 #ifndef NET_QUIC_QUIC_WRITE_BLOCKED_LIST_H_ | 5 #ifndef NET_QUIC_QUIC_WRITE_BLOCKED_LIST_H_ |
6 #define NET_QUIC_QUIC_WRITE_BLOCKED_LIST_H_ | 6 #define NET_QUIC_QUIC_WRITE_BLOCKED_LIST_H_ |
7 | 7 |
8 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "net/base/net_export.h" | 10 #include "net/base/net_export.h" |
| 11 #include "net/quic/quic_flags.h" |
11 #include "net/quic/quic_protocol.h" | 12 #include "net/quic/quic_protocol.h" |
12 #include "net/spdy/write_blocked_list.h" | 13 #include "net/spdy/write_blocked_list.h" |
13 | 14 |
14 namespace net { | 15 namespace net { |
15 | 16 |
16 // Keeps tracks of the QUIC streams that have data to write, sorted by | 17 // Keeps tracks of the QUIC streams that have data to write, sorted by |
17 // priority. QUIC stream priority order is: | 18 // priority. QUIC stream priority order is: |
18 // Crypto stream > Headers stream > Data streams by requested priority. | 19 // Crypto stream > Headers stream > Data streams by requested priority. |
19 class NET_EXPORT_PRIVATE QuicWriteBlockedList { | 20 class NET_EXPORT_PRIVATE QuicWriteBlockedList { |
20 private: | 21 private: |
21 typedef WriteBlockedList<QuicStreamId> QuicWriteBlockedListBase; | 22 typedef WriteBlockedList<QuicStreamId> QuicWriteBlockedListBase; |
22 | 23 |
23 public: | 24 public: |
24 static const QuicPriority kHighestPriority; | |
25 static const QuicPriority kLowestPriority; | |
26 | |
27 QuicWriteBlockedList(); | 25 QuicWriteBlockedList(); |
28 ~QuicWriteBlockedList(); | 26 ~QuicWriteBlockedList(); |
29 | 27 |
30 bool HasWriteBlockedDataStreams() const { | 28 bool HasWriteBlockedDataStreams() const { |
31 return base_write_blocked_list_.HasWriteBlockedStreams(); | 29 return base_write_blocked_list_.HasWriteBlockedStreams(); |
32 } | 30 } |
33 | 31 |
34 bool HasWriteBlockedCryptoOrHeadersStream() const { | 32 bool HasWriteBlockedCryptoOrHeadersStream() const { |
35 return crypto_stream_blocked_ || headers_stream_blocked_; | 33 return crypto_stream_blocked_ || headers_stream_blocked_; |
36 } | 34 } |
37 | 35 |
38 size_t NumBlockedStreams() const { | 36 size_t NumBlockedStreams() const { |
39 size_t num_blocked = base_write_blocked_list_.NumBlockedStreams(); | 37 size_t num_blocked = base_write_blocked_list_.NumBlockedStreams(); |
40 if (crypto_stream_blocked_) { | 38 if (crypto_stream_blocked_) { |
41 ++num_blocked; | 39 ++num_blocked; |
42 } | 40 } |
43 if (headers_stream_blocked_) { | 41 if (headers_stream_blocked_) { |
44 ++num_blocked; | 42 ++num_blocked; |
45 } | 43 } |
46 | 44 |
47 return num_blocked; | 45 return num_blocked; |
48 } | 46 } |
49 | 47 |
| 48 // Pops the highest priorty stream, special casing crypto and headers streams. |
| 49 // Latches the most recently popped data stream for batch writing purposes. |
50 QuicStreamId PopFront() { | 50 QuicStreamId PopFront() { |
51 if (crypto_stream_blocked_) { | 51 if (crypto_stream_blocked_) { |
52 crypto_stream_blocked_ = false; | 52 crypto_stream_blocked_ = false; |
53 return kCryptoStreamId; | 53 return kCryptoStreamId; |
54 } | 54 } |
55 | 55 |
56 if (headers_stream_blocked_) { | 56 if (headers_stream_blocked_) { |
57 headers_stream_blocked_ = false; | 57 headers_stream_blocked_ = false; |
58 return kHeadersStreamId; | 58 return kHeadersStreamId; |
59 } | 59 } |
60 | 60 |
61 SpdyPriority priority = | 61 SpdyPriority priority = |
62 base_write_blocked_list_.GetHighestPriorityWriteBlockedList(); | 62 base_write_blocked_list_.GetHighestPriorityWriteBlockedList(); |
63 QuicStreamId id = base_write_blocked_list_.PopFront(priority); | 63 QuicStreamId id = base_write_blocked_list_.PopFront(priority); |
| 64 |
| 65 if (base_write_blocked_list_.NumBlockedStreams(priority) == 0) { |
| 66 // If no streams are blocked, don't bother latching. This stream will be |
| 67 // the first popped for its priority anyway. |
| 68 batch_write_stream_id_[priority] = 0; |
| 69 last_priority_popped_ = priority; |
| 70 } else if (batch_write_stream_id_[priority] != id) { |
| 71 // If newly latching this batch write stream, let it write 16k. |
| 72 batch_write_stream_id_[priority] = id; |
| 73 bytes_left_for_batch_write_[priority] = 16000; |
| 74 last_priority_popped_ = priority; |
| 75 } |
| 76 |
64 return id; | 77 return id; |
65 } | 78 } |
66 | 79 |
67 void PushBack(QuicStreamId stream_id, QuicPriority priority) { | 80 void UpdateBytesForStream(QuicStreamId stream_id, size_t bytes) { |
| 81 if (batch_write_stream_id_[last_priority_popped_] == stream_id) { |
| 82 // If this was the last data stream popped by PopFront, update the |
| 83 // bytes remaining in its batch write. |
| 84 bytes_left_for_batch_write_[last_priority_popped_] -= |
| 85 static_cast<int32>(bytes); |
| 86 } else { |
| 87 // If a batch write stream was set, it should only be preempted by the |
| 88 // crypto or headers streams. Any higher priority data stream would |
| 89 // *become* the new batch write stream. |
| 90 if (FLAGS_quic_respect_send_alarm && FLAGS_quic_batch_writes) { |
| 91 DCHECK(stream_id == kCryptoStreamId || stream_id == kHeadersStreamId || |
| 92 batch_write_stream_id_[last_priority_popped_] == 0 || |
| 93 bytes == 0); |
| 94 } |
| 95 } |
| 96 } |
| 97 |
| 98 // Pushes a stream to the back of the std::list for this priority level |
| 99 // *unless* it is latched for doing batched writes in which case it goes to |
| 100 // the front of the std::list for this priority level. |
| 101 // Headers and crypto streams are special cased to always resume first. |
| 102 void AddStream(QuicStreamId stream_id, SpdyPriority priority) { |
68 if (stream_id == kCryptoStreamId) { | 103 if (stream_id == kCryptoStreamId) { |
69 DCHECK_EQ(kHighestPriority, priority); | 104 DCHECK_EQ(net::kHighestPriority, priority); |
70 // TODO(avd) Add DCHECK(!crypto_stream_blocked_) | 105 // TODO(avd) Add DCHECK(!crypto_stream_blocked_) |
71 crypto_stream_blocked_ = true; | 106 crypto_stream_blocked_ = true; |
72 return; | 107 return; |
73 } | 108 } |
74 | 109 |
75 if (stream_id == kHeadersStreamId) { | 110 if (stream_id == kHeadersStreamId) { |
76 DCHECK_EQ(kHighestPriority, priority); | 111 DCHECK_EQ(net::kHighestPriority, priority); |
77 // TODO(avd) Add DCHECK(!headers_stream_blocked_); | 112 // TODO(avd) Add DCHECK(!headers_stream_blocked_); |
78 headers_stream_blocked_ = true; | 113 headers_stream_blocked_ = true; |
79 return; | 114 return; |
80 } | 115 } |
81 | 116 if (FLAGS_quic_batch_writes && |
82 base_write_blocked_list_.PushBack( | 117 stream_id == batch_write_stream_id_[last_priority_popped_] && |
83 stream_id, static_cast<SpdyPriority>(priority)); | 118 bytes_left_for_batch_write_[last_priority_popped_] > 0) { |
| 119 // If the batch write stream has more data to write, push it to the front |
| 120 // for its priority level. |
| 121 base_write_blocked_list_.PushFront(stream_id, priority); |
| 122 } else { |
| 123 base_write_blocked_list_.PushBack(stream_id, priority); |
| 124 } |
84 return; | 125 return; |
85 } | 126 } |
86 | 127 |
87 bool crypto_stream_blocked() const { return crypto_stream_blocked_; } | 128 bool crypto_stream_blocked() const { return crypto_stream_blocked_; } |
88 bool headers_stream_blocked() const { return headers_stream_blocked_; } | 129 bool headers_stream_blocked() const { return headers_stream_blocked_; } |
89 | 130 |
90 private: | 131 private: |
91 QuicWriteBlockedListBase base_write_blocked_list_; | 132 QuicWriteBlockedListBase base_write_blocked_list_; |
| 133 |
| 134 // If performing batch writes, this will be the stream ID of the stream doing |
| 135 // batch writes for this priority level. We will allow this stream to write |
| 136 // until it has written kBatchWriteSize bytes, it has no more data to write, |
| 137 // or a higher priority stream preempts. |
| 138 QuicStreamId batch_write_stream_id_[kLowestPriority + 1]; |
| 139 // Set to kBatchWriteSize when we set a new batch_write_stream_id_ for a given |
| 140 // priority. This is decremented with each write the stream does until it is |
| 141 // done with its batch write. |
| 142 int32 bytes_left_for_batch_write_[kLowestPriority + 1]; |
| 143 // Tracks the last priority popped for UpdateBytesForStream. |
| 144 SpdyPriority last_priority_popped_; |
| 145 |
92 bool crypto_stream_blocked_; | 146 bool crypto_stream_blocked_; |
93 bool headers_stream_blocked_; | 147 bool headers_stream_blocked_; |
94 | 148 |
95 DISALLOW_COPY_AND_ASSIGN(QuicWriteBlockedList); | 149 DISALLOW_COPY_AND_ASSIGN(QuicWriteBlockedList); |
96 }; | 150 }; |
97 | 151 |
98 } // namespace net | 152 } // namespace net |
99 | 153 |
100 | 154 |
101 #endif // NET_QUIC_QUIC_WRITE_BLOCKED_LIST_H_ | 155 #endif // NET_QUIC_QUIC_WRITE_BLOCKED_LIST_H_ |
OLD | NEW |