hlsf goes bad

This commit is contained in:
Youmu Liu
2026-05-08 18:09:12 +08:00
parent 8aa319a120
commit d1aca8f745
47 changed files with 5020 additions and 446 deletions
+65 -75
View File
@@ -1,34 +1,31 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="HlsfStressTest.cs"
// file="HlsfStressTest"
// 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 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.
// 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 StgSharp.Mathematics.Memory;
using StgSharp.HighPerformance.Memory;
using System;
using System.Collections.Generic;
using System.Diagnostics;
@@ -48,36 +45,8 @@ namespace StgSharp
3UL * 1024UL * 1024UL * 1024UL; // lower default to 512MB
private static readonly uint[] SizeSweep = {
64,
128,
192,
256,
512,
768,
1024,
2048,
3072,
4096,
8192,
12288,
16384,
32768,
49152,
65536,
131072,
196608,
262144,
524288,
786432,
1_048_576,
2_097_152,
3_145_728,
4_194_304,
8_388_608,
12_582_912,
16_777_216,
25_165_824,
33_554_432 };
64, 128, 192, 256, 512, 768, 1024, 2048, 3072, 4096, 8192, 12288, 16384, 32768, 49152, 65536, 131072, 196608, 262144, 524288, 786432, 1_048_576, 2_097_152, 3_145_728, 4_194_304, 8_388_608, 12_582_912, 16_777_216, 25_165_824, 33_554_432
};
public static unsafe void Run()
{
@@ -95,7 +64,10 @@ namespace StgSharp
{
uint size = sz > MaxAllocSize ? MaxAllocSize : sz;
BenchAllocThroughput(allocator, size, touch:false);
allocator.Collect();
// allocator.DumpPositionChain();
GC.Collect();
GC.WaitForPendingFinalizers();
}
@@ -147,12 +119,16 @@ namespace StgSharp
Console.WriteLine("=== Benchmark Complete ===");
}
private static void BenchAllocFreePairs(HybridLayerSegregatedFitAllocator allocator, int pairs, uint blockSize)
private static void BenchAllocFreePairs(
HybridLayerSegregatedFitAllocator allocator,
int pairs,
uint blockSize
)
{
Stopwatch sw = Stopwatch.StartNew();
for (int i = 0; i < pairs; i++)
{
hlsfHandle h = allocator.Alloc(blockSize);
HlsfHandle h = allocator.Alloc(blockSize);
allocator.Free(h);
}
sw.Stop();
@@ -165,7 +141,8 @@ namespace StgSharp
private static void BenchAllocThroughput(
HybridLayerSegregatedFitAllocator allocator,
uint blockSize,
bool touch)
bool touch
)
{
ulong budget = ArenaBytes / 2; // use half arena for test
if (blockSize == 0) {
@@ -178,11 +155,11 @@ namespace StgSharp
return;
}
List<hlsfHandle> list = new List<hlsfHandle>((int)ops);
List<HlsfHandle> list = new List<HlsfHandle>((int)ops);
Stopwatch sw = Stopwatch.StartNew();
for (long i = 0; i < ops; i++)
{
hlsfHandle h = allocator.Alloc(blockSize);
HlsfHandle h = allocator.Alloc(blockSize);
if (touch)
{
Span<byte> span = h.BufferHandle;
@@ -196,30 +173,33 @@ namespace StgSharp
}
sw.Stop();
double allocOpsPerSec = ops / sw.Elapsed.TotalSeconds;
foreach (hlsfHandle h in list) {
foreach (HlsfHandle h in list) {
allocator.Free(h);
}
// Left align and widen columns
Console.WriteLine($" size={Fmt(blockSize),-12} ops={ops,-12} alloc/s={allocOpsPerSec,-16:0.0} touch={touch,-5}");
Console.WriteLine($" size={Fmt(blockSize),-12} ops={ops,-12} alloc/s={allocOpsPerSec,-16:0.000000} touch={touch,-5}");
}
private static void BenchFragmentationCycle(HybridLayerSegregatedFitAllocator allocator, int targetMiB)
private static void BenchFragmentationCycle(
HybridLayerSegregatedFitAllocator allocator,
int targetMiB
)
{
int targetBytes = targetMiB * 1024 * 1024;
Random rng = new Random(123);
List<hlsfHandle> live = new List<hlsfHandle>(128 * 1024);
List<HlsfHandle> live = new List<HlsfHandle>(128 * 1024);
long allocated = 0;
Stopwatch swA = Stopwatch.StartNew();
while (allocated < targetBytes)
{
uint sz = PickSize(rng);
hlsfHandle h = allocator.Alloc(sz);
HlsfHandle h = allocator.Alloc(sz);
live.Add(h);
allocated += sz;
}
swA.Stop();
Console.WriteLine($" phaseA alloc {targetMiB}MB in {swA.Elapsed.TotalSeconds:0.00}s blocks={live.Count}");
Console.WriteLine($" phaseA alloc {targetMiB}MB in {swA.Elapsed.TotalSeconds:0.000000}s blocks={live.Count}");
Stopwatch swB = Stopwatch.StartNew();
int freed = 0;
@@ -230,7 +210,7 @@ namespace StgSharp
freed++;
}
swB.Stop();
Console.WriteLine($" phaseB freed {freed} in {swB.Elapsed.TotalSeconds:0.00}s remain={live.Count}");
Console.WriteLine($" phaseB freed {freed} in {swB.Elapsed.TotalSeconds:0.000000}s remain={live.Count}");
allocated = 0;
int reCount = 0;
@@ -238,7 +218,7 @@ namespace StgSharp
while (allocated < targetBytes / 2)
{
uint sz = PickSize(rng);
hlsfHandle h = allocator.Alloc(sz);
HlsfHandle h = allocator.Alloc(sz);
live.Add(h);
allocated += sz;
reCount++;
@@ -247,7 +227,7 @@ namespace StgSharp
Console.WriteLine($" phaseC re-alloc {reCount} in {swC.Elapsed.TotalSeconds:0.00}s ~{allocated / (1024.0 * 1024.0):0}MB");
Stopwatch swD = Stopwatch.StartNew();
foreach (hlsfHandle h in live) {
foreach (HlsfHandle h in live) {
allocator.Free(h);
}
@@ -255,9 +235,13 @@ namespace StgSharp
Console.WriteLine($" cleanup freed {live.Count} in {swD.Elapsed.TotalSeconds:0.00}s");
}
private static void BenchFreeThroughput(HybridLayerSegregatedFitAllocator allocator, int count, uint blockSize)
private static void BenchFreeThroughput(
HybridLayerSegregatedFitAllocator allocator,
int count,
uint blockSize
)
{
List<hlsfHandle> list = new List<hlsfHandle>(count);
List<HlsfHandle> list = new List<HlsfHandle>(count);
for (int i = 0; i < count; i++) {
list.Add(allocator.Alloc(blockSize));
}
@@ -274,7 +258,9 @@ namespace StgSharp
Console.WriteLine($" size={Fmt(blockSize),-12} count={count,-12} free/s={freesPerSec,-16:0.0}");
}
private static string Fmt(uint bytes)
private static string Fmt(
uint bytes
)
{
if (bytes >= 1024 * 1024) {
return $"{bytes / (1024 * 1024)}MB";
@@ -287,20 +273,24 @@ namespace StgSharp
return $"{bytes}B";
}
private static uint PickSize(Random rng)
private static uint PickSize(
Random rng
)
{
uint raw = SizeSweep[rng.Next(SizeSweep.Length)];
return raw > MaxAllocSize ? MaxAllocSize : raw;
}
private static void Warmup(HybridLayerSegregatedFitAllocator allocator)
private static void Warmup(
HybridLayerSegregatedFitAllocator allocator
)
{
List<hlsfHandle> tmp = new List<hlsfHandle>(4096);
List<HlsfHandle> tmp = new List<HlsfHandle>(4096);
for (int i = 0; i < 4096; i++) {
tmp.Add(allocator.Alloc(256));
}
foreach (hlsfHandle h in tmp) {
foreach (HlsfHandle h in tmp) {
allocator.Free(h);
}
}