-
Notifications
You must be signed in to change notification settings - Fork 644
Expand file tree
/
Copy pathFrozenUtil.h
More file actions
304 lines (263 loc) · 9.12 KB
/
Copy pathFrozenUtil.h
File metadata and controls
304 lines (263 loc) · 9.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <stdexcept>
#include <folly/Exception.h>
#include <folly/File.h>
#include <folly/portability/GFlags.h>
#include <folly/system/MemoryMapping.h>
#include <thrift/lib/cpp2/frozen/Frozen.h>
#include <thrift/lib/cpp2/protocol/Serializer.h>
#include <thrift/lib/thrift/gen-cpp2/frozen_constants.h>
FOLLY_GFLAGS_DECLARE_bool(thrift_frozen_util_disable_mlock);
FOLLY_GFLAGS_DECLARE_bool(thrift_frozen_util_mlock_on_fault);
namespace apache::thrift::frozen {
class FrozenFileForwardIncompatible : public std::runtime_error {
public:
explicit FrozenFileForwardIncompatible(int fileVersion);
int fileVersion() const { return fileVersion_; }
int supportedVersion() const {
return schema::frozen_constants::kCurrentFrozenFileVersion();
}
private:
int fileVersion_;
};
/**
* A FreezeRoot that mallocs buffers as needed.
*/
class MallocFreezer final : public FreezeRoot {
public:
explicit MallocFreezer() {}
template <class T>
void freeze(const Layout<T>& layout, const T& root) {
doFreeze(layout, root);
}
void appendTo(std::string& out) const {
out.reserve(size_ + out.size());
for (auto& segment : segments_) {
out.append(segment.buffer, segment.buffer + segment.size);
}
}
private:
size_t distanceToEnd(const byte* origin) const;
size_t offsetOf(const byte* origin) const;
folly::MutableByteRange appendBuffer(size_t size);
void doAppendBytes(
byte* origin,
size_t n,
folly::MutableByteRange& range,
size_t& distance,
size_t alignment) override;
struct Segment {
explicit Segment(size_t size);
Segment(Segment&& other) : size(other.size), buffer(other.buffer) {
other.buffer = nullptr;
}
~Segment();
size_t size{0};
byte* buffer{nullptr}; // owned
};
std::map<const byte*, size_t> offsets_;
std::vector<Segment> segments_;
size_t size_{0};
};
/**
* Returns an upper bound estimate of the number of bytes required to freeze
* this object with a minimal layout. Actual bytes required will depend on the
* alignment of the freeze buffer.
*/
template <class T>
size_t frozenSize(const T& v) {
Layout<T> layout;
return LayoutRoot::layout(v, layout) - LayoutRoot::kPaddingBytes;
}
/**
* Returns an upper bound estimate of the number of bytes required to freeze
* this object with a given layout. Actual bytes required will depend on
* the alignment of the freeze buffer.
*/
template <class T>
size_t frozenSize(const T& v, const Layout<T>& fixedLayout) {
Layout<T> layout = fixedLayout;
size_t size;
bool changed;
LayoutRoot::layout(v, layout, changed, size);
if (changed) {
throw LayoutException();
}
return size;
}
template <class T>
void serializeRootLayout(const Layout<T>& layout, std::string& out) {
schema::MemorySchema memSchema;
schema::Schema schema;
saveRoot(layout, memSchema);
schema::convert(memSchema, schema);
*schema.fileVersion() = schema::frozen_constants::kCurrentFrozenFileVersion();
out.clear();
CompactSerializer::serialize(schema, &out);
}
template <class T>
void deserializeRootLayout(folly::ByteRange& range, Layout<T>& layoutOut) {
schema::Schema schema;
size_t schemaSize = CompactSerializer::deserialize(range, schema);
if (*schema.fileVersion() >
schema::frozen_constants::kCurrentFrozenFileVersion()) {
throw FrozenFileForwardIncompatible(*schema.fileVersion());
}
schema::MemorySchema memSchema;
schema::convert(std::move(schema), memSchema);
loadRoot(layoutOut, memSchema);
range.advance(schemaSize);
}
template <class T>
void freezeToFile(const T& x, folly::File file) {
std::string schemaStr;
auto layout = std::make_unique<Layout<T>>();
auto contentSize = LayoutRoot::layout(x, *layout);
serializeRootLayout(*layout, schemaStr);
size_t initialBufferSize = contentSize + schemaStr.size();
folly::MemoryMapping mapping(
file.dup(), 0, initialBufferSize, folly::MemoryMapping::writable());
auto mappingRange = mapping.writableRange();
auto writeRange = mapping.writableRange();
std::copy(schemaStr.begin(), schemaStr.end(), writeRange.begin());
writeRange.advance(schemaStr.size());
ByteRangeFreezer::freeze(*layout, x, writeRange);
size_t finalBufferSize = writeRange.begin() - mappingRange.begin();
folly::checkUnixError(
ftruncate(file.fd(), finalBufferSize),
"MallocFreezer: ftruncate() failed");
}
template <class T>
void freezeToString(const T& x, std::string& out) {
out.clear();
Layout<T> layout;
size_t contentSize = LayoutRoot::layout(x, layout);
serializeRootLayout(layout, out);
size_t schemaSize = out.size();
size_t bufferSize = schemaSize + contentSize;
out.resize(bufferSize, 0);
folly::MutableByteRange writeRange(
reinterpret_cast<byte*>(&out[schemaSize]), contentSize);
ByteRangeFreezer::freeze(layout, x, writeRange);
out.resize(out.size() - writeRange.size());
}
template <class T>
std::string freezeToString(const T& x) {
std::string result;
freezeToString(x, result);
return result;
}
template <class T>
std::string freezeDataToString(const T& x, const Layout<T>& layout) {
std::string out;
MallocFreezer freezer;
freezer.freeze(layout, x);
freezer.appendTo(out);
return out;
}
template <class T>
void freezeToStringMalloc(const T& x, std::string& out) {
Layout<T> layout;
LayoutRoot::layout(x, layout);
out.clear();
serializeRootLayout(layout, out);
MallocFreezer freezer;
freezer.freeze(layout, x);
freezer.appendTo(out);
}
/**
* mapFrozen<T>() returns an owned reference to a frozen object which can be
* used as a Frozen view of the object. All overloads of this function return
* MappedFrozen<T>, which is an alias for a type which bundles the view with its
* associated resources. This type may be used directly to hold references to
* mapped frozen objects.
*
* Depending on which overload is used, this bundle will hold references to
* different associated data:
*
* - mapFrozen<T>(ByteRange): Only the layout tree associated with the object.
* - mapFrozen<T>(StringPiece): Same as mapFrozen<T>(ByteRange).
* - mapFrozen<T>(MemoryMapping): Takes ownership of the memory mapping
* in addition to the layout tree.
* - mapFrozen<T>(File): Owns the memory mapping created from the File (which,
* in turn, takes ownership of the File) in addition to the layout tree.
*/
template <class T>
using MappedFrozen = Bundled<typename Layout<T>::View>;
template <class T>
MappedFrozen<T> mapFrozen(folly::ByteRange range) {
auto layout = std::make_unique<Layout<T>>();
deserializeRootLayout(range, *layout);
MappedFrozen<T> ret(layout->view({range.begin(), 0}));
ret.hold(std::move(layout));
return ret;
}
template <class T>
MappedFrozen<T> mapFrozen(folly::StringPiece range) {
return mapFrozen<T>(folly::ByteRange(range));
}
template <class T>
MappedFrozen<T> mapFrozen(folly::MemoryMapping mapping) {
auto ret = mapFrozen<T>(mapping.range());
ret.hold(std::move(mapping));
return ret;
}
/**
* Maps from the given string, taking ownership of it and bundling it with the
* return object to ensure its lifetime.
* @param trim Trims the serialized layout from the input string.
*/
template <class T>
MappedFrozen<T> mapFrozen(std::string&& str, bool trim = true) {
auto layout = std::make_unique<Layout<T>>();
auto holder = std::make_unique<HolderImpl<std::string>>(std::move(str));
auto& ownedStr = holder->t_;
folly::ByteRange rangeBefore = folly::StringPiece(ownedStr);
folly::ByteRange range = rangeBefore;
deserializeRootLayout(range, *layout);
if (trim) {
size_t trimSize = range.begin() - rangeBefore.begin();
ownedStr.erase(ownedStr.begin(), ownedStr.begin() + trimSize);
ownedStr.shrink_to_fit();
range = folly::StringPiece(ownedStr);
}
MappedFrozen<T> ret(layout->view({range.begin(), 0}));
ret.holdImpl(std::move(holder));
ret.hold(std::move(layout));
return ret;
}
template <class T>
[[deprecated(
"std::string values must be passed by move with std::move(str) or "
"passed through non-owning StringPiece")]] MappedFrozen<T>
mapFrozen(const std::string& str) = delete;
template <class T>
MappedFrozen<T> mapFrozen(
folly::File file, folly::MemoryMapping::LockMode lockMode) {
folly::MemoryMapping mapping(std::move(file), 0);
folly::MemoryMapping::LockFlags flags{};
flags.lockOnFault = FLAGS_thrift_frozen_util_mlock_on_fault;
mapping.mlock(lockMode, flags);
return mapFrozen<T>(std::move(mapping));
}
template <class T>
MappedFrozen<T> mapFrozen(folly::File file) {
return mapFrozen<T>(
std::move(file), folly::MemoryMapping::LockMode::TRY_LOCK);
}
} // namespace apache::thrift::frozen