add ProfilerMarker

This commit is contained in:
Mikhail 2023-03-30 06:03:05 +08:00
parent f23ed953d7
commit 67a57621d5
2 changed files with 51 additions and 30 deletions

View File

@ -1,5 +1,8 @@
namespace DCFApixels.DragonECS using DCFApixels.DragonECS.Profile;
namespace DCFApixels.DragonECS
{ {
public interface IEcsPreInitSystem : IEcsSystem public interface IEcsPreInitSystem : IEcsSystem
{ {
public void PreInit(EcsPipeline pipeline); public void PreInit(EcsPipeline pipeline);
@ -22,17 +25,15 @@
public sealed class EcsPreInitRunner : EcsRunner<IEcsPreInitSystem>, IEcsPreInitSystem public sealed class EcsPreInitRunner : EcsRunner<IEcsPreInitSystem>, IEcsPreInitSystem
{ {
#if DEBUG #if DEBUG
private int[] _targetIds; private ProfilerMarker[] _markers;
#endif #endif
public void PreInit(EcsPipeline pipeline) public void PreInit(EcsPipeline pipeline)
{ {
#if DEBUG #if DEBUG
for (int i = 0; i < targets.Length; i++) for (int i = 0; i < targets.Length; i++)
{ {
int id = _targetIds[i]; using (_markers[i].Auto())
EcsDebug.ProfileMarkBegin(id); targets[i].PreInit(pipeline);
targets[i].PreInit(pipeline);
EcsDebug.ProfileMarkEnd(id);
} }
#else #else
foreach (var item in targets) item.PreInit(pipeline); foreach (var item in targets) item.PreInit(pipeline);
@ -42,10 +43,10 @@
#if DEBUG #if DEBUG
protected override void OnSetup() protected override void OnSetup()
{ {
_targetIds = new int[targets.Length]; _markers = new ProfilerMarker[targets.Length];
for (int i = 0; i < targets.Length; i++) for (int i = 0; i < targets.Length; i++)
{ {
_targetIds[i] = EcsDebug.RegisterMark($"EcsRunner.{targets[i].GetType().Name}.{nameof(PreInit)}"); _markers[i] = new ProfilerMarker(EcsDebug.RegisterMark($"EcsRunner.{targets[i].GetType().Name}.{nameof(PreInit)}"));
} }
} }
#endif #endif
@ -53,17 +54,15 @@
public sealed class EcsInitRunner : EcsRunner<IEcsInitSystem>, IEcsInitSystem public sealed class EcsInitRunner : EcsRunner<IEcsInitSystem>, IEcsInitSystem
{ {
#if DEBUG #if DEBUG
private int[] _targetIds; private ProfilerMarker[] _markers;
#endif #endif
public void Init(EcsPipeline pipeline) public void Init(EcsPipeline pipeline)
{ {
#if DEBUG #if DEBUG
for (int i = 0; i < targets.Length; i++) for (int i = 0; i < targets.Length; i++)
{ {
int id = _targetIds[i]; using (_markers[i].Auto())
EcsDebug.ProfileMarkBegin(id); targets[i].Init(pipeline);
targets[i].Init(pipeline);
EcsDebug.ProfileMarkEnd(id);
} }
#else #else
foreach (var item in targets) item.Init(pipeline); foreach (var item in targets) item.Init(pipeline);
@ -73,10 +72,10 @@
#if DEBUG #if DEBUG
protected override void OnSetup() protected override void OnSetup()
{ {
_targetIds = new int[targets.Length]; _markers = new ProfilerMarker[targets.Length];
for (int i = 0; i < targets.Length; i++) for (int i = 0; i < targets.Length; i++)
{ {
_targetIds[i] = EcsDebug.RegisterMark($"EcsRunner.{targets[i].GetType().Name}.{nameof(Init)}"); _markers[i] = new ProfilerMarker(EcsDebug.RegisterMark($"EcsRunner.{targets[i].GetType().Name}.{nameof(Init)}"));
} }
} }
#endif #endif
@ -84,17 +83,16 @@
public sealed class EcsRunRunner : EcsRunner<IEcsRunSystem>, IEcsRunSystem public sealed class EcsRunRunner : EcsRunner<IEcsRunSystem>, IEcsRunSystem
{ {
#if DEBUG #if DEBUG
private int[] _targetIds; private ProfilerMarker[] _markers;
#endif #endif
public void Run(EcsPipeline pipeline) public void Run(EcsPipeline pipeline)
{ {
#if DEBUG #if DEBUG
for (int i = 0; i < targets.Length; i++) for (int i = 0; i < targets.Length; i++)
{ {
int id = _targetIds[i]; using (_markers[i].Auto())
EcsDebug.ProfileMarkBegin(id); targets[i].Run(pipeline);
targets[i].Run(pipeline);
EcsDebug.ProfileMarkEnd(id);
} }
#else #else
foreach (var item in targets) item.Run(pipeline); foreach (var item in targets) item.Run(pipeline);
@ -104,10 +102,10 @@
#if DEBUG #if DEBUG
protected override void OnSetup() protected override void OnSetup()
{ {
_targetIds = new int[targets.Length]; _markers = new ProfilerMarker[targets.Length];
for (int i = 0; i < targets.Length; i++) for (int i = 0; i < targets.Length; i++)
{ {
_targetIds[i] = EcsDebug.RegisterMark($"EcsRunner.{targets[i].GetType().Name}.{nameof(Run)}"); _markers[i] = new ProfilerMarker(EcsDebug.RegisterMark($"EcsRunner.{targets[i].GetType().Name}.{nameof(Run)}"));
} }
} }
#endif #endif
@ -115,17 +113,15 @@
public sealed class EcsDestroyRunner : EcsRunner<IEcsDestroySystem>, IEcsDestroySystem public sealed class EcsDestroyRunner : EcsRunner<IEcsDestroySystem>, IEcsDestroySystem
{ {
#if DEBUG #if DEBUG
private int[] _targetIds; private ProfilerMarker[] _markers;
#endif #endif
public void Destroy(EcsPipeline pipeline) public void Destroy(EcsPipeline pipeline)
{ {
#if DEBUG #if DEBUG
for (int i = 0; i < targets.Length; i++) for (int i = 0; i < targets.Length; i++)
{ {
int id = _targetIds[i]; using (_markers[i].Auto())
EcsDebug.ProfileMarkBegin(id); targets[i].Destroy(pipeline);
targets[i].Destroy(pipeline);
EcsDebug.ProfileMarkEnd(id);
} }
#else #else
foreach (var item in targets) item.Destroy(pipeline); foreach (var item in targets) item.Destroy(pipeline);
@ -135,10 +131,10 @@
#if DEBUG #if DEBUG
protected override void OnSetup() protected override void OnSetup()
{ {
_targetIds = new int[targets.Length]; _markers = new ProfilerMarker[targets.Length];
for (int i = 0; i < targets.Length; i++) for (int i = 0; i < targets.Length; i++)
{ {
_targetIds[i] = EcsDebug.RegisterMark($"EcsRunner.{targets[i].GetType().Name}.{nameof(Destroy)}"); _markers[i] = new ProfilerMarker(EcsDebug.RegisterMark($"EcsRunner.{targets[i].GetType().Name}.{nameof(Destroy)}"));
} }
} }
#endif #endif

View File

@ -5,6 +5,31 @@ using System.Runtime.CompilerServices;
namespace DCFApixels.DragonECS namespace DCFApixels.DragonECS
{ {
namespace Profile
{
public readonly struct ProfilerMarker
{
public readonly int id;
public ProfilerMarker(int id) => this.id = id;
public void Begin() => EcsDebug.ProfileMarkBegin(id);
public void End() => EcsDebug.ProfileMarkEnd(id);
public AutoScope Auto() => new AutoScope(id);
public readonly struct AutoScope : IDisposable
{
private readonly int id;
public AutoScope(int id)
{
this.id = id;
EcsDebug.ProfileMarkBegin(id);
}
public void Dispose()
{
EcsDebug.ProfileMarkEnd(id);
}
}
}
}
public static class EcsDebug public static class EcsDebug
{ {
public static void Set<T>() where T : DebugService, new() => DebugService.Set<T>(); public static void Set<T>() where T : DebugService, new() => DebugService.Set<T>();
@ -52,8 +77,8 @@ namespace DCFApixels.DragonECS
return 0; return 0;
#endif #endif
} }
} }
public abstract class DebugService public abstract class DebugService
{ {
private static DebugService _instance; private static DebugService _instance;