CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
Loading...
Searching...
No Matches
TemporalMemory.h
Go to the documentation of this file.
1
17
18#if !defined(_TEMPORALMEMORY_H_)
19#define _TEMPORALMEMORY_H_
20
21#include "MemoryManager.h"
22#include "PsyTime.h"
23
24namespace cmlabs {
25
29 uint16 serial;
30 uint64 eol;
31 uint32 bin;
32 uint64 offset;
33 uint64 size;
34 uint64 usage;
35 uint32 startBlock;
36 uint32 blockUsage;
37 //uint32 blockSize;
38 };
39
44 uint64 size;
45 uint32 cid;
46 uint32 slotCount;
47 uint32 slotsInUse;
48 uint64 usage;
49 uint64 growStep;
50 uint64 maxSize;
51 uint64 createdTime; // time of creation
54 uint16 binCount;
55 uint32 bitFieldSize; // number of bytes in the bitfield
56 // Bitfield(bitFieldSize)
57 // slotCount * SlotEntryStruct
58 // bin0(size), bin1(size), bin2(size)...
59 };
60
65 uint64 size;
66 uint32 blockCount;
67 uint32 blockSize;
68 uint32 blocksInUse;
69 uint64 usage;
70 uint32 bitFieldSize; // number of bytes in the bitfield
71 // Bitfield(bitFieldSize)
72 // blockCount * blockSize data
73 };
74
75 //struct BinSpaceFit {
76 // uint32 bin;
77 // uint32 blocksNeeded;
78 // uint32 spaceWaisted;
79 //};
80 //bool BinSpaceFitSort(const BinSpaceFit& a, const BinSpaceFit& b) {
81 // return (a.blocksNeeded < b.blocksNeeded);
82 //}
83
87#define GETMSGNODE(id) (uint16)((id & 0xffff000000000000L)>>48)
88#define GETMSGSERIAL(id) (uint16)((id & 0x0000ffff00000000L)>>32)
89#define GETMSGSLOT(id) (uint32)(id & 0x00000000ffffffffL)
90#define SETMSGID(id,node,serial,slot) id = (((uint64)node) << 48) | (((uint64)serial) << 32) | ((uint64)slot)
92
95#define CHECKTEMPORALMEMORYSERIAL if (serial != master->getDynamicShmemSerial()) {if (!open()) {mutex->leave();return 0;}}
96
97 class MasterMemory;
111 public:
115
118 bool getMemoryUsage(uint64& alloc, uint64& usage);
119
121 bool open();
128 bool create(uint32 slotCount, uint16 binCount, uint32 minBlockSize, uint32 maxBlockSize, uint64 initSize, uint64 maxSize, uint32 growSteps = 8);
130 bool setNodeID(uint16 id);
131
132 // Insert new block of memory and return full id
137 bool insertMessage(DataMessage* msg, uint64& id);
138 // Get copy of block of memory
142 DataMessage* getCopyOfMessage(uint64 id);
143
145 bool logUsage();
146
149 bool maintenance();
150
152 static bool UnitTest();
153
154 private:
155
156 std::vector<BinHeaderStruct> calcBinHeaders(uint32 binCount, uint32 minBlockSize, uint32 maxBlockSize, uint64 binDataSize);
157 BinHeaderStruct* getBestBin(uint32 size, uint32& bin, uint32 &blocksNeeded);
158 char* getMemoryBlock(uint64 id);
159 bool checkSlots(uint64 now);
160
161 bool resizeSlots();
162 bool resizeBin(uint32 bin, BinHeaderStruct* binHeader, uint32 msgSize);
163 std::vector<BinHeaderStruct> calcBinHeadersGrowth(uint32 bin = 0, uint32 msgSize = 0);
164 bool resizeMemory(uint32 slotGrowth, std::vector<BinHeaderStruct>& binHeaderSizes);
165
166 utils::Mutex* mutex;
167 MemoryController* master;
168 TemporalMemoryStruct* header;
169 SlotEntryStruct* slotEntries;
170 uint64 memorySize;
171 uint16 port;
172 uint16 node;
173 uint32 serial;
174 };
175
176
177//
178//
179//struct TemporalMemoryStruct {
180// uint64 size;
181// uint32 cid;
182// uint64 usage;
183// uint64 growStep;
184// uint64 maxSize;
185// uint64 createdTime; // time of creation
186// uint16 slotCount;
187// uint64 slotSize;
188// uint32 slotDuration;
189// uint16 startSlot;
190// uint64 startTime;
191// // slots * SlotHeaderStruct
192//};
193//
194//struct SlotHeaderStruct {
195// uint64 size;
196// uint32 count;
197// uint64 usage;
198// uint64 eol;
199// uint64 offset;
200//};
201//
202//#define GETMSGNODE(id) (uint16)((id & 0xffff000000000000L)>>48)
203//#define GETMSGSLOT(id) (uint16)((id & 0x0000ffff00000000L)>>32)
204//#define GETMSGOFFSET(id) (uint64)(id & 0x00000000ffffffffL)
205//#define SETMSGID(id,node,slot,offset) id = (((uint64)node) << 48) | (((uint64)slot) << 32) | ((uint64)offset)
206//
207//#define CHECKTEMPORALMEMORYSERIAL if (serial != master->getDynamicShmemSerial()) {if (!open()) {mutex->leave();return 0;}}
208//
209//class MasterMemory;
210//class TemporalMemory {
211//public:
212// TemporalMemory(MemoryController* master);
213// ~TemporalMemory();
214//
215// bool getMemoryUsage(uint64& alloc, uint64& usage);
216//
217// bool open();
218// bool create(uint16 slotCount, uint32 slotDuration, uint64 initSize, uint64 maxSize, uint32 growSteps = 8);
219// bool setNodeID(uint16 id);
220//
221// // Insert new block of memory and return full id
222// bool insertMessage(DataMessage* msg, uint64& id);
223// // Get copy of block of memory
224// DataMessage* getCopyOfMessage(uint64 id);
225//
226// bool logUsage();
227//
228// bool maintenance();
229//
230// static bool UnitTest();
231//
232//private:
233// bool getSlotFromEOL(uint64 eol, uint16& slot);
234// char* getMemoryBlock(uint64 id);
235// bool growSlotSize(uint64 growAmount);
236// bool checkSlots();
237// bool resetSlot(uint16 slot, uint64 eol);
238// uint64 calcResizeAmount(uint32 msgSize);
239//
240// utils::Mutex* mutex;
241// MemoryController* master;
242// TemporalMemoryStruct* header;
243// SlotHeaderStruct* slotHeaders;
244// uint64 memorySize;
245// uint16 port;
246// uint16 node;
247// uint32 serial;
248//};
249
250} // namespace cmlabs
251
252#endif //_TEMPORALMEMORY_H_
253
Central shared-memory manager for a Psyclone node: master segment, per-subsystem shared maps and the ...
CMSDK time: µs-resolution 64-bit timestamps and the Time Mapping Constant (TMC).
The central Psyclone data container: a self-contained binary message with typed, named user entries.
Pure-virtual interface for shared-segment bookkeeping (implemented by MasterMemory).
bool getMemoryUsage(uint64 &alloc, uint64 &usage)
Report allocation/usage of the temporal segment.
bool maintenance()
Expire slots whose EOL has passed and reclaim their blocks.
bool logUsage()
Write current usage statistics to the log.
DataMessage * getCopyOfMessage(uint64 id)
Retrieve a private copy of a stored message.
bool setNodeID(uint16 id)
Set the node id used when composing message ids.
bool insertMessage(DataMessage *msg, uint64 &id)
Copy msg into shared memory until its EOL passes.
bool create(uint32 slotCount, uint16 binCount, uint32 minBlockSize, uint32 maxBlockSize, uint64 initSize, uint64 maxSize, uint32 growSteps=8)
Create the temporal segment (master process only).
TemporalMemory(MemoryController *master)
static bool UnitTest()
Self-test of the temporal store.
bool open()
Attach to the existing temporal segment (size/serial from the master).
Recursive mutual-exclusion lock, optionally named for cross-process use.
Definition Utils.h:463
Header of one fixed-block-size bin inside the temporal segment.
uint64 usage
Payload bytes currently stored.
uint32 blocksInUse
Blocks currently allocated.
uint32 blockSize
Byte size of each block.
uint64 size
Total bin size in bytes (header + bitfield + data).
uint32 blockCount
Number of fixed-size blocks in this bin.
Per-message slot record: which bin/blocks hold the message and until when.
uint64 size
Message byte size; 0 means the slot is not in use.
uint64 eol
Absolute end-of-life timestamp (µs); slot reclaimable after this.
uint32 bin
Index of the bin holding the message data.
uint32 startBlock
First block index used within the bin.
uint16 serial
Reuse serial; incremented when the slot is recycled and checked against the serial packed in the mess...
uint64 usage
Bytes actually reserved (blocks used * blocksize >= size).
uint32 blockUsage
Number of contiguous blocks used.
uint64 offset
Byte offset of the message inside the bin's data area.
Root header of the temporal-memory segment.
uint64 growStep
Byte increment used when growing the segment.
uint64 size
Total segment size in bytes.
uint32 slotsInUse
Slots currently holding live messages.
uint64 maxSize
Hard ceiling for segment growth in bytes.
uint32 cid
Check/magic id validated on attach.
uint64 lastCheckTime
Timestamp of the last expiry sweep.
uint16 binCount
Number of block-size bins.
uint64 checkTimeInterval
Interval between expiry sweeps (µs).
uint32 slotCount
Total message slots allocated.
uint64 usage
Bytes currently in use across all bins.