2 #include "artdaq-core/Core/SharedMemoryEventReceiver.hh"
5 #include "artdaq-core/Data/Fragment.hh"
6 #define TRACE_NAME "SharedMemoryEventReceiver"
12 : current_read_buffer_(-1)
14 , current_header_(nullptr)
15 , current_data_source_(nullptr)
17 , broadcasts_(broadcast_shm_key)
19 TLOG(TLVL_TRACE) <<
"SharedMemoryEventReceiver CONSTRUCTOR";
24 TLOG(TLVL_TRACE) <<
"ReadyForRead BEGIN timeout_us=" << timeout_us;
25 if (current_read_buffer_ != -1 && (current_data_source_ !=
nullptr) && (current_header_ !=
nullptr))
27 TLOG(TLVL_TRACE) <<
"ReadyForRead Returning true because already reading buffer";
33 uint64_t time_diff = 0;
34 uint64_t max_sleep = 5000000;
36 while (first || time_diff < timeout_us)
38 if (broadcasts_.ReadyForRead())
40 buf = broadcasts_.GetBufferForReading();
41 current_data_source_ = &broadcasts_;
43 else if (!broadcast && data_.ReadyForRead())
45 buf = data_.GetBufferForReading();
46 current_data_source_ = &data_;
48 if (buf != -1 && (current_data_source_ !=
nullptr))
50 current_read_buffer_ = buf;
51 current_data_source_->ResetReadPos(buf);
53 TLOG(TLVL_TRACE) <<
"ReadyForRead Found buffer, returning true. event hdr sequence_id=" << current_header_->
sequence_id;
56 if (current_data_source_ == &broadcasts_)
59 auto types = GetFragmentTypes(err);
73 current_data_source_ =
nullptr;
77 auto sleep_time = time_diff;
78 if (sleep_time < 10000) sleep_time = 10000;
79 if (sleep_time > max_sleep) sleep_time = max_sleep;
82 TLOG(TLVL_TRACE) <<
"ReadyForRead returning false";
88 TLOG(TLVL_TRACE) <<
"ReadHeader BEGIN";
89 if (current_read_buffer_ != -1 && (current_data_source_ !=
nullptr))
94 TLOG(TLVL_WARNING) <<
"Buffer was in incorrect state, resetting";
95 current_data_source_ =
nullptr;
96 current_read_buffer_ = -1;
97 current_header_ =
nullptr;
101 TLOG(TLVL_TRACE) <<
"Already have buffer, returning stored header";
102 return current_header_;
107 if (current_read_buffer_ == -1 || (current_header_ ==
nullptr) || (current_data_source_ ==
nullptr))
109 throw cet::exception(
"AccessViolation") <<
"Cannot call GetFragmentTypes when not currently reading a buffer! Call ReadHeader() first!";
115 return std::set<Fragment::type_t>();
118 current_data_source_->ResetReadPos(current_read_buffer_);
120 auto output = std::set<Fragment::type_t>();
122 while (current_data_source_->MoreDataInBuffer(current_read_buffer_))
127 return std::set<Fragment::type_t>();
130 output.insert(fragHdr->type);
131 current_data_source_->IncrementReadPos(current_read_buffer_, fragHdr->word_count *
sizeof(
RawDataType));
139 if ((current_data_source_ ==
nullptr) || (current_header_ ==
nullptr) || current_read_buffer_ == -1)
141 throw cet::exception(
"AccessViolation") <<
"Cannot call GetFragmentsByType when not currently reading a buffer! Call ReadHeader() first!";
149 current_data_source_->ResetReadPos(current_read_buffer_);
152 std::unique_ptr<Fragments> output(
new Fragments());
154 while (current_data_source_->MoreDataInBuffer(current_read_buffer_))
165 current_data_source_->Read(current_read_buffer_, output->back().headerAddress(), fragHdr->word_count *
sizeof(
RawDataType));
166 output->back().autoResize();
170 current_data_source_->IncrementReadPos(current_read_buffer_, fragHdr->word_count *
sizeof(
RawDataType));
177 std::string artdaq::SharedMemoryEventReceiver::printBuffers_(
SharedMemoryManager* data_source)
179 std::ostringstream ostr;
180 for (
size_t ii = 0; ii < data_source->
size(); ++ii)
182 ostr <<
"Buffer " << ii <<
": " << std::endl;
190 ostr <<
" Fragment " << fragHdr->
fragment_id <<
": Sequence ID: " << fragHdr->sequence_id <<
", Type:" << fragHdr->type;
195 ostr <<
", Size: " << fragHdr->word_count <<
" words." << std::endl;
204 std::ostringstream ostr;
205 ostr << data_.toString() << std::endl;
207 ostr <<
"Data Buffer Fragment Counts: " << std::endl;
208 ostr << printBuffers_(&data_);
210 if (data_.GetKey() != broadcasts_.GetKey())
212 ostr <<
"Broadcast Buffer Fragment Counts: " << std::endl;
213 ostr << printBuffers_(&broadcasts_);
221 TLOG(TLVL_TRACE) <<
"ReleaseBuffer BEGIN";
224 if (current_data_source_ !=
nullptr)
226 current_data_source_->MarkBufferEmpty(current_read_buffer_);
229 catch (cet::exception
const& e)
231 TLOG(TLVL_WARNING) <<
"A cet::exception occured while trying to release the buffer: " << e;
235 TLOG(TLVL_ERROR) <<
"An unknown exception occured while trying to release the buffer";
237 current_read_buffer_ = -1;
238 current_header_ =
nullptr;
239 current_data_source_ =
nullptr;
240 TLOG(TLVL_TRACE) <<
"ReleaseBuffer END";
static constexpr type_t InvalidFragmentType
Copy InvalidFragmentType from RawFragmentHeader.
std::vector< Fragment > Fragments
A std::vector of Fragment objects.
void ReleaseBuffer()
Release the buffer currently being read to the Empty state.
void IncrementReadPos(int buffer, size_t read)
Increment the read position for a given buffer.
detail::RawFragmentHeader::type_t type_t
typedef for type_t from RawFragmentHeader
void ResetReadPos(int buffer)
Set the read position of the given buffer to the beginning of the buffer.
bool MoreDataInBuffer(int buffer)
Determine if more data is available to be read, based on the read position and data size...
static constexpr type_t InitFragmentType
Copy InitFragmentType from RawFragmentHeader.
The buffer is currently being read from.
The SharedMemoryManager creates a Shared Memory area which is divided into a number of fixed-size buf...
SharedMemoryEventReceiver(uint32_t shm_key, uint32_t broadcast_shm_key)
Connect to a Shared Memory segment using the given parameters.
size_t size() const
Get the number of buffers in the shared memory segment.
detail::RawFragmentHeader::RawDataType RawDataType
The RawDataType (currently a 64-bit integer) is the basic unit of data representation within artdaq ...
std::set< Fragment::type_t > GetFragmentTypes(bool &err)
Get a set of Fragment Types present in the event.
void * GetReadPos(int buffer)
Get a pointer to the current read position of the buffer.
bool ReadyForRead(bool broadcast=false, size_t timeout_us=1000000)
Determine whether an event is available for reading.
std::string toString()
Write out information about the Shared Memory to a string.
detail::RawEventHeader * ReadHeader(bool &err)
Get the Event header.
uint64_t gettimeofday_us()
Get the current time of day in microseconds (from gettimeofday system call)
std::unique_ptr< Fragments > GetFragmentsByType(bool &err, Fragment::type_t type)
Get a pointer to the Fragments of a given type in the event.