forked from intel/iaa-plugin-rocksdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiaa_compressor.cc
314 lines (262 loc) · 9.53 KB
/
iaa_compressor.cc
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
// Copyright (C) 2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#include "iaa_compressor.h"
#include <atomic>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <list>
#include <memory>
#include <string>
#include <vector>
#include "logging/logging.h"
#include "qpl/qpl.h"
#include "rocksdb/compressor.h"
#include "rocksdb/configurable.h"
#include "rocksdb/env.h"
#include "rocksdb/utilities/options_type.h"
#include "util/coding.h"
namespace ROCKSDB_NAMESPACE {
// Error messages
#define MEMORY_ALLOCATION_ERROR "memory allocation error"
#define JOB_INIT_ERROR "job init error"
#define QPL_STATUS(status) "QPL status " + std::to_string(status)
extern "C" FactoryFunc<Compressor> iaa_compressor_reg;
FactoryFunc<Compressor> iaa_compressor_reg =
ObjectLibrary::Default()->AddFactory<Compressor>(
"com.intel.iaa_compressor_rocksdb",
[](const std::string& /* uri */,
std::unique_ptr<Compressor>* compressor, std::string* /* errmsg */) {
*compressor = NewIAACompressor();
return compressor->get();
});
std::unordered_map<std::string, qpl_path_t> execution_paths{
{"auto", qpl_path_auto},
{"hw", qpl_path_hardware},
{"sw", qpl_path_software}};
enum qpl_compression_mode { dynamic_mode, fixed_mode };
std::unordered_map<std::string, qpl_compression_mode> compression_modes{
{"dynamic", dynamic_mode}, {"fixed", fixed_mode}};
struct IAACompressorOptions {
static const char* kName() { return "IAACompressorOptions"; };
qpl_path_t execution_path = qpl_path_auto;
qpl_compression_mode compression_mode = dynamic_mode;
bool verify = false;
int level = 0;
uint32_t parallel_threads = 1;
};
static std::unordered_map<std::string, OptionTypeInfo>
iaa_compressor_type_info = {
{"execution_path",
OptionTypeInfo::Enum(
offsetof(struct IAACompressorOptions, execution_path),
&execution_paths)},
{"compression_mode",
OptionTypeInfo::Enum(
offsetof(struct IAACompressorOptions, compression_mode),
&compression_modes)},
{"verify",
{offsetof(struct IAACompressorOptions, verify), OptionType::kBoolean,
OptionVerificationType::kNormal, OptionTypeFlags::kNone}},
{"level",
{offsetof(struct IAACompressorOptions, level), OptionType::kInt,
OptionVerificationType::kNormal, OptionTypeFlags::kNone}},
{"parallel_threads",
{offsetof(struct IAACompressorOptions, parallel_threads),
OptionType::kUInt32T, OptionVerificationType::kNormal,
OptionTypeFlags::kNone}}};
class IAAJob {
public:
IAAJob() : jobs_(3, nullptr) {
InitJob(qpl_path_hardware);
InitJob(qpl_path_software);
InitJob(qpl_path_auto);
}
~IAAJob() {
for (qpl_job* job : jobs_) {
if (job != nullptr) {
qpl_fini_job(job);
delete[] job;
}
}
}
qpl_job* GetJob(qpl_path_t execution_path) { return jobs_[execution_path]; }
private:
void InitJob(qpl_path_t execution_path) {
uint32_t size;
qpl_status status = qpl_get_job_size(execution_path, &size);
if (status != QPL_STS_OK) {
jobs_[execution_path] = nullptr;
return;
}
try {
jobs_[execution_path] = reinterpret_cast<qpl_job*>(new char[size]);
} catch (std::bad_alloc& e) {
jobs_[execution_path] = nullptr;
return;
}
status = qpl_init_job(execution_path, jobs_[execution_path]);
if (status != QPL_STS_OK) {
jobs_[execution_path] = nullptr;
}
}
std::vector<qpl_job*> jobs_;
};
class IAACompressor : public Compressor {
public:
IAACompressor() {
RegisterOptions(&options_, &iaa_compressor_type_info);
#ifndef NDEBUG
Status s =
Env::Default()->NewLogger("/tmp/iaa_compressor_log.txt", &logger_);
if (s.ok()) {
logger_->SetInfoLogLevel(DEBUG_LEVEL);
}
#endif
};
static const char* kClassName() { return "com.intel.iaa_compressor_rocksdb"; }
const char* Name() const override { return kClassName(); }
bool DictCompressionSupported() const override { return false; }
uint32_t GetParallelThreads() const override {
return options_.parallel_threads;
};
Status Compress(const CompressionInfo& /* info */, const Slice& input,
std::string* output) override {
// Max size of a RocksDB block is 4GiB
uint32_t output_header_length = EncodeSize(input.size(), output);
// If data is incompressible, QPL returns stored blocks
// A stored block is at most 2^16-1 bytes in size and it has a 5-byte header
// So, in the worst case, data grows by 5*ceil(input.size()/65535)
size_t input_length = input.size();
size_t output_length =
output_header_length + input_length +
(input_length / 65535 + (input_length % 65535 != 0)) * 5;
if (output_length > std::numeric_limits<uint32_t>::max()) {
// Attempt compression with largest possible buffer. QPL will return an
// error if not sufficient.
output_length = std::numeric_limits<uint32_t>::max();
}
output->resize(output_length);
qpl_status status;
qpl_compression_levels level = GetQplLevel(options_.level);
qpl_path_t execution_path = options_.execution_path;
if (level == qpl_high_level && execution_path == qpl_path_hardware) {
execution_path = qpl_path_software;
}
qpl_job* job = job_.GetJob(execution_path);
if (job == nullptr) {
return Status::Corruption(JOB_INIT_ERROR);
}
uint8_t* source =
const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(input.data()));
uint8_t* destination =
reinterpret_cast<uint8_t*>(&(*output)[0] + output_header_length);
job->next_in_ptr = source;
job->available_in = input.size();
job->next_out_ptr = destination;
job->available_out = output_length - output_header_length;
job->level = level;
job->op = qpl_op_compress;
job->flags = QPL_FLAG_FIRST | QPL_FLAG_LAST;
if (!options_.verify) {
job->flags |= QPL_FLAG_OMIT_VERIFY;
}
job->huffman_table = nullptr;
job->dictionary = nullptr;
if (options_.compression_mode == dynamic_mode) {
job->flags |= QPL_FLAG_DYNAMIC_HUFFMAN;
}
status = QPL_STS_QUEUES_ARE_BUSY_ERR;
while (status == QPL_STS_QUEUES_ARE_BUSY_ERR) {
status = qpl_execute_job(job);
}
if (status != QPL_STS_OK) {
return Status::Corruption(QPL_STATUS(status));
}
output->resize(output_header_length + job->total_out);
Debug(logger_, "Compress - input size: %lu - output size: %u\n",
input.size(), job->total_out);
return Status::OK();
}
Status Uncompress(const UncompressionInfo& info, const char* input,
size_t input_length, char** output,
size_t* output_length) override {
// Extract uncompressed size
uint32_t encoded_output_length = 0;
if (!DecodeSize(&input, &input_length, &encoded_output_length)) {
return Status::Corruption("size decoding error");
}
// Memory allocator may return null pointer or throw bad_alloc exception
try {
*output = Allocate(encoded_output_length, info.GetMemoryAllocator());
if (*output == nullptr) {
return Status::Corruption(MEMORY_ALLOCATION_ERROR);
}
} catch (std::bad_alloc& e) {
return Status::Corruption(MEMORY_ALLOCATION_ERROR);
}
qpl_status status;
qpl_job* job = job_.GetJob(options_.execution_path);
if (job == nullptr) {
return Status::Corruption(JOB_INIT_ERROR);
}
uint8_t* source =
const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(input));
uint8_t* destination = reinterpret_cast<uint8_t*>(*output);
job->next_in_ptr = source;
job->available_in = input_length;
job->next_out_ptr = destination;
job->available_out = encoded_output_length;
job->op = qpl_op_decompress;
job->huffman_table = nullptr;
job->flags = QPL_FLAG_FIRST | QPL_FLAG_LAST;
status = QPL_STS_QUEUES_ARE_BUSY_ERR;
while (status == QPL_STS_QUEUES_ARE_BUSY_ERR) {
status = qpl_execute_job(job);
}
if (status != QPL_STS_OK) {
return Status::Corruption(QPL_STATUS(status));
} else if (job->total_out != encoded_output_length) {
return Status::Corruption("size mismatch");
}
*output_length = job->total_out;
Debug(logger_, "Uncompress - input size: %lu - output size: %u\n",
input_length, job->total_out);
return Status::OK();
}
bool IsDictEnabled() const override { return false; }
private:
IAACompressorOptions options_;
static thread_local IAAJob job_;
std::shared_ptr<Logger> logger_;
uint32_t EncodeSize(size_t length, std::string* output) {
PutVarint32(output, length);
return output->size();
}
bool DecodeSize(const char** input, size_t* input_length,
uint32_t* output_length) {
auto new_input =
GetVarint32Ptr(*input, *input + *input_length, output_length);
if (new_input == nullptr) {
return false;
}
*input_length -= (new_input - *input);
*input = new_input;
return true;
}
int GetLevel() const override { return options_.level; }
qpl_compression_levels GetQplLevel(int level) {
if (level == 0 || level == CompressionOptions::kDefaultCompressionLevel) {
return qpl_default_level;
} else {
return qpl_high_level;
}
}
};
// Reuse job structs across calls. Have one struct per thread and execution path
// (hw, sw, auto).
thread_local IAAJob IAACompressor::job_;
std::unique_ptr<Compressor> NewIAACompressor() {
return std::unique_ptr<Compressor>(new IAACompressor());
}
} // namespace ROCKSDB_NAMESPACE