-
Notifications
You must be signed in to change notification settings - Fork 499
Expand file tree
/
Copy pathDataInspector.cxx
More file actions
150 lines (132 loc) · 5.61 KB
/
DataInspector.cxx
File metadata and controls
150 lines (132 loc) · 5.61 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
// Copyright 2019-2020 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.
#include "DataInspector.h"
#include "Framework/DataProcessorSpec.h"
#include "Framework/DeviceSpec.h"
#include "Framework/OutputObjHeader.h"
#include "Framework/RawDeviceService.h"
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cstdint>
#include <fcntl.h>
#include <iomanip>
#include <ios>
#include <iostream>
#include <iterator>
#include <memory>
#include <sstream>
#include <stdexcept>
#include <string>
#include <unistd.h>
#include <utility>
#include <rapidjson/document.h>
#include <rapidjson/prettywriter.h>
#include "boost/asio.hpp"
#include <TBufferJSON.h>
#include <boost/algorithm/string/join.hpp>
#include <arrow/table.h>
#include "Framework/TableConsumer.h"
#include "boost/archive/iterators/base64_from_binary.hpp"
#include "boost/archive/iterators/transform_width.hpp"
#include "boost/predef/other/endian.h"
using namespace rapidjson;
namespace o2::framework::data_inspector
{
#if BOOST_ENDIAN_BIG_BYTE
static const auto endianness = "BIG";
#elif BOOST_ENDIAN_LITTLE_BYTE
static const auto endianness = "LITTLE";
#else
static const auto endianness = "UNKNOWN";
#endif
inline size_t base64PaddingSize(uint64_t dataSize)
{
return (3 - dataSize % 3) % 3;
}
std::string encode64(const char* data, uint64_t size)
{
auto* begin = data;
auto* end = data + size;
using namespace boost::archive::iterators;
using EncodingIt = base64_from_binary<transform_width<const char*, 6, 8>>;
return std::string(EncodingIt(begin), EncodingIt(end)).append(base64PaddingSize(size), '=');
}
void addPayload(Document& message,
uint64_t payloadSize,
const char* payload,
Document::AllocatorType& alloc)
{
message.AddMember("payload", Value(encode64(payload, payloadSize).c_str(), alloc), alloc);
message.AddMember("payloadEndianness", Value(endianness, alloc), alloc);
}
void addBasicDataHeaderInfo(Document& message, const header::DataHeader* header, Document::AllocatorType& alloc)
{
std::string origin = header->dataOrigin.as<std::string>();
std::string description = header->dataDescription.as<std::string>();
std::string method = header->payloadSerializationMethod.as<std::string>();
message.AddMember("origin", Value(origin.c_str(), alloc), alloc);
message.AddMember("description", Value(description.c_str(), alloc), alloc);
message.AddMember("subSpecification", Value(header->subSpecification), alloc);
message.AddMember("firstTForbit", Value(header->firstTForbit), alloc);
message.AddMember("tfCounter", Value(header->tfCounter), alloc);
message.AddMember("runNumber", Value(header->runNumber), alloc);
message.AddMember("payloadSize", Value(header->payloadSize), alloc);
message.AddMember("splitPayloadParts", Value(header->splitPayloadParts), alloc);
message.AddMember("payloadSerialization", Value(method.c_str(), alloc), alloc);
message.AddMember("payloadSplitIndex", Value(header->splitPayloadIndex), alloc);
}
void addBasicDataProcessingHeaderInfo(Document& message, const DataProcessingHeader* header, Document::AllocatorType& alloc)
{
message.AddMember("startTime", Value(header->startTime), alloc);
message.AddMember("duration", Value(header->duration), alloc);
message.AddMember("creationTimer", Value(header->creation), alloc);
}
void addBasicOutputObjHeaderInfo(Document& message, const OutputObjHeader* header, Document::AllocatorType& alloc)
{
message.AddMember("taskHash", Value(header->mTaskHash), alloc);
}
void buildDocument(Document& message, std::string sender, const DataRef& ref)
{
message.SetObject();
Document::AllocatorType& alloc = message.GetAllocator();
message.AddMember("sender", Value(sender.c_str(), alloc), alloc);
const header::BaseHeader* baseHeader = header::BaseHeader::get(reinterpret_cast<const std::byte*>(ref.header));
for (; baseHeader != nullptr; baseHeader = baseHeader->next()) {
if (baseHeader->description == header::DataHeader::sHeaderType) {
const auto* header = header::get<header::DataHeader*>(baseHeader->data());
addBasicDataHeaderInfo(message, header, alloc);
addPayload(message, header->payloadSize, ref.payload, alloc);
} else if (baseHeader->description == DataProcessingHeader::sHeaderType) {
const auto* header = header::get<DataProcessingHeader*>(baseHeader->data());
addBasicDataProcessingHeaderInfo(message, header, alloc);
} else if (baseHeader->description == OutputObjHeader::sHeaderType) {
const auto* header = header::get<OutputObjHeader*>(baseHeader->data());
addBasicOutputObjHeaderInfo(message, header, alloc);
}
}
}
/* Callback which transforms each `DataRef` to a JSON object*/
std::vector<DIMessage> serializeO2Messages(const std::vector<DataRef>& refs, const std::string& deviceName)
{
std::vector<DIMessage> messages{};
for (auto& ref : refs) {
Document message;
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
buildDocument(message, deviceName, ref);
message.Accept(writer);
messages.emplace_back(DIMessage{DIMessage::Header::Type::DATA, std::string{buffer.GetString(), buffer.GetSize()}});
}
return messages;
}
} // namespace o2::framework::data_inspector