1 #include "TRACE/tracemf.h"
2 #define TRACE_NAME "ShmemWrapper"
4 #include "artdaq/ArtModules/detail/ShmemWrapper.hh"
6 #include "art/Framework/Services/Registry/ServiceHandle.h"
7 #include "artdaq/ArtModules/ArtdaqSharedMemoryServiceInterface.h"
8 #include "artdaq/DAQdata/NetMonHeader.hh"
12 init_timeout_s_ = ps.get<
double>(
"init_fragment_timeout_seconds", 600.0);
14 art::ServiceHandle<ArtdaqSharedMemoryServiceInterface> shm;
19 TLOG(TLVL_DEBUG + 34) <<
"Receiving Fragment from NetMonTransportService";
20 TLOG(TLVL_DEBUG + 33) <<
"receiveMessage BEGIN";
21 art::ServiceHandle<ArtdaqSharedMemoryServiceInterface> shm;
22 artdaq::FragmentPtrs output;
25 auto start = std::chrono::steady_clock::now();
26 while (!init_received_ && artdaq::TimeUtils::GetElapsedTime(start) < init_timeout_s_)
28 usleep(static_cast<unsigned>(init_timeout_s_ * 1000000 / 100));
32 TLOG(TLVL_ERROR) <<
"Did not receive Init Fragment after " << init_timeout_s_ <<
" seconds.";
36 artdaq::Fragments recvd_fragments;
37 while (recvd_fragments.empty())
39 auto eventMap = shm->ReceiveEvent(
false);
43 TLOG(TLVL_DEBUG + 32) <<
"Did not receive event after timeout, returning from receiveMessage ";
47 if (eventMap.count(artdaq::Fragment::type_t(artdaq::Fragment::EndOfDataFragmentType)) != 0u)
49 TLOG(TLVL_DEBUG + 32) <<
"Received shutdown message, returning";
52 if (eventMap.count(artdaq::Fragment::type_t(artdaq::Fragment::DataFragmentType)) != 0u)
54 std::move(eventMap[artdaq::Fragment::type_t(artdaq::Fragment::DataFragmentType)]->begin(), eventMap[artdaq::Fragment::type_t(artdaq::Fragment::DataFragmentType)]->end(), std::back_inserter(recvd_fragments));
56 std::sort(recvd_fragments.begin(), recvd_fragments.end(), artdaq::fragmentSequenceIDCompare);
59 TLOG(TLVL_DEBUG + 33) <<
"receiveMessage: Returning top Fragment";
60 for (
auto& frag : recvd_fragments)
62 TLOG(TLVL_DEBUG + 33) <<
"receiveMessage: Returning Fragment, length="
64 output.emplace_back(
new artdaq::Fragment(std::move(frag)));
67 #if DUMP_RECEIVE_MESSAGE
68 std::string fileName =
"receiveMessage_" + std::to_string(my_rank) +
"_" + std::to_string(getpid()) +
"_" +
69 std::to_string(topFrag.sequenceID()) +
".bin";
70 std::fstream ostream(fileName.c_str(), std::ios::out | std::ios::binary);
71 ostream.write(buffer, header->data_length);
75 TLOG(TLVL_DEBUG + 33) <<
"receiveMessage END";
77 TLOG(TLVL_DEBUG + 34) <<
"Done Receiving Fragment from NetMonTransportService";
83 TLOG(TLVL_DEBUG + 34) <<
"Receiving Fragment from NetMonTransportService";
84 TLOG(TLVL_DEBUG + 33) <<
"receiveMessage BEGIN";
85 art::ServiceHandle<ArtdaqSharedMemoryServiceInterface> shm;
86 std::unordered_map<artdaq::Fragment::type_t, std::unique_ptr<artdaq::Fragments>> output;
89 auto start = std::chrono::steady_clock::now();
90 while (!init_received_ && artdaq::TimeUtils::GetElapsedTime(start) < init_timeout_s_)
92 usleep(static_cast<unsigned>(init_timeout_s_ * 1000000 / 100));
96 TLOG(TLVL_ERROR) <<
"Did not receive Init Fragment after " << init_timeout_s_ <<
" seconds.";
99 output = shm->ReceiveEvent(
false);
103 TLOG(TLVL_DEBUG + 32) <<
"Did not receive event after timeout, returning from receiveMessage ";
107 hdr_ptr_ = shm->GetEventHeader();
108 TLOG(TLVL_DEBUG + 33) <<
"receiveMessage END";
110 TLOG(TLVL_DEBUG + 34) <<
"Done Receiving Fragments from Shared Memory";
116 TLOG(TLVL_DEBUG + 34) <<
"Receiving Init Fragment from NetMonTransportService";
118 TLOG(TLVL_DEBUG + 33) <<
"receiveInitMessage BEGIN";
119 art::ServiceHandle<ArtdaqSharedMemoryServiceInterface> shm;
120 auto start = std::chrono::steady_clock::now();
121 std::unordered_map<artdaq::Fragment::type_t, std::unique_ptr<artdaq::Fragments>> eventMap;
122 while (eventMap.empty())
124 eventMap = shm->ReceiveEvent(
true);
126 if (eventMap.count(artdaq::Fragment::type_t(artdaq::Fragment::EndOfDataFragmentType)) != 0u)
128 TLOG(TLVL_DEBUG + 32) <<
"Received shutdown message, returning";
129 artdaq::FragmentPtrs output;
130 for (
auto& frag : *eventMap[artdaq::Fragment::EndOfDataFragmentType])
132 output.emplace_back(
new artdaq::Fragment(std::move(frag)));
136 if ((eventMap.count(artdaq::Fragment::type_t(artdaq::Fragment::InitFragmentType)) == 0u) && !eventMap.empty())
138 TLOG(TLVL_WARNING) <<
"Did NOT receive Init Fragment as first broadcast! Type="
139 << artdaq::detail::RawFragmentHeader::SystemTypeToString(eventMap.begin()->first);
142 else if (artdaq::TimeUtils::GetElapsedTime(start) > init_timeout_s_)
144 TLOG(TLVL_WARNING) <<
"Did not receive Init fragment after init_fragment_timeout_seconds (" << artdaq::TimeUtils::GetElapsedTime(start) <<
")!";
145 return artdaq::FragmentPtrs();
155 TLOG(TLVL_DEBUG + 33) <<
"receiveInitMessage: Returning top Fragment";
156 artdaq::FragmentPtrs output;
157 for (
auto& frag : *eventMap[artdaq::Fragment::InitFragmentType])
159 output.emplace_back(
new artdaq::Fragment(std::move(frag)));
162 #if DUMP_RECEIVE_MESSAGE
163 std::string fileName =
"receiveInitMessage_" + std::to_string(getpid()) +
".bin";
164 std::fstream ostream(fileName.c_str(), std::ios::out | std::ios::binary);
165 ostream.write(buffer, header->data_length);
169 TLOG(TLVL_DEBUG + 33) <<
"receiveInitMessage END";
170 init_received_ =
true;
172 TLOG(TLVL_DEBUG + 34) <<
"Done Receiving Init Fragment from NetMonTransportService";
std::unordered_map< artdaq::Fragment::type_t, std::unique_ptr< artdaq::Fragments > > receiveMessages()
Receive all messsages for an event from ArtdaqSharedMemoryService.
artdaq::FragmentPtrs receiveMessage()
Receive a message from the ArtdaqSharedMemoryService.
ShmemWrapper(fhicl::ParameterSet const &ps)
ShmemWrapper Constructor.
artdaq::FragmentPtrs receiveInitMessage()
Receive an init message from the ArtdaqSharedMemoryService.