| 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 #include "base/trace_event/trace_log.h" | 5 #include "base/trace_event/trace_log.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cmath> | 8 #include <cmath> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 449 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 460 TraceLogStatus::~TraceLogStatus() {} | 460 TraceLogStatus::~TraceLogStatus() {} |
| 461 | 461 |
| 462 // static | 462 // static |
| 463 TraceLog* TraceLog::GetInstance() { | 463 TraceLog* TraceLog::GetInstance() { |
| 464 return Singleton<TraceLog, LeakySingletonTraits<TraceLog>>::get(); | 464 return Singleton<TraceLog, LeakySingletonTraits<TraceLog>>::get(); |
| 465 } | 465 } |
| 466 | 466 |
| 467 TraceLog::TraceLog() | 467 TraceLog::TraceLog() |
| 468 : mode_(0), | 468 : mode_(0), |
| 469 num_traces_recorded_(0), | 469 num_traces_recorded_(0), |
| 470 event_callback_(0), | |
| 471 dispatching_to_observer_list_(false), | 470 dispatching_to_observer_list_(false), |
| 472 process_sort_index_(0), | 471 process_sort_index_(0), |
| 473 process_id_hash_(0), | 472 process_id_hash_(0), |
| 474 process_id_(0), | 473 process_id_(0), |
| 475 watch_category_(0), | 474 watch_category_(0), |
| 476 trace_options_(kInternalRecordUntilFull), | 475 trace_options_(kInternalRecordUntilFull), |
| 477 sampling_thread_handle_(0), | 476 sampling_thread_handle_(0), |
| 478 trace_config_(TraceConfig()), | 477 trace_config_(TraceConfig()), |
| 479 event_callback_trace_config_(TraceConfig()), | |
| 480 thread_shared_chunk_index_(0), | 478 thread_shared_chunk_index_(0), |
| 481 generation_(0), | 479 generation_(0), |
| 482 use_worker_thread_(false) { | 480 use_worker_thread_(false) { |
| 483 // Trace is enabled or disabled on one thread while other threads are | 481 // Trace is enabled or disabled on one thread while other threads are |
| 484 // accessing the enabled flag. We don't care whether edge-case events are | 482 // accessing the enabled flag. We don't care whether edge-case events are |
| 485 // traced or not, so we allow races on the enabled flag to keep the trace | 483 // traced or not, so we allow races on the enabled flag to keep the trace |
| 486 // macros fast. | 484 // macros fast. |
| 487 // TODO(jbates): ANNOTATE_BENIGN_RACE_SIZED crashes windows TSAN bots: | 485 // TODO(jbates): ANNOTATE_BENIGN_RACE_SIZED crashes windows TSAN bots: |
| 488 // ANNOTATE_BENIGN_RACE_SIZED(g_category_group_enabled, | 486 // ANNOTATE_BENIGN_RACE_SIZED(g_category_group_enabled, |
| 489 // sizeof(g_category_group_enabled), | 487 // sizeof(g_category_group_enabled), |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 562 } | 560 } |
| 563 | 561 |
| 564 void TraceLog::UpdateCategoryGroupEnabledFlag(size_t category_index) { | 562 void TraceLog::UpdateCategoryGroupEnabledFlag(size_t category_index) { |
| 565 unsigned char enabled_flag = 0; | 563 unsigned char enabled_flag = 0; |
| 566 const char* category_group = g_category_groups[category_index]; | 564 const char* category_group = g_category_groups[category_index]; |
| 567 if (mode_ & RECORDING_MODE && | 565 if (mode_ & RECORDING_MODE && |
| 568 trace_config_.IsCategoryGroupEnabled(category_group)) { | 566 trace_config_.IsCategoryGroupEnabled(category_group)) { |
| 569 enabled_flag |= ENABLED_FOR_RECORDING; | 567 enabled_flag |= ENABLED_FOR_RECORDING; |
| 570 } | 568 } |
| 571 | 569 |
| 572 if (event_callback_ && | |
| 573 event_callback_trace_config_.IsCategoryGroupEnabled(category_group)) { | |
| 574 enabled_flag |= ENABLED_FOR_EVENT_CALLBACK; | |
| 575 } | |
| 576 | |
| 577 #if defined(OS_WIN) | 570 #if defined(OS_WIN) |
| 578 if (base::trace_event::TraceEventETWExport::IsCategoryGroupEnabled( | 571 if (base::trace_event::TraceEventETWExport::IsCategoryGroupEnabled( |
| 579 category_group)) { | 572 category_group)) { |
| 580 enabled_flag |= ENABLED_FOR_ETW_EXPORT; | 573 enabled_flag |= ENABLED_FOR_ETW_EXPORT; |
| 581 } | 574 } |
| 582 #endif | 575 #endif |
| 583 | 576 |
| 584 // TODO(primiano): this is a temporary workaround for catapult:#2341, | 577 // TODO(primiano): this is a temporary workaround for catapult:#2341, |
| 585 // to guarantee that metadata events are always added even if the category | 578 // to guarantee that metadata events are always added even if the category |
| 586 // filter is "-*". See crbug.com/618054 for more details and long-term fix. | 579 // filter is "-*". See crbug.com/618054 for more details and long-term fix. |
| (...skipping 434 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1021 void TraceLog::CheckIfBufferIsFullWhileLocked() { | 1014 void TraceLog::CheckIfBufferIsFullWhileLocked() { |
| 1022 lock_.AssertAcquired(); | 1015 lock_.AssertAcquired(); |
| 1023 if (logged_events_->IsFull()) { | 1016 if (logged_events_->IsFull()) { |
| 1024 if (buffer_limit_reached_timestamp_.is_null()) { | 1017 if (buffer_limit_reached_timestamp_.is_null()) { |
| 1025 buffer_limit_reached_timestamp_ = OffsetNow(); | 1018 buffer_limit_reached_timestamp_ = OffsetNow(); |
| 1026 } | 1019 } |
| 1027 SetDisabledWhileLocked(RECORDING_MODE); | 1020 SetDisabledWhileLocked(RECORDING_MODE); |
| 1028 } | 1021 } |
| 1029 } | 1022 } |
| 1030 | 1023 |
| 1031 void TraceLog::SetEventCallbackEnabled(const TraceConfig& trace_config, | |
| 1032 EventCallback cb) { | |
| 1033 AutoLock lock(lock_); | |
| 1034 subtle::NoBarrier_Store(&event_callback_, | |
| 1035 reinterpret_cast<subtle::AtomicWord>(cb)); | |
| 1036 event_callback_trace_config_ = trace_config; | |
| 1037 UpdateCategoryGroupEnabledFlags(); | |
| 1038 } | |
| 1039 | |
| 1040 void TraceLog::SetEventCallbackDisabled() { | |
| 1041 AutoLock lock(lock_); | |
| 1042 subtle::NoBarrier_Store(&event_callback_, 0); | |
| 1043 UpdateCategoryGroupEnabledFlags(); | |
| 1044 } | |
| 1045 | |
| 1046 // Flush() works as the following: | 1024 // Flush() works as the following: |
| 1047 // 1. Flush() is called in thread A whose task runner is saved in | 1025 // 1. Flush() is called in thread A whose task runner is saved in |
| 1048 // flush_task_runner_; | 1026 // flush_task_runner_; |
| 1049 // 2. If thread_message_loops_ is not empty, thread A posts task to each message | 1027 // 2. If thread_message_loops_ is not empty, thread A posts task to each message |
| 1050 // loop to flush the thread local buffers; otherwise finish the flush; | 1028 // loop to flush the thread local buffers; otherwise finish the flush; |
| 1051 // 3. FlushCurrentThread() deletes the thread local event buffer: | 1029 // 3. FlushCurrentThread() deletes the thread local event buffer: |
| 1052 // - The last batch of events of the thread are flushed into the main buffer; | 1030 // - The last batch of events of the thread are flushed into the main buffer; |
| 1053 // - The message loop will be removed from thread_message_loops_; | 1031 // - The message loop will be removed from thread_message_loops_; |
| 1054 // If this is the last message loop, finish the flush; | 1032 // If this is the last message loop, finish the flush; |
| 1055 // 4. If any thread hasn't finish its flush in time, finish the flush. | 1033 // 4. If any thread hasn't finish its flush in time, finish the flush. |
| (...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1530 AutoLock lock(lock_); | 1508 AutoLock lock(lock_); |
| 1531 event_name_matches = watch_event_name_ == name; | 1509 event_name_matches = watch_event_name_ == name; |
| 1532 watch_event_callback_copy = watch_event_callback_; | 1510 watch_event_callback_copy = watch_event_callback_; |
| 1533 } | 1511 } |
| 1534 if (event_name_matches) { | 1512 if (event_name_matches) { |
| 1535 if (!watch_event_callback_copy.is_null()) | 1513 if (!watch_event_callback_copy.is_null()) |
| 1536 watch_event_callback_copy.Run(); | 1514 watch_event_callback_copy.Run(); |
| 1537 } | 1515 } |
| 1538 } | 1516 } |
| 1539 | 1517 |
| 1540 if (*category_group_enabled & ENABLED_FOR_EVENT_CALLBACK) { | |
| 1541 EventCallback event_callback = reinterpret_cast<EventCallback>( | |
| 1542 subtle::NoBarrier_Load(&event_callback_)); | |
| 1543 if (event_callback) { | |
| 1544 event_callback( | |
| 1545 offset_event_timestamp, | |
| 1546 phase == TRACE_EVENT_PHASE_COMPLETE ? TRACE_EVENT_PHASE_BEGIN : phase, | |
| 1547 category_group_enabled, name, scope, id, num_args, arg_names, | |
| 1548 arg_types, arg_values, flags); | |
| 1549 } | |
| 1550 } | |
| 1551 | |
| 1552 return handle; | 1518 return handle; |
| 1553 } | 1519 } |
| 1554 | 1520 |
| 1555 void TraceLog::AddMetadataEvent( | 1521 void TraceLog::AddMetadataEvent( |
| 1556 const unsigned char* category_group_enabled, | 1522 const unsigned char* category_group_enabled, |
| 1557 const char* name, | 1523 const char* name, |
| 1558 int num_args, | 1524 int num_args, |
| 1559 const char** arg_names, | 1525 const char** arg_names, |
| 1560 const unsigned char* arg_types, | 1526 const unsigned char* arg_types, |
| 1561 const unsigned long long* arg_values, | 1527 const unsigned long long* arg_values, |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1689 | 1655 |
| 1690 if (trace_options() & kInternalEchoToConsole) { | 1656 if (trace_options() & kInternalEchoToConsole) { |
| 1691 console_message = | 1657 console_message = |
| 1692 EventToConsoleMessage(TRACE_EVENT_PHASE_END, now, trace_event); | 1658 EventToConsoleMessage(TRACE_EVENT_PHASE_END, now, trace_event); |
| 1693 } | 1659 } |
| 1694 } | 1660 } |
| 1695 | 1661 |
| 1696 if (!console_message.empty()) | 1662 if (!console_message.empty()) |
| 1697 LOG(ERROR) << console_message; | 1663 LOG(ERROR) << console_message; |
| 1698 | 1664 |
| 1699 if (category_group_enabled_local & ENABLED_FOR_EVENT_CALLBACK) { | |
| 1700 EventCallback event_callback = reinterpret_cast<EventCallback>( | |
| 1701 subtle::NoBarrier_Load(&event_callback_)); | |
| 1702 if (event_callback) { | |
| 1703 event_callback( | |
| 1704 now, TRACE_EVENT_PHASE_END, category_group_enabled, name, | |
| 1705 trace_event_internal::kGlobalScope, trace_event_internal::kNoId, 0, | |
| 1706 nullptr, nullptr, nullptr, TRACE_EVENT_FLAG_NONE); | |
| 1707 } | |
| 1708 } | |
| 1709 | |
| 1710 if (category_group_enabled_local & ENABLED_FOR_FILTERING) | 1665 if (category_group_enabled_local & ENABLED_FOR_FILTERING) |
| 1711 EndFilteredEvent(category_group_enabled, name, handle); | 1666 EndFilteredEvent(category_group_enabled, name, handle); |
| 1712 } | 1667 } |
| 1713 | 1668 |
| 1714 void TraceLog::SetWatchEvent(const std::string& category_name, | 1669 void TraceLog::SetWatchEvent(const std::string& category_name, |
| 1715 const std::string& event_name, | 1670 const std::string& event_name, |
| 1716 const WatchEventCallback& callback) { | 1671 const WatchEventCallback& callback) { |
| 1717 const unsigned char* category = | 1672 const unsigned char* category = |
| 1718 GetCategoryGroupEnabled(category_name.c_str()); | 1673 GetCategoryGroupEnabled(category_name.c_str()); |
| 1719 AutoLock lock(lock_); | 1674 AutoLock lock(lock_); |
| (...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1998 } | 1953 } |
| 1999 | 1954 |
| 2000 ScopedTraceBinaryEfficient::~ScopedTraceBinaryEfficient() { | 1955 ScopedTraceBinaryEfficient::~ScopedTraceBinaryEfficient() { |
| 2001 if (*category_group_enabled_) { | 1956 if (*category_group_enabled_) { |
| 2002 TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(category_group_enabled_, name_, | 1957 TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(category_group_enabled_, name_, |
| 2003 event_handle_); | 1958 event_handle_); |
| 2004 } | 1959 } |
| 2005 } | 1960 } |
| 2006 | 1961 |
| 2007 } // namespace trace_event_internal | 1962 } // namespace trace_event_internal |
| OLD | NEW |