| Index: base/trace_event/memory_profiler_allocation_context_unittest.cc
|
| diff --git a/base/trace_event/memory_profiler_allocation_context_unittest.cc b/base/trace_event/memory_profiler_allocation_context_unittest.cc
|
| index e94b15d1f2b152ad909343ed2f0ac0771ca10dd5..d668c767107680b08935c4d5d29be9f7988bf550 100644
|
| --- a/base/trace_event/memory_profiler_allocation_context_unittest.cc
|
| +++ b/base/trace_event/memory_profiler_allocation_context_unittest.cc
|
| @@ -9,17 +9,23 @@
|
| namespace base {
|
| namespace trace_event {
|
|
|
| -// Asserts that the fixed-size array |expected_stack| matches the pseudo
|
| -// stack. Syntax note: |const StackFrame (&expected_stack)[N]| is the syntax
|
| -// for "expected_stack is a reference to a const fixed-size array of StackFrame
|
| -// of length N".
|
| +// Returns a pointer past the end of the fixed-size array |array| of |T| of
|
| +// length |N|, identical to C++11 |std::end|.
|
| +template <typename T, int N>
|
| +const T* End(const T(&array)[N]) {
|
| + return array + N;
|
| +}
|
| +
|
| +// Asserts that the fixed-size array |expected_backtrace| matches the backtrace
|
| +// in |AllocationContextTracker::GetContext|.
|
| template <size_t N>
|
| -void AssertPseudoStackEquals(const StackFrame (&expected_stack)[N]) {
|
| - auto pseudo_stack = AllocationContextTracker::GetPseudoStackForTesting();
|
| - auto actual = pseudo_stack->top();
|
| - auto actual_bottom = pseudo_stack->bottom();
|
| - auto expected = expected_stack;
|
| - auto expected_bottom = expected_stack + N;
|
| +void AssertBacktraceEquals(const StackFrame(&expected_backtrace)[N]) {
|
| + AllocationContext ctx = AllocationContextTracker::GetContext();
|
| +
|
| + auto actual = ctx.backtrace;
|
| + auto actual_bottom = End(ctx.backtrace);
|
| + auto expected = expected_backtrace;
|
| + auto expected_bottom = End(expected_backtrace);
|
|
|
| for (; actual != actual_bottom && expected != expected_bottom;
|
| actual++, expected++) {
|
| @@ -31,106 +37,109 @@ void AssertPseudoStackEquals(const StackFrame (&expected_stack)[N]) {
|
| ASSERT_EQ(expected, expected_bottom);
|
| }
|
|
|
| -void AssertPseudoStackEmpty() {
|
| - auto pseudo_stack = AllocationContextTracker::GetPseudoStackForTesting();
|
| - ASSERT_EQ(pseudo_stack->top(), pseudo_stack->bottom());
|
| +void AssertBacktraceEmpty() {
|
| + AllocationContext ctx = AllocationContextTracker::GetContext();
|
| +
|
| + for (StackFrame frame : ctx.backtrace)
|
| + ASSERT_EQ(nullptr, frame);
|
| }
|
|
|
| class AllocationContextTest : public testing::Test {
|
| public:
|
| - void EnableTracing() {
|
| + void SetUp() override {
|
| TraceConfig config("");
|
| TraceLog::GetInstance()->SetEnabled(config, TraceLog::RECORDING_MODE);
|
| AllocationContextTracker::SetCaptureEnabled(true);
|
| }
|
|
|
| - void DisableTracing() {
|
| + void TearDown() override {
|
| AllocationContextTracker::SetCaptureEnabled(false);
|
| TraceLog::GetInstance()->SetDisabled();
|
| }
|
| };
|
|
|
| +// Check that |TRACE_EVENT| macros push and pop to the pseudo stack correctly.
|
| +// Also check that |GetContext| fills the backtrace with null pointers when the
|
| +// pseudo stack height is less than the capacity.
|
| TEST_F(AllocationContextTest, PseudoStackScopedTrace) {
|
| StackFrame c = "Cupcake";
|
| StackFrame d = "Donut";
|
| StackFrame e = "Eclair";
|
| StackFrame f = "Froyo";
|
|
|
| - EnableTracing();
|
| - AssertPseudoStackEmpty();
|
| + AssertBacktraceEmpty();
|
|
|
| {
|
| TRACE_EVENT0("Testing", "Cupcake");
|
| - StackFrame frame_c[] = {c};
|
| - AssertPseudoStackEquals(frame_c);
|
| + StackFrame frame_c[] = {c, 0, 0, 0, 0, 0, 0, 0};
|
| + AssertBacktraceEquals(frame_c);
|
|
|
| {
|
| TRACE_EVENT0("Testing", "Donut");
|
| - StackFrame frame_dc[] = {d, c};
|
| - AssertPseudoStackEquals(frame_dc);
|
| + StackFrame frame_dc[] = {d, c, 0, 0, 0, 0, 0, 0};
|
| + AssertBacktraceEquals(frame_dc);
|
| }
|
|
|
| - AssertPseudoStackEquals(frame_c);
|
| + AssertBacktraceEquals(frame_c);
|
|
|
| {
|
| TRACE_EVENT0("Testing", "Eclair");
|
| - StackFrame frame_ec[] = {e, c};
|
| - AssertPseudoStackEquals(frame_ec);
|
| + StackFrame frame_ec[] = {e, c, 0, 0, 0, 0, 0, 0};
|
| + AssertBacktraceEquals(frame_ec);
|
| }
|
|
|
| - AssertPseudoStackEquals(frame_c);
|
| + AssertBacktraceEquals(frame_c);
|
| }
|
|
|
| - AssertPseudoStackEmpty();
|
| + AssertBacktraceEmpty();
|
|
|
| {
|
| TRACE_EVENT0("Testing", "Froyo");
|
| - StackFrame frame_f[] = {f};
|
| - AssertPseudoStackEquals(frame_f);
|
| + StackFrame frame_f[] = {f, 0, 0, 0, 0, 0, 0, 0};
|
| + AssertBacktraceEquals(frame_f);
|
| }
|
|
|
| - AssertPseudoStackEmpty();
|
| - DisableTracing();
|
| + AssertBacktraceEmpty();
|
| }
|
|
|
| +// Same as |PseudoStackScopedTrace|, but now test the |TRACE_EVENT_BEGIN| and
|
| +// |TRACE_EVENT_END| macros.
|
| TEST_F(AllocationContextTest, PseudoStackBeginEndTrace) {
|
| StackFrame c = "Cupcake";
|
| StackFrame d = "Donut";
|
| StackFrame e = "Eclair";
|
| StackFrame f = "Froyo";
|
|
|
| - StackFrame frame_c[] = {c};
|
| - StackFrame frame_dc[] = {d, c};
|
| - StackFrame frame_ec[] = {e, c};
|
| - StackFrame frame_f[] = {f};
|
| + StackFrame frame_c[] = {c, 0, 0, 0, 0, 0, 0, 0};
|
| + StackFrame frame_dc[] = {d, c, 0, 0, 0, 0, 0, 0};
|
| + StackFrame frame_ec[] = {e, c, 0, 0, 0, 0, 0, 0};
|
| + StackFrame frame_f[] = {f, 0, 0, 0, 0, 0, 0, 0};
|
|
|
| - EnableTracing();
|
| - AssertPseudoStackEmpty();
|
| + AssertBacktraceEmpty();
|
|
|
| TRACE_EVENT_BEGIN0("Testing", "Cupcake");
|
| - AssertPseudoStackEquals(frame_c);
|
| + AssertBacktraceEquals(frame_c);
|
|
|
| TRACE_EVENT_BEGIN0("Testing", "Donut");
|
| - AssertPseudoStackEquals(frame_dc);
|
| + AssertBacktraceEquals(frame_dc);
|
| TRACE_EVENT_END0("Testing", "Donut");
|
|
|
| - AssertPseudoStackEquals(frame_c);
|
| + AssertBacktraceEquals(frame_c);
|
|
|
| TRACE_EVENT_BEGIN0("Testing", "Eclair");
|
| - AssertPseudoStackEquals(frame_ec);
|
| + AssertBacktraceEquals(frame_ec);
|
| TRACE_EVENT_END0("Testing", "Eclair");
|
|
|
| - AssertPseudoStackEquals(frame_c);
|
| + AssertBacktraceEquals(frame_c);
|
| TRACE_EVENT_END0("Testing", "Cupcake");
|
|
|
| - AssertPseudoStackEmpty();
|
| + AssertBacktraceEmpty();
|
|
|
| TRACE_EVENT_BEGIN0("Testing", "Froyo");
|
| - AssertPseudoStackEquals(frame_f);
|
| + AssertBacktraceEquals(frame_f);
|
| TRACE_EVENT_END0("Testing", "Froyo");
|
|
|
| - AssertPseudoStackEmpty();
|
| - DisableTracing();
|
| + AssertBacktraceEmpty();
|
| }
|
|
|
| TEST_F(AllocationContextTest, PseudoStackMixedTrace) {
|
| @@ -139,61 +148,60 @@ TEST_F(AllocationContextTest, PseudoStackMixedTrace) {
|
| StackFrame e = "Eclair";
|
| StackFrame f = "Froyo";
|
|
|
| - StackFrame frame_c[] = {c};
|
| - StackFrame frame_dc[] = {d, c};
|
| - StackFrame frame_e[] = {e};
|
| - StackFrame frame_fe[] = {f, e};
|
| + StackFrame frame_c[] = {c, 0, 0, 0, 0, 0, 0, 0};
|
| + StackFrame frame_dc[] = {d, c, 0, 0, 0, 0, 0, 0};
|
| + StackFrame frame_e[] = {e, 0, 0, 0, 0, 0, 0, 0};
|
| + StackFrame frame_fe[] = {f, e, 0, 0, 0, 0, 0, 0};
|
|
|
| - EnableTracing();
|
| - AssertPseudoStackEmpty();
|
| + AssertBacktraceEmpty();
|
|
|
| TRACE_EVENT_BEGIN0("Testing", "Cupcake");
|
| - AssertPseudoStackEquals(frame_c);
|
| + AssertBacktraceEquals(frame_c);
|
|
|
| {
|
| TRACE_EVENT0("Testing", "Donut");
|
| - AssertPseudoStackEquals(frame_dc);
|
| + AssertBacktraceEquals(frame_dc);
|
| }
|
|
|
| - AssertPseudoStackEquals(frame_c);
|
| + AssertBacktraceEquals(frame_c);
|
| TRACE_EVENT_END0("Testing", "Cupcake");
|
| - AssertPseudoStackEmpty();
|
| + AssertBacktraceEmpty();
|
|
|
| {
|
| TRACE_EVENT0("Testing", "Eclair");
|
| - AssertPseudoStackEquals(frame_e);
|
| + AssertBacktraceEquals(frame_e);
|
|
|
| TRACE_EVENT_BEGIN0("Testing", "Froyo");
|
| - AssertPseudoStackEquals(frame_fe);
|
| + AssertBacktraceEquals(frame_fe);
|
| TRACE_EVENT_END0("Testing", "Froyo");
|
| - AssertPseudoStackEquals(frame_e);
|
| + AssertBacktraceEquals(frame_e);
|
| }
|
|
|
| - AssertPseudoStackEmpty();
|
| - DisableTracing();
|
| + AssertBacktraceEmpty();
|
| }
|
|
|
| -TEST_F(AllocationContextTest, PseudoStackEnableWithEventInScope) {
|
| - StackFrame h = "Honeycomb";
|
| +TEST_F(AllocationContextTest, BacktraceTakesTop) {
|
| + TRACE_EVENT0("Testing", "Cupcake");
|
| + TRACE_EVENT0("Testing", "Donut");
|
| + TRACE_EVENT0("Testing", "Eclair");
|
| + TRACE_EVENT0("Testing", "Froyo");
|
| + TRACE_EVENT0("Testing", "Gingerbread");
|
| + TRACE_EVENT0("Testing", "Honeycomb");
|
| + TRACE_EVENT0("Testing", "Ice Cream Sandwich");
|
| + TRACE_EVENT0("Testing", "Jelly Bean");
|
|
|
| {
|
| - TRACE_EVENT0("Testing", "Gingerbread");
|
| - EnableTracing();
|
| - AssertPseudoStackEmpty();
|
| -
|
| - {
|
| - TRACE_EVENT0("Testing", "Honeycomb");
|
| - StackFrame frame_h[] = {h};
|
| - AssertPseudoStackEquals(frame_h);
|
| - }
|
| -
|
| - AssertPseudoStackEmpty();
|
| + TRACE_EVENT0("Testing", "KitKat");
|
| + AllocationContext ctx = AllocationContextTracker::GetContext();
|
| + ASSERT_STREQ("KitKat", ctx.backtrace[0]);
|
| + ASSERT_STREQ("Donut", ctx.backtrace[7]);
|
| + }
|
|
|
| - // The pop at the end of this scope for the 'Gingerbread' frame must not
|
| - // cause a stack underflow.
|
| + {
|
| + AllocationContext ctx = AllocationContextTracker::GetContext();
|
| + ASSERT_STREQ("Jelly Bean", ctx.backtrace[0]);
|
| + ASSERT_STREQ("Cupcake", ctx.backtrace[7]);
|
| }
|
| - AssertPseudoStackEmpty();
|
| - DisableTracing();
|
| }
|
|
|
| } // namespace trace_event
|
|
|