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