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

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

Issue 2134683002: tracing v2: Add base class for zero-copy protobuf (ProtoZero) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@proto_utils
Patch Set: alph review Created 4 years, 5 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
« no previous file with comments | « components/tracing/core/proto_utils.h ('k') | components/tracing/core/proto_zero_message.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_H_
6 #define COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_H_
7
8 #include <stdint.h>
9
10 #include "base/compiler_specific.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/logging.h"
13 #include "base/macros.h"
14 #include "base/template_util.h"
15 #include "components/tracing/core/scattered_stream_writer.h"
16 #include "components/tracing/tracing_export.h"
17
18 namespace tracing {
19 namespace v2 {
20
21 // Base class extended by the proto C++ stubs generated by the ProtoZero
22 // compiler (see //components/tracing/tools/). This class provides the minimal
23 // runtime required to support append-only operations and is desiged for
24 // performance. None of the methods require any dynamic memory allocation.
25 class TRACING_EXPORT ProtoZeroMessage {
26 public:
27 // Commits all the changes to the buffer (backfills the size field of this and
28 // all nested messages) and seals the message. Returns the size of the message
29 // (and all nested sub-messages), without taking into account any chunking.
30 // Finalize is idempotent and can be called several times w/o side effects.
31 size_t Finalize();
32
33 // Optional. If is_valid() the corresponding memory region (which length
34 // necessarily == proto::kMessageLengthFieldSize) is backfilled with the size
35 // of this message (minus |size_already_written| below) when the message is
36 // finalized. This is the mechanism used by messages to backfill their
37 // corresponding size field in the parent message.
38 ContiguousMemoryRange size_field() const { return size_field_; }
39 void set_size_field(const ContiguousMemoryRange& reserved_range) {
40 size_field_ = reserved_range;
41 }
42
43 // This is to deal with case of backfilling the size of a root (non-nested)
44 // message which is split into multiple chunks. Upon finalization only the
45 // partial size that lies in the last chunk has to be backfilled.
46 void inc_size_already_written(size_t size) { size_already_written_ += size; }
47
48 protected:
49 ProtoZeroMessage();
50
51 // Clears up the state, allowing the message to be reused as a fresh one.
52 void Reset(ScatteredStreamWriter*);
53
54 void AppendVarIntU64(uint32_t field_id, uint64_t value);
55 void AppendVarIntU32(uint32_t field_id, uint32_t value);
56 void AppendFloat(uint32_t field_id, float value);
57 void AppendDouble(uint32_t field_id, double value);
58 void AppendString(uint32_t field_id, const char* str);
59 void AppendBytes(uint32_t field_id, const void* value, size_t size);
60 // TODO(kraynov): implement AppendVarIntS32/64(...) w/ zig-zag encoding.
61
62 // Begins a nested message, using the static storage provided by the parent
63 // class (see comment in |nested_messages_arena_|). The nested message ends
64 // either when Finalize() is called or when any other Append* method is called
65 // in the parent class.
66 // The template argument T is supposed to be a stub class auto generated from
67 // a .proto, hence a subclass of ProtoZeroMessage.
68 template <class T>
69 T* BeginNestedMessage(uint32_t field_id) {
70 // This is to prevent subclasses (which should be autogenerated, though), to
71 // introduce extra state fields (they won't be initialized by Reset()).
72 static_assert(sizeof(T) == sizeof(ProtoZeroMessage),
73 "ProtoZeroMessage subclasses cannot introduce extra state.");
74 T* message = reinterpret_cast<T*>(nested_messages_arena_);
75 BeginNestedMessageInternal(field_id, message);
76 return message;
77 }
78
79 void EndNestedMessage();
80
81 private:
82 friend class ProtoZeroMessageTest;
83 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, BasicTypesNoNesting);
84 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, BackfillSizeOnFinalization);
85 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, NestedMessagesSimple);
86 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, StressTest);
87
88 enum : uint32_t { kMaxNestingDepth = 8 };
89
90 void BeginNestedMessageInternal(uint32_t field_id, ProtoZeroMessage*);
91
92 inline void WriteToStream(const uint8_t* src_begin, const uint8_t* src_end) {
93 const size_t size = static_cast<size_t>(src_end - src_begin);
94 stream_writer_->WriteBytes(src_begin, size);
95 size_ += size;
96 }
97
98 // Only PODs fields are allowed. This class' dtor is never called.
99 // See the comment on the static_assert in the the corresponding .cc file.
100
101 // The stream writer interface used for the serialization.
102 ScatteredStreamWriter* stream_writer_;
103
104 // Keeps track of the size of the current message.
105 size_t size_;
106
107 ContiguousMemoryRange size_field_;
108 size_t size_already_written_;
109
110 // Used to detect attemps to create messages with a nesting level >
111 // kMaxNestingDepth. |nesting_depth_| == 0 for root (non-nested) messages.
112 uint32_t nesting_depth_;
113
114 #if DCHECK_IS_ON()
115 // When true no more changes to the message are allowed. This is to DCHECK
116 // attempts of writing to a message which has been Finalize()-d.
117 bool sealed_;
118 #endif
119
120 // Pointer to the last child message created through BeginNestedMessage(), if
121 // any. nullptr otherwise. There is no need to keep track of more than one
122 // message per nesting level as he proto-zero API contract mandates that
123 // nested fields can be filled only in a stacked fashion. In othwe words,
124 // nested messages are finalized and sealed when any other field is set in the
125 // parent messag (or the parent message itself is finalized) and cannot be
126 // accessed anymore after that point.
127 ProtoZeroMessage* nested_message_;
128
129 // The root message owns the storage for all its nested messages, up to a max
130 // of kMaxNestingDepth levels (see the .cc file). Note that the boundaries of
131 // the arena are meaningful only for the root message. The static_assert in
132 // the .cc file guarantees that the sizeof(nested_messages_arena_) is enough
133 // to contain up to kMaxNestingDepth messages.
134 ALIGNAS(sizeof(void*)) uint8_t nested_messages_arena_[512];
kraynov 2016/07/12 10:31:03 Please explain how are you going to construct this
Primiano Tucci (use gerrit) 2016/07/12 17:25:17 It is not, that is the neat thing. This works even
135
136 // DO NOT add any fields below nested_messages_arena_. The memory layout of
137 // nested messaged would overflow the storage allocated by the root message.
138
139 DISALLOW_COPY_AND_ASSIGN(ProtoZeroMessage);
140 };
141
142 } // namespace v2
143 } // namespace tracing
144
145 #endif // COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_H_
OLDNEW
« no previous file with comments | « components/tracing/core/proto_utils.h ('k') | components/tracing/core/proto_zero_message.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698