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