-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathReadoutDataModel.h
More file actions
428 lines (352 loc) · 12.3 KB
/
ReadoutDataModel.h
File metadata and controls
428 lines (352 loc) · 12.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// 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
#ifndef ALICEO2_READOUT_DATAMODEL_H_
#define ALICEO2_READOUT_DATAMODEL_H_
#include <DataDistLogger.h>
#include <Headers/DataHeader.h>
#include <Headers/RAWDataHeader.h>
#include <Headers/DAQID.h>
#include <fairmq/Message.h>
#include <istream>
#include <cstdint>
#include <tuple>
#include <variant>
namespace o2::DataDistribution
{
////////////////////////////////////////////////////////////////////////////////
/// RDH reader interface
////////////////////////////////////////////////////////////////////////////////
class RDHReaderException : public std::runtime_error {
const char *mData;
const std::size_t mSize;
public:
RDHReaderException() = delete;
RDHReaderException(const char* data, const std::size_t size, const std::string &pMsg)
: std::runtime_error("READOUT INTERFACE: Error when accessing RDH: " + pMsg),
mData(data),
mSize(size) {}
};
struct RDHReaderIf {
public:
virtual ~RDHReaderIf() = default;
// We walidate the data and the size on creation of the reader. No need to pass the size again
virtual std::size_t CheckRdhData(const char* data, const std::size_t size) const = 0;
// equipment
virtual std::uint8_t getSystemID(const char* data) const = 0;
virtual std::uint64_t getFeeID(const char* data) const = 0;
virtual std::uint16_t getLinkID(const char* data) const = 0;
virtual std::uint8_t getEndPointID(const char* data) const = 0;
virtual std::uint16_t getCruID(const char* data) const = 0;
// memory layout
virtual std::uint32_t getMemorySize(const char* data) const = 0;
virtual std::uint32_t getOffsetToNext(const char* data) const = 0;
virtual bool getStopBit(const char* data) const = 0;
virtual std::uint16_t getPageCounter(const char* data) const = 0;
// trigger
virtual std::uint32_t getOrbit(const char* data) const = 0;
virtual std::uint16_t getBC(const char* data) const = 0;
virtual std::uint32_t getTriggerType(const char* data) const = 0;
};
template<typename RDH>
class RDHReaderImpl final : public RDHReaderIf {
inline static
const RDH& getHdrRef(const char* data) {
return *reinterpret_cast<const RDH*>(data);
}
public:
RDHReaderImpl() = default;
virtual ~RDHReaderImpl() {};
virtual inline
std::size_t CheckRdhData(const char* data, const std::size_t size) const override final {
if (!data) {
throw RDHReaderException(data, size, "Received NULL pointer instead of an RDH.");
}
if (sizeof(RDH) > size) {
throw RDHReaderException(data, size, "RDH size is too small. size=" + std::to_string(size));
}
return sizeof(RDH);
}
// RDH field for SubSpecification
virtual inline
std::uint8_t getSystemID(const char* data) const override final {
if constexpr (std::is_same_v<RDH, o2::header::RAWDataHeaderV6>) {
const RDH &lRdh = getHdrRef(data);
return lRdh.sourceID; /* systemID */
}
(void) data;
return o2::header::DAQID::INVALID;
}
virtual inline
std::uint64_t getFeeID(const char* data) const override final {
const RDH &lRdh = getHdrRef(data);
return lRdh.feeId;
}
virtual inline
std::uint16_t getLinkID(const char* data) const override final {
const RDH &lRdh = getHdrRef(data);
return lRdh.linkID;
}
virtual inline
std::uint8_t getEndPointID(const char* data) const override final {
const RDH &lRdh = getHdrRef(data);
return lRdh.endPointID;
}
virtual inline
std::uint16_t getCruID(const char* data) const override final {
const RDH &lRdh = getHdrRef(data);
return lRdh.cruID;
}
// RDH memory layout
virtual inline
std::uint32_t getMemorySize(const char* data) const override final {
const RDH &lRdh = getHdrRef(data);
return lRdh.memorySize;
}
virtual inline
std::uint32_t getOffsetToNext(const char* data) const override final {
const RDH &lRdh = getHdrRef(data);
return lRdh.offsetToNext;
}
virtual inline
bool getStopBit(const char* data) const override final {
const RDH &lRdh = getHdrRef(data);
return lRdh.stop;
}
virtual inline
std::uint16_t getPageCounter(const char* data) const override final {
const RDH &lRdh = getHdrRef(data);
return lRdh.pageCnt;
}
// RDH trigger information
virtual inline
std::uint32_t getOrbit(const char* data) const override final {
const RDH &lRdh = getHdrRef(data);
if constexpr(std::is_same_v<RDH, o2::header::RAWDataHeaderV4>) {
return lRdh.heartbeatOrbit;
} else {
return lRdh.orbit;
}
}
virtual inline
std::uint16_t getBC(const char* data) const override final {
const RDH &lRdh = getHdrRef(data);
if constexpr(std::is_same_v<RDH, o2::header::RAWDataHeaderV4>) {
return lRdh.heartbeatBC;
} else {
return lRdh.bunchCrossing;
}
}
virtual inline
std::uint32_t getTriggerType(const char* data) const override final {
const RDH &lRdh = getHdrRef(data);
return lRdh.triggerType;
}
};
using RDHv3Reader = RDHReaderImpl<o2::header::RAWDataHeaderV4>; // V4 and V3 are equivalent?
using RDHv4Reader = RDHReaderImpl<o2::header::RAWDataHeaderV4>; // V4 and V3 are equivalent?
using RDHv5Reader = RDHReaderImpl<o2::header::RAWDataHeaderV5>;
using RDHv6Reader = RDHReaderImpl<o2::header::RAWDataHeaderV6>;
using RDHv7Reader = RDHReaderImpl<o2::header::RAWDataHeaderV7>;
class RDHReader {
// NOTE: This must be set in the Init() phase. Logical program error otherwise.
static std::unique_ptr<RDHReaderIf> sRDHReader;
inline static const RDHReaderIf& I() { return *sRDHReader; }
// store the RDH pointer for later use
char *mData;
std::size_t mSize;
std::size_t mRDHSize;
RDHReader()
: mData(nullptr),
mSize(0),
mRDHSize(0) {}
RDHReader(const char* data, const std::size_t size, const std::size_t rsize)
: mData(const_cast<char*>(data)),
mSize(size),
mRDHSize(rsize) {}
public:
static void Initialize(const unsigned pVer) {
switch (pVer) {
case 3:
sRDHReader = std::make_unique<RDHv3Reader>();
break;
case 4:
sRDHReader = std::make_unique<RDHv4Reader>();
break;
case 5:
sRDHReader = std::make_unique<RDHv5Reader>();
break;
case 6:
sRDHReader = std::make_unique<RDHv6Reader>();
break;
case 7:
sRDHReader = std::make_unique<RDHv7Reader>();
break;
default:
EDDLOG("Unknown RDH version! version={} Supported versions are RDHv3, RDHv4, RDHv5, RDHv6, and RDHv7", pVer);
throw std::runtime_error("Unknown RDH version");
break;
}
}
RDHReader(const char* data, const std::size_t size)
: mData(const_cast<char*>(data)),
mSize(size)
{
if (!sRDHReader) {
WDDLOG("RDH version not initialized manually! Using the value from the first data packet.");
if (size > 0) {
Initialize(data[0]);
} else {
throw RDHReaderException(data, size, "Cannot determine RDH version. Requested=" + std::to_string(data[0]));
}
}
assert(!!sRDHReader);
mRDHSize = sRDHReader->CheckRdhData(mData, mSize);
}
explicit RDHReader(const fair::mq::MessagePtr &msg) {
if (!msg) {
throw std::runtime_error("RDHReader::msg is null");
}
*this = std::move(RDHReader(reinterpret_cast<const char*>(msg->GetData()), msg->GetSize()));
}
RDHReader(const RDHReader &b) = default;
RDHReader(RDHReader &&b) = default;
constexpr RDHReader& operator=(const RDHReader &b) {
mData = b.mData;
mSize = b.mSize;
mRDHSize = b.mRDHSize;
return *this;
}
inline
RDHReader next() const {
if (getStopBit()) {
return RDHReader();
}
const auto lOffNext = this->getOffsetToNext();
if (lOffNext < 64 || lOffNext > 8192) {
return RDHReader(); // error
}
const char *p = mData + lOffNext;
if (((mData + mSize) - mRDHSize) < p) {
return RDHReader(); // the rest of original buffer is too short
}
return RDHReader(p, mData + mSize - p, mRDHSize);
}
inline
RDHReader end() const { return RDHReader(); }
inline
bool operator==(const RDHReader& b) {
if (mData == b.mData && mSize == b.mSize) {
return true;
}
return false;
}
inline
bool operator!=(const RDHReader &b) {
return !(*this == b);
}
inline
std::size_t getRDHSize() const { return mRDHSize; };
// RDH equipment
inline
std::uint8_t getSystemID() const { return I().getSystemID(mData); }
inline
std::uint64_t getFeeID() const { return I().getFeeID(mData); }
inline
std::uint16_t getLinkID() const { return I().getLinkID(mData); }
inline
std::uint8_t getEndPointID() const { return I().getEndPointID(mData); }
inline
std::uint16_t getCruID() const { return I().getCruID(mData); }
// RDH memory layout
inline
std::uint32_t getMemorySize() const { return I().getMemorySize(mData); }
inline
std::uint32_t getOffsetToNext() const { return I().getOffsetToNext(mData); }
inline
bool getStopBit() const { return I().getStopBit(mData); }
inline
std::uint16_t getPageCounter() const { return I().getPageCounter(mData); }
// RDH trigger information
inline
std::uint32_t getOrbit() const { return I().getOrbit(mData); };
inline
std::uint16_t getBC() const { return I().getBC(mData); };
inline
std::uint32_t getTriggerType() const { return I().getTriggerType(mData); };
};
////////////////////////////////////////////////////////////////////////////////
/// ReadoutSubTimeframeHeader
////////////////////////////////////////////////////////////////////////////////
// FIXME: copied from Readout/SubTimeframe.h
// definition of the header message for a subtimeframe made of 1
// message with this header followed by a message for each HBFrame
// All data belong to the same source (a link or user logic)
struct ReadoutSubTimeframeHeader {
uint8_t mVersion = 2;
uint32_t mTimeFrameId = 0; // id of timeframe
uint32_t mRunNumber = 0;
uint8_t mSystemId = 0xFF;
uint16_t mFeeId = 0xFFFF;
uint16_t mEquipmentId = 0xFFFF;
uint8_t mLinkId = 0xFF;
uint32_t mTimeframeOrbitFirst = 0;
uint32_t mTimeframeOrbitLast = 0;
struct {
uint8_t mLastTFMessage : 1; // bit 0
uint8_t mIsRdhFormat : 1; // bit 1
uint8_t mFlagsUnused : 6; // bit 2-7: unused
} mFlags;
};
class ReadoutDataUtils {
public:
enum SubSpecMode {
eCruLinkId,
eFeeId
};
static SubSpecMode sRawDataSubspectype;
enum SanityCheckMode {
eNoSanityCheck = 0,
eSanityCheckDrop,
eSanityCheckPrint
};
static SanityCheckMode sRdhSanityCheckMode;
enum RdhVersion {
eRdhInvalid = -1,
eRdhVer3 = 3,
eRdhVer4 = 4,
eRdhVer5 = 5,
eRdhVer6 = 6,
};
enum RunType {
eInvalid = -1,
ePhysics = 1,
eTopology = 2,
};
static o2::header::DataOrigin sSpecifiedDataOrigin; // to be initialized if not RDH6
static RdhVersion sRdhVersion;
static RunType sRunType;
static bool sEmptyTriggerHBFrameFilterring;
static std::uint32_t sFirstSeenHBOrbitCnt;
static o2::header::DataOrigin getDataOrigin(const RDHReader &R);
static o2::header::DataHeader::SubSpecificationType getSubSpecification(const RDHReader &R);
static std::tuple<std::size_t, bool> getHBFrameMemorySize(const fair::mq::MessagePtr &pMsg);
static bool rdhSanityCheck(const char* data, const std::size_t len);
static bool filterEmptyTriggerBlocks(const char* pData, const std::size_t pLen);
};
std::istream& operator>>(std::istream& in, ReadoutDataUtils::SanityCheckMode& pRetVal);
std::istream& operator>>(std::istream& in, ReadoutDataUtils::SubSpecMode& pRetVal);
std::istream& operator>>(std::istream& in, ReadoutDataUtils::RdhVersion& pRetVal);
std::istream& operator>>(std::istream& in, ReadoutDataUtils::RunType& pRetVal);
std::string to_string (ReadoutDataUtils::SubSpecMode c);
std::string to_string (ReadoutDataUtils::RunType c);
} /* o2::DataDistribution */
#endif /* ALICEO2_READOUT_DATAMODEL_H_ */