OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 | 5 |
6 #ifndef BASE_TRACE_EVENT_TRACE_EVENT_IMPL_H_ | 6 #ifndef BASE_TRACE_EVENT_TRACE_EVENT_IMPL_H_ |
7 #define BASE_TRACE_EVENT_TRACE_EVENT_IMPL_H_ | 7 #define BASE_TRACE_EVENT_TRACE_EVENT_IMPL_H_ |
8 | 8 |
9 #include <stack> | 9 #include <stack> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/atomicops.h" | 13 #include "base/atomicops.h" |
14 #include "base/base_export.h" | 14 #include "base/base_export.h" |
15 #include "base/callback.h" | 15 #include "base/callback.h" |
16 #include "base/containers/hash_tables.h" | 16 #include "base/containers/hash_tables.h" |
17 #include "base/gtest_prod_util.h" | 17 #include "base/gtest_prod_util.h" |
18 #include "base/memory/ref_counted_memory.h" | 18 #include "base/memory/ref_counted_memory.h" |
19 #include "base/memory/scoped_vector.h" | 19 #include "base/memory/scoped_vector.h" |
20 #include "base/observer_list.h" | 20 #include "base/observer_list.h" |
21 #include "base/single_thread_task_runner.h" | 21 #include "base/single_thread_task_runner.h" |
22 #include "base/strings/string_util.h" | 22 #include "base/strings/string_util.h" |
23 #include "base/synchronization/condition_variable.h" | 23 #include "base/synchronization/condition_variable.h" |
24 #include "base/synchronization/lock.h" | 24 #include "base/synchronization/lock.h" |
25 #include "base/threading/thread.h" | 25 #include "base/threading/thread.h" |
26 #include "base/threading/thread_local.h" | 26 #include "base/threading/thread_local.h" |
| 27 #include "base/trace_event/memory_dump_provider.h" |
27 #include "base/trace_event/trace_config.h" | 28 #include "base/trace_event/trace_config.h" |
| 29 #include "base/trace_event/trace_event_memory_overhead.h" |
28 | 30 |
29 // Older style trace macros with explicit id and extra data | 31 // Older style trace macros with explicit id and extra data |
30 // Only these macros result in publishing data to ETW as currently implemented. | 32 // Only these macros result in publishing data to ETW as currently implemented. |
31 // TODO(georgesak): Update/replace these with new ETW macros. | 33 // TODO(georgesak): Update/replace these with new ETW macros. |
32 #define TRACE_EVENT_BEGIN_ETW(name, id, extra) \ | 34 #define TRACE_EVENT_BEGIN_ETW(name, id, extra) \ |
33 base::trace_event::TraceLog::AddTraceEventEtw( \ | 35 base::trace_event::TraceLog::AddTraceEventEtw( \ |
34 TRACE_EVENT_PHASE_BEGIN, \ | 36 TRACE_EVENT_PHASE_BEGIN, \ |
35 name, reinterpret_cast<const void*>(id), extra) | 37 name, reinterpret_cast<const void*>(id), extra) |
36 | 38 |
37 #define TRACE_EVENT_END_ETW(name, id, extra) \ | 39 #define TRACE_EVENT_END_ETW(name, id, extra) \ |
(...skipping 20 matching lines...) Expand all Loading... |
58 // class must implement this interface. | 60 // class must implement this interface. |
59 class BASE_EXPORT ConvertableToTraceFormat | 61 class BASE_EXPORT ConvertableToTraceFormat |
60 : public RefCounted<ConvertableToTraceFormat> { | 62 : public RefCounted<ConvertableToTraceFormat> { |
61 public: | 63 public: |
62 // Append the class info to the provided |out| string. The appended | 64 // Append the class info to the provided |out| string. The appended |
63 // data must be a valid JSON object. Strings must be properly quoted, and | 65 // data must be a valid JSON object. Strings must be properly quoted, and |
64 // escaped. There is no processing applied to the content after it is | 66 // escaped. There is no processing applied to the content after it is |
65 // appended. | 67 // appended. |
66 virtual void AppendAsTraceFormat(std::string* out) const = 0; | 68 virtual void AppendAsTraceFormat(std::string* out) const = 0; |
67 | 69 |
| 70 virtual void EstimateTraceMemoryOverhead(TraceEventMemoryOverhead* overhead); |
| 71 |
68 std::string ToString() const { | 72 std::string ToString() const { |
69 std::string result; | 73 std::string result; |
70 AppendAsTraceFormat(&result); | 74 AppendAsTraceFormat(&result); |
71 return result; | 75 return result; |
72 } | 76 } |
73 | 77 |
74 protected: | 78 protected: |
75 virtual ~ConvertableToTraceFormat() {} | 79 virtual ~ConvertableToTraceFormat() {} |
76 | 80 |
77 private: | 81 private: |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 ThreadTicks thread_timestamp, | 114 ThreadTicks thread_timestamp, |
111 char phase, | 115 char phase, |
112 const unsigned char* category_group_enabled, | 116 const unsigned char* category_group_enabled, |
113 const char* name, | 117 const char* name, |
114 unsigned long long id, | 118 unsigned long long id, |
115 int num_args, | 119 int num_args, |
116 const char** arg_names, | 120 const char** arg_names, |
117 const unsigned char* arg_types, | 121 const unsigned char* arg_types, |
118 const unsigned long long* arg_values, | 122 const unsigned long long* arg_values, |
119 const scoped_refptr<ConvertableToTraceFormat>* convertable_values, | 123 const scoped_refptr<ConvertableToTraceFormat>* convertable_values, |
120 unsigned char flags); | 124 unsigned int flags); |
121 | 125 |
122 void Reset(); | 126 void Reset(); |
123 | 127 |
124 void UpdateDuration(const TraceTicks& now, const ThreadTicks& thread_now); | 128 void UpdateDuration(const TraceTicks& now, const ThreadTicks& thread_now); |
125 | 129 |
| 130 void EstimateTraceMemoryOverhead(TraceEventMemoryOverhead*); |
| 131 |
126 // Serialize event data to JSON | 132 // Serialize event data to JSON |
127 typedef base::Callback<bool(const char* category_group_name, | 133 typedef base::Callback<bool(const char* category_group_name, |
128 const char* event_name)> ArgumentFilterPredicate; | 134 const char* event_name)> ArgumentFilterPredicate; |
129 void AppendAsJSON( | 135 void AppendAsJSON( |
130 std::string* out, | 136 std::string* out, |
131 const ArgumentFilterPredicate& argument_filter_predicate) const; | 137 const ArgumentFilterPredicate& argument_filter_predicate) const; |
132 void AppendPrettyPrinted(std::ostringstream* out) const; | 138 void AppendPrettyPrinted(std::ostringstream* out) const; |
133 | 139 |
134 static void AppendValueAsJSON(unsigned char type, | 140 static void AppendValueAsJSON(unsigned char type, |
135 TraceValue value, | 141 TraceValue value, |
136 std::string* out); | 142 std::string* out); |
137 | 143 |
138 TraceTicks timestamp() const { return timestamp_; } | 144 TraceTicks timestamp() const { return timestamp_; } |
139 ThreadTicks thread_timestamp() const { return thread_timestamp_; } | 145 ThreadTicks thread_timestamp() const { return thread_timestamp_; } |
140 char phase() const { return phase_; } | 146 char phase() const { return phase_; } |
141 int thread_id() const { return thread_id_; } | 147 int thread_id() const { return thread_id_; } |
142 TimeDelta duration() const { return duration_; } | 148 TimeDelta duration() const { return duration_; } |
143 TimeDelta thread_duration() const { return thread_duration_; } | 149 TimeDelta thread_duration() const { return thread_duration_; } |
144 unsigned long long id() const { return id_; } | 150 unsigned long long id() const { return id_; } |
145 unsigned char flags() const { return flags_; } | 151 unsigned int flags() const { return flags_; } |
146 | 152 |
147 // Exposed for unittesting: | 153 // Exposed for unittesting: |
148 | 154 |
149 const base::RefCountedString* parameter_copy_storage() const { | 155 const base::RefCountedString* parameter_copy_storage() const { |
150 return parameter_copy_storage_.get(); | 156 return parameter_copy_storage_.get(); |
151 } | 157 } |
152 | 158 |
153 const unsigned char* category_group_enabled() const { | 159 const unsigned char* category_group_enabled() const { |
154 return category_group_enabled_; | 160 return category_group_enabled_; |
155 } | 161 } |
156 | 162 |
157 const char* name() const { return name_; } | 163 const char* name() const { return name_; } |
158 | 164 |
159 #if defined(OS_ANDROID) | 165 #if defined(OS_ANDROID) |
160 void SendToATrace(); | 166 void SendToATrace(); |
161 #endif | 167 #endif |
162 | 168 |
163 private: | 169 private: |
164 // Note: these are ordered by size (largest first) for optimal packing. | 170 // Note: these are ordered by size (largest first) for optimal packing. |
165 TraceTicks timestamp_; | 171 TraceTicks timestamp_; |
166 ThreadTicks thread_timestamp_; | 172 ThreadTicks thread_timestamp_; |
167 TimeDelta duration_; | 173 TimeDelta duration_; |
168 TimeDelta thread_duration_; | 174 TimeDelta thread_duration_; |
169 // id_ can be used to store phase-specific data. | 175 // id_ can be used to store phase-specific data. |
170 unsigned long long id_; | 176 unsigned long long id_; |
| 177 scoped_ptr<TraceEventMemoryOverhead> cached_memory_overhead_estimate_; |
171 TraceValue arg_values_[kTraceMaxNumArgs]; | 178 TraceValue arg_values_[kTraceMaxNumArgs]; |
172 const char* arg_names_[kTraceMaxNumArgs]; | 179 const char* arg_names_[kTraceMaxNumArgs]; |
173 scoped_refptr<ConvertableToTraceFormat> convertable_values_[kTraceMaxNumArgs]; | 180 scoped_refptr<ConvertableToTraceFormat> convertable_values_[kTraceMaxNumArgs]; |
174 const unsigned char* category_group_enabled_; | 181 const unsigned char* category_group_enabled_; |
175 const char* name_; | 182 const char* name_; |
176 scoped_refptr<base::RefCountedString> parameter_copy_storage_; | 183 scoped_refptr<base::RefCountedString> parameter_copy_storage_; |
177 int thread_id_; | 184 int thread_id_; |
178 char phase_; | 185 char phase_; |
179 unsigned char flags_; | 186 unsigned int flags_; |
180 unsigned char arg_types_[kTraceMaxNumArgs]; | 187 unsigned char arg_types_[kTraceMaxNumArgs]; |
181 | 188 |
182 DISALLOW_COPY_AND_ASSIGN(TraceEvent); | 189 DISALLOW_COPY_AND_ASSIGN(TraceEvent); |
183 }; | 190 }; |
184 | 191 |
185 // TraceBufferChunk is the basic unit of TraceBuffer. | 192 // TraceBufferChunk is the basic unit of TraceBuffer. |
186 class BASE_EXPORT TraceBufferChunk { | 193 class BASE_EXPORT TraceBufferChunk { |
187 public: | 194 public: |
188 explicit TraceBufferChunk(uint32 seq) | 195 explicit TraceBufferChunk(uint32 seq); |
189 : next_free_(0), | 196 ~TraceBufferChunk(); |
190 seq_(seq) { | |
191 } | |
192 | 197 |
193 void Reset(uint32 new_seq); | 198 void Reset(uint32 new_seq); |
194 TraceEvent* AddTraceEvent(size_t* event_index); | 199 TraceEvent* AddTraceEvent(size_t* event_index); |
195 bool IsFull() const { return next_free_ == kTraceBufferChunkSize; } | 200 bool IsFull() const { return next_free_ == kTraceBufferChunkSize; } |
196 | 201 |
197 uint32 seq() const { return seq_; } | 202 uint32 seq() const { return seq_; } |
198 size_t capacity() const { return kTraceBufferChunkSize; } | 203 size_t capacity() const { return kTraceBufferChunkSize; } |
199 size_t size() const { return next_free_; } | 204 size_t size() const { return next_free_; } |
200 | 205 |
201 TraceEvent* GetEventAt(size_t index) { | 206 TraceEvent* GetEventAt(size_t index) { |
202 DCHECK(index < size()); | 207 DCHECK(index < size()); |
203 return &chunk_[index]; | 208 return &chunk_[index]; |
204 } | 209 } |
205 const TraceEvent* GetEventAt(size_t index) const { | 210 const TraceEvent* GetEventAt(size_t index) const { |
206 DCHECK(index < size()); | 211 DCHECK(index < size()); |
207 return &chunk_[index]; | 212 return &chunk_[index]; |
208 } | 213 } |
209 | 214 |
210 scoped_ptr<TraceBufferChunk> Clone() const; | 215 scoped_ptr<TraceBufferChunk> Clone() const; |
211 | 216 |
| 217 void EstimateTraceMemoryOverhead(TraceEventMemoryOverhead* overhead); |
| 218 |
212 static const size_t kTraceBufferChunkSize = 64; | 219 static const size_t kTraceBufferChunkSize = 64; |
213 | 220 |
214 private: | 221 private: |
215 size_t next_free_; | 222 size_t next_free_; |
| 223 scoped_ptr<TraceEventMemoryOverhead> cached_overhead_estimate_when_full_; |
216 TraceEvent chunk_[kTraceBufferChunkSize]; | 224 TraceEvent chunk_[kTraceBufferChunkSize]; |
217 uint32 seq_; | 225 uint32 seq_; |
218 }; | 226 }; |
219 | 227 |
220 // TraceBuffer holds the events as they are collected. | 228 // TraceBuffer holds the events as they are collected. |
221 class BASE_EXPORT TraceBuffer { | 229 class BASE_EXPORT TraceBuffer { |
222 public: | 230 public: |
223 virtual ~TraceBuffer() {} | 231 virtual ~TraceBuffer() {} |
224 | 232 |
225 virtual scoped_ptr<TraceBufferChunk> GetChunk(size_t *index) = 0; | 233 virtual scoped_ptr<TraceBufferChunk> GetChunk(size_t *index) = 0; |
226 virtual void ReturnChunk(size_t index, | 234 virtual void ReturnChunk(size_t index, |
227 scoped_ptr<TraceBufferChunk> chunk) = 0; | 235 scoped_ptr<TraceBufferChunk> chunk) = 0; |
228 | 236 |
229 virtual bool IsFull() const = 0; | 237 virtual bool IsFull() const = 0; |
230 virtual size_t Size() const = 0; | 238 virtual size_t Size() const = 0; |
231 virtual size_t Capacity() const = 0; | 239 virtual size_t Capacity() const = 0; |
232 virtual TraceEvent* GetEventByHandle(TraceEventHandle handle) = 0; | 240 virtual TraceEvent* GetEventByHandle(TraceEventHandle handle) = 0; |
233 | 241 |
234 // For iteration. Each TraceBuffer can only be iterated once. | 242 // For iteration. Each TraceBuffer can only be iterated once. |
235 virtual const TraceBufferChunk* NextChunk() = 0; | 243 virtual const TraceBufferChunk* NextChunk() = 0; |
236 | 244 |
237 virtual scoped_ptr<TraceBuffer> CloneForIteration() const = 0; | 245 virtual scoped_ptr<TraceBuffer> CloneForIteration() const = 0; |
| 246 |
| 247 // Computes an estimate of the size of the buffer, including all the retained |
| 248 // objects. |
| 249 virtual void EstimateTraceMemoryOverhead( |
| 250 TraceEventMemoryOverhead* overhead) = 0; |
238 }; | 251 }; |
239 | 252 |
240 // TraceResultBuffer collects and converts trace fragments returned by TraceLog | 253 // TraceResultBuffer collects and converts trace fragments returned by TraceLog |
241 // to JSON output. | 254 // to JSON output. |
242 class BASE_EXPORT TraceResultBuffer { | 255 class BASE_EXPORT TraceResultBuffer { |
243 public: | 256 public: |
244 typedef base::Callback<void(const std::string&)> OutputCallback; | 257 typedef base::Callback<void(const std::string&)> OutputCallback; |
245 | 258 |
246 // If you don't need to stream JSON chunks out efficiently, and just want to | 259 // If you don't need to stream JSON chunks out efficiently, and just want to |
247 // get a complete JSON string after calling Finish, use this struct to collect | 260 // get a complete JSON string after calling Finish, use this struct to collect |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 | 295 |
283 class TraceSamplingThread; | 296 class TraceSamplingThread; |
284 | 297 |
285 struct BASE_EXPORT TraceLogStatus { | 298 struct BASE_EXPORT TraceLogStatus { |
286 TraceLogStatus(); | 299 TraceLogStatus(); |
287 ~TraceLogStatus(); | 300 ~TraceLogStatus(); |
288 size_t event_capacity; | 301 size_t event_capacity; |
289 size_t event_count; | 302 size_t event_count; |
290 }; | 303 }; |
291 | 304 |
292 class BASE_EXPORT TraceLog { | 305 class BASE_EXPORT TraceLog : public MemoryDumpProvider { |
293 public: | 306 public: |
294 enum Mode { | 307 enum Mode { |
295 DISABLED = 0, | 308 DISABLED = 0, |
296 RECORDING_MODE, | 309 RECORDING_MODE, |
297 MONITORING_MODE, | 310 MONITORING_MODE, |
298 }; | 311 }; |
299 | 312 |
300 // The pointer returned from GetCategoryGroupEnabledInternal() points to a | 313 // The pointer returned from GetCategoryGroupEnabledInternal() points to a |
301 // value with zero or more of the following bits. Used in this class only. | 314 // value with zero or more of the following bits. Used in this class only. |
302 // The TRACE_EVENT macros should only use the value as a bool. | 315 // The TRACE_EVENT macros should only use the value as a bool. |
(...skipping 11 matching lines...) Expand all Loading... |
314 | 327 |
315 static TraceLog* GetInstance(); | 328 static TraceLog* GetInstance(); |
316 | 329 |
317 // Get set of known category groups. This can change as new code paths are | 330 // Get set of known category groups. This can change as new code paths are |
318 // reached. The known category groups are inserted into |category_groups|. | 331 // reached. The known category groups are inserted into |category_groups|. |
319 void GetKnownCategoryGroups(std::vector<std::string>* category_groups); | 332 void GetKnownCategoryGroups(std::vector<std::string>* category_groups); |
320 | 333 |
321 // Retrieves a copy (for thread-safety) of the current TraceConfig. | 334 // Retrieves a copy (for thread-safety) of the current TraceConfig. |
322 TraceConfig GetCurrentTraceConfig() const; | 335 TraceConfig GetCurrentTraceConfig() const; |
323 | 336 |
| 337 // Initializes the thread-local event buffer, if not already initialized and |
| 338 // if the current thread supports that (has a message loop). |
| 339 void InitializeThreadLocalEventBufferIfSupported(); |
| 340 |
324 // Enables normal tracing (recording trace events in the trace buffer). | 341 // Enables normal tracing (recording trace events in the trace buffer). |
325 // See TraceConfig comments for details on how to control what categories | 342 // See TraceConfig comments for details on how to control what categories |
326 // will be traced. If tracing has already been enabled, |category_filter| will | 343 // will be traced. If tracing has already been enabled, |category_filter| will |
327 // be merged into the current category filter. | 344 // be merged into the current category filter. |
328 void SetEnabled(const TraceConfig& trace_config, Mode mode); | 345 void SetEnabled(const TraceConfig& trace_config, Mode mode); |
329 | 346 |
330 // Disables normal tracing for all categories. | 347 // Disables normal tracing for all categories. |
331 void SetDisabled(); | 348 void SetDisabled(); |
332 | 349 |
333 bool IsEnabled() { return mode_ != DISABLED; } | 350 bool IsEnabled() { return mode_ != DISABLED; } |
334 | 351 |
335 // The number of times we have begun recording traces. If tracing is off, | 352 // The number of times we have begun recording traces. If tracing is off, |
336 // returns -1. If tracing is on, then it returns the number of times we have | 353 // returns -1. If tracing is on, then it returns the number of times we have |
337 // recorded a trace. By watching for this number to increment, you can | 354 // recorded a trace. By watching for this number to increment, you can |
338 // passively discover when a new trace has begun. This is then used to | 355 // passively discover when a new trace has begun. This is then used to |
339 // implement the TRACE_EVENT_IS_NEW_TRACE() primitive. | 356 // implement the TRACE_EVENT_IS_NEW_TRACE() primitive. |
340 int GetNumTracesRecorded(); | 357 int GetNumTracesRecorded(); |
341 | 358 |
342 #if defined(OS_ANDROID) | 359 #if defined(OS_ANDROID) |
343 void StartATrace(); | 360 void StartATrace(); |
344 void StopATrace(); | 361 void StopATrace(); |
345 void AddClockSyncMetadataEvent(); | 362 void AddClockSyncMetadataEvent(); |
346 #endif | 363 #endif |
347 | 364 |
348 // Enabled state listeners give a callback when tracing is enabled or | 365 // Enabled state listeners give a callback when tracing is enabled or |
349 // disabled. This can be used to tie into other library's tracing systems | 366 // disabled. This can be used to tie into other library's tracing systems |
350 // on-demand. | 367 // on-demand. |
351 class BASE_EXPORT EnabledStateObserver { | 368 class BASE_EXPORT EnabledStateObserver { |
352 public: | 369 public: |
| 370 virtual ~EnabledStateObserver() = default; |
| 371 |
353 // Called just after the tracing system becomes enabled, outside of the | 372 // Called just after the tracing system becomes enabled, outside of the |
354 // |lock_|. TraceLog::IsEnabled() is true at this point. | 373 // |lock_|. TraceLog::IsEnabled() is true at this point. |
355 virtual void OnTraceLogEnabled() = 0; | 374 virtual void OnTraceLogEnabled() = 0; |
356 | 375 |
357 // Called just after the tracing system disables, outside of the |lock_|. | 376 // Called just after the tracing system disables, outside of the |lock_|. |
358 // TraceLog::IsEnabled() is false at this point. | 377 // TraceLog::IsEnabled() is false at this point. |
359 virtual void OnTraceLogDisabled() = 0; | 378 virtual void OnTraceLogDisabled() = 0; |
360 }; | 379 }; |
361 void AddEnabledStateObserver(EnabledStateObserver* listener); | 380 void AddEnabledStateObserver(EnabledStateObserver* listener); |
362 void RemoveEnabledStateObserver(EnabledStateObserver* listener); | 381 void RemoveEnabledStateObserver(EnabledStateObserver* listener); |
363 bool HasEnabledStateObserver(EnabledStateObserver* listener) const; | 382 bool HasEnabledStateObserver(EnabledStateObserver* listener) const; |
364 | 383 |
365 TraceLogStatus GetStatus() const; | 384 TraceLogStatus GetStatus() const; |
366 bool BufferIsFull() const; | 385 bool BufferIsFull() const; |
367 | 386 |
| 387 // Computes an estimate of the size of the TraceLog including all the retained |
| 388 // objects. |
| 389 void EstimateTraceMemoryOverhead(TraceEventMemoryOverhead* overhead); |
| 390 |
368 // Not using base::Callback because of its limited by 7 parameters. | 391 // Not using base::Callback because of its limited by 7 parameters. |
369 // Also, using primitive type allows directly passing callback from WebCore. | 392 // Also, using primitive type allows directly passing callback from WebCore. |
370 // WARNING: It is possible for the previously set callback to be called | 393 // WARNING: It is possible for the previously set callback to be called |
371 // after a call to SetEventCallbackEnabled() that replaces or a call to | 394 // after a call to SetEventCallbackEnabled() that replaces or a call to |
372 // SetEventCallbackDisabled() that disables the callback. | 395 // SetEventCallbackDisabled() that disables the callback. |
373 // This callback may be invoked on any thread. | 396 // This callback may be invoked on any thread. |
374 // For TRACE_EVENT_PHASE_COMPLETE events, the client will still receive pairs | 397 // For TRACE_EVENT_PHASE_COMPLETE events, the client will still receive pairs |
375 // of TRACE_EVENT_PHASE_BEGIN and TRACE_EVENT_PHASE_END events to keep the | 398 // of TRACE_EVENT_PHASE_BEGIN and TRACE_EVENT_PHASE_END events to keep the |
376 // interface simple. | 399 // interface simple. |
377 typedef void (*EventCallback)(TraceTicks timestamp, | 400 typedef void (*EventCallback)(TraceTicks timestamp, |
378 char phase, | 401 char phase, |
379 const unsigned char* category_group_enabled, | 402 const unsigned char* category_group_enabled, |
380 const char* name, | 403 const char* name, |
381 unsigned long long id, | 404 unsigned long long id, |
382 int num_args, | 405 int num_args, |
383 const char* const arg_names[], | 406 const char* const arg_names[], |
384 const unsigned char arg_types[], | 407 const unsigned char arg_types[], |
385 const unsigned long long arg_values[], | 408 const unsigned long long arg_values[], |
386 unsigned char flags); | 409 unsigned int flags); |
387 | 410 |
388 // Enable tracing for EventCallback. | 411 // Enable tracing for EventCallback. |
389 void SetEventCallbackEnabled(const TraceConfig& trace_config, | 412 void SetEventCallbackEnabled(const TraceConfig& trace_config, |
390 EventCallback cb); | 413 EventCallback cb); |
391 void SetEventCallbackDisabled(); | 414 void SetEventCallbackDisabled(); |
392 void SetArgumentFilterPredicate( | 415 void SetArgumentFilterPredicate( |
393 const TraceEvent::ArgumentFilterPredicate& argument_filter_predicate); | 416 const TraceEvent::ArgumentFilterPredicate& argument_filter_predicate); |
394 | 417 |
395 // Flush all collected events to the given output callback. The callback will | 418 // Flush all collected events to the given output callback. The callback will |
396 // be called one or more times either synchronously or asynchronously from | 419 // be called one or more times either synchronously or asynchronously from |
397 // the current thread with IPC-bite-size chunks. The string format is | 420 // the current thread with IPC-bite-size chunks. The string format is |
398 // undefined. Use TraceResultBuffer to convert one or more trace strings to | 421 // undefined. Use TraceResultBuffer to convert one or more trace strings to |
399 // JSON. The callback can be null if the caller doesn't want any data. | 422 // JSON. The callback can be null if the caller doesn't want any data. |
400 // Due to the implementation of thread-local buffers, flush can't be | 423 // Due to the implementation of thread-local buffers, flush can't be |
401 // done when tracing is enabled. If called when tracing is enabled, the | 424 // done when tracing is enabled. If called when tracing is enabled, the |
402 // callback will be called directly with (empty_string, false) to indicate | 425 // callback will be called directly with (empty_string, false) to indicate |
403 // the end of this unsuccessful flush. Flush does the serialization | 426 // the end of this unsuccessful flush. Flush does the serialization |
404 // on the same thread if the caller doesn't set use_worker_thread explicitly. | 427 // on the same thread if the caller doesn't set use_worker_thread explicitly. |
405 typedef base::Callback<void(const scoped_refptr<base::RefCountedString>&, | 428 typedef base::Callback<void(const scoped_refptr<base::RefCountedString>&, |
406 bool has_more_events)> OutputCallback; | 429 bool has_more_events)> OutputCallback; |
407 void Flush(const OutputCallback& cb, bool use_worker_thread = false); | 430 void Flush(const OutputCallback& cb, bool use_worker_thread = false); |
408 void FlushButLeaveBufferIntact(const OutputCallback& flush_output_callback); | 431 void FlushButLeaveBufferIntact(const OutputCallback& flush_output_callback); |
409 | 432 |
| 433 // Cancels tracing and discards collected data. |
| 434 void CancelTracing(const OutputCallback& cb); |
| 435 |
410 // Called by TRACE_EVENT* macros, don't call this directly. | 436 // Called by TRACE_EVENT* macros, don't call this directly. |
411 // The name parameter is a category group for example: | 437 // The name parameter is a category group for example: |
412 // TRACE_EVENT0("renderer,webkit", "WebViewImpl::HandleInputEvent") | 438 // TRACE_EVENT0("renderer,webkit", "WebViewImpl::HandleInputEvent") |
413 static const unsigned char* GetCategoryGroupEnabled(const char* name); | 439 static const unsigned char* GetCategoryGroupEnabled(const char* name); |
414 static const char* GetCategoryGroupName( | 440 static const char* GetCategoryGroupName( |
415 const unsigned char* category_group_enabled); | 441 const unsigned char* category_group_enabled); |
416 | 442 |
417 // Called by TRACE_EVENT* macros, don't call this directly. | 443 // Called by TRACE_EVENT* macros, don't call this directly. |
418 // If |copy| is set, |name|, |arg_name1| and |arg_name2| will be deep copied | 444 // If |copy| is set, |name|, |arg_name1| and |arg_name2| will be deep copied |
419 // into the event; see "Memory scoping note" and TRACE_EVENT_COPY_XXX above. | 445 // into the event; see "Memory scoping note" and TRACE_EVENT_COPY_XXX above. |
420 TraceEventHandle AddTraceEvent( | 446 TraceEventHandle AddTraceEvent( |
421 char phase, | 447 char phase, |
422 const unsigned char* category_group_enabled, | 448 const unsigned char* category_group_enabled, |
423 const char* name, | 449 const char* name, |
424 unsigned long long id, | 450 unsigned long long id, |
425 int num_args, | 451 int num_args, |
426 const char** arg_names, | 452 const char** arg_names, |
427 const unsigned char* arg_types, | 453 const unsigned char* arg_types, |
428 const unsigned long long* arg_values, | 454 const unsigned long long* arg_values, |
429 const scoped_refptr<ConvertableToTraceFormat>* convertable_values, | 455 const scoped_refptr<ConvertableToTraceFormat>* convertable_values, |
430 unsigned char flags); | 456 unsigned int flags); |
431 TraceEventHandle AddTraceEventWithThreadIdAndTimestamp( | 457 TraceEventHandle AddTraceEventWithThreadIdAndTimestamp( |
432 char phase, | 458 char phase, |
433 const unsigned char* category_group_enabled, | 459 const unsigned char* category_group_enabled, |
434 const char* name, | 460 const char* name, |
435 unsigned long long id, | 461 unsigned long long id, |
436 int thread_id, | 462 int thread_id, |
437 const TraceTicks& timestamp, | 463 const TraceTicks& timestamp, |
438 int num_args, | 464 int num_args, |
439 const char** arg_names, | 465 const char** arg_names, |
440 const unsigned char* arg_types, | 466 const unsigned char* arg_types, |
441 const unsigned long long* arg_values, | 467 const unsigned long long* arg_values, |
442 const scoped_refptr<ConvertableToTraceFormat>* convertable_values, | 468 const scoped_refptr<ConvertableToTraceFormat>* convertable_values, |
443 unsigned char flags); | 469 unsigned int flags); |
444 static void AddTraceEventEtw(char phase, | 470 static void AddTraceEventEtw(char phase, |
445 const char* category_group, | 471 const char* category_group, |
446 const void* id, | 472 const void* id, |
447 const char* extra); | 473 const char* extra); |
448 static void AddTraceEventEtw(char phase, | 474 static void AddTraceEventEtw(char phase, |
449 const char* category_group, | 475 const char* category_group, |
450 const void* id, | 476 const void* id, |
451 const std::string& extra); | 477 const std::string& extra); |
452 | 478 |
453 void UpdateTraceEventDuration(const unsigned char* category_group_enabled, | 479 void UpdateTraceEventDuration(const unsigned char* category_group_enabled, |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
521 TraceBufferVectorReportFull); | 547 TraceBufferVectorReportFull); |
522 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, | 548 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, |
523 ConvertTraceConfigToInternalOptions); | 549 ConvertTraceConfigToInternalOptions); |
524 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, | 550 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, |
525 TraceRecordAsMuchAsPossibleMode); | 551 TraceRecordAsMuchAsPossibleMode); |
526 | 552 |
527 // This allows constructor and destructor to be private and usable only | 553 // This allows constructor and destructor to be private and usable only |
528 // by the Singleton class. | 554 // by the Singleton class. |
529 friend struct DefaultSingletonTraits<TraceLog>; | 555 friend struct DefaultSingletonTraits<TraceLog>; |
530 | 556 |
| 557 // MemoryDumpProvider implementation. |
| 558 bool OnMemoryDump(ProcessMemoryDump* pmd) override; |
| 559 |
531 // Enable/disable each category group based on the current mode_, | 560 // Enable/disable each category group based on the current mode_, |
532 // category_filter_, event_callback_ and event_callback_category_filter_. | 561 // category_filter_, event_callback_ and event_callback_category_filter_. |
533 // Enable the category group in the enabled mode if category_filter_ matches | 562 // Enable the category group in the enabled mode if category_filter_ matches |
534 // the category group, or event_callback_ is not null and | 563 // the category group, or event_callback_ is not null and |
535 // event_callback_category_filter_ matches the category group. | 564 // event_callback_category_filter_ matches the category group. |
536 void UpdateCategoryGroupEnabledFlags(); | 565 void UpdateCategoryGroupEnabledFlags(); |
537 void UpdateCategoryGroupEnabledFlag(size_t category_index); | 566 void UpdateCategoryGroupEnabledFlag(size_t category_index); |
538 | 567 |
539 // Configure synthetic delays based on the values set in the current | 568 // Configure synthetic delays based on the values set in the current |
540 // trace config. | 569 // trace config. |
541 void UpdateSyntheticDelaysFromTraceConfig(); | 570 void UpdateSyntheticDelaysFromTraceConfig(); |
542 | 571 |
543 InternalTraceOptions GetInternalOptionsFromTraceConfig( | 572 InternalTraceOptions GetInternalOptionsFromTraceConfig( |
544 const TraceConfig& config); | 573 const TraceConfig& config); |
545 | 574 |
546 class ThreadLocalEventBuffer; | 575 class ThreadLocalEventBuffer; |
547 class OptionalAutoLock; | 576 class OptionalAutoLock; |
548 | 577 |
549 TraceLog(); | 578 TraceLog(); |
550 ~TraceLog(); | 579 ~TraceLog() override; |
551 const unsigned char* GetCategoryGroupEnabledInternal(const char* name); | 580 const unsigned char* GetCategoryGroupEnabledInternal(const char* name); |
552 void AddMetadataEventsWhileLocked(); | 581 void AddMetadataEventsWhileLocked(); |
553 | 582 |
554 InternalTraceOptions trace_options() const { | 583 InternalTraceOptions trace_options() const { |
555 return static_cast<InternalTraceOptions>( | 584 return static_cast<InternalTraceOptions>( |
556 subtle::NoBarrier_Load(&trace_options_)); | 585 subtle::NoBarrier_Load(&trace_options_)); |
557 } | 586 } |
558 | 587 |
559 TraceBuffer* trace_buffer() const { return logged_events_.get(); } | 588 TraceBuffer* trace_buffer() const { return logged_events_.get(); } |
560 TraceBuffer* CreateTraceBuffer(); | 589 TraceBuffer* CreateTraceBuffer(); |
561 TraceBuffer* CreateTraceBufferVectorOfSize(size_t max_chunks); | 590 TraceBuffer* CreateTraceBufferVectorOfSize(size_t max_chunks); |
562 | 591 |
563 std::string EventToConsoleMessage(unsigned char phase, | 592 std::string EventToConsoleMessage(unsigned char phase, |
564 const TraceTicks& timestamp, | 593 const TraceTicks& timestamp, |
565 TraceEvent* trace_event); | 594 TraceEvent* trace_event); |
566 | 595 |
567 TraceEvent* AddEventToThreadSharedChunkWhileLocked(TraceEventHandle* handle, | 596 TraceEvent* AddEventToThreadSharedChunkWhileLocked(TraceEventHandle* handle, |
568 bool check_buffer_is_full); | 597 bool check_buffer_is_full); |
569 void CheckIfBufferIsFullWhileLocked(); | 598 void CheckIfBufferIsFullWhileLocked(); |
570 void SetDisabledWhileLocked(); | 599 void SetDisabledWhileLocked(); |
571 | 600 |
572 TraceEvent* GetEventByHandleInternal(TraceEventHandle handle, | 601 TraceEvent* GetEventByHandleInternal(TraceEventHandle handle, |
573 OptionalAutoLock* lock); | 602 OptionalAutoLock* lock); |
574 | 603 |
| 604 void FlushInternal(const OutputCallback& cb, |
| 605 bool use_worker_thread, |
| 606 bool discard_events); |
| 607 |
575 // |generation| is used in the following callbacks to check if the callback | 608 // |generation| is used in the following callbacks to check if the callback |
576 // is called for the flush of the current |logged_events_|. | 609 // is called for the flush of the current |logged_events_|. |
577 void FlushCurrentThread(int generation); | 610 void FlushCurrentThread(int generation, bool discard_events); |
578 // Usually it runs on a different thread. | 611 // Usually it runs on a different thread. |
579 static void ConvertTraceEventsToTraceFormat( | 612 static void ConvertTraceEventsToTraceFormat( |
580 scoped_ptr<TraceBuffer> logged_events, | 613 scoped_ptr<TraceBuffer> logged_events, |
581 const TraceLog::OutputCallback& flush_output_callback, | 614 const TraceLog::OutputCallback& flush_output_callback, |
582 const TraceEvent::ArgumentFilterPredicate& argument_filter_predicate); | 615 const TraceEvent::ArgumentFilterPredicate& argument_filter_predicate); |
583 void FinishFlush(int generation); | 616 void FinishFlush(int generation, bool discard_events); |
584 void OnFlushTimeout(int generation); | 617 void OnFlushTimeout(int generation, bool discard_events); |
585 | 618 |
586 int generation() const { | 619 int generation() const { |
587 return static_cast<int>(subtle::NoBarrier_Load(&generation_)); | 620 return static_cast<int>(subtle::NoBarrier_Load(&generation_)); |
588 } | 621 } |
589 bool CheckGeneration(int generation) const { | 622 bool CheckGeneration(int generation) const { |
590 return generation == this->generation(); | 623 return generation == this->generation(); |
591 } | 624 } |
592 void UseNextTraceBuffer(); | 625 void UseNextTraceBuffer(); |
593 | 626 |
594 TraceTicks OffsetNow() const { | 627 TraceTicks OffsetNow() const { |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
675 subtle::AtomicWord generation_; | 708 subtle::AtomicWord generation_; |
676 bool use_worker_thread_; | 709 bool use_worker_thread_; |
677 | 710 |
678 DISALLOW_COPY_AND_ASSIGN(TraceLog); | 711 DISALLOW_COPY_AND_ASSIGN(TraceLog); |
679 }; | 712 }; |
680 | 713 |
681 } // namespace trace_event | 714 } // namespace trace_event |
682 } // namespace base | 715 } // namespace base |
683 | 716 |
684 #endif // BASE_TRACE_EVENT_TRACE_EVENT_IMPL_H_ | 717 #endif // BASE_TRACE_EVENT_TRACE_EVENT_IMPL_H_ |
OLD | NEW |