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) { | |
| 14 DCHECK_GE(size, Chunk::kSize); | |
|
petrcermak
2016/06/28 10:22:29
maybe check DCHECK_GT(num_chunks_, 0) since that's
Primiano Tucci (use gerrit)
2016/06/28 11:30:14
makes sense, thanks. Done
| |
| 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_begin + Chunk::kSize; | |
|
petrcermak
2016/06/28 10:22:29
chunk_begin += Chunk::kSize
alternatively, use mu
Primiano Tucci (use gerrit)
2016/06/28 11:30:14
Oh definitely. The only reason why it is that way
| |
| 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 Chunk* chunk = nullptr; | |
|
petrcermak
2016/06/28 10:22:29
Why don't you declare this inside the loop on line
Primiano Tucci (use gerrit)
2016/06/28 11:30:14
Same as above, historical evolution of this code.
| |
| 31 for (size_t i = 0; i < num_chunks_; ++i) { | |
| 32 chunk = &chunks_[current_chunk_idx_]; | |
| 33 current_chunk_idx_ = (current_chunk_idx_ + 1) % num_chunks_; | |
| 34 if (!chunk->is_owned()) { | |
| 35 chunk->Clear(); | |
| 36 chunk->set_owner(base::PlatformThread::CurrentId()); | |
| 37 return chunk; | |
| 38 } | |
| 39 } | |
| 40 | |
| 41 // Bankrupcy: there are more threads than chunks. All chunks were on flight. | |
|
petrcermak
2016/06/28 10:22:29
nit: s/on/in/
Primiano Tucci (use gerrit)
2016/06/28 11:30:14
Done.
| |
| 42 if (!bankrupcy_chunk_storage_) { | |
| 43 bankrupcy_chunk_storage_.reset(new uint8_t[Chunk::kSize]); | |
| 44 uint8_t* const begin = &bankrupcy_chunk_storage_.get()[0]; | |
|
petrcermak
2016/06/28 10:22:29
Opinion: I don't think that naming this "begin" ma
Primiano Tucci (use gerrit)
2016/06/28 11:30:14
Oh, as above. Before there was a "begin" and "end"
| |
| 45 bankrupcy_chunk_.Initialize(begin); | |
| 46 } | |
| 47 bankrupcy_chunk_.Clear(); | |
| 48 return &bankrupcy_chunk_; | |
| 49 } | |
| 50 | |
| 51 void TraceRingBuffer::ReturnChunk(TraceRingBuffer::Chunk* chunk, | |
| 52 uint32_t used_size) { | |
| 53 base::AutoLock lock(lock_); | |
|
petrcermak
2016/06/28 10:22:29
Would it make sense to DCHECK that the chunk is ac
Primiano Tucci (use gerrit)
2016/06/28 11:30:14
Hmm that would make the debug (or DCHECK_ALWAYS_ON
| |
| 54 chunk->set_used_size(used_size); | |
| 55 chunk->clear_owner(); | |
| 56 } | |
| 57 | |
| 58 TraceRingBuffer::Chunk::Chunk() | |
| 59 : begin_(nullptr), owner_(base::kInvalidThreadId) {} | |
| 60 | |
| 61 TraceRingBuffer::Chunk::~Chunk() {} | |
| 62 | |
| 63 void TraceRingBuffer::Chunk::Clear() { | |
|
petrcermak
2016/06/28 10:22:29
Shouldn't this also clear owner?
Primiano Tucci (use gerrit)
2016/06/28 11:30:14
No this one is deliberate. I mean whether conceptu
| |
| 64 set_used_size(0); | |
| 65 } | |
| 66 | |
| 67 void TraceRingBuffer::Chunk::Initialize(uint8_t* begin) { | |
| 68 DCHECK_EQ(0ul, reinterpret_cast<uintptr_t>(begin) % sizeof(uintptr_t)); | |
| 69 begin_ = begin; | |
| 70 } | |
| 71 | |
| 72 } // namespace v2 | |
| 73 } // namespace tracing | |
| OLD | NEW |