forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_segmented_compress_params.py
executable file
·203 lines (143 loc) · 5.97 KB
/
gen_segmented_compress_params.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (C) 2017-present ScyllaDB
#
#
# SPDX-License-Identifier: AGPL-3.0-or-later
#
'''
Generates compression segmentation parameters.
These parameters are used to reduce the memory footprint of the
in-memory compression database. See sstables/compress.hh for more
details.
'''
import argparse
import math
def data_size_range_log2():
return range(4, 51)
def chunk_size_range_log2():
return range(4, 31)
def base_offset_size(data_size, chunk_size, n):
return int(math.ceil(math.log2(data_size)))
def relative_offset_size(data_size, chunk_size, n):
if n == 1:
return int(0)
else:
return int(math.ceil(math.log2((n - 1) * (chunk_size + 64))))
def segment_size(data_size, chunk_size, n):
return base_offset_size(data_size, chunk_size, n) + (n - 1) * relative_offset_size(data_size, chunk_size, n)
def no_of_segments(data_size, chunk_size, n):
return int(math.ceil((data_size / chunk_size) / n))
def n_for(data_size, chunk_size, n_values):
nominal_data_size = int(math.ceil(math.log2(data_size)))
chunk_size_log2 = int(math.ceil(math.log2(chunk_size)))
return next(filter(lambda x: x[0] == nominal_data_size and x[1] == chunk_size_log2, n_values))[2]
def size_deque(data_size, chunk_size):
return int(math.ceil(data_size / chunk_size)) * 64
def size_grouped_segments(data_size, chunk_size, n):
return no_of_segments(data_size, chunk_size, n) * segment_size(data_size, chunk_size, n)
def best_nominal_data_size_for_bucket_size(chunk_size, bucket_size, n_values):
def addressable_space(data_size_log2):
data_size = 2**data_size_log2
n = n_for(data_size, chunk_size, n_values)
bucket_size_bits = bucket_size * 8
total_size_bits = size_grouped_segments(data_size, chunk_size, n)
if bucket_size_bits >= total_size_bits:
return data_size, data_size_log2
else:
segments_pb = segments_per_bucket(data_size, chunk_size, n, bucket_size)
return n * segments_pb * chunk_size, data_size_log2
space = map(addressable_space, data_size_range_log2())
return max(space, key=lambda x: x[0])[1]
def segments_per_bucket(data_size, chunk_size, n, bucket_size):
# A safety padding of 7 bytes has to be reserved at the end
bucket_size_bits = bucket_size * 8 - 56
segment_size_bits = segment_size(data_size, chunk_size, n)
fits = int(math.floor(bucket_size_bits / segment_size_bits))
# We can't have more segments than the sizes support
return min(no_of_segments(data_size, chunk_size, n), fits)
def all_n_values():
optimal_sizes = {}
for f in data_size_range_log2():
for c in chunk_size_range_log2():
optimal_size = None
for n in range(1, 201):
s = size_grouped_segments(2**f, 2**c, n)
if optimal_size is None or optimal_size[3] > s:
optimal_size = (f, c, n, s)
optimal_sizes[(f, c)] = optimal_size
n_values = []
for k in sorted(optimal_sizes.keys()):
f, c, n, s = optimal_sizes[k]
n_values.append((f, c, n))
return n_values
file_str = """
/*
* Copyright (C) 2017-present ScyllaDB
*/
// SPDX-License-Identifier: AGPL-3.0-or-later
/*
* This file was autogenerated by gen_segmented_compress_params.py.
*/
#include "compress.hh"
#include <array>
namespace sstables {{
const uint64_t bucket_size{{{bucket_size}}};
struct bucket_info {{
uint64_t chunk_size_log2;
uint64_t best_data_size_log2;
uint64_t segments_per_bucket;
}};
// The largest data chunk from the file a bucketful of offsets can
// cover, precalculated for different chunk sizes, plus the number
// of segments that are needed to address the whole area.
const std::array<bucket_info, {bucket_infos_size}> bucket_infos{{{{
{bucket_infos}}}}};
struct segment_info {{
uint8_t data_size_log2;
uint8_t chunk_size_log2;
uint8_t grouped_offsets;
}};
// Precomputed optimal segment information for different data and chunk sizes.
const std::array<segment_info, {segment_infos_size}> segment_infos{{{{
{segment_infos}}}}};
}} // namespace sstables
"""
if __name__ == '__main__':
cmdline_parser = argparse.ArgumentParser()
cmdline_parser.add_argument('--bucket-size-log2', action='store', help='specify bucket size (defaults to 4K)')
args = cmdline_parser.parse_args()
if args.bucket_size_log2 is not None:
bucket_size_log2 = int(args.bucket_size_log2)
if bucket_size_log2 < 10 or bucket_size_log2 > 30:
print("Bucket size is either too large or too small")
exit(1)
else:
bucket_size_log2 = 12 # 4K
bucket_size = 2**bucket_size_log2
n_values = all_n_values()
with open("sstables/segmented_compress_params.hh", "w") as infos_file:
bucket_infos = []
data_sizes = []
for chunk_size_log2 in chunk_size_range_log2():
chunk_size = 2**chunk_size_log2
data_size_log2 = best_nominal_data_size_for_bucket_size(chunk_size, bucket_size, n_values)
data_size = 2**data_size_log2
n = n_for(data_size, chunk_size, n_values)
bucket_infos.append(" {{{}, {}, {} /*out of the max of {}*/}}".format(
chunk_size_log2,
data_size_log2,
segments_per_bucket(data_size, chunk_size, n, bucket_size), # no of segments that fit into the bucket
no_of_segments(data_size, chunk_size, n))) # normal no of segments for these sizes
data_sizes.append(data_size_log2)
segment_infos = []
for n_value in n_values:
if n_value[0] in data_sizes:
segment_infos.append(" {{{}, {}, {}}}".format(*n_value))
infos_file.write(file_str.format(
bucket_size=bucket_size,
bucket_infos=",\n".join(bucket_infos),
bucket_infos_size=len(bucket_infos),
segment_infos=",\n".join(segment_infos),
segment_infos_size=len(segment_infos)))