Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(56)

Side by Side Diff: base/trace_event/trace_event_unittest.cc

Issue 2415183004: Revert of [tracing] Add filtering mode in TraceLog (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/trace_event/trace_config.cc ('k') | base/trace_event/trace_log.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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(TraceLog::RECORDING_MODE | 139 TraceLog::GetInstance()->SetDisabled();
140 TraceLog::FILTERING_MODE);
141 TraceLog::GetInstance()->Flush( 140 TraceLog::GetInstance()->Flush(
142 base::Bind(&TraceEventTestFixture::OnTraceDataCollected, 141 base::Bind(&TraceEventTestFixture::OnTraceDataCollected,
143 base::Unretained(static_cast<TraceEventTestFixture*>(this)), 142 base::Unretained(static_cast<TraceEventTestFixture*>(this)),
144 base::Unretained(flush_complete_event))); 143 base::Unretained(flush_complete_event)));
145 } 144 }
146 145
147 void SetUp() override { 146 void SetUp() override {
148 const char* name = PlatformThread::GetName(); 147 const char* name = PlatformThread::GetName();
149 old_thread_name_ = name ? strdup(name) : NULL; 148 old_thread_name_ = name ? strdup(name) : NULL;
150 149
(...skipping 3049 matching lines...) Expand 10 before | Expand all | Expand 10 after
3200 TEST_F(TraceEventTestFixture, SyntheticDelayConfigurationToString) { 3199 TEST_F(TraceEventTestFixture, SyntheticDelayConfigurationToString) {
3201 const char filter[] = "DELAY(test.Delay;16;oneshot)"; 3200 const char filter[] = "DELAY(test.Delay;16;oneshot)";
3202 TraceConfig config(filter, ""); 3201 TraceConfig config(filter, "");
3203 EXPECT_EQ(filter, config.ToCategoryFilterString()); 3202 EXPECT_EQ(filter, config.ToCategoryFilterString());
3204 } 3203 }
3205 3204
3206 class TestEventFilter : public TraceLog::TraceEventFilter { 3205 class TestEventFilter : public TraceLog::TraceEventFilter {
3207 public: 3206 public:
3208 bool FilterTraceEvent(const TraceEvent& trace_event) const override { 3207 bool FilterTraceEvent(const TraceEvent& trace_event) const override {
3209 filter_trace_event_hit_count_++; 3208 filter_trace_event_hit_count_++;
3210 return filter_return_value_; 3209 return true;
3211 } 3210 }
3212 3211
3213 void EndEvent(const char* category_group, const char* name) override { 3212 void EndEvent(const char* category_group, const char* name) override {
3214 end_event_hit_count_++; 3213 end_event_hit_count_++;
3215 } 3214 }
3216 3215
3217 static void set_filter_return_value(bool value) {
3218 filter_return_value_ = value;
3219 }
3220
3221 static size_t filter_trace_event_hit_count() { 3216 static size_t filter_trace_event_hit_count() {
3222 return filter_trace_event_hit_count_; 3217 return filter_trace_event_hit_count_;
3223 } 3218 }
3224 static size_t end_event_hit_count() { return end_event_hit_count_; } 3219 static size_t end_event_hit_count() { return end_event_hit_count_; }
3225 3220
3226 static void clear_counts() {
3227 filter_trace_event_hit_count_ = 0;
3228 end_event_hit_count_ = 0;
3229 }
3230
3231 private: 3221 private:
3232 static size_t filter_trace_event_hit_count_; 3222 static size_t filter_trace_event_hit_count_;
3233 static size_t end_event_hit_count_; 3223 static size_t end_event_hit_count_;
3234 static bool filter_return_value_;
3235 }; 3224 };
3236 3225
3237 size_t TestEventFilter::filter_trace_event_hit_count_ = 0; 3226 size_t TestEventFilter::filter_trace_event_hit_count_ = 0;
3238 size_t TestEventFilter::end_event_hit_count_ = 0; 3227 size_t TestEventFilter::end_event_hit_count_ = 0;
3239 bool TestEventFilter::filter_return_value_ = false;
3240 3228
3241 std::unique_ptr<TraceLog::TraceEventFilter> ConstructTestEventFilter() { 3229 std::unique_ptr<TraceLog::TraceEventFilter> ConstructTestEventFilter() {
3242 return WrapUnique(new TestEventFilter); 3230 return WrapUnique(new TestEventFilter);
3243 } 3231 }
3244 3232
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
3346 TEST_F(TraceEventTestFixture, EventFiltering) { 3233 TEST_F(TraceEventTestFixture, EventFiltering) {
3347 const char config_json[] = 3234 const char config_json[] =
3348 "{" 3235 "{"
3349 " \"included_categories\": [" 3236 " \"included_categories\": ["
3350 " \"filtered_cat\"," 3237 " \"filtered_cat\","
3351 " \"unfiltered_cat\"]," 3238 " \"unfiltered_cat\"],"
3352 " \"event_filters\": [" 3239 " \"event_filters\": ["
3353 " {" 3240 " {"
3354 " \"filter_predicate\": \"testing_predicate\", " 3241 " \"filter_predicate\": \"testing_predicate\", "
3355 " \"included_categories\": [\"filtered_cat\"]" 3242 " \"included_categories\": [\"filtered_cat\"]"
3356 " }" 3243 " }"
3357 " " 3244 " "
3358 " ]" 3245 " ]"
3359 "}"; 3246 "}";
3360 3247
3361 TestEventFilter::set_filter_return_value(true);
3362 TraceLog::SetTraceEventFilterConstructorForTesting(ConstructTestEventFilter); 3248 TraceLog::SetTraceEventFilterConstructorForTesting(ConstructTestEventFilter);
3363 TraceConfig trace_config(config_json); 3249 TraceConfig trace_config(config_json);
3364 TraceLog::GetInstance()->SetEnabled( 3250 TraceLog::GetInstance()->SetEnabled(trace_config, TraceLog::RECORDING_MODE);
3365 trace_config, TraceLog::RECORDING_MODE | TraceLog::FILTERING_MODE);
3366 ASSERT_TRUE(TraceLog::GetInstance()->IsEnabled()); 3251 ASSERT_TRUE(TraceLog::GetInstance()->IsEnabled());
3367 3252
3368 TRACE_EVENT0("filtered_cat", "a snake"); 3253 TRACE_EVENT0("filtered_cat", "a snake");
3369 TRACE_EVENT0("filtered_cat", "a mushroom"); 3254 TRACE_EVENT0("filtered_cat", "a mushroom");
3370 TRACE_EVENT0("unfiltered_cat", "a horse"); 3255 TRACE_EVENT0("unfiltered_cat", "a horse");
3371 3256
3372 // This is scoped so we can test the end event being filtered. 3257 // This is scoped so we can test the end event being filtered.
3373 { TRACE_EVENT0("filtered_cat", "another cat whoa"); } 3258 { TRACE_EVENT0("filtered_cat", "another cat whoa"); }
3374 3259
3375 EndTraceAndFlush(); 3260 EndTraceAndFlush();
3376 3261
3377 EXPECT_EQ(3u, TestEventFilter::filter_trace_event_hit_count()); 3262 EXPECT_EQ(3u, TestEventFilter::filter_trace_event_hit_count());
3378 EXPECT_EQ(1u, TestEventFilter::end_event_hit_count()); 3263 EXPECT_EQ(1u, TestEventFilter::end_event_hit_count());
3379 TestEventFilter::clear_counts();
3380 } 3264 }
3381 3265
3382 TEST_F(TraceEventTestFixture, EventWhitelistFiltering) { 3266 TEST_F(TraceEventTestFixture, EventWhitelistFiltering) {
3383 std::string config_json = StringPrintf( 3267 std::string config_json = StringPrintf(
3384 "{" 3268 "{"
3385 " \"included_categories\": [" 3269 " \"included_categories\": ["
3386 " \"filtered_cat\"," 3270 " \"filtered_cat\","
3387 " \"unfiltered_cat\"]," 3271 " \"unfiltered_cat\"],"
3388 " \"event_filters\": [" 3272 " \"event_filters\": ["
3389 " {" 3273 " {"
3390 " \"filter_predicate\": \"%s\", " 3274 " \"filter_predicate\": \"%s\", "
3391 " \"included_categories\": [\"*\"], " 3275 " \"included_categories\": [\"*\"], "
3392 " \"excluded_categories\": [\"unfiltered_cat\"], " 3276 " \"excluded_categories\": [\"unfiltered_cat\"], "
3393 " \"filter_args\": {" 3277 " \"filter_args\": {"
3394 " \"event_name_whitelist\": [\"a snake\", \"a dog\"]" 3278 " \"event_name_whitelist\": [\"a snake\", \"a dog\"]"
3395 " }" 3279 " }"
3396 " }" 3280 " }"
3397 " " 3281 " "
3398 " ]" 3282 " ]"
3399 "}", 3283 "}",
3400 TraceLog::TraceEventFilter::kEventWhitelistPredicate); 3284 TraceLog::TraceEventFilter::kEventWhitelistPredicate);
3401 3285
3402 TraceConfig trace_config(config_json); 3286 TraceConfig trace_config(config_json);
3403 TraceLog::GetInstance()->SetEnabled( 3287 TraceLog::GetInstance()->SetEnabled(trace_config, TraceLog::RECORDING_MODE);
3404 trace_config, TraceLog::RECORDING_MODE | TraceLog::FILTERING_MODE);
3405 EXPECT_TRUE(TraceLog::GetInstance()->IsEnabled()); 3288 EXPECT_TRUE(TraceLog::GetInstance()->IsEnabled());
3406 3289
3407 TRACE_EVENT0("filtered_cat", "a snake"); 3290 TRACE_EVENT0("filtered_cat", "a snake");
3408 TRACE_EVENT0("filtered_cat", "a mushroom"); 3291 TRACE_EVENT0("filtered_cat", "a mushroom");
3409 TRACE_EVENT0("unfiltered_cat", "a cat"); 3292 TRACE_EVENT0("unfiltered_cat", "a cat");
3410 3293
3411 EndTraceAndFlush(); 3294 EndTraceAndFlush();
3412 3295
3413 EXPECT_TRUE(FindMatchingValue("name", "a snake")); 3296 EXPECT_TRUE(FindMatchingValue("name", "a snake"));
3414 EXPECT_FALSE(FindMatchingValue("name", "a mushroom")); 3297 EXPECT_FALSE(FindMatchingValue("name", "a mushroom"));
(...skipping 10 matching lines...) Expand all
3425 " \"event_filters\": [" 3308 " \"event_filters\": ["
3426 " {" 3309 " {"
3427 " \"filter_predicate\": \"%s\", " 3310 " \"filter_predicate\": \"%s\", "
3428 " \"included_categories\": [\"*\"]" 3311 " \"included_categories\": [\"*\"]"
3429 " }" 3312 " }"
3430 " ]" 3313 " ]"
3431 "}", 3314 "}",
3432 TraceLog::TraceEventFilter::kHeapProfilerPredicate); 3315 TraceLog::TraceEventFilter::kHeapProfilerPredicate);
3433 3316
3434 TraceConfig trace_config(config_json); 3317 TraceConfig trace_config(config_json);
3435 TraceLog::GetInstance()->SetEnabled( 3318 TraceLog::GetInstance()->SetEnabled(trace_config, TraceLog::RECORDING_MODE);
3436 trace_config, TraceLog::RECORDING_MODE | TraceLog::FILTERING_MODE);
3437 EXPECT_TRUE(TraceLog::GetInstance()->IsEnabled()); 3319 EXPECT_TRUE(TraceLog::GetInstance()->IsEnabled());
3438 3320
3439 TRACE_EVENT0("filtered_cat", "a snake"); 3321 TRACE_EVENT0("filtered_cat", "a snake");
3440 TRACE_EVENT0("excluded_cat", "a mushroom"); 3322 TRACE_EVENT0("excluded_cat", "a mushroom");
3441 TRACE_EVENT0("unfiltered_cat", "a cat"); 3323 TRACE_EVENT0("unfiltered_cat", "a cat");
3442 3324
3443 EndTraceAndFlush(); 3325 EndTraceAndFlush();
3444 3326
3445 // The predicate should not change behavior of the trace events. 3327 // The predicate should not change behavior of the trace events.
3446 EXPECT_TRUE(FindMatchingValue("name", "a snake")); 3328 EXPECT_TRUE(FindMatchingValue("name", "a snake"));
3447 EXPECT_FALSE(FindMatchingValue("name", "a mushroom")); 3329 EXPECT_FALSE(FindMatchingValue("name", "a mushroom"));
3448 EXPECT_TRUE(FindMatchingValue("name", "a cat")); 3330 EXPECT_TRUE(FindMatchingValue("name", "a cat"));
3449 } 3331 }
3450 3332
3451 TEST_F(TraceEventTestFixture, ClockSyncEventsAreAlwaysAddedToTrace) { 3333 TEST_F(TraceEventTestFixture, ClockSyncEventsAreAlwaysAddedToTrace) {
3452 BeginSpecificTrace("-*"); 3334 BeginSpecificTrace("-*");
3453 TRACE_EVENT_CLOCK_SYNC_RECEIVER(1); 3335 TRACE_EVENT_CLOCK_SYNC_RECEIVER(1);
3454 EndTraceAndFlush(); 3336 EndTraceAndFlush();
3455 EXPECT_TRUE(FindNamePhase("clock_sync", "c")); 3337 EXPECT_TRUE(FindNamePhase("clock_sync", "c"));
3456 } 3338 }
3457 3339
3458 } // namespace trace_event 3340 } // namespace trace_event
3459 } // namespace base 3341 } // namespace base
OLDNEW
« no previous file with comments | « base/trace_event/trace_config.cc ('k') | base/trace_event/trace_log.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698