| 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/category_filter.h" | 27 #include "base/trace_event/trace_config.h" |
| 28 #include "base/trace_event/trace_options.h" | |
| 29 | 28 |
| 30 // Older style trace macros with explicit id and extra data | 29 // Older style trace macros with explicit id and extra data |
| 31 // 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. |
| 32 // TODO(georgesak): Update/replace these with new ETW macros. | 31 // TODO(georgesak): Update/replace these with new ETW macros. |
| 33 #define TRACE_EVENT_BEGIN_ETW(name, id, extra) \ | 32 #define TRACE_EVENT_BEGIN_ETW(name, id, extra) \ |
| 34 base::trace_event::TraceLog::AddTraceEventEtw( \ | 33 base::trace_event::TraceLog::AddTraceEventEtw( \ |
| 35 TRACE_EVENT_PHASE_BEGIN, \ | 34 TRACE_EVENT_PHASE_BEGIN, \ |
| 36 name, reinterpret_cast<const void*>(id), extra) | 35 name, reinterpret_cast<const void*>(id), extra) |
| 37 | 36 |
| 38 #define TRACE_EVENT_END_ETW(name, id, extra) \ | 37 #define TRACE_EVENT_END_ETW(name, id, extra) \ |
| (...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 312 // Category group enabled to export events to ETW. | 311 // Category group enabled to export events to ETW. |
| 313 ENABLED_FOR_ETW_EXPORT = 1 << 3 | 312 ENABLED_FOR_ETW_EXPORT = 1 << 3 |
| 314 }; | 313 }; |
| 315 | 314 |
| 316 static TraceLog* GetInstance(); | 315 static TraceLog* GetInstance(); |
| 317 | 316 |
| 318 // 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 |
| 319 // reached. The known category groups are inserted into |category_groups|. | 318 // reached. The known category groups are inserted into |category_groups|. |
| 320 void GetKnownCategoryGroups(std::vector<std::string>* category_groups); | 319 void GetKnownCategoryGroups(std::vector<std::string>* category_groups); |
| 321 | 320 |
| 322 // Retrieves a copy (for thread-safety) of the current CategoryFilter. | 321 // Retrieves a copy (for thread-safety) of the current TraceConfig. |
| 323 CategoryFilter GetCurrentCategoryFilter(); | 322 TraceConfig GetCurrentTraceConfig() const; |
| 324 | |
| 325 // Retrieves a copy (for thread-safety) of the current TraceOptions. | |
| 326 TraceOptions GetCurrentTraceOptions() const; | |
| 327 | 323 |
| 328 // Enables normal tracing (recording trace events in the trace buffer). | 324 // Enables normal tracing (recording trace events in the trace buffer). |
| 329 // See CategoryFilter comments for details on how to control what categories | 325 // See TraceConfig comments for details on how to control what categories |
| 330 // 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 |
| 331 // be merged into the current category filter. | 327 // be merged into the current category filter. |
| 332 void SetEnabled(const CategoryFilter& category_filter, | 328 void SetEnabled(const TraceConfig& trace_config, Mode mode); |
| 333 Mode mode, const TraceOptions& options); | |
| 334 | 329 |
| 335 // Disables normal tracing for all categories. | 330 // Disables normal tracing for all categories. |
| 336 void SetDisabled(); | 331 void SetDisabled(); |
| 337 | 332 |
| 338 bool IsEnabled() { return mode_ != DISABLED; } | 333 bool IsEnabled() { return mode_ != DISABLED; } |
| 339 | 334 |
| 340 // 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, |
| 341 // 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 |
| 342 // 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 |
| 343 // 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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 const unsigned char* category_group_enabled, | 379 const unsigned char* category_group_enabled, |
| 385 const char* name, | 380 const char* name, |
| 386 unsigned long long id, | 381 unsigned long long id, |
| 387 int num_args, | 382 int num_args, |
| 388 const char* const arg_names[], | 383 const char* const arg_names[], |
| 389 const unsigned char arg_types[], | 384 const unsigned char arg_types[], |
| 390 const unsigned long long arg_values[], | 385 const unsigned long long arg_values[], |
| 391 unsigned char flags); | 386 unsigned char flags); |
| 392 | 387 |
| 393 // Enable tracing for EventCallback. | 388 // Enable tracing for EventCallback. |
| 394 void SetEventCallbackEnabled(const CategoryFilter& category_filter, | 389 void SetEventCallbackEnabled(const TraceConfig& trace_config, |
| 395 EventCallback cb); | 390 EventCallback cb); |
| 396 void SetEventCallbackDisabled(); | 391 void SetEventCallbackDisabled(); |
| 397 void SetArgumentFilterPredicate( | 392 void SetArgumentFilterPredicate( |
| 398 const TraceEvent::ArgumentFilterPredicate& argument_filter_predicate); | 393 const TraceEvent::ArgumentFilterPredicate& argument_filter_predicate); |
| 399 | 394 |
| 400 // 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 |
| 401 // be called one or more times either synchronously or asynchronously from | 396 // be called one or more times either synchronously or asynchronously from |
| 402 // 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 |
| 403 // undefined. Use TraceResultBuffer to convert one or more trace strings to | 398 // undefined. Use TraceResultBuffer to convert one or more trace strings to |
| 404 // 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. |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 518 | 513 |
| 519 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, | 514 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, |
| 520 TraceBufferRingBufferGetReturnChunk); | 515 TraceBufferRingBufferGetReturnChunk); |
| 521 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, | 516 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, |
| 522 TraceBufferRingBufferHalfIteration); | 517 TraceBufferRingBufferHalfIteration); |
| 523 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, | 518 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, |
| 524 TraceBufferRingBufferFullIteration); | 519 TraceBufferRingBufferFullIteration); |
| 525 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, | 520 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, |
| 526 TraceBufferVectorReportFull); | 521 TraceBufferVectorReportFull); |
| 527 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, | 522 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, |
| 528 ConvertTraceOptionsToInternalOptions); | 523 ConvertTraceConfigToInternalOptions); |
| 529 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, | 524 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, |
| 530 TraceRecordAsMuchAsPossibleMode); | 525 TraceRecordAsMuchAsPossibleMode); |
| 531 | 526 |
| 532 // This allows constructor and destructor to be private and usable only | 527 // This allows constructor and destructor to be private and usable only |
| 533 // by the Singleton class. | 528 // by the Singleton class. |
| 534 friend struct DefaultSingletonTraits<TraceLog>; | 529 friend struct DefaultSingletonTraits<TraceLog>; |
| 535 | 530 |
| 536 // Enable/disable each category group based on the current mode_, | 531 // Enable/disable each category group based on the current mode_, |
| 537 // category_filter_, event_callback_ and event_callback_category_filter_. | 532 // category_filter_, event_callback_ and event_callback_category_filter_. |
| 538 // 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 |
| 539 // the category group, or event_callback_ is not null and | 534 // the category group, or event_callback_ is not null and |
| 540 // event_callback_category_filter_ matches the category group. | 535 // event_callback_category_filter_ matches the category group. |
| 541 void UpdateCategoryGroupEnabledFlags(); | 536 void UpdateCategoryGroupEnabledFlags(); |
| 542 void UpdateCategoryGroupEnabledFlag(size_t category_index); | 537 void UpdateCategoryGroupEnabledFlag(size_t category_index); |
| 543 | 538 |
| 544 // Configure synthetic delays based on the values set in the current | 539 // Configure synthetic delays based on the values set in the current |
| 545 // category filter. | 540 // trace config. |
| 546 void UpdateSyntheticDelaysFromCategoryFilter(); | 541 void UpdateSyntheticDelaysFromTraceConfig(); |
| 547 | 542 |
| 548 InternalTraceOptions GetInternalOptionsFromTraceOptions( | 543 InternalTraceOptions GetInternalOptionsFromTraceConfig( |
| 549 const TraceOptions& options); | 544 const TraceConfig& config); |
| 550 | 545 |
| 551 class ThreadLocalEventBuffer; | 546 class ThreadLocalEventBuffer; |
| 552 class OptionalAutoLock; | 547 class OptionalAutoLock; |
| 553 | 548 |
| 554 TraceLog(); | 549 TraceLog(); |
| 555 ~TraceLog(); | 550 ~TraceLog(); |
| 556 const unsigned char* GetCategoryGroupEnabledInternal(const char* name); | 551 const unsigned char* GetCategoryGroupEnabledInternal(const char* name); |
| 557 void AddMetadataEventsWhileLocked(); | 552 void AddMetadataEventsWhileLocked(); |
| 558 | 553 |
| 559 InternalTraceOptions trace_options() const { | 554 InternalTraceOptions trace_options() const { |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 649 WatchEventCallback watch_event_callback_; | 644 WatchEventCallback watch_event_callback_; |
| 650 subtle::AtomicWord /* const unsigned char* */ watch_category_; | 645 subtle::AtomicWord /* const unsigned char* */ watch_category_; |
| 651 std::string watch_event_name_; | 646 std::string watch_event_name_; |
| 652 | 647 |
| 653 subtle::AtomicWord /* Options */ trace_options_; | 648 subtle::AtomicWord /* Options */ trace_options_; |
| 654 | 649 |
| 655 // Sampling thread handles. | 650 // Sampling thread handles. |
| 656 scoped_ptr<TraceSamplingThread> sampling_thread_; | 651 scoped_ptr<TraceSamplingThread> sampling_thread_; |
| 657 PlatformThreadHandle sampling_thread_handle_; | 652 PlatformThreadHandle sampling_thread_handle_; |
| 658 | 653 |
| 659 CategoryFilter category_filter_; | 654 TraceConfig trace_config_; |
| 660 CategoryFilter event_callback_category_filter_; | 655 TraceConfig event_callback_trace_config_; |
| 661 | 656 |
| 662 ThreadLocalPointer<ThreadLocalEventBuffer> thread_local_event_buffer_; | 657 ThreadLocalPointer<ThreadLocalEventBuffer> thread_local_event_buffer_; |
| 663 ThreadLocalBoolean thread_blocks_message_loop_; | 658 ThreadLocalBoolean thread_blocks_message_loop_; |
| 664 ThreadLocalBoolean thread_is_in_trace_event_; | 659 ThreadLocalBoolean thread_is_in_trace_event_; |
| 665 | 660 |
| 666 // 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 |
| 667 // added into the local event buffer. Not using SingleThreadTaskRunner | 662 // added into the local event buffer. Not using SingleThreadTaskRunner |
| 668 // 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. |
| 669 hash_set<MessageLoop*> thread_message_loops_; | 664 hash_set<MessageLoop*> thread_message_loops_; |
| 670 | 665 |
| 671 // 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 |
| 672 // from threads without a message loop. | 667 // from threads without a message loop. |
| 673 scoped_ptr<TraceBufferChunk> thread_shared_chunk_; | 668 scoped_ptr<TraceBufferChunk> thread_shared_chunk_; |
| 674 size_t thread_shared_chunk_index_; | 669 size_t thread_shared_chunk_index_; |
| 675 | 670 |
| 676 // Set when asynchronous Flush is in progress. | 671 // Set when asynchronous Flush is in progress. |
| 677 OutputCallback flush_output_callback_; | 672 OutputCallback flush_output_callback_; |
| 678 scoped_refptr<SingleThreadTaskRunner> flush_task_runner_; | 673 scoped_refptr<SingleThreadTaskRunner> flush_task_runner_; |
| 679 TraceEvent::ArgumentFilterPredicate argument_filter_predicate_; | 674 TraceEvent::ArgumentFilterPredicate argument_filter_predicate_; |
| 680 subtle::AtomicWord generation_; | 675 subtle::AtomicWord generation_; |
| 681 bool use_worker_thread_; | 676 bool use_worker_thread_; |
| 682 | 677 |
| 683 DISALLOW_COPY_AND_ASSIGN(TraceLog); | 678 DISALLOW_COPY_AND_ASSIGN(TraceLog); |
| 684 }; | 679 }; |
| 685 | 680 |
| 686 } // namespace trace_event | 681 } // namespace trace_event |
| 687 } // namespace base | 682 } // namespace base |
| 688 | 683 |
| 689 #endif // BASE_TRACE_EVENT_TRACE_EVENT_IMPL_H_ | 684 #endif // BASE_TRACE_EVENT_TRACE_EVENT_IMPL_H_ |
| OLD | NEW |