artdaq  v3_04_01
CapacityTest_policy.cc
1 #include "artdaq/Application/Routing/RoutingMasterPolicy.hh"
2 #include "artdaq/Application/Routing/PolicyMacros.hh"
3 #include "fhiclcpp/ParameterSet.h"
4 #include <cmath>
5 
6 namespace artdaq
7 {
12  {
13  public:
23  explicit CapacityTestPolicy(fhicl::ParameterSet ps);
24 
28  virtual ~CapacityTestPolicy() = default;
29 
41  private:
42  int tokenUsagePercent_;
43  };
44 
47  , tokenUsagePercent_(ps.get<int>("tokens_used_per_table_percent", 50))
48  {}
49 
51  {
52  auto tokens = getTokensSnapshot();
53  std::map<int, int> table;
54  auto tokenCount = 0;
55  for (auto token : *tokens)
56  {
57  table[token]++;
58  tokenCount++;
59  }
60  tokens->clear();
61 
62  int tokensToUse = ceil(tokenCount * tokenUsagePercent_ / 100.0);
63  auto tokensUsed = 0;
64 
65  detail::RoutingPacket output;
66  for (auto r : table)
67  {
68  bool breakCondition = false;
69  while (table[r.first] > 0) {
70  output.emplace_back(detail::RoutingPacketEntry(next_sequence_id_++, r.first));
71  table[r.first]--;
72  tokensUsed++;
73  if(tokensUsed >= tokensToUse)
74  {
75  breakCondition = true;
76  break;
77  }
78  }
79  if (breakCondition) break;
80  }
81 
82  for (auto r : table)
83  {
84  for (auto i = 0; i < r.second; ++i)
85  {
86  tokens->push_back(r.first);
87  }
88  }
89  addUnusedTokens(std::move(tokens));
90 
91  return output;
92  }
93 }
94 
95 DEFINE_ARTDAQ_ROUTING_POLICY(artdaq::CapacityTestPolicy)
A row of the Routing Table.
detail::RoutingPacket GetCurrentTable() override
Apply the policy to the current tokens.
The interface through which RoutingMasterCore obtains Routing Tables using received Routing Tokens...
CapacityTestPolicy(fhicl::ParameterSet ps)
CapacityTestPolicy Constructor.
std::vector< RoutingPacketEntry > RoutingPacket
A RoutingPacket is simply a vector of RoutingPacketEntry objects. It is not suitable for network tran...
Fragment::sequence_id_t next_sequence_id_
The next sequence ID to be assigned.
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.
A RoutingMasterPolicy which tries to fully load the first receiver, then the second, and so on.
virtual ~CapacityTestPolicy()=default
Default virtual Destructor.