|
artdaq_core
v3_08_01
|
The SharedMemoryManager creates a Shared Memory area which is divided into a number of fixed-size buffers. It provides for multiple readers and multiple writers through a dual semaphore system. More...
#include <artdaq-core/Core/SharedMemoryManager.hh>
Public Types | |
| enum | BufferSemaphoreFlags { BufferSemaphoreFlags::Empty, BufferSemaphoreFlags::Writing, BufferSemaphoreFlags::Full, BufferSemaphoreFlags::Reading } |
| The BufferSemaphoreFlags enumeration represents the different possible "states" of a given shared memory buffer. More... | |
Public Member Functions | |
| 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. More... | |
| virtual | ~SharedMemoryManager () noexcept |
| SharedMemoryManager Destructor. | |
| bool | Attach (size_t timeout_usec=0) |
| Reconnect to the shared memory segment. | |
| int | GetBufferForReading () |
| Finds a buffer that is ready to be read, and reserves it for the calling manager. More... | |
| int | GetBufferForWriting (bool overwrite) |
| Finds a buffer that is ready to be written to, and reserves it for the calling manager. More... | |
| bool | ReadyForRead () |
| Whether any buffer is ready for read. More... | |
| virtual bool | ReadyForWrite (bool overwrite) |
| Whether any buffer is available for write. More... | |
| size_t | ReadReadyCount () |
| Count the number of buffers that are ready for reading. More... | |
| size_t | WriteReadyCount (bool overwrite) |
| Count the number of buffers that are ready for writing. More... | |
| std::deque< int > | GetBuffersOwnedByManager (bool locked=true) |
| Get the list of all buffers currently owned by this manager instance. More... | |
| size_t | BufferDataSize (int buffer) |
| Get the current size of the buffer's data. More... | |
| size_t | BufferSize () |
| Get the size of of a single buffer. More... | |
| void | ResetReadPos (int buffer) |
| Set the read position of the given buffer to the beginning of the buffer. More... | |
| void | ResetWritePos (int buffer) |
| Set the write position of the given buffer to the beginning of the buffer. More... | |
| void | IncrementReadPos (int buffer, size_t read) |
| Increment the read position for a given buffer. More... | |
| bool | IncrementWritePos (int buffer, size_t written) |
| Increment the write position for a given buffer. More... | |
| bool | MoreDataInBuffer (int buffer) |
| Determine if more data is available to be read, based on the read position and data size. More... | |
| bool | CheckBuffer (int buffer, BufferSemaphoreFlags flags) |
| Check both semaphore conditions (Mode flag and manager ID) for a given buffer. More... | |
| void | MarkBufferFull (int buffer, int destination=-1) |
| Release a buffer from a writer, marking it Full and ready for a reader. More... | |
| void | MarkBufferEmpty (int buffer, bool force=false, bool detachOnException=true) |
| Release a buffer from a reader, marking it Empty and ready to accept more data. More... | |
| bool | ResetBuffer (int buffer) |
| Resets the buffer from Reading to Full. This operation will only have an effect if performed by the owning manager or if the buffer has timed out. More... | |
| void | GetNewId () |
| Assign a new ID to the current SharedMemoryManager, if one has not yet been assigned. | |
| uint16_t | GetAttachedCount () const |
| Get the number of attached SharedMemoryManagers. More... | |
| void | ResetAttachedCount () const |
| Reset the attached manager count to 0. | |
| int | GetMyId () const |
| Get the ID number of the current SharedMemoryManager. More... | |
| int | GetRank () const |
| Get the rank of the owner of the Shared Memory (artdaq assigns rank to each artdaq process for data flow) More... | |
| void | SetRank (int rank) |
| Set the rank stored in the Shared Memory, if the current instance is the owner of the shared memory. More... | |
| bool | IsValid () const |
| Is the shared memory pointer valid? More... | |
| bool | IsEndOfData () const |
| Determine whether the Shared Memory is marked for destruction (End of Data) | |
| size_t | size () const |
| Get the number of buffers in the shared memory segment. More... | |
| size_t | Write (int buffer, void *data, size_t size) |
| Write size bytes of data from the given pointer to a buffer. More... | |
| bool | Read (int buffer, void *data, size_t size) |
| Read size bytes of data from buffer into the given pointer. More... | |
| virtual std::string | toString () |
| Write information about the SharedMemory to a string. More... | |
| uint32_t | GetKey () const |
| Get the key of the shared memory attached to this SharedMemoryManager. More... | |
| void * | GetReadPos (int buffer) |
| Get a pointer to the current read position of the buffer. More... | |
| void * | GetWritePos (int buffer) |
| Get a pointer to the current write position of the buffer. More... | |
| void * | GetBufferStart (int buffer) |
| Get a pointer to the start position of the buffer. More... | |
| void | Detach (bool throwException=false, const std::string &category="", const std::string &message="", bool force=false) |
| Detach from the Shared Memory segment, optionally throwing a cet::exception with the specified properties. More... | |
| uint64_t | GetBufferTimeout () const |
| Gets the configured timeout for buffers to be declared "stale". More... | |
| size_t | GetBufferCount () const |
| Gets the number of buffers which have been processed through the Shared Memory. More... | |
| size_t | GetLastSeenBufferID () const |
| Gets the highest buffer number either written or read by this SharedMemoryManager. More... | |
| size_t | GetLowestSeqIDRead () const |
| Gets the lowest sequence ID that has been read by any reader, as reported by the readers. | |
| void | SetMinWriteSize (size_t size) |
| Sets the threshold after which a buffer should be considered "non-empty" (in case of default headers) More... | |
| std::vector< std::pair< int, BufferSemaphoreFlags > > | GetBufferReport () |
| Get a report on the status of each buffer. More... | |
| void | TouchBuffer (int buffer) |
| Touch the given buffer (update its last_touch_time) | |
Static Public Member Functions | |
| static std::string | FlagToString (BufferSemaphoreFlags flag) |
| Convert a BufferSemaphoreFlags variable to its string represenatation. More... | |
The SharedMemoryManager creates a Shared Memory area which is divided into a number of fixed-size buffers. It provides for multiple readers and multiple writers through a dual semaphore system.
Definition at line 17 of file SharedMemoryManager.hh.
The BufferSemaphoreFlags enumeration represents the different possible "states" of a given shared memory buffer.
Definition at line 23 of file SharedMemoryManager.hh.
| artdaq::SharedMemoryManager::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.
| shm_key | The key to use when attaching/creating the shared memory segment |
| buffer_count | The number of buffers in the shared memory |
| buffer_size | The size of each buffer |
| buffer_timeout_us | The maximum amount of time a buffer can be left untouched by its owner (if 0, buffers do not expire) before being returned to its previous state. |
| destructive_read_mode | Whether a read operation empties the buffer (default: true, false for broadcast mode) |
Definition at line 60 of file SharedMemoryManager.cc.
| size_t artdaq::SharedMemoryManager::BufferDataSize | ( | int | buffer | ) |
Get the current size of the buffer's data.
| buffer | Buffer ID of buffer |
Definition at line 731 of file SharedMemoryManager.cc.
|
inline |
Get the size of of a single buffer.
Definition at line 130 of file SharedMemoryManager.hh.
| bool artdaq::SharedMemoryManager::CheckBuffer | ( | int | buffer, |
| BufferSemaphoreFlags | flags | ||
| ) |
Check both semaphore conditions (Mode flag and manager ID) for a given buffer.
| buffer | Buffer ID of buffer |
| flags | Expected Mode flag |
Definition at line 893 of file SharedMemoryManager.cc.
| void artdaq::SharedMemoryManager::Detach | ( | bool | throwException = false, |
| const std::string & | category = "", |
||
| const std::string & | message = "", |
||
| bool | force = false |
||
| ) |
Detach from the Shared Memory segment, optionally throwing a cet::exception with the specified properties.
| throwException | Whether to throw an exception after detaching |
| category | Category for the cet::exception |
| message | Message for the cet::exception |
| force | Whether to mark shared memory for destruction even if not owner (i.e. from signal handler) |
Definition at line 1282 of file SharedMemoryManager.cc.
|
inlinestatic |
Convert a BufferSemaphoreFlags variable to its string represenatation.
| flag | BufferSemaphoreFlags variable to convert |
Definition at line 36 of file SharedMemoryManager.hh.
| uint16_t artdaq::SharedMemoryManager::GetAttachedCount | ( | ) | const |
Get the number of attached SharedMemoryManagers.
Definition at line 1078 of file SharedMemoryManager.cc.
|
inline |
Gets the number of buffers which have been processed through the Shared Memory.
Definition at line 326 of file SharedMemoryManager.hh.
| int artdaq::SharedMemoryManager::GetBufferForReading | ( | ) |
Finds a buffer that is ready to be read, and reserves it for the calling manager.
Definition at line 277 of file SharedMemoryManager.cc.
| int artdaq::SharedMemoryManager::GetBufferForWriting | ( | bool | overwrite | ) |
Finds a buffer that is ready to be written to, and reserves it for the calling manager.
| overwrite | Whether to consider buffers that are in the Full and Reading state as ready for write (non-reliable mode) |
Definition at line 384 of file SharedMemoryManager.cc.
| std::vector< std::pair< int, artdaq::SharedMemoryManager::BufferSemaphoreFlags > > artdaq::SharedMemoryManager::GetBufferReport | ( | ) |
Get a report on the status of each buffer.
Definition at line 1227 of file SharedMemoryManager.cc.
| std::deque< int > artdaq::SharedMemoryManager::GetBuffersOwnedByManager | ( | bool | locked = true | ) |
Get the list of all buffers currently owned by this manager instance.
| locked | Default = true, Whether to lock search_mutex_ before checking buffer ownership (skipped in Detach) |
Definition at line 683 of file SharedMemoryManager.cc.
| void * artdaq::SharedMemoryManager::GetBufferStart | ( | int | buffer | ) |
Get a pointer to the start position of the buffer.
| buffer | Buffer ID of buffer |
Definition at line 1222 of file SharedMemoryManager.cc.
|
inline |
Gets the configured timeout for buffers to be declared "stale".
Definition at line 320 of file SharedMemoryManager.hh.
|
inline |
Get the key of the shared memory attached to this SharedMemoryManager.
Definition at line 284 of file SharedMemoryManager.hh.
|
inline |
Gets the highest buffer number either written or read by this SharedMemoryManager.
Definition at line 332 of file SharedMemoryManager.hh.
|
inline |
Get the ID number of the current SharedMemoryManager.
Definition at line 222 of file SharedMemoryManager.hh.
|
inline |
Get the rank of the owner of the Shared Memory (artdaq assigns rank to each artdaq process for data flow)
Definition at line 228 of file SharedMemoryManager.hh.
| void * artdaq::SharedMemoryManager::GetReadPos | ( | int | buffer | ) |
Get a pointer to the current read position of the buffer.
| buffer | Buffer ID of buffer |
Definition at line 1203 of file SharedMemoryManager.cc.
| void * artdaq::SharedMemoryManager::GetWritePos | ( | int | buffer | ) |
Get a pointer to the current write position of the buffer.
| buffer | Buffer ID of buffer |
Definition at line 1212 of file SharedMemoryManager.cc.
| void artdaq::SharedMemoryManager::IncrementReadPos | ( | int | buffer, |
| size_t | read | ||
| ) |
Increment the read position for a given buffer.
| buffer | Buffer ID of buffer |
| read | Number of bytes by which to increment read position |
Definition at line 807 of file SharedMemoryManager.cc.
| bool artdaq::SharedMemoryManager::IncrementWritePos | ( | int | buffer, |
| size_t | written | ||
| ) |
Increment the write position for a given buffer.
| buffer | Buffer ID of buffer |
| written | Number of bytes by which to increment write position |
Definition at line 835 of file SharedMemoryManager.cc.
|
inline |
Is the shared memory pointer valid?
Definition at line 243 of file SharedMemoryManager.hh.
| void artdaq::SharedMemoryManager::MarkBufferEmpty | ( | int | buffer, |
| bool | force = false, |
||
| bool | detachOnException = true |
||
| ) |
Release a buffer from a reader, marking it Empty and ready to accept more data.
| buffer | Buffer ID of buffer |
| force | Force buffer to empty state (only if manager_id_ == 0) |
| detachOnException | Whether to throw exceptions when buffers are not in the expected state (default true) |
Definition at line 936 of file SharedMemoryManager.cc.
| void artdaq::SharedMemoryManager::MarkBufferFull | ( | int | buffer, |
| int | destination = -1 |
||
| ) |
Release a buffer from a writer, marking it Full and ready for a reader.
| buffer | Buffer ID of buffer |
| destination | If desired, a destination manager ID may be specified for a buffer |
Definition at line 907 of file SharedMemoryManager.cc.
| bool artdaq::SharedMemoryManager::MoreDataInBuffer | ( | int | buffer | ) |
Determine if more data is available to be read, based on the read position and data size.
| buffer | Buffer ID of buffer |
Definition at line 871 of file SharedMemoryManager.cc.
| bool artdaq::SharedMemoryManager::Read | ( | int | buffer, |
| void * | data, | ||
| size_t | size | ||
| ) |
Read size bytes of data from buffer into the given pointer.
| buffer | Buffer ID of buffer |
| data | Destination pointer for read |
| size | Size of read, in bytes |
Definition at line 1132 of file SharedMemoryManager.cc.
| size_t artdaq::SharedMemoryManager::ReadReadyCount | ( | ) |
Count the number of buffers that are ready for reading.
Definition at line 532 of file SharedMemoryManager.cc.
| bool artdaq::SharedMemoryManager::ReadyForRead | ( | ) |
Whether any buffer is ready for read.
Definition at line 604 of file SharedMemoryManager.cc.
|
virtual |
Whether any buffer is available for write.
| overwrite | Whether to allow overwriting full buffers |
Reimplemented in artdaq::SharedMemoryFragmentManager.
Definition at line 647 of file SharedMemoryManager.cc.
| bool artdaq::SharedMemoryManager::ResetBuffer | ( | int | buffer | ) |
Resets the buffer from Reading to Full. This operation will only have an effect if performed by the owning manager or if the buffer has timed out.
| buffer | Buffer ID of buffer |
Definition at line 976 of file SharedMemoryManager.cc.
| void artdaq::SharedMemoryManager::ResetReadPos | ( | int | buffer | ) |
Set the read position of the given buffer to the beginning of the buffer.
| buffer | Buffer ID of buffer |
Definition at line 756 of file SharedMemoryManager.cc.
| void artdaq::SharedMemoryManager::ResetWritePos | ( | int | buffer | ) |
Set the write position of the given buffer to the beginning of the buffer.
| buffer | Buffer ID of buffer |
Definition at line 781 of file SharedMemoryManager.cc.
|
inline |
Sets the threshold after which a buffer should be considered "non-empty" (in case of default headers)
| size | Size (in bytes) after which a buffer will be considered non-empty |
Definition at line 343 of file SharedMemoryManager.hh.
|
inline |
Set the rank stored in the Shared Memory, if the current instance is the owner of the shared memory.
| rank | Rank to set |
Definition at line 234 of file SharedMemoryManager.hh.
|
inline |
Get the number of buffers in the shared memory segment.
Definition at line 254 of file SharedMemoryManager.hh.
|
virtual |
Write information about the SharedMemory to a string.
Definition at line 1168 of file SharedMemoryManager.cc.
| size_t artdaq::SharedMemoryManager::Write | ( | int | buffer, |
| void * | data, | ||
| size_t | size | ||
| ) |
Write size bytes of data from the given pointer to a buffer.
| buffer | Buffer ID of buffer |
| data | Source pointer for write |
| size | Size of write, in bytes |
Definition at line 1096 of file SharedMemoryManager.cc.
| size_t artdaq::SharedMemoryManager::WriteReadyCount | ( | bool | overwrite | ) |
Count the number of buffers that are ready for writing.
| overwrite | Whether to consider buffers that are in the Full and Reading state as ready for write (non-reliable mode) |
Definition at line 570 of file SharedMemoryManager.cc.