00001 #define TRACE_NAME "routing_master_t"
00002 #include "MPIProg.hh"
00003 #include "artdaq/DAQrate/detail/RoutingPacket.hh"
00004 #include "artdaq/DAQdata/TCPConnect.hh"
00005 #include "artdaq-mpich-plugin/Utilities/quiet_mpi.hh"
00006 #include "cetlib/filepath_maker.h"
00007 #include "fhiclcpp/ParameterSet.h"
00008 #include "fhiclcpp/make_ParameterSet.h"
00009
00010 #include <boost/program_options.hpp>
00011 #include "artdaq/Application/RoutingMasterCore.hh"
00012 #include "artdaq/Application/RoutingMasterApp.hh"
00013 #include <netdb.h>
00014 namespace bpo = boost::program_options;
00015
00016 #include <algorithm>
00017 #include <cmath>
00018 #include <cstdio>
00019
00020 extern "C"
00021 {
00022 #include <unistd.h>
00023 }
00024
00025 #include <iostream>
00026 #include <memory>
00027 #include <utility>
00028 #include <arpa/inet.h>
00029 #include <netinet/in.h>
00030 #include <sys/types.h>
00031 #include <sys/socket.h>
00032
00033 extern "C"
00034 {
00035 #include <sys/time.h>
00036 #include <sys/resource.h>
00037 }
00038
00042 class RoutingMasterTest : public MPIProg
00043 {
00044 public:
00059 RoutingMasterTest(int argc, char* argv[]);
00060
00064 void go();
00065
00069 void generate_tokens();
00070
00074 void routing_master();
00075
00079 void table_receiver();
00080
00087 fhicl::ParameterSet getPset(int argc, char* argv[]) const;
00088
00089 private:
00090 enum class TestRole_t : int
00091 {
00092 TOKEN_GEN = 0,
00093 ROUTING_MASTER = 1,
00094 TABLE_RECEIVER = 2
00095 };
00096
00097 void printHost(const std::string& functionName) const;
00098
00099 fhicl::ParameterSet const pset_;
00100 fhicl::ParameterSet const daq_pset_;
00101 TestRole_t role_;
00102
00103 std::string routing_master_address_;
00104 std::string multicast_address_;
00105 int token_port_;
00106 int table_port_;
00107 int ack_port_;
00108 std::vector<int> eb_ranks_;
00109 int token_count_;
00110 size_t token_interval_us_;
00111 };
00112
00113 RoutingMasterTest::RoutingMasterTest(int argc, char* argv[]) :
00114 MPIProg(argc, argv)
00115 , pset_(getPset(argc, argv))
00116 , daq_pset_(pset_.get<fhicl::ParameterSet>("daq"))
00117 , routing_master_address_(daq_pset_.get<std::string>("routing_master_hostname", "localhost"))
00118 , multicast_address_(daq_pset_.get<std::string>("table_update_address", "227.128.12.28"))
00119 , token_port_(daq_pset_.get<int>("routing_token_port", 35555))
00120 , table_port_(daq_pset_.get<int>("table_update_port", 35556))
00121 , ack_port_(daq_pset_.get<int>("table_acknowledge_port", 35557))
00122 , token_count_(pset_.get<int>("token_count", 1000))
00123 , token_interval_us_(pset_.get<size_t>("token_interval_us", 5000))
00124 {
00125 assert(!(my_rank < 0));
00126 switch (my_rank)
00127 {
00128 case 0:
00129 role_ = TestRole_t::TOKEN_GEN;
00130 break;
00131 case 1:
00132 role_ = TestRole_t::ROUTING_MASTER;
00133 break;
00134 default:
00135 role_ = TestRole_t::TABLE_RECEIVER;
00136 break;
00137 }
00138 auto policy_pset = daq_pset_.get<fhicl::ParameterSet>("policy");
00139 eb_ranks_ = policy_pset.get<std::vector<int>>("receiver_ranks");
00140
00141 }
00142
00143 fhicl::ParameterSet RoutingMasterTest::getPset(int argc, char* argv[]) const
00144 {
00145 std::ostringstream descstr;
00146 descstr << "-- <-c <config-file>>";
00147 bpo::options_description desc(descstr.str());
00148 desc.add_options()
00149 ("config,c", bpo::value<std::string>(), "Configuration file.");
00150 bpo::variables_map vm;
00151 try
00152 {
00153 bpo::store(bpo::command_line_parser(argc, argv).
00154 options(desc).allow_unregistered().run(), vm);
00155 bpo::notify(vm);
00156 }
00157 catch (bpo::error const& e)
00158 {
00159 std::cerr << "Exception from command line processing in Config::getArtPset: " << e.what() << "\n";
00160 throw "cmdline parsing error.";
00161 }
00162 if (!vm.count("config"))
00163 {
00164 std::cerr << "Expected \"-- -c <config-file>\" fhicl file specification.\n";
00165 throw "cmdline parsing error.";
00166 }
00167 fhicl::ParameterSet pset;
00168 cet::filepath_lookup lookup_policy("FHICL_FILE_PATH");
00169 fhicl::make_ParameterSet(vm["config"].as<std::string>(), lookup_policy, pset);
00170
00171 return pset;
00172 }
00173
00174 void RoutingMasterTest::go()
00175 {
00176 MPI_Barrier(MPI_COMM_WORLD);
00177
00178
00179 switch (role_)
00180 {
00181 case TestRole_t::TABLE_RECEIVER:
00182 table_receiver();
00183 break;
00184 case TestRole_t::ROUTING_MASTER:
00185 routing_master();
00186 break;
00187 case TestRole_t::TOKEN_GEN:
00188 generate_tokens();
00189 break;
00190 default:
00191 throw "No such node type";
00192 }
00193 TLOG(TLVL_DEBUG) << "Rank " << my_rank << " complete." ;
00194 }
00195
00196 void RoutingMasterTest::generate_tokens()
00197 {
00198 TLOG(TLVL_DEBUG) << "generate_tokens(): Init" ;
00199 printHost("generate_tokens");
00200 sleep(1);
00201
00202 int token_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
00203 if (token_socket < 0)
00204 {
00205 TLOG(TLVL_ERROR) << "generate_tokens(): I failed to create the socket for sending Routing Tokens!" ;
00206 exit(1);
00207 }
00208 struct sockaddr_in token_addr;
00209 auto sts = ResolveHost(routing_master_address_.c_str(), token_port_, token_addr);
00210 if(sts == -1)
00211 {
00212 TLOG(TLVL_ERROR) << "generate_tokens(): Could not resolve host name" ;
00213 }
00214
00215 connect(token_socket, (struct sockaddr*)&token_addr, sizeof(token_addr));
00216
00217 int sent_tokens = 0;
00218 std::map<int, int> token_counter;
00219 for(auto rank : eb_ranks_)
00220 {
00221 token_counter[rank] = 0;
00222 }
00223 while (sent_tokens < token_count_) {
00224 int this_rank = eb_ranks_[seedAndRandom() % eb_ranks_.size()];
00225 token_counter[this_rank]++;
00226 artdaq::detail::RoutingToken token;
00227 token.header = TOKEN_MAGIC;
00228 token.rank = this_rank;
00229 token.new_slots_free = 1;
00230
00231 TLOG(TLVL_DEBUG) << "generate_tokens(): Sending RoutingToken " << std::to_string(++sent_tokens) << " for rank " << this_rank << " to " << routing_master_address_ ;
00232 send(token_socket, &token, sizeof(artdaq::detail::RoutingToken), 0);
00233 usleep(token_interval_us_);
00234 }
00235 auto max_rank = 0;
00236 for(auto rank : token_counter)
00237 {
00238 if (rank.second > max_rank) max_rank = rank.second;
00239 }
00240 for(auto rank : token_counter)
00241 {
00242 artdaq::detail::RoutingToken token;
00243 token.header = TOKEN_MAGIC;
00244 token.rank = rank.first;
00245 token.new_slots_free = max_rank - rank.second;
00246
00247 TLOG(TLVL_DEBUG) << "generate_tokens(): Sending RoutingToken " << std::to_string(++sent_tokens) << " for rank " << rank.first << " to " << routing_master_address_ ;
00248 send(token_socket, &token, sizeof(artdaq::detail::RoutingToken), 0);
00249 usleep(token_interval_us_);
00250
00251 }
00252
00253 TLOG(TLVL_INFO) << "generate_tokens(): Waiting at MPI_Barrier" ;
00254 MPI_Barrier(MPI_COMM_WORLD);
00255 TLOG(TLVL_INFO) << "generate_tokens(): Done with MPI_Barrier" ;
00256 }
00257
00258 void RoutingMasterTest::table_receiver()
00259 {
00260 TLOG(TLVL_DEBUG) << "table_receiver(): Init" ;
00261 printHost("table_receiver");
00262
00263
00264 auto table_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
00265 if (table_socket < 0)
00266 {
00267 TLOG(TLVL_ERROR) << "table_receiver(): Error creating socket for receiving data requests!" ;
00268 exit(1);
00269 }
00270
00271 struct sockaddr_in si_me_request;
00272
00273 int yes = 1;
00274 if (setsockopt(table_socket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0)
00275 {
00276 TLOG(TLVL_ERROR) << "table_receiver(): Unable to enable port reuse on request socket" ;
00277 exit(1);
00278 }
00279 memset(&si_me_request, 0, sizeof(si_me_request));
00280 si_me_request.sin_family = AF_INET;
00281 si_me_request.sin_port = htons(table_port_);
00282 si_me_request.sin_addr.s_addr = htonl(INADDR_ANY);
00283 if (bind(table_socket, (struct sockaddr *)&si_me_request, sizeof(si_me_request)) == -1)
00284 {
00285 TLOG(TLVL_ERROR) << "table_receiver(): Cannot bind request socket to port " << table_port_ ;
00286 exit(1);
00287 }
00288
00289 struct ip_mreq mreq;
00290 long int sts = ResolveHost(multicast_address_.c_str(), mreq.imr_multiaddr);
00291 if(sts == -1)
00292 {
00293 TLOG(TLVL_ERROR) << "table_receiver(): Unable to resolve multicast hostname" ;
00294 exit(1);
00295 }
00296 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
00297 if (setsockopt(table_socket, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0)
00298 {
00299 TLOG(TLVL_ERROR) << "table_receiver(): Unable to join multicast group" ;
00300 exit(1);
00301 }
00302
00303 struct epoll_event ev;
00304 int table_epoll_fd = epoll_create1(0);
00305 ev.events = EPOLLIN | EPOLLPRI;
00306 ev.data.fd = table_socket;
00307 if (epoll_ctl(table_epoll_fd, EPOLL_CTL_ADD, table_socket, &ev) == -1)
00308 {
00309 TLOG(TLVL_ERROR) << "table_receiver(): Could not register listen socket to epoll fd" ;
00310 exit(3);
00311 }
00312
00313 auto ack_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
00314 struct sockaddr_in ack_addr;
00315 sts = ResolveHost(routing_master_address_.c_str(), ack_port_, ack_addr);
00316 if(sts == -1)
00317 {
00318 TLOG(TLVL_ERROR) << "table_receiver(): Unable to resolve routing master hostname" ;
00319 exit(1);
00320 }
00321
00322 if (table_socket == -1 || table_epoll_fd == -1 || ack_socket == -1)
00323 {
00324 TLOG(TLVL_DEBUG) << "table_receiver(): One of the listen sockets was not opened successfully." ;
00325 exit(4);
00326 }
00327 artdaq::Fragment::sequence_id_t max_sequence_id = token_count_;
00328 artdaq::Fragment::sequence_id_t current_sequence_id = 0;
00329 std::map<artdaq::Fragment::sequence_id_t, int> routing_table;
00330 TLOG(TLVL_INFO) << "table_receiver(): Expecting " << std::to_string(max_sequence_id) << " as the last Sequence ID in this run" ;
00331 while (current_sequence_id < max_sequence_id)
00332 {
00333 std::vector<epoll_event> table_events_(4);
00334 TLOG(TLVL_DEBUG) << "table_receiver(): Waiting for event on table socket" ;
00335 auto nfds = epoll_wait(table_epoll_fd, &table_events_[0], table_events_.size(), -1);
00336 if (nfds == -1) {
00337 perror("epoll_wait");
00338 exit(EXIT_FAILURE);
00339 }
00340
00341 TLOG(TLVL_DEBUG) << "table_receiver(): Received " << nfds << " table update(s)" ;
00342 for (auto n = 0; n < nfds; ++n) {
00343 auto first = artdaq::Fragment::InvalidSequenceID;
00344 auto last = artdaq::Fragment::InvalidSequenceID;
00345 artdaq::detail::RoutingPacketHeader hdr;
00346 recv(table_events_[n].data.fd, &hdr, sizeof(artdaq::detail::RoutingPacketHeader), 0);
00347
00348 TLOG(TLVL_DEBUG) << "table_receiver(): Checking for valid header" ;
00349 if (hdr.header == ROUTING_MAGIC) {
00350 artdaq::detail::RoutingPacket buffer(hdr.nEntries);
00351 TLOG(TLVL_DEBUG) << "table_receiver(): Receiving data buffer" ;
00352 sts = recv(table_events_[n].data.fd, &buffer[0], sizeof(artdaq::detail::RoutingPacketEntry) * hdr.nEntries, 0);
00353 assert(static_cast<size_t>(sts) == sizeof(artdaq::detail::RoutingPacketEntry) * hdr.nEntries);
00354
00355 first = buffer[0].sequence_id;
00356 last = buffer[buffer.size() - 1].sequence_id;
00357
00358 for (auto entry : buffer)
00359 {
00360 if (routing_table.count(entry.sequence_id))
00361 {
00362 assert(routing_table[entry.sequence_id] == entry.destination_rank);
00363 continue;
00364 }
00365 routing_table[entry.sequence_id] = entry.destination_rank;
00366 TLOG(TLVL_DEBUG) << "table_receiver(): table_receiver " << std::to_string(my_rank) << ": received update: SeqID " << std::to_string(entry.sequence_id) << " -> Rank " << std::to_string(entry.destination_rank) ;
00367 }
00368
00369 artdaq::detail::RoutingAckPacket ack;
00370 ack.rank = my_rank;
00371 ack.first_sequence_id = first;
00372 ack.last_sequence_id = last;
00373
00374 TLOG(TLVL_DEBUG) << "table_receiver(): Sending RoutingAckPacket with first= " << std::to_string(first) << " and last= " << std::to_string(last) << " to " << routing_master_address_ << ", port " << ack_port_ ;
00375 sendto(ack_socket, &ack, sizeof(artdaq::detail::RoutingAckPacket), 0, (struct sockaddr *)&ack_addr, sizeof(ack_addr));
00376 current_sequence_id = last;
00377 }
00378 }
00379 }
00380
00381 TLOG(TLVL_INFO) << "table_receiver(): Waiting at MPI_Barrier" ;
00382 MPI_Barrier(MPI_COMM_WORLD);
00383 TLOG(TLVL_INFO) << "table_receiver(): Done with MPI_Barrier" ;
00384 }
00385
00386 void RoutingMasterTest::routing_master()
00387 {
00388 TLOG(TLVL_DEBUG) << "routing_master: Init" ;
00389 printHost("routing_master");
00390
00391 app_name = "RoutingMaster";
00392
00393 auto app = std::make_unique<artdaq::RoutingMasterApp>();
00394
00395 app->initialize(pset_, 0, 0);
00396 app->do_start(art::RunID(1), 0, 0);
00397 TLOG(TLVL_INFO) << "routing_master: Waiting at MPI_Barrier" ;
00398 MPI_Barrier(MPI_COMM_WORLD);
00399 TLOG(TLVL_INFO) << "routing_master: Done with MPI_Barrier, calling RoutingMasterCore::stop" ;
00400 app->do_stop(0, 0);
00401 TLOG(TLVL_INFO) << "routing_master: Done with RoutingMasterCore::stop, calling shutdown" ;
00402 app->do_shutdown(0);
00403 TLOG(TLVL_INFO) << "routing_master: Done with RoutingMasterCore::shutdown" ;
00404 }
00405
00406 void RoutingMasterTest::printHost(const std::string& functionName) const
00407 {
00408 char* doPrint = getenv("PRINT_HOST");
00409 if (doPrint == 0) { return; }
00410 const int ARRSIZE = 80;
00411 char hostname[ARRSIZE];
00412 std::string hostString;
00413 if (!gethostname(hostname, ARRSIZE))
00414 {
00415 hostString = hostname;
00416 }
00417 else
00418 {
00419 hostString = "unknown";
00420 }
00421 TLOG(TLVL_DEBUG) << "Running " << functionName
00422 << " on host " << hostString
00423 << " with rank " << my_rank << "."
00424 ;
00425 }
00426
00427 void printUsage()
00428 {
00429 int myid = 0;
00430 struct rusage usage;
00431 getrusage(RUSAGE_SELF, &usage);
00432 std::cout << myid << ":"
00433 << " user=" << artdaq::TimeUtils::convertUnixTimeToSeconds(usage.ru_utime)
00434 << " sys=" << artdaq::TimeUtils::convertUnixTimeToSeconds(usage.ru_stime)
00435 << std::endl;
00436 }
00437
00438 int main(int argc, char* argv[])
00439 {
00440 artdaq::configureMessageFacility("routing_master", false);
00441 int rc = 1;
00442 try
00443 {
00444 RoutingMasterTest p(argc, argv);
00445 std::cerr << "Started process " << my_rank << " of " << p.procs_ << ".\n";
00446 p.go();
00447 rc = 0;
00448 }
00449 catch (std::string& x)
00450 {
00451 std::cerr << "Exception (type string) caught in routing_master: "
00452 << x
00453 << '\n';
00454 return 1;
00455 }
00456 catch (char const* m)
00457 {
00458 std::cerr << "Exception (type char const*) caught in routing_master: ";
00459 if (m)
00460 {
00461 std::cerr << m;
00462 }
00463 else
00464 {
00465 std::cerr << "[the value was a null pointer, so no message is available]";
00466 }
00467 std::cerr << '\n';
00468 }
00469 return rc;
00470 }