| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include <iterator> | 7 #include <iterator> |
| 8 | 8 |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/pending_task.h" | 10 #include "base/pending_task.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 " \"filter_predicate\": \"heap_profiler_predicate\"," | 36 " \"filter_predicate\": \"heap_profiler_predicate\"," |
| 37 " \"included_categories\": [\"*\"]" | 37 " \"included_categories\": [\"*\"]" |
| 38 " }" | 38 " }" |
| 39 " ]" | 39 " ]" |
| 40 "}"; | 40 "}"; |
| 41 | 41 |
| 42 // Asserts that the fixed-size array |expected_backtrace| matches the backtrace | 42 // Asserts that the fixed-size array |expected_backtrace| matches the backtrace |
| 43 // in |AllocationContextTracker::GetContextSnapshot|. | 43 // in |AllocationContextTracker::GetContextSnapshot|. |
| 44 template <size_t N> | 44 template <size_t N> |
| 45 void AssertBacktraceEquals(const StackFrame(&expected_backtrace)[N]) { | 45 void AssertBacktraceEquals(const StackFrame(&expected_backtrace)[N]) { |
| 46 AllocationContext ctx = | 46 AllocationContext ctx; |
| 47 AllocationContextTracker::GetInstanceForCurrentThread() | 47 ASSERT_TRUE(AllocationContextTracker::GetInstanceForCurrentThread() |
| 48 ->GetContextSnapshot(); | 48 ->GetContextSnapshot(&ctx)); |
| 49 | 49 |
| 50 auto* actual = std::begin(ctx.backtrace.frames); | 50 auto* actual = std::begin(ctx.backtrace.frames); |
| 51 auto* actual_bottom = actual + ctx.backtrace.frame_count; | 51 auto* actual_bottom = actual + ctx.backtrace.frame_count; |
| 52 auto expected = std::begin(expected_backtrace); | 52 auto expected = std::begin(expected_backtrace); |
| 53 auto expected_bottom = std::end(expected_backtrace); | 53 auto expected_bottom = std::end(expected_backtrace); |
| 54 | 54 |
| 55 // Note that this requires the pointers to be equal, this is not doing a deep | 55 // Note that this requires the pointers to be equal, this is not doing a deep |
| 56 // string comparison. | 56 // string comparison. |
| 57 for (; actual != actual_bottom && expected != expected_bottom; | 57 for (; actual != actual_bottom && expected != expected_bottom; |
| 58 actual++, expected++) | 58 actual++, expected++) |
| 59 ASSERT_EQ(*expected, *actual); | 59 ASSERT_EQ(*expected, *actual); |
| 60 | 60 |
| 61 // Ensure that the height of the stacks is the same. | 61 // Ensure that the height of the stacks is the same. |
| 62 ASSERT_EQ(actual, actual_bottom); | 62 ASSERT_EQ(actual, actual_bottom); |
| 63 ASSERT_EQ(expected, expected_bottom); | 63 ASSERT_EQ(expected, expected_bottom); |
| 64 } | 64 } |
| 65 | 65 |
| 66 void AssertBacktraceContainsOnlyThreadName() { | 66 void AssertBacktraceContainsOnlyThreadName() { |
| 67 StackFrame t = StackFrame::FromThreadName(kThreadName); | 67 StackFrame t = StackFrame::FromThreadName(kThreadName); |
| 68 AllocationContext ctx = | 68 AllocationContext ctx; |
| 69 AllocationContextTracker::GetInstanceForCurrentThread() | 69 ASSERT_TRUE(AllocationContextTracker::GetInstanceForCurrentThread() |
| 70 ->GetContextSnapshot(); | 70 ->GetContextSnapshot(&ctx)); |
| 71 | 71 |
| 72 ASSERT_EQ(1u, ctx.backtrace.frame_count); | 72 ASSERT_EQ(1u, ctx.backtrace.frame_count); |
| 73 ASSERT_EQ(t, ctx.backtrace.frames[0]); | 73 ASSERT_EQ(t, ctx.backtrace.frames[0]); |
| 74 } | 74 } |
| 75 | 75 |
| 76 class AllocationContextTrackerTest : public testing::Test { | 76 class AllocationContextTrackerTest : public testing::Test { |
| 77 public: | 77 public: |
| 78 void SetUp() override { | 78 void SetUp() override { |
| 79 AllocationContextTracker::SetCaptureMode( | 79 AllocationContextTracker::SetCaptureMode( |
| 80 AllocationContextTracker::CaptureMode::PSEUDO_STACK); | 80 AllocationContextTracker::CaptureMode::PSEUDO_STACK); |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 TRACE_EVENT0("Testing", kCupcake); | 236 TRACE_EVENT0("Testing", kCupcake); |
| 237 TRACE_EVENT0("Testing", kCupcake); | 237 TRACE_EVENT0("Testing", kCupcake); |
| 238 | 238 |
| 239 TRACE_EVENT0("Testing", kCupcake); | 239 TRACE_EVENT0("Testing", kCupcake); |
| 240 TRACE_EVENT0("Testing", kDonut); | 240 TRACE_EVENT0("Testing", kDonut); |
| 241 TRACE_EVENT0("Testing", kEclair); | 241 TRACE_EVENT0("Testing", kEclair); |
| 242 TRACE_EVENT0("Testing", kFroyo); | 242 TRACE_EVENT0("Testing", kFroyo); |
| 243 | 243 |
| 244 { | 244 { |
| 245 TRACE_EVENT0("Testing", kGingerbread); | 245 TRACE_EVENT0("Testing", kGingerbread); |
| 246 AllocationContext ctx = | 246 AllocationContext ctx; |
| 247 AllocationContextTracker::GetInstanceForCurrentThread() | 247 ASSERT_TRUE(AllocationContextTracker::GetInstanceForCurrentThread() |
| 248 ->GetContextSnapshot(); | 248 ->GetContextSnapshot(&ctx)); |
| 249 | 249 |
| 250 // The pseudo stack relies on pointer equality, not deep string comparisons. | 250 // The pseudo stack relies on pointer equality, not deep string comparisons. |
| 251 ASSERT_EQ(t, ctx.backtrace.frames[0]); | 251 ASSERT_EQ(t, ctx.backtrace.frames[0]); |
| 252 ASSERT_EQ(c, ctx.backtrace.frames[1]); | 252 ASSERT_EQ(c, ctx.backtrace.frames[1]); |
| 253 ASSERT_EQ(f, ctx.backtrace.frames[11]); | 253 ASSERT_EQ(f, ctx.backtrace.frames[11]); |
| 254 } | 254 } |
| 255 | 255 |
| 256 { | 256 { |
| 257 AllocationContext ctx = | 257 AllocationContext ctx; |
| 258 AllocationContextTracker::GetInstanceForCurrentThread() | 258 ASSERT_TRUE(AllocationContextTracker::GetInstanceForCurrentThread() |
| 259 ->GetContextSnapshot(); | 259 ->GetContextSnapshot(&ctx)); |
| 260 ASSERT_EQ(t, ctx.backtrace.frames[0]); | 260 ASSERT_EQ(t, ctx.backtrace.frames[0]); |
| 261 ASSERT_EQ(c, ctx.backtrace.frames[1]); | 261 ASSERT_EQ(c, ctx.backtrace.frames[1]); |
| 262 ASSERT_EQ(f, ctx.backtrace.frames[11]); | 262 ASSERT_EQ(f, ctx.backtrace.frames[11]); |
| 263 } | 263 } |
| 264 } | 264 } |
| 265 | 265 |
| 266 TEST_F(AllocationContextTrackerTest, TrackCategoryName) { | 266 TEST_F(AllocationContextTrackerTest, TrackCategoryName) { |
| 267 const char kContext1[] = "context1"; | 267 const char kContext1[] = "context1"; |
| 268 const char kContext2[] = "context2"; | 268 const char kContext2[] = "context2"; |
| 269 { | 269 { |
| 270 // The context from the scoped task event should be used as type name. | 270 // The context from the scoped task event should be used as type name. |
| 271 TRACE_HEAP_PROFILER_API_SCOPED_TASK_EXECUTION event1(kContext1); | 271 TRACE_HEAP_PROFILER_API_SCOPED_TASK_EXECUTION event1(kContext1); |
| 272 AllocationContext ctx1 = | 272 AllocationContext ctx1; |
| 273 AllocationContextTracker::GetInstanceForCurrentThread() | 273 ASSERT_TRUE(AllocationContextTracker::GetInstanceForCurrentThread() |
| 274 ->GetContextSnapshot(); | 274 ->GetContextSnapshot(&ctx1)); |
| 275 ASSERT_EQ(kContext1, ctx1.type_name); | 275 ASSERT_EQ(kContext1, ctx1.type_name); |
| 276 | 276 |
| 277 // In case of nested events, the last event's context should be used. | 277 // In case of nested events, the last event's context should be used. |
| 278 TRACE_HEAP_PROFILER_API_SCOPED_TASK_EXECUTION event2(kContext2); | 278 TRACE_HEAP_PROFILER_API_SCOPED_TASK_EXECUTION event2(kContext2); |
| 279 AllocationContext ctx2 = | 279 AllocationContext ctx2; |
| 280 AllocationContextTracker::GetInstanceForCurrentThread() | 280 ASSERT_TRUE(AllocationContextTracker::GetInstanceForCurrentThread() |
| 281 ->GetContextSnapshot(); | 281 ->GetContextSnapshot(&ctx2)); |
| 282 ASSERT_EQ(kContext2, ctx2.type_name); | 282 ASSERT_EQ(kContext2, ctx2.type_name); |
| 283 } | 283 } |
| 284 | 284 |
| 285 { | 285 { |
| 286 // Type should be category name of the last seen trace event. | 286 // Type should be category name of the last seen trace event. |
| 287 TRACE_EVENT0("Testing", kCupcake); | 287 TRACE_EVENT0("Testing", kCupcake); |
| 288 AllocationContext ctx1 = | 288 AllocationContext ctx1; |
| 289 AllocationContextTracker::GetInstanceForCurrentThread() | 289 ASSERT_TRUE(AllocationContextTracker::GetInstanceForCurrentThread() |
| 290 ->GetContextSnapshot(); | 290 ->GetContextSnapshot(&ctx1)); |
| 291 ASSERT_EQ("Testing", std::string(ctx1.type_name)); | 291 ASSERT_EQ("Testing", std::string(ctx1.type_name)); |
| 292 | 292 |
| 293 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("Testing"), kCupcake); | 293 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("Testing"), kCupcake); |
| 294 AllocationContext ctx2 = | 294 AllocationContext ctx2; |
| 295 AllocationContextTracker::GetInstanceForCurrentThread() | 295 ASSERT_TRUE(AllocationContextTracker::GetInstanceForCurrentThread() |
| 296 ->GetContextSnapshot(); | 296 ->GetContextSnapshot(&ctx2)); |
| 297 ASSERT_EQ(TRACE_DISABLED_BY_DEFAULT("Testing"), | 297 ASSERT_EQ(TRACE_DISABLED_BY_DEFAULT("Testing"), |
| 298 std::string(ctx2.type_name)); | 298 std::string(ctx2.type_name)); |
| 299 } | 299 } |
| 300 | 300 |
| 301 // Type should be nullptr without task event. | 301 // Type should be nullptr without task event. |
| 302 AllocationContext ctx = | 302 AllocationContext ctx; |
| 303 AllocationContextTracker::GetInstanceForCurrentThread() | 303 ASSERT_TRUE(AllocationContextTracker::GetInstanceForCurrentThread() |
| 304 ->GetContextSnapshot(); | 304 ->GetContextSnapshot(&ctx)); |
| 305 ASSERT_FALSE(ctx.type_name); | 305 ASSERT_FALSE(ctx.type_name); |
| 306 } | 306 } |
| 307 | 307 |
| 308 TEST_F(AllocationContextTrackerTest, IgnoreAllocationTest) { | 308 TEST_F(AllocationContextTrackerTest, IgnoreAllocationTest) { |
| 309 TRACE_EVENT0("Testing", kCupcake); | 309 TRACE_EVENT0("Testing", kCupcake); |
| 310 TRACE_EVENT0("Testing", kDonut); | 310 TRACE_EVENT0("Testing", kDonut); |
| 311 HEAP_PROFILER_SCOPED_IGNORE; | 311 HEAP_PROFILER_SCOPED_IGNORE; |
| 312 AllocationContext ctx = | 312 AllocationContext ctx; |
| 313 AllocationContextTracker::GetInstanceForCurrentThread() | 313 ASSERT_FALSE(AllocationContextTracker::GetInstanceForCurrentThread() |
| 314 ->GetContextSnapshot(); | 314 ->GetContextSnapshot(&ctx)); |
| 315 const StringPiece kTracingOverhead("tracing_overhead"); | |
| 316 ASSERT_EQ(kTracingOverhead, | |
| 317 static_cast<const char*>(ctx.backtrace.frames[0].value)); | |
| 318 ASSERT_EQ(1u, ctx.backtrace.frame_count); | |
| 319 } | 315 } |
| 320 | 316 |
| 321 } // namespace trace_event | 317 } // namespace trace_event |
| 322 } // namespace base | 318 } // namespace base |
| OLD | NEW |