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