| 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 3204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3215 TEST_F(TraceEventTestFixture, SyntheticDelayConfigurationToString) { | 3215 TEST_F(TraceEventTestFixture, SyntheticDelayConfigurationToString) { |
| 3216 const char filter[] = "DELAY(test.Delay;16;oneshot)"; | 3216 const char filter[] = "DELAY(test.Delay;16;oneshot)"; |
| 3217 TraceConfig config(filter, ""); | 3217 TraceConfig config(filter, ""); |
| 3218 EXPECT_EQ(filter, config.ToCategoryFilterString()); | 3218 EXPECT_EQ(filter, config.ToCategoryFilterString()); |
| 3219 } | 3219 } |
| 3220 | 3220 |
| 3221 class TestEventFilter : public TraceLog::TraceEventFilter { | 3221 class TestEventFilter : public TraceLog::TraceEventFilter { |
| 3222 public: | 3222 public: |
| 3223 bool FilterTraceEvent(const TraceEvent& trace_event) const override { | 3223 bool FilterTraceEvent(const TraceEvent& trace_event) const override { |
| 3224 filter_trace_event_hit_count_++; | 3224 filter_trace_event_hit_count_++; |
| 3225 return true; | 3225 return filter_return_value_; |
| 3226 } | 3226 } |
| 3227 | 3227 |
| 3228 void EndEvent(const char* category_group, const char* name) override { | 3228 void EndEvent(const char* category_group, const char* name) override { |
| 3229 end_event_hit_count_++; | 3229 end_event_hit_count_++; |
| 3230 } | 3230 } |
| 3231 | 3231 |
| 3232 static void set_filter_return_value(bool value) { |
| 3233 filter_return_value_ = value; |
| 3234 } |
| 3235 |
| 3232 static size_t filter_trace_event_hit_count() { | 3236 static size_t filter_trace_event_hit_count() { |
| 3233 return filter_trace_event_hit_count_; | 3237 return filter_trace_event_hit_count_; |
| 3234 } | 3238 } |
| 3235 static size_t end_event_hit_count() { return end_event_hit_count_; } | 3239 static size_t end_event_hit_count() { return end_event_hit_count_; } |
| 3236 | 3240 |
| 3241 static void clear_counts() { |
| 3242 filter_trace_event_hit_count_ = 0; |
| 3243 end_event_hit_count_ = 0; |
| 3244 } |
| 3245 |
| 3237 private: | 3246 private: |
| 3238 static size_t filter_trace_event_hit_count_; | 3247 static size_t filter_trace_event_hit_count_; |
| 3239 static size_t end_event_hit_count_; | 3248 static size_t end_event_hit_count_; |
| 3249 static bool filter_return_value_; |
| 3240 }; | 3250 }; |
| 3241 | 3251 |
| 3242 size_t TestEventFilter::filter_trace_event_hit_count_ = 0; | 3252 size_t TestEventFilter::filter_trace_event_hit_count_ = 0; |
| 3243 size_t TestEventFilter::end_event_hit_count_ = 0; | 3253 size_t TestEventFilter::end_event_hit_count_ = 0; |
| 3254 bool TestEventFilter::filter_return_value_ = false; |
| 3244 | 3255 |
| 3245 std::unique_ptr<TraceLog::TraceEventFilter> ConstructTestEventFilter() { | 3256 std::unique_ptr<TraceLog::TraceEventFilter> ConstructTestEventFilter() { |
| 3246 return WrapUnique(new TestEventFilter); | 3257 return WrapUnique(new TestEventFilter); |
| 3247 } | 3258 } |
| 3248 | 3259 |
| 3260 TEST_F(TraceEventTestFixture, TraceFilteringMode) { |
| 3261 const char config_json[] = |
| 3262 "{" |
| 3263 " \"event_filters\": [" |
| 3264 " {" |
| 3265 " \"filter_predicate\": \"testing_predicate\", " |
| 3266 " \"included_categories\": [\"*\"]" |
| 3267 " }" |
| 3268 " ]" |
| 3269 "}"; |
| 3270 |
| 3271 // Run RECORDING_MODE within FILTERING_MODE. |
| 3272 TestEventFilter::set_filter_return_value(true); |
| 3273 TraceLog::SetTraceEventFilterConstructorForTesting(ConstructTestEventFilter); |
| 3274 TraceLog::GetInstance()->SetEnabled(TraceConfig(config_json), |
| 3275 TraceLog::FILTERING_MODE); |
| 3276 EXPECT_EQ(TraceLog::FILTERING_MODE, TraceLog::GetInstance()->mode_); |
| 3277 { |
| 3278 void* ptr = this; |
| 3279 TRACE_EVENT0("c0", "name0"); |
| 3280 TRACE_EVENT_ASYNC_BEGIN0("c1", "name1", ptr); |
| 3281 TRACE_EVENT_INSTANT0("c0", "name0", TRACE_EVENT_SCOPE_THREAD); |
| 3282 TRACE_EVENT_ASYNC_END0("c1", "name1", ptr); |
| 3283 } |
| 3284 TraceLog::GetInstance()->SetEnabled(TraceConfig("", ""), |
| 3285 TraceLog::RECORDING_MODE); |
| 3286 EXPECT_EQ(TraceLog::RECORDING_MODE | TraceLog::FILTERING_MODE, |
| 3287 TraceLog::GetInstance()->mode_); |
| 3288 { TRACE_EVENT0("c2", "name2"); } |
| 3289 TraceLog::GetInstance()->SetDisabled(); |
| 3290 EXPECT_EQ(TraceLog::FILTERING_MODE, TraceLog::GetInstance()->mode_); |
| 3291 { TRACE_EVENT0("c0", "name0"); } |
| 3292 TraceLog::GetInstance()->SetDisabled(TraceLog::FILTERING_MODE); |
| 3293 EXPECT_EQ(0, TraceLog::GetInstance()->mode_); |
| 3294 |
| 3295 EndTraceAndFlush(); |
| 3296 EXPECT_FALSE(FindMatchingValue("cat", "c0")); |
| 3297 EXPECT_FALSE(FindMatchingValue("cat", "c1")); |
| 3298 EXPECT_FALSE(FindMatchingValue("name", "name0")); |
| 3299 EXPECT_FALSE(FindMatchingValue("name", "name1")); |
| 3300 EXPECT_TRUE(FindMatchingValue("cat", "c2")); |
| 3301 EXPECT_TRUE(FindMatchingValue("name", "name2")); |
| 3302 EXPECT_EQ(6u, TestEventFilter::filter_trace_event_hit_count()); |
| 3303 EXPECT_EQ(3u, TestEventFilter::end_event_hit_count()); |
| 3304 Clear(); |
| 3305 TestEventFilter::clear_counts(); |
| 3306 |
| 3307 // Run FILTERING_MODE within RECORDING_MODE. |
| 3308 TestEventFilter::set_filter_return_value(false); |
| 3309 TraceLog::GetInstance()->SetEnabled(TraceConfig("", ""), |
| 3310 TraceLog::RECORDING_MODE); |
| 3311 EXPECT_EQ(TraceLog::RECORDING_MODE, TraceLog::GetInstance()->mode_); |
| 3312 { TRACE_EVENT0("c0", "name0"); } |
| 3313 TraceLog::GetInstance()->SetEnabled(TraceConfig(config_json), |
| 3314 TraceLog::FILTERING_MODE); |
| 3315 EXPECT_EQ(TraceLog::RECORDING_MODE | TraceLog::FILTERING_MODE, |
| 3316 TraceLog::GetInstance()->mode_); |
| 3317 { TRACE_EVENT0("c1", "name1"); } |
| 3318 TraceLog::GetInstance()->SetDisabled(TraceLog::FILTERING_MODE); |
| 3319 EXPECT_EQ(TraceLog::RECORDING_MODE, TraceLog::GetInstance()->mode_); |
| 3320 { TRACE_EVENT0("c2", "name2"); } |
| 3321 TraceLog::GetInstance()->SetDisabled(); |
| 3322 EXPECT_EQ(0, TraceLog::GetInstance()->mode_); |
| 3323 |
| 3324 EndTraceAndFlush(); |
| 3325 EXPECT_TRUE(FindMatchingValue("cat", "c0")); |
| 3326 EXPECT_TRUE(FindMatchingValue("cat", "c2")); |
| 3327 EXPECT_TRUE(FindMatchingValue("name", "name0")); |
| 3328 EXPECT_TRUE(FindMatchingValue("name", "name2")); |
| 3329 EXPECT_FALSE(FindMatchingValue("cat", "c1")); |
| 3330 EXPECT_FALSE(FindMatchingValue("name", "name1")); |
| 3331 EXPECT_EQ(1u, TestEventFilter::filter_trace_event_hit_count()); |
| 3332 EXPECT_EQ(1u, TestEventFilter::end_event_hit_count()); |
| 3333 Clear(); |
| 3334 TestEventFilter::clear_counts(); |
| 3335 } |
| 3336 |
| 3249 TEST_F(TraceEventTestFixture, EventFiltering) { | 3337 TEST_F(TraceEventTestFixture, EventFiltering) { |
| 3250 const char config_json[] = | 3338 const char config_json[] = |
| 3251 "{" | 3339 "{" |
| 3252 " \"included_categories\": [" | 3340 " \"included_categories\": [" |
| 3253 " \"filtered_cat\"," | 3341 " \"filtered_cat\"," |
| 3254 " \"unfiltered_cat\"]," | 3342 " \"unfiltered_cat\"]," |
| 3255 " \"event_filters\": [" | 3343 " \"event_filters\": [" |
| 3256 " {" | 3344 " {" |
| 3257 " \"filter_predicate\": \"testing_predicate\", " | 3345 " \"filter_predicate\": \"testing_predicate\", " |
| 3258 " \"included_categories\": [\"filtered_cat\"]" | 3346 " \"included_categories\": [\"filtered_cat\"]" |
| 3259 " }" | 3347 " }" |
| 3260 " " | 3348 " " |
| 3261 " ]" | 3349 " ]" |
| 3262 "}"; | 3350 "}"; |
| 3263 | 3351 |
| 3352 TestEventFilter::set_filter_return_value(true); |
| 3264 TraceLog::SetTraceEventFilterConstructorForTesting(ConstructTestEventFilter); | 3353 TraceLog::SetTraceEventFilterConstructorForTesting(ConstructTestEventFilter); |
| 3265 TraceConfig trace_config(config_json); | 3354 TraceConfig trace_config(config_json); |
| 3266 TraceLog::GetInstance()->SetEnabled(trace_config, TraceLog::RECORDING_MODE); | 3355 TraceLog::GetInstance()->SetEnabled(trace_config, TraceLog::RECORDING_MODE); |
| 3267 ASSERT_TRUE(TraceLog::GetInstance()->IsEnabled()); | 3356 ASSERT_TRUE(TraceLog::GetInstance()->IsEnabled()); |
| 3268 | 3357 |
| 3269 TRACE_EVENT0("filtered_cat", "a snake"); | 3358 TRACE_EVENT0("filtered_cat", "a snake"); |
| 3270 TRACE_EVENT0("filtered_cat", "a mushroom"); | 3359 TRACE_EVENT0("filtered_cat", "a mushroom"); |
| 3271 TRACE_EVENT0("unfiltered_cat", "a horse"); | 3360 TRACE_EVENT0("unfiltered_cat", "a horse"); |
| 3272 | 3361 |
| 3273 // This is scoped so we can test the end event being filtered. | 3362 // This is scoped so we can test the end event being filtered. |
| 3274 { TRACE_EVENT0("filtered_cat", "another cat whoa"); } | 3363 { TRACE_EVENT0("filtered_cat", "another cat whoa"); } |
| 3275 | 3364 |
| 3276 EndTraceAndFlush(); | 3365 EndTraceAndFlush(); |
| 3277 | 3366 |
| 3278 EXPECT_EQ(3u, TestEventFilter::filter_trace_event_hit_count()); | 3367 EXPECT_EQ(3u, TestEventFilter::filter_trace_event_hit_count()); |
| 3279 EXPECT_EQ(1u, TestEventFilter::end_event_hit_count()); | 3368 EXPECT_EQ(1u, TestEventFilter::end_event_hit_count()); |
| 3369 TestEventFilter::clear_counts(); |
| 3280 } | 3370 } |
| 3281 | 3371 |
| 3282 TEST_F(TraceEventTestFixture, EventWhitelistFiltering) { | 3372 TEST_F(TraceEventTestFixture, EventWhitelistFiltering) { |
| 3283 std::string config_json = StringPrintf( | 3373 std::string config_json = StringPrintf( |
| 3284 "{" | 3374 "{" |
| 3285 " \"included_categories\": [" | 3375 " \"included_categories\": [" |
| 3286 " \"filtered_cat\"," | 3376 " \"filtered_cat\"," |
| 3287 " \"unfiltered_cat\"]," | 3377 " \"unfiltered_cat\"]," |
| 3288 " \"event_filters\": [" | 3378 " \"event_filters\": [" |
| 3289 " {" | 3379 " {" |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3348 | 3438 |
| 3349 TEST_F(TraceEventTestFixture, ClockSyncEventsAreAlwaysAddedToTrace) { | 3439 TEST_F(TraceEventTestFixture, ClockSyncEventsAreAlwaysAddedToTrace) { |
| 3350 BeginSpecificTrace("-*"); | 3440 BeginSpecificTrace("-*"); |
| 3351 TRACE_EVENT_CLOCK_SYNC_RECEIVER(1); | 3441 TRACE_EVENT_CLOCK_SYNC_RECEIVER(1); |
| 3352 EndTraceAndFlush(); | 3442 EndTraceAndFlush(); |
| 3353 EXPECT_TRUE(FindNamePhase("clock_sync", "c")); | 3443 EXPECT_TRUE(FindNamePhase("clock_sync", "c")); |
| 3354 } | 3444 } |
| 3355 | 3445 |
| 3356 } // namespace trace_event | 3446 } // namespace trace_event |
| 3357 } // namespace base | 3447 } // namespace base |
| OLD | NEW |