From f2c04ca49918adb687d034d33bf929320c208981 Mon Sep 17 00:00:00 2001 From: James Moschou Date: Sat, 17 Jan 2026 18:28:25 +0100 Subject: [PATCH 1/6] Update tracing types --- Sources/ComputeCxx/Graph/AGGraph.cpp | 14 +- Sources/ComputeCxx/Graph/Graph.cpp | 14 +- Sources/ComputeCxx/Graph/Graph.h | 10 +- Sources/ComputeCxx/Graph/TraceRecorder.cpp | 6 +- Sources/ComputeCxx/Graph/TraceRecorder.h | 4 +- Sources/ComputeCxx/Trace/ExternalTrace.cpp | 132 +++++++++--------- Sources/ComputeCxx/Trace/ExternalTrace.h | 11 +- Sources/ComputeCxx/Trace/Trace.h | 2 +- .../ComputeCxx/include/ComputeCxx/AGGraph.h | 16 +-- .../include/ComputeCxx/AGGraphTracing.h | 20 +++ .../ComputeCxx/include/ComputeCxx/AGTrace.h | 81 ----------- .../include/ComputeCxx/AGTraceFlags.h | 21 --- .../include/ComputeCxx/AGTraceType.h | 82 +++++++++++ .../include/ComputeCxx/ComputeCxx.h | 6 +- 14 files changed, 213 insertions(+), 206 deletions(-) create mode 100644 Sources/ComputeCxx/include/ComputeCxx/AGGraphTracing.h delete mode 100644 Sources/ComputeCxx/include/ComputeCxx/AGTrace.h delete mode 100644 Sources/ComputeCxx/include/ComputeCxx/AGTraceFlags.h create mode 100644 Sources/ComputeCxx/include/ComputeCxx/AGTraceType.h diff --git a/Sources/ComputeCxx/Graph/AGGraph.cpp b/Sources/ComputeCxx/Graph/AGGraph.cpp index 8ba4a68..d3636fc 100644 --- a/Sources/ComputeCxx/Graph/AGGraph.cpp +++ b/Sources/ComputeCxx/Graph/AGGraph.cpp @@ -901,9 +901,9 @@ void AGGraphSetOutputValue(const void *value, AGTypeID type) { #pragma mark - Trace -void AGGraphStartTracing(AGGraphRef graph, AGTraceFlags trace_flags) { AGGraphStartTracing2(graph, trace_flags, NULL); } +void AGGraphStartTracing(AGGraphRef graph, AGGraphTraceOptions trace_options) { AGGraphStartTracing2(graph, trace_options, NULL); } -void AGGraphStartTracing2(AGGraphRef graph, AGTraceFlags trace_flags, CFArrayRef subsystems) { +void AGGraphStartTracing2(AGGraphRef graph, AGGraphTraceOptions trace_options, CFArrayRef subsystems) { auto subsystems_vector = AG::vector, 0, uint64_t>(); if (subsystems) { auto subsystems_count = CFArrayGetCount(subsystems); @@ -928,12 +928,12 @@ void AGGraphStartTracing2(AGGraphRef graph, AGTraceFlags trace_flags, CFArrayRef std::span((const char **)subsystems_vector.data(), subsystems_vector.size()); if (graph == nullptr) { - AG::Graph::all_start_tracing(trace_flags, subsystems_span); + AG::Graph::all_start_tracing(trace_options, subsystems_span); return; } auto graph_context = AG::Graph::Context::from_cf(graph); - graph_context->graph().start_tracing(trace_flags, subsystems_span); + graph_context->graph().start_tracing(trace_options, subsystems_span); } void AGGraphStopTracing(AGGraphRef graph) { @@ -965,7 +965,7 @@ CFStringRef AGGraphCopyTracePath(AGGraphRef graph) { return graph_context->graph().copy_trace_path(); } -uint64_t AGGraphAddTrace(AGGraphRef graph, const AGTraceRef trace, void *context) { +uint64_t AGGraphAddTrace(AGGraphRef graph, const AGTraceTypeRef trace, void *context) { auto graph_context = AG::Graph::Context::from_cf(graph); auto external_trace = new ExternalTrace(trace, context); graph_context->graph().add_trace(external_trace); @@ -977,7 +977,7 @@ void AGGraphRemoveTrace(AGGraphRef graph, uint64_t trace_id) { graph_context->graph().remove_trace(trace_id); } -void AGGraphSetTrace(AGGraphRef graph, const AGTraceRef trace, void *context) { +void AGGraphSetTrace(AGGraphRef graph, const AGTraceTypeRef trace, void *context) { auto graph_context = AG::Graph::Context::from_cf(graph); graph_context->graph().remove_trace(0); @@ -995,7 +995,7 @@ bool AGGraphIsTracingActive(AGGraphRef graph) { return graph_context->graph().traces().size() > 0; } -void AGGraphPrepareTrace(AGGraphRef graph, const AGTraceRef trace, void *context) { +void AGGraphPrepareTrace(AGGraphRef graph, const AGTraceTypeRef trace, void *context) { auto graph_context = AG::Graph::Context::from_cf(graph); auto external_trace = new ExternalTrace(trace, context); graph_context->graph().prepare_trace(*external_trace); diff --git a/Sources/ComputeCxx/Graph/Graph.cpp b/Sources/ComputeCxx/Graph/Graph.cpp index ec700b5..342c3b2 100644 --- a/Sources/ComputeCxx/Graph/Graph.cpp +++ b/Sources/ComputeCxx/Graph/Graph.cpp @@ -19,7 +19,7 @@ #include "Attribute/AttributeID/OffsetAttributeID.h" #include "Attribute/AttributeType/AttributeType.h" #include "Attribute/AttributeView/AttributeView.h" -#include "ComputeCxx/AGTrace.h" +#include "ComputeCxx/AGGraphTracing.h" #include "ComputeCxx/AGUniqueID.h" #include "Context.h" #include "KeyTable.h" @@ -1826,10 +1826,10 @@ void *Graph::output_value_ref(data::ptr node, const swift::metadata &value #pragma mark - Trace -void Graph::start_tracing(AGTraceFlags trace_flags, std::span subsystems) { - if (trace_flags & AGTraceFlagsEnabled && _trace_recorder == nullptr) { - _trace_recorder = new TraceRecorder(this, trace_flags, subsystems); - if (trace_flags & AGTraceFlagsPrepare) { +void Graph::start_tracing(AGGraphTraceOptions trace_options, std::span subsystems) { + if (trace_options & AGGraphTraceOptionsEnabled && _trace_recorder == nullptr) { + _trace_recorder = new TraceRecorder(this, trace_options, subsystems); + if (trace_options & AGGraphTraceOptionsPrepare) { prepare_trace(*_trace_recorder); } add_trace(_trace_recorder); @@ -1883,10 +1883,10 @@ void Graph::remove_trace(uint64_t trace_id) { } } -void Graph::all_start_tracing(AGTraceFlags trace_flags, std::span span) { +void Graph::all_start_tracing(AGGraphTraceOptions trace_options, std::span span) { all_lock(); for (auto graph = _all_graphs; graph != nullptr; graph = graph->_next) { - graph->start_tracing(trace_flags, span); + graph->start_tracing(trace_options, span); } all_unlock(); } diff --git a/Sources/ComputeCxx/Graph/Graph.h b/Sources/ComputeCxx/Graph/Graph.h index 2e7cf75..1ed4840 100644 --- a/Sources/ComputeCxx/Graph/Graph.h +++ b/Sources/ComputeCxx/Graph/Graph.h @@ -11,15 +11,15 @@ #endif #endif -#include #include #include #include +#include #include "Attribute/AttributeID/AttributeID.h" #include "Attribute/AttributeType/AttributeType.h" #include "Closure/ClosureFunction.h" -#include "ComputeCxx/AGTrace.h" +#include "ComputeCxx/AGGraphTracing.h" #include "Swift/Metadata.h" #include "Vector/Vector.h" @@ -236,7 +236,7 @@ class Graph { void add_subgraph(Subgraph &subgraph); void remove_subgraph(Subgraph &subgraph); - + void add_subgraphs_with_cached_nodes(Subgraph &subgraph) { _subgraphs_with_cached_nodes.push_back(&subgraph); }; class without_invalidating { @@ -389,7 +389,7 @@ class Graph { // MARK: Trace - void start_tracing(AGTraceFlags trace_flags, std::span subsystems); + void start_tracing(AGGraphTraceOptions trace_options, std::span subsystems); void stop_tracing(); void sync_tracing(); CFStringRef copy_trace_path(); @@ -399,7 +399,7 @@ class Graph { void add_trace(Trace *_Nullable trace); void remove_trace(uint64_t trace_id); - static void all_start_tracing(AGTraceFlags trace_flags, std::span span); + static void all_start_tracing(AGGraphTraceOptions trace_options, std::span span); static void all_stop_tracing(); static void all_sync_tracing(); static CFStringRef all_copy_trace_path(); diff --git a/Sources/ComputeCxx/Graph/TraceRecorder.cpp b/Sources/ComputeCxx/Graph/TraceRecorder.cpp index 1f8548f..9e1631b 100644 --- a/Sources/ComputeCxx/Graph/TraceRecorder.cpp +++ b/Sources/ComputeCxx/Graph/TraceRecorder.cpp @@ -6,7 +6,7 @@ #include "Attribute/AttributeData/Node/IndirectNode.h" #include "Attribute/AttributeType/AttributeType.h" #include "ComputeCxx/AGGraph.h" -#include "ComputeCxx/AGTrace.h" +#include "ComputeCxx/AGGraphTracing.h" #include "ComputeCxx/AGUniqueID.h" #include "Context.h" #include "Log/Log.h" @@ -15,7 +15,7 @@ namespace AG { -Graph::TraceRecorder::TraceRecorder(Graph *graph, AGTraceFlags trace_flags, std::span subsystems) { +Graph::TraceRecorder::TraceRecorder(Graph *graph, AGGraphTraceOptions trace_options, std::span subsystems) { // TODO: not implemented } @@ -139,7 +139,7 @@ void Graph::TraceRecorder::added(data::ptr node) { // TODO: not implemented } -void Graph::TraceRecorder::add_edge(data::ptr node, AttributeID input, uint8_t input_edge_flags) { +void Graph::TraceRecorder::add_edge(data::ptr node, AttributeID input, AGInputOptions input_options) { // TODO: not implemented } diff --git a/Sources/ComputeCxx/Graph/TraceRecorder.h b/Sources/ComputeCxx/Graph/TraceRecorder.h index ab270f2..96e169c 100644 --- a/Sources/ComputeCxx/Graph/TraceRecorder.h +++ b/Sources/ComputeCxx/Graph/TraceRecorder.h @@ -12,7 +12,7 @@ namespace AG { class Graph::TraceRecorder : public Trace { public: - TraceRecorder(Graph *graph, AGTraceFlags trace_flags, std::span subsystems); + TraceRecorder(Graph *graph, AGGraphTraceOptions trace_options, std::span subsystems); ~TraceRecorder(); uint64_t id() { return _id; }; @@ -65,7 +65,7 @@ class Graph::TraceRecorder : public Trace { void added(data::ptr node) override; - void add_edge(data::ptr node, AttributeID input, uint8_t input_edge_flags) override; + void add_edge(data::ptr node, AttributeID input, AGInputOptions input_options) override; void remove_edge(data::ptr node, uint32_t input_index) override; void set_edge_pending(data::ptr node, AttributeID input, bool pending) override; diff --git a/Sources/ComputeCxx/Trace/ExternalTrace.cpp b/Sources/ComputeCxx/Trace/ExternalTrace.cpp index e4f234b..8f9ea22 100644 --- a/Sources/ComputeCxx/Trace/ExternalTrace.cpp +++ b/Sources/ComputeCxx/Trace/ExternalTrace.cpp @@ -25,80 +25,80 @@ void ExternalTrace::end_trace(const AG::Graph &graph) { void ExternalTrace::begin_update(const AG::Subgraph &subgraph, uint32_t options) { auto cf_subgraph = subgraph.to_cf(); - if (auto callback = _trace->begin_update_subgraph) { + if (auto callback = _trace->begin_subgraph_update) { callback(_context, cf_subgraph, options); } } void ExternalTrace::end_update(const AG::Subgraph &subgraph) { auto cf_subgraph = subgraph.to_cf(); - if (auto callback = _trace->end_update_subgraph) { + if (auto callback = _trace->end_subgraph_update) { callback(_context, cf_subgraph); } } void ExternalTrace::begin_update(const AG::Graph::UpdateStack &update_stack, AG::data::ptr node, uint32_t options) { - if (auto callback = _trace->begin_update_stack) { + if (auto callback = _trace->begin_node_update) { callback(_context, AGAttribute(AG::AttributeID(node))); } } void ExternalTrace::end_update(const AG::Graph::UpdateStack &update_stack, AG::data::ptr node, AGGraphUpdateStatus update_status) { - if (auto callback = _trace->end_update_stack) { + if (auto callback = _trace->end_node_update) { callback(_context, update_status == AGGraphUpdateStatusChanged); } } void ExternalTrace::begin_update(AG::data::ptr node) { - if (auto callback = _trace->begin_update_attribute) { + if (auto callback = _trace->begin_value_update) { callback(_context, AGAttribute(AG::AttributeID(node))); } } void ExternalTrace::end_update(AG::data::ptr node, bool changed) { - if (auto callback = _trace->end_update_attribute) { + if (auto callback = _trace->end_value_update) { callback(_context, AGAttribute(AG::AttributeID(node)), changed); } } void ExternalTrace::begin_update(const AG::Graph::Context &context) { auto cf_context = context.to_cf(); - if (auto callback = _trace->begin_update_graph) { + if (auto callback = _trace->begin_graph_update) { callback(_context, cf_context); } } void ExternalTrace::end_update(const AG::Graph::Context &context) { auto cf_context = context.to_cf(); - if (auto callback = _trace->end_update_graph) { + if (auto callback = _trace->end_graph_update) { callback(_context, cf_context); } } void ExternalTrace::begin_invalidation(const AG::Graph::Context &context, AG::AttributeID attribute) { auto cf_context = context.to_cf(); - if (auto callback = _trace->begin_invalidation) { + if (auto callback = _trace->begin_graph_invalidation) { callback(_context, cf_context, AGAttribute(attribute)); } } void ExternalTrace::end_invalidation(const AG::Graph::Context &context, AG::AttributeID attribute) { auto cf_context = context.to_cf(); - if (auto callback = _trace->end_invalidation) { + if (auto callback = _trace->end_graph_invalidation) { callback(_context, cf_context, AGAttribute(attribute)); } } void ExternalTrace::begin_modify(AG::data::ptr node) { - if (auto callback = _trace->begin_modify) { + if (auto callback = _trace->begin_modify_node) { callback(_context, AGAttribute(AG::AttributeID(node))); } } void ExternalTrace::end_modify(AG::data::ptr node) { - if (auto callback = _trace->end_modify) { + if (auto callback = _trace->end_modify_node) { callback(_context, AGAttribute(AG::AttributeID(node))); } } @@ -123,35 +123,35 @@ void ExternalTrace::end_event(AG::data::ptr node, uint32_t event_id) { void ExternalTrace::created(const AG::Graph::Context &context) { auto cf_context = context.to_cf(); - if (auto callback = _trace->created_graph) { + if (auto callback = _trace->graph_created) { callback(_context, cf_context); } } void ExternalTrace::destroy(const AG::Graph::Context &context) { auto cf_context = context.to_cf(); - if (auto callback = _trace->destroy_graph) { + if (auto callback = _trace->graph_destroy) { callback(_context, cf_context); } } void ExternalTrace::needs_update(const AG::Graph::Context &context) { auto cf_context = context.to_cf(); - if (auto callback = _trace->needs_update) { + if (auto callback = _trace->graph_needs_update) { callback(_context, cf_context); } } void ExternalTrace::created(const AG::Subgraph &subgraph) { auto cf_subgraph = subgraph.to_cf(); - if (auto callback = _trace->created_subgraph) { + if (auto callback = _trace->subgraph_created) { callback(_context, cf_subgraph); } } void ExternalTrace::invalidate(const AG::Subgraph &subgraph) { auto cf_subgraph = subgraph.to_cf(); - if (auto callback = _trace->invalidate_subgraph) { + if (auto callback = _trace->subgraph_destroy) { callback(_context, cf_subgraph); } } @@ -161,7 +161,7 @@ void ExternalTrace::destroy(const AG::Subgraph &subgraph) {} void ExternalTrace::add_child(const AG::Subgraph &subgraph, const AG::Subgraph &child) { auto cf_subgraph = subgraph.to_cf(); auto cf_child = subgraph.to_cf(); - if (auto callback = _trace->add_child_subgraph) { + if (auto callback = _trace->subgraph_add_child) { callback(_context, cf_subgraph, cf_child); } } @@ -169,25 +169,25 @@ void ExternalTrace::add_child(const AG::Subgraph &subgraph, const AG::Subgraph & void ExternalTrace::remove_child(const AG::Subgraph &subgraph, const AG::Subgraph &child) { auto cf_subgraph = subgraph.to_cf(); auto cf_child = subgraph.to_cf(); - if (auto callback = _trace->remove_child_subgraph) { + if (auto callback = _trace->subgraph_remove_child) { callback(_context, cf_subgraph, cf_child); } } void ExternalTrace::added(AG::data::ptr node) { - if (auto callback = _trace->added_attribute) { + if (auto callback = _trace->node_added) { callback(_context, AGAttribute(AG::AttributeID(node))); } } -void ExternalTrace::add_edge(AG::data::ptr node, AG::AttributeID input, uint8_t input_edge_flags) { - if (auto callback = _trace->add_edge) { - callback(_context, AGAttribute(AG::AttributeID(node)), AGAttribute(AG::AttributeID(input)), input_edge_flags); +void ExternalTrace::add_edge(AG::data::ptr node, AG::AttributeID input, AGInputOptions input_options) { + if (auto callback = _trace->node_add_edge) { + callback(_context, AGAttribute(AG::AttributeID(node)), AGAttribute(AG::AttributeID(input)), input_options); } } void ExternalTrace::remove_edge(AG::data::ptr node, uint32_t input_index) { - if (auto callback = _trace->remove_edge) { + if (auto callback = _trace->node_remove_edge) { if (AG::AttributeID(node).subgraph()) { callback(_context, AGAttribute(AG::AttributeID(node)), input_index); } @@ -195,7 +195,7 @@ void ExternalTrace::remove_edge(AG::data::ptr node, uint32_t input_ind } void ExternalTrace::set_edge_pending(AG::data::ptr node, AG::AttributeID input, bool pending) { - if (auto callback = _trace->set_edge_pending) { + if (auto callback = _trace->node_set_edge_pending) { if (AG::AttributeID(node).subgraph()) { callback(_context, AGAttribute(AG::AttributeID(node)), AGAttribute(input), pending); } @@ -203,49 +203,50 @@ void ExternalTrace::set_edge_pending(AG::data::ptr node, AG::Attribute } void ExternalTrace::set_dirty(AG::data::ptr node, bool dirty) { - if (auto callback = _trace->set_dirty) { + if (auto callback = _trace->node_set_dirty) { callback(_context, AGAttribute(AG::AttributeID(node)), dirty); } } void ExternalTrace::set_pending(AG::data::ptr node, bool pending) { - if (auto callback = _trace->set_pending) { + if (auto callback = _trace->node_set_pending) { callback(_context, AGAttribute(AG::AttributeID(node)), pending); } } void ExternalTrace::set_value(AG::data::ptr node, const void *value) { - if (auto callback = _trace->set_value) { + if (auto callback = _trace->node_set_value) { callback(_context, AGAttribute(AG::AttributeID(node))); } } void ExternalTrace::mark_value(AG::data::ptr node) { - if (auto callback = _trace->mark_value) { + if (auto callback = _trace->node_mark_value) { callback(_context, AGAttribute(AG::AttributeID(node))); } } void ExternalTrace::added(AG::data::ptr indirect_node) { - if (auto callback = _trace->added_indirect_attribute) { + if (auto callback = _trace->indirect_node_added) { callback(_context, AGAttribute(AG::AttributeID(indirect_node))); // TODO: check sets kind } } void ExternalTrace::set_source(AG::data::ptr indirect_node, AG::AttributeID source) { - if (auto callback = _trace->set_source) { + if (auto callback = _trace->indirect_node_set_source) { callback(_context, AGAttribute(AG::AttributeID(indirect_node)), AGAttribute(source)); // TODO: check sets kind } } void ExternalTrace::set_dependency(AG::data::ptr indirect_node, AG::AttributeID dependency) { - if (auto callback = _trace->set_dependency) { - callback(_context, AGAttribute(AG::AttributeID(indirect_node)), AGAttribute(dependency)); // TODO: check sets kind + if (auto callback = _trace->indirect_node_set_dependency) { + callback(_context, AGAttribute(AG::AttributeID(indirect_node)), + AGAttribute(dependency)); // TODO: check sets kind } } void ExternalTrace::mark_profile(const AG::Graph &graph, uint32_t event_id) { - if (auto callback = _trace->mark_profile) { + if (auto callback = _trace->profile_mark) { const char *event_name = graph.key_name(event_id); callback(_context, event_name); } @@ -253,56 +254,61 @@ void ExternalTrace::mark_profile(const AG::Graph &graph, uint32_t event_id) { void ExternalTrace::custom_event(const AG::Graph::Context &context, const char *event_name, const void *value, const AG::swift::metadata &type) { - if (_trace->events >= AGTraceEventsCustom) { - auto cf_context = context.to_cf(); - if (auto callback = _trace->custom_event) { - callback(_context, cf_context, event_name, value, AGTypeID(&type)); - } + if (_trace->version < AGTraceTypeVersionCustom) { + return; + } + auto cf_context = context.to_cf(); + if (auto callback = _trace->custom_event) { + callback(_context, cf_context, event_name, value, AGTypeID(&type)); } } void ExternalTrace::named_event(const AG::Graph::Context &context, uint32_t event_id, uint32_t event_arg_count, const void *event_args, CFDataRef data, uint32_t arg6) { - if (_trace->events >= AGTraceEventsNamed) { - auto cf_context = context.to_cf(); - if (auto callback = _trace->named_event) { - callback(_context, cf_context, event_id, event_arg_count, event_args, data, arg6); - } + if (_trace->version < AGTraceTypeVersionNamed) { + return; + } + auto cf_context = context.to_cf(); + if (auto callback = _trace->named_event) { + callback(_context, cf_context, event_id, event_arg_count, event_args, data, arg6); } } bool ExternalTrace::named_event_enabled(uint32_t event_id) { - if (_trace->events >= AGTraceEventsNamed) { - if (auto callback = _trace->named_event_enabled) { - return callback(_context); - } - return _trace->named_event != nullptr; + if (_trace->version < AGTraceTypeVersionNamed) { + return false; } - return false; + if (auto callback = _trace->named_event_enabled) { + return callback(_context); + } + return _trace->named_event != nullptr; } void ExternalTrace::set_deadline(uint64_t deadline) { - if (_trace->events >= AGTraceEventsDeadline) { - if (auto callback = _trace->set_deadline) { - callback(_context); - } + if (_trace->version < AGTraceTypeVersionDeadline) { + return; + } + if (auto callback = _trace->set_deadline) { + callback(_context); } } void ExternalTrace::passed_deadline() { - if (_trace->events >= AGTraceEventsDeadline) { - if (auto callback = _trace->passed_deadline) { - callback(_context); - } + if (_trace->version < AGTraceTypeVersionDeadline) { + return; + } + if (auto callback = _trace->passed_deadline) { + callback(_context); } } void ExternalTrace::compare_failed(AG::data::ptr node, const void *lhs, const void *rhs, size_t range_offset, size_t range_size, const AG::swift::metadata *_Nullable type) { - if (_trace->events >= AGTraceEventsCompareFailed) { - AGComparisonStateStorage storage = {lhs, rhs, range_offset, range_size, AGTypeID(&type)}; - if (auto callback = _trace->compare_failed) { - callback(_context, AGAttribute(AG::AttributeID(node)), &storage); - } + if (_trace->version < AGTraceTypeVersionCompareFailed) { + return; + } + AGComparisonStateStorage storage = {lhs, rhs, range_offset, range_size, AGTypeID(&type)}; + if (auto callback = _trace->compare_failed) { + callback(_context, AGAttribute(AG::AttributeID(node)), &storage); } } diff --git a/Sources/ComputeCxx/Trace/ExternalTrace.h b/Sources/ComputeCxx/Trace/ExternalTrace.h index dbbe974..9e6bd05 100644 --- a/Sources/ComputeCxx/Trace/ExternalTrace.h +++ b/Sources/ComputeCxx/Trace/ExternalTrace.h @@ -1,9 +1,10 @@ #pragma once -#include "ComputeCxx/AGBase.h" #include "ComputeCxx/AGAttribute.h" +#include "ComputeCxx/AGBase.h" #include "ComputeCxx/AGComparison.h" #include "ComputeCxx/AGGraph.h" +#include "ComputeCxx/AGTraceType.h" #include "ComputeCxx/AGType.h" #include "Graph/Context.h" #include "Subgraph/Subgraph.h" @@ -14,12 +15,12 @@ AG_ASSUME_NONNULL_BEGIN class ExternalTrace : public AG::Trace { public: private: - const AGTrace *_trace; + const AGTraceTypeRef _trace; void *_Nullable _context; public: - ExternalTrace(AGTrace *trace, void *context) : _trace(trace), _context(context) {}; - ExternalTrace(uint64_t id, const AGTrace *trace, void *context) + ExternalTrace(AGTraceTypeRef trace, void *context) : _trace(trace), _context(context) {}; + ExternalTrace(uint64_t id, const AGTraceTypeRef trace, void *context) : AG::Trace(id), _trace(trace), _context(context) {}; void graph_destroyed() override; @@ -62,7 +63,7 @@ class ExternalTrace : public AG::Trace { void added(AG::data::ptr node) override; - void add_edge(AG::data::ptr node, AG::AttributeID input, uint8_t input_edge_flags) override; + void add_edge(AG::data::ptr node, AG::AttributeID input, AGInputOptions input_options) override; void remove_edge(AG::data::ptr node, uint32_t input_index) override; void set_edge_pending(AG::data::ptr node, AG::AttributeID input, bool pending) override; diff --git a/Sources/ComputeCxx/Trace/Trace.h b/Sources/ComputeCxx/Trace/Trace.h index fb285f9..c4f6cf4 100644 --- a/Sources/ComputeCxx/Trace/Trace.h +++ b/Sources/ComputeCxx/Trace/Trace.h @@ -74,7 +74,7 @@ class Trace { virtual void added(data::ptr node) {}; - virtual void add_edge(data::ptr node, AttributeID input, uint8_t input_edge_flags) {}; + virtual void add_edge(data::ptr node, AttributeID input, AGInputOptions input_options) {}; virtual void remove_edge(data::ptr node, uint32_t input_index) {}; virtual void set_edge_pending(data::ptr node, AttributeID input, bool pending) {}; diff --git a/Sources/ComputeCxx/include/ComputeCxx/AGGraph.h b/Sources/ComputeCxx/include/ComputeCxx/AGGraph.h index e824076..8d5fa2c 100644 --- a/Sources/ComputeCxx/include/ComputeCxx/AGGraph.h +++ b/Sources/ComputeCxx/include/ComputeCxx/AGGraph.h @@ -19,9 +19,9 @@ #include #include #include +#include #include #include -#include #include #include #include @@ -36,7 +36,7 @@ AG_EXTERN_C_BEGIN typedef struct AG_BRIDGED_TYPE(id) AGGraphStorage *AGGraphRef AG_SWIFT_NAME(Graph); typedef void *AGUnownedGraphContextRef AG_SWIFT_STRUCT; -typedef struct AGTrace *AGTraceRef; +typedef struct AGTraceType *AGTraceTypeRef; AG_EXPORT AG_REFINED_FOR_SWIFT @@ -365,13 +365,13 @@ void AGGraphSetOutputValue(const void *value, AGTypeID type); AG_EXPORT AG_REFINED_FOR_SWIFT -void AGGraphStartTracing(AGGraphRef _Nullable graph, AGTraceFlags trace_flags) +void AGGraphStartTracing(AGGraphRef _Nullable graph, AGGraphTraceOptions trace_options) AG_SWIFT_NAME(AGGraphRef.startTracing(_:flags:)); AG_EXPORT AG_REFINED_FOR_SWIFT -void AGGraphStartTracing2(AGGraphRef _Nullable graph, AGTraceFlags trace_flags, CFArrayRef _Nullable subsystems) - AG_SWIFT_NAME(AGGraphRef.startTracing(_:flags:subsystems:)); // TODO: flags or options +void AGGraphStartTracing2(AGGraphRef _Nullable graph, AGGraphTraceOptions trace_options, CFArrayRef _Nullable subsystems) + AG_SWIFT_NAME(AGGraphRef.startTracing(_:options:subsystems:)); AG_EXPORT AG_REFINED_FOR_SWIFT @@ -387,7 +387,7 @@ CFStringRef AGGraphCopyTracePath(AGGraphRef graph) AG_SWIFT_NAME(getter:AGGraphR AG_EXPORT AG_REFINED_FOR_SWIFT -uint64_t AGGraphAddTrace(AGGraphRef graph, const AGTraceRef trace, void *_Nullable context) +uint64_t AGGraphAddTrace(AGGraphRef graph, const AGTraceTypeRef trace, void *_Nullable context) AG_SWIFT_NAME(AGGraphRef.addTrace(self:_:context:)); AG_EXPORT @@ -396,7 +396,7 @@ void AGGraphRemoveTrace(AGGraphRef graph, uint64_t trace_id) AG_SWIFT_NAME(AGGra AG_EXPORT AG_REFINED_FOR_SWIFT -void AGGraphSetTrace(AGGraphRef graph, const AGTraceRef trace, void *_Nullable context) +void AGGraphSetTrace(AGGraphRef graph, const AGTraceTypeRef trace, void *_Nullable context) AG_SWIFT_NAME(AGGraphRef.setTrace(self:_:context:)); AG_EXPORT @@ -409,7 +409,7 @@ bool AGGraphIsTracingActive(AGGraphRef graph) AG_SWIFT_NAME(getter:AGGraphRef.is AG_EXPORT AG_REFINED_FOR_SWIFT -void AGGraphPrepareTrace(AGGraphRef graph, const AGTraceRef trace, void *_Nullable context); +void AGGraphPrepareTrace(AGGraphRef graph, const AGTraceTypeRef trace, void *_Nullable context); AG_EXPORT AG_REFINED_FOR_SWIFT diff --git a/Sources/ComputeCxx/include/ComputeCxx/AGGraphTracing.h b/Sources/ComputeCxx/include/ComputeCxx/AGGraphTracing.h new file mode 100644 index 0000000..d7a9a3f --- /dev/null +++ b/Sources/ComputeCxx/include/ComputeCxx/AGGraphTracing.h @@ -0,0 +1,20 @@ +#pragma once + +#include + +AG_ASSUME_NONNULL_BEGIN + +AG_EXTERN_C_BEGIN + +typedef AG_OPTIONS(uint32_t, AGGraphTraceOptions) { + AGGraphTraceOptionsEnabled = 1 << 0, + AGGraphTraceOptionsFull = 1 << 1, + AGGraphTraceOptionsBacktrace = 1 << 2, + AGGraphTraceOptionsPrepare = 1 << 3, + AGGraphTraceOptionsCustom = 1 << 4, + AGGraphTraceOptionsAll = 1 << 5, +} AG_SWIFT_NAME(AGGraphRef.TraceOptions); + +AG_EXTERN_C_END + +AG_ASSUME_NONNULL_END diff --git a/Sources/ComputeCxx/include/ComputeCxx/AGTrace.h b/Sources/ComputeCxx/include/ComputeCxx/AGTrace.h deleted file mode 100644 index 4648852..0000000 --- a/Sources/ComputeCxx/include/ComputeCxx/AGTrace.h +++ /dev/null @@ -1,81 +0,0 @@ -#pragma once - -#include -#include -#include - -AG_ASSUME_NONNULL_BEGIN - -AG_EXTERN_C_BEGIN - -typedef AG_ENUM(uint64_t, AGTraceEvents) { - AGTraceEventsCustom = 1, - AGTraceEventsNamed = 2, - AGTraceEventsDeadline = 3, - AGTraceEventsCompareFailed = 4, -}; - -typedef struct AGTrace { - AGTraceEvents events; - - void (*_Nullable begin_trace)(void *_Nullable context, AGGraphRef graph); - void (*_Nullable end_trace)(void *_Nullable context, AGGraphRef graph); - - void (*_Nullable begin_update_subgraph)(void *_Nullable context, AGSubgraphRef subgraph, uint32_t options); - void (*_Nullable end_update_subgraph)(void *_Nullable context, AGSubgraphRef subgraph); - void (*_Nullable begin_update_stack)(void *_Nullable context, AGAttribute attribute); - void (*_Nullable end_update_stack)(void *_Nullable context, bool changed); - void (*_Nullable begin_update_attribute)(void *_Nullable context, AGAttribute attribute); - void (*_Nullable end_update_attribute)(void *_Nullable context, AGAttribute attribute, bool changed); - void (*_Nullable begin_update_graph)(void *_Nullable context, AGGraphRef graph); - void (*_Nullable end_update_graph)(void *_Nullable context, AGGraphRef graph); - - void (*_Nullable begin_invalidation)(void *_Nullable context, AGGraphRef graph, AGAttribute attribute); - void (*_Nullable end_invalidation)(void *_Nullable context, AGGraphRef graph, AGAttribute attribute); - - void (*_Nullable begin_modify)(void *_Nullable context, AGAttribute attribute); - void (*_Nullable end_modify)(void *_Nullable context, AGAttribute attribute); - - void (*_Nullable begin_event)(void *_Nullable context, AGAttribute attribute, const char *event_name); - void (*_Nullable end_event)(void *_Nullable context, AGAttribute attribute, const char *event_name); - - void (*_Nullable created_graph)(void *_Nullable context, AGGraphRef graph); - void (*_Nullable destroy_graph)(void *_Nullable context, AGGraphRef graph); - void (*_Nullable needs_update)(void *_Nullable context, AGGraphRef graph); - - void (*_Nullable created_subgraph)(void *_Nullable context, AGSubgraphRef subgraph); - void (*_Nullable invalidate_subgraph)(void *_Nullable context, AGSubgraphRef subgraph); - void (*_Nullable add_child_subgraph)(void *_Nullable context, AGSubgraphRef subgraph, AGSubgraphRef child); - void (*_Nullable remove_child_subgraph)(void *_Nullable context, AGSubgraphRef subgraph, AGSubgraphRef child); - - void (*_Nullable added_attribute)(void *_Nullable context, AGAttribute attribute); - void (*_Nullable add_edge)(void *_Nullable context, AGAttribute attribute, AGAttribute input, uint32_t flags); - void (*_Nullable remove_edge)(void *_Nullable context, AGAttribute attribute, uint32_t index); - void (*_Nullable set_edge_pending)(void *_Nullable context, AGAttribute attribute, AGAttribute input, bool pending); - - void (*_Nullable set_dirty)(void *_Nullable context, AGAttribute attribute, bool dirty); - void (*_Nullable set_pending)(void *_Nullable context, AGAttribute attribute, bool pending); - void (*_Nullable set_value)(void *_Nullable context, AGAttribute attribute); - void (*_Nullable mark_value)(void *_Nullable context, AGAttribute attribute); - - void (*_Nullable added_indirect_attribute)(void *_Nullable context, AGAttribute attribute); - void (*_Nullable set_source)(void *_Nullable context, AGAttribute attribute, AGAttribute source); - void (*_Nullable set_dependency)(void *_Nullable context, AGAttribute attribute, AGAttribute dependency); - - void (*_Nullable mark_profile)(void *_Nullable context, const char *event_name); - - void (*_Nullable custom_event)(void *_Nullable context, AGGraphRef graph, const char *event_name, const void *value, - AGTypeID type); - void (*_Nullable named_event)(void *_Nullable context, AGGraphRef graph, uint32_t eventID, uint32_t eventArgCount, - const void *eventArgs, CFDataRef data, uint32_t arg6); - bool (*_Nullable named_event_enabled)(void *_Nullable context); - - void (*_Nullable set_deadline)(void *_Nullable context); - void (*_Nullable passed_deadline)(void *_Nullable context); - - void (*_Nullable compare_failed)(void *_Nullable context, AGAttribute attribute, AGComparisonState comparisonState); -} AGTrace; - -AG_EXTERN_C_END - -AG_ASSUME_NONNULL_END diff --git a/Sources/ComputeCxx/include/ComputeCxx/AGTraceFlags.h b/Sources/ComputeCxx/include/ComputeCxx/AGTraceFlags.h deleted file mode 100644 index 33bafbd..0000000 --- a/Sources/ComputeCxx/include/ComputeCxx/AGTraceFlags.h +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -#include -#include - -AG_ASSUME_NONNULL_BEGIN - -AG_EXTERN_C_BEGIN - -typedef AG_OPTIONS(uint32_t, AGTraceFlags) { - AGTraceFlagsEnabled = 1 << 0, - AGTraceFlagsFull = 1 << 1, - AGTraceFlagsBacktrace = 1 << 2, - AGTraceFlagsPrepare = 1 << 3, - AGTraceFlagsCustom = 1 << 4, - AGTraceFlagsAll = 1 << 5, -} AG_SWIFT_NAME(AGGraphRef.TraceFlags); // TODO: what is real name - -AG_EXTERN_C_END - -AG_ASSUME_NONNULL_END diff --git a/Sources/ComputeCxx/include/ComputeCxx/AGTraceType.h b/Sources/ComputeCxx/include/ComputeCxx/AGTraceType.h new file mode 100644 index 0000000..2f9bb90 --- /dev/null +++ b/Sources/ComputeCxx/include/ComputeCxx/AGTraceType.h @@ -0,0 +1,82 @@ +#pragma once + +#include +#include +#include + +AG_ASSUME_NONNULL_BEGIN + +AG_EXTERN_C_BEGIN + +typedef AG_ENUM(uint64_t, AGTraceTypeVersion) { + AGTraceTypeVersionInitial = 0, + AGTraceTypeVersionCustom = 1, + AGTraceTypeVersionNamed = 2, + AGTraceTypeVersionDeadline = 3, + AGTraceTypeVersionCompareFailed = 4, +}; + +typedef struct AGTraceType { + AGTraceTypeVersion version; + + void (*_Nullable begin_trace)(void *_Nullable context, AGGraphRef graph); + void (*_Nullable end_trace)(void *_Nullable context, AGGraphRef graph); + + void (*_Nullable begin_subgraph_update)(void *_Nullable context, AGSubgraphRef subgraph, uint32_t options); + void (*_Nullable end_subgraph_update)(void *_Nullable context, AGSubgraphRef subgraph); + void (*_Nullable begin_node_update)(void *_Nullable context, AGAttribute attribute); + void (*_Nullable end_node_update)(void *_Nullable context, bool changed); + void (*_Nullable begin_value_update)(void *_Nullable context, AGAttribute attribute); + void (*_Nullable end_value_update)(void *_Nullable context, AGAttribute attribute, bool changed); + void (*_Nullable begin_graph_update)(void *_Nullable context, AGGraphRef graph); + void (*_Nullable end_graph_update)(void *_Nullable context, AGGraphRef graph); + + void (*_Nullable begin_graph_invalidation)(void *_Nullable context, AGGraphRef graph, AGAttribute attribute); + void (*_Nullable end_graph_invalidation)(void *_Nullable context, AGGraphRef graph, AGAttribute attribute); + + void (*_Nullable begin_modify_node)(void *_Nullable context, AGAttribute attribute); + void (*_Nullable end_modify_node)(void *_Nullable context, AGAttribute attribute); + + void (*_Nullable begin_event)(void *_Nullable context, AGAttribute attribute, const char *event_name); + void (*_Nullable end_event)(void *_Nullable context, AGAttribute attribute, const char *event_name); + + void (*_Nullable graph_created)(void *_Nullable context, AGGraphRef graph); + void (*_Nullable graph_destroy)(void *_Nullable context, AGGraphRef graph); + void (*_Nullable graph_needs_update)(void *_Nullable context, AGGraphRef graph); + + void (*_Nullable subgraph_created)(void *_Nullable context, AGSubgraphRef subgraph); + void (*_Nullable subgraph_destroy)(void *_Nullable context, AGSubgraphRef subgraph); + void (*_Nullable subgraph_add_child)(void *_Nullable context, AGSubgraphRef subgraph, AGSubgraphRef child); + void (*_Nullable subgraph_remove_child)(void *_Nullable context, AGSubgraphRef subgraph, AGSubgraphRef child); + + void (*_Nullable node_added)(void *_Nullable context, AGAttribute attribute); + void (*_Nullable node_add_edge)(void *_Nullable context, AGAttribute attribute, AGAttribute input, AGInputOptions input_options); + void (*_Nullable node_remove_edge)(void *_Nullable context, AGAttribute attribute, uint32_t index); + void (*_Nullable node_set_edge_pending)(void *_Nullable context, AGAttribute attribute, AGAttribute input, bool pending); + + void (*_Nullable node_set_dirty)(void *_Nullable context, AGAttribute attribute, bool dirty); + void (*_Nullable node_set_pending)(void *_Nullable context, AGAttribute attribute, bool pending); + void (*_Nullable node_set_value)(void *_Nullable context, AGAttribute attribute); + void (*_Nullable node_mark_value)(void *_Nullable context, AGAttribute attribute); + + void (*_Nullable indirect_node_added)(void *_Nullable context, AGAttribute attribute); + void (*_Nullable indirect_node_set_source)(void *_Nullable context, AGAttribute attribute, AGAttribute source); + void (*_Nullable indirect_node_set_dependency)(void *_Nullable context, AGAttribute attribute, AGAttribute dependency); + + void (*_Nullable profile_mark)(void *_Nullable context, const char *event_name); + + void (*_Nullable custom_event)(void *_Nullable context, AGGraphRef graph, const char *event_name, const void *value, + AGTypeID type); + void (*_Nullable named_event)(void *_Nullable context, AGGraphRef graph, uint32_t eventID, uint32_t eventArgCount, + const void *eventArgs, CFDataRef data, uint32_t arg6); + bool (*_Nullable named_event_enabled)(void *_Nullable context); + + void (*_Nullable set_deadline)(void *_Nullable context); + void (*_Nullable passed_deadline)(void *_Nullable context); + + void (*_Nullable compare_failed)(void *_Nullable context, AGAttribute attribute, AGComparisonState comparisonState); +} AGTraceType; + +AG_EXTERN_C_END + +AG_ASSUME_NONNULL_END diff --git a/Sources/ComputeCxx/include/ComputeCxx/ComputeCxx.h b/Sources/ComputeCxx/include/ComputeCxx/ComputeCxx.h index 57b361b..6abc3b9 100644 --- a/Sources/ComputeCxx/include/ComputeCxx/ComputeCxx.h +++ b/Sources/ComputeCxx/include/ComputeCxx/ComputeCxx.h @@ -3,20 +3,20 @@ #include #include #include -#include #include +#include #include #include #include #include #include #include +#include #include #include #include #include -#include -#include +#include #include #include #include From 69b37841297ff72fae412a0cab06ee27fdbf76fe Mon Sep 17 00:00:00 2001 From: James Moschou Date: Sat, 17 Jan 2026 18:31:48 +0100 Subject: [PATCH 2/6] Move AGGraph tracing functions to AGGraphTracing.h --- .../ComputeCxx/include/ComputeCxx/AGGraph.h | 83 ----------------- .../include/ComputeCxx/AGGraphTracing.h | 90 ++++++++++++++++++- 2 files changed, 86 insertions(+), 87 deletions(-) diff --git a/Sources/ComputeCxx/include/ComputeCxx/AGGraph.h b/Sources/ComputeCxx/include/ComputeCxx/AGGraph.h index 8d5fa2c..777f9cb 100644 --- a/Sources/ComputeCxx/include/ComputeCxx/AGGraph.h +++ b/Sources/ComputeCxx/include/ComputeCxx/AGGraph.h @@ -19,7 +19,6 @@ #include #include #include -#include #include #include #include @@ -36,8 +35,6 @@ AG_EXTERN_C_BEGIN typedef struct AG_BRIDGED_TYPE(id) AGGraphStorage *AGGraphRef AG_SWIFT_NAME(Graph); typedef void *AGUnownedGraphContextRef AG_SWIFT_STRUCT; -typedef struct AGTraceType *AGTraceTypeRef; - AG_EXPORT AG_REFINED_FOR_SWIFT CFTypeID AGGraphGetTypeID(void) AG_SWIFT_NAME(getter:AGGraphRef.typeID()); @@ -361,86 +358,6 @@ AG_EXPORT AG_REFINED_FOR_SWIFT void AGGraphSetOutputValue(const void *value, AGTypeID type); -// MARK: Trace - -AG_EXPORT -AG_REFINED_FOR_SWIFT -void AGGraphStartTracing(AGGraphRef _Nullable graph, AGGraphTraceOptions trace_options) - AG_SWIFT_NAME(AGGraphRef.startTracing(_:flags:)); - -AG_EXPORT -AG_REFINED_FOR_SWIFT -void AGGraphStartTracing2(AGGraphRef _Nullable graph, AGGraphTraceOptions trace_options, CFArrayRef _Nullable subsystems) - AG_SWIFT_NAME(AGGraphRef.startTracing(_:options:subsystems:)); - -AG_EXPORT -AG_REFINED_FOR_SWIFT -void AGGraphStopTracing(AGGraphRef _Nullable graph) AG_SWIFT_NAME(AGGraphRef.stopTracing(_:)); - -AG_EXPORT -AG_REFINED_FOR_SWIFT -void AGGraphSyncTracing(AGGraphRef graph) AG_SWIFT_NAME(AGGraphRef.syncTracing(self:)); - -AG_EXPORT -AG_REFINED_FOR_SWIFT -CFStringRef AGGraphCopyTracePath(AGGraphRef graph) AG_SWIFT_NAME(getter:AGGraphRef.tracePath(self:)); - -AG_EXPORT -AG_REFINED_FOR_SWIFT -uint64_t AGGraphAddTrace(AGGraphRef graph, const AGTraceTypeRef trace, void *_Nullable context) - AG_SWIFT_NAME(AGGraphRef.addTrace(self:_:context:)); - -AG_EXPORT -AG_REFINED_FOR_SWIFT -void AGGraphRemoveTrace(AGGraphRef graph, uint64_t trace_id) AG_SWIFT_NAME(AGGraphRef.removeTrace(self:traceID:)); - -AG_EXPORT -AG_REFINED_FOR_SWIFT -void AGGraphSetTrace(AGGraphRef graph, const AGTraceTypeRef trace, void *_Nullable context) - AG_SWIFT_NAME(AGGraphRef.setTrace(self:_:context:)); - -AG_EXPORT -AG_REFINED_FOR_SWIFT -void AGGraphResetTrace(AGGraphRef graph) AG_SWIFT_NAME(AGGraphRef.resetTrace(self:)); - -AG_EXPORT -AG_REFINED_FOR_SWIFT -bool AGGraphIsTracingActive(AGGraphRef graph) AG_SWIFT_NAME(getter:AGGraphRef.isTracingActive(self:)); - -AG_EXPORT -AG_REFINED_FOR_SWIFT -void AGGraphPrepareTrace(AGGraphRef graph, const AGTraceTypeRef trace, void *_Nullable context); - -AG_EXPORT -AG_REFINED_FOR_SWIFT -bool AGGraphTraceEventEnabled(AGGraphRef graph, uint32_t event_id) - AG_SWIFT_NAME(AGGraphRef.traceEventEnabled(self:for:)); - -AG_EXPORT -AG_REFINED_FOR_SWIFT -void AGGraphAddTraceEvent(AGGraphRef graph, const char *event_name, const void *value, AGTypeID type) - AG_SWIFT_NAME(AGGraphRef.addTraceEvent(self:name:value:type:)); - -AG_EXPORT -AG_REFINED_FOR_SWIFT -void AGGraphAddNamedTraceEvent(AGGraphRef graph, uint32_t event_id, uint32_t event_arg_count, const void *event_args, - CFDataRef data, uint32_t arg6) - AG_SWIFT_NAME(AGGraphRef.addNamedTraceEvent(self:eventID:eventArgCount:eventArgs:data:arg6:)); - -AG_EXPORT -AG_REFINED_FOR_SWIFT -const char *_Nullable AGGraphGetTraceEventName(uint32_t event_id) AG_SWIFT_NAME(AGGraphRef.traceEventName(for:)); - -AG_EXPORT -AG_REFINED_FOR_SWIFT -const char *_Nullable AGGraphGetTraceEventSubsystem(uint32_t event_id) - AG_SWIFT_NAME(AGGraphRef.traceEventSubsystem(for:)); - -AG_EXPORT -AG_REFINED_FOR_SWIFT -uint32_t AGGraphRegisterNamedTraceEvent(const char *event_name, const char *event_subsystem) - AG_SWIFT_NAME(AGGraphRef.registerNamedTraceEvent(name:subsystem:)); - // MARK: Description #if TARGET_OS_MAC diff --git a/Sources/ComputeCxx/include/ComputeCxx/AGGraphTracing.h b/Sources/ComputeCxx/include/ComputeCxx/AGGraphTracing.h index d7a9a3f..5d8e08e 100644 --- a/Sources/ComputeCxx/include/ComputeCxx/AGGraphTracing.h +++ b/Sources/ComputeCxx/include/ComputeCxx/AGGraphTracing.h @@ -1,10 +1,7 @@ #pragma once #include - -AG_ASSUME_NONNULL_BEGIN - -AG_EXTERN_C_BEGIN +#include typedef AG_OPTIONS(uint32_t, AGGraphTraceOptions) { AGGraphTraceOptionsEnabled = 1 << 0, @@ -15,6 +12,91 @@ typedef AG_OPTIONS(uint32_t, AGGraphTraceOptions) { AGGraphTraceOptionsAll = 1 << 5, } AG_SWIFT_NAME(AGGraphRef.TraceOptions); +typedef struct AGTraceType *AGTraceTypeRef; + +AG_ASSUME_NONNULL_BEGIN + +AG_EXTERN_C_BEGIN + +AG_EXPORT +AG_REFINED_FOR_SWIFT +void AGGraphStartTracing(AGGraphRef _Nullable graph, AGGraphTraceOptions trace_options) + AG_SWIFT_NAME(AGGraphRef.startTracing(_:flags:)); + +AG_EXPORT +AG_REFINED_FOR_SWIFT +void AGGraphStartTracing2(AGGraphRef _Nullable graph, AGGraphTraceOptions trace_options, + CFArrayRef _Nullable subsystems) + AG_SWIFT_NAME(AGGraphRef.startTracing(_:options:subsystems:)); + +AG_EXPORT +AG_REFINED_FOR_SWIFT +void AGGraphStopTracing(AGGraphRef _Nullable graph) AG_SWIFT_NAME(AGGraphRef.stopTracing(_:)); + +AG_EXPORT +AG_REFINED_FOR_SWIFT +void AGGraphSyncTracing(AGGraphRef graph) AG_SWIFT_NAME(AGGraphRef.syncTracing(self:)); + +AG_EXPORT +AG_REFINED_FOR_SWIFT +CFStringRef AGGraphCopyTracePath(AGGraphRef graph) AG_SWIFT_NAME(getter:AGGraphRef.tracePath(self:)); + +AG_EXPORT +AG_REFINED_FOR_SWIFT +uint64_t AGGraphAddTrace(AGGraphRef graph, const AGTraceTypeRef trace, void *_Nullable context) + AG_SWIFT_NAME(AGGraphRef.addTrace(self:_:context:)); + +AG_EXPORT +AG_REFINED_FOR_SWIFT +void AGGraphRemoveTrace(AGGraphRef graph, uint64_t trace_id) AG_SWIFT_NAME(AGGraphRef.removeTrace(self:traceID:)); + +AG_EXPORT +AG_REFINED_FOR_SWIFT +void AGGraphSetTrace(AGGraphRef graph, const AGTraceTypeRef trace, void *_Nullable context) + AG_SWIFT_NAME(AGGraphRef.setTrace(self:_:context:)); + +AG_EXPORT +AG_REFINED_FOR_SWIFT +void AGGraphResetTrace(AGGraphRef graph) AG_SWIFT_NAME(AGGraphRef.resetTrace(self:)); + +AG_EXPORT +AG_REFINED_FOR_SWIFT +bool AGGraphIsTracingActive(AGGraphRef graph) AG_SWIFT_NAME(getter:AGGraphRef.isTracingActive(self:)); + +AG_EXPORT +AG_REFINED_FOR_SWIFT +void AGGraphPrepareTrace(AGGraphRef graph, const AGTraceTypeRef trace, void *_Nullable context); + +AG_EXPORT +AG_REFINED_FOR_SWIFT +bool AGGraphTraceEventEnabled(AGGraphRef graph, uint32_t event_id) + AG_SWIFT_NAME(AGGraphRef.traceEventEnabled(self:for:)); + +AG_EXPORT +AG_REFINED_FOR_SWIFT +void AGGraphAddTraceEvent(AGGraphRef graph, const char *event_name, const void *value, AGTypeID type) + AG_SWIFT_NAME(AGGraphRef.addTraceEvent(self:name:value:type:)); + +AG_EXPORT +AG_REFINED_FOR_SWIFT +void AGGraphAddNamedTraceEvent(AGGraphRef graph, uint32_t event_id, uint32_t event_arg_count, const void *event_args, + CFDataRef data, uint32_t arg6) + AG_SWIFT_NAME(AGGraphRef.addNamedTraceEvent(self:eventID:eventArgCount:eventArgs:data:arg6:)); + +AG_EXPORT +AG_REFINED_FOR_SWIFT +const char *_Nullable AGGraphGetTraceEventName(uint32_t event_id) AG_SWIFT_NAME(AGGraphRef.traceEventName(for:)); + +AG_EXPORT +AG_REFINED_FOR_SWIFT +const char *_Nullable AGGraphGetTraceEventSubsystem(uint32_t event_id) + AG_SWIFT_NAME(AGGraphRef.traceEventSubsystem(for:)); + +AG_EXPORT +AG_REFINED_FOR_SWIFT +uint32_t AGGraphRegisterNamedTraceEvent(const char *event_name, const char *event_subsystem) + AG_SWIFT_NAME(AGGraphRef.registerNamedTraceEvent(name:subsystem:)); + AG_EXTERN_C_END AG_ASSUME_NONNULL_END From 61ca73845149d0849871b8dabf593b1ebf620353 Mon Sep 17 00:00:00 2001 From: James Moschou Date: Sat, 17 Jan 2026 18:41:32 +0100 Subject: [PATCH 3/6] Parse AG_TRACE environment variable on startup --- Sources/ComputeCxx/Graph/Graph.cpp | 57 ++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/Sources/ComputeCxx/Graph/Graph.cpp b/Sources/ComputeCxx/Graph/Graph.cpp index 342c3b2..534f554 100644 --- a/Sources/ComputeCxx/Graph/Graph.cpp +++ b/Sources/ComputeCxx/Graph/Graph.cpp @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -47,6 +48,62 @@ Graph::Graph() _types.push_back(nullptr); + static auto [trace_options, trace_subsystems] = + []() -> std::tuple, 0, uint64_t>> { + // TODO: debug server + // TODO: profile + + vector, 0, uint64_t> trace_subsystems = {}; + + AGGraphTraceOptions trace_options = 0; + const char *trace_string = getenv("AG_TRACE"); + if (trace_string) { + char *endptr = nullptr; + trace_options = (uint32_t)strtol(trace_string, &endptr, 0); + + if (endptr) { + const char *c = endptr + strspn(endptr, ", \t\n\f\r"); + while (c) { + size_t option_length = strcspn(c, ", \t\n\f\r"); + + char *option = (char *)malloc(option_length + 1); + memcpy(option, c, option_length); + option[option_length] = 0; + + if (strcasecmp(option, "enabled") == 0) { + trace_options |= AGGraphTraceOptionsEnabled; + free(option); + } else if (strcasecmp(option, "full") == 0) { + trace_options |= AGGraphTraceOptionsFull; + free(option); + } else if (strcasecmp(option, "backtrace") == 0) { + trace_options |= AGGraphTraceOptionsBacktrace; + free(option); + } else if (strcasecmp(option, "prepare") == 0) { + trace_options |= AGGraphTraceOptionsPrepare; + free(option); + } else if (strcasecmp(option, "custom") == 0) { + trace_options |= AGGraphTraceOptionsCustom; + free(option); + } else if (strcasecmp(option, "all") == 0) { + trace_options |= AGGraphTraceOptionsAll; + free(option); + } else { + trace_subsystems.push_back(std::unique_ptr(option)); + } + + c += strspn(c + option_length, ", \t\n\f\r"); + } + } + } + + return {trace_options, std::move(trace_subsystems)}; + }(); + + if (trace_options && !trace_subsystems.empty()) { + start_tracing(trace_options, std::span((const char **)trace_subsystems.data(), trace_subsystems.size())); + } + // Prepend this graph all_lock(); _next = _all_graphs; From d285e92c324bdb0c82b56e2cff1e2a33dc35522e Mon Sep 17 00:00:00 2001 From: James Moschou Date: Sat, 17 Jan 2026 19:01:09 +0100 Subject: [PATCH 4/6] Trace process exiting --- Sources/ComputeCxx/Graph/Graph.cpp | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/Sources/ComputeCxx/Graph/Graph.cpp b/Sources/ComputeCxx/Graph/Graph.cpp index 534f554..0e19ca2 100644 --- a/Sources/ComputeCxx/Graph/Graph.cpp +++ b/Sources/ComputeCxx/Graph/Graph.cpp @@ -99,7 +99,7 @@ Graph::Graph() return {trace_options, std::move(trace_subsystems)}; }(); - + if (trace_options && !trace_subsystems.empty()) { start_tracing(trace_options, std::span((const char **)trace_subsystems.data(), trace_subsystems.size())); } @@ -1884,18 +1884,25 @@ void *Graph::output_value_ref(data::ptr node, const swift::metadata &value #pragma mark - Trace void Graph::start_tracing(AGGraphTraceOptions trace_options, std::span subsystems) { - if (trace_options & AGGraphTraceOptionsEnabled && _trace_recorder == nullptr) { - _trace_recorder = new TraceRecorder(this, trace_options, subsystems); - if (trace_options & AGGraphTraceOptionsPrepare) { - prepare_trace(*_trace_recorder); - } - add_trace(_trace_recorder); + if ((trace_options & AGGraphTraceOptionsEnabled) == 0) { + return; + } + if (_trace_recorder) { + return; + } - static platform_once_t cleanup; - platform_once(&cleanup, []() { - // TODO - }); + _trace_recorder = new TraceRecorder(this, trace_options, subsystems); + if (trace_options & AGGraphTraceOptionsPrepare) { + prepare_trace(*_trace_recorder); } + add_trace(_trace_recorder); + + static platform_once_t cleanup; + platform_once(&cleanup, []() { + atexit([]() { + Graph::trace_assertion_failure(true, "process exiting"); + }); + }); } void Graph::stop_tracing() { From b9cd5a97e83b444b85b4e2b84bd4afd415d301b2 Mon Sep 17 00:00:00 2001 From: James Moschou Date: Sat, 17 Jan 2026 19:19:50 +0100 Subject: [PATCH 5/6] Implement Graph::prepare_trace --- .../Attribute/AttributeView/AttributeView.h | 9 +-- Sources/ComputeCxx/Graph/Graph.cpp | 72 ++++++++++++++++++- 2 files changed, 74 insertions(+), 7 deletions(-) diff --git a/Sources/ComputeCxx/Attribute/AttributeView/AttributeView.h b/Sources/ComputeCxx/Attribute/AttributeView/AttributeView.h index 9df36b3..2746c0c 100644 --- a/Sources/ComputeCxx/Attribute/AttributeView/AttributeView.h +++ b/Sources/ComputeCxx/Attribute/AttributeView/AttributeView.h @@ -38,7 +38,7 @@ class attribute_iterator { }; class attribute_view { - private: + protected: data::ptr _page; public: @@ -48,12 +48,9 @@ class attribute_view { attribute_iterator end() { return attribute_iterator(_page, RelativeAttributeID(nullptr)); }; }; -class const_attribute_view { - private: - data::ptr _page; - +class const_attribute_view: public attribute_view { public: - const_attribute_view(data::ptr page) : _page(page) {}; + const_attribute_view(data::ptr page) : attribute_view(page) {}; attribute_iterator begin() { return attribute_iterator(_page, RelativeAttributeID(_page->const_bytes_list)); }; attribute_iterator end() { return attribute_iterator(_page, RelativeAttributeID(nullptr)); }; diff --git a/Sources/ComputeCxx/Graph/Graph.cpp b/Sources/ComputeCxx/Graph/Graph.cpp index 0e19ca2..380f929 100644 --- a/Sources/ComputeCxx/Graph/Graph.cpp +++ b/Sources/ComputeCxx/Graph/Graph.cpp @@ -1925,7 +1925,77 @@ CFStringRef Graph::copy_trace_path() { } void Graph::prepare_trace(Trace &trace) { - // TODO: Not implemented + _contexts_by_id.for_each([](const uint64_t context_id, Context *const context, + void *trace_ref) { ((Trace *)trace_ref)->created(*context); }, + &trace); + for (auto subgraph : _subgraphs) { + trace.created(*subgraph); + } + for (auto subgraph : _subgraphs) { + for (auto child : subgraph->children()) { + trace.add_child(*subgraph, *child.subgraph()); + } + } + for (auto subgraph : _subgraphs) { + for (uint32_t iteration = 0; iteration < 2; ++iteration) { + for (auto page : subgraph->pages()) { + bool found_nil_attribute = false; + auto view = iteration == 0 ? const_attribute_view(page) : attribute_view(page); + for (auto attribute : view) { + if (auto node = attribute.get_node()) { + trace.added(node); + if (node->is_dirty()) { + trace.set_dirty(node, true); + } + if (node->is_pending()) { + trace.set_pending(node, true); + } + if (node->is_value_initialized()) { + void *value = node->get_value(); + trace.set_value(node, value); + } + } else if (auto indirect_node = attribute.get_indirect_node()) { + trace.added(indirect_node); + } else if (attribute.is_nil()) { + found_nil_attribute = true; + break; + } + } + if (found_nil_attribute) { + break; + } + } + } + } + for (auto subgraph : _subgraphs) { + for (uint32_t iteration = 0; iteration < 2; ++iteration) { + for (auto page : subgraph->pages()) { + bool found_nil_attribute = false; + auto view = iteration == 0 ? const_attribute_view(page) : attribute_view(page); + for (auto attribute : view) { + if (auto node = attribute.get_node()) { + for (auto input_edge : node->input_edges()) { + trace.add_edge(node, input_edge.attribute, input_edge.options); + if (input_edge.options & AGInputOptionsChanged) { + trace.set_edge_pending(node, input_edge.attribute, true); + } + } + } else if (auto indirect_node = attribute.get_indirect_node()) { + trace.set_source(indirect_node, indirect_node->source().identifier()); + if (indirect_node->is_mutable() && indirect_node->to_mutable().dependency() != 0) { + trace.set_dependency(indirect_node, indirect_node->to_mutable().dependency()); + } + } else if (attribute.is_nil()) { + found_nil_attribute = true; + break; + } + } + if (found_nil_attribute) { + break; + } + } + } + } } void Graph::add_trace(Trace *_Nullable trace) { From 29a3d179a0eccd64b34f3f9c9f945a37fea17aab Mon Sep 17 00:00:00 2001 From: James Moschou Date: Sat, 17 Jan 2026 19:33:26 +0100 Subject: [PATCH 6/6] Fix Swift name --- Sources/ComputeCxx/include/ComputeCxx/AGGraphTracing.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/ComputeCxx/include/ComputeCxx/AGGraphTracing.h b/Sources/ComputeCxx/include/ComputeCxx/AGGraphTracing.h index 5d8e08e..efa4009 100644 --- a/Sources/ComputeCxx/include/ComputeCxx/AGGraphTracing.h +++ b/Sources/ComputeCxx/include/ComputeCxx/AGGraphTracing.h @@ -21,7 +21,7 @@ AG_EXTERN_C_BEGIN AG_EXPORT AG_REFINED_FOR_SWIFT void AGGraphStartTracing(AGGraphRef _Nullable graph, AGGraphTraceOptions trace_options) - AG_SWIFT_NAME(AGGraphRef.startTracing(_:flags:)); + AG_SWIFT_NAME(AGGraphRef.startTracing(_:options:)); AG_EXPORT AG_REFINED_FOR_SWIFT