| OLD | NEW |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 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 #include "net/quic/core/quic_stream_sequencer_buffer.h" | 5 #include "net/quic/core/quic_stream_sequencer_buffer.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "net/quic/core/quic_bug_tracker.h" | 9 #include "net/quic/core/quic_bug_tracker.h" |
| 10 #include "net/quic/core/quic_flags.h" | 10 #include "net/quic/core/quic_flags.h" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 | 40 |
| 41 QuicStreamSequencerBuffer::FrameInfo::FrameInfo(size_t length, | 41 QuicStreamSequencerBuffer::FrameInfo::FrameInfo(size_t length, |
| 42 QuicTime timestamp) | 42 QuicTime timestamp) |
| 43 : length(length), timestamp(timestamp) {} | 43 : length(length), timestamp(timestamp) {} |
| 44 | 44 |
| 45 QuicStreamSequencerBuffer::QuicStreamSequencerBuffer(size_t max_capacity_bytes) | 45 QuicStreamSequencerBuffer::QuicStreamSequencerBuffer(size_t max_capacity_bytes) |
| 46 : max_buffer_capacity_bytes_(max_capacity_bytes), | 46 : max_buffer_capacity_bytes_(max_capacity_bytes), |
| 47 blocks_count_( | 47 blocks_count_( |
| 48 ceil(static_cast<double>(max_capacity_bytes) / kBlockSizeBytes)), | 48 ceil(static_cast<double>(max_capacity_bytes) / kBlockSizeBytes)), |
| 49 total_bytes_read_(0), | 49 total_bytes_read_(0), |
| 50 blocks_(blocks_count_) { | 50 reduce_sequencer_buffer_memory_life_time_( |
| 51 FLAGS_quic_reduce_sequencer_buffer_memory_life_time), // NOLINT |
| 52 blocks_(reduce_sequencer_buffer_memory_life_time_ |
| 53 ? nullptr |
| 54 : new BufferBlock*[blocks_count_]()) { |
| 51 Clear(); | 55 Clear(); |
| 52 } | 56 } |
| 53 | 57 |
| 54 QuicStreamSequencerBuffer::~QuicStreamSequencerBuffer() { | 58 QuicStreamSequencerBuffer::~QuicStreamSequencerBuffer() { |
| 55 Clear(); | 59 Clear(); |
| 56 } | 60 } |
| 57 | 61 |
| 58 void QuicStreamSequencerBuffer::Clear() { | 62 void QuicStreamSequencerBuffer::Clear() { |
| 59 for (size_t i = 0; i < blocks_count_; ++i) { | 63 if (!reduce_sequencer_buffer_memory_life_time_ || blocks_ != nullptr) { |
| 60 if (blocks_[i] != nullptr) { | 64 for (size_t i = 0; i < blocks_count_; ++i) { |
| 61 RetireBlock(i); | 65 if (blocks_[i] != nullptr) { |
| 66 RetireBlock(i); |
| 67 } |
| 62 } | 68 } |
| 63 } | 69 } |
| 64 num_bytes_buffered_ = 0; | 70 num_bytes_buffered_ = 0; |
| 65 // Reset gaps_ so that buffer is in a state as if all data before | 71 // Reset gaps_ so that buffer is in a state as if all data before |
| 66 // total_bytes_read_ has been consumed, and those after total_bytes_read_ | 72 // total_bytes_read_ has been consumed, and those after total_bytes_read_ |
| 67 // has never arrived. | 73 // has never arrived. |
| 68 gaps_ = std::list<Gap>( | 74 gaps_ = std::list<Gap>( |
| 69 1, Gap(total_bytes_read_, std::numeric_limits<QuicStreamOffset>::max())), | 75 1, Gap(total_bytes_read_, std::numeric_limits<QuicStreamOffset>::max())), |
| 70 frame_arrival_time_map_.clear(); | 76 frame_arrival_time_map_.clear(); |
| 71 } | 77 } |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 | 168 |
| 163 size_t block_capacity = GetBlockCapacity(write_block_num); | 169 size_t block_capacity = GetBlockCapacity(write_block_num); |
| 164 size_t bytes_avail = block_capacity - write_block_offset; | 170 size_t bytes_avail = block_capacity - write_block_offset; |
| 165 | 171 |
| 166 // If this write meets the upper boundary of the buffer, | 172 // If this write meets the upper boundary of the buffer, |
| 167 // reduce the available free bytes. | 173 // reduce the available free bytes. |
| 168 if (offset + bytes_avail > total_bytes_read_ + max_buffer_capacity_bytes_) { | 174 if (offset + bytes_avail > total_bytes_read_ + max_buffer_capacity_bytes_) { |
| 169 bytes_avail = total_bytes_read_ + max_buffer_capacity_bytes_ - offset; | 175 bytes_avail = total_bytes_read_ + max_buffer_capacity_bytes_ - offset; |
| 170 } | 176 } |
| 171 | 177 |
| 178 if (reduce_sequencer_buffer_memory_life_time_ && blocks_ == nullptr) { |
| 179 blocks_.reset(new BufferBlock*[blocks_count_]()); |
| 180 for (size_t i = 0; i < blocks_count_; ++i) { |
| 181 blocks_[i] = nullptr; |
| 182 } |
| 183 } |
| 184 |
| 172 if (blocks_[write_block_num] == nullptr) { | 185 if (blocks_[write_block_num] == nullptr) { |
| 173 // TODO(danzh): Investigate if using a freelist would improve performance. | 186 // TODO(danzh): Investigate if using a freelist would improve performance. |
| 174 // Same as RetireBlock(). | 187 // Same as RetireBlock(). |
| 175 blocks_[write_block_num] = new BufferBlock(); | 188 blocks_[write_block_num] = new BufferBlock(); |
| 176 } | 189 } |
| 177 | 190 |
| 178 const size_t bytes_to_copy = min<size_t>(bytes_avail, source_remaining); | 191 const size_t bytes_to_copy = min<size_t>(bytes_avail, source_remaining); |
| 179 char* dest = blocks_[write_block_num]->buffer + write_block_offset; | 192 char* dest = blocks_[write_block_num]->buffer + write_block_offset; |
| 180 DVLOG(1) << "Write at offset: " << offset << " length: " << bytes_to_copy; | 193 DVLOG(1) << "Write at offset: " << offset << " length: " << bytes_to_copy; |
| 181 memcpy(dest, source, bytes_to_copy); | 194 memcpy(dest, source, bytes_to_copy); |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 386 return true; | 399 return true; |
| 387 } | 400 } |
| 388 | 401 |
| 389 size_t QuicStreamSequencerBuffer::FlushBufferedFrames() { | 402 size_t QuicStreamSequencerBuffer::FlushBufferedFrames() { |
| 390 size_t prev_total_bytes_read = total_bytes_read_; | 403 size_t prev_total_bytes_read = total_bytes_read_; |
| 391 total_bytes_read_ = gaps_.back().begin_offset; | 404 total_bytes_read_ = gaps_.back().begin_offset; |
| 392 Clear(); | 405 Clear(); |
| 393 return total_bytes_read_ - prev_total_bytes_read; | 406 return total_bytes_read_ - prev_total_bytes_read; |
| 394 } | 407 } |
| 395 | 408 |
| 409 void QuicStreamSequencerBuffer::ReleaseWholeBuffer() { |
| 410 if (!reduce_sequencer_buffer_memory_life_time_) { |
| 411 // Don't release buffer if flag is off. |
| 412 return; |
| 413 } |
| 414 Clear(); |
| 415 blocks_.reset(nullptr); |
| 416 } |
| 417 |
| 396 size_t QuicStreamSequencerBuffer::ReadableBytes() const { | 418 size_t QuicStreamSequencerBuffer::ReadableBytes() const { |
| 397 return gaps_.front().begin_offset - total_bytes_read_; | 419 return gaps_.front().begin_offset - total_bytes_read_; |
| 398 } | 420 } |
| 399 | 421 |
| 400 bool QuicStreamSequencerBuffer::HasBytesToRead() const { | 422 bool QuicStreamSequencerBuffer::HasBytesToRead() const { |
| 401 return ReadableBytes() > 0; | 423 return ReadableBytes() > 0; |
| 402 } | 424 } |
| 403 | 425 |
| 404 QuicStreamOffset QuicStreamSequencerBuffer::BytesConsumed() const { | 426 QuicStreamOffset QuicStreamSequencerBuffer::BytesConsumed() const { |
| 405 return total_bytes_read_; | 427 return total_bytes_read_; |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 517 QuicStreamOffset current_frame_begin_offset = it.first; | 539 QuicStreamOffset current_frame_begin_offset = it.first; |
| 518 QuicStreamOffset current_frame_end_offset = | 540 QuicStreamOffset current_frame_end_offset = |
| 519 it.second.length + current_frame_begin_offset; | 541 it.second.length + current_frame_begin_offset; |
| 520 current_frames_string += | 542 current_frames_string += |
| 521 RangeDebugString(current_frame_begin_offset, current_frame_end_offset); | 543 RangeDebugString(current_frame_begin_offset, current_frame_end_offset); |
| 522 } | 544 } |
| 523 return current_frames_string; | 545 return current_frames_string; |
| 524 } | 546 } |
| 525 | 547 |
| 526 } // namespace net | 548 } // namespace net |
| OLD | NEW |