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_DEBUG_TRACE_EVENT_IMPL_H_ | 6 #ifndef BASE_DEBUG_TRACE_EVENT_IMPL_H_ |
7 #define BASE_DEBUG_TRACE_EVENT_IMPL_H_ | 7 #define BASE_DEBUG_TRACE_EVENT_IMPL_H_ |
8 | 8 |
9 #include <stack> | 9 #include <stack> |
10 #include <string> | 10 #include <string> |
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 // Example: CategoryFilter("DELAY(gpu.PresentingFrame;16)"); would make swap | 309 // Example: CategoryFilter("DELAY(gpu.PresentingFrame;16)"); would make swap |
310 // buffers always take at least 16 ms. | 310 // buffers always take at least 16 ms. |
311 // Example: CategoryFilter("DELAY(gpu.PresentingFrame;16;oneshot)"); would | 311 // Example: CategoryFilter("DELAY(gpu.PresentingFrame;16;oneshot)"); would |
312 // make swap buffers take at least 16 ms the first time it is | 312 // make swap buffers take at least 16 ms the first time it is |
313 // called. | 313 // called. |
314 // Example: CategoryFilter("DELAY(gpu.PresentingFrame;16;alternating)"); | 314 // Example: CategoryFilter("DELAY(gpu.PresentingFrame;16;alternating)"); |
315 // would make swap buffers take at least 16 ms every other time it | 315 // would make swap buffers take at least 16 ms every other time it |
316 // is called. | 316 // is called. |
317 explicit CategoryFilter(const std::string& filter_string); | 317 explicit CategoryFilter(const std::string& filter_string); |
318 | 318 |
| 319 CategoryFilter(); |
| 320 |
319 CategoryFilter(const CategoryFilter& cf); | 321 CategoryFilter(const CategoryFilter& cf); |
320 | 322 |
321 ~CategoryFilter(); | 323 ~CategoryFilter(); |
322 | 324 |
323 CategoryFilter& operator=(const CategoryFilter& rhs); | 325 CategoryFilter& operator=(const CategoryFilter& rhs); |
324 | 326 |
325 // Writes the string representation of the CategoryFilter. This is a comma | 327 // Writes the string representation of the CategoryFilter. This is a comma |
326 // separated string, similar in nature to the one used to determine | 328 // separated string, similar in nature to the one used to determine |
327 // enabled/disabled category patterns, except here there is an arbitrary | 329 // enabled/disabled category patterns, except here there is an arbitrary |
328 // order, included categories go first, then excluded categories. Excluded | 330 // order, included categories go first, then excluded categories. Excluded |
329 // categories are distinguished from included categories by the prefix '-'. | 331 // categories are distinguished from included categories by the prefix '-'. |
330 std::string ToString() const; | 332 std::string ToString() const; |
331 | 333 |
332 // Determines whether category group would be enabled or | 334 // Determines whether category group would be enabled or |
333 // disabled by this category filter. | 335 // disabled by this category filter. |
334 bool IsCategoryGroupEnabled(const char* category_group) const; | 336 bool IsCategoryGroupEnabled(const char* category_group) const; |
335 | 337 |
336 // Return a list of the synthetic delays specified in this category filter. | 338 // Return a list of the synthetic delays specified in this category filter. |
337 const StringList& GetSyntheticDelayValues() const; | 339 const StringList& GetSyntheticDelayValues() const; |
338 | 340 |
339 // Merges nested_filter with the current CategoryFilter | 341 // Merges nested_filter with the current CategoryFilter |
340 void Merge(const CategoryFilter& nested_filter); | 342 void Merge(const CategoryFilter& nested_filter); |
341 | 343 |
342 // Clears both included/excluded pattern lists. This would be equivalent to | 344 // Clears both included/excluded pattern lists. This would be equivalent to |
343 // creating a CategoryFilter with an empty string, through the constructor. | 345 // creating a CategoryFilter with an empty string, through the constructor. |
344 // i.e: CategoryFilter(""). | 346 // i.e: CategoryFilter(). |
345 // | 347 // |
346 // When using an empty filter, all categories are considered included as we | 348 // When using an empty filter, all categories are considered included as we |
347 // are not excluding anything. | 349 // are not excluding anything. |
348 void Clear(); | 350 void Clear(); |
349 | 351 |
350 private: | 352 private: |
351 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, CategoryFilter); | 353 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, CategoryFilter); |
352 | 354 |
353 static bool IsEmptyOrContainsLeadingOrTrailingWhitespace( | 355 static bool IsEmptyOrContainsLeadingOrTrailingWhitespace( |
354 const std::string& str); | 356 const std::string& str); |
355 | 357 |
356 void Initialize(const std::string& filter_string); | 358 void Initialize(const std::string& filter_string); |
357 void WriteString(const StringList& values, | 359 void WriteString(const StringList& values, |
358 std::string* out, | 360 std::string* out, |
359 bool included) const; | 361 bool included) const; |
360 void WriteString(const StringList& delays, std::string* out) const; | 362 void WriteString(const StringList& delays, std::string* out) const; |
361 bool HasIncludedPatterns() const; | 363 bool HasIncludedPatterns() const; |
362 | 364 |
363 bool DoesCategoryGroupContainCategory(const char* category_group, | 365 bool DoesCategoryGroupContainCategory(const char* category_group, |
364 const char* category) const; | 366 const char* category) const; |
365 | 367 |
366 StringList included_; | 368 StringList included_; |
367 StringList disabled_; | 369 StringList disabled_; |
368 StringList excluded_; | 370 StringList excluded_; |
369 StringList delays_; | 371 StringList delays_; |
370 }; | 372 }; |
371 | 373 |
372 class TraceSamplingThread; | 374 class TraceSamplingThread; |
373 | 375 |
| 376 // Options determines how the trace buffer stores data. |
| 377 enum TraceRecordMode { |
| 378 // Record until the trace buffer is full. |
| 379 RECORD_UNTIL_FULL, |
| 380 |
| 381 // Record until the user ends the trace. The trace buffer is a fixed size |
| 382 // and we use it as a ring buffer during recording. |
| 383 RECORD_CONTINUOUSLY, |
| 384 |
| 385 // Echo to console. Events are discarded. |
| 386 ECHO_TO_CONSOLE, |
| 387 }; |
| 388 |
| 389 struct BASE_EXPORT TraceOptions { |
| 390 |
| 391 TraceOptions() |
| 392 : record_mode(RECORD_UNTIL_FULL), |
| 393 enable_sampling(false), |
| 394 enable_systrace(false) {} |
| 395 |
| 396 TraceOptions(TraceRecordMode record_mode) |
| 397 : record_mode(record_mode), |
| 398 enable_sampling(false), |
| 399 enable_systrace(false) {} |
| 400 |
| 401 // |options_string| is a comma-delimited list of trace options. |
| 402 // Possible options are: "record-until-full", "record-continuously", |
| 403 // "trace-to-console", "enable-sampling" and "enable-systrace". |
| 404 // The first 3 options are trace recoding modes and hence |
| 405 // mutually exclusive. If more than one trace recording modes appear in the |
| 406 // options_string, the last one takes precedence. If none of the trace |
| 407 // recording mode is specified, recording mode is RECORD_UNTIL_FULL. |
| 408 // |
| 409 // Example: TraceOptions("record-until-full") |
| 410 // Example: TraceOptions("record-continuously, enable-sampling") |
| 411 // Example: TraceOptions("record-until-full, trace-to-console") would have |
| 412 // ECHO_TO_CONSOLE as the recording mode. |
| 413 explicit TraceOptions(const std::string& options_string); |
| 414 |
| 415 std::string ToString() const; |
| 416 |
| 417 TraceRecordMode record_mode; |
| 418 bool enable_sampling; |
| 419 bool enable_systrace; |
| 420 }; |
| 421 |
374 class BASE_EXPORT TraceLog { | 422 class BASE_EXPORT TraceLog { |
375 public: | 423 public: |
376 enum Mode { | 424 enum Mode { |
377 DISABLED = 0, | 425 DISABLED = 0, |
378 RECORDING_MODE, | 426 RECORDING_MODE, |
379 MONITORING_MODE, | 427 MONITORING_MODE, |
380 }; | 428 }; |
381 | 429 |
382 // Options determines how the trace buffer stores data. | |
383 enum Options { | |
384 // Record until the trace buffer is full. | |
385 RECORD_UNTIL_FULL = 1 << 0, | |
386 | |
387 // Record until the user ends the trace. The trace buffer is a fixed size | |
388 // and we use it as a ring buffer during recording. | |
389 RECORD_CONTINUOUSLY = 1 << 1, | |
390 | |
391 // Enable the sampling profiler in the recording mode. | |
392 ENABLE_SAMPLING = 1 << 2, | |
393 | |
394 // Echo to console. Events are discarded. | |
395 ECHO_TO_CONSOLE = 1 << 3, | |
396 }; | |
397 | |
398 // The pointer returned from GetCategoryGroupEnabledInternal() points to a | 430 // The pointer returned from GetCategoryGroupEnabledInternal() points to a |
399 // value with zero or more of the following bits. Used in this class only. | 431 // value with zero or more of the following bits. Used in this class only. |
400 // The TRACE_EVENT macros should only use the value as a bool. | 432 // The TRACE_EVENT macros should only use the value as a bool. |
401 // These values must be in sync with macro values in TraceEvent.h in Blink. | 433 // These values must be in sync with macro values in TraceEvent.h in Blink. |
402 enum CategoryGroupEnabledFlags { | 434 enum CategoryGroupEnabledFlags { |
403 // Category group enabled for the recording mode. | 435 // Category group enabled for the recording mode. |
404 ENABLED_FOR_RECORDING = 1 << 0, | 436 ENABLED_FOR_RECORDING = 1 << 0, |
405 // Category group enabled for the monitoring mode. | 437 // Category group enabled for the monitoring mode. |
406 ENABLED_FOR_MONITORING = 1 << 1, | 438 ENABLED_FOR_MONITORING = 1 << 1, |
407 // Category group enabled by SetEventCallbackEnabled(). | 439 // Category group enabled by SetEventCallbackEnabled(). |
408 ENABLED_FOR_EVENT_CALLBACK = 1 << 2, | 440 ENABLED_FOR_EVENT_CALLBACK = 1 << 2, |
409 }; | 441 }; |
410 | 442 |
411 static TraceLog* GetInstance(); | 443 static TraceLog* GetInstance(); |
412 | 444 |
413 // Get set of known category groups. This can change as new code paths are | 445 // Get set of known category groups. This can change as new code paths are |
414 // reached. The known category groups are inserted into |category_groups|. | 446 // reached. The known category groups are inserted into |category_groups|. |
415 void GetKnownCategoryGroups(std::vector<std::string>* category_groups); | 447 void GetKnownCategoryGroups(std::vector<std::string>* category_groups); |
416 | 448 |
417 // Retrieves a copy (for thread-safety) of the current CategoryFilter. | 449 // Retrieves a copy (for thread-safety) of the current CategoryFilter. |
418 CategoryFilter GetCurrentCategoryFilter(); | 450 CategoryFilter GetCurrentCategoryFilter(); |
419 | 451 |
420 Options trace_options() const { | 452 // Retrieves a copy (for thread-safety) of the current TraceOptions. |
421 return static_cast<Options>(subtle::NoBarrier_Load(&trace_options_)); | 453 TraceOptions GetCurrentTraceOptions() const; |
422 } | |
423 | 454 |
424 // Enables normal tracing (recording trace events in the trace buffer). | 455 // Enables normal tracing (recording trace events in the trace buffer). |
425 // See CategoryFilter comments for details on how to control what categories | 456 // See CategoryFilter comments for details on how to control what categories |
426 // will be traced. If tracing has already been enabled, |category_filter| will | 457 // will be traced. If tracing has already been enabled, |category_filter| will |
427 // be merged into the current category filter. | 458 // be merged into the current category filter. |
428 void SetEnabled(const CategoryFilter& category_filter, | 459 void SetEnabled(const CategoryFilter& category_filter, |
429 Mode mode, Options options); | 460 Mode mode, const TraceOptions& options); |
430 | 461 |
431 // Disables normal tracing for all categories. | 462 // Disables normal tracing for all categories. |
432 void SetDisabled(); | 463 void SetDisabled(); |
433 | 464 |
434 bool IsEnabled() { return mode_ != DISABLED; } | 465 bool IsEnabled() { return mode_ != DISABLED; } |
435 | 466 |
436 // The number of times we have begun recording traces. If tracing is off, | 467 // The number of times we have begun recording traces. If tracing is off, |
437 // returns -1. If tracing is on, then it returns the number of times we have | 468 // returns -1. If tracing is on, then it returns the number of times we have |
438 // recorded a trace. By watching for this number to increment, you can | 469 // recorded a trace. By watching for this number to increment, you can |
439 // passively discover when a new trace has begun. This is then used to | 470 // passively discover when a new trace has begun. This is then used to |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
599 void SetTimeOffset(TimeDelta offset); | 630 void SetTimeOffset(TimeDelta offset); |
600 | 631 |
601 size_t GetObserverCountForTest() const; | 632 size_t GetObserverCountForTest() const; |
602 | 633 |
603 // Call this method if the current thread may block the message loop to | 634 // Call this method if the current thread may block the message loop to |
604 // prevent the thread from using the thread-local buffer because the thread | 635 // prevent the thread from using the thread-local buffer because the thread |
605 // may not handle the flush request in time causing lost of unflushed events. | 636 // may not handle the flush request in time causing lost of unflushed events. |
606 void SetCurrentThreadBlocksMessageLoop(); | 637 void SetCurrentThreadBlocksMessageLoop(); |
607 | 638 |
608 private: | 639 private: |
| 640 typedef unsigned int InternalTraceOptions; |
| 641 |
609 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, | 642 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, |
610 TraceBufferRingBufferGetReturnChunk); | 643 TraceBufferRingBufferGetReturnChunk); |
611 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, | 644 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, |
612 TraceBufferRingBufferHalfIteration); | 645 TraceBufferRingBufferHalfIteration); |
613 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, | 646 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, |
614 TraceBufferRingBufferFullIteration); | 647 TraceBufferRingBufferFullIteration); |
615 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, | 648 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, |
616 TraceBufferVectorReportFull); | 649 TraceBufferVectorReportFull); |
| 650 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, |
| 651 ConvertTraceOptionsToInternalOptions); |
| 652 |
617 | 653 |
618 // This allows constructor and destructor to be private and usable only | 654 // This allows constructor and destructor to be private and usable only |
619 // by the Singleton class. | 655 // by the Singleton class. |
620 friend struct DefaultSingletonTraits<TraceLog>; | 656 friend struct DefaultSingletonTraits<TraceLog>; |
621 | 657 |
622 // Enable/disable each category group based on the current mode_, | 658 // Enable/disable each category group based on the current mode_, |
623 // category_filter_, event_callback_ and event_callback_category_filter_. | 659 // category_filter_, event_callback_ and event_callback_category_filter_. |
624 // Enable the category group in the enabled mode if category_filter_ matches | 660 // Enable the category group in the enabled mode if category_filter_ matches |
625 // the category group, or event_callback_ is not null and | 661 // the category group, or event_callback_ is not null and |
626 // event_callback_category_filter_ matches the category group. | 662 // event_callback_category_filter_ matches the category group. |
627 void UpdateCategoryGroupEnabledFlags(); | 663 void UpdateCategoryGroupEnabledFlags(); |
628 void UpdateCategoryGroupEnabledFlag(size_t category_index); | 664 void UpdateCategoryGroupEnabledFlag(size_t category_index); |
629 | 665 |
630 // Configure synthetic delays based on the values set in the current | 666 // Configure synthetic delays based on the values set in the current |
631 // category filter. | 667 // category filter. |
632 void UpdateSyntheticDelaysFromCategoryFilter(); | 668 void UpdateSyntheticDelaysFromCategoryFilter(); |
633 | 669 |
| 670 InternalTraceOptions GetInternalOptionsFromTraceOptions( |
| 671 const TraceOptions& options); |
| 672 |
634 class ThreadLocalEventBuffer; | 673 class ThreadLocalEventBuffer; |
635 class OptionalAutoLock; | 674 class OptionalAutoLock; |
636 | 675 |
637 TraceLog(); | 676 TraceLog(); |
638 ~TraceLog(); | 677 ~TraceLog(); |
639 const unsigned char* GetCategoryGroupEnabledInternal(const char* name); | 678 const unsigned char* GetCategoryGroupEnabledInternal(const char* name); |
640 void AddMetadataEventsWhileLocked(); | 679 void AddMetadataEventsWhileLocked(); |
641 | 680 |
| 681 InternalTraceOptions trace_options() const { |
| 682 return static_cast<InternalTraceOptions>( |
| 683 subtle::NoBarrier_Load(&trace_options_)); |
| 684 } |
| 685 |
642 TraceBuffer* trace_buffer() const { return logged_events_.get(); } | 686 TraceBuffer* trace_buffer() const { return logged_events_.get(); } |
643 TraceBuffer* CreateTraceBuffer(); | 687 TraceBuffer* CreateTraceBuffer(); |
644 TraceBuffer* CreateTraceBufferVectorOfSize(size_t max_chunks); | 688 TraceBuffer* CreateTraceBufferVectorOfSize(size_t max_chunks); |
645 | 689 |
646 std::string EventToConsoleMessage(unsigned char phase, | 690 std::string EventToConsoleMessage(unsigned char phase, |
647 const TimeTicks& timestamp, | 691 const TimeTicks& timestamp, |
648 TraceEvent* trace_event); | 692 TraceEvent* trace_event); |
649 | 693 |
650 TraceEvent* AddEventToThreadSharedChunkWhileLocked(TraceEventHandle* handle, | 694 TraceEvent* AddEventToThreadSharedChunkWhileLocked(TraceEventHandle* handle, |
651 bool check_buffer_is_full); | 695 bool check_buffer_is_full); |
(...skipping 19 matching lines...) Expand all Loading... |
671 } | 715 } |
672 void UseNextTraceBuffer(); | 716 void UseNextTraceBuffer(); |
673 | 717 |
674 TimeTicks OffsetNow() const { | 718 TimeTicks OffsetNow() const { |
675 return OffsetTimestamp(TimeTicks::NowFromSystemTraceTime()); | 719 return OffsetTimestamp(TimeTicks::NowFromSystemTraceTime()); |
676 } | 720 } |
677 TimeTicks OffsetTimestamp(const TimeTicks& timestamp) const { | 721 TimeTicks OffsetTimestamp(const TimeTicks& timestamp) const { |
678 return timestamp - time_offset_; | 722 return timestamp - time_offset_; |
679 } | 723 } |
680 | 724 |
| 725 // Internal representation of trace options since we store the currently used |
| 726 // trace option as an AtomicWord. |
| 727 static const InternalTraceOptions kInternalNone; |
| 728 static const InternalTraceOptions kInternalRecordUntilFull; |
| 729 static const InternalTraceOptions kInternalRecordContinuously; |
| 730 static const InternalTraceOptions kInternalEchoToConsole; |
| 731 static const InternalTraceOptions kInternalEnableSampling; |
| 732 |
681 // This lock protects TraceLog member accesses (except for members protected | 733 // This lock protects TraceLog member accesses (except for members protected |
682 // by thread_info_lock_) from arbitrary threads. | 734 // by thread_info_lock_) from arbitrary threads. |
683 mutable Lock lock_; | 735 mutable Lock lock_; |
684 // This lock protects accesses to thread_names_, thread_event_start_times_ | 736 // This lock protects accesses to thread_names_, thread_event_start_times_ |
685 // and thread_colors_. | 737 // and thread_colors_. |
686 Lock thread_info_lock_; | 738 Lock thread_info_lock_; |
687 int locked_line_; | 739 int locked_line_; |
688 Mode mode_; | 740 Mode mode_; |
689 int num_traces_recorded_; | 741 int num_traces_recorded_; |
690 scoped_ptr<TraceBuffer> logged_events_; | 742 scoped_ptr<TraceBuffer> logged_events_; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
744 scoped_refptr<MessageLoopProxy> flush_message_loop_proxy_; | 796 scoped_refptr<MessageLoopProxy> flush_message_loop_proxy_; |
745 subtle::AtomicWord generation_; | 797 subtle::AtomicWord generation_; |
746 | 798 |
747 DISALLOW_COPY_AND_ASSIGN(TraceLog); | 799 DISALLOW_COPY_AND_ASSIGN(TraceLog); |
748 }; | 800 }; |
749 | 801 |
750 } // namespace debug | 802 } // namespace debug |
751 } // namespace base | 803 } // namespace base |
752 | 804 |
753 #endif // BASE_DEBUG_TRACE_EVENT_IMPL_H_ | 805 #endif // BASE_DEBUG_TRACE_EVENT_IMPL_H_ |
OLD | NEW |