1 // Copyright 2015 The Chromium Authors 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 BASE_TRACE_EVENT_TRACE_BUFFER_H_ 6 #define BASE_TRACE_EVENT_TRACE_BUFFER_H_ 7 8 #include <stddef.h> 9 #include <stdint.h> 10 11 #include "base/base_export.h" 12 #include "base/check.h" 13 #include "base/trace_event/trace_event.h" 14 #include "base/trace_event/trace_event_impl.h" 15 16 namespace base { 17 18 namespace trace_event { 19 20 // TraceBufferChunk is the basic unit of TraceBuffer. 21 class BASE_EXPORT TraceBufferChunk { 22 public: 23 explicit TraceBufferChunk(uint32_t seq); 24 ~TraceBufferChunk(); 25 26 void Reset(uint32_t new_seq); 27 TraceEvent* AddTraceEvent(size_t* event_index); IsFull()28 bool IsFull() const { return next_free_ == kTraceBufferChunkSize; } 29 seq()30 uint32_t seq() const { return seq_; } capacity()31 size_t capacity() const { return kTraceBufferChunkSize; } size()32 size_t size() const { return next_free_; } 33 GetEventAt(size_t index)34 TraceEvent* GetEventAt(size_t index) { 35 DCHECK(index < size()); 36 return &chunk_[index]; 37 } GetEventAt(size_t index)38 const TraceEvent* GetEventAt(size_t index) const { 39 DCHECK(index < size()); 40 return &chunk_[index]; 41 } 42 43 void EstimateTraceMemoryOverhead(TraceEventMemoryOverhead* overhead); 44 45 // These values must be kept consistent with the numbers of bits of 46 // chunk_index and event_index fields in TraceEventHandle 47 // (in trace_event_impl.h). 48 static const size_t kMaxChunkIndex = (1u << 26) - 1; 49 static const size_t kTraceBufferChunkSize = 64; 50 51 private: 52 size_t next_free_; 53 std::unique_ptr<TraceEventMemoryOverhead> cached_overhead_estimate_; 54 TraceEvent chunk_[kTraceBufferChunkSize]; 55 uint32_t seq_; 56 }; 57 58 // TraceBuffer holds the events as they are collected. 59 class BASE_EXPORT TraceBuffer { 60 public: 61 virtual ~TraceBuffer() = default; 62 63 virtual std::unique_ptr<TraceBufferChunk> GetChunk(size_t* index) = 0; 64 virtual void ReturnChunk(size_t index, 65 std::unique_ptr<TraceBufferChunk> chunk) = 0; 66 67 virtual bool IsFull() const = 0; 68 virtual size_t Size() const = 0; 69 virtual size_t Capacity() const = 0; 70 virtual TraceEvent* GetEventByHandle(TraceEventHandle handle) = 0; 71 72 // For iteration. Each TraceBuffer can only be iterated once. 73 virtual const TraceBufferChunk* NextChunk() = 0; 74 75 76 // Computes an estimate of the size of the buffer, including all the retained 77 // objects. 78 virtual void EstimateTraceMemoryOverhead( 79 TraceEventMemoryOverhead* overhead) = 0; 80 81 static TraceBuffer* CreateTraceBufferRingBuffer(size_t max_chunks); 82 static TraceBuffer* CreateTraceBufferVectorOfSize(size_t max_chunks); 83 }; 84 85 // TraceResultBuffer collects and converts trace fragments returned by TraceLog 86 // to JSON output. 87 class BASE_EXPORT TraceResultBuffer { 88 public: 89 using OutputCallback = base::RepeatingCallback<void(const std::string&)>; 90 91 // If you don't need to stream JSON chunks out efficiently, and just want to 92 // get a complete JSON string after calling Finish, use this struct to collect 93 // JSON trace output. 94 struct BASE_EXPORT SimpleOutput { 95 OutputCallback GetCallback(); 96 void Append(const std::string& json_string); 97 98 // Do what you want with the json_output_ string after calling 99 // TraceResultBuffer::Finish. 100 std::string json_output; 101 }; 102 103 TraceResultBuffer(); 104 ~TraceResultBuffer(); 105 106 // Set callback. The callback will be called during Start with the initial 107 // JSON output and during AddFragment and Finish with following JSON output 108 // chunks. The callback target must live past the last calls to 109 // TraceResultBuffer::Start/AddFragment/Finish. 110 void SetOutputCallback(OutputCallback json_chunk_callback); 111 112 // Start JSON output. This resets all internal state, so you can reuse 113 // the TraceResultBuffer by calling Start. 114 void Start(); 115 116 // Call AddFragment 0 or more times to add trace fragments from TraceLog. 117 void AddFragment(const std::string& trace_fragment); 118 119 // When all fragments have been added, call Finish to complete the JSON 120 // formatted output. 121 void Finish(); 122 123 private: 124 OutputCallback output_callback_; 125 bool append_comma_; 126 }; 127 128 } // namespace trace_event 129 } // namespace base 130 131 #endif // BASE_TRACE_EVENT_TRACE_BUFFER_H_ 132