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/trace_config.h" |
27 | 28 |
28 // Older style trace macros with explicit id and extra data | 29 // Older style trace macros with explicit id and extra data |
29 // Only these macros result in publishing data to ETW as currently implemented. | 30 // Only these macros result in publishing data to ETW as currently implemented. |
30 // TODO(georgesak): Update/replace these with new ETW macros. | 31 // TODO(georgesak): Update/replace these with new ETW macros. |
31 #define TRACE_EVENT_BEGIN_ETW(name, id, extra) \ | 32 #define TRACE_EVENT_BEGIN_ETW(name, id, extra) \ |
32 base::trace_event::TraceLog::AddTraceEventEtw( \ | 33 base::trace_event::TraceLog::AddTraceEventEtw( \ |
33 TRACE_EVENT_PHASE_BEGIN, \ | 34 TRACE_EVENT_PHASE_BEGIN, \ |
34 name, reinterpret_cast<const void*>(id), extra) | 35 name, reinterpret_cast<const void*>(id), extra) |
35 | 36 |
36 #define TRACE_EVENT_END_ETW(name, id, extra) \ | 37 #define TRACE_EVENT_END_ETW(name, id, extra) \ |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 | 99 |
99 TraceEvent(); | 100 TraceEvent(); |
100 ~TraceEvent(); | 101 ~TraceEvent(); |
101 | 102 |
102 // We don't need to copy TraceEvent except when TraceEventBuffer is cloned. | 103 // We don't need to copy TraceEvent except when TraceEventBuffer is cloned. |
103 // Use explicit copy method to avoid accidentally misuse of copy. | 104 // Use explicit copy method to avoid accidentally misuse of copy. |
104 void CopyFrom(const TraceEvent& other); | 105 void CopyFrom(const TraceEvent& other); |
105 | 106 |
106 void Initialize( | 107 void Initialize( |
107 int thread_id, | 108 int thread_id, |
108 TimeTicks timestamp, | 109 TraceTicks timestamp, |
109 TimeTicks thread_timestamp, | 110 ThreadTicks thread_timestamp, |
110 char phase, | 111 char phase, |
111 const unsigned char* category_group_enabled, | 112 const unsigned char* category_group_enabled, |
112 const char* name, | 113 const char* name, |
113 unsigned long long id, | 114 unsigned long long id, |
114 int num_args, | 115 int num_args, |
115 const char** arg_names, | 116 const char** arg_names, |
116 const unsigned char* arg_types, | 117 const unsigned char* arg_types, |
117 const unsigned long long* arg_values, | 118 const unsigned long long* arg_values, |
118 const scoped_refptr<ConvertableToTraceFormat>* convertable_values, | 119 const scoped_refptr<ConvertableToTraceFormat>* convertable_values, |
119 unsigned char flags); | 120 unsigned char flags); |
120 | 121 |
121 void Reset(); | 122 void Reset(); |
122 | 123 |
123 void UpdateDuration(const TimeTicks& now, const TimeTicks& thread_now); | 124 void UpdateDuration(const TraceTicks& now, const ThreadTicks& thread_now); |
124 | 125 |
125 // Serialize event data to JSON | 126 // Serialize event data to JSON |
126 void AppendAsJSON(std::string* out) const; | 127 typedef base::Callback<bool(const char* category_group_name, |
| 128 const char* event_name)> ArgumentFilterPredicate; |
| 129 void AppendAsJSON( |
| 130 std::string* out, |
| 131 const ArgumentFilterPredicate& argument_filter_predicate) const; |
127 void AppendPrettyPrinted(std::ostringstream* out) const; | 132 void AppendPrettyPrinted(std::ostringstream* out) const; |
128 | 133 |
129 static void AppendValueAsJSON(unsigned char type, | 134 static void AppendValueAsJSON(unsigned char type, |
130 TraceValue value, | 135 TraceValue value, |
131 std::string* out); | 136 std::string* out); |
132 | 137 |
133 TimeTicks timestamp() const { return timestamp_; } | 138 TraceTicks timestamp() const { return timestamp_; } |
134 TimeTicks thread_timestamp() const { return thread_timestamp_; } | 139 ThreadTicks thread_timestamp() const { return thread_timestamp_; } |
135 char phase() const { return phase_; } | 140 char phase() const { return phase_; } |
136 int thread_id() const { return thread_id_; } | 141 int thread_id() const { return thread_id_; } |
137 TimeDelta duration() const { return duration_; } | 142 TimeDelta duration() const { return duration_; } |
138 TimeDelta thread_duration() const { return thread_duration_; } | 143 TimeDelta thread_duration() const { return thread_duration_; } |
139 unsigned long long id() const { return id_; } | 144 unsigned long long id() const { return id_; } |
140 unsigned char flags() const { return flags_; } | 145 unsigned char flags() const { return flags_; } |
141 | 146 |
142 // Exposed for unittesting: | 147 // Exposed for unittesting: |
143 | 148 |
144 const base::RefCountedString* parameter_copy_storage() const { | 149 const base::RefCountedString* parameter_copy_storage() const { |
145 return parameter_copy_storage_.get(); | 150 return parameter_copy_storage_.get(); |
146 } | 151 } |
147 | 152 |
148 const unsigned char* category_group_enabled() const { | 153 const unsigned char* category_group_enabled() const { |
149 return category_group_enabled_; | 154 return category_group_enabled_; |
150 } | 155 } |
151 | 156 |
152 const char* name() const { return name_; } | 157 const char* name() const { return name_; } |
153 | 158 |
154 #if defined(OS_ANDROID) | 159 #if defined(OS_ANDROID) |
155 void SendToATrace(); | 160 void SendToATrace(); |
156 #endif | 161 #endif |
157 | 162 |
158 private: | 163 private: |
159 // Note: these are ordered by size (largest first) for optimal packing. | 164 // Note: these are ordered by size (largest first) for optimal packing. |
160 TimeTicks timestamp_; | 165 TraceTicks timestamp_; |
161 TimeTicks thread_timestamp_; | 166 ThreadTicks thread_timestamp_; |
162 TimeDelta duration_; | 167 TimeDelta duration_; |
163 TimeDelta thread_duration_; | 168 TimeDelta thread_duration_; |
164 // id_ can be used to store phase-specific data. | 169 // id_ can be used to store phase-specific data. |
165 unsigned long long id_; | 170 unsigned long long id_; |
166 TraceValue arg_values_[kTraceMaxNumArgs]; | 171 TraceValue arg_values_[kTraceMaxNumArgs]; |
167 const char* arg_names_[kTraceMaxNumArgs]; | 172 const char* arg_names_[kTraceMaxNumArgs]; |
168 scoped_refptr<ConvertableToTraceFormat> convertable_values_[kTraceMaxNumArgs]; | 173 scoped_refptr<ConvertableToTraceFormat> convertable_values_[kTraceMaxNumArgs]; |
169 const unsigned char* category_group_enabled_; | 174 const unsigned char* category_group_enabled_; |
170 const char* name_; | 175 const char* name_; |
171 scoped_refptr<base::RefCountedString> parameter_copy_storage_; | 176 scoped_refptr<base::RefCountedString> parameter_copy_storage_; |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 | 273 |
269 // When all fragments have been added, call Finish to complete the JSON | 274 // When all fragments have been added, call Finish to complete the JSON |
270 // formatted output. | 275 // formatted output. |
271 void Finish(); | 276 void Finish(); |
272 | 277 |
273 private: | 278 private: |
274 OutputCallback output_callback_; | 279 OutputCallback output_callback_; |
275 bool append_comma_; | 280 bool append_comma_; |
276 }; | 281 }; |
277 | 282 |
278 class BASE_EXPORT CategoryFilter { | |
279 public: | |
280 typedef std::vector<std::string> StringList; | |
281 | |
282 // The default category filter, used when none is provided. | |
283 // Allows all categories through, except if they end in the suffix 'Debug' or | |
284 // 'Test'. | |
285 static const char kDefaultCategoryFilterString[]; | |
286 | |
287 // |filter_string| is a comma-delimited list of category wildcards. | |
288 // A category can have an optional '-' prefix to make it an excluded category. | |
289 // All the same rules apply above, so for example, having both included and | |
290 // excluded categories in the same list would not be supported. | |
291 // | |
292 // Example: CategoryFilter"test_MyTest*"); | |
293 // Example: CategoryFilter("test_MyTest*,test_OtherStuff"); | |
294 // Example: CategoryFilter("-excluded_category1,-excluded_category2"); | |
295 // Example: CategoryFilter("-*,webkit"); would disable everything but webkit. | |
296 // Example: CategoryFilter("-webkit"); would enable everything but webkit. | |
297 // | |
298 // Category filters can also be used to configure synthetic delays. | |
299 // | |
300 // Example: CategoryFilter("DELAY(gpu.PresentingFrame;16)"); would make swap | |
301 // buffers always take at least 16 ms. | |
302 // Example: CategoryFilter("DELAY(gpu.PresentingFrame;16;oneshot)"); would | |
303 // make swap buffers take at least 16 ms the first time it is | |
304 // called. | |
305 // Example: CategoryFilter("DELAY(gpu.PresentingFrame;16;alternating)"); | |
306 // would make swap buffers take at least 16 ms every other time it | |
307 // is called. | |
308 explicit CategoryFilter(const std::string& filter_string); | |
309 | |
310 CategoryFilter(); | |
311 | |
312 CategoryFilter(const CategoryFilter& cf); | |
313 | |
314 ~CategoryFilter(); | |
315 | |
316 CategoryFilter& operator=(const CategoryFilter& rhs); | |
317 | |
318 // Writes the string representation of the CategoryFilter. This is a comma | |
319 // separated string, similar in nature to the one used to determine | |
320 // enabled/disabled category patterns, except here there is an arbitrary | |
321 // order, included categories go first, then excluded categories. Excluded | |
322 // categories are distinguished from included categories by the prefix '-'. | |
323 std::string ToString() const; | |
324 | |
325 // Returns true if at least one category in the list is enabled by this | |
326 // category filter. | |
327 bool IsCategoryGroupEnabled(const char* category_group) const; | |
328 | |
329 // Return a list of the synthetic delays specified in this category filter. | |
330 const StringList& GetSyntheticDelayValues() const; | |
331 | |
332 // Merges nested_filter with the current CategoryFilter | |
333 void Merge(const CategoryFilter& nested_filter); | |
334 | |
335 // Clears both included/excluded pattern lists. This would be equivalent to | |
336 // creating a CategoryFilter with an empty string, through the constructor. | |
337 // i.e: CategoryFilter(). | |
338 // | |
339 // When using an empty filter, all categories are considered included as we | |
340 // are not excluding anything. | |
341 void Clear(); | |
342 | |
343 private: | |
344 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, CategoryFilter); | |
345 | |
346 // Returns true if category is enable according to this filter. | |
347 bool IsCategoryEnabled(const char* category_name) const; | |
348 | |
349 static bool IsEmptyOrContainsLeadingOrTrailingWhitespace( | |
350 const std::string& str); | |
351 | |
352 void Initialize(const std::string& filter_string); | |
353 void WriteString(const StringList& values, | |
354 std::string* out, | |
355 bool included) const; | |
356 void WriteString(const StringList& delays, std::string* out) const; | |
357 bool HasIncludedPatterns() const; | |
358 | |
359 StringList included_; | |
360 StringList disabled_; | |
361 StringList excluded_; | |
362 StringList delays_; | |
363 }; | |
364 | |
365 class TraceSamplingThread; | 283 class TraceSamplingThread; |
366 | 284 |
367 // Options determines how the trace buffer stores data. | |
368 enum TraceRecordMode { | |
369 // Record until the trace buffer is full. | |
370 RECORD_UNTIL_FULL, | |
371 | |
372 // Record until the user ends the trace. The trace buffer is a fixed size | |
373 // and we use it as a ring buffer during recording. | |
374 RECORD_CONTINUOUSLY, | |
375 | |
376 // Echo to console. Events are discarded. | |
377 ECHO_TO_CONSOLE, | |
378 | |
379 // Record until the trace buffer is full, but with a huge buffer size. | |
380 RECORD_AS_MUCH_AS_POSSIBLE | |
381 }; | |
382 | |
383 struct BASE_EXPORT TraceOptions { | |
384 TraceOptions() | |
385 : record_mode(RECORD_UNTIL_FULL), | |
386 enable_sampling(false), | |
387 enable_systrace(false) {} | |
388 | |
389 explicit TraceOptions(TraceRecordMode record_mode) | |
390 : record_mode(record_mode), | |
391 enable_sampling(false), | |
392 enable_systrace(false) {} | |
393 | |
394 // |options_string| is a comma-delimited list of trace options. | |
395 // Possible options are: "record-until-full", "record-continuously", | |
396 // "trace-to-console", "enable-sampling" and "enable-systrace". | |
397 // The first 3 options are trace recoding modes and hence | |
398 // mutually exclusive. If more than one trace recording modes appear in the | |
399 // options_string, the last one takes precedence. If none of the trace | |
400 // recording mode is specified, recording mode is RECORD_UNTIL_FULL. | |
401 // | |
402 // The trace option will first be reset to the default option | |
403 // (record_mode set to RECORD_UNTIL_FULL, enable_sampling and enable_systrace | |
404 // set to false) before options parsed from |options_string| are applied on | |
405 // it. | |
406 // If |options_string| is invalid, the final state of trace_options is | |
407 // undefined. | |
408 // | |
409 // Example: trace_options.SetFromString("record-until-full") | |
410 // Example: trace_options.SetFromString( | |
411 // "record-continuously, enable-sampling") | |
412 // Example: trace_options.SetFromString("record-until-full, trace-to-console") | |
413 // will set ECHO_TO_CONSOLE as the recording mode. | |
414 // | |
415 // Returns true on success. | |
416 bool SetFromString(const std::string& options_string); | |
417 | |
418 std::string ToString() const; | |
419 | |
420 TraceRecordMode record_mode; | |
421 bool enable_sampling; | |
422 bool enable_systrace; | |
423 }; | |
424 | |
425 struct BASE_EXPORT TraceLogStatus { | 285 struct BASE_EXPORT TraceLogStatus { |
426 TraceLogStatus(); | 286 TraceLogStatus(); |
427 ~TraceLogStatus(); | 287 ~TraceLogStatus(); |
428 size_t event_capacity; | 288 size_t event_capacity; |
429 size_t event_count; | 289 size_t event_count; |
430 }; | 290 }; |
431 | 291 |
432 class BASE_EXPORT TraceLog { | 292 class BASE_EXPORT TraceLog { |
433 public: | 293 public: |
434 enum Mode { | 294 enum Mode { |
(...skipping 16 matching lines...) Expand all Loading... |
451 // Category group enabled to export events to ETW. | 311 // Category group enabled to export events to ETW. |
452 ENABLED_FOR_ETW_EXPORT = 1 << 3 | 312 ENABLED_FOR_ETW_EXPORT = 1 << 3 |
453 }; | 313 }; |
454 | 314 |
455 static TraceLog* GetInstance(); | 315 static TraceLog* GetInstance(); |
456 | 316 |
457 // Get set of known category groups. This can change as new code paths are | 317 // Get set of known category groups. This can change as new code paths are |
458 // reached. The known category groups are inserted into |category_groups|. | 318 // reached. The known category groups are inserted into |category_groups|. |
459 void GetKnownCategoryGroups(std::vector<std::string>* category_groups); | 319 void GetKnownCategoryGroups(std::vector<std::string>* category_groups); |
460 | 320 |
461 // Retrieves a copy (for thread-safety) of the current CategoryFilter. | 321 // Retrieves a copy (for thread-safety) of the current TraceConfig. |
462 CategoryFilter GetCurrentCategoryFilter(); | 322 TraceConfig GetCurrentTraceConfig() const; |
463 | |
464 // Retrieves a copy (for thread-safety) of the current TraceOptions. | |
465 TraceOptions GetCurrentTraceOptions() const; | |
466 | 323 |
467 // Enables normal tracing (recording trace events in the trace buffer). | 324 // Enables normal tracing (recording trace events in the trace buffer). |
468 // See CategoryFilter comments for details on how to control what categories | 325 // See TraceConfig comments for details on how to control what categories |
469 // will be traced. If tracing has already been enabled, |category_filter| will | 326 // will be traced. If tracing has already been enabled, |category_filter| will |
470 // be merged into the current category filter. | 327 // be merged into the current category filter. |
471 void SetEnabled(const CategoryFilter& category_filter, | 328 void SetEnabled(const TraceConfig& trace_config, Mode mode); |
472 Mode mode, const TraceOptions& options); | |
473 | 329 |
474 // Disables normal tracing for all categories. | 330 // Disables normal tracing for all categories. |
475 void SetDisabled(); | 331 void SetDisabled(); |
476 | 332 |
477 bool IsEnabled() { return mode_ != DISABLED; } | 333 bool IsEnabled() { return mode_ != DISABLED; } |
478 | 334 |
479 // The number of times we have begun recording traces. If tracing is off, | 335 // The number of times we have begun recording traces. If tracing is off, |
480 // returns -1. If tracing is on, then it returns the number of times we have | 336 // returns -1. If tracing is on, then it returns the number of times we have |
481 // recorded a trace. By watching for this number to increment, you can | 337 // recorded a trace. By watching for this number to increment, you can |
482 // passively discover when a new trace has begun. This is then used to | 338 // passively discover when a new trace has begun. This is then used to |
(...skipping 28 matching lines...) Expand all Loading... |
511 | 367 |
512 // Not using base::Callback because of its limited by 7 parameters. | 368 // Not using base::Callback because of its limited by 7 parameters. |
513 // Also, using primitive type allows directly passing callback from WebCore. | 369 // Also, using primitive type allows directly passing callback from WebCore. |
514 // WARNING: It is possible for the previously set callback to be called | 370 // WARNING: It is possible for the previously set callback to be called |
515 // after a call to SetEventCallbackEnabled() that replaces or a call to | 371 // after a call to SetEventCallbackEnabled() that replaces or a call to |
516 // SetEventCallbackDisabled() that disables the callback. | 372 // SetEventCallbackDisabled() that disables the callback. |
517 // This callback may be invoked on any thread. | 373 // This callback may be invoked on any thread. |
518 // For TRACE_EVENT_PHASE_COMPLETE events, the client will still receive pairs | 374 // For TRACE_EVENT_PHASE_COMPLETE events, the client will still receive pairs |
519 // of TRACE_EVENT_PHASE_BEGIN and TRACE_EVENT_PHASE_END events to keep the | 375 // of TRACE_EVENT_PHASE_BEGIN and TRACE_EVENT_PHASE_END events to keep the |
520 // interface simple. | 376 // interface simple. |
521 typedef void (*EventCallback)(TimeTicks timestamp, | 377 typedef void (*EventCallback)(TraceTicks timestamp, |
522 char phase, | 378 char phase, |
523 const unsigned char* category_group_enabled, | 379 const unsigned char* category_group_enabled, |
524 const char* name, | 380 const char* name, |
525 unsigned long long id, | 381 unsigned long long id, |
526 int num_args, | 382 int num_args, |
527 const char* const arg_names[], | 383 const char* const arg_names[], |
528 const unsigned char arg_types[], | 384 const unsigned char arg_types[], |
529 const unsigned long long arg_values[], | 385 const unsigned long long arg_values[], |
530 unsigned char flags); | 386 unsigned char flags); |
531 | 387 |
532 // Enable tracing for EventCallback. | 388 // Enable tracing for EventCallback. |
533 void SetEventCallbackEnabled(const CategoryFilter& category_filter, | 389 void SetEventCallbackEnabled(const TraceConfig& trace_config, |
534 EventCallback cb); | 390 EventCallback cb); |
535 void SetEventCallbackDisabled(); | 391 void SetEventCallbackDisabled(); |
| 392 void SetArgumentFilterPredicate( |
| 393 const TraceEvent::ArgumentFilterPredicate& argument_filter_predicate); |
536 | 394 |
537 // Flush all collected events to the given output callback. The callback will | 395 // Flush all collected events to the given output callback. The callback will |
538 // be called one or more times either synchronously or asynchronously from | 396 // be called one or more times either synchronously or asynchronously from |
539 // the current thread with IPC-bite-size chunks. The string format is | 397 // the current thread with IPC-bite-size chunks. The string format is |
540 // undefined. Use TraceResultBuffer to convert one or more trace strings to | 398 // undefined. Use TraceResultBuffer to convert one or more trace strings to |
541 // JSON. The callback can be null if the caller doesn't want any data. | 399 // JSON. The callback can be null if the caller doesn't want any data. |
542 // Due to the implementation of thread-local buffers, flush can't be | 400 // Due to the implementation of thread-local buffers, flush can't be |
543 // done when tracing is enabled. If called when tracing is enabled, the | 401 // done when tracing is enabled. If called when tracing is enabled, the |
544 // callback will be called directly with (empty_string, false) to indicate | 402 // callback will be called directly with (empty_string, false) to indicate |
545 // the end of this unsuccessful flush. Flush does the serialization | 403 // the end of this unsuccessful flush. Flush does the serialization |
(...skipping 23 matching lines...) Expand all Loading... |
569 const unsigned char* arg_types, | 427 const unsigned char* arg_types, |
570 const unsigned long long* arg_values, | 428 const unsigned long long* arg_values, |
571 const scoped_refptr<ConvertableToTraceFormat>* convertable_values, | 429 const scoped_refptr<ConvertableToTraceFormat>* convertable_values, |
572 unsigned char flags); | 430 unsigned char flags); |
573 TraceEventHandle AddTraceEventWithThreadIdAndTimestamp( | 431 TraceEventHandle AddTraceEventWithThreadIdAndTimestamp( |
574 char phase, | 432 char phase, |
575 const unsigned char* category_group_enabled, | 433 const unsigned char* category_group_enabled, |
576 const char* name, | 434 const char* name, |
577 unsigned long long id, | 435 unsigned long long id, |
578 int thread_id, | 436 int thread_id, |
579 const TimeTicks& timestamp, | 437 const TraceTicks& timestamp, |
580 int num_args, | 438 int num_args, |
581 const char** arg_names, | 439 const char** arg_names, |
582 const unsigned char* arg_types, | 440 const unsigned char* arg_types, |
583 const unsigned long long* arg_values, | 441 const unsigned long long* arg_values, |
584 const scoped_refptr<ConvertableToTraceFormat>* convertable_values, | 442 const scoped_refptr<ConvertableToTraceFormat>* convertable_values, |
585 unsigned char flags); | 443 unsigned char flags); |
586 static void AddTraceEventEtw(char phase, | 444 static void AddTraceEventEtw(char phase, |
587 const char* category_group, | 445 const char* category_group, |
588 const void* id, | 446 const void* id, |
589 const char* extra); | 447 const char* extra); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
632 // Processes can have labels in addition to their names. Use labels, for | 490 // Processes can have labels in addition to their names. Use labels, for |
633 // instance, to list out the web page titles that a process is handling. | 491 // instance, to list out the web page titles that a process is handling. |
634 void UpdateProcessLabel(int label_id, const std::string& current_label); | 492 void UpdateProcessLabel(int label_id, const std::string& current_label); |
635 void RemoveProcessLabel(int label_id); | 493 void RemoveProcessLabel(int label_id); |
636 | 494 |
637 // Thread sort indices, if set, override the order of a thread will appear | 495 // Thread sort indices, if set, override the order of a thread will appear |
638 // within its process in the trace viewer. Threads are sorted first on their | 496 // within its process in the trace viewer. Threads are sorted first on their |
639 // sort index, ascending, then by their name, and then tid. | 497 // sort index, ascending, then by their name, and then tid. |
640 void SetThreadSortIndex(PlatformThreadId , int sort_index); | 498 void SetThreadSortIndex(PlatformThreadId , int sort_index); |
641 | 499 |
642 // Allow setting an offset between the current TimeTicks time and the time | 500 // Allow setting an offset between the current TraceTicks time and the time |
643 // that should be reported. | 501 // that should be reported. |
644 void SetTimeOffset(TimeDelta offset); | 502 void SetTimeOffset(TimeDelta offset); |
645 | 503 |
646 size_t GetObserverCountForTest() const; | 504 size_t GetObserverCountForTest() const; |
647 | 505 |
648 // Call this method if the current thread may block the message loop to | 506 // Call this method if the current thread may block the message loop to |
649 // prevent the thread from using the thread-local buffer because the thread | 507 // prevent the thread from using the thread-local buffer because the thread |
650 // may not handle the flush request in time causing lost of unflushed events. | 508 // may not handle the flush request in time causing lost of unflushed events. |
651 void SetCurrentThreadBlocksMessageLoop(); | 509 void SetCurrentThreadBlocksMessageLoop(); |
652 | 510 |
653 private: | 511 private: |
654 typedef unsigned int InternalTraceOptions; | 512 typedef unsigned int InternalTraceOptions; |
655 | 513 |
656 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, | 514 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, |
657 TraceBufferRingBufferGetReturnChunk); | 515 TraceBufferRingBufferGetReturnChunk); |
658 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, | 516 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, |
659 TraceBufferRingBufferHalfIteration); | 517 TraceBufferRingBufferHalfIteration); |
660 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, | 518 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, |
661 TraceBufferRingBufferFullIteration); | 519 TraceBufferRingBufferFullIteration); |
662 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, | 520 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, |
663 TraceBufferVectorReportFull); | 521 TraceBufferVectorReportFull); |
664 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, | 522 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, |
665 ConvertTraceOptionsToInternalOptions); | 523 ConvertTraceConfigToInternalOptions); |
666 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, | 524 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, |
667 TraceRecordAsMuchAsPossibleMode); | 525 TraceRecordAsMuchAsPossibleMode); |
668 | 526 |
669 // This allows constructor and destructor to be private and usable only | 527 // This allows constructor and destructor to be private and usable only |
670 // by the Singleton class. | 528 // by the Singleton class. |
671 friend struct DefaultSingletonTraits<TraceLog>; | 529 friend struct DefaultSingletonTraits<TraceLog>; |
672 | 530 |
673 // Enable/disable each category group based on the current mode_, | 531 // Enable/disable each category group based on the current mode_, |
674 // category_filter_, event_callback_ and event_callback_category_filter_. | 532 // category_filter_, event_callback_ and event_callback_category_filter_. |
675 // Enable the category group in the enabled mode if category_filter_ matches | 533 // Enable the category group in the enabled mode if category_filter_ matches |
676 // the category group, or event_callback_ is not null and | 534 // the category group, or event_callback_ is not null and |
677 // event_callback_category_filter_ matches the category group. | 535 // event_callback_category_filter_ matches the category group. |
678 void UpdateCategoryGroupEnabledFlags(); | 536 void UpdateCategoryGroupEnabledFlags(); |
679 void UpdateCategoryGroupEnabledFlag(size_t category_index); | 537 void UpdateCategoryGroupEnabledFlag(size_t category_index); |
680 | 538 |
681 // Configure synthetic delays based on the values set in the current | 539 // Configure synthetic delays based on the values set in the current |
682 // category filter. | 540 // trace config. |
683 void UpdateSyntheticDelaysFromCategoryFilter(); | 541 void UpdateSyntheticDelaysFromTraceConfig(); |
684 | 542 |
685 InternalTraceOptions GetInternalOptionsFromTraceOptions( | 543 InternalTraceOptions GetInternalOptionsFromTraceConfig( |
686 const TraceOptions& options); | 544 const TraceConfig& config); |
687 | 545 |
688 class ThreadLocalEventBuffer; | 546 class ThreadLocalEventBuffer; |
689 class OptionalAutoLock; | 547 class OptionalAutoLock; |
690 | 548 |
691 TraceLog(); | 549 TraceLog(); |
692 ~TraceLog(); | 550 ~TraceLog(); |
693 const unsigned char* GetCategoryGroupEnabledInternal(const char* name); | 551 const unsigned char* GetCategoryGroupEnabledInternal(const char* name); |
694 void AddMetadataEventsWhileLocked(); | 552 void AddMetadataEventsWhileLocked(); |
695 | 553 |
696 InternalTraceOptions trace_options() const { | 554 InternalTraceOptions trace_options() const { |
697 return static_cast<InternalTraceOptions>( | 555 return static_cast<InternalTraceOptions>( |
698 subtle::NoBarrier_Load(&trace_options_)); | 556 subtle::NoBarrier_Load(&trace_options_)); |
699 } | 557 } |
700 | 558 |
701 TraceBuffer* trace_buffer() const { return logged_events_.get(); } | 559 TraceBuffer* trace_buffer() const { return logged_events_.get(); } |
702 TraceBuffer* CreateTraceBuffer(); | 560 TraceBuffer* CreateTraceBuffer(); |
703 TraceBuffer* CreateTraceBufferVectorOfSize(size_t max_chunks); | 561 TraceBuffer* CreateTraceBufferVectorOfSize(size_t max_chunks); |
704 | 562 |
705 std::string EventToConsoleMessage(unsigned char phase, | 563 std::string EventToConsoleMessage(unsigned char phase, |
706 const TimeTicks& timestamp, | 564 const TraceTicks& timestamp, |
707 TraceEvent* trace_event); | 565 TraceEvent* trace_event); |
708 | 566 |
709 TraceEvent* AddEventToThreadSharedChunkWhileLocked(TraceEventHandle* handle, | 567 TraceEvent* AddEventToThreadSharedChunkWhileLocked(TraceEventHandle* handle, |
710 bool check_buffer_is_full); | 568 bool check_buffer_is_full); |
711 void CheckIfBufferIsFullWhileLocked(); | 569 void CheckIfBufferIsFullWhileLocked(); |
712 void SetDisabledWhileLocked(); | 570 void SetDisabledWhileLocked(); |
713 | 571 |
714 TraceEvent* GetEventByHandleInternal(TraceEventHandle handle, | 572 TraceEvent* GetEventByHandleInternal(TraceEventHandle handle, |
715 OptionalAutoLock* lock); | 573 OptionalAutoLock* lock); |
716 | 574 |
717 // |generation| is used in the following callbacks to check if the callback | 575 // |generation| is used in the following callbacks to check if the callback |
718 // is called for the flush of the current |logged_events_|. | 576 // is called for the flush of the current |logged_events_|. |
719 void FlushCurrentThread(int generation); | 577 void FlushCurrentThread(int generation); |
720 // Usually it runs on a different thread. | 578 // Usually it runs on a different thread. |
721 static void ConvertTraceEventsToTraceFormat( | 579 static void ConvertTraceEventsToTraceFormat( |
722 scoped_ptr<TraceBuffer> logged_events, | 580 scoped_ptr<TraceBuffer> logged_events, |
723 const TraceLog::OutputCallback& flush_output_callback); | 581 const TraceLog::OutputCallback& flush_output_callback, |
| 582 const TraceEvent::ArgumentFilterPredicate& argument_filter_predicate); |
724 void FinishFlush(int generation); | 583 void FinishFlush(int generation); |
725 void OnFlushTimeout(int generation); | 584 void OnFlushTimeout(int generation); |
726 | 585 |
727 int generation() const { | 586 int generation() const { |
728 return static_cast<int>(subtle::NoBarrier_Load(&generation_)); | 587 return static_cast<int>(subtle::NoBarrier_Load(&generation_)); |
729 } | 588 } |
730 bool CheckGeneration(int generation) const { | 589 bool CheckGeneration(int generation) const { |
731 return generation == this->generation(); | 590 return generation == this->generation(); |
732 } | 591 } |
733 void UseNextTraceBuffer(); | 592 void UseNextTraceBuffer(); |
734 | 593 |
735 TimeTicks OffsetNow() const { | 594 TraceTicks OffsetNow() const { |
736 return OffsetTimestamp(TimeTicks::NowFromSystemTraceTime()); | 595 return OffsetTimestamp(TraceTicks::Now()); |
737 } | 596 } |
738 TimeTicks OffsetTimestamp(const TimeTicks& timestamp) const { | 597 TraceTicks OffsetTimestamp(const TraceTicks& timestamp) const { |
739 return timestamp - time_offset_; | 598 return timestamp - time_offset_; |
740 } | 599 } |
741 | 600 |
742 // Internal representation of trace options since we store the currently used | 601 // Internal representation of trace options since we store the currently used |
743 // trace option as an AtomicWord. | 602 // trace option as an AtomicWord. |
744 static const InternalTraceOptions kInternalNone; | 603 static const InternalTraceOptions kInternalNone; |
745 static const InternalTraceOptions kInternalRecordUntilFull; | 604 static const InternalTraceOptions kInternalRecordUntilFull; |
746 static const InternalTraceOptions kInternalRecordContinuously; | 605 static const InternalTraceOptions kInternalRecordContinuously; |
747 static const InternalTraceOptions kInternalEchoToConsole; | 606 static const InternalTraceOptions kInternalEchoToConsole; |
748 static const InternalTraceOptions kInternalEnableSampling; | 607 static const InternalTraceOptions kInternalEnableSampling; |
749 static const InternalTraceOptions kInternalRecordAsMuchAsPossible; | 608 static const InternalTraceOptions kInternalRecordAsMuchAsPossible; |
| 609 static const InternalTraceOptions kInternalEnableArgumentFilter; |
750 | 610 |
751 // This lock protects TraceLog member accesses (except for members protected | 611 // This lock protects TraceLog member accesses (except for members protected |
752 // by thread_info_lock_) from arbitrary threads. | 612 // by thread_info_lock_) from arbitrary threads. |
753 mutable Lock lock_; | 613 mutable Lock lock_; |
754 // This lock protects accesses to thread_names_, thread_event_start_times_ | 614 // This lock protects accesses to thread_names_, thread_event_start_times_ |
755 // and thread_colors_. | 615 // and thread_colors_. |
756 Lock thread_info_lock_; | 616 Lock thread_info_lock_; |
757 Mode mode_; | 617 Mode mode_; |
758 int num_traces_recorded_; | 618 int num_traces_recorded_; |
759 scoped_ptr<TraceBuffer> logged_events_; | 619 scoped_ptr<TraceBuffer> logged_events_; |
760 subtle::AtomicWord /* EventCallback */ event_callback_; | 620 subtle::AtomicWord /* EventCallback */ event_callback_; |
761 bool dispatching_to_observer_list_; | 621 bool dispatching_to_observer_list_; |
762 std::vector<EnabledStateObserver*> enabled_state_observer_list_; | 622 std::vector<EnabledStateObserver*> enabled_state_observer_list_; |
763 | 623 |
764 std::string process_name_; | 624 std::string process_name_; |
765 base::hash_map<int, std::string> process_labels_; | 625 base::hash_map<int, std::string> process_labels_; |
766 int process_sort_index_; | 626 int process_sort_index_; |
767 base::hash_map<int, int> thread_sort_indices_; | 627 base::hash_map<int, int> thread_sort_indices_; |
768 base::hash_map<int, std::string> thread_names_; | 628 base::hash_map<int, std::string> thread_names_; |
769 | 629 |
770 // The following two maps are used only when ECHO_TO_CONSOLE. | 630 // The following two maps are used only when ECHO_TO_CONSOLE. |
771 base::hash_map<int, std::stack<TimeTicks> > thread_event_start_times_; | 631 base::hash_map<int, std::stack<TraceTicks> > thread_event_start_times_; |
772 base::hash_map<std::string, int> thread_colors_; | 632 base::hash_map<std::string, int> thread_colors_; |
773 | 633 |
774 TimeTicks buffer_limit_reached_timestamp_; | 634 TraceTicks buffer_limit_reached_timestamp_; |
775 | 635 |
776 // XORed with TraceID to make it unlikely to collide with other processes. | 636 // XORed with TraceID to make it unlikely to collide with other processes. |
777 unsigned long long process_id_hash_; | 637 unsigned long long process_id_hash_; |
778 | 638 |
779 int process_id_; | 639 int process_id_; |
780 | 640 |
781 TimeDelta time_offset_; | 641 TimeDelta time_offset_; |
782 | 642 |
783 // Allow tests to wake up when certain events occur. | 643 // Allow tests to wake up when certain events occur. |
784 WatchEventCallback watch_event_callback_; | 644 WatchEventCallback watch_event_callback_; |
785 subtle::AtomicWord /* const unsigned char* */ watch_category_; | 645 subtle::AtomicWord /* const unsigned char* */ watch_category_; |
786 std::string watch_event_name_; | 646 std::string watch_event_name_; |
787 | 647 |
788 subtle::AtomicWord /* Options */ trace_options_; | 648 subtle::AtomicWord /* Options */ trace_options_; |
789 | 649 |
790 // Sampling thread handles. | 650 // Sampling thread handles. |
791 scoped_ptr<TraceSamplingThread> sampling_thread_; | 651 scoped_ptr<TraceSamplingThread> sampling_thread_; |
792 PlatformThreadHandle sampling_thread_handle_; | 652 PlatformThreadHandle sampling_thread_handle_; |
793 | 653 |
794 CategoryFilter category_filter_; | 654 TraceConfig trace_config_; |
795 CategoryFilter event_callback_category_filter_; | 655 TraceConfig event_callback_trace_config_; |
796 | 656 |
797 ThreadLocalPointer<ThreadLocalEventBuffer> thread_local_event_buffer_; | 657 ThreadLocalPointer<ThreadLocalEventBuffer> thread_local_event_buffer_; |
798 ThreadLocalBoolean thread_blocks_message_loop_; | 658 ThreadLocalBoolean thread_blocks_message_loop_; |
799 ThreadLocalBoolean thread_is_in_trace_event_; | 659 ThreadLocalBoolean thread_is_in_trace_event_; |
800 | 660 |
801 // Contains the message loops of threads that have had at least one event | 661 // Contains the message loops of threads that have had at least one event |
802 // added into the local event buffer. Not using SingleThreadTaskRunner | 662 // added into the local event buffer. Not using SingleThreadTaskRunner |
803 // because we need to know the life time of the message loops. | 663 // because we need to know the life time of the message loops. |
804 hash_set<MessageLoop*> thread_message_loops_; | 664 hash_set<MessageLoop*> thread_message_loops_; |
805 | 665 |
806 // For events which can't be added into the thread local buffer, e.g. events | 666 // For events which can't be added into the thread local buffer, e.g. events |
807 // from threads without a message loop. | 667 // from threads without a message loop. |
808 scoped_ptr<TraceBufferChunk> thread_shared_chunk_; | 668 scoped_ptr<TraceBufferChunk> thread_shared_chunk_; |
809 size_t thread_shared_chunk_index_; | 669 size_t thread_shared_chunk_index_; |
810 | 670 |
811 // Set when asynchronous Flush is in progress. | 671 // Set when asynchronous Flush is in progress. |
812 OutputCallback flush_output_callback_; | 672 OutputCallback flush_output_callback_; |
813 scoped_refptr<SingleThreadTaskRunner> flush_task_runner_; | 673 scoped_refptr<SingleThreadTaskRunner> flush_task_runner_; |
| 674 TraceEvent::ArgumentFilterPredicate argument_filter_predicate_; |
814 subtle::AtomicWord generation_; | 675 subtle::AtomicWord generation_; |
815 bool use_worker_thread_; | 676 bool use_worker_thread_; |
816 | 677 |
817 DISALLOW_COPY_AND_ASSIGN(TraceLog); | 678 DISALLOW_COPY_AND_ASSIGN(TraceLog); |
818 }; | 679 }; |
819 | 680 |
820 } // namespace trace_event | 681 } // namespace trace_event |
821 } // namespace base | 682 } // namespace base |
822 | 683 |
823 #endif // BASE_TRACE_EVENT_TRACE_EVENT_IMPL_H_ | 684 #endif // BASE_TRACE_EVENT_TRACE_EVENT_IMPL_H_ |
OLD | NEW |