artdaq_demo  v3_09_06
ToySimulator_generator.cc
1 // For an explanation of this class, look at its header,
2 // ToySimulator.hh, as well as
3 // https://cdcvs.fnal.gov/redmine/projects/artdaq-demo/wiki/Fragments_and_FragmentGenerators_w_Toy_Fragments_as_Examples
4 
5 #include "artdaq-demo/Generators/ToySimulator.hh"
6 
7 #include "canvas/Utilities/Exception.h"
8 #include "cetlib_except/exception.h"
9 #include "fhiclcpp/ParameterSet.h"
10 
11 #include "artdaq-core-demo/Overlays/FragmentType.hh"
12 #include "artdaq-core-demo/Overlays/ToyFragment.hh"
13 #include "artdaq-core/Utilities/SimpleLookupPolicy.hh"
14 #include "artdaq/Generators/GeneratorMacros.hh"
15 
16 #define TRACE_NAME "ToySimulator"
17 #include "TRACE/tracemf.h" // TRACE, TLOG*
18 
19 #include <unistd.h>
20 #include <fstream>
21 #include <iomanip>
22 #include <iostream>
23 #include <iterator>
24 
25 demo::ToySimulator::ToySimulator(fhicl::ParameterSet const& ps)
26  : CommandableFragmentGenerator(ps)
27  , hardware_interface_(new ToyHardwareInterface(ps))
28  , timestamp_(0)
29  , timestampScale_(ps.get<int>("timestamp_scale_factor", 1))
30  , rollover_subrun_interval_(ps.get<int>("rollover_subrun_interval", 0))
31  , metadata_({0, 0, 0})
32  , readout_buffer_(nullptr)
33  , fragment_type_(static_cast<decltype(fragment_type_)>(artdaq::Fragment::InvalidFragmentType))
34  , distribution_type_(static_cast<ToyHardwareInterface::DistributionType>(ps.get<int>("distribution_type")))
35  , generated_fragments_per_event_(ps.get<int>("generated_fragments_per_event", 1))
36  , exception_on_config_(ps.get<bool>("exception_on_config", false))
37  , dies_on_config_(ps.get<bool>("dies_on_config", false))
38  , lazy_mode_(ps.get<bool>("lazy_mode", false))
39 
40 {
41  hardware_interface_->AllocateReadoutBuffer(&readout_buffer_);
42 
43  if (exception_on_config_)
44  {
45  throw cet::exception("ToySimulator") << "This is an engineered exception designed for testing purposes, set " // NOLINT(cert-err60-cpp)
46  "by the exception_on_config FHiCL variable";
47  }
48  if (dies_on_config_)
49  {
50  TLOG(TLVL_ERROR) << "This is an engineered process death, set by the dies_on_config FHiCL variable";
51  std::exit(1);
52  }
53 
54  metadata_.board_serial_number = hardware_interface_->SerialNumber() & 0xFFFF;
55  metadata_.num_adc_bits = hardware_interface_->NumADCBits();
56  TLOG(TLVL_INFO) << "Constructor: metadata_.unused = 0x" << std::hex << metadata_.unused
57  << " sizeof(metadata_) = " << std::dec << sizeof(metadata_);
58 
59  switch (hardware_interface_->BoardType())
60  {
61  case 1002:
62  fragment_type_ = toFragmentType("TOY1");
63  break;
64  case 1003:
65  fragment_type_ = toFragmentType("TOY2");
66  break;
67  default:
68  throw cet::exception("ToySimulator") << "Unable to determine board type supplied by hardware"; // NOLINT(cert-err60-cpp)
69  }
70 }
71 
72 demo::ToySimulator::~ToySimulator() { hardware_interface_->FreeReadoutBuffer(readout_buffer_); }
73 
74 bool demo::ToySimulator::getNext_(artdaq::FragmentPtrs& frags)
75 {
76  if (should_stop())
77  {
78  return false;
79  }
80 
81  // ToyHardwareInterface (an instance to which "hardware_interface_"
82  // is a unique_ptr object) is just one example of the sort of
83  // interface a hardware library might offer. For example, other
84  // interfaces might require you to allocate and free the memory used
85  // to store hardware data in your generator using standard C++ tools
86  // (rather than via the "AllocateReadoutBuffer" and
87  // "FreeReadoutBuffer" functions provided here), or could have a
88  // function which directly returns a pointer to the data buffer
89  // rather than sticking the data in the location pointed to by your
90  // pointer (which is what happens here with readout_buffer_)
91 
92  // 15-Nov-2019, KAB, JCF: added handling of the 'lazy' mode.
93  // In this context, "lazy" is intended to mean "only generate data when
94  // it is requested". With this code, we return before doing the work
95  // of filling the buffer (lazy!), and we overwrite whatever local
96  // calculation of the timestamp has been done with the very specific
97  // timestamp that is contained in the request. We could also capture
98  // the sequence_id from the request and use it when creating the
99  // artdaq::Fragment, but that isn't strictly necessary since the sequence_ids
100  // of pull-mode fragments get overwritten when they are matched to requests
101  // in CommandableFragmentGenerator.
102  // For completeness, we include tests of both the GetNextRequest and
103  // GetRequest methods (controlled by the LAZY_MODEL pre-processor variable).
104  if (lazy_mode_)
105  {
106 #define LAZY_MODEL 0
107 #if LAZY_MODEL == 0
108 
109  auto requests = GetRequestBuffer();
110  if (requests == nullptr)
111  {
112  throw cet::exception("ToySimulator") << "Lazy mode is enabled, but the RequestBuffer is nullptr";
113  }
114 
115  auto request = requests->GetNextRequest();
116  if (request.first == 0)
117  {
118  usleep(10);
119  return true;
120  }
121 
122  timestamp_ = request.second;
123  TLOG(51) << "Received a request for the fragment with timestamp " << timestamp_
124  << " and sequenceId " << request.first << ". Proceeding to fill the fragment buffer, etc.";
125 #else
126  auto requests = GetRequests();
127  auto request_iterator = requests.begin();
128  std::pair<artdaq::Fragment::sequence_id_t, artdaq::Fragment::timestamp_t> new_request(0, 0);
129  TLOG(52) << "Looping through " << requests.size() << " requests to see if there is a new one.";
130  while (request_iterator != requests.end())
131  {
132  if (lazily_handled_requests_.find(request_iterator->first) == lazily_handled_requests_.end())
133  {
134  lazily_handled_requests_.insert(request_iterator->first);
135  new_request = *request_iterator;
136  break;
137  }
138  ++request_iterator;
139  }
140  if (new_request.first == 0)
141  {
142  usleep(10);
143  return true;
144  }
145  timestamp_ = new_request.second;
146  TLOG(51) << "Found a new request for the fragment with timestamp " << timestamp_
147  << " and sequenceId " << new_request.first << ". Proceeding to fill the fragment buffer, etc.";
148 #endif
149  }
150 
151  TLOG(TLVL_DEBUG + 3) << "getNext_: Calling ToyHardwareInterface::FillBuffer";
152  std::size_t bytes_read = 0;
153  hardware_interface_->FillBuffer(readout_buffer_, &bytes_read);
154  TLOG(TLVL_DEBUG + 3) << "getNext_: Done with FillBuffer";
155 
156  // We'll use the static factory function
157 
158  // artdaq::Fragment::FragmentBytes(std::size_t payload_size_in_bytes, sequence_id_t sequence_id,
159  // fragment_id_t fragment_id, type_t type, const T & metadata)
160 
161  // which will then return a unique_ptr to an artdaq::Fragment
162  // object.
163 
164  TLOG(TLVL_DEBUG + 3) << "getNext_: Creating Fragments for configured Fragment IDs";
165  for (auto& id : fragmentIDs())
166  {
167  // The offset logic below is designed to both ensure
168  // backwards compatibility and to (help) avoid collisions
169  // with fragment_ids from other boardreaders if more than
170  // one fragment is generated per event
171 
172  std::unique_ptr<artdaq::Fragment> fragptr(
173  artdaq::Fragment::FragmentBytes(bytes_read, ev_counter(), id, fragment_type_, metadata_, timestamp_));
174  frags.emplace_back(std::move(fragptr));
175 
176  TLOG(TLVL_DEBUG + 4) << "getNext_: Before memcpy";
178  {
179  memcpy(frags.back()->dataBeginBytes(), readout_buffer_, bytes_read);
180  }
181  else
182  {
183  // Must preserve the Header!
184  memcpy(frags.back()->dataBeginBytes(), readout_buffer_, sizeof(ToyFragment::Header));
185  }
186 
187  TLOG(TLVL_DEBUG + 4) << "getNext_ after memcpy " << bytes_read
188  << " bytes and std::move dataSizeBytes()=" << frags.back()->sizeBytes()
189  << " metabytes=" << sizeof(metadata_);
190  }
191 
192  if (metricMan != nullptr)
193  {
194  metricMan->sendMetric("Fragments Sent", ev_counter(), "Events", 3, artdaq::MetricMode::LastPoint);
195  }
196 
197  TLOG(TLVL_DEBUG + 3) << "getNext_: Checking for subrun rollover";
198  if (rollover_subrun_interval_ > 0 && ev_counter() % rollover_subrun_interval_ == 0 && fragment_id() == 0)
199  {
200  bool fragmentIdZero = false;
201  for (auto& id : fragmentIDs())
202  {
203  if (id == 0)
204  {
205  fragmentIdZero = true;
206  }
207  }
208  if (fragmentIdZero)
209  {
210  artdaq::FragmentPtr endOfSubrunFrag(new artdaq::Fragment(static_cast<size_t>(
211  ceil(sizeof(my_rank) / static_cast<double>(sizeof(artdaq::Fragment::value_type))))));
212  endOfSubrunFrag->setSystemType(artdaq::Fragment::EndOfSubrunFragmentType);
213 
214  endOfSubrunFrag->setSequenceID(ev_counter() + 1);
215  endOfSubrunFrag->setTimestamp(1 + (ev_counter() / rollover_subrun_interval_));
216 
217  *endOfSubrunFrag->dataBegin() = my_rank;
218  frags.emplace_back(std::move(endOfSubrunFrag));
219  }
220  }
221 
222  ev_counter_inc();
223  timestamp_ += timestampScale_;
224 
225  TLOG(TLVL_DEBUG + 3) << "getNext_: DONE";
226  return true;
227 }
228 
229 void demo::ToySimulator::start()
230 {
231  hardware_interface_->StartDatataking();
232  timestamp_ = 0;
233  lazily_handled_requests_.clear();
234 }
235 
236 void demo::ToySimulator::stop() { hardware_interface_->StopDatataking(); }
237 
238 // The following macro is defined in artdaq's GeneratorMacros.hh header
239 DEFINE_ARTDAQ_COMMANDABLE_GENERATOR(demo::ToySimulator)
A use-after-free expliot distribution.
ToySimulator(fhicl::ParameterSet const &ps)
ToySimulator Constructor.
JCF, Mar-17-2016: ToyHardwareInterface is meant to mimic a vendor-provided hardware API...
virtual ~ToySimulator()
Shutdown the ToySimulator.
ToySimulator is a simple type of fragment generator intended to be studied by new users of artdaq as ...
Definition: ToySimulator.hh:35