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