Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / base / trace_event / trace_event_impl.h
blob9e5d9f9d541c2b1026189944c55d6cebef196d0b
1 // Copyright (c) 2012 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.
6 #ifndef BASE_TRACE_EVENT_TRACE_EVENT_IMPL_H_
7 #define BASE_TRACE_EVENT_TRACE_EVENT_IMPL_H_
9 #include <stack>
10 #include <string>
11 #include <vector>
13 #include "base/atomicops.h"
14 #include "base/base_export.h"
15 #include "base/callback.h"
16 #include "base/containers/hash_tables.h"
17 #include "base/gtest_prod_util.h"
18 #include "base/memory/ref_counted_memory.h"
19 #include "base/observer_list.h"
20 #include "base/single_thread_task_runner.h"
21 #include "base/strings/string_util.h"
22 #include "base/synchronization/condition_variable.h"
23 #include "base/synchronization/lock.h"
24 #include "base/threading/thread.h"
25 #include "base/threading/thread_local.h"
26 #include "base/trace_event/trace_event_memory_overhead.h"
28 namespace base {
30 class WaitableEvent;
31 class MessageLoop;
33 namespace trace_event {
35 typedef base::Callback<bool(const char* category_group_name,
36 const char* event_name)> ArgumentFilterPredicate;
38 // For any argument of type TRACE_VALUE_TYPE_CONVERTABLE the provided
39 // class must implement this interface.
40 class BASE_EXPORT ConvertableToTraceFormat
41 : public RefCounted<ConvertableToTraceFormat> {
42 public:
43 // Append the class info to the provided |out| string. The appended
44 // data must be a valid JSON object. Strings must be properly quoted, and
45 // escaped. There is no processing applied to the content after it is
46 // appended.
47 virtual void AppendAsTraceFormat(std::string* out) const = 0;
49 virtual void EstimateTraceMemoryOverhead(TraceEventMemoryOverhead* overhead);
51 std::string ToString() const {
52 std::string result;
53 AppendAsTraceFormat(&result);
54 return result;
57 protected:
58 virtual ~ConvertableToTraceFormat() {}
60 private:
61 friend class RefCounted<ConvertableToTraceFormat>;
64 const int kTraceMaxNumArgs = 2;
66 struct TraceEventHandle {
67 uint32 chunk_seq;
68 // These numbers of bits must be kept consistent with
69 // TraceBufferChunk::kMaxTrunkIndex and
70 // TraceBufferChunk::kTraceBufferChunkSize (in trace_buffer.h).
71 unsigned chunk_index : 26;
72 unsigned event_index : 6;
75 class BASE_EXPORT TraceEvent {
76 public:
77 union TraceValue {
78 bool as_bool;
79 unsigned long long as_uint;
80 long long as_int;
81 double as_double;
82 const void* as_pointer;
83 const char* as_string;
86 TraceEvent();
87 ~TraceEvent();
89 // We don't need to copy TraceEvent except when TraceEventBuffer is cloned.
90 // Use explicit copy method to avoid accidentally misuse of copy.
91 void CopyFrom(const TraceEvent& other);
93 void Initialize(
94 int thread_id,
95 TraceTicks timestamp,
96 ThreadTicks thread_timestamp,
97 char phase,
98 const unsigned char* category_group_enabled,
99 const char* name,
100 unsigned long long id,
101 unsigned long long context_id,
102 unsigned long long bind_id,
103 int num_args,
104 const char** arg_names,
105 const unsigned char* arg_types,
106 const unsigned long long* arg_values,
107 const scoped_refptr<ConvertableToTraceFormat>* convertable_values,
108 unsigned int flags);
110 void Reset();
112 void UpdateDuration(const TraceTicks& now, const ThreadTicks& thread_now);
114 void EstimateTraceMemoryOverhead(TraceEventMemoryOverhead*);
116 // Serialize event data to JSON
117 void AppendAsJSON(
118 std::string* out,
119 const ArgumentFilterPredicate& argument_filter_predicate) const;
120 void AppendPrettyPrinted(std::ostringstream* out) const;
122 static void AppendValueAsJSON(unsigned char type,
123 TraceValue value,
124 std::string* out);
126 TraceTicks timestamp() const { return timestamp_; }
127 ThreadTicks thread_timestamp() const { return thread_timestamp_; }
128 char phase() const { return phase_; }
129 int thread_id() const { return thread_id_; }
130 TimeDelta duration() const { return duration_; }
131 TimeDelta thread_duration() const { return thread_duration_; }
132 unsigned long long id() const { return id_; }
133 unsigned long long context_id() const { return context_id_; }
134 unsigned int flags() const { return flags_; }
136 // Exposed for unittesting:
138 const base::RefCountedString* parameter_copy_storage() const {
139 return parameter_copy_storage_.get();
142 const unsigned char* category_group_enabled() const {
143 return category_group_enabled_;
146 const char* name() const { return name_; }
148 #if defined(OS_ANDROID)
149 void SendToATrace();
150 #endif
152 private:
153 // Note: these are ordered by size (largest first) for optimal packing.
154 TraceTicks timestamp_;
155 ThreadTicks thread_timestamp_;
156 TimeDelta duration_;
157 TimeDelta thread_duration_;
158 // id_ can be used to store phase-specific data.
159 unsigned long long id_;
160 // context_id_ is used to store context information.
161 unsigned long long context_id_;
162 TraceValue arg_values_[kTraceMaxNumArgs];
163 const char* arg_names_[kTraceMaxNumArgs];
164 scoped_refptr<ConvertableToTraceFormat> convertable_values_[kTraceMaxNumArgs];
165 const unsigned char* category_group_enabled_;
166 const char* name_;
167 scoped_refptr<base::RefCountedString> parameter_copy_storage_;
168 int thread_id_;
169 char phase_;
170 unsigned int flags_;
171 unsigned long long bind_id_;
172 unsigned char arg_types_[kTraceMaxNumArgs];
174 DISALLOW_COPY_AND_ASSIGN(TraceEvent);
177 } // namespace trace_event
178 } // namespace base
180 #endif // BASE_TRACE_EVENT_TRACE_EVENT_IMPL_H_