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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 trace_string_.clear(); | 51 trace_string_.clear(); |
52 trace_parsed_.Clear(); | 52 trace_parsed_.Clear(); |
53 } | 53 } |
54 | 54 |
55 std::string trace_string_; | 55 std::string trace_string_; |
56 ListValue trace_parsed_; | 56 ListValue trace_parsed_; |
57 | 57 |
58 private: | 58 private: |
59 // We want our singleton torn down after each test. | 59 // We want our singleton torn down after each test. |
60 ShadowingAtExitManager at_exit_manager_; | 60 ShadowingAtExitManager at_exit_manager_; |
| 61 Lock lock_; |
61 }; | 62 }; |
62 | 63 |
63 void TraceEventTestFixture::ManualTestSetUp() { | 64 void TraceEventTestFixture::ManualTestSetUp() { |
| 65 TraceLog::DeleteForTesting(); |
64 TraceLog::Resurrect(); | 66 TraceLog::Resurrect(); |
65 TraceLog* tracelog = TraceLog::GetInstance(); | 67 TraceLog* tracelog = TraceLog::GetInstance(); |
66 ASSERT_TRUE(tracelog); | 68 ASSERT_TRUE(tracelog); |
67 ASSERT_FALSE(tracelog->IsEnabled()); | 69 ASSERT_FALSE(tracelog->IsEnabled()); |
68 tracelog->SetOutputCallback( | 70 tracelog->SetOutputCallback( |
69 base::Bind(&TraceEventTestFixture::OnTraceDataCollected, | 71 base::Bind(&TraceEventTestFixture::OnTraceDataCollected, |
70 base::Unretained(this))); | 72 base::Unretained(this))); |
71 } | 73 } |
72 | 74 |
73 void TraceEventTestFixture::OnTraceDataCollected( | 75 void TraceEventTestFixture::OnTraceDataCollected( |
74 scoped_refptr<TraceLog::RefCountedString> json_events_str) { | 76 scoped_refptr<TraceLog::RefCountedString> json_events_str) { |
| 77 AutoLock lock(lock_); |
75 trace_string_ += json_events_str->data; | 78 trace_string_ += json_events_str->data; |
76 | 79 |
77 scoped_ptr<Value> root; | 80 scoped_ptr<Value> root; |
78 root.reset(base::JSONReader::Read(json_events_str->data, false)); | 81 root.reset(base::JSONReader::Read(json_events_str->data, false)); |
79 | 82 |
80 ListValue* root_list = NULL; | 83 ListValue* root_list = NULL; |
81 ASSERT_TRUE(root.get()); | 84 ASSERT_TRUE(root.get()); |
82 ASSERT_TRUE(root->GetAsList(&root_list)); | 85 ASSERT_TRUE(root->GetAsList(&root_list)); |
83 | 86 |
84 // Move items into our aggregate collection | 87 // Move items into our aggregate collection |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 | 279 |
277 if (task_complete_event) | 280 if (task_complete_event) |
278 task_complete_event->Signal(); | 281 task_complete_event->Signal(); |
279 } | 282 } |
280 | 283 |
281 void ValidateAllTraceMacrosCreatedData(const ListValue& trace_parsed, | 284 void ValidateAllTraceMacrosCreatedData(const ListValue& trace_parsed, |
282 const std::string& trace_string) { | 285 const std::string& trace_string) { |
283 DictionaryValue* item = NULL; | 286 DictionaryValue* item = NULL; |
284 | 287 |
285 #define EXPECT_FIND_(string) \ | 288 #define EXPECT_FIND_(string) \ |
286 EXPECT_TRUE((item = FindTraceEntry(trace_parsed, string))); | 289 EXPECT_TRUE((item = FindTraceEntry(trace_parsed, string))); |
287 #define EXPECT_NOT_FIND_(string) \ | 290 #define EXPECT_NOT_FIND_(string) \ |
288 EXPECT_FALSE((item = FindTraceEntry(trace_parsed, string))); | 291 EXPECT_FALSE((item = FindTraceEntry(trace_parsed, string))); |
289 #define EXPECT_SUB_FIND_(string) \ | 292 #define EXPECT_SUB_FIND_(string) \ |
290 if (item) EXPECT_TRUE((IsStringInDict(string, item))); | 293 if (item) EXPECT_TRUE((IsStringInDict(string, item))); |
291 | 294 |
292 EXPECT_FIND_("ETW Trace Event"); | 295 EXPECT_FIND_("ETW Trace Event"); |
293 EXPECT_FIND_("all"); | 296 EXPECT_FIND_("all"); |
294 EXPECT_FIND_("TRACE_EVENT_BEGIN_ETW call"); | 297 EXPECT_FIND_("TRACE_EVENT_BEGIN_ETW call"); |
295 { | 298 { |
296 int int_val = 0; | 299 int int_val = 0; |
297 EXPECT_TRUE(item && item->GetInteger("args.id", &int_val)); | 300 EXPECT_TRUE(item && item->GetInteger("args.id", &int_val)); |
298 EXPECT_EQ(1122, int_val); | 301 EXPECT_EQ(1122, int_val); |
299 } | 302 } |
300 EXPECT_SUB_FIND_("extrastring1"); | 303 EXPECT_SUB_FIND_("extrastring1"); |
(...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
672 // Test that data sent from other threads is gathered | 675 // Test that data sent from other threads is gathered |
673 TEST_F(TraceEventTestFixture, DataCapturedOnThread) { | 676 TEST_F(TraceEventTestFixture, DataCapturedOnThread) { |
674 ManualTestSetUp(); | 677 ManualTestSetUp(); |
675 TraceLog::GetInstance()->SetEnabled(true); | 678 TraceLog::GetInstance()->SetEnabled(true); |
676 | 679 |
677 Thread thread("1"); | 680 Thread thread("1"); |
678 WaitableEvent task_complete_event(false, false); | 681 WaitableEvent task_complete_event(false, false); |
679 thread.Start(); | 682 thread.Start(); |
680 | 683 |
681 thread.message_loop()->PostTask( | 684 thread.message_loop()->PostTask( |
682 FROM_HERE, NewRunnableFunction(&TraceWithAllMacroVariants, | 685 FROM_HERE, NewRunnableFunction(&TraceWithAllMacroVariants, |
683 &task_complete_event)); | 686 &task_complete_event)); |
684 task_complete_event.Wait(); | 687 task_complete_event.Wait(); |
| 688 thread.Stop(); |
685 | 689 |
686 TraceLog::GetInstance()->SetEnabled(false); | 690 TraceLog::GetInstance()->SetEnabled(false); |
687 thread.Stop(); | |
688 ValidateAllTraceMacrosCreatedData(trace_parsed_, trace_string_); | 691 ValidateAllTraceMacrosCreatedData(trace_parsed_, trace_string_); |
689 } | 692 } |
690 | 693 |
691 // Test that data sent from multiple threads is gathered | 694 // Test that data sent from multiple threads is gathered |
692 TEST_F(TraceEventTestFixture, DataCapturedManyThreads) { | 695 TEST_F(TraceEventTestFixture, DataCapturedManyThreads) { |
693 ManualTestSetUp(); | 696 ManualTestSetUp(); |
694 TraceLog::GetInstance()->SetEnabled(true); | 697 TraceLog::GetInstance()->SetEnabled(true); |
695 | 698 |
696 const int num_threads = 4; | 699 const int num_threads = 4; |
697 const int num_events = 4000; | 700 const int num_events = 4000; |
698 Thread* threads[num_threads]; | 701 Thread* threads[num_threads]; |
699 WaitableEvent* task_complete_events[num_threads]; | 702 WaitableEvent* task_complete_events[num_threads]; |
700 for (int i = 0; i < num_threads; i++) { | 703 for (int i = 0; i < num_threads; i++) { |
701 threads[i] = new Thread(StringPrintf("Thread %d", i).c_str()); | 704 threads[i] = new Thread(StringPrintf("Thread %d", i).c_str()); |
702 task_complete_events[i] = new WaitableEvent(false, false); | 705 task_complete_events[i] = new WaitableEvent(false, false); |
703 threads[i]->Start(); | 706 threads[i]->Start(); |
704 threads[i]->message_loop()->PostTask( | 707 threads[i]->message_loop()->PostTask( |
705 FROM_HERE, NewRunnableFunction(&TraceManyInstantEvents, | 708 FROM_HERE, NewRunnableFunction(&TraceManyInstantEvents, |
706 i, num_events, task_complete_events[i])); | 709 i, num_events, task_complete_events[i])); |
707 } | 710 } |
708 | 711 |
709 for (int i = 0; i < num_threads; i++) { | 712 for (int i = 0; i < num_threads; i++) { |
710 task_complete_events[i]->Wait(); | 713 task_complete_events[i]->Wait(); |
711 } | 714 } |
712 | 715 |
713 TraceLog::GetInstance()->SetEnabled(false); | |
714 | |
715 for (int i = 0; i < num_threads; i++) { | 716 for (int i = 0; i < num_threads; i++) { |
716 threads[i]->Stop(); | 717 threads[i]->Stop(); |
717 delete threads[i]; | 718 delete threads[i]; |
718 delete task_complete_events[i]; | 719 delete task_complete_events[i]; |
719 } | 720 } |
720 | 721 |
| 722 TraceLog::GetInstance()->SetEnabled(false); |
| 723 |
721 ValidateInstantEventPresentOnEveryThread(trace_parsed_, trace_string_, | 724 ValidateInstantEventPresentOnEveryThread(trace_parsed_, trace_string_, |
722 num_threads, num_events); | 725 num_threads, num_events); |
723 } | 726 } |
724 | 727 |
725 // Test that thread and process names show up in the trace | 728 // Test that thread and process names show up in the trace |
726 TEST_F(TraceEventTestFixture, ThreadNames) { | 729 TEST_F(TraceEventTestFixture, ThreadNames) { |
727 ManualTestSetUp(); | 730 ManualTestSetUp(); |
728 | 731 |
729 // Create threads before we enable tracing to make sure | 732 // Create threads before we enable tracing to make sure |
730 // that tracelog still captures them. | 733 // that tracelog still captures them. |
731 const int num_threads = 4; | 734 const int num_threads = 4; |
732 const int num_events = 10; | 735 const int num_events = 10; |
733 Thread* threads[num_threads]; | 736 Thread* threads[num_threads]; |
734 PlatformThreadId thread_ids[num_threads]; | 737 PlatformThreadId thread_ids[num_threads]; |
735 for (int i = 0; i < num_threads; i++) | 738 for (int i = 0; i < num_threads; i++) |
736 threads[i] = new Thread(StringPrintf("Thread %d", i).c_str()); | 739 threads[i] = new Thread(StringPrintf("Thread %d", i).c_str()); |
737 | 740 |
738 // Enable tracing. | 741 // Enable tracing. |
739 TraceLog::GetInstance()->SetEnabled(true); | 742 TraceLog::GetInstance()->SetEnabled(true); |
740 | 743 |
741 // Now run some trace code on these threads. | 744 // Now run some trace code on these threads. |
742 WaitableEvent* task_complete_events[num_threads]; | 745 WaitableEvent* task_complete_events[num_threads]; |
743 for (int i = 0; i < num_threads; i++) { | 746 for (int i = 0; i < num_threads; i++) { |
744 task_complete_events[i] = new WaitableEvent(false, false); | 747 task_complete_events[i] = new WaitableEvent(false, false); |
745 threads[i]->Start(); | 748 threads[i]->Start(); |
746 thread_ids[i] = threads[i]->thread_id(); | 749 thread_ids[i] = threads[i]->thread_id(); |
747 threads[i]->message_loop()->PostTask( | 750 threads[i]->message_loop()->PostTask( |
748 FROM_HERE, NewRunnableFunction(&TraceManyInstantEvents, | 751 FROM_HERE, NewRunnableFunction(&TraceManyInstantEvents, |
749 i, num_events, task_complete_events[i])); | 752 i, num_events, task_complete_events[i])); |
750 } | 753 } |
751 for (int i = 0; i < num_threads; i++) { | 754 for (int i = 0; i < num_threads; i++) { |
752 task_complete_events[i]->Wait(); | 755 task_complete_events[i]->Wait(); |
753 } | 756 } |
754 | 757 |
755 // Shut things down. | 758 // Shut things down. |
756 TraceLog::GetInstance()->SetEnabled(false); | |
757 for (int i = 0; i < num_threads; i++) { | 759 for (int i = 0; i < num_threads; i++) { |
758 threads[i]->Stop(); | 760 threads[i]->Stop(); |
759 delete threads[i]; | 761 delete threads[i]; |
760 delete task_complete_events[i]; | 762 delete task_complete_events[i]; |
761 } | 763 } |
762 | 764 |
| 765 TraceLog::GetInstance()->SetEnabled(false); |
| 766 |
763 std::string tmp; | 767 std::string tmp; |
764 int tmp_int; | 768 int tmp_int; |
765 DictionaryValue* item; | 769 DictionaryValue* item; |
766 | 770 |
767 // Make sure we get thread name metadata. | 771 // Make sure we get thread name metadata. |
768 // Note, the test suite may have created a ton of threads. | 772 // Note, the test suite may have created a ton of threads. |
769 // So, we'll have thread names for threads we didn't create. | 773 // So, we'll have thread names for threads we didn't create. |
770 std::vector<DictionaryValue*> items = | 774 std::vector<DictionaryValue*> items = |
771 FindTraceEntries(trace_parsed_, "thread_name"); | 775 FindTraceEntries(trace_parsed_, "thread_name"); |
772 for (int i = 0; i < static_cast<int>(items.size()); i++) { | 776 for (int i = 0; i < static_cast<int>(items.size()); i++) { |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
917 | 921 |
918 std::string s; | 922 std::string s; |
919 EXPECT_TRUE(entry3->GetString("args.arg1", &s)); | 923 EXPECT_TRUE(entry3->GetString("args.arg1", &s)); |
920 EXPECT_EQ("val1", s); | 924 EXPECT_EQ("val1", s); |
921 EXPECT_TRUE(entry3->GetString("args.arg2", &s)); | 925 EXPECT_TRUE(entry3->GetString("args.arg2", &s)); |
922 EXPECT_EQ("val2", s); | 926 EXPECT_EQ("val2", s); |
923 } | 927 } |
924 | 928 |
925 } // namespace debug | 929 } // namespace debug |
926 } // namespace base | 930 } // namespace base |
OLD | NEW |