-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool.hpp
343 lines (284 loc) · 9.8 KB
/
pool.hpp
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
Copyright (C) 2012 Brazil, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef GRNXX_IO_POOL_HPP
#define GRNXX_IO_POOL_HPP
#include "io/chunk.hpp"
#include "mutex.hpp"
#include "recycler.hpp"
namespace grnxx {
namespace io {
constexpr uint64_t POOL_MAX_FILE_SIZE = CHUNK_MAX_OFFSET;
constexpr uint16_t POOL_MAX_NUM_FILES = 1000;
constexpr uint32_t POOL_MAX_NUM_BLOCKS = BLOCK_MAX_ID + 1;
constexpr uint16_t POOL_MAX_NUM_BLOCK_CHUNKS = uint16_t(1) << 11;
constexpr uint16_t POOL_MAX_NUM_BLOCK_INFO_CHUNKS =
32 - (CHUNK_UNIT_SIZE_BITS - BLOCK_INFO_SIZE_BITS) + 1;
constexpr uint8_t POOL_MIN_BLOCK_INFO_CHUNK_SIZE_BITS =
CHUNK_UNIT_SIZE_BITS - BLOCK_INFO_SIZE_BITS;
constexpr uint64_t POOL_MIN_BLOCK_INFO_CHUNK_SIZE =
uint64_t(1) << POOL_MIN_BLOCK_INFO_CHUNK_SIZE_BITS;
// For PoolOptions.
constexpr uint64_t POOL_DEFAULT_MAX_FILE_SIZE = uint64_t(1) << 40;
constexpr uint64_t POOL_DEFAULT_MIN_BLOCK_CHUNK_SIZE = uint64_t(1) << 22;
constexpr double POOL_MAX_NEXT_BLOCK_CHUNK_SIZE_RATIO = 1.0;
constexpr double POOL_DEFAULT_NEXT_BLOCK_CHUNK_SIZE_RATIO = 1.0 / 64;
constexpr Duration POOL_MAX_FROZEN_DURATION = Duration::days(1);
constexpr Duration POOL_DEFAULT_FROZEN_DURATION = Duration::minutes(10);
constexpr uint32_t POOL_DEFAULT_UNFREEZE_COUNT_PER_OPERATION = 32;
// For PoolHeader.
constexpr uint8_t POOL_HEADER_CHUNK_SIZE_BITS = CHUNK_UNIT_SIZE_BITS;
constexpr uint64_t POOL_HEADER_CHUNK_SIZE = CHUNK_UNIT_SIZE;
constexpr char POOL_HEADER_FORMAT_STRING[64] = "grnxx::io::Pool";
constexpr char POOL_HEADER_VERSION_STRING[64] = "0.0.0";
class Pool;
typedef FlagsImpl<Pool> PoolFlags;
// If POOL_READ_ONLY is not specified, a pool is created/opened in
// read-write mode.
// Read-only mode.
constexpr PoolFlags POOL_READ_ONLY = PoolFlags::define(0x0001);
// POOL_ANONYMOUS disables all the flags other than GRNXX_HUGE_TLB and
// enables POOL_PRIVATE.
// POOL_CREATE disables POOL_READ_ONLY.
// POOL_OPEN is enabled if POOL_CREATE is not specified.
// POOL_TEMPORARY disables other flags.
// Anonymous (non-file-backed) mode.
constexpr PoolFlags POOL_ANONYMOUS = PoolFlags::define(0x0010);
// Create a pool if it does not exist.
constexpr PoolFlags POOL_CREATE = PoolFlags::define(0x0040);
// Try to use huge pages.
constexpr PoolFlags POOL_HUGE_TLB = PoolFlags::define(0x0080);
// Open an existing pool.
constexpr PoolFlags POOL_OPEN = PoolFlags::define(0x0100);
// Create a pool, if it does not exist, or open an existing pool.
constexpr PoolFlags POOL_CREATE_OR_OPEN = PoolFlags::define(0x0140);
// Create a temporary pool.
constexpr PoolFlags POOL_TEMPORARY = PoolFlags::define(0x0200);
StringBuilder &operator<<(StringBuilder &builder, PoolFlags flags);
std::ostream &operator<<(std::ostream &builder, PoolFlags flags);
class PoolOptions {
public:
PoolOptions();
void adjust();
uint64_t max_block_size() const {
return max_block_size_;
}
uint64_t min_block_chunk_size() const {
return min_block_chunk_size_;
}
uint64_t max_block_chunk_size() const {
return max_block_chunk_size_;
}
uint64_t max_file_size() const {
return max_file_size_;
}
double next_block_chunk_size_ratio() const {
return next_block_chunk_size_ratio_;
}
Duration frozen_duration() const {
return frozen_duration_;
}
uint32_t unfreeze_count_per_operation() const {
return unfreeze_count_per_operation_;
}
void set_max_block_size(uint64_t value) {
max_block_size_ = value;
}
void set_min_block_chunk_size(uint64_t value) {
min_block_chunk_size_ = value;
}
void set_max_block_chunk_size(uint64_t value) {
max_block_chunk_size_ = value;
}
void set_max_file_size(uint64_t value) {
max_file_size_ = value;
}
void set_next_block_chunk_size_ratio(double value) {
next_block_chunk_size_ratio_ = value;
}
void set_frozen_duration(Duration value) {
frozen_duration_ = value;
}
void set_unfreeze_count_per_operation(uint32_t value) {
unfreeze_count_per_operation_ = value;
}
StringBuilder &write_to(StringBuilder &builder) const;
private:
uint64_t max_block_size_;
uint64_t min_block_chunk_size_;
uint64_t max_block_chunk_size_;
uint64_t max_file_size_;
// The ratio of the next block size to the total size.
double next_block_chunk_size_ratio_;
Duration frozen_duration_;
uint32_t unfreeze_count_per_operation_;
};
inline StringBuilder &operator<<(StringBuilder &builder,
const PoolOptions &options) {
return options.write_to(builder);
}
class PoolHeader {
public:
explicit PoolHeader(const PoolOptions &options = PoolOptions());
const char *format_string() const {
return format_string_;
}
const char *version_string() const {
return format_string_;
}
const PoolOptions &options() const {
return options_;
}
uint64_t total_size() const {
return total_size_;
}
uint32_t num_blocks() const {
return num_blocks_;
};
uint32_t max_num_blocks() const {
return max_num_blocks_;
}
uint16_t next_block_chunk_id() const {
return next_block_chunk_id_;
}
uint32_t latest_phantom_block_id() const {
return latest_phantom_block_id_;
}
uint32_t latest_frozen_block_id() const {
return latest_frozen_block_id_;
}
uint32_t oldest_idle_block_ids(uint8_t list_id) const {
return oldest_idle_block_ids_[list_id];
}
const ChunkInfo &block_chunk_infos(uint16_t chunk_id) const {
return block_chunk_infos_[chunk_id];
}
const ChunkInfo &block_info_chunk_infos(uint16_t chunk_id) const {
return block_info_chunk_infos_[chunk_id];
}
void set_total_size(uint64_t value) {
total_size_ = value;
}
void set_num_blocks(uint32_t value) {
num_blocks_ = value;
};
void set_max_num_blocks(uint32_t value) {
max_num_blocks_ = value;
}
void set_next_block_chunk_id(uint16_t value) {
next_block_chunk_id_ = value;
}
void set_latest_phantom_block_id(uint32_t value) {
latest_phantom_block_id_ = value;
}
void set_latest_frozen_block_id(uint32_t value) {
latest_frozen_block_id_ = value;
}
void set_oldest_idle_block_ids(uint8_t list_id, uint32_t value) {
oldest_idle_block_ids_[list_id] = value;
}
void set_block_chunk_infos(const ChunkInfo &value) {
block_chunk_infos_[value.id()] = value;
}
void set_block_info_chunk_infos(const ChunkInfo & value) {
block_info_chunk_infos_[value.id()] = value;
}
Recycler *mutable_recycler() {
return &recycler_;
}
Mutex *mutable_inter_process_data_mutex() {
return &inter_process_data_mutex_;
}
Mutex *mutable_inter_process_file_mutex() {
return &inter_process_file_mutex_;
}
StringBuilder &write_to(StringBuilder &builder) const;
private:
char format_string_[sizeof(POOL_HEADER_FORMAT_STRING)];
char version_string_[sizeof(POOL_HEADER_VERSION_STRING)];
PoolOptions options_;
uint64_t total_size_;
uint32_t num_blocks_;
uint32_t max_num_blocks_;
uint16_t next_block_chunk_id_;
uint32_t latest_phantom_block_id_;
uint32_t latest_frozen_block_id_;
uint32_t oldest_idle_block_ids_[32];
ChunkInfo block_chunk_infos_[POOL_MAX_NUM_BLOCK_CHUNKS];
ChunkInfo block_info_chunk_infos_[POOL_MAX_NUM_BLOCK_INFO_CHUNKS];
Recycler recycler_;
Mutex inter_process_data_mutex_;
Mutex inter_process_file_mutex_;
};
static_assert(sizeof(PoolHeader) <= POOL_HEADER_CHUNK_SIZE,
"sizeof(PoolHeader) > POOL_HEADER_CHUNK_SIZE");
inline StringBuilder &operator<<(StringBuilder &builder,
const PoolHeader &header) {
return header.write_to(builder);
}
class PoolImpl;
class Pool {
public:
Pool();
Pool(PoolFlags flags, const char *path = nullptr,
const PoolOptions &options = PoolOptions());
~Pool();
Pool(const Pool &pool);
Pool &operator=(const Pool &pool);
Pool(Pool &&pool);
Pool &operator=(Pool &&pool);
explicit operator bool() const {
return static_cast<bool>(impl_);
}
bool operator==(const Pool &rhs) const {
return impl_ == rhs.impl_;
}
bool operator!=(const Pool &rhs) const {
return impl_ != rhs.impl_;
}
void open(PoolFlags flags, const char *path = nullptr,
const PoolOptions &options = PoolOptions()) {
*this = Pool(flags, path, options);
}
void close() {
*this = Pool();
}
const BlockInfo *create_block(uint64_t size);
const BlockInfo *get_block_info(uint32_t block_id);
void *get_block_address(uint32_t block_id);
void *get_block_address(const BlockInfo &block_info);
void free_block(uint32_t block_id);
void free_block(const BlockInfo &block_info);
String path() const;
PoolFlags flags() const;
const PoolOptions &options() const;
const PoolHeader &header() const;
Recycler *mutable_recycler();
void swap(Pool &rhs);
StringBuilder &write_to(StringBuilder &builder) const;
static bool exists(const char *path);
static void unlink(const char *path);
static bool unlink_if_exists(const char *path);
private:
std::shared_ptr<PoolImpl> impl_;
};
inline void swap(Pool &lhs, Pool &rhs) {
lhs.swap(rhs);
}
inline StringBuilder &operator<<(StringBuilder &builder, const Pool &pool) {
return pool.write_to(builder);
}
} // namespace io
} // namespace grnxx
#endif // GRNXX_IO_POOL_HPP