CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
Loading...
Searching...
No Matches
cmlabs::MemoryManager Class Reference

Top-level facade of the shared-memory subsystem for one process. More...

#include <MemoryManager.h>

Collaboration diagram for cmlabs::MemoryManager:
[legend]

Public Member Functions

 MemoryManager ()
 ~MemoryManager ()
bool getMemoryUsage (uint64 &sysAlloc, uint64 &sysUsage, uint64 &dataAlloc, uint64 &dataUsage)
 Report allocation/usage figures for the system and data segments.
uint8 getNodeStatus (uint64 &lastseen, uint64 &createdTime)
 Get the node status from the master segment.
bool connect (uint16 sysID, bool isMaster)
 Attach this process to an existing node's shared segments.
bool create (uint16 sysID, uint32 slotCount=100000, uint16 binCount=2, uint32 minBlockSize=1024, uint32 maxBlockSize=64 *1024, uint64 initSize=50000000L, uint64 maxSize=1000000000L, bool force=false)
 Create all shared segments for a new node instance (master process only).
bool insertMessage (DataMessage *msg, uint64 &id)
 Publish a DataMessage into shared (temporal) memory.
DataMessagegetCopyOfMessage (uint64 id)
 Fetch a private copy of a previously published message.
std::string typeToText (PsyType type)
 Resolve a type id to its registered name.
std::string subTypeToText (uint16 subtype)
 Resolve a subtype id to its registered name.
std::string contextToText (PsyContext context)
 Resolve a context id to its registered name.
std::string subContextToText (uint16 subcontext)
 Resolve a subcontext id to its registered name.
std::string getComponentName (uint32 compID)
 Resolve a component id to its name.
uint16 getNodeID ()
bool setNodeID (uint16 id)
 Set this node's cluster-wide id.
DataMessageretrieveAllSystemIDs ()
 Enumerate all live Psyclone system ids/ports on this machine (via the PsycloneIndex).
bool getTimeSyncData (uint64 &tmc, int64 &adjust)
 Read the cluster time-sync values from the master segment.
bool setTimeSyncData (uint64 tmc, int64 adjust)
 Write the cluster time-sync values into the master segment.

Static Public Member Functions

static bool UnitTest ()
 Self-test of the shared-memory manager.
static bool ShmUnitTest ()
 Self-test of the raw shared-memory primitives.

Public Attributes

TemporalMemorytemporalMemory
 Accessor for time-limited message storage.
ProcessMemoryprocessMemory
 Accessor for the process table and per-process queues.
ComponentMemorycomponentMemory
 Accessor for the component registry.
DataMapsMemorydataMapsMemory
 Accessor for the type/context/tag/crank/request maps.

Static Public Attributes

static MemoryManagerSingleton = NULL
 Per-process singleton instance, set by the constructor.

Friends

THREAD_RET THREAD_FUNCTION_CALL MemoryManagement (THREAD_ARG arg)
 Background maintenance thread entry point for MemoryManager / MemoryManagerX.

Detailed Description

Top-level facade of the shared-memory subsystem for one process.

Every Psyclone process creates exactly one MemoryManager (exposed via ::Singleton). The node's master process calls create() to lay out all segments; every other space process calls connect() to attach to them. After that, the four public subsystem pointers (temporalMemory, processMemory, componentMemory, dataMapsMemory) give typed access to the shared state. A background management thread (MemoryManagement) runs in the master process to expire end-of-life data and perform maintenance.

Ownership: the MemoryManager owns the subsystem accessor objects (deleted in the destructor); the shared segments themselves outlive any single process and are owned by the OS. Thread-safety: all subsystem calls take their own cross-process mutexes; the facade itself is safe to use from multiple threads.

// Master process:
mm->create(port); // create all shared segments
// Any space process:
mm2->connect(port, false); // attach to existing segments
uint64 id;
mm2->insertMessage(msg, id); // publish a message into shared memory
DataMessage* copy = mm->getCopyOfMessage(id); // retrieve it from any process
The central Psyclone data container: a self-contained binary message with typed, named user entries.
DataMessage * getCopyOfMessage(uint64 id)
Fetch a private copy of a previously published message.
bool create(uint16 sysID, uint32 slotCount=100000, uint16 binCount=2, uint32 minBlockSize=1024, uint32 maxBlockSize=64 *1024, uint64 initSize=50000000L, uint64 maxSize=1000000000L, bool force=false)
Create all shared segments for a new node instance (master process only).
bool insertMessage(DataMessage *msg, uint64 &id)
Publish a DataMessage into shared (temporal) memory.
bool connect(uint16 sysID, bool isMaster)
Attach this process to an existing node's shared segments.

Definition at line 412 of file MemoryManager.h.

Constructor & Destructor Documentation

◆ MemoryManager()

cmlabs::MemoryManager::MemoryManager ( )

Definition at line 373 of file MemoryManager.cpp.

References componentMemory, dataMapsMemory, processMemory, and temporalMemory.

Referenced by MemoryManagement, and UnitTest().

◆ ~MemoryManager()

cmlabs::MemoryManager::~MemoryManager ( )

Member Function Documentation

◆ connect()

bool cmlabs::MemoryManager::connect ( uint16 sysID,
bool isMaster )

Attach this process to an existing node's shared segments.

Parameters
sysIDNode port/system id whose segments to open.
isMastertrue if this process should run the management thread.
Returns
true if all segments were found and mapped.

Definition at line 456 of file MemoryManager.cpp.

References componentMemory, cmlabs::ThreadManager::CreateThread(), dataMapsMemory, cmlabs::PsycloneIndex::GetStatus(), cmlabs::GetTimeAgeMS(), LOG_MEMORY, LogPrint, MemoryManagement, processMemory, PSYCLONE_STATUS_ENDED, PSYCLONE_STATUS_INIT, PSYCLONE_STATUS_NONE, and temporalMemory.

◆ contextToText()

std::string cmlabs::MemoryManager::contextToText ( PsyContext context)

Resolve a context id to its registered name.

Parameters
contextContext id.
Returns
Name or empty.

Definition at line 677 of file MemoryManager.cpp.

References subContextToText().

◆ create()

bool cmlabs::MemoryManager::create ( uint16 sysID,
uint32 slotCount = 100000,
uint16 binCount = 2,
uint32 minBlockSize = 1024,
uint32 maxBlockSize = 64*1024,
uint64 initSize = 50000000L,
uint64 maxSize = 1000000000L,
bool force = false )

Create all shared segments for a new node instance (master process only).

Parameters
sysIDNode port/system id used to name the segments.
slotCountNumber of message slots in temporal memory.
binCountNumber of block-size bins in temporal memory.
minBlockSizeSmallest bin block size in bytes.
maxBlockSizeLargest bin block size in bytes.
initSizeInitial temporal segment size in bytes.
maxSizeMaximum temporal segment size in bytes.
forceIf true, replace any stale existing segments.
Returns
true on success.
Warning
Only one process per node may call this; all others must use connect().

Definition at line 534 of file MemoryManager.cpp.

References componentMemory, cmlabs::ThreadManager::CreateThread(), dataMapsMemory, cmlabs::PsycloneIndex::GetStatus(), cmlabs::GetTimeAge(), cmlabs::GetTimeAgeMS(), cmlabs::GetTimeNow(), LOG_MEMORY, LogPrint, MemoryManagement, cmlabs::PrintTimeDifString(), processMemory, PSYCLONE_STATUS_INIT, PSYCLONE_STATUS_READY, and temporalMemory.

Referenced by cmlabs::ProcessMemory::PerfTest(), cmlabs::ComponentData::UnitTest(), cmlabs::ComponentMemory::UnitTest(), UnitTest(), and cmlabs::ProcessMemory::UnitTest().

◆ getComponentName()

std::string cmlabs::MemoryManager::getComponentName ( uint32 compID)

Resolve a component id to its name.

Parameters
compIDComponent id.
Returns
Name or empty.

Definition at line 700 of file MemoryManager.cpp.

References componentMemory, and MAXKEYNAMELEN.

◆ getCopyOfMessage()

DataMessage * cmlabs::MemoryManager::getCopyOfMessage ( uint64 id)

Fetch a private copy of a previously published message.

Parameters
idMessage id returned by insertMessage() (possibly from another process).
Returns
Newly allocated DataMessage owned by the caller (delete it), or NULL if the id is unknown or the message has expired (end-of-life reached).

Definition at line 646 of file MemoryManager.cpp.

References temporalMemory.

Referenced by UnitTest().

◆ getMemoryUsage()

bool cmlabs::MemoryManager::getMemoryUsage ( uint64 & sysAlloc,
uint64 & sysUsage,
uint64 & dataAlloc,
uint64 & dataUsage )

Report allocation/usage figures for the system and data segments.

Parameters
sysAllocReceives bytes allocated for system segments.
sysUsageReceives bytes in use in system segments.
dataAllocReceives bytes allocated for data (temporal) memory.
dataUsageReceives bytes in use in data memory.
Returns
true on success.

Definition at line 417 of file MemoryManager.cpp.

References componentMemory, dataMapsMemory, processMemory, and temporalMemory.

◆ getNodeID()

uint16 cmlabs::MemoryManager::getNodeID ( )
Returns
This node's cluster-wide id (from the master segment).

Definition at line 601 of file MemoryManager.cpp.

◆ getNodeStatus()

uint8 cmlabs::MemoryManager::getNodeStatus ( uint64 & lastseen,
uint64 & createdTime )

Get the node status from the master segment.

Parameters
lastseenReceives the last heartbeat timestamp (µs).
createdTimeReceives the segment creation time (µs).
Returns
Node status value (0-10, 10 = ready).

Definition at line 445 of file MemoryManager.cpp.

References cmlabs::PsycloneIndex::GetStatus(), and PSYCLONE_STATUS_NONE.

◆ getTimeSyncData()

bool cmlabs::MemoryManager::getTimeSyncData ( uint64 & tmc,
int64 & adjust )

Read the cluster time-sync values from the master segment.

Parameters
tmcReceives the time-master clock (µs).
adjustReceives the local offset.
Returns
true on success.

Definition at line 626 of file MemoryManager.cpp.

◆ insertMessage()

bool cmlabs::MemoryManager::insertMessage ( DataMessage * msg,
uint64 & id )

Publish a DataMessage into shared (temporal) memory.

Parameters
msgMessage to copy into shared memory; caller retains ownership of msg.
idReceives the cluster-unique message id (node|serial|slot encoded).
Returns
true on success. May block briefly on the temporal-memory mutex.

Definition at line 639 of file MemoryManager.cpp.

References temporalMemory.

Referenced by UnitTest().

◆ retrieveAllSystemIDs()

DataMessage * cmlabs::MemoryManager::retrieveAllSystemIDs ( )

Enumerate all live Psyclone system ids/ports on this machine (via the PsycloneIndex).

Returns
Newly allocated message listing the systems; caller owns and must delete it.

Definition at line 611 of file MemoryManager.cpp.

References componentMemory, and dataMapsMemory.

◆ setNodeID()

bool cmlabs::MemoryManager::setNodeID ( uint16 id)

Set this node's cluster-wide id.

Parameters
idNew id.
Returns
true on success.

Definition at line 605 of file MemoryManager.cpp.

References temporalMemory.

◆ setTimeSyncData()

bool cmlabs::MemoryManager::setTimeSyncData ( uint64 tmc,
int64 adjust )

Write the cluster time-sync values into the master segment.

Parameters
tmcTime-master clock (µs).
adjustLocal clock offset.
Returns
true on success.

Definition at line 632 of file MemoryManager.cpp.

◆ ShmUnitTest()

bool cmlabs::MemoryManager::ShmUnitTest ( )
static

Self-test of the raw shared-memory primitives.

Returns
true if all checks pass.

Definition at line 3051 of file MemoryManager.cpp.

References cmlabs::unittest::progress().

Referenced by cmlabs::Register_MemoryManager_Tests().

◆ subContextToText()

std::string cmlabs::MemoryManager::subContextToText ( uint16 subcontext)

Resolve a subcontext id to its registered name.

Parameters
subcontextSubcontext id.
Returns
Name or empty.

Definition at line 687 of file MemoryManager.cpp.

References dataMapsMemory, and MAXKEYNAMELEN.

Referenced by contextToText().

◆ subTypeToText()

std::string cmlabs::MemoryManager::subTypeToText ( uint16 subtype)

Resolve a subtype id to its registered name.

Parameters
subtypeSubtype id.
Returns
Name or empty.

Definition at line 664 of file MemoryManager.cpp.

References dataMapsMemory, and MAXKEYNAMELEN.

Referenced by typeToText().

◆ typeToText()

std::string cmlabs::MemoryManager::typeToText ( PsyType type)

Resolve a type id to its registered name.

Parameters
typeType id.
Returns
Name or empty.

Definition at line 652 of file MemoryManager.cpp.

References cmlabs::utils::StringFormat(), and subTypeToText().

◆ UnitTest()

◆ MemoryManagement

THREAD_RET THREAD_FUNCTION_CALL MemoryManagement ( THREAD_ARG arg)
friend

Background maintenance thread entry point for MemoryManager / MemoryManagerX.

Parameters
argThe manager instance (cast internally).
Returns
Thread exit code.

Definition at line 743 of file MemoryManager.cpp.

References MemoryManager(), THREAD_ARG, THREAD_FUNCTION_CALL, THREAD_RET, and thread_ret_val.

Referenced by connect(), and create().

Member Data Documentation

◆ componentMemory

ComponentMemory* cmlabs::MemoryManager::componentMemory

Accessor for the component registry.

Owned; backed by shared memory.

Definition at line 506 of file MemoryManager.h.

Referenced by connect(), create(), getComponentName(), getMemoryUsage(), MemoryManager(), retrieveAllSystemIDs(), cmlabs::ComponentMemory::UnitTest(), and ~MemoryManager().

◆ dataMapsMemory

DataMapsMemory* cmlabs::MemoryManager::dataMapsMemory

Accessor for the type/context/tag/crank/request maps.

Owned; backed by shared memory.

Definition at line 507 of file MemoryManager.h.

Referenced by connect(), create(), getMemoryUsage(), MemoryManager(), retrieveAllSystemIDs(), subContextToText(), subTypeToText(), cmlabs::ComponentMemory::UnitTest(), and ~MemoryManager().

◆ processMemory

ProcessMemory* cmlabs::MemoryManager::processMemory

Accessor for the process table and per-process queues.

Owned; backed by shared memory.

Definition at line 505 of file MemoryManager.h.

Referenced by connect(), create(), getMemoryUsage(), MemoryManager(), cmlabs::ProcessMemory::PerfTest(), cmlabs::ProcessMemoryPerfTest(), cmlabs::MessageQueue::QueueTest, cmlabs::ProcessMemory::UnitTest(), and ~MemoryManager().

◆ Singleton

MemoryManager * cmlabs::MemoryManager::Singleton = NULL
static

Per-process singleton instance, set by the constructor.

Definition at line 414 of file MemoryManager.h.

◆ temporalMemory

TemporalMemory* cmlabs::MemoryManager::temporalMemory

Accessor for time-limited message storage.

Owned by this object; backing store is shared memory.

Definition at line 504 of file MemoryManager.h.

Referenced by connect(), create(), getCopyOfMessage(), getMemoryUsage(), insertMessage(), MemoryManager(), setNodeID(), and ~MemoryManager().


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