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/trace_event/heap_profiler_allocation_context.h" | 10 #include "base/trace_event/heap_profiler_allocation_context.h" |
11 #include "base/trace_event/heap_profiler_allocation_context_tracker.h" | 11 #include "base/trace_event/heap_profiler_allocation_context_tracker.h" |
12 #include "base/trace_event/trace_event.h" | 12 #include "base/trace_event/trace_event.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 | 14 |
15 namespace base { | 15 namespace base { |
16 namespace trace_event { | 16 namespace trace_event { |
17 | 17 |
18 // Define all strings once, because the pseudo stack requires pointer equality, | 18 // Define all strings once, because the pseudo stack requires pointer equality, |
19 // and string interning is unreliable. | 19 // and string interning is unreliable. |
20 const char kCupcake[] = "Cupcake"; | 20 const char kCupcake[] = "Cupcake"; |
21 const char kDonut[] = "Donut"; | 21 const char kDonut[] = "Donut"; |
22 const char kEclair[] = "Eclair"; | 22 const char kEclair[] = "Eclair"; |
23 const char kFroyo[] = "Froyo"; | 23 const char kFroyo[] = "Froyo"; |
24 const char kGingerbread[] = "Gingerbread"; | 24 const char kGingerbread[] = "Gingerbread"; |
25 | 25 |
26 // Asserts that the fixed-size array |expected_backtrace| matches the backtrace | 26 // Asserts that the fixed-size array |expected_backtrace| matches the backtrace |
27 // in |AllocationContextTracker::GetContextSnapshot|. | 27 // in |AllocationContextTracker::GetContextSnapshot|. |
28 template <size_t N> | 28 template <size_t N> |
29 void AssertBacktraceEquals(const StackFrame(&expected_backtrace)[N]) { | 29 void AssertBacktraceEquals(const StackFrame(&expected_backtrace)[N]) { |
30 AllocationContext ctx = AllocationContextTracker::GetContextSnapshot(); | 30 AllocationContext ctx = |
| 31 AllocationContextTracker::GetInstanceForCurrentThread() |
| 32 ->GetContextSnapshot(); |
31 | 33 |
32 auto actual = std::begin(ctx.backtrace.frames); | 34 auto actual = std::begin(ctx.backtrace.frames); |
33 auto actual_bottom = std::end(ctx.backtrace.frames); | 35 auto actual_bottom = std::end(ctx.backtrace.frames); |
34 auto expected = std::begin(expected_backtrace); | 36 auto expected = std::begin(expected_backtrace); |
35 auto expected_bottom = std::end(expected_backtrace); | 37 auto expected_bottom = std::end(expected_backtrace); |
36 | 38 |
37 // Note that this requires the pointers to be equal, this is not doing a deep | 39 // Note that this requires the pointers to be equal, this is not doing a deep |
38 // string comparison. | 40 // string comparison. |
39 for (; actual != actual_bottom && expected != expected_bottom; | 41 for (; actual != actual_bottom && expected != expected_bottom; |
40 actual++, expected++) | 42 actual++, expected++) |
41 ASSERT_EQ(*expected, *actual); | 43 ASSERT_EQ(*expected, *actual); |
42 | 44 |
43 // Ensure that the height of the stacks is the same. | 45 // Ensure that the height of the stacks is the same. |
44 ASSERT_EQ(actual, actual_bottom); | 46 ASSERT_EQ(actual, actual_bottom); |
45 ASSERT_EQ(expected, expected_bottom); | 47 ASSERT_EQ(expected, expected_bottom); |
46 } | 48 } |
47 | 49 |
48 void AssertBacktraceEmpty() { | 50 void AssertBacktraceEmpty() { |
49 AllocationContext ctx = AllocationContextTracker::GetContextSnapshot(); | 51 AllocationContext ctx = |
| 52 AllocationContextTracker::GetInstanceForCurrentThread() |
| 53 ->GetContextSnapshot(); |
50 | 54 |
51 for (StackFrame frame : ctx.backtrace.frames) | 55 for (StackFrame frame : ctx.backtrace.frames) |
52 ASSERT_EQ(nullptr, frame); | 56 ASSERT_EQ(nullptr, frame); |
53 } | 57 } |
54 | 58 |
55 class AllocationContextTrackerTest : public testing::Test { | 59 class AllocationContextTrackerTest : public testing::Test { |
56 public: | 60 public: |
57 void SetUp() override { | 61 void SetUp() override { |
58 TraceConfig config(""); | 62 TraceConfig config(""); |
59 TraceLog::GetInstance()->SetEnabled(config, TraceLog::RECORDING_MODE); | 63 TraceLog::GetInstance()->SetEnabled(config, TraceLog::RECORDING_MODE); |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 TRACE_EVENT0("Testing", kCupcake); | 204 TRACE_EVENT0("Testing", kCupcake); |
201 TRACE_EVENT0("Testing", kCupcake); | 205 TRACE_EVENT0("Testing", kCupcake); |
202 | 206 |
203 TRACE_EVENT0("Testing", kCupcake); | 207 TRACE_EVENT0("Testing", kCupcake); |
204 TRACE_EVENT0("Testing", kDonut); | 208 TRACE_EVENT0("Testing", kDonut); |
205 TRACE_EVENT0("Testing", kEclair); | 209 TRACE_EVENT0("Testing", kEclair); |
206 TRACE_EVENT0("Testing", kFroyo); | 210 TRACE_EVENT0("Testing", kFroyo); |
207 | 211 |
208 { | 212 { |
209 TRACE_EVENT0("Testing", kGingerbread); | 213 TRACE_EVENT0("Testing", kGingerbread); |
210 AllocationContext ctx = AllocationContextTracker::GetContextSnapshot(); | 214 AllocationContext ctx = |
| 215 AllocationContextTracker::GetInstanceForCurrentThread() |
| 216 ->GetContextSnapshot(); |
211 | 217 |
212 // The pseudo stack relies on pointer equality, not deep string comparisons. | 218 // The pseudo stack relies on pointer equality, not deep string comparisons. |
213 ASSERT_EQ(kCupcake, ctx.backtrace.frames[0]); | 219 ASSERT_EQ(kCupcake, ctx.backtrace.frames[0]); |
214 ASSERT_EQ(kFroyo, ctx.backtrace.frames[11]); | 220 ASSERT_EQ(kFroyo, ctx.backtrace.frames[11]); |
215 } | 221 } |
216 | 222 |
217 { | 223 { |
218 AllocationContext ctx = AllocationContextTracker::GetContextSnapshot(); | 224 AllocationContext ctx = |
| 225 AllocationContextTracker::GetInstanceForCurrentThread() |
| 226 ->GetContextSnapshot(); |
219 ASSERT_EQ(kCupcake, ctx.backtrace.frames[0]); | 227 ASSERT_EQ(kCupcake, ctx.backtrace.frames[0]); |
220 ASSERT_EQ(kFroyo, ctx.backtrace.frames[11]); | 228 ASSERT_EQ(kFroyo, ctx.backtrace.frames[11]); |
221 } | 229 } |
222 } | 230 } |
223 | 231 |
224 } // namespace trace_event | 232 } // namespace trace_event |
225 } // namespace base | 233 } // namespace base |
OLD | NEW |