| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/trace_event/memory_profiler_allocation_context.h" | |
| 6 #include "base/trace_event/trace_event.h" | |
| 7 #include "testing/gtest/include/gtest/gtest.h" | |
| 8 | |
| 9 namespace base { | |
| 10 namespace trace_event { | |
| 11 | |
| 12 // Asserts that the fixed-size array |expected_stack| matches the pseudo | |
| 13 // stack. Syntax note: |const StackFrame (&expected_stack)[N]| is the syntax | |
| 14 // for "expected_stack is a reference to a const fixed-size array of StackFrame | |
| 15 // of length N". | |
| 16 template <size_t N> | |
| 17 void AssertPseudoStackEquals(const StackFrame (&expected_stack)[N]) { | |
| 18 auto pseudo_stack = AllocationContextTracker::GetPseudoStackForTesting(); | |
| 19 auto actual = pseudo_stack->top(); | |
| 20 auto actual_bottom = pseudo_stack->bottom(); | |
| 21 auto expected = expected_stack; | |
| 22 auto expected_bottom = expected_stack + N; | |
| 23 | |
| 24 for (; actual != actual_bottom && expected != expected_bottom; | |
| 25 actual++, expected++) { | |
| 26 ASSERT_STREQ(*expected, *actual); | |
| 27 } | |
| 28 | |
| 29 // Ensure that the height of the stacks is the same. | |
| 30 ASSERT_EQ(actual, actual_bottom); | |
| 31 ASSERT_EQ(expected, expected_bottom); | |
| 32 } | |
| 33 | |
| 34 void AssertPseudoStackEmpty() { | |
| 35 auto pseudo_stack = AllocationContextTracker::GetPseudoStackForTesting(); | |
| 36 ASSERT_EQ(pseudo_stack->top(), pseudo_stack->bottom()); | |
| 37 } | |
| 38 | |
| 39 class AllocationContextTest : public testing::Test { | |
| 40 public: | |
| 41 void EnableTracing() { | |
| 42 TraceConfig config(""); | |
| 43 TraceLog::GetInstance()->SetEnabled(config, TraceLog::RECORDING_MODE); | |
| 44 AllocationContextTracker::SetCaptureEnabled(true); | |
| 45 } | |
| 46 | |
| 47 void DisableTracing() { | |
| 48 AllocationContextTracker::SetCaptureEnabled(false); | |
| 49 TraceLog::GetInstance()->SetDisabled(); | |
| 50 } | |
| 51 }; | |
| 52 | |
| 53 TEST_F(AllocationContextTest, PseudoStackScopedTrace) { | |
| 54 StackFrame c = "Cupcake"; | |
| 55 StackFrame d = "Donut"; | |
| 56 StackFrame e = "Eclair"; | |
| 57 StackFrame f = "Froyo"; | |
| 58 | |
| 59 EnableTracing(); | |
| 60 AssertPseudoStackEmpty(); | |
| 61 | |
| 62 { | |
| 63 TRACE_EVENT0("Testing", "Cupcake"); | |
| 64 StackFrame frame_c[] = {c}; | |
| 65 AssertPseudoStackEquals(frame_c); | |
| 66 | |
| 67 { | |
| 68 TRACE_EVENT0("Testing", "Donut"); | |
| 69 StackFrame frame_dc[] = {d, c}; | |
| 70 AssertPseudoStackEquals(frame_dc); | |
| 71 } | |
| 72 | |
| 73 AssertPseudoStackEquals(frame_c); | |
| 74 | |
| 75 { | |
| 76 TRACE_EVENT0("Testing", "Eclair"); | |
| 77 StackFrame frame_ec[] = {e, c}; | |
| 78 AssertPseudoStackEquals(frame_ec); | |
| 79 } | |
| 80 | |
| 81 AssertPseudoStackEquals(frame_c); | |
| 82 } | |
| 83 | |
| 84 AssertPseudoStackEmpty(); | |
| 85 | |
| 86 { | |
| 87 TRACE_EVENT0("Testing", "Froyo"); | |
| 88 StackFrame frame_f[] = {f}; | |
| 89 AssertPseudoStackEquals(frame_f); | |
| 90 } | |
| 91 | |
| 92 AssertPseudoStackEmpty(); | |
| 93 DisableTracing(); | |
| 94 } | |
| 95 | |
| 96 TEST_F(AllocationContextTest, PseudoStackBeginEndTrace) { | |
| 97 StackFrame c = "Cupcake"; | |
| 98 StackFrame d = "Donut"; | |
| 99 StackFrame e = "Eclair"; | |
| 100 StackFrame f = "Froyo"; | |
| 101 | |
| 102 StackFrame frame_c[] = {c}; | |
| 103 StackFrame frame_dc[] = {d, c}; | |
| 104 StackFrame frame_ec[] = {e, c}; | |
| 105 StackFrame frame_f[] = {f}; | |
| 106 | |
| 107 EnableTracing(); | |
| 108 AssertPseudoStackEmpty(); | |
| 109 | |
| 110 TRACE_EVENT_BEGIN0("Testing", "Cupcake"); | |
| 111 AssertPseudoStackEquals(frame_c); | |
| 112 | |
| 113 TRACE_EVENT_BEGIN0("Testing", "Donut"); | |
| 114 AssertPseudoStackEquals(frame_dc); | |
| 115 TRACE_EVENT_END0("Testing", "Donut"); | |
| 116 | |
| 117 AssertPseudoStackEquals(frame_c); | |
| 118 | |
| 119 TRACE_EVENT_BEGIN0("Testing", "Eclair"); | |
| 120 AssertPseudoStackEquals(frame_ec); | |
| 121 TRACE_EVENT_END0("Testing", "Eclair"); | |
| 122 | |
| 123 AssertPseudoStackEquals(frame_c); | |
| 124 TRACE_EVENT_END0("Testing", "Cupcake"); | |
| 125 | |
| 126 AssertPseudoStackEmpty(); | |
| 127 | |
| 128 TRACE_EVENT_BEGIN0("Testing", "Froyo"); | |
| 129 AssertPseudoStackEquals(frame_f); | |
| 130 TRACE_EVENT_END0("Testing", "Froyo"); | |
| 131 | |
| 132 AssertPseudoStackEmpty(); | |
| 133 DisableTracing(); | |
| 134 } | |
| 135 | |
| 136 TEST_F(AllocationContextTest, PseudoStackMixedTrace) { | |
| 137 StackFrame c = "Cupcake"; | |
| 138 StackFrame d = "Donut"; | |
| 139 StackFrame e = "Eclair"; | |
| 140 StackFrame f = "Froyo"; | |
| 141 | |
| 142 StackFrame frame_c[] = {c}; | |
| 143 StackFrame frame_dc[] = {d, c}; | |
| 144 StackFrame frame_e[] = {e}; | |
| 145 StackFrame frame_fe[] = {f, e}; | |
| 146 | |
| 147 EnableTracing(); | |
| 148 AssertPseudoStackEmpty(); | |
| 149 | |
| 150 TRACE_EVENT_BEGIN0("Testing", "Cupcake"); | |
| 151 AssertPseudoStackEquals(frame_c); | |
| 152 | |
| 153 { | |
| 154 TRACE_EVENT0("Testing", "Donut"); | |
| 155 AssertPseudoStackEquals(frame_dc); | |
| 156 } | |
| 157 | |
| 158 AssertPseudoStackEquals(frame_c); | |
| 159 TRACE_EVENT_END0("Testing", "Cupcake"); | |
| 160 AssertPseudoStackEmpty(); | |
| 161 | |
| 162 { | |
| 163 TRACE_EVENT0("Testing", "Eclair"); | |
| 164 AssertPseudoStackEquals(frame_e); | |
| 165 | |
| 166 TRACE_EVENT_BEGIN0("Testing", "Froyo"); | |
| 167 AssertPseudoStackEquals(frame_fe); | |
| 168 TRACE_EVENT_END0("Testing", "Froyo"); | |
| 169 AssertPseudoStackEquals(frame_e); | |
| 170 } | |
| 171 | |
| 172 AssertPseudoStackEmpty(); | |
| 173 DisableTracing(); | |
| 174 } | |
| 175 | |
| 176 TEST_F(AllocationContextTest, PseudoStackEnableWithEventInScope) { | |
| 177 StackFrame h = "Honeycomb"; | |
| 178 | |
| 179 { | |
| 180 TRACE_EVENT0("Testing", "Gingerbread"); | |
| 181 EnableTracing(); | |
| 182 AssertPseudoStackEmpty(); | |
| 183 | |
| 184 { | |
| 185 TRACE_EVENT0("Testing", "Honeycomb"); | |
| 186 StackFrame frame_h[] = {h}; | |
| 187 AssertPseudoStackEquals(frame_h); | |
| 188 } | |
| 189 | |
| 190 AssertPseudoStackEmpty(); | |
| 191 | |
| 192 // The pop at the end of this scope for the 'Gingerbread' frame must not | |
| 193 // cause a stack underflow. | |
| 194 } | |
| 195 AssertPseudoStackEmpty(); | |
| 196 DisableTracing(); | |
| 197 } | |
| 198 | |
| 199 } // namespace trace_event | |
| 200 } // namespace base | |
| OLD | NEW |