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 = 32 * 1024; |
| 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 // Accesses to |owner_| must happen under the buffer |lock_|. |
| 43 bool is_owned() const { return owner_ != base::kInvalidThreadId; } |
| 44 void clear_owner() { owner_ = base::kInvalidThreadId; } |
| 45 void set_owner(base::PlatformThreadId tid) { owner_ = tid; } |
| 46 |
| 47 private: |
| 48 uint8_t* begin_; |
| 49 base::PlatformThreadId owner_; // kInvalidThreadId -> Chunk is not owned. |
| 50 |
| 51 DISALLOW_COPY_AND_ASSIGN(Chunk); |
| 52 }; |
| 53 |
| 54 TraceRingBuffer(uint8_t* begin, size_t size); |
| 55 ~TraceRingBuffer(); |
| 56 |
| 57 Chunk* TakeChunk(); |
| 58 void ReturnChunk(Chunk* chunk, uint32_t used_size); |
| 59 |
| 60 private: |
| 61 base::Lock lock_; |
| 62 std::unique_ptr<Chunk[]> chunks_; |
| 63 const size_t num_chunks_; |
| 64 size_t current_chunk_idx_; |
| 65 |
| 66 // An emergency chunk used in the rare case in which all chunks are in flight. |
| 67 // This chunk is not part of the ring buffer and its contents are always |
| 68 // discarded. Its only purpose is to avoid a crash (due to TakeChunk returning |
| 69 // nullptr) in the case of a thread storm. |
| 70 Chunk bankrupcy_chunk_; |
| 71 std::unique_ptr<uint8_t[]> bankrupcy_chunk_storage_; |
| 72 |
| 73 DISALLOW_COPY_AND_ASSIGN(TraceRingBuffer); |
| 74 }; |
| 75 |
| 76 } // namespace v2 |
| 77 } // namespace tracing |
| 78 |
| 79 #endif // COMPONENTS_TRACING_CORE_TRACE_RING_BUFFER_H_ |
OLD | NEW |