artdaq  v3_06_00
RoutingMasterCore.cc
1 #include <arpa/inet.h>
2 #include <netdb.h>
3 #include <pthread.h>
4 #include <sched.h>
5 #include <sys/epoll.h>
6 #include <sys/time.h>
7 #include <sys/un.h>
8 #include <algorithm>
9 
10 #include "canvas/Utilities/Exception.h"
11 #include "cetlib_except/exception.h"
12 
13 #include "artdaq/DAQdata/Globals.hh" // include these 2 first to get tracemf.h -
14 #define TRACE_NAME (app_name + "_RoutingMasterCore").c_str() // before trace.h
15 #include "artdaq-core/Data/Fragment.hh"
16 #include "artdaq-core/Utilities/ExceptionHandler.hh"
17 
18 #include "artdaq/Application/RoutingMasterCore.hh"
21 #include "artdaq/RoutingPolicies/makeRoutingMasterPolicy.hh"
22 
23 const std::string artdaq::RoutingMasterCore::
24  TABLE_UPDATES_STAT_KEY("RoutingMasterCoreTableUpdates");
25 const std::string artdaq::RoutingMasterCore::
26  TOKENS_RECEIVED_STAT_KEY("RoutingMasterCoreTokensReceived");
27 
29  : shutdown_requested_(false)
30  , stop_requested_(true)
31  , pause_requested_(false)
32  , statsHelperPtr_(new artdaq::StatisticsHelper())
33  , table_socket_(-1)
34  , ack_socket_(-1)
35 {
36  TLOG(TLVL_DEBUG) << "Constructor";
37  statsHelperPtr_->addMonitoredQuantityName(TABLE_UPDATES_STAT_KEY);
38  statsHelperPtr_->addMonitoredQuantityName(TOKENS_RECEIVED_STAT_KEY);
39 }
40 
42 {
43  TLOG(TLVL_DEBUG) << "Destructor";
44  artdaq::StatisticsCollection::getInstance().requestStop();
45  token_receiver_->stopTokenReception(true);
46 }
47 
48 bool artdaq::RoutingMasterCore::initialize(fhicl::ParameterSet const& pset, uint64_t, uint64_t)
49 {
50  TLOG(TLVL_DEBUG) << "initialize method called with "
51  << "ParameterSet = \"" << pset.to_string()
52  << "\".";
53 
54  // pull out the relevant parts of the ParameterSet
55  fhicl::ParameterSet daq_pset;
56  try
57  {
58  daq_pset = pset.get<fhicl::ParameterSet>("daq");
59  }
60  catch (...)
61  {
62  TLOG(TLVL_ERROR)
63  << "Unable to find the DAQ parameters in the initialization "
64  << "ParameterSet: \"" + pset.to_string() + "\".";
65  return false;
66  }
67 
68  if (daq_pset.has_key("rank"))
69  {
70  if (my_rank >= 0 && daq_pset.get<int>("rank") != my_rank)
71  {
72  TLOG(TLVL_WARNING) << "Routing Master rank specified at startup is different than rank specified at configure! Using rank received at configure!";
73  }
74  my_rank = daq_pset.get<int>("rank");
75  }
76  if (my_rank == -1)
77  {
78  TLOG(TLVL_ERROR) << "Routing Master rank not specified at startup or in configuration! Aborting";
79  exit(1);
80  }
81 
82  try
83  {
84  policy_pset_ = daq_pset.get<fhicl::ParameterSet>("policy");
85  }
86  catch (...)
87  {
88  TLOG(TLVL_ERROR)
89  << "Unable to find the policy parameters in the DAQ initialization ParameterSet: \"" + daq_pset.to_string() + "\".";
90  return false;
91  }
92 
93  try
94  {
95  token_receiver_pset_ = daq_pset.get<fhicl::ParameterSet>("token_receiver");
96  }
97  catch (...)
98  {
99  TLOG(TLVL_ERROR)
100  << "Unable to find the token_receiver parameters in the DAQ initialization ParameterSet: \"" + daq_pset.to_string() + "\".";
101  return false;
102  }
103 
104  // pull out the Metric part of the ParameterSet
105  fhicl::ParameterSet metric_pset;
106  try
107  {
108  metric_pset = daq_pset.get<fhicl::ParameterSet>("metrics");
109  }
110  catch (...)
111  {} // OK if there's no metrics table defined in the FHiCL
112 
113  if (metric_pset.is_empty())
114  {
115  TLOG(TLVL_INFO) << "No metric plugins appear to be defined";
116  }
117  try
118  {
119  metricMan->initialize(metric_pset, app_name);
120  }
121  catch (...)
122  {
123  ExceptionHandler(ExceptionHandlerRethrow::no,
124  "Error loading metrics in RoutingMasterCore::initialize()");
125  }
126 
127  // create the requested RoutingPolicy
128  auto policy_plugin_spec = policy_pset_.get<std::string>("policy", "");
129  if (policy_plugin_spec.length() == 0)
130  {
131  TLOG(TLVL_ERROR)
132  << "No fragment generator (parameter name = \"policy\") was "
133  << "specified in the policy ParameterSet. The "
134  << "DAQ initialization PSet was \"" << daq_pset.to_string() << "\".";
135  return false;
136  }
137  try
138  {
139  policy_ = artdaq::makeRoutingMasterPolicy(policy_plugin_spec, policy_pset_);
140  }
141  catch (...)
142  {
143  std::stringstream exception_string;
144  exception_string << "Exception thrown during initialization of policy of type \""
145  << policy_plugin_spec << "\"";
146 
147  ExceptionHandler(ExceptionHandlerRethrow::no, exception_string.str());
148 
149  TLOG(TLVL_DEBUG) << "FHiCL parameter set used to initialize the policy which threw an exception: " << policy_pset_.to_string();
150 
151  return false;
152  }
153 
154  rt_priority_ = daq_pset.get<int>("rt_priority", 0);
155  sender_ranks_ = daq_pset.get<std::vector<int>>("sender_ranks");
156 
157  receive_ack_events_ = std::vector<epoll_event>(sender_ranks_.size());
158 
159  auto mode = daq_pset.get<bool>("senders_send_by_send_count", false);
161  max_table_update_interval_ms_ = daq_pset.get<size_t>("table_update_interval_ms", 1000);
162  current_table_interval_ms_ = max_table_update_interval_ms_;
163  max_ack_cycle_count_ = daq_pset.get<size_t>("table_ack_retry_count", 5);
164  send_tables_port_ = daq_pset.get<int>("table_update_port", 35556);
165  receive_acks_port_ = daq_pset.get<int>("table_acknowledge_port", 35557);
166  send_tables_address_ = daq_pset.get<std::string>("table_update_address", "227.128.12.28");
167  multicast_out_hostname_ = daq_pset.get<std::string>("routing_master_hostname", "localhost");
168 
169  // fetch the monitoring parameters and create the MonitoredQuantity instances
170  statsHelperPtr_->createCollectors(daq_pset, 100, 30.0, 60.0, TABLE_UPDATES_STAT_KEY);
171 
172  // create the requested TokenReceiver
173  token_receiver_.reset(new TokenReceiver(token_receiver_pset_, policy_, routing_mode_, sender_ranks_.size(), max_table_update_interval_ms_));
174  token_receiver_->setStatsHelper(statsHelperPtr_, TOKENS_RECEIVED_STAT_KEY);
175  token_receiver_->startTokenReception();
176  token_receiver_->pauseTokenReception();
177 
178  shutdown_requested_.store(false);
179  return true;
180 }
181 
182 bool artdaq::RoutingMasterCore::start(art::RunID id, uint64_t, uint64_t)
183 {
184  run_id_ = id;
185  stop_requested_.store(false);
186  pause_requested_.store(false);
187 
188  statsHelperPtr_->resetStatistics();
189 
190  metricMan->do_start();
191  table_update_count_ = 0;
192  token_receiver_->setRunNumber(run_id_.run());
193  token_receiver_->resumeTokenReception();
194 
195  TLOG(TLVL_INFO) << "Started run " << run_id_.run();
196  return true;
197 }
198 
199 bool artdaq::RoutingMasterCore::stop(uint64_t, uint64_t)
200 {
201  TLOG(TLVL_INFO) << "Stopping run " << run_id_.run()
202  << " after " << table_update_count_ << " table updates."
203  << " and " << token_receiver_->getReceivedTokenCount() << " received tokens.";
204  stop_requested_.store(true);
205  token_receiver_->pauseTokenReception();
206  run_id_ = art::RunID::flushRun();
207  return true;
208 }
209 
210 bool artdaq::RoutingMasterCore::pause(uint64_t, uint64_t)
211 {
212  TLOG(TLVL_INFO) << "Pausing run " << run_id_.run()
213  << " after " << table_update_count_ << " table updates."
214  << " and " << token_receiver_->getReceivedTokenCount() << " received tokens.";
215  pause_requested_.store(true);
216  return true;
217 }
218 
219 bool artdaq::RoutingMasterCore::resume(uint64_t, uint64_t)
220 {
221  TLOG(TLVL_DEBUG) << "Resuming run " << run_id_.run();
222  pause_requested_.store(false);
223  metricMan->do_start();
224  return true;
225 }
226 
228 {
229  shutdown_requested_.store(true);
230  token_receiver_->stopTokenReception();
231  policy_.reset();
232  metricMan->shutdown();
233  return true;
234 }
235 
236 bool artdaq::RoutingMasterCore::soft_initialize(fhicl::ParameterSet const& pset, uint64_t e, uint64_t f)
237 {
238  TLOG(TLVL_INFO) << "soft_initialize method called with "
239  << "ParameterSet = \"" << pset.to_string()
240  << "\".";
241  return initialize(pset, e, f);
242 }
243 
244 bool artdaq::RoutingMasterCore::reinitialize(fhicl::ParameterSet const& pset, uint64_t e, uint64_t f)
245 {
246  TLOG(TLVL_INFO) << "reinitialize method called with "
247  << "ParameterSet = \"" << pset.to_string()
248  << "\".";
249  return initialize(pset, e, f);
250 }
251 
253 {
254  if (rt_priority_ > 0)
255  {
256 #pragma GCC diagnostic push
257 #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
258  sched_param s_param = {};
259  s_param.sched_priority = rt_priority_;
260  if (pthread_setschedparam(pthread_self(), SCHED_RR, &s_param))
261  TLOG(TLVL_WARNING) << "setting realtime priority failed";
262 #pragma GCC diagnostic pop
263  }
264 
265  // try-catch block here?
266 
267  // how to turn RT PRI off?
268  if (rt_priority_ > 0)
269  {
270 #pragma GCC diagnostic push
271 #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
272  sched_param s_param = {};
273  s_param.sched_priority = rt_priority_;
274  int status = pthread_setschedparam(pthread_self(), SCHED_RR, &s_param);
275  if (status != 0)
276  {
277  TLOG(TLVL_ERROR)
278  << "Failed to set realtime priority to " << rt_priority_
279  << ", return code = " << status;
280  }
281 #pragma GCC diagnostic pop
282  }
283 
284  //MPI_Barrier(local_group_comm_);
285 
286  TLOG(TLVL_DEBUG) << "Sending initial table.";
287  auto startTime = artdaq::MonitoredQuantity::getCurrentTime();
288  auto nextSendTime = startTime;
289  double delta_time;
290  while (!stop_requested_ && !pause_requested_)
291  {
292  startTime = artdaq::MonitoredQuantity::getCurrentTime();
293 
294  if (startTime >= nextSendTime)
295  {
296  auto table = policy_->GetCurrentTable();
297  if (table.size() > 0)
298  {
299  send_event_table(table);
300  ++table_update_count_;
301  delta_time = artdaq::MonitoredQuantity::getCurrentTime() - startTime;
302  statsHelperPtr_->addSample(TABLE_UPDATES_STAT_KEY, delta_time);
303  TLOG(16) << "process_fragments TABLE_UPDATES_STAT_KEY=" << delta_time;
304 
305  bool readyToReport = statsHelperPtr_->readyToReport();
306  if (readyToReport)
307  {
308  std::string statString = buildStatisticsString_();
309  TLOG(TLVL_INFO) << statString;
310  sendMetrics_();
311  }
312  }
313  else
314  {
315  TLOG(TLVL_TRACE) << "No tokens received in this update interval (" << current_table_interval_ms_ << " ms)! This most likely means that the receivers are not keeping up!";
316  }
317  auto max_tokens = policy_->GetMaxNumberOfTokens();
318  if (max_tokens > 0)
319  {
320  auto frac = table.size() / static_cast<double>(max_tokens);
321  if (frac > 0.75) current_table_interval_ms_ = 9 * current_table_interval_ms_ / 10;
322  if (frac < 0.5) current_table_interval_ms_ = 11 * current_table_interval_ms_ / 10;
323  if (current_table_interval_ms_ > max_table_update_interval_ms_) current_table_interval_ms_ = max_table_update_interval_ms_;
324  if (current_table_interval_ms_ < 1) current_table_interval_ms_ = 1;
325  }
326  nextSendTime = startTime + current_table_interval_ms_ / 1000.0;
327  TLOG(TLVL_TRACE) << "current_table_interval_ms is now " << current_table_interval_ms_;
328  }
329  else
330  {
331  usleep(current_table_interval_ms_ * 10); // 1/100 of the table update interval
332  }
333  }
334 
335  policy_->Reset();
336  metricMan->do_stop();
337 }
338 
340 {
341  // Reconnect table socket, if necessary
342  if (table_socket_ == -1)
343  {
344  table_socket_ = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
345  if (table_socket_ < 0)
346  {
347  TLOG(TLVL_ERROR) << "I failed to create the socket for sending Data Requests! Errno: " << errno;
348  exit(1);
349  }
350  auto sts = ResolveHost(send_tables_address_.c_str(), send_tables_port_, send_tables_addr_);
351  if (sts == -1)
352  {
353  TLOG(TLVL_ERROR) << "Unable to resolve table_update_address";
354  exit(1);
355  }
356 
357  auto yes = 1;
358  if (multicast_out_hostname_ != "localhost")
359  {
360  TLOG(TLVL_DEBUG) << "Making sure that multicast sending uses the correct interface for hostname " << multicast_out_hostname_;
361  struct in_addr addr;
362  sts = ResolveHost(multicast_out_hostname_.c_str(), addr);
363  if (sts == -1)
364  {
365  throw art::Exception(art::errors::Configuration) << "RoutingMasterCore: Unable to resolve routing_master_address" << std::endl;
366  ;
367  }
368 
369  if (setsockopt(table_socket_, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0)
370  {
371  throw art::Exception(art::errors::Configuration) << "RoutingMasterCore: Unable to enable port reuse on table update socket" << std::endl;
372  exit(1);
373  }
374 
375  if (setsockopt(table_socket_, IPPROTO_IP, IP_MULTICAST_LOOP, &yes, sizeof(yes)) < 0)
376  {
377  TLOG(TLVL_ERROR) << "Unable to enable multicast loopback on table socket";
378  exit(1);
379  }
380  if (setsockopt(table_socket_, IPPROTO_IP, IP_MULTICAST_IF, &addr, sizeof(addr)) == -1)
381  {
382  TLOG(TLVL_ERROR) << "Cannot set outgoing interface. Errno: " << errno;
383  exit(1);
384  }
385  }
386  if (setsockopt(table_socket_, SOL_SOCKET, SO_BROADCAST, (void*)&yes, sizeof(int)) == -1)
387  {
388  TLOG(TLVL_ERROR) << "Cannot set request socket to broadcast. Errno: " << errno;
389  exit(1);
390  }
391  }
392 
393  // Reconnect ack socket, if necessary
394  if (ack_socket_ == -1)
395  {
396  ack_socket_ = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
397  if (ack_socket_ < 0)
398  {
399  throw art::Exception(art::errors::Configuration) << "RoutingMasterCore: Error creating socket for receiving table update acks!" << std::endl;
400  exit(1);
401  }
402 
403  struct sockaddr_in si_me_request;
404 
405  auto yes = 1;
406  if (setsockopt(ack_socket_, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0)
407  {
408  throw art::Exception(art::errors::Configuration) << "RoutingMasterCore: Unable to enable port reuse on ack socket" << std::endl;
409  exit(1);
410  }
411 
412  // 10-Apr-2019, KAB: debugging information about the size of the receive buffer
413  int sts;
414  int len = 0;
415  socklen_t arglen = sizeof(len);
416  sts = getsockopt(ack_socket_, SOL_SOCKET, SO_RCVBUF, &len, &arglen);
417  TLOG(TLVL_INFO) << "ACK RCVBUF initial: " << len << " sts/errno=" << sts << "/" << errno << " arglen=" << arglen;
418 
419  memset(&si_me_request, 0, sizeof(si_me_request));
420  si_me_request.sin_family = AF_INET;
421  si_me_request.sin_port = htons(receive_acks_port_);
422  si_me_request.sin_addr.s_addr = htonl(INADDR_ANY);
423  if (bind(ack_socket_, reinterpret_cast<struct sockaddr*>(&si_me_request), sizeof(si_me_request)) == -1)
424  {
425  throw art::Exception(art::errors::Configuration) << "RoutingMasterCore: Cannot bind request socket to port " << receive_acks_port_ << std::endl;
426  exit(1);
427  }
428  TLOG(TLVL_DEBUG) << "Listening for acks on 0.0.0.0 port " << receive_acks_port_;
429  }
430 
431  auto acks = std::unordered_map<int, bool>();
432  for (auto& r : sender_ranks_)
433  {
434  acks[r] = false;
435  }
436  auto counter = 0U;
437  auto start_time = std::chrono::steady_clock::now();
438  while (std::count_if(acks.begin(), acks.end(), [](std::pair<int, bool> p) { return !p.second; }) > 0 && !stop_requested_)
439  {
440  // Send table update
441  auto header = detail::RoutingPacketHeader(routing_mode_, packet.size());
442  auto packetSize = sizeof(detail::RoutingPacketEntry) * packet.size();
443 
444  // 10-Apr-2019, KAB: added information on which senders have already acknowledged this update
445  for (auto ackIter = acks.begin(); ackIter != acks.end(); ++ackIter)
446  {
447  TLOG(27) << "Table update already acknowledged? rank " << ackIter->first << " is " << ackIter->second
448  << " (size of 'already_acknowledged_ranks bitset is " << (8 * sizeof(header.already_acknowledged_ranks)) << ")";
449  if (ackIter->first < static_cast<int>(8 * sizeof(header.already_acknowledged_ranks)))
450  {
451  if (ackIter->second) { header.already_acknowledged_ranks.set(ackIter->first); }
452  }
453  }
454 
455  assert(packetSize + sizeof(header) < MAX_ROUTING_TABLE_SIZE);
456  std::vector<uint8_t> buffer(packetSize + sizeof(header));
457  memcpy(&buffer[0], &header, sizeof(detail::RoutingPacketHeader));
458  memcpy(&buffer[sizeof(detail::RoutingPacketHeader)], &packet[0], packetSize);
459 
460  TLOG(TLVL_DEBUG) << "Sending table information for " << header.nEntries << " events to multicast group " << send_tables_address_ << ", port " << send_tables_port_ << ", outgoing interface " << multicast_out_hostname_;
461  TRACE(16, "headerData:0x%016lx%016lx packetData:0x%016lx%016lx", ((unsigned long*)&header)[0], ((unsigned long*)&header)[1], ((unsigned long*)&packet[0])[0], ((unsigned long*)&packet[0])[1]);
462  auto sts = sendto(table_socket_, &buffer[0], buffer.size(), 0, reinterpret_cast<struct sockaddr*>(&send_tables_addr_), sizeof(send_tables_addr_));
463  if (sts != static_cast<ssize_t>(buffer.size()))
464  {
465  TLOG(TLVL_ERROR) << "Error sending routing table. sts=" << sts;
466  }
467 
468  // Collect acks
469 
470  auto first = packet[0].sequence_id;
471  auto last = packet.rbegin()->sequence_id;
472  TLOG(TLVL_DEBUG) << "Sent " << sts << " bytes. Expecting acks to have first= " << first << ", and last= " << last;
473 
474  auto startTime = std::chrono::steady_clock::now();
475  while (std::count_if(acks.begin(), acks.end(), [](std::pair<int, bool> p) { return !p.second; }) > 0)
476  {
477  auto table_ack_wait_time_ms = current_table_interval_ms_ / max_ack_cycle_count_;
478  if (TimeUtils::GetElapsedTimeMilliseconds(startTime) > table_ack_wait_time_ms)
479  {
480  if (++counter > max_ack_cycle_count_ && table_update_count_ > 0)
481  {
482  TLOG(TLVL_WARNING) << "Did not receive acks from all senders after resending table " << counter
483  << " times during the table_update_interval. Check the status of the senders!";
484  }
485  else
486  {
487  TLOG(TLVL_WARNING) << "Did not receive acks from all senders within the timeout (" << table_ack_wait_time_ms << " ms). Resending table update";
488  }
489 
490  if (std::count_if(acks.begin(), acks.end(), [](std::pair<int, bool> p) { return !p.second; }) <= 3)
491  {
492  auto ackIter = acks.begin();
493  while (ackIter != acks.end())
494  {
495  if (!ackIter->second)
496  {
497  TLOG(TLVL_TRACE) << "Did not receive ack from rank " << ackIter->first;
498  }
499  ++ackIter;
500  }
501  }
502  break;
503  }
504 
505  TLOG(20) << "send_event_table: Polling Request socket for new requests";
506  auto ready = true;
507  while (ready)
508  {
510  if (recvfrom(ack_socket_, &buffer, sizeof(detail::RoutingAckPacket), MSG_DONTWAIT, NULL, NULL) < 0)
511  {
512  if (errno == EWOULDBLOCK || errno == EAGAIN)
513  {
514  TLOG(20) << "send_event_table: No more ack datagrams on ack socket.";
515  ready = false;
516  }
517  else
518  {
519  TLOG(TLVL_ERROR) << "An unexpected error occurred during ack packet receive";
520  exit(2);
521  }
522  }
523  else
524  {
525  TLOG(TLVL_DEBUG) << "Ack packet from rank " << buffer.rank << " has first= " << buffer.first_sequence_id
526  << " and last= " << buffer.last_sequence_id << ", packet_size=" << sizeof(detail::RoutingAckPacket);
527  if (acks.count(buffer.rank) && buffer.first_sequence_id == first && buffer.last_sequence_id == last)
528  {
529  TLOG(TLVL_DEBUG) << "Received table update acknowledgement from sender with rank " << buffer.rank << ".";
530  acks[buffer.rank] = true;
531  TLOG(TLVL_DEBUG) << "There are now " << std::count_if(acks.begin(), acks.end(), [](std::pair<int, bool> p) { return !p.second; })
532  << " acks outstanding";
533  }
534  else
535  {
536  if (!acks.count(buffer.rank))
537  {
538  TLOG(TLVL_ERROR) << "Received acknowledgement from invalid rank " << buffer.rank << "!"
539  << " Cross-talk between RoutingMasters means there's a configuration error!";
540  }
541  else
542  {
543  TLOG(TLVL_WARNING) << "Received acknowledgement from rank " << buffer.rank
544  << " that had incorrect sequence ID information. Discarding."
545  << " Expected first/last=" << first << "/" << last
546  << " recvd=" << buffer.first_sequence_id << "/" << buffer.last_sequence_id;
547  }
548  }
549  }
550  }
551  usleep(table_ack_wait_time_ms * 1000 / 10);
552  }
553  }
554  if (metricMan)
555  {
556  artdaq::TimeUtils::seconds delta = std::chrono::steady_clock::now() - start_time;
557  metricMan->sendMetric("Avg Table Acknowledge Time", delta.count(), "seconds", 3, MetricMode::Average);
558  }
559 }
560 
561 std::string artdaq::RoutingMasterCore::report(std::string const&) const
562 {
563  std::string resultString;
564 
565  // if we haven't been able to come up with any report so far, say so
566  auto tmpString = app_name + " run number = " + std::to_string(run_id_.run()) + ", table updates sent = " + std::to_string(table_update_count_) + ", Receiver tokens received = " + std::to_string(token_receiver_->getReceivedTokenCount());
567  return tmpString;
568 }
569 
570 std::string artdaq::RoutingMasterCore::buildStatisticsString_() const
571 {
572  std::ostringstream oss;
573  oss << app_name << " statistics:" << std::endl;
574 
575  auto mqPtr = artdaq::StatisticsCollection::getInstance().getMonitoredQuantity(TABLE_UPDATES_STAT_KEY);
576  if (mqPtr.get() != nullptr)
577  {
578  artdaq::MonitoredQuantityStats stats;
579  mqPtr->getStats(stats);
580  oss << " Table Update statistics: "
581  << stats.recentSampleCount << " table updates sent at "
582  << stats.recentSampleRate << " table updates/sec, , monitor window = "
583  << stats.recentDuration << " sec" << std::endl;
584  oss << " Average times per table update: ";
585  if (stats.recentSampleRate > 0.0)
586  {
587  oss << " elapsed time = "
588  << (1.0 / stats.recentSampleRate) << " sec";
589  }
590  oss << ", avg table acknowledgement wait time = "
591  << (mqPtr->getRecentValueSum() / sender_ranks_.size()) << " sec" << std::endl;
592  }
593 
594  mqPtr = artdaq::StatisticsCollection::getInstance().getMonitoredQuantity(TOKENS_RECEIVED_STAT_KEY);
595  if (mqPtr.get() != nullptr)
596  {
597  artdaq::MonitoredQuantityStats stats;
598  mqPtr->getStats(stats);
599  oss << " Received Token statistics: "
600  << stats.recentSampleCount << " tokens received at "
601  << stats.recentSampleRate << " tokens/sec, , monitor window = "
602  << stats.recentDuration << " sec" << std::endl;
603  oss << " Average times per token: ";
604  if (stats.recentSampleRate > 0.0)
605  {
606  oss << " elapsed time = "
607  << (1.0 / stats.recentSampleRate) << " sec";
608  }
609  oss << ", input token wait time = "
610  << mqPtr->getRecentValueSum() << " sec" << std::endl;
611  }
612 
613  return oss.str();
614 }
615 
616 void artdaq::RoutingMasterCore::sendMetrics_()
617 {
618  if (metricMan)
619  {
620  auto mqPtr = artdaq::StatisticsCollection::getInstance().getMonitoredQuantity(TABLE_UPDATES_STAT_KEY);
621  if (mqPtr.get() != nullptr)
622  {
623  artdaq::MonitoredQuantityStats stats;
624  mqPtr->getStats(stats);
625  metricMan->sendMetric("Table Update Count", static_cast<unsigned long>(stats.fullSampleCount), "updates", 1, MetricMode::LastPoint);
626  metricMan->sendMetric("Table Update Rate", stats.recentSampleRate, "updates/sec", 1, MetricMode::Average);
627  metricMan->sendMetric("Average Sender Acknowledgement Time", (mqPtr->getRecentValueSum() / sender_ranks_.size()), "seconds", 3, MetricMode::Average);
628  }
629 
630  mqPtr = artdaq::StatisticsCollection::getInstance().getMonitoredQuantity(TOKENS_RECEIVED_STAT_KEY);
631  if (mqPtr.get() != nullptr)
632  {
633  artdaq::MonitoredQuantityStats stats;
634  mqPtr->getStats(stats);
635  metricMan->sendMetric("Receiver Token Count", static_cast<unsigned long>(stats.fullSampleCount), "updates", 1, MetricMode::LastPoint);
636  metricMan->sendMetric("Receiver Token Rate", stats.recentSampleRate, "updates/sec", 1, MetricMode::Average);
637  metricMan->sendMetric("Total Receiver Token Wait Time", mqPtr->getRecentValueSum(), "seconds", 3, MetricMode::Average);
638  }
639  }
640 }
This class manages MonitoredQuantity instances for the *Core classes.
bool resume(uint64_t, uint64_t)
Resumes the RoutingMasterCore.
int ResolveHost(char const *host_in, in_addr &addr)
Convert a string hostname to a in_addr suitable for socket communication.
Definition: TCPConnect.cc:33
bool soft_initialize(fhicl::ParameterSet const &pset, uint64_t timeout, uint64_t timestamp)
Soft-Initializes the RoutingMasterCore.
static const std::string TOKENS_RECEIVED_STAT_KEY
Key for the Tokens Received MonitoredQuantity.
A row of the Routing Table.
Receives event builder &quot;free buffer&quot; tokens and adds them to a specified RoutingPolicy.
bool reinitialize(fhicl::ParameterSet const &pset, uint64_t timeout, uint64_t timestamp)
Reinitializes the RoutingMasterCore.
bool start(art::RunID id, uint64_t, uint64_t)
Start the RoutingMasterCore.
A RoutingAckPacket contains the rank of the table receiver, plus the first and last sequence IDs in t...
RoutingMasterCore()
RoutingMasterCore Constructor.
Events should be routed by sequence ID (BR -&gt; EB)
bool initialize(fhicl::ParameterSet const &pset, uint64_t, uint64_t)
Processes the initialize request.
std::shared_ptr< RoutingMasterPolicy > makeRoutingMasterPolicy(std::string const &policy_plugin_spec, fhicl::ParameterSet const &ps)
Load a RoutingMasterPolicy plugin.
Fragment::sequence_id_t first_sequence_id
The first sequence ID in the received RoutingPacket.
bool pause(uint64_t, uint64_t)
Pauses the RoutingMasterCore.
The header of the Routing Table, containing the magic bytes and the number of entries.
Fragment::sequence_id_t last_sequence_id
The last sequence ID in the received RoutingPacket.
std::vector< RoutingPacketEntry > RoutingPacket
A RoutingPacket is simply a vector of RoutingPacketEntry objects. It is not suitable for network tran...
bool stop(uint64_t, uint64_t)
Stops the RoutingMasterCore.
int rank
The rank from which the RoutingAckPacket came.
std::string report(std::string const &) const
Send a report on the current status of the RoutingMasterCore.
static const std::string TABLE_UPDATES_STAT_KEY
Key for Table Update count MonnitoredQuantity.
bool shutdown(uint64_t)
Shuts Down the RoutingMasterCore.
void process_event_table()
Main loop of the RoutingMasterCore. Determines when to send the next table update, asks the RoutingMasterPolicy for the table to send, and sends it.
void send_event_table(detail::RoutingPacket table)
Sends a detail::RoutingPacket to the table receivers.
Events should be routed by send count (EB -&gt; Agg)