1 #define TRACE_NAME "ArtdaqSharedMemoryService"
6 #include "art/Framework/Services/Registry/ServiceHandle.h"
7 #include "artdaq-core/Core/SharedMemoryEventReceiver.hh"
8 #include "artdaq-core/Utilities/ExceptionHandler.hh"
9 #include "artdaq/ArtModules/ArtdaqSharedMemoryService.h"
11 #include "artdaq/DAQdata/Globals.hh"
13 #define build_key(seed) ((seed) + ((GetPartitionNumber() + 1) << 16) + (getppid() & 0xFFFF))
15 static fhicl::ParameterSet empty_pset;
18 : incoming_events_(nullptr)
20 , read_timeout_(pset.get<size_t>(
"read_timeout_us", static_cast<size_t>(pset.get<double>(
"waiting_time", 600.0) * 1000000)))
21 , resume_after_timeout_(pset.get<bool>(
"resume_after_timeout", true))
23 TLOG(TLVL_DEBUG + 33) <<
"ArtdaqSharedMemoryService CONSTRUCTOR";
25 incoming_events_ = std::make_unique<artdaq::SharedMemoryEventReceiver>(
26 pset.get<
int>(
"shared_memory_key", build_key(0xEE000000)),
27 pset.get<
int>(
"broadcast_shared_memory_key", build_key(0xBB000000)));
29 char const* artapp_env = getenv(
"ARTDAQ_APPLICATION_NAME");
30 std::string artapp_str;
31 if (artapp_env !=
nullptr)
33 artapp_str = std::string(artapp_env) +
"_";
36 TLOG(TLVL_DEBUG + 33) <<
"Setting app_name";
37 app_name = artapp_str +
"art" + std::to_string(incoming_events_->GetMyId());
40 artapp_env = getenv(
"ARTDAQ_RANK");
41 if (artapp_env !=
nullptr && my_rank < 0)
43 TLOG(TLVL_DEBUG + 33) <<
"Setting rank from envrionment";
44 my_rank = strtol(artapp_env,
nullptr, 10);
48 TLOG(TLVL_DEBUG + 33) <<
"Setting my_rank from shared memory";
49 my_rank = incoming_events_->GetRank();
56 metricMan->initialize(pset.get<fhicl::ParameterSet>(
"metrics", fhicl::ParameterSet()), app_name);
57 metricMan->do_start();
62 artdaq::ExceptionHandler(artdaq::ExceptionHandlerRethrow::no,
"Error loading metrics in ArtdaqSharedMemoryService()");
65 TLOG(TLVL_INFO) <<
"app_name is " << app_name <<
", rank " << my_rank;
75 TLOG(TLVL_DEBUG + 33) <<
"ReceiveEvent BEGIN";
76 std::unordered_map<artdaq::Fragment::type_t, std::unique_ptr<artdaq::Fragments>> recvd_fragments;
78 while (recvd_fragments.empty())
80 TLOG(TLVL_DEBUG + 33) <<
"ReceiveEvent: Waiting for available buffer";
81 bool got_event =
false;
82 auto start_time = std::chrono::steady_clock::now();
83 auto read_timeout_to_use = read_timeout_ > 100000 ? 100000 : read_timeout_;
84 if (!resume_after_timeout_ || broadcast) read_timeout_to_use = read_timeout_;
85 while (!incoming_events_->IsEndOfData() && !got_event)
87 got_event = incoming_events_->ReadyForRead(broadcast, read_timeout_to_use);
88 if (!got_event && (!resume_after_timeout_ || broadcast))
90 TLOG(TLVL_ERROR) <<
"Timeout occurred! No data received after " << read_timeout_to_use <<
" us. Returning empty Fragment list!";
91 return recvd_fragments;
93 if (!got_event && artdaq::TimeUtils::GetElapsedTimeMicroseconds(start_time) > read_timeout_)
95 TLOG(TLVL_WARNING) <<
"Timeout occurred! No data received after " << artdaq::TimeUtils::GetElapsedTimeMicroseconds(start_time) <<
" us. Retrying.";
98 if (incoming_events_->IsEndOfData())
100 TLOG(TLVL_INFO) <<
"End of Data signal received, exiting";
101 return recvd_fragments;
104 TLOG(TLVL_DEBUG + 33) <<
"ReceiveEvent: Reading buffer header";
105 auto errflag =
false;
106 auto hdrPtr = incoming_events_->ReadHeader(errflag);
107 if (errflag || hdrPtr ==
nullptr)
109 incoming_events_->ReleaseBuffer();
113 evtHeader_ = std::make_shared<artdaq::detail::RawEventHeader>(*hdrPtr);
114 TLOG(TLVL_DEBUG + 33) <<
"ReceiveEvent: Getting Fragment types";
115 auto fragmentTypes = incoming_events_->GetFragmentTypes(errflag);
118 incoming_events_->ReleaseBuffer();
122 if (fragmentTypes.empty())
124 TLOG(TLVL_ERROR) <<
"Event has no Fragments! Aborting!";
125 incoming_events_->ReleaseBuffer();
126 return recvd_fragments;
129 for (
auto const& type : fragmentTypes)
131 TLOG(TLVL_DEBUG + 33) <<
"ReceiveEvent: Getting all Fragments of type " <<
static_cast<int>(type);
132 recvd_fragments[type] = incoming_events_->GetFragmentsByType(errflag, type);
133 if (!recvd_fragments[type])
135 TLOG(TLVL_ERROR) <<
"Error retrieving Fragments from shared memory! (Most likely due to a buffer overwrite) Retrying...";
136 incoming_events_->ReleaseBuffer();
137 recvd_fragments.clear();
143 std::sort(recvd_fragments[type]->begin(), recvd_fragments[type]->end(), artdaq::fragmentSequenceIDCompare);
145 TLOG(TLVL_DEBUG + 33) <<
"ReceiveEvent: Releasing buffer";
146 incoming_events_->ReleaseBuffer();
149 TLOG(TLVL_DEBUG + 33) <<
"ReceiveEvent END";
150 return recvd_fragments;
Interface for ArtdaqSharedMemoryService. This interface is declared to art as part of the required re...
static void CleanUpGlobals()
Clean up statically-allocated Manager class instances.
virtual ~ArtdaqSharedMemoryService()
NetMonTransportService Destructor. Calls disconnect().
ArtdaqSharedMemoryService(fhicl::ParameterSet const &pset, art::ActivityRegistry &)
NetMonTransportService Constructor.
ArtdaqSharedMemoryService extends ArtdaqSharedMemoryServiceInterface. It receives events from shared ...
std::unordered_map< artdaq::Fragment::type_t, std::unique_ptr< artdaq::Fragments > > ReceiveEvent(bool broadcast) override
Receive an event from the shared memory.