2 #include "artdaq-core/Core/SharedMemoryEventReceiver.hh"
4 #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_ && current_header_)
27 TLOG(TLVL_TRACE) <<
"ReadyForRead Returning true because already reading buffer" ;
33 uint64_t time_diff = 0;
35 while (first || time_diff < timeout_us)
37 if (broadcasts_.ReadyForRead())
39 buf = broadcasts_.GetBufferForReading();
40 current_data_source_ = &broadcasts_;
42 else if (!broadcast && data_.ReadyForRead())
44 buf = data_.GetBufferForReading();
45 current_data_source_ = &data_;
47 if (buf != -1 && current_data_source_)
49 current_read_buffer_ = buf;
50 current_data_source_->ResetReadPos(buf);
52 TLOG(TLVL_TRACE) <<
"ReadyForRead Found buffer, returning true. event hdr sequence_id=" << current_header_->
sequence_id;
55 if (current_data_source_ == &broadcasts_)
58 auto types = GetFragmentTypes(err);
72 current_data_source_ =
nullptr;
76 usleep(time_diff < 10000 ? time_diff : 10000 );
78 TLOG(TLVL_TRACE) <<
"ReadyForRead returning false" ;
84 TLOG(TLVL_TRACE) <<
"ReadHeader BEGIN" ;
85 if (current_read_buffer_ != -1 && current_data_source_)
90 TLOG(TLVL_WARNING) <<
"Buffer was in incorrect state, resetting" ;
91 current_data_source_ =
nullptr;
92 current_read_buffer_ = -1;
93 current_header_ =
nullptr;
97 TLOG(TLVL_TRACE) <<
"Already have buffer, returning stored header" ;
98 return current_header_;
103 if (current_read_buffer_ == -1 || !current_header_ || !current_data_source_)
throw cet::exception(
"AccessViolation") <<
"Cannot call GetFragmentTypes when not currently reading a buffer! Call ReadHeader() first!";
106 if (err)
return std::set<Fragment::type_t>();
108 current_data_source_->ResetReadPos(current_read_buffer_);
110 auto output = std::set<Fragment::type_t>();
112 while (current_data_source_->MoreDataInBuffer(current_read_buffer_))
115 if (err)
return std::set<Fragment::type_t>();
117 output.insert(fragHdr->type);
118 current_data_source_->IncrementReadPos(current_read_buffer_, fragHdr->word_count *
sizeof(
RawDataType));
127 if (!current_data_source_ || !current_header_ || current_read_buffer_ == -1)
throw cet::exception(
"AccessViolation") <<
"Cannot call GetFragmentsByType when not currently reading a buffer! Call ReadHeader() first!";
129 if (err)
return nullptr;
131 current_data_source_->ResetReadPos(current_read_buffer_);
134 std::unique_ptr<Fragments> output(
new Fragments());
136 while (current_data_source_->MoreDataInBuffer(current_read_buffer_))
139 if (err)
return nullptr;
144 current_data_source_->Read(current_read_buffer_, output->back().headerAddress(), fragHdr->word_count *
sizeof(
RawDataType));
145 output->back().autoResize();
149 current_data_source_->IncrementReadPos(current_read_buffer_, fragHdr->word_count *
sizeof(
RawDataType));
156 std::string artdaq::SharedMemoryEventReceiver::printBuffers_(
SharedMemoryManager* data_source)
158 std::ostringstream ostr;
159 for (
size_t ii = 0; ii < data_source->
size(); ++ii)
161 ostr <<
"Buffer " << ii <<
": " << std::endl;
169 ostr <<
" Fragment " << fragHdr->
fragment_id <<
": Sequence ID: " << fragHdr->sequence_id <<
", Type:" << fragHdr->type;
170 if (fragHdr->MakeVerboseSystemTypeMap().count(fragHdr->type))
172 ostr <<
" (" << fragHdr->MakeVerboseSystemTypeMap()[fragHdr->type] <<
")";
174 ostr <<
", Size: " << fragHdr->word_count <<
" words." << std::endl;
184 std::ostringstream ostr;
185 ostr << data_.toString() << std::endl;
187 ostr <<
"Data Buffer Fragment Counts: " << std::endl;
188 ostr << printBuffers_(&data_);
190 if (data_.GetKey() != broadcasts_.GetKey())
192 ostr <<
"Broadcast Buffer Fragment Counts: " << std::endl;
193 ostr << printBuffers_(&broadcasts_);
201 TLOG(TLVL_TRACE) <<
"ReleaseBuffer BEGIN" ;
204 if(current_data_source_) current_data_source_->MarkBufferEmpty(current_read_buffer_);
206 catch (cet::exception
const& e)
208 TLOG(TLVL_WARNING) <<
"A cet::exception occured while trying to release the buffer: " << e ;
212 TLOG(TLVL_ERROR) <<
"An unknown exception occured while trying to release the buffer" ;
214 current_read_buffer_ = -1;
215 current_header_ =
nullptr;
216 current_data_source_ =
nullptr;
217 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.