00001 #include "artdaq/Application/Routing/RoutingMasterPolicy.hh" 00002 #include "artdaq/Application/Routing/PolicyMacros.hh" 00003 #include "fhiclcpp/ParameterSet.h" 00004 #include "tracemf.h" 00005 #define TRACE_NAME "RoundRobin_policy" 00006 00007 namespace artdaq 00008 { 00013 class RoundRobinPolicy : public RoutingMasterPolicy 00014 { 00015 public: 00023 explicit RoundRobinPolicy(fhicl::ParameterSet ps) 00024 : RoutingMasterPolicy(ps) 00025 , use_all_tokens_(ps.get<bool>("use_all_tokens", false)) 00026 {} 00027 00031 virtual ~RoundRobinPolicy() = default; 00032 00041 detail::RoutingPacket GetCurrentTable() override; 00042 00043 private: 00044 bool use_all_tokens_; 00045 }; 00046 00047 detail::RoutingPacket RoundRobinPolicy::GetCurrentTable() 00048 { 00049 TLOG(12) << "RoundRobinPolicy::GetCurrentTable start"; 00050 auto tokens = getTokensSnapshot(); 00051 TLOG(13) << "RoundRobinPolicy::GetCurrentTable token list size is " << tokens->size(); 00052 std::map<int, int> table; 00053 for (auto token : *tokens.get()) 00054 { 00055 TLOG(14) << "RoundRobinPolicy::GetCurrentTable adding token for rank " << token << " to table"; 00056 table[token]++; 00057 } 00058 tokens->clear(); 00059 TLOG(13) << "RoundRobinPolicy::GetCurrentTable table size is " << table.size() << ", token list size is " << tokens->size(); 00060 00061 detail::RoutingPacket output; 00062 auto endCondition = table.size() < (use_all_tokens_ ? 1 : GetReceiverCount()); 00063 TLOG(15) << "RoundRobinPolicy::GetCurrentTable initial endCondition is " << endCondition; 00064 00065 while (!endCondition) 00066 { 00067 for (auto it = table.begin(); it != table.end();) 00068 { 00069 TLOG(16) << "RoundRobinPolicy::GetCurrentTable assigning sequenceID " << next_sequence_id_ << " to rank " << it->first; 00070 output.emplace_back(detail::RoutingPacketEntry(next_sequence_id_++, it->first)); 00071 table[it->first]--; 00072 00073 if (table[it->first] <= 0) it = table.erase(it); 00074 else ++it; 00075 } 00076 endCondition = table.size() < (use_all_tokens_ ? 1 : GetReceiverCount()); 00077 } 00078 00079 for(auto r : table) 00080 { 00081 for(auto i = 0;i < r.second; ++i) 00082 { 00083 tokens->push_back(r.first); 00084 } 00085 } 00086 TLOG(13) << "RoundRobinPolicy::GetCurrentTable unused tokens for " << tokens->size() << " ranks will be saved for later"; 00087 addUnusedTokens(std::move(tokens)); 00088 00089 TLOG(12) << "RoundRobinPolicy::GetCurrentTable return with table size " << output.size(); 00090 return output; 00091 } 00092 } 00093 00094 DEFINE_ARTDAQ_ROUTING_POLICY(artdaq::RoundRobinPolicy)