artdaq  v3_12_01
ListenTransferWrapper.cc
1 #include "artdaq/DAQdata/Globals.hh"
2 #define TRACE_NAME "ListenTransferWrapper"
3 
4 #include "artdaq-core/Data/Fragment.hh"
5 #include "artdaq-core/Utilities/ExceptionHandler.hh"
6 #include "artdaq-core/Utilities/TimeUtils.hh"
7 #include "artdaq/ArtModules/detail/ListenTransferWrapper.hh"
8 #include "artdaq/DAQdata/NetMonHeader.hh"
9 #include "artdaq/TransferPlugins/MakeTransferPlugin.hh"
10 
11 #include "cetlib/BasicPluginFactory.h"
12 #include "cetlib_except/exception.h"
13 #include "fhiclcpp/ParameterSet.h"
14 
15 #include <csignal>
16 #include <iostream>
17 #include <limits>
18 #include <memory>
19 #include <sstream>
20 #include <string>
21 
22 namespace {
23 volatile std::sig_atomic_t gListenSignalStatus = 0;
24 }
25 
30 void listen_signal_handler(int signal)
31 {
32  gListenSignalStatus = signal;
33 }
34 
36  : timeoutInUsecs_(pset.get<std::size_t>("timeoutInUsecs", 100000))
37  , last_received_data_()
38  , last_report_(std::chrono::steady_clock::now())
39  , transfer_(nullptr)
40  , pset_(pset)
41  , maxEventsBeforeInit_(pset.get<std::size_t>("maxEventsBeforeInit", 5))
42  , allowedFragmentTypes_(pset.get<std::vector<int>>("allowedFragmentTypes", {226, 227, 229}))
43  , runningStateTimeout_(pset.get<double>("dispatcherConnectTimeout", 0))
44  , runningStateInterval_us_(pset.get<size_t>("dispatcherConnectRetryInterval_us", 1000000))
45  , quitOnFragmentIntegrityProblem_(pset.get<bool>("quitOnFragmentIntegrityProblem", true))
46  , multi_run_mode_(pset.get<bool>("allowMultipleRuns", true))
47 {
48  std::signal(SIGINT, listen_signal_handler);
49 
50  try
51  {
52  if (metricMan)
53  {
54  metricMan->initialize(pset.get<fhicl::ParameterSet>("metrics", fhicl::ParameterSet()), "Online Monitor");
55  metricMan->do_start();
56  }
57  }
58  catch (...)
59  {
60  artdaq::ExceptionHandler(
61  artdaq::ExceptionHandlerRethrow::no,
62  "ListenTransferWrapper: could not configure metrics");
63  }
64 
65  try {
66  transfer_ = artdaq::MakeTransferPlugin(pset_, "transfer_plugin", artdaq::TransferInterface::Role::kReceive);
67  } catch(...) {
68  artdaq::ExceptionHandler(artdaq::ExceptionHandlerRethrow::no, "ListenTransferWrapper: failure in call to MakeTransferPlugin");
69  }
70 
71 
72  // Clamp possible values
73  if (runningStateInterval_us_ < 1000)
74  {
75  TLOG(TLVL_WARNING) << "Invalid value " << runningStateInterval_us_ << " us detected for dispatcherConnectRetryInterval_us. Setting to 1000 us";
76  runningStateInterval_us_ = 1000;
77  }
78  if (runningStateInterval_us_ > 30000000)
79  {
80  TLOG(TLVL_WARNING) << "Invalid value " << runningStateInterval_us_ << " us detected for dispatcherConnectRetryInterval_us. Setting to 30,000,000 us";
81  runningStateInterval_us_ = 30000000;
82  }
83 }
84 
86 {
87  artdaq::FragmentPtrs fragmentPtrs;
88  bool receivedFragment = false;
89  static bool initialized = false;
90  static size_t fragments_received = 0;
91 
92  while (gListenSignalStatus == 0)
93  {
94  receivedFragment = false;
95  auto fragmentPtr = std::make_unique<artdaq::Fragment>();
96 
97  while (!receivedFragment)
98  {
99  if (gListenSignalStatus != 0)
100  {
101  TLOG(TLVL_INFO) << "Ctrl-C appears to have been hit";
102  return fragmentPtrs;
103  }
104 
105  try
106  {
107  auto result = transfer_->receiveFragment(*fragmentPtr, timeoutInUsecs_);
108 
110  {
111  receivedFragment = true;
112  fragments_received++;
113 
114  static size_t cntr = 0;
115  auto mod = ++cntr % 10;
116  auto suffix = "-th";
117  if (mod == 1)
118  {
119  suffix = "-st";
120  }
121  if (mod == 2)
122  {
123  suffix = "-nd";
124  }
125  if (mod == 3)
126  {
127  suffix = "-rd";
128  }
129  TLOG(TLVL_INFO) << "Received " << cntr << suffix << " event, "
130  << "seqID == " << fragmentPtr->sequenceID()
131  << ", type == " << fragmentPtr->typeString();
132  last_received_data_ = std::chrono::steady_clock::now();
133  continue;
134  }
136  {
137  TLOG(TLVL_ERROR) << "Transfer Plugin disconnected or other unrecoverable error. Shutting down.";
138  if (multi_run_mode_)
139  {
140  initialized = false;
141  continue;
142  }
143  return fragmentPtrs;
144  }
145  else
146  {
147  auto tlvl = TLVL_DEBUG + 33;
148  if (artdaq::TimeUtils::GetElapsedTime(last_report_) > 1.0 && artdaq::TimeUtils::GetElapsedTime(last_received_data_) > 1.0)
149  {
150  tlvl = TLVL_WARNING;
151  last_report_ = std::chrono::steady_clock::now();
152  }
153 
154  auto last_received_milliseconds = artdaq::TimeUtils::GetElapsedTimeMilliseconds(last_received_data_);
155 
156  // 02-Jun-2018, KAB: added status/result printout
157  // to-do: add another else clause that explicitly checks for RECV_TIMEOUT
158  TLOG(tlvl) << "Timeout occurred in call to transfer_->receiveFragmentFrom; will try again"
159  << ", status = " << result << ", last received data " << last_received_milliseconds << " ms ago.";
160  }
161  }
162  catch (...)
163  {
164  artdaq::ExceptionHandler(
165  artdaq::ExceptionHandlerRethrow::yes,
166  "Problem receiving data in ListenTransferWrapper::receiveMessage");
167  }
168  }
169 
170  if (fragmentPtr->type() == artdaq::Fragment::EndOfSubrunFragmentType || fragmentPtr->type() == artdaq::Fragment::EndOfRunFragmentType) {
171  // Ignore these for now
172  continue;
173  }
174 
175  if (fragmentPtr->type() == artdaq::Fragment::EndOfDataFragmentType)
176  {
177  //if (monitorRegistered_)
178  //{
179  // unregisterMonitor();
180  //}
181  if (multi_run_mode_)
182  {
183  // initialized = false;
184  continue;
185  }
186 
187  return fragmentPtrs;
188  }
189 
190  checkIntegrity(*fragmentPtr);
191 
192  if (initialized || fragmentPtr->type() == artdaq::Fragment::InitFragmentType)
193  {
194  if(initialized && fragmentPtr->type() == artdaq::Fragment::InitFragmentType) {
195  // Ignore reinit for now, maybe handle in ArtdaqInputHelper later
196  continue;
197  }
198  initialized = true;
199  fragmentPtrs.push_back(std::move(fragmentPtr));
200  break;
201  }
202 
203  if (fragments_received > maxEventsBeforeInit_)
204  {
205  throw cet::exception("ListenTransferWrapper") << "First " << maxEventsBeforeInit_ << " events received did not include the \"Init\" event containing necessary info for art; exiting..."; // NOLINT(cert-err60-cpp)
206  }
207  }
208 
209  return fragmentPtrs;
210 }
211 
212 std::unordered_map<artdaq::Fragment::type_t, std::unique_ptr<artdaq::Fragments>>
214 {
215  std::unordered_map<artdaq::Fragment::type_t, std::unique_ptr<artdaq::Fragments>> output;
216 
217  auto ptrs = receiveMessage();
218  for (auto& ptr : ptrs)
219  {
220  auto fragType = ptr->type();
221  auto fragPtr = ptr.release();
222  ptr.reset(nullptr);
223 
224  if (output.count(fragType) == 0u)
225  {
226  output[fragType] = std::make_unique<artdaq::Fragments>();
227  }
228 
229  output[fragType]->emplace_back(std::move(*fragPtr));
230  }
231 
232  return output;
233 }
234 
235 void artdaq::ListenTransferWrapper::checkIntegrity(
236  const artdaq::Fragment& fragment) const {
237  const size_t artdaqheader = artdaq::detail::RawFragmentHeader::num_words() *
238  sizeof(artdaq::detail::RawFragmentHeader::RawDataType);
239  const auto payload = static_cast<size_t>(fragment.dataEndBytes() - fragment.dataBeginBytes());
240  const size_t metadata = sizeof(artdaq::NetMonHeader);
241  const size_t totalsize = fragment.sizeBytes();
242 
243  const auto type = static_cast<size_t>(fragment.type());
244 
245  if (totalsize != artdaqheader + metadata + payload)
246  {
247  std::stringstream errmsg;
248  errmsg << "Error: artdaq fragment of type " << fragment.typeString() << ", sequence ID " << fragment.sequenceID() << " has internally inconsistent measures of its size, signalling data corruption: in bytes,"
249  << " total size = " << totalsize << ", artdaq fragment header = " << artdaqheader << ", metadata = " << metadata << ", payload = " << payload;
250 
251  TLOG(TLVL_ERROR) << errmsg.str();
252 
253  if (quitOnFragmentIntegrityProblem_)
254  {
255  throw cet::exception("ListenTransferWrapper") << errmsg.str(); // NOLINT(cert-err60-cpp)
256  }
257 
258  return;
259  }
260 
261  auto findloc = std::find(allowedFragmentTypes_.begin(), allowedFragmentTypes_.end(), static_cast<int>(type));
262 
263  if (findloc == allowedFragmentTypes_.end())
264  {
265  std::stringstream errmsg;
266  errmsg << "Error: artdaq fragment appears to have type "
267  << type << ", not found in the allowed fragment types list";
268 
269  TLOG(TLVL_ERROR) << errmsg.str();
270  if (quitOnFragmentIntegrityProblem_)
271  {
272  throw cet::exception("ListenTransferWrapper") << errmsg.str(); // NOLINT(cert-err60-cpp)
273  }
274 
275  return;
276  }
277 }
278 
280 {
282 }
static void CleanUpGlobals()
Clean up statically-allocated Manager class instances.
Definition: Globals.hh:156
virtual ~ListenTransferWrapper()
ListenTransferWrapper Destructor.
std::unordered_map< artdaq::Fragment::type_t, std::unique_ptr< artdaq::Fragments > > receiveMessages()
Receive all messsages for an event from ArtdaqSharedMemoryService.
std::unique_ptr< artdaq::TransferInterface > MakeTransferPlugin(const fhicl::ParameterSet &pset, const std::string &plugin_label, TransferInterface::Role role)
Load a TransferInterface plugin.
This TransferInterface is a Receiver.
Header with length information for NetMonTransport messages.
Definition: NetMonHeader.hh:13
Value that is to be returned when a Transfer plugin determines that no more data will be arriving...
artdaq::FragmentPtrs receiveMessage()
Receive a Fragment from the TransferInterface, and send it to art.
volatile std::sig_atomic_t gListenSignalStatus
Stores singal from signal handler.
For code clarity, things checking for successful receive should check retval &gt;= NO_RANK_INFO.
ListenTransferWrapper(const fhicl::ParameterSet &pset)
ListenTransferWrapper Constructor.