2 #define TRACE_NAME "SharedMemoryFragmentManager"
3 #include "artdaq-core/Core/SharedMemoryFragmentManager.hh"
16 TLOG(12) <<
"ReadyForWrite: active_buffer is " << active_buffer_;
17 if (active_buffer_ != -1)
return true;
18 active_buffer_ = GetBufferForWriting(overwrite);
20 return active_buffer_ != -1;
25 if (!IsValid()) {
return -1; }
27 auto waitStart = std::chrono::steady_clock::now();
32 if (!ReadyForWrite(overwrite))
34 int64_t loopCount = 0;
35 size_t sleepTime = 1000;
36 int64_t nloops = (timeout_us - 1000) / sleepTime;
38 while (!ReadyForWrite(overwrite) && (!overwrite || timeout_us == 0 || loopCount < nloops))
44 if (!ReadyForWrite(overwrite))
50 TLOG(13) <<
"Sending fragment with seqID=" << fragment.sequenceID() <<
" using buffer " << active_buffer_;
54 auto sts = Write(active_buffer_, fragAddr, fragSize);
57 TLOG(13) <<
"Done sending Fragment with seqID=" << fragment.sequenceID() <<
" using buffer " << active_buffer_;
58 MarkBufferFull(active_buffer_);
63 TLOG(TLVL_ERROR) <<
"Unexpected status from SharedMemory Write call!";
69 TLOG(14) <<
"ReadFragment BEGIN";
72 TLOG(14) <<
"Reading Fragment Header";
73 auto sts = ReadFragmentHeader(tmpHdr);
74 if (sts != 0)
return sts;
77 TLOG(14) <<
"Reading Fragment Body";
83 if (!IsValid())
return -3;
86 active_buffer_ = GetBufferForReading();
88 if (active_buffer_ == -1)
return -1;
90 auto sts = Read(active_buffer_, &header, hdrSize);
92 TLOG(TLVL_ERROR) <<
"ReadFragmentData: Buffer " << active_buffer_ <<
" returned bad status code from Read";
93 MarkBufferEmpty(active_buffer_);
103 if (!IsValid() || active_buffer_ == -1 || !CheckBuffer(active_buffer_, BufferSemaphoreFlags::Reading)) {
104 TLOG(TLVL_ERROR) <<
"ReadFragmentData: Buffer " << active_buffer_ <<
" failed status checks: IsValid()=" << std::boolalpha << IsValid() <<
", CheckBuffer=" << CheckBuffer(active_buffer_, BufferSemaphoreFlags::Reading);
108 auto sts = Read(active_buffer_, destination, words *
sizeof(
RawDataType));
110 TLOG(TLVL_ERROR) <<
"ReadFragmentData: Buffer " << active_buffer_ <<
" returned bad status code from Read";
111 MarkBufferEmpty(active_buffer_);
116 MarkBufferEmpty(active_buffer_);
constexpr size_t GetElapsedTimeMicroseconds(std::chrono::steady_clock::time_point then, std::chrono::steady_clock::time_point now=std::chrono::steady_clock::now())
Gets the number of microseconds in the given time interval
void resize(std::size_t sz)
Resize the data payload to hold sz RawDataType words.
SharedMemoryFragmentManager(uint32_t shm_key, size_t buffer_count, size_t max_buffer_size, size_t buffer_timeout_us=100 *1000000)
SharedMemoryFragmentManager Constructor.
The SharedMemoryManager creates a Shared Memory area which is divided into a number of fixed-size buf...
int WriteFragment(Fragment &&fragment, bool overwrite, size_t timeout_us)
Write a Fragment to the Shared Memory.
detail::RawFragmentHeader::RawDataType RawDataType
The RawDataType (currently a 64-bit integer) is the basic unit of data representation within artdaq ...
int ReadFragmentHeader(detail::RawFragmentHeader &header)
Read a Fragment Header from the Shared Memory.
int ReadFragment(Fragment &fragment)
Read a Fragment from the Shared Memory.
bool ReadyForWrite(bool overwrite) override
Check if a buffer is ready for writing, and if so, reserves it for use.
A Fragment contains the data from one piece of the DAQ system for one event The artdaq::Fragment is t...
int ReadFragmentData(RawDataType *destination, size_t words)
Read Fragment Data from the Shared Memory.
RawDataType * headerAddress()
Gets the address of the header.