some chore
This commit is contained in:
@@ -2,8 +2,8 @@
|
||||
// -----------------------------------------------------------------------
|
||||
// file="L4"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. All rights reserved.
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
@@ -26,18 +26,12 @@
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using StgSharp.Collections;
|
||||
using StgSharp.Mathematics.Memory;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Data;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace StgSharp.HighPerformance.Memory
|
||||
{
|
||||
public unsafe partial class L4 : IDisposable
|
||||
public sealed unsafe partial class L4 : IDisposable
|
||||
{
|
||||
|
||||
private const int CacheLineCount = 8192;
|
||||
@@ -55,25 +49,40 @@ namespace StgSharp.HighPerformance.Memory
|
||||
|
||||
private int _aheadCount;
|
||||
private int _maxId;
|
||||
private volatile int _prefetching;
|
||||
private readonly nuint _baseAddress;
|
||||
private readonly SwissTable _map;
|
||||
private readonly UnmanagedStack<int> _idMux;
|
||||
|
||||
public L4()
|
||||
{
|
||||
int cacheSize = Unsafe.SizeOf<CacheLineData>() * CacheLineCount;
|
||||
int entrySize = Unsafe.SizeOf<CacheLineHead>() * CacheLineCount;
|
||||
int stackSize = sizeof(int) * CacheLineCount;
|
||||
int mapSize = SwissTable.RequestedNativeMemorySize;
|
||||
int predictionSize = Unsafe.SizeOf<CacheLinePrediction>() * PredictionCount;
|
||||
int rrpvSize = Unsafe.SizeOf<EvictionArray>();
|
||||
_baseAddress = (nuint)NativeMemory.AlignedAlloc((nuint)(cacheSize + entrySize + stackSize + mapSize + predictionSize + rrpvSize), 16);
|
||||
// init order is fallow by align requirement of each component.
|
||||
nuint cacheSize = (nuint)Unsafe.SizeOf<CacheLineData>() * CacheLineCount; // SIMDID, no less than 16 byte bit
|
||||
nuint entrySize = (nuint)Unsafe.SizeOf<CacheLineHead>() * CacheLineCount; // 16 byte align
|
||||
nuint predictionSize = (nuint)Unsafe.SizeOf<CacheLinePrediction>() * PredictionCount; //16 byte align
|
||||
nuint mapSize = (nuint)SwissTable.RequestedNativeMemorySize; // 16 byte align
|
||||
nuint rrpvSize = (nuint)Unsafe.SizeOf<EvictionArray>(); // 16 byte align
|
||||
nuint stackSize = (nuint)sizeof(int) * CacheLineCount; // int align
|
||||
|
||||
// malloc all memory at once, and free at once in Dispose.
|
||||
// This is to avoid fragmentation and improve performance.
|
||||
_baseAddress = (nuint)NativeMemory.AlignedAlloc(cacheSize + entrySize + stackSize + mapSize + predictionSize + rrpvSize, 16);
|
||||
nuint ptr = _baseAddress;
|
||||
|
||||
// init all components with the buffer, and move the buffer pointer forward.
|
||||
nuint _buffer = _baseAddress;
|
||||
_data = (CacheLineData*)_buffer;
|
||||
_head = (CacheLineHead*)(_buffer + (nuint)cacheSize);
|
||||
_prediction = (CacheLinePrediction*)(_buffer + (nuint)(cacheSize + entrySize));
|
||||
_map = SwissTable.Create(_baseAddress + (nuint)(cacheSize + entrySize), _ => { });
|
||||
_eviction = (EvictionArray*)(_baseAddress + (nuint)(cacheSize + entrySize + mapSize));
|
||||
ptr += cacheSize;
|
||||
_head = (CacheLineHead*)ptr;
|
||||
ptr += entrySize;
|
||||
_prediction = (CacheLinePrediction*)ptr;
|
||||
ptr += predictionSize;
|
||||
_map = SwissTable.Create(_baseAddress + cacheSize + entrySize, _ => { });
|
||||
_prediction = (CacheLinePrediction*)ptr;
|
||||
_eviction = (EvictionArray*)ptr;
|
||||
ptr += rrpvSize;
|
||||
_idMux = new UnmanagedStack<int>((int*)ptr, CacheLineCount, _ => { });
|
||||
ptr += stackSize;
|
||||
}
|
||||
|
||||
public IPrefetchPredict PrefetchPredict { get; set; }
|
||||
@@ -93,30 +102,58 @@ namespace StgSharp.HighPerformance.Memory
|
||||
}
|
||||
_disposed = true;
|
||||
_map.Dispose();
|
||||
_idMux.Dispose();
|
||||
NativeMemory.AlignedFree((void*)_baseAddress);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
public CacheLine Map(nuint origin)
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public CacheLine Map(
|
||||
nuint origin
|
||||
)
|
||||
{
|
||||
if (_map.TryGet(origin, out nuint entryHandle))
|
||||
if (_aheadCount < PredictionLeastAhead)
|
||||
{
|
||||
// cache hit
|
||||
CacheLineHead* entry = (CacheLineHead*)entryHandle;
|
||||
return new CacheLine(entry->_address, ref _activeCount, ref entry->_refCount);
|
||||
while (true)
|
||||
{
|
||||
if (Interlocked.CompareExchange(ref _prefetching, 1, 0) == 0)
|
||||
{
|
||||
if (_aheadCount < PredictionLeastAhead)
|
||||
{
|
||||
// do prefetch in work load now, sync
|
||||
Prefetch();
|
||||
break;
|
||||
} else
|
||||
{
|
||||
_prefetching = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
while (true)
|
||||
{
|
||||
if (_map.TryGet(origin, out nuint entryHandle))
|
||||
{
|
||||
// cache hit
|
||||
CacheLineHead* entry = (CacheLineHead*)entryHandle;
|
||||
if (Interlocked.Increment(ref entry->_refCount) < 0)
|
||||
{
|
||||
(*_eviction)[entry->_id] = 0;
|
||||
return new CacheLine(entry->_address, ref _activeCount, ref entry->_refCount);
|
||||
} else
|
||||
{
|
||||
_ = Interlocked.Decrement(ref entry->_refCount);
|
||||
}
|
||||
}
|
||||
Thread.Sleep(0);
|
||||
}
|
||||
|
||||
// cache miss
|
||||
/*
|
||||
* post command
|
||||
* then wait for completion
|
||||
*/
|
||||
return /*origin/**/default; //error now
|
||||
}
|
||||
|
||||
private partial void Prefetch();
|
||||
|
||||
private partial bool TestHit(nuint origin);
|
||||
private partial bool TestHit(
|
||||
nuint origin
|
||||
);
|
||||
|
||||
public ref struct CacheLine : IDisposable
|
||||
{
|
||||
@@ -125,7 +162,11 @@ namespace StgSharp.HighPerformance.Memory
|
||||
private ref int _refCount;
|
||||
private nuint _handle;
|
||||
|
||||
internal CacheLine(nuint handle, ref int activeCount, ref int cacheRefCount)
|
||||
internal CacheLine(
|
||||
nuint handle,
|
||||
ref int activeCount,
|
||||
ref int cacheRefCount
|
||||
)
|
||||
{
|
||||
_handle = handle;
|
||||
_refCount = ref cacheRefCount;
|
||||
@@ -134,6 +175,9 @@ namespace StgSharp.HighPerformance.Memory
|
||||
_ = Interlocked.Increment(ref _refCount);
|
||||
}
|
||||
|
||||
[Obsolete("You should not create a CacheLineHandle outside a L4", true)]
|
||||
public CacheLine() { }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_handle == 0) {
|
||||
|
||||
Reference in New Issue
Block a user