hlsf goes bad
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
+13
-12
@@ -25,7 +25,7 @@
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using StgSharp.Mathematics.Memory;
|
||||
using StgSharp.HighPerformance.Memory;
|
||||
using StgSharp.Mathematics.Numeric;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -41,10 +41,10 @@ namespace StgSharp
|
||||
internal static partial class Program
|
||||
{
|
||||
|
||||
private const long size = 1024L * 1024L * 1024L * 4L;
|
||||
|
||||
private static int sideLength = 1000;
|
||||
|
||||
private static readonly nuint size = (nuint)(1024UL * 1024UL * 1024UL * 4UL);
|
||||
|
||||
internal static unsafe void TestMatrix(
|
||||
)
|
||||
{
|
||||
@@ -52,18 +52,18 @@ namespace StgSharp
|
||||
/**/
|
||||
HybridLayerSegregatedFitAllocator hlsf;
|
||||
unchecked {
|
||||
hlsf = new((nuint)size, 32, size);
|
||||
hlsf = new(size, 32, size);
|
||||
}
|
||||
Matrix<T> m = Matrix<T>.Create(sideLength, sideLength, MatrixLayout.DenseRectangle,
|
||||
hlsf);
|
||||
Matrix<float> m = Matrix<float>.Create(sideLength, sideLength,
|
||||
MatrixLayout.DenseRectangle, hlsf);
|
||||
|
||||
// MatrixCompute.Fill(m, 1.0f);
|
||||
Matrix<T> n = Matrix<T>.Create(sideLength, sideLength, MatrixLayout.DenseRectangle,
|
||||
hlsf);
|
||||
Matrix<float> n = Matrix<float>.Create(sideLength, sideLength,
|
||||
MatrixLayout.DenseRectangle, hlsf);
|
||||
|
||||
// MatrixCompute.Fill(n, 1.0f);
|
||||
Matrix<T> p = Matrix<T>.Create(sideLength, sideLength, MatrixLayout.DenseRectangle,
|
||||
hlsf);
|
||||
Matrix<float> p = Matrix<float>.Create(sideLength, sideLength,
|
||||
MatrixLayout.DenseRectangle, hlsf);
|
||||
|
||||
// MatrixCompute.Fill(p, 0.0f);
|
||||
|
||||
@@ -89,7 +89,7 @@ namespace StgSharp
|
||||
for (int j = 0; j < 20; j++)
|
||||
{
|
||||
string extra = j % 4 == 3 ? " " : ",";
|
||||
T value = p[j, i];
|
||||
float value = p[j, i];
|
||||
|
||||
// value = T.Abs(value) < 1e-6 ? 0 : value;
|
||||
|
||||
@@ -102,7 +102,8 @@ namespace StgSharp
|
||||
Console.WriteLine("===================================================");
|
||||
|
||||
/**/
|
||||
hlsf.Dispose();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,13 +25,13 @@
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using StgSharp.Common.Timing;
|
||||
using StgSharp.Graphics;
|
||||
using StgSharp.HighPerformance.Memory;
|
||||
using StgSharp.Internal;
|
||||
using StgSharp.Mathematics;
|
||||
using StgSharp.Mathematics.Memory;
|
||||
using StgSharp.Mathematics.Numeric;
|
||||
using StgSharp.Timing;
|
||||
using StgSharp.Timing;
|
||||
|
||||
namespace StgSharp
|
||||
{
|
||||
@@ -47,16 +47,18 @@ namespace StgSharp
|
||||
try
|
||||
{
|
||||
World.Initialize()
|
||||
.UseModule<NumericalModule>()
|
||||
.UseModule<GraphicModule>()
|
||||
|
||||
// .UseModule<NumericalModule>()
|
||||
// .UseModule<GraphicModule>()
|
||||
.InitializeModule();
|
||||
|
||||
// TestRegexAnalyse.Run();
|
||||
TestHlsf();
|
||||
|
||||
// HlsfStressTest.Run();
|
||||
// TestSwiss();
|
||||
TestL4Prefetch();
|
||||
|
||||
// TestSwiss();
|
||||
// TestL4Prefetch();
|
||||
// TestMatrix();
|
||||
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using StgSharp.Mathematics.Memory;
|
||||
using StgSharp.HighPerformance.Memory;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
|
||||
+69
-36
@@ -2,8 +2,8 @@
|
||||
// -----------------------------------------------------------------------
|
||||
// file="hlsf-test"
|
||||
// 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
|
||||
@@ -25,7 +25,7 @@
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using StgSharp.Mathematics.Memory;
|
||||
using StgSharp.HighPerformance.Memory;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
@@ -40,44 +40,77 @@ namespace StgSharp
|
||||
|
||||
public static void TestHlsf()
|
||||
{
|
||||
// HLSF test arena
|
||||
Console.WriteLine(
|
||||
"=== HLSF (Hybrid Layer Segregated Fit Allocator) Single-threaded Stress Test ===\n");
|
||||
HybridLayerSegregatedFitAllocator hlsfAllocator = new HybridLayerSegregatedFitAllocator(1024 * 1024 * 1024);
|
||||
|
||||
// === 简洁交替测试:100次alloc与100次free交替进行,直到触发异常或达到最大循环 ===
|
||||
Console.WriteLine("\n=== 简洁交替测试:100次alloc + 100次free 交替 ===");
|
||||
|
||||
int maxRounds = 10000; // 可调整
|
||||
uint[] sizes = {
|
||||
64u,
|
||||
128u,
|
||||
256u,
|
||||
512u,
|
||||
1024u };
|
||||
Random rnd = new Random(42);
|
||||
for (int round = 1; round <= maxRounds; round++)
|
||||
unchecked
|
||||
{
|
||||
// 100 次分配
|
||||
List<hlsfHandle> batch = new List<hlsfHandle>(100);
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
uint sz = sizes[rnd.Next(sizes.Length)];
|
||||
batch.Add(hlsfAllocator.Alloc(sz));
|
||||
Console.WriteLine($"alloced:{i}");
|
||||
}
|
||||
nuint size = (nuint)(4UL * 1024UL * 1024UL * 1024UL);
|
||||
|
||||
// 100 次释放(逆序)
|
||||
for (int i = batch.Count - 1; i >= 0; i--)
|
||||
// HLSF test arena
|
||||
Console.WriteLine(
|
||||
"=== HLSF (Hybrid Layer Segregated Fit Allocator) Single-threaded Stress Test ===\n");
|
||||
HybridLayerSegregatedFitAllocator hlsfAllocator = new(size);
|
||||
|
||||
// === 简洁交替测试:100次alloc与100次free交替进行,直到触发异常或达到最大循环 ===
|
||||
Console.WriteLine("\n=== 简洁交替测试:100次alloc + 100次free 交替 ===");
|
||||
|
||||
int maxRounds = 10000; // 可调整
|
||||
int maxCount = 10000;
|
||||
uint[] sizes = [
|
||||
64u,
|
||||
128u,
|
||||
256u,
|
||||
512u,
|
||||
1024u,
|
||||
4096u,
|
||||
16384u,
|
||||
65536u,
|
||||
262144u,
|
||||
1048576u
|
||||
];
|
||||
int[] offset = [
|
||||
-4,
|
||||
-3,
|
||||
-2,
|
||||
-1,
|
||||
0,
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
4
|
||||
];
|
||||
Random rnd = new Random(42);
|
||||
for (int round = 1; round <= maxRounds; round++)
|
||||
{
|
||||
hlsfAllocator.Free(batch[i]);
|
||||
Console.WriteLine($"freed:{i}");
|
||||
}
|
||||
if (round % 100 == 0) {
|
||||
Console.WriteLine($"[交替测试] 已完成 {round} 轮 100alloc+100free");
|
||||
List<HlsfHandle> batch = new(maxCount);
|
||||
for (int i = 0; i < maxCount; i++)
|
||||
{
|
||||
uint sz = sizes[rnd.Next(sizes.Length)];
|
||||
batch.Add(hlsfAllocator.Alloc(sz));
|
||||
|
||||
// Console.WriteLine($"alloced:{i}");
|
||||
}
|
||||
for (int i = 5; i < maxCount; i += 10)
|
||||
{
|
||||
HlsfHandle origin = batch[i];
|
||||
int index = i + offset[rnd.Next(offset.Length)];
|
||||
batch[i] = batch[index];
|
||||
batch[index] = origin;
|
||||
}
|
||||
for (int i = batch.Count - 1; i >= 0; i--)
|
||||
{
|
||||
hlsfAllocator.Free(batch[i]);
|
||||
|
||||
// Console.WriteLine($"freed:{i}");
|
||||
}
|
||||
if (round % 100 == 0)
|
||||
{
|
||||
Console.WriteLine($"[交替测试] 已完成 {round} 轮 {maxCount}alloc+{maxCount}free");
|
||||
hlsfAllocator.DumpPositionChain(false, 10);
|
||||
hlsfAllocator.Collect();
|
||||
hlsfAllocator.DumpPositionChain(false, 10);
|
||||
}
|
||||
}
|
||||
Console.WriteLine("[交替测试] 正常结束,无异常触发");
|
||||
}
|
||||
Console.WriteLine("[交替测试] 正常结束,无异常触发");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user