Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/tracing/core/trace_ring_buffer.h" | |
| 6 | |
| 7 #include "base/threading/platform_thread.h" | |
| 8 | |
| 9 namespace tracing { | |
| 10 namespace v2 { | |
| 11 | |
| 12 TraceRingBuffer::TraceRingBuffer(uint8_t* begin, size_t size) | |
| 13 : num_chunks_(size / Chunk::kSize), current_chunk_idx_(0) { | |
|
kraynov
2016/06/28 13:15:16
What if (size % Chunk::kSize) != 0 ?
Primiano Tucci (use gerrit)
2016/06/28 14:52:11
we'll waste the reminder and use less than the pas
| |
| 14 DCHECK_GT(num_chunks_, 0u); | |
| 15 DCHECK_EQ(0ul, reinterpret_cast<uintptr_t>(begin) % sizeof(uintptr_t)); | |
| 16 chunks_.reset(new Chunk[num_chunks_]); | |
| 17 uint8_t* chunk_begin = begin; | |
| 18 for (size_t i = 0; i < num_chunks_; ++i) { | |
| 19 chunks_[i].Initialize(chunk_begin); | |
| 20 chunk_begin += Chunk::kSize; | |
| 21 } | |
| 22 } | |
| 23 | |
| 24 TraceRingBuffer::~TraceRingBuffer() {} | |
| 25 | |
| 26 TraceRingBuffer::Chunk* TraceRingBuffer::TakeChunk() { | |
| 27 base::AutoLock lock(lock_); | |
| 28 DCHECK_GT(num_chunks_, 0ul); | |
| 29 DCHECK_LT(current_chunk_idx_, num_chunks_); | |
| 30 for (size_t i = 0; i < num_chunks_; ++i) { | |
| 31 Chunk* chunk = &chunks_[current_chunk_idx_]; | |
| 32 current_chunk_idx_ = (current_chunk_idx_ + 1) % num_chunks_; | |
| 33 if (!chunk->is_owned()) { | |
| 34 chunk->Clear(); | |
| 35 chunk->set_owner(base::PlatformThread::CurrentId()); | |
| 36 return chunk; | |
| 37 } | |
| 38 } | |
| 39 | |
| 40 // Bankrupcy: there are more threads than chunks. All chunks were in flight. | |
| 41 if (!bankrupcy_chunk_storage_) { | |
| 42 bankrupcy_chunk_storage_.reset(new uint8_t[Chunk::kSize]); | |
| 43 bankrupcy_chunk_.Initialize(&bankrupcy_chunk_storage_.get()[0]); | |
| 44 } | |
| 45 bankrupcy_chunk_.Clear(); | |
| 46 return &bankrupcy_chunk_; | |
| 47 } | |
| 48 | |
| 49 void TraceRingBuffer::ReturnChunk(TraceRingBuffer::Chunk* chunk, | |
| 50 uint32_t used_size) { | |
| 51 base::AutoLock lock(lock_); | |
| 52 chunk->set_used_size(used_size); | |
| 53 chunk->clear_owner(); | |
| 54 } | |
| 55 | |
| 56 TraceRingBuffer::Chunk::Chunk() | |
| 57 : begin_(nullptr), owner_(base::kInvalidThreadId) {} | |
| 58 | |
| 59 TraceRingBuffer::Chunk::~Chunk() {} | |
| 60 | |
| 61 void TraceRingBuffer::Chunk::Clear() { | |
| 62 set_used_size(0); | |
| 63 } | |
| 64 | |
| 65 void TraceRingBuffer::Chunk::Initialize(uint8_t* begin) { | |
| 66 DCHECK_EQ(0ul, reinterpret_cast<uintptr_t>(begin) % sizeof(uintptr_t)); | |
| 67 begin_ = begin; | |
| 68 } | |
| 69 | |
| 70 } // namespace v2 | |
| 71 } // namespace tracing | |
| OLD | NEW |