CMSDK  2.0.1
TemporalMemory.h
1 #if !defined(_TEMPORALMEMORY_H_)
2 #define _TEMPORALMEMORY_H_
3 
4 #include "MemoryManager.h"
5 #include "PsyTime.h"
6 
7 namespace cmlabs {
8 
9  struct SlotEntryStruct {
10  uint16 serial; // wraps around
11  uint64 eol;
12  uint32 bin;
13  uint64 offset;
14  uint64 size; // if 0 not in use
15  uint64 usage; // blocks used * blocksize >= size
16  uint32 startBlock;
17  uint32 blockUsage;
18  //uint32 blockSize;
19  };
20 
22  uint64 size;
23  uint32 cid;
24  uint32 slotCount;
25  uint32 slotsInUse;
26  uint64 usage;
27  uint64 growStep;
28  uint64 maxSize;
29  uint64 createdTime; // time of creation
30  uint64 lastCheckTime;
31  uint64 checkTimeInterval;
32  uint16 binCount;
33  uint32 bitFieldSize; // number of bytes in the bitfield
34  // Bitfield(bitFieldSize)
35  // slotCount * SlotEntryStruct
36  // bin0(size), bin1(size), bin2(size)...
37  };
38 
39  struct BinHeaderStruct {
40  uint64 size;
41  uint32 blockCount;
42  uint32 blockSize;
43  uint32 blocksInUse;
44  uint64 usage;
45  uint32 bitFieldSize; // number of bytes in the bitfield
46  // Bitfield(bitFieldSize)
47  // blockCount * blockSize data
48  };
49 
50  //struct BinSpaceFit {
51  // uint32 bin;
52  // uint32 blocksNeeded;
53  // uint32 spaceWaisted;
54  //};
55  //bool BinSpaceFitSort(const BinSpaceFit& a, const BinSpaceFit& b) {
56  // return (a.blocksNeeded < b.blocksNeeded);
57  //}
58 
59 #define GETMSGNODE(id) (uint16)((id & 0xffff000000000000L)>>48)
60 #define GETMSGSERIAL(id) (uint16)((id & 0x0000ffff00000000L)>>32)
61 #define GETMSGSLOT(id) (uint32)(id & 0x00000000ffffffffL)
62 #define SETMSGID(id,node,serial,slot) id = (((uint64)node) << 48) | (((uint64)serial) << 32) | ((uint64)slot)
63 
64 #define CHECKTEMPORALMEMORYSERIAL if (serial != master->getDynamicShmemSerial()) {if (!open()) {mutex->leave();return 0;}}
65 
66  class MasterMemory;
68  public:
70  ~TemporalMemory();
71 
72  bool getMemoryUsage(uint64& alloc, uint64& usage);
73 
74  bool open();
75  bool create(uint32 slotCount, uint16 binCount, uint32 minBlockSize, uint32 maxBlockSize, uint64 initSize, uint64 maxSize, uint32 growSteps = 8);
76  bool setNodeID(uint16 id);
77 
78  // Insert new block of memory and return full id
79  bool insertMessage(DataMessage* msg, uint64& id);
80  // Get copy of block of memory
81  DataMessage* getCopyOfMessage(uint64 id);
82 
83  bool logUsage();
84 
85  bool maintenance();
86 
87  static bool UnitTest();
88 
89  private:
90 
91  std::vector<BinHeaderStruct> calcBinHeaders(uint32 binCount, uint32 minBlockSize, uint32 maxBlockSize, uint64 binDataSize);
92  BinHeaderStruct* getBestBin(uint32 size, uint32& bin, uint32 &blocksNeeded);
93  char* getMemoryBlock(uint64 id);
94  bool checkSlots(uint64 now);
95 
96  bool resizeSlots();
97  bool resizeBin(uint32 bin, BinHeaderStruct* binHeader, uint32 msgSize);
98  std::vector<BinHeaderStruct> calcBinHeadersGrowth(uint32 bin = 0, uint32 msgSize = 0);
99  bool resizeMemory(uint32 slotGrowth, std::vector<BinHeaderStruct>& binHeaderSizes);
100 
101  utils::Mutex* mutex;
102  MemoryController* master;
103  TemporalMemoryStruct* header;
104  SlotEntryStruct* slotEntries;
105  uint64 memorySize;
106  uint16 port;
107  uint16 node;
108  uint32 serial;
109  };
110 
111 
112 //
113 //
114 //struct TemporalMemoryStruct {
115 // uint64 size;
116 // uint32 cid;
117 // uint64 usage;
118 // uint64 growStep;
119 // uint64 maxSize;
120 // uint64 createdTime; // time of creation
121 // uint16 slotCount;
122 // uint64 slotSize;
123 // uint32 slotDuration;
124 // uint16 startSlot;
125 // uint64 startTime;
126 // // slots * SlotHeaderStruct
127 //};
128 //
129 //struct SlotHeaderStruct {
130 // uint64 size;
131 // uint32 count;
132 // uint64 usage;
133 // uint64 eol;
134 // uint64 offset;
135 //};
136 //
137 //#define GETMSGNODE(id) (uint16)((id & 0xffff000000000000L)>>48)
138 //#define GETMSGSLOT(id) (uint16)((id & 0x0000ffff00000000L)>>32)
139 //#define GETMSGOFFSET(id) (uint64)(id & 0x00000000ffffffffL)
140 //#define SETMSGID(id,node,slot,offset) id = (((uint64)node) << 48) | (((uint64)slot) << 32) | ((uint64)offset)
141 //
142 //#define CHECKTEMPORALMEMORYSERIAL if (serial != master->getDynamicShmemSerial()) {if (!open()) {mutex->leave();return 0;}}
143 //
144 //class MasterMemory;
145 //class TemporalMemory {
146 //public:
147 // TemporalMemory(MemoryController* master);
148 // ~TemporalMemory();
149 //
150 // bool getMemoryUsage(uint64& alloc, uint64& usage);
151 //
152 // bool open();
153 // bool create(uint16 slotCount, uint32 slotDuration, uint64 initSize, uint64 maxSize, uint32 growSteps = 8);
154 // bool setNodeID(uint16 id);
155 //
156 // // Insert new block of memory and return full id
157 // bool insertMessage(DataMessage* msg, uint64& id);
158 // // Get copy of block of memory
159 // DataMessage* getCopyOfMessage(uint64 id);
160 //
161 // bool logUsage();
162 //
163 // bool maintenance();
164 //
165 // static bool UnitTest();
166 //
167 //private:
168 // bool getSlotFromEOL(uint64 eol, uint16& slot);
169 // char* getMemoryBlock(uint64 id);
170 // bool growSlotSize(uint64 growAmount);
171 // bool checkSlots();
172 // bool resetSlot(uint16 slot, uint64 eol);
173 // uint64 calcResizeAmount(uint32 msgSize);
174 //
175 // utils::Mutex* mutex;
176 // MemoryController* master;
177 // TemporalMemoryStruct* header;
178 // SlotHeaderStruct* slotHeaders;
179 // uint64 memorySize;
180 // uint16 port;
181 // uint16 node;
182 // uint32 serial;
183 //};
184 
185 } // namespace cmlabs
186 
187 #endif //_TEMPORALMEMORY_H_
188 
Definition: MemoryManager.h:169
Definition: MemoryController.h:9
Definition: Bitmap.h:7
Definition: TemporalMemory.h:39
Definition: TemporalMemory.h:9
Definition: TemporalMemory.h:67
Definition: Utils.h:276
Definition: TemporalMemory.h:21
Definition: DataMessage.h:95