| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #ifndef BASE_TRACE_EVENT_TRACE_LOG_H_ | 5 #ifndef BASE_TRACE_EVENT_TRACE_LOG_H_ |
| 6 #define BASE_TRACE_EVENT_TRACE_LOG_H_ | 6 #define BASE_TRACE_EVENT_TRACE_LOG_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include <memory> | 11 #include <memory> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <unordered_map> |
| 13 #include <vector> | 14 #include <vector> |
| 14 | 15 |
| 15 #include "base/atomicops.h" | 16 #include "base/atomicops.h" |
| 16 #include "base/containers/hash_tables.h" | 17 #include "base/containers/hash_tables.h" |
| 17 #include "base/gtest_prod_util.h" | 18 #include "base/gtest_prod_util.h" |
| 18 #include "base/macros.h" | 19 #include "base/macros.h" |
| 19 #include "base/memory/scoped_vector.h" | 20 #include "base/memory/scoped_vector.h" |
| 21 #include "base/message_loop/message_loop.h" |
| 20 #include "base/trace_event/memory_dump_provider.h" | 22 #include "base/trace_event/memory_dump_provider.h" |
| 21 #include "base/trace_event/trace_config.h" | 23 #include "base/trace_event/trace_config.h" |
| 22 #include "base/trace_event/trace_event_impl.h" | 24 #include "base/trace_event/trace_event_impl.h" |
| 23 #include "build/build_config.h" | 25 #include "build/build_config.h" |
| 24 | 26 |
| 25 namespace base { | 27 namespace base { |
| 26 | 28 |
| 27 template <typename Type> | 29 template <typename Type> |
| 28 struct DefaultSingletonTraits; | 30 struct DefaultSingletonTraits; |
| 29 class MessageLoop; | 31 class MessageLoop; |
| 30 class RefCountedString; | 32 class RefCountedString; |
| 31 | 33 |
| 32 namespace trace_event { | 34 namespace trace_event { |
| 33 | 35 |
| 36 class ThreadLocalEventBuffer; |
| 34 struct TraceCategory; | 37 struct TraceCategory; |
| 35 class TraceBuffer; | 38 class TraceBuffer; |
| 36 class TraceBufferChunk; | 39 class TraceBufferChunk; |
| 37 class TraceEvent; | 40 class TraceEvent; |
| 38 class TraceEventMemoryOverhead; | 41 class TraceEventMemoryOverhead; |
| 39 | 42 |
| 40 struct BASE_EXPORT TraceLogStatus { | 43 struct BASE_EXPORT TraceLogStatus { |
| 41 TraceLogStatus(); | 44 TraceLogStatus(); |
| 42 ~TraceLogStatus(); | 45 ~TraceLogStatus(); |
| 43 uint32_t event_capacity; | 46 uint32_t event_capacity; |
| 44 uint32_t event_count; | 47 uint32_t event_count; |
| 45 }; | 48 }; |
| 46 | 49 |
| 47 class BASE_EXPORT TraceLog : public MemoryDumpProvider { | 50 class BASE_EXPORT TraceLog : public MessageLoop::DestructionObserver, |
| 51 public MemoryDumpProvider { |
| 48 public: | 52 public: |
| 49 // Argument passed to TraceLog::SetEnabled. | 53 // Argument passed to TraceLog::SetEnabled. |
| 50 enum Mode : uint8_t { | 54 enum Mode : uint8_t { |
| 51 // Enables normal tracing (recording trace events in the trace buffer). | 55 // Enables normal tracing (recording trace events in the trace buffer). |
| 52 RECORDING_MODE = 1 << 0, | 56 RECORDING_MODE = 1 << 0, |
| 53 | 57 |
| 54 // Trace events are enabled just for filtering but not for recording. Only | 58 // Trace events are enabled just for filtering but not for recording. Only |
| 55 // event filters config of |trace_config| argument is used. | 59 // event filters config of |trace_config| argument is used. |
| 56 FILTERING_MODE = 1 << 1 | 60 FILTERING_MODE = 1 << 1 |
| 57 }; | 61 }; |
| 58 | 62 |
| 59 static TraceLog* GetInstance(); | 63 static TraceLog* GetInstance(); |
| 60 | 64 |
| 61 // Get set of known category groups. This can change as new code paths are | 65 // Get set of known category groups. This can change as new code paths are |
| 62 // reached. The known category groups are inserted into |category_groups|. | 66 // reached. The known category groups are inserted into |category_groups|. |
| 63 void GetKnownCategoryGroups(std::vector<std::string>* category_groups); | 67 void GetKnownCategoryGroups(std::vector<std::string>* category_groups); |
| 64 | 68 |
| 65 // Retrieves a copy (for thread-safety) of the current TraceConfig. | 69 // Retrieves a copy (for thread-safety) of the current TraceConfig. |
| 66 TraceConfig GetCurrentTraceConfig() const; | 70 TraceConfig GetCurrentTraceConfig() const; |
| 67 | 71 |
| 68 // Initializes the thread-local event buffer, if not already initialized and | 72 // Initializes the thread-local event buffer, if not already initialized and |
| 69 // if the current thread supports that (has a message loop). | 73 // if the current thread supports that (has a message loop). |
| 70 void InitializeThreadLocalEventBufferIfSupported(); | 74 void InitializeThreadLocalEventBufferIfSupported(); |
| 75 void DestroyThreadLocalEventBufferIfSupported(); |
| 71 | 76 |
| 72 // See TraceConfig comments for details on how to control which categories | 77 // See TraceConfig comments for details on how to control which categories |
| 73 // will be traced. SetDisabled must be called distinctly for each mode that is | 78 // will be traced. SetDisabled must be called distinctly for each mode that is |
| 74 // enabled. If tracing has already been enabled for recording, category filter | 79 // enabled. If tracing has already been enabled for recording, category filter |
| 75 // (enabled and disabled categories) will be merged into the current category | 80 // (enabled and disabled categories) will be merged into the current category |
| 76 // filter. Enabling RECORDING_MODE does not enable filters. Trace event | 81 // filter. Enabling RECORDING_MODE does not enable filters. Trace event |
| 77 // filters will be used only if FILTERING_MODE is set on |modes_to_enable|. | 82 // filters will be used only if FILTERING_MODE is set on |modes_to_enable|. |
| 78 // Conversely to RECORDING_MODE, FILTERING_MODE doesn't support upgrading, | 83 // Conversely to RECORDING_MODE, FILTERING_MODE doesn't support upgrading, |
| 79 // i.e. filters can only be enabled if not previously enabled. | 84 // i.e. filters can only be enabled if not previously enabled. |
| 80 void SetEnabled(const TraceConfig& trace_config, uint8_t modes_to_enable); | 85 void SetEnabled(const TraceConfig& trace_config, uint8_t modes_to_enable); |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 TraceEventHandle handle); | 274 TraceEventHandle handle); |
| 270 | 275 |
| 271 void EndFilteredEvent(const unsigned char* category_group_enabled, | 276 void EndFilteredEvent(const unsigned char* category_group_enabled, |
| 272 const char* name, | 277 const char* name, |
| 273 TraceEventHandle handle); | 278 TraceEventHandle handle); |
| 274 | 279 |
| 275 int process_id() const { return process_id_; } | 280 int process_id() const { return process_id_; } |
| 276 | 281 |
| 277 uint64_t MangleEventId(uint64_t id); | 282 uint64_t MangleEventId(uint64_t id); |
| 278 | 283 |
| 284 // Functions to exchange chunks with the ThreadLocalEventBuffer. Ownership of |
| 285 // |logged_events_| chunks is trasnferred to each thread and moved back to |
| 286 // the TraceLog once the thread-local instance has filled the chunk, or upon |
| 287 // flush (end of tracing). |
| 288 std::unique_ptr<TraceBufferChunk> TakeChunk(size_t* chunk_index); |
| 289 void ReturnChunk(std::unique_ptr<TraceBufferChunk>, |
| 290 int generation, |
| 291 size_t chunk_index); |
| 292 |
| 279 // Exposed for unittesting: | 293 // Exposed for unittesting: |
| 280 | 294 |
| 281 // Allows deleting our singleton instance. | 295 // Allows deleting our singleton instance. |
| 282 static void DeleteForTesting(); | 296 static void DeleteForTesting(); |
| 283 | 297 |
| 284 // Allow tests to inspect TraceEvents. | 298 // Allow tests to inspect TraceEvents. |
| 285 TraceEvent* GetEventByHandle(TraceEventHandle handle); | 299 TraceEvent* GetEventByHandle(TraceEventHandle handle); |
| 286 | 300 |
| 287 void SetProcessID(int process_id); | 301 void SetProcessID(int process_id); |
| 288 | 302 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 317 void SetCurrentThreadBlocksMessageLoop(); | 331 void SetCurrentThreadBlocksMessageLoop(); |
| 318 | 332 |
| 319 #if defined(OS_WIN) | 333 #if defined(OS_WIN) |
| 320 // This function is called by the ETW exporting module whenever the ETW | 334 // This function is called by the ETW exporting module whenever the ETW |
| 321 // keyword (flags) changes. This keyword indicates which categories should be | 335 // keyword (flags) changes. This keyword indicates which categories should be |
| 322 // exported, so whenever it changes, we adjust accordingly. | 336 // exported, so whenever it changes, we adjust accordingly. |
| 323 void UpdateETWCategoryGroupEnabledFlags(); | 337 void UpdateETWCategoryGroupEnabledFlags(); |
| 324 #endif | 338 #endif |
| 325 | 339 |
| 326 private: | 340 private: |
| 341 // Used to report the overhead of each thread-local instance to memory-infra. |
| 342 class MemoryDumpProviderForThreadLocalBuffer : public MemoryDumpProvider { |
| 343 public: |
| 344 explicit MemoryDumpProviderForThreadLocalBuffer(ThreadLocalEventBuffer*); |
| 345 ~MemoryDumpProviderForThreadLocalBuffer() override; |
| 346 |
| 347 bool OnMemoryDump(const MemoryDumpArgs&, ProcessMemoryDump*) override; |
| 348 |
| 349 private: |
| 350 ThreadLocalEventBuffer* const thread_local_event_buffer_; |
| 351 |
| 352 DISALLOW_COPY_AND_ASSIGN(MemoryDumpProviderForThreadLocalBuffer); |
| 353 }; |
| 354 |
| 327 typedef unsigned int InternalTraceOptions; | 355 typedef unsigned int InternalTraceOptions; |
| 328 | 356 |
| 329 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, | 357 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, |
| 330 TraceBufferRingBufferGetReturnChunk); | 358 TraceBufferRingBufferGetReturnChunk); |
| 331 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, | 359 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, |
| 332 TraceBufferRingBufferHalfIteration); | 360 TraceBufferRingBufferHalfIteration); |
| 333 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, | 361 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, |
| 334 TraceBufferRingBufferFullIteration); | 362 TraceBufferRingBufferFullIteration); |
| 335 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, TraceBufferVectorReportFull); | 363 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, TraceBufferVectorReportFull); |
| 336 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, | 364 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, |
| 337 ConvertTraceConfigToInternalOptions); | 365 ConvertTraceConfigToInternalOptions); |
| 338 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, | 366 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, |
| 339 TraceRecordAsMuchAsPossibleMode); | 367 TraceRecordAsMuchAsPossibleMode); |
| 340 | 368 |
| 341 // This allows constructor and destructor to be private and usable only | 369 // This allows constructor and destructor to be private and usable only |
| 342 // by the Singleton class. | 370 // by the Singleton class. |
| 343 friend struct DefaultSingletonTraits<TraceLog>; | 371 friend struct DefaultSingletonTraits<TraceLog>; |
| 344 | 372 |
| 345 // MemoryDumpProvider implementation. | 373 // MemoryDumpProvider implementation. |
| 346 bool OnMemoryDump(const MemoryDumpArgs& args, | 374 bool OnMemoryDump(const MemoryDumpArgs& args, |
| 347 ProcessMemoryDump* pmd) override; | 375 ProcessMemoryDump* pmd) override; |
| 348 | 376 |
| 377 // MessageLoop::DestructionObserver implementation. |
| 378 void WillDestroyCurrentMessageLoop() override; |
| 379 |
| 349 // Enable/disable each category group based on the current mode_, | 380 // Enable/disable each category group based on the current mode_, |
| 350 // category_filter_ and event_filters_enabled_. | 381 // category_filter_ and event_filters_enabled_. |
| 351 // Enable the category group in the recording mode if category_filter_ matches | 382 // Enable the category group in the recording mode if category_filter_ matches |
| 352 // the category group, is not null. Enable category for filtering if any | 383 // the category group, is not null. Enable category for filtering if any |
| 353 // filter in event_filters_enabled_ enables it. | 384 // filter in event_filters_enabled_ enables it. |
| 354 void UpdateCategoryRegistry(); | 385 void UpdateCategoryRegistry(); |
| 355 void UpdateCategoryState(TraceCategory* category); | 386 void UpdateCategoryState(TraceCategory* category); |
| 356 | 387 |
| 357 void CreateFiltersForTraceConfig(); | 388 void CreateFiltersForTraceConfig(); |
| 358 | 389 |
| 359 // Configure synthetic delays based on the values set in the current | 390 // Configure synthetic delays based on the values set in the current |
| 360 // trace config. | 391 // trace config. |
| 361 void UpdateSyntheticDelaysFromTraceConfig(); | 392 void UpdateSyntheticDelaysFromTraceConfig(); |
| 362 | 393 |
| 363 InternalTraceOptions GetInternalOptionsFromTraceConfig( | 394 InternalTraceOptions GetInternalOptionsFromTraceConfig( |
| 364 const TraceConfig& config); | 395 const TraceConfig& config); |
| 365 | 396 |
| 366 class ThreadLocalEventBuffer; | |
| 367 class OptionalAutoLock; | 397 class OptionalAutoLock; |
| 368 struct RegisteredAsyncObserver; | 398 struct RegisteredAsyncObserver; |
| 369 | 399 |
| 370 TraceLog(); | 400 TraceLog(); |
| 371 ~TraceLog() override; | 401 ~TraceLog() override; |
| 372 void AddMetadataEventsWhileLocked(); | 402 void AddMetadataEventsWhileLocked(); |
| 373 | 403 |
| 374 InternalTraceOptions trace_options() const { | 404 InternalTraceOptions trace_options() const { |
| 375 return static_cast<InternalTraceOptions>( | 405 return static_cast<InternalTraceOptions>( |
| 376 subtle::NoBarrier_Load(&trace_options_)); | 406 subtle::NoBarrier_Load(&trace_options_)); |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 469 | 499 |
| 470 ThreadLocalPointer<ThreadLocalEventBuffer> thread_local_event_buffer_; | 500 ThreadLocalPointer<ThreadLocalEventBuffer> thread_local_event_buffer_; |
| 471 ThreadLocalBoolean thread_blocks_message_loop_; | 501 ThreadLocalBoolean thread_blocks_message_loop_; |
| 472 ThreadLocalBoolean thread_is_in_trace_event_; | 502 ThreadLocalBoolean thread_is_in_trace_event_; |
| 473 | 503 |
| 474 // Contains the message loops of threads that have had at least one event | 504 // Contains the message loops of threads that have had at least one event |
| 475 // added into the local event buffer. Not using SingleThreadTaskRunner | 505 // added into the local event buffer. Not using SingleThreadTaskRunner |
| 476 // because we need to know the life time of the message loops. | 506 // because we need to know the life time of the message loops. |
| 477 hash_set<MessageLoop*> thread_message_loops_; | 507 hash_set<MessageLoop*> thread_message_loops_; |
| 478 | 508 |
| 509 // Maps each MessageLoop to the corresponding MDP that is used to report the |
| 510 // overhead of the ThreadLocalEventBuffer. |
| 511 std::unordered_map<MessageLoop*, |
| 512 std::unique_ptr<MemoryDumpProviderForThreadLocalBuffer>> |
| 513 thread_local_mdps_; |
| 514 |
| 515 // TODO get rid of this and use a locked writer. /////////////////////////////
/////// |
| 479 // For events which can't be added into the thread local buffer, e.g. events | 516 // For events which can't be added into the thread local buffer, e.g. events |
| 480 // from threads without a message loop. | 517 // from threads without a message loop. |
| 481 std::unique_ptr<TraceBufferChunk> thread_shared_chunk_; | 518 std::unique_ptr<TraceBufferChunk> thread_shared_chunk_; |
| 482 size_t thread_shared_chunk_index_; | 519 size_t thread_shared_chunk_index_; |
| 483 | 520 |
| 484 // Set when asynchronous Flush is in progress. | 521 // Set when asynchronous Flush is in progress. |
| 485 OutputCallback flush_output_callback_; | 522 OutputCallback flush_output_callback_; |
| 486 scoped_refptr<SingleThreadTaskRunner> flush_task_runner_; | 523 scoped_refptr<SingleThreadTaskRunner> flush_task_runner_; |
| 487 ArgumentFilterPredicate argument_filter_predicate_; | 524 ArgumentFilterPredicate argument_filter_predicate_; |
| 488 subtle::AtomicWord generation_; | 525 subtle::AtomicWord generation_; |
| 489 bool use_worker_thread_; | 526 bool use_worker_thread_; |
| 490 | 527 |
| 491 DISALLOW_COPY_AND_ASSIGN(TraceLog); | 528 DISALLOW_COPY_AND_ASSIGN(TraceLog); |
| 492 }; | 529 }; |
| 493 | 530 |
| 494 } // namespace trace_event | 531 } // namespace trace_event |
| 495 } // namespace base | 532 } // namespace base |
| 496 | 533 |
| 497 #endif // BASE_TRACE_EVENT_TRACE_LOG_H_ | 534 #endif // BASE_TRACE_EVENT_TRACE_LOG_H_ |
| OLD | NEW |