Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions Sources/ComputeCxx/Attribute/AttributeView/AttributeView.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class attribute_iterator {
};

class attribute_view {
private:
protected:
data::ptr<data::page> _page;

public:
Expand All @@ -48,12 +48,9 @@ class attribute_view {
attribute_iterator end() { return attribute_iterator(_page, RelativeAttributeID(nullptr)); };
};

class const_attribute_view {
private:
data::ptr<data::page> _page;

class const_attribute_view: public attribute_view {
public:
const_attribute_view(data::ptr<data::page> page) : _page(page) {};
const_attribute_view(data::ptr<data::page> 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)); };
Expand Down
14 changes: 7 additions & 7 deletions Sources/ComputeCxx/Graph/AGGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::unique_ptr<const char, util::free_deleter>, 0, uint64_t>();
if (subsystems) {
auto subsystems_count = CFArrayGetCount(subsystems);
Expand All @@ -928,12 +928,12 @@ void AGGraphStartTracing2(AGGraphRef graph, AGTraceFlags trace_flags, CFArrayRef
std::span<const char *>((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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand All @@ -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);
Expand Down
164 changes: 149 additions & 15 deletions Sources/ComputeCxx/Graph/Graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <ranges>
#include <set>

#include <Utilities/FreeDeleter.h>
#include <Utilities/List.h>
#include <platform/lock.h>
#include <platform/once.h>
Expand All @@ -19,7 +20,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"
Expand Down Expand Up @@ -47,6 +48,62 @@ Graph::Graph()

_types.push_back(nullptr);

static auto [trace_options, trace_subsystems] =
[]() -> std::tuple<uint32_t, vector<std::unique_ptr<const char, util::free_deleter>, 0, uint64_t>> {
// TODO: debug server
// TODO: profile

vector<std::unique_ptr<const char, util::free_deleter>, 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<const char, util::free_deleter>(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;
Expand Down Expand Up @@ -1826,19 +1883,26 @@ void *Graph::output_value_ref(data::ptr<Node> node, const swift::metadata &value

#pragma mark - Trace

void Graph::start_tracing(AGTraceFlags trace_flags, std::span<const char *> subsystems) {
if (trace_flags & AGTraceFlagsEnabled && _trace_recorder == nullptr) {
_trace_recorder = new TraceRecorder(this, trace_flags, subsystems);
if (trace_flags & AGTraceFlagsPrepare) {
prepare_trace(*_trace_recorder);
}
add_trace(_trace_recorder);
void Graph::start_tracing(AGGraphTraceOptions trace_options, std::span<const char *> subsystems) {
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() {
Expand All @@ -1861,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) {
Expand All @@ -1883,10 +2017,10 @@ void Graph::remove_trace(uint64_t trace_id) {
}
}

void Graph::all_start_tracing(AGTraceFlags trace_flags, std::span<const char *> span) {
void Graph::all_start_tracing(AGGraphTraceOptions trace_options, std::span<const char *> 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();
}
Expand Down
10 changes: 5 additions & 5 deletions Sources/ComputeCxx/Graph/Graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
#endif
#endif

#include <platform/lock.h>
#include <Utilities/HashTable.h>
#include <Utilities/Heap.h>
#include <Utilities/TaggedPointer.h>
#include <platform/lock.h>

#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"

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -389,7 +389,7 @@ class Graph {

// MARK: Trace

void start_tracing(AGTraceFlags trace_flags, std::span<const char *> subsystems);
void start_tracing(AGGraphTraceOptions trace_options, std::span<const char *> subsystems);
void stop_tracing();
void sync_tracing();
CFStringRef copy_trace_path();
Expand All @@ -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<const char *> span);
static void all_start_tracing(AGGraphTraceOptions trace_options, std::span<const char *> span);
static void all_stop_tracing();
static void all_sync_tracing();
static CFStringRef all_copy_trace_path();
Expand Down
6 changes: 3 additions & 3 deletions Sources/ComputeCxx/Graph/TraceRecorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -15,7 +15,7 @@

namespace AG {

Graph::TraceRecorder::TraceRecorder(Graph *graph, AGTraceFlags trace_flags, std::span<const char *> subsystems) {
Graph::TraceRecorder::TraceRecorder(Graph *graph, AGGraphTraceOptions trace_options, std::span<const char *> subsystems) {
// TODO: not implemented
}

Expand Down Expand Up @@ -139,7 +139,7 @@ void Graph::TraceRecorder::added(data::ptr<Node> node) {
// TODO: not implemented
}

void Graph::TraceRecorder::add_edge(data::ptr<Node> node, AttributeID input, uint8_t input_edge_flags) {
void Graph::TraceRecorder::add_edge(data::ptr<Node> node, AttributeID input, AGInputOptions input_options) {
// TODO: not implemented
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/ComputeCxx/Graph/TraceRecorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace AG {

class Graph::TraceRecorder : public Trace {
public:
TraceRecorder(Graph *graph, AGTraceFlags trace_flags, std::span<const char *> subsystems);
TraceRecorder(Graph *graph, AGGraphTraceOptions trace_options, std::span<const char *> subsystems);
~TraceRecorder();

uint64_t id() { return _id; };
Expand Down Expand Up @@ -65,7 +65,7 @@ class Graph::TraceRecorder : public Trace {

void added(data::ptr<Node> node) override;

void add_edge(data::ptr<Node> node, AttributeID input, uint8_t input_edge_flags) override;
void add_edge(data::ptr<Node> node, AttributeID input, AGInputOptions input_options) override;
void remove_edge(data::ptr<Node> node, uint32_t input_index) override;
void set_edge_pending(data::ptr<Node> node, AttributeID input, bool pending) override;

Expand Down
Loading