Chromium Code Reviews| 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 |
| 17 class TRACING_EXPORT TraceRingBuffer { | 18 class TRACING_EXPORT TraceRingBuffer { |
| 18 public: | 19 public: |
| 19 class Chunk { | 20 class Chunk { |
| 20 public: | 21 public: |
| 21 using Header = base::subtle::Atomic32; | 22 using Header = base::subtle::Atomic32; |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 32 uint8_t* payload() const { return begin_ + sizeof(Header); } | 33 uint8_t* payload() const { return begin_ + sizeof(Header); } |
| 33 uint8_t* end() const { return begin_ + kSize; } | 34 uint8_t* end() const { return begin_ + kSize; } |
| 34 | 35 |
| 35 void set_used_size(uint32_t size) { | 36 void set_used_size(uint32_t size) { |
| 36 base::subtle::NoBarrier_Store(header(), size); | 37 base::subtle::NoBarrier_Store(header(), size); |
| 37 } | 38 } |
| 38 uint32_t used_size() const { | 39 uint32_t used_size() const { |
| 39 return base::subtle::NoBarrier_Load(header()); | 40 return base::subtle::NoBarrier_Load(header()); |
| 40 } | 41 } |
| 41 | 42 |
| 43 void set_next_in_owner_list(Chunk* next) { next_in_owner_list_ = next; } | |
| 44 Chunk* next_in_owner_list() const { return next_in_owner_list_; } | |
| 45 | |
| 46 // Owner is a flag matching the id of the TraceBufferWriter, 0 if not owned. | |
| 42 // Accesses to |owner_| must happen under the buffer |lock_|. | 47 // Accesses to |owner_| must happen under the buffer |lock_|. |
| 43 bool is_owned() const { return owner_ != base::kInvalidThreadId; } | 48 bool is_owned() const { return owner_ != 0; } |
| 44 void clear_owner() { owner_ = base::kInvalidThreadId; } | 49 uint32_t owner() const { return owner_; } |
| 45 void set_owner(base::PlatformThreadId tid) { owner_ = tid; } | 50 void clear_owner() { owner_ = 0; } |
| 51 void set_owner(uint32_t owner) { owner_ = owner; } | |
|
alph
2016/07/30 07:41:14
DCHECK(owner != 0)
Primiano Tucci (use gerrit)
2016/08/04 15:26:55
Done.
| |
| 46 | 52 |
| 47 private: | 53 private: |
| 48 uint8_t* begin_; | 54 uint8_t* begin_; |
| 49 base::PlatformThreadId owner_; // kInvalidThreadId -> Chunk is not owned. | 55 uint32_t owner_; // 0 -> Chunk is not owned. |
|
alph
2016/07/30 07:41:15
static const uint32_t kNoOwner = 0; ?
Primiano Tucci (use gerrit)
2016/08/04 15:26:55
Done.
| |
| 56 | |
| 57 // When a chunk is owned, this is the next pointer to keep track of all | |
| 58 // owned chunks in a singly linked list. | |
| 59 Chunk* next_in_owner_list_; | |
| 50 | 60 |
| 51 DISALLOW_COPY_AND_ASSIGN(Chunk); | 61 DISALLOW_COPY_AND_ASSIGN(Chunk); |
| 52 }; | 62 }; |
| 53 | 63 |
| 54 TraceRingBuffer(uint8_t* begin, size_t size); | 64 TraceRingBuffer(uint8_t* begin, size_t size); |
| 55 ~TraceRingBuffer(); | 65 ~TraceRingBuffer(); |
| 56 | 66 |
| 57 Chunk* TakeChunk(); | 67 Chunk* TakeChunk(uint32_t writer_id); |
| 58 void ReturnChunk(Chunk* chunk, uint32_t used_size); | 68 void ReturnChunk(Chunk* chunk); |
| 69 | |
| 70 size_t num_chunks() const { return num_chunks_; } | |
| 71 | |
| 72 // Returns the number of chunks taken and not returned, without counting any | |
| 73 // bankrupcy chunk obtained when the ring buffer was full. | |
| 74 size_t GetNumChunksTaken() const; | |
| 75 | |
| 76 const Chunk* chunks_for_testing() const { return chunks_.get(); } | |
| 77 bool IsBankrupcyChunkForTesting(const Chunk*) const; | |
| 59 | 78 |
| 60 private: | 79 private: |
| 61 base::Lock lock_; | 80 mutable base::Lock lock_; |
| 62 std::unique_ptr<Chunk[]> chunks_; | 81 std::unique_ptr<Chunk[]> chunks_; |
| 63 const size_t num_chunks_; | 82 const size_t num_chunks_; |
| 83 size_t num_chunks_taken_; | |
| 64 size_t current_chunk_idx_; | 84 size_t current_chunk_idx_; |
| 65 | 85 |
| 66 // An emergency chunk used in the rare case in which all chunks are in flight. | 86 // 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 | 87 // 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 | 88 // discarded. Its only purpose is to avoid a crash (due to TakeChunk returning |
| 69 // nullptr) in the case of a thread storm. | 89 // nullptr) in the case of a thread storm. |
| 70 Chunk bankrupcy_chunk_; | 90 Chunk bankrupcy_chunk_; |
| 71 std::unique_ptr<uint8_t[]> bankrupcy_chunk_storage_; | 91 std::unique_ptr<uint8_t[]> bankrupcy_chunk_storage_; |
| 72 | 92 |
| 73 DISALLOW_COPY_AND_ASSIGN(TraceRingBuffer); | 93 DISALLOW_COPY_AND_ASSIGN(TraceRingBuffer); |
| 74 }; | 94 }; |
| 75 | 95 |
| 76 } // namespace v2 | 96 } // namespace v2 |
| 77 } // namespace tracing | 97 } // namespace tracing |
| 78 | 98 |
| 79 #endif // COMPONENTS_TRACING_CORE_TRACE_RING_BUFFER_H_ | 99 #endif // COMPONENTS_TRACING_CORE_TRACE_RING_BUFFER_H_ |
| OLD | NEW |