| 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" |
| 11 #include "base/trace_event/heap_profiler.h" | 11 #include "base/trace_event/heap_profiler.h" |
| 12 #include "base/trace_event/heap_profiler_allocation_context.h" | 12 #include "base/trace_event/heap_profiler_allocation_context.h" |
| 13 #include "base/trace_event/heap_profiler_allocation_context_tracker.h" | 13 #include "base/trace_event/heap_profiler_allocation_context_tracker.h" |
| 14 #include "base/trace_event/memory_dump_manager.h" | 14 #include "base/trace_event/memory_dump_manager.h" |
| 15 #include "base/trace_event/trace_event.h" | 15 #include "base/trace_event/trace_event.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 17 |
| 18 namespace base { | 18 namespace base { |
| 19 namespace trace_event { | 19 namespace trace_event { |
| 20 | 20 |
| 21 // Define all strings once, because the pseudo stack requires pointer equality, | 21 // Define all strings once, because the pseudo stack requires pointer equality, |
| 22 // and string interning is unreliable. | 22 // and string interning is unreliable. |
| 23 const char kThreadName[] = "TestThread"; | 23 const char kThreadName[] = "TestThread"; |
| 24 const char kCupcake[] = "Cupcake"; | 24 const char kCupcake[] = "Cupcake"; |
| 25 const char kDonut[] = "Donut"; | 25 const char kDonut[] = "Donut"; |
| 26 const char kEclair[] = "Eclair"; | 26 const char kEclair[] = "Eclair"; |
| 27 const char kFroyo[] = "Froyo"; | 27 const char kFroyo[] = "Froyo"; |
| 28 const char kGingerbread[] = "Gingerbread"; | 28 const char kGingerbread[] = "Gingerbread"; |
| 29 | 29 |
| 30 const char kFilteringTraceConfig[] = |
| 31 "{" |
| 32 " \"event_filters\": [" |
| 33 " {" |
| 34 " \"excluded_categories\": []," |
| 35 " \"filter_args\": {}," |
| 36 " \"filter_predicate\": \"heap_profiler_predicate\"," |
| 37 " \"included_categories\": [\"*\"]" |
| 38 " }" |
| 39 " ]" |
| 40 "}"; |
| 41 |
| 30 // Asserts that the fixed-size array |expected_backtrace| matches the backtrace | 42 // Asserts that the fixed-size array |expected_backtrace| matches the backtrace |
| 31 // in |AllocationContextTracker::GetContextSnapshot|. | 43 // in |AllocationContextTracker::GetContextSnapshot|. |
| 32 template <size_t N> | 44 template <size_t N> |
| 33 void AssertBacktraceEquals(const StackFrame(&expected_backtrace)[N]) { | 45 void AssertBacktraceEquals(const StackFrame(&expected_backtrace)[N]) { |
| 34 AllocationContext ctx = | 46 AllocationContext ctx = |
| 35 AllocationContextTracker::GetInstanceForCurrentThread() | 47 AllocationContextTracker::GetInstanceForCurrentThread() |
| 36 ->GetContextSnapshot(); | 48 ->GetContextSnapshot(); |
| 37 | 49 |
| 38 auto* actual = std::begin(ctx.backtrace.frames); | 50 auto* actual = std::begin(ctx.backtrace.frames); |
| 39 auto* actual_bottom = actual + ctx.backtrace.frame_count; | 51 auto* actual_bottom = actual + ctx.backtrace.frame_count; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 61 ASSERT_EQ(t, ctx.backtrace.frames[0]); | 73 ASSERT_EQ(t, ctx.backtrace.frames[0]); |
| 62 } | 74 } |
| 63 | 75 |
| 64 class AllocationContextTrackerTest : public testing::Test { | 76 class AllocationContextTrackerTest : public testing::Test { |
| 65 public: | 77 public: |
| 66 void SetUp() override { | 78 void SetUp() override { |
| 67 AllocationContextTracker::SetCaptureMode( | 79 AllocationContextTracker::SetCaptureMode( |
| 68 AllocationContextTracker::CaptureMode::PSEUDO_STACK); | 80 AllocationContextTracker::CaptureMode::PSEUDO_STACK); |
| 69 // Enabling memory-infra category sets default memory dump config which | 81 // Enabling memory-infra category sets default memory dump config which |
| 70 // includes filters for capturing pseudo stack. | 82 // includes filters for capturing pseudo stack. |
| 71 TraceConfig config(MemoryDumpManager::kTraceCategory, ""); | 83 TraceConfig config(kFilteringTraceConfig); |
| 72 TraceLog::GetInstance()->SetEnabled(config, TraceLog::RECORDING_MODE); | 84 TraceLog::GetInstance()->SetEnabled(config, TraceLog::FILTERING_MODE); |
| 73 AllocationContextTracker::SetCurrentThreadName(kThreadName); | 85 AllocationContextTracker::SetCurrentThreadName(kThreadName); |
| 74 } | 86 } |
| 75 | 87 |
| 76 void TearDown() override { | 88 void TearDown() override { |
| 77 AllocationContextTracker::SetCaptureMode( | 89 AllocationContextTracker::SetCaptureMode( |
| 78 AllocationContextTracker::CaptureMode::DISABLED); | 90 AllocationContextTracker::CaptureMode::DISABLED); |
| 79 TraceLog::GetInstance()->SetDisabled(); | 91 TraceLog::GetInstance()->SetDisabled(TraceLog::FILTERING_MODE); |
| 80 } | 92 } |
| 81 }; | 93 }; |
| 82 | 94 |
| 83 // Check that |TRACE_EVENT| macros push and pop to the pseudo stack correctly. | 95 // Check that |TRACE_EVENT| macros push and pop to the pseudo stack correctly. |
| 84 TEST_F(AllocationContextTrackerTest, PseudoStackScopedTrace) { | 96 TEST_F(AllocationContextTrackerTest, PseudoStackScopedTrace) { |
| 85 StackFrame t = StackFrame::FromThreadName(kThreadName); | 97 StackFrame t = StackFrame::FromThreadName(kThreadName); |
| 86 StackFrame c = StackFrame::FromTraceEventName(kCupcake); | 98 StackFrame c = StackFrame::FromTraceEventName(kCupcake); |
| 87 StackFrame d = StackFrame::FromTraceEventName(kDonut); | 99 StackFrame d = StackFrame::FromTraceEventName(kDonut); |
| 88 StackFrame e = StackFrame::FromTraceEventName(kEclair); | 100 StackFrame e = StackFrame::FromTraceEventName(kEclair); |
| 89 StackFrame f = StackFrame::FromTraceEventName(kFroyo); | 101 StackFrame f = StackFrame::FromTraceEventName(kFroyo); |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 AllocationContextTracker::GetInstanceForCurrentThread() | 313 AllocationContextTracker::GetInstanceForCurrentThread() |
| 302 ->GetContextSnapshot(); | 314 ->GetContextSnapshot(); |
| 303 const StringPiece kTracingOverhead("tracing_overhead"); | 315 const StringPiece kTracingOverhead("tracing_overhead"); |
| 304 ASSERT_EQ(kTracingOverhead, | 316 ASSERT_EQ(kTracingOverhead, |
| 305 static_cast<const char*>(ctx.backtrace.frames[0].value)); | 317 static_cast<const char*>(ctx.backtrace.frames[0].value)); |
| 306 ASSERT_EQ(1u, ctx.backtrace.frame_count); | 318 ASSERT_EQ(1u, ctx.backtrace.frame_count); |
| 307 } | 319 } |
| 308 | 320 |
| 309 } // namespace trace_event | 321 } // namespace trace_event |
| 310 } // namespace base | 322 } // namespace base |
| OLD | NEW |