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 #ifndef COMPONENTS_TRACING_CORE_TRACE_RING_BUFFER_H_ | |
| 6 #define COMPONENTS_TRACING_CORE_TRACE_RING_BUFFER_H_ | |
| 7 | |
| 8 #include "base/atomicops.h" | |
| 9 #include "base/macros.h" | |
| 10 #include "base/synchronization/lock.h" | |
| 11 #include "base/threading/thread.h" | |
| 12 #include "components/tracing/tracing_export.h" | |
| 13 | |
| 14 namespace tracing { | |
| 15 namespace v2 { | |
| 16 | |
| 17 class TRACING_EXPORT TraceRingBuffer { | |
| 18 public: | |
| 19 class Chunk { | |
| 20 public: | |
| 21 using Header = base::subtle::Atomic32; | |
| 22 static constexpr size_t kSize = 32768; | |
| 23 | |
| 24 Chunk(); | |
| 25 ~Chunk(); | |
| 26 | |
| 27 void Initialize(uint8_t* begin); | |
| 28 void Clear(); | |
| 29 | |
| 30 uint8_t* begin() const { return begin_; } | |
| 31 Header* header() const { return reinterpret_cast<Header*>(begin_); } | |
| 32 uint8_t* payload() const { return begin_ + sizeof(Header); } | |
| 33 uint8_t* end() const { return begin_ + kSize; } | |
| 34 | |
| 35 void set_used_size(uint32_t size) { | |
| 36 base::subtle::NoBarrier_Store(header(), size); | |
| 37 } | |
| 38 uint32_t used_size() const { | |
| 39 return base::subtle::NoBarrier_Load(header()); | |
| 40 } | |
| 41 | |
| 42 bool is_owned() const { return owner_ != base::kInvalidThreadId; } | |
|
kraynov
2016/06/28 13:15:16
Is there a guarantee that is_owned(), clear_owner(
Primiano Tucci (use gerrit)
2016/06/28 14:52:11
No, see below, the expectations is that they are g
| |
| 43 void clear_owner() { owner_ = base::kInvalidThreadId; } | |
| 44 void set_owner(base::PlatformThreadId tid) { owner_ = tid; } | |
| 45 | |
| 46 private: | |
| 47 uint8_t* begin_; | |
| 48 | |
| 49 // Accesses to owner_ must happen under the buffer |lock_|. | |
| 50 base::PlatformThreadId owner_; // kInvalidThreadId -> Chunk is returned. | |
| 51 | |
| 52 DISALLOW_COPY_AND_ASSIGN(Chunk); | |
| 53 }; | |
| 54 | |
| 55 TraceRingBuffer(uint8_t* begin, size_t size); | |
| 56 ~TraceRingBuffer(); | |
| 57 | |
| 58 Chunk* TakeChunk(); | |
| 59 void ReturnChunk(Chunk* chunk, uint32_t used_size); | |
| 60 | |
| 61 private: | |
| 62 base::Lock lock_; | |
| 63 std::unique_ptr<Chunk[]> chunks_; | |
| 64 const size_t num_chunks_; | |
| 65 size_t current_chunk_idx_; | |
| 66 | |
| 67 // An emergency chunk used in the rare case in which all chunks are in flight. | |
| 68 // This chunk is not part of the ring buffer and its contents are always | |
| 69 // discarded. Its only purpose is to avoid a crash (due to TakeChunk returning | |
| 70 // nullptr) in the case of a thread storm. | |
| 71 Chunk bankrupcy_chunk_; | |
| 72 std::unique_ptr<uint8_t[]> bankrupcy_chunk_storage_; | |
| 73 | |
| 74 DISALLOW_COPY_AND_ASSIGN(TraceRingBuffer); | |
| 75 }; | |
| 76 | |
| 77 } // namespace v2 | |
| 78 } // namespace tracing | |
| 79 | |
| 80 #endif // COMPONENTS_TRACING_CORE_TRACE_RING_BUFFER_H_ | |
| OLD | NEW |