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