artdaq  v3_09_05
CommandableFragmentGenerator_t.cc
1 #define TRACE_NAME "CommandableFragmentGenerator_t"
2 
3 #define BOOST_TEST_MODULE CommandableFragmentGenerator_t
4 #include <boost/test/unit_test.hpp>
5 
6 #include "artdaq-core/Data/ContainerFragment.hh"
7 #include "artdaq-core/Data/Fragment.hh"
8 #include "artdaq/DAQrate/RequestSender.hh"
9 #include "artdaq/Generators/CommandableFragmentGenerator.hh"
10 
11 #define TRACE_REQUIRE_EQUAL(l, r) \
12  do \
13  { \
14  if ((l) == (r)) \
15  { \
16  TLOG(TLVL_DEBUG) << __LINE__ << ": Checking if " << #l << " (" << (l) << ") equals " << #r << " (" << (r) << ")...YES!"; \
17  } \
18  else \
19  { \
20  TLOG(TLVL_ERROR) << __LINE__ << ": Checking if " << #l << " (" << (l) << ") equals " << #r << " (" << (r) << ")...NO!"; \
21  } \
22  BOOST_REQUIRE_EQUAL((l), (r)); \
23  } while (false)
24 
25 #define TRACE_REQUIRE(b) \
26  do \
27  { \
28  if (b) \
29  { \
30  TLOG(TLVL_DEBUG) << __LINE__ << ": Checking if " << #b << " (" << (b) << ") is true...YES!"; \
31  } \
32  else \
33  { \
34  TLOG(TLVL_ERROR) << __LINE__ << ": Checking if " << #b << " (" << (b) << ") is true...NO!"; \
35  } \
36  BOOST_REQUIRE((b)); \
37  } while (false)
38 
39 namespace artdaqtest {
40 class CommandableFragmentGeneratorTest;
41 }
42 
47 {
48 public:
52  explicit CommandableFragmentGeneratorTest(const fhicl::ParameterSet& ps);
53 
55 
56 private:
61 
62 protected:
70  bool getNext_(artdaq::FragmentPtrs& frags) override;
71 
76  bool checkHWStatus_() override { return !hwFail_.load(); }
77 
81  void start() override;
82 
86  void stopNoMutex() override;
87 
91  void stop() override;
92 
96  void pause() override;
97 
101  void resume() override;
102 
103 public:
108  void setFireCount(size_t count) { fireCount_ = count; }
109 
113  void setHwFail() { hwFail_ = true; }
114 
122  void setEnabledIds(uint64_t bitmask) { enabled_ids_ = bitmask; }
123 
128  void setTimestamp(artdaq::Fragment::timestamp_t ts) { ts_ = ts; }
129 
134  artdaq::Fragment::timestamp_t getTimestamp() { return ts_; }
135 
136 private:
137  std::atomic<size_t> fireCount_;
138  std::atomic<bool> hwFail_;
139  artdaq::Fragment::timestamp_t ts_;
140  std::atomic<bool> hw_stop_;
141  std::atomic<uint64_t> enabled_ids_;
142 };
143 
145  : CommandableFragmentGenerator(ps)
146  , fireCount_(1)
147  , hwFail_(false)
148  , ts_(0)
149  , hw_stop_(false)
150  , enabled_ids_(-1)
151 {
152  metricMan->initialize(ps.get<fhicl::ParameterSet>("metrics", fhicl::ParameterSet()));
153  metricMan->do_start();
154 }
155 
157 {
158  while (fireCount_ > 0)
159  {
160  ++ts_;
161  for (auto& id : fragmentIDs())
162  {
163  if (id < 64 && ((enabled_ids_ & (0x1 << id)) != 0))
164  {
165  TLOG(TLVL_DEBUG) << "Adding Fragment with ID " << id << ", SeqID " << ev_counter() << ", and timestamp " << ts_;
166  frags.emplace_back(new artdaq::Fragment(ev_counter(), id, artdaq::Fragment::FirstUserFragmentType, ts_));
167  }
168  }
169  fireCount_--;
170  ev_counter_inc();
171  }
172 
173  return !hw_stop_;
174 }
175 
177 
179 
181 
183 
185 
186 BOOST_AUTO_TEST_SUITE(CommandableFragmentGenerator_t)
187 
188 BOOST_AUTO_TEST_CASE(Simple)
189 {
190  artdaq::configureMessageFacility("CommandableFragmentGenerator_t");
191  TLOG(TLVL_INFO) << "Simple test case BEGIN";
192  fhicl::ParameterSet ps;
193  ps.put<int>("board_id", 1);
194  ps.put<int>("fragment_id", 1);
196 
197  testGen.StartCmd(1, 1, 1);
198 
199  artdaq::FragmentPtrs fps;
200  auto sts = testGen.getNext(fps);
201  TRACE_REQUIRE_EQUAL(sts, true);
202  TRACE_REQUIRE_EQUAL(fps.size(), 1u);
203  TRACE_REQUIRE_EQUAL(fps.front()->fragmentID(), 1);
204  TRACE_REQUIRE_EQUAL(fps.front()->timestamp(), 1);
205  TRACE_REQUIRE_EQUAL(fps.front()->sequenceID(), 1);
206  TLOG(TLVL_INFO) << "Simple test case END";
207 }
208 
209 BOOST_AUTO_TEST_CASE(WaitForStart)
210 {
211  artdaq::configureMessageFacility("CommandableFragmentGenerator_t");
212  TLOG(TLVL_INFO) << "WaitForStart test case BEGIN";
213  fhicl::ParameterSet ps;
214  ps.put<int>("board_id", 1);
215  ps.put<int>("fragment_id", 1);
217 
218  artdaq::FragmentPtrs fps;
219  auto sts = testGen.getNext(fps);
220  TRACE_REQUIRE_EQUAL(sts, false);
221  TRACE_REQUIRE_EQUAL(fps.size(), 0u);
222 
223  usleep(10000);
224  sts = testGen.getNext(fps);
225  TRACE_REQUIRE_EQUAL(sts, false);
226  TRACE_REQUIRE_EQUAL(fps.size(), 0u);
227 
228  testGen.StartCmd(2, 1, 1);
229 
230  sts = testGen.getNext(fps);
231  TRACE_REQUIRE_EQUAL(sts, true);
232  TRACE_REQUIRE_EQUAL(fps.size(), 1u);
233  TRACE_REQUIRE_EQUAL(fps.front()->fragmentID(), 1);
234  TRACE_REQUIRE_EQUAL(fps.front()->timestamp(), 1);
235  TRACE_REQUIRE_EQUAL(fps.front()->sequenceID(), 1);
236  TLOG(TLVL_INFO) << "WaitForStart test case END";
237 }
238 
239 BOOST_AUTO_TEST_CASE(StateMachine)
240 {
241  artdaq::configureMessageFacility("CommandableFragmentGenerator_t");
242  TLOG(TLVL_INFO) << "StateMachine test case BEGIN";
243  fhicl::ParameterSet ps;
244  ps.put<int>("board_id", 1);
245  ps.put<int>("fragment_id", 1);
247 
248  artdaq::FragmentPtrs fps;
249  auto sts = testGen.getNext(fps);
250  TRACE_REQUIRE_EQUAL(sts, false);
251  TRACE_REQUIRE_EQUAL(fps.size(), 0u);
252 
253  usleep(10000);
254  sts = testGen.getNext(fps);
255  TRACE_REQUIRE_EQUAL(sts, false);
256  TRACE_REQUIRE_EQUAL(fps.size(), 0u);
257 
258  testGen.StartCmd(2, 1, 1);
259 
260  sts = testGen.getNext(fps);
261  TRACE_REQUIRE_EQUAL(sts, true);
262  TRACE_REQUIRE_EQUAL(fps.size(), 1u);
263  TRACE_REQUIRE_EQUAL(fps.front()->fragmentID(), 1);
264  TRACE_REQUIRE_EQUAL(fps.front()->timestamp(), 1);
265  TRACE_REQUIRE_EQUAL(fps.front()->sequenceID(), 1);
266  fps.clear();
267 
268  testGen.setFireCount(1);
269  testGen.PauseCmd(1, 1);
270  sts = testGen.getNext(fps);
271  TRACE_REQUIRE_EQUAL(sts, false);
272  TRACE_REQUIRE_EQUAL(fps.size(), 0u);
273 
274  testGen.ResumeCmd(1, 1);
275 
276  sts = testGen.getNext(fps);
277  TRACE_REQUIRE_EQUAL(sts, true);
278  TRACE_REQUIRE_EQUAL(fps.size(), 1u);
279  TRACE_REQUIRE_EQUAL(fps.front()->fragmentID(), 1);
280  TRACE_REQUIRE_EQUAL(fps.front()->timestamp(), 2);
281  TRACE_REQUIRE_EQUAL(fps.front()->sequenceID(), 2);
282  fps.clear();
283 
284  testGen.StopCmd(1, 1);
285  sts = testGen.getNext(fps);
286  TRACE_REQUIRE_EQUAL(sts, false);
287  TRACE_REQUIRE_EQUAL(fps.size(), 0u);
288 
289  testGen.StartCmd(2, 1, 1);
290  sts = testGen.getNext(fps);
291  TRACE_REQUIRE_EQUAL(sts, true);
292  TRACE_REQUIRE_EQUAL(fps.size(), 0u);
293 
294  testGen.setFireCount(1);
295  sts = testGen.getNext(fps);
296  TRACE_REQUIRE_EQUAL(sts, true);
297  TRACE_REQUIRE_EQUAL(fps.size(), 1u);
298  TRACE_REQUIRE_EQUAL(fps.front()->fragmentID(), 1);
299  TRACE_REQUIRE_EQUAL(fps.front()->timestamp(), 3);
300  TRACE_REQUIRE_EQUAL(fps.front()->sequenceID(), 1);
301  fps.clear();
302 
303  TLOG(TLVL_INFO) << "StateMachine test case END";
304 }
305 
306 BOOST_AUTO_TEST_CASE(MultipleIDs)
307 {
308  artdaq::configureMessageFacility("CommandableFragmentGenerator_t");
309  TLOG(TLVL_INFO) << "MultipleIDs test case BEGIN";
310  fhicl::ParameterSet ps;
311  ps.put<int>("board_id", 1);
312  ps.put<std::vector<int>>("fragment_ids", {1, 2, 3});
313 
315  testGen.StartCmd(3, 1, 1);
316 
317  artdaq::FragmentPtrs fps;
318  auto sts = testGen.getNext(fps);
319 
320  std::map<artdaq::Fragment::fragment_id_t, size_t> ids;
321  TRACE_REQUIRE_EQUAL(sts, true);
322  TRACE_REQUIRE_EQUAL(fps.size(), 3u);
323  while (fps.size() > 0)
324  {
325  ids[fps.front()->fragmentID()]++;
326  TRACE_REQUIRE_EQUAL(fps.front()->timestamp(), 1);
327  TRACE_REQUIRE_EQUAL(fps.front()->sequenceID(), 1);
328  fps.pop_front();
329  }
330 
331  TRACE_REQUIRE_EQUAL(ids[1], 1);
332  TRACE_REQUIRE_EQUAL(ids[2], 1);
333  TRACE_REQUIRE_EQUAL(ids[3], 1);
334  ids.clear();
335 
336  fps.clear();
337 
338  testGen.setEnabledIds(0x6); // 0110b, ID 3 disabled
339  testGen.setFireCount(1);
340 
341  sts = testGen.getNext(fps);
342 
343  TRACE_REQUIRE_EQUAL(sts, true);
344  TRACE_REQUIRE_EQUAL(fps.size(), 2u);
345  while (!fps.empty())
346  {
347  ids[fps.front()->fragmentID()]++;
348  TRACE_REQUIRE_EQUAL(fps.front()->timestamp(), 2);
349  TRACE_REQUIRE_EQUAL(fps.front()->sequenceID(), 2);
350  fps.pop_front();
351  }
352  TRACE_REQUIRE_EQUAL(ids[1], 1);
353  TRACE_REQUIRE_EQUAL(ids[2], 1);
354  TRACE_REQUIRE_EQUAL(ids[3], 0);
355 
356  TLOG(TLVL_INFO) << "MultipleIDs test case END";
357 }
358 
359 BOOST_AUTO_TEST_CASE(HardwareFailure_NonThreaded)
360 {
361  artdaq::configureMessageFacility("CommandableFragmentGenerator_t");
362  TLOG(TLVL_INFO) << "HardwareFailure_NonThreaded test case BEGIN";
363  fhicl::ParameterSet ps;
364  ps.put<int>("board_id", 1);
365  ps.put<int>("fragment_id", 1);
366  ps.put<bool>("separate_data_thread", false);
367  ps.put<bool>("separate_monitoring_thread", false);
368  ps.put<int64_t>("hardware_poll_interval_us", 10);
369 
370  auto buffer = std::make_shared<artdaq::RequestBuffer>();
371  buffer->setRunning(true);
373  gen.SetRequestBuffer(buffer);
374  gen.StartCmd(4, 0xFFFFFFFF, 1);
375 
376  artdaq::FragmentPtrs fps;
377  auto sts = gen.getNext(fps);
378  TRACE_REQUIRE_EQUAL(sts, true);
379  TRACE_REQUIRE_EQUAL(fps.size(), 1u);
380  TRACE_REQUIRE_EQUAL(fps.front()->fragmentID(), 1);
381  TRACE_REQUIRE_EQUAL(fps.front()->timestamp(), 1);
382  TRACE_REQUIRE_EQUAL(fps.front()->sequenceID(), 1);
383  fps.clear();
384 
385  gen.setFireCount(1);
386  gen.setHwFail();
387  usleep(10000);
388  sts = gen.getNext(fps);
389  TRACE_REQUIRE_EQUAL(sts, false);
390  TRACE_REQUIRE(fps.empty());
391 
392  gen.StopCmd(0xFFFFFFFF, 1);
393  gen.joinThreads();
394  TLOG(TLVL_INFO) << "HardwareFailure_NonThreaded test case END";
395 }
396 
397 BOOST_AUTO_TEST_CASE(HardwareFailure_Threaded)
398 {
399  artdaq::configureMessageFacility("CommandableFragmentGenerator_t");
400  TLOG(TLVL_INFO) << "HardwareFailure_Threaded test case BEGIN";
401  fhicl::ParameterSet ps;
402  ps.put<int>("board_id", 1);
403  ps.put<int>("fragment_id", 1);
404  ps.put<bool>("separate_monitoring_thread", true);
405  ps.put<int64_t>("hardware_poll_interval_us", 750000);
406 
407  auto buffer = std::make_shared<artdaq::RequestBuffer>();
408  buffer->setRunning(true);
410  gen.SetRequestBuffer(buffer);
411  gen.StartCmd(5, 0xFFFFFFFF, 1);
412 
413  artdaq::FragmentPtrs fps;
414  auto sts = gen.getNext(fps);
415  TRACE_REQUIRE_EQUAL(sts, true);
416  TRACE_REQUIRE_EQUAL(fps.size(), 1u);
417  TRACE_REQUIRE_EQUAL(fps.front()->fragmentID(), 1);
418  TRACE_REQUIRE_EQUAL(fps.front()->timestamp(), 1);
419  TRACE_REQUIRE_EQUAL(fps.front()->sequenceID(), 1);
420  fps.clear();
421 
422  TLOG(TLVL_INFO) << "Setting failure bit";
423  gen.setHwFail();
424 
425  sleep(1);
426 
427  TLOG(TLVL_INFO) << "Checking that failure is reported by getNext";
428  gen.setFireCount(1);
429  sts = gen.getNext(fps);
430  TRACE_REQUIRE_EQUAL(sts, false);
431  TRACE_REQUIRE(fps.empty());
432 
433  gen.StopCmd(0xFFFFFFFF, 1);
434  gen.joinThreads();
435  TLOG(TLVL_INFO) << "HardwareFailure_Threaded test case END";
436 }
437 
438 BOOST_AUTO_TEST_SUITE_END()
bool getNext_(artdaq::FragmentPtrs &frags) override
Generate data and return it to CommandableFragmentGenerator.
void setEnabledIds(uint64_t bitmask)
Set the enabled IDs mask for the Fragment Generator.
CommandableFragmentGenerator derived class for testing.
void StopCmd(uint64_t timeout, uint64_t timestamp)
Stop the CommandableFragmentGenerator.
CommandableFragmentGeneratorTest(const fhicl::ParameterSet &ps)
CommandableFragmentGeneratorTest Constructor.
void StartCmd(int run, uint64_t timeout, uint64_t timestamp)
Start the CommandableFragmentGenerator.
bool getNext(FragmentPtrs &output) overridefinal
getNext calls either applyRequests or getNext_ to get any data that is ready to be sent to the EventB...
bool checkHWStatus_() override
Returns whether the hwFail flag has not been set.
void stopNoMutex() override
Perform immediate stop actions. No-Op.
void start() override
Perform start actions. No-Op.
CommandableFragmentGenerator is a FragmentGenerator-derived abstract class that defines the interface...
void resume() override
Perform resume actions. No-Op.
void pause() override
Perform pause actions. No-Op.
void setTimestamp(artdaq::Fragment::timestamp_t ts)
Set the timestamp to be used for the next Fragment.
artdaq::Fragment::timestamp_t getTimestamp()
Get the timestamp that will be used for the next Fragment.
void stop() override
Perform stop actions. No-Op.
void SetRequestBuffer(std::shared_ptr< RequestBuffer > buffer)
Set the shared_ptr to the RequestBuffer.
void joinThreads()
Join any data-taking threads. Should be called when destructing CommandableFragmentGenerator.
void setFireCount(size_t count)
Have getNext_ generate count fragments.