Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
task_registry_debug.hpp
Go to the documentation of this file.
1#pragma once
2
9
11
12#include <coroutine>
13#include <cstdio>
14#include <exception>
15#include <string>
16#include <vector>
17
18namespace uni20::async
19{
20class EpochContext;
21class NodeInfo;
22} // namespace uni20::async
23
24namespace uni20
25{
26
28class TaskRegistryDebug;
29
32
35 public:
37 enum class DumpMode
38 {
39 None,
40 Basic,
41 Full,
42 };
43
45 enum class TaskState
46 {
48 Running,
50 Failed,
51 Leaked,
52 };
53
55 enum class EpochTaskRole
56 {
57 Reader,
58 Writer,
59 };
60
63 {
64 std::string output_dir{"/tmp"};
65 std::string file_prefix{"uni20-dag"};
66 };
67
75
85
88
91
94
97 static void register_task(std::coroutine_handle<> h);
100 static void destroy_task(std::coroutine_handle<> h);
103 static void leak_task(std::coroutine_handle<> h);
106 static void mark_running(std::coroutine_handle<> h);
109 static void mark_suspended(std::coroutine_handle<> h);
114 static void record_unhandled_exception(std::coroutine_handle<> h, std::exception_ptr exception,
115 bool originating_failure) noexcept;
118 static void record_task_scheduled(std::coroutine_handle<> h);
123 static void record_task_dependencies(std::coroutine_handle<> h,
124 std::vector<async::NodeInfo const*> const& read_dependencies,
125 std::vector<async::NodeInfo const*> const& write_dependencies);
130 static void record_await_dependency(std::coroutine_handle<> h, async::NodeInfo const* node, EpochTaskRole role);
134 static void name_task(std::coroutine_handle<> h, std::string const& label);
138 static void name_async_value(async::NodeInfo const* node, std::string const& label);
141 static void register_epoch_context(async::EpochContext const* epoch_context);
144 static void destroy_epoch_context(async::EpochContext const* epoch_context);
149 static void bind_epoch_task(async::EpochContext const* epoch_context, std::coroutine_handle<> h,
150 EpochTaskRole role);
155 static void unbind_epoch_task(async::EpochContext const* epoch_context, std::coroutine_handle<> h,
156 EpochTaskRole role);
160 static std::vector<std::coroutine_handle<>> epoch_reader_tasks(async::EpochContext const* epoch_context);
164 static std::vector<std::coroutine_handle<>> epoch_writer_tasks(async::EpochContext const* epoch_context);
167 static DumpMode dump_mode() noexcept;
171 static void dump_epoch_context(async::EpochContext const* epoch_context, char const* reason = nullptr);
184 static std::string graphviz_dot();
191 static std::string graphviz_dot(GraphSnapshot const& snapshot);
196 static std::string graphviz_dot(GraphSnapshot const& snapshot, GraphDiagnostics const& diagnostics);
199 static void dump_graphviz(std::FILE* stream = stderr);
203 static bool dump_graphviz_file(std::string const& path);
207 static bool dump_graphviz_file_best_effort(std::string const& path);
233 static void set_stacktrace_options(StacktraceOptions const& options);
242 static std::string default_graphviz_dump_path(GraphvizDumpOptions const& options);
244 static void request_graphviz_dump() noexcept;
251 static bool service_debug_requests(GraphvizDumpOptions const& options);
260 static void stop_diagnostics_service() noexcept;
262 static void dump();
263};
264
265} // namespace uni20
266
267namespace uni20::detail
268{
269
270void initialize_task_registry_diagnostics_from_environment() noexcept;
271void finalize_task_registry_diagnostics_from_environment() noexcept;
272
273namespace
274{
275// One guard per translation unit gives later namespace-scope users ordered access to the diagnostics runtime.
276[[maybe_unused]] nifty_counter<initialize_task_registry_diagnostics_from_environment,
277 finalize_task_registry_diagnostics_from_environment> task_registry_diagnostics_lifetime;
278} // namespace
279
280} // namespace uni20::detail
Tracks coroutine and epoch-context lifecycle events in debug builds.
Definition task_registry_debug.hpp:34
static GraphvizDumpOptions default_graphviz_dump_options()
Returns default Graphviz file-output options.
static std::string graphviz_dot()
Returns a Graphviz DOT snapshot of the currently tracked async DAG.
static void register_task(std::coroutine_handle<> h)
Registers a newly-created coroutine handle.
static StacktraceOptions default_stacktrace_options()
Returns default stacktrace formatting options.
static void dump_graphviz(std::FILE *stream=stderr)
Prints a Graphviz DOT snapshot to a C stream.
static GraphSnapshot snapshot_best_effort()
Captures a best-effort task/epoch/value DAG snapshot without indefinite lock waits.
static GraphSnapshot snapshot()
Captures the current task/epoch/value DAG as structured data.
static void request_graphviz_dump() noexcept
Queues one Graphviz dump request for scheduler or service processing.
static void leak_task(std::coroutine_handle<> h)
Marks a coroutine handle as leaked.
static void record_task_dependencies(std::coroutine_handle<> h, std::vector< async::NodeInfo const * > const &read_dependencies, std::vector< async::NodeInfo const * > const &write_dependencies)
Records constructor-discovered coarse DAG dependencies for a coroutine.
static std::vector< std::coroutine_handle<> > epoch_reader_tasks(async::EpochContext const *epoch_context)
Returns coroutine handles currently bound as readers.
static bool service_debug_requests()
Processes pending dump requests on the calling thread.
static void bind_epoch_task(async::EpochContext const *epoch_context, std::coroutine_handle<> h, EpochTaskRole role)
Associates a coroutine with an epoch context as reader or writer.
static void reset_stacktrace_options()
Resets active stacktrace formatting options from environment defaults.
static void reset_coroutine_exception_diagnostics_options()
Resets the automatic coroutine-exception diagnostics policy from the environment.
TaskState
Logical lifecycle states tracked for each coroutine handle.
Definition task_registry_debug.hpp:46
static void name_async_value(async::NodeInfo const *node, std::string const &label)
Assigns an optional human-readable label to an async value node.
static void name_task(std::coroutine_handle<> h, std::string const &label)
Assigns an optional human-readable label to a coroutine task.
static bool start_diagnostics_service()
Starts the optional background DAG diagnostics service.
static std::string graphviz_dot_best_effort()
Returns a best-effort Graphviz DOT snapshot without waiting indefinitely for locks.
static void destroy_task(std::coroutine_handle<> h)
Marks a coroutine handle as destroyed.
static bool dump_graphviz_file_best_effort(std::string const &path)
Writes a best-effort Graphviz DOT snapshot to a file.
static bool dump_graphviz_file(std::string const &path)
Writes a Graphviz DOT snapshot to a file.
static DiagnosticsServiceOptions default_diagnostics_service_options()
Returns default diagnostics-service options.
static void stop_diagnostics_service() noexcept
Stops the background DAG diagnostics service if it is running.
static void mark_suspended(std::coroutine_handle<> h)
Marks a coroutine handle as suspended.
static void dump()
Prints global task-registry diagnostics.
static void mark_running(std::coroutine_handle<> h)
Marks a coroutine handle as currently running.
static void record_unhandled_exception(std::coroutine_handle<> h, std::exception_ptr exception, bool originating_failure) noexcept
Records an exception escaping a coroutine and optionally captures its live DAG.
static void set_coroutine_exception_diagnostics_options(CoroutineExceptionDiagnosticsOptions const &options)
Replaces the process-wide automatic coroutine-exception diagnostics policy.
static void register_epoch_context(async::EpochContext const *epoch_context)
Registers an epoch context for debug tracking.
static CoroutineExceptionDiagnosticsOptions coroutine_exception_diagnostics_options()
Returns the active automatic coroutine-exception diagnostics policy.
EpochTaskRole
Role of a coroutine relative to an epoch context.
Definition task_registry_debug.hpp:56
static CoroutineExceptionDiagnosticsOptions default_coroutine_exception_diagnostics_options()
Returns the environment-derived automatic coroutine-exception diagnostics policy.
static void record_await_dependency(std::coroutine_handle<> h, async::NodeInfo const *node, EpochTaskRole role)
Records a buffer dependency discovered at a concrete co_await site.
static std::vector< std::coroutine_handle<> > epoch_writer_tasks(async::EpochContext const *epoch_context)
Returns coroutine handles currently bound as writers.
DumpMode
Verbosity levels for registry dump output.
Definition task_registry_debug.hpp:38
static StacktraceOptions stacktrace_options()
Returns active stacktrace formatting options.
static void dump_epoch_context(async::EpochContext const *epoch_context, char const *reason=nullptr)
Prints diagnostics for a specific epoch context.
static void unbind_epoch_task(async::EpochContext const *epoch_context, std::coroutine_handle<> h, EpochTaskRole role)
Removes a coroutine-role binding from an epoch context.
static std::string default_graphviz_dump_path()
Creates a default DOT output path for the current process.
static GraphDiagnostics diagnose_snapshot(GraphSnapshot const &snapshot)
Diagnoses blocked tasks, missing writers, and dependency cycles in a graph snapshot.
static DumpMode dump_mode() noexcept
Reports the current debug dump mode.
static void record_task_scheduled(std::coroutine_handle<> h)
Records the call stack where a coroutine was submitted to a scheduler.
static void set_stacktrace_options(StacktraceOptions const &options)
Replaces active stacktrace formatting options.
static void destroy_epoch_context(async::EpochContext const *epoch_context)
Removes an epoch context from debug tracking.
No-op task registry implementation for non-debug async builds.
Definition task_registry_dummy.hpp:26
Definition epoch_context.hpp:169
Records node information for DAG/debugging visualization.
Definition async_node.hpp:171
Runs shared initialization before namespace-scope users and shared cleanup after them.
Definition nifty_counter.hpp:26
Definition qnum.hpp:403
This represents the reverse-mode adjoint for functions of complex variables, assuming a real-valued s...
Definition async/async.hpp:26
Definition async/async.hpp:26
Provides ordered one-time initialization across translation units.
Runtime policy for diagnostics captured when an exception escapes a coroutine.
Definition task_registry_debug.hpp:70
GraphvizDumpOptions dump_options
Definition task_registry_debug.hpp:73
bool enabled
Definition task_registry_debug.hpp:71
bool write_graphviz
Definition task_registry_debug.hpp:72
Options for the opt-in background DAG diagnostics service.
Definition task_registry_debug.hpp:78
int signal_number
Definition task_registry_debug.hpp:80
std::string request_file
Definition task_registry_debug.hpp:81
int poll_interval_ms
Definition task_registry_debug.hpp:82
bool block_signal_in_calling_thread
Definition task_registry_debug.hpp:83
GraphvizDumpOptions dump_options
Definition task_registry_debug.hpp:79
File-output options for Graphviz DAG dumps.
Definition task_registry_debug.hpp:63
std::string file_prefix
Definition task_registry_debug.hpp:65
std::string output_dir
Definition task_registry_debug.hpp:64
Diagnostics derived from a structured task-registry graph snapshot.
Definition task_registry_snapshot.hpp:114
Structured task-registry graph snapshot.
Definition task_registry_snapshot.hpp:104
Runtime controls for formatting captured task-registry stacktraces.
Definition task_registry_snapshot.hpp:33
Structured async task-registry graph snapshots and diagnostics.