| 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" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 scoped_refptr<TraceLog::RefCountedString> json_events_str); | 37 scoped_refptr<TraceLog::RefCountedString> json_events_str); |
| 38 bool FindMatchingTraceEntry(const JsonKeyValue* key_values); | 38 bool FindMatchingTraceEntry(const JsonKeyValue* key_values); |
| 39 bool FindNamePhase(const char* name, const char* phase); | 39 bool FindNamePhase(const char* name, const char* phase); |
| 40 | 40 |
| 41 std::string trace_string_; | 41 std::string trace_string_; |
| 42 ListValue trace_parsed_; | 42 ListValue trace_parsed_; |
| 43 | 43 |
| 44 private: | 44 private: |
| 45 // We want our singleton torn down after each test. | 45 // We want our singleton torn down after each test. |
| 46 ShadowingAtExitManager at_exit_manager_; | 46 ShadowingAtExitManager at_exit_manager_; |
| 47 Lock lock_; |
| 47 }; | 48 }; |
| 48 | 49 |
| 49 void TraceEventTestFixture::ManualTestSetUp() { | 50 void TraceEventTestFixture::ManualTestSetUp() { |
| 51 TraceLog::DeleteForTesting(); |
| 50 TraceLog::Resurrect(); | 52 TraceLog::Resurrect(); |
| 51 TraceLog* tracelog = TraceLog::GetInstance(); | 53 TraceLog* tracelog = TraceLog::GetInstance(); |
| 52 ASSERT_TRUE(tracelog); | 54 ASSERT_TRUE(tracelog); |
| 53 ASSERT_FALSE(tracelog->IsEnabled()); | 55 ASSERT_FALSE(tracelog->IsEnabled()); |
| 54 tracelog->SetOutputCallback( | 56 tracelog->SetOutputCallback( |
| 55 base::Bind(&TraceEventTestFixture::OnTraceDataCollected, | 57 base::Bind(&TraceEventTestFixture::OnTraceDataCollected, |
| 56 base::Unretained(this))); | 58 base::Unretained(this))); |
| 57 } | 59 } |
| 58 | 60 |
| 59 void TraceEventTestFixture::OnTraceDataCollected( | 61 void TraceEventTestFixture::OnTraceDataCollected( |
| 60 scoped_refptr<TraceLog::RefCountedString> json_events_str) { | 62 scoped_refptr<TraceLog::RefCountedString> json_events_str) { |
| 63 AutoLock lock(lock_); |
| 61 trace_string_ += json_events_str->data; | 64 trace_string_ += json_events_str->data; |
| 62 | 65 |
| 63 scoped_ptr<Value> root; | 66 scoped_ptr<Value> root; |
| 64 root.reset(base::JSONReader::Read(json_events_str->data, false)); | 67 root.reset(base::JSONReader::Read(json_events_str->data, false)); |
| 65 | 68 |
| 66 ListValue* root_list = NULL; | 69 ListValue* root_list = NULL; |
| 67 ASSERT_TRUE(root.get()); | 70 ASSERT_TRUE(root.get()); |
| 68 ASSERT_TRUE(root->GetAsList(&root_list)); | 71 ASSERT_TRUE(root->GetAsList(&root_list)); |
| 69 | 72 |
| 70 // Move items into our aggregate collection | 73 // Move items into our aggregate collection |
| (...skipping 451 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 522 TraceLog::GetInstance()->SetEnabled(true); | 525 TraceLog::GetInstance()->SetEnabled(true); |
| 523 | 526 |
| 524 Thread thread("1"); | 527 Thread thread("1"); |
| 525 WaitableEvent task_complete_event(false, false); | 528 WaitableEvent task_complete_event(false, false); |
| 526 thread.Start(); | 529 thread.Start(); |
| 527 | 530 |
| 528 thread.message_loop()->PostTask( | 531 thread.message_loop()->PostTask( |
| 529 FROM_HERE, NewRunnableFunction(&TraceWithAllMacroVariants, | 532 FROM_HERE, NewRunnableFunction(&TraceWithAllMacroVariants, |
| 530 &task_complete_event)); | 533 &task_complete_event)); |
| 531 task_complete_event.Wait(); | 534 task_complete_event.Wait(); |
| 535 thread.Stop(); |
| 532 | 536 |
| 533 TraceLog::GetInstance()->SetEnabled(false); | 537 TraceLog::GetInstance()->SetEnabled(false); |
| 534 thread.Stop(); | |
| 535 ValidateAllTraceMacrosCreatedData(trace_parsed_, trace_string_); | 538 ValidateAllTraceMacrosCreatedData(trace_parsed_, trace_string_); |
| 536 } | 539 } |
| 537 | 540 |
| 538 // Test that data sent from multiple threads is gathered | 541 // Test that data sent from multiple threads is gathered |
| 539 TEST_F(TraceEventTestFixture, DataCapturedManyThreads) { | 542 TEST_F(TraceEventTestFixture, DataCapturedManyThreads) { |
| 540 ManualTestSetUp(); | 543 ManualTestSetUp(); |
| 541 TraceLog::GetInstance()->SetEnabled(true); | 544 TraceLog::GetInstance()->SetEnabled(true); |
| 542 | 545 |
| 543 const int num_threads = 4; | 546 const int num_threads = 4; |
| 544 const int num_events = 4000; | 547 const int num_events = 4000; |
| 545 Thread* threads[num_threads]; | 548 Thread* threads[num_threads]; |
| 546 WaitableEvent* task_complete_events[num_threads]; | 549 WaitableEvent* task_complete_events[num_threads]; |
| 547 for (int i = 0; i < num_threads; i++) { | 550 for (int i = 0; i < num_threads; i++) { |
| 548 threads[i] = new Thread(StringPrintf("Thread %d", i).c_str()); | 551 threads[i] = new Thread(StringPrintf("Thread %d", i).c_str()); |
| 549 task_complete_events[i] = new WaitableEvent(false, false); | 552 task_complete_events[i] = new WaitableEvent(false, false); |
| 550 threads[i]->Start(); | 553 threads[i]->Start(); |
| 551 threads[i]->message_loop()->PostTask( | 554 threads[i]->message_loop()->PostTask( |
| 552 FROM_HERE, NewRunnableFunction(&TraceManyInstantEvents, | 555 FROM_HERE, NewRunnableFunction(&TraceManyInstantEvents, |
| 553 i, num_events, task_complete_events[i])); | 556 i, num_events, task_complete_events[i])); |
| 554 } | 557 } |
| 555 | 558 |
| 556 for (int i = 0; i < num_threads; i++) { | 559 for (int i = 0; i < num_threads; i++) { |
| 557 task_complete_events[i]->Wait(); | 560 task_complete_events[i]->Wait(); |
| 558 } | 561 } |
| 559 | 562 |
| 560 TraceLog::GetInstance()->SetEnabled(false); | |
| 561 | |
| 562 for (int i = 0; i < num_threads; i++) { | 563 for (int i = 0; i < num_threads; i++) { |
| 563 threads[i]->Stop(); | 564 threads[i]->Stop(); |
| 564 delete threads[i]; | 565 delete threads[i]; |
| 565 delete task_complete_events[i]; | 566 delete task_complete_events[i]; |
| 566 } | 567 } |
| 567 | 568 |
| 569 TraceLog::GetInstance()->SetEnabled(false); |
| 570 |
| 568 ValidateInstantEventPresentOnEveryThread(trace_parsed_, trace_string_, | 571 ValidateInstantEventPresentOnEveryThread(trace_parsed_, trace_string_, |
| 569 num_threads, num_events); | 572 num_threads, num_events); |
| 570 } | 573 } |
| 571 | 574 |
| 572 // Test that thread and process names show up in the trace | 575 // Test that thread and process names show up in the trace |
| 573 TEST_F(TraceEventTestFixture, ThreadNames) { | 576 TEST_F(TraceEventTestFixture, ThreadNames) { |
| 574 ManualTestSetUp(); | 577 ManualTestSetUp(); |
| 575 | 578 |
| 576 // Create threads before we enable tracing to make sure | 579 // Create threads before we enable tracing to make sure |
| 577 // that tracelog still captures them. | 580 // that tracelog still captures them. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 593 thread_ids[i] = threads[i]->thread_id(); | 596 thread_ids[i] = threads[i]->thread_id(); |
| 594 threads[i]->message_loop()->PostTask( | 597 threads[i]->message_loop()->PostTask( |
| 595 FROM_HERE, NewRunnableFunction(&TraceManyInstantEvents, | 598 FROM_HERE, NewRunnableFunction(&TraceManyInstantEvents, |
| 596 i, num_events, task_complete_events[i])); | 599 i, num_events, task_complete_events[i])); |
| 597 } | 600 } |
| 598 for (int i = 0; i < num_threads; i++) { | 601 for (int i = 0; i < num_threads; i++) { |
| 599 task_complete_events[i]->Wait(); | 602 task_complete_events[i]->Wait(); |
| 600 } | 603 } |
| 601 | 604 |
| 602 // Shut things down. | 605 // Shut things down. |
| 603 TraceLog::GetInstance()->SetEnabled(false); | |
| 604 for (int i = 0; i < num_threads; i++) { | 606 for (int i = 0; i < num_threads; i++) { |
| 605 threads[i]->Stop(); | 607 threads[i]->Stop(); |
| 606 delete threads[i]; | 608 delete threads[i]; |
| 607 delete task_complete_events[i]; | 609 delete task_complete_events[i]; |
| 608 } | 610 } |
| 609 | 611 |
| 612 TraceLog::GetInstance()->SetEnabled(false); |
| 613 |
| 610 std::string tmp; | 614 std::string tmp; |
| 611 int tmp_int; | 615 int tmp_int; |
| 612 DictionaryValue* item; | 616 DictionaryValue* item; |
| 613 | 617 |
| 614 // Make sure we get thread name metadata. | 618 // Make sure we get thread name metadata. |
| 615 // Note, the test suite may have created a ton of threads. | 619 // Note, the test suite may have created a ton of threads. |
| 616 // So, we'll have thread names for threads we didn't create. | 620 // So, we'll have thread names for threads we didn't create. |
| 617 std::vector<DictionaryValue*> items = | 621 std::vector<DictionaryValue*> items = |
| 618 FindTraceEntries(trace_parsed_, "thread_name"); | 622 FindTraceEntries(trace_parsed_, "thread_name"); |
| 619 for (int i = 0; i < static_cast<int>(items.size()); i++) { | 623 for (int i = 0; i < static_cast<int>(items.size()); i++) { |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 764 | 768 |
| 765 std::string s; | 769 std::string s; |
| 766 EXPECT_TRUE(entry3->GetString("args.arg1", &s)); | 770 EXPECT_TRUE(entry3->GetString("args.arg1", &s)); |
| 767 EXPECT_EQ("val1", s); | 771 EXPECT_EQ("val1", s); |
| 768 EXPECT_TRUE(entry3->GetString("args.arg2", &s)); | 772 EXPECT_TRUE(entry3->GetString("args.arg2", &s)); |
| 769 EXPECT_EQ("val2", s); | 773 EXPECT_EQ("val2", s); |
| 770 } | 774 } |
| 771 | 775 |
| 772 } // namespace debug | 776 } // namespace debug |
| 773 } // namespace base | 777 } // namespace base |
| OLD | NEW |