SupervisedUser SafeSites: Switch to the new SafeSearch API
[chromium-blink-merge.git] / base / trace_event / trace_event_impl.h
blob0d66bb2f9eaa4b1aeac8be24f394fc6db04f5933
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/memory/scoped_vector.h"
20 #include "base/observer_list.h"
21 #include "base/single_thread_task_runner.h"
22 #include "base/strings/string_util.h"
23 #include "base/synchronization/condition_variable.h"
24 #include "base/synchronization/lock.h"
25 #include "base/threading/thread.h"
26 #include "base/threading/thread_local.h"
27 #include "base/trace_event/trace_event_memory_overhead.h"
29 namespace base {
31 class WaitableEvent;
32 class MessageLoop;
34 namespace trace_event {
36 typedef base::Callback<bool(const char* category_group_name,
37 const char* event_name)> ArgumentFilterPredicate;
39 // For any argument of type TRACE_VALUE_TYPE_CONVERTABLE the provided
40 // class must implement this interface.
41 class BASE_EXPORT ConvertableToTraceFormat
42 : public RefCounted<ConvertableToTraceFormat> {
43 public:
44 // Append the class info to the provided |out| string. The appended
45 // data must be a valid JSON object. Strings must be properly quoted, and
46 // escaped. There is no processing applied to the content after it is
47 // appended.
48 virtual void AppendAsTraceFormat(std::string* out) const = 0;
50 virtual void EstimateTraceMemoryOverhead(TraceEventMemoryOverhead* overhead);
52 std::string ToString() const {
53 std::string result;
54 AppendAsTraceFormat(&result);
55 return result;
58 protected:
59 virtual ~ConvertableToTraceFormat() {}
61 private:
62 friend class RefCounted<ConvertableToTraceFormat>;
65 const int kTraceMaxNumArgs = 2;
67 struct TraceEventHandle {
68 uint32 chunk_seq;
69 // These numbers of bits must be kept consistent with
70 // TraceBufferChunk::kMaxTrunkIndex and
71 // TraceBufferChunk::kTraceBufferChunkSize (in trace_buffer.h).
72 unsigned chunk_index : 26;
73 unsigned event_index : 6;
76 class BASE_EXPORT TraceEvent {
77 public:
78 union TraceValue {
79 bool as_bool;
80 unsigned long long as_uint;
81 long long as_int;
82 double as_double;
83 const void* as_pointer;
84 const char* as_string;
87 TraceEvent();
88 ~TraceEvent();
90 // We don't need to copy TraceEvent except when TraceEventBuffer is cloned.
91 // Use explicit copy method to avoid accidentally misuse of copy.
92 void CopyFrom(const TraceEvent& other);
94 void Initialize(
95 int thread_id,
96 TraceTicks timestamp,
97 ThreadTicks thread_timestamp,
98 char phase,
99 const unsigned char* category_group_enabled,
100 const char* name,
101 unsigned long long id,
102 unsigned long long context_id,
103 unsigned long long bind_id,
104 int num_args,
105 const char** arg_names,
106 const unsigned char* arg_types,
107 const unsigned long long* arg_values,
108 const scoped_refptr<ConvertableToTraceFormat>* convertable_values,
109 unsigned int flags);
111 void Reset();
113 void UpdateDuration(const TraceTicks& now, const ThreadTicks& thread_now);
115 void EstimateTraceMemoryOverhead(TraceEventMemoryOverhead*);
117 // Serialize event data to JSON
118 void AppendAsJSON(
119 std::string* out,
120 const ArgumentFilterPredicate& argument_filter_predicate) const;
121 void AppendPrettyPrinted(std::ostringstream* out) const;
123 static void AppendValueAsJSON(unsigned char type,
124 TraceValue value,
125 std::string* out);
127 TraceTicks timestamp() const { return timestamp_; }
128 ThreadTicks thread_timestamp() const { return thread_timestamp_; }
129 char phase() const { return phase_; }
130 int thread_id() const { return thread_id_; }
131 TimeDelta duration() const { return duration_; }
132 TimeDelta thread_duration() const { return thread_duration_; }
133 unsigned long long id() const { return id_; }
134 unsigned long long context_id() const { return context_id_; }
135 unsigned int flags() const { return flags_; }
137 // Exposed for unittesting:
139 const base::RefCountedString* parameter_copy_storage() const {
140 return parameter_copy_storage_.get();
143 const unsigned char* category_group_enabled() const {
144 return category_group_enabled_;
147 const char* name() const { return name_; }
149 #if defined(OS_ANDROID)
150 void SendToATrace();
151 #endif
153 private:
154 // Note: these are ordered by size (largest first) for optimal packing.
155 TraceTicks timestamp_;
156 ThreadTicks thread_timestamp_;
157 TimeDelta duration_;
158 TimeDelta thread_duration_;
159 // id_ can be used to store phase-specific data.
160 unsigned long long id_;
161 // context_id_ is used to store context information.
162 unsigned long long context_id_;
163 TraceValue arg_values_[kTraceMaxNumArgs];
164 const char* arg_names_[kTraceMaxNumArgs];
165 scoped_refptr<ConvertableToTraceFormat> convertable_values_[kTraceMaxNumArgs];
166 const unsigned char* category_group_enabled_;
167 const char* name_;
168 scoped_refptr<base::RefCountedString> parameter_copy_storage_;
169 int thread_id_;
170 char phase_;
171 unsigned int flags_;
172 unsigned long long bind_id_;
173 unsigned char arg_types_[kTraceMaxNumArgs];
175 DISALLOW_COPY_AND_ASSIGN(TraceEvent);
178 } // namespace trace_event
179 } // namespace base
181 #endif // BASE_TRACE_EVENT_TRACE_EVENT_IMPL_H_