-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathSubTimeFrameFileReader.cxx
More file actions
367 lines (297 loc) · 11.3 KB
/
SubTimeFrameFileReader.cxx
File metadata and controls
367 lines (297 loc) · 11.3 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
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// Copyright 2019-2022 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
/// \author Gvozden Nešković, Frankfurt Institute for Advanced Studies and Goethe University Frankfurt
#include "SubTimeFrameFile.h"
#include "SubTimeFrameFileReader.h"
#include "SubTimeFrameBuilder.h"
#include "DataDistLogger.h"
#if __linux__
#include <sys/mman.h>
#endif
namespace o2::DataDistribution
{
using namespace o2::header;
////////////////////////////////////////////////////////////////////////////////
/// SubTimeFrameFileReader
////////////////////////////////////////////////////////////////////////////////
SubTimeFrameFileReader::SubTimeFrameFileReader(boost::filesystem::path& pFileName)
{
mFileName = pFileName.string();
mFileMap.open(mFileName);
if (! mFileMap.is_open()) {
EDDLOG("Failed to open TF file for reading (mmap).");
return;
}
mFileSize = mFileMap.size();
mFileMapOffset = 0;
#if __linux__
madvise((void*)mFileMap.data(), mFileMap.size(), MADV_HUGEPAGE | MADV_SEQUENTIAL | MADV_DONTDUMP);
#endif
}
SubTimeFrameFileReader::~SubTimeFrameFileReader()
{
if (! mFileMap.is_open()) {
#if __linux__
madvise((void*)mFileMap.data(), mFileMap.size(), MADV_DONTNEED);
#endif
mFileMap.close();
}
}
void SubTimeFrameFileReader::visit(SubTimeFrame& pStf, void*)
{
DataIdentifier lDataId;
DataHeader::SubSpecificationType lSubSpec;
std::vector<fair::mq::MessagePtr> *lVec = nullptr;
for (auto& lStfDataPair : mStfData) {
if (lStfDataPair.mHeader) {
const auto *lDh = lStfDataPair.getDataHeader();
lDataId = impl::getDataIdentifier(*lDh);
lSubSpec = lDh->subSpecification;
lVec = pStf.addStfDataStart(lDataId, lSubSpec, {std::move(lStfDataPair.mHeader), std::move(lStfDataPair.mData)});
} else {
assert (lVec);
pStf.addStfDataAppend(lVec, std::move(lStfDataPair.mData));
}
}
}
std::size_t SubTimeFrameFileReader::getHeaderStackSize() // throws ios_base::failure
{
// Expect valid Stack in the file.
// First Header must be DataHeader. The size is unknown since there are multiple versions.
// Each header in the stack extends BaseHeader
// Read first the base header then the rest of the extended header. Keep going until the next flag is set.
// reset the file pointer to the original incoming position, so the complete Stack can be read in
bool readNextHeader = true;
std::size_t lStackSize = 0;
DataHeader lBaseHdr; // Use DataHeader since the BaseHeader has no default contructor.
const auto lFilePosStart = position();
const auto cMaxHeaders = 16; /* make sure we don't loop forever */
auto lNumHeaders = 0;
while (readNextHeader && (++lNumHeaders <= cMaxHeaders)) {
// read BaseHeader only!
const auto lBaseHdrPos = position();
if(!read_advance(&lBaseHdr, sizeof(BaseHeader))) {
return 0;
}
// go back, and read the whole O2 header (Base+Derived)
set_position(lBaseHdrPos);
if (!ignore_nbytes(lBaseHdr.size())) {
return 0;
}
lStackSize += lBaseHdr.size();
readNextHeader = (lBaseHdr.next() != nullptr);
}
// reset the file pointer
set_position(lFilePosStart);
if (lNumHeaders >= cMaxHeaders) {
EDDLOG("FileReader: Reached max number of headers allowed: {}.", cMaxHeaders);
return 0;
}
return lStackSize;
}
Stack SubTimeFrameFileReader::getHeaderStack(std::size_t &pOrigsize)
{
const auto lStackSize = getHeaderStackSize();
pOrigsize = lStackSize;
if (lStackSize < sizeof(BaseHeader)) {
// error in the stream
pOrigsize = 0;
return Stack{};
}
std::byte* lStackMem = reinterpret_cast<std::byte*>(peek());
if (!ignore_nbytes(lStackSize) ) {
// error in the stream
pOrigsize = 0;
return Stack{};
}
// This must handle different versions of DataHeader
// check if DataHeader needs an upgrade by looking at the version number
const BaseHeader *lBaseOfDH = BaseHeader::get(lStackMem);
if (!lBaseOfDH) {
return Stack{};
}
if (lBaseOfDH->headerVersion < DataHeader::sVersion) {
DataHeader lNewDh;
// Write over the new DataHeader. We need to update some of the BaseHeader values.
assert (sizeof (DataHeader) > lBaseOfDH->size() ); // current DataHeader must be larger
std::memcpy(&lNewDh, (void*)lBaseOfDH->data(), lBaseOfDH->size());
// make sure to bump the version in the BaseHeader.
lNewDh.headerSize = sizeof(DataHeader);
lNewDh.headerVersion = DataHeader::sVersion;
if (lBaseOfDH->headerVersion == 1 || lBaseOfDH->headerVersion == 2) {
/* nothing to do for the upgrade */
} else {
EDDLOG_RL(1000, "FileReader: DataHeader v{} read from file is not upgraded to the current version {}",
lBaseOfDH->headerVersion, DataHeader::sVersion);
EDDLOG_RL(1000, "Try using a newer version of DataDistribution or file a BUG");
}
if (lBaseOfDH->size() == lStackSize) {
return Stack(lNewDh);
} else {
assert(lBaseOfDH->size() < lStackSize);
return Stack(
lNewDh,
Stack(lStackMem + lBaseOfDH->size())
);
}
}
return Stack(lStackMem);
}
std::uint64_t SubTimeFrameFileReader::sStfId = 0;
std::unique_ptr<SubTimeFrame> SubTimeFrameFileReader::read(SubTimeFrameFileBuilder &pFileBuilder)
{
// make sure headers and chunk pointers don't linger
mStfData.clear();
// record current position
const auto lTfStartPosition = position();
if (lTfStartPosition == size()) {
return nullptr;
}
// If mFile is good, we're positioned to read a TF
if (!mFileMap.is_open() || eof()) {
return nullptr;
}
// NOTE: StfID will be updated from the stf header
std::unique_ptr<SubTimeFrame> lStf = std::make_unique<SubTimeFrame>(sStfId++);
std::size_t lMetaHdrStackSize = 0;
const DataHeader *lStfMetaDataHdr = nullptr;
SubTimeFrameFileMeta lStfFileMeta;
// Read DataHeader + SubTimeFrameFileMeta
auto lMetaHdrStack = getHeaderStack(lMetaHdrStackSize);
if (lMetaHdrStackSize == 0) {
EDDLOG("Failed to read the TF file header. The file might be corrupted.");
mFileMap.close();
return nullptr;
}
lStfMetaDataHdr = o2::header::DataHeader::Get(lMetaHdrStack.first());
if (!read_advance(&lStfFileMeta, sizeof(SubTimeFrameFileMeta))) {
return nullptr;
}
// use the stored timestamp for creation time
lStf->updateCreationTimeMs(lStfFileMeta.mWriteTimeMs);
// verify we're actually reading the correct data in
if (!(SubTimeFrameFileMeta::getDataHeader().dataDescription == lStfMetaDataHdr->dataDescription)) {
WDDLOG("Reading bad data: SubTimeFrame META header");
mFileMap.close();
return nullptr;
}
// prepare to read the TF data
const auto lStfSizeInFile = lStfFileMeta.mStfSizeInFile;
if (lStfSizeInFile == (sizeof(DataHeader) + sizeof(SubTimeFrameFileMeta))) {
WDDLOG("Reading an empty TF from file. Only meta information present");
mFileMap.close();
return nullptr;
}
// check there's enough data in the file
if ((lTfStartPosition + lStfSizeInFile) > this->size()) {
WDDLOG_RL(200, "Not enough data in file for this TF. Required: {}, available: {}",
lStfSizeInFile, (this->size() - lTfStartPosition));
mFileMap.close();
return nullptr;
}
// Index
std::size_t lStfIndexHdrStackSize = 0;
const DataHeader *lStfIndexHdr = nullptr;
// Read DataHeader + SubTimeFrameFileMeta
auto lStfIndexHdrStack = getHeaderStack(lStfIndexHdrStackSize);
if (lStfIndexHdrStackSize == 0 ) {
mFileMap.close();
return nullptr;
}
lStfIndexHdr = o2::header::DataHeader::Get(lStfIndexHdrStack.first());
if (!lStfIndexHdr) {
EDDLOG("Failed to read the TF index structure. The file might be corrupted.");
return nullptr;
}
if (!ignore_nbytes(lStfIndexHdr->payloadSize)) {
return nullptr;
}
// Remaining data size of the TF:
// total size in file - meta (hdr+struct) - index (hdr + payload)
const auto lStfDataSize = lStfSizeInFile - (lMetaHdrStackSize + sizeof(SubTimeFrameFileMeta))
- (lStfIndexHdrStackSize + lStfIndexHdr->payloadSize);
// read all data blocks and headers
assert(mStfData.empty());
std::int64_t lLeftToRead = lStfDataSize;
// Get the firstOrbit and run number from data
std::uint32_t lFirstOrbit = std::numeric_limits<std::uint32_t>::max();
std::uint32_t lRunNumber = std::numeric_limits<std::uint32_t>::max();
// read <hdrStack + data> pairs
while (lLeftToRead > 0) {
// allocate and read the Headers
std::size_t lDataHeaderStackSize = 0;
Stack lDataHeaderStack = getHeaderStack(lDataHeaderStackSize);
if (lDataHeaderStackSize == 0) {
mFileMap.close();
return nullptr;
}
const DataHeader *lDataHeader = o2::header::DataHeader::Get(lDataHeaderStack.first());
if (!lDataHeader) {
EDDLOG("Failed to read the TF HBF DataHeader structure. The file might be corrupted.");
mFileMap.close();
return nullptr;
}
// get the first orbit and run
if (lDataHeader->firstTForbit > 0) {
// we'll use RDH reader if not provided in the header
lFirstOrbit = std::min(lFirstOrbit, lDataHeader->firstTForbit);
}
lRunNumber = std::min(lRunNumber, lDataHeader->runNumber);
// read the data
const std::uint64_t lDataSize = lDataHeader->payloadSize;
auto lDataMsg = pFileBuilder.newDataMessage(lDataSize);
if (!lDataMsg) {
IDDLOG("Data memory resource stopped. Exiting.");
mFileMap.close();
return nullptr;
}
if (!read_advance(lDataMsg->GetData(), lDataSize) ) {
return nullptr;
}
// Try to figure out the first orbit in case not provided by readout or sim
try {
const auto lHdr = reinterpret_cast<DataHeader*>(lDataHeaderStack.data());
if (lHdr && lHdr->firstTForbit == 0 && lHdr->dataDescription == o2::header::gDataDescriptionRawData) {
const auto R = RDHReader(lDataMsg);
lFirstOrbit = std::min(lFirstOrbit, R.getOrbit());
}
} catch (...) {
EDDLOG("Error getting RDHReader instance. Not setting firstOrbit for file data");
}
// only create o2 hdr for the first message of split payload or single messages
if (lDataHeader->splitPayloadIndex == 0 || lDataHeader->splitPayloadParts <= 1) {
// header stack will add DPL header
auto lHdrStackMsg = pFileBuilder.newHeaderMessage(lDataHeaderStack, lStf->id());
if (!lHdrStackMsg) {
DDDLOG_RL(1000, "Header memory resource stopped. Exiting.");
mFileMap.close();
return nullptr;
}
mStfData.emplace_back(std::move(lHdrStackMsg), std::move(lDataMsg));
} else {
assert (!mStfData.empty());
mStfData.emplace_back(nullptr, std::move(lDataMsg));
}
// update the counter
lLeftToRead -= (lDataHeaderStackSize + lDataSize);
}
if (lLeftToRead < 0) {
EDDLOG("FileRead: Read more data than it is indicated in the META header!");
return nullptr;
}
// update the first orbit and run number
lStf->updateFirstOrbit(lFirstOrbit);
lStf->updateRunNumber(lRunNumber);
// build the SubtimeFrame
lStf->accept(*this);
return lStf;
}
} /* o2::DataDistribution */