DragonECS/src/Debug/EcsDebug.cs

185 lines
5.6 KiB
C#
Raw Normal View History

using DCFApixels.DragonECS.Utils;
using System;
2023-03-27 17:34:12 +08:00
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
2023-03-27 17:34:12 +08:00
namespace DCFApixels.DragonECS
{
public readonly struct EcsProfilerMarker
2023-03-30 06:03:05 +08:00
{
public readonly int id;
public EcsProfilerMarker(int id) => this.id = id;
2023-03-30 06:17:06 +08:00
public EcsProfilerMarker(string name) => id = EcsDebug.RegisterMark(name);
2023-03-30 11:17:17 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Begin() => EcsDebug.ProfileMarkBegin(id);
2023-03-30 11:17:17 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void End() => EcsDebug.ProfileMarkEnd(id);
2023-03-30 11:17:17 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public AutoScope Auto() => new AutoScope(id);
2023-03-30 06:03:05 +08:00
2023-04-01 20:45:37 +08:00
public readonly ref struct AutoScope
{
private readonly int _id;
public AutoScope(int id)
2023-03-30 06:03:05 +08:00
{
_id = id;
EcsDebug.ProfileMarkBegin(id);
}
public void Dispose() => EcsDebug.ProfileMarkEnd(_id);
2023-03-30 06:03:05 +08:00
}
}
2023-03-27 17:34:12 +08:00
public static class EcsDebug
{
public static void Set<T>() where T : DebugService, new() => DebugService.Set<T>();
public static void Set(DebugService service) => DebugService.Set(service);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2023-03-27 17:34:12 +08:00
public static void Print(object v) => DebugService.Instance.Print(v);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2023-03-27 17:34:12 +08:00
public static void Print(string tag, object v)
{
#if !DISABLE_DRAGONECS_DEBUGGER
2023-03-27 17:34:12 +08:00
DebugService.Instance.Print(tag, v);
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2023-03-27 17:34:12 +08:00
public static int RegisterMark(string name)
{
#if !DISABLE_DRAGONECS_DEBUGGER
2023-03-27 17:34:12 +08:00
return DebugService.Instance.RegisterMark(name);
#else
return 0;
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2023-03-27 17:34:12 +08:00
public static void DeleteMark(string name)
{
#if !DISABLE_DRAGONECS_DEBUGGER
2023-03-27 17:34:12 +08:00
DebugService.Instance.DeleteMark(name);
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2023-03-27 17:34:12 +08:00
public static void ProfileMarkBegin(int id)
{
#if !DISABLE_DRAGONECS_DEBUGGER
2023-03-27 17:34:12 +08:00
DebugService.Instance.ProfileMarkBegin(id);
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2023-03-30 16:39:16 +08:00
public static void ProfileMarkEnd(int id)
2023-03-27 17:34:12 +08:00
{
#if !DISABLE_DRAGONECS_DEBUGGER
2023-03-30 16:39:16 +08:00
DebugService.Instance.ProfileMarkEnd(id);
2023-03-27 17:34:12 +08:00
#endif
}
}
2023-03-30 06:03:05 +08:00
2023-03-27 17:34:12 +08:00
public abstract class DebugService
{
private static DebugService _instance;
public static DebugService Instance
{
get
{
if (_instance == null)
{
_instance = new DefaultDebugService();
}
return _instance;
}
}
public static void Set<T>() where T : DebugService, new() => Set(new T());
public static void Set(DebugService service)
{
_instance = service;
OnServiceChanged(_instance);
}
public static Action<DebugService> OnServiceChanged = delegate { };
2023-04-07 15:11:48 +08:00
private IntDispenser _idDispenser = new IntDispenser(-1);
2023-03-27 17:34:12 +08:00
private Dictionary<string, int> _nameIdTable = new Dictionary<string, int>();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2023-03-27 17:34:12 +08:00
public void Print(object v) => Print(null, v);
public abstract void Print(string tag, object v);
public int RegisterMark(string name)
{
int id;
if(!_nameIdTable.TryGetValue(name, out id))
{
id = _idDispenser.GetFree();
_nameIdTable.Add(name, id);
}
OnNewMark(id, name);
return id;
}
public void DeleteMark(string name)
{
int id = _nameIdTable[name];
_nameIdTable.Remove(name);
_idDispenser.Release(id);
OnDelMark(id);
}
protected abstract void OnNewMark(int id, string name);
protected abstract void OnDelMark(int id);
public abstract void ProfileMarkBegin(int id);
2023-03-30 16:39:16 +08:00
public abstract void ProfileMarkEnd(int id);
2023-03-27 17:34:12 +08:00
}
public sealed class DefaultDebugService : DebugService
{
private Stopwatch[] _stopwatchs;
2023-03-30 16:39:16 +08:00
private string[] _stopwatchsNames;
2023-03-27 17:34:12 +08:00
public DefaultDebugService()
{
#if !DISABLE_DRAGONECS_DEBUGGER
2023-03-27 17:34:12 +08:00
_stopwatchs = new Stopwatch[64];
2023-03-31 14:58:07 +08:00
_stopwatchsNames= new string[64];
2023-03-27 17:34:12 +08:00
#endif
}
public override void Print(string tag, object v)
{
Console.WriteLine($"[{tag}] {v}");
}
public override void ProfileMarkBegin(int id)
{
_stopwatchs[id].Start();
}
2023-03-30 16:39:16 +08:00
public override void ProfileMarkEnd(int id)
2023-03-27 17:34:12 +08:00
{
_stopwatchs[id].Stop();
var time = _stopwatchs[id].Elapsed;
_stopwatchs[id].Reset();
2023-03-30 16:39:16 +08:00
Print(_stopwatchsNames[id] + " s:" + time.TotalSeconds);
2023-03-27 17:34:12 +08:00
}
protected override void OnDelMark(int id)
{
_stopwatchs[id] = null;
}
protected override void OnNewMark(int id, string name)
{
2023-03-30 16:39:16 +08:00
if (id >= _stopwatchs.Length)
{
Array.Resize(ref _stopwatchs, _stopwatchs.Length << 1);
Array.Resize(ref _stopwatchsNames, _stopwatchsNames.Length << 1);
}
2023-03-27 17:34:12 +08:00
_stopwatchs[id] = new Stopwatch();
2023-03-30 16:39:16 +08:00
_stopwatchsNames[id] = name;
2023-03-27 17:34:12 +08:00
}
}
}