artdaq  v3_07_02
CapacityTest_policy.cc
1 #include <cmath>
2 #include "artdaq/RoutingPolicies/PolicyMacros.hh"
3 #include "artdaq/RoutingPolicies/RoutingMasterPolicy.hh"
4 #include "fhiclcpp/ParameterSet.h"
5 
6 namespace artdaq {
11 {
12 public:
22  explicit CapacityTestPolicy(fhicl::ParameterSet ps);
23 
27  virtual ~CapacityTestPolicy() = default;
28 
40 
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  {
71  output.emplace_back(detail::RoutingPacketEntry(next_sequence_id_++, r.first));
72  table[r.first]--;
73  tokensUsed++;
74  if (tokensUsed >= tokensToUse)
75  {
76  breakCondition = true;
77  break;
78  }
79  }
80  if (breakCondition) break;
81  }
82 
83  for (auto r : table)
84  {
85  for (auto i = 0; i < r.second; ++i)
86  {
87  tokens->push_back(r.first);
88  }
89  }
90  addUnusedTokens(std::move(tokens));
91 
92  return output;
93 }
94 } // namespace artdaq
95 
96 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.