Skip to content

Commit

Permalink
Fix crash when fetching WorldSummary before it's initialized
Browse files Browse the repository at this point in the history
Summary: This upstreams a fix for a crash that happens when an explorer client sends a request before the first time the world summary system has ran.

Reviewed By: Jason-M-Fugate

Differential Revision: D62050988
  • Loading branch information
SanderMertens committed Sep 6, 2024
1 parent d52b0f9 commit 3c8a2f0
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 82 deletions.
66 changes: 39 additions & 27 deletions distr/flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -62384,39 +62384,47 @@ void FlecsWorldMonitorImport(

ECS_COMPONENT_DECLARE(EcsWorldSummary);

static
void UpdateWorldSummary(ecs_iter_t *it) {
EcsWorldSummary *summary = ecs_field(it, EcsWorldSummary, 0);
static
void flecs_copy_world_summary(
ecs_world_t *world,
EcsWorldSummary *dst)
{
const ecs_world_info_t *info = ecs_get_world_info(world);

const ecs_world_info_t *info = ecs_get_world_info(it->world);
dst->target_fps = (double)info->target_fps;
dst->time_scale = (double)info->time_scale;

int32_t i, count = it->count;
for (i = 0; i < count; i ++) {
summary[i].target_fps = (double)info->target_fps;
summary[i].time_scale = (double)info->time_scale;
dst->frame_time_last = (double)info->frame_time_total - dst->frame_time_total;
dst->system_time_last = (double)info->system_time_total - dst->system_time_total;
dst->merge_time_last = (double)info->merge_time_total - dst->merge_time_total;

summary[i].frame_time_last = (double)info->frame_time_total - summary[i].frame_time_total;
summary[i].system_time_last = (double)info->system_time_total - summary[i].system_time_total;
summary[i].merge_time_last = (double)info->merge_time_total - summary[i].merge_time_total;
dst->frame_time_total = (double)info->frame_time_total;
dst->system_time_total = (double)info->system_time_total;
dst->merge_time_total = (double)info->merge_time_total;

summary[i].frame_time_total = (double)info->frame_time_total;
summary[i].system_time_total = (double)info->system_time_total;
summary[i].merge_time_total = (double)info->merge_time_total;
dst->frame_count ++;
dst->command_count +=
info->cmd.add_count +
info->cmd.remove_count +
info->cmd.delete_count +
info->cmd.clear_count +
info->cmd.set_count +
info->cmd.ensure_count +
info->cmd.modified_count +
info->cmd.discard_count +
info->cmd.event_count +
info->cmd.other_count;

summary[i].frame_count ++;
summary[i].command_count +=
info->cmd.add_count +
info->cmd.remove_count +
info->cmd.delete_count +
info->cmd.clear_count +
info->cmd.set_count +
info->cmd.ensure_count +
info->cmd.modified_count +
info->cmd.discard_count +
info->cmd.event_count +
info->cmd.other_count;
dst->build_info = *ecs_get_build_info();
}

static
void UpdateWorldSummary(ecs_iter_t *it) {
EcsWorldSummary *summary = ecs_field(it, EcsWorldSummary, 0);

summary[i].build_info = *ecs_get_build_info();
int32_t i, count = it->count;
for (i = 0; i < count; i ++) {
flecs_copy_world_summary(it->world, &summary[i]);
}
}

Expand Down Expand Up @@ -62479,6 +62487,10 @@ void FlecsWorldSummaryImport(
.target_fps = (double)info->target_fps,
.time_scale = (double)info->time_scale
});

EcsWorldSummary *summary = ecs_ensure(world, EcsWorld, EcsWorldSummary);
flecs_copy_world_summary(world, summary);
ecs_modified(world, EcsWorld, EcsWorldSummary);
}

#endif
Expand Down
66 changes: 39 additions & 27 deletions src/addons/stats/world_summary.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,47 @@

ECS_COMPONENT_DECLARE(EcsWorldSummary);

static
void flecs_copy_world_summary(
ecs_world_t *world,
EcsWorldSummary *dst)
{
const ecs_world_info_t *info = ecs_get_world_info(world);

dst->target_fps = (double)info->target_fps;
dst->time_scale = (double)info->time_scale;

dst->frame_time_last = (double)info->frame_time_total - dst->frame_time_total;
dst->system_time_last = (double)info->system_time_total - dst->system_time_total;
dst->merge_time_last = (double)info->merge_time_total - dst->merge_time_total;

dst->frame_time_total = (double)info->frame_time_total;
dst->system_time_total = (double)info->system_time_total;
dst->merge_time_total = (double)info->merge_time_total;

dst->frame_count ++;
dst->command_count +=
info->cmd.add_count +
info->cmd.remove_count +
info->cmd.delete_count +
info->cmd.clear_count +
info->cmd.set_count +
info->cmd.ensure_count +
info->cmd.modified_count +
info->cmd.discard_count +
info->cmd.event_count +
info->cmd.other_count;

dst->build_info = *ecs_get_build_info();
}

static
void UpdateWorldSummary(ecs_iter_t *it) {
EcsWorldSummary *summary = ecs_field(it, EcsWorldSummary, 0);

const ecs_world_info_t *info = ecs_get_world_info(it->world);

int32_t i, count = it->count;
for (i = 0; i < count; i ++) {
summary[i].target_fps = (double)info->target_fps;
summary[i].time_scale = (double)info->time_scale;

summary[i].frame_time_last = (double)info->frame_time_total - summary[i].frame_time_total;
summary[i].system_time_last = (double)info->system_time_total - summary[i].system_time_total;
summary[i].merge_time_last = (double)info->merge_time_total - summary[i].merge_time_total;

summary[i].frame_time_total = (double)info->frame_time_total;
summary[i].system_time_total = (double)info->system_time_total;
summary[i].merge_time_total = (double)info->merge_time_total;

summary[i].frame_count ++;
summary[i].command_count +=
info->cmd.add_count +
info->cmd.remove_count +
info->cmd.delete_count +
info->cmd.clear_count +
info->cmd.set_count +
info->cmd.ensure_count +
info->cmd.modified_count +
info->cmd.discard_count +
info->cmd.event_count +
info->cmd.other_count;

summary[i].build_info = *ecs_get_build_info();
flecs_copy_world_summary(it->world, &summary[i]);
}
}

Expand Down Expand Up @@ -105,6 +113,10 @@ void FlecsWorldSummaryImport(
.target_fps = (double)info->target_fps,
.time_scale = (double)info->time_scale
});

EcsWorldSummary *summary = ecs_ensure(world, EcsWorld, EcsWorldSummary);
flecs_copy_world_summary(world, summary);
ecs_modified(world, EcsWorld, EcsWorldSummary);
}

#endif
50 changes: 25 additions & 25 deletions test/addons/include/addons/bake_config.h
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
/*
)
(.)
.|.
| |
_.--| |--._
.-'; ;`-'& ; `&.
\ & ; & &_/
|"""---...---"""|
\ | | | | | | | /
`---.|.|.|.---'
* This file is generated by bake.lang.c for your convenience. Headers of
* dependencies will automatically show up in this file. Include bake_config.h
* in your main project file. Do not edit! */

#ifndef ADDONS_BAKE_CONFIG_H
#define ADDONS_BAKE_CONFIG_H

/* Headers of public dependencies */
#include <flecs.h>
#include <bake_test.h>

#endif

/*
)
(.)
.|.
| |
_.--| |--._
.-'; ;`-'& ; `&.
\ & ; & &_/
|"""---...---"""|
\ | | | | | | | /
`---.|.|.|.---'
* This file is generated by bake.lang.c for your convenience. Headers of
* dependencies will automatically show up in this file. Include bake_config.h
* in your main project file. Do not edit! */

#ifndef ADDONS_BAKE_CONFIG_H
#define ADDONS_BAKE_CONFIG_H

/* Headers of public dependencies */
#include <flecs.h>
#include <bake_test.h>

#endif
5 changes: 3 additions & 2 deletions test/addons/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
"invalid_entity_id",
"invalid_null_string",
"invalid_empty_string",
"invalid_empty_string_w_space",
"invalid_empty_string_w_space",
"redefine_row_system",
"system_w_or_prefab",
"system_w_or_disabled",
Expand Down Expand Up @@ -499,7 +499,8 @@
"request_commands_garbage_collect",
"script_error",
"import_rest_after_mini",
"get_pipeline_stats_after_delete_system"
"get_pipeline_stats_after_delete_system",
"request_world_summary_before_monitor_sys_run"
]
}, {
"id": "Metrics",
Expand Down
22 changes: 22 additions & 0 deletions test/addons/src/Rest.c
Original file line number Diff line number Diff line change
Expand Up @@ -542,3 +542,25 @@ void Rest_get_pipeline_stats_after_delete_system(void) {

ecs_fini(world);
}

void Rest_request_world_summary_before_monitor_sys_run(void) {
ecs_world_t *world = ecs_init();

ECS_IMPORT(world, FlecsStats);

ecs_http_server_t *srv = ecs_rest_server_init(world, NULL);
test_assert(srv != NULL);

ecs_http_reply_t reply = ECS_HTTP_REPLY_INIT;
test_int(0, ecs_http_server_request(srv, "GET",
"/entity/flecs/core/World?values=true", &reply));
test_int(reply.code, 200);

char *reply_str = ecs_strbuf_get(&reply.body);
test_assert(reply_str != NULL);
ecs_os_free(reply_str);

ecs_rest_server_fini(srv);

ecs_fini(world);
}
7 changes: 6 additions & 1 deletion test/addons/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ void Rest_request_commands_garbage_collect(void);
void Rest_script_error(void);
void Rest_import_rest_after_mini(void);
void Rest_get_pipeline_stats_after_delete_system(void);
void Rest_request_world_summary_before_monitor_sys_run(void);

// Testsuite 'Metrics'
void Metrics_member_gauge_1_entity(void);
Expand Down Expand Up @@ -2183,6 +2184,10 @@ bake_test_case Rest_testcases[] = {
{
"get_pipeline_stats_after_delete_system",
Rest_get_pipeline_stats_after_delete_system
},
{
"request_world_summary_before_monitor_sys_run",
Rest_request_world_summary_before_monitor_sys_run
}
};

Expand Down Expand Up @@ -2635,7 +2640,7 @@ static bake_test_suite suites[] = {
"Rest",
NULL,
NULL,
16,
17,
Rest_testcases
},
{
Expand Down

0 comments on commit 3c8a2f0

Please sign in to comment.