1 #define TRACE_NAME "shared_memory_reader_t"
3 #include "artdaq/ArtModules/detail/SharedMemoryReader.hh"
4 #include "artdaq/DAQrate/SharedMemoryEventManager.hh"
6 #include "art/Framework/Core/FileBlock.h"
8 #include "art/Framework/IO/Sources/SourceHelper.h"
9 #include "art/Framework/Principal/Event.h"
10 #include "art/Framework/Principal/EventPrincipal.h"
11 #include "art/Framework/Principal/Handle.h"
12 #include "art/Framework/Principal/RunPrincipal.h"
13 #include "art/Framework/Principal/SubRunPrincipal.h"
15 #if ART_HEX_VERSION < 0x20900
16 #include "art/Persistency/Provenance/BranchIDListRegistry.h"
17 #include "canvas/Utilities/GetPassID.h"
19 #include "art/Persistency/Provenance/MasterProductRegistry.h"
20 #include "art/Persistency/Provenance/ProductMetaData.h"
21 #include "canvas/Persistency/Provenance/EventID.h"
22 #include "canvas/Persistency/Provenance/FileFormatVersion.h"
23 #include "canvas/Persistency/Provenance/ModuleDescription.h"
24 #include "canvas/Persistency/Provenance/Parentage.h"
25 #include "canvas/Persistency/Provenance/ProcessConfiguration.h"
26 #include "canvas/Persistency/Provenance/RunID.h"
27 #include "canvas/Persistency/Provenance/SubRunID.h"
28 #include "canvas/Persistency/Provenance/Timestamp.h"
29 #include "canvas/Utilities/Exception.h"
30 #include "art/Version/GetReleaseVersion.h"
31 #include "artdaq-core/Data/Fragment.hh"
32 #include "artdaq-core/Utilities/configureMessageFacility.hh"
33 #include "fhiclcpp/make_ParameterSet.h"
35 #define BOOST_TEST_MODULE shared_memory_reader_t
36 #include <boost/test/auto_unit_test.hpp>
56 typedef std::map<std::string, art::BranchKey>
BKmap_t;
70 art::ProcessConfiguration*
72 std::string
const& processName,
73 fhicl::ParameterSet
const& moduleParams,
74 std::string
const& release = art::getReleaseVersion()
75 #
if ART_HEX_VERSION < 0x20800
76 , std::string
const& pass = art::getPassID()
87 std::unique_ptr<art::BranchDescription>
89 std::string
const& processName,
90 std::string
const& productInstanceName = std::string());
104 , processConfigurations_()
120 #if ART_HEX_VERSION < 0x20900
126 art::ProcessConfiguration*
129 std::string
const& processName,
130 fhicl::ParameterSet
const& moduleParams,
131 std::string
const& release
132 #
if ART_HEX_VERSION < 0x20800
133 , std::string
const& pass
137 fhicl::ParameterSet processParams;
138 processParams.put(processName, moduleParams);
139 processParams.put<std::string>(
"process_name",
143 std::make_unique<art::ProcessConfiguration>(processName, processParams.id(), release, pass));
144 return emplace_pair.first->second.get();
147 std::unique_ptr<art::BranchDescription>
150 std::string
const& processName,
151 std::string
const& productInstanceName)
153 std::string moduleLabel = processName +
"dummyMod";
154 std::string moduleClass(
"DummyModule");
155 fhicl::ParameterSet modParams;
156 modParams.put<std::string>(
"module_type", moduleClass);
157 modParams.put<std::string>(
"module_label", moduleLabel);
158 art::ProcessConfiguration* process =
160 art::ModuleDescription mod(modParams.id(),
164 art::TypeID dummyType(
typeid(
int));
165 art::BranchDescription* result =
166 new art::BranchDescription(
168 art::TypeLabel(dummyType,
169 productInstanceName),
173 branchKeys_.insert(std::make_pair(tag, art::BranchKey(*result)));
174 return std::unique_ptr<art::BranchDescription>(result);
187 static bool once(
true);
190 artdaq::configureMessageFacility(
"shared_memory_reader_t");
192 art::ModuleDescription md(fhicl::ParameterSet().
id(),
195 *
gf().processConfigurations_[
"daq"]);
198 helper().registerProducts(
gf().productRegistry_, md);
220 static art::ProductRegistryHelper s_helper;
230 static std::unique_ptr<art::SourceHelper>
232 if (!s_source_helper)
234 fhicl::ParameterSet sourceParams;
235 std::string moduleType{
"DummySource" };
236 std::string moduleLabel{
"daq" };
237 sourceParams.put<std::string>(
"module_type", moduleType);
238 sourceParams.put<std::string>(
"module_label", moduleLabel);
242 art::ModuleDescription md(sourceParams.id(),
246 s_source_helper = std::make_unique<art::SourceHelper>(md);
248 return *s_source_helper;
257 static uint32_t key =
static_cast<uint32_t
>(std::hash<std::string>()(
"shared_memory_reader_t"));
266 static uint32_t key =
static_cast<uint32_t
>(std::hash<std::string>()(
"shared_memory_reader_t BROADCAST"));
277 fhicl::ParameterSet pset;
278 pset.put(
"shared_memory_key",
getKey());
280 pset.put(
"max_event_size_bytes", 0x100000);
281 pset.put(
"buffer_count", 10);
286 gf().productRegistry_);
287 static bool reader_initialized =
false;
288 if (!reader_initialized)
291 helper().reconstitutes<artdaq::Fragments, art::InEvent>(
"daq",
"ABCDEF");
292 reader_initialized =
true;
303 fhicl::ParameterSet pset;
304 pset.put(
"shared_memory_key",
getKey());
306 pset.put(
"max_event_size_bytes", 0x100000);
307 pset.put(
"art_analyzer_count", 0);
308 pset.put(
"stale_buffer_timeout_usec", 100000);
309 pset.put(
"expected_fragments_per_event", 1);
310 pset.put(
"buffer_count", 10);
312 s_writer(pset, pset);
332 std::unique_ptr<art::RunPrincipal>&& run,
333 std::unique_ptr<art::SubRunPrincipal>&& subrun,
334 art::EventID
const& eventid)
336 BOOST_REQUIRE(run || subrun ==
nullptr);
337 std::vector<artdaq::Fragment::value_type> fakeData{ 1, 2, 3, 4 };
339 tmpFrag(artdaq::Fragment::dataFrag(eventid.event(),
343 tmpFrag->setUserType(1);
348 auto iter = tmpFrag->dataBegin();
349 std::ostringstream str;
351 while (iter != tmpFrag->dataEnd())
353 str << *iter <<
", ";
358 TLOG(TLVL_DEBUG) <<
"Fragment to art: " << str.str();
361 artdaq::FragmentPtr tempFrag;
362 auto sts = writer.AddFragment(std::move(tmpFrag), 1000000, tempFrag);
363 BOOST_REQUIRE_EQUAL(sts,
true);
371 art::EventPrincipal* newevent =
nullptr;
372 art::SubRunPrincipal* newsubrun =
nullptr;
373 art::RunPrincipal* newrun =
nullptr;
374 bool rc = reader.
readNext(run.get(), subrun.get(), newrun, newsubrun, newevent);
376 if (run.get() && run->run() == eventid.run())
378 BOOST_CHECK(newrun ==
nullptr);
383 BOOST_CHECK(newrun->id() == eventid.runID());
385 if (!newrun && subrun.get() && subrun->subRun() == eventid.subRun())
387 BOOST_CHECK(newsubrun ==
nullptr);
391 BOOST_CHECK(newsubrun);
392 BOOST_CHECK(newsubrun->id() == eventid.subRunID());
394 BOOST_CHECK(newevent);
395 BOOST_CHECK(newevent->id() == eventid);
396 art::Event e(*newevent, art::ModuleDescription());
397 art::Handle<std::vector<artdaq::Fragment>> h;
398 e.getByLabel(
"daq",
"ABCDEF", h);
399 BOOST_CHECK(h.isValid());
400 BOOST_CHECK(h->size() == 1);
402 auto iter2 = h->front().dataBegin();
403 std::ostringstream str2;
405 while (iter2 != h->front().dataEnd())
407 str2 << *iter2 <<
", ";
412 TLOG(TLVL_DEBUG) <<
"Fragment from art: " << str2.str();
414 BOOST_CHECK(std::equal(fakeData.begin(),
416 h->front().dataBegin()));
425 art::EventID eventid(2112, 1, 3);
427 basic_test(reader(), writer(),
428 std::unique_ptr<art::RunPrincipal>(source_helper().makeRunPrincipal(eventid.run(), now)),
429 std::unique_ptr<art::SubRunPrincipal>(source_helper().makeSubRunPrincipal(eventid.run(), eventid.subRun(), now)),
435 art::EventID eventid(2112, 1, 3);
437 basic_test(reader(), writer(),
445 art::EventID eventid(2112, 1, 3);
447 basic_test(reader(), writer(),
448 std::unique_ptr<art::RunPrincipal>(source_helper().makeRunPrincipal(eventid.run(), now)),
449 std::unique_ptr<art::SubRunPrincipal>(source_helper().makeSubRunPrincipal(eventid.run(), 0, now)),
455 art::EventID eventid(2112, 1, 3);
457 basic_test(reader(), writer(),
458 std::unique_ptr<art::RunPrincipal>(source_helper().makeRunPrincipal(eventid.run() - 1, now)),
459 std::unique_ptr<art::SubRunPrincipal>(source_helper().makeSubRunPrincipal(eventid.run() - 1,
470 std::string
const fakeFileName(
"no such file exists");
471 art::FileBlock* pFile =
nullptr;
472 reader().readFile(fakeFileName, pFile);
474 BOOST_CHECK(pFile->fileFormatVersion() == art::FileFormatVersion(1,
"RawEvent2011"));
475 BOOST_CHECK(pFile->tree() ==
nullptr);
477 BOOST_CHECK(!pFile->fastClonable());
481 art::RunID runid(2112);
482 art::SubRunID subrunid(2112, 1);
483 art::EventID eventid(2112, 1, 3);
485 std::unique_ptr<art::RunPrincipal> run(source_helper().makeRunPrincipal(runid.run(), now));
486 std::unique_ptr<art::SubRunPrincipal> subrun(source_helper().makeSubRunPrincipal(runid.run(), subrunid.subRun(), now));
487 std::unique_ptr<art::EventPrincipal> event(source_helper().makeEventPrincipal(runid.run(),
491 writer().endOfData();
492 art::EventPrincipal* newevent =
nullptr;
493 art::SubRunPrincipal* newsubrun =
nullptr;
494 art::RunPrincipal* newrun =
nullptr;
495 bool rc = reader().readNext(run.get(), subrun.get(), newrun, newsubrun, newevent);
497 BOOST_CHECK(newrun ==
nullptr);
498 BOOST_CHECK(newsubrun ==
nullptr);
499 BOOST_CHECK(newevent ==
nullptr);
502 BOOST_AUTO_TEST_SUITE_END()
SharedMemoryReader Test Fixture.
std::map< std::string, std::unique_ptr< art::ProcessConfiguration > > processConfigurations_
Configurations.
The SharedMemoryReader is a class which implements the methods needed by art::Source.
void finalize()
Finalizes the ProductRegistry.
size_t GetLockedBufferCount()
Returns the number of buffers currently owned by this manager.
The SharedMemoryEventManager is a SharedMemoryManger which tracks events as they are built...
ShmRTestFixture()
ShmRTestFixture Constructor.
artdaq::SharedMemoryEventManager & writer()
Get the instance of the SharedMemoryEventManager.
art::MasterProductRegistry productRegistry_
MasterProductRegistry instance.
uint32_t getBroadcastKey()
Gets the key for the broadcast shared memory segment.
art::ProcessConfiguration * fake_single_module_process(std::string const &tag, std::string const &processName, fhicl::ParameterSet const &moduleParams, std::string const &release=art::getReleaseVersion(), std::string const &pass=art::getPassID())
Create the ProcessConfiguration for a single module art process.
void basic_test(artdaq::detail::SharedMemoryReader<> &reader, artdaq::SharedMemoryEventManager &writer, std::unique_ptr< art::RunPrincipal > &&run, std::unique_ptr< art::SubRunPrincipal > &&subrun, art::EventID const &eventid)
Run a basic checkout of the SharedMemoryReader.
std::unique_ptr< art::BranchDescription > fake_single_process_branch(std::string const &tag, std::string const &processName, std::string const &productInstanceName=std::string())
Create a BranchDescription for a process.
MasterProductRegistry Test Fixture.
uint32_t getKey()
Gets the key for the shared memory segment.
void sendMetrics()
Send metrics to the MetricManager, if one has been instantiated in the application.
std::map< std::string, art::BranchKey > BKmap_t
BKmap_t associates a string with a art::BranchKey.
MPRGlobalTestFixture()
MPRGlobalTestFixture Constructor.
artdaq::detail::SharedMemoryReader & reader()
Get an artdaq::detail::SharedMemoryReader object, creating a static instance if necessary.
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.
BKmap_t branchKeys_
Keys in this test fixture.
std::map< Fragment::type_t, std::string > fragment_type_map_
The Fragment type names that this SharedMemoryReader knows about.
MPRGlobalTestFixture & gf()
Get a MPRGlobalTestFixture, creating a static instance if necessary.
art::SourceHelper & source_helper()
Get an art::SourceHelper object, creating a static instance if necessary.
art::ProductRegistryHelper & helper()
Get an art::ProductRegistryHelper, creating a static instance if necessary.
void startRun(run_id_t runID)
Start a Run.