CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
Loading...
Searching...
No Matches
DataMapsMemory.h
Go to the documentation of this file.
1
17
18#if !defined(_DATAMAPSMEMORY_H_)
19#define _DATAMAPSMEMORY_H_
20
21#include "MemoryManager.h"
22#include "PsyTime.h"
23
24namespace cmlabs {
25
31 uint64 size;
32 uint32 cid;
33 uint64 createdTime;
36 uint64 typesSize;
39 uint64 contextsSize;
40 uint32 tagsMaxCount;
42 uint64 tagsSize;
45 uint64 cranksSize;
46// uint32 componentsMaxCount;
47// uint32 componentsBitFieldSize;
48// uint32 componentsSize;
51 uint64 requestsSize;
52};
53
58 uint64 size; // total size of map structure in bytes
59 uint32 maxID; // maximum number of entries allocated
60 uint32 count; // number of entries
61 uint32 bitFieldSize; // number of bytes in the bitfield
62 // Bitfield
63 // Entries
64};
65
69 uint16 id; // id, corresponds to the index in map
70 uint8 status; // status = 0: not there, 1: in-sync, 2: ready
71 uint64 time; // timestamp when entered
72 char name[MAXKEYNAMELEN+1]; // name in text
73 std::string toXML() {
74 return utils::StringFormat("\t<type id=\"%u\" name=\"%s\" status=\"%u\" time=\"%llu\" />\n", id, name, status, time);
75 }
76 static std::string GetOuterXMLName() { return "types"; }
77 bool writeToMsg(uint32 n, DataMessage* msg) {
78 if (!msg) return false;
79 msg->setInt((int64)n, "ID", id);
80 msg->setInt((int64)n, "Status", status);
81 msg->setString((int64)n, "Name", name);
82 msg->setTime((int64)n, "CreatedTime", time);
83 return true;
84 }
85 bool writeIDToMsg(const char* arrayname, uint32 n, DataMessage* msg) {
86 if (!msg) return false;
87 msg->setString((int64)n, arrayname, name);
88 return true;
89 }
90};
91
95 uint16 id; // id, corresponds to the index in map
96 uint8 status; // status = 0: not there, 1: in-sync, 2: ready
97 uint64 time; // timestamp when entered
98 char name[MAXKEYNAMELEN+1]; // name in text
99 std::string toXML() {
100 return utils::StringFormat("\t<context id=\"%u\" name=\"%s\" status=\"%u\" time=\"%llu\" />\n", id, name, status, time);
101 }
102 static std::string GetOuterXMLName() { return "contexts"; }
103 bool writeToMsg(uint32 n, DataMessage* msg) {
104 if (!msg) return false;
105 msg->setInt((int64)n, "ID", id);
106 msg->setInt((int64)n, "Status", status);
107 msg->setString((int64)n, "Name", name);
108 msg->setTime((int64)n, "CreatedTime", time);
109 return true;
110 }
111 bool writeIDToMsg(const char* arrayname, uint32 n, DataMessage* msg) {
112 if (!msg) return false;
113 msg->setString((int64)n, arrayname, name);
114 return true;
115 }
116};
117
121 uint32 id; // id, corresponds to the index in map
122 uint8 status; // status = 0: not there, 1: in-sync, 2: ready
123 uint64 time; // timestamp when entered
124 char name[MAXKEYNAMELEN+1]; // name in text
125 std::string toXML() {
126 return utils::StringFormat("\t<tag id=\"%u\" name=\"%s\" status=\"%u\" time=\"%llu\" />\n", id, name, status, time);
127 }
128 static std::string GetOuterXMLName() { return "tags"; }
129 bool writeToMsg(uint32 n, DataMessage* msg) {
130 if (!msg) return false;
131 msg->setInt((int64)n, "ID", id);
132 msg->setInt((int64)n, "Status", status);
133 msg->setString((int64)n, "Name", name);
134 msg->setTime((int64)n, "CreatedTime", time);
135 return true;
136 }
137 bool writeIDToMsg(const char* arrayname, uint32 n, DataMessage* msg) {
138 if (!msg) return false;
139 msg->setString((int64)n, arrayname, name);
140 return true;
141 }
142};
143
147 uint16 id; // id, corresponds to the index in map
148 uint8 status; // status = 0: not there, 1: in-sync, 2: ready
149 uint64 time; // timestamp when entered
150 uint32 compID; // id of parent component
151 char name[MAXKEYNAMELEN + 1]; // name in text
152 char language[MAXKEYNAMELEN + 1]; // name in text
153 char function[MAXKEYNAMELEN+1]; // name in text
154 char libraryFilename[MAXKEYNAMELEN+1]; // name in text
155 char script[MAXSCRIPTLEN+1]; // script in text
156 std::string toXML() {
157 if (strlen(script))
158 return utils::StringFormat("\t<crank id=\"%u\" name=\"%s\" status=\"%u\" time=\"%llu\" compid=\"%u\" function=\"%s\" library=\"%s\">\n\t\t%s\n\t</crank>\n",
160 else
161 return utils::StringFormat("\t<crank id=\"%u\" name=\"%s\" status=\"%u\" time=\"%llu\" compid=\"%u\" function=\"%s\" library=\"%s\" />\n",
163 }
164 static std::string GetOuterXMLName() { return "cranks"; }
165 bool writeToMsg(uint32 n, DataMessage* msg) {
166 if (!msg) return false;
167 msg->setInt((int64)n, "ID", id);
168 msg->setInt((int64)n, "CompID", compID);
169 msg->setInt((int64)n, "Status", status);
170 msg->setString((int64)n, "Name", name);
171 msg->setString((int64)n, "Language", language);
172 msg->setString((int64)n, "Function", function);
173 msg->setString((int64)n, "LibraryFilename", libraryFilename);
174 msg->setString((int64)n, "Script", script);
175 msg->setTime((int64)n, "CreatedTime", time);
176 return true;
177 }
178 bool writeIDToMsg(const char* arrayname, uint32 n, DataMessage* msg) {
179 if (!msg) return false;
180 msg->setString((int64)n, arrayname, name);
181 return true;
182 }
183};
184
190#define REQ_CREATED 1
191#define REQ_RECEIVED_LOCAL 2
192#define REQ_RECEIVED_REMOTE 3
193#define REQ_PROCESSING_LOCAL 4
194#define REQ_PROCESSING_REMOTE 5
195#define REQ_REPLY_READY 6
196#define REQ_FAILED_TO_SEND 7
197#define REQ_FAILED 8
198#define REQ_FAILED_DATA 9
199#define REQ_FAILED_NODATA 10
200#define REQ_FAILED_DATA_EOL 11
201#define REQ_SUCCESS 12
202#define REQ_SUCCESS_DATA 13
203#define REQ_SUCCESS_NODATA 14
204#define REQ_SUCCESS_DATA_EOL 15
206
211 uint32 id;
212 uint64 time;
214 uint64 lastUpdate;
215 uint8 status;
216 uint32 from;
217 uint32 to;
218 uint32 remoteID;
221 std::string toXML() {
222 return utils::StringFormat("\t<request id=\"%u\" name=\"%s\" status=\"%u\" time=\"%llu\" lastupdate=\"%llu\" from=\"%u\" to=\"%u\" remoteid=\"%u\" msgid=\"%llu\" msgeol=\"%llu\" />\n",
224 }
225 static std::string GetOuterXMLName() { return "requests"; }
226};
227
230#define CHECKDATAMAPSMEMORYSERIAL if (serial != master->getDataMapsShmemSerial()) {if (!open()) {mutex->leave();return 0;}}
231
232class MasterMemory;
246public:
250
251 bool open();
253 bool create(uint32 typesMaxCount, uint32 contextsMaxCount, uint32 tagsMaxCount, uint32 cranksMaxCount, uint32 requestsMaxCount);
254
255 bool getMemoryUsage(uint64& alloc, uint64& usage);
256 bool maintenance();
257
258 std::string printFriendlyHTML();
259 std::string toXML();
260
261 bool writeIDsToMsg(DataMessage* msg);
262
263 // TypeLevelMap
269 bool confirmTypeLevelID(uint16 id);
270 bool cancelTypeLevelID(uint16 id);
271 bool createNewTypeLevel(uint16 id, const char* name, uint64 time, uint16& existingID);
272 bool syncTypeLevels(DataMessage* msg);
273 bool getTypeLevelName(uint16 id, char* name, uint32 maxSize);
274 bool getTypeLevelID(const char* name, uint16 &id);
275 uint8 lookupTypeLevelID(const char* name, uint16 &id); // status = 0: not there, 1: in-sync, 2: ready
276 bool deleteTypeLevel(const char* name);
277 bool deleteTypeLevel(uint16 id);
278 uint64 getTypeLevelCreateTime(uint16 id);
279 std::string printAllTypes();
281
282 // ContextLevelMap
286 bool confirmContextLevelID(uint16 id);
287 bool cancelContextLevelID(uint16 id);
288 bool createNewContextLevel(uint16 id, const char* name, uint64 time, uint16& existingID);
290 bool getContextLevelName(uint16 id, char* name, uint32 maxSize);
291 bool getContextLevelID(const char* name, uint16 &id);
292 uint8 lookupContextLevelID(const char* name, uint16 &id); // status = 0: not there, 1: in-sync, 2: ready
293 bool deleteContextLevel(const char* name);
294 bool deleteContextLevel(uint16 id);
295 uint64 getContextLevelCreateTime(uint16 id);
296 std::string printAllContexts();
298
299 // TagMap
302 bool writeTagsToMsg(DataMessage* msg);
303 bool confirmTagID(uint32 id);
304 bool cancelTagID(uint32 id);
305 bool createNewTag(uint32 id, const char* name, uint64 time, uint32& existingID);
306 bool syncTags(DataMessage* msg);
307 bool getTagName(uint32 id, char* name, uint32 maxSize);
308 bool getTagID(const char* name, uint32 &id);
309 uint8 lookupTagID(const char* name, uint32 &id); // status = 0: not there, 1: in-sync, 2: ready
310 bool deleteTag(const char* name);
311 bool deleteTag(uint32 id);
312 uint64 getTagCreateTime(uint32 id);
314
315 // CrankMap
319 bool writeCranksToMsg(DataMessage* msg);
320 bool confirmCrankID(uint16 id);
321 bool cancelCrankID(uint16 id);
322 bool createNewCrank(uint16 id, uint32 compID, const char* name, const char* function, const char* libraryFilename, const char* language, const char* script, uint64 time, uint16& existingID);
323 bool syncCranks(DataMessage* msg);
324 bool getCrankName(uint16 id, char* name, uint32 maxSize);
325 bool getCrankFunction(uint16 id, char* function, uint32 maxSize);
326 bool getCrankLanguage(uint16 id, char* language, uint32 maxSize);
327 bool getCrankLibraryFilename(uint16 id, char* libraryFilename, uint32 maxSize);
328 bool getCrankID(const char* name, uint16 &id);
329 uint8 lookupCrankID(const char* name, uint16 &id); // status = 0: not there, 1: in-sync, 2: ready
330 uint32 getCrankCompID(uint16 id);
331 bool getCrankScript(uint16 id, char* script, uint32 maxSize);
332 bool deleteCrank(uint16 id);
333 bool deleteCrank(const char* name);
334 uint64 getCrankCreateTime(uint16 id);
336
337 // RequestMap
343 bool createNewRequest(uint32 from, uint32 to, uint32 remoteID, uint32 &id);
344 bool getRequestInfo(uint32 id, uint32& from, uint32& to, uint32& remoteID);
345 bool getRequestStatus(uint32 id, uint8& status, uint64& time, uint64& msgID, uint64& msgEOL);
346 bool setRequestStatus(uint32 id, uint8 status);
347 bool setRequestStatus(uint32 id, uint8 status, uint64 msgID, uint64 msgEOL);
348 bool waitForRequestReply(uint32 id, uint32 timeout, uint8& status, uint64& time, uint64& msgID, uint64& msgEOL);
349 bool deleteRequest(uint32 id);
350 uint32 getRequestCount();
352
353private:
354 bool resize(uint16 port, uint32 serial, uint32 typesMaxCount, uint32 contextsMaxCount, uint32 tagsMaxCount, uint32 cranksMaxCount, uint32 requestsMaxCount);
355
356 utils::Mutex* mutex;
357 MasterMemory* master;
358 DataMapsMemoryStruct* header;
359 uint64 memorySize;
360 uint16 port;
361 uint32 serial;
362
363 // headers for each map type
364 GenericMapHeader* typeMapHeader;
365 GenericMapHeader* contextMapHeader;
366 GenericMapHeader* tagMapHeader;
367 GenericMapHeader* crankMapHeader;
368// GenericMapHeader* componentMapHeader;
369 GenericMapHeader* requestMapHeader;
370};
371
378template <typename T, typename ID>
380public:
381 // Generic MemoryMap
382 static T* CreateEntry(char* data, ID id, const char* name);
383 static T* CreateFirstFreeEntry(char* data, ID &id, const char* name);
384 static T* GetEntry(char* data, ID id);
385 static bool ConfirmEntry(char* data, ID id);
386 static bool CancelEntry(char* data, ID id);
387 static bool DeleteEntry(char* data, ID id);
388 static uint64 GetEntryTime(char* data, ID id);
389 static bool GetEntryName(char* data, ID id, char* name, uint32 maxSize);
390 static ID GetEntryID(char* data, const char* name, bool force = false);
391 static uint8 LookupEntryID(char* data, const char* name, ID& id);
392 static uint32 GetCount(char* data);
393 static uint32 GetUsage(char* data);
394 static std::string PrintAllEntries(char* data);
395 static std::string PrintAllEntriesHTML(char* data);
396 static std::string ToXML(char* data);
397 static bool WriteAllEntriesToMsg(char* data, DataMessage* msg);
398 static bool WriteAllIDsToMsg(const char* arrayname, char* data, DataMessage* msg);
399};
400
401} // namespace cmlabs
402
403#include "MemoryMaps.tpl.h"
404
405#endif //_DATAMAPSMEMORY_H_
406
Central shared-memory manager for a Psyclone node: master segment, per-subsystem shared maps and the ...
Template implementations of the GenericMemoryMap shared-map algorithms.
CMSDK time: µs-resolution 64-bit timestamps and the Time Mapping Constant (TMC).
#define MAXKEYNAMELEN
Definition Utils.h:85
#define MAXSCRIPTLEN
Definition Utils.h:91
bool getTypeLevelID(const char *name, uint16 &id)
bool syncContextLevels(DataMessage *msg)
bool waitForRequestReply(uint32 id, uint32 timeout, uint8 &status, uint64 &time, uint64 &msgID, uint64 &msgEOL)
bool confirmContextLevelID(uint16 id)
DataMapsMemory(MasterMemory *master)
bool createNewCrank(uint16 id, uint32 compID, const char *name, const char *function, const char *libraryFilename, const char *language, const char *script, uint64 time, uint16 &existingID)
bool getRequestStatus(uint32 id, uint8 &status, uint64 &time, uint64 &msgID, uint64 &msgEOL)
bool createNewTag(uint32 id, const char *name, uint64 time, uint32 &existingID)
bool getTagName(uint32 id, char *name, uint32 maxSize)
bool create(uint32 typesMaxCount, uint32 contextsMaxCount, uint32 tagsMaxCount, uint32 cranksMaxCount, uint32 requestsMaxCount)
Create the segment (master only) with the given per-map capacities.
bool writeTagsToMsg(DataMessage *msg)
bool getTagID(const char *name, uint32 &id)
bool deleteContextLevel(const char *name)
uint64 getTagCreateTime(uint32 id)
uint64 getTypeLevelCreateTime(uint16 id)
uint8 lookupTagID(const char *name, uint32 &id)
bool deleteRequest(uint32 id)
bool cancelCrankID(uint16 id)
bool getContextLevelName(uint16 id, char *name, uint32 maxSize)
bool maintenance()
Periodic cleanup (e.g.
bool confirmTypeLevelID(uint16 id)
uint8 lookupTypeLevelID(const char *name, uint16 &id)
bool writeContextLevelsToMsg(DataMessage *msg)
bool open()
Attach to the existing data-maps segment.
bool getRequestInfo(uint32 id, uint32 &from, uint32 &to, uint32 &remoteID)
bool createNewTypeLevel(uint16 id, const char *name, uint64 time, uint16 &existingID)
bool getCrankFunction(uint16 id, char *function, uint32 maxSize)
bool writeIDsToMsg(DataMessage *msg)
Write all map names/ids into msg for cluster sync.
bool syncCranks(DataMessage *msg)
bool cancelContextLevelID(uint16 id)
bool getCrankLanguage(uint16 id, char *language, uint32 maxSize)
bool confirmTagID(uint32 id)
bool setRequestStatus(uint32 id, uint8 status)
bool writeTypeLevelsToMsg(DataMessage *msg)
bool syncTags(DataMessage *msg)
bool getContextLevelID(const char *name, uint16 &id)
bool confirmCrankID(uint16 id)
uint32 getCrankCompID(uint16 id)
bool getMemoryUsage(uint64 &alloc, uint64 &usage)
Report allocation/usage.
uint64 getCrankCreateTime(uint16 id)
bool cancelTagID(uint32 id)
bool syncTypeLevels(DataMessage *msg)
bool deleteTag(const char *name)
bool cancelTypeLevelID(uint16 id)
bool createNewContextLevel(uint16 id, const char *name, uint64 time, uint16 &existingID)
uint8 lookupContextLevelID(const char *name, uint16 &id)
bool getCrankID(const char *name, uint16 &id)
bool getCrankName(uint16 id, char *name, uint32 maxSize)
bool getCrankLibraryFilename(uint16 id, char *libraryFilename, uint32 maxSize)
uint64 getContextLevelCreateTime(uint16 id)
bool writeCranksToMsg(DataMessage *msg)
uint8 lookupCrankID(const char *name, uint16 &id)
bool getCrankScript(uint16 id, char *script, uint32 maxSize)
bool createNewRequest(uint32 from, uint32 to, uint32 remoteID, uint32 &id)
bool getTypeLevelName(uint16 id, char *name, uint32 maxSize)
bool deleteTypeLevel(const char *name)
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)
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)
Static algorithms shared by all bitfield-indexed maps in the data-maps segment.
static bool GetEntryName(char *data, ID id, char *name, uint32 maxSize)
Copy the entry name.
static T * CreateEntry(char *data, ID id, const char *name)
Reserve entry id with name (status 1).
static bool ConfirmEntry(char *data, ID id)
Mark entry ready (status 2).
static bool WriteAllEntriesToMsg(char *data, DataMessage *msg)
Serialize all entries into msg.
static ID GetEntryID(char *data, const char *name, bool force=false)
Case-insensitive reverse lookup: find the id registered under name.
static std::string ToXML(char *data)
Serialize all occupied entries as XML by delegating to each entry's toXML(), wrapped in the outer ele...
static T * CreateFirstFreeEntry(char *data, ID &id, const char *name)
Reserve the first free id.
static bool DeleteEntry(char *data, ID id)
Free the entry and its bit.
static uint32 GetCount(char *data)
Number of entries currently registered in the map (from the header's count field).
static uint32 GetUsage(char *data)
Approximate bytes in use by entry payloads (count * sizeof(T)); excludes the header and bitfield.
static uint8 LookupEntryID(char *data, const char *name, ID &id)
Case-insensitive lookup returning the entry's sync status rather than filtering by it.
static std::string PrintAllEntries(char *data)
Human-readable text dump of all occupied entries, one per line ("id = name (time)"; unconfirmed entri...
static T * GetEntry(char *data, ID id)
Fetch the confirmed entry at index id.
static bool WriteAllIDsToMsg(const char *arrayname, char *data, DataMessage *msg)
Write just names into msg under arrayname.
static uint64 GetEntryTime(char *data, ID id)
Creation timestamp of entry id.
static bool CancelEntry(char *data, ID id)
Roll back a reserved entry.
static std::string PrintAllEntriesHTML(char *data)
Dump all occupied entries as HTML table rows (no surrounding table element); used by the node's monit...
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
Context-map entry: one registered psychological/system context name/id.
bool writeToMsg(uint32 n, DataMessage *msg)
uint8 status
char name[MAXKEYNAMELEN+1]
static std::string GetOuterXMLName()
uint64 time
bool writeIDToMsg(const char *arrayname, uint32 n, DataMessage *msg)
uint16 id
std::string toXML()
Crank-map entry: one registered crank (user callback/script) with its function, library and inline sc...
uint32 compID
char name[MAXKEYNAMELEN+1]
char function[MAXKEYNAMELEN+1]
uint64 time
char language[MAXKEYNAMELEN+1]
bool writeToMsg(uint32 n, DataMessage *msg)
std::string toXML()
static std::string GetOuterXMLName()
char script[MAXSCRIPTLEN+1]
uint8 status
bool writeIDToMsg(const char *arrayname, uint32 n, DataMessage *msg)
uint16 id
char libraryFilename[MAXKEYNAMELEN+1]
Root header of the data-maps segment; records capacity/size of each contained map.
uint32 typesBitFieldSize
Bytes in the type map's occupancy bitfield.
uint64 requestsSize
Total bytes of the request map.
uint64 size
Total segment size in bytes.
uint32 contextsBitFieldSize
Bytes in the context map's occupancy bitfield.
uint32 requestsMaxCount
Capacity of the request map (entries).
uint32 cid
Check/magic id validated on attach.
uint64 contextsSize
Total bytes of the context map.
uint32 typesMaxCount
Capacity of the type map (entries).
uint64 typesSize
Total bytes of the type map.
uint32 cranksMaxCount
Capacity of the crank map (entries).
uint32 requestsBitFieldSize
Bytes in the request map's occupancy bitfield.
uint64 createdTime
Creation timestamp (µs).
uint64 tagsSize
Total bytes of the tag map.
uint64 cranksSize
Total bytes of the crank map.
uint32 contextsMaxCount
Capacity of the context map (entries).
uint32 cranksBitFieldSize
Bytes in the crank map's occupancy bitfield.
uint32 tagsMaxCount
Capacity of the tag map (entries).
uint32 tagsBitFieldSize
Bytes in the tag map's occupancy bitfield.
Common header of every map inside the data-maps segment.
Request-map entry tracking one cross-component request/reply transaction.
uint8 status
REQ_* lifecycle status.
uint32 remoteID
Id of the request at the origin node, if forwarded cross-node.
uint64 time
Creation timestamp (µs).
char name[MAXKEYNAMELEN+1]
Internal use only, for named events.
uint32 id
Request id; corresponds to the index in the map.
uint64 dataMessageEOL
End-of-life of the reply message; after this the payload is gone (REQ_*_DATA_EOL).
uint64 dataMessageID
TemporalMemory id of the reply DataMessage (0 = none yet).
std::string toXML()
uint32 to
Id of the component processing the request.
static std::string GetOuterXMLName()
uint32 from
Id of the component making the request.
uint64 lastUpdate
Timestamp of the last status change (µs); used to expire stale requests.
Tag-map entry: one registered tag name/id.
uint32 id
bool writeIDToMsg(const char *arrayname, uint32 n, DataMessage *msg)
uint64 time
static std::string GetOuterXMLName()
char name[MAXKEYNAMELEN+1]
bool writeToMsg(uint32 n, DataMessage *msg)
std::string toXML()
uint8 status
Type-map entry: one registered message type name/id.
bool writeToMsg(uint32 n, DataMessage *msg)
std::string toXML()
bool writeIDToMsg(const char *arrayname, uint32 n, DataMessage *msg)
uint64 time
uint16 id
char name[MAXKEYNAMELEN+1]
static std::string GetOuterXMLName()
uint8 status