Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(77)

Side by Side Diff: base/trace_event/memory_profiler_allocation_context_unittest.cc

Issue 1340013002: Add thread-local allocation context for tracing (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Move push/pop into TraceLog, add sanity checks to pseudo stack Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 <cstring>
6
7 #include "base/trace_event/memory_profiler_allocation_context.h"
8 #include "base/trace_event/trace_event.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace base {
12 namespace trace_event {
13
14 void AssertFramesEqual(StackFrame lhs, StackFrame rhs) {
Primiano Tucci (use gerrit) 2015/09/24 15:58:35 you can remove this function and just use ASSERT_S
Ruud van Asseldonk 2015/09/24 17:05:40 Done.
15 // In the test, do a deep strcmp to check that the stack frames are equal,
16 // because they are different string literals in the source. The pointers
17 // might be equal anyway due to string interning, but this is not guaranteed.
18 // In production it is fine to compare the pointers, because the only source
19 // of const char ptrs are the string literals in the trace macros.
20 ASSERT_EQ(0, strcmp(lhs, rhs));
21 };
22
23 // Asserts that the fixed-size array |expected_stack| matches the pseudo
24 // stack. Syntax note: |StackFrame (&expected_stack)[N]| is the syntax
25 // for "expected_stack is a reference to a fixed-size array of StackFrame
26 // of length N".
27 template <size_t N>
28 void AssertPseudoStackEquals(StackFrame(&expected_stack)[N]) {
Primiano Tucci (use gerrit) 2015/09/24 15:58:35 +const? (otherwise there is the risk that plebeian
Ruud van Asseldonk 2015/09/24 17:05:40 Of course.
29 auto pseudo_stack = AllocationContextTracker::GetPseudoStackForTesting();
30 auto actual = pseudo_stack->top();
31 auto actual_bottom = pseudo_stack->bottom();
32 auto expected = expected_stack;
33 auto expected_bottom = expected_stack + N;
34
35 for (; actual != actual_bottom && expected != expected_bottom;
36 actual++, expected++) {
37 AssertFramesEqual(*expected, *actual);
38 }
39
40 // Ensure that the height of the stacks is the same.
41 ASSERT_EQ(actual, actual_bottom);
42 ASSERT_EQ(expected, expected_bottom);
43 }
44
45 void AssertPseudoStackEmpty() {
46 auto pseudo_stack = AllocationContextTracker::GetPseudoStackForTesting();
47 ASSERT_EQ(pseudo_stack->top(), pseudo_stack->bottom());
48 }
49
50 class AllocationContextTest : public testing::Test {
51 public:
52 void EnableTracing() {
53 TraceConfig config("");
54 TraceLog::GetInstance()->SetEnabled(config, TraceLog::RECORDING_MODE);
55 AllocationContextTracker::SetCaptureEnabled(true);
56 }
57
58 void DisableTracing() {
59 AllocationContextTracker::SetCaptureEnabled(false);
60 TraceLog::GetInstance()->SetDisabled();
61 }
62 };
63
64 TEST_F(AllocationContextTest, PseudoStackScopedTrace) {
65 StackFrame c = "Cupcake";
66 StackFrame d = "Donut";
67 StackFrame e = "Eclair";
68 StackFrame f = "Froyo";
69
70 EnableTracing();
71 AssertPseudoStackEmpty();
72
73 {
74 TRACE_EVENT0("Testing", "Cupcake");
75 StackFrame frame_c[] = {c};
76 AssertPseudoStackEquals(frame_c);
77
78 {
79 TRACE_EVENT0("Testing", "Donut");
80 StackFrame frame_dc[] = {d, c};
81 AssertPseudoStackEquals(frame_dc);
Primiano Tucci (use gerrit) 2015/09/24 15:58:34 maybe just AssertPseudoStackEquals({d, c}); withou
Ruud van Asseldonk 2015/09/24 17:05:40 Then the compiler cannot infer the int template ar
Primiano Tucci (use gerrit) 2015/09/24 19:34:43 oh right, nevermind then. sgtm
82 }
83
84 AssertPseudoStackEquals(frame_c);
85
86 {
87 TRACE_EVENT0("Testing", "Eclair");
88 StackFrame frame_ec[] = {e, c};
89 AssertPseudoStackEquals(frame_ec);
90 }
91
92 AssertPseudoStackEquals(frame_c);
93 }
94
95 AssertPseudoStackEmpty();
96
97 {
98 TRACE_EVENT0("Testing", "Froyo");
99 StackFrame frame_f[] = {f};
100 AssertPseudoStackEquals(frame_f);
101 }
102
103 AssertPseudoStackEmpty();
104 DisableTracing();
105 }
106
107 TEST_F(AllocationContextTest, PseudoStackBeginEndTrace) {
108 StackFrame c = "Cupcake";
109 StackFrame d = "Donut";
110 StackFrame e = "Eclair";
111 StackFrame f = "Froyo";
112
113 StackFrame frame_c[] = {c};
114 StackFrame frame_dc[] = {d, c};
115 StackFrame frame_ec[] = {e, c};
116 StackFrame frame_f[] = {f};
117
118 EnableTracing();
119 AssertPseudoStackEmpty();
120
121 TRACE_EVENT_BEGIN0("Testing", "Cupcake");
122 AssertPseudoStackEquals(frame_c);
123
124 TRACE_EVENT_BEGIN0("Testing", "Donut");
125 AssertPseudoStackEquals(frame_dc);
126 TRACE_EVENT_END0("Testing", "Donut");
127
128 AssertPseudoStackEquals(frame_c);
129
130 TRACE_EVENT_BEGIN0("Testing", "Eclair");
131 AssertPseudoStackEquals(frame_ec);
132 TRACE_EVENT_END0("Testing", "Eclair");
133
134 AssertPseudoStackEquals(frame_c);
135 TRACE_EVENT_END0("Testing", "Cupcake");
136
137 AssertPseudoStackEmpty();
138
139 TRACE_EVENT_BEGIN0("Testing", "Froyo");
140 AssertPseudoStackEquals(frame_f);
141 TRACE_EVENT_END0("Testing", "Froyo");
142
143 AssertPseudoStackEmpty();
144 DisableTracing();
145 }
146
147 TEST_F(AllocationContextTest, PseudoStackEnableWithEventInScope) {
148 StackFrame h = "Honeycomb";
149
150 {
151 TRACE_EVENT0("Testing", "Gingerbread");
152 EnableTracing();
153 AssertPseudoStackEmpty();
154
155 {
156 TRACE_EVENT0("Testing", "Honeycomb");
157 StackFrame frame_h[] = {h};
158 AssertPseudoStackEquals(frame_h);
159 }
160
161 AssertPseudoStackEmpty();
162
163 // The pop at the end of this scope for the 'Gingerbread' frame must not
164 // cause a stack underflow.
165 }
166 AssertPseudoStackEmpty();
167 DisableTracing();
168 }
169
Primiano Tucci (use gerrit) 2015/09/24 15:58:35 you probably want also a test that covers mixed ca
Ruud van Asseldonk 2015/09/24 17:05:41 Done.
170 } // namespace trace_event
171 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698