Skip to content

Commit

Permalink
#88 Collect world statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Oct 31, 2019
1 parent 18b436b commit 1de1a1c
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 18 deletions.
20 changes: 13 additions & 7 deletions include/flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,13 @@ typedef void (*ecs_module_init_action_t)(
ecs_world_t *world,
int flags);

#define ECS_INSTANCEOF ((ecs_entity_t)1 << 63)
#define ECS_CHILDOF ((ecs_entity_t)1 << 62)
#define ECS_ENTITY_FLAGS_MASK ((ecs_entity_t)(ECS_INSTANCEOF | ECS_CHILDOF))
#define ECS_ENTITY_MASK ((ecs_entity_t)~ECS_ENTITY_FLAGS_MASK)
#define ECS_SINGLETON ((ecs_entity_t)(ECS_ENTITY_MASK) - 1)
#define ECS_INVALID_ENTITY (0)

/** Handles to builtin components */
#define EEcsComponent (1)
#define EEcsTypeComponent (2)
Expand All @@ -209,6 +216,12 @@ typedef void (*ecs_module_init_action_t)(
#define EEcsDisabled (10)
#define EEcsOnDemand (11)

/* World entity */
#define EcsWorld (12)

/* Singleton entity */
#define EcsSingleton (ECS_SINGLETON)

/* Type handles to builtin components */
FLECS_EXPORT
extern ecs_type_t
Expand All @@ -224,13 +237,6 @@ extern ecs_type_t
TEcsDisabled,
TEcsOnDemand;

#define ECS_INSTANCEOF ((ecs_entity_t)1 << 63)
#define ECS_CHILDOF ((ecs_entity_t)1 << 62)
#define ECS_ENTITY_FLAGS_MASK ((ecs_entity_t)(ECS_INSTANCEOF | ECS_CHILDOF))
#define ECS_ENTITY_MASK ((ecs_entity_t)~ECS_ENTITY_FLAGS_MASK)
#define ECS_SINGLETON ((ecs_entity_t)(ECS_ENTITY_MASK) - 1)
#define ECS_INVALID_ENTITY (0)

/* This allows passing 0 as type to functions that accept types */
#define T0 (0)

Expand Down
3 changes: 2 additions & 1 deletion include/flecs/util/stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ typedef struct EcsMemoryStats {
} EcsMemoryStats;

typedef struct EcsWorldStats {
uint32_t system_count;
uint32_t table_count;
uint32_t component_count;
uint32_t entity_count;
Expand All @@ -63,6 +62,8 @@ typedef struct EcsWorldStats {
double system_time;
double frame_time;
double merge_time;
double world_time;
double target_fps;
} EcsWorldStats;

typedef struct ecs_world_stats_t {
Expand Down
62 changes: 57 additions & 5 deletions src/stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -500,9 +500,9 @@ void ecs_get_stats(
stats->memory.families.allocd += type_memory; */

stats->entity_count = ecs_map_count(world->main_stage.entity_index);
stats->tick_count = world->tick;
stats->tick_count = world->tick_count;

if (world->tick) {
if (world->tick_count) {
stats->frame_time = world->frame_time;
stats->system_time = world->system_time;
stats->merge_time = world->merge_time;
Expand All @@ -514,7 +514,7 @@ void ecs_get_stats(
stats->frame_profiling = world->measure_frame_time;
stats->system_profiling = world->measure_system_time;

world->tick = 0;
world->tick_count = 0;
world->frame_time = 0;
world->system_time = 0;
}
Expand Down Expand Up @@ -548,6 +548,13 @@ void ecs_free_stats(

/* ---- */

static
void AddWorldStats(ecs_rows_t *rows) {
ECS_COLUMN_COMPONENT(rows, EcsWorldStats, 1);

ecs_add(rows->world, EcsWorld, EcsWorldStats);
}

static
void AddSystemStats(ecs_rows_t *rows) {
ECS_COLUMN_COMPONENT(rows, EcsSystemStats, 2);
Expand All @@ -568,6 +575,42 @@ void AddComponentStats(ecs_rows_t *rows) {
}
}

static
void CollectWorldStats_StatusAction(
ecs_world_t *world,
ecs_entity_t system,
ecs_system_status_t status,
void *ctx)
{
if (status == EcsSystemActivated) {
ecs_measure_frame_time(world, true);
} else if (status == EcsSystemDeactivated) {
ecs_measure_frame_time(world, false);
}
}

static
void CollectWorldStats(ecs_rows_t *rows) {
ECS_COLUMN(rows, EcsWorldStats, stats, 1);

ecs_world_t *world = rows->world;

stats->entity_count = ecs_map_count(world->main_stage.entity_index);
stats->table_count = ecs_chunked_count(world->main_stage.tables);
stats->component_count = ecs_count(world, EcsComponent);
stats->thread_count = ecs_vector_count(world->worker_threads);
stats->frame_time = world->frame_time;
stats->system_time = world->system_time;
stats->merge_time = world->merge_time;
stats->world_time = world->world_time;
stats->target_fps = world->target_fps;
stats->tick_count = world->tick_count;

world->frame_time = 0;
world->system_time = 0;
world->merge_time = 0;
}

static
void CollectSystemStats_StatusAction(
ecs_world_t *world,
Expand Down Expand Up @@ -685,15 +728,24 @@ void FlecsStatsImport(
ECS_COMPONENT(world, EcsWorldStats);
ECS_TAG(world, EcsStatsSkipCollect);

ECS_SYSTEM(world, AddWorldStats, EcsOnStore, [out] !EcsWorld.EcsWorldStats,
SYSTEM.EcsOnDemand, SYSTEM.EcsHidden);

ECS_SYSTEM(world, AddSystemStats, EcsOnStore,
EcsColSystem, [out] !EcsSystemStats,
SYSTEM.EcsOnDemand, SYSTEM.EcsHidden,
SYSTEM.EcsStatsSkipCollect, !EcsStatsSkipCollect);

ECS_SYSTEM(world, AddComponentStats, EcsOnStore,
EcsComponent, [out] !EcsComponentStats,
SYSTEM.EcsOnDemand, SYSTEM.EcsHidden,
SYSTEM.EcsStatsSkipCollect, !EcsStatsSkipCollect);
SYSTEM.EcsOnDemand, SYSTEM.EcsHidden);

ECS_SYSTEM(world, CollectWorldStats, EcsPostLoad,
[out] EcsWorldStats,
SYSTEM.EcsOnDemand, SYSTEM.EcsHidden);

ecs_set_system_status_action(
world, CollectWorldStats, CollectWorldStats_StatusAction, NULL);

ECS_SYSTEM(world, CollectSystemStats, EcsPostLoad,
EcsColSystem, [out] EcsSystemStats,
Expand Down
2 changes: 1 addition & 1 deletion src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ struct ecs_world {

/* -- Time management -- */

uint32_t tick; /* Number of computed frames by world */
uint32_t tick_count; /* Number of computed frames by world */
ecs_time_t frame_start; /* Starting timestamp of frame */
float frame_time; /* Time spent processing a frame */
float system_time; /* Time spent processing systems */
Expand Down
11 changes: 7 additions & 4 deletions src/world.c
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ ecs_world_t *ecs_init(void) {
world->system_time = 0;
world->target_fps = 0;
world->fps_sleep = 0;
world->tick = 0;
world->tick_count = 0;

world->context = NULL;

Expand Down Expand Up @@ -719,7 +719,10 @@ ecs_world_t *ecs_init(void) {
bootstrap_component(world, table, EEcsDisabled, ECS_DISABLED_ID, 0);
bootstrap_component(world, table, EEcsOnDemand, ECS_ON_DEMAND_ID, 0);

world->last_handle = EEcsOnDemand + 1;
/* Initialize EcsWorld */
ecs_set(world, EcsWorld, EcsId, {"EcsWorld"});

world->last_handle = EcsWorld + 1;
world->min_handle = 0;
world->max_handle = 0;

Expand Down Expand Up @@ -1208,7 +1211,7 @@ bool ecs_progress(

/* -- System execution stops here -- */

world->tick ++;
world->tick_count ++;

stop_measure_frame(world, delta_time);

Expand Down Expand Up @@ -1352,7 +1355,7 @@ uint32_t ecs_get_tick(
ecs_world_t *world)
{
ecs_get_stage(&world);
return world->tick;
return world->tick_count;
}

void ecs_set_context(
Expand Down

0 comments on commit 1de1a1c

Please sign in to comment.