artdaq  v3_09_00
FragmentReceiverManager.cc
1 #include "artdaq/DAQdata/Globals.hh"
2 #define TRACE_NAME (app_name + "_FragmentReceiverManager").c_str()
3 
4 #include <chrono>
5 #include <memory>
6 
7 #include "artdaq/DAQrate/FragmentReceiverManager.hh"
8 #include "artdaq/TransferPlugins/MakeTransferPlugin.hh"
9 #include "cetlib_except/exception.h"
10 
12  : stop_requested_(false)
13  , recv_frag_count_()
14  , recv_frag_size_()
15  , recv_seq_count_()
16  , suppress_noisy_senders_(pset.get<bool>("auto_suppression_enabled", true))
17  , suppression_threshold_(pset.get<size_t>("max_receive_difference", 50))
18  , receive_timeout_(pset.get<size_t>("receive_timeout_usec", 100000))
19  , last_source_(-1)
20 {
21  TLOG(TLVL_DEBUG) << "Constructor";
22  auto enabled_srcs = pset.get<std::vector<int>>("enabled_sources", std::vector<int>());
23  auto enabled_srcs_empty = enabled_srcs.empty();
24  if (enabled_srcs_empty)
25  {
26  TLOG(TLVL_INFO) << "enabled_sources not specified, assuming all sources enabled.";
27  }
28  else
29  {
30  for (auto& s : enabled_srcs)
31  {
32  enabled_sources_[s] = true;
33  }
34  }
35 
36  auto srcs = pset.get<fhicl::ParameterSet>("sources", fhicl::ParameterSet());
37  for (auto& s : srcs.get_pset_names())
38  {
39  try
40  {
41  auto transfer = std::unique_ptr<TransferInterface>(MakeTransferPlugin(srcs, s,
43  auto source_rank = transfer->source_rank();
44  if (enabled_srcs_empty)
45  {
46  enabled_sources_[source_rank] = true;
47  }
48  else if (enabled_sources_.count(source_rank) == 0u)
49  {
50  enabled_sources_[source_rank] = false;
51  }
52  running_sources_[source_rank] = false;
53  source_plugins_[source_rank] = std::move(transfer);
54  fragment_store_[source_rank];
55  source_metric_send_time_[source_rank] = std::chrono::steady_clock::now();
56  source_metric_data_[source_rank] = std::pair<size_t, double>();
57  }
58  catch (const cet::exception& ex)
59  {
60  TLOG(TLVL_WARNING) << "cet::exception caught while setting up source " << s << ": " << ex.what();
61  }
62  catch (const std::exception& ex)
63  {
64  TLOG(TLVL_WARNING) << "std::exception caught while setting up source " << s << ": " << ex.what();
65  }
66  catch (...)
67  {
68  TLOG(TLVL_WARNING) << "Non-cet exception caught while setting up source " << s << ".";
69  }
70  }
71  if (srcs.get_pset_names().empty())
72  {
73  TLOG(TLVL_ERROR) << "No sources configured!";
74  }
75 }
76 
78 {
79  TLOG(TLVL_DEBUG) << "Destructor";
80  TLOG(5) << "~FragmentReceiverManager: BEGIN: Setting stop_requested to true, frags=" << count() << ", bytes=" << byteCount();
81  stop_requested_ = true;
82 
83  TLOG(5) << "~FragmentReceiverManager: Notifying all threads";
84  output_cv_.notify_all();
85 
86  TLOG(5) << "~FragmentReceiverManager: Joining all threads";
87  for (auto& s : source_threads_)
88  {
89  auto& thread = s.second;
90  try
91  {
92  if (thread.joinable())
93  {
94  thread.join();
95  }
96  }
97  catch (...)
98  {
99  // IGNORED
100  }
101  }
102  TLOG(5) << "~FragmentReceiverManager: DONE";
103 }
104 
105 bool artdaq::FragmentReceiverManager::fragments_ready_() const
106 {
107  for (auto& it : fragment_store_)
108  {
109  if (enabled_sources_.count(it.first) == 0u)
110  {
111  continue;
112  }
113  if (!it.second.empty()) { return true; }
114  }
115  return false;
116 }
117 
118 int artdaq::FragmentReceiverManager::get_next_source_() const
119 {
120  //std::unique_lock<std::mutex> lck(fragment_store_mutex_);
121  std::set<int> ready_sources;
122  for (auto& it : fragment_store_)
123  {
124  if (enabled_sources_.count(it.first) == 0u)
125  {
126  continue;
127  }
128  if (!it.second.empty())
129  {
130  ready_sources.insert(it.first);
131  }
132  }
133 
134  if (!ready_sources.empty())
135  {
136  auto iter = ready_sources.find(last_source_);
137  if (iter == ready_sources.end() || ++iter == ready_sources.end())
138  {
139  TLOG(10) << "get_next_source returning " << *ready_sources.begin();
140  last_source_ = *ready_sources.begin();
141  return *ready_sources.begin();
142  }
143 
144  TLOG(10) << "get_next_source returning " << *iter;
145  last_source_ = *iter;
146  return *iter;
147  }
148 
149  TLOG(10) << "get_next_source returning -1";
150  return -1;
151 }
152 
154 {
155  for (auto& source : source_plugins_)
156  {
157  auto& rank = source.first;
158  if (enabled_sources_.count(rank) != 0u)
159  {
160  running_sources_[rank] = true;
161  try
162  {
163  source_threads_[rank] = boost::thread(&FragmentReceiverManager::runReceiver_, this, rank);
164  }
165  catch (const boost::exception& e)
166  {
167  TLOG(TLVL_ERROR) << "Caught boost::exception starting Receiver " << rank << " thread: " << boost::diagnostic_information(e) << ", errno=" << errno;
168  std::cerr << "Caught boost::exception starting Receiver " << rank << " thread: " << boost::diagnostic_information(e) << ", errno=" << errno << std::endl;
169  exit(5);
170  }
171  }
172  }
173 }
174 
175 artdaq::FragmentPtr artdaq::FragmentReceiverManager::recvFragment(int& rank, size_t timeout_usec)
176 {
177  TLOG(5) << "recvFragment entered tmo=" << timeout_usec << " us";
178 
179  if (timeout_usec == 0)
180  {
181  timeout_usec = 1000000;
182  }
183 
184  auto ready = fragments_ready_();
185  size_t waited = 0;
186  auto wait_amount = timeout_usec / 1000 > 1000 ? timeout_usec / 1000 : 1000;
187  TLOG(5) << "recvFragment fragment_ready_=" << ready << " before wait";
188  while (!ready && waited < timeout_usec)
189  {
190  {
191  std::unique_lock<std::mutex> lck(input_cv_mutex_);
192  input_cv_.wait_for(lck, std::chrono::microseconds(wait_amount));
193  }
194  waited += wait_amount;
195  ready = fragments_ready_();
196  if (running_sources().empty())
197  {
198  break;
199  }
200  }
201  TLOG(5) << "recvFragment fragment_ready_=" << ready << " after waited=" << waited;
202  if (!ready)
203  {
204  TLOG(5) << "recvFragment: No fragments ready, returning empty";
206  return std::unique_ptr<Fragment>{};
207  }
208 
209  int current_source = get_next_source_();
210  FragmentPtr current_fragment = fragment_store_[current_source].front();
211  output_cv_.notify_all();
212  rank = current_source;
213 
214  if (current_fragment != nullptr)
215  {
216  TLOG(5) << "recvFragment: Done rank=" << rank << ", fragment size=" << std::to_string(current_fragment->size()) << " words, seqId=" << current_fragment->sequenceID();
217  }
218  return current_fragment;
219 }
220 
222 {
223  std::set<int> output;
224  for (auto& src : running_sources_)
225  {
226  if (src.second)
227  {
228  output.insert(src.first);
229  }
230  }
231  return output;
232 }
233 
235 {
236  std::set<int> output;
237  for (auto& src : enabled_sources_)
238  {
239  if (src.second)
240  {
241  output.insert(src.first);
242  }
243  }
244  return output;
245 }
246 
247 void artdaq::FragmentReceiverManager::runReceiver_(int source_rank)
248 {
249  while (!stop_requested_ && (enabled_sources_.count(source_rank) != 0u))
250  {
251  TLOG(16) << "runReceiver_ " << source_rank << ": Begin loop";
252  auto is_suppressed = suppress_noisy_senders_ && recv_seq_count_.slotCount(source_rank) > suppression_threshold_ + recv_seq_count_.minCount();
253  while (!stop_requested_ && is_suppressed)
254  {
255  TLOG(6) << "runReceiver_: Suppressing receiver rank " << source_rank;
256  if (!is_suppressed)
257  {
258  input_cv_.notify_all();
259  }
260  else
261  {
262  std::unique_lock<std::mutex> lck(output_cv_mutex_);
263  output_cv_.wait_for(lck, std::chrono::seconds(1));
264  }
265  is_suppressed = suppress_noisy_senders_ && recv_seq_count_.slotCount(source_rank) > suppression_threshold_ + recv_seq_count_.minCount();
266  }
267  if (stop_requested_)
268  {
269  running_sources_[source_rank] = false;
270  return;
271  }
272 
273  if (fragment_store_[source_rank].GetEndOfData() <= recv_frag_count_.slotCount(source_rank) && !source_plugins_[source_rank]->isRunning())
274  {
275  TLOG(TLVL_DEBUG) << "runReceiver_: EndOfData conditions satisfied, ending receive loop";
276  running_sources_[source_rank] = false;
277  return;
278  }
279 
280  auto start_time = std::chrono::steady_clock::now();
281  TLOG(16) << "runReceiver_: Calling receiveFragment";
282  auto fragment = std::make_unique<Fragment>();
283 #if 0
284  auto ret = source_plugins_[source_rank]->receiveFragment(*fragment, receive_timeout_);
285  TLOG(16) << "runReceiver_: Done with receiveFragment, ret=" << ret << " (should be " << source_rank << ")";
286  if (ret != source_rank) continue; // Receive timeout or other oddness
287 #else
288  artdaq::detail::RawFragmentHeader hdr;
289  auto ret1 = source_plugins_[source_rank]->receiveFragmentHeader(hdr, receive_timeout_);
290  TLOG(16) << "runReceiver_: Done with receiveFragmentHeader, ret1=" << ret1 << " (should be " << source_rank << ")";
291 
292  if (ret1 != source_rank)
293  {
294  continue; // Receive timeout or other oddness
295  }
296 
297  fragment->resize(hdr.word_count - hdr.num_words());
298  memcpy(fragment->headerAddress(), &hdr, hdr.num_words() * sizeof(artdaq::RawDataType));
299  auto ret2 = source_plugins_[source_rank]->receiveFragmentData(fragment->headerAddress() + hdr.num_words(), hdr.word_count - hdr.num_words()); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
300  if (ret2 != ret1)
301  {
302  TLOG(TLVL_ERROR) << "ReceiveFragmentHeader returned " << ret1 << ", but ReceiveFragmentData returned " << ret2;
303  continue;
304  }
305 #endif
306 
307  if (fragment->type() == artdaq::Fragment::EndOfDataFragmentType)
308  {
309  TLOG(TLVL_TRACE) << "runReceiver_: EndOfData Fragment received!";
310  fragment_store_[source_rank].SetEndOfData(*reinterpret_cast<size_t*>(fragment->dataBegin())); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
311  }
312  else if (fragment->type() == artdaq::Fragment::DataFragmentType || fragment->type() == artdaq::Fragment::ContainerFragmentType || fragment->isUserFragmentType(fragment->type()))
313  {
314  TLOG(TLVL_TRACE) << "runReceiver_: Data Fragment received!";
315  recv_frag_count_.incSlot(source_rank);
316  recv_frag_size_.incSlot(source_rank, fragment->size() * sizeof(RawDataType));
317  recv_seq_count_.setSlot(source_rank, fragment->sequenceID());
318  }
319  else
320  {
321  continue;
322  }
323 
324  auto delta_t = std::chrono::duration_cast<std::chrono::duration<double, std::ratio<1>>>(std::chrono::steady_clock::now() - start_time).count();
325  source_metric_data_[source_rank].first += fragment->size() * sizeof(RawDataType);
326  source_metric_data_[source_rank].second += delta_t;
327 
328  if (metricMan && TimeUtils::GetElapsedTime(source_metric_send_time_[source_rank]) > 1)
329  {
330  TLOG(6) << "runReceiver_: Sending receive stats";
331  metricMan->sendMetric("Data Receive Time From Rank " + std::to_string(source_rank), source_metric_data_[source_rank].second, "s", 1, MetricMode::Accumulate);
332  metricMan->sendMetric("Data Receive Size From Rank " + std::to_string(source_rank), source_metric_data_[source_rank].first, "B", 1, MetricMode::Accumulate);
333  metricMan->sendMetric("Data Receive Rate From Rank " + std::to_string(source_rank), source_metric_data_[source_rank].first / source_metric_data_[source_rank].second, "B/s", 1, MetricMode::Average);
334 
335  source_metric_send_time_[source_rank] = std::chrono::steady_clock::now();
336  source_metric_data_[source_rank].first = 0;
337  source_metric_data_[source_rank].second = 0.0;
338  }
339 
340  fragment_store_[source_rank].emplace_back(std::move(fragment));
341  TLOG(TLVL_TRACE) << "runReceiver_: There are now " << fragment_store_[source_rank].size() << " Fragments stored from this source";
342  input_cv_.notify_all();
343  }
344 
345  running_sources_[source_rank] = false;
346 }
void start_threads()
Start receiver threads for all enabled sources.
std::set< int > enabled_sources() const
Get the list of enabled sources.
virtual ~FragmentReceiverManager()
FragmentReceiverManager Destructor.
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.
FragmentReceiverManager(const fhicl::ParameterSet &ps)
FragmentReceiverManager Constructor.
FragmentPtr recvFragment(int &rank, size_t timeout_usec=0)
Receive a Fragment.
std::set< int > running_sources() const
Get the list of sources which are still receiving data.
Value to be returned upon receive timeout.