1 #ifndef artdaq_ArtModules_detail_SharedMemoryReader_hh
2 #define artdaq_ArtModules_detail_SharedMemoryReader_hh
4 #include "artdaq/DAQdata/Globals.hh"
5 #include "art/Framework/Core/Frameworkfwd.h"
7 #include "art/Framework/Core/FileBlock.h"
8 #include "art/Framework/Core/ProductRegistryHelper.h"
9 #include "art/Framework/IO/Sources/SourceHelper.h"
10 #include "art/Framework/Principal/EventPrincipal.h"
11 #include "art/Framework/Principal/RunPrincipal.h"
12 #include "art/Framework/Principal/SubRunPrincipal.h"
13 #include "artdaq-core/Core/SharedMemoryManager.hh"
14 #include "artdaq-core/Utilities/TimeUtils.hh"
15 #include "fhiclcpp/ParameterSet.h"
16 #include "artdaq-core/Data/Fragment.hh"
17 #include "artdaq-core/Data/ContainerFragment.hh"
18 #include "artdaq-core/Core/SharedMemoryEventReceiver.hh"
19 #include "art/Framework/IO/Sources/put_product_in_principal.h"
20 #include "canvas/Persistency/Provenance/FileFormatVersion.h"
26 #include "artdaq-core/Data/RawEvent.hh"
35 template<std::map<artdaq::Fragment::type_t, std::
string> getDefaultTypes() = artdaq::Fragment::MakeSystemTypeMap >
76 art::ProductRegistryHelper& help,
77 art::SourceHelper
const& pm)
95 incoming_events.reset(
new SharedMemoryEventReceiver(ps.get<uint32_t>(
"shared_memory_key", 0xBEE70000 + getppid()), ps.get<uint32_t>(
"broadcast_shared_memory_key", 0xCEE70000 + getppid())));
101 help.reconstitutes<Fragments, art::InEvent>(
pretend_module_name,
"Container" + it->second);
103 auto extraTypes = ps.get<std::vector<std::pair<Fragment::type_t, std::string>>>(
"fragment_type_map", std::vector<std::pair<Fragment::type_t, std::string>>());
104 for (
auto it = extraTypes.begin(); it != extraTypes.end(); ++it)
108 help.reconstitutes<Fragments, art::InEvent>(
pretend_module_name,
"Container" + it->second);
110 TLOG_INFO(
"SharedMemoryReader") <<
"SharedMemoryReader initialized with ParameterSet: " << ps.to_string() ;
126 art::ProductRegistryHelper& help,
127 art::SourceHelper
const& pm,
145 void readFile(std::string
const&, art::FileBlock*& fb)
147 TLOG_ARB(5,
"SharedMemoryReader") <<
"readFile enter/start";
148 fb =
new art::FileBlock(art::FileFormatVersion(1,
"RawEvent2011"),
"nothing");
168 art::SubRunPrincipal*
const & inSR,
169 art::RunPrincipal*& outR,
170 art::SubRunPrincipal*& outSR,
171 art::EventPrincipal*& outE)
173 TLOG_DEBUG(
"SharedMemoryReader") <<
"readNext BEGIN" ;
191 bool keep_looping =
true;
192 bool got_event =
false;
194 if (sleepTimeUsec > 100000) sleepTimeUsec = 100000;
197 keep_looping =
false;
198 auto start_time = std::chrono::steady_clock::now();
199 while (!got_event && TimeUtils::GetElapsedTimeMicroseconds(start_time) < 1000)
204 while (!got_event && TimeUtils::GetElapsedTime(start_time) <
waiting_time)
209 usleep(sleepTimeUsec);
215 TLOG_INFO(
"SharedMemoryReader")
216 <<
"InputFailure: Reading timed out in SharedMemoryReader::readNext()" ;
223 TLOG_INFO(
"SharedMemoryReader") <<
"Did not receive an event from Shared Memory, returning false" ;
227 TLOG_DEBUG(
"SharedMemoryReader") <<
"Got Event!" ;
229 auto errflag =
false;
231 if (errflag)
goto start;
233 if (errflag)
goto start;
234 if (fragmentTypes.size() == 0)
236 TLOG_ERROR(
"SharedMemoryReader") <<
"Event has no Fragments! Aborting!" ;
240 auto firstFragmentType = *fragmentTypes.begin();
241 TLOG_DEBUG(
"SharedMemoryReader") <<
"First Fragment type is " << (int)firstFragmentType <<
" (" <<
fragment_type_map_[firstFragmentType] <<
")" ;
248 if (firstFragmentType == Fragment::EndOfDataFragmentType)
250 TLOG_DEBUG(
"SharedMemoryReader") <<
"Received shutdown message, returning false" ;
261 art::Timestamp currentTime = time(0);
264 if (inR == 0 || inR->run() != evtHeader->run_id)
266 outR =
pmaker.makeRunPrincipal(evtHeader->run_id,
270 if (firstFragmentType == Fragment::EndOfRunFragmentType)
272 art::EventID
const evid(art::EventID::flushEvent());
273 outR =
pmaker.makeRunPrincipal(evid.runID(), currentTime);
274 outSR =
pmaker.makeSubRunPrincipal(evid.subRunID(), currentTime);
275 outE =
pmaker.makeEventPrincipal(evid, currentTime);
279 else if (firstFragmentType == Fragment::EndOfSubrunFragmentType)
282 if (inR == 0 || inR->run() != evtHeader->run_id)
284 outSR =
pmaker.makeSubRunPrincipal(evtHeader->run_id,
285 evtHeader->subrun_id,
287 art::EventID
const evid(art::EventID::flushEvent(outSR->id()));
288 outE =
pmaker.makeEventPrincipal(evid, currentTime);
296 if (inSR != 0 && !inSR->id().isFlush() && inSR->subRun() == evtHeader->subrun_id)
298 art::EventID
const evid(art::EventID::flushEvent(inR->id()));
299 outSR =
pmaker.makeSubRunPrincipal(evid.subRunID(), currentTime);
300 outE =
pmaker.makeEventPrincipal(evid, currentTime);
307 outSR =
pmaker.makeSubRunPrincipal(evtHeader->run_id,
308 evtHeader->subrun_id,
310 art::EventID
const evid(art::EventID::flushEvent(outSR->id()));
311 outE =
pmaker.makeEventPrincipal(evid, currentTime);
323 art::SubRunID subrun_check(evtHeader->run_id, evtHeader->subrun_id);
324 if (inSR == 0 || subrun_check != inSR->id())
326 outSR =
pmaker.makeSubRunPrincipal(evtHeader->run_id,
327 evtHeader->subrun_id,
330 outE =
pmaker.makeEventPrincipal(evtHeader->run_id,
331 evtHeader->subrun_id,
336 std::map<Fragment::type_t, std::string>::const_iterator iter_end =
338 for (
auto& type_code : fragmentTypes)
340 std::map<Fragment::type_t, std::string>::const_iterator iter =
342 auto product =
incoming_events->GetFragmentsByType(errflag, type_code);
343 if (errflag)
goto start;
344 for (
auto &frag : *product)
346 if (iter != iter_end)
348 if (type_code == artdaq::Fragment::ContainerFragmentType)
350 std::unordered_map<std::string, std::unique_ptr<Fragments>> derived_fragments;
351 derived_fragments[iter->second] = std::make_unique<Fragments>();
353 for (
size_t ii = 0; ii < product->size(); ++ii)
355 ContainerFragment cf(product->at(ii));
357 if (contained_type != iter_end)
359 auto label = iter->second + contained_type->second;
360 if (!derived_fragments.count(label))
362 derived_fragments[label] = std::make_unique<Fragments>();
364 derived_fragments[label]->emplace_back(std::move(product->at(ii)));
368 derived_fragments[iter->second]->emplace_back(std::move(product->at(ii)));
372 for (
auto& type : derived_fragments)
374 put_product_in_principal(std::move(type.second),
383 put_product_in_principal(std::move(product),
391 put_product_in_principal(std::move(product),
395 TLOG_WARNING(
"SharedMemoryReader")
396 <<
"UnknownFragmentType: The product instance name mapping for fragment type \""
397 << ((int)type_code) <<
"\" is not known. Fragments of this "
398 <<
"type will be stored in the event with an instance name of \""
403 TLOG_ARB(10,
"SharedMemoryReader") <<
"readNext: bytesRead=" <<
bytesRead <<
" qsize=" << qsize <<
" cap=" <<
incoming_events->size() <<
" metricMan=" << (
void*)metricMan ;
406 metricMan->sendMetric(
"bytesRead",
bytesRead,
"B", 5, MetricMode::Accumulate,
"",
true);
407 metricMan->sendMetric(
"queue%Used", static_cast<unsigned long int>(qsize * 100 /
incoming_events->size()),
"%", 5, MetricMode::LastPoint,
"",
true);
The SharedMemoryReader is a class which implements the methods needed by art::Source.
unsigned readNext_calls_
The number of times readNext has been called.
SharedMemoryReader(fhicl::ParameterSet const &ps, art::ProductRegistryHelper &help, art::SourceHelper const &pm)
SharedMemoryReader Constructor.
SharedMemoryReader(fhicl::ParameterSet const &ps, art::ProductRegistryHelper &help, art::SourceHelper const &pm, art::MasterProductRegistry &)
SharedMemoryReader Constructor.
virtual ~SharedMemoryReader()=default
SharedMemoryReader destructor.
art::SourceHelper const & pmaker
An art::SourceHelper instance.
SharedMemoryReader & operator=(SharedMemoryReader const &)=delete
Copy Assignment operator is deleted.
double waiting_time
The amount of time to wait for an event from the queue.
bool resume_after_timeout
Whether to resume if the dequeue action times out.
std::string unidentified_instance_name
The name to use for unknown Fragment types.
bool outputFileCloseNeeded
If an explicit output file close message is needed.
std::unique_ptr< SharedMemoryEventReceiver > incoming_events
The events from the EventStore.
size_t bytesRead
running total of number of bytes received
void closeCurrentFile()
Emulate closing a file. No-Op.
std::string pretend_module_name
The module name to store data under.
bool readNext(art::RunPrincipal *const &inR, art::SubRunPrincipal *const &inSR, art::RunPrincipal *&outR, art::SubRunPrincipal *&outSR, art::EventPrincipal *&outE)
Dequeue a RawEvent and declare its Fragment contents to art, creating Run, SubRun, and EventPrincipal objects as necessary.
std::map< Fragment::type_t, std::string > fragment_type_map_
The Fragment type names that this SharedMemoryReader knows about.
bool hasMoreData() const
Whether more data is expected from the SharedMemoryReader.
void readFile(std::string const &, art::FileBlock *&fb)
Emulate opening a file.
SharedMemoryReader(SharedMemoryReader const &)=delete
Copy Constructor is deleted.
bool shutdownMsgReceived
Whether a shutdown message has been received.