CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
Loading...
Searching...
No Matches
ComponentMemory.h
Go to the documentation of this file.
1
18
19#if !defined(_COMPONENTMEMORY_H_)
20#define _COMPONENTMEMORY_H_
21
22#include "MemoryManager.h"
23#include "PsyTime.h"
24#include "Subscriptions.h"
25
26namespace cmlabs {
27
28
31#define COMPSTATUS_IDLE 1
32#define COMPSTATUS_STARTING 2
33#define COMPSTATUS_RUNNING 3
34#define COMPSTATUS_FINISHED 4
36
38#define NOTINUSE 65535
39
44#define GETCOMPOFFSETP(header,id) (uint64*)(((char*)header) + sizeof(ComponentMemoryStruct) + (id*sizeof(uint64)))
45#define GETCOMPOFFSET(header,id) *GETCOMPOFFSETP(header,id)
46#define GETCOMPDATA(header,id) (ComponentInfoStruct*)(GETCOMPOFFSET(header,id) ? ((char*)header) + (header->indexSize * sizeof(uint64)) + GETCOMPOFFSET(header,id) : NULL)
48
53 uint64 size;
54 uint32 cid;
55 uint64 createdTime; // time of creation
56 uint32 indexSize;
57 uint32 count;
58 uint32 maxID;
59 uint64 usage;
60 // index * N, each a uint64
61};
62
65#define PARAM_STRING 1
66#define PARAM_INTEGER 2
67#define PARAM_FLOAT 3
68#define PARAM_STRING_COLL 4
69#define PARAM_INTEGER_COLL 5
70#define PARAM_FLOAT_COLL 6
71#define PARAM_TYPE_COLL 7
73
75#define COMPONENT_CAN_MIGRATE 1
76
81struct ComponentStats {
82 uint64 time;
83 uint64 msgInCount;
84 uint64 msgInBytes;
86 uint64 msgOutCount;
87 uint64 msgOutBytes;
91 uint64 cycleCount;
92 uint64 runCount;
95 uint32 recentInPos; // circular write index into recentInMsg ring (newest written here, then advanced)
96 uint32 recentOutPos; // circular write index into recentOutMsg ring
98 std::string toXML() {
99 std::string xmlIn, xmlOut;
100 int n;
101 const char* oldData = NULL;
102 DataMessage* msg = new DataMessage();
103 char* msgData = recentInMsg;
104 for (n = 0; n < 10; n++) {
105 if (((DataMessageHeader*)msgData)->time) {
106 if (!oldData) oldData = msg->swapMessageData(msgData);
107 else msg->swapMessageData(msgData);
108 xmlIn += msg->toXML();
109 }
110 msgData += DRAFTMSGSIZE;
111 }
112 msgData = recentOutMsg;
113 for (n = 0; n < 10; n++) {
114 if (((DataMessageHeader*)msgData)->time) {
115 if (!oldData) oldData = msg->swapMessageData(msgData);
116 else msg->swapMessageData(msgData);
117 xmlOut += msg->toXML();
118 }
119 msgData += DRAFTMSGSIZE;
120 }
121 if (oldData)
122 msg->swapMessageData(oldData);
123 delete(msg);
124 return utils::StringFormat("<componentstats runcount=\"%llu\" msgincount=\"%llu\" msginbytes=\"%llu\" msgoutcount=\"%llu\" msgoutbytes=\"%llu\" time=\"%llu\">\n<inmsg>\n%s</inmsg>\n<outmsg>\n%s</outmsg>\n</componentstats>\n",
125 runCount, msgInCount, msgInBytes, msgOutCount, msgOutBytes, time, xmlIn.c_str(), xmlOut.c_str());
126 }
127};
128
131#define SELFTRIGGER_NONE 0
132#define SELFTRIGGER_WARN 1
133#define SELFTRIGGER_ALLOW 2
135
136
142 uint64 size;
143 uint32 id;
146 uint8 type;
147 uint8 status;
148 uint8 policy;
149 uint16 nodeID;
150 uint16 procID;
151 // Basic data
152 uint64 createdTime;
156 // Statistics
159 // Parameters
161 uint32 paramSize;
162 // Private Data
163 uint16 dataCount;
164 uint64 dataSize;
165
166 std::string toXML(bool printStats = true) {
167 if (printStats)
168 return utils::StringFormat("<component id=\"%u\" status=\"%u\" name=\"%s\" node=\"%u\" proc=\"%u\" type=\"%u\" policy=\"%u\" paramcount=\"%u\" paramsize=\"%u\" datacount=\"%u\" datasize=\"%llu\" createdtime=\"%llu\" migratedtime=\"%llu\" lastupdatetime=\"%llu\" selftrigger=\"%u\">\n%s%s</component>\n",
170 else
171 return utils::StringFormat("<component id=\"%u\" status=\"%u\" name=\"%s\" node=\"%u\" proc=\"%u\" type=\"%u\" policy=\"%u\" paramcount=\"%u\" paramsize=\"%u\" datacount=\"%u\" datasize=\"%llu\" createdtime=\"%llu\" migratedtime=\"%llu\" lastupdatetime=\"%llu\" selftrigger=\"%u\" />\n",
173 }
174 bool writeToMsg(uint32 n, DataMessage* msg) {
175 if (!msg) return false;
176 msg->setInt((int64)n, "ID", id);
177 msg->setInt((int64)n, "Status", status);
178 msg->setInt((int64)n, "Type", type);
179 msg->setInt((int64)n, "Policy", policy);
180 msg->setInt((int64)n, "SelfTrigger", selfTrigger);
181 msg->setInt((int64)n, "Size", size);
182 msg->setInt((int64)n, "NodeID", nodeID);
183 msg->setInt((int64)n, "ProcID", procID);
184 msg->setString((int64)n, "Name", name);
185 msg->setTime((int64)n, "CreatedTime", createdTime);
186 return true;
187 }
188};
189
191#define COMPINFOUSAGE(info) (sizeof(ComponentInfoStruct) + info->paramSize + info->dataSize)
192
197 uint64 size;
198 // name, 0, mimetype, 0, data
199};
200
206 uint32 size;
207 uint8 type;
208 // name, 0, struct
209};
210
211// -- integer --
215 int64 val;
217 int64 minVal;
218 int64 maxVal;
219 int64 interval;
220};
221
222// -- float --
226 float64 val;
227 float64 defaultVal;
228 float64 minVal;
229 float64 maxVal;
230 float64 interval;
231};
232
233// -- integer collection --
237 uint32 index;
239 uint32 count;
240};
241
244#define CHECKCOMPONENTMEMORYSERIAL if (serial != master->getComponentShmemSerial()) {if (!open()) {mutex->leave();return 0;}}
245#define CHECKCOMPONENTMEMORYSERIALRETURN(a) if (serial != master->getComponentShmemSerial()) {if (!open()) {mutex->leave();return a;}}
246
247class MasterMemory;
263public:
264 static bool UnitTest();
265
269
271 bool getMemoryUsage(uint64& alloc, uint64& usage);
272
273 bool open();
275 bool create(uint32 indexSize);
276
277 std::vector<ComponentInfoStruct>* getAllComponents();
278 std::vector<ComponentInfoStruct>* getAllLocalComponents();
279 std::vector<ComponentInfoStruct>* getAllLocalComponents(uint8 type);
280 std::vector<ComponentInfoStruct>* getAllModules();
281 std::vector<ComponentInfoStruct>* getAllCatalogs();
282
285 bool syncComponents(DataMessage* msg);
286
287 bool confirmComponentID(uint32 id);
288 bool cancelComponentID(uint32 id);
293 uint8 lookupComponentID(const char* name, uint32 &id); // status = 0: not there, 1: in-sync, 2: ready
300 bool createComponent(uint32 id, uint8 type, uint8 policy, uint8 selfTrigger, const char* name, uint64 size, uint16 nodeID, uint16 procID, uint64 time, uint32& existingID);
301// bool recordRemoteComponent(uint32 id, const char* name, uint16 nodeID, uint64 time);
302// bool reserveComponentID(uint32 id, const char* name);
303 bool destroyComponent(uint32 cid);
304 bool getComponentName(uint32 cid, char* name, uint32 maxNameLen);
305 std::string getComponentNameString(uint32 cid);
306 uint8 getComponentType(uint32 cid);
307 bool getComponentID(const char* name, uint32 &cid);
309 bool setComponentStats(uint32 cid, ComponentStats& stats);
311 bool setComponentPerfStats(uint32 cid, AveragePerfStats &perfStruct);
312
313 bool isComponentLocal(uint32 cid);
314 bool isComponentLocal(const char* name);
315 bool getComponentLocation(uint32 cid, uint16& nodeID, uint16& procID);
316 uint8 getComponentPolicy(uint32 cid);
318 bool updateComponentInformation(uint32 cid, uint8 type, uint8 policy, uint8 selfTrigger, uint16 nodeID, uint16 procID);
319 bool setComponentPolicy(uint32 cid, uint8 policy);
320 uint8 getComponentSelfTrigger(uint32 cid);
321 bool setComponentSelfTrigger(uint32 cid, uint8 selfTrigger);
322 uint16 getComponentNodeID(uint32 cid);
323 uint16 getComponentProcessID(uint32 cid);
324 bool updateComponentLocation(uint32 cid, uint16 nodeID, uint16 procID);
325 // char* GetAndRemoveComponent(uint32 cid, uint32& dataSize);
326 // bool addExistingComponent(uint32 cid, uint16 nodeID, uint16 procID, char* oldData, uint32 dataSize);
327 // bool updateRemoteComponent(uint32 cid, uint16 nodeID, uint16 procID, const char* name);
330 bool addComponentStats(uint32 cid, uint8 status, uint64 usageCPUTicks,
331 DataMessage* inputMsg, DataMessage* outputMsg, uint32 runCount, uint32 cycleCount);
332
333 bool canComponentMigrate(uint32 cid);
334 std::list<uint16>* findProcessComponents(uint16 procID);
335
338 char* getComponentData(uint32 cid, uint64& size);
340 bool setComponentData(uint32 cid, const char* data, uint64 size, bool wasMigrated);
341
342 // Private Data
346 bool setPrivateData(uint32 cid, const char* name, const char* data, uint64 size, const char* mimetype = NULL);
347 uint64 getPrivateDataSize(uint32 cid, const char* name);
348 std::string getPrivateDataMimetype(uint32 cid, const char* name);
349 bool getPrivateData(uint32 cid, const char* name, char* data, uint64 maxSize);
350 char* getPrivateDataCopy(uint32 cid, const char* name, uint64 &size);
351 bool deletePrivateData(uint32 cid, const char* name);
352 std::map<std::string, std::string> getPrivateDataKeysAndTypes(uint32 cid);
353 std::list<std::string> getPrivateDataKeys(uint32 cid);
355
356 // Parameters
362 bool createParameter(uint32 cid, const char* name, const char* val, const char* defaultValue = NULL);
363 bool createParameter(uint32 cid, const char* name, const char* val, uint32 count, uint32 defaultIndex);
364 bool createParameter(uint32 cid, const char* name, std::vector<std::string> values, const char* defaultValue = NULL);
365 bool createParameter(uint32 cid, const char* name, int64* val, uint32 count, uint32 defaultIndex);
366 bool createParameter(uint32 cid, const char* name, std::vector<std::string> values, int64 defaultValue);
367 bool createParameter(uint32 cid, const char* name, std::vector<int64> values, int64 defaultValue = 0);
368 bool createParameter(uint32 cid, const char* name, float64* val, uint32 count, uint32 defaultIndex);
369 bool createParameter(uint32 cid, const char* name, std::vector<std::string> values, float64 defaultValue);
370 bool createParameter(uint32 cid, const char* name, std::vector<float64> values, float64 defaultValue = 0);
371 bool createParameter(uint32 cid, const char* name, int64 val, int64 min = 0, int64 max = 0, int64 interval = 0);
372 bool createParameter(uint32 cid, const char* name, float64 val, float64 min = 0, float64 max = 0, float64 interval = 0);
374
375 bool hasParameter(uint32 cid, const char* name);
376 bool deleteParameter(uint32 cid, const char* name);
377
382 uint8 getParameterDataType(uint32 cid, const char* name);
383 uint32 getParameterValueSize(uint32 cid, const char* name);
384 bool getParameter(uint32 cid, const char* name, char* val, uint32 maxSize);
385 bool getParameter(uint32 cid, const char* name, int64& val);
386 bool getParameter(uint32 cid, const char* name, float64& val);
387 std::string getParameterString(uint32 cid, const char* name, uint32 maxSize);
388 std::string getParameterString(uint32 cid, const char* name);
389 int64 getParameterInt(uint32 cid, const char* name);
390 float64 getParameterFloat(uint32 cid, const char* name);
391 bool getParameterAsBool(uint32 cid, const char* name);
392
393 bool setParameter(uint32 cid, const char* name, const char* val);
394 bool setParameter(uint32 cid, const char* name, int64 val);
395 bool setParameter(uint32 cid, const char* name, float64 val);
396
397 bool resetParameter(uint32 cid, const char* name);
398 bool tweakParameter(uint32 cid, const char* name, int32 tweak);
400
401 bool addLocalPerformanceStats(std::list<PerfStats> &perfStats);
402 std::string printFriendly();
403 std::string toXML(bool stats = true);
404
405private:
406 bool resize(uint64 newMemorySize);
407 bool resizeIndex(uint32 newIndexSize);
408 bool resizeComponent(uint32 id, uint64 newSize);
409 bool growComponent(uint32 id, uint64 addSize);
410 bool getSurroundingComponents(uint32 cid, uint32& prevID, uint32& nextID);
411
412 char* makeRoomForPrivateData(uint32 cid, uint64 newSize);
413 char* makeRoomForParameter(uint32 cid, uint64 newSize);
414
415 ParamHeader* getParameter(uint32 cid, const char* name);
416 PrivateHeader* getPrivateData(uint32 cid, const char* name);
417
418 utils::Mutex* mutex;
419 MasterMemory* master;
420 ComponentMemoryStruct* header;
421 uint64 memorySize;
422 uint16 port;
423 uint32 serial;
424};
425
426} // namespace cmlabs
427
428#endif //_COMPONENTMEMORY_H_
429
Central shared-memory manager for a Psyclone node: master segment, per-subsystem shared maps and the ...
#define DRAFTMSGSIZE
Fixed byte size of the per-message draft slots used in the recent-message rings of ComponentStats / P...
CMSDK time: µs-resolution 64-bit timestamps and the Time Mapping Constant (TMC).
Subscription, trigger, retrieval, query, post and signal specifications — the declarative wiring of a...
#define MAXKEYNAMELEN
Definition Utils.h:85
bool setParameter(uint32 cid, const char *name, const char *val)
bool writeComponentsToMsg(DataMessage *msg)
Serialize all component records into msg.
bool create(uint32 indexSize)
Create the component segment (master only).
uint16 getComponentNodeID(uint32 cid)
bool getComponentName(uint32 cid, char *name, uint32 maxNameLen)
Copy the component name into name.
bool open()
Attach to the existing component segment.
bool getParameterAsBool(uint32 cid, const char *name)
bool syncComponents(DataMessage *msg)
Merge component records received from a remote node.
std::string getComponentNameString(uint32 cid)
bool updateComponentInformation(uint32 cid, uint8 type, uint8 policy, uint8 selfTrigger, uint16 nodeID, uint16 procID)
Update type/policy/self-trigger/location in one call.
bool deletePrivateData(uint32 cid, const char *name)
Delete blob name.
bool updateComponentLocation(uint32 cid, uint16 nodeID, uint16 procID)
Record a new location (after migration).
std::vector< ComponentInfoStruct > * getAllModules()
Snapshot all module components.
uint8 getParameterDataType(uint32 cid, const char *name)
bool deleteParameter(uint32 cid, const char *name)
Remove the parameter.
bool setComponentStats(uint32 cid, ComponentStats &stats)
Overwrite the component's stats.
bool cancelComponentID(uint32 id)
Release a provisionally reserved component id.
bool getComponentID(const char *name, uint32 &cid)
Look up a component id by name.
AveragePerfStats getComponentPerfStats(uint32 cid)
uint16 getComponentProcessID(uint32 cid)
bool setComponentData(uint32 cid, const char *data, uint64 size, bool wasMigrated)
Replace a component's data area (after migration).
bool addLocalPerformanceStats(std::list< PerfStats > &perfStats)
Append PerfStats for all local components.
bool tweakParameter(uint32 cid, const char *name, int32 tweak)
Step the value by tweak * interval (or collection index).
char * getPrivateDataCopy(uint32 cid, const char *name, uint64 &size)
ComponentMemory(MasterMemory *master)
std::list< std::string > getPrivateDataKeys(uint32 cid)
uint8 lookupComponentID(const char *name, uint32 &id)
Look up a component by name without filtering on readiness.
bool destroyComponent(uint32 cid)
Remove a component record and free its space.
float64 getParameterFloat(uint32 cid, const char *name)
uint32 getParameterValueSize(uint32 cid, const char *name)
uint8 getComponentPolicy(uint32 cid)
bool confirmComponentID(uint32 id)
Mark a provisionally reserved component id as ready (syncStatus 2).
bool getPrivateData(uint32 cid, const char *name, char *data, uint64 maxSize)
Copy blob into caller buffer data (max maxSize).
bool canComponentMigrate(uint32 cid)
std::string getPrivateDataMimetype(uint32 cid, const char *name)
bool getComponentLocation(uint32 cid, uint16 &nodeID, uint16 &procID)
Get the node/process the component runs in.
std::list< uint16 > * findProcessComponents(uint16 procID)
List ids of components in process procID.
std::vector< ComponentInfoStruct > * getAllComponents()
Snapshot all components.
uint64 getPrivateDataSize(uint32 cid, const char *name)
std::vector< ComponentInfoStruct > * getAllCatalogs()
Snapshot all catalog components.
bool setComponentPerfStats(uint32 cid, AveragePerfStats &perfStruct)
Store new performance averages.
char * getComponentData(uint32 cid, uint64 &size)
Copy a component's whole data area (for migration).
static bool UnitTest()
Self-test.
bool hasParameter(uint32 cid, const char *name)
std::map< std::string, std::string > getPrivateDataKeysAndTypes(uint32 cid)
uint8 getComponentSelfTrigger(uint32 cid)
bool createComponent(uint32 id, uint8 type, uint8 policy, uint8 selfTrigger, const char *name, uint64 size, uint16 nodeID, uint16 procID, uint64 time, uint32 &existingID)
Register a component record.
bool isComponentLocal(uint32 cid)
bool addComponentStats(uint32 cid, uint8 status, uint64 usageCPUTicks, DataMessage *inputMsg, DataMessage *outputMsg, uint32 runCount, uint32 cycleCount)
Fold a run's activity into the component's stats (counters, rings, CPU ticks).
bool setComponentPolicy(uint32 cid, uint8 policy)
Set the migration policy.
bool getParameter(uint32 cid, const char *name, char *val, uint32 maxSize)
bool resetParameter(uint32 cid, const char *name)
Restore the parameter's default value/index.
std::vector< ComponentInfoStruct > * getAllLocalComponents()
Snapshot components on this node.
uint8 getComponentType(uint32 cid)
ComponentStats getComponentStats(uint32 cid)
bool setPrivateData(uint32 cid, const char *name, const char *data, uint64 size, const char *mimetype=NULL)
Create/replace blob name.
std::string toXML(bool stats=true)
int64 getParameterInt(uint32 cid, const char *name)
std::string getParameterString(uint32 cid, const char *name, uint32 maxSize)
bool createParameter(uint32 cid, const char *name, const char *val, const char *defaultValue=NULL)
bool writeComponentNamesToMsg(DataMessage *msg)
Write just the component names into msg.
bool setComponentSelfTrigger(uint32 cid, uint8 selfTrigger)
Set the self-trigger policy.
bool getMemoryUsage(uint64 &alloc, uint64 &usage)
Report allocation/usage of the component segment.
The central Psyclone data container: a self-contained binary message with typed, named user entries.
bool setTime(const char *key, uint64 value)
setTime(const char* key, uint64 value)
std::string toXML(std::map< uint16, std::string > *subtypes=NULL, std::map< uint16, std::string > *subcontexts=NULL, std::map< uint32, std::string > *compNames=NULL)
toXML()
bool setString(const char *key, const char *value)
setString(const char* key, const char* value)
bool setInt(const char *key, int64 value)
setInt(const char* key, int64 value)
const char * swapMessageData(const char *data)
swapMessageData(const char* data)
Handle to the node's master shared-memory segment (MemoryMasterStruct).
Recursive mutual-exclusion lock, optionally named for cross-process use.
Definition Utils.h:463
std::string StringFormat(const char *format,...)
printf into a std::string.
Definition Utils.cpp:7033
Sliding-window averages derived from successive PerfStats snapshots.
One component's registry record in shared memory.
uint16 nodeID
Node the component currently runs on.
uint8 policy
Migration policy (COMPONENT_CAN_MIGRATE flag).
uint64 size
Total record size in bytes including parameter and data areas.
uint8 type
Component type (Whiteboard, Catalog, Module, ...).
uint8 selfTrigger
SELFTRIGGER_* policy.
std::string toXML(bool printStats=true)
char name[MAXKEYNAMELEN]
Component name (unique per system).
uint64 createdTime
Creation timestamp (µs).
uint16 procID
Process (space) the component currently runs in.
bool writeToMsg(uint32 n, DataMessage *msg)
uint64 migratedTime
Timestamp of the last migration (µs), 0 if never migrated.
uint64 lastUpdateTime
Timestamp of the last record update (µs).
uint32 id
Component id (index into the segment's offset table).
uint8 status
COMPSTATUS_* run status.
uint8 syncStatus
Cluster sync status (0: not there, 1: in-sync, 2: ready).
Root header of the component-memory segment.
uint64 size
Total segment size in bytes.
uint32 count
Number of registered components.
uint32 maxID
Highest component id assigned so far.
uint64 usage
Bytes currently in use in the segment.
uint32 cid
Check/magic id validated on attach.
uint32 indexSize
Number of offset slots in the index.
Legacy per-component throughput counters (DataMessageHeader-based rings).
uint64 usageCPUTicks
Cumulative CPU ticks consumed.
uint32 migrationCount
Times this component has migrated.
uint64 msgInBytes
Total bytes received.
uint64 msgInCount
Total messages received.
uint64 cycleCount
Total processing cycles.
uint64 time
Timestamp of the last update (µs).
uint64 currentRunStartTime
Timestamp the current/last run started (µs).
uint64 runCount
Total runs.
uint64 msgOutBytes
Total bytes posted.
DataMessageHeader recentInMsg[10]
uint64 firstRunStartTime
Timestamp of the first run (µs).
std::string toXML()
Serialize counters and recent messages as a <componentstats> XML element.
DataMessageHeader recentOutMsg[10]
uint64 msgOutCount
Total messages posted.
The current (version 10) DataMessage wire/shared-memory header.
Value block of a PARAM_*_COLL parameter; followed by the packed collection values.
uint32 defaultIndex
Entry restored by resetParameter().
uint32 index
Currently selected entry.
uint32 count
Number of entries in the collection.
Value block of a PARAM_FLOAT parameter.
float64 interval
Step used by tweakParameter().
float64 minVal
Inclusive lower bound.
float64 maxVal
Inclusive upper bound.
float64 defaultVal
Value restored by resetParameter().
float64 val
Current value.
Size- and type-prefixed parameter entry stored after a component record.
uint32 size
Total entry size in bytes including this header.
uint8 type
PARAM_* data type.
Value block of a PARAM_INTEGER parameter.
int64 defaultVal
Value restored by resetParameter().
int64 maxVal
Inclusive upper bound.
int64 interval
Step used by tweakParameter().
int64 minVal
Inclusive lower bound (0 = unbounded when max also 0).
Size-prefixed private-data entry stored after a component record.
uint64 size
Total entry size in bytes including this header.