Skip to content

Commit

Permalink
#88 fix bug in system activation/deactivation, add&rename metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Oct 30, 2019
1 parent bb2475b commit c42c1bf
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 12 deletions.
5 changes: 3 additions & 2 deletions include/flecs/util/stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ typedef struct EcsSystemStats {
uint32_t tables_matched_count; /* Number of tables matched */
uint32_t entities_matched_count; /* Number of entities matched */
uint64_t invoke_count_total; /* Number of times system got invoked */
float seconds_total; /* Total time spent in system */
float seconds_total; /* Total time spent in system */
bool is_enabled; /* Is system enabled */
bool is_active; /* Is system active */
bool is_hidden; /* Is system hidden */
Expand All @@ -87,7 +87,7 @@ typedef struct EcsSystemStats {
/* Type statistics (only for named types, created with ECS_TYPE) */
typedef struct EcsTypeStats {
ecs_entity_t entity; /* Entity handle of type */
const char *id; /* Type name */
const char *name; /* Type name */
ecs_type_t type; /* Reference to type with nesting intact */
ecs_type_t normalized_type; /* Reference to normalized type */
uint32_t entities_count; /* Number of plain entities in type */
Expand All @@ -98,6 +98,7 @@ typedef struct EcsTypeStats {
uint32_t row_systems_count; /* Number of row (reactive) systems in type */
uint32_t enabled_systems_count; /* Number of enabled systems in type */
uint32_t active_systems_count; /* Number of active systems in type */
uint32_t instance_count; /* Number of instances of this type */
bool is_hidden; /* Is type hidden */
} EcsTypeStats;

Expand Down
7 changes: 3 additions & 4 deletions src/stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,6 @@ void StatsCollectSystemStats(ecs_rows_t *rows) {
stats[i].is_enabled = system[i].base.enabled;
stats[i].is_active = ecs_vector_count(system[i].tables);
stats[i].is_hidden = ecs_has(rows->world, entity, EcsHidden);

/* Reset time_spent between measuremen=t intervals */
system[i].base.time_spent = 0;
}
}

Expand Down Expand Up @@ -497,7 +494,8 @@ void StatsCollectTypeStats(ecs_rows_t *rows) {

uint32_t i;
for (i = 0; i < rows->count; i ++) {
stats[i].id = ecs_get_id(world, rows->entities[i]);
stats[i].name = ecs_get_id(world, rows->entities[i]);
stats[i].entity = rows->entities[i];
stats[i].type = type_component[i].type;
stats[i].normalized_type = type_component[i].resolved;
stats[i].is_hidden = ecs_has(world, rows->entities[i], EcsHidden);
Expand All @@ -509,6 +507,7 @@ void StatsCollectTypeStats(ecs_rows_t *rows) {
stats[i].row_systems_count = 0;
stats[i].enabled_systems_count = 0;
stats[i].active_systems_count = 0;
stats[i].instance_count = _ecs_count(world, type_component[i].resolved);

uint32_t j, count = ecs_vector_count(type_component[i].resolved);
ecs_entity_t *entities = ecs_vector_first(type_component[i].resolved);
Expand Down
3 changes: 2 additions & 1 deletion src/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,8 @@ void* get_shared_column(
rows->world, rows->references[-table_column - 1].component,
EcsComponent);
ecs_assert(cdata != NULL, ECS_INTERNAL_ERROR, NULL);
ecs_assert(cdata->size == size, ECS_COLUMN_TYPE_MISMATCH, NULL);
ecs_assert(cdata->size == size, ECS_COLUMN_TYPE_MISMATCH,
ecs_get_id(rows->world, rows->system));
}
#endif

Expand Down
28 changes: 27 additions & 1 deletion src/vector.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ void* ecs_vector_addn(

array->count = count;

ecs_assert(array->size >= array->count, ECS_INTERNAL_ERROR, NULL);

return ECS_OFFSET(ARRAY_BUFFER(array), element_size * old_count);
}

Expand All @@ -99,8 +101,10 @@ uint32_t ecs_vector_move_index(
{
void *dst_elem = ecs_vector_add(dst_array, params);
void *src_elem = ecs_vector_get(src_array, params, index);

memcpy(dst_elem, src_elem, params->element_size);

ecs_assert((*dst_array)->size >= (*dst_array)->count, ECS_INTERNAL_ERROR, NULL);

return ecs_vector_remove_index(src_array, params, index);
}

Expand Down Expand Up @@ -130,6 +134,8 @@ uint32_t ecs_vector_remove(
count --;
array->count = count;

ecs_assert(array->size >= array->count, ECS_INTERNAL_ERROR, NULL);

return count;
}

Expand Down Expand Up @@ -163,6 +169,8 @@ bool ecs_vector_pop(

ecs_vector_remove_last(array);

ecs_assert(array->size >= array->count, ECS_INTERNAL_ERROR, NULL);

return true;
}

Expand Down Expand Up @@ -190,6 +198,8 @@ uint32_t ecs_vector_remove_index(
count --;
array->count = count;

ecs_assert(array->size >= count, ECS_INTERNAL_ERROR, NULL);

return count;
}

Expand All @@ -208,6 +218,8 @@ void ecs_vector_reclaim(
array->size = size;
*array_inout = array;
}

ecs_assert(array->size >= array->count, ECS_INTERNAL_ERROR, NULL);
}

uint32_t ecs_vector_count(
Expand Down Expand Up @@ -249,6 +261,8 @@ uint32_t ecs_vector_set_size(
array->size = size;
*array_inout = array;
result = size;

ecs_assert(array->size >= array->count, ECS_INTERNAL_ERROR, NULL);
}

return result;
Expand All @@ -265,6 +279,9 @@ uint32_t ecs_vector_set_count(
}
(*array_inout)->count = count;
uint32_t size = ecs_vector_set_size(array_inout, params, count);

ecs_assert(size >= count, ECS_INTERNAL_ERROR, NULL);

return size;
}

Expand All @@ -286,6 +303,8 @@ void* ecs_vector_get(
uint32_t count = array->count;
uint32_t element_size = params->element_size;

ecs_assert(array->size >= array->count, ECS_INTERNAL_ERROR, NULL);

if (index >= count) {
return NULL;
}
Expand All @@ -299,6 +318,8 @@ uint32_t ecs_vector_get_index(
void *elem)
{
ecs_assert(elem != NULL, ECS_INVALID_PARAMETER, NULL);
ecs_assert(array->size >= array->count, ECS_INTERNAL_ERROR, NULL);

uint32_t element_size = params->element_size;
void *buffer = ARRAY_BUFFER(array);
return ((char*)elem - (char*)buffer) / element_size;
Expand All @@ -309,6 +330,7 @@ void* ecs_vector_last(
const ecs_vector_params_t *params)
{
if (array) {
ecs_assert(array->size >= array->count, ECS_INTERNAL_ERROR, NULL);
uint32_t count = array->count;
uint32_t element_size = params->element_size;
return ECS_OFFSET(ARRAY_BUFFER(array), element_size * (count - 1));
Expand All @@ -325,6 +347,7 @@ void ecs_vector_sort(
if (!array)
return;

ecs_assert(array->size >= array->count, ECS_INTERNAL_ERROR, NULL);
uint32_t count = array->count;
uint32_t element_size = params->element_size;
void *buffer = ARRAY_BUFFER(array);
Expand All @@ -341,6 +364,9 @@ void ecs_vector_memory(
uint32_t *used)
{
if (!array) return;

ecs_assert(array->size >= array->count, ECS_INTERNAL_ERROR, NULL);

if (allocd) {
*allocd += array->size * params->element_size + sizeof(ecs_vector_t);
}
Expand Down
13 changes: 9 additions & 4 deletions src/world.c
Original file line number Diff line number Diff line change
Expand Up @@ -349,16 +349,21 @@ void ecs_world_activate_system(
ecs_vector_move_index(
&dst_array, src_array, &handle_arr_params, i);

ecs_entity_t *to_sort;
uint32_t sort_count;

if (active) {
*ecs_system_array(world, kind) = dst_array;
qsort(dst_array, ecs_vector_count(dst_array) + 1,
sizeof(ecs_entity_t), compare_handle);
to_sort = ecs_vector_first(dst_array);
sort_count = ecs_vector_count(dst_array);
} else {
world->inactive_systems = dst_array;
qsort(src_array, ecs_vector_count(src_array) + 1,
sizeof(ecs_entity_t), compare_handle);
to_sort = ecs_vector_first(src_array);
sort_count = ecs_vector_count(src_array);
}

qsort(to_sort, sort_count, sizeof(ecs_entity_t), compare_handle);

/* Signal that system has been either activated or deactivated */
ecs_system_activate(world, system, active);

Expand Down

0 comments on commit c42c1bf

Please sign in to comment.