| 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 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 } | 129 } |
| 130 | 130 |
| 131 void CancelTraceAsync(WaitableEvent* flush_complete_event) { | 131 void CancelTraceAsync(WaitableEvent* flush_complete_event) { |
| 132 TraceLog::GetInstance()->CancelTracing( | 132 TraceLog::GetInstance()->CancelTracing( |
| 133 base::Bind(&TraceEventTestFixture::OnTraceDataCollected, | 133 base::Bind(&TraceEventTestFixture::OnTraceDataCollected, |
| 134 base::Unretained(static_cast<TraceEventTestFixture*>(this)), | 134 base::Unretained(static_cast<TraceEventTestFixture*>(this)), |
| 135 base::Unretained(flush_complete_event))); | 135 base::Unretained(flush_complete_event))); |
| 136 } | 136 } |
| 137 | 137 |
| 138 void EndTraceAndFlushAsync(WaitableEvent* flush_complete_event) { | 138 void EndTraceAndFlushAsync(WaitableEvent* flush_complete_event) { |
| 139 TraceLog::GetInstance()->SetDisabled(); | 139 TraceLog::GetInstance()->SetDisabled(TraceLog::RECORDING_MODE | |
| 140 TraceLog::FILTERING_MODE); |
| 140 TraceLog::GetInstance()->Flush( | 141 TraceLog::GetInstance()->Flush( |
| 141 base::Bind(&TraceEventTestFixture::OnTraceDataCollected, | 142 base::Bind(&TraceEventTestFixture::OnTraceDataCollected, |
| 142 base::Unretained(static_cast<TraceEventTestFixture*>(this)), | 143 base::Unretained(static_cast<TraceEventTestFixture*>(this)), |
| 143 base::Unretained(flush_complete_event))); | 144 base::Unretained(flush_complete_event))); |
| 144 } | 145 } |
| 145 | 146 |
| 146 void SetUp() override { | 147 void SetUp() override { |
| 147 const char* name = PlatformThread::GetName(); | 148 const char* name = PlatformThread::GetName(); |
| 148 old_thread_name_ = name ? strdup(name) : NULL; | 149 old_thread_name_ = name ? strdup(name) : NULL; |
| 149 | 150 |
| (...skipping 3049 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3199 TEST_F(TraceEventTestFixture, SyntheticDelayConfigurationToString) { | 3200 TEST_F(TraceEventTestFixture, SyntheticDelayConfigurationToString) { |
| 3200 const char filter[] = "DELAY(test.Delay;16;oneshot)"; | 3201 const char filter[] = "DELAY(test.Delay;16;oneshot)"; |
| 3201 TraceConfig config(filter, ""); | 3202 TraceConfig config(filter, ""); |
| 3202 EXPECT_EQ(filter, config.ToCategoryFilterString()); | 3203 EXPECT_EQ(filter, config.ToCategoryFilterString()); |
| 3203 } | 3204 } |
| 3204 | 3205 |
| 3205 class TestEventFilter : public TraceLog::TraceEventFilter { | 3206 class TestEventFilter : public TraceLog::TraceEventFilter { |
| 3206 public: | 3207 public: |
| 3207 bool FilterTraceEvent(const TraceEvent& trace_event) const override { | 3208 bool FilterTraceEvent(const TraceEvent& trace_event) const override { |
| 3208 filter_trace_event_hit_count_++; | 3209 filter_trace_event_hit_count_++; |
| 3209 return true; | 3210 return filter_return_value_; |
| 3210 } | 3211 } |
| 3211 | 3212 |
| 3212 void EndEvent(const char* category_group, const char* name) override { | 3213 void EndEvent(const char* category_group, const char* name) override { |
| 3213 end_event_hit_count_++; | 3214 end_event_hit_count_++; |
| 3214 } | 3215 } |
| 3215 | 3216 |
| 3217 static void set_filter_return_value(bool value) { |
| 3218 filter_return_value_ = value; |
| 3219 } |
| 3220 |
| 3216 static size_t filter_trace_event_hit_count() { | 3221 static size_t filter_trace_event_hit_count() { |
| 3217 return filter_trace_event_hit_count_; | 3222 return filter_trace_event_hit_count_; |
| 3218 } | 3223 } |
| 3219 static size_t end_event_hit_count() { return end_event_hit_count_; } | 3224 static size_t end_event_hit_count() { return end_event_hit_count_; } |
| 3220 | 3225 |
| 3226 static void clear_counts() { |
| 3227 filter_trace_event_hit_count_ = 0; |
| 3228 end_event_hit_count_ = 0; |
| 3229 } |
| 3230 |
| 3221 private: | 3231 private: |
| 3222 static size_t filter_trace_event_hit_count_; | 3232 static size_t filter_trace_event_hit_count_; |
| 3223 static size_t end_event_hit_count_; | 3233 static size_t end_event_hit_count_; |
| 3234 static bool filter_return_value_; |
| 3224 }; | 3235 }; |
| 3225 | 3236 |
| 3226 size_t TestEventFilter::filter_trace_event_hit_count_ = 0; | 3237 size_t TestEventFilter::filter_trace_event_hit_count_ = 0; |
| 3227 size_t TestEventFilter::end_event_hit_count_ = 0; | 3238 size_t TestEventFilter::end_event_hit_count_ = 0; |
| 3239 bool TestEventFilter::filter_return_value_ = false; |
| 3228 | 3240 |
| 3229 std::unique_ptr<TraceLog::TraceEventFilter> ConstructTestEventFilter() { | 3241 std::unique_ptr<TraceLog::TraceEventFilter> ConstructTestEventFilter() { |
| 3230 return WrapUnique(new TestEventFilter); | 3242 return WrapUnique(new TestEventFilter); |
| 3231 } | 3243 } |
| 3232 | 3244 |
| 3245 TEST_F(TraceEventTestFixture, TraceFilteringMode) { |
| 3246 const char config_json[] = |
| 3247 "{" |
| 3248 " \"event_filters\": [" |
| 3249 " {" |
| 3250 " \"filter_predicate\": \"testing_predicate\", " |
| 3251 " \"included_categories\": [\"*\"]" |
| 3252 " }" |
| 3253 " ]" |
| 3254 "}"; |
| 3255 |
| 3256 // Run RECORDING_MODE within FILTERING_MODE: |
| 3257 TestEventFilter::set_filter_return_value(true); |
| 3258 TraceLog::SetTraceEventFilterConstructorForTesting(ConstructTestEventFilter); |
| 3259 |
| 3260 // Only filtering mode is enabled with test filters. |
| 3261 TraceLog::GetInstance()->SetEnabled(TraceConfig(config_json), |
| 3262 TraceLog::FILTERING_MODE); |
| 3263 EXPECT_EQ(TraceLog::FILTERING_MODE, TraceLog::GetInstance()->enabled_modes()); |
| 3264 { |
| 3265 void* ptr = this; |
| 3266 TRACE_EVENT0("c0", "name0"); |
| 3267 TRACE_EVENT_ASYNC_BEGIN0("c1", "name1", ptr); |
| 3268 TRACE_EVENT_INSTANT0("c0", "name0", TRACE_EVENT_SCOPE_THREAD); |
| 3269 TRACE_EVENT_ASYNC_END0("c1", "name1", ptr); |
| 3270 } |
| 3271 |
| 3272 // Recording mode is enabled when filtering mode is turned on. |
| 3273 TraceLog::GetInstance()->SetEnabled(TraceConfig("", ""), |
| 3274 TraceLog::RECORDING_MODE); |
| 3275 EXPECT_EQ(TraceLog::RECORDING_MODE | TraceLog::FILTERING_MODE, |
| 3276 TraceLog::GetInstance()->enabled_modes()); |
| 3277 { |
| 3278 TRACE_EVENT0("c2", "name2"); |
| 3279 } |
| 3280 // Only recording mode is disabled and filtering mode will continue to run. |
| 3281 TraceLog::GetInstance()->SetDisabled(TraceLog::RECORDING_MODE); |
| 3282 EXPECT_EQ(TraceLog::FILTERING_MODE, TraceLog::GetInstance()->enabled_modes()); |
| 3283 |
| 3284 { |
| 3285 TRACE_EVENT0("c0", "name0"); |
| 3286 } |
| 3287 // Filtering mode is disabled and no tracing mode should be enabled. |
| 3288 TraceLog::GetInstance()->SetDisabled(TraceLog::FILTERING_MODE); |
| 3289 EXPECT_EQ(0, TraceLog::GetInstance()->enabled_modes()); |
| 3290 |
| 3291 EndTraceAndFlush(); |
| 3292 EXPECT_FALSE(FindMatchingValue("cat", "c0")); |
| 3293 EXPECT_FALSE(FindMatchingValue("cat", "c1")); |
| 3294 EXPECT_FALSE(FindMatchingValue("name", "name0")); |
| 3295 EXPECT_FALSE(FindMatchingValue("name", "name1")); |
| 3296 EXPECT_TRUE(FindMatchingValue("cat", "c2")); |
| 3297 EXPECT_TRUE(FindMatchingValue("name", "name2")); |
| 3298 EXPECT_EQ(6u, TestEventFilter::filter_trace_event_hit_count()); |
| 3299 EXPECT_EQ(3u, TestEventFilter::end_event_hit_count()); |
| 3300 Clear(); |
| 3301 TestEventFilter::clear_counts(); |
| 3302 |
| 3303 // Run FILTERING_MODE within RECORDING_MODE: |
| 3304 // Only recording mode is enabled and all events must be recorded. |
| 3305 TraceLog::GetInstance()->SetEnabled(TraceConfig("", ""), |
| 3306 TraceLog::RECORDING_MODE); |
| 3307 EXPECT_EQ(TraceLog::RECORDING_MODE, TraceLog::GetInstance()->enabled_modes()); |
| 3308 { |
| 3309 TRACE_EVENT0("c0", "name0"); |
| 3310 } |
| 3311 |
| 3312 // Filtering mode is also enabled and all events must be filtered-out. |
| 3313 TestEventFilter::set_filter_return_value(false); |
| 3314 TraceLog::GetInstance()->SetEnabled(TraceConfig(config_json), |
| 3315 TraceLog::FILTERING_MODE); |
| 3316 EXPECT_EQ(TraceLog::RECORDING_MODE | TraceLog::FILTERING_MODE, |
| 3317 TraceLog::GetInstance()->enabled_modes()); |
| 3318 { |
| 3319 TRACE_EVENT0("c1", "name1"); |
| 3320 } |
| 3321 // Only filtering mode is disabled and recording mode should continue to run |
| 3322 // with all events being recorded. |
| 3323 TraceLog::GetInstance()->SetDisabled(TraceLog::FILTERING_MODE); |
| 3324 EXPECT_EQ(TraceLog::RECORDING_MODE, TraceLog::GetInstance()->enabled_modes()); |
| 3325 |
| 3326 { |
| 3327 TRACE_EVENT0("c2", "name2"); |
| 3328 } |
| 3329 // Recording mode is disabled and no tracing mode should be enabled. |
| 3330 TraceLog::GetInstance()->SetDisabled(TraceLog::RECORDING_MODE); |
| 3331 EXPECT_EQ(0, TraceLog::GetInstance()->enabled_modes()); |
| 3332 |
| 3333 EndTraceAndFlush(); |
| 3334 EXPECT_TRUE(FindMatchingValue("cat", "c0")); |
| 3335 EXPECT_TRUE(FindMatchingValue("cat", "c2")); |
| 3336 EXPECT_TRUE(FindMatchingValue("name", "name0")); |
| 3337 EXPECT_TRUE(FindMatchingValue("name", "name2")); |
| 3338 EXPECT_FALSE(FindMatchingValue("cat", "c1")); |
| 3339 EXPECT_FALSE(FindMatchingValue("name", "name1")); |
| 3340 EXPECT_EQ(1u, TestEventFilter::filter_trace_event_hit_count()); |
| 3341 EXPECT_EQ(1u, TestEventFilter::end_event_hit_count()); |
| 3342 Clear(); |
| 3343 TestEventFilter::clear_counts(); |
| 3344 } |
| 3345 |
| 3233 TEST_F(TraceEventTestFixture, EventFiltering) { | 3346 TEST_F(TraceEventTestFixture, EventFiltering) { |
| 3234 const char config_json[] = | 3347 const char config_json[] = |
| 3235 "{" | 3348 "{" |
| 3236 " \"included_categories\": [" | 3349 " \"included_categories\": [" |
| 3237 " \"filtered_cat\"," | 3350 " \"filtered_cat\"," |
| 3238 " \"unfiltered_cat\"]," | 3351 " \"unfiltered_cat\"]," |
| 3239 " \"event_filters\": [" | 3352 " \"event_filters\": [" |
| 3240 " {" | 3353 " {" |
| 3241 " \"filter_predicate\": \"testing_predicate\", " | 3354 " \"filter_predicate\": \"testing_predicate\", " |
| 3242 " \"included_categories\": [\"filtered_cat\"]" | 3355 " \"included_categories\": [\"filtered_cat\"]" |
| 3243 " }" | 3356 " }" |
| 3244 " " | 3357 " " |
| 3245 " ]" | 3358 " ]" |
| 3246 "}"; | 3359 "}"; |
| 3247 | 3360 |
| 3361 TestEventFilter::set_filter_return_value(true); |
| 3248 TraceLog::SetTraceEventFilterConstructorForTesting(ConstructTestEventFilter); | 3362 TraceLog::SetTraceEventFilterConstructorForTesting(ConstructTestEventFilter); |
| 3249 TraceConfig trace_config(config_json); | 3363 TraceConfig trace_config(config_json); |
| 3250 TraceLog::GetInstance()->SetEnabled(trace_config, TraceLog::RECORDING_MODE); | 3364 TraceLog::GetInstance()->SetEnabled( |
| 3365 trace_config, TraceLog::RECORDING_MODE | TraceLog::FILTERING_MODE); |
| 3251 ASSERT_TRUE(TraceLog::GetInstance()->IsEnabled()); | 3366 ASSERT_TRUE(TraceLog::GetInstance()->IsEnabled()); |
| 3252 | 3367 |
| 3253 TRACE_EVENT0("filtered_cat", "a snake"); | 3368 TRACE_EVENT0("filtered_cat", "a snake"); |
| 3254 TRACE_EVENT0("filtered_cat", "a mushroom"); | 3369 TRACE_EVENT0("filtered_cat", "a mushroom"); |
| 3255 TRACE_EVENT0("unfiltered_cat", "a horse"); | 3370 TRACE_EVENT0("unfiltered_cat", "a horse"); |
| 3256 | 3371 |
| 3257 // This is scoped so we can test the end event being filtered. | 3372 // This is scoped so we can test the end event being filtered. |
| 3258 { TRACE_EVENT0("filtered_cat", "another cat whoa"); } | 3373 { TRACE_EVENT0("filtered_cat", "another cat whoa"); } |
| 3259 | 3374 |
| 3260 EndTraceAndFlush(); | 3375 EndTraceAndFlush(); |
| 3261 | 3376 |
| 3262 EXPECT_EQ(3u, TestEventFilter::filter_trace_event_hit_count()); | 3377 EXPECT_EQ(3u, TestEventFilter::filter_trace_event_hit_count()); |
| 3263 EXPECT_EQ(1u, TestEventFilter::end_event_hit_count()); | 3378 EXPECT_EQ(1u, TestEventFilter::end_event_hit_count()); |
| 3379 TestEventFilter::clear_counts(); |
| 3264 } | 3380 } |
| 3265 | 3381 |
| 3266 TEST_F(TraceEventTestFixture, EventWhitelistFiltering) { | 3382 TEST_F(TraceEventTestFixture, EventWhitelistFiltering) { |
| 3267 std::string config_json = StringPrintf( | 3383 std::string config_json = StringPrintf( |
| 3268 "{" | 3384 "{" |
| 3269 " \"included_categories\": [" | 3385 " \"included_categories\": [" |
| 3270 " \"filtered_cat\"," | 3386 " \"filtered_cat\"," |
| 3271 " \"unfiltered_cat\"]," | 3387 " \"unfiltered_cat\"]," |
| 3272 " \"event_filters\": [" | 3388 " \"event_filters\": [" |
| 3273 " {" | 3389 " {" |
| 3274 " \"filter_predicate\": \"%s\", " | 3390 " \"filter_predicate\": \"%s\", " |
| 3275 " \"included_categories\": [\"*\"], " | 3391 " \"included_categories\": [\"*\"], " |
| 3276 " \"excluded_categories\": [\"unfiltered_cat\"], " | 3392 " \"excluded_categories\": [\"unfiltered_cat\"], " |
| 3277 " \"filter_args\": {" | 3393 " \"filter_args\": {" |
| 3278 " \"event_name_whitelist\": [\"a snake\", \"a dog\"]" | 3394 " \"event_name_whitelist\": [\"a snake\", \"a dog\"]" |
| 3279 " }" | 3395 " }" |
| 3280 " }" | 3396 " }" |
| 3281 " " | 3397 " " |
| 3282 " ]" | 3398 " ]" |
| 3283 "}", | 3399 "}", |
| 3284 TraceLog::TraceEventFilter::kEventWhitelistPredicate); | 3400 TraceLog::TraceEventFilter::kEventWhitelistPredicate); |
| 3285 | 3401 |
| 3286 TraceConfig trace_config(config_json); | 3402 TraceConfig trace_config(config_json); |
| 3287 TraceLog::GetInstance()->SetEnabled(trace_config, TraceLog::RECORDING_MODE); | 3403 TraceLog::GetInstance()->SetEnabled( |
| 3404 trace_config, TraceLog::RECORDING_MODE | TraceLog::FILTERING_MODE); |
| 3288 EXPECT_TRUE(TraceLog::GetInstance()->IsEnabled()); | 3405 EXPECT_TRUE(TraceLog::GetInstance()->IsEnabled()); |
| 3289 | 3406 |
| 3290 TRACE_EVENT0("filtered_cat", "a snake"); | 3407 TRACE_EVENT0("filtered_cat", "a snake"); |
| 3291 TRACE_EVENT0("filtered_cat", "a mushroom"); | 3408 TRACE_EVENT0("filtered_cat", "a mushroom"); |
| 3292 TRACE_EVENT0("unfiltered_cat", "a cat"); | 3409 TRACE_EVENT0("unfiltered_cat", "a cat"); |
| 3293 | 3410 |
| 3294 EndTraceAndFlush(); | 3411 EndTraceAndFlush(); |
| 3295 | 3412 |
| 3296 EXPECT_TRUE(FindMatchingValue("name", "a snake")); | 3413 EXPECT_TRUE(FindMatchingValue("name", "a snake")); |
| 3297 EXPECT_FALSE(FindMatchingValue("name", "a mushroom")); | 3414 EXPECT_FALSE(FindMatchingValue("name", "a mushroom")); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 3308 " \"event_filters\": [" | 3425 " \"event_filters\": [" |
| 3309 " {" | 3426 " {" |
| 3310 " \"filter_predicate\": \"%s\", " | 3427 " \"filter_predicate\": \"%s\", " |
| 3311 " \"included_categories\": [\"*\"]" | 3428 " \"included_categories\": [\"*\"]" |
| 3312 " }" | 3429 " }" |
| 3313 " ]" | 3430 " ]" |
| 3314 "}", | 3431 "}", |
| 3315 TraceLog::TraceEventFilter::kHeapProfilerPredicate); | 3432 TraceLog::TraceEventFilter::kHeapProfilerPredicate); |
| 3316 | 3433 |
| 3317 TraceConfig trace_config(config_json); | 3434 TraceConfig trace_config(config_json); |
| 3318 TraceLog::GetInstance()->SetEnabled(trace_config, TraceLog::RECORDING_MODE); | 3435 TraceLog::GetInstance()->SetEnabled( |
| 3436 trace_config, TraceLog::RECORDING_MODE | TraceLog::FILTERING_MODE); |
| 3319 EXPECT_TRUE(TraceLog::GetInstance()->IsEnabled()); | 3437 EXPECT_TRUE(TraceLog::GetInstance()->IsEnabled()); |
| 3320 | 3438 |
| 3321 TRACE_EVENT0("filtered_cat", "a snake"); | 3439 TRACE_EVENT0("filtered_cat", "a snake"); |
| 3322 TRACE_EVENT0("excluded_cat", "a mushroom"); | 3440 TRACE_EVENT0("excluded_cat", "a mushroom"); |
| 3323 TRACE_EVENT0("unfiltered_cat", "a cat"); | 3441 TRACE_EVENT0("unfiltered_cat", "a cat"); |
| 3324 | 3442 |
| 3325 EndTraceAndFlush(); | 3443 EndTraceAndFlush(); |
| 3326 | 3444 |
| 3327 // The predicate should not change behavior of the trace events. | 3445 // The predicate should not change behavior of the trace events. |
| 3328 EXPECT_TRUE(FindMatchingValue("name", "a snake")); | 3446 EXPECT_TRUE(FindMatchingValue("name", "a snake")); |
| 3329 EXPECT_FALSE(FindMatchingValue("name", "a mushroom")); | 3447 EXPECT_FALSE(FindMatchingValue("name", "a mushroom")); |
| 3330 EXPECT_TRUE(FindMatchingValue("name", "a cat")); | 3448 EXPECT_TRUE(FindMatchingValue("name", "a cat")); |
| 3331 } | 3449 } |
| 3332 | 3450 |
| 3333 TEST_F(TraceEventTestFixture, ClockSyncEventsAreAlwaysAddedToTrace) { | 3451 TEST_F(TraceEventTestFixture, ClockSyncEventsAreAlwaysAddedToTrace) { |
| 3334 BeginSpecificTrace("-*"); | 3452 BeginSpecificTrace("-*"); |
| 3335 TRACE_EVENT_CLOCK_SYNC_RECEIVER(1); | 3453 TRACE_EVENT_CLOCK_SYNC_RECEIVER(1); |
| 3336 EndTraceAndFlush(); | 3454 EndTraceAndFlush(); |
| 3337 EXPECT_TRUE(FindNamePhase("clock_sync", "c")); | 3455 EXPECT_TRUE(FindNamePhase("clock_sync", "c")); |
| 3338 } | 3456 } |
| 3339 | 3457 |
| 3340 } // namespace trace_event | 3458 } // namespace trace_event |
| 3341 } // namespace base | 3459 } // namespace base |
| OLD | NEW |