1 #ifndef artdaq_core_Core_SharedMemoryManager_hh
2 #define artdaq_core_Core_SharedMemoryManager_hh 1
10 #include "artdaq-core/Utilities/TimeUtils.hh"
61 SharedMemoryManager(uint32_t shm_key,
size_t buffer_count = 0,
size_t buffer_size = 0, uint64_t buffer_timeout_us = 100 * 1000000,
bool destructive_read_mode =
true);
71 bool Attach(
size_t timeout_usec = 0);
129 size_t BufferSize() {
return (shm_ptr_ !=
nullptr ? shm_ptr_->buffer_size : 0); }
199 if (manager_id_ < 0 &&
IsValid()) manager_id_ = shm_ptr_->next_id.fetch_add(1);
213 if (manager_id_ == 0 &&
IsValid()) shm_ptr_->next_id = 1;
234 if (manager_id_ == 0 &&
IsValid()) shm_ptr_->rank = rank;
241 bool IsValid()
const {
return shm_ptr_ ?
true :
false; }
252 size_t size()
const {
return IsValid() ? shm_ptr_->buffer_count : 0; }
261 size_t Write(
int buffer,
void* data,
size_t size);
270 bool Read(
int buffer,
void* data,
size_t size);
282 uint32_t
GetKey()
const {
return shm_key_; }
312 void Detach(
bool throwException =
false, std::string category =
"", std::string message =
"",
bool force =
false);
352 void TouchBuffer(
int buffer) {
return touchBuffer_(getBufferInfo_(buffer)); }
359 std::atomic<BufferSemaphoreFlags> sem;
360 std::atomic<int16_t> sem_id;
361 std::atomic<size_t> sequence_id;
362 std::atomic<uint64_t> last_touch_time;
367 std::atomic<unsigned int> reader_pos;
368 std::atomic<unsigned int> writer_pos;
371 size_t buffer_timeout_us;
372 size_t next_sequence_id;
373 size_t lowest_seq_id_read;
374 bool destructive_read_mode;
376 std::atomic<int> next_id;
378 unsigned ready_magic;
381 uint8_t* dataStart_()
const;
382 uint8_t* bufferStart_(
int buffer);
383 ShmBuffer* getBufferInfo_(
int buffer);
385 void touchBuffer_(ShmBuffer* buffer);
387 ShmStruct requested_shm_parameters_;
393 mutable std::vector<std::mutex> buffer_mutexes_;
394 mutable std::mutex search_mutex_;
396 std::atomic<size_t> last_seen_id_;
397 size_t min_write_size_;
402 #endif // artdaq_core_Core_SharedMemoryManager_hh
void TouchBuffer(int buffer)
Touch the given buffer (update its last_touch_time)
bool Read(int buffer, void *data, size_t size)
Read size bytes of data from buffer into the given pointer.
void IncrementReadPos(int buffer, size_t read)
Increment the read position for a given buffer.
void ResetAttachedCount() const
Reset the attached manager count to 0.
bool IsValid() const
Is the shared memory pointer valid?
bool Attach(size_t timeout_usec=0)
Reconnect to the shared memory segment.
std::deque< int > GetBuffersOwnedByManager(bool locked=true)
Get the list of all buffers currently owned by this manager instance.
void ResetReadPos(int buffer)
Set the read position of the given buffer to the beginning of the buffer.
static std::string FlagToString(BufferSemaphoreFlags flag)
Convert a BufferSemaphoreFlags variable to its string represenatation.
bool MoreDataInBuffer(int buffer)
Determine if more data is available to be read, based on the read position and data size...
virtual std::string toString()
Write information about the SharedMemory to a string.
void GetNewId()
Assign a new ID to the current SharedMemoryManager, if one has not yet been assigned.
std::list< std::pair< int, BufferSemaphoreFlags > > GetBufferReport()
Get a report on the status of each buffer.
The buffer is full, and waiting for a reader.
BufferSemaphoreFlags
The BufferSemaphoreFlags enumeration represents the different possible "states" of a given shared mem...
size_t BufferDataSize(int buffer)
Get the current size of the buffer's data.
size_t BufferSize()
Get the size of of a single buffer.
void Detach(bool throwException=false, std::string category="", std::string message="", bool force=false)
Detach from the Shared Memory segment, optionally throwing a cet::exception with the specified proper...
virtual ~SharedMemoryManager() noexcept
SharedMemoryManager Destructor.
The buffer is currently being read from.
The buffer is currently being written to.
The SharedMemoryManager creates a Shared Memory area which is divided into a number of fixed-size buf...
The buffer is empty, and waiting for a writer.
size_t ReadReadyCount()
Count the number of buffers that are ready for reading.
bool ResetBuffer(int buffer)
Resets the buffer from Reading to Full. This operation will only have an effect if performed by the o...
int GetRank() const
Get the rank of the owner of the Shared Memory (artdaq assigns rank to each artdaq process for data f...
size_t GetLowestSeqIDRead() const
Gets the lowest sequence ID that has been read by any reader, as reported by the readers.
size_t size() const
Get the number of buffers in the shared memory segment.
bool IncrementWritePos(int buffer, size_t written)
Increment the write position for a given buffer.
bool IsEndOfData() const
Determine whether the Shared Memory is marked for destruction (End of Data)
bool CheckBuffer(int buffer, BufferSemaphoreFlags flags)
Check both semaphore conditions (Mode flag and manager ID) for a given buffer.
size_t GetLastSeenBufferID() const
Gets the highest buffer number either written or read by this SharedMemoryManager.
uint32_t GetKey() const
Get the key of the shared memory attached to this SharedMemoryManager.
void MarkBufferFull(int buffer, int destination=-1)
Release a buffer from a writer, marking it Full and ready for a reader.
void MarkBufferEmpty(int buffer, bool force=false)
Release a buffer from a reader, marking it Empty and ready to accept more data.
uint64_t GetBufferTimeout() const
Gets the configured timeout for buffers to be declared "stale".
void ResetWritePos(int buffer)
Set the write position of the given buffer to the beginning of the buffer.
void SetMinWriteSize(size_t size)
Sets the threshold after which a buffer should be considered "non-empty" (in case of default headers)...
size_t GetBufferCount() const
Gets the number of buffers which have been processed through the Shared Memory.
void * GetWritePos(int buffer)
Get a pointer to the current write position of the buffer.
size_t Write(int buffer, void *data, size_t size)
Write size bytes of data from the given pointer to a buffer.
void * GetReadPos(int buffer)
Get a pointer to the current read position of the buffer.
void * GetBufferStart(int buffer)
Get a pointer to the start position of the buffer.
SharedMemoryManager(uint32_t shm_key, size_t buffer_count=0, size_t buffer_size=0, uint64_t buffer_timeout_us=100 *1000000, bool destructive_read_mode=true)
SharedMemoryManager Constructor.
bool ReadyForRead()
Whether any buffer is ready for read.
void SetRank(int rank)
Set the rank stored in the Shared Memory, if the current instance is the owner of the shared memory...
int GetBufferForWriting(bool overwrite)
Finds a buffer that is ready to be written to, and reserves it for the calling manager.
uint16_t GetAttachedCount() const
Get the number of attached SharedMemoryManagers.
size_t WriteReadyCount(bool overwrite)
Count the number of buffers that are ready for writing.
int GetMyId() const
Get the ID number of the current SharedMemoryManager.
int GetBufferForReading()
Finds a buffer that is ready to be read, and reserves it for the calling manager. ...
virtual bool ReadyForWrite(bool overwrite)
Whether any buffer is available for write.