artdaq_demo  3.12.04
MisbehaviorTest_policy.cc
1 #include "artdaq/DAQdata/Globals.hh"
2 #include "artdaq/RoutingPolicies/PolicyMacros.hh"
3 #include "artdaq/RoutingPolicies/RoutingManagerPolicy.hh"
4 #include "fhiclcpp/ParameterSet.h"
5 #include "messagefacility/MessageLogger/MessageLogger.h"
6 
7 namespace demo {
11 class MisbehaviorTest : public artdaq::RoutingManagerPolicy
12 {
13 public:
28  explicit MisbehaviorTest(const fhicl::ParameterSet& ps);
29 
33  ~MisbehaviorTest() override = default;
34 
39  void CreateRoutingTable(artdaq::detail::RoutingPacket& table) override;
40 
47  artdaq::detail::RoutingPacketEntry CreateRouteForSequenceID(artdaq::Fragment::sequence_id_t seq, int requesting_rank) override;
48 
49 private:
50  MisbehaviorTest(MisbehaviorTest const&) = delete;
51  MisbehaviorTest(MisbehaviorTest&&) = delete;
52  MisbehaviorTest& operator=(MisbehaviorTest const&) = delete;
53  MisbehaviorTest& operator=(MisbehaviorTest&&) = delete;
54 
55  artdaq::Fragment::sequence_id_t misbehave_after_;
56  artdaq::Fragment::sequence_id_t misbehave_until_{0}; // For overloading EventBuilder in request mode
57  size_t misbehave_pause_ms_;
58  bool misbehave_conflicting_table_data_;
59  bool misbehave_corrupt_table_data_;
60  bool misbehave_overload_event_builder_;
61 };
62 
63 MisbehaviorTest::MisbehaviorTest(const fhicl::ParameterSet& ps)
64  : RoutingManagerPolicy(ps)
65  , misbehave_after_(ps.get<size_t>("misbehave_after_n_events", 1000))
66  , misbehave_pause_ms_(ps.get<size_t>("misbehave_pause_time_ms", 0))
67  , misbehave_conflicting_table_data_(ps.get<bool>("misbehave_send_conflicting_table_data", false))
68  , misbehave_corrupt_table_data_(ps.get<bool>("misbehave_send_corrupt_table_data", false))
69  , misbehave_overload_event_builder_(ps.get<bool>("misbehave_overload_event_builder", false))
70 {
71  srand(time(nullptr)); // NOLINT(cert-msc51-cpp)
72  auto count = (misbehave_conflicting_table_data_ ? 1 : 0) + (misbehave_corrupt_table_data_ ? 1 : 0) +
73  (misbehave_overload_event_builder_ ? 1 : 0) + (misbehave_pause_ms_ > 0 ? 1 : 0);
74  if (count > 1)
75  {
76  mf::LogWarning("MisbehaviorTest") << "Only one misbehavior is allowed at a time!";
77  exit(3);
78  }
79 }
80 
81 void MisbehaviorTest::CreateRoutingTable(artdaq::detail::RoutingPacket& table)
82 {
83  auto half = tokens_.size() / 2;
84  size_t counter = 0;
85  for (; counter < half; ++counter)
86  {
87  table.emplace_back(artdaq::detail::RoutingPacketEntry(next_sequence_id_, tokens_.at(counter)));
88  next_sequence_id_++;
89  }
90 
91  if (next_sequence_id_ > misbehave_after_)
92  {
93  if (!tokens_.empty())
94  {
95  if (misbehave_pause_ms_ > 0)
96  {
97  mf::LogError("MisbehaviorTest")
98  << "Pausing for " << misbehave_pause_ms_ << " milliseconds before sending table update";
99  usleep(misbehave_pause_ms_ * 1000);
100  }
101  if (misbehave_conflicting_table_data_)
102  {
103  mf::LogError("MisbehaviorTest") << "Adding conflicting data point to output";
104  table.emplace_back(next_sequence_id_, tokens_.at(counter) + 1);
105  }
106  if (misbehave_corrupt_table_data_)
107  {
108  mf::LogError("MisbehaviorTest") << "Adding random data point";
109  table.emplace_back(seedAndRandom(), rand()); // NOLINT(cert-msc50-cpp)
110  }
111  if (misbehave_overload_event_builder_)
112  {
113  mf::LogError("MisbehaviorTest") << "Sending 100 events in a row to Rank " << tokens_.at(0);
114  for (auto ii = 0; ii < 100; ++ii)
115  {
116  table.emplace_back(next_sequence_id_, tokens_.at(0));
117  next_sequence_id_++;
118  }
119  }
120  misbehave_after_ += misbehave_after_;
121  }
122  }
123 
124  for (; counter < tokens_.size(); ++counter)
125  {
126  table.emplace_back(artdaq::detail::RoutingPacketEntry(next_sequence_id_, tokens_.at(counter)));
127  next_sequence_id_++;
128  }
129 }
130 artdaq::detail::RoutingPacketEntry MisbehaviorTest::CreateRouteForSequenceID(artdaq::Fragment::sequence_id_t seq, int)
131 {
132  artdaq::detail::RoutingPacketEntry output;
133  if (!tokens_.empty())
134  {
135  if (seq > misbehave_after_ || seq < misbehave_until_)
136  {
137  if (seq > misbehave_until_)
138  {
139  misbehave_after_ += misbehave_after_;
140  }
141  if (misbehave_pause_ms_ > 0)
142  {
143  mf::LogError("MisbehaviorTest")
144  << "Pausing for " << misbehave_pause_ms_ << " milliseconds before sending table update";
145  usleep(misbehave_pause_ms_ * 1000);
146 
147  auto dest = tokens_.front(); // No-Op: Use first token
148  output = artdaq::detail::RoutingPacketEntry(seq, dest);
149  tokens_.pop_front();
150  tokens_used_since_last_update_++;
151  }
152  // misbehave_conflicting_table_data_ is not applicable for request mode
153  if (misbehave_corrupt_table_data_)
154  {
155  mf::LogError("MisbehaviorTest") << "Adding random data point";
156  output = artdaq::detail::RoutingPacketEntry(seedAndRandom(), rand()); // NOLINT(cert-msc50-cpp)
157  }
158  if (misbehave_overload_event_builder_)
159  {
160  output = artdaq::detail::RoutingPacketEntry(seq, tokens_.front());
161  // Not removing token
162  if (seq > misbehave_until_)
163  {
164  misbehave_until_ = seq + 100;
165  }
166  }
167  }
168  else
169  {
170  auto dest = tokens_.front(); // No-Op: Use first token
171  output = artdaq::detail::RoutingPacketEntry(seq, dest);
172  tokens_.pop_front();
173  tokens_used_since_last_update_++;
174  }
175  }
176 
177  return output;
178 }
179 } // namespace demo
180 
181 DEFINE_ARTDAQ_ROUTING_POLICY(demo::MisbehaviorTest)
void CreateRoutingTable(artdaq::detail::RoutingPacket &table) override
Use the current tokens to add entries to the routing table.
artdaq::detail::RoutingPacketEntry CreateRouteForSequenceID(artdaq::Fragment::sequence_id_t seq, int requesting_rank) override
Using the existing tokens, determine a route for a given Sequence ID.
A test RoutingManagerPolicy which does various &quot;bad&quot; things, determined by configuration.
MisbehaviorTest(const fhicl::ParameterSet &ps)
MisbehaviorTest Constructor.
~MisbehaviorTest() override=default
MisbehaviorTest default Destructor.