chore: additional debugging to try to track down FreeSegment issues (#781)

This commit is contained in:
bd_ 2024-03-16 15:57:11 +09:00 committed by GitHub
parent 12981d1060
commit 88cf2e14b7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 69 additions and 2 deletions

View File

@ -2,6 +2,8 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
#endregion
@ -102,7 +104,13 @@ namespace nadena.dev.modular_avatar.core.armature_lock
if (s == null) throw new ArgumentException("Passed a foreign segment???");
int index = segments.BinarySearch(s, Comparer<Segment>.Create((a, b) => a._offset.CompareTo(b._offset)));
if (index < 0 || segments[index] != s) throw new Exception("Segment not found in FreeSegment");
if (index < 0 || segments[index] != s)
{
var segmentDump = string.Join("\n", segments.ConvertAll(seg => $"{seg._offset} {seg._length} {seg._inUse} id={RuntimeHelpers.GetHashCode(seg)}"));
segmentDump += "\n\nTarget segment " + s._offset + " " + s._length + " " + s._inUse + " id=" + RuntimeHelpers.GetHashCode(s);
throw new Exception("Segment not found in FreeSegment\nCurrent segments:\n" + segmentDump);
}
if (index == segments.Count - 1)
{

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using nadena.dev.modular_avatar.core.armature_lock;
using NUnit.Framework;
@ -66,6 +67,64 @@ namespace UnitTests.ArmatureAwase
AssertSegment(s4, 0, 20, true);
}
enum Op
{
Allocate, Deallocate, Defrag
}
[Test]
public void SegmentRandomOps()
{
for (int i = 0; i < 1000; i++)
{
AllocationMap map = new AllocationMap();
List<ISegment> segments = new List<ISegment>();
List<(Op, int)> ops = new List<(Op, int)>();
try
{
Random r = new Random();
for (int j = 0; j < 100; j++)
{
switch (r.Next(0, segments.Count == 0 ? 1 : 3))
{
case 0:
{
int segSize = r.Next(1, 16);
ISegment s = map.Allocate(segSize);
ops.Add((Op.Allocate, segSize));
segments.Add(s);
break;
}
case 1:
{
int idx = r.Next(0, segments.Count);
ISegment s = segments[idx];
map.FreeSegment(s);
ops.Add((Op.Deallocate, idx));
segments.RemoveAt(idx);
break;
}
case 2:
{
ops.Add((Op.Defrag, 0));
map.Defragment((src, dst, length) => {});
break;
}
}
}
}
catch (Exception e)
{
var trace = string.Join("\n", ops.ConvertAll(op => $"{op.Item1} {op.Item2}"));
Assert.Fail($"Failed at iteration {i} with exception {e}\n{trace}");
throw;
}
}
}
private void AssertSegment(ISegment segment, int offset, int length, bool inUse)
{
var s = segment as AllocationMap.Segment;