artdaq::SharedMemoryManager Class Reference

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>

Inheritance diagram for artdaq::SharedMemoryManager:
artdaq::SharedMemoryFragmentManager

List of all members.

Classes

struct  ShmBuffer
struct  ShmStruct

Public Types

enum  BufferSemaphoreFlags { Empty, Writing, Full, 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.
virtual ~SharedMemoryManager () noexcept
 SharedMemoryManager Destructor.
void Attach ()
 Reconnect to the shared memory segment.
int GetBufferForReading ()
 Finds a buffer that is ready to be read, and reserves it for the calling manager.
int GetBufferForWriting (bool overwrite)
 Finds a buffer that is ready to be written to, and reserves it for the calling manager.
bool ReadyForRead ()
 Whether any buffer is ready for read.
bool ReadyForWrite (bool overwrite)
 Whether any buffer is available for write.
size_t ReadReadyCount ()
 Count the number of buffers that are ready for reading.
size_t WriteReadyCount (bool overwrite)
 Count the number of buffers that are ready for writing.
std::deque< int > GetBuffersOwnedByManager ()
 Get the list of all buffers currently owned by this manager instance.
size_t BufferDataSize (int buffer)
 Get the current size of the buffer's data.
void ResetReadPos (int buffer)
 Set the read position of the given buffer to the beginning of the buffer.
void ResetWritePos (int buffer)
 Set the write position of the given buffer to the beginning of the buffer.
void IncrementReadPos (int buffer, size_t read)
 Increment the read position for a given buffer.
void IncrementWritePos (int buffer, size_t written)
 Increment the write position for a given buffer.
bool MoreDataInBuffer (int buffer)
 Determine if more data is available to be read, based on the read position and data size.
bool CheckBuffer (int buffer, BufferSemaphoreFlags flags)
 Check both semaphore conditions (Mode flag and manager ID) for a given buffer.
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.
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.
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.
void ResetAttachedCount () const
 Reset the attached manager count to 0.
int GetMyId () const
 Get the ID number of the current SharedMemoryManager.
int GetRank () const
 Get the rank of the owner of the Shared Memory (artdaq assigns rank to each artdaq process for data flow).
void SetRank (int rank)
 Set the rank stored in the Shared Memory, if the current instance is the owner of the shared memory.
bool IsValid () const
 Is the shared memory pointer valid?
size_t size () const
 Get the number of buffers in the shared memory segment.
size_t Write (int buffer, void *data, size_t size)
 Write size bytes of data from the given pointer to a buffer.
bool Read (int buffer, void *data, size_t size)
 Read size bytes of data from buffer into the given pointer.
virtual std::string toString ()
 Write information about the SharedMemory to a string.
uint32_t GetKey () const
 Get the key of the shared memory attached to this SharedMemoryManager.
void * GetReadPos (int buffer)
 Get a pointer to the current read position of the buffer.
void * GetWritePos (int buffer)
 Get a pointer to the current write position of the buffer.
void * GetBufferStart (int buffer)
 Get a pointer to the start position of the buffer.
void Detach (bool throwException=false, std::string category="", std::string message="")
 Detach from the Shared Memory segment, optionally throwing a cet::exception with the specified properties.
uint64_t GetBufferTimeout () const
 Gets the configured timeout for buffers to be declared "stale".
size_t GetBufferCount () const
 Gets the number of buffers which have been processed through the Shared Memory.
size_t GetLastSeenBufferID () const
 Gets the highest buffer number either written or read by this SharedMemoryManager.
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).

Static Public Member Functions

static std::string FlagToString (BufferSemaphoreFlags flag)
 Convert a BufferSemaphoreFlags variable to its string represenatation.

Detailed Description

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 18 of file SharedMemoryManager.hh.


Member Enumeration Documentation

The BufferSemaphoreFlags enumeration represents the different possible "states" of a given shared memory buffer.

Enumerator:
Empty 

The buffer is empty, and waiting for a writer.

Writing 

The buffer is currently being written to.

Full 

The buffer is full, and waiting for a reader.

Reading 

The buffer is currently being read from.

Definition at line 25 of file SharedMemoryManager.hh.


Constructor & Destructor Documentation

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.

Parameters:
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 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 12 of file SharedMemoryManager.cc.


Member Function Documentation

size_t artdaq::SharedMemoryManager::BufferDataSize ( int  buffer  ) 

Get the current size of the buffer's data.

Parameters:
buffer Buffer ID of buffer
Returns:
Current size of data in the buffer

Definition at line 339 of file SharedMemoryManager.cc.

bool artdaq::SharedMemoryManager::CheckBuffer ( int  buffer,
BufferSemaphoreFlags  flags 
)

Check both semaphore conditions (Mode flag and manager ID) for a given buffer.

Parameters:
buffer Buffer ID of buffer
flags Expected Mode flag
Returns:
Whether the buffer passed the check

Definition at line 401 of file SharedMemoryManager.cc.

void artdaq::SharedMemoryManager::Detach ( bool  throwException = false,
std::string  category = "",
std::string  message = "" 
)

Detach from the Shared Memory segment, optionally throwing a cet::exception with the specified properties.

Parameters:
throwException Whether to throw an exception after detaching
category Category for the cet::exception
message Message for the cet::exception

Definition at line 627 of file SharedMemoryManager.cc.

static std::string artdaq::SharedMemoryManager::FlagToString ( BufferSemaphoreFlags  flag  )  [inline, static]

Convert a BufferSemaphoreFlags variable to its string represenatation.

Parameters:
flag BufferSemaphoreFlags variable to convert
Returns:
String representation of flag

Definition at line 38 of file SharedMemoryManager.hh.

uint16_t artdaq::SharedMemoryManager::GetAttachedCount (  )  const [inline]

Get the number of attached SharedMemoryManagers.

Returns:
The number of attached SharedMemoryManagers

Definition at line 199 of file SharedMemoryManager.hh.

size_t artdaq::SharedMemoryManager::GetBufferCount (  )  const [inline]

Gets the number of buffers which have been processed through the Shared Memory.

Returns:
The number of buffers processed by the Shared Memory

Definition at line 305 of file SharedMemoryManager.hh.

int artdaq::SharedMemoryManager::GetBufferForReading (  ) 

Finds a buffer that is ready to be read, and reserves it for the calling manager.

Returns:
The id number of the buffer. -1 indicates no buffers available for read.

Definition at line 134 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.

Parameters:
overwrite Whether to consider buffers that are in the Full and Reading state as ready for write (non-reliable mode)
Returns:
The id number of the buffer. -1 indicates no buffers available for write.

Definition at line 198 of file SharedMemoryManager.cc.

std::deque< int > artdaq::SharedMemoryManager::GetBuffersOwnedByManager (  ) 

Get the list of all buffers currently owned by this manager instance.

Returns:
A std::deque<int> of buffer IDs currently owned by this manager instance.

Definition at line 321 of file SharedMemoryManager.cc.

void * artdaq::SharedMemoryManager::GetBufferStart ( int  buffer  ) 

Get a pointer to the start position of the buffer.

Parameters:
buffer Buffer ID of buffer
Returns:
void* pointer to buffer start position

Definition at line 579 of file SharedMemoryManager.cc.

uint64_t artdaq::SharedMemoryManager::GetBufferTimeout (  )  const [inline]

Gets the configured timeout for buffers to be declared "stale".

Returns:
The buffer timeout, in microseconds

Definition at line 299 of file SharedMemoryManager.hh.

uint32_t artdaq::SharedMemoryManager::GetKey (  )  const [inline]

Get the key of the shared memory attached to this SharedMemoryManager.

Returns:
The shared memory key

Definition at line 264 of file SharedMemoryManager.hh.

size_t artdaq::SharedMemoryManager::GetLastSeenBufferID (  )  const [inline]

Gets the highest buffer number either written or read by this SharedMemoryManager.

Returns:
The highest buffer id written or read by this SharedMemoryManager

Definition at line 311 of file SharedMemoryManager.hh.

int artdaq::SharedMemoryManager::GetMyId (  )  const [inline]

Get the ID number of the current SharedMemoryManager.

Returns:
The ID number of the current SharedMemoryManager

Definition at line 210 of file SharedMemoryManager.hh.

int artdaq::SharedMemoryManager::GetRank (  )  const [inline]

Get the rank of the owner of the Shared Memory (artdaq assigns rank to each artdaq process for data flow).

Returns:
The rank of the owner of the Shared Memory

Definition at line 216 of file SharedMemoryManager.hh.

void * artdaq::SharedMemoryManager::GetReadPos ( int  buffer  ) 

Get a pointer to the current read position of the buffer.

Parameters:
buffer Buffer ID of buffer
Returns:
void* pointer to the buffer's current read position

Definition at line 568 of file SharedMemoryManager.cc.

void * artdaq::SharedMemoryManager::GetWritePos ( int  buffer  ) 

Get a pointer to the current write position of the buffer.

Parameters:
buffer Buffer ID of buffer
Returns:
void* pointer to buffer's current write position

Definition at line 573 of file SharedMemoryManager.cc.

void artdaq::SharedMemoryManager::IncrementReadPos ( int  buffer,
size_t  read 
)

Increment the read position for a given buffer.

Parameters:
buffer Buffer ID of buffer
read Number of bytes by which to increment read position

Definition at line 368 of file SharedMemoryManager.cc.

void artdaq::SharedMemoryManager::IncrementWritePos ( int  buffer,
size_t  written 
)

Increment the write position for a given buffer.

Parameters:
buffer Buffer ID of buffer
written Number of bytes by which to increment write position

Definition at line 380 of file SharedMemoryManager.cc.

bool artdaq::SharedMemoryManager::IsValid (  )  const [inline]

Is the shared memory pointer valid?

Returns:
Whether the shared memory pointer is valid

Definition at line 228 of file SharedMemoryManager.hh.

void artdaq::SharedMemoryManager::MarkBufferEmpty ( int  buffer,
bool  force = false 
)

Release a buffer from a reader, marking it Empty and ready to accept more data.

Parameters:
buffer Buffer ID of buffer
force Force buffer to empty state (only if manager_id_ == 0)

Definition at line 423 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.

Parameters:
buffer Buffer ID of buffer
destination If desired, a destination manager ID may be specified for a buffer

Definition at line 408 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.

Parameters:
buffer Buffer ID of buffer
Returns:
Whether more data is available in the given buffer.

Definition at line 392 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.

Parameters:
buffer Buffer ID of buffer
data Destination pointer for read
size Size of read, in bytes
Returns:
Whether the read was successful

Definition at line 524 of file SharedMemoryManager.cc.

size_t artdaq::SharedMemoryManager::ReadReadyCount (  ) 

Count the number of buffers that are ready for reading.

Returns:
The number of buffers ready for reading

Definition at line 278 of file SharedMemoryManager.cc.

bool artdaq::SharedMemoryManager::ReadyForRead (  )  [inline]

Whether any buffer is ready for read.

Returns:
True if there is a buffer available

Definition at line 93 of file SharedMemoryManager.hh.

bool artdaq::SharedMemoryManager::ReadyForWrite ( bool  overwrite  )  [inline]

Whether any buffer is available for write.

Parameters:
overwrite Whether to allow overwriting full buffers
Returns:
True if there is a buffer available

Definition at line 100 of file SharedMemoryManager.hh.

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.

Parameters:
buffer Buffer ID of buffer
Returns:
Whether the buffer has exceeded the maximum age

Definition at line 453 of file SharedMemoryManager.cc.

void artdaq::SharedMemoryManager::ResetReadPos ( int  buffer  ) 

Set the read position of the given buffer to the beginning of the buffer.

Parameters:
buffer Buffer ID of buffer

Definition at line 349 of file SharedMemoryManager.cc.

void artdaq::SharedMemoryManager::ResetWritePos ( int  buffer  ) 

Set the write position of the given buffer to the beginning of the buffer.

Parameters:
buffer Buffer ID of buffer

Definition at line 358 of file SharedMemoryManager.cc.

void artdaq::SharedMemoryManager::SetMinWriteSize ( size_t  size  )  [inline]

Sets the threshold after which a buffer should be considered "non-empty" (in case of default headers).

Parameters:
size Size (in bytes) after which a buffer will be considered non-empty

Definition at line 322 of file SharedMemoryManager.hh.

void artdaq::SharedMemoryManager::SetRank ( int  rank  )  [inline]

Set the rank stored in the Shared Memory, if the current instance is the owner of the shared memory.

Parameters:
rank Rank to set

Definition at line 222 of file SharedMemoryManager.hh.

size_t artdaq::SharedMemoryManager::size (  )  const [inline]

Get the number of buffers in the shared memory segment.

Returns:
The number of buffers in the shared memory segment

Definition at line 234 of file SharedMemoryManager.hh.

std::string artdaq::SharedMemoryManager::toString (  )  [virtual]

Write information about the SharedMemory to a string.

Returns:
String describing current state of SharedMemory and buffers

Definition at line 540 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.

Parameters:
buffer Buffer ID of buffer
data Source pointer for write
size Size of write, in bytes
Returns:
Amount of data written, in bytes

Definition at line 502 of file SharedMemoryManager.cc.

size_t artdaq::SharedMemoryManager::WriteReadyCount ( bool  overwrite  ) 

Count the number of buffers that are ready for writing.

Parameters:
overwrite Whether to consider buffers that are in the Full and Reading state as ready for write (non-reliable mode)
Returns:
The number of buffers ready for writing

Definition at line 302 of file SharedMemoryManager.cc.


The documentation for this class was generated from the following files:

Generated on 6 Feb 2018 for artdaq_core by  doxygen 1.6.1