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 <math.h> | 5 #include <math.h> |
6 #include <stddef.h> | 6 #include <stddef.h> |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <cstdlib> | 9 #include <cstdlib> |
10 | 10 |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 } | 127 } |
128 | 128 |
129 void EndTraceAndFlushAsync(WaitableEvent* flush_complete_event) { | 129 void EndTraceAndFlushAsync(WaitableEvent* flush_complete_event) { |
130 TraceLog::GetInstance()->SetDisabled(); | 130 TraceLog::GetInstance()->SetDisabled(); |
131 TraceLog::GetInstance()->Flush( | 131 TraceLog::GetInstance()->Flush( |
132 base::Bind(&TraceEventTestFixture::OnTraceDataCollected, | 132 base::Bind(&TraceEventTestFixture::OnTraceDataCollected, |
133 base::Unretained(static_cast<TraceEventTestFixture*>(this)), | 133 base::Unretained(static_cast<TraceEventTestFixture*>(this)), |
134 base::Unretained(flush_complete_event))); | 134 base::Unretained(flush_complete_event))); |
135 } | 135 } |
136 | 136 |
137 void FlushMonitoring() { | |
138 WaitableEvent flush_complete_event(false, false); | |
139 FlushMonitoring(&flush_complete_event); | |
140 flush_complete_event.Wait(); | |
141 } | |
142 | |
143 void FlushMonitoring(WaitableEvent* flush_complete_event) { | |
144 TraceLog::GetInstance()->FlushButLeaveBufferIntact( | |
145 base::Bind(&TraceEventTestFixture::OnTraceDataCollected, | |
146 base::Unretained(static_cast<TraceEventTestFixture*>(this)), | |
147 base::Unretained(flush_complete_event))); | |
148 } | |
149 | |
150 void SetUp() override { | 137 void SetUp() override { |
151 const char* name = PlatformThread::GetName(); | 138 const char* name = PlatformThread::GetName(); |
152 old_thread_name_ = name ? strdup(name) : NULL; | 139 old_thread_name_ = name ? strdup(name) : NULL; |
153 | 140 |
154 TraceLog::DeleteForTesting(); | 141 TraceLog::DeleteForTesting(); |
155 TraceLog* tracelog = TraceLog::GetInstance(); | 142 TraceLog* tracelog = TraceLog::GetInstance(); |
156 ASSERT_TRUE(tracelog); | 143 ASSERT_TRUE(tracelog); |
157 ASSERT_FALSE(tracelog->IsEnabled()); | 144 ASSERT_FALSE(tracelog->IsEnabled()); |
158 trace_buffer_.SetOutputCallback(json_output_.GetCallback()); | 145 trace_buffer_.SetOutputCallback(json_output_.GetCallback()); |
159 event_watch_notification_ = 0; | 146 event_watch_notification_ = 0; |
(...skipping 1108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1268 EndTraceAndFlush(); | 1255 EndTraceAndFlush(); |
1269 ASSERT_EQ(1, num_calls); | 1256 ASSERT_EQ(1, num_calls); |
1270 EXPECT_TRUE(FindNamePhaseKeyValue("metadata_event_name", "M", | 1257 EXPECT_TRUE(FindNamePhaseKeyValue("metadata_event_name", "M", |
1271 "metadata_arg_name", "metadata_value")); | 1258 "metadata_arg_name", "metadata_value")); |
1272 | 1259 |
1273 // The metadata event should only be adde to the current trace. In this new | 1260 // The metadata event should only be adde to the current trace. In this new |
1274 // trace, the event should not appear. | 1261 // trace, the event should not appear. |
1275 BeginTrace(); | 1262 BeginTrace(); |
1276 EndTraceAndFlush(); | 1263 EndTraceAndFlush(); |
1277 ASSERT_EQ(1, num_calls); | 1264 ASSERT_EQ(1, num_calls); |
1278 | |
1279 // Flushing should cause |AppendAsTraceFormat| to be called, but if the buffer | |
1280 // is left intact, it the flush at the end of the trace should still call it; | |
1281 // the metadata event should not be removed. | |
1282 TraceLog::GetInstance()->SetEnabled( | |
1283 TraceConfig(kRecordAllCategoryFilter, | |
1284 "record-until-full,enable-sampling"), | |
1285 TraceLog::MONITORING_MODE); | |
1286 TRACE_EVENT_API_ADD_METADATA_EVENT("metadata_event_name", "metadata_arg_name", | |
1287 convertable); | |
1288 FlushMonitoring(); | |
1289 ASSERT_EQ(2, num_calls); | |
1290 | |
1291 // Flushing the trace at this point will case |AppendAsTraceFormat| to be | |
1292 // called twice: once for the event that was added by the monitoring flush, | |
1293 // and once for the end trace flush; the metadata event will be duplicated. | |
1294 // This is consistent with the other metadata events. | |
1295 EndTraceAndFlush(); | |
1296 ASSERT_EQ(4, num_calls); | |
1297 } | 1265 } |
1298 | 1266 |
1299 // Test that categories work. | 1267 // Test that categories work. |
1300 TEST_F(TraceEventTestFixture, Categories) { | 1268 TEST_F(TraceEventTestFixture, Categories) { |
1301 // Test that categories that are used can be retrieved whether trace was | 1269 // Test that categories that are used can be retrieved whether trace was |
1302 // enabled or disabled when the trace event was encountered. | 1270 // enabled or disabled when the trace event was encountered. |
1303 TRACE_EVENT_INSTANT0("c1", "name", TRACE_EVENT_SCOPE_THREAD); | 1271 TRACE_EVENT_INSTANT0("c1", "name", TRACE_EVENT_SCOPE_THREAD); |
1304 TRACE_EVENT_INSTANT0("c2", "name", TRACE_EVENT_SCOPE_THREAD); | 1272 TRACE_EVENT_INSTANT0("c2", "name", TRACE_EVENT_SCOPE_THREAD); |
1305 BeginTrace(); | 1273 BeginTrace(); |
1306 TRACE_EVENT_INSTANT0("c3", "name", TRACE_EVENT_SCOPE_THREAD); | 1274 TRACE_EVENT_INSTANT0("c3", "name", TRACE_EVENT_SCOPE_THREAD); |
(...skipping 745 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2052 TRACE_EVENT_SET_SAMPLING_STATE("DDD", "name"); | 2020 TRACE_EVENT_SET_SAMPLING_STATE("DDD", "name"); |
2053 TraceLog::GetInstance()->WaitSamplingEventForTesting(); | 2021 TraceLog::GetInstance()->WaitSamplingEventForTesting(); |
2054 EXPECT_STREQ(TRACE_EVENT_GET_SAMPLING_STATE(), "DDD"); | 2022 EXPECT_STREQ(TRACE_EVENT_GET_SAMPLING_STATE(), "DDD"); |
2055 } | 2023 } |
2056 TraceLog::GetInstance()->WaitSamplingEventForTesting(); | 2024 TraceLog::GetInstance()->WaitSamplingEventForTesting(); |
2057 EXPECT_STREQ(TRACE_EVENT_GET_SAMPLING_STATE(), "DDD"); | 2025 EXPECT_STREQ(TRACE_EVENT_GET_SAMPLING_STATE(), "DDD"); |
2058 | 2026 |
2059 EndTraceAndFlush(); | 2027 EndTraceAndFlush(); |
2060 } | 2028 } |
2061 | 2029 |
2062 TEST_F(TraceEventTestFixture, TraceContinuousSampling) { | |
2063 TraceLog::GetInstance()->SetEnabled( | |
2064 TraceConfig(kRecordAllCategoryFilter, "record-until-full,enable-sampling"), | |
2065 TraceLog::MONITORING_MODE); | |
2066 | |
2067 TRACE_EVENT_SET_SAMPLING_STATE_FOR_BUCKET(1, "category", "AAA"); | |
2068 TraceLog::GetInstance()->WaitSamplingEventForTesting(); | |
2069 TRACE_EVENT_SET_SAMPLING_STATE_FOR_BUCKET(1, "category", "BBB"); | |
2070 TraceLog::GetInstance()->WaitSamplingEventForTesting(); | |
2071 | |
2072 FlushMonitoring(); | |
2073 | |
2074 // Make sure we can get the profiled data. | |
2075 EXPECT_TRUE(FindNamePhase("AAA", "P")); | |
2076 EXPECT_TRUE(FindNamePhase("BBB", "P")); | |
2077 | |
2078 Clear(); | |
2079 TraceLog::GetInstance()->WaitSamplingEventForTesting(); | |
2080 | |
2081 TRACE_EVENT_SET_SAMPLING_STATE_FOR_BUCKET(1, "category", "CCC"); | |
2082 TraceLog::GetInstance()->WaitSamplingEventForTesting(); | |
2083 TRACE_EVENT_SET_SAMPLING_STATE_FOR_BUCKET(1, "category", "DDD"); | |
2084 TraceLog::GetInstance()->WaitSamplingEventForTesting(); | |
2085 | |
2086 FlushMonitoring(); | |
2087 | |
2088 // Make sure the profiled data is accumulated. | |
2089 EXPECT_TRUE(FindNamePhase("AAA", "P")); | |
2090 EXPECT_TRUE(FindNamePhase("BBB", "P")); | |
2091 EXPECT_TRUE(FindNamePhase("CCC", "P")); | |
2092 EXPECT_TRUE(FindNamePhase("DDD", "P")); | |
2093 | |
2094 Clear(); | |
2095 | |
2096 TraceLog::GetInstance()->SetDisabled(); | |
2097 | |
2098 // Make sure disabling the continuous sampling thread clears | |
2099 // the profiled data. | |
2100 EXPECT_FALSE(FindNamePhase("AAA", "P")); | |
2101 EXPECT_FALSE(FindNamePhase("BBB", "P")); | |
2102 EXPECT_FALSE(FindNamePhase("CCC", "P")); | |
2103 EXPECT_FALSE(FindNamePhase("DDD", "P")); | |
2104 | |
2105 Clear(); | |
2106 } | |
2107 | |
2108 class MyData : public ConvertableToTraceFormat { | 2030 class MyData : public ConvertableToTraceFormat { |
2109 public: | 2031 public: |
2110 MyData() {} | 2032 MyData() {} |
2111 | 2033 |
2112 void AppendAsTraceFormat(std::string* out) const override { | 2034 void AppendAsTraceFormat(std::string* out) const override { |
2113 out->append("{\"foo\":1}"); | 2035 out->append("{\"foo\":1}"); |
2114 } | 2036 } |
2115 | 2037 |
2116 private: | 2038 private: |
2117 ~MyData() override {} | 2039 ~MyData() override {} |
(...skipping 1021 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3139 } | 3061 } |
3140 | 3062 |
3141 TEST_F(TraceEventTestFixture, SyntheticDelayConfigurationToString) { | 3063 TEST_F(TraceEventTestFixture, SyntheticDelayConfigurationToString) { |
3142 const char filter[] = "DELAY(test.Delay;16;oneshot)"; | 3064 const char filter[] = "DELAY(test.Delay;16;oneshot)"; |
3143 TraceConfig config(filter, ""); | 3065 TraceConfig config(filter, ""); |
3144 EXPECT_EQ(filter, config.ToCategoryFilterString()); | 3066 EXPECT_EQ(filter, config.ToCategoryFilterString()); |
3145 } | 3067 } |
3146 | 3068 |
3147 } // namespace trace_event | 3069 } // namespace trace_event |
3148 } // namespace base | 3070 } // namespace base |
OLD | NEW |