Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
diagnostics.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <atomic>
6#include <cstddef>
7#include <cstdint>
8#include <expected>
9#include <mutex>
10#include <string>
11#include <string_view>
12#include <vector>
13
14namespace portals {
37
59
69
76
83
86 std::uint64_t session{};
87 std::size_t slot{};
88 std::uint64_t generation{};
89};
90
93 std::uint64_t correlationId{};
94 std::uint64_t parentCorrelationId{};
95 std::uint64_t period{};
96 std::uint64_t publicationId{};
97 std::uint64_t frameId{};
98};
99
113
116 std::size_t capacity{1024};
117};
118
121 std::uint32_t schemaVersion{1};
124 std::uint64_t period{};
125 std::uint64_t periodBegin{};
126 std::uint64_t periodEnd{};
127 std::uint64_t droppedOverflow{};
128 std::uint64_t droppedContention{};
130 bool incomplete{};
131 // The count constructor lets Debug iterator-proxy allocation failures propagate.
132 std::vector<DiagnosticRecord> records = std::vector<DiagnosticRecord>(0);
133};
134class DiagnosticScope;
135
142public:
144 Diagnostics() = default;
145 Diagnostics(const Diagnostics &) = delete;
146 Diagnostics &operator=(const Diagnostics &) = delete;
151 std::expected<void, DiagnosticError> enable(DiagnosticOptions options = {});
153 void disable() noexcept;
157 bool enabled() const noexcept;
161 std::uint64_t nextIdentity() noexcept;
170 DiagnosticContext current() const noexcept;
181 DiagnosticNodeId node = {}) noexcept;
186
187private:
188 friend class DiagnosticScope;
189 void lostContention() noexcept;
190 mutable std::mutex mutex_;
191 std::atomic<DiagnosticState> state_{DiagnosticState::DISABLED};
192 std::atomic<std::uint64_t> identity_{};
193 std::atomic<std::uint64_t> period_{};
194 std::atomic<std::uint64_t> contention_{};
195 std::atomic<bool> saturated_{};
196 DiagnosticOptions options_{};
197 std::vector<DiagnosticRecord> ring_;
198 std::size_t start_{};
199 std::size_t count_{};
200 std::uint64_t sequence_{};
201 std::uint64_t begin_{};
202 std::uint64_t end_{};
203 std::uint64_t overflow_{};
204};
205
211public:
216 DiagnosticScope(Diagnostics *diagnostics, DiagnosticContext context) noexcept;
219 DiagnosticScope(const DiagnosticScope &) = delete;
220 DiagnosticScope &operator=(const DiagnosticScope &) = delete;
224 DiagnosticReason outcome() const noexcept;
228 bool frameRequested() const noexcept;
229
230private:
231 friend class Diagnostics;
232 static thread_local DiagnosticScope *active_;
233 Diagnostics *diagnostics_{};
234 DiagnosticContext context_{};
235 DiagnosticScope *previous_{};
236 bool changed_{};
237 bool unchanged_{};
238 bool frame_requested_{};
239};
240
245std::string exportDiagnostics(const DiagnosticSnapshot &snapshot);
250std::expected<void, DiagnosticError> validateDiagnosticVersion(std::string_view text) noexcept;
251} // namespace portals
Stack-only causal context on one thread; restores the previous scope on every exit....
Definition diagnostics.hpp:210
~DiagnosticScope()
Restore the enclosing context.
DiagnosticReason outcome() const noexcept
Summarize only property evidence observed in this scope.
DiagnosticScope(Diagnostics *diagnostics, DiagnosticContext context) noexcept
Install a borrowed context without allocation.
bool frameRequested() const noexcept
Inspect actual invalidation evidence in this scope.
std::expected< void, DiagnosticError > enable(DiagnosticOptions options={})
Start a fresh recording interval; retain monotonic identities across intervals.
DiagnosticContext current() const noexcept
Obtain this thread's explicitly installed context for this recorder.
std::uint64_t nextIdentity() noexcept
Issue a never-reused identity only while enabled.
DiagnosticContext beginInput(DiagnosticInput input) noexcept
Record the input boundary before publishing to the scheduler.
bool enabled() const noexcept
Fast recording gate; safe on any thread.
void record(DiagnosticRecord record) noexcept
Attempt one bounded append; disabled calls return before touching counters.
Diagnostics()=default
Construct a disabled recorder without allocating ring storage.
void disable() noexcept
Stop admission and preserve retained evidence for export.
void note(DiagnosticEvent event, DiagnosticReason reason=DiagnosticReason::NONE, DiagnosticNodeId node={}) noexcept
Record a common synchronous stage using the current context.
DiagnosticSnapshot snapshot() const
Snapshot outside frame hooks; may allocate and throw std::bad_alloc.
Public Portals API.
Definition action.hpp:8
@ NO_TARGET
No eligible destination was selected.
Definition action.hpp:56
@ UNHANDLED
An eligible destination existed but no handler consumed the event.
Definition action.hpp:57
DiagnosticInput
Input category only; scalar, clipboard, modifiers and coordinates are omitted.
Definition diagnostics.hpp:61
@ OTHER
Another instrumented input category.
Definition diagnostics.hpp:67
@ KEY
Key category without scalar or modifiers.
Definition diagnostics.hpp:63
@ TEXT
Text category without content.
Definition diagnostics.hpp:64
@ PASTE
Paste category without clipboard or payload.
Definition diagnostics.hpp:65
ui::ValueText text(std::format_string< const ui::detail::TextArgumentValue< Args > &... > pattern, Args &&...args)
Declare live formatted text over explicit scalar roots, fields or value properties.
Definition ui_value_text.hpp:567
DiagnosticState
Recording state, including a terminal identity exhaustion condition.
Definition diagnostics.hpp:71
@ DISABLED
Recording is stopped; retained evidence remains available.
Definition diagnostics.hpp:72
@ ENABLED
Ring admission is active.
Definition diagnostics.hpp:73
DiagnosticReason
Enumerated evidence, never an exception message or inferred model value.
Definition diagnostics.hpp:39
@ HANDLER_FAILED
Handler boundary caught a failure; prior effects remain.
Definition diagnostics.hpp:49
@ OUTPUT_ACCEPTED
Transport accepted the complete consistent transaction.
Definition diagnostics.hpp:56
@ VALUE_UNCHANGED
An observed Property comparison was equal.
Definition diagnostics.hpp:50
@ UNBOUND_ACTION
No eligible handler was registered along the route.
Definition diagnostics.hpp:47
@ OUTCOME_UNKNOWN
Return alone cannot establish application effects.
Definition diagnostics.hpp:52
@ TARGET_DISABLED
Explicit target is hidden or disabled.
Definition diagnostics.hpp:45
@ OUTPUT_FAILED
Encoding or output failed; no rollback is implied.
Definition diagnostics.hpp:55
@ OUTPUT_PENDING
Transport retained an unfinished suffix.
Definition diagnostics.hpp:57
@ STALE_TARGET
Target identity is no longer live.
Definition diagnostics.hpp:46
@ DIFF_EMPTY
The actual encoded diff was empty.
Definition diagnostics.hpp:54
@ REPLACED
A later snapshot displaced this publication.
Definition diagnostics.hpp:42
@ QUEUE_FULL
Configured queue capacity rejected the publication.
Definition diagnostics.hpp:41
@ VALUE_CHANGED
An observed Property commit or tree work change occurred.
Definition diagnostics.hpp:51
@ NO_MEMORY
Allocation failed at a runner exception boundary.
Definition application.hpp:33
@ CANCELLED
Explicit cancellation or replacement stopped the invocation.
Definition async.hpp:29
@ IDENTITY_EXHAUSTED
Invocation identity cannot advance without reuse.
Definition async.hpp:33
@ INVALID_ARGUMENT
Empty task, callback or immutable data owner, or invalid policy.
Definition async.hpp:26
DiagnosticError
Configuration/export failure.
Definition diagnostics.hpp:78
@ UNSUPPORTED_VERSION
The export does not declare the supported schema.
Definition diagnostics.hpp:81
std::expected< void, DiagnosticError > validateDiagnosticVersion(std::string_view text) noexcept
Check the version line before interpreting an application's diagnostic export.
std::string exportDiagnostics(const DiagnosticSnapshot &snapshot)
Serialize metadata in memory; performs no stream/file/terminal I/O.
DiagnosticEvent
Metadata stages; no stage contains application text or terminal bytes.
Definition diagnostics.hpp:16
@ INVALIDATION
Tree work decision.
Definition diagnostics.hpp:29
@ QUEUE_REPLACED
New publication links to one displaced publication.
Definition diagnostics.hpp:21
@ QUEUE_RETURNED
Scheduler callback returned or failed; application effects remain separate evidence.
Definition diagnostics.hpp:35
@ OUTPUT
Diff or transport result, never proof of physical display.
Definition diagnostics.hpp:33
@ FRAME_REQUESTED
A cause linked to a coalesced frame.
Definition diagnostics.hpp:30
@ TARGET
Actual target selection or eligibility result.
Definition diagnostics.hpp:23
@ QUEUE_ACCEPTED
Publication admitted to the existing scheduler.
Definition diagnostics.hpp:18
@ PROPERTY
Observed equality or committed property change.
Definition diagnostics.hpp:28
@ FRAME_NOT_REQUESTED
No new frame was requested in this scope.
Definition diagnostics.hpp:31
@ HANDLER_RETURNED
Callback returned or failed; model effects are separate evidence.
Definition diagnostics.hpp:27
@ COMPLETED
Synchronous action/dispatch returned.
Definition diagnostics.hpp:34
@ QUEUE_REJECTED
Publication rejected before delivery.
Definition diagnostics.hpp:19
@ QUEUE_DISPATCH
Callback admitted on the UI executor.
Definition diagnostics.hpp:20
@ QUEUE_CANCELLED
Queued publication will not be delivered.
Definition diagnostics.hpp:22
@ FOCUS
Focus selection or change.
Definition diagnostics.hpp:24
@ ACTION
Semantic action resolution or unhandled result.
Definition diagnostics.hpp:25
@ HANDLER_STARTED
User event callback admitted.
Definition diagnostics.hpp:26
@ FRAME_SUBMITTED
Actual frame encoded and accepted by FrameWriter.
Definition diagnostics.hpp:32
@ MOUSE
Mouse input supplied by the existing router.
Definition action.hpp:27
@ NONE
Caption only, including a single-glyph action button.
Definition controls.hpp:157
@ INPUT
Read, decode and enqueue terminal input or resize events.
Definition application.hpp:18
Fixed-size causal token; zero fields mean unobserved, never a reused identity.
Definition diagnostics.hpp:92
std::uint64_t publicationId
Queue publication, not subscription identity.
Definition diagnostics.hpp:96
std::uint64_t parentCorrelationId
Causal action, if known.
Definition diagnostics.hpp:94
std::uint64_t period
Recording interval identity.
Definition diagnostics.hpp:95
std::uint64_t correlationId
Action identity in this recorder's lifetime.
Definition diagnostics.hpp:93
std::uint64_t frameId
Coalesced frame identity, if known.
Definition diagnostics.hpp:97
Session-local identity; never an object address.
Definition diagnostics.hpp:85
std::size_t slot
Tree registry slot.
Definition diagnostics.hpp:87
std::uint64_t session
Tree session.
Definition diagnostics.hpp:86
std::uint64_t generation
Slot generation.
Definition diagnostics.hpp:88
Allocation budget applied when recording is enabled.
Definition diagnostics.hpp:115
std::size_t capacity
Positive maximum retained records; oldest records are overwritten.
Definition diagnostics.hpp:116
Fixed-size bounded record.
Definition diagnostics.hpp:101
DiagnosticContext context
Causal metadata.
Definition diagnostics.hpp:103
DiagnosticReason reason
Evidence at this stage.
Definition diagnostics.hpp:107
DiagnosticInput input
Category for INPUT only.
Definition diagnostics.hpp:108
std::uint64_t relatedPublicationId
Replaced publication, at most one per record.
Definition diagnostics.hpp:105
std::uint64_t sequence
Monotonic retained record number.
Definition diagnostics.hpp:102
bool incomplete
Cross-period or missing-context evidence.
Definition diagnostics.hpp:111
std::uint32_t semanticAction
Action enum ordinal plus one; zero means unavailable.
Definition diagnostics.hpp:109
bool controlQueue
True for the control lane; false for data/timer.
Definition diagnostics.hpp:110
DiagnosticNodeId stableNodeId
Full generation-aware node identity.
Definition diagnostics.hpp:104
Owning bounded snapshot; copy only between frames, serialize after releasing the recorder lock.
Definition diagnostics.hpp:120
std::uint64_t droppedContention
Failed nonblocking write attempts in this interval, saturating.
Definition diagnostics.hpp:128
std::uint64_t periodEnd
Last sequence when disabled; zero while recording.
Definition diagnostics.hpp:126
bool countersSaturated
A loss counter reached its representable limit.
Definition diagnostics.hpp:129
std::uint64_t droppedOverflow
Overwritten records in this interval, saturating.
Definition diagnostics.hpp:127
std::vector< DiagnosticRecord > records
Oldest-to-newest bounded records.
Definition diagnostics.hpp:132
DiagnosticState state
State at the snapshot boundary.
Definition diagnostics.hpp:123
DiagnosticOptions options
Active ring capacity.
Definition diagnostics.hpp:122
std::uint32_t schemaVersion
Supported metadata schema; unrelated to PORTALS-TRACE.
Definition diagnostics.hpp:121
std::uint64_t period
Current or last recording interval.
Definition diagnostics.hpp:124
std::uint64_t periodBegin
First possible sequence in this interval.
Definition diagnostics.hpp:125
bool incomplete
Conservative: losses, open chains, or recording boundaries may hide work.
Definition diagnostics.hpp:130