Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(80)

Side by Side Diff: base/trace_event/trace_log.cc

Issue 2415183004: Revert of [tracing] Add filtering mode in TraceLog (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/trace_event/trace_log.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 444 matching lines...) Expand 10 before | Expand all | Expand 10 after
455 TraceLogStatus::TraceLogStatus() : event_capacity(0), event_count(0) {} 455 TraceLogStatus::TraceLogStatus() : event_capacity(0), event_count(0) {}
456 456
457 TraceLogStatus::~TraceLogStatus() {} 457 TraceLogStatus::~TraceLogStatus() {}
458 458
459 // static 459 // static
460 TraceLog* TraceLog::GetInstance() { 460 TraceLog* TraceLog::GetInstance() {
461 return Singleton<TraceLog, LeakySingletonTraits<TraceLog>>::get(); 461 return Singleton<TraceLog, LeakySingletonTraits<TraceLog>>::get();
462 } 462 }
463 463
464 TraceLog::TraceLog() 464 TraceLog::TraceLog()
465 : enabled_modes_(0), 465 : mode_(DISABLED),
466 num_traces_recorded_(0), 466 num_traces_recorded_(0),
467 event_callback_(0), 467 event_callback_(0),
468 dispatching_to_observer_list_(false), 468 dispatching_to_observer_list_(false),
469 process_sort_index_(0), 469 process_sort_index_(0),
470 process_id_hash_(0), 470 process_id_hash_(0),
471 process_id_(0), 471 process_id_(0),
472 watch_category_(0), 472 watch_category_(0),
473 trace_options_(kInternalRecordUntilFull), 473 trace_options_(kInternalRecordUntilFull),
474 trace_config_(TraceConfig()), 474 trace_config_(TraceConfig()),
475 event_callback_trace_config_(TraceConfig()), 475 event_callback_trace_config_(TraceConfig()),
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
553 } 553 }
554 554
555 const char* TraceLog::GetCategoryGroupName( 555 const char* TraceLog::GetCategoryGroupName(
556 const unsigned char* category_group_enabled) { 556 const unsigned char* category_group_enabled) {
557 return g_category_groups[GetCategoryIndex(category_group_enabled)]; 557 return g_category_groups[GetCategoryIndex(category_group_enabled)];
558 } 558 }
559 559
560 void TraceLog::UpdateCategoryGroupEnabledFlag(size_t category_index) { 560 void TraceLog::UpdateCategoryGroupEnabledFlag(size_t category_index) {
561 unsigned char enabled_flag = 0; 561 unsigned char enabled_flag = 0;
562 const char* category_group = g_category_groups[category_index]; 562 const char* category_group = g_category_groups[category_index];
563 if (enabled_modes_ & RECORDING_MODE && 563 if (mode_ == RECORDING_MODE &&
564 trace_config_.IsCategoryGroupEnabled(category_group)) { 564 trace_config_.IsCategoryGroupEnabled(category_group)) {
565 enabled_flag |= ENABLED_FOR_RECORDING; 565 enabled_flag |= ENABLED_FOR_RECORDING;
566 } 566 }
567 567
568 if (event_callback_ && 568 if (event_callback_ &&
569 event_callback_trace_config_.IsCategoryGroupEnabled(category_group)) { 569 event_callback_trace_config_.IsCategoryGroupEnabled(category_group)) {
570 enabled_flag |= ENABLED_FOR_EVENT_CALLBACK; 570 enabled_flag |= ENABLED_FOR_EVENT_CALLBACK;
571 } 571 }
572 572
573 #if defined(OS_WIN) 573 #if defined(OS_WIN)
574 if (base::trace_event::TraceEventETWExport::IsCategoryGroupEnabled( 574 if (base::trace_event::TraceEventETWExport::IsCategoryGroupEnabled(
575 category_group)) { 575 category_group)) {
576 enabled_flag |= ENABLED_FOR_ETW_EXPORT; 576 enabled_flag |= ENABLED_FOR_ETW_EXPORT;
577 } 577 }
578 #endif 578 #endif
579 579
580 // TODO(primiano): this is a temporary workaround for catapult:#2341, 580 // TODO(primiano): this is a temporary workaround for catapult:#2341,
581 // to guarantee that metadata events are always added even if the category 581 // to guarantee that metadata events are always added even if the category
582 // filter is "-*". See crbug.com/618054 for more details and long-term fix. 582 // filter is "-*". See crbug.com/618054 for more details and long-term fix.
583 if (enabled_modes_ & RECORDING_MODE && !strcmp(category_group, "__metadata")) 583 if (mode_ == RECORDING_MODE && !strcmp(category_group, "__metadata"))
584 enabled_flag |= ENABLED_FOR_RECORDING; 584 enabled_flag |= ENABLED_FOR_RECORDING;
585 585
586 uint32_t enabled_filters_bitmap = 0; 586 uint32_t enabled_filters_bitmap = 0;
587 int index = 0; 587 int index = 0;
588 for (const auto& event_filter : enabled_event_filters_) { 588 for (const auto& event_filter : trace_config_.event_filters()) {
589 if (event_filter.IsCategoryGroupEnabled(category_group)) { 589 if (event_filter.IsCategoryGroupEnabled(category_group)) {
590 enabled_flag |= ENABLED_FOR_FILTERING; 590 enabled_flag |= ENABLED_FOR_FILTERING;
591 DCHECK(g_category_group_filters.Get()[index]); 591 DCHECK(g_category_group_filters.Get()[index]);
592 enabled_filters_bitmap |= 1 << index; 592 enabled_filters_bitmap |= 1 << index;
593 } 593 }
594 if (index++ >= MAX_TRACE_EVENT_FILTERS) { 594 if (index++ >= MAX_TRACE_EVENT_FILTERS) {
595 NOTREACHED(); 595 NOTREACHED();
596 break; 596 break;
597 } 597 }
598 } 598 }
599 g_category_group_filters_enabled[category_index] = enabled_filters_bitmap; 599 g_category_group_filters_enabled[category_index] = enabled_filters_bitmap;
600 600
601 g_category_group_enabled[category_index] = enabled_flag; 601 g_category_group_enabled[category_index] = enabled_flag;
602 } 602 }
603 603
604 void TraceLog::UpdateCategoryGroupEnabledFlags() { 604 void TraceLog::UpdateCategoryGroupEnabledFlags() {
605 CreateFiltersForTraceConfig(); 605 CreateFiltersForTraceConfig();
606 size_t category_index = base::subtle::NoBarrier_Load(&g_category_index); 606 size_t category_index = base::subtle::NoBarrier_Load(&g_category_index);
607 for (size_t i = 0; i < category_index; i++) 607 for (size_t i = 0; i < category_index; i++)
608 UpdateCategoryGroupEnabledFlag(i); 608 UpdateCategoryGroupEnabledFlag(i);
609 } 609 }
610 610
611 void TraceLog::CreateFiltersForTraceConfig() { 611 void TraceLog::CreateFiltersForTraceConfig() {
612 if (!(enabled_modes_ & FILTERING_MODE))
613 return;
614
615 // Filters were already added and tracing could be enabled. Filters list 612 // Filters were already added and tracing could be enabled. Filters list
616 // cannot be changed when trace events are using them. 613 // cannot be changed when trace events are using them.
617 if (g_category_group_filters.Get().size()) 614 if (g_category_group_filters.Get().size())
618 return; 615 return;
619 616
620 for (auto& event_filter : enabled_event_filters_) { 617 for (auto& event_filter : trace_config_.event_filters()) {
621 if (g_category_group_filters.Get().size() >= MAX_TRACE_EVENT_FILTERS) { 618 if (g_category_group_filters.Get().size() >= MAX_TRACE_EVENT_FILTERS) {
622 NOTREACHED() 619 NOTREACHED()
623 << "Too many trace event filters installed in the current session"; 620 << "Too many trace event filters installed in the current session";
624 break; 621 break;
625 } 622 }
626 623
627 std::unique_ptr<TraceEventFilter> new_filter; 624 std::unique_ptr<TraceEventFilter> new_filter;
628 if (event_filter.predicate_name() == 625 if (event_filter.predicate_name() ==
629 TraceEventFilter::kEventWhitelistPredicate) { 626 TraceEventFilter::kEventWhitelistPredicate) {
630 new_filter = MakeUnique<EventNameFilter>(event_filter.filter_args()); 627 new_filter = MakeUnique<EventNameFilter>(event_filter.filter_args());
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
723 } 720 }
724 721
725 void TraceLog::GetKnownCategoryGroups( 722 void TraceLog::GetKnownCategoryGroups(
726 std::vector<std::string>* category_groups) { 723 std::vector<std::string>* category_groups) {
727 AutoLock lock(lock_); 724 AutoLock lock(lock_);
728 size_t category_index = base::subtle::NoBarrier_Load(&g_category_index); 725 size_t category_index = base::subtle::NoBarrier_Load(&g_category_index);
729 for (size_t i = kNumBuiltinCategories; i < category_index; i++) 726 for (size_t i = kNumBuiltinCategories; i < category_index; i++)
730 category_groups->push_back(g_category_groups[i]); 727 category_groups->push_back(g_category_groups[i]);
731 } 728 }
732 729
733 void TraceLog::SetEnabled(const TraceConfig& trace_config, 730 void TraceLog::SetEnabled(const TraceConfig& trace_config, Mode mode) {
734 uint8_t modes_to_enable) {
735 std::vector<EnabledStateObserver*> observer_list; 731 std::vector<EnabledStateObserver*> observer_list;
736 std::map<AsyncEnabledStateObserver*, RegisteredAsyncObserver> observer_map; 732 std::map<AsyncEnabledStateObserver*, RegisteredAsyncObserver> observer_map;
737 { 733 {
738 AutoLock lock(lock_); 734 AutoLock lock(lock_);
739 735
740 // Can't enable tracing when Flush() is in progress. 736 // Can't enable tracing when Flush() is in progress.
741 DCHECK(!flush_task_runner_); 737 DCHECK(!flush_task_runner_);
742 738
743 InternalTraceOptions new_options = 739 InternalTraceOptions new_options =
744 GetInternalOptionsFromTraceConfig(trace_config); 740 GetInternalOptionsFromTraceConfig(trace_config);
745 741
746 InternalTraceOptions old_options = trace_options(); 742 InternalTraceOptions old_options = trace_options();
747 743
744 if (IsEnabled()) {
745 if (new_options != old_options) {
746 DLOG(ERROR) << "Attempting to re-enable tracing with a different "
747 << "set of options.";
748 }
749
750 if (mode != mode_) {
751 DLOG(ERROR) << "Attempting to re-enable tracing with a different mode.";
752 }
753
754 DCHECK(!trace_config.event_filters().size())
755 << "Adding new filters while tracing was already enabled is not "
756 "supported.";
757
758 trace_config_.Merge(trace_config);
759 UpdateCategoryGroupEnabledFlags();
760 return;
761 }
762
748 if (dispatching_to_observer_list_) { 763 if (dispatching_to_observer_list_) {
749 NOTREACHED() 764 DLOG(ERROR)
750 << "Cannot manipulate TraceLog::Enabled state from an observer."; 765 << "Cannot manipulate TraceLog::Enabled state from an observer.";
751 return; 766 return;
752 } 767 }
753 768
754 // Clear all filters from previous tracing session. These filters are not 769 mode_ = mode;
755 // cleared at the end of tracing because some threads which hit trace event
756 // when disabling, could try to use the filters.
757 if (!enabled_modes_)
758 g_category_group_filters.Get().clear();
759
760 // Update trace config for recording.
761 const bool already_recording = enabled_modes_ & RECORDING_MODE;
762 if (modes_to_enable & RECORDING_MODE) {
763 if (already_recording) {
764 // TODO(ssid): Stop suporting enabling of RECODING_MODE when already
765 // enabled crbug.com/625170.
766 DCHECK_EQ(new_options, old_options) << "Attempting to re-enable "
767 "tracing with a different set "
768 "of options.";
769 trace_config_.Merge(trace_config);
770 } else {
771 trace_config_ = trace_config;
772 }
773 }
774
775 // Update event filters.
776 if (modes_to_enable & FILTERING_MODE) {
777 DCHECK(!trace_config.event_filters().empty())
778 << "Attempting to enable filtering without any filters";
779 DCHECK(enabled_event_filters_.empty()) << "Attempting to re-enable "
780 "filtering when filters are "
781 "already enabled.";
782
783 // Use the given event filters only if filtering was not enabled.
784 if (enabled_event_filters_.empty())
785 enabled_event_filters_ = trace_config.event_filters();
786 }
787 // Keep the |trace_config_| updated with only enabled filters in case anyone
788 // tries to read it using |GetCurrentTraceConfig| (even if filters are
789 // empty).
790 trace_config_.SetEventFilters(enabled_event_filters_);
791
792 enabled_modes_ |= modes_to_enable;
793 UpdateCategoryGroupEnabledFlags();
794
795 // Do not notify observers or create trace buffer if only enabled for
796 // filtering or if recording was already enabled.
797 if (!(modes_to_enable & RECORDING_MODE) || already_recording)
798 return;
799 770
800 if (new_options != old_options) { 771 if (new_options != old_options) {
801 subtle::NoBarrier_Store(&trace_options_, new_options); 772 subtle::NoBarrier_Store(&trace_options_, new_options);
802 UseNextTraceBuffer(); 773 UseNextTraceBuffer();
803 } 774 }
804 775
805 num_traces_recorded_++; 776 num_traces_recorded_++;
806 777
778 // Clear all filters from previous tracing session. These filters are not
779 // cleared at the end of tracing because some threads which hit trace event
780 // when disabling, could try to use the filters.
781 g_category_group_filters.Get().clear();
782
783 trace_config_ = TraceConfig(trace_config);
807 UpdateCategoryGroupEnabledFlags(); 784 UpdateCategoryGroupEnabledFlags();
808 UpdateSyntheticDelaysFromTraceConfig(); 785 UpdateSyntheticDelaysFromTraceConfig();
809 786
810 dispatching_to_observer_list_ = true; 787 dispatching_to_observer_list_ = true;
811 observer_list = enabled_state_observer_list_; 788 observer_list = enabled_state_observer_list_;
812 observer_map = async_observers_; 789 observer_map = async_observers_;
813 } 790 }
814 // Notify observers outside the lock in case they trigger trace events. 791 // Notify observers outside the lock in case they trigger trace events.
815 for (EnabledStateObserver* observer : observer_list) 792 for (EnabledStateObserver* observer : observer_list)
816 observer->OnTraceLogEnabled(); 793 observer->OnTraceLogEnabled();
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
853 return kInternalNone; 830 return kInternalNone;
854 } 831 }
855 832
856 TraceConfig TraceLog::GetCurrentTraceConfig() const { 833 TraceConfig TraceLog::GetCurrentTraceConfig() const {
857 AutoLock lock(lock_); 834 AutoLock lock(lock_);
858 return trace_config_; 835 return trace_config_;
859 } 836 }
860 837
861 void TraceLog::SetDisabled() { 838 void TraceLog::SetDisabled() {
862 AutoLock lock(lock_); 839 AutoLock lock(lock_);
863 SetDisabledWhileLocked(RECORDING_MODE); 840 SetDisabledWhileLocked();
864 } 841 }
865 842
866 void TraceLog::SetDisabled(uint8_t modes_to_disable) { 843 void TraceLog::SetDisabledWhileLocked() {
867 AutoLock lock(lock_);
868 SetDisabledWhileLocked(modes_to_disable);
869 }
870
871 void TraceLog::SetDisabledWhileLocked(uint8_t modes_to_disable) {
872 lock_.AssertAcquired(); 844 lock_.AssertAcquired();
873 845
874 if (!(enabled_modes_ & modes_to_disable)) 846 if (!IsEnabled())
875 return; 847 return;
876 848
877 if (dispatching_to_observer_list_) { 849 if (dispatching_to_observer_list_) {
878 NOTREACHED() 850 DLOG(ERROR)
879 << "Cannot manipulate TraceLog::Enabled state from an observer."; 851 << "Cannot manipulate TraceLog::Enabled state from an observer.";
880 return; 852 return;
881 } 853 }
882 854
883 bool is_recording_mode_disabled = 855 mode_ = DISABLED;
884 (enabled_modes_ & RECORDING_MODE) && (modes_to_disable & RECORDING_MODE);
885 enabled_modes_ &= ~modes_to_disable;
886 856
887 if (modes_to_disable & FILTERING_MODE) 857 trace_config_.Clear();
888 enabled_event_filters_.clear(); 858 subtle::NoBarrier_Store(&watch_category_, 0);
889 859 watch_event_name_.clear();
890 if (modes_to_disable & RECORDING_MODE) {
891 trace_config_.Clear();
892
893 subtle::NoBarrier_Store(&watch_category_, 0);
894 watch_event_name_.clear();
895 }
896
897 UpdateCategoryGroupEnabledFlags(); 860 UpdateCategoryGroupEnabledFlags();
898
899 // Add metadata events and notify observers only if recording mode was
900 // disabled now.
901 if (!is_recording_mode_disabled)
902 return;
903
904 AddMetadataEventsWhileLocked(); 861 AddMetadataEventsWhileLocked();
905 862
906 // Remove metadata events so they will not get added to a subsequent trace. 863 // Remove metadata events so they will not get added to a subsequent trace.
907 metadata_events_.clear(); 864 metadata_events_.clear();
908 865
909 dispatching_to_observer_list_ = true; 866 dispatching_to_observer_list_ = true;
910 std::vector<EnabledStateObserver*> observer_list = 867 std::vector<EnabledStateObserver*> observer_list =
911 enabled_state_observer_list_; 868 enabled_state_observer_list_;
912 std::map<AsyncEnabledStateObserver*, RegisteredAsyncObserver> observer_map = 869 std::map<AsyncEnabledStateObserver*, RegisteredAsyncObserver> observer_map =
913 async_observers_; 870 async_observers_;
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
993 } 950 }
994 return trace_event; 951 return trace_event;
995 } 952 }
996 953
997 void TraceLog::CheckIfBufferIsFullWhileLocked() { 954 void TraceLog::CheckIfBufferIsFullWhileLocked() {
998 lock_.AssertAcquired(); 955 lock_.AssertAcquired();
999 if (logged_events_->IsFull()) { 956 if (logged_events_->IsFull()) {
1000 if (buffer_limit_reached_timestamp_.is_null()) { 957 if (buffer_limit_reached_timestamp_.is_null()) {
1001 buffer_limit_reached_timestamp_ = OffsetNow(); 958 buffer_limit_reached_timestamp_ = OffsetNow();
1002 } 959 }
1003 SetDisabledWhileLocked(RECORDING_MODE); 960 SetDisabledWhileLocked();
1004 } 961 }
1005 } 962 }
1006 963
1007 void TraceLog::SetEventCallbackEnabled(const TraceConfig& trace_config, 964 void TraceLog::SetEventCallbackEnabled(const TraceConfig& trace_config,
1008 EventCallback cb) { 965 EventCallback cb) {
1009 AutoLock lock(lock_); 966 AutoLock lock(lock_);
1010 subtle::NoBarrier_Store(&event_callback_, 967 subtle::NoBarrier_Store(&event_callback_,
1011 reinterpret_cast<subtle::AtomicWord>(cb)); 968 reinterpret_cast<subtle::AtomicWord>(cb));
1012 event_callback_trace_config_ = trace_config; 969 event_callback_trace_config_ = trace_config;
1013 UpdateCategoryGroupEnabledFlags(); 970 UpdateCategoryGroupEnabledFlags();
(...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after
1380 if (flags & TRACE_EVENT_FLAG_MANGLE_ID) { 1337 if (flags & TRACE_EVENT_FLAG_MANGLE_ID) {
1381 if ((flags & TRACE_EVENT_FLAG_FLOW_IN) || 1338 if ((flags & TRACE_EVENT_FLAG_FLOW_IN) ||
1382 (flags & TRACE_EVENT_FLAG_FLOW_OUT)) 1339 (flags & TRACE_EVENT_FLAG_FLOW_OUT))
1383 bind_id = MangleEventId(bind_id); 1340 bind_id = MangleEventId(bind_id);
1384 id = MangleEventId(id); 1341 id = MangleEventId(id);
1385 } 1342 }
1386 1343
1387 TimeTicks offset_event_timestamp = OffsetTimestamp(timestamp); 1344 TimeTicks offset_event_timestamp = OffsetTimestamp(timestamp);
1388 ThreadTicks thread_now = ThreadNow(); 1345 ThreadTicks thread_now = ThreadNow();
1389 1346
1390 ThreadLocalEventBuffer* thread_local_event_buffer = nullptr; 1347 // |thread_local_event_buffer_| can be null if the current thread doesn't have
1391 if (enabled_modes_ & RECORDING_MODE) { 1348 // a message loop or the message loop is blocked.
1392 // |thread_local_event_buffer_| can be null if the current thread doesn't 1349 InitializeThreadLocalEventBufferIfSupported();
1393 // have a message loop or the message loop is blocked. 1350 auto* thread_local_event_buffer = thread_local_event_buffer_.Get();
1394 InitializeThreadLocalEventBufferIfSupported();
1395 thread_local_event_buffer = thread_local_event_buffer_.Get();
1396 }
1397 1351
1398 // Check and update the current thread name only if the event is for the 1352 // Check and update the current thread name only if the event is for the
1399 // current thread to avoid locks in most cases. 1353 // current thread to avoid locks in most cases.
1400 if (thread_id == static_cast<int>(PlatformThread::CurrentId())) { 1354 if (thread_id == static_cast<int>(PlatformThread::CurrentId())) {
1401 const char* new_name = 1355 const char* new_name =
1402 ThreadIdNameManager::GetInstance()->GetName(thread_id); 1356 ThreadIdNameManager::GetInstance()->GetName(thread_id);
1403 // Check if the thread name has been set or changed since the previous 1357 // Check if the thread name has been set or changed since the previous
1404 // call (if any), but don't bother if the new name is empty. Note this will 1358 // call (if any), but don't bother if the new name is empty. Note this will
1405 // not detect a thread name change within the same char* buffer address: we 1359 // not detect a thread name change within the same char* buffer address: we
1406 // favor common case performance over corner case correctness. 1360 // favor common case performance over corner case correctness.
(...skipping 564 matching lines...) Expand 10 before | Expand all | Expand 10 after
1971 } 1925 }
1972 1926
1973 ScopedTraceBinaryEfficient::~ScopedTraceBinaryEfficient() { 1927 ScopedTraceBinaryEfficient::~ScopedTraceBinaryEfficient() {
1974 if (*category_group_enabled_) { 1928 if (*category_group_enabled_) {
1975 TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(category_group_enabled_, name_, 1929 TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(category_group_enabled_, name_,
1976 event_handle_); 1930 event_handle_);
1977 } 1931 }
1978 } 1932 }
1979 1933
1980 } // namespace trace_event_internal 1934 } // namespace trace_event_internal
OLDNEW
« no previous file with comments | « base/trace_event/trace_log.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698