1 #include "artdaq/DAQdata/Globals.hh"
2 #define TRACE_NAME (app_name + "_ShmemTransfer").c_str()
4 #include "artdaq/TransferPlugins/ShmemTransfer.hh"
5 #include "cetlib_except/exception.h"
11 TLOG(TLVL_DEBUG) << GetTraceName() <<
": Constructor BEGIN";
33 throw cet::exception(
"ConfigurationException",
"Buffer Count is too large for Shmem transfer!");
36 auto partition = GetPartitionNumber() + 1;
38 auto shmKey = pset.get<uint32_t>(
"shm_key_offset", 0) + (partition << 24) + ((
source_rank() & 0xFFF) << 12) + (
destination_rank() & 0xFFF);
41 if (pset.has_key(
"shm_key")) {
42 shmKey = pset.get<uint32_t>(
"shm_key");
51 shm_manager_ = std::make_unique<SharedMemoryFragmentManager>(shmKey);
53 TLOG(TLVL_DEBUG) << GetTraceName() <<
": Constructor END";
58 TLOG(5) << GetTraceName() <<
" ~ShmemTransfer called - " << uniqueLabel() ;
59 shm_manager_.reset(
nullptr);
60 TLOG(5) << GetTraceName() <<
" ~ShmemTransfer done - " << uniqueLabel() ;
64 size_t receiveTimeout)
66 auto waitStart = std::chrono::steady_clock::now();
67 while (!shm_manager_->ReadyForRead() && TimeUtils::GetElapsedTimeMicroseconds(waitStart) < 1000)
71 if (!shm_manager_->ReadyForRead())
73 int64_t loopCount = 0;
74 size_t sleepTime = 1000;
75 int64_t nloops = (receiveTimeout - 1000) / sleepTime;
77 while (!shm_manager_->ReadyForRead() && loopCount < nloops)
84 TLOG(TLVL_TRACE) << GetTraceName() <<
": receiveFragment ReadyForRead=" << shm_manager_->ReadyForRead();
86 if (shm_manager_->ReadyForRead())
88 auto sts = shm_manager_->ReadFragment(fragment);
91 TLOG(TLVL_TRACE) <<
"Non-zero status (" << sts <<
") returned from ReadFragment, returning...";
95 if (fragment.type() != artdaq::Fragment::DataFragmentType)
97 TLOG(8) << GetTraceName() <<
": Recvd frag from shmem, type=" << fragment.typeString() <<
", sequenceID=" << fragment.sequenceID() <<
", source_rank=" << source_rank() ;
100 return source_rank();
108 auto waitStart = std::chrono::steady_clock::now();
109 while (!shm_manager_->ReadyForRead() && TimeUtils::GetElapsedTimeMicroseconds(waitStart) < 1000)
113 if (!shm_manager_->ReadyForRead())
115 int64_t loopCount = 0;
116 size_t sleepTime = 1000;
117 int64_t nloops = (receiveTimeout - 1000) / sleepTime;
119 while (!shm_manager_->ReadyForRead() && loopCount < nloops)
126 if (!shm_manager_->ReadyForRead() && shm_manager_->IsEndOfData())
133 if (shm_manager_->ReadyForRead())
135 auto sts = shm_manager_->ReadFragmentHeader(header);
138 TLOG(TLVL_TRACE) <<
"Non-zero status (" << sts <<
") returned from ReadFragmentHeader, returning...";
142 if (header.type != artdaq::Fragment::DataFragmentType)
144 TLOG(8) << GetTraceName() <<
": Recvd fragment header from shmem, type=" << (int)header.type <<
", sequenceID=" << header.sequence_id <<
", source_rank=" << source_rank() ;
147 return source_rank();
155 auto sts = shm_manager_->ReadFragmentData(destination, word_count);
157 TLOG(TLVL_TRACE) << GetTraceName() <<
": Return status from ReadFragmentData is " << sts ;
160 TLOG(TLVL_TRACE) <<
"Non-zero status (" << sts <<
") returned from ReadFragmentData, returning...";
164 return source_rank();
172 return sendFragment(Fragment(fragment), send_timeout_usec,
false);
178 return sendFragment(std::move(fragment), 0,
true);
182 artdaq::ShmemTransfer::sendFragment(artdaq::Fragment&& fragment,
size_t send_timeout_usec,
bool reliableMode)
184 if (!shm_manager_->IsValid()) {
185 shm_manager_->Attach();
186 if (!shm_manager_->IsValid()) {
187 TLOG(TLVL_ERROR) << GetTraceName() <<
": Attempted to send Fragment when not attached to Shared Memory! Returning kSuccess, and dropping data!";
188 return CopyStatus::kSuccess;
191 shm_manager_->SetRank(my_rank);
194 TLOG(5) << GetTraceName() <<
": Sending fragment with seqID=" << fragment.sequenceID() ;
195 artdaq::RawDataType* fragAddr = fragment.headerAddress();
196 size_t fragSize = fragment.size() *
sizeof(artdaq::RawDataType);
200 if (fragment.type() != artdaq::Fragment::InvalidFragmentType && fragSize < (max_fragment_size_words_ *
sizeof(artdaq::RawDataType)))
202 TLOG(5) << GetTraceName() <<
": Writing fragment with seqID=" << fragment.sequenceID();
203 auto sts = shm_manager_->WriteFragment(std::move(fragment), !reliableMode, send_timeout_usec);
206 TLOG(TLVL_WARNING) << GetTraceName() <<
": Timeout writing fragment with seqID=" << fragment.sequenceID();
207 return CopyStatus::kTimeout;
211 TLOG(TLVL_WARNING) << GetTraceName() <<
": Error writing fragment with seqID=" << fragment.sequenceID();
212 return CopyStatus::kErrorNotRequiringException;
215 TLOG(5) << GetTraceName() <<
": Successfully sent Fragment with seqID=" << fragment.sequenceID();
216 return CopyStatus::kSuccess;
220 TLOG(TLVL_WARNING) << GetTraceName() <<
": Fragment invalid for shared memory! "
221 <<
"fragment address and size = "
222 << fragAddr <<
" " << fragSize <<
" "
223 <<
"sequence ID, fragment ID, and type = "
224 << fragment.sequenceID() <<
" "
225 << fragment.fragmentID() <<
" "
226 << fragment.typeString() ;
227 return CopyStatus::kErrorNotRequiringException;
230 TLOG(TLVL_WARNING) << GetTraceName() <<
": Unreachable code reached!" ;
231 return CopyStatus::kErrorNotRequiringException;
240 ret = shm_manager_->IsValid();
243 ret = shm_manager_->GetAttachedCount() > 1;
252 for (
size_t ii = 0; ii < shm_manager_->size(); ++ii)
254 shm_manager_->MarkBufferEmpty(ii,
true);
size_t buffer_count_
The number of Fragment transfers the TransferInterface can handle simultaneously. ...
virtual int source_rank() const
Get the source rank for this TransferInterface instance.
CopyStatus transfer_fragment_reliable_mode(Fragment &&fragment) override
Transfer a Fragment to the destination. This should be reliable, if the underlying transport mechanis...
CopyStatus transfer_fragment_min_blocking_mode(Fragment const &fragment, size_t send_timeout_usec) override
Transfer a Fragment to the destination. May not necessarily be reliable, but will not block longer th...
This TransferInterface is a Receiver.
int receiveFragmentHeader(detail::RawFragmentHeader &header, size_t receiveTimeout) override
Receive a Fragment Header from the transport mechanism.
void flush_buffers() override
Flush any in-flight data. This should be used by the receiver after the receive loop has ended...
ShmemTransfer(fhicl::ParameterSet const &pset, Role role)
ShmemTransfer Constructor.
Value that is to be returned when a Transfer plugin determines that no more data will be arriving...
int receiveFragmentData(RawDataType *destination, size_t wordCount) override
Receive the body of a Fragment to the given destination pointer.
This TransferInterface is a Sender.
virtual ~ShmemTransfer() noexcept
ShmemTransfer Destructor.
Role
Used to determine if a TransferInterface is a Sender or Receiver.
A TransferInterface implementation plugin that transfers data using Shared Memory.
bool isRunning() override
Determine whether the TransferInterface plugin is able to send/receive data.
This interface defines the functions used to transfer data between artdaq applications.
virtual int destination_rank() const
Get the destination rank for this TransferInterface instance.
int receiveFragment(Fragment &fragment, size_t receiveTimeout) override
Receive a Fragment from Shared Memory.
Value to be returned upon receive timeout.
CopyStatus
Returned from the send functions, this enumeration describes the possible return codes. If an exception occurs, it will be thrown and should be handled normally.
const size_t max_fragment_size_words_
The maximum size of the transferred Fragment objects, in artdaq::Fragment::RawDataType words...