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.
5 #ifndef BASE_TRACE_EVENT_TRACE_BUFFER_H_
6 #define BASE_TRACE_EVENT_TRACE_BUFFER_H_
8 #include "base/base_export.h"
9 #include "base/trace_event/trace_event.h"
10 #include "base/trace_event/trace_event_impl.h"
14 namespace trace_event
{
16 // TraceBufferChunk is the basic unit of TraceBuffer.
17 class BASE_EXPORT TraceBufferChunk
{
19 explicit TraceBufferChunk(uint32 seq
);
22 void Reset(uint32 new_seq
);
23 TraceEvent
* AddTraceEvent(size_t* event_index
);
24 bool IsFull() const { return next_free_
== kTraceBufferChunkSize
; }
26 uint32
seq() const { return seq_
; }
27 size_t capacity() const { return kTraceBufferChunkSize
; }
28 size_t size() const { return next_free_
; }
30 TraceEvent
* GetEventAt(size_t index
) {
31 DCHECK(index
< size());
32 return &chunk_
[index
];
34 const TraceEvent
* GetEventAt(size_t index
) const {
35 DCHECK(index
< size());
36 return &chunk_
[index
];
39 scoped_ptr
<TraceBufferChunk
> Clone() const;
41 void EstimateTraceMemoryOverhead(TraceEventMemoryOverhead
* overhead
);
43 static const size_t kTraceBufferChunkSize
= 64;
47 scoped_ptr
<TraceEventMemoryOverhead
> cached_overhead_estimate_
;
48 TraceEvent chunk_
[kTraceBufferChunkSize
];
52 // TraceBuffer holds the events as they are collected.
53 class BASE_EXPORT TraceBuffer
{
55 virtual ~TraceBuffer() {}
57 virtual scoped_ptr
<TraceBufferChunk
> GetChunk(size_t* index
) = 0;
58 virtual void ReturnChunk(size_t index
,
59 scoped_ptr
<TraceBufferChunk
> chunk
) = 0;
61 virtual bool IsFull() const = 0;
62 virtual size_t Size() const = 0;
63 virtual size_t Capacity() const = 0;
64 virtual TraceEvent
* GetEventByHandle(TraceEventHandle handle
) = 0;
66 // For iteration. Each TraceBuffer can only be iterated once.
67 virtual const TraceBufferChunk
* NextChunk() = 0;
69 virtual scoped_ptr
<TraceBuffer
> CloneForIteration() const = 0;
71 // Computes an estimate of the size of the buffer, including all the retained
73 virtual void EstimateTraceMemoryOverhead(
74 TraceEventMemoryOverhead
* overhead
) = 0;
76 static TraceBuffer
* CreateTraceBufferRingBuffer(size_t max_chunks
);
77 static TraceBuffer
* CreateTraceBufferVectorOfSize(size_t max_chunks
);
80 // TraceResultBuffer collects and converts trace fragments returned by TraceLog
82 class BASE_EXPORT TraceResultBuffer
{
84 typedef base::Callback
<void(const std::string
&)> OutputCallback
;
86 // If you don't need to stream JSON chunks out efficiently, and just want to
87 // get a complete JSON string after calling Finish, use this struct to collect
89 struct BASE_EXPORT SimpleOutput
{
90 OutputCallback
GetCallback();
91 void Append(const std::string
& json_string
);
93 // Do what you want with the json_output_ string after calling
94 // TraceResultBuffer::Finish.
95 std::string json_output
;
101 // Set callback. The callback will be called during Start with the initial
102 // JSON output and during AddFragment and Finish with following JSON output
103 // chunks. The callback target must live past the last calls to
104 // TraceResultBuffer::Start/AddFragment/Finish.
105 void SetOutputCallback(const OutputCallback
& json_chunk_callback
);
107 // Start JSON output. This resets all internal state, so you can reuse
108 // the TraceResultBuffer by calling Start.
111 // Call AddFragment 0 or more times to add trace fragments from TraceLog.
112 void AddFragment(const std::string
& trace_fragment
);
114 // When all fragments have been added, call Finish to complete the JSON
119 OutputCallback output_callback_
;
123 } // namespace trace_event
126 #endif // BASE_TRACE_EVENT_TRACE_BUFFER_H_