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/ref_counted_memory.h" | 11 #include "base/memory/ref_counted_memory.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/memory/singleton.h" |
13 #include "base/process_util.h" | 14 #include "base/process_util.h" |
14 #include "base/stringprintf.h" | 15 #include "base/stringprintf.h" |
15 #include "base/synchronization/waitable_event.h" | 16 #include "base/synchronization/waitable_event.h" |
| 17 #include "base/threading/platform_thread.h" |
16 #include "base/threading/thread.h" | 18 #include "base/threading/thread.h" |
17 #include "base/values.h" | 19 #include "base/values.h" |
18 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
20 | 22 |
21 namespace base { | 23 namespace base { |
22 namespace debug { | 24 namespace debug { |
23 | 25 |
24 namespace { | 26 namespace { |
25 | 27 |
(...skipping 19 matching lines...) Expand all Loading... |
45 bool FindNamePhase(const char* name, const char* phase); | 47 bool FindNamePhase(const char* name, const char* phase); |
46 bool FindMatchingValue(const char* key, | 48 bool FindMatchingValue(const char* key, |
47 const char* value); | 49 const char* value); |
48 bool FindNonMatchingValue(const char* key, | 50 bool FindNonMatchingValue(const char* key, |
49 const char* value); | 51 const char* value); |
50 void Clear() { | 52 void Clear() { |
51 trace_parsed_.Clear(); | 53 trace_parsed_.Clear(); |
52 json_output_.json_output.clear(); | 54 json_output_.json_output.clear(); |
53 } | 55 } |
54 | 56 |
| 57 virtual void SetUp() { |
| 58 old_thread_name_ = PlatformThread::GetName(); |
| 59 } |
| 60 virtual void TearDown() { |
| 61 PlatformThread::SetName(old_thread_name_); |
| 62 } |
| 63 |
| 64 const char* old_thread_name_; |
55 ListValue trace_parsed_; | 65 ListValue trace_parsed_; |
56 base::debug::TraceResultBuffer trace_buffer_; | 66 base::debug::TraceResultBuffer trace_buffer_; |
57 base::debug::TraceResultBuffer::SimpleOutput json_output_; | 67 base::debug::TraceResultBuffer::SimpleOutput json_output_; |
58 | 68 |
59 private: | 69 private: |
60 // We want our singleton torn down after each test. | 70 // We want our singleton torn down after each test. |
61 ShadowingAtExitManager at_exit_manager_; | 71 ShadowingAtExitManager at_exit_manager_; |
62 Lock lock_; | 72 Lock lock_; |
63 }; | 73 }; |
64 | 74 |
(...skipping 706 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
771 int tmp_int; | 781 int tmp_int; |
772 DictionaryValue* item; | 782 DictionaryValue* item; |
773 | 783 |
774 // Make sure we get thread name metadata. | 784 // Make sure we get thread name metadata. |
775 // Note, the test suite may have created a ton of threads. | 785 // Note, the test suite may have created a ton of threads. |
776 // So, we'll have thread names for threads we didn't create. | 786 // So, we'll have thread names for threads we didn't create. |
777 std::vector<DictionaryValue*> items = | 787 std::vector<DictionaryValue*> items = |
778 FindTraceEntries(trace_parsed_, "thread_name"); | 788 FindTraceEntries(trace_parsed_, "thread_name"); |
779 for (int i = 0; i < static_cast<int>(items.size()); i++) { | 789 for (int i = 0; i < static_cast<int>(items.size()); i++) { |
780 item = items[i]; | 790 item = items[i]; |
781 EXPECT_TRUE(item); | 791 ASSERT_TRUE(item); |
782 EXPECT_TRUE(item->GetInteger("tid", &tmp_int)); | 792 EXPECT_TRUE(item->GetInteger("tid", &tmp_int)); |
783 | 793 |
784 // See if this thread name is one of the threads we just created | 794 // See if this thread name is one of the threads we just created |
785 for (int j = 0; j < num_threads; j++) { | 795 for (int j = 0; j < num_threads; j++) { |
786 if(static_cast<int>(thread_ids[j]) != tmp_int) | 796 if(static_cast<int>(thread_ids[j]) != tmp_int) |
787 continue; | 797 continue; |
788 | 798 |
789 std::string expected_name = StringPrintf("Thread %d", j).c_str(); | 799 std::string expected_name = StringPrintf("Thread %d", j).c_str(); |
790 EXPECT_TRUE(item->GetString("ph", &tmp) && tmp == "M"); | 800 EXPECT_TRUE(item->GetString("ph", &tmp) && tmp == "M"); |
791 EXPECT_TRUE(item->GetInteger("pid", &tmp_int) && | 801 EXPECT_TRUE(item->GetInteger("pid", &tmp_int) && |
792 tmp_int == static_cast<int>(base::GetCurrentProcId())); | 802 tmp_int == static_cast<int>(base::GetCurrentProcId())); |
793 EXPECT_TRUE(item->GetString("args.name", &tmp) && | 803 EXPECT_TRUE(item->GetString("args.name", &tmp) && |
794 tmp == expected_name); | 804 tmp == expected_name); |
795 } | 805 } |
796 } | 806 } |
797 } | 807 } |
798 | 808 |
| 809 TEST_F(TraceEventTestFixture, ThreadNameChanges) { |
| 810 ManualTestSetUp(); |
| 811 |
| 812 TraceLog::GetInstance()->SetEnabled(true); |
| 813 |
| 814 PlatformThread::SetName(NULL); |
| 815 TRACE_EVENT_INSTANT0("drink", "water"); |
| 816 |
| 817 PlatformThread::SetName("cafe"); |
| 818 TRACE_EVENT_INSTANT0("drink", "coffee"); |
| 819 |
| 820 PlatformThread::SetName("shop"); |
| 821 // No event here, so won't appear in combined name. |
| 822 |
| 823 PlatformThread::SetName("pub"); |
| 824 TRACE_EVENT_INSTANT0("drink", "beer"); |
| 825 TRACE_EVENT_INSTANT0("drink", "wine"); |
| 826 |
| 827 PlatformThread::SetName(" bar"); |
| 828 TRACE_EVENT_INSTANT0("drink", "whisky"); |
| 829 |
| 830 TraceLog::GetInstance()->SetEnabled(false); |
| 831 |
| 832 std::vector<DictionaryValue*> items = |
| 833 FindTraceEntries(trace_parsed_, "thread_name"); |
| 834 EXPECT_EQ(1u, items.size()); |
| 835 ASSERT_GT(items.size(), 0u); |
| 836 DictionaryValue* item = items[0]; |
| 837 ASSERT_TRUE(item); |
| 838 int tid; |
| 839 EXPECT_TRUE(item->GetInteger("tid", &tid)); |
| 840 EXPECT_EQ(PlatformThread::CurrentId(), static_cast<PlatformThreadId>(tid)); |
| 841 |
| 842 std::string expected_name = "cafe,pub, bar"; |
| 843 std::string tmp; |
| 844 EXPECT_TRUE(item->GetString("args.name", &tmp)); |
| 845 EXPECT_EQ(expected_name, tmp); |
| 846 } |
799 | 847 |
800 // Test trace calls made after tracing singleton shut down. | 848 // Test trace calls made after tracing singleton shut down. |
801 // | 849 // |
802 // The singleton is destroyed by our base::AtExitManager, but there can be | 850 // The singleton is destroyed by our base::AtExitManager, but there can be |
803 // code still executing as the C++ static objects are destroyed. This test | 851 // code still executing as the C++ static objects are destroyed. This test |
804 // forces the singleton to destroy early, and intentinally makes trace calls | 852 // forces the singleton to destroy early, and intentinally makes trace calls |
805 // afterwards. | 853 // afterwards. |
806 TEST_F(TraceEventTestFixture, AtExit) { | 854 TEST_F(TraceEventTestFixture, AtExit) { |
807 // Repeat this test a few times. Besides just showing robustness, it also | 855 // Repeat this test a few times. Besides just showing robustness, it also |
808 // allows us to test that events at shutdown do not appear with valid events | 856 // allows us to test that events at shutdown do not appear with valid events |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
946 Clear(); | 994 Clear(); |
947 | 995 |
948 trace_buffer_.Start(); | 996 trace_buffer_.Start(); |
949 trace_buffer_.AddFragment("bla1,bla2,bla3,bla4"); | 997 trace_buffer_.AddFragment("bla1,bla2,bla3,bla4"); |
950 trace_buffer_.Finish(); | 998 trace_buffer_.Finish(); |
951 EXPECT_STREQ(json_output_.json_output.c_str(), "[bla1,bla2,bla3,bla4]"); | 999 EXPECT_STREQ(json_output_.json_output.c_str(), "[bla1,bla2,bla3,bla4]"); |
952 } | 1000 } |
953 | 1001 |
954 } // namespace debug | 1002 } // namespace debug |
955 } // namespace base | 1003 } // namespace base |
OLD | NEW |