CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
Loading...
Searching...
No Matches
ProcessMemory.h
Go to the documentation of this file.
1
14
15#if !defined(_PROCESSMEMORY_H_)
16#define _PROCESSMEMORY_H_
17
18#include "MemoryManager.h"
19#include "PsyTime.h"
20#include "HTML.h"
21
22namespace cmlabs {
23
26#define SIGNALQ_ID 0
27#define MSGQ_ID 1
28#define REQQ_ID 2
29#define CMDQ_ID 3
31
32#define MAXPROC 64
33#define MAXPROC4 256
34
37#define PSYPROC_ERROR 0
38#define PSYPROC_CREATED 1
39#define PSYPROC_INIT 2
40#define PSYPROC_IDLE 3
41#define PSYPROC_READY 4
42#define PSYPROC_ACTIVE 5
43#define PSYPROC_SHUTTING_DOWN 6
44#define PSYPROC_TERMINATED 7
46
49#define CMDQ_TYPE 1
50#define MSGQ_TYPE 2
51#define SIGQ_TYPE 3
52#define REQQ_TYPE 4
54
57
64 uint64 time;
65 uint64 msgInCount;
66 uint64 msgInBytes;
68 uint64 msgOutCount;
69 uint64 msgOutBytes;
71 uint64 procMemUsage;
73 uint32 recentInPos; // circular write index into recentInMsg ring (newest written here, then advanced)
74 uint32 recentOutPos; // circular write index into recentOutMsg ring
75
77 std::string toXML() {
78 std::string xmlIn, xmlOut;
79 int n;
80 const char* oldData = NULL;
81 DataMessage* msg = new DataMessage();
82 char* msgData = recentInMsg;
83 for (n = 0; n < 10; n++) {
84 if (((DataMessageHeader*)msgData)->time) {
85 if (!oldData) oldData = msg->swapMessageData(msgData);
86 else msg->swapMessageData(msgData);
87 xmlIn += msg->toXML();
88 }
89 msgData += DRAFTMSGSIZE;
90 }
91 msgData = recentOutMsg;
92 for (n = 0; n < 10; n++) {
93 if (((DataMessageHeader*)msgData)->time) {
94 if (!oldData) oldData = msg->swapMessageData(msgData);
95 else msg->swapMessageData(msgData);
96 xmlOut += msg->toXML();
97 }
98 msgData += DRAFTMSGSIZE;
99 }
100 if (oldData)
101 msg->swapMessageData(oldData);
102 delete(msg);
103 return utils::StringFormat("<processstats msgincount=\"%llu\" msginbytes=\"%llu\" msgoutcount=\"%llu\" msgoutbytes=\"%llu\" time=\"%llu\" memusage=\"%llu\">\n<inmsg>\n%s</inmsg>\n<outmsg>\n%s</outmsg>\n</processstats>\n",
104 msgInCount, msgInBytes, msgOutCount, msgOutBytes, time, procMemUsage, xmlIn.c_str(), xmlOut.c_str());
105 }
106};
107
114 uint32 id;
115 uint16 nodeID;
116 uint64 createdTime;
117 uint64 lastseen;
118 uint8 status;
119 uint32 osID;
120 uint8 type;
121// stats(activity)
126 uint16 cmdQID;
127 uint16 msgQID;
128 uint16 sigQID;
129 uint16 reqQID;
130
132 std::string toXML() {
133 return utils::StringFormat("<space id=\"%u\" name=\"%s\" node=\"%u\" status=\"%u\" osid=\"%u\" commandline=\"%s\" created=\"%llu\" lastseen=\"%llu\">\n%s%s</space>",
134 id, name, nodeID, status, osID, html::EncodeHTML(commandline).c_str(), createdTime, lastseen, stats.toXML().c_str(), perfStats.toXML().c_str());
135 }
136};
137
143 uint64 size;
144 uint32 cid;
145 uint64 createdTime; // time of creation
146 uint32 count;
147 uint64 usage;
149 uint64 qIndex[MAXPROC*4];
150};
151
153#define INITIALQSIZE sizeof(MessageQueueHeader) + 2048
158struct MessageQueueHeader {
159 uint64 size;
160 uint32 id;
161 uint32 count;
162 uint64 startPos;
163 uint64 endPos;
164 uint64 padding;
166};
167
170#define CHECKPROCESSMEMORYSERIAL if (serial != master->getProcessShmemSerial()) {if (!open()) {mutex->leave();return 0;}}
171
172class MasterMemory;
188public:
189 static bool UnitTest();
190 static bool PerfTest();
197 static bool FuzzTest();
211 static bool ConcFuzzTest();
214 MessageQueueHeader* fuzzPeekQHeader(uint16 procID, uint8 qType) { return getQHeader(procID, qType); }
215
219
221 bool getMemoryUsage(uint64& alloc, uint64& usage);
223 std::vector<ProcessInfoStruct>* getAllProcesses();
227 bool checkProcessHeartbeats(uint32 timeoutMS, std::list<ProcessInfoStruct>& procIssues);
228
229 bool open();
231 bool create(uint32 initialProcCount);
232
233 // Processes
235 bool createNewProcess(const char* name, uint16 &id);
237 bool deleteProcess(uint16 id);
238
239 // Process Info
240 bool getProcessName(uint16 id, char* name, uint32 maxSize);
241 bool getProcessID(const char* name, uint16 &id);
242 uint64 getProcessCreateTime(uint16 id);
243
244 uint8 getProcessStatus(uint16 id, uint64& lastseen);
245 uint8 getProcessStatus(uint16 id, uint64& lastseen, uint64& createTime);
246 bool getProcessCommandLine(uint16 id, char* cmdline, uint32 maxSize);
247 uint16 getProcessIDFromOSID(uint32 osid);
248 uint32 getProcessOSID(uint16 id);
249
251 bool setProcessStatus(uint16 id, uint8 status, uint64 currentCPUTicks = 0);
252 bool setProcessCommandLine(uint16 id, const char* cmdline);
253 bool setProcessOSID(uint16 id, uint32 osid);
254 bool setProcessType(uint16 id, uint8 type);
255
257 bool setProcessPerfStats(uint16 procID, AveragePerfStats &perfStruct);
258
260 bool addToProcessStats(uint16 id, DataMessage* inputMsg, DataMessage* outputMsg);
261
262 // Process Queues
267 bool addToCmdQ(uint16 procID, DataMessage* msg);
268 bool addToMsgQ(uint16 procID, DataMessage* msg);
269 bool addToSigQ(uint16 procID, DataMessage* msg);
270 bool addToReqQ(uint16 procID, DataMessage* msg);
271 bool addToAllSignalQs(DataMessage* msg);
272 bool addToAllSignalQsExcept(DataMessage* msg, uint16 except);
274
279 DataMessage* waitForCmdQ(uint16 procID, uint32 timeout);
280 DataMessage* waitForMsgQ(uint16 procID, uint32 timeout);
281 DataMessage* waitForSigQ(uint16 procID, uint32 timeout);
282 DataMessage* waitForReqQ(uint16 procID, uint32 timeout);
284
285 uint32 getCmdQCount(uint16 procID);
286 uint32 getMsgQCount(uint16 procID);
287 uint32 getSigQCount(uint16 procID);
288 uint32 getReqQCount(uint16 procID);
289
291 bool getQueueSizes(uint16 procID, uint64 &bytes, uint32 &count);
293 bool addLocalPerformanceStats(std::list<PerfStats> &perfStats);
294
295private:
296 bool resize(uint64 newMemorySize);
297 bool setupNextAvailableQ(uint16& qID);
298 bool deleteQ(uint16 id);
299 bool resizeQ(uint16 id, uint64 newSize);
300
301 bool printQ(MessageQueueHeader* qHeader);
302
303 MessageQueueHeader* getQHeader(uint16 procID, uint8 qType);
304
305 bool addToQ(uint16 procID, uint8 qType, DataMessage* msg);
306 DataMessage* waitForQ(uint16 procID, uint8 qType, uint32 timeout);
307
308 utils::Mutex* mutex;
309 MasterMemory* master;
310 ProcessMemoryStruct* header;
311 uint64 memorySize;
312 uint16 port;
313 uint32 serial;
314
315 utils::Semaphore* qSemaphores[MAXPROC4];
316};
317
318} // namespace cmlabs
319
320#endif //_PROCESSMEMORY_H_
321
HTML/URL helper utilities: entity encoding/decoding, MIME type lookup and URL component parsing.
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...
#define MAXPROC4
Maximum queues per node (MAXPROC x 4 queue types).
#define MAXPROC
Maximum processes (spaces) per node.
CMSDK time: µs-resolution 64-bit timestamps and the Time Mapping Constant (TMC).
#define MAXKEYNAMELEN
Definition Utils.h:85
#define THREAD_RET
Definition Utils.h:127
#define THREAD_FUNCTION_CALL
Definition Utils.h:129
#define MAXCOMMANDLINELEN
Definition Utils.h:88
#define THREAD_ARG
Definition Utils.h:130
The central Psyclone data container: a self-contained binary message with typed, named user entries.
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()
const char * swapMessageData(const char *data)
swapMessageData(const char* data)
Handle to the node's master shared-memory segment (MemoryMasterStruct).
bool setProcessCommandLine(uint16 id, const char *cmdline)
Record the launch command line.
DataMessage * waitForCmdQ(uint16 procID, uint32 timeout)
Wait on the command queue.
static bool UnitTest()
Self-test.
bool addToCmdQ(uint16 procID, DataMessage *msg)
Enqueue on the command queue.
bool getProcessName(uint16 id, char *name, uint32 maxSize)
Copy the process name into name (max maxSize bytes).
uint32 getProcessOSID(uint16 id)
bool createNewProcess(const char *name, uint16 &id)
Register a new process (space) in the table.
uint32 getCmdQCount(uint16 procID)
bool addToReqQ(uint16 procID, DataMessage *msg)
Enqueue on the request queue.
MessageQueueHeader * fuzzPeekQHeader(uint16 procID, uint8 qType)
Diagnostics-only: read-only access to a live queue header for the Phase-2 fuzz invariant checker (Pro...
bool addToMsgQ(uint16 procID, DataMessage *msg)
Enqueue on the data-message queue.
ProcessMemory(MasterMemory *master)
static bool PerfTest()
Queue throughput benchmark.
static bool FuzzTest()
Seeded, oracle-checked fuzz of the circular queue arithmetic (single process).
bool addLocalPerformanceStats(std::list< PerfStats > &perfStats)
Append PerfStats snapshots for all local processes to perfStats.
uint32 getSigQCount(uint16 procID)
AveragePerfStats getProcessPerfStats(uint16 procID)
bool addToAllSignalQs(DataMessage *msg)
Broadcast to every process's signal queue.
bool addToSigQ(uint16 procID, DataMessage *msg)
Enqueue on the signal queue.
bool addToAllSignalQsExcept(DataMessage *msg, uint16 except)
Broadcast to all signal queues except process except.
uint64 getProcessCreateTime(uint16 id)
uint32 getMsgQCount(uint16 procID)
DataMessage * waitForMsgQ(uint16 procID, uint32 timeout)
Wait on the data-message queue.
bool setProcessOSID(uint16 id, uint32 osid)
Record the OS pid.
bool setProcessStatus(uint16 id, uint8 status, uint64 currentCPUTicks=0)
Update status + heartbeat (and optionally CPU ticks) for process id.
bool getQueueSizes(uint16 procID, uint64 &bytes, uint32 &count)
Total queued bytes/messages across all four queues of procID.
uint8 getProcessStatus(uint16 id, uint64 &lastseen)
bool getMemoryUsage(uint64 &alloc, uint64 &usage)
Report allocation/usage of the process segment.
bool deleteProcess(uint16 id)
Remove a process entry and free its queues.
bool checkProcessHeartbeats(uint32 timeoutMS, std::list< ProcessInfoStruct > &procIssues)
Find processes whose heartbeat is older than timeoutMS.
bool getProcessID(const char *name, uint16 &id)
Look up a process id by name.
bool open()
Attach to the existing process segment.
bool addToProcessStats(uint16 id, DataMessage *inputMsg, DataMessage *outputMsg)
Add message traffic to the process's stats/rings.
DataMessage * waitForReqQ(uint16 procID, uint32 timeout)
Wait on the request queue.
uint32 getReqQCount(uint16 procID)
bool setProcessType(uint16 id, uint8 type)
Set type (0 = normal, 1 = inside node).
static bool ConcFuzzTest()
Phase 2: CONCURRENT + cross-process fuzz of the ring (ProcessMemoryConcFuzz.cpp).
uint16 getProcessIDFromOSID(uint32 osid)
Map an OS pid to a Psyclone process id.
bool getProcessCommandLine(uint16 id, char *cmdline, uint32 maxSize)
Copy the launch command line.
std::vector< ProcessInfoStruct > * getAllProcesses()
Snapshot all process records.
DataMessage * waitForSigQ(uint16 procID, uint32 timeout)
Wait on the signal queue.
bool setProcessPerfStats(uint16 procID, AveragePerfStats &perfStruct)
Store new performance averages.
bool create(uint32 initialProcCount)
Create the process segment (master only).
Recursive mutual-exclusion lock, optionally named for cross-process use.
Definition Utils.h:463
Counting semaphore, optionally named for cross-process use.
Definition Utils.h:519
std::string EncodeHTML(std::string str)
Encode a plain string for safe embedding in HTML (e.g.
Definition HTML.cpp:51
std::string StringFormat(const char *format,...)
printf into a std::string.
Definition Utils.cpp:8067
THREAD_RET THREAD_FUNCTION_CALL ProcessMemoryPerfTest(THREAD_ARG arg)
static THREAD_RET THREAD_FUNCTION_CALL QueueTest(THREAD_ARG arg)
Sliding-window averages derived from successive PerfStats snapshots.
The current (version 10) DataMessage wire/shared-memory header.
On-segment header of a circular message queue (legacy 32-bit layout).
uint32 padding
Alignment/reserved.
uint32 size
Total size of the queue region in bytes (header + buffer).
uint32 count
Number of messages currently in the queue.
uint32 startPos
Read offset into the circular buffer.
char name[MAXKEYNAMELEN+1]
Queue name, used to derive the names of its cross-process mutex/semaphore.
uint32 id
Queue index within qIndex.
uint32 endPos
Write offset into the circular buffer.
One process (space) record in the shared process table.
uint16 cmdQID
Queue index of this process's command queue.
char commandline[MAXCOMMANDLINELEN+1]
Command line used to launch the process.
uint16 sigQID
Queue index of this process's signal queue.
uint64 createdTime
Creation timestamp (µs).
uint16 msgQID
Queue index of this process's data-message queue.
uint16 reqQID
Queue index of this process's request queue.
uint32 osID
OS process id (pid).
std::string toXML()
Serialize this record (including stats) as a <space> XML element.
char name[MAXKEYNAMELEN+1]
Process (space) name.
uint32 id
Process id; corresponds to the index in the table.
AveragePerfStats perfStats
Windowed performance averages.
uint64 lastseen
Last heartbeat/status update (µs); staleness implies a crashed space.
ProcessStats stats
Throughput counters and recent-message rings.
uint8 status
PSYPROC_* lifecycle status.
uint16 nodeID
Id of the node this process belongs to.
uint8 type
0: normal space process, 1: runs inside the node process.
Root header of the process-memory segment: fixed process table plus queue index.
ProcessInfoStruct processes[MAXPROC]
Fixed-size process table.
uint64 qIndex[MAXPROC *4]
Byte offsets of each process's four queues (0 = none).
uint64 usage
Bytes currently in use in the segment.
uint32 cid
Check/magic id validated on attach.
uint64 size
Total segment size in bytes.
uint32 count
Number of process entries in use.
Per-process throughput counters and recent-message rings, kept in shared memory.
uint64 msgOutBytes
Total bytes sent.
uint64 msgInBytes
Total bytes received.
char recentOutMsg[10 *DRAFTMSGSIZE]
Ring of the 10 most recent outbound message drafts.
uint64 time
Timestamp of the last update (µs).
uint64 msgOutCount
Total messages sent.
uint64 msgInCount
Total messages received.
char recentInMsg[10 *DRAFTMSGSIZE]
Ring of the 10 most recent inbound message drafts.
std::string toXML()
Serialize counters and recent messages as a <processstats> XML element.
uint64 currentCPUTicks
Cumulative CPU ticks consumed.
uint64 procMemUsage
Current process memory footprint in bytes.