5 #include "artdaq-demo/Generators/ToySimulator.hh"
7 #include "canvas/Utilities/Exception.h"
8 #include "cetlib_except/exception.h"
9 #include "fhiclcpp/ParameterSet.h"
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"
16 #define TRACE_NAME "ToySimulator"
17 #include "TRACE/tracemf.h"
26 : CommandableFragmentGenerator(ps)
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))
41 hardware_interface_->AllocateReadoutBuffer(&readout_buffer_);
43 if (exception_on_config_)
45 throw cet::exception(
"ToySimulator") <<
"This is an engineered exception designed for testing purposes, set "
46 "by the exception_on_config FHiCL variable";
50 TLOG(TLVL_ERROR) <<
"This is an engineered process death, set by the dies_on_config FHiCL variable";
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_);
59 switch (hardware_interface_->BoardType())
62 fragment_type_ = toFragmentType(
"TOY1");
65 fragment_type_ = toFragmentType(
"TOY2");
68 throw cet::exception(
"ToySimulator") <<
"Unable to determine board type supplied by hardware";
74 bool demo::ToySimulator::getNext_(artdaq::FragmentPtrs& frags)
109 auto requests = GetRequestBuffer();
110 if (requests ==
nullptr)
112 throw cet::exception(
"ToySimulator") <<
"Lazy mode is enabled, but the RequestBuffer is nullptr";
115 auto request = requests->GetNextRequest();
116 if (request.first == 0)
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.";
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())
132 if (lazily_handled_requests_.find(request_iterator->first) == lazily_handled_requests_.end())
134 lazily_handled_requests_.insert(request_iterator->first);
135 new_request = *request_iterator;
140 if (new_request.first == 0)
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.";
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";
164 TLOG(TLVL_DEBUG + 3) <<
"getNext_: Creating Fragments for configured Fragment IDs";
165 for (
auto&
id : fragmentIDs())
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));
176 TLOG(TLVL_DEBUG + 4) <<
"getNext_: Before memcpy";
179 memcpy(frags.back()->dataBeginBytes(), readout_buffer_, bytes_read);
184 memcpy(frags.back()->dataBeginBytes(), readout_buffer_,
sizeof(ToyFragment::Header));
187 TLOG(TLVL_DEBUG + 4) <<
"getNext_ after memcpy " << bytes_read
188 <<
" bytes and std::move dataSizeBytes()=" << frags.back()->sizeBytes()
189 <<
" metabytes=" <<
sizeof(metadata_);
192 if (metricMan !=
nullptr)
194 metricMan->sendMetric(
"Fragments Sent", ev_counter(),
"Events", 3, artdaq::MetricMode::LastPoint);
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)
200 bool fragmentIdZero =
false;
201 for (
auto&
id : fragmentIDs())
205 fragmentIdZero =
true;
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);
214 endOfSubrunFrag->setSequenceID(ev_counter() + 1);
215 endOfSubrunFrag->setTimestamp(1 + (ev_counter() / rollover_subrun_interval_));
217 *endOfSubrunFrag->dataBegin() = my_rank;
218 frags.emplace_back(std::move(endOfSubrunFrag));
223 timestamp_ += timestampScale_;
225 TLOG(TLVL_DEBUG + 3) <<
"getNext_: DONE";
229 void demo::ToySimulator::start()
231 hardware_interface_->StartDatataking();
233 lazily_handled_requests_.clear();
236 void demo::ToySimulator::stop() { hardware_interface_->StopDatataking(); }
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 ...