| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/tracing/core/trace_ring_buffer.h" | 5 #include "components/tracing/core/trace_ring_buffer.h" |
| 6 | 6 |
| 7 #include "base/threading/platform_thread.h" | 7 #include "base/threading/platform_thread.h" |
| 8 | 8 |
| 9 namespace tracing { | 9 namespace tracing { |
| 10 namespace v2 { | 10 namespace v2 { |
| 11 | 11 |
| 12 TraceRingBuffer::TraceRingBuffer(uint8_t* begin, size_t size) | 12 TraceRingBuffer::TraceRingBuffer(uint8_t* begin, size_t size) |
| 13 : num_chunks_(size / Chunk::kSize), | 13 : num_chunks_(size / kChunkSize), |
| 14 num_chunks_taken_(0), | 14 num_chunks_taken_(0), |
| 15 current_chunk_idx_(0) { | 15 current_chunk_idx_(0) { |
| 16 DCHECK_GT(num_chunks_, 0u); | 16 DCHECK_GT(num_chunks_, 0u); |
| 17 DCHECK_EQ(0ul, reinterpret_cast<uintptr_t>(begin) % sizeof(uintptr_t)); | 17 DCHECK_EQ(0ul, reinterpret_cast<uintptr_t>(begin) % sizeof(uintptr_t)); |
| 18 chunks_.reset(new Chunk[num_chunks_]); | 18 chunks_.reset(new Chunk[num_chunks_]); |
| 19 uint8_t* chunk_begin = begin; | 19 uint8_t* chunk_begin = begin; |
| 20 for (size_t i = 0; i < num_chunks_; ++i) { | 20 for (size_t i = 0; i < num_chunks_; ++i) { |
| 21 chunks_[i].Initialize(chunk_begin); | 21 chunks_[i].Initialize(chunk_begin); |
| 22 chunk_begin += Chunk::kSize; | 22 chunk_begin += kChunkSize; |
| 23 } | 23 } |
| 24 } | 24 } |
| 25 | 25 |
| 26 TraceRingBuffer::~TraceRingBuffer() {} | 26 TraceRingBuffer::~TraceRingBuffer() {} |
| 27 | 27 |
| 28 TraceRingBuffer::Chunk* TraceRingBuffer::TakeChunk(uint32_t writer_id) { | 28 TraceRingBuffer::Chunk* TraceRingBuffer::TakeChunk(uint32_t writer_id) { |
| 29 base::AutoLock lock(lock_); | 29 base::AutoLock lock(lock_); |
| 30 DCHECK_GT(num_chunks_, 0ul); | 30 DCHECK_GT(num_chunks_, 0ul); |
| 31 DCHECK_LT(current_chunk_idx_, num_chunks_); | 31 DCHECK_LT(current_chunk_idx_, num_chunks_); |
| 32 for (size_t i = 0; i < num_chunks_; ++i) { | 32 for (size_t i = 0; i < num_chunks_; ++i) { |
| 33 Chunk* chunk = &chunks_[current_chunk_idx_]; | 33 Chunk* chunk = &chunks_[current_chunk_idx_]; |
| 34 current_chunk_idx_ = (current_chunk_idx_ + 1) % num_chunks_; | 34 current_chunk_idx_ = (current_chunk_idx_ + 1) % num_chunks_; |
| 35 if (!chunk->is_owned()) { | 35 if (!chunk->is_owned()) { |
| 36 chunk->Clear(); | 36 chunk->Clear(); |
| 37 DCHECK_NE(0u, writer_id); | 37 DCHECK_NE(0u, writer_id); |
| 38 chunk->set_owner(writer_id); | 38 chunk->set_owner(writer_id); |
| 39 num_chunks_taken_++; | 39 num_chunks_taken_++; |
| 40 return chunk; | 40 return chunk; |
| 41 } | 41 } |
| 42 } | 42 } |
| 43 | 43 |
| 44 // Bankrupcy: there are more threads than chunks. All chunks were in flight. | 44 // Bankrupcy: there are more threads than chunks. All chunks were in flight. |
| 45 if (!bankrupcy_chunk_storage_) { | 45 if (!bankrupcy_chunk_storage_) { |
| 46 bankrupcy_chunk_storage_.reset(new uint8_t[Chunk::kSize]); | 46 bankrupcy_chunk_storage_.reset(new uint8_t[kChunkSize]); |
| 47 bankrupcy_chunk_.Initialize(&bankrupcy_chunk_storage_.get()[0]); | 47 bankrupcy_chunk_.Initialize(&bankrupcy_chunk_storage_.get()[0]); |
| 48 } | 48 } |
| 49 bankrupcy_chunk_.Clear(); | 49 bankrupcy_chunk_.Clear(); |
| 50 return &bankrupcy_chunk_; | 50 return &bankrupcy_chunk_; |
| 51 } | 51 } |
| 52 | 52 |
| 53 void TraceRingBuffer::ReturnChunk(TraceRingBuffer::Chunk* chunk) { | 53 void TraceRingBuffer::ReturnChunk(TraceRingBuffer::Chunk* chunk) { |
| 54 // Returning a chunk without using it is quite odd, very likely a bug. | 54 // Returning a chunk without using it is quite odd, very likely a bug. |
| 55 DCHECK_GT(chunk->used_size(), 0u); | 55 DCHECK_GT(chunk->used_size(), 0u); |
| 56 | 56 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 86 set_next_in_owner_list(nullptr); | 86 set_next_in_owner_list(nullptr); |
| 87 } | 87 } |
| 88 | 88 |
| 89 void TraceRingBuffer::Chunk::Initialize(uint8_t* begin) { | 89 void TraceRingBuffer::Chunk::Initialize(uint8_t* begin) { |
| 90 DCHECK_EQ(0ul, reinterpret_cast<uintptr_t>(begin) % sizeof(uintptr_t)); | 90 DCHECK_EQ(0ul, reinterpret_cast<uintptr_t>(begin) % sizeof(uintptr_t)); |
| 91 begin_ = begin; | 91 begin_ = begin; |
| 92 } | 92 } |
| 93 | 93 |
| 94 } // namespace v2 | 94 } // namespace v2 |
| 95 } // namespace tracing | 95 } // namespace tracing |
| OLD | NEW |