Skip to content

Commit f7b355a

Browse files
committed
Add world summary component to monitor addon
1 parent 063e9f1 commit f7b355a

File tree

5 files changed

+115
-1
lines changed

5 files changed

+115
-1
lines changed

docs/Queries.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Here are some of the highlights of Flecs queries:
2626

2727
- Queries can be created with the regular API or by parsing a query string, making it possible to [create tools that create queries at runtime](https://www.flecs.dev/explorer/).
2828

29-
- Queries support [component inheritance](/examples/cpp/rules/component_inheritance).
29+
- Queries support [component inheritance](https://github.com/SanderMertens/flecs/tree/master/examples/cpp/rules/component_inheritance).
3030

3131
## Definitions
3232

flecs.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18234,6 +18234,7 @@ void FlecsPipelineImport(
1823418234

1823518235
ECS_COMPONENT_DECLARE(FlecsMonitor);
1823618236
ECS_COMPONENT_DECLARE(EcsWorldStats);
18237+
ECS_COMPONENT_DECLARE(EcsWorldSummary);
1823718238
ECS_COMPONENT_DECLARE(EcsPipelineStats);
1823818239

1823918240
ecs_entity_t EcsPeriod1s = 0;
@@ -18263,6 +18264,26 @@ ECS_DTOR(EcsPipelineStats, ptr, {
1826318264
ecs_pipeline_stats_fini(&ptr->stats);
1826418265
})
1826518266

18267+
static
18268+
void UpdateWorldSummary(ecs_iter_t *it) {
18269+
EcsWorldSummary *summary = ecs_field(it, EcsWorldSummary, 1);
18270+
18271+
const ecs_world_info_t *info = ecs_get_world_info(it->world);
18272+
18273+
int32_t i, count = it->count;
18274+
for (i = 0; i < count; i ++) {
18275+
summary[i].target_fps = (double)info->target_fps;
18276+
18277+
summary[i].frame_time_last = (double)info->frame_time_total - summary[i].frame_time_total;
18278+
summary[i].system_time_last = (double)info->system_time_total - summary[i].system_time_total;
18279+
summary[i].merge_time_last = (double)info->merge_time_total - summary[i].merge_time_total;
18280+
18281+
summary[i].frame_time_total = (double)info->frame_time_total;
18282+
summary[i].system_time_total = (double)info->system_time_total;
18283+
summary[i].merge_time_total = (double)info->merge_time_total;
18284+
}
18285+
}
18286+
1826618287
static
1826718288
void MonitorStats(ecs_iter_t *it) {
1826818289
ecs_world_t *world = it->real_world;
@@ -18526,6 +18547,26 @@ void FlecsMonitorImport(
1852618547
EcsPeriod1d = ecs_new_entity(world, "EcsPeriod1d");
1852718548
EcsPeriod1w = ecs_new_entity(world, "EcsPeriod1w");
1852818549

18550+
ECS_COMPONENT_DEFINE(world, EcsWorldSummary);
18551+
18552+
#ifdef FLECS_META
18553+
ecs_struct(world, {
18554+
.entity = ecs_id(EcsWorldSummary),
18555+
.members = {
18556+
{ .name = "target_fps", .type = ecs_id(ecs_f64_t), .unit = EcsHertz },
18557+
{ .name = "frame_time_total", .type = ecs_id(ecs_f64_t), .unit = EcsSeconds },
18558+
{ .name = "system_time_total", .type = ecs_id(ecs_f64_t), .unit = EcsSeconds },
18559+
{ .name = "merge_time_total", .type = ecs_id(ecs_f64_t), .unit = EcsSeconds },
18560+
{ .name = "frame_time_last", .type = ecs_id(ecs_f64_t), .unit = EcsSeconds },
18561+
{ .name = "system_time_last", .type = ecs_id(ecs_f64_t), .unit = EcsSeconds },
18562+
{ .name = "merge_time_last", .type = ecs_id(ecs_f64_t), .unit = EcsSeconds }
18563+
}
18564+
});
18565+
#endif
18566+
18567+
ECS_SYSTEM(world, UpdateWorldSummary, EcsPreFrame, EcsWorldSummary);
18568+
ecs_set(world, EcsWorld, EcsWorldSummary, {0});
18569+
1852918570
flecs_world_monitor_import(world);
1853018571
flecs_pipeline_monitor_import(world);
1853118572

flecs.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11780,6 +11780,7 @@ extern "C" {
1178011780

1178111781
FLECS_API extern ECS_COMPONENT_DECLARE(FlecsMonitor);
1178211782
FLECS_API extern ECS_COMPONENT_DECLARE(EcsWorldStats);
11783+
FLECS_API extern ECS_COMPONENT_DECLARE(EcsWorldSummary);
1178311784
FLECS_API extern ECS_COMPONENT_DECLARE(EcsPipelineStats);
1178411785

1178511786
FLECS_API extern ecs_entity_t EcsPeriod1s;
@@ -11803,6 +11804,21 @@ typedef struct {
1180311804
ecs_pipeline_stats_t stats;
1180411805
} EcsPipelineStats;
1180511806

11807+
typedef struct {
11808+
/* Target FPS */
11809+
double target_fps; /**< Target FPS */
11810+
11811+
/* Total time */
11812+
double frame_time_total; /**< Total time spent processing a frame */
11813+
double system_time_total; /**< Total time spent in systems */
11814+
double merge_time_total; /**< Total time spent in merges */
11815+
11816+
/* Last frame time */
11817+
double frame_time_last; /**< Time spent processing a frame */
11818+
double system_time_last; /**< Time spent in systems */
11819+
double merge_time_last; /**< Time spent in merges */
11820+
} EcsWorldSummary;
11821+
1180611822
/* Module import */
1180711823
FLECS_API
1180811824
void FlecsMonitorImport(

include/flecs/addons/monitor.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ extern "C" {
3333

3434
FLECS_API extern ECS_COMPONENT_DECLARE(FlecsMonitor);
3535
FLECS_API extern ECS_COMPONENT_DECLARE(EcsWorldStats);
36+
FLECS_API extern ECS_COMPONENT_DECLARE(EcsWorldSummary);
3637
FLECS_API extern ECS_COMPONENT_DECLARE(EcsPipelineStats);
3738

3839
FLECS_API extern ecs_entity_t EcsPeriod1s;
@@ -56,6 +57,21 @@ typedef struct {
5657
ecs_pipeline_stats_t stats;
5758
} EcsPipelineStats;
5859

60+
typedef struct {
61+
/* Target FPS */
62+
double target_fps; /**< Target FPS */
63+
64+
/* Total time */
65+
double frame_time_total; /**< Total time spent processing a frame */
66+
double system_time_total; /**< Total time spent in systems */
67+
double merge_time_total; /**< Total time spent in merges */
68+
69+
/* Last frame time */
70+
double frame_time_last; /**< Time spent processing a frame */
71+
double system_time_last; /**< Time spent in systems */
72+
double merge_time_last; /**< Time spent in merges */
73+
} EcsWorldSummary;
74+
5975
/* Module import */
6076
FLECS_API
6177
void FlecsMonitorImport(

src/addons/monitor.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
ECS_COMPONENT_DECLARE(FlecsMonitor);
1212
ECS_COMPONENT_DECLARE(EcsWorldStats);
13+
ECS_COMPONENT_DECLARE(EcsWorldSummary);
1314
ECS_COMPONENT_DECLARE(EcsPipelineStats);
1415

1516
ecs_entity_t EcsPeriod1s = 0;
@@ -39,6 +40,26 @@ ECS_DTOR(EcsPipelineStats, ptr, {
3940
ecs_pipeline_stats_fini(&ptr->stats);
4041
})
4142

43+
static
44+
void UpdateWorldSummary(ecs_iter_t *it) {
45+
EcsWorldSummary *summary = ecs_field(it, EcsWorldSummary, 1);
46+
47+
const ecs_world_info_t *info = ecs_get_world_info(it->world);
48+
49+
int32_t i, count = it->count;
50+
for (i = 0; i < count; i ++) {
51+
summary[i].target_fps = (double)info->target_fps;
52+
53+
summary[i].frame_time_last = (double)info->frame_time_total - summary[i].frame_time_total;
54+
summary[i].system_time_last = (double)info->system_time_total - summary[i].system_time_total;
55+
summary[i].merge_time_last = (double)info->merge_time_total - summary[i].merge_time_total;
56+
57+
summary[i].frame_time_total = (double)info->frame_time_total;
58+
summary[i].system_time_total = (double)info->system_time_total;
59+
summary[i].merge_time_total = (double)info->merge_time_total;
60+
}
61+
}
62+
4263
static
4364
void MonitorStats(ecs_iter_t *it) {
4465
ecs_world_t *world = it->real_world;
@@ -302,6 +323,26 @@ void FlecsMonitorImport(
302323
EcsPeriod1d = ecs_new_entity(world, "EcsPeriod1d");
303324
EcsPeriod1w = ecs_new_entity(world, "EcsPeriod1w");
304325

326+
ECS_COMPONENT_DEFINE(world, EcsWorldSummary);
327+
328+
#ifdef FLECS_META
329+
ecs_struct(world, {
330+
.entity = ecs_id(EcsWorldSummary),
331+
.members = {
332+
{ .name = "target_fps", .type = ecs_id(ecs_f64_t), .unit = EcsHertz },
333+
{ .name = "frame_time_total", .type = ecs_id(ecs_f64_t), .unit = EcsSeconds },
334+
{ .name = "system_time_total", .type = ecs_id(ecs_f64_t), .unit = EcsSeconds },
335+
{ .name = "merge_time_total", .type = ecs_id(ecs_f64_t), .unit = EcsSeconds },
336+
{ .name = "frame_time_last", .type = ecs_id(ecs_f64_t), .unit = EcsSeconds },
337+
{ .name = "system_time_last", .type = ecs_id(ecs_f64_t), .unit = EcsSeconds },
338+
{ .name = "merge_time_last", .type = ecs_id(ecs_f64_t), .unit = EcsSeconds }
339+
}
340+
});
341+
#endif
342+
343+
ECS_SYSTEM(world, UpdateWorldSummary, EcsPreFrame, EcsWorldSummary);
344+
ecs_set(world, EcsWorld, EcsWorldSummary, {0});
345+
305346
flecs_world_monitor_import(world);
306347
flecs_pipeline_monitor_import(world);
307348

0 commit comments

Comments
 (0)