-
Notifications
You must be signed in to change notification settings - Fork 570
/
Copy pathLightEpoch.cs
572 lines (506 loc) · 19.3 KB
/
LightEpoch.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Threading;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using System.Diagnostics;
namespace FASTER.core
{
/// <summary>
/// Epoch protection
/// </summary>
public unsafe sealed class LightEpoch
{
private const int kCacheLineBytes = 64;
/// <summary>
/// Default invalid index entry.
/// </summary>
const int kInvalidIndex = 0;
/// <summary>
/// Default number of entries in the entries table
/// </summary>
static readonly ushort kTableSize = Math.Max((ushort)128, (ushort)(Environment.ProcessorCount * 2));
/// <summary>
/// Default drainlist size
/// </summary>
const int kDrainListSize = 16;
/// <summary>
/// Thread protection status entries.
/// </summary>
Entry[] tableRaw;
Entry* tableAligned;
#if !NET5_0_OR_GREATER
GCHandle tableHandle;
#endif
static readonly Entry[] threadIndex;
static readonly Entry* threadIndexAligned;
#if !NET5_0_OR_GREATER
static GCHandle threadIndexHandle;
#endif
/// <summary>
/// List of action, epoch pairs containing actions to be performed when an epoch becomes safe to reclaim.
/// Marked volatile to ensure latest value is seen by the last suspended thread.
/// </summary>
volatile int drainCount = 0;
readonly EpochActionPair[] drainList = new EpochActionPair[kDrainListSize];
/// <summary>
/// A thread's entry in the epoch table.
/// </summary>
[ThreadStatic]
static int threadEntryIndex;
/// <summary>
/// Number of instances using this entry
/// </summary>
[ThreadStatic]
static int threadEntryIndexCount;
/// <summary>
/// Managed thread id of this thread
/// </summary>
[ThreadStatic]
static int threadId;
/// <summary>
/// Start offset to reserve entry in the epoch table
/// </summary>
[ThreadStatic]
static ushort startOffset1;
/// <summary>
/// Alternate start offset to reserve entry in the epoch table (to reduce probing if <see cref="startOffset1"/> slot is already filled)
/// </summary>
[ThreadStatic]
static ushort startOffset2;
/// <summary>
/// Global current epoch value
/// </summary>
public int CurrentEpoch;
/// <summary>
/// Cached value of latest epoch that is safe to reclaim
/// </summary>
public int SafeToReclaimEpoch;
/// <summary>
/// Local view of current epoch, for an epoch-protected thread
/// </summary>
public int LocalCurrentEpoch => (*(tableAligned + threadEntryIndex)).localCurrentEpoch;
/// <summary>
/// Static constructor to setup shared cache-aligned space
/// to store per-entry count of instances using that entry
/// </summary>
static LightEpoch()
{
long p;
// Over-allocate to do cache-line alignment
#if NET5_0_OR_GREATER
threadIndex = GC.AllocateArray<Entry>(kTableSize + 2, true);
p = (long)Unsafe.AsPointer(ref threadIndex[0]);
#else
threadIndex = new Entry[kTableSize + 2];
threadIndexHandle = GCHandle.Alloc(threadIndex, GCHandleType.Pinned);
p = (long)threadIndexHandle.AddrOfPinnedObject();
#endif
// Force the pointer to align to 64-byte boundaries
long p2 = (p + (kCacheLineBytes - 1)) & ~(kCacheLineBytes - 1);
threadIndexAligned = (Entry*)p2;
}
/// <summary>
/// Instantiate the epoch table
/// </summary>
public LightEpoch()
{
long p;
#if NET5_0_OR_GREATER
tableRaw = GC.AllocateArray<Entry>(kTableSize + 2, true);
p = (long)Unsafe.AsPointer(ref tableRaw[0]);
#else
// Over-allocate to do cache-line alignment
tableRaw = new Entry[kTableSize + 2];
tableHandle = GCHandle.Alloc(tableRaw, GCHandleType.Pinned);
p = (long)tableHandle.AddrOfPinnedObject();
#endif
// Force the pointer to align to 64-byte boundaries
long p2 = (p + (kCacheLineBytes - 1)) & ~(kCacheLineBytes - 1);
tableAligned = (Entry*)p2;
CurrentEpoch = 1;
SafeToReclaimEpoch = 0;
// Mark all epoch table entries as "available"
for (int i = 0; i < kDrainListSize; i++)
drainList[i].epoch = int.MaxValue;
drainCount = 0;
}
/// <summary>
/// Clean up epoch table
/// </summary>
public void Dispose()
{
#if !NET5_0_OR_GREATER
tableHandle.Free();
#endif
CurrentEpoch = 1;
SafeToReclaimEpoch = 0;
}
/// <summary>
/// Check whether current epoch instance is protected on this thread
/// </summary>
/// <returns>Result of the check</returns>
public bool ThisInstanceProtected()
{
int entry = threadEntryIndex;
if (kInvalidIndex != entry)
{
if ((*(tableAligned + entry)).threadId == entry)
return true;
}
return false;
}
/// <summary>
/// Check whether any epoch instance is protected on this thread
/// </summary>
/// <returns>Result of the check</returns>
public static bool AnyInstanceProtected()
{
int entry = threadEntryIndex;
if (kInvalidIndex != entry)
{
return threadEntryIndexCount > 0;
}
return false;
}
/// <summary>
/// Enter the thread into the protected code region
/// </summary>
/// <returns>Current epoch</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int ProtectAndDrain()
{
int entry = threadEntryIndex;
// Protect CurrentEpoch by making an entry for it in the non-static epoch table so ComputeNewSafeToReclaimEpoch() will see it.
(*(tableAligned + entry)).threadId = threadEntryIndex;
(*(tableAligned + entry)).localCurrentEpoch = CurrentEpoch;
if (drainCount > 0)
{
Drain((*(tableAligned + entry)).localCurrentEpoch);
}
return (*(tableAligned + entry)).localCurrentEpoch;
}
/// <summary>
/// Thread suspends its epoch entry
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Suspend()
{
Release();
if (drainCount > 0) SuspendDrain();
}
/// <summary>
/// Thread resumes its epoch entry
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Resume()
{
Acquire();
ProtectAndDrain();
}
/// <summary>
/// Thread resumes its epoch entry
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Resume(out int resumeEpoch)
{
Acquire();
resumeEpoch = ProtectAndDrain();
}
/// <summary>
/// Increment current epoch and associate trigger action
/// with the prior epoch
/// </summary>
/// <param name="onDrain">Trigger action</param>
/// <returns></returns>
public void BumpCurrentEpoch(Action onDrain)
{
int PriorEpoch = BumpCurrentEpoch() - 1;
int i = 0;
while (true)
{
if (drainList[i].epoch == int.MaxValue)
{
// This was an empty slot. If it still is, assign this action/epoch to the slot.
if (Interlocked.CompareExchange(ref drainList[i].epoch, int.MaxValue - 1, int.MaxValue) == int.MaxValue)
{
drainList[i].action = onDrain;
drainList[i].epoch = PriorEpoch;
Interlocked.Increment(ref drainCount);
break;
}
}
else
{
var triggerEpoch = drainList[i].epoch;
if (triggerEpoch <= SafeToReclaimEpoch)
{
// This was a slot with an epoch that was safe to reclaim. If it still is, execute its trigger, then assign this action/epoch to the slot.
if (Interlocked.CompareExchange(ref drainList[i].epoch, int.MaxValue - 1, triggerEpoch) == triggerEpoch)
{
var triggerAction = drainList[i].action;
drainList[i].action = onDrain;
drainList[i].epoch = PriorEpoch;
triggerAction();
break;
}
}
}
if (++i == kDrainListSize)
{
// We are at the end of the drain list and found no empty or reclaimable slot. ProtectAndDrain, which should clear one or more slots.
ProtectAndDrain();
i = 0;
Thread.Yield();
}
}
// Now ProtectAndDrain, which may execute the action we just added.
ProtectAndDrain();
}
/// <summary>
/// Mechanism for threads to mark some activity as completed until
/// some version by this thread
/// </summary>
/// <param name="markerIdx">ID of activity</param>
/// <param name="version">Version</param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Mark(int markerIdx, long version)
{
(*(tableAligned + threadEntryIndex)).markers[markerIdx] = (int)version;
}
/// <summary>
/// Check if all active threads have completed the some
/// activity until given version.
/// </summary>
/// <param name="markerIdx">ID of activity</param>
/// <param name="version">Version</param>
/// <returns>Whether complete</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool CheckIsComplete(int markerIdx, long version)
{
// check if all threads have reported complete
for (int index = 1; index <= kTableSize; ++index)
{
int entry_epoch = (*(tableAligned + index)).localCurrentEpoch;
int fc_version = (*(tableAligned + index)).markers[markerIdx];
if (0 != entry_epoch)
{
if ((fc_version != (int)version) && (entry_epoch < int.MaxValue))
{
return false;
}
}
}
return true;
}
/// <summary>
/// Increment global current epoch
/// </summary>
/// <returns></returns>
int BumpCurrentEpoch()
{
int nextEpoch = Interlocked.Add(ref CurrentEpoch, 1);
if (drainCount > 0)
Drain(nextEpoch);
return nextEpoch;
}
/// <summary>
/// Looks at all threads and return the latest safe epoch
/// </summary>
/// <param name="currentEpoch">Current epoch</param>
/// <returns>Safe epoch</returns>
int ComputeNewSafeToReclaimEpoch(int currentEpoch)
{
int oldestOngoingCall = currentEpoch;
for (int index = 1; index <= kTableSize; ++index)
{
int entry_epoch = (*(tableAligned + index)).localCurrentEpoch;
if (0 != entry_epoch)
{
if (entry_epoch < oldestOngoingCall)
{
oldestOngoingCall = entry_epoch;
}
}
}
// The latest safe epoch is the one just before the earliest unsafe epoch.
SafeToReclaimEpoch = oldestOngoingCall - 1;
return SafeToReclaimEpoch;
}
/// <summary>
/// Take care of pending drains after epoch suspend
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void SuspendDrain()
{
while (drainCount > 0)
{
// Barrier ensures we see the latest epoch table entries. Ensures
// that the last suspended thread drains all pending actions.
Thread.MemoryBarrier();
for (int index = 1; index <= kTableSize; ++index)
{
int entry_epoch = (*(tableAligned + index)).localCurrentEpoch;
if (0 != entry_epoch)
{
return;
}
}
Resume();
Release();
}
}
/// <summary>
/// Check and invoke trigger actions that are ready
/// </summary>
/// <param name="nextEpoch">Next epoch</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void Drain(int nextEpoch)
{
ComputeNewSafeToReclaimEpoch(nextEpoch);
for (int i = 0; i < kDrainListSize; i++)
{
var trigger_epoch = drainList[i].epoch;
if (trigger_epoch <= SafeToReclaimEpoch)
{
if (Interlocked.CompareExchange(ref drainList[i].epoch, int.MaxValue - 1, trigger_epoch) == trigger_epoch)
{
// Store off the trigger action, then set epoch to int.MaxValue to mark this slot as "available for use".
var trigger_action = drainList[i].action;
drainList[i].action = null;
drainList[i].epoch = int.MaxValue;
Interlocked.Decrement(ref drainCount);
// Execute the action
trigger_action();
if (drainCount == 0) break;
}
}
}
}
/// <summary>
/// Thread acquires its epoch entry
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void Acquire()
{
if (threadEntryIndex == kInvalidIndex)
threadEntryIndex = ReserveEntryForThread();
Debug.Assert((*(tableAligned + threadEntryIndex)).localCurrentEpoch == 0,
"Trying to acquire protected epoch. Make sure you do not re-enter FASTER from callbacks or IDevice implementations. If using tasks, use TaskCreationOptions.RunContinuationsAsynchronously.");
// This corresponds to AnyInstanceProtected(). We do not mark "ThisInstanceProtected" until ProtectAndDrain().
threadEntryIndexCount++;
}
/// <summary>
/// Thread releases its epoch entry
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void Release()
{
int entry = threadEntryIndex;
Debug.Assert((*(tableAligned + entry)).localCurrentEpoch != 0,
"Trying to release unprotected epoch. Make sure you do not re-enter FASTER from callbacks or IDevice implementations. If using tasks, use TaskCreationOptions.RunContinuationsAsynchronously.");
// Clear "ThisInstanceProtected()" (non-static epoch table)
(*(tableAligned + entry)).localCurrentEpoch = 0;
(*(tableAligned + entry)).threadId = 0;
// Decrement "AnyInstanceProtected()" (static thread table)
threadEntryIndexCount--;
if (threadEntryIndexCount == 0)
{
(threadIndexAligned + threadEntryIndex)->threadId = 0;
threadEntryIndex = kInvalidIndex;
}
}
/// <summary>
/// Reserve entry for thread. This method relies on the fact that no
/// thread will ever have ID 0.
/// </summary>
/// <returns>Reserved entry</returns>
static int ReserveEntry()
{
while (true)
{
// Try to acquire entry
if (0 == (threadIndexAligned + startOffset1)->threadId)
{
if (0 == Interlocked.CompareExchange(
ref (threadIndexAligned + startOffset1)->threadId,
threadId, 0))
return startOffset1;
}
if (startOffset2 > 0)
{
// Try alternate entry
startOffset1 = startOffset2;
startOffset2 = 0;
}
else startOffset1++; // Probe next sequential entry
if (startOffset1 > kTableSize)
{
startOffset1 -= kTableSize;
Thread.Yield();
}
}
}
/// <summary>
/// A 32-bit murmur3 implementation.
/// </summary>
/// <param name="h"></param>
/// <returns></returns>
private static int Murmur3(int h)
{
uint a = (uint)h;
a ^= a >> 16;
a *= 0x85ebca6b;
a ^= a >> 13;
a *= 0xc2b2ae35;
a ^= a >> 16;
return (int)a;
}
/// <summary>
/// Allocate a new entry in epoch table. This is called
/// once for a thread.
/// </summary>
/// <returns>Reserved entry</returns>
static int ReserveEntryForThread()
{
if (threadId == 0) // run once per thread for performance
{
threadId = Environment.CurrentManagedThreadId;
uint code = (uint)Murmur3(threadId);
startOffset1 = (ushort)(1 + (code % kTableSize));
startOffset2 = (ushort)(1 + ((code >> 16) % kTableSize));
}
return ReserveEntry();
}
/// <summary>
/// Epoch table entry (cache line size).
/// </summary>
[StructLayout(LayoutKind.Explicit, Size = kCacheLineBytes)]
struct Entry
{
/// <summary>
/// Thread-local value of epoch
/// </summary>
[FieldOffset(0)]
public int localCurrentEpoch;
/// <summary>
/// ID of thread associated with this entry.
/// </summary>
[FieldOffset(4)]
public int threadId;
[FieldOffset(8)]
public int reentrant;
[FieldOffset(12)]
public fixed int markers[13];
public override string ToString() => $"lce = {localCurrentEpoch}, tid = {threadId}, re-ent {reentrant}";
}
struct EpochActionPair
{
public long epoch;
public Action action;
public override string ToString() => $"epoch = {epoch}, action = {(action is null ? "n/a" : action.Method.ToString())}";
}
}
}