1 #define TRACE_NAME "routing_master_t"
4 #include "artdaq/DAQdata/TCPConnect.hh"
5 #include "artdaq/DAQrate/detail/RoutingPacket.hh"
6 #include "cetlib/filepath_maker.h"
7 #include "fhiclcpp/ParameterSet.h"
8 #include "fhiclcpp/make_ParameterSet.h"
11 #include <boost/program_options.hpp>
12 #include "artdaq/Application/RoutingMasterApp.hh"
13 #include "artdaq/Application/RoutingMasterCore.hh"
14 namespace bpo = boost::program_options;
24 #include <arpa/inet.h>
25 #include <netinet/in.h>
26 #include <sys/socket.h>
27 #include <sys/types.h>
33 #include <sys/resource.h>
86 fhicl::ParameterSet
getPset(
int argc,
char* argv[])
const;
89 enum class TestRole_t : int
96 void printHost(
const std::string& functionName)
const;
98 fhicl::ParameterSet
const pset_;
99 fhicl::ParameterSet
const daq_pset_;
102 std::string routing_master_address_;
103 std::string multicast_address_;
107 std::vector<int> eb_ranks_;
109 size_t token_interval_us_;
114 :
MPIProg(argc, argv), pset_(getPset(argc, argv)), daq_pset_(pset_.get<fhicl::ParameterSet>(
"daq")), routing_master_address_(daq_pset_.get<std::string>(
"routing_master_hostname",
"localhost")), multicast_address_(daq_pset_.get<std::string>(
"table_update_address",
"227.128.12.28")), token_port_(daq_pset_.get<int>(
"routing_token_port", 35555)), table_port_(daq_pset_.get<int>(
"table_update_port", 35556)), ack_port_(daq_pset_.get<int>(
"table_acknowledge_port", 35557)), token_count_(pset_.get<int>(
"token_count", 1000)), token_interval_us_(pset_.get<size_t>(
"token_interval_us", 5000)), run_number_(pset_.get<size_t>(
"run_number"))
116 assert(!(my_rank < 0));
120 role_ = TestRole_t::TOKEN_GEN;
123 role_ = TestRole_t::ROUTING_MASTER;
126 role_ = TestRole_t::TABLE_RECEIVER;
129 auto policy_pset = daq_pset_.get<fhicl::ParameterSet>(
"policy");
130 eb_ranks_ = policy_pset.get<std::vector<int>>(
"receiver_ranks");
135 std::ostringstream descstr;
136 descstr <<
"-- <-c <config-file>>";
137 bpo::options_description desc(descstr.str());
138 desc.add_options()(
"config,c", bpo::value<std::string>(),
"Configuration file.");
139 bpo::variables_map vm;
142 bpo::store(bpo::command_line_parser(argc, argv).options(desc).allow_unregistered().run(), vm);
145 catch (bpo::error
const& e)
147 std::cerr <<
"Exception from command line processing in Config::getArtPset: " << e.what() <<
"\n";
148 throw "cmdline parsing error.";
150 if (!vm.count(
"config"))
152 std::cerr <<
"Expected \"-- -c <config-file>\" fhicl file specification.\n";
153 throw "cmdline parsing error.";
155 fhicl::ParameterSet pset;
156 cet::filepath_lookup lookup_policy(
"FHICL_FILE_PATH");
157 fhicl::make_ParameterSet(vm[
"config"].as<std::string>(), lookup_policy, pset);
164 TLOG(TLVL_INFO) <<
"Entering MPI_Barrier";
165 MPI_Barrier(MPI_COMM_WORLD);
166 TLOG(TLVL_INFO) <<
"Done with Barrier";
172 case TestRole_t::TABLE_RECEIVER:
175 case TestRole_t::ROUTING_MASTER:
178 case TestRole_t::TOKEN_GEN:
182 throw "No such node type";
184 TLOG(TLVL_INFO) <<
"Rank " << my_rank <<
" complete.";
189 TLOG(TLVL_INFO) <<
"generate_tokens(): Init";
190 printHost(
"generate_tokens");
193 int token_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
194 if (token_socket < 0)
196 TLOG(TLVL_ERROR) <<
"generate_tokens(): I failed to create the socket for sending Routing Tokens!";
199 struct sockaddr_in token_addr;
200 auto sts = ResolveHost(routing_master_address_.c_str(), token_port_, token_addr);
203 TLOG(TLVL_ERROR) <<
"generate_tokens(): Could not resolve host name";
206 connect(token_socket, (
struct sockaddr*)&token_addr,
sizeof(token_addr));
209 std::map<int, int> token_counter;
210 for (
auto rank : eb_ranks_)
212 token_counter[rank] = 0;
214 while (sent_tokens < token_count_)
216 int this_rank = eb_ranks_[seedAndRandom() % eb_ranks_.size()];
217 token_counter[this_rank]++;
218 artdaq::detail::RoutingToken token;
219 token.header = TOKEN_MAGIC;
220 token.rank = this_rank;
221 token.new_slots_free = 1;
222 token.run_number = run_number_;
224 TLOG(TLVL_INFO) <<
"generate_tokens(): Sending RoutingToken " << ++sent_tokens <<
" for rank " << this_rank
225 <<
" to " << routing_master_address_;
226 send(token_socket, &token,
sizeof(artdaq::detail::RoutingToken), 0);
227 usleep(token_interval_us_);
230 for (
auto rank : token_counter)
232 if (rank.second > max_rank) max_rank = rank.second;
234 for (
auto rank : token_counter)
236 artdaq::detail::RoutingToken token;
237 token.header = TOKEN_MAGIC;
238 token.rank = rank.first;
239 token.new_slots_free = max_rank - rank.second;
240 token.run_number = run_number_;
242 TLOG(TLVL_INFO) <<
"generate_tokens(): Sending RoutingToken " << ++sent_tokens <<
" for rank " << rank.first
243 <<
" to " << routing_master_address_;
244 send(token_socket, &token,
sizeof(artdaq::detail::RoutingToken), 0);
245 usleep(token_interval_us_);
248 TLOG(TLVL_INFO) <<
"generate_tokens(): Waiting at MPI_Barrier";
249 MPI_Barrier(MPI_COMM_WORLD);
250 TLOG(TLVL_INFO) <<
"generate_tokens(): Done with MPI_Barrier";
255 TLOG(TLVL_INFO) <<
"table_receiver(): Init";
256 printHost(
"table_receiver");
258 auto table_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
259 if (table_socket < 0)
261 TLOG(TLVL_ERROR) <<
"table_receiver(): Error creating socket for receiving data requests!";
265 struct sockaddr_in si_me_request;
268 if (setsockopt(table_socket, SOL_SOCKET, SO_REUSEADDR, &yes,
sizeof(yes)) < 0)
270 TLOG(TLVL_ERROR) <<
"table_receiver(): Unable to enable port reuse on request socket";
273 memset(&si_me_request, 0,
sizeof(si_me_request));
274 si_me_request.sin_family = AF_INET;
275 si_me_request.sin_port = htons(table_port_);
276 si_me_request.sin_addr.s_addr = htonl(INADDR_ANY);
277 if (bind(table_socket, (
struct sockaddr*)&si_me_request,
sizeof(si_me_request)) == -1)
279 TLOG(TLVL_ERROR) <<
"table_receiver(): Cannot bind request socket to port " << table_port_;
284 long int sts = ResolveHost(multicast_address_.c_str(), mreq.imr_multiaddr);
287 TLOG(TLVL_ERROR) <<
"table_receiver(): Unable to resolve multicast hostname";
290 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
291 if (setsockopt(table_socket, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq,
sizeof(mreq)) < 0)
293 TLOG(TLVL_ERROR) <<
"table_receiver(): Unable to join multicast group";
297 struct epoll_event ev;
298 int table_epoll_fd = epoll_create1(0);
299 ev.events = EPOLLIN | EPOLLPRI;
300 ev.data.fd = table_socket;
301 if (epoll_ctl(table_epoll_fd, EPOLL_CTL_ADD, table_socket, &ev) == -1)
303 TLOG(TLVL_ERROR) <<
"table_receiver(): Could not register listen socket to epoll fd";
307 auto ack_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
308 struct sockaddr_in ack_addr;
309 sts = ResolveHost(routing_master_address_.c_str(), ack_port_, ack_addr);
312 TLOG(TLVL_ERROR) <<
"table_receiver(): Unable to resolve routing master hostname";
316 if (table_socket == -1 || table_epoll_fd == -1 || ack_socket == -1)
318 TLOG(TLVL_INFO) <<
"table_receiver(): One of the listen sockets was not opened successfully.";
321 artdaq::Fragment::sequence_id_t max_sequence_id = token_count_;
322 artdaq::Fragment::sequence_id_t current_sequence_id = 0;
323 std::map<artdaq::Fragment::sequence_id_t, int> routing_table;
324 TLOG(TLVL_INFO) <<
"table_receiver(): Expecting " << max_sequence_id <<
" as the last Sequence ID in this run";
325 while (current_sequence_id < max_sequence_id)
327 std::vector<epoll_event> table_events_(4);
328 TLOG(TLVL_INFO) <<
"table_receiver(): Waiting for event on table socket";
329 auto nfds = epoll_wait(table_epoll_fd, &table_events_[0], table_events_.size(), -1);
332 perror(
"epoll_wait");
336 TLOG(TLVL_INFO) <<
"table_receiver(): Received " << nfds <<
" table update(s)";
337 for (
auto n = 0; n < nfds; ++n)
339 auto first = artdaq::Fragment::InvalidSequenceID;
340 auto last = artdaq::Fragment::InvalidSequenceID;
342 std::vector<uint8_t> buf(MAX_ROUTING_TABLE_SIZE);
343 artdaq::detail::RoutingPacketHeader hdr;
344 auto stss = recv(table_events_[n].data.fd, &buf[0], MAX_ROUTING_TABLE_SIZE, 0);
346 if (stss > static_cast<ssize_t>(
sizeof(hdr)))
348 memcpy(&hdr, &buf[0],
sizeof(hdr));
352 TLOG(TLVL_TRACE) << __func__ <<
": Incorrect size received. Discarding.";
356 TLOG(TLVL_INFO) <<
"table_receiver(): Checking for valid header";
357 if (hdr.header == ROUTING_MAGIC)
359 artdaq::detail::RoutingPacket buffer(hdr.nEntries);
360 TLOG(TLVL_INFO) <<
"table_receiver(): Receiving data buffer";
361 memcpy(&buffer[0], &buf[
sizeof(artdaq::detail::RoutingPacketHeader)],
362 sizeof(artdaq::detail::RoutingPacketEntry) * hdr.nEntries);
364 first = buffer[0].sequence_id;
365 last = buffer[buffer.size() - 1].sequence_id;
367 for (
auto entry : buffer)
369 if (routing_table.count(entry.sequence_id))
371 assert(routing_table[entry.sequence_id] == entry.destination_rank);
374 routing_table[entry.sequence_id] = entry.destination_rank;
375 TLOG(TLVL_INFO) <<
"table_receiver(): table_receiver " << my_rank <<
": received update: SeqID "
376 << entry.sequence_id <<
" -> Rank " << entry.destination_rank;
379 artdaq::detail::RoutingAckPacket ack;
381 ack.first_sequence_id = first;
382 ack.last_sequence_id = last;
384 TLOG(TLVL_INFO) <<
"table_receiver(): Sending RoutingAckPacket with first= " << first <<
" and last= " << last
385 <<
" to " << routing_master_address_ <<
", port " << ack_port_;
386 sendto(ack_socket, &ack,
sizeof(artdaq::detail::RoutingAckPacket), 0, (
struct sockaddr*)&ack_addr,
388 current_sequence_id = last;
393 TLOG(TLVL_INFO) <<
"table_receiver(): Waiting at MPI_Barrier";
394 MPI_Barrier(MPI_COMM_WORLD);
395 TLOG(TLVL_INFO) <<
"table_receiver(): Done with MPI_Barrier";
400 TLOG(TLVL_INFO) <<
"routing_master: Init";
401 printHost(
"routing_master");
403 app_name =
"RoutingMaster";
405 auto app = std::make_unique<artdaq::RoutingMasterApp>();
407 auto sts = app->initialize(pset_, 0, 0);
410 TLOG(TLVL_ERROR) <<
"routing_master: Failed to initalize!";
412 app->do_start(art::RunID(run_number_), 0, 0);
413 TLOG(TLVL_INFO) <<
"routing_master: Waiting at MPI_Barrier";
414 MPI_Barrier(MPI_COMM_WORLD);
415 TLOG(TLVL_INFO) <<
"routing_master: Done with MPI_Barrier, calling RoutingMasterCore::stop";
417 TLOG(TLVL_INFO) <<
"routing_master: Done with RoutingMasterCore::stop, calling shutdown";
419 TLOG(TLVL_INFO) <<
"routing_master: Done with RoutingMasterCore::shutdown";
422 void RoutingMasterTest::printHost(
const std::string& functionName)
const
424 char* doPrint = getenv(
"PRINT_HOST");
429 const int ARRSIZE = 80;
430 char hostname[ARRSIZE];
431 std::string hostString;
432 if (!gethostname(hostname, ARRSIZE))
434 hostString = hostname;
438 hostString =
"unknown";
440 TLOG(TLVL_INFO) <<
"Running " << functionName <<
" on host " << hostString <<
" with rank " << my_rank <<
".";
447 getrusage(RUSAGE_SELF, &usage);
448 std::cout << myid <<
":"
449 <<
" user=" << artdaq::TimeUtils::convertUnixTimeToSeconds(usage.ru_utime)
450 <<
" sys=" << artdaq::TimeUtils::convertUnixTimeToSeconds(usage.ru_stime) << std::endl;
453 int main(
int argc,
char* argv[])
455 artdaq::configureMessageFacility(
"routing_master",
true);
459 std::cerr <<
"PID: " << getpid() << std::endl;
460 volatile bool attach =
true;
470 std::cerr <<
"Started process " << my_rank <<
" of " << p.procs_ <<
".\n";
474 catch (std::string& x)
476 std::cerr <<
"Exception (type string) caught in routing_master: " << x <<
'\n';
479 catch (
char const* m)
481 std::cerr <<
"Exception (type char const*) caught in routing_master: ";
488 std::cerr <<
"[the value was a null pointer, so no message is available]";
The RoutingMasterTest class runs the routing_master test.
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.
void go()
Start the test, using the role assigned.
RoutingMasterTest(int argc, char *argv[])
RoutingMasterTest Constructor.
void table_receiver()
Receive Routing Tables from the Routing Master and send acknowledgement packets back.
fhicl::ParameterSet getPset(int argc, char *argv[]) const
Parse the command line arguments and load a configuration FHiCL file.
void generate_tokens()
Generate tokens and send them to the Routing Master.