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

Side by Side Diff: components/tracing/core/proto_zero_message.h

Issue 2240043004: Tracing V2: Fully-functional plugin. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: nit Created 4 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 #ifndef COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_H_ 5 #ifndef COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_H_
6 #define COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_H_ 6 #define COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_H_
7 7
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <set>
10 #include <type_traits> 11 #include <type_traits>
11 12
12 #include "base/compiler_specific.h" 13 #include "base/compiler_specific.h"
13 #include "base/gtest_prod_util.h" 14 #include "base/gtest_prod_util.h"
14 #include "base/logging.h" 15 #include "base/logging.h"
15 #include "base/macros.h" 16 #include "base/macros.h"
16 #include "base/template_util.h" 17 #include "base/template_util.h"
17 #include "build/build_config.h" 18 #include "build/build_config.h"
18 #include "components/tracing/core/proto_utils.h" 19 #include "components/tracing/core/proto_utils.h"
19 #include "components/tracing/core/scattered_stream_writer.h" 20 #include "components/tracing/core/scattered_stream_writer.h"
20 #include "components/tracing/tracing_export.h" 21 #include "components/tracing/tracing_export.h"
21 22
22 namespace tracing { 23 namespace tracing {
23 namespace v2 { 24 namespace v2 {
24 25
26 #if DCHECK_IS_ON()
Primiano Tucci (use gerrit) 2016/08/24 08:59:45 is this causing an actual compilation error? If no
25 class ProtoZeroMessageHandleBase; 27 class ProtoZeroMessageHandleBase;
28 #endif
26 29
27 // Base class extended by the proto C++ stubs generated by the ProtoZero 30 // Base class extended by the proto C++ stubs generated by the ProtoZero
28 // compiler (see //components/tracing/tools/). This class provides the minimal 31 // compiler (see //components/tracing/tools/). This class provides the minimal
29 // runtime required to support append-only operations and is desiged for 32 // runtime required to support append-only operations and is desiged for
30 // performance. None of the methods require any dynamic memory allocation. 33 // performance. None of the methods require any dynamic memory allocation.
31 class TRACING_EXPORT ProtoZeroMessage { 34 class TRACING_EXPORT ProtoZeroMessage {
32 public: 35 public:
33 // Clears up the state, allowing the message to be reused as a fresh one. 36 // Clears up the state, allowing the message to be reused as a fresh one.
34 void Reset(ScatteredStreamWriter*); 37 void Reset(ScatteredStreamWriter*);
35 38
(...skipping 19 matching lines...) Expand all
55 void inc_size_already_written(size_t size) { size_already_written_ += size; } 58 void inc_size_already_written(size_t size) { size_already_written_ += size; }
56 59
57 #if DCHECK_IS_ON() 60 #if DCHECK_IS_ON()
58 // Only for ProtoZeroMessageHandleBase. 61 // Only for ProtoZeroMessageHandleBase.
59 void set_handle(ProtoZeroMessageHandleBase* handle) { handle_ = handle; } 62 void set_handle(ProtoZeroMessageHandleBase* handle) { handle_ = handle; }
60 #endif 63 #endif
61 64
62 protected: 65 protected:
63 ProtoZeroMessage(); 66 ProtoZeroMessage();
64 67
65 // Proto types: uint64, uint32, int64, int32, bool, enum. 68 // Proto types: uint64, uint32, int64, int32, enum (large).
66 template <typename T> 69 template <typename T>
67 void AppendVarInt(uint32_t field_id, T value) { 70 void AppendVarInt(uint32_t field_id, T value) {
68 if (nested_message_) 71 if (nested_message_)
69 EndNestedMessage(); 72 EndNestedMessage();
70 73
71 uint8_t buffer[proto::kMaxSimpleFieldEncodedSize]; 74 uint8_t buffer[proto::kMaxSimpleFieldEncodedSize];
72 uint8_t* pos = buffer; 75 uint8_t* pos = buffer;
73 76
74 pos = proto::WriteVarInt(proto::MakeTagVarInt(field_id), pos); 77 pos = proto::WriteVarInt(proto::MakeTagVarInt(field_id), pos);
75 // WriteVarInt encodes signed values in two's complement form. 78 // WriteVarInt encodes signed values in two's complement form.
76 pos = proto::WriteVarInt(value, pos); 79 pos = proto::WriteVarInt(value, pos);
77 WriteToStream(buffer, pos); 80 WriteToStream(buffer, pos);
78 } 81 }
79 82
80 // Proto types: sint64, sint32. 83 // Proto types: sint64, sint32.
81 template <typename T> 84 template <typename T>
82 void AppendSignedVarInt(uint32_t field_id, T value) { 85 void AppendSignedVarInt(uint32_t field_id, T value) {
83 AppendVarInt(field_id, proto::ZigZagEncode(value)); 86 AppendVarInt(field_id, proto::ZigZagEncode(value));
84 } 87 }
85 88
89 // Proto types: bool, enum (small).
90 // It could be looking weird not to optimize writing field_id, but it's
Primiano Tucci (use gerrit) 2016/08/24 08:59:45 Honestly I feel this comment is just too apologeti
91 // unnecessary because compiler already optimize WriteVarInt for constexpr.
92 void AppendTinyVarInt(uint32_t field_id, int32_t value) {
93 DCHECK(0 <= value && value < 0x80);
94 if (nested_message_)
95 EndNestedMessage();
96
97 uint8_t buffer[proto::kMaxSimpleFieldEncodedSize];
98 uint8_t* pos = buffer;
99 pos = proto::WriteVarInt(proto::MakeTagVarInt(field_id), pos);
100 *pos++ = static_cast<uint8_t>(value);
101 WriteToStream(buffer, pos);
102 }
103
86 // Proto types: fixed64, sfixed64, fixed32, sfixed32, double, float. 104 // Proto types: fixed64, sfixed64, fixed32, sfixed32, double, float.
87 template <typename T> 105 template <typename T>
88 void AppendFixed(uint32_t field_id, T value) { 106 void AppendFixed(uint32_t field_id, T value) {
89 if (nested_message_) 107 if (nested_message_)
90 EndNestedMessage(); 108 EndNestedMessage();
91 109
92 uint8_t buffer[proto::kMaxSimpleFieldEncodedSize]; 110 uint8_t buffer[proto::kMaxSimpleFieldEncodedSize];
93 uint8_t* pos = buffer; 111 uint8_t* pos = buffer;
94 112
95 pos = proto::WriteVarInt(proto::MakeTagFixed<T>(field_id), pos); 113 pos = proto::WriteVarInt(proto::MakeTagFixed<T>(field_id), pos);
96 memcpy(pos, &value, sizeof(T)); 114 memcpy(pos, &value, sizeof(T));
97 pos += sizeof(T); 115 pos += sizeof(T);
98 // TODO(kraynov): Optimize memcpy performance, see http://crbug.com/624311 . 116 // TODO(kraynov): Optimize memcpy performance, see http://crbug.com/624311 .
99 WriteToStream(buffer, pos); 117 WriteToStream(buffer, pos);
100 } 118 }
101 119
102 // TODO(kraynov): Add AppendTinyInt.
103 void AppendString(uint32_t field_id, const char* str); 120 void AppendString(uint32_t field_id, const char* str);
104 void AppendBytes(uint32_t field_id, const void* value, size_t size); 121 void AppendBytes(uint32_t field_id, const void* value, size_t size);
105 122
106 // Begins a nested message, using the static storage provided by the parent 123 // Begins a nested message, using the static storage provided by the parent
107 // class (see comment in |nested_messages_arena_|). The nested message ends 124 // class (see comment in |nested_messages_arena_|). The nested message ends
108 // either when Finalize() is called or when any other Append* method is called 125 // either when Finalize() is called or when any other Append* method is called
109 // in the parent class. 126 // in the parent class.
110 // The template argument T is supposed to be a stub class auto generated from 127 // The template argument T is supposed to be a stub class auto generated from
111 // a .proto, hence a subclass of ProtoZeroMessage. 128 // a .proto, hence a subclass of ProtoZeroMessage.
112 template <class T> 129 template <class T>
113 T* BeginNestedMessage(uint32_t field_id) { 130 T* BeginNestedMessage(uint32_t field_id) {
114 // This is to prevent subclasses (which should be autogenerated, though), to 131 // This is to prevent subclasses (which should be autogenerated, though), to
115 // introduce extra state fields (which wouldn't be initialized by Reset()). 132 // introduce extra state fields (which wouldn't be initialized by Reset()).
116 static_assert(std::is_base_of<ProtoZeroMessage, T>::value, 133 static_assert(std::is_base_of<ProtoZeroMessage, T>::value,
117 "T must be a subclass of ProtoZeroMessage"); 134 "T must be a subclass of ProtoZeroMessage");
118 static_assert(sizeof(T) == sizeof(ProtoZeroMessage), 135 static_assert(sizeof(T) == sizeof(ProtoZeroMessage),
119 "ProtoZeroMessage subclasses cannot introduce extra state."); 136 "ProtoZeroMessage subclasses cannot introduce extra state.");
120 T* message = reinterpret_cast<T*>(nested_messages_arena_); 137 T* message = reinterpret_cast<T*>(nested_messages_arena_);
121 BeginNestedMessageInternal(field_id, message); 138 BeginNestedMessageInternal(field_id, message);
122 return message; 139 return message;
123 } 140 }
124 141
142 // Prevent multiple appends for non-repeated fields.
Primiano Tucci (use gerrit) 2016/08/24 08:59:45 honestly I feel this duplicated field detection is
143 inline void DCheckSealField(uint32_t field_id) {
144 #if DCHECK_IS_ON()
145 DCHECK(field_id > 0);
146 SealField(field_id);
147 #endif
148 }
149
125 private: 150 private:
126 friend class ProtoZeroMessageTest; 151 friend class ProtoZeroMessageTest;
127 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, BasicTypesNoNesting); 152 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, BasicTypesNoNesting);
128 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, BackfillSizeOnFinalization); 153 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, BackfillSizeOnFinalization);
129 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, NestedMessagesSimple); 154 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, NestedMessagesSimple);
130 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, StressTest); 155 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, StressTest);
131 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, MessageHandle); 156 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, MessageHandle);
132 157
133 enum : uint32_t { kMaxNestingDepth = 8 }; 158 enum : uint32_t { kMaxNestingDepth = 8 };
134 159
135 void BeginNestedMessageInternal(uint32_t field_id, ProtoZeroMessage*); 160 void BeginNestedMessageInternal(uint32_t field_id, ProtoZeroMessage*);
136 161
137 // Called by Finalize and Append* methods. 162 // Called by Finalize and Append* methods.
138 void EndNestedMessage(); 163 void EndNestedMessage();
139 164
140 void WriteToStream(const uint8_t* src_begin, const uint8_t* src_end) { 165 void WriteToStream(const uint8_t* src_begin, const uint8_t* src_end) {
141 #if DCHECK_IS_ON() 166 #if DCHECK_IS_ON()
142 DCHECK(!sealed_); 167 DCHECK(!sealed_);
143 #endif 168 #endif
144 DCHECK(src_begin < src_end); 169 DCHECK(src_begin < src_end);
145 const size_t size = static_cast<size_t>(src_end - src_begin); 170 const size_t size = static_cast<size_t>(src_end - src_begin);
146 stream_writer_->WriteBytes(src_begin, size); 171 stream_writer_->WriteBytes(src_begin, size);
147 size_ += size; 172 size_ += size;
148 } 173 }
149 174
175 #if DCHECK_IS_ON()
176 void SealField(uint32_t field_id);
177 #endif
178
150 // Only POD fields are allowed. This class's dtor is never called. 179 // Only POD fields are allowed. This class's dtor is never called.
151 // See the comment on the static_assert in the the corresponding .cc file. 180 // See the comment on the static_assert in the the corresponding .cc file.
152 181
153 // The stream writer interface used for the serialization. 182 // The stream writer interface used for the serialization.
154 ScatteredStreamWriter* stream_writer_; 183 ScatteredStreamWriter* stream_writer_;
155 184
156 // Keeps track of the size of the current message. 185 // Keeps track of the size of the current message.
157 size_t size_; 186 size_t size_;
158 187
159 ContiguousMemoryRange size_field_; 188 ContiguousMemoryRange size_field_;
(...skipping 30 matching lines...) Expand all
190 // DO NOT add any fields below |nested_messages_arena_|. The memory layout of 219 // DO NOT add any fields below |nested_messages_arena_|. The memory layout of
191 // nested messages would overflow the storage allocated by the root message. 220 // nested messages would overflow the storage allocated by the root message.
192 221
193 DISALLOW_COPY_AND_ASSIGN(ProtoZeroMessage); 222 DISALLOW_COPY_AND_ASSIGN(ProtoZeroMessage);
194 }; 223 };
195 224
196 } // namespace v2 225 } // namespace v2
197 } // namespace tracing 226 } // namespace tracing
198 227
199 #endif // COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_H_ 228 #endif // COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698