| 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_name_filter.h" |
| 34 #include "base/trace_event/heap_profiler_event_filter.h" |
| 33 #include "base/trace_event/trace_buffer.h" | 35 #include "base/trace_event/trace_buffer.h" |
| 34 #include "base/trace_event/trace_event.h" | 36 #include "base/trace_event/trace_event.h" |
| 37 #include "base/trace_event/trace_event_filter.h" |
| 38 #include "base/trace_event/trace_event_filter_test_utils.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 { | |
| 2960 public: | |
| 2961 bool FilterTraceEvent(const TraceEvent& trace_event) const override { | |
| 2962 filter_trace_event_hit_count_++; | |
| 2963 return filter_return_value_; | |
| 2964 } | |
| 2965 | |
| 2966 void EndEvent(const char* category_group, const char* name) override { | |
| 2967 end_event_hit_count_++; | |
| 2968 } | |
| 2969 | |
| 2970 static void set_filter_return_value(bool value) { | |
| 2971 filter_return_value_ = value; | |
| 2972 } | |
| 2973 | |
| 2974 static size_t filter_trace_event_hit_count() { | |
| 2975 return filter_trace_event_hit_count_; | |
| 2976 } | |
| 2977 static size_t end_event_hit_count() { return end_event_hit_count_; } | |
| 2978 | |
| 2979 static void clear_counts() { | |
| 2980 filter_trace_event_hit_count_ = 0; | |
| 2981 end_event_hit_count_ = 0; | |
| 2982 } | |
| 2983 | |
| 2984 private: | |
| 2985 static size_t filter_trace_event_hit_count_; | |
| 2986 static size_t end_event_hit_count_; | |
| 2987 static bool filter_return_value_; | |
| 2988 }; | |
| 2989 | |
| 2990 size_t TestEventFilter::filter_trace_event_hit_count_ = 0; | |
| 2991 size_t TestEventFilter::end_event_hit_count_ = 0; | |
| 2992 bool TestEventFilter::filter_return_value_ = false; | |
| 2993 | |
| 2994 std::unique_ptr<TraceLog::TraceEventFilter> ConstructTestEventFilter() { | |
| 2995 return WrapUnique(new TestEventFilter); | |
| 2996 } | |
| 2997 | |
| 2998 TEST_F(TraceEventTestFixture, TraceFilteringMode) { | 2963 TEST_F(TraceEventTestFixture, TraceFilteringMode) { |
| 2999 const char config_json[] = | 2964 const char config_json[] = |
| 3000 "{" | 2965 "{" |
| 3001 " \"event_filters\": [" | 2966 " \"event_filters\": [" |
| 3002 " {" | 2967 " {" |
| 3003 " \"filter_predicate\": \"testing_predicate\", " | 2968 " \"filter_predicate\": \"testing_predicate\", " |
| 3004 " \"included_categories\": [\"*\"]" | 2969 " \"included_categories\": [\"*\"]" |
| 3005 " }" | 2970 " }" |
| 3006 " ]" | 2971 " ]" |
| 3007 "}"; | 2972 "}"; |
| 3008 | 2973 |
| 3009 // Run RECORDING_MODE within FILTERING_MODE: | 2974 // Run RECORDING_MODE within FILTERING_MODE: |
| 2975 TestEventFilter::HitsCounter filter_hits_counter; |
| 3010 TestEventFilter::set_filter_return_value(true); | 2976 TestEventFilter::set_filter_return_value(true); |
| 3011 TraceLog::SetTraceEventFilterConstructorForTesting(ConstructTestEventFilter); | 2977 TraceLog::GetInstance()->SetFilterFactoryForTesting(TestEventFilter::Factory); |
| 3012 | 2978 |
| 3013 // Only filtering mode is enabled with test filters. | 2979 // Only filtering mode is enabled with test filters. |
| 3014 TraceLog::GetInstance()->SetEnabled(TraceConfig(config_json), | 2980 TraceLog::GetInstance()->SetEnabled(TraceConfig(config_json), |
| 3015 TraceLog::FILTERING_MODE); | 2981 TraceLog::FILTERING_MODE); |
| 3016 EXPECT_EQ(TraceLog::FILTERING_MODE, TraceLog::GetInstance()->enabled_modes()); | 2982 EXPECT_EQ(TraceLog::FILTERING_MODE, TraceLog::GetInstance()->enabled_modes()); |
| 3017 { | 2983 { |
| 3018 void* ptr = this; | 2984 void* ptr = this; |
| 3019 TRACE_EVENT0("c0", "name0"); | 2985 TRACE_EVENT0("c0", "name0"); |
| 3020 TRACE_EVENT_ASYNC_BEGIN0("c1", "name1", ptr); | 2986 TRACE_EVENT_ASYNC_BEGIN0("c1", "name1", ptr); |
| 3021 TRACE_EVENT_INSTANT0("c0", "name0", TRACE_EVENT_SCOPE_THREAD); | 2987 TRACE_EVENT_INSTANT0("c0", "name0", TRACE_EVENT_SCOPE_THREAD); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 3041 TraceLog::GetInstance()->SetDisabled(TraceLog::FILTERING_MODE); | 3007 TraceLog::GetInstance()->SetDisabled(TraceLog::FILTERING_MODE); |
| 3042 EXPECT_EQ(0, TraceLog::GetInstance()->enabled_modes()); | 3008 EXPECT_EQ(0, TraceLog::GetInstance()->enabled_modes()); |
| 3043 | 3009 |
| 3044 EndTraceAndFlush(); | 3010 EndTraceAndFlush(); |
| 3045 EXPECT_FALSE(FindMatchingValue("cat", "c0")); | 3011 EXPECT_FALSE(FindMatchingValue("cat", "c0")); |
| 3046 EXPECT_FALSE(FindMatchingValue("cat", "c1")); | 3012 EXPECT_FALSE(FindMatchingValue("cat", "c1")); |
| 3047 EXPECT_FALSE(FindMatchingValue("name", "name0")); | 3013 EXPECT_FALSE(FindMatchingValue("name", "name0")); |
| 3048 EXPECT_FALSE(FindMatchingValue("name", "name1")); | 3014 EXPECT_FALSE(FindMatchingValue("name", "name1")); |
| 3049 EXPECT_TRUE(FindMatchingValue("cat", "c2")); | 3015 EXPECT_TRUE(FindMatchingValue("cat", "c2")); |
| 3050 EXPECT_TRUE(FindMatchingValue("name", "name2")); | 3016 EXPECT_TRUE(FindMatchingValue("name", "name2")); |
| 3051 EXPECT_EQ(6u, TestEventFilter::filter_trace_event_hit_count()); | 3017 EXPECT_EQ(6u, filter_hits_counter.filter_trace_event_hit_count); |
| 3052 EXPECT_EQ(3u, TestEventFilter::end_event_hit_count()); | 3018 EXPECT_EQ(3u, filter_hits_counter.end_event_hit_count); |
| 3053 Clear(); | 3019 Clear(); |
| 3054 TestEventFilter::clear_counts(); | 3020 filter_hits_counter.Reset(); |
| 3055 | 3021 |
| 3056 // Run FILTERING_MODE within RECORDING_MODE: | 3022 // Run FILTERING_MODE within RECORDING_MODE: |
| 3057 // Only recording mode is enabled and all events must be recorded. | 3023 // Only recording mode is enabled and all events must be recorded. |
| 3058 TraceLog::GetInstance()->SetEnabled(TraceConfig("", ""), | 3024 TraceLog::GetInstance()->SetEnabled(TraceConfig("", ""), |
| 3059 TraceLog::RECORDING_MODE); | 3025 TraceLog::RECORDING_MODE); |
| 3060 EXPECT_EQ(TraceLog::RECORDING_MODE, TraceLog::GetInstance()->enabled_modes()); | 3026 EXPECT_EQ(TraceLog::RECORDING_MODE, TraceLog::GetInstance()->enabled_modes()); |
| 3061 { | 3027 { |
| 3062 TRACE_EVENT0("c0", "name0"); | 3028 TRACE_EVENT0("c0", "name0"); |
| 3063 } | 3029 } |
| 3064 | 3030 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 3083 TraceLog::GetInstance()->SetDisabled(TraceLog::RECORDING_MODE); | 3049 TraceLog::GetInstance()->SetDisabled(TraceLog::RECORDING_MODE); |
| 3084 EXPECT_EQ(0, TraceLog::GetInstance()->enabled_modes()); | 3050 EXPECT_EQ(0, TraceLog::GetInstance()->enabled_modes()); |
| 3085 | 3051 |
| 3086 EndTraceAndFlush(); | 3052 EndTraceAndFlush(); |
| 3087 EXPECT_TRUE(FindMatchingValue("cat", "c0")); | 3053 EXPECT_TRUE(FindMatchingValue("cat", "c0")); |
| 3088 EXPECT_TRUE(FindMatchingValue("cat", "c2")); | 3054 EXPECT_TRUE(FindMatchingValue("cat", "c2")); |
| 3089 EXPECT_TRUE(FindMatchingValue("name", "name0")); | 3055 EXPECT_TRUE(FindMatchingValue("name", "name0")); |
| 3090 EXPECT_TRUE(FindMatchingValue("name", "name2")); | 3056 EXPECT_TRUE(FindMatchingValue("name", "name2")); |
| 3091 EXPECT_FALSE(FindMatchingValue("cat", "c1")); | 3057 EXPECT_FALSE(FindMatchingValue("cat", "c1")); |
| 3092 EXPECT_FALSE(FindMatchingValue("name", "name1")); | 3058 EXPECT_FALSE(FindMatchingValue("name", "name1")); |
| 3093 EXPECT_EQ(1u, TestEventFilter::filter_trace_event_hit_count()); | 3059 EXPECT_EQ(1u, filter_hits_counter.filter_trace_event_hit_count); |
| 3094 EXPECT_EQ(1u, TestEventFilter::end_event_hit_count()); | 3060 EXPECT_EQ(1u, filter_hits_counter.end_event_hit_count); |
| 3095 Clear(); | 3061 Clear(); |
| 3096 TestEventFilter::clear_counts(); | |
| 3097 } | 3062 } |
| 3098 | 3063 |
| 3099 TEST_F(TraceEventTestFixture, EventFiltering) { | 3064 TEST_F(TraceEventTestFixture, EventFiltering) { |
| 3100 const char config_json[] = | 3065 const char config_json[] = |
| 3101 "{" | 3066 "{" |
| 3102 " \"included_categories\": [" | 3067 " \"included_categories\": [" |
| 3103 " \"filtered_cat\"," | 3068 " \"filtered_cat\"," |
| 3104 " \"unfiltered_cat\"]," | 3069 " \"unfiltered_cat\"]," |
| 3105 " \"event_filters\": [" | 3070 " \"event_filters\": [" |
| 3106 " {" | 3071 " {" |
| 3107 " \"filter_predicate\": \"testing_predicate\", " | 3072 " \"filter_predicate\": \"testing_predicate\", " |
| 3108 " \"included_categories\": [\"filtered_cat\"]" | 3073 " \"included_categories\": [\"filtered_cat\"]" |
| 3109 " }" | 3074 " }" |
| 3110 " " | 3075 " " |
| 3111 " ]" | 3076 " ]" |
| 3112 "}"; | 3077 "}"; |
| 3113 | 3078 |
| 3079 TestEventFilter::HitsCounter filter_hits_counter; |
| 3114 TestEventFilter::set_filter_return_value(true); | 3080 TestEventFilter::set_filter_return_value(true); |
| 3115 TraceLog::SetTraceEventFilterConstructorForTesting(ConstructTestEventFilter); | 3081 TraceLog::GetInstance()->SetFilterFactoryForTesting(TestEventFilter::Factory); |
| 3082 |
| 3116 TraceConfig trace_config(config_json); | 3083 TraceConfig trace_config(config_json); |
| 3117 TraceLog::GetInstance()->SetEnabled( | 3084 TraceLog::GetInstance()->SetEnabled( |
| 3118 trace_config, TraceLog::RECORDING_MODE | TraceLog::FILTERING_MODE); | 3085 trace_config, TraceLog::RECORDING_MODE | TraceLog::FILTERING_MODE); |
| 3119 ASSERT_TRUE(TraceLog::GetInstance()->IsEnabled()); | 3086 ASSERT_TRUE(TraceLog::GetInstance()->IsEnabled()); |
| 3120 | 3087 |
| 3121 TRACE_EVENT0("filtered_cat", "a snake"); | 3088 TRACE_EVENT0("filtered_cat", "a snake"); |
| 3122 TRACE_EVENT0("filtered_cat", "a mushroom"); | 3089 TRACE_EVENT0("filtered_cat", "a mushroom"); |
| 3123 TRACE_EVENT0("unfiltered_cat", "a horse"); | 3090 TRACE_EVENT0("unfiltered_cat", "a horse"); |
| 3124 | 3091 |
| 3125 // This is scoped so we can test the end event being filtered. | 3092 // This is scoped so we can test the end event being filtered. |
| 3126 { TRACE_EVENT0("filtered_cat", "another cat whoa"); } | 3093 { TRACE_EVENT0("filtered_cat", "another cat whoa"); } |
| 3127 | 3094 |
| 3128 EndTraceAndFlush(); | 3095 EndTraceAndFlush(); |
| 3129 | 3096 |
| 3130 EXPECT_EQ(3u, TestEventFilter::filter_trace_event_hit_count()); | 3097 EXPECT_EQ(3u, filter_hits_counter.filter_trace_event_hit_count); |
| 3131 EXPECT_EQ(1u, TestEventFilter::end_event_hit_count()); | 3098 EXPECT_EQ(1u, filter_hits_counter.end_event_hit_count); |
| 3132 TestEventFilter::clear_counts(); | |
| 3133 } | 3099 } |
| 3134 | 3100 |
| 3135 TEST_F(TraceEventTestFixture, EventWhitelistFiltering) { | 3101 TEST_F(TraceEventTestFixture, EventWhitelistFiltering) { |
| 3136 std::string config_json = StringPrintf( | 3102 std::string config_json = StringPrintf( |
| 3137 "{" | 3103 "{" |
| 3138 " \"included_categories\": [" | 3104 " \"included_categories\": [" |
| 3139 " \"filtered_cat\"," | 3105 " \"filtered_cat\"," |
| 3140 " \"unfiltered_cat\"]," | 3106 " \"unfiltered_cat\"]," |
| 3141 " \"event_filters\": [" | 3107 " \"event_filters\": [" |
| 3142 " {" | 3108 " {" |
| 3143 " \"filter_predicate\": \"%s\", " | 3109 " \"filter_predicate\": \"%s\", " |
| 3144 " \"included_categories\": [\"*\"], " | 3110 " \"included_categories\": [\"*\"], " |
| 3145 " \"excluded_categories\": [\"unfiltered_cat\"], " | 3111 " \"excluded_categories\": [\"unfiltered_cat\"], " |
| 3146 " \"filter_args\": {" | 3112 " \"filter_args\": {" |
| 3147 " \"event_name_whitelist\": [\"a snake\", \"a dog\"]" | 3113 " \"event_name_whitelist\": [\"a snake\", \"a dog\"]" |
| 3148 " }" | 3114 " }" |
| 3149 " }" | 3115 " }" |
| 3150 " " | 3116 " " |
| 3151 " ]" | 3117 " ]" |
| 3152 "}", | 3118 "}", |
| 3153 TraceLog::TraceEventFilter::kEventWhitelistPredicate); | 3119 EventNameFilter::kName); |
| 3154 | 3120 |
| 3155 TraceConfig trace_config(config_json); | 3121 TraceConfig trace_config(config_json); |
| 3156 TraceLog::GetInstance()->SetEnabled( | 3122 TraceLog::GetInstance()->SetEnabled( |
| 3157 trace_config, TraceLog::RECORDING_MODE | TraceLog::FILTERING_MODE); | 3123 trace_config, TraceLog::RECORDING_MODE | TraceLog::FILTERING_MODE); |
| 3158 EXPECT_TRUE(TraceLog::GetInstance()->IsEnabled()); | 3124 EXPECT_TRUE(TraceLog::GetInstance()->IsEnabled()); |
| 3159 | 3125 |
| 3160 TRACE_EVENT0("filtered_cat", "a snake"); | 3126 TRACE_EVENT0("filtered_cat", "a snake"); |
| 3161 TRACE_EVENT0("filtered_cat", "a mushroom"); | 3127 TRACE_EVENT0("filtered_cat", "a mushroom"); |
| 3162 TRACE_EVENT0("unfiltered_cat", "a cat"); | 3128 TRACE_EVENT0("unfiltered_cat", "a cat"); |
| 3163 | 3129 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 3175 " \"filtered_cat\"," | 3141 " \"filtered_cat\"," |
| 3176 " \"unfiltered_cat\"]," | 3142 " \"unfiltered_cat\"]," |
| 3177 " \"excluded_categories\": [\"excluded_cat\"]," | 3143 " \"excluded_categories\": [\"excluded_cat\"]," |
| 3178 " \"event_filters\": [" | 3144 " \"event_filters\": [" |
| 3179 " {" | 3145 " {" |
| 3180 " \"filter_predicate\": \"%s\", " | 3146 " \"filter_predicate\": \"%s\", " |
| 3181 " \"included_categories\": [\"*\"]" | 3147 " \"included_categories\": [\"*\"]" |
| 3182 " }" | 3148 " }" |
| 3183 " ]" | 3149 " ]" |
| 3184 "}", | 3150 "}", |
| 3185 TraceLog::TraceEventFilter::kHeapProfilerPredicate); | 3151 HeapProfilerEventFilter::kName); |
| 3186 | 3152 |
| 3187 TraceConfig trace_config(config_json); | 3153 TraceConfig trace_config(config_json); |
| 3188 TraceLog::GetInstance()->SetEnabled( | 3154 TraceLog::GetInstance()->SetEnabled( |
| 3189 trace_config, TraceLog::RECORDING_MODE | TraceLog::FILTERING_MODE); | 3155 trace_config, TraceLog::RECORDING_MODE | TraceLog::FILTERING_MODE); |
| 3190 EXPECT_TRUE(TraceLog::GetInstance()->IsEnabled()); | 3156 EXPECT_TRUE(TraceLog::GetInstance()->IsEnabled()); |
| 3191 | 3157 |
| 3192 TRACE_EVENT0("filtered_cat", "a snake"); | 3158 TRACE_EVENT0("filtered_cat", "a snake"); |
| 3193 TRACE_EVENT0("excluded_cat", "a mushroom"); | 3159 TRACE_EVENT0("excluded_cat", "a mushroom"); |
| 3194 TRACE_EVENT0("unfiltered_cat", "a cat"); | 3160 TRACE_EVENT0("unfiltered_cat", "a cat"); |
| 3195 | 3161 |
| 3196 EndTraceAndFlush(); | 3162 EndTraceAndFlush(); |
| 3197 | 3163 |
| 3198 // The predicate should not change behavior of the trace events. | 3164 // The predicate should not change behavior of the trace events. |
| 3199 EXPECT_TRUE(FindMatchingValue("name", "a snake")); | 3165 EXPECT_TRUE(FindMatchingValue("name", "a snake")); |
| 3200 EXPECT_FALSE(FindMatchingValue("name", "a mushroom")); | 3166 EXPECT_FALSE(FindMatchingValue("name", "a mushroom")); |
| 3201 EXPECT_TRUE(FindMatchingValue("name", "a cat")); | 3167 EXPECT_TRUE(FindMatchingValue("name", "a cat")); |
| 3202 } | 3168 } |
| 3203 | 3169 |
| 3204 TEST_F(TraceEventTestFixture, ClockSyncEventsAreAlwaysAddedToTrace) { | 3170 TEST_F(TraceEventTestFixture, ClockSyncEventsAreAlwaysAddedToTrace) { |
| 3205 BeginSpecificTrace("-*"); | 3171 BeginSpecificTrace("-*"); |
| 3206 TRACE_EVENT_CLOCK_SYNC_RECEIVER(1); | 3172 TRACE_EVENT_CLOCK_SYNC_RECEIVER(1); |
| 3207 EndTraceAndFlush(); | 3173 EndTraceAndFlush(); |
| 3208 EXPECT_TRUE(FindNamePhase("clock_sync", "c")); | 3174 EXPECT_TRUE(FindNamePhase("clock_sync", "c")); |
| 3209 } | 3175 } |
| 3210 | 3176 |
| 3211 } // namespace trace_event | 3177 } // namespace trace_event |
| 3212 } // namespace base | 3178 } // namespace base |
| OLD | NEW |