-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMappableFileStreamManager.cs
404 lines (322 loc) · 14.3 KB
/
MappableFileStreamManager.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
using System;
using System.Buffers;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using static MappableFileStream.HybridHelper;
namespace MappableFileStream
{
/// <summary>
/// Manager that manages all created <see cref="MappableFileStream"/>s.
/// </summary>
public static class MappableFileStreamManager
{
/// <summary>
/// Holds a list of all created <see cref="MappableFileStream"/>.
/// </summary>
readonly static ConditionalWeakTable<MappableFileStream, object> Streams = new();
private static ulong MaxAvailableMemoryOnStartup;
static IntPtr LoMemResourceHandle;
static IntPtr HiMemResourceHandle;
internal static readonly ReaderWriterLockSlim FlushLock = new(LockRecursionPolicy.SupportsRecursion);
/// <summary>
/// Gets a value of how many items are allowed at maximum to be stored concurrently in memory.
/// This values is calculated from the average blocksize of the stream.
/// </summary>
static int MaxAllowedItems;
/// <summary>
/// This is the threshold that is used to get start paging
/// and unmapping data when the current memory siutation reaches this threshold.
/// It is calculated from the MaxMemory and the <see cref="LowerOSMemoryPercentage"/>.
/// </summary>
static ulong LowerOSMemoryThreshold;
/// <summary>
/// This is the percentage of the <see cref="MaxAvailableMemory"/>. The <see cref="LowerOSMemoryThreshold"/> is calculated by
/// multiplying the <see cref="MaxAvailableMemory"/> with the <see cref="LowerOSMemoryPercentage"/>.
/// </summary>
static double LowerOSMemoryPercentage = 0.1d;
static Thread MemoryThread;
/// <summary>
/// Wait until the memory manager allows access to the data.
/// </summary>
/// <returns></returns>
public static IDisposable WaitForDataAccess() => FlushLock.EnterRead();
static MappableFileStreamManager()
{
// Get the current memory situation. This reflects the current situation and is seen as an upper boundary
// when the management starts.
MaxAvailableMemoryOnStartup = GetOSMemory().ullAvailPhys;
Process.GetCurrentProcess().MaxWorkingSet = (nint)(MaxAvailableMemoryOnStartup * 0.95d);
AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit;
LoMemResourceHandle = CreateMemoryResourceNotification(MemoryResourceNotificationType.LowMemoryResourceNotification);
HiMemResourceHandle = CreateMemoryResourceNotification(MemoryResourceNotificationType.HighMemoryResourceNotification);
// This is the threshold that is used to get active with paging
// and unmapping when the current memory siutation reaches this threshold.
LowerOSMemoryThreshold = (ulong)(MaxAvailableMemoryOnStartup * LowerOSMemoryPercentage);
if (!HybridHelper.InitializeProcessForWsWatch(Process.GetCurrentProcess().SafeHandle))
{
Console.WriteLine(Marshal.GetLastWin32Error());
}
}
/// <summary>
/// Adds a stream being managed by the <see cref="MappableFileStreamManager"/>.
/// </summary>
/// <param name="stream"></param>
internal static void AddStream(MappableFileStream stream)
{
lock (Streams)
{
Streams.Add(stream, null);
AdjustMemoryLimits();
if (MemoryThread is null)
{
MemoryThread = new Thread(MemoryManagerThread);
MemoryThread.Name = "IPIPE Memory Manager";
MemoryThread.Start();
}
}
}
/// <summary>
/// Sets the upper boundary of memory that is allowed to be used by all <see cref="MappableFileStream"/>s.
/// </summary>
/// <param name="allowedMaximumMemory">Sets the allowed memory in bytes.</param>
public static void SetMaxMemory(ulong allowedMaximumMemory)
{
var process = Process.GetCurrentProcess();
if (allowedMaximumMemory != default)
process.MaxWorkingSet = (nint)Math.Min(MaxAvailableMemoryOnStartup, allowedMaximumMemory);
else
process.MaxWorkingSet = (nint)MaxAvailableMemoryOnStartup;
Console.WriteLine($"Maximum Allowed Memory: {process.MaxWorkingSet}");
AdjustMemoryLimits();
}
private static void CurrentDomain_ProcessExit(object sender, EventArgs e)
{
}
/// <summary>
/// Continously adjust the memory limits according to the current situation.
/// </summary>
private static void AdjustMemoryLimits()
{
if (!Streams.Any())
return;
var process = Process.GetCurrentProcess();
var memoryInfo = Streams.Select(x => x.Key.GetMemoryInfo()).ToArray();
var possibleMemory = (long)process.MaxWorkingSet - (long)process.MinWorkingSet;
var averageBlockSize = memoryInfo.Average(x => x.BlockSizeInBytes);
MaxAllowedItems = (int)Math.Floor(possibleMemory / averageBlockSize);
// Calculate how much memory the MaxAllowedItems would use.
var memoryOccupiedByMaxItems = MaxAllowedItems * averageBlockSize;
// It could happen the average blocksize is too high so we need to increase to the max blocksize.
if (memoryOccupiedByMaxItems > possibleMemory)
{
var maxBlockSize = memoryInfo.Max(x => x.BlockSizeInBytes);
MaxAllowedItems = (int)Math.Floor(possibleMemory / (double)maxBlockSize);
}
Console.WriteLine($"Max Items: {MaxAllowedItems}");
}
/// <summary>
/// Removes a stream from being managed by the <see cref="MappableFileStreamManager"/>.
/// </summary>
/// <param name="stream"></param>
internal static void RemoveStream(MappableFileStream stream)
{
lock (Streams)
{
Streams.Remove(stream);
}
}
private static void MemoryManagerThread()
{
while (true)
{
// It only makes sense to perform the cleanup thread if there are streams registered.
SpinWait.SpinUntil(() => Streams.Any());
// Query for low resources.
CleanupMappableStreams();
}
}
/// <summary>
/// Check if we have hi memory resources.
/// </summary>
/// <returns></returns>
[SkipLocalsInit]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
private static bool IsMemoryHi()
{
if (QueryMemoryResourceNotification(HiMemResourceHandle, out var state))
{
return state;
}
return false;
}
/// <summary>
/// Check if we have lo memory resources.
/// </summary>
/// <returns></returns>
[SkipLocalsInit]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
private static bool IsMemoryLo()
{
[SkipLocalsInit]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
static bool TooManyBooklets()
{
var (activeStreams, totalBookletCount) = GetStreams();
return totalBookletCount > MaxAllowedItems;
}
if (QueryMemoryResourceNotification(LoMemResourceHandle, out var state))
{
if ((!state && (GetOSMemory().ullAvailPhys < LowerOSMemoryThreshold)) || TooManyBooklets())
return true;
return state;
}
return false;
}
/// <summary>
/// Returns the total amount of currently allocated items.
/// </summary>
/// <returns></returns>
[SkipLocalsInit]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
private static (MappableFileStream[] activeStreams, int totalBookletCount) GetStreams()
{
int totalBookletCount = 0;
lock (Streams)
{
List<MappableFileStream> result = new(Streams.Count());
foreach (var stream in Streams)
{
// Remove already disposed streams.
if (stream.Key.IsDisposed)
{
Streams.Remove(stream.Key);
continue;
}
totalBookletCount += stream.Key.InternalStore.Count;
result.Add(stream.Key);
}
return (result.ToArray(), totalBookletCount);
}
}
private unsafe static void CleanupMappableStreams()
{
try
{
do
{
// Escape the cleanup if memory resources are enough.
if (!IsMemoryLo() && IsMemoryHi()) return;
// Get the total amount of currently allocated items.
(MappableFileStream[] activeStreams, int totalBookletCount) = GetStreams();
// Block the access to streams so that noone can currently allocate new memory.
using var disallowedProcessingForOtherThreads = FlushLock.EnterWrite();
try
{
Stopwatch unmapWatch = Stopwatch.StartNew();
Console.Write($"Unmapping");
Dictionary<MappableFileStream, List<ValueTuple<int, int>>> streamlets = new();
foreach (var stream in activeStreams)
{
if (!stream.InternalStore.Values.Any())
continue;
var bookletList = stream.InternalStore.Values.OrderBy(x => x.Index).ThenBy(x => x.LastAccess);
int NextIndex = bookletList.First().Index;
List<ValueTuple<int, int>> booklets = new();
ValueTuple<int, int> currentRange = new (NextIndex, NextIndex);
foreach (var booklet in bookletList)
{
if (booklet.Index == NextIndex)
{
currentRange = new (currentRange.Item1, booklet.Index);
NextIndex++;
}
else
{
booklets.Add(currentRange);
currentRange = new (booklet.Index, booklet.Index);
NextIndex = booklet.Index + 1;
}
}
streamlets.Add(stream, booklets);
}
foreach (var key in streamlets)
{
foreach (var section in key.Value)
{
key.Key.UnmapRange(section.Item1, section.Item2 - section.Item1 + 1);
}
key.Key.Flush();
}
Console.WriteLine($"{unmapWatch.Elapsed.TotalSeconds}s");
// int i = 0;
// foreach (var (stream, booklet) in items)
// {
//// stream.Unmap(booklet.Index);
// i++;
// Console.WriteLine(i);
// }
// Console.WriteLine($"{unmapWatch.Elapsed.TotalSeconds}s");
// foreach (var stream in activeStreams)
// stream.Flush();
//Stopwatch flushWatch = Stopwatch.StartNew();
//Console.Write("Flushing: ");
//Parallel.ForEach(activeStreams, stream =>
//{
// stream.Flush();
//});
//Console.WriteLine($"{flushWatch.Elapsed.TotalSeconds}s");
//if (!HybridHelper.K32EmptyWorkingSet(Process.GetCurrentProcess().SafeHandle))
//{
// Console.WriteLine($"EmptyWorkingSet filed an error: {Marshal.GetLastWin32Error()}");
//}
}
finally
{
}
}
while (true);
}
catch (Exception ex)
{
}
}
}
static class ReaderWriterLockExtensions
{
public static IDisposable EnterWrite(this ReaderWriterLockSlim rw) => new ReaderWriterLock_WriteLock_Disposable(rw);
public static IDisposable EnterRead(this ReaderWriterLockSlim rw) => new ReaderWriterLock_ReadLock_Disposable(rw);
}
struct ReaderWriterLock_WriteLock_Disposable : IDisposable
{
private readonly ReaderWriterLockSlim RWLock;
public ReaderWriterLock_WriteLock_Disposable(ReaderWriterLockSlim rwLock)
{
RWLock = rwLock;
RWLock.EnterWriteLock();
}
public void Dispose()
{
RWLock.ExitWriteLock();
}
}
struct ReaderWriterLock_ReadLock_Disposable : IDisposable
{
private readonly ReaderWriterLockSlim RWLock;
public ReaderWriterLock_ReadLock_Disposable(ReaderWriterLockSlim rwLock)
{
RWLock = rwLock;
RWLock.EnterReadLock();
}
public void Dispose()
{
RWLock.ExitReadLock();
}
}
}