OLD | NEW |
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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 const char kFlowIdStr[] = "0x7"; | 62 const char kFlowIdStr[] = "0x7"; |
63 | 63 |
64 const char kRecordAllCategoryFilter[] = "*"; | 64 const char kRecordAllCategoryFilter[] = "*"; |
65 | 65 |
66 class TraceEventTestFixture : public testing::Test { | 66 class TraceEventTestFixture : public testing::Test { |
67 public: | 67 public: |
68 void OnTraceDataCollected( | 68 void OnTraceDataCollected( |
69 WaitableEvent* flush_complete_event, | 69 WaitableEvent* flush_complete_event, |
70 const scoped_refptr<base::RefCountedString>& events_str, | 70 const scoped_refptr<base::RefCountedString>& events_str, |
71 bool has_more_events); | 71 bool has_more_events); |
72 void OnWatchEventMatched() { | |
73 ++event_watch_notification_; | |
74 } | |
75 DictionaryValue* FindMatchingTraceEntry(const JsonKeyValue* key_values); | 72 DictionaryValue* FindMatchingTraceEntry(const JsonKeyValue* key_values); |
76 DictionaryValue* FindNamePhase(const char* name, const char* phase); | 73 DictionaryValue* FindNamePhase(const char* name, const char* phase); |
77 DictionaryValue* FindNamePhaseKeyValue(const char* name, | 74 DictionaryValue* FindNamePhaseKeyValue(const char* name, |
78 const char* phase, | 75 const char* phase, |
79 const char* key, | 76 const char* key, |
80 const char* value); | 77 const char* value); |
81 void DropTracedMetadataRecords(); | 78 void DropTracedMetadataRecords(); |
82 bool FindMatchingValue(const char* key, | 79 bool FindMatchingValue(const char* key, |
83 const char* value); | 80 const char* value); |
84 bool FindNonMatchingValue(const char* key, | 81 bool FindNonMatchingValue(const char* key, |
85 const char* value); | 82 const char* value); |
86 void Clear() { | 83 void Clear() { |
87 trace_parsed_.Clear(); | 84 trace_parsed_.Clear(); |
88 json_output_.json_output.clear(); | 85 json_output_.json_output.clear(); |
89 } | 86 } |
90 | 87 |
91 void BeginTrace() { | 88 void BeginTrace() { |
92 BeginSpecificTrace("*"); | 89 BeginSpecificTrace("*"); |
93 } | 90 } |
94 | 91 |
95 void BeginSpecificTrace(const std::string& filter) { | 92 void BeginSpecificTrace(const std::string& filter) { |
96 event_watch_notification_ = 0; | |
97 TraceLog::GetInstance()->SetEnabled(TraceConfig(filter, ""), | 93 TraceLog::GetInstance()->SetEnabled(TraceConfig(filter, ""), |
98 TraceLog::RECORDING_MODE); | 94 TraceLog::RECORDING_MODE); |
99 } | 95 } |
100 | 96 |
101 void CancelTrace() { | 97 void CancelTrace() { |
102 WaitableEvent flush_complete_event( | 98 WaitableEvent flush_complete_event( |
103 WaitableEvent::ResetPolicy::AUTOMATIC, | 99 WaitableEvent::ResetPolicy::AUTOMATIC, |
104 WaitableEvent::InitialState::NOT_SIGNALED); | 100 WaitableEvent::InitialState::NOT_SIGNALED); |
105 CancelTraceAsync(&flush_complete_event); | 101 CancelTraceAsync(&flush_complete_event); |
106 flush_complete_event.Wait(); | 102 flush_complete_event.Wait(); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 | 143 |
148 void SetUp() override { | 144 void SetUp() override { |
149 const char* name = PlatformThread::GetName(); | 145 const char* name = PlatformThread::GetName(); |
150 old_thread_name_ = name ? strdup(name) : NULL; | 146 old_thread_name_ = name ? strdup(name) : NULL; |
151 | 147 |
152 TraceLog::DeleteForTesting(); | 148 TraceLog::DeleteForTesting(); |
153 TraceLog* tracelog = TraceLog::GetInstance(); | 149 TraceLog* tracelog = TraceLog::GetInstance(); |
154 ASSERT_TRUE(tracelog); | 150 ASSERT_TRUE(tracelog); |
155 ASSERT_FALSE(tracelog->IsEnabled()); | 151 ASSERT_FALSE(tracelog->IsEnabled()); |
156 trace_buffer_.SetOutputCallback(json_output_.GetCallback()); | 152 trace_buffer_.SetOutputCallback(json_output_.GetCallback()); |
157 event_watch_notification_ = 0; | |
158 num_flush_callbacks_ = 0; | 153 num_flush_callbacks_ = 0; |
159 } | 154 } |
160 void TearDown() override { | 155 void TearDown() override { |
161 if (TraceLog::GetInstance()) | 156 if (TraceLog::GetInstance()) |
162 EXPECT_FALSE(TraceLog::GetInstance()->IsEnabled()); | 157 EXPECT_FALSE(TraceLog::GetInstance()->IsEnabled()); |
163 PlatformThread::SetName(old_thread_name_ ? old_thread_name_ : ""); | 158 PlatformThread::SetName(old_thread_name_ ? old_thread_name_ : ""); |
164 free(old_thread_name_); | 159 free(old_thread_name_); |
165 old_thread_name_ = NULL; | 160 old_thread_name_ = NULL; |
166 // We want our singleton torn down after each test. | 161 // We want our singleton torn down after each test. |
167 TraceLog::DeleteForTesting(); | 162 TraceLog::DeleteForTesting(); |
168 } | 163 } |
169 | 164 |
170 char* old_thread_name_; | 165 char* old_thread_name_; |
171 ListValue trace_parsed_; | 166 ListValue trace_parsed_; |
172 TraceResultBuffer trace_buffer_; | 167 TraceResultBuffer trace_buffer_; |
173 TraceResultBuffer::SimpleOutput json_output_; | 168 TraceResultBuffer::SimpleOutput json_output_; |
174 int event_watch_notification_; | |
175 size_t num_flush_callbacks_; | 169 size_t num_flush_callbacks_; |
176 | 170 |
177 private: | 171 private: |
178 // We want our singleton torn down after each test. | 172 // We want our singleton torn down after each test. |
179 ShadowingAtExitManager at_exit_manager_; | 173 ShadowingAtExitManager at_exit_manager_; |
180 Lock lock_; | 174 Lock lock_; |
181 }; | 175 }; |
182 | 176 |
183 void TraceEventTestFixture::OnTraceDataCollected( | 177 void TraceEventTestFixture::OnTraceDataCollected( |
184 WaitableEvent* flush_complete_event, | 178 WaitableEvent* flush_complete_event, |
(...skipping 1377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1562 TRACE_EVENT_INSTANT0("cat1", "included", TRACE_EVENT_SCOPE_THREAD); | 1556 TRACE_EVENT_INSTANT0("cat1", "included", TRACE_EVENT_SCOPE_THREAD); |
1563 TRACE_EVENT_INSTANT0("cat2", "included", TRACE_EVENT_SCOPE_THREAD); | 1557 TRACE_EVENT_INSTANT0("cat2", "included", TRACE_EVENT_SCOPE_THREAD); |
1564 EndTraceAndFlush(); | 1558 EndTraceAndFlush(); |
1565 EXPECT_TRUE(FindMatchingValue("cat", "inc_wildchar_bla_end")); | 1559 EXPECT_TRUE(FindMatchingValue("cat", "inc_wildchar_bla_end")); |
1566 EXPECT_TRUE(FindMatchingValue("cat", "cat1")); | 1560 EXPECT_TRUE(FindMatchingValue("cat", "cat1")); |
1567 EXPECT_TRUE(FindMatchingValue("cat", "cat2")); | 1561 EXPECT_TRUE(FindMatchingValue("cat", "cat2")); |
1568 EXPECT_FALSE(FindMatchingValue("name", "not_inc")); | 1562 EXPECT_FALSE(FindMatchingValue("name", "not_inc")); |
1569 } | 1563 } |
1570 | 1564 |
1571 | 1565 |
1572 // Test EVENT_WATCH_NOTIFICATION | |
1573 TEST_F(TraceEventTestFixture, EventWatchNotification) { | |
1574 // Basic one occurrence. | |
1575 BeginTrace(); | |
1576 TraceLog::WatchEventCallback callback = | |
1577 base::Bind(&TraceEventTestFixture::OnWatchEventMatched, | |
1578 base::Unretained(this)); | |
1579 TraceLog::GetInstance()->SetWatchEvent("cat", "event", callback); | |
1580 TRACE_EVENT_INSTANT0("cat", "event", TRACE_EVENT_SCOPE_THREAD); | |
1581 EndTraceAndFlush(); | |
1582 EXPECT_EQ(event_watch_notification_, 1); | |
1583 | |
1584 // Auto-reset after end trace. | |
1585 BeginTrace(); | |
1586 TraceLog::GetInstance()->SetWatchEvent("cat", "event", callback); | |
1587 EndTraceAndFlush(); | |
1588 BeginTrace(); | |
1589 TRACE_EVENT_INSTANT0("cat", "event", TRACE_EVENT_SCOPE_THREAD); | |
1590 EndTraceAndFlush(); | |
1591 EXPECT_EQ(event_watch_notification_, 0); | |
1592 | |
1593 // Multiple occurrence. | |
1594 BeginTrace(); | |
1595 int num_occurrences = 5; | |
1596 TraceLog::GetInstance()->SetWatchEvent("cat", "event", callback); | |
1597 for (int i = 0; i < num_occurrences; ++i) | |
1598 TRACE_EVENT_INSTANT0("cat", "event", TRACE_EVENT_SCOPE_THREAD); | |
1599 EndTraceAndFlush(); | |
1600 EXPECT_EQ(event_watch_notification_, num_occurrences); | |
1601 | |
1602 // Wrong category. | |
1603 BeginTrace(); | |
1604 TraceLog::GetInstance()->SetWatchEvent("cat", "event", callback); | |
1605 TRACE_EVENT_INSTANT0("wrong_cat", "event", TRACE_EVENT_SCOPE_THREAD); | |
1606 EndTraceAndFlush(); | |
1607 EXPECT_EQ(event_watch_notification_, 0); | |
1608 | |
1609 // Wrong name. | |
1610 BeginTrace(); | |
1611 TraceLog::GetInstance()->SetWatchEvent("cat", "event", callback); | |
1612 TRACE_EVENT_INSTANT0("cat", "wrong_event", TRACE_EVENT_SCOPE_THREAD); | |
1613 EndTraceAndFlush(); | |
1614 EXPECT_EQ(event_watch_notification_, 0); | |
1615 | |
1616 // Canceled. | |
1617 BeginTrace(); | |
1618 TraceLog::GetInstance()->SetWatchEvent("cat", "event", callback); | |
1619 TraceLog::GetInstance()->CancelWatchEvent(); | |
1620 TRACE_EVENT_INSTANT0("cat", "event", TRACE_EVENT_SCOPE_THREAD); | |
1621 EndTraceAndFlush(); | |
1622 EXPECT_EQ(event_watch_notification_, 0); | |
1623 } | |
1624 | |
1625 // Test ASYNC_BEGIN/END events | 1566 // Test ASYNC_BEGIN/END events |
1626 TEST_F(TraceEventTestFixture, AsyncBeginEndEvents) { | 1567 TEST_F(TraceEventTestFixture, AsyncBeginEndEvents) { |
1627 BeginTrace(); | 1568 BeginTrace(); |
1628 | 1569 |
1629 unsigned long long id = 0xfeedbeeffeedbeefull; | 1570 unsigned long long id = 0xfeedbeeffeedbeefull; |
1630 TRACE_EVENT_ASYNC_BEGIN0("cat", "name1", id); | 1571 TRACE_EVENT_ASYNC_BEGIN0("cat", "name1", id); |
1631 TRACE_EVENT_ASYNC_STEP_INTO0("cat", "name1", id, "step1"); | 1572 TRACE_EVENT_ASYNC_STEP_INTO0("cat", "name1", id, "step1"); |
1632 TRACE_EVENT_ASYNC_END0("cat", "name1", id); | 1573 TRACE_EVENT_ASYNC_END0("cat", "name1", id); |
1633 TRACE_EVENT_BEGIN0("cat", "name2"); | 1574 TRACE_EVENT_BEGIN0("cat", "name2"); |
1634 TRACE_EVENT_ASYNC_BEGIN0("cat", "name3", 0); | 1575 TRACE_EVENT_ASYNC_BEGIN0("cat", "name3", 0); |
(...skipping 1627 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3262 | 3203 |
3263 TEST_F(TraceEventTestFixture, ClockSyncEventsAreAlwaysAddedToTrace) { | 3204 TEST_F(TraceEventTestFixture, ClockSyncEventsAreAlwaysAddedToTrace) { |
3264 BeginSpecificTrace("-*"); | 3205 BeginSpecificTrace("-*"); |
3265 TRACE_EVENT_CLOCK_SYNC_RECEIVER(1); | 3206 TRACE_EVENT_CLOCK_SYNC_RECEIVER(1); |
3266 EndTraceAndFlush(); | 3207 EndTraceAndFlush(); |
3267 EXPECT_TRUE(FindNamePhase("clock_sync", "c")); | 3208 EXPECT_TRUE(FindNamePhase("clock_sync", "c")); |
3268 } | 3209 } |
3269 | 3210 |
3270 } // namespace trace_event | 3211 } // namespace trace_event |
3271 } // namespace base | 3212 } // namespace base |
OLD | NEW |