29 if (e->
port == port) {
78 if (e->
port == port) {
82 else if (!firstEmpty && !e->
port)
89 firstEmpty = firstStale;
96 entry->instanceID = instID;
120 mutex->enter(5000, __FUNCTION__);
131 if (!mutex->enter(5000, __FUNCTION__)) {
158 if (!mutex->enter(5000, __FUNCTION__))
178 if (!mutex || !master || !mutex->enter(5000, __FUNCTION__))
180 uint16 res = master->nodeID;
186 if (!mutex || !master || !mutex->enter(5000, __FUNCTION__))
188 uint64 res = master->createdTime;
194 if (!mutex || !master || !mutex->enter(5000, __FUNCTION__))
202 if (!mutex || !master || !mutex->enter(5000, __FUNCTION__))
204 uint32 res = master->dynamicShmemSerial;
210 if (!mutex || !master || !mutex->enter(5000, __FUNCTION__))
212 uint32 res = master->processShmemSerial;
218 if (!mutex || !master || !mutex->enter(5000, __FUNCTION__))
220 uint32 res = master->componentShmemSerial;
226 if (!mutex || !master || !mutex->enter(5000, __FUNCTION__))
228 uint32 res = master->datamapsShmemSerial;
236 if (!mutex || !master || !mutex->enter(5000, __FUNCTION__))
238 uint32 res = ++master->dynamicShmemSerial;
244 if (!mutex || !master || !mutex->enter(5000, __FUNCTION__))
246 uint32 res = ++master->processShmemSerial;
252 if (!mutex || !master || !mutex->enter(5000, __FUNCTION__))
254 uint32 res = ++master->componentShmemSerial;
260 if (!mutex || !master || !mutex->enter(5000, __FUNCTION__))
262 uint32 res = ++master->datamapsShmemSerial;
269 if (!mutex || !master || !mutex->enter(5000, __FUNCTION__))
271 uint64 res = master->dynamicShmemSize;
277 if (!mutex || !master || !mutex->enter(5000, __FUNCTION__))
279 uint64 res = master->processShmemSize;
285 if (!mutex || !master || !mutex->enter(5000, __FUNCTION__))
287 uint64 res = master->componentShmemSize;
293 if (!mutex || !master || !mutex->enter(5000, __FUNCTION__))
295 uint64 res = master->datamapsShmemSize;
301 if (!mutex || !master || !mutex->enter(5000, __FUNCTION__))
303 master->dynamicShmemSize = size;
309 if (!mutex || !master || !mutex->enter(5000, __FUNCTION__))
311 master->processShmemSize = size;
317 if (!mutex || !master || !mutex->enter(5000, __FUNCTION__))
319 master->componentShmemSize = size;
325 if (!mutex || !master || !mutex->enter(5000, __FUNCTION__))
327 master->datamapsShmemSize = size;
392 psycloneIndex = NULL;
399 shouldContinue =
true;
401 isLocalMaster =
false;
402 managementThreadID = 0;
408 shouldContinue =
false;
418 uint16 sweepPort = (isLocalMaster && masterMemory) ? masterMemory->port : 0;
428 delete(masterMemory);
436 LogPrint(0,
LOG_MEMORY, 3,
"Removed %u shared name(s) of port %u at shutdown...", removed, sweepPort);
440 delete(psycloneIndex);
441 psycloneIndex = NULL;
448 uint64 alloc = 0, usage = 0;
478 createdTime = masterMemory->getCreatedTime();
485 uint64 heartbeat = 0;
499 LogPrint(0,
LOG_MEMORY,3,
"Node starting up on port %u, please try again in a little while...", port);
503 this->isLocalMaster = isLocalMaster;
505 if (!masterMemory->open(port)) {
507 delete(masterMemory);
517 delete(masterMemory);
528 delete(masterMemory);
538 delete(masterMemory);
548 delete(masterMemory);
561bool MemoryManager::create(uint16 sysID, uint32 slotCount, uint16 binCount, uint32 minBlockSize, uint32 maxBlockSize, uint64 initSize, uint64 maxSize,
bool force) {
563 this->isLocalMaster =
true;
565 uint64 heartbeat = 0;
582 if (!psycloneIndex->init(sysID,
GetTimeNow())) {
588 if (!masterMemory->create(sysID)) {
594 if (!
temporalMemory->create(slotCount, binCount, minBlockSize, maxBlockSize, initSize, maxSize)) {
633 return masterMemory->getNodeID();
637 masterMemory->setNodeID(
id);
658 tmc = masterMemory->master->currentTMC;
659 adjust = masterMemory->master->localSyncAdjustment;
664 masterMemory->master->currentTMC = tmc;
665 masterMemory->master->localSyncAdjustment = adjust;
690 while (type[level+1])
698 else if (subtype == 0xFFFF)
713 while (context[level+1])
721 else if (subcontext == 0xFFFF)
779uint32 MemoryManager::runManager() {
785 uint64 nextPageCheck = 0;
787 while (shouldContinue) {
814 uint32 maxPageCount = 15;
815 if (!manager->
create(0)) {
823 char* data =
new char[size];
826 uint32 expectedCount = 100;
827 uint32 expectedMaxCount = (maxPageCount-10) * expectedCount;
828 uint32 count = expectedMaxCount;
829 uint64* ids =
new uint64[count];
832 msg->
setData(
"data", data, size);
840 for (n=0; n<count; n++) {
857 for (n=0; n<count; n++) {
860 unittest::fail(
"MemoryManager test: getCopyOfMessage %u failed", n);
877 if ((!msg2->
getInt(
"Test", val64)) || (val64 != n)) {
878 unittest::fail(
"MemoryManager test: getCopyOfMessage wrong data (%lld != %u)", val64, n);
891 double writeUs = (double)(t2 - t1);
892 double readUs = (double)(t3 - t2);
894 unittest::metric(
"write_throughput", (1.0*size*count)/writeUs,
"MB/s",
true);
904 count, (1.0*size*count)/writeUs, (1000000.0*count)/writeUs, (uint32)writeUs, size);
906 count, (1.0*size*count)/readUs, (1000000.0*count)/readUs, (uint32)readUs, size);
3107 "Static memory management insert/read-back throughput",
"memory");
3109 "Shared memory segment smoke check",
"memory");
Central shared-memory manager for a Psyclone node: master segment, per-subsystem shared maps and the ...
#define PSYCLONE_STATUS_ENDED
Instance has shut down (or crashed and was reaped).
#define PSYCLONE_INDEX_SIZE
Total byte size of the instance-index shared-memory segment.
#define PSYCLONE_STATUS_INIT
Instance is starting up, not yet serving.
#define PSYCLONE_INDEX_MAXCOUNT
Maximum number of concurrently tracked Psyclone instances per machine.
#define PSYCLONE_STATUS_READY
Instance is fully operational.
#define PSYCLONE_STATUS_NONE
No instance recorded in this slot.
CMSDK time: µs-resolution 64-bit timestamps and the Time Mapping Constant (TMC).
Small, dependency-free unit test harness used by all CMSDK object tests.
#define thread_ret_val(ret)
#define THREAD_FUNCTION_CALL
Accessor for the shared component registry.
Accessor for the shared name/id maps and the request map.
bool maintenance()
Periodic cleanup (e.g.
The central Psyclone data container: a self-contained binary message with typed, named user entries.
bool setEOL(uint64 eol)
setEOL(uint64 eol)
bool getInt(const char *key, int64 &value)
getInt(const char* key, int64& value)
bool setInt(const char *key, int64 value)
setInt(const char* key, int64 value)
uint32 getSize()
getSize() Get message size Many types of data of any size can be put into a message as user entries; ...
bool setData(const char *key, const char *value, uint32 size)
setData(const char* key, const char* value, uint32 size)
Handle to the node's master shared-memory segment (MemoryMasterStruct).
uint32 incrementDynamicShmemSerial()
Bump the dynamic segment serial after resize.
uint64 getProcessShmemSize()
bool create(uint16 port)
Create the master segment for the node instance listening on port.
uint32 getDataMapsShmemSerial()
uint32 incrementDataMapsShmemSerial()
Bump the data-maps segment serial after resize.
uint16 getID()
MemoryController interface:
uint64 getComponentShmemSize()
bool setComponentShmemSize(uint64 size)
Record a new component segment size.
bool setID(uint16 id)
MemoryController interface: set the node id.
uint16 port
Node port this master segment belongs to (names all related segments).
bool setDataMapsShmemSize(uint64 size)
Record a new data-maps segment size.
uint32 incrementProcessShmemSerial()
Bump the process segment serial after resize.
uint32 incrementComponentShmemSerial()
Bump the component segment serial after resize.
uint32 getComponentShmemSerial()
uint32 getProcessShmemSerial()
uint64 getDynamicShmemSize()
uint32 getDynamicShmemSerial()
bool setDynamicShmemSize(uint64 size)
Record a new dynamic segment size.
bool setNodeID(uint16 id)
Set the cluster-wide node id.
bool setProcessShmemSize(uint64 size)
Record a new process segment size.
bool open(uint16 port)
Attach to an existing master segment created by another process.
uint64 getDataMapsShmemSize()
Top-level facade of the shared-memory subsystem for one process.
ProcessMemory * processMemory
Accessor for the process table and per-process queues.
DataMessage * getCopyOfMessage(uint64 id)
Fetch a private copy of a previously published message.
ComponentMemory * componentMemory
Accessor for the component registry.
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).
static bool UnitTest()
Self-test of the shared-memory manager.
std::string subTypeToText(uint16 subtype)
Resolve a subtype id to its registered name.
DataMessage * retrieveAllSystemIDs()
Enumerate all live Psyclone system ids/ports on this machine (via the PsycloneIndex).
uint8 getNodeStatus(uint64 &lastseen, uint64 &createdTime)
Get the node status from the master segment.
bool setNodeID(uint16 id)
Set this node's cluster-wide id.
static MemoryManager * Singleton
Per-process singleton instance, set by the constructor.
static bool ShmUnitTest()
Self-test of the raw shared-memory primitives.
bool insertMessage(DataMessage *msg, uint64 &id)
Publish a DataMessage into shared (temporal) memory.
std::string getComponentName(uint32 compID)
Resolve a component id to its name.
std::string subContextToText(uint16 subcontext)
Resolve a subcontext id to its registered name.
std::string typeToText(PsyType type)
Resolve a type id to its registered name.
bool getTimeSyncData(uint64 &tmc, int64 &adjust)
Read the cluster time-sync values from the master segment.
bool getMemoryUsage(uint64 &sysAlloc, uint64 &sysUsage, uint64 &dataAlloc, uint64 &dataUsage)
Report allocation/usage figures for the system and data segments.
DataMapsMemory * dataMapsMemory
Accessor for the type/context/tag/crank/request maps.
bool connect(uint16 sysID, bool isMaster)
Attach this process to an existing node's shared segments.
TemporalMemory * temporalMemory
Accessor for time-limited message storage.
friend THREAD_RET THREAD_FUNCTION_CALL MemoryManagement(THREAD_ARG arg)
Background maintenance thread entry point for MemoryManager / MemoryManagerX.
std::string contextToText(PsyContext context)
Resolve a context id to its registered name.
bool setTimeSyncData(uint64 tmc, int64 adjust)
Write the cluster time-sync values into the master segment.
Accessor for the shared process table and per-process queues.
Accessor for the machine-global instance index segment.
bool setStatus(uint8 status)
Update this instance's status and heartbeat in the shared index.
bool init(uint16 port, uint64 instID)
Attach to (or create) the index segment and claim/refresh the slot for this instance.
static bool GetStatus(uint16 port, uint8 &status, uint64 &heartbeat)
Look up the status of the instance registered on port.
Accessor for the temporal (dynamic) message store in shared memory.
static bool CreateThread(THREAD_FUNCTION func, void *args, uint32 &newID, uint32 reqID=0)
Create a new native thread and start it immediately.
static bool Shutdown()
Terminate all managed threads, then destroy the singleton.
static UnitTestRunner & instance()
Access the singleton (created on first use).
void registerTest(const char *name, UnitTestFunc func, const char *description="", const char *category="", bool inDefaultRun=true, uint32 timeoutSec=0)
Register a test with the runner.
Recursive mutual-exclusion lock, optionally named for cross-process use.
uint64 GetTimeNow()
Return the current absolute time (µs since year 0) according to the TMC.
bool SetCurrentTimeSyncData(uint64 tmc, int64 netTimeAdjust)
Install externally computed sync data (e.g.
std::string PrintTimeDifString(uint64 t, bool us=true, bool ms=true)
int32 GetTimeAgeMS(uint64 t)
Age of a timestamp relative to now, in milliseconds.
int64 GetTimeAge(uint64 t)
Age of a timestamp relative to now.
bool GetCurrentTimeSyncData(uint64 &tmc, int64 &netTimeAdjust)
Read the current sync data.
uint32 RemovePortSharedSegments(uint16 port)
Remove every shared-memory/semaphore name for this port (as ClearStaleSharedSegments,...
bool Sleep(uint32 ms)
Suspend the calling thread.
char * OpenSharedMemorySegment(const char *name, uint64 size)
Open and map an existing named shared memory segment.
char * CreateSharedMemorySegment(const char *name, uint64 size, bool force=false)
Create a named shared memory segment and map it into this process.
bool CloseSharedMemorySegment(char *data, uint64 size)
Unmap a segment previously created/opened here.
bool DisownSharedMemorySegment(char *data)
Give up this process's ownership of a segment's name, so closing it here only unmaps.
void ClearStaleSharedSegments(uint16 port)
Remove stale OS shared-memory/semaphore objects left by a crashed node on this port.
void fail(const char *fmt,...)
Set an explanatory reason shown on the FAIL line.
void metric(const char *name, double value, const char *unit="", bool higherIsBetter=true)
Record a performance metric.
void detail(const char *fmt,...)
Verbose-only indented diagnostic line (shown only when verbose=1).
void progress(int percent, const char *action)
Report progress with a short description of the current action.
std::string StringFormat(const char *format,...)
printf into a std::string.
THREAD_RET THREAD_FUNCTION_CALL MemoryManagement(THREAD_ARG arg)
Background maintenance thread entry point for MemoryManager / MemoryManagerX.
void Register_MemoryManager_Tests()
Hierarchical execution-context identifier.
Hierarchical message type identifier — the key used for publish/subscribe matching in Psyclone.
Root header of the node's master shared-memory segment.
One slot in the machine-global Psyclone instance index.
uint16 port
TCP port the instance listens on; doubles as the instance key.
uint64 heartbeat
Last heartbeat timestamp (µs since epoch); staleness implies a dead instance.
uint8 status
One of the PSYCLONE_STATUS_* values.
Header of the machine-global Psyclone instance index shared-memory segment.