Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/debug/trace_event.h" | 5 #include "base/debug/trace_event.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
| 10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/process_util.h" | |
| 12 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
| 13 #include "base/synchronization/waitable_event.h" | 14 #include "base/synchronization/waitable_event.h" |
| 14 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
| 15 #include "base/values.h" | 16 #include "base/values.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 19 |
| 19 namespace base { | 20 namespace base { |
| 20 namespace debug { | 21 namespace debug { |
| 21 | 22 |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 169 if (!value || value->GetType() != Value::TYPE_DICTIONARY) | 170 if (!value || value->GetType() != Value::TYPE_DICTIONARY) |
| 170 continue; | 171 continue; |
| 171 DictionaryValue* dict = static_cast<DictionaryValue*>(value); | 172 DictionaryValue* dict = static_cast<DictionaryValue*>(value); |
| 172 | 173 |
| 173 if (IsStringInDict(string_to_match, dict)) | 174 if (IsStringInDict(string_to_match, dict)) |
| 174 return dict; | 175 return dict; |
| 175 } | 176 } |
| 176 return NULL; | 177 return NULL; |
| 177 } | 178 } |
| 178 | 179 |
| 180 std::vector<DictionaryValue*> FindTraceEntries( | |
| 181 const ListValue& trace_parsed, | |
| 182 const char *string_to_match) { | |
|
joth
2011/07/26 09:53:17
nit: most of this file attaches the * to the type
| |
| 183 std::vector<DictionaryValue*> hits; | |
| 184 size_t trace_parsed_count = trace_parsed.GetSize(); | |
| 185 for (size_t i = 0; i < trace_parsed_count; i++) { | |
| 186 Value* value = NULL; | |
| 187 trace_parsed.Get(i, &value); | |
| 188 if (!value || value->GetType() != Value::TYPE_DICTIONARY) | |
| 189 continue; | |
| 190 DictionaryValue* dict = static_cast<DictionaryValue*>(value); | |
| 191 | |
| 192 if (IsStringInDict(string_to_match, dict)) | |
| 193 hits.push_back(dict); | |
| 194 } | |
| 195 return hits; | |
| 196 } | |
| 197 | |
| 179 void DataCapturedCallTraces(WaitableEvent* task_complete_event) { | 198 void DataCapturedCallTraces(WaitableEvent* task_complete_event) { |
| 180 { | 199 { |
| 181 TRACE_EVENT_BEGIN_ETW("TRACE_EVENT_BEGIN_ETW call", 1122, "extrastring1"); | 200 TRACE_EVENT_BEGIN_ETW("TRACE_EVENT_BEGIN_ETW call", 1122, "extrastring1"); |
| 182 TRACE_EVENT_END_ETW("TRACE_EVENT_END_ETW call", 3344, "extrastring2"); | 201 TRACE_EVENT_END_ETW("TRACE_EVENT_END_ETW call", 3344, "extrastring2"); |
| 183 TRACE_EVENT_INSTANT_ETW("TRACE_EVENT_INSTANT_ETW call", | 202 TRACE_EVENT_INSTANT_ETW("TRACE_EVENT_INSTANT_ETW call", |
| 184 5566, "extrastring3"); | 203 5566, "extrastring3"); |
| 185 | 204 |
| 186 TRACE_EVENT0("all", "TRACE_EVENT0 call"); | 205 TRACE_EVENT0("all", "TRACE_EVENT0 call"); |
| 187 TRACE_EVENT1("all", "TRACE_EVENT1 call", "name1", "value1"); | 206 TRACE_EVENT1("all", "TRACE_EVENT1 call", "name1", "value1"); |
| 188 TRACE_EVENT2("all", "TRACE_EVENT2 call", | 207 TRACE_EVENT2("all", "TRACE_EVENT2 call", |
| (...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 486 for (int i = 0; i < num_threads; i++) { | 505 for (int i = 0; i < num_threads; i++) { |
| 487 threads[i]->Stop(); | 506 threads[i]->Stop(); |
| 488 delete threads[i]; | 507 delete threads[i]; |
| 489 delete task_complete_events[i]; | 508 delete task_complete_events[i]; |
| 490 } | 509 } |
| 491 | 510 |
| 492 DataCapturedValidateManyTraces(trace_parsed_, trace_string_, | 511 DataCapturedValidateManyTraces(trace_parsed_, trace_string_, |
| 493 num_threads, num_events); | 512 num_threads, num_events); |
| 494 } | 513 } |
| 495 | 514 |
| 515 // Test that thread and process names show up in the trace | |
| 516 TEST_F(TraceEventTestFixture, ThreadNames) { | |
| 517 ManualTestSetUp(); | |
| 518 | |
| 519 // Create threads before we enable tracing to make sure | |
| 520 // that tracelog still captures them. | |
| 521 const int num_threads = 4; | |
| 522 const int num_events = 10; | |
| 523 Thread* threads[num_threads]; | |
| 524 PlatformThreadId thread_ids[num_threads]; | |
| 525 for (int i = 0; i < num_threads; i++) | |
| 526 threads[i] = new Thread(StringPrintf("Thread %d", i).c_str()); | |
| 527 | |
| 528 // Enable tracing. | |
| 529 TraceLog::GetInstance()->SetEnabled(true); | |
| 530 | |
| 531 // Now run some trace code on these threads. | |
| 532 WaitableEvent* task_complete_events[num_threads]; | |
| 533 for (int i = 0; i < num_threads; i++) { | |
| 534 task_complete_events[i] = new WaitableEvent(false, false); | |
| 535 threads[i]->Start(); | |
| 536 thread_ids[i] = threads[i]->thread_id(); | |
| 537 threads[i]->message_loop()->PostTask( | |
| 538 FROM_HERE, NewRunnableFunction(&DataCapturedCallManyTraces, | |
| 539 i, num_events, task_complete_events[i])); | |
| 540 } | |
| 541 for (int i = 0; i < num_threads; i++) { | |
| 542 task_complete_events[i]->Wait(); | |
| 543 } | |
| 544 | |
| 545 // Shut things down. | |
| 546 TraceLog::GetInstance()->SetEnabled(false); | |
| 547 for (int i = 0; i < num_threads; i++) { | |
| 548 threads[i]->Stop(); | |
| 549 delete threads[i]; | |
| 550 delete task_complete_events[i]; | |
| 551 } | |
| 552 | |
| 553 std::string tmp; | |
| 554 int tmp_int; | |
| 555 DictionaryValue* item; | |
| 556 | |
| 557 // Make sure we get thread name metadata. | |
| 558 // Note, the test suite may have created a ton of threads. | |
| 559 // So, we'll have thread names for threads we didn't create. | |
| 560 std::vector<DictionaryValue*> items = | |
| 561 FindTraceEntries(trace_parsed_, "thread_name"); | |
| 562 for (int i = 0; i < static_cast<int>(items.size()); i++) { | |
| 563 item = items[i]; | |
|
joth
2011/07/26 09:53:17
maybe just ASSERT_TRUE(item) here, then you don't
| |
| 564 EXPECT_TRUE(item && item->GetInteger("tid", &tmp_int)); | |
| 565 | |
| 566 // See if this thread name is one of the threads we just created | |
| 567 for (int j = 0; j < num_threads; j++) { | |
| 568 if(static_cast<int>(thread_ids[j]) != tmp_int) | |
| 569 continue; | |
| 570 | |
| 571 std::string expected_name = StringPrintf("Thread %d", j).c_str(); | |
| 572 EXPECT_TRUE(item && item->GetString("ph", &tmp) && tmp == "M"); | |
| 573 EXPECT_TRUE(item && item->GetInteger("pid", &tmp_int) && | |
| 574 tmp_int == static_cast<int>(base::GetCurrentProcId())); | |
| 575 EXPECT_TRUE(item && item->GetString("args.name", &tmp) && | |
| 576 tmp == expected_name); | |
| 577 } | |
| 578 } | |
| 579 } | |
| 580 | |
| 581 namespace { | |
| 582 | |
| 496 void TraceCallsWithCachedCategoryPointersPointers(const char* name_str) { | 583 void TraceCallsWithCachedCategoryPointersPointers(const char* name_str) { |
| 497 TRACE_EVENT0("category name1", name_str); | 584 TRACE_EVENT0("category name1", name_str); |
| 498 TRACE_EVENT_INSTANT0("category name2", name_str); | 585 TRACE_EVENT_INSTANT0("category name2", name_str); |
| 499 TRACE_EVENT_BEGIN0("category name3", name_str); | 586 TRACE_EVENT_BEGIN0("category name3", name_str); |
| 500 TRACE_EVENT_END0("category name4", name_str); | 587 TRACE_EVENT_END0("category name4", name_str); |
| 501 } | 588 } |
| 502 | 589 |
| 590 | |
| 591 } // namespace | |
| 592 | |
| 593 | |
| 503 // Test trace calls made after tracing singleton shut down. | 594 // Test trace calls made after tracing singleton shut down. |
| 504 // | 595 // |
| 505 // The singleton is destroyed by our base::AtExitManager, but there can be | 596 // The singleton is destroyed by our base::AtExitManager, but there can be |
| 506 // code still executing as the C++ static objects are destroyed. This test | 597 // code still executing as the C++ static objects are destroyed. This test |
| 507 // forces the singleton to destroy early, and intentinally makes trace calls | 598 // forces the singleton to destroy early, and intentinally makes trace calls |
| 508 // afterwards. | 599 // afterwards. |
| 509 TEST_F(TraceEventTestFixture, AtExit) { | 600 TEST_F(TraceEventTestFixture, AtExit) { |
| 510 // Repeat this test a few times. Besides just showing robustness, it also | 601 // Repeat this test a few times. Besides just showing robustness, it also |
| 511 // allows us to test that events at shutdown do not appear with valid events | 602 // allows us to test that events at shutdown do not appear with valid events |
| 512 // recorded after the system is started again. | 603 // recorded after the system is started again. |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 627 | 718 |
| 628 std::string s; | 719 std::string s; |
| 629 EXPECT_TRUE(entry3->GetString("args.arg1", &s)); | 720 EXPECT_TRUE(entry3->GetString("args.arg1", &s)); |
| 630 EXPECT_EQ("val1", s); | 721 EXPECT_EQ("val1", s); |
| 631 EXPECT_TRUE(entry3->GetString("args.arg2", &s)); | 722 EXPECT_TRUE(entry3->GetString("args.arg2", &s)); |
| 632 EXPECT_EQ("val2", s); | 723 EXPECT_EQ("val2", s); |
| 633 } | 724 } |
| 634 | 725 |
| 635 } // namespace debug | 726 } // namespace debug |
| 636 } // namespace base | 727 } // namespace base |
| OLD | NEW |