CMSDK  2.0.1
ComponentMemory.h
1 #if !defined(_COMPONENTMEMORY_H_)
2 #define _COMPONENTMEMORY_H_
3 
4 #include "MemoryManager.h"
5 #include "PsyTime.h"
6 #include "Subscriptions.h"
7 
8 namespace cmlabs {
9 
10 
11 #define COMPSTATUS_IDLE 1
12 #define COMPSTATUS_STARTING 2
13 #define COMPSTATUS_RUNNING 3
14 #define COMPSTATUS_FINISHED 4
15 
16 #define NOTINUSE 65535
17 
18 #define GETCOMPOFFSETP(header,id) (uint64*)(((char*)header) + sizeof(ComponentMemoryStruct) + (id*sizeof(uint64)))
19 #define GETCOMPOFFSET(header,id) *GETCOMPOFFSETP(header,id)
20 #define GETCOMPDATA(header,id) (ComponentInfoStruct*)(GETCOMPOFFSET(header,id) ? ((char*)header) + (header->indexSize * sizeof(uint64)) + GETCOMPOFFSET(header,id) : NULL)
21 
23  uint64 size;
24  uint32 cid;
25  uint64 createdTime; // time of creation
26  uint32 indexSize;
27  uint32 count;
28  uint32 maxID;
29  uint64 usage;
30  // index * N, each a uint64
31 };
32 
33 #define PARAM_STRING 1
34 #define PARAM_INTEGER 2
35 #define PARAM_FLOAT 3
36 #define PARAM_STRING_COLL 4
37 #define PARAM_INTEGER_COLL 5
38 #define PARAM_FLOAT_COLL 6
39 #define PARAM_TYPE_COLL 7
40 
41 #define COMPONENT_CAN_MIGRATE 1
42 
43 struct ComponentStats {
44  uint64 time;
45  uint64 msgInCount;
46  uint64 msgInBytes;
47  char recentInMsg[10*DRAFTMSGSIZE];
48  uint64 msgOutCount;
49  uint64 msgOutBytes;
50  char recentOutMsg[10*DRAFTMSGSIZE];
51  uint64 firstRunStartTime;
52  uint64 currentRunStartTime;
53  uint64 cycleCount;
54  uint64 runCount;
55  uint32 migrationCount;
56  uint64 usageCPUTicks;
57  std::string toXML() {
58  std::string xmlIn, xmlOut;
59  int n;
60  const char* oldData = NULL;
61  DataMessage* msg = new DataMessage();
62  char* msgData = recentInMsg;
63  for (n = 0; n < 10; n++) {
64  if (((DataMessageHeader*)msgData)->time) {
65  if (!oldData) oldData = msg->swapMessageData(msgData);
66  else msg->swapMessageData(msgData);
67  xmlIn += msg->toXML();
68  }
69  msgData += DRAFTMSGSIZE;
70  }
71  msgData = recentOutMsg;
72  for (n = 0; n < 10; n++) {
73  if (((DataMessageHeader*)msgData)->time) {
74  if (!oldData) oldData = msg->swapMessageData(msgData);
75  else msg->swapMessageData(msgData);
76  xmlOut += msg->toXML();
77  }
78  msgData += DRAFTMSGSIZE;
79  }
80  if (oldData)
81  msg->swapMessageData(oldData);
82  delete(msg);
83  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",
84  runCount, msgInCount, msgInBytes, msgOutCount, msgOutBytes, time, xmlIn.c_str(), xmlOut.c_str());
85  }
86 };
87 
88 #define SELFTRIGGER_NONE 0
89 #define SELFTRIGGER_WARN 1
90 #define SELFTRIGGER_ALLOW 2
91 
92 
94  uint64 size;
95  uint32 id;
96  uint8 syncStatus;
97  uint8 selfTrigger;
98  uint8 type; // Component type (Whiteboard, Catalog, Module, etc.)
99  uint8 status; // Component status (idle, running, etc.)
100  uint8 policy; // Migration policy
101  uint16 nodeID; // id of process component is running in currently
102  uint16 procID; // id of process component is running in currently
103  // Basic data
104  uint64 createdTime;
105  uint64 migratedTime;
106  uint64 lastUpdateTime;
107  char name[MAXKEYNAMELEN]; // name in text
108  // Statistics
109  ComponentStats stats;
110  AveragePerfStats perfStats;
111  // Parameters
112  uint16 paramCount;
113  uint32 paramSize;
114  // Private Data
115  uint16 dataCount;
116  uint64 dataSize;
117 
118  std::string toXML(bool printStats = true) {
119  if (printStats)
120  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",
121  id, syncStatus, name, nodeID, procID, type, policy, paramCount, paramSize, dataCount, dataSize, createdTime, migratedTime, lastUpdateTime, selfTrigger, stats.toXML().c_str(), perfStats.toXML().c_str());
122  else
123  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",
124  id, syncStatus, name, nodeID, procID, type, policy, paramCount, paramSize, dataCount, dataSize, createdTime, migratedTime, lastUpdateTime, selfTrigger);
125  }
126  bool writeToMsg(uint32 n, DataMessage* msg) {
127  if (!msg) return false;
128  msg->setInt((int64)n, "ID", id);
129  msg->setInt((int64)n, "Status", status);
130  msg->setInt((int64)n, "Type", type);
131  msg->setInt((int64)n, "Policy", policy);
132  msg->setInt((int64)n, "SelfTrigger", selfTrigger);
133  msg->setInt((int64)n, "Size", size);
134  msg->setInt((int64)n, "NodeID", nodeID);
135  msg->setInt((int64)n, "ProcID", procID);
136  msg->setString((int64)n, "Name", name);
137  msg->setTime((int64)n, "CreatedTime", createdTime);
138  return true;
139  }
140 };
141 
142 #define COMPINFOUSAGE(info) (sizeof(ComponentInfoStruct) + info->paramSize + info->dataSize)
143 
145  uint64 size;
146  // name, 0, mimetype, 0, data
147 };
148 
149 struct ParamHeader {
150  uint32 size;
151  uint8 type;
152  // name, 0, struct
153 };
154 
155 // -- integer --
157  int64 val;
158  int64 defaultVal;
159  int64 minVal;
160  int64 maxVal;
161  int64 interval;
162 };
163 
164 // -- float --
166  float64 val;
167  float64 defaultVal;
168  float64 minVal;
169  float64 maxVal;
170  float64 interval;
171 };
172 
173 // -- integer collection --
175  uint32 index;
176  uint32 defaultIndex;
177  uint32 count;
178 };
179 
180 #define CHECKCOMPONENTMEMORYSERIAL if (serial != master->getComponentShmemSerial()) {if (!open()) {mutex->leave();return 0;}}
181 #define CHECKCOMPONENTMEMORYSERIALRETURN(a) if (serial != master->getComponentShmemSerial()) {if (!open()) {mutex->leave();return a;}}
182 
183 class MasterMemory;
185 public:
186  static bool UnitTest();
187 
188  ComponentMemory(MasterMemory* master);
189  ~ComponentMemory();
190 
191  bool getMemoryUsage(uint64& alloc, uint64& usage);
192 
193  bool open();
194  bool create(uint32 indexSize);
195 
196  std::vector<ComponentInfoStruct>* getAllComponents();
197  std::vector<ComponentInfoStruct>* getAllLocalComponents();
198  std::vector<ComponentInfoStruct>* getAllLocalComponents(uint8 type);
199  std::vector<ComponentInfoStruct>* getAllModules();
200  std::vector<ComponentInfoStruct>* getAllCatalogs();
201 
202  bool writeComponentsToMsg(DataMessage* msg);
203  bool writeComponentNamesToMsg(DataMessage* msg);
204  bool syncComponents(DataMessage* msg);
205 
206  bool confirmComponentID(uint32 id);
207  bool cancelComponentID(uint32 id);
208  uint8 lookupComponentID(const char* name, uint32 &id); // status = 0: not there, 1: in-sync, 2: ready
209  bool createComponent(uint32 id, uint8 type, uint8 policy, uint8 selfTrigger, const char* name, uint64 size, uint16 nodeID, uint16 procID, uint64 time, uint32& existingID);
210 // bool recordRemoteComponent(uint32 id, const char* name, uint16 nodeID, uint64 time);
211 // bool reserveComponentID(uint32 id, const char* name);
212  bool destroyComponent(uint32 cid);
213  bool getComponentName(uint32 cid, char* name, uint32 maxNameLen);
214  std::string getComponentNameString(uint32 cid);
215  uint8 getComponentType(uint32 cid);
216  bool getComponentID(const char* name, uint32 &cid);
217  ComponentStats getComponentStats(uint32 cid);
218  bool setComponentStats(uint32 cid, ComponentStats& stats);
219  AveragePerfStats getComponentPerfStats(uint32 cid);
220  bool setComponentPerfStats(uint32 cid, AveragePerfStats &perfStruct);
221 
222  bool isComponentLocal(uint32 cid);
223  bool isComponentLocal(const char* name);
224  bool getComponentLocation(uint32 cid, uint16& nodeID, uint16& procID);
225  uint8 getComponentPolicy(uint32 cid);
226  bool updateComponentInformation(uint32 cid, uint8 type, uint8 policy, uint8 selfTrigger, uint16 nodeID, uint16 procID);
227  bool setComponentPolicy(uint32 cid, uint8 policy);
228  uint8 getComponentSelfTrigger(uint32 cid);
229  bool setComponentSelfTrigger(uint32 cid, uint8 selfTrigger);
230  uint16 getComponentNodeID(uint32 cid);
231  uint16 getComponentProcessID(uint32 cid);
232  bool updateComponentLocation(uint32 cid, uint16 nodeID, uint16 procID);
233  // char* GetAndRemoveComponent(uint32 cid, uint32& dataSize);
234  // bool addExistingComponent(uint32 cid, uint16 nodeID, uint16 procID, char* oldData, uint32 dataSize);
235  // bool updateRemoteComponent(uint32 cid, uint16 nodeID, uint16 procID, const char* name);
236  bool addComponentStats(uint32 cid, uint8 status, uint64 usageCPUTicks,
237  DataMessage* inputMsg, DataMessage* outputMsg, uint32 runCount, uint32 cycleCount);
238 
239  bool canComponentMigrate(uint32 cid);
240  std::list<uint16>* findProcessComponents(uint16 procID);
241 
242  char* getComponentData(uint32 cid, uint64& size);
243  bool setComponentData(uint32 cid, const char* data, uint64 size, bool wasMigrated);
244 
245  // Private Data
246  bool setPrivateData(uint32 cid, const char* name, const char* data, uint64 size, const char* mimetype = NULL);
247  uint64 getPrivateDataSize(uint32 cid, const char* name);
248  std::string getPrivateDataMimetype(uint32 cid, const char* name);
249  bool getPrivateData(uint32 cid, const char* name, char* data, uint64 maxSize);
250  char* getPrivateDataCopy(uint32 cid, const char* name, uint64 &size);
251  bool deletePrivateData(uint32 cid, const char* name);
252  std::map<std::string, std::string> getPrivateDataKeysAndTypes(uint32 cid);
253  std::list<std::string> getPrivateDataKeys(uint32 cid);
254 
255  // Parameters
256  bool createParameter(uint32 cid, const char* name, const char* val, const char* defaultValue = NULL);
257  bool createParameter(uint32 cid, const char* name, const char* val, uint32 count, uint32 defaultIndex);
258  bool createParameter(uint32 cid, const char* name, std::vector<std::string> values, const char* defaultValue = NULL);
259  bool createParameter(uint32 cid, const char* name, int64* val, uint32 count, uint32 defaultIndex);
260  bool createParameter(uint32 cid, const char* name, std::vector<std::string> values, int64 defaultValue);
261  bool createParameter(uint32 cid, const char* name, std::vector<int64> values, int64 defaultValue = 0);
262  bool createParameter(uint32 cid, const char* name, float64* val, uint32 count, uint32 defaultIndex);
263  bool createParameter(uint32 cid, const char* name, std::vector<std::string> values, float64 defaultValue);
264  bool createParameter(uint32 cid, const char* name, std::vector<float64> values, float64 defaultValue = 0);
265  bool createParameter(uint32 cid, const char* name, int64 val, int64 min = 0, int64 max = 0, int64 interval = 0);
266  bool createParameter(uint32 cid, const char* name, float64 val, float64 min = 0, float64 max = 0, float64 interval = 0);
267 
268  bool hasParameter(uint32 cid, const char* name);
269  bool deleteParameter(uint32 cid, const char* name);
270 
271  uint8 getParameterDataType(uint32 cid, const char* name);
272  uint32 getParameterValueSize(uint32 cid, const char* name);
273  bool getParameter(uint32 cid, const char* name, char* val, uint32 maxSize);
274  bool getParameter(uint32 cid, const char* name, int64& val);
275  bool getParameter(uint32 cid, const char* name, float64& val);
276  std::string getParameterString(uint32 cid, const char* name, uint32 maxSize);
277  std::string getParameterString(uint32 cid, const char* name);
278  int64 getParameterInt(uint32 cid, const char* name);
279  float64 getParameterFloat(uint32 cid, const char* name);
280  bool getParameterAsBool(uint32 cid, const char* name);
281 
282  bool setParameter(uint32 cid, const char* name, const char* val);
283  bool setParameter(uint32 cid, const char* name, int64 val);
284  bool setParameter(uint32 cid, const char* name, float64 val);
285 
286  bool resetParameter(uint32 cid, const char* name);
287  bool tweakParameter(uint32 cid, const char* name, int32 tweak);
288 
289  bool addLocalPerformanceStats(std::list<PerfStats> &perfStats);
290  std::string printFriendly();
291  std::string toXML(bool stats = true);
292 
293 private:
294  bool resize(uint64 newMemorySize);
295  bool resizeIndex(uint32 newIndexSize);
296  bool resizeComponent(uint32 id, uint64 newSize);
297  bool growComponent(uint32 id, uint64 addSize);
298  bool getSurroundingComponents(uint32 cid, uint32& prevID, uint32& nextID);
299 
300  char* makeRoomForPrivateData(uint32 cid, uint64 newSize);
301  char* makeRoomForParameter(uint32 cid, uint64 newSize);
302 
303  ParamHeader* getParameter(uint32 cid, const char* name);
304  PrivateHeader* getPrivateData(uint32 cid, const char* name);
305 
306  utils::Mutex* mutex;
307  MasterMemory* master;
308  ComponentMemoryStruct* header;
309  uint64 memorySize;
310  uint16 port;
311  uint32 serial;
312 };
313 
314 } // namespace cmlabs
315 
316 #endif //_COMPONENTMEMORY_H_
317 
Definition: MemoryManager.h:169
Definition: Bitmap.h:7
bool setTime(const char *key, uint64 value)
Definition: DataMessage.cpp:1782
Definition: ComponentMemory.h:165
Definition: ComponentMemory.h:174
Definition: ComponentMemory.h:149
Definition: ComponentMemory.h:22
Definition: DataMessage.h:54
Definition: ComponentMemory.h:156
bool setString(const char *key, const char *value)
Definition: DataMessage.cpp:1776
Definition: ComponentMemory.h:184
Definition: ComponentData.h:34
Definition: Utils.h:276
std::string toXML(std::map< uint16, std::string > *subtypes=NULL, std::map< uint16, std::string > *subcontexts=NULL, std::map< uint32, std::string > *compNames=NULL)
Definition: DataMessage.cpp:775
bool setInt(const char *key, int64 value)
Definition: DataMessage.cpp:1786
Definition: MemoryManager.h:76
Definition: ComponentMemory.h:144
const char * swapMessageData(const char *data)
Definition: DataMessage.cpp:1399
Definition: DataMessage.h:95
Definition: ComponentMemory.h:93