artdaq  v3_06_00
RoutingMasterPolicy.cc
1 #define TRACE_NAME "RoutingMasterPolicy"
2 
3 #include "artdaq/RoutingPolicies/RoutingMasterPolicy.hh"
4 #include "fhiclcpp/ParameterSet.h"
5 
7  : next_sequence_id_(1)
8  , tokens_()
9  , max_token_count_(0)
10 {
11  auto receiver_ranks = ps.get<std::vector<int>>("receiver_ranks");
12  receiver_ranks_.insert(receiver_ranks.begin(), receiver_ranks.end());
13 }
14 
15 void artdaq::RoutingMasterPolicy::AddReceiverToken(int rank, unsigned new_slots_free)
16 {
17  if (!receiver_ranks_.count(rank)) return;
18  TLOG(10) << "AddReceiverToken BEGIN" ;
19  std::unique_lock<std::mutex> lk(tokens_mutex_);
20  if (new_slots_free == 1)
21  {
22  tokens_.push_back(rank);
23  }
24  else
25  {
26  // Randomly distribute multitokens through the token list
27  // Only used at start run time, so we can take the performance hit
28  for (unsigned i = 0; i < new_slots_free; ++i)
29  {
30  auto it = tokens_.begin();
31  if(tokens_.size()) std::advance(it, rand() % tokens_.size());
32  tokens_.insert(it, rank);
33  }
34  }
35  if (tokens_.size() > max_token_count_) max_token_count_ = tokens_.size();
36  TLOG(10) << "AddReceiverToken END" ;
37 }
38 
39 std::unique_ptr<std::deque<int>> artdaq::RoutingMasterPolicy::getTokensSnapshot()
40 {
41  TLOG(10) << "getTokensSnapshot BEGIN" ;
42  std::unique_lock<std::mutex> lk(tokens_mutex_);
43  auto out = std::make_unique<std::deque<int>>(tokens_);
44  tokens_.clear();
45  TLOG(10) << "getTokensSnapshot END" ;
46  return out;
47 }
48 
49 void artdaq::RoutingMasterPolicy::addUnusedTokens(std::unique_ptr<std::deque<int>> tokens)
50 {
51  std::unique_lock<std::mutex> lk(tokens_mutex_);
52  for (auto token = tokens.get()->rbegin(); token != tokens.get()->rend(); ++token)
53  {
54  tokens_.push_front(*token);
55  }
56  if (tokens_.size() > max_token_count_) max_token_count_ = tokens_.size();
57 }
58 
60 {
61  next_sequence_id_ = 1;
62  std::unique_lock<std::mutex> lk(tokens_mutex_);
63  tokens_.clear();
64 }
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 Reset()
Reset the policy, setting the next sequence ID to be used to 1, and removing any tokens.
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.