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::clear_counts(); | |
Primiano Tucci (use gerrit)
2016/12/06 14:02:31
this is purely for test isolation. I think I notic
kraynov
2016/12/06 18:24:23
I would prefer not making stateful design.
Primiano Tucci (use gerrit)
2016/12/07 11:17:47
Fixed by splitting out the HitCounter class
| |
3010 TestEventFilter::set_filter_return_value(true); | 2976 TestEventFilter::set_filter_return_value(true); |
3011 TraceLog::SetTraceEventFilterConstructorForTesting(ConstructTestEventFilter); | |
3012 | 2977 |
3013 // Only filtering mode is enabled with test filters. | 2978 // Only filtering mode is enabled with test filters. |
3014 TraceLog::GetInstance()->SetEnabled(TraceConfig(config_json), | 2979 TraceLog::GetInstance()->SetEnabled(TraceConfig(config_json), |
3015 TraceLog::FILTERING_MODE); | 2980 TraceLog::FILTERING_MODE); |
3016 EXPECT_EQ(TraceLog::FILTERING_MODE, TraceLog::GetInstance()->enabled_modes()); | 2981 EXPECT_EQ(TraceLog::FILTERING_MODE, TraceLog::GetInstance()->enabled_modes()); |
3017 { | 2982 { |
3018 void* ptr = this; | 2983 void* ptr = this; |
3019 TRACE_EVENT0("c0", "name0"); | 2984 TRACE_EVENT0("c0", "name0"); |
3020 TRACE_EVENT_ASYNC_BEGIN0("c1", "name1", ptr); | 2985 TRACE_EVENT_ASYNC_BEGIN0("c1", "name1", ptr); |
3021 TRACE_EVENT_INSTANT0("c0", "name0", TRACE_EVENT_SCOPE_THREAD); | 2986 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\"]," | 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::clear_counts(); | |
3114 TestEventFilter::set_filter_return_value(true); | 3080 TestEventFilter::set_filter_return_value(true); |
3115 TraceLog::SetTraceEventFilterConstructorForTesting(ConstructTestEventFilter); | 3081 |
3116 TraceConfig trace_config(config_json); | 3082 TraceConfig trace_config(config_json); |
3117 TraceLog::GetInstance()->SetEnabled( | 3083 TraceLog::GetInstance()->SetEnabled( |
3118 trace_config, TraceLog::RECORDING_MODE | TraceLog::FILTERING_MODE); | 3084 trace_config, TraceLog::RECORDING_MODE | TraceLog::FILTERING_MODE); |
3119 ASSERT_TRUE(TraceLog::GetInstance()->IsEnabled()); | 3085 ASSERT_TRUE(TraceLog::GetInstance()->IsEnabled()); |
3120 | 3086 |
3121 TRACE_EVENT0("filtered_cat", "a snake"); | 3087 TRACE_EVENT0("filtered_cat", "a snake"); |
3122 TRACE_EVENT0("filtered_cat", "a mushroom"); | 3088 TRACE_EVENT0("filtered_cat", "a mushroom"); |
3123 TRACE_EVENT0("unfiltered_cat", "a horse"); | 3089 TRACE_EVENT0("unfiltered_cat", "a horse"); |
3124 | 3090 |
3125 // This is scoped so we can test the end event being filtered. | 3091 // This is scoped so we can test the end event being filtered. |
(...skipping 17 matching lines...) Expand all Loading... | |
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 |