Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
tracerecorder.h
Go to the documentation of this file.
1 
18 #ifndef ION_PROFILE_TRACERECORDER_H_
19 #define ION_PROFILE_TRACERECORDER_H_
20 
21 #include <memory>
22 #include <string>
23 #include <unordered_map>
24 
25 #include "base/integral_types.h"
26 #include "ion/base/allocatable.h"
29 #include "ion/port/threadutils.h"
32 #include "third_party/jsoncpp/include/json/json.h"
33 
34 namespace ion {
35 namespace profile {
36 
37 class CallTraceManager;
38 
43  public:
44  struct TraceHeader {
45  TraceHeader(uint32 id, uint32 time_micros)
46  : id(id), time_micros(time_micros) {}
47  uint32 id; // Event id
48  uint32 time_micros; // in microseconds since the timebase
49  };
50 
52  typedef std::unordered_map<uint32, std::string> IndexToStringMap;
53 
54  explicit TraceRecorder(CallTraceManager* manager);
55 
57  TraceRecorder(CallTraceManager* manager, size_t buffer_size);
58 
60  static size_t GetDefaultBufferSize() { return s_default_buffer_size_; }
61  static void SetDefaultBufferSize(size_t s) { s_default_buffer_size_ = s; }
62 
65  static bool GetReserveBuffer() { return s_reserve_buffer_; }
66  static void SetReserveBuffer(bool reserve) { s_reserve_buffer_ = reserve; }
67 
69  void EnterScope(int event_id);
70 
78  void AnnotateCurrentScope(const std::string& name, const std::string& value);
79 
82  void LeaveScope();
83 
85  void EnterScopeAtTime(uint32 timestamp, int event_id);
86 
88  void AnnotateCurrentScopeAtTime(uint32 timestamp,
89  const std::string& name,
90  const std::string& value);
91 
93  void LeaveScopeAtTime(uint32 timestamp);
94 
96  void EnterFrame(uint32 frame_number);
97 
99  void LeaveFrame();
100 
105  void EnterTimeRange(uint32 unique_id, const char* name, const char* value);
106 
111  uint32 EnterTimeRange(const char* name, const char* value);
112 
114  void LeaveTimeRange(uint32 id);
115 
117  void CreateTimeStamp(const char* name, const char* value);
118 
121  uint32 timestamp, const char* name, const char* value);
122 
125  size_t GetNumTraces() const;
126 
128  const ion::port::ThreadId& GetThreadId() const { return thread_id_; }
129 
131  void SetThreadName(const std::string& name) { thread_name_ = name; }
132 
135  std::string GetThreadName() const { return thread_name_; }
136 
138  void DumpStrings(std::vector<std::string>* table) const;
139 
141  void DumpTrace(std::string* output, uint32 string_index_offset) const;
142 
144  void AddTraceToTimelineNode(TimelineNode* root) const;
145 
148  uint32 GetCurrentFrameNumber() const;
149 
151  bool IsInFrameScope() const { return frame_level_ > 0; }
152 
153  private:
155  static size_t s_default_buffer_size_;
158  static bool s_reserve_buffer_;
159 
162  uint32 GetStringIndex(const std::string& str);
163 
167  std::string GetStringArg(size_t index, int arg_index,
168  const IndexToStringMap& inverse_string_buffer) const;
169 
173  std::unique_ptr<TimelineEvent> GetTimelineEvent(
174  size_t index, const IndexToStringMap& inverse_string_buffer) const;
175 
177  CallTraceManager* manager_;
178 
180  TraceBuffer trace_buffer_;
181 
190  std::unordered_map<std::string, uint32> string_buffer_;
191 
193  int scope_level_;
194 
196  ion::port::ThreadId thread_id_;
197 
199  std::string thread_name_;
200 
203  int frame_level_;
204  uint32 current_frame_number_;
205 };
206 
207 } // namespace profile
208 } // namespace ion
209 
210 #endif // ION_PROFILE_TRACERECORDER_H_
uint32 GetCurrentFrameNumber() const
Returns the frame number of the current frame scope, or 0 (and a warning message) if TraceRecorder is...
void AnnotateCurrentScopeAtTime(uint32 timestamp, const std::string &name, const std::string &value)
Same as AnnotateCurrentScope, but with specified timestamp.
void EnterTimeRange(uint32 unique_id, const char *name, const char *value)
Records the start of a time range event.
static size_t GetDefaultBufferSize()
Manipulate the default buffer size in bytes used for future instantiations.
Definition: tracerecorder.h:60
void AnnotateCurrentScope(const std::string &name, const std::string &value)
Attaches data to the current scope, which will be visible on mouse-over.
const std::string & str
static void SetDefaultBufferSize(size_t s)
Definition: tracerecorder.h:61
Class for recording frame events.
Definition: tracerecorder.h:42
void SetThreadName(const std::string &name)
Sets a name for the thread that this recorder is tracing.
double value
static void SetReserveBuffer(bool reserve)
Definition: tracerecorder.h:66
size_t GetNumTraces() const
Returns the total number of recorded trace events.
Table table
Definition: printer.cc:333
void EnterScopeAtTime(uint32 timestamp, int event_id)
Same as EnterScope, but with specified timestamp.
TraceRecorder(CallTraceManager *manager)
void AddTraceToTimelineNode(TimelineNode *root) const
Adds all events in the trace as a sub-tree under the passed in root node.
static bool GetReserveBuffer()
Maniupulate whether or not to reserve the full buffer size immediately for future instantiations...
Definition: tracerecorder.h:65
void LeaveScope()
Leaves the current (most recent) scope.
void CreateTimeStampAtTime(uint32 timestamp, const char *name, const char *value)
Same as CreateTimeStamp, but with specified timestamp.
TraceHeader(uint32 id, uint32 time_micros)
Definition: tracerecorder.h:45
void CreateTimeStamp(const char *name, const char *value)
Records a timeStamp event.
Allocatable is an abstract base class for classes whose memory is managed by an Allocator.
Definition: allocatable.h:60
void DumpStrings(std::vector< std::string > *table) const
Dumps the strings one by one to a string table.
std::string name
Definition: printer.cc:324
void LeaveFrame()
Records a frame exit event for the current frame index.
Copyright 2016 Google Inc.
Definition: timelinenode.h:28
base::CircularBuffer< uint32 > TraceBuffer
Definition: tracerecorder.h:51
void LeaveTimeRange(uint32 id)
Records the end of a time range event for the specified unique id.
std::unordered_map< uint32, std::string > IndexToStringMap
Definition: tracerecorder.h:52
void EnterScope(int event_id)
Queries and records the event corresponding to the provided event_id.
void DumpTrace(std::string *output, uint32 string_index_offset) const
Appends a binary dump of the trace to the output string.
void EnterFrame(uint32 frame_number)
Records a frame enter event with a specified frame index.
pthread_t ThreadId
Defines a type that can be used to identify a thread.
Definition: threadutils.h:40
bool IsInFrameScope() const
Returns if the TraceRecorder is currently in a frame scope.
Manages call trace recording for visualization in Web Tracing Framework (WTF) format.
const ion::port::ThreadId & GetThreadId() const
Returns the ID of the thread that this recorder is tracing.
std::string GetThreadName() const
Returns the name for the thread that this recorder is tracing.
void LeaveScopeAtTime(uint32 timestamp)
Same as LeaveScope, but with specified timestamp.