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

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

Issue 1340013002: Add thread-local allocation context for tracing (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix typo Created 5 years, 3 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 (c) 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 #include <iterator>
7
8 #include "base/trace_event/pseudo_stack.h"
9 #include "base/trace_event/trace_event.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace base {
13 namespace trace_event {
14
15 bool operator==(const PseudoStackFrame& lhs, const PseudoStackFrame& rhs) {
16 return strcmp(lhs.name, rhs.name) == 0;
17 }
18
19 void CheckPseudoStackEquals(const PseudoStackFrame* expected_top,
20 const PseudoStackFrame* expected_bottom) {
21 auto actual = PseudoStack::GetInstance()->Top();
22 auto actual_bottom = PseudoStack::GetInstance()->Bottom();
23 auto expected = expected_top;
24
25 for (; actual != actual_bottom && expected != expected_bottom;
26 actual++, expected++) {
27 ASSERT_EQ(*expected, *actual);
28 }
29
30 // Ensure that the height of the stacks is the same.
31 ASSERT_TRUE(actual == actual_bottom && expected == expected_bottom);
32 }
33
34 void CheckPseudoStackEmpty() {
35 CheckPseudoStackEquals(nullptr, nullptr);
36 }
37
38 class PseudoStackTest : public testing::Test {
39 public:
40 void EnableTracing() {
41 TraceConfig config(
42 "{\"included_categories\": [\"disabled-by-default-memory-infra\"]}");
43 TraceLog::GetInstance()->SetEnabled(config, TraceLog::RECORDING_MODE);
44 }
45
46 void DisableTracing() { TraceLog::GetInstance()->SetDisabled(); }
47
48 private:
49 PseudoStackController controller_;
50 };
51
52 TEST_F(PseudoStackTest, ScopedTrace) {
53 PseudoStackFrame c = {"Cupcake"};
54 PseudoStackFrame d = {"Donut"};
55 PseudoStackFrame e = {"Eclair"};
56 PseudoStackFrame f = {"Froyo"};
57
58 EnableTracing();
59 CheckPseudoStackEmpty();
60
61 {
62 TRACE_EVENT0("Testing", "Cupcake");
63 PseudoStackFrame frame_c[] = {c};
64 CheckPseudoStackEquals(std::begin(frame_c), std::end(frame_c));
65
66 {
67 TRACE_EVENT0("Testing", "Donut");
68 PseudoStackFrame frame_dc[] = {d, c};
69 CheckPseudoStackEquals(std::begin(frame_dc), std::end(frame_dc));
70 }
71
72 CheckPseudoStackEquals(std::begin(frame_c), std::end(frame_c));
73
74 {
75 TRACE_EVENT0("Testing", "Eclair");
76 PseudoStackFrame frame_ec[] = {e, c};
77 CheckPseudoStackEquals(std::begin(frame_ec), std::end(frame_ec));
78 }
79
80 CheckPseudoStackEquals(std::begin(frame_c), std::end(frame_c));
81 }
82
83 CheckPseudoStackEmpty();
84
85 {
86 TRACE_EVENT0("Testing", "Froyo");
87 PseudoStackFrame frame_f[] = {f};
88 CheckPseudoStackEquals(std::begin(frame_f), std::end(frame_f));
89 }
90
91 CheckPseudoStackEmpty();
92 DisableTracing();
93 }
94
95 TEST_F(PseudoStackTest, EnableWithEventInScope) {
96 PseudoStackFrame h = {"Honeycomb"};
97
98 {
99 TRACE_EVENT0("Testing", "Gingerbread");
100 EnableTracing();
101 CheckPseudoStackEmpty();
102
103 {
104 TRACE_EVENT0("Testing", "Honeycomb");
105 PseudoStackFrame frame_h[] = {h};
106 CheckPseudoStackEquals(std::begin(frame_h), std::end(frame_h));
107 }
108
109 CheckPseudoStackEmpty();
110
111 // The pop at the end of this scope for the 'Gingerbread' frame must not
112 // cause a stack underflow.
113 }
114 CheckPseudoStackEmpty();
115 DisableTracing();
116 }
117
118 TEST_F(PseudoStackTest, GenerationsAreIndependent) {
119 PseudoStackFrame i = {"Ice Cream Sandwich"};
120
121 EnableTracing();
122 CheckPseudoStackEmpty();
123 {
124 TRACE_EVENT0("Testing", "Ice Cream Sandwich");
125 PseudoStackFrame frame_i[] = {i};
126 CheckPseudoStackEquals(std::begin(frame_i), std::end(frame_i));
127
128 // Disable tracing before popping the frame, so it never gets popped.
129 DisableTracing();
130 }
131
132 EnableTracing();
133 CheckPseudoStackEmpty();
134 DisableTracing();
135 }
136
137 } // namespace trace_event
138 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698