L4 briefly pass unit test

This commit is contained in:
Youmu Liu
2026-03-17 20:31:06 +08:00
parent 98d711a3ca
commit dccb815811
13 changed files with 1710 additions and 32 deletions
@@ -0,0 +1,107 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="L4.Debug"
// Project: StgSharp
// 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
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.HighPerformance.Memory
{
public partial class L4
{
public void DebugAgeAll()
{
AgeAll();
}
public bool DebugContains(
nuint origin
)
{
return _map.TryGet(origin, out _);
}
public int DebugEvict(
int count = 1
)
{
return Evict(count);
}
public unsafe DebugSnapshot GetDebugSnapshot()
{
int maxId = _maxId;
byte[] rrpv = new byte[maxId];
byte* p = (byte*)_eviction;
for (int i = 0; i < maxId; i++) {
rrpv[i] = p[i];
}
return new DebugSnapshot(_activeCount, _aheadCount, maxId, rrpv);
}
public readonly struct DebugSnapshot
{
/// <summary>
/// RRPV value for each line(0~3), length == MaxId
/// </summary>
public readonly byte[] RrpvSnapshot;
/// <summary>
/// Count of activated cache line handle
/// </summary>
public readonly int ActiveCount;
/// <summary>
/// Count of predictor ahead of current map position(aheadCount)
/// </summary>
public readonly int AheadCount;
/// <summary>
/// Max ID of the line being using or once used(maxId)
/// </summary>
public readonly int MaxId;
internal DebugSnapshot(
int activeCount,
int aheadCount,
int maxId,
byte[] rrpv
)
{
ActiveCount = activeCount;
AheadCount = aheadCount;
MaxId = maxId;
RrpvSnapshot = rrpv;
}
}
}
}
@@ -2,8 +2,8 @@
// -----------------------------------------------------------------------
// file="L4.Evict"
// 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
@@ -64,7 +64,9 @@ namespace StgSharp.HighPerformance.Memory
/// Count of lines not evicted
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private unsafe int Evict(int count = 1)
private unsafe int Evict(
int count = 1
)
{
int remain = count;
Vector128<byte> one = Vector128.Create((byte)0x01);
@@ -84,7 +86,7 @@ namespace StgSharp.HighPerformance.Memory
{
// EvictLine(i + index);
_ = _map.TryRemove(head->_origin, out _);
PrefetchPredict.WriteBack(head->_address, head->_profile, new ReadOnlySpan<byte>((byte*)head->_address, Unsafe.SizeOf<CacheLineData>()));
PrefetchPredict.WriteBack(head->_origin, head->_profile, new ReadOnlySpan<byte>((byte*)head->_address, Unsafe.SizeOf<CacheLineData>()));
RecycleLine(index);
remain--;
}
@@ -95,13 +97,17 @@ namespace StgSharp.HighPerformance.Memory
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void RecycleLine(int id)
private void RecycleLine(
int id
)
{
_idMux.Push(id);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool TryGetEmptyLine(out int index)
private bool TryGetEmptyLine(
out int index
)
{
if (_idMux.TryPop(out index)) {
return true;
@@ -2,8 +2,8 @@
// -----------------------------------------------------------------------
// file="L4.Prefetch"
// 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
@@ -74,7 +74,7 @@ namespace StgSharp.HighPerformance.Memory
const int maxRetry = 1024;
while (!TryGetEmptyLine(out index))
{
missIndex = Evict(missIndex);
remain = Evict(remain);
retryCount++;
if (retryCount > maxRetry) {
throw new OverflowException($"Failed to evict cache line after {maxRetry} retries.");
@@ -94,7 +94,9 @@ namespace StgSharp.HighPerformance.Memory
_ = Interlocked.Add(ref _aheadCount, actualCount);
}
private partial bool TestHit(nuint origin)
private partial bool TestHit(
nuint origin
)
{
if (_map.TryGet(origin, out nuint handle))
{
+16 -6
View File
@@ -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
@@ -77,7 +77,7 @@ namespace StgSharp.HighPerformance.Memory
ptr += entrySize;
_prediction = (CacheLinePrediction*)ptr;
ptr += predictionSize;
_map = SwissTable.Create(_baseAddress + cacheSize + entrySize, _ => { });
_map = SwissTable.Create(ptr, _ => { });
ptr += mapSize;
_eviction = (EvictionArray*)ptr;
ptr += rrpvSize;
@@ -108,7 +108,9 @@ namespace StgSharp.HighPerformance.Memory
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public CacheLine Map(nuint origin)
public CacheLine Map(
nuint origin
)
{
if (_aheadCount < PredictionLeastAhead)
{
@@ -151,7 +153,9 @@ namespace StgSharp.HighPerformance.Memory
private partial void Prefetch();
private partial bool TestHit(nuint origin);
private partial bool TestHit(
nuint origin
);
public ref struct CacheLine : IDisposable
{
@@ -160,7 +164,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;
@@ -170,6 +178,8 @@ namespace StgSharp.HighPerformance.Memory
[Obsolete("You should not create a CacheLineHandle outside a L4", true)]
public CacheLine() { }
public nuint Address => _handle;
public void Dispose()
{
if (_handle == 0) {