-
Notifications
You must be signed in to change notification settings - Fork 559
/
Copy pathRATTest.cs
287 lines (235 loc) · 11.2 KB
/
RATTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Native = System.UInt32;
namespace Cosmos.Core.Memory.Test
{
[TestClass]
public class MemoryTests
{
[TestMethod]
public unsafe void InitTest()
{
var xRAM = new byte[128 * 1024 * 1024]; // 128 MB
fixed (byte* xPtr = xRAM)
{
RAT.Debug = true;
RAT.Init(xPtr, (uint)xRAM.Length);
Assert.IsTrue(HeapSmall.mMaxItemSize > 512);
uint xRatPages = RAT.GetPageCount((byte)RAT.PageType.RAT);
Assert.IsTrue(xRatPages > 0);
var xFreePages = RAT.GetPageCount((byte)RAT.PageType.Empty);
Assert.IsTrue(xFreePages > 0);
Assert.IsTrue(RAT.GetPageCount((byte)RAT.PageType.HeapSmall) > 0);
Assert.AreEqual(0, HeapSmall.GetAllocatedObjectCount());
Assert.AreEqual((uint)8, RAT.GetPageCount((byte)RAT.PageType.RAT));
}
}
[TestMethod]
public unsafe void RATMethods()
{
var xRAM = new byte[1024 * 1024];
fixed (byte* xPtr = xRAM)
{
RAT.Debug = true;
RAT.Init(xPtr, (uint)xRAM.Length);
uint freePageCount = RAT.FreePageCount;
Assert.IsTrue(freePageCount < RAT.TotalPageCount);
Assert.AreEqual(freePageCount, RAT.GetPageCount((byte)RAT.PageType.Empty));
var largePage = RAT.AllocPages(RAT.PageType.HeapLarge, 3);
Assert.AreEqual(RAT.PageType.HeapLarge, RAT.GetPageType(largePage));
Assert.AreEqual(RAT.GetFirstRATIndex(largePage), RAT.GetFirstRATIndex((byte*)largePage + 20));
Assert.AreEqual(RAT.PageType.HeapLarge, RAT.GetPageType((byte*)largePage + RAT.PageSize));
Assert.AreEqual(RAT.FreePageCount, freePageCount - 3);
}
}
[TestMethod]
public unsafe void SmallAllocTest()
{
var xRAM = new byte[32 * 1024 * 1024]; // 32 MB
fixed (byte* xPtr = xRAM)
{
RAT.Debug = true;
RAT.Init(xPtr, (uint)xRAM.Length);
uint smallPages = RAT.GetPageCount((byte)RAT.PageType.HeapSmall);
uint largePages = RAT.GetPageCount((byte)RAT.PageType.HeapLarge);
// the following allocations should all go on the same page
var ptr1 = Heap.Alloc(8);
var ptr2 = Heap.Alloc(3);
ptr2[0] = 12;
ptr2[1] = 101;
var ptr3 = Heap.Alloc(8);
var ptr4 = Heap.Alloc(20);
var ptr5 = Heap.Alloc(22);
Assert.AreNotEqual((uint)ptr1, (uint)ptr2);
Assert.AreEqual((uint)ptr2 - (uint)ptr1, (uint)ptr3 - (uint)ptr2);
Assert.AreEqual(24 + HeapSmall.PrefixItemBytes, (uint)ptr5 - (uint)ptr4);
Assert.AreEqual(RAT.PageSize, (uint)ptr4 - (uint)ptr1);
Assert.AreEqual(12, ptr2[0]);
Assert.AreEqual(smallPages, RAT.GetPageCount((byte)RAT.PageType.HeapSmall));
Assert.AreEqual(largePages, RAT.GetPageCount((byte)RAT.PageType.HeapLarge));
Assert.AreEqual(5, HeapSmall.GetAllocatedObjectCount());
Heap.Free(ptr2);
Assert.AreEqual(4, HeapSmall.GetAllocatedObjectCount());
Assert.AreEqual(0, ptr2[0]);
var nptr2 = Heap.Alloc(10);
Heap.Alloc(10);
Assert.AreEqual((uint)ptr2, (uint)nptr2); // we use the earliest free position
Assert.AreEqual(6, HeapSmall.GetAllocatedObjectCount());
}
}
[TestMethod]
public unsafe void SmallHeapMultiPageAllocationTest()
{
var xRAM = new byte[1024 * 1024]; // 4 MB
fixed (byte* xPtr = xRAM)
{
RAT.Debug = true;
RAT.Init(xPtr, (uint)xRAM.Length);
uint smallPages = RAT.GetPageCount((byte)RAT.PageType.HeapSmall);
var ptr1 = Heap.Alloc(HeapSmall.mMaxItemSize); // 4 of them should fit on one page
var ptr2 = Heap.Alloc(HeapSmall.mMaxItemSize);
var ptr3 = Heap.Alloc(HeapSmall.mMaxItemSize);
var ptr4 = Heap.Alloc(HeapSmall.mMaxItemSize);
Assert.AreEqual((uint)ptr2 - (uint)ptr1, (uint)ptr4 - (uint)ptr3);
Assert.AreEqual((uint)RAT.GetPagePtr(ptr1), (uint)RAT.GetPagePtr(ptr2));
Assert.AreEqual(4, HeapSmall.GetAllocatedObjectCount());
Heap.Free(ptr4);
var nptr4 = Heap.Alloc(HeapSmall.mMaxItemSize);
Assert.AreEqual((uint)RAT.GetPagePtr(ptr1), (uint)RAT.GetPagePtr(ptr4));
var ptr5 = Heap.Alloc(HeapSmall.mMaxItemSize); // this should cause a new page to have to be created
uint largePages = RAT.GetPageCount((byte)RAT.PageType.HeapLarge);
Assert.AreEqual((uint)0, largePages);
Assert.AreEqual(smallPages + 1, RAT.GetPageCount((byte)RAT.PageType.HeapSmall));
Assert.AreNotEqual((uint)ptr1, (uint)ptr5);
Assert.IsTrue(((uint)ptr5 - (uint)ptr1) % RAT.PageSize == 0);
Assert.AreEqual(5, HeapSmall.GetAllocatedObjectCount());
// now lets force them to allocate 2 more pages
for (int i = 0; i < 8; i++)
{
Heap.Alloc(HeapSmall.mMaxItemSize - 2);
}
Assert.AreEqual(smallPages + 3, RAT.GetPageCount((byte)RAT.PageType.HeapSmall));
Assert.AreEqual(13, HeapSmall.GetAllocatedObjectCount());
}
}
[TestMethod]
public unsafe void SmallHeapTestExpansion()
{
var xRAM = new byte[1024 * 1024]; // 4 MB
fixed (byte* xPtr = xRAM)
{
RAT.Debug = true;
RAT.Init(xPtr, (uint)xRAM.Length);
uint smallPages = RAT.GetPageCount((byte)RAT.PageType.HeapSmall);
for (int i = 0; i < 9; i++)
{
Heap.Alloc(600);
}
Assert.AreEqual(smallPages + 2, RAT.GetPageCount((byte)RAT.PageType.HeapSmall));
}
}
[TestMethod]
public unsafe void SmallHeapStressTest() // this test is important since it tests that the SMT table can grow to multiple pages
{
var xRAM = new byte[1024 * 1024]; // 4 MB
fixed (byte* xPtr = xRAM)
{
RAT.Debug = true;
RAT.Init(xPtr, (uint)xRAM.Length);
Random random = new Random();
for (int i = 0; i < 1000; i++)
{
if (Heap.Alloc((uint)random.Next(4, (int)HeapSmall.mMaxItemSize)) == null)
{
Assert.Fail();
}
}
Assert.AreEqual(1000, HeapSmall.GetAllocatedObjectCount());
}
}
[TestMethod]
public unsafe void MediumLargeHeapTest() // as long as medium just does the same as the large heap, we can test them together
{
var xRAM = new byte[1024 * 1024]; // 4 MB
fixed (byte* xPtr = xRAM)
{
RAT.Debug = true;
RAT.Init(xPtr, (uint)xRAM.Length);
var largeCount = RAT.GetPageCount((byte)RAT.PageType.HeapLarge);
var mediumCount = RAT.GetPageCount((byte)RAT.PageType.HeapMedium);
var ptr1 = Heap.Alloc(HeapMedium.MaxItemSize); // this will allocate two pages,
// since we simplify the math by assuming we never want only one full page
var ptr2 = Heap.Alloc(HeapMedium.MaxItemSize - 10);
var ptr3 = Heap.Alloc(HeapMedium.MaxItemSize + 10);
Assert.AreEqual(largeCount + 2, RAT.GetPageCount((byte)RAT.PageType.HeapLarge));
Assert.AreEqual(mediumCount + 3, RAT.GetPageCount((byte)RAT.PageType.HeapMedium));
var ptr4 = Heap.Alloc(RAT.PageSize * 5 - HeapLarge.PrefixBytes - 1);
Assert.AreEqual(largeCount + 7, RAT.GetPageCount((byte)RAT.PageType.HeapLarge));
Assert.AreEqual(6, (int)RAT.GetPageCount((byte)RAT.PageType.Extension));
Heap.Free(ptr4);
Assert.AreEqual(largeCount + 2, RAT.GetPageCount((byte)RAT.PageType.HeapLarge));
Assert.AreEqual(2, (int)RAT.GetPageCount((byte)RAT.PageType.Extension));
Heap.Free(ptr1);
Heap.Free(ptr2);
Assert.AreEqual(mediumCount, RAT.GetPageCount((byte)RAT.PageType.HeapMedium));
Assert.AreEqual(largeCount + 2, RAT.GetPageCount((byte)RAT.PageType.HeapLarge));
}
}
unsafe void FillRandom(byte* ptr, uint aSize) // fake new obj and fill object with something
{
Random random = new Random();
for (int i = 0; i < aSize; i++)
{
ptr[i] = (byte)random.Next(16, 32);
}
}
[TestMethod]
public unsafe void TestAllocPages() // ensure that we fail gracefully when memory gets full
{
var xRAM = new byte[10 * RAT.PageSize]; // 10 Pages - 1 for RAT and 9 for values
fixed (byte* xPtr = xRAM)
{
RAT.Debug = true;
RAT.Init(xPtr, (uint)xRAM.Length);
Assert.AreEqual((uint)1, RAT.GetPageCount((byte)RAT.PageType.RAT));
try
{
for (int i = 0; i < 10000; i++)
{
byte* ptr = Heap.Alloc(40);
FillRandom(ptr, 40);
if (ptr == null)
{
Assert.Fail();
}
Assert.AreEqual((uint)1, RAT.GetPageCount((byte)RAT.PageType.RAT));
}
}
catch (Exception e)
{
Assert.AreEqual("289", e.Message);
}
}
}
[TestMethod]
public unsafe void TestRATHeapMethods()
{
var xRAM = new byte[10 * RAT.PageSize]; // 10 Pages - 1 for RAT and 9 for values
fixed (byte* xPtr = xRAM)
{
RAT.Debug = true;
RAT.Init(xPtr, (uint)xRAM.Length);
var ptr1 = Heap.Alloc(10);
var ptr2 = Heap.Alloc(10);
var ptr3 = Heap.Alloc(10);
Assert.AreNotEqual((uint)ptr1, (uint)ptr2);
Assert.AreNotEqual((uint)ptr1, (uint)ptr3);
Assert.AreNotEqual((uint)ptr2, (uint)ptr3);
Assert.AreEqual(RAT.GetFirstRATIndex(ptr1), RAT.GetFirstRATIndex(ptr2));
Assert.AreEqual(RAT.GetFirstRATIndex(ptr1), RAT.GetFirstRATIndex(ptr3));
Assert.AreEqual((uint)RAT.GetPagePtr(ptr1), (uint)RAT.GetPagePtr(ptr2));
Assert.AreEqual((uint)RAT.GetPagePtr(ptr1), (uint)RAT.GetPagePtr(ptr3));
}
}
}
}