artdaq  v3_00_03
RoutingMasterPolicy.cc
1 #include "artdaq/Application/Routing/RoutingMasterPolicy.hh"
2 #include "fhiclcpp/ParameterSet.h"
3 
5  : next_sequence_id_(0)
6  , tokens_()
7  , max_token_count_(0)
8 {
9  auto receiver_ranks = ps.get<std::vector<int>>("receiver_ranks");
10  receiver_ranks_.insert(receiver_ranks.begin(), receiver_ranks.end());
11 }
12 
13 void artdaq::RoutingMasterPolicy::AddReceiverToken(int rank, unsigned new_slots_free)
14 {
15  if (!receiver_ranks_.count(rank)) return;
16  TLOG_ARB(10, "RoutingMasterPolicy") << "AddReceiverToken BEGIN" << TLOG_ENDL;
17  std::unique_lock<std::mutex> lk(tokens_mutex_);
18  if (new_slots_free == 1)
19  {
20  tokens_.push_back(rank);
21  }
22  else
23  {
24  // Randomly distribute multitokens through the token list
25  // Only used at start run time, so we can take the performance hit
26  for (unsigned i = 0; i < new_slots_free; ++i)
27  {
28  auto it = tokens_.begin();
29  if(tokens_.size()) std::advance(it, rand() % tokens_.size());
30  tokens_.insert(it, rank);
31  }
32  }
33  if (tokens_.size() > max_token_count_) max_token_count_ = tokens_.size();
34  TLOG_ARB(10, "RoutingMasterPolicy") << "AddReceiverToken END" << TLOG_ENDL;
35 }
36 
37 std::unique_ptr<std::deque<int>> artdaq::RoutingMasterPolicy::getTokensSnapshot()
38 {
39  TLOG_ARB(10, "RoutingMasterPolicy" ) << "getTokensSnapshot BEGIN" << TLOG_ENDL;
40  std::unique_lock<std::mutex> lk(tokens_mutex_);
41  auto out = std::make_unique<std::deque<int>>(tokens_);
42  tokens_.clear();
43  TLOG_ARB(10, "RoutingMasterPolicy") << "getTokensSnapshot END" << TLOG_ENDL;
44  return out;
45 }
46 
47 void artdaq::RoutingMasterPolicy::addUnusedTokens(std::unique_ptr<std::deque<int>> tokens)
48 {
49  std::unique_lock<std::mutex> lk(tokens_mutex_);
50  for (auto token = tokens.get()->rbegin(); token != tokens.get()->rend(); ++token)
51  {
52  tokens_.push_front(*token);
53  }
54  if (tokens_.size() > max_token_count_) max_token_count_ = tokens_.size();
55 }
void AddReceiverToken(int rank, unsigned new_slots_free)
Add a token to the token list.
std::unique_ptr< std::deque< int > > getTokensSnapshot()
Gets the current token list, used for building Routing Tables.
void addUnusedTokens(std::unique_ptr< std::deque< int >> tokens)
If necessary, return unused tokens to the token list, for subsequent updates.
RoutingMasterPolicy(fhicl::ParameterSet ps)
RoutingMasterPolicy Constructor.