artdaq  v3_09_00
CapacityTest_policy.cc
1 #include <cmath>
2 #include "artdaq/RoutingPolicies/PolicyMacros.hh"
3 #include "artdaq/RoutingPolicies/RoutingManagerPolicy.hh"
4 #include "fhiclcpp/ParameterSet.h"
5 
6 namespace artdaq {
11 {
12 public:
22  explicit CapacityTestPolicy(const fhicl::ParameterSet& ps);
23 
27  ~CapacityTestPolicy() override = default;
28 
40 
41 private:
42  CapacityTestPolicy(CapacityTestPolicy const&) = delete;
44  CapacityTestPolicy& operator=(CapacityTestPolicy const&) = delete;
45  CapacityTestPolicy& operator=(CapacityTestPolicy&&) = delete;
46 
47  int tokenUsagePercent_;
48 };
49 
50 CapacityTestPolicy::CapacityTestPolicy(const fhicl::ParameterSet& ps)
52  , tokenUsagePercent_(ps.get<int>("tokens_used_per_table_percent", 50))
53 {}
54 
56 {
57  auto tokens = getTokensSnapshot();
58  std::map<int, int> table;
59  auto tokenCount = 0;
60  for (auto token : *tokens)
61  {
62  table[token]++;
63  tokenCount++;
64  }
65  tokens->clear();
66 
67  int tokensToUse = ceil(tokenCount * tokenUsagePercent_ / 100.0);
68  auto tokensUsed = 0;
69 
70  detail::RoutingPacket output;
71  for (auto r : table)
72  {
73  bool breakCondition = false;
74  while (table[r.first] > 0)
75  {
76  output.emplace_back(detail::RoutingPacketEntry(next_sequence_id_++, r.first));
77  table[r.first]--;
78  tokensUsed++;
79  if (tokensUsed >= tokensToUse)
80  {
81  breakCondition = true;
82  break;
83  }
84  }
85  if (breakCondition)
86  {
87  break;
88  }
89  }
90 
91  for (auto r : table)
92  {
93  for (auto i = 0; i < r.second; ++i)
94  {
95  tokens->push_back(r.first);
96  }
97  }
98  addUnusedTokens(std::move(tokens));
99 
100  return output;
101 }
102 } // namespace artdaq
103 
104 DEFINE_ARTDAQ_ROUTING_POLICY(artdaq::CapacityTestPolicy)
A row of the Routing Table.
detail::RoutingPacket GetCurrentTable() override
Apply the policy to the current tokens.
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.
The interface through which RoutingManagerCore obtains Routing Tables using received Routing Tokens...
void addUnusedTokens(std::unique_ptr< std::deque< int >> tokens)
If necessary, return unused tokens to the token list, for subsequent updates.
~CapacityTestPolicy() override=default
Default virtual Destructor.
std::vector< RoutingPacketEntry > RoutingPacket
A RoutingPacket is simply a vector of RoutingPacketEntry objects. It is not suitable for network tran...
A RoutingManagerPolicy which tries to fully load the first receiver, then the second, and so on.
CapacityTestPolicy(const fhicl::ParameterSet &ps)
CapacityTestPolicy Constructor.