Debugging and diagnostics
Debug a running EasyEcs world: tables and memory with Describe, an entity's components in the debugger, per-system timing and the systems that failed.
An entity is an id, and its data is spread over table columns, so a debugger watch on a handle shows little by
itself, and a server that slows down does not say which system grew. EasyEcs answers both from data the world already
has: world.Describe() lists the tables with their rows and bytes, entity.Describe() (and the debugger) shows an
entity's component values, system.Stats times every system, and system.Error keeps the exception of a system that
failed.
When to use it
Use it for
An admin or health endpoint of a live server (entity counts, memory per table, the slowest system, the systems that failed), a log line when a tick runs long, a breakpoint on an entity in the IDE, and a memory report after a load test.
Skip it for
Game logic: a system that needs to know what an entity has asks with a filter or HasX, not with Describe. Every
tick: Describe allocates its answer, so call it on a request or a timer, not in the loop. Timing a change:
system.Stats shows trends on a live server; compare two versions with BenchmarkDotNet
(performance).
How to use it
Look at an entity
entity.Describe() returns the entity's components as ComponentValues: a boxed copy of each value, and Enabled
false for a disabled one. A stale handle describes as
nothing. In a debugger, an Entity shows as Entity 1000v1 (index and version) and expands to its id, whether it is
alive, and its components, read when you expand it.
var guard = world.CreateEntity(new Position(5, 5), new Health(50));
foreach (var component in guard.Describe())
Console.WriteLine(component.Enabled ? $"{component.Value}" : $"{component.Value} (disabled)");Console.WriteLine(entity) prints the same Entity 1000v1, and an EntityId prints as 1000v1: what to log, and
what a client sends back to name an entity.
Look at the tables
world.Describe() returns one TableInfo per table: its Columns (each a component Type, the rows Without it,
and its Group in a grouped world), its living Entities, the Rows in use, the
Capacity allocated and the Bytes that costs.
foreach (var table in world.Describe())
{
var names = new List<string>();
foreach (var column in table.Columns) names.Add(column.Type.Name);
Console.WriteLine($"[{string.Join(", ", names)}] {table.Entities} entities, {table.Bytes:N0} bytes");
}What to read in it:
- Where the memory is:
Bytesper table, which grows withCapacity, not withEntities: a table that once held a wave keeps its rows for the next one. - Removed components: a column's
Withoutcounts the rows that removed or disabled it, since removing keeps the row. A large share means most entities of the table no longer have it. - Combinations: each distinct set of components is a table. Hundreds of small tables mean the code adds many different combinations; a group splits tables on purpose.
- Holes:
RowsaboveEntitiesis a pass in progress that left holes; they close when it ends.
world.EntityCount is the total (O(1)), and world.Count<T>(filter) counts the entities a query would visit by
summing table counts.
Time each system
With WorldOptions.MeasureSystems, every run of a system (its OnUpdate and its [ForEach] methods) is timed:
system.Stats holds Runs, Last, Total and Average. world.Systems lists the systems without allocating, so
a status page can show the slowest one (measuring systems).
public class RegenSystem : EcsSystem
{
[ForEach]
static void Regen(ref Health health) => health.Value++;
}using var measured = new World(new WorldOptions { MeasureSystems = true });
measured.AddSystem<RegenSystem>();
measured.Update();
foreach (var system in measured.Systems)
Console.WriteLine($"{system.GetType().Name}: last {system.Stats.Last.TotalMicroseconds} μs");Find the systems that failed
By default an exception a system throws leaves Update(). With SystemErrors = SystemErrorPolicy.Disable, the
system is switched off instead, its exception stays in system.Error, and the update goes on: a failure becomes
data a health check reads. Setting Enabled = true clears Error and tries the system again
(errors).
using var guarded = new World(new WorldOptions { SystemErrors = SystemErrorPolicy.Disable });
guarded.AddSystem<RegenSystem>();
guarded.Update();
foreach (var system in guarded.Systems)
if (system.Error is { } error) Console.Error.WriteLine($"{system.GetType().Name} is off: {error.Message}");Which thread
Read diagnostics on the world's owner thread between two updates, like the hosting guide
publishes a status: the numbers then describe one finished tick. world.Describe() may also be called from another
thread for a rough picture (it never corrupts the world), but a table changed at the same time may be read half-way.
entity.Describe() and system.Stats belong to the owner thread.
Full example
A world of walkers and guards, with one buggy system, described after three updates. Put the files in a console
project that references EasyEcs (dotnet add package EasyEcs) and run it with dotnet run.
It prints:
1200 entities
[] 0 entities, 64 rows allocated, 512 bytes
[Position, Velocity (1 without)] 1000 entities, 1024 rows allocated, 24,840 bytes
[Position, Cooldown, Health] 200 entities, 256 rows allocated, 6,256 bytes
Entity 1000v1
Position { X = 5, Y = 5 }
Cooldown { Ticks = -1 }
Health { Value = 50 }
MoveSystem: 3 runs, on
CooldownSystem: 3 runs, off: a cooldown went below zeroThe walker that stopped stays in its table, counted in Velocity (1 without). The first table is the world's empty
table, where CreateEntity() without components puts an entity. The guard shows the value the buggy system left,
Cooldown { Ticks = -1 }, and CooldownSystem is off with its exception kept while MoveSystem goes on running:
the third update did not stop at the bug.
How to use it fast
| Instead of | Do | Why |
|---|---|---|
Describe() on every tick | On a request or a timer, between updates | It allocates its answer |
entity.Describe() to test for a component | HasX, TryGetX, a filter | No boxing, no allocation |
A stopwatch around world.Update() | MeasureSystems and system.Stats | Per system, two timestamp reads per run |
A try/catch around Update() on a live server | SystemErrorPolicy.Disable and system.Error | The other systems keep running |
Describing the battle server's world for a dashboard and one entity for a debugger (IntrospectionBenchmark):
| Case | Mean | vs baseline | Allocated |
|---|---|---|---|
| Tables | 10.6 μs | — | 25.0 KB |
| Entity | 244 ns | — | 832 B |
What it costs
world.Describe(): O(tables + columns); allocates oneTableInfoper table and its columns.entity.Describe(): O(components of the entity); boxes each value.MeasureSystems: two timestamp reads per system run when on; nothing when off.SystemErrorPolicy.Disable: nothing until a system throws.- The debugger view costs nothing at run time: it is read only when a debugger asks.
Pitfalls
| Mistake | What happens | Caught by |
|---|---|---|
Describe() in a system or every tick | Allocations on the hot path | Code review |
Reading Describe() from another thread during an update | Counts of a table caught half-way through a change | Read between updates on the owner thread |
Expecting Bytes to drop when entities leave | Tables keep their capacity for the next wave | Read Entities against Capacity |
Leaving SystemErrorPolicy.Disable on in tests | A failing system is silently off and the test checks other things | Keep the default Throw in tests (testing) |
Logging entity.Id.Index alone | The index is reused by later entities | Log the entity or its EntityId (1000v1) |
Related
Last updated on
How is this guide?
Testing systems
Test EasyEcs systems with a world per test: arrange entities and singletons, call Update, assert on data. Shared setup, errors, allocation budgets, saves.
Analyzer rules
Every EasyEcs analyzer rule, EECS002 to EECS034, with its severity, its code fix, the code it rejects and the code to write instead.