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

Side by Side Diff: base/debug/trace_event_value.h

Issue 19642005: Make TracedValue lower overhead. Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Added pickle unit test, cleaned up traced value's unit tests. Created 7 years, 4 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 2013 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 #ifndef BASE_TRACE_EVENT_VALUE_H_
dsinclair 2013/08/07 15:40:20 This should be BASE_DEBUG_TRACE_EVENT_VALUE_H_.
rterrazas 2013/08/08 06:21:30 Done.
6 #define BASE_TRACE_EVENT_VALUE_H_
7
8 #include <stack>
9 #include <string>
10
11 #include "base/debug/trace_event.h"
12 #include "base/debug/traced_object.h"
13 #include "base/format_macros.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/pickle.h"
16 #include "base/threading/non_thread_safe.h"
17
18 namespace base {
19 namespace debug {
20
21 // TracedValue has several functions to push data that will be converted to
22 // trace event format without depending on base::Value objects.
23 class BASE_EXPORT TracedValue : public base::debug::ConvertableToTraceFormat,
24 public base::NonThreadSafe {
25 public:
26 TracedValue();
27 virtual ~TracedValue();
28
29 virtual void AppendAsTraceFormat(std::string* out) const OVERRIDE;
30
31 // Begin a dictionary, that is a value within another dictionary.
32 void BeginDictionary(const char* key_in_parent_dict);
dsinclair 2013/08/07 15:40:20 What about calling the two begins that take a key:
rterrazas 2013/08/08 06:21:30 Done.
33 // Begin a dictionary, it can either be a top-level structure or a value in
34 // an array.
35 void BeginDictionary();
36 // Used for both kinds of BeginDictionary functions.
37 void EndDictionary();
38 // Begin an array, that is a value within a dictonary.
39 void BeginArray(const char* key_in_parent_dict);
40 // Begin an array, that is either a top-level structure or a value in an
41 // array.
42 void BeginArray();
43 // Used for both kinds of BeginArray functions.
44 void EndArray();
45
46 // Put stuff in dictionary using statically allocated string keys.
47 #define TRACE_PUSH(type) \
rterrazas 2013/08/05 02:58:29 Linter think's we're using a C-style cast here, ca
48 void Push(const char* key, type value) { \
dsinclair 2013/08/07 15:40:20 nit: indent 2
rterrazas 2013/08/08 06:21:30 Done.
49 PushKeyToPickle(key); \
dsinclair 2013/08/07 15:40:20 Should this have a: DCHECK_EQ(BEGIN_DICTIONARY, be
rterrazas 2013/08/08 06:21:30 Yes, I had it in PushKeyToPickle() but I agree, ma
50 PushValueToPickle(value); \
51 }
52 TRACE_PUSH(const char*)
53 TRACE_PUSH(const std::string&)
54 TRACE_PUSH(int32)
55 TRACE_PUSH(int64)
56 TRACE_PUSH(uint32)
57 TRACE_PUSH(uint64)
58 TRACE_PUSH(float)
59 TRACE_PUSH(bool)
60 TRACE_PUSH(const TracedObject&)
61 #undef TRACE_PUSH
62
63 // Put stuff in an array.
64 #define TRACE_PUSH(type) \
65 void Push(type value) { \
dsinclair 2013/08/07 15:40:20 nit: indent 2
rterrazas 2013/08/08 06:21:30 Done.
66 DCHECK_EQ(BEGIN_ARRAY, begin_opcodes_.top()); \
67 PushValueToPickle(value); \
68 }
69 TRACE_PUSH(const char*)
70 TRACE_PUSH(const std::string&)
71 TRACE_PUSH(int32)
72 TRACE_PUSH(int64)
73 TRACE_PUSH(uint32)
74 TRACE_PUSH(uint64)
75 TRACE_PUSH(float)
76 TRACE_PUSH(bool)
77 TRACE_PUSH(const TracedObject&)
78 #undef TRACE_PUSH
79
80 private:
81 // Push key/value should not be exposed, instead, the Push methods are
82 // exposed, to have them assert that we are in the right state before writing
83 // to the pickle.
84 void PushKeyToPickle(const char* key);
85 void PushValueToPickle(const char* value);
86 void PushValueToPickle(const std::string& value);
87 void PushValueToPickle(int value);
88 void PushValueToPickle(int64 value);
89 void PushValueToPickle(uint32 value);
90 void PushValueToPickle(uint64 value);
91 void PushValueToPickle(float value);
92 void PushValueToPickle(bool value);
93 void PushValueToPickle(const TracedObject& value);
94
95 // Helper methods used in AppendAsTraceFormat().
96 void AppendKeyAsRawPointer(std::string* out, PickleIterator* iter) const;
97 void AppendKeyAsString(std::string* out,
98 PickleIterator* iter) const;
99 void AppendValueAsRawPointer(std::string* out, PickleIterator* iter) const;
100 void AppendValueAsString(std::string* out, PickleIterator* iter) const;
101 void AppendValueAsInt32(std::string* out, PickleIterator* iter) const;
102 void AppendValueAsInt64(std::string* out, PickleIterator* iter) const;
103 void AppendValueAsUInt32(std::string* out, PickleIterator* iter) const;
104 void AppendValueAsUInt64(std::string* out, PickleIterator* iter) const;
105 void AppendValueAsFloat(std::string* out, PickleIterator* iter) const;
106 void AppendValueAsBool(std::string* out, PickleIterator* iter) const;
107
108 Pickle pickle_;
109 enum Opcode {
110 BEGIN_DICTIONARY,
111 END_DICTIONARY,
112 BEGIN_ARRAY,
113 END_ARRAY,
114 KEY_AS_RAW_POINTER,
115 VALUE_AS_RAW_POINTER,
116 VALUE_AS_STRING,
117 INT32_VALUE,
118 INT64_VALUE,
119 UINT32_VALUE,
120 UINT64_VALUE,
121 FLOAT_VALUE,
122 BOOL_VALUE
123 };
124
125 // This method is in charge of preppending a comma before a value within an
126 // array, or a dictionary. If necessary, the element count will be
127 // incremented.
128 static void AppendComma(std::string* out, Opcode begin_opcode,
129 Opcode current_opcode, int* element_count);
130
131 // This is used to keep track of the operations that need to be closed, in
132 // in order to enforce consistency in the operations. The only opcodes that
133 // need to be kept in this datastructure are BEGIN_* opcodes, such as
134 // BEGIN_ARRAY or BEGIN_DICTIONARY, using this stack, we know that we can't
135 // do an array push, when we have a dictionary, and we end a dictionary, when
136 // we were working on an array.
137 std::stack<Opcode> begin_opcodes_;
138
139 DISALLOW_COPY_AND_ASSIGN(TracedValue);
140 };
141
142 } // namespace debug
143 } // namespace base
144
145 #endif
146
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698