@@ -25308,6 +25308,9 @@ struct ecs_pipeline_state_t {
2530825308 ecs_vec_t ops; /* Pipeline schedule */
2530925309 ecs_vec_t systems; /* Vector with system ids */
2531025310
25311+ ecs_vec_t phase_offsets; /* Vector of offsets into phase_names (for perf tracing) */
25312+ ecs_vec_t phase_names; /* Vector with phase names (for perf tracing) */
25313+
2531125314 ecs_entity_t last_system; /* Last system ran by pipeline */
2531225315 ecs_id_record_t *idr_inactive; /* Cached record for quick inactive test */
2531325316 int32_t match_count; /* Used to track of rebuild is necessary */
@@ -52630,6 +52633,17 @@ static void flecs_pipeline_free(
5263052633 ecs_allocator_t *a = &world->allocator;
5263152634 ecs_vec_fini_t(a, &p->ops, ecs_pipeline_op_t);
5263252635 ecs_vec_fini_t(a, &p->systems, ecs_entity_t);
52636+
52637+ #ifdef FLECS_PERF_TRACE
52638+ ecs_vec_fini_t(a, &p->phase_offsets, int32_t);
52639+ int32_t i, count = ecs_vec_count(&p->phase_names);
52640+ const char** phase_names = ecs_vec_first_t(&p->phase_names, const char*);
52641+ for (i = 0; i < count; i ++) {
52642+ ecs_os_free(ECS_CONST_CAST(char*, phase_names[i]));
52643+ }
52644+ ecs_vec_fini_t(a, &p->phase_names, const char*);
52645+ #endif
52646+
5263352647 ecs_os_free(p->iters);
5263452648 ecs_query_fini(p->query);
5263552649 ecs_os_free(p);
@@ -52890,6 +52904,14 @@ bool flecs_pipeline_build(
5289052904 ecs_vec_reset_t(a, &pq->ops, ecs_pipeline_op_t);
5289152905 ecs_vec_reset_t(a, &pq->systems, ecs_entity_t);
5289252906
52907+ #ifdef FLECS_PERF_TRACE
52908+ ecs_vec_reset_t(a, &pq->phase_offsets, int32_t);
52909+ ecs_vec_reset_t(a, &pq->phase_names, const char*);
52910+ /* Local map for building up phase_offsets & phase_names */
52911+ ecs_map_t phase_offset_map;
52912+ ecs_map_init(&phase_offset_map, a);
52913+ #endif
52914+
5289352915 bool multi_threaded = false;
5289452916 bool immediate = false;
5289552917 bool first = true;
@@ -52900,6 +52922,23 @@ bool flecs_pipeline_build(
5290052922 bool is_active = ecs_table_get_type_index(
5290152923 world, it.table, EcsEmpty) == -1;
5290252924
52925+ #ifdef FLECS_PERF_TRACE
52926+ ecs_entity_t phase = ecs_field_src(&it, 1);
52927+
52928+ ecs_map_val_t* phase_offset_p = ecs_map_get(&phase_offset_map, phase);
52929+ int32_t phase_offset = 0;
52930+ if (!phase_offset_p) {
52931+ /* New phase, record its name into the name vector */
52932+ phase_offset = ecs_vec_count(&pq->phase_names);
52933+ const char* phase_name = ecs_get_path(world, phase);
52934+
52935+ ecs_map_insert(&phase_offset_map, phase, (int64_t)(phase_offset));
52936+ ecs_vec_append_t(a, &pq->phase_names, const char*)[0] = phase_name;
52937+ } else {
52938+ phase_offset = (int64_t)*phase_offset_p;
52939+ }
52940+ #endif
52941+
5290352942 int32_t i;
5290452943 for (i = 0; i < it.count; i ++) {
5290552944 flecs_poly_assert(poly[i].poly, ecs_system_t);
@@ -52973,6 +53012,12 @@ bool flecs_pipeline_build(
5297353012 if (is_active) {
5297453013 ecs_vec_append_t(a, &pq->systems, ecs_entity_t)[0] =
5297553014 it.entities[i];
53015+
53016+ #ifdef FLECS_PERF_TRACE
53017+ /* Each system in the systems vector has a corresponding phase offset */
53018+ ecs_vec_append_t(a, &pq->phase_offsets, int32_t)[0] = phase_offset;
53019+ #endif
53020+
5297653021 if (!op->count) {
5297753022 op->multi_threaded = multi_threaded;
5297853023 op->immediate = immediate;
@@ -52989,6 +53034,10 @@ bool flecs_pipeline_build(
5298953034 ecs_map_fini(&ws.ids);
5299053035 ecs_map_fini(&ws.wildcard_ids);
5299153036
53037+ #ifdef FLECS_PERF_TRACE
53038+ ecs_map_fini(&phase_offset_map);
53039+ #endif
53040+
5299253041 op = ecs_vec_first_t(&pq->ops, ecs_pipeline_op_t);
5299353042
5299453043 if (!op) {
@@ -53166,7 +53215,27 @@ int32_t flecs_run_pipeline_ops(
5316653215 ecs_entity_t* systems = ecs_vec_first_t(&pq->systems, ecs_entity_t);
5316753216 int32_t ran_since_merge = i - op->offset;
5316853217
53218+ #ifdef FLECS_PERF_TRACE
53219+ int32_t* phase_offsets = ecs_vec_first_t(&pq->phase_offsets, int32_t);
53220+ const char** phase_names = ecs_vec_first_t(&pq->phase_names, const char*);
53221+ #endif
53222+
5316953223 for (; i < count; i++) {
53224+ #ifdef FLECS_PERF_TRACE
53225+ if (i > 0) {
53226+ int32_t phase = phase_offsets[i];
53227+ int32_t last_phase = phase_offsets[i - 1];
53228+
53229+ if (phase != last_phase) {
53230+ /* Close the span of the previous phase and open the current one.
53231+ * The first/last phases are handled in flecs_run_pipeline because
53232+ * this function may run multiple times during one pipeline. */
53233+ ecs_os_perf_trace_pop(phase_names[last_phase]);
53234+ ecs_os_perf_trace_push(phase_names[phase]);
53235+ }
53236+ }
53237+ #endif
53238+
5317053239 ecs_entity_t system = systems[i];
5317153240 const EcsPoly* poly = ecs_get_pair(world, system, EcsPoly, EcsSystem);
5317253241 flecs_poly_assert(poly->poly, ecs_system_t);
@@ -53225,6 +53294,17 @@ void flecs_run_pipeline(
5322553294 // Update the pipeline before waking the workers.
5322653295 flecs_pipeline_update(world, pq, true);
5322753296
53297+ #ifdef FLECS_PERF_TRACE
53298+ int32_t* phase_offsets = ecs_vec_first_t(&pq->phase_offsets, int32_t);
53299+ const char** phase_names = ecs_vec_first_t(&pq->phase_names, const char*);
53300+
53301+ if (phase_offsets && phase_names) {
53302+ /* Open the span of the first phase in the pipeline.
53303+ * Intermediate phases are handled in flecs_run_pipeline_ops. */
53304+ ecs_os_perf_trace_push(phase_names[phase_offsets[0]]);
53305+ }
53306+ #endif
53307+
5322853308 // If there are no operations to execute in the pipeline bail early,
5322953309 // no need to wake the workers since they have nothing to do.
5323053310 while (pq->cur_op != NULL) {
@@ -53291,6 +53371,14 @@ void flecs_run_pipeline(
5329153371
5329253372 flecs_pipeline_update(world, pq, false);
5329353373 }
53374+
53375+ #ifdef FLECS_PERF_TRACE
53376+ if (phase_offsets && phase_names) {
53377+ int32_t last_phase_offset = ecs_vec_last_t(&pq->phase_offsets, int32_t)[0];
53378+ /* Close the span of the first phase in the pipeline */
53379+ ecs_os_perf_trace_pop(phase_names[last_phase_offset]);
53380+ }
53381+ #endif
5329453382}
5329553383
5329653384static
0 commit comments