| 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 #include "base/trace_event/trace_event.h" | 5 #include "base/trace_event/trace_event.h" |
| 6 | 6 |
| 7 #include <math.h> | 7 #include <math.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 #include "base/memory/singleton.h" | 23 #include "base/memory/singleton.h" |
| 24 #include "base/process/process_handle.h" | 24 #include "base/process/process_handle.h" |
| 25 #include "base/single_thread_task_runner.h" | 25 #include "base/single_thread_task_runner.h" |
| 26 #include "base/stl_util.h" | 26 #include "base/stl_util.h" |
| 27 #include "base/strings/pattern.h" | 27 #include "base/strings/pattern.h" |
| 28 #include "base/strings/stringprintf.h" | 28 #include "base/strings/stringprintf.h" |
| 29 #include "base/synchronization/waitable_event.h" | 29 #include "base/synchronization/waitable_event.h" |
| 30 #include "base/threading/platform_thread.h" | 30 #include "base/threading/platform_thread.h" |
| 31 #include "base/threading/thread.h" | 31 #include "base/threading/thread.h" |
| 32 #include "base/time/time.h" | 32 #include "base/time/time.h" |
| 33 #include "base/trace_event/event_filter_registry.h" |
| 34 #include "base/trace_event/event_name_filter.h" |
| 35 #include "base/trace_event/heap_profiler_event_filter.h" |
| 33 #include "base/trace_event/trace_buffer.h" | 36 #include "base/trace_event/trace_buffer.h" |
| 34 #include "base/trace_event/trace_event.h" | 37 #include "base/trace_event/trace_event.h" |
| 38 #include "base/trace_event/trace_event_filter.h" |
| 35 #include "base/trace_event/trace_event_synthetic_delay.h" | 39 #include "base/trace_event/trace_event_synthetic_delay.h" |
| 36 #include "base/values.h" | 40 #include "base/values.h" |
| 37 #include "testing/gmock/include/gmock/gmock.h" | 41 #include "testing/gmock/include/gmock/gmock.h" |
| 38 #include "testing/gtest/include/gtest/gtest.h" | 42 #include "testing/gtest/include/gtest/gtest.h" |
| 39 | 43 |
| 40 namespace base { | 44 namespace base { |
| 41 namespace trace_event { | 45 namespace trace_event { |
| 42 | 46 |
| 43 namespace { | 47 namespace { |
| 44 | 48 |
| (...skipping 2904 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2949 config1.Merge(config2); | 2953 config1.Merge(config2); |
| 2950 EXPECT_EQ(2u, config1.GetSyntheticDelayValues().size()); | 2954 EXPECT_EQ(2u, config1.GetSyntheticDelayValues().size()); |
| 2951 } | 2955 } |
| 2952 | 2956 |
| 2953 TEST_F(TraceEventTestFixture, SyntheticDelayConfigurationToString) { | 2957 TEST_F(TraceEventTestFixture, SyntheticDelayConfigurationToString) { |
| 2954 const char filter[] = "DELAY(test.Delay;16;oneshot)"; | 2958 const char filter[] = "DELAY(test.Delay;16;oneshot)"; |
| 2955 TraceConfig config(filter, ""); | 2959 TraceConfig config(filter, ""); |
| 2956 EXPECT_EQ(filter, config.ToCategoryFilterString()); | 2960 EXPECT_EQ(filter, config.ToCategoryFilterString()); |
| 2957 } | 2961 } |
| 2958 | 2962 |
| 2959 class TestEventFilter : public TraceLog::TraceEventFilter { | 2963 class TestEventFilter : public TraceEventFilter { |
| 2960 public: | 2964 public: |
| 2965 static const char kName[]; |
| 2966 |
| 2961 bool FilterTraceEvent(const TraceEvent& trace_event) const override { | 2967 bool FilterTraceEvent(const TraceEvent& trace_event) const override { |
| 2962 filter_trace_event_hit_count_++; | 2968 filter_trace_event_hit_count_++; |
| 2963 return filter_return_value_; | 2969 return filter_return_value_; |
| 2964 } | 2970 } |
| 2965 | 2971 |
| 2966 void EndEvent(const char* category_group, const char* name) override { | 2972 void EndEvent(const char* category_group, const char* name) const override { |
| 2967 end_event_hit_count_++; | 2973 end_event_hit_count_++; |
| 2968 } | 2974 } |
| 2969 | 2975 |
| 2976 const char* name() const override { return kName; } |
| 2977 |
| 2970 static void set_filter_return_value(bool value) { | 2978 static void set_filter_return_value(bool value) { |
| 2971 filter_return_value_ = value; | 2979 filter_return_value_ = value; |
| 2972 } | 2980 } |
| 2973 | 2981 |
| 2974 static size_t filter_trace_event_hit_count() { | 2982 static size_t filter_trace_event_hit_count() { |
| 2975 return filter_trace_event_hit_count_; | 2983 return filter_trace_event_hit_count_; |
| 2976 } | 2984 } |
| 2977 static size_t end_event_hit_count() { return end_event_hit_count_; } | 2985 static size_t end_event_hit_count() { return end_event_hit_count_; } |
| 2978 | 2986 |
| 2979 static void clear_counts() { | 2987 static void clear_counts() { |
| 2980 filter_trace_event_hit_count_ = 0; | 2988 filter_trace_event_hit_count_ = 0; |
| 2981 end_event_hit_count_ = 0; | 2989 end_event_hit_count_ = 0; |
| 2982 } | 2990 } |
| 2983 | 2991 |
| 2984 private: | 2992 private: |
| 2985 static size_t filter_trace_event_hit_count_; | 2993 static size_t filter_trace_event_hit_count_; |
| 2986 static size_t end_event_hit_count_; | 2994 static size_t end_event_hit_count_; |
| 2987 static bool filter_return_value_; | 2995 static bool filter_return_value_; |
| 2988 }; | 2996 }; |
| 2989 | 2997 |
| 2998 const char TestEventFilter::kName[] = "testing_predicate"; |
| 2990 size_t TestEventFilter::filter_trace_event_hit_count_ = 0; | 2999 size_t TestEventFilter::filter_trace_event_hit_count_ = 0; |
| 2991 size_t TestEventFilter::end_event_hit_count_ = 0; | 3000 size_t TestEventFilter::end_event_hit_count_ = 0; |
| 2992 bool TestEventFilter::filter_return_value_ = false; | 3001 bool TestEventFilter::filter_return_value_ = false; |
| 2993 | 3002 |
| 2994 std::unique_ptr<TraceLog::TraceEventFilter> ConstructTestEventFilter() { | |
| 2995 return WrapUnique(new TestEventFilter); | |
| 2996 } | |
| 2997 | |
| 2998 TEST_F(TraceEventTestFixture, TraceFilteringMode) { | 3003 TEST_F(TraceEventTestFixture, TraceFilteringMode) { |
| 2999 const char config_json[] = | 3004 const char config_json[] = |
| 3000 "{" | 3005 "{" |
| 3001 " \"event_filters\": [" | 3006 " \"event_filters\": [" |
| 3002 " {" | 3007 " {" |
| 3003 " \"filter_predicate\": \"testing_predicate\", " | 3008 " \"filter_predicate\": \"testing_predicate\", " |
| 3004 " \"included_categories\": [\"*\"]" | 3009 " \"included_categories\": [\"*\"]" |
| 3005 " }" | 3010 " }" |
| 3006 " ]" | 3011 " ]" |
| 3007 "}"; | 3012 "}"; |
| 3008 | 3013 |
| 3009 // Run RECORDING_MODE within FILTERING_MODE: | 3014 // Run RECORDING_MODE within FILTERING_MODE: |
| 3015 TestEventFilter::clear_counts(); |
| 3010 TestEventFilter::set_filter_return_value(true); | 3016 TestEventFilter::set_filter_return_value(true); |
| 3011 TraceLog::SetTraceEventFilterConstructorForTesting(ConstructTestEventFilter); | 3017 EventFilterRegistry::RegisterFilterLocked<TestEventFilter>(); |
| 3012 | 3018 |
| 3013 // Only filtering mode is enabled with test filters. | 3019 // Only filtering mode is enabled with test filters. |
| 3014 TraceLog::GetInstance()->SetEnabled(TraceConfig(config_json), | 3020 TraceLog::GetInstance()->SetEnabled(TraceConfig(config_json), |
| 3015 TraceLog::FILTERING_MODE); | 3021 TraceLog::FILTERING_MODE); |
| 3016 EXPECT_EQ(TraceLog::FILTERING_MODE, TraceLog::GetInstance()->enabled_modes()); | 3022 EXPECT_EQ(TraceLog::FILTERING_MODE, TraceLog::GetInstance()->enabled_modes()); |
| 3017 { | 3023 { |
| 3018 void* ptr = this; | 3024 void* ptr = this; |
| 3019 TRACE_EVENT0("c0", "name0"); | 3025 TRACE_EVENT0("c0", "name0"); |
| 3020 TRACE_EVENT_ASYNC_BEGIN0("c1", "name1", ptr); | 3026 TRACE_EVENT_ASYNC_BEGIN0("c1", "name1", ptr); |
| 3021 TRACE_EVENT_INSTANT0("c0", "name0", TRACE_EVENT_SCOPE_THREAD); | 3027 TRACE_EVENT_INSTANT0("c0", "name0", TRACE_EVENT_SCOPE_THREAD); |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3104 " \"unfiltered_cat\"]," | 3110 " \"unfiltered_cat\"]," |
| 3105 " \"event_filters\": [" | 3111 " \"event_filters\": [" |
| 3106 " {" | 3112 " {" |
| 3107 " \"filter_predicate\": \"testing_predicate\", " | 3113 " \"filter_predicate\": \"testing_predicate\", " |
| 3108 " \"included_categories\": [\"filtered_cat\"]" | 3114 " \"included_categories\": [\"filtered_cat\"]" |
| 3109 " }" | 3115 " }" |
| 3110 " " | 3116 " " |
| 3111 " ]" | 3117 " ]" |
| 3112 "}"; | 3118 "}"; |
| 3113 | 3119 |
| 3120 TestEventFilter::clear_counts(); |
| 3114 TestEventFilter::set_filter_return_value(true); | 3121 TestEventFilter::set_filter_return_value(true); |
| 3115 TraceLog::SetTraceEventFilterConstructorForTesting(ConstructTestEventFilter); | 3122 EventFilterRegistry::RegisterFilterLocked<TestEventFilter>(); |
| 3123 |
| 3116 TraceConfig trace_config(config_json); | 3124 TraceConfig trace_config(config_json); |
| 3117 TraceLog::GetInstance()->SetEnabled( | 3125 TraceLog::GetInstance()->SetEnabled( |
| 3118 trace_config, TraceLog::RECORDING_MODE | TraceLog::FILTERING_MODE); | 3126 trace_config, TraceLog::RECORDING_MODE | TraceLog::FILTERING_MODE); |
| 3119 ASSERT_TRUE(TraceLog::GetInstance()->IsEnabled()); | 3127 ASSERT_TRUE(TraceLog::GetInstance()->IsEnabled()); |
| 3120 | 3128 |
| 3121 TRACE_EVENT0("filtered_cat", "a snake"); | 3129 TRACE_EVENT0("filtered_cat", "a snake"); |
| 3122 TRACE_EVENT0("filtered_cat", "a mushroom"); | 3130 TRACE_EVENT0("filtered_cat", "a mushroom"); |
| 3123 TRACE_EVENT0("unfiltered_cat", "a horse"); | 3131 TRACE_EVENT0("unfiltered_cat", "a horse"); |
| 3124 | 3132 |
| 3125 // This is scoped so we can test the end event being filtered. | 3133 // This is scoped so we can test the end event being filtered. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 3143 " \"filter_predicate\": \"%s\", " | 3151 " \"filter_predicate\": \"%s\", " |
| 3144 " \"included_categories\": [\"*\"], " | 3152 " \"included_categories\": [\"*\"], " |
| 3145 " \"excluded_categories\": [\"unfiltered_cat\"], " | 3153 " \"excluded_categories\": [\"unfiltered_cat\"], " |
| 3146 " \"filter_args\": {" | 3154 " \"filter_args\": {" |
| 3147 " \"event_name_whitelist\": [\"a snake\", \"a dog\"]" | 3155 " \"event_name_whitelist\": [\"a snake\", \"a dog\"]" |
| 3148 " }" | 3156 " }" |
| 3149 " }" | 3157 " }" |
| 3150 " " | 3158 " " |
| 3151 " ]" | 3159 " ]" |
| 3152 "}", | 3160 "}", |
| 3153 TraceLog::TraceEventFilter::kEventWhitelistPredicate); | 3161 EventNameFilter::kName); |
| 3154 | 3162 |
| 3155 TraceConfig trace_config(config_json); | 3163 TraceConfig trace_config(config_json); |
| 3156 TraceLog::GetInstance()->SetEnabled( | 3164 TraceLog::GetInstance()->SetEnabled( |
| 3157 trace_config, TraceLog::RECORDING_MODE | TraceLog::FILTERING_MODE); | 3165 trace_config, TraceLog::RECORDING_MODE | TraceLog::FILTERING_MODE); |
| 3158 EXPECT_TRUE(TraceLog::GetInstance()->IsEnabled()); | 3166 EXPECT_TRUE(TraceLog::GetInstance()->IsEnabled()); |
| 3159 | 3167 |
| 3160 TRACE_EVENT0("filtered_cat", "a snake"); | 3168 TRACE_EVENT0("filtered_cat", "a snake"); |
| 3161 TRACE_EVENT0("filtered_cat", "a mushroom"); | 3169 TRACE_EVENT0("filtered_cat", "a mushroom"); |
| 3162 TRACE_EVENT0("unfiltered_cat", "a cat"); | 3170 TRACE_EVENT0("unfiltered_cat", "a cat"); |
| 3163 | 3171 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 3175 " \"filtered_cat\"," | 3183 " \"filtered_cat\"," |
| 3176 " \"unfiltered_cat\"]," | 3184 " \"unfiltered_cat\"]," |
| 3177 " \"excluded_categories\": [\"excluded_cat\"]," | 3185 " \"excluded_categories\": [\"excluded_cat\"]," |
| 3178 " \"event_filters\": [" | 3186 " \"event_filters\": [" |
| 3179 " {" | 3187 " {" |
| 3180 " \"filter_predicate\": \"%s\", " | 3188 " \"filter_predicate\": \"%s\", " |
| 3181 " \"included_categories\": [\"*\"]" | 3189 " \"included_categories\": [\"*\"]" |
| 3182 " }" | 3190 " }" |
| 3183 " ]" | 3191 " ]" |
| 3184 "}", | 3192 "}", |
| 3185 TraceLog::TraceEventFilter::kHeapProfilerPredicate); | 3193 HeapProfilerEventFilter::kName); |
| 3186 | 3194 |
| 3187 TraceConfig trace_config(config_json); | 3195 TraceConfig trace_config(config_json); |
| 3188 TraceLog::GetInstance()->SetEnabled( | 3196 TraceLog::GetInstance()->SetEnabled( |
| 3189 trace_config, TraceLog::RECORDING_MODE | TraceLog::FILTERING_MODE); | 3197 trace_config, TraceLog::RECORDING_MODE | TraceLog::FILTERING_MODE); |
| 3190 EXPECT_TRUE(TraceLog::GetInstance()->IsEnabled()); | 3198 EXPECT_TRUE(TraceLog::GetInstance()->IsEnabled()); |
| 3191 | 3199 |
| 3192 TRACE_EVENT0("filtered_cat", "a snake"); | 3200 TRACE_EVENT0("filtered_cat", "a snake"); |
| 3193 TRACE_EVENT0("excluded_cat", "a mushroom"); | 3201 TRACE_EVENT0("excluded_cat", "a mushroom"); |
| 3194 TRACE_EVENT0("unfiltered_cat", "a cat"); | 3202 TRACE_EVENT0("unfiltered_cat", "a cat"); |
| 3195 | 3203 |
| 3196 EndTraceAndFlush(); | 3204 EndTraceAndFlush(); |
| 3197 | 3205 |
| 3198 // The predicate should not change behavior of the trace events. | 3206 // The predicate should not change behavior of the trace events. |
| 3199 EXPECT_TRUE(FindMatchingValue("name", "a snake")); | 3207 EXPECT_TRUE(FindMatchingValue("name", "a snake")); |
| 3200 EXPECT_FALSE(FindMatchingValue("name", "a mushroom")); | 3208 EXPECT_FALSE(FindMatchingValue("name", "a mushroom")); |
| 3201 EXPECT_TRUE(FindMatchingValue("name", "a cat")); | 3209 EXPECT_TRUE(FindMatchingValue("name", "a cat")); |
| 3202 } | 3210 } |
| 3203 | 3211 |
| 3204 TEST_F(TraceEventTestFixture, ClockSyncEventsAreAlwaysAddedToTrace) { | 3212 TEST_F(TraceEventTestFixture, ClockSyncEventsAreAlwaysAddedToTrace) { |
| 3205 BeginSpecificTrace("-*"); | 3213 BeginSpecificTrace("-*"); |
| 3206 TRACE_EVENT_CLOCK_SYNC_RECEIVER(1); | 3214 TRACE_EVENT_CLOCK_SYNC_RECEIVER(1); |
| 3207 EndTraceAndFlush(); | 3215 EndTraceAndFlush(); |
| 3208 EXPECT_TRUE(FindNamePhase("clock_sync", "c")); | 3216 EXPECT_TRUE(FindNamePhase("clock_sync", "c")); |
| 3209 } | 3217 } |
| 3210 | 3218 |
| 3211 } // namespace trace_event | 3219 } // namespace trace_event |
| 3212 } // namespace base | 3220 } // namespace base |
| OLD | NEW |