| 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 #ifndef COMPONENTS_TRACING_CORE_TRACE_RING_BUFFER_H_ | 5 #ifndef COMPONENTS_TRACING_CORE_TRACE_RING_BUFFER_H_ |
| 6 #define COMPONENTS_TRACING_CORE_TRACE_RING_BUFFER_H_ | 6 #define COMPONENTS_TRACING_CORE_TRACE_RING_BUFFER_H_ |
| 7 | 7 |
| 8 #include <memory> |
| 9 |
| 8 #include "base/atomicops.h" | 10 #include "base/atomicops.h" |
| 9 #include "base/macros.h" | 11 #include "base/macros.h" |
| 10 #include "base/synchronization/lock.h" | 12 #include "base/synchronization/lock.h" |
| 11 #include "base/threading/thread.h" | |
| 12 #include "components/tracing/tracing_export.h" | 13 #include "components/tracing/tracing_export.h" |
| 13 | 14 |
| 14 namespace tracing { | 15 namespace tracing { |
| 15 namespace v2 { | 16 namespace v2 { |
| 16 | 17 |
| 18 static const uint32_t kNoChunkOwner = 0; |
| 19 |
| 17 class TRACING_EXPORT TraceRingBuffer { | 20 class TRACING_EXPORT TraceRingBuffer { |
| 18 public: | 21 public: |
| 19 class Chunk { | 22 class Chunk { |
| 20 public: | 23 public: |
| 21 using Header = base::subtle::Atomic32; | 24 using Header = base::subtle::Atomic32; |
| 22 static constexpr size_t kSize = 32 * 1024; | 25 static constexpr size_t kSize = 32 * 1024; |
| 23 | 26 |
| 24 Chunk(); | 27 Chunk(); |
| 25 ~Chunk(); | 28 ~Chunk(); |
| 26 | 29 |
| 27 void Initialize(uint8_t* begin); | 30 void Initialize(uint8_t* begin); |
| 28 void Clear(); | 31 void Clear(); |
| 29 | 32 |
| 30 uint8_t* begin() const { return begin_; } | 33 uint8_t* begin() const { return begin_; } |
| 31 Header* header() const { return reinterpret_cast<Header*>(begin_); } | 34 Header* header() const { return reinterpret_cast<Header*>(begin_); } |
| 32 uint8_t* payload() const { return begin_ + sizeof(Header); } | 35 uint8_t* payload() const { return begin_ + sizeof(Header); } |
| 33 uint8_t* end() const { return begin_ + kSize; } | 36 uint8_t* end() const { return begin_ + kSize; } |
| 34 | 37 |
| 35 void set_used_size(uint32_t size) { | 38 void set_used_size(uint32_t size) { |
| 36 base::subtle::NoBarrier_Store(header(), size); | 39 base::subtle::NoBarrier_Store(header(), size); |
| 37 } | 40 } |
| 38 uint32_t used_size() const { | 41 uint32_t used_size() const { |
| 39 return base::subtle::NoBarrier_Load(header()); | 42 return base::subtle::NoBarrier_Load(header()); |
| 40 } | 43 } |
| 41 | 44 |
| 45 void set_next_in_owner_list(Chunk* next) { next_in_owner_list_ = next; } |
| 46 Chunk* next_in_owner_list() const { return next_in_owner_list_; } |
| 47 |
| 48 // Owner is a flag matching the id of the TraceBufferWriter, 0 if not owned. |
| 42 // Accesses to |owner_| must happen under the buffer |lock_|. | 49 // Accesses to |owner_| must happen under the buffer |lock_|. |
| 43 bool is_owned() const { return owner_ != base::kInvalidThreadId; } | 50 bool is_owned() const { return owner_ != kNoChunkOwner; } |
| 44 void clear_owner() { owner_ = base::kInvalidThreadId; } | 51 uint32_t owner() const { return owner_; } |
| 45 void set_owner(base::PlatformThreadId tid) { owner_ = tid; } | 52 void clear_owner() { owner_ = kNoChunkOwner; } |
| 53 void set_owner(uint32_t owner) { |
| 54 DCHECK_NE(kNoChunkOwner, owner); |
| 55 owner_ = owner; |
| 56 } |
| 46 | 57 |
| 47 private: | 58 private: |
| 48 uint8_t* begin_; | 59 uint8_t* begin_; |
| 49 base::PlatformThreadId owner_; // kInvalidThreadId -> Chunk is not owned. | 60 uint32_t owner_; |
| 61 |
| 62 // When a chunk is owned, this is the next pointer to keep track of all |
| 63 // owned chunks in a singly linked list. |
| 64 Chunk* next_in_owner_list_; |
| 50 | 65 |
| 51 DISALLOW_COPY_AND_ASSIGN(Chunk); | 66 DISALLOW_COPY_AND_ASSIGN(Chunk); |
| 52 }; | 67 }; |
| 53 | 68 |
| 54 TraceRingBuffer(uint8_t* begin, size_t size); | 69 TraceRingBuffer(uint8_t* begin, size_t size); |
| 55 ~TraceRingBuffer(); | 70 ~TraceRingBuffer(); |
| 56 | 71 |
| 57 Chunk* TakeChunk(); | 72 Chunk* TakeChunk(uint32_t writer_id); |
| 58 void ReturnChunk(Chunk* chunk, uint32_t used_size); | 73 void ReturnChunk(Chunk* chunk); |
| 74 |
| 75 size_t num_chunks() const { return num_chunks_; } |
| 76 |
| 77 // Returns the number of chunks taken and not returned, without counting any |
| 78 // bankrupcy chunk obtained when the ring buffer was full. |
| 79 size_t GetNumChunksTaken() const; |
| 80 |
| 81 const Chunk* chunks_for_testing() const { return chunks_.get(); } |
| 82 bool IsBankrupcyChunkForTesting(const Chunk*) const; |
| 59 | 83 |
| 60 private: | 84 private: |
| 61 base::Lock lock_; | 85 mutable base::Lock lock_; |
| 62 std::unique_ptr<Chunk[]> chunks_; | 86 std::unique_ptr<Chunk[]> chunks_; |
| 63 const size_t num_chunks_; | 87 const size_t num_chunks_; |
| 88 size_t num_chunks_taken_; |
| 64 size_t current_chunk_idx_; | 89 size_t current_chunk_idx_; |
| 65 | 90 |
| 66 // An emergency chunk used in the rare case in which all chunks are in flight. | 91 // 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 | 92 // 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 | 93 // discarded. Its only purpose is to avoid a crash (due to TakeChunk returning |
| 69 // nullptr) in the case of a thread storm. | 94 // nullptr) in the case of a thread storm. |
| 70 Chunk bankrupcy_chunk_; | 95 Chunk bankrupcy_chunk_; |
| 71 std::unique_ptr<uint8_t[]> bankrupcy_chunk_storage_; | 96 std::unique_ptr<uint8_t[]> bankrupcy_chunk_storage_; |
| 72 | 97 |
| 73 DISALLOW_COPY_AND_ASSIGN(TraceRingBuffer); | 98 DISALLOW_COPY_AND_ASSIGN(TraceRingBuffer); |
| 74 }; | 99 }; |
| 75 | 100 |
| 76 } // namespace v2 | 101 } // namespace v2 |
| 77 } // namespace tracing | 102 } // namespace tracing |
| 78 | 103 |
| 79 #endif // COMPONENTS_TRACING_CORE_TRACE_RING_BUFFER_H_ | 104 #endif // COMPONENTS_TRACING_CORE_TRACE_RING_BUFFER_H_ |
| OLD | NEW |