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