1 #ifndef artdaq_ArtModules_detail_SharedMemoryReader_hh
2 #define artdaq_ArtModules_detail_SharedMemoryReader_hh
4 #include "artdaq/DAQdata/Globals.hh"
5 #include "artdaq-core/Utilities/ExceptionHandler.hh"
6 #include "art/Framework/Core/Frameworkfwd.h"
8 #include "art/Framework/Core/FileBlock.h"
9 #include "art/Framework/Core/ProductRegistryHelper.h"
10 #include "art/Framework/IO/Sources/SourceHelper.h"
11 #include "art/Framework/Principal/EventPrincipal.h"
12 #include "art/Framework/Principal/RunPrincipal.h"
13 #include "art/Framework/Principal/SubRunPrincipal.h"
14 #include "artdaq-core/Core/SharedMemoryManager.hh"
15 #include "artdaq-core/Utilities/TimeUtils.hh"
16 #include "fhiclcpp/ParameterSet.h"
17 #include "artdaq-core/Data/Fragment.hh"
18 #include "artdaq-core/Data/ContainerFragment.hh"
19 #include "artdaq-core/Core/SharedMemoryEventReceiver.hh"
20 #include "art/Framework/IO/Sources/put_product_in_principal.h"
21 #include "canvas/Persistency/Provenance/FileFormatVersion.h"
27 #include "artdaq-core/Data/RawEvent.hh"
36 template<std::map<artdaq::Fragment::type_t, std::
string> getDefaultTypes() = artdaq::Fragment::MakeSystemTypeMap >
77 art::ProductRegistryHelper& help,
78 art::SourceHelper
const& pm)
93 metricMan->initialize(ps.get<fhicl::ParameterSet>(
"metrics", fhicl::ParameterSet()),
"artdaqart");
94 metricMan->do_start();
99 ExceptionHandler(ExceptionHandlerRethrow::no,
"Error loading metrics in SharedMemoryReader()");
108 incoming_events.reset(
new SharedMemoryEventReceiver(ps.get<uint32_t>(
"shared_memory_key", 0xBEE70000 + getppid()), ps.get<uint32_t>(
"broadcast_shared_memory_key", 0xCEE70000 + getppid())));
115 help.reconstitutes<Fragments, art::InEvent>(
pretend_module_name,
"Container" + it->second);
117 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>>());
118 for (
auto it = extraTypes.begin(); it != extraTypes.end(); ++it)
122 help.reconstitutes<Fragments, art::InEvent>(
pretend_module_name,
"Container" + it->second);
124 TLOG_INFO(
"SharedMemoryReader") <<
"SharedMemoryReader initialized with ParameterSet: " << ps.to_string();
140 art::ProductRegistryHelper& help,
141 art::SourceHelper
const& pm,
149 artdaq::Globals::CleanUpGlobals();
161 void readFile(std::string
const&, art::FileBlock*& fb)
163 TLOG_ARB(5,
"SharedMemoryReader") <<
"readFile enter/start";
164 fb =
new art::FileBlock(art::FileFormatVersion(1,
"RawEvent2011"),
"nothing");
184 art::SubRunPrincipal*
const & inSR,
185 art::RunPrincipal*& outR,
186 art::SubRunPrincipal*& outSR,
187 art::EventPrincipal*& outE)
189 TLOG_DEBUG(
"SharedMemoryReader") <<
"readNext BEGIN";
206 TLOG_INFO(
"SharedMemoryReader") <<
"Shutdown Message received, returning false (should exit art)";
211 bool keep_looping =
true;
212 bool got_event =
false;
214 if (sleepTimeUsec > 100000) sleepTimeUsec = 100000;
217 TLOG_TRACE(
"SharedMemoryReader") <<
"ReadyForRead loops BEGIN";
218 keep_looping =
false;
219 auto start_time = std::chrono::steady_clock::now();
220 while (!got_event && TimeUtils::GetElapsedTimeMicroseconds(start_time) < 1000)
225 TLOG_TRACE(
"SharedMemoryReader") <<
"ReadyForRead spin end, poll begin";
226 while (!got_event && TimeUtils::GetElapsedTime(start_time) <
waiting_time)
231 usleep(sleepTimeUsec);
235 TLOG_TRACE(
"SharedMemoryReader") <<
"ReadyForRead loops END";
238 TLOG_INFO(
"SharedMemoryReader")
239 <<
"InputFailure: Reading timed out in SharedMemoryReader::readNext()";
246 TLOG_INFO(
"SharedMemoryReader") <<
"Did not receive an event from Shared Memory, returning false";
250 TLOG_DEBUG(
"SharedMemoryReader") <<
"Got Event!";
252 auto errflag =
false;
254 if (errflag)
goto start;
256 if (errflag)
goto start;
257 if (fragmentTypes.size() == 0)
259 TLOG_ERROR(
"SharedMemoryReader") <<
"Event has no Fragments! Aborting!";
263 auto firstFragmentType = *fragmentTypes.begin();
264 TLOG_DEBUG(
"SharedMemoryReader") <<
"First Fragment type is " << (int)firstFragmentType <<
" (" <<
fragment_type_map_[firstFragmentType] <<
")";
271 if (firstFragmentType == Fragment::EndOfDataFragmentType)
273 TLOG_DEBUG(
"SharedMemoryReader") <<
"Received shutdown message, returning false";
284 art::Timestamp currentTime = 0;
286 art::TimeValue_t lo_res_time = time(0);
287 TLOG_ARB(15,
"SharedMemoryReader") <<
"lo_res_time = " << lo_res_time;
288 currentTime = ((lo_res_time & 0xffffffff) << 32);
290 timespec hi_res_time;
291 int retcode = clock_gettime(CLOCK_REALTIME, &hi_res_time);
292 TLOG_ARB(15,
"SharedMemoryReader") <<
"hi_res_time tv_sec = " << hi_res_time.tv_sec
293 <<
" tv_nsec = " << hi_res_time.tv_nsec
294 <<
" (retcode = " << retcode <<
")";
297 currentTime = ((hi_res_time.tv_sec & 0xffffffff) << 32) |
298 (hi_res_time.tv_nsec & 0xffffffff);
302 TLOG_ERROR(
"SharedMemoryReader") <<
"Unable to fetch a high-resolution time with clock_gettime for art::Event Timestamp. "
303 <<
"The art::Event Timestamp will be zero for event " << evtHeader->event_id;
307 if (inR == 0 || inR->run() != evtHeader->run_id)
309 outR =
pmaker.makeRunPrincipal(evtHeader->run_id,
313 if (firstFragmentType == Fragment::EndOfRunFragmentType)
315 art::EventID
const evid(art::EventID::flushEvent());
316 outR =
pmaker.makeRunPrincipal(evid.runID(), currentTime);
317 outSR =
pmaker.makeSubRunPrincipal(evid.subRunID(), currentTime);
318 outE =
pmaker.makeEventPrincipal(evid, currentTime);
322 else if (firstFragmentType == Fragment::EndOfSubrunFragmentType)
325 if (inR == 0 || inR->run() != evtHeader->run_id)
327 outSR =
pmaker.makeSubRunPrincipal(evtHeader->run_id,
328 evtHeader->subrun_id,
330 art::EventID
const evid(art::EventID::flushEvent(outSR->id()));
331 outE =
pmaker.makeEventPrincipal(evid, currentTime);
339 if (inSR != 0 && !inSR->id().isFlush() && inSR->subRun() == evtHeader->subrun_id)
341 art::EventID
const evid(art::EventID::flushEvent(inR->id()));
342 outSR =
pmaker.makeSubRunPrincipal(evid.subRunID(), currentTime);
343 outE =
pmaker.makeEventPrincipal(evid, currentTime);
350 outSR =
pmaker.makeSubRunPrincipal(evtHeader->run_id,
351 evtHeader->subrun_id,
353 art::EventID
const evid(art::EventID::flushEvent(outSR->id()));
354 outE =
pmaker.makeEventPrincipal(evid, currentTime);
366 art::SubRunID subrun_check(evtHeader->run_id, evtHeader->subrun_id);
367 if (inSR == 0 || subrun_check != inSR->id())
369 outSR =
pmaker.makeSubRunPrincipal(evtHeader->run_id,
370 evtHeader->subrun_id,
373 outE =
pmaker.makeEventPrincipal(evtHeader->run_id,
374 evtHeader->subrun_id,
379 std::map<Fragment::type_t, std::string>::const_iterator iter_end =
381 for (
auto& type_code : fragmentTypes)
383 std::map<Fragment::type_t, std::string>::const_iterator iter =
385 auto product =
incoming_events->GetFragmentsByType(errflag, type_code);
386 if (errflag)
goto start;
387 for (
auto &frag : *product)
389 if (iter != iter_end)
391 if (type_code == artdaq::Fragment::ContainerFragmentType)
393 std::unordered_map<std::string, std::unique_ptr<Fragments>> derived_fragments;
394 derived_fragments[iter->second] = std::make_unique<Fragments>();
396 for (
size_t ii = 0; ii < product->size(); ++ii)
398 ContainerFragment cf(product->at(ii));
400 if (contained_type != iter_end)
402 auto label = iter->second + contained_type->second;
403 if (!derived_fragments.count(label))
405 derived_fragments[label] = std::make_unique<Fragments>();
407 derived_fragments[label]->emplace_back(std::move(product->at(ii)));
411 derived_fragments[iter->second]->emplace_back(std::move(product->at(ii)));
415 for (
auto& type : derived_fragments)
417 put_product_in_principal(std::move(type.second),
426 put_product_in_principal(std::move(product),
434 put_product_in_principal(std::move(product),
438 TLOG_WARNING(
"SharedMemoryReader")
439 <<
"UnknownFragmentType: The product instance name mapping for fragment type \""
440 << ((int)type_code) <<
"\" is not known. Fragments of this "
441 <<
"type will be stored in the event with an instance name of \""
446 TLOG_ARB(10,
"SharedMemoryReader") <<
"readNext: bytesRead=" <<
bytesRead <<
" qsize=" << qsize <<
" cap=" <<
incoming_events->size() <<
" metricMan=" << (
void*)metricMan.get();
449 metricMan->sendMetric(
"bytesRead",
bytesRead,
"B", 5, MetricMode::Accumulate,
"",
true);
450 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.
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.
virtual ~SharedMemoryReader()
SharedMemoryReader destructor.
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.