artdaq_mpich_plugin  v1_00_06
routing_master_t.cc
1 #define TRACE_NAME "routing_master_t"
2 #include "MPIProg.hh"
3 #include "artdaq/DAQrate/detail/RoutingPacket.hh"
6 #include "cetlib/filepath_maker.h"
7 #include "fhiclcpp/ParameterSet.h"
8 #include "fhiclcpp/make_ParameterSet.h"
9 
10 #include <boost/program_options.hpp>
11 #include "artdaq/Application/RoutingMasterCore.hh"
12 #include "artdaq/Application/RoutingMasterApp.hh"
13 #include <netdb.h>
14 namespace bpo = boost::program_options;
15 
16 #include <algorithm>
17 #include <cmath>
18 #include <cstdio>
19 
20 extern "C"
21 {
22 #include <unistd.h>
23 }
24 
25 #include <iostream>
26 #include <memory>
27 #include <utility>
28 #include <arpa/inet.h>
29 #include <netinet/in.h>
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 
33 extern "C"
34 {
35 #include <sys/time.h>
36 #include <sys/resource.h>
37 }
38 
42 class RoutingMasterTest : public MPIProg
43 {
44 public:
59  RoutingMasterTest(int argc, char* argv[]);
60 
64  void go();
65 
69  void generate_tokens();
70 
74  void routing_master();
75 
79  void table_receiver();
80 
87  fhicl::ParameterSet getPset(int argc, char* argv[]) const;
88 
89 private:
90  enum class TestRole_t : int
91  {
92  TOKEN_GEN = 0,
93  ROUTING_MASTER = 1,
94  TABLE_RECEIVER = 2
95  };
96 
97  void printHost(const std::string& functionName) const;
98 
99  fhicl::ParameterSet const pset_;
100  fhicl::ParameterSet const daq_pset_;
101  TestRole_t role_;
102 
103  std::string routing_master_address_;
104  std::string multicast_address_;
105  int token_port_;
106  int table_port_;
107  int ack_port_;
108  std::vector<int> eb_ranks_;
109  int token_count_;
110  size_t token_interval_us_;
111 };
112 
113 RoutingMasterTest::RoutingMasterTest(int argc, char* argv[]) :
114  MPIProg(argc, argv)
115  , pset_(getPset(argc, argv))
116  , daq_pset_(pset_.get<fhicl::ParameterSet>("daq"))
117  , routing_master_address_(daq_pset_.get<std::string>("routing_master_hostname", "localhost"))
118  , multicast_address_(daq_pset_.get<std::string>("table_update_address", "227.128.12.28"))
119  , token_port_(daq_pset_.get<int>("routing_token_port", 35555))
120  , table_port_(daq_pset_.get<int>("table_update_port", 35556))
121  , ack_port_(daq_pset_.get<int>("table_acknowledge_port", 35557))
122  , token_count_(pset_.get<int>("token_count", 1000))
123  , token_interval_us_(pset_.get<size_t>("token_interval_us", 5000))
124 {
125  assert(!(my_rank < 0));
126  switch (my_rank)
127  {
128  case 0:
129  role_ = TestRole_t::TOKEN_GEN;
130  break;
131  case 1:
132  role_ = TestRole_t::ROUTING_MASTER;
133  break;
134  default:
135  role_ = TestRole_t::TABLE_RECEIVER;
136  break;
137  }
138  auto policy_pset = daq_pset_.get<fhicl::ParameterSet>("policy");
139  eb_ranks_ = policy_pset.get<std::vector<int>>("receiver_ranks");
140 }
141 
142 fhicl::ParameterSet RoutingMasterTest::getPset(int argc, char* argv[]) const
143 {
144  std::ostringstream descstr;
145  descstr << "-- <-c <config-file>>";
146  bpo::options_description desc(descstr.str());
147  desc.add_options()
148  ("config,c", bpo::value<std::string>(), "Configuration file.");
149  bpo::variables_map vm;
150  try
151  {
152  bpo::store(bpo::command_line_parser(argc, argv).
153  options(desc).allow_unregistered().run(), vm);
154  bpo::notify(vm);
155  }
156  catch (bpo::error const& e)
157  {
158  std::cerr << "Exception from command line processing in Config::getArtPset: " << e.what() << "\n";
159  throw "cmdline parsing error.";
160  }
161  if (!vm.count("config"))
162  {
163  std::cerr << "Expected \"-- -c <config-file>\" fhicl file specification.\n";
164  throw "cmdline parsing error.";
165  }
166  fhicl::ParameterSet pset;
167  cet::filepath_lookup lookup_policy("FHICL_FILE_PATH");
168  fhicl::make_ParameterSet(vm["config"].as<std::string>(), lookup_policy, pset);
169 
170  return pset;
171 }
172 
174 {
175  TLOG(TLVL_INFO) << "Entering MPI_Barrier";
176  MPI_Barrier(MPI_COMM_WORLD);
177  TLOG(TLVL_INFO) << "Done with Barrier";
178  //std::cout << "daq_pset_: " << daq_pset_.to_string() << std::endl << "conf_.makeParameterSet(): " << conf_.makeParameterSet().to_string() << std::endl;
179 
180  switch (role_)
181  {
182  case TestRole_t::TABLE_RECEIVER:
183  table_receiver();
184  break;
185  case TestRole_t::ROUTING_MASTER:
186  routing_master();
187  break;
188  case TestRole_t::TOKEN_GEN:
189  generate_tokens();
190  break;
191  default:
192  throw "No such node type";
193  }
194  TLOG(TLVL_INFO) << "Rank " << my_rank << " complete." ;
195 }
196 
198 {
199  TLOG(TLVL_INFO) << "generate_tokens(): Init" ;
200  printHost("generate_tokens");
201  sleep(1);
202 
203  int token_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
204  if (token_socket < 0)
205  {
206  TLOG(TLVL_ERROR) << "generate_tokens(): I failed to create the socket for sending Routing Tokens!" ;
207  exit(1);
208  }
209  struct sockaddr_in token_addr;
210  auto sts = ResolveHost(routing_master_address_.c_str(), token_port_, token_addr);
211  if(sts == -1)
212  {
213  TLOG(TLVL_ERROR) << "generate_tokens(): Could not resolve host name" ;
214  }
215 
216  connect(token_socket, (struct sockaddr*)&token_addr, sizeof(token_addr));
217 
218  int sent_tokens = 0;
219  std::map<int, int> token_counter;
220  for(auto rank : eb_ranks_)
221  {
222  token_counter[rank] = 0;
223  }
224  while (sent_tokens < token_count_) {
225  int this_rank = eb_ranks_[seedAndRandom() % eb_ranks_.size()];
226  token_counter[this_rank]++;
228  token.header = TOKEN_MAGIC;
229  token.rank = this_rank;
230  token.new_slots_free = 1;
231  token.run_number = 1;
232 
233  TLOG(TLVL_INFO) << "generate_tokens(): Sending RoutingToken " << ++sent_tokens << " for rank " << this_rank << " to " << routing_master_address_ ;
234  send(token_socket, &token, sizeof(artdaq::detail::RoutingToken), 0);
235  usleep(token_interval_us_);
236  }
237  auto max_rank = 0;
238  for(auto rank : token_counter)
239  {
240  if (rank.second > max_rank) max_rank = rank.second;
241  }
242  for(auto rank : token_counter)
243  {
245  token.header = TOKEN_MAGIC;
246  token.rank = rank.first;
247  token.new_slots_free = max_rank - rank.second;
248  token.run_number = 1;
249 
250  TLOG(TLVL_INFO) << "generate_tokens(): Sending RoutingToken " << ++sent_tokens << " for rank " << rank.first << " to " << routing_master_address_ ;
251  send(token_socket, &token, sizeof(artdaq::detail::RoutingToken), 0);
252  usleep(token_interval_us_);
253 
254  }
255 
256  TLOG(TLVL_INFO) << "generate_tokens(): Waiting at MPI_Barrier" ;
257  MPI_Barrier(MPI_COMM_WORLD);
258  TLOG(TLVL_INFO) << "generate_tokens(): Done with MPI_Barrier" ;
259 }
260 
262 {
263  TLOG(TLVL_INFO) << "table_receiver(): Init" ;
264  printHost("table_receiver");
265 
266 
267  auto table_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
268  if (table_socket < 0)
269  {
270  TLOG(TLVL_ERROR) << "table_receiver(): Error creating socket for receiving data requests!" ;
271  exit(1);
272  }
273 
274  struct sockaddr_in si_me_request;
275 
276  int yes = 1;
277  if (setsockopt(table_socket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0)
278  {
279  TLOG(TLVL_ERROR) << "table_receiver(): Unable to enable port reuse on request socket" ;
280  exit(1);
281  }
282  memset(&si_me_request, 0, sizeof(si_me_request));
283  si_me_request.sin_family = AF_INET;
284  si_me_request.sin_port = htons(table_port_);
285  si_me_request.sin_addr.s_addr = htonl(INADDR_ANY);
286  if (bind(table_socket, (struct sockaddr *)&si_me_request, sizeof(si_me_request)) == -1)
287  {
288  TLOG(TLVL_ERROR) << "table_receiver(): Cannot bind request socket to port " << table_port_ ;
289  exit(1);
290  }
291 
292  struct ip_mreq mreq;
293  long int sts = ResolveHost(multicast_address_.c_str(), mreq.imr_multiaddr);
294  if(sts == -1)
295  {
296  TLOG(TLVL_ERROR) << "table_receiver(): Unable to resolve multicast hostname" ;
297  exit(1);
298  }
299  mreq.imr_interface.s_addr = htonl(INADDR_ANY);
300  if (setsockopt(table_socket, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0)
301  {
302  TLOG(TLVL_ERROR) << "table_receiver(): Unable to join multicast group" ;
303  exit(1);
304  }
305 
306  struct epoll_event ev;
307  int table_epoll_fd = epoll_create1(0);
308  ev.events = EPOLLIN | EPOLLPRI;
309  ev.data.fd = table_socket;
310  if (epoll_ctl(table_epoll_fd, EPOLL_CTL_ADD, table_socket, &ev) == -1)
311  {
312  TLOG(TLVL_ERROR) << "table_receiver(): Could not register listen socket to epoll fd" ;
313  exit(3);
314  }
315 
316  auto ack_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
317  struct sockaddr_in ack_addr;
318  sts = ResolveHost(routing_master_address_.c_str(), ack_port_, ack_addr);
319  if(sts == -1)
320  {
321  TLOG(TLVL_ERROR) << "table_receiver(): Unable to resolve routing master hostname" ;
322  exit(1);
323  }
324 
325  if (table_socket == -1 || table_epoll_fd == -1 || ack_socket == -1)
326  {
327  TLOG(TLVL_INFO) << "table_receiver(): One of the listen sockets was not opened successfully." ;
328  exit(4);
329  }
330  artdaq::Fragment::sequence_id_t max_sequence_id = token_count_;
331  artdaq::Fragment::sequence_id_t current_sequence_id = 0;
332  std::map<artdaq::Fragment::sequence_id_t, int> routing_table;
333  TLOG(TLVL_INFO) << "table_receiver(): Expecting " << max_sequence_id << " as the last Sequence ID in this run" ;
334  while (current_sequence_id < max_sequence_id)
335  {
336  std::vector<epoll_event> table_events_(4);
337  TLOG(TLVL_INFO) << "table_receiver(): Waiting for event on table socket" ;
338  auto nfds = epoll_wait(table_epoll_fd, &table_events_[0], table_events_.size(), -1);
339  if (nfds == -1) {
340  perror("epoll_wait");
341  exit(EXIT_FAILURE);
342  }
343 
344  TLOG(TLVL_INFO) << "table_receiver(): Received " << nfds << " table update(s)" ;
345  for (auto n = 0; n < nfds; ++n) {
348 
349  std::vector<uint8_t> buf(MAX_ROUTING_TABLE_SIZE);
351  auto stss = recv(table_events_[n].data.fd, &buf[0], MAX_ROUTING_TABLE_SIZE, 0);
352 
353 
354  if (stss > static_cast<ssize_t>(sizeof(hdr)))
355  {
356  memcpy(&hdr, &buf[0], sizeof(hdr));
357  }
358  else
359  {
360  TLOG(TLVL_TRACE) << __func__ << ": Incorrect size received. Discarding.";
361  continue;
362  }
363 
364  TLOG(TLVL_INFO) << "table_receiver(): Checking for valid header" ;
365  if (hdr.header == ROUTING_MAGIC) {
366  artdaq::detail::RoutingPacket buffer(hdr.nEntries);
367  TLOG(TLVL_INFO) << "table_receiver(): Receiving data buffer" ;
368  memcpy(&buffer[0], &buf[sizeof(artdaq::detail::RoutingPacketHeader)], sizeof(artdaq::detail::RoutingPacketEntry) * hdr.nEntries);
369 
370  first = buffer[0].sequence_id;
371  last = buffer[buffer.size() - 1].sequence_id;
372 
373  for (auto entry : buffer)
374  {
375  if (routing_table.count(entry.sequence_id))
376  {
377  assert(routing_table[entry.sequence_id] == entry.destination_rank);
378  continue;
379  }
380  routing_table[entry.sequence_id] = entry.destination_rank;
381  TLOG(TLVL_INFO) << "table_receiver(): table_receiver " << my_rank << ": received update: SeqID " << entry.sequence_id << " -> Rank " << entry.destination_rank ;
382  }
383 
385  ack.rank = my_rank;
386  ack.first_sequence_id = first;
387  ack.last_sequence_id = last;
388 
389  TLOG(TLVL_INFO) << "table_receiver(): Sending RoutingAckPacket with first= " << first << " and last= " << last << " to " << routing_master_address_ << ", port " << ack_port_ ;
390  sendto(ack_socket, &ack, sizeof(artdaq::detail::RoutingAckPacket), 0, (struct sockaddr *)&ack_addr, sizeof(ack_addr));
391  current_sequence_id = last;
392  }
393  }
394  }
395 
396  TLOG(TLVL_INFO) << "table_receiver(): Waiting at MPI_Barrier" ;
397  MPI_Barrier(MPI_COMM_WORLD);
398  TLOG(TLVL_INFO) << "table_receiver(): Done with MPI_Barrier" ;
399 }
400 
402 {
403  TLOG(TLVL_INFO) << "routing_master: Init" ;
404  printHost("routing_master");
405 
406  app_name = "RoutingMaster";
407 
408  auto app = std::make_unique<artdaq::RoutingMasterApp>();
409 
410  app->initialize(pset_, 0, 0);
411  app->do_start(art::RunID(1), 0, 0);
412  TLOG(TLVL_INFO) << "routing_master: Waiting at MPI_Barrier" ;
413  MPI_Barrier(MPI_COMM_WORLD);
414  TLOG(TLVL_INFO) << "routing_master: Done with MPI_Barrier, calling RoutingMasterCore::stop" ;
415  app->do_stop(0, 0);
416  TLOG(TLVL_INFO) << "routing_master: Done with RoutingMasterCore::stop, calling shutdown" ;
417  app->do_shutdown(0);
418  TLOG(TLVL_INFO) << "routing_master: Done with RoutingMasterCore::shutdown" ;
419 }
420 
421 void RoutingMasterTest::printHost(const std::string& functionName) const
422 {
423  char* doPrint = getenv("PRINT_HOST");
424  if (doPrint == 0) { return; }
425  const int ARRSIZE = 80;
426  char hostname[ARRSIZE];
427  std::string hostString;
428  if (!gethostname(hostname, ARRSIZE))
429  {
430  hostString = hostname;
431  }
432  else
433  {
434  hostString = "unknown";
435  }
436  TLOG(TLVL_INFO) << "Running " << functionName
437  << " on host " << hostString
438  << " with rank " << my_rank << "."
439  ;
440 }
441 
442 void printUsage()
443 {
444  int myid = 0;
445  struct rusage usage;
446  getrusage(RUSAGE_SELF, &usage);
447  std::cout << myid << ":"
448  << " user=" << artdaq::TimeUtils::convertUnixTimeToSeconds(usage.ru_utime)
449  << " sys=" << artdaq::TimeUtils::convertUnixTimeToSeconds(usage.ru_stime)
450  << std::endl;
451 }
452 
453 int main(int argc, char* argv[])
454 {
455  artdaq::configureMessageFacility("routing_master", true);
456  int rc = 1;
457 #if 0
458  std::cerr << "PID: " << getpid() << std::endl;
459  volatile bool attach = true;
460  while (attach)
461  {
462  usleep(100000);
463  }
464 #endif
465 
466  try
467  {
468  RoutingMasterTest p(argc, argv);
469  std::cerr << "Started process " << my_rank << " of " << p.procs_ << ".\n";
470  p.go();
471  rc = 0;
472  }
473  catch (std::string& x)
474  {
475  std::cerr << "Exception (type string) caught in routing_master: "
476  << x
477  << '\n';
478  return 1;
479  }
480  catch (char const* m)
481  {
482  std::cerr << "Exception (type char const*) caught in routing_master: ";
483  if (m)
484  {
485  std::cerr << m;
486  }
487  else
488  {
489  std::cerr << "[the value was a null pointer, so no message is available]";
490  }
491  std::cerr << '\n';
492  }
493  return rc;
494 }
The RoutingMasterTest class runs the routing_master test.
int ResolveHost(char const *host_in, in_addr &addr)
void routing_master()
Load a RoutingMasterCore instance, receive tokens from the token generators, and send table updates t...
A wrapper for a MPI program. Similar to MPISentry.
Definition: MPIProg.hh:10
void go()
Start the test, using the role assigned.
static constexpr sequence_id_t InvalidSequenceID
RoutingMasterTest(int argc, char *argv[])
RoutingMasterTest Constructor.
void table_receiver()
Receive Routing Tables from the Routing Master and send acknowledgement packets back.
Fragment::sequence_id_t first_sequence_id
double convertUnixTimeToSeconds(time_t inputUnixTime)
fhicl::ParameterSet getPset(int argc, char *argv[]) const
Parse the command line arguments and load a configuration FHiCL file.
Fragment::sequence_id_t last_sequence_id
void configureMessageFacility(char const *progname, bool useConsole=true, bool printDebug=false)
void generate_tokens()
Generate tokens and send them to the Routing Master.
detail::RawFragmentHeader::sequence_id_t sequence_id_t