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 // These values must be kept consistent with the numbers of bits of
44 // chunk_index and event_index fields in TraceEventHandle
45 // (in trace_event_impl.h).
46 static const size_t kMaxChunkIndex
= (1u << 26) - 1;
47 static const size_t kTraceBufferChunkSize
= 64;
51 scoped_ptr
<TraceEventMemoryOverhead
> cached_overhead_estimate_
;
52 TraceEvent chunk_
[kTraceBufferChunkSize
];
56 // TraceBuffer holds the events as they are collected.
57 class BASE_EXPORT TraceBuffer
{
59 virtual ~TraceBuffer() {}
61 virtual scoped_ptr
<TraceBufferChunk
> GetChunk(size_t* index
) = 0;
62 virtual void ReturnChunk(size_t index
,
63 scoped_ptr
<TraceBufferChunk
> chunk
) = 0;
65 virtual bool IsFull() const = 0;
66 virtual size_t Size() const = 0;
67 virtual size_t Capacity() const = 0;
68 virtual TraceEvent
* GetEventByHandle(TraceEventHandle handle
) = 0;
70 // For iteration. Each TraceBuffer can only be iterated once.
71 virtual const TraceBufferChunk
* NextChunk() = 0;
73 virtual scoped_ptr
<TraceBuffer
> CloneForIteration() const = 0;
75 // Computes an estimate of the size of the buffer, including all the retained
77 virtual void EstimateTraceMemoryOverhead(
78 TraceEventMemoryOverhead
* overhead
) = 0;
80 static TraceBuffer
* CreateTraceBufferRingBuffer(size_t max_chunks
);
81 static TraceBuffer
* CreateTraceBufferVectorOfSize(size_t max_chunks
);
84 // TraceResultBuffer collects and converts trace fragments returned by TraceLog
86 class BASE_EXPORT TraceResultBuffer
{
88 typedef base::Callback
<void(const std::string
&)> OutputCallback
;
90 // If you don't need to stream JSON chunks out efficiently, and just want to
91 // get a complete JSON string after calling Finish, use this struct to collect
93 struct BASE_EXPORT SimpleOutput
{
94 OutputCallback
GetCallback();
95 void Append(const std::string
& json_string
);
97 // Do what you want with the json_output_ string after calling
98 // TraceResultBuffer::Finish.
99 std::string json_output
;
103 ~TraceResultBuffer();
105 // Set callback. The callback will be called during Start with the initial
106 // JSON output and during AddFragment and Finish with following JSON output
107 // chunks. The callback target must live past the last calls to
108 // TraceResultBuffer::Start/AddFragment/Finish.
109 void SetOutputCallback(const OutputCallback
& json_chunk_callback
);
111 // Start JSON output. This resets all internal state, so you can reuse
112 // the TraceResultBuffer by calling Start.
115 // Call AddFragment 0 or more times to add trace fragments from TraceLog.
116 void AddFragment(const std::string
& trace_fragment
);
118 // When all fragments have been added, call Finish to complete the JSON
123 OutputCallback output_callback_
;
127 } // namespace trace_event
130 #endif // BASE_TRACE_EVENT_TRACE_BUFFER_H_