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 const size_t kSize; | |
23 | |
24 Chunk(); | |
25 ~Chunk(); | |
26 | |
27 void Initialize(uint8_t* begin, uint8_t* end); | |
oystein (OOO til 10th of July)
2016/06/15 07:52:52
Maybe 'end' can just be omitted? Looks like end =
Primiano Tucci (use gerrit)
2016/06/28 09:53:24
Done.
| |
28 void Clear(); | |
29 | |
30 uint8_t* begin() const { return begin_; } | |
31 Header* header() const { return reinterpret_cast<Header*>(begin_); } | |
32 uint8_t* proto_begin() const { return begin_ + sizeof(Header); } | |
33 uint8_t* end() const { return end_; } | |
34 | |
35 void set_proto_used_size(uint32_t size) { | |
36 base::subtle::NoBarrier_Store(header(), size); | |
37 } | |
38 uint32_t proto_used_size() const { | |
oystein (OOO til 10th of July)
2016/06/15 07:52:52
Should this layer be proto aware? Maybe it could b
Primiano Tucci (use gerrit)
2016/06/28 09:53:24
Yup you are right, this is not proto aware. Rename
| |
39 return base::subtle::NoBarrier_Load(header()); | |
40 } | |
41 | |
42 bool is_returned() const { return owner_ == base::kInvalidThreadId; } | |
oystein (OOO til 10th of July)
2016/06/15 07:52:52
is_returned() / and set_returned() was a little co
Primiano Tucci (use gerrit)
2016/06/28 09:53:24
Done.
| |
43 void set_returned() { owner_ = base::kInvalidThreadId; } | |
44 void set_owner(base::PlatformThreadId tid) { owner_ = tid; } | |
45 | |
46 private: | |
47 uint8_t* begin_; | |
48 uint8_t* end_; | |
49 | |
50 // Accesses to owner_ must happen under the buffer |lock_|. | |
51 base::PlatformThreadId owner_; // kInvalidThreadId -> Chunk is returned. | |
52 | |
53 DISALLOW_COPY_AND_ASSIGN(Chunk); | |
54 }; | |
55 | |
56 TraceRingBuffer(uint8_t* begin, size_t size); | |
57 ~TraceRingBuffer(); | |
58 | |
59 Chunk* TakeChunk(); | |
60 void ReturnChunk(Chunk* chunk, uint32_t used_size); | |
61 | |
62 private: | |
63 base::Lock lock_; | |
64 std::unique_ptr<Chunk[]> chunks_; | |
65 const size_t num_chunks_; | |
66 size_t current_chunk_idx_; | |
67 | |
68 // An emergency chunk used in the rare case in which all chunks are in flight. | |
69 // This chunk is not part of the ring buffer and its contents are always | |
70 // discarded. Its only purpose is to avoid a crash (due to TakeChunk returning | |
71 // nullptr) in the case of a thread storm. | |
72 Chunk bankrupcy_chunk_; | |
73 std::unique_ptr<uint8_t[]> bankrupcy_chunk_storage_; | |
74 | |
75 DISALLOW_COPY_AND_ASSIGN(TraceRingBuffer); | |
76 }; | |
77 | |
78 } // namespace v2 | |
79 } // namespace tracing | |
80 | |
81 #endif // COMPONENTS_TRACING_CORE_TRACE_RING_BUFFER_H_ | |
OLD | NEW |