CbmRoot
Loading...
Searching...
No Matches
CbmMQTsaInfo.cxx
Go to the documentation of this file.
1/* Copyright (C) 2017-2019 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Florian Uhlig [committer] */
4
13#include "CbmMQTsaInfo.h"
14
15#include "CbmMQDefs.h"
16
17#include "TimesliceInputArchive.hpp"
18#include "TimesliceSubscriber.hpp"
19
20#include "FairMQLogger.h"
21#include "FairMQProgOptions.h" // device->fConfig
22
23#include <thread> // this_thread::sleep_for
24
25#include <boost/archive/binary_oarchive.hpp>
26
27#include <chrono>
28#include <ctime>
29
30#include <stdio.h>
31
32using namespace std;
33
34#include <stdexcept>
35
36struct InitTaskError : std::runtime_error {
37 using std::runtime_error::runtime_error;
38};
39
40
42 : FairMQDevice()
43 , fMaxTimeslices(0)
44 , fFileName("")
45 , fInputFileList()
46 , fFileCounter(0)
47 , fHost("")
48 , fPort(0)
49 , fTSNumber(0)
50 , fTSCounter(0)
51 , fMessageCounter(0)
52 , fTime()
53{
54}
55
57try {
58 // Get the values from the command line options (via fConfig)
59 fFileName = fConfig->GetValue<string>("filename");
60 fHost = fConfig->GetValue<string>("flib-host");
61 fPort = fConfig->GetValue<uint64_t>("flib-port");
62 fMaxTimeslices = fConfig->GetValue<uint64_t>("max-timeslices");
63
64
65 LOG(info) << "Filename: " << fFileName;
66 LOG(info) << "Host: " << fHost;
67 LOG(info) << "Port: " << fPort;
68
69 LOG(info) << "MaxTimeslices: " << fMaxTimeslices;
70
71 // Get the information about created channels from the device
72 // Check if the defined channels from the topology (by name)
73 // are in the list of channels which are possible/allowed
74 // for the device
75 // The idea is to check at initilization if the devices are
76 // properly connected. For the time beeing this is done with a
77 // nameing convention. It is not avoided that someone sends other
78 // data on this channel.
79 int noChannel = fChannels.size();
80 LOG(info) << "Number of defined output channels: " << noChannel;
81 for (auto const& entry : fChannels) {
82 LOG(info) << "Channel name: " << entry.first;
83 if (!IsChannelNameAllowed(entry.first)) throw InitTaskError("Channel name does not match.");
84 }
85
86 if (0 == fFileName.size() && 0 != fHost.size()) {
87 std::string connector = "tcp://" + fHost + ":" + std::to_string(fPort);
88 LOG(info) << "Open TSPublisher at " << connector;
89 fSource = new fles::TimesliceSubscriber(connector, 1);
90 if (!fSource) { throw InitTaskError("Could not connect to publisher."); }
91 }
92 else {
93 LOG(info) << "Open the Flib input file " << fFileName;
94 // Check if the input file exist
95 FILE* inputFile = fopen(fFileName.c_str(), "r");
96 if (!inputFile) { throw InitTaskError("Input file doesn't exist."); }
97 fclose(inputFile);
98 fSource = new fles::TimesliceInputArchive(fFileName);
99 if (!fSource) { throw InitTaskError("Could not open input file."); }
100 }
101 fTime = std::chrono::steady_clock::now();
102}
103catch (InitTaskError& e) {
104 LOG(error) << e.what();
105 // Wrapper defined in CbmMQDefs.h to support different FairMQ versions
107}
108
109bool CbmMQTsaInfo::IsChannelNameAllowed(std::string channelName)
110{
111 if (std::find(fAllowedChannels.begin(), fAllowedChannels.end(), channelName) != fAllowedChannels.end()) {
112 LOG(info) << "Channel name " << channelName << " found in list of allowed channel names.";
113 return true;
114 }
115 else {
116 LOG(info) << "Channel name " << channelName << " not found in list of allowed channel names.";
117 LOG(error) << "Stop device.";
118 return false;
119 }
120}
121
123{
124
125
126 auto timeslice = fSource->get();
127 if (timeslice) {
128 fTSCounter++;
129 if (fTSCounter % 10000 == 0) LOG(info) << "Analyse Event " << fTSCounter;
130
131
132 const fles::Timeslice& ts = *timeslice;
133 // auto tsIndex = ts.index();
134
135
136 LOG(info) << "Found " << ts.num_components() << " different components in timeslice";
137
138 CheckTimeslice(ts);
139
140 if (fTSCounter < fMaxTimeslices) { return true; }
141 else {
142 CalcRuntime();
143 return false;
144 }
145 }
146 else {
147 CalcRuntime();
148 return false;
149 }
150}
151
152
154
156{
157 std::chrono::duration<double> run_time = std::chrono::steady_clock::now() - fTime;
158
159 LOG(info) << "Runtime: " << run_time.count();
160 LOG(info) << "No more input data";
161}
162
163
164void CbmMQTsaInfo::PrintMicroSliceDescriptor(const fles::MicrosliceDescriptor& mdsc)
165{
166 LOG(info) << "Header ID: Ox" << std::hex << static_cast<int>(mdsc.hdr_id) << std::dec;
167 LOG(info) << "Header version: Ox" << std::hex << static_cast<int>(mdsc.hdr_ver) << std::dec;
168 LOG(info) << "Equipement ID: " << mdsc.eq_id;
169 LOG(info) << "Flags: " << mdsc.flags;
170 LOG(info) << "Sys ID: Ox" << std::hex << static_cast<int>(mdsc.sys_id) << std::dec;
171 LOG(info) << "Sys version: Ox" << std::hex << static_cast<int>(mdsc.sys_ver) << std::dec;
172 LOG(info) << "Microslice Idx: " << mdsc.idx;
173 LOG(info) << "Checksum: " << mdsc.crc;
174 LOG(info) << "Size: " << mdsc.size;
175 LOG(info) << "Offset: " << mdsc.offset;
176}
177
178bool CbmMQTsaInfo::CheckTimeslice(const fles::Timeslice& ts)
179{
180 if (0 == ts.num_components()) {
181 LOG(error) << "No Component in TS " << ts.index();
182 return 1;
183 }
184 LOG(info) << "Found " << ts.num_components() << " different components in timeslice";
185
186 for (size_t c = 0; c < ts.num_components(); ++c) {
187 LOG(info) << "Found " << ts.num_microslices(c) << " microslices in component " << c;
188 LOG(info) << "Component " << c << " has a size of " << ts.size_component(c) << " bytes";
189 LOG(info) << "Component " << c << " has the system id 0x" << std::hex
190 << static_cast<int>(ts.descriptor(c, 0).sys_id) << std::dec;
191
192 /*
193 for (size_t m = 0; m < ts.num_microslices(c); ++m) {
194 PrintMicroSliceDescriptor(ts.descriptor(c,m));
195 }
196*/
197 }
198
199 return true;
200}
std::vector< std::string > fAllowedChannels
virtual bool ConditionalRun()
std::chrono::steady_clock::time_point fTime
uint64_t fTSCounter
bool IsChannelNameAllowed(std::string)
uint64_t fMaxTimeslices
uint64_t fPort
virtual void InitTask()
std::string fFileName
bool CheckTimeslice(const fles::Timeslice &ts)
fles::TimesliceSource * fSource
void PrintMicroSliceDescriptor(const fles::MicrosliceDescriptor &mdsc)
std::string fHost
virtual ~CbmMQTsaInfo()
void ChangeState(FairMQDevice *device, cbm::mq::Transition transition)
Definition CbmMQDefs.h:26
Hash for CbmL1LinkKey.