/
output.cpp
331 lines (288 loc) · 9.19 KB
/
output.cpp
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
#include "output.h"
#include "buffer_pool.h"
#include "compress.h"
#include "cso.h"
namespace maxcso {
// TODO: Tune, less may be better.
static const size_t QUEUE_SIZE = 32;
Output::Output(uv_loop_t *loop, const Task &task)
: loop_(loop), flags_(task.flags), state_(STATE_INIT), fmt_(CSO_FMT_CSO1), srcSize_(-1), index_(nullptr) {
for (size_t i = 0; i < QUEUE_SIZE; ++i) {
freeSectors_.push_back(new Sector(flags_, task.orig_max_cost, task.lz4_max_cost));
}
}
Output::~Output() {
for (Sector *sector : freeSectors_) {
delete sector;
}
for (auto pair : pendingSectors_) {
delete pair.second;
}
for (auto pair : partialSectors_) {
delete pair.second;
}
freeSectors_.clear();
pendingSectors_.clear();
partialSectors_.clear();
delete [] index_;
index_ = nullptr;
}
void Output::SetFile(uv_file file, int64_t srcSize, uint32_t blockSize, CSOFormat fmt) {
file_ = file;
srcSize_ = srcSize;
srcPos_ = 0;
fmt_ = fmt;
blockSize_ = blockSize;
for (blockShift_ = 0; blockSize > 1; blockSize >>= 1) {
++blockShift_;
}
const uint32_t sectors = static_cast<uint32_t>(srcSize >> blockShift_);
// Start after the header and index, which we'll fill in later.
index_ = new uint32_t[sectors + 1];
// Start after the end of the index data and header.
dstPos_ = sizeof(CSOHeader) + (sectors + 1) * sizeof(uint32_t);
// TODO: We might be able to optimize shift better by running through the data.
// That would require either a second pass or keeping the entire result in RAM.
// For now, just take worst case (all blocks stored uncompressed.)
int64_t worstSize = dstPos_ + srcSize;
indexShift_ = 0;
for (int i = 62; i >= 31; --i) {
int64_t max = 1LL << i;
if (worstSize >= max) {
// This means we need i + 1 bits to store the position.
// We have to shift enough off to fit into 31.
indexShift_ = i + 1 - 31;
break;
}
}
// If the shift is above 11, the padding could make it need more space.
// But that would be > 4 TB anyway, so let's not worry about it.
indexAlign_ = 1 << indexShift_;
Align(dstPos_);
state_ |= STATE_HAS_FILE;
for (Sector *sector : freeSectors_) {
sector->Setup(loop_, blockSize_, indexAlign_);
}
}
int32_t Output::Align(int64_t &pos) {
uint32_t off = static_cast<uint32_t>(pos % indexAlign_);
if (off != 0) {
pos += indexAlign_ - off;
return indexAlign_ - off;
}
return 0;
}
void Output::Enqueue(int64_t pos, uint8_t *buffer) {
// We might not compress all blocks.
const bool tryCompress = ShouldCompress(pos, buffer);
const uint32_t block = static_cast<uint32_t>(pos >> blockShift_);
Sector *sector;
if (blockSize_ != SECTOR_SIZE) {
// Guaranteed to be zero-initialized on insert.
sector = partialSectors_[block];
if (sector == nullptr) {
sector = freeSectors_.back();
freeSectors_.pop_back();
partialSectors_[block] = sector;
}
} else {
sector = freeSectors_.back();
freeSectors_.pop_back();
}
if (!tryCompress) {
sector->DisableCompress();
}
sector->Process(pos, buffer, [this, sector, block](bool status, const char *reason) {
if (blockSize_ != SECTOR_SIZE) {
partialSectors_.erase(block);
}
HandleReadySector(sector);
});
// Only check for the last block of a larger block size.
if (blockSize_ != SECTOR_SIZE && pos + SECTOR_SIZE >= srcSize_) {
// Our src may not be aligned to the blockSize_, so this sector might never wake up.
// So let's send in some padding if needed.
const int64_t paddedSize = (srcSize_ + blockSize_ - 1) & ~static_cast<int64_t>(blockSize_ - 1);
for (int64_t padPos = srcSize_; padPos < paddedSize; padPos += SECTOR_SIZE) {
// Sector takes ownership, so we need a new one each time.
uint8_t *padBuffer = pool.Alloc();
memset(padBuffer, 0, SECTOR_SIZE);
sector->Process(padPos, padBuffer, [this, sector, block](bool status, const char *reason) {
partialSectors_.erase(block);
HandleReadySector(sector);
});
}
}
}
void Output::HandleReadySector(Sector *sector) {
if (sector != nullptr) {
if (srcPos_ != sector->Pos()) {
// We're not there yet in the file stream. Queue this, get to it later.
pendingSectors_[sector->Pos()] = sector;
return;
}
} else {
// If no sector was provided, we're looking at the first in the queue.
if (pendingSectors_.empty()) {
return;
}
sector = pendingSectors_.begin()->second;
if (srcPos_ != sector->Pos()) {
return;
}
// Remove it from the queue, and then run with it.
pendingSectors_.erase(pendingSectors_.begin());
}
// Check for any sectors that immediately follow the one we're writing.
// We'll just write them all together.
std::vector<Sector *> sectors;
sectors.push_back(sector);
// TODO: Try other numbers.
static const size_t MAX_BUFS = 8;
int64_t nextPos = srcPos_ + blockSize_;
auto it = pendingSectors_.find(nextPos);
while (it != pendingSectors_.end()) {
sectors.push_back(it->second);
pendingSectors_.erase(it);
nextPos += blockSize_;
it = pendingSectors_.find(nextPos);
// Don't do more than 4 at a time.
if (sectors.size() >= MAX_BUFS) {
break;
}
}
int64_t dstPos = dstPos_;
uv_buf_t bufs[MAX_BUFS * 2];
unsigned int nbufs = 0;
static char padding[2048] = {0};
for (size_t i = 0; i < sectors.size(); ++i) {
unsigned int bestSize = sectors[i]->BestSize();
bufs[nbufs++] = uv_buf_init(reinterpret_cast<char *>(sectors[i]->BestBuffer()), bestSize);
// Update the index.
const int32_t s = static_cast<int32_t>(sectors[i]->Pos() >> blockShift_);
index_[s] = static_cast<int32_t>(dstPos >> indexShift_);
// CSO2 doesn't use a flag for uncompressed, only the size of the block.
if (!sectors[i]->Compressed() && fmt_ != CSO_FMT_CSO2) {
index_[s] |= CSO_INDEX_UNCOMPRESSED;
}
switch (fmt_) {
case CSO_FMT_CSO1:
if (sectors[i]->Format() == SECTOR_FMT_LZ4) {
finish_(false, "LZ4 format not supported within CSO v1 file");
return;
}
break;
case CSO_FMT_ZSO:
if (sectors[i]->Format() == SECTOR_FMT_DEFLATE) {
finish_(false, "Deflate format not supported within ZSO file");
return;
}
break;
case CSO_FMT_CSO2:
if (sectors[i]->Format() == SECTOR_FMT_LZ4) {
index_[s] |= CSO2_INDEX_LZ4;
}
break;
}
dstPos += bestSize;
int32_t padSize = Align(dstPos);
if (padSize != 0) {
// We need uv to write the padding out as well.
bufs[nbufs++] = uv_buf_init(padding, padSize);
}
}
// If we're working on the last sectors, then the index is ready to write.
if (nextPos >= srcSize_) {
// Update the final index entry.
const int32_t s = static_cast<int32_t>(srcSize_ >> blockShift_);
index_[s] = static_cast<int32_t>(dstPos >> indexShift_);
state_ |= STATE_INDEX_READY;
Flush();
}
const int64_t totalWrite = dstPos - dstPos_;
uv_.fs_write(loop_, sector->WriteReq(), file_, bufs, nbufs, dstPos_, [this, sectors, nextPos, totalWrite](uv_fs_t *req) {
for (Sector *sector : sectors) {
sector->Release();
freeSectors_.push_back(sector);
}
if (req->result != totalWrite) {
finish_(false, "Data could not be written to output file");
uv_fs_req_cleanup(req);
return;
}
uv_fs_req_cleanup(req);
srcPos_ = nextPos;
dstPos_ += totalWrite;
progress_(srcPos_, srcSize_, dstPos_);
if (nextPos >= srcSize_) {
state_ |= STATE_DATA_WRITTEN;
CheckFinish();
} else {
// Check if there's more data to write out.
HandleReadySector(nullptr);
}
});
}
bool Output::ShouldCompress(int64_t pos, uint8_t *buffer) {
if (flags_ & TASKFLAG_FORCE_ALL) {
return true;
}
if (pos == 16 * SECTOR_SIZE) {
// This is the volume descriptor.
// TODO: Could read it in and map all the directory structures.
// Would just need to keep a list, assuming they are sequential, we'd get most of them.
// TODO: This doesn't really seem to help anyone. Rethink.
//return false;
}
// TODO
return true;
}
bool Output::QueueFull() {
return freeSectors_.empty();
}
void Output::OnProgress(OutputCallback callback) {
progress_ = callback;
}
void Output::OnFinish(OutputFinishCallback callback) {
finish_ = callback;
}
void Output::Flush() {
if (!(state_ & STATE_INDEX_READY)) {
finish_(false, "Flush called before index finalized");
return;
}
CSOHeader *header = new CSOHeader;
if (fmt_ == CSO_FMT_ZSO) {
memcpy(header->magic, ZSO_MAGIC, sizeof(header->magic));
} else {
memcpy(header->magic, CSO_MAGIC, sizeof(header->magic));
}
header->header_size = sizeof(CSOHeader);
header->uncompressed_size = srcSize_;
header->sector_size = blockSize_;
header->version = fmt_ == CSO_FMT_CSO2 ? 2 : 1;
header->index_shift = indexShift_;
header->unused[0] = 0;
header->unused[1] = 0;
const uint32_t sectors = static_cast<uint32_t>(srcSize_ >> blockShift_);
uv_buf_t bufs[2];
bufs[0] = uv_buf_init(reinterpret_cast<char *>(header), sizeof(CSOHeader));
bufs[1] = uv_buf_init(reinterpret_cast<char *>(index_), (sectors + 1) * sizeof(uint32_t));
const size_t totalBytes = sizeof(CSOHeader) + (sectors + 1) * sizeof(uint32_t);
uv_.fs_write(loop_, &flush_, file_, bufs, 2, 0, [this, header, totalBytes](uv_fs_t *req) {
if (req->result != totalBytes) {
finish_(false, "Unable to write header data");
} else {
state_ |= STATE_INDEX_WRITTEN;
CheckFinish();
}
uv_fs_req_cleanup(req);
delete header;
});
}
void Output::CheckFinish() {
if ((state_ & STATE_INDEX_WRITTEN) && (state_ & STATE_DATA_WRITTEN)) {
finish_(true, nullptr);
}
}
};