artdaq_demo  v3_09_00
ToyHardwareInterface.cc
1 #include "artdaq-demo/Generators/ToyHardwareInterface/ToyHardwareInterface.hh"
2 #define TRACE_NAME "ToyHardwareInterface"
3 #include "artdaq-core-demo/Overlays/FragmentType.hh"
4 #include "artdaq-core-demo/Overlays/ToyFragment.hh"
5 #include "artdaq/DAQdata/Globals.hh"
6 
7 #include "cetlib_except/exception.h"
8 #include "fhiclcpp/ParameterSet.h"
9 
10 #include <unistd.h>
11 #include <cstdlib>
12 #include <iostream>
13 #include <random>
14 
15 // JCF, Mar-17-2016
16 
17 // ToyHardwareInterface is meant to mimic a vendor-provided hardware
18 // API, usable within the the ToySimulator fragment generator. For
19 // purposes of realism, it's a C++03-style API, as opposed to, say, one
20 // based in C++11 capable of taking advantage of smart pointers, etc.
21 
22 ToyHardwareInterface::ToyHardwareInterface(fhicl::ParameterSet const& ps)
23  : taking_data_(false)
24  , nADCcounts_(ps.get<size_t>("nADCcounts", 40))
25  , maxADCcounts_(ps.get<size_t>("maxADCcounts", 50000000))
26  , change_after_N_seconds_(ps.get<size_t>("change_after_N_seconds", std::numeric_limits<size_t>::max()))
27  , pause_after_N_seconds_(ps.get<size_t>("pause_after_N_seconds", 0))
28  , nADCcounts_after_N_seconds_(ps.get<size_t>("nADCcounts_after_N_seconds", nADCcounts_))
29  , exception_after_N_seconds_(ps.get<bool>("exception_after_N_seconds", false))
30  , exit_after_N_seconds_(ps.get<bool>("exit_after_N_seconds", false))
31  , abort_after_N_seconds_(ps.get<bool>("abort_after_N_seconds", false))
32  , hang_after_N_seconds_(ps.get<bool>("hang_after_N_seconds",false))
33  , fragment_type_(demo::toFragmentType(ps.get<std::string>("fragment_type")))
34  , maxADCvalue_(static_cast<size_t>(pow(2, NumADCBits()) - 1))
35  , // MUST be after "fragment_type"
36  throttle_usecs_(ps.get<size_t>("throttle_usecs", 100000))
37  , usecs_between_sends_(ps.get<size_t>("usecs_between_sends", 0))
38  , distribution_type_(static_cast<DistributionType>(ps.get<int>("distribution_type")))
39  , engine_(ps.get<int64_t>("random_seed", 314159))
40  , uniform_distn_(new std::uniform_int_distribution<data_t>(0, maxADCvalue_))
41  , gaussian_distn_(new std::normal_distribution<double>(0.5 * maxADCvalue_, 0.1 * maxADCvalue_))
42  , start_time_(fake_time_)
43  , send_calls_(0)
44  , serial_number_((*uniform_distn_)(engine_))
45 {
46 
47  if (nADCcounts_ > maxADCcounts_ ||
48  nADCcounts_after_N_seconds_ > maxADCcounts_)
49  {
50  throw cet::exception("HardwareInterface") // NOLINT(cert-err60-cpp)
51  << R"(Either (or both) of "nADCcounts" and "nADCcounts_after_N_seconds")"
52  << " is larger than the \"maxADCcounts\" setting (currently at " << maxADCcounts_ << ")";
53  }
54 
55  bool planned_disruption = nADCcounts_after_N_seconds_ != nADCcounts_ || exception_after_N_seconds_ ||
56  exit_after_N_seconds_ || abort_after_N_seconds_;
57 
58  if (planned_disruption && change_after_N_seconds_ == std::numeric_limits<size_t>::max())
59  {
60  throw cet::exception("HardwareInterface") << "A FHiCL parameter designed to create a disruption has been " // NOLINT(cert-err60-cpp)
61  "set, so \"change_after_N_seconds\" should be set as well";
62  }
63 }
64 
65 // JCF, Mar-18-2017
66 
67 // "StartDatataking" is meant to mimic actions one would take when
68 // telling the hardware to start sending data - the uploading of
69 // values to registers, etc.
70 
72 {
73  taking_data_ = true;
74  send_calls_ = 0;
75 }
76 
78 {
79  taking_data_ = false;
80  start_time_ = fake_time_;
81 }
82 
83 void ToyHardwareInterface::FillBuffer(char* buffer, size_t* bytes_read)
84 {
85  TLOG(TLVL_TRACE) << "FillBuffer BEGIN";
86  if (taking_data_)
87  {
88  TLOG(6) << "FillBuffer: Sleeping for " << throttle_usecs_ << " microseconds";
89  usleep(throttle_usecs_);
90 
91  auto elapsed_secs_since_datataking_start = artdaq::TimeUtils::GetElapsedTime(start_time_);
92  if (elapsed_secs_since_datataking_start < 0) elapsed_secs_since_datataking_start = 0;
93 
94  if (static_cast<size_t>(elapsed_secs_since_datataking_start) < change_after_N_seconds_ || send_calls_ == 0)
95  {
96  TLOG(6) << "FillBuffer: Setting bytes_read to " << sizeof(demo::ToyFragment::Header) + nADCcounts_ * sizeof(data_t);
97  *bytes_read = sizeof(demo::ToyFragment::Header) + nADCcounts_ * sizeof(data_t);
98  }
99  else
100  {
101  if (abort_after_N_seconds_)
102  {
103  TLOG(TLVL_ERROR) << "Engineered Abort!";
104  std::abort();
105  }
106  else if (exit_after_N_seconds_)
107  {
108  TLOG(TLVL_ERROR) << "Engineered Exit!";
109  std::exit(1);
110  }
111  else if (exception_after_N_seconds_)
112  {
113  TLOG(TLVL_ERROR) << "Engineered Exception!";
114  throw cet::exception("HardwareInterface") // NOLINT(cert-err60-cpp)
115  << "This is an engineered exception designed for testing purposes";
116  }
117  else if (hang_after_N_seconds_)
118  {
119  TLOG(TLVL_ERROR) << "Pretending that the hardware has hung! Variable name for gdb: hardwareIsHung";
120  volatile bool hardwareIsHung = true;
121  // Pretend the hardware hangs
122  while (hardwareIsHung)
123  {
124  usleep(10000);
125  }
126  }
127  else
128  {
129  if ((pause_after_N_seconds_ != 0u) && (static_cast<size_t>(elapsed_secs_since_datataking_start) % change_after_N_seconds_ == 0))
130  {
131  TLOG(6) << "pausing " << pause_after_N_seconds_ << " seconds";
132  sleep(pause_after_N_seconds_);
133  TLOG(6) << "resuming after pause of " << pause_after_N_seconds_ << " seconds";
134  }
135  TLOG(6) << "FillBuffer: Setting bytes_read to " << sizeof(demo::ToyFragment::Header) + nADCcounts_after_N_seconds_ * sizeof(data_t);
136  *bytes_read = sizeof(demo::ToyFragment::Header) + nADCcounts_after_N_seconds_ * sizeof(data_t);
137  }
138  }
139 
140  TLOG(6) << "FillBuffer: Making the fake data, starting with the header";
141 
142  // Can't handle a fragment whose size isn't evenly divisible by
143  // the demo::ToyFragment::Header::data_t type size in bytes
144  // std::cout << "Bytes to read: " << *bytes_read << ", sizeof(data_t): " <<
145  // sizeof(demo::ToyFragment::Header::data_t) << std::endl;
146  assert(*bytes_read % sizeof(demo::ToyFragment::Header::data_t) == 0);
147 
148  auto* header = reinterpret_cast<demo::ToyFragment::Header*>(buffer); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
149 
150  header->event_size = *bytes_read / sizeof(demo::ToyFragment::Header::data_t);
151  header->trigger_number = 99;
152  header->distribution_type = static_cast<uint8_t>(distribution_type_);
153 
154  TLOG(6) << "FillBuffer: Generating nADCcounts ADC values ranging from 0 to max based on the desired distribution";
155 
156  std::function<data_t()> generator;
157  data_t gen_seed = 0;
158 
159  switch (distribution_type_)
160  {
162  generator = [&]() { return static_cast<data_t>((*uniform_distn_)(engine_)); };
163  break;
164 
166  generator = [&]() {
167  do
168  {
169  gen_seed = static_cast<data_t>(std::round((*gaussian_distn_)(engine_)));
170  } while (gen_seed > maxADCvalue_);
171  return gen_seed;
172  };
173  break;
174 
176  {
177  generator = [&]() {
178  if (++gen_seed > maxADCvalue_)
179  {
180  gen_seed = 0;
181  }
182  return gen_seed;
183  };
184  }
185  break;
186 
188  case DistributionType::uninit2:
189  break;
190 
191  default:
192  throw cet::exception("HardwareInterface") << "Unknown distribution type specified"; // NOLINT(cert-err60-cpp)
193  }
194 
195  if (distribution_type_ != DistributionType::uninitialized && distribution_type_ != DistributionType::uninit2)
196  {
197  TLOG(6) << "FillBuffer: Calling generate_n";
198  std::generate_n(reinterpret_cast<data_t*>(reinterpret_cast<demo::ToyFragment::Header*>(buffer) + 1),// NOLINT(cppcoreguidelines-pro-type-reinterpret-cast,cppcoreguidelines-pro-bounds-pointer-arithmetic)
199  nADCcounts_, generator);
200  }
201  }
202  else
203  {
204  throw cet::exception("ToyHardwareInterface") << "Attempt to call FillBuffer when not sending data"; // NOLINT(cert-err60-cpp)
205  }
206 
207  if (send_calls_ == 0)
208  {
209  TLOG(6) << "FillBuffer has set the start_time_";
210  start_time_ = std::chrono::steady_clock::now();
211  }
212 
213  if (usecs_between_sends_ != 0)
214  {
215  if (send_calls_ > 0)
216  {
217 
218  auto usecs_since_start = artdaq::TimeUtils::GetElapsedTimeMicroseconds(start_time_);
219  double delta = static_cast<double>(usecs_between_sends_ * send_calls_) - usecs_since_start;
220  TLOG(6) << "FillBuffer send_calls=" << send_calls_ << " usecs_since_start=" << usecs_since_start
221  << " delta=" << delta;
222  if (delta > 0)
223  {
224  TLOG(6) << "FillBuffer: Sleeping for " << delta << " microseconds";
225  usleep(delta);
226  }
227 
228 
229  }
230  }
231  ++send_calls_;
232  TLOG(TLVL_TRACE) << "FillBuffer END";
233 }
234 
236 {
237  *buffer = reinterpret_cast<char*>( // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
238  new uint8_t[sizeof(demo::ToyFragment::Header) + maxADCcounts_ * sizeof(data_t)]);
239 }
240 
241 void ToyHardwareInterface::FreeReadoutBuffer(const char* buffer) { delete[] buffer; }
242 
244 {
245  // Pretend that the "BoardType" is some vendor-defined integer which
246  // differs from the fragment_type_ we want to use as developers (and
247  // which must be between 1 and 224, inclusive) so add an offset
248  return static_cast<int>(fragment_type_) + 1000;
249 }
250 
252 {
253  switch (fragment_type_)
254  {
255  case demo::FragmentType::TOY1:
256  return 12;
257  break;
258  case demo::FragmentType::TOY2:
259  return 14;
260  break;
261  default:
262  throw cet::exception("ToyHardwareInterface") << "Unknown board type " << fragment_type_ << " (" // NOLINT(cert-err60-cpp)
263  << demo::fragmentTypeToString(fragment_type_) << ").\n";
264  };
265 }
266 
268 {
269  // Serial number is generated from the uniform distribution on initialization of the class
270  return serial_number_;
271 }
int NumADCBits() const
Get the number of ADC bits used in generating data.
void StartDatataking()
&quot;StartDatataking&quot; is meant to mimic actions one would take when telling the hardware to start sending...
uint16_t data_t
The type used to represent ADC counts (which are 12 or 14 bits, for TOY1 or TOY2) ...
void StopDatataking()
Performs shutdown actions.
void FillBuffer(char *buffer, size_t *bytes_read)
Use configured generator to fill a buffer with data.
DistributionType
Allow for the selection of output distribution.
void FreeReadoutBuffer(const char *buffer)
Release the given buffer to the hardware.
A monotonically-increasing distribution.
ToyHardwareInterface(fhicl::ParameterSet const &ps)
Construct and configure ToyHardwareInterface.
A use-after-free expliot distribution.
void AllocateReadoutBuffer(char **buffer)
Request a buffer from the hardware.
int SerialNumber() const
Gets the serial number of the simulated hardware.
int BoardType() const
Return the &quot;board type&quot; of the simulated hardware.