fission-diagnostics 0.1.0

Diagnostics
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
//! Structured diagnostics and telemetry for the Fission rendering pipeline.
//!
//! Provides a global, thread-safe diagnostics system that emits structured JSON
//! events covering every stage of the frame lifecycle. Configure via environment
//! variables ([`init_from_env()`]) or programmatically ([`init()`]).
//!
//! # Quick start
//!
//! ```rust,ignore
//! use fission_diagnostics::prelude::*;
//! init_from_env();
//! begin_frame(None);
//! emit(DiagCategory::Layout, DiagLevel::Debug, DiagEventKind::LayoutSummary {
//!     nodes: 100, dirty_count: 2, full_rebuild: false, duration_ns: 500_000,
//! });
//! end_frame(FrameStats::default());
//! ```

use once_cell::sync::OnceCell;
use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
use std::collections::{BTreeSet, HashSet};
use std::fs::{File, OpenOptions};
use std::io::Write as _;
use std::path::PathBuf;
use std::sync::atomic::{AtomicU64, Ordering};

// --------- Public Types ---------

/// Severity level for diagnostic events.
///
/// Ordered from most to least severe: `Error` > `Warn` > `Info` > `Debug` > `Trace`.
/// The [`allows()`](DiagLevel::allows) method checks if a given level passes the
/// configured minimum threshold.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(rename_all = "lowercase")]
pub enum DiagLevel {
    Error,
    Warn,
    Info,
    Debug,
    Trace,
}

impl DiagLevel {
    pub fn allows(self, level: DiagLevel) -> bool {
        use DiagLevel::*;
        let a = match self {
            Error => 0,
            Warn => 1,
            Info => 2,
            Debug => 3,
            Trace => 4,
        };
        let b = match level {
            Error => 0,
            Warn => 1,
            Info => 2,
            Debug => 3,
            Trace => 4,
        };
        b <= a
    }
}

/// Pipeline subsystem that a diagnostic event belongs to.
///
/// Used for category-based filtering. Enable specific categories via the
/// `FISSION_DIAG` environment variable (comma-separated, or `*` for all).
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash, Ord, PartialOrd)]
#[serde(rename_all = "lowercase")]
pub enum DiagCategory {
    Frame,
    Diff,
    Layout,
    Paint,
    Raster,
    Input,
    Semantics,
    Animation,
    Media,
    Invariants,
    Test,
}

/// The top-level diagnostic event envelope.
///
/// Contains metadata (schema version, timestamp, frame number, category, level)
/// and the concrete event payload ([`DiagEventKind`]).
/// Serialized as a single JSON line (JSONL).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DiagEvent {
    pub schema_version: u16, // v1 = 1
    pub timestamp_ns: u64,
    pub frame_no: u64,
    pub category: DiagCategory,
    pub level: DiagLevel,
    #[serde(flatten)]
    pub event: DiagEventKind,
}

/// The concrete payload for a diagnostic event.
///
/// Each variant covers a specific pipeline stage or cross-cutting concern.
/// Serialized with `#[serde(tag = "kind", content = "payload")]`.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind", content = "payload")]
pub enum DiagEventKind {
    FrameStart { root: Option<u128> },
    FrameEnd { stats: FrameStats },

    DiffSummary {
        nodes_total: u32,
        nodes_created: u32,
        nodes_removed: u32,
        nodes_changed: u32,
        dirty_layout: u32,
        dirty_paint: u32,
    },

    LayoutSummary {
        nodes: u32,
        dirty_count: u32,
        full_rebuild: bool,
        duration_ns: u64,
    },

    PaintSummary {
        segments_reused: u32,
        segments_regenerated: u32,
        paint_ops_total: u32,
    },
    PaintNode {
        node: u128,
        note: Option<String>,
    },
    PaintNodeRect {
        node: u128,
        x: f32,
        y: f32,
        w: f32,
        h: f32,
        note: Option<String>,
    },
    
    NodeProps {
        node: u128,
        op_tag: String,
        flex_grow: f32,
        flex_shrink: f32,
        width: Option<f32>,
        height: Option<f32>,
    },

    RasterSummary {
        cache_hits: u32,
        cache_misses: u32,
        tiles_rasterized: u32,
    },

    AnimationSummary {
        active_count: u32,
        started: u32,
        replaced: u32,
        ended: u32,
    },

    MediaSummary {
        video_nodes: u32,
        audio_nodes: u32,
        embeds_total: u32,
    },

    // Overlay/Portal + Anchor diagnostics (layout investigation helpers)
    PortalsComposed { portal_count: u32 },
    AnchorPlacement {
        widget: u128,
        node: u128,
        rect_x: f32,
        rect_y: f32,
        rect_w: f32,
        rect_h: f32,
        place_left: f32,
        place_top: f32,
        note: Option<String>,
    },

    InvariantViolation {
        kind: String,
        node: Option<u128>,
        details: String,
        dump_ref: Option<String>,
    },

    InputEvent {
        kind: String,
        target: Option<u128>,
        position: Option<(f32, f32)>,
    },

    MediaEvent {
        kind: String,
        id: Option<u128>,
        duration_ms: Option<u64>,
        position_ms: Option<u64>,
    },

    // Text input auto-scroll diagnostics
    TextInputAutoScroll {
        scroll_id: u128,
        text_id: u128,
        text_len: u32,
        measured_w: f32,
        line_h: f32,
        viewport_x: f32,
        viewport_w: f32,
        content_w: f32,
        caret_abs_x: f32,
        offset_before: f32,
        offset_after: f32,
    },

    // General scrolling diagnostics
    ScrollExtent {
        node: u128,
        viewport_w: f32,
        viewport_h: f32,
        content_w: f32,
        content_h: f32,
        note: Option<String>,
    },
    ScrollUpdate {
        node: u128,
        axis: String,
        point_x: f32,
        point_y: f32,
        delta: f32,
        old_offset: f32,
        new_offset: f32,
        max_offset: f32,
        viewport_w: f32,
        viewport_h: f32,
        content_w: f32,
        content_h: f32,
    },
    ScrollPaintTranslate {
        node: u128,
        axis: String,
        offset: f32,
        translate_x: f32,
        translate_y: f32,
    },
    TextLayoutPerformance {
        text_len: u32,
        is_rich: bool,
        duration_ns: u64,
    },
}

/// Summary statistics for a completed frame, attached to [`DiagEventKind::FrameEnd`].
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct FrameStats {
    pub dirty_nodes: u32,
    pub layout_updates: u32,
    pub paint_misses: u32,
    pub paint_hits: u32,
    pub video_surfaces: u32,
}

/// Configuration for the diagnostics system.
///
/// Controls which categories and levels are emitted, the output sink, and
/// the sampling rate.
#[derive(Debug, Clone)]
pub struct DiagnosticsConfig {
    pub enabled_categories: BTreeSet<DiagCategory>,
    pub min_level: DiagLevel,
    pub sink: DiagSink,
    pub sampling: f32,
}

impl Default for DiagnosticsConfig {
    fn default() -> Self {
        Self {
            enabled_categories: BTreeSet::new(),
            min_level: DiagLevel::Error,
            sink: DiagSink::Stdout,
            sampling: 1.0,
        }
    }
}

// --------- Sinks ---------

/// Output destination for diagnostic events.
#[derive(Debug, Clone)]
pub enum DiagSink {
    Stdout,
    File(PathBuf),
    RingBuffer(usize),
    Disabled,
}

trait SinkImpl: Send + Sync {
    fn write(&self, event: &DiagEvent);
}

struct StdoutSinkImpl;
impl SinkImpl for StdoutSinkImpl {
    fn write(&self, event: &DiagEvent) {
        // JSONL for stable tooling integration
        let _ = serde_json::to_string(event)
            .map(|line| println!("{}", line));
    }
}

struct FileSinkImpl {
    file: RwLock<File>,
}
impl SinkImpl for FileSinkImpl {
    fn write(&self, event: &DiagEvent) {
        if let Ok(s) = serde_json::to_string(event) {
            let mut f = self.file.write();
            let _ = f.write_all(s.as_bytes());
            let _ = f.write_all(b"\n");
        }
    }
}

struct RingBufferSinkImpl {
    // very simple ring buffer of JSON strings for now
    buf: RwLock<Vec<String>>,
    cap: usize,
}
impl SinkImpl for RingBufferSinkImpl {
    fn write(&self, event: &DiagEvent) {
        if let Ok(s) = serde_json::to_string(event) {
            let mut w = self.buf.write();
            if w.len() >= self.cap { w.remove(0); }
            w.push(s);
        }
    }
}

// --------- Global Diagnostics ---------

struct DiagnosticsInner {
    config: DiagnosticsConfig,
    sink_impl: Box<dyn SinkImpl>,
    frame_no: AtomicU64,
    timestamp_ns: AtomicU64,
}

impl DiagnosticsInner {
    fn should_emit(&self, cat: &DiagCategory, level: DiagLevel) -> bool {
        if matches!(self.config.sink, DiagSink::Disabled) { return false; }
        if !self.config.enabled_categories.contains(cat) { return false; }
        self.config.min_level.allows(level)
    }
}

static DIAGNOSTICS: OnceCell<RwLock<DiagnosticsInner>> = OnceCell::new();

/// Initialize the diagnostics system from environment variables.
///
/// Reads `FISSION_DIAG` (categories), `FISSION_DIAG_LEVEL`, `FISSION_DIAG_SINK`,
/// and `FISSION_DIAG_SAMPLING`. See the crate-level documentation for details.
pub fn init_from_env() {
    // Categories
    let cats = std::env::var("FISSION_DIAG").unwrap_or_default();
    let enabled_categories: BTreeSet<DiagCategory> = cats
        .split(',')
        .filter_map(|s| match s.trim().to_lowercase().as_str() {
            "frame" => Some(DiagCategory::Frame),
            "diff" => Some(DiagCategory::Diff),
            "layout" => Some(DiagCategory::Layout),
            "paint" => Some(DiagCategory::Paint),
            "raster" => Some(DiagCategory::Raster),
            "input" => Some(DiagCategory::Input),
            "semantics" => Some(DiagCategory::Semantics),
            "animation" => Some(DiagCategory::Animation),
            "media" => Some(DiagCategory::Media),
            "invariants" => Some(DiagCategory::Invariants),
            "test" => Some(DiagCategory::Test),
            "*" => None, // handled below
            _ => None,
        })
        .collect();

    // Level
    let min_level = match std::env::var("FISSION_DIAG_LEVEL").unwrap_or_default().to_lowercase().as_str() {
        "error" => DiagLevel::Error,
        "warn" => DiagLevel::Warn,
        "info" => DiagLevel::Info,
        "debug" => DiagLevel::Debug,
        "trace" => DiagLevel::Trace,
        _ => DiagLevel::Warn,
    };

    // Sink
    let sink_env = std::env::var("FISSION_DIAG_SINK").unwrap_or_default();
    let sink = if sink_env.starts_with("file:") {
        DiagSink::File(PathBuf::from(sink_env.trim_start_matches("file:")))
    } else if sink_env.starts_with("ipc:") {
        // Not implemented v1; fallback to stdout
        DiagSink::Stdout
    } else if sink_env == "stdout" || sink_env.is_empty() {
        DiagSink::Stdout
    } else {
        DiagSink::Disabled
    };

    let sampling = std::env::var("FISSION_DIAG_SAMPLING")
        .ok()
        .and_then(|s| s.parse::<f32>().ok())
        .unwrap_or(1.0);

    let mut cfg = DiagnosticsConfig {
        enabled_categories,
        min_level,
        sink,
        sampling,
    };

    // Handle wildcard * for categories (enable all)
    if cats.split(',').any(|s| s.trim() == "*") {
        cfg.enabled_categories = [
            DiagCategory::Frame,
            DiagCategory::Diff,
            DiagCategory::Layout,
            DiagCategory::Paint,
            DiagCategory::Raster,
            DiagCategory::Input,
            DiagCategory::Semantics,
            DiagCategory::Animation,
            DiagCategory::Media,
            DiagCategory::Invariants,
            DiagCategory::Test,
        ]
        .into_iter()
        .collect();
    }

    init(cfg);
}

/// Initialize the diagnostics system with the given configuration.
///
/// Can only be called once (uses `OnceCell`). Subsequent calls are silently ignored.
pub fn init(config: DiagnosticsConfig) {
    let sink_impl: Box<dyn SinkImpl> = match &config.sink {
        DiagSink::Stdout => Box::new(StdoutSinkImpl),
        DiagSink::File(path) => {
            let file = OpenOptions::new().create(true).append(true).open(path).unwrap();
            Box::new(FileSinkImpl { file: RwLock::new(file) })
        }
        DiagSink::RingBuffer(cap) => Box::new(RingBufferSinkImpl { buf: RwLock::new(Vec::with_capacity(*cap)), cap: *cap }),
        DiagSink::Disabled => Box::new(StdoutSinkImpl), // won't be used
    };

    let inner = DiagnosticsInner {
        config,
        sink_impl,
        frame_no: AtomicU64::new(0),
        timestamp_ns: AtomicU64::new(0),
    };
    let _ = DIAGNOSTICS.set(RwLock::new(inner));
}

fn with_diag<T>(f: impl FnOnce(&DiagnosticsInner) -> T) -> Option<T> {
    DIAGNOSTICS.get().map(|cell| {
        let guard = cell.read();
        f(&*guard)
    })
}

fn with_diag_mut<T>(f: impl FnOnce(&mut DiagnosticsInner) -> T) -> Option<T> {
    DIAGNOSTICS.get().map(|cell| {
        let mut guard = cell.write();
        f(&mut *guard)
    })
}

/// Mark the start of a new frame. Increments the frame counter and emits a
/// [`DiagEventKind::FrameStart`] event.
pub fn begin_frame(root: Option<u128>) {
    let _ = with_diag_mut(|d| {
        let ts = d.timestamp_ns.fetch_add(16666666, Ordering::Relaxed) + 1; // ~60fps increment
        let fno = d.frame_no.fetch_add(1, Ordering::Relaxed) + 1;
        let ev = DiagEvent {
            schema_version: 1,
            timestamp_ns: ts,
            frame_no: fno,
            category: DiagCategory::Frame,
            level: DiagLevel::Debug,
            event: DiagEventKind::FrameStart { root },
        };
        if d.should_emit(&ev.category, ev.level) {
            d.sink_impl.write(&ev);
        }
    });
}

/// Mark the end of the current frame, attaching the given [`FrameStats`].
pub fn end_frame(stats: FrameStats) {
    let _ = with_diag_mut(|d| {
        let ts = d.timestamp_ns.fetch_add(1, Ordering::Relaxed) + 1;
        let fno = d.frame_no.load(Ordering::Relaxed);
        let ev = DiagEvent {
            schema_version: 1,
            timestamp_ns: ts,
            frame_no: fno,
            category: DiagCategory::Frame,
            level: DiagLevel::Debug,
            event: DiagEventKind::FrameEnd { stats },
        };
        if d.should_emit(&ev.category, ev.level) {
            d.sink_impl.write(&ev);
        }
    });
}

/// Emit a diagnostic event if the given category and level pass the current filter.
///
/// This is the primary entry point for all diagnostic events. The event is
/// automatically timestamped and tagged with the current frame number.
pub fn emit(category: DiagCategory, level: DiagLevel, event: DiagEventKind) {
    let _ = with_diag_mut(|d| {
        if !d.should_emit(&category, level) { return; }
        let ts = d.timestamp_ns.fetch_add(1, Ordering::Relaxed) + 1;
        let fno = d.frame_no.load(Ordering::Relaxed);
        let ev = DiagEvent {
            schema_version: 1,
            timestamp_ns: ts,
            frame_no: fno,
            category,
            level,
            event,
        };
        d.sink_impl.write(&ev);
    });
}

/// Convenience re-exports for common diagnostic operations.
pub mod prelude {
    pub use super::{begin_frame, end_frame, emit, DiagCategory, DiagEventKind, DiagLevel, FrameStats, init_from_env};
}

// --------- Snapshot Provider (v1 minimal) ---------

/// The type of snapshot that a [`SnapshotProvider`] can produce.
#[derive(Debug, Clone, Copy)]
pub enum SnapshotKind { Layout }

/// A serialized snapshot blob containing JSON data.
#[derive(Debug, Clone)]
pub struct SnapshotBlob {
    pub kind: SnapshotKind,
    pub json: String,
}

/// Trait for components that can produce a JSON snapshot of their internal state.
pub trait SnapshotProvider {
    fn snapshot(&self, kind: SnapshotKind) -> Option<SnapshotBlob>;
}