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

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: petrcermak 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
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 "build/build_config.h"
16 #include "components/tracing/core/scattered_stream_writer.h"
17 #include "components/tracing/tracing_export.h"
18
19 namespace tracing {
20 namespace v2 {
21
22 // Base class extended by the proto C++ stubs generated by the ProtoZero
23 // compiler (see //components/tracing/tools/). This class provides the minimal
24 // runtime required to support append-only operations and is desiged for
25 // performance. None of the methods require any dynamic memory allocation.
26 class TRACING_EXPORT ProtoZeroMessage {
27 public:
28 // Commits all the changes to the buffer (backfills the size field of this and
29 // all nested messages) and seals the message. Returns the size of the message
30 // (and all nested sub-messages), without taking into account any chunking.
31 // Finalize is idempotent and can be called several times w/o side effects.
32 size_t Finalize();
33
34 // Optional. If is_valid() == true, the corresponding memory region (its
35 // length == proto::kMessageLengthFieldSize) is backfilled with the size of
36 // this message (minus |size_already_written| below) when the message is
37 // finalized. This is the mechanism used by messages to backfill their
38 // corresponding size field in the parent message.
39 ContiguousMemoryRange size_field() const { return size_field_; }
40 void set_size_field(const ContiguousMemoryRange& reserved_range) {
41 size_field_ = reserved_range;
42 }
43
44 // This is to deal with case of backfilling the size of a root (non-nested)
45 // message which is split into multiple chunks. Upon finalization only the
46 // partial size that lies in the last chunk has to be backfilled.
47 void inc_size_already_written(size_t size) { size_already_written_ += size; }
48
49 protected:
50 ProtoZeroMessage();
51
52 // Clears up the state, allowing the message to be reused as a fresh one.
53 void Reset(ScatteredStreamWriter*);
54
55 void AppendVarIntU64(uint32_t field_id, uint64_t value);
56 void AppendVarIntU32(uint32_t field_id, uint32_t value);
57 void AppendFloat(uint32_t field_id, float value);
58 void AppendDouble(uint32_t field_id, double value);
59 void AppendString(uint32_t field_id, const char* str);
60 void AppendBytes(uint32_t field_id, const void* value, size_t size);
61 // TODO(kraynov): implement AppendVarIntS32/64(...) w/ zig-zag encoding.
62
63 // Begins a nested message, using the static storage provided by the parent
64 // class (see comment in |nested_messages_arena_|). The nested message ends
65 // either when Finalize() is called or when any other Append* method is called
66 // in the parent class.
67 // The template argument T is supposed to be a stub class auto generated from
68 // a .proto, hence a subclass of ProtoZeroMessage.
69 template <class T>
70 T* BeginNestedMessage(uint32_t field_id) {
71 // This is to prevent subclasses (which should be autogenerated, though), to
72 // introduce extra state fields (they won't be initialized by Reset()).
petrcermak 2016/07/12 17:41:17 nit: s/they won't/which wouldn't/ would sound bett
Primiano Tucci (use gerrit) 2016/07/13 10:25:07 Done.
73 static_assert(sizeof(T) == sizeof(ProtoZeroMessage),
oystein (OOO til 10th of July) 2016/07/12 21:03:29 Maybe an std::is_base_of assert too.
Primiano Tucci (use gerrit) 2016/07/13 10:25:07 TIL std::is_base_of. Excellent, done.
74 "ProtoZeroMessage subclasses cannot introduce extra state.");
75 T* message = reinterpret_cast<T*>(nested_messages_arena_);
76 BeginNestedMessageInternal(field_id, message);
77 return message;
78 }
79
80 private:
81 friend class ProtoZeroMessageTest;
82 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, BasicTypesNoNesting);
83 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, BackfillSizeOnFinalization);
84 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, NestedMessagesSimple);
85 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, StressTest);
86
87 enum : uint32_t { kMaxNestingDepth = 8 };
88
89 void BeginNestedMessageInternal(uint32_t field_id, ProtoZeroMessage*);
90
91 // Called by Finalize and Append* methods.
92 void EndNestedMessage();
93
94 inline void WriteToStream(const uint8_t* src_begin, const uint8_t* src_end) {
oystein (OOO til 10th of July) 2016/07/12 21:03:29 nit: isn't 'inline' redundant here?
Primiano Tucci (use gerrit) 2016/07/13 10:25:08 This is something I never really remember.I think
95 #if DCHECK_IS_ON()
96 DCHECK(!sealed_);
97 #endif
98 const size_t size = static_cast<size_t>(src_end - src_begin);
oystein (OOO til 10th of July) 2016/07/12 21:03:29 DCHECK(src_begin < src_end) maybe?
Primiano Tucci (use gerrit) 2016/07/13 10:25:08 Done.
99 stream_writer_->WriteBytes(src_begin, size);
100 size_ += size;
101 }
102
103 // Only POD fields are allowed. This class's dtor is never called.
104 // See the comment on the static_assert in the the corresponding .cc file.
105
106 // The stream writer interface used for the serialization.
107 ScatteredStreamWriter* stream_writer_;
108
109 // Keeps track of the size of the current message.
110 size_t size_;
111
112 ContiguousMemoryRange size_field_;
113 size_t size_already_written_;
114
115 // Used to detect attemps to create messages with a nesting level >
116 // kMaxNestingDepth. |nesting_depth_| == 0 for root (non-nested) messages.
117 uint32_t nesting_depth_;
118
119 #if DCHECK_IS_ON()
120 // When true, no more changes to the message are allowed. This is to DCHECK
121 // attempts of writing to a message which has been Finalize()-d.
122 bool sealed_;
123 #endif
124
125 // Pointer to the last child message created through BeginNestedMessage(), if
126 // any. nullptr otherwise. There is no need to keep track of more than one
127 // message per nesting level as the proto-zero API contract mandates that
128 // nested fields can be filled only in a stacked fashion. In other words,
129 // nested messages are finalized and sealed when any other field is set in the
130 // parent message (or the parent message itself is finalized) and cannot be
131 // accessed anymore afterwards.
132 ProtoZeroMessage* nested_message_;
133
134 // The root message owns the storage for all its nested messages, up to a max
135 // of kMaxNestingDepth levels (see the .cc file). Note that the boundaries of
136 // the arena are meaningful only for the root message. The static_assert in
137 // the .cc file guarantees that the sizeof(nested_messages_arena_) is enough
138 // to contain up to kMaxNestingDepth messages.
139 ALIGNAS(sizeof(void*)) uint8_t nested_messages_arena_[512];
140
141 // DO NOT add any fields below |nested_messages_arena_|. The memory layout of
142 // nested messages would overflow the storage allocated by the root message.
143
144 DISALLOW_COPY_AND_ASSIGN(ProtoZeroMessage);
145 };
146
147 } // namespace v2
148 } // namespace tracing
149
150 #endif // COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698