sk-app 0.5.0

The Skathi host: windows, the event loop, the JS bindings and the embedding API
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
//! `trace`: a frame-by-frame record of where the host's time went during an interaction.
//!
//! A frame is the span between two `pump_frame` entries. The sk-tree phase counters are drained at
//! the NEXT frame's start, so a frame's record includes its own draw. Idle frames are not stored:
//! the next active frame carries their count (`idleBefore`).
//!
//! It shares `sk_tree::perf`'s counters with `SK_PERF`, so a trace refuses to start while `SK_PERF` is on.

use std::cell::{Cell, RefCell};
use std::time::{Instant, SystemTime, UNIX_EPOCH};

use sk_tree::perf::{DomOp, FullReason, Phases, SyncCaller};

use crate::json::Json;

/// Active frames kept.
const DEFAULT_CAP: usize = 20_000;

#[derive(Clone)]
struct Frame {
    /// Since the trace started, at `pump_frame` entry.
    at_ms: f64,
    /// To the next frame's start.
    dur_ms: f32,
    /// `pump_frame` entry to `perf::pump_done`.
    pump_ms: f32,
    /// `rt.pump(...)` alone: timers, rAF, microtasks.
    js_ms: f32,
    /// `Shell::render` — our lowering of the tree into gpui elements.
    render_ms: f32,
    drew: bool,
    phases: Phases,
    /// gpui line-layout lookups, and the misses that shaped text.
    lookups: u64,
    shapes: u64,
    shape_ms: f32,
    /// Idle frames since the previous stored frame.
    idle_before: u32,
    marks: Vec<String>,
}

struct Trace {
    start: Instant,
    start_epoch_ms: f64,
    cap: usize,
    frames: Vec<Frame>,
    dropped: u64,
    /// The frame in progress.
    cur: Option<(Instant, Frame)>,
    /// When this frame's pump closed; `render` stamps land after it.
    pump_open: Option<Instant>,
    render_open: Option<Instant>,
    idle: u32,
    idle_ms: f64,
    lines: (u64, u64, u64),
    total_frames: u64,
}

thread_local! {
    static TRACE: RefCell<Option<Trace>> = const { RefCell::new(None) };
    static NATIVE_PROFILE_WINDOW: Cell<usize> = const { Cell::new(0) };
}

fn epoch_ms() -> f64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs_f64() * 1000.0)
        .unwrap_or(0.0)
}

fn active() -> bool {
    TRACE.with(|t| t.borrow().is_some())
}

pub(crate) fn start(cap: Option<usize>) -> Result<Json, String> {
    if sk_tree::perf::env_enabled() {
        return Err(
            "SK_PERF is on and owns the phase counters (it drains them every second); \
                    a trace would read zeros. Restart the host without SK_PERF."
                .into(),
        );
    }
    sk_tree::perf::set_tracing(true);
    let _ = sk_tree::perf::take_all();
    let now = Instant::now();
    let at = epoch_ms();
    TRACE.with(|t| {
        *t.borrow_mut() = Some(Trace {
            start: now,
            start_epoch_ms: at,
            cap: cap.unwrap_or(DEFAULT_CAP).max(16),
            frames: Vec::new(),
            dropped: 0,
            cur: None,
            pump_open: None,
            render_open: None,
            idle: 0,
            idle_ms: 0.0,
            lines: gpui::line_layout_counters(),
            total_frames: 0,
        })
    });
    Ok(Json::obj(vec![
        ("started", Json::Bool(true)),
        ("startEpochMs", Json::n(at)),
    ]))
}

pub(crate) fn mark(label: &str) {
    sk_runtime::redjs::profiling::mark(label);
    TRACE.with(|t| {
        if let Some(tr) = t.borrow_mut().as_mut() {
            let at = tr.start.elapsed().as_secs_f64() * 1000.0;
            match tr.cur.as_mut() {
                Some((_, f)) => f.marks.push(label.to_string()),
                None => tr.frames.push(Frame::empty(at, vec![label.to_string()])),
            }
        }
    });
}

impl Frame {
    fn empty(at_ms: f64, marks: Vec<String>) -> Frame {
        Frame {
            at_ms,
            dur_ms: 0.0,
            pump_ms: 0.0,
            js_ms: 0.0,
            render_ms: 0.0,
            drew: false,
            phases: Phases::default(),
            lookups: 0,
            shapes: 0,
            shape_ms: 0.0,
            idle_before: 0,
            marks,
        }
    }

    fn idle(&self) -> bool {
        let p = &self.phases;
        self.marks.is_empty()
            && !self.drew
            && self.shapes == 0
            && p.restyle_passes == 0
            && p.build_passes == 0
            && p.draw_passes == 0
            && p.js_passes == 0
            && p.sync_layouts + p.sync_layout_refused == 0
            && p.observer_steps == 0
            && p.dom_ops.iter().all(|&n| n == 0)
            && p.layout_reads + p.style_reads + p.queries + p.scroll_reads == 0
            && self.pump_ms < 2.0
            && self.js_ms < 1.0
    }
}

pub(crate) fn frame_start() {
    sk_runtime::redjs::profiling::frame_start();
    if !active() {
        return;
    }
    let now = Instant::now();
    let phases = sk_tree::perf::take_all();
    let lines = gpui::line_layout_counters();
    TRACE.with(|t| {
        let mut t = t.borrow_mut();
        let Some(tr) = t.as_mut() else { return };
        if let Some((opened, mut f)) = tr.cur.take() {
            f.dur_ms = (now - opened).as_secs_f32() * 1000.0;
            f.phases = phases;
            f.lookups = lines.0.saturating_sub(tr.lines.0);
            f.shapes = lines.1.saturating_sub(tr.lines.1);
            f.shape_ms = (lines.2.saturating_sub(tr.lines.2)) as f32 / 1e6;
            tr.total_frames += 1;
            if f.idle() {
                tr.idle += 1;
                tr.idle_ms += f64::from(f.dur_ms);
            } else {
                f.idle_before = std::mem::take(&mut tr.idle);
                if tr.frames.len() >= tr.cap {
                    tr.frames.remove(0);
                    tr.dropped += 1;
                }
                tr.frames.push(f);
            }
        }
        tr.lines = lines;
        let at = (now - tr.start).as_secs_f64() * 1000.0;
        tr.cur = Some((now, Frame::empty(at, Vec::new())));
        tr.pump_open = Some(now);
    });
}

pub(crate) fn pump_done() {
    with_cur(|tr, now| {
        if let (Some(open), Some((_, f))) = (tr.pump_open.take(), tr.cur.as_mut()) {
            f.pump_ms += (now - open).as_secs_f32() * 1000.0;
        }
    });
}

pub(crate) fn note_js(ms: f32) {
    with_cur(|tr, _| {
        if let Some((_, f)) = tr.cur.as_mut() {
            f.js_ms += ms;
        }
    });
}

pub(crate) fn render_begin() {
    with_cur(|tr, now| tr.render_open = Some(now));
}

pub(crate) fn render_done() {
    with_cur(|tr, now| {
        if let (Some(open), Some((_, f))) = (tr.render_open.take(), tr.cur.as_mut()) {
            f.render_ms += (now - open).as_secs_f32() * 1000.0;
            f.drew = true;
        }
    });
}

fn with_cur(f: impl FnOnce(&mut Trace, Instant)) {
    TRACE.with(|t| {
        if let Some(tr) = t.borrow_mut().as_mut() {
            f(tr, Instant::now());
        }
    });
}

pub(crate) fn report(stop: bool, frames: bool) -> Result<Json, String> {
    let taken = TRACE.with(|t| {
        let mut t = t.borrow_mut();
        if stop {
            t.take()
        } else {
            t.as_ref().map(|tr| Trace {
                start: tr.start,
                start_epoch_ms: tr.start_epoch_ms,
                cap: tr.cap,
                frames: tr.frames.clone(),
                dropped: tr.dropped,
                cur: None,
                pump_open: None,
                render_open: None,
                idle: tr.idle,
                idle_ms: tr.idle_ms,
                lines: tr.lines,
                total_frames: tr.total_frames,
            })
        }
    });
    if stop {
        sk_tree::perf::set_tracing(false);
        let _ = sk_tree::perf::take_all();
    }
    let Some(tr) = taken else {
        return Err("no trace is running — `trace start` first".into());
    };
    let mut out = vec![
        ("startEpochMs", Json::n(tr.start_epoch_ms)),
        (
            "elapsedMs",
            Json::n(round1(tr.start.elapsed().as_secs_f64() * 1000.0)),
        ),
        ("framesTotal", Json::n(tr.total_frames as f64)),
        ("framesStored", Json::n(tr.frames.len() as f64)),
        (
            "framesIdle",
            Json::n((tr.total_frames as f64) - tr.frames.len() as f64),
        ),
        ("dropped", Json::n(tr.dropped as f64)),
        ("summary", summary(&tr.frames)),
    ];
    if frames {
        out.push((
            "frames",
            Json::arr(tr.frames.iter().map(frame_json).collect()),
        ));
    }
    Ok(Json::obj(out))
}

fn round1(v: f64) -> f64 {
    (v * 10.0).round() / 10.0
}

fn r2(v: f32) -> Json {
    Json::n((f64::from(v) * 100.0).round() / 100.0)
}

fn dom_total(p: &Phases) -> u32 {
    p.dom_ops.iter().sum()
}

fn dom_span_ms(p: &Phases) -> f32 {
    match (p.dom_first, p.dom_last) {
        (Some(a), Some(b)) => (b - a).as_secs_f32() * 1000.0,
        _ => 0.0,
    }
}

fn frame_json(f: &Frame) -> Json {
    let p = &f.phases;
    let mut o: Vec<(&str, Json)> = vec![
        ("t", Json::n(round1(f.at_ms))),
        ("dur", r2(f.dur_ms)),
        ("pump", r2(f.pump_ms)),
        ("js", r2(f.js_ms)),
    ];
    if f.idle_before > 0 {
        o.push(("idleBefore", Json::n(f64::from(f.idle_before))));
    }
    if !f.marks.is_empty() {
        o.push((
            "marks",
            Json::arr(f.marks.iter().map(|m| Json::s(m.clone())).collect()),
        ));
    }
    if p.js_passes > 0 {
        o.push((
            "inputJs",
            Json::obj(vec![
                ("n", Json::n(f64::from(p.js_passes))),
                ("ms", r2(p.js_ms)),
            ]),
        ));
    }
    if dom_total(p) > 0 {
        let mut d: Vec<(&str, Json)> = DomOp::NAMES
            .iter()
            .zip(p.dom_ops.iter())
            .filter(|(_, n)| **n > 0)
            .map(|(name, n)| (*name, Json::n(f64::from(*n))))
            .collect();
        d.push(("total", Json::n(f64::from(dom_total(p)))));
        d.push(("spanMs", r2(dom_span_ms(p))));
        o.push(("dom", Json::obj(d)));
    }
    if p.layout_reads + p.scroll_reads + p.style_reads + p.queries + p.style_flushes > 0 {
        o.push((
            "reads",
            Json::obj(vec![
                ("layout", Json::n(f64::from(p.layout_reads))),
                ("scroll", Json::n(f64::from(p.scroll_reads))),
                ("style", Json::n(f64::from(p.style_reads))),
                ("query", Json::n(f64::from(p.queries))),
                ("styleFlushes", Json::n(f64::from(p.style_flushes))),
            ]),
        ));
    }
    if p.sync_layouts + p.sync_layout_refused > 0 {
        let by: Vec<(&str, Json)> = SyncCaller::NAMES
            .iter()
            .zip(p.sync_by.iter())
            .filter(|(_, n)| **n > 0)
            .map(|(name, n)| (*name, Json::n(f64::from(*n))))
            .collect();
        o.push((
            "sync",
            Json::obj(vec![
                ("n", Json::n(f64::from(p.sync_layouts))),
                ("ms", r2(p.sync_layout_ms)),
                ("maxMs", r2(p.sync_layout_max_ms)),
                ("refused", Json::n(f64::from(p.sync_layout_refused))),
                // `tail`: waves that ended with changes already drawn by a forced layout; `tailFree`: and nothing else asked for a draw.
                ("tail", Json::n(f64::from(p.sync_tail))),
                ("tailFree", Json::n(f64::from(p.sync_tail_free))),
                ("by", Json::obj(by)),
            ]),
        ));
    }
    if p.observer_steps > 0 {
        o.push((
            "observers",
            observers_json(
                p.observer_steps,
                p.observer_rounds,
                p.observer_relayouts,
                p.observer_relayout_ms,
                p.observer_ms,
                p.observer_loop_errors,
            ),
        ));
    }
    if p.restyle_passes > 0 || p.recompiles > 0 || p.full_reasons.iter().any(|&n| n > 0) {
        let reasons: Vec<(&str, Json)> = FullReason::NAMES
            .iter()
            .zip(p.full_reasons.iter())
            .filter(|(_, n)| **n > 0)
            .map(|(name, n)| (*name, Json::n(f64::from(*n))))
            .collect();
        o.push((
            "restyle",
            Json::obj(vec![
                ("n", Json::n(f64::from(p.restyle_passes))),
                ("full", Json::n(f64::from(p.restyle_full))),
                ("ms", r2(p.restyle_ms)),
                ("maxMs", r2(p.restyle_max_ms)),
                ("nodes", Json::n(p.nodes_cascaded as f64)),
                ("changed", Json::n(p.nodes_changed as f64)),
                ("matchMs", r2(p.match_ms)),
                ("varsMs", r2(p.vars_ms)),
                ("foldMs", r2(p.fold_ms)),
                ("inlineMs", r2(p.inline_ms)),
                ("storeMs", r2(p.store_ms)),
                ("selectors", Json::n(p.rules_tested as f64)),
                ("recompiles", Json::n(f64::from(p.recompiles))),
                ("recompileMs", r2(p.recompile_ms)),
                ("why", Json::obj(reasons)),
            ]),
        ));
    }
    if p.build_passes > 0 || f.drew {
        o.push((
            "build",
            Json::obj(vec![
                ("n", Json::n(f64::from(p.build_passes))),
                ("ms", r2(p.build_ms)),
                ("elements", Json::n(p.elements_built as f64)),
                ("renderMs", r2(f.render_ms)),
                (
                    "cache",
                    Json::arr(vec![
                        Json::n(f64::from(p.cache_offers.saturating_sub(p.cached_renders))),
                        Json::n(f64::from(p.cached_renders)),
                        Json::n(f64::from(p.cache_standdowns)),
                    ]),
                ),
            ]),
        ));
    }
    if p.draw_passes > 0 {
        o.push((
            "draw",
            Json::obj(vec![
                ("n", Json::n(f64::from(p.draw_passes))),
                ("layoutReq", r2(p.layout_req_ms)),
                ("taffy", r2(p.taffy_ms)),
                ("prepaint", r2(p.prepaint_ms)),
                ("paint", r2(p.paint_ms)),
            ]),
        ));
    }
    if f.shapes > 0 {
        o.push((
            "text",
            Json::obj(vec![
                ("lookups", Json::n(f.lookups as f64)),
                ("shapes", Json::n(f.shapes as f64)),
                ("shapeMs", r2(f.shape_ms)),
            ]),
        ));
    }
    if p.resample_sync + p.resample_async > 0 {
        o.push((
            "resample",
            Json::obj(vec![
                ("sync", Json::n(f64::from(p.resample_sync))),
                ("syncMs", r2(p.resample_sync_ms)),
                ("async", Json::n(f64::from(p.resample_async))),
            ]),
        ));
    }
    if p.fonts_registered > 0 {
        o.push((
            "fonts",
            Json::obj(vec![
                ("registered", Json::n(f64::from(p.fonts_registered))),
                ("bytes", Json::n(p.font_bytes as f64)),
            ]),
        ));
    }
    if p.img_srcs > 0 {
        o.push(("imgSrcs", Json::n(f64::from(p.img_srcs))));
    }
    Json::obj(o)
}

fn observers_json(
    steps: u32,
    rounds: u32,
    relayouts: u32,
    relayout_ms: f32,
    ms: f32,
    loop_errors: u32,
) -> Json {
    Json::obj(vec![
        ("n", Json::n(f64::from(steps))),
        ("rounds", Json::n(f64::from(rounds))),
        ("relayouts", Json::n(f64::from(relayouts))),
        ("relayoutMs", r2(relayout_ms)),
        ("ms", r2(ms)),
        ("loopErrors", Json::n(f64::from(loop_errors))),
    ])
}

fn summary(frames: &[Frame]) -> Json {
    let mut s = Totals::default();
    for f in frames {
        s.add(f);
    }
    let mut longest: Vec<&Frame> = frames.iter().collect();
    longest.sort_by(|a, b| {
        b.dur_ms
            .partial_cmp(&a.dur_ms)
            .unwrap_or(std::cmp::Ordering::Equal)
    });
    longest.truncate(10);
    let over = |ms: f32| frames.iter().filter(|f| f.dur_ms > ms).count() as f64;
    let reasons: Vec<(&str, Json)> = FullReason::NAMES
        .iter()
        .zip(s.full_reasons.iter())
        .map(|(name, n)| (*name, Json::n(f64::from(*n))))
        .collect();
    let by: Vec<(&str, Json)> = SyncCaller::NAMES
        .iter()
        .zip(s.sync_by.iter())
        .map(|(name, n)| (*name, Json::n(f64::from(*n))))
        .collect();
    let dom: Vec<(&str, Json)> = DomOp::NAMES
        .iter()
        .zip(s.dom.iter())
        .map(|(name, n)| (*name, Json::n(*n as f64)))
        .collect();
    Json::obj(vec![
        ("frames", Json::n(frames.len() as f64)),
        ("over16ms", Json::n(over(16.7))),
        ("over33ms", Json::n(over(33.4))),
        ("over100ms", Json::n(over(100.0))),
        ("sumDurMs", r2(s.dur)),
        ("pumpMs", r2(s.pump)),
        ("jsMs", r2(s.js)),
        ("inputJsMs", r2(s.input_js)),
        ("renderMs", r2(s.render)),
        ("domOps", Json::obj(dom)),
        ("layoutReads", Json::n(s.layout_reads as f64)),
        ("styleReads", Json::n(s.style_reads as f64)),
        ("styleFlushes", Json::n(s.style_flushes as f64)),
        ("queries", Json::n(s.queries as f64)),
        (
            "sync",
            Json::obj(vec![
                ("n", Json::n(s.sync as f64)),
                ("ms", r2(s.sync_ms)),
                ("maxMs", r2(s.sync_max)),
                ("refused", Json::n(s.sync_refused as f64)),
                ("tail", Json::n(s.sync_tail as f64)),
                ("tailFree", Json::n(s.sync_tail_free as f64)),
                ("maxPerFrame", Json::n(s.sync_per_frame_max as f64)),
                ("by", Json::obj(by)),
            ]),
        ),
        (
            "observers",
            observers_json(
                s.observer_steps,
                s.observer_rounds,
                s.observer_relayouts,
                s.observer_relayout_ms,
                s.observer_ms,
                s.observer_loop_errors,
            ),
        ),
        (
            "restyle",
            Json::obj(vec![
                ("n", Json::n(s.restyles as f64)),
                ("full", Json::n(s.restyle_full as f64)),
                ("ms", r2(s.restyle_ms)),
                ("matchMs", r2(s.match_ms)),
                ("nodes", Json::n(s.nodes as f64)),
                ("changed", Json::n(s.changed as f64)),
                ("recompiles", Json::n(s.recompiles as f64)),
                ("recompileMs", r2(s.recompile_ms)),
                ("why", Json::obj(reasons)),
            ]),
        ),
        (
            "build",
            Json::obj(vec![
                ("n", Json::n(s.builds as f64)),
                ("ms", r2(s.build_ms)),
                ("elements", Json::n(s.elements as f64)),
            ]),
        ),
        (
            "draw",
            Json::obj(vec![
                ("n", Json::n(s.draws as f64)),
                ("layoutReq", r2(s.layout_req)),
                ("taffy", r2(s.taffy)),
                ("prepaint", r2(s.prepaint)),
                ("paint", r2(s.paint)),
            ]),
        ),
        (
            "text",
            Json::obj(vec![
                ("lookups", Json::n(s.lookups as f64)),
                ("shapes", Json::n(s.shapes as f64)),
                ("shapeMs", r2(s.shape_ms)),
            ]),
        ),
        (
            "resample",
            Json::obj(vec![
                ("sync", Json::n(s.resample_sync as f64)),
                ("syncMs", r2(s.resample_sync_ms)),
                ("async", Json::n(s.resample_async as f64)),
            ]),
        ),
        ("fontsRegistered", Json::n(s.fonts as f64)),
        ("imgSrcs", Json::n(s.img_srcs as f64)),
        (
            "longest",
            Json::arr(
                longest
                    .iter()
                    .map(|f| {
                        let p = &f.phases;
                        Json::obj(vec![
                            ("t", Json::n(round1(f.at_ms))),
                            ("dur", r2(f.dur_ms)),
                            ("pump", r2(f.pump_ms)),
                            ("js", r2(f.js_ms)),
                            ("inputJs", r2(p.js_ms)),
                            ("sync", Json::n(f64::from(p.sync_layouts))),
                            ("syncMs", r2(p.sync_layout_ms)),
                            ("restyleMs", r2(p.restyle_ms)),
                            ("restyleFull", Json::n(f64::from(p.restyle_full))),
                            ("recompileMs", r2(p.recompile_ms)),
                            ("buildMs", r2(p.build_ms)),
                            ("draws", Json::n(f64::from(p.draw_passes))),
                            ("taffy", r2(p.taffy_ms)),
                            ("prepaint", r2(p.prepaint_ms)),
                            ("paint", r2(p.paint_ms)),
                            ("shapeMs", r2(f.shape_ms)),
                            ("dom", Json::n(f64::from(dom_total(p)))),
                            (
                                "marks",
                                Json::arr(f.marks.iter().map(|m| Json::s(m.clone())).collect()),
                            ),
                        ])
                    })
                    .collect(),
            ),
        ),
    ])
}

#[derive(Default)]
struct Totals {
    dur: f32,
    pump: f32,
    js: f32,
    input_js: f32,
    render: f32,
    dom: [u64; DomOp::COUNT],
    layout_reads: u64,
    style_reads: u64,
    style_flushes: u64,
    queries: u64,
    sync: u64,
    sync_ms: f32,
    sync_max: f32,
    sync_refused: u64,
    sync_tail: u64,
    sync_tail_free: u64,
    sync_per_frame_max: u32,
    observer_steps: u32,
    observer_rounds: u32,
    observer_relayouts: u32,
    observer_relayout_ms: f32,
    observer_ms: f32,
    observer_loop_errors: u32,
    sync_by: [u32; SyncCaller::COUNT],
    restyles: u64,
    restyle_full: u64,
    restyle_ms: f32,
    match_ms: f32,
    nodes: u64,
    changed: u64,
    recompiles: u64,
    recompile_ms: f32,
    full_reasons: [u32; FullReason::COUNT],
    builds: u64,
    build_ms: f32,
    elements: u64,
    draws: u64,
    layout_req: f32,
    taffy: f32,
    prepaint: f32,
    paint: f32,
    lookups: u64,
    shapes: u64,
    shape_ms: f32,
    resample_sync: u64,
    resample_sync_ms: f32,
    resample_async: u64,
    fonts: u64,
    img_srcs: u64,
}

impl Totals {
    fn add(&mut self, f: &Frame) {
        let p = &f.phases;
        self.dur += f.dur_ms;
        self.pump += f.pump_ms;
        self.js += f.js_ms;
        self.input_js += p.js_ms;
        self.render += f.render_ms;
        for (a, b) in self.dom.iter_mut().zip(p.dom_ops.iter()) {
            *a += u64::from(*b);
        }
        self.layout_reads += u64::from(p.layout_reads);
        self.style_reads += u64::from(p.style_reads);
        self.style_flushes += u64::from(p.style_flushes);
        self.queries += u64::from(p.queries);
        self.sync += u64::from(p.sync_layouts);
        self.sync_ms += p.sync_layout_ms;
        self.sync_max = self.sync_max.max(p.sync_layout_max_ms);
        self.sync_refused += u64::from(p.sync_layout_refused);
        self.sync_tail += u64::from(p.sync_tail);
        self.sync_tail_free += u64::from(p.sync_tail_free);
        self.observer_steps += p.observer_steps;
        self.observer_rounds += p.observer_rounds;
        self.observer_relayouts += p.observer_relayouts;
        self.observer_relayout_ms += p.observer_relayout_ms;
        self.observer_ms += p.observer_ms;
        self.observer_loop_errors += p.observer_loop_errors;
        self.sync_per_frame_max = self.sync_per_frame_max.max(p.sync_layouts);
        for (a, b) in self.sync_by.iter_mut().zip(p.sync_by.iter()) {
            *a += *b;
        }
        self.restyles += u64::from(p.restyle_passes);
        self.restyle_full += u64::from(p.restyle_full);
        self.restyle_ms += p.restyle_ms;
        self.match_ms += p.match_ms;
        self.nodes += p.nodes_cascaded;
        self.changed += p.nodes_changed;
        self.recompiles += u64::from(p.recompiles);
        self.recompile_ms += p.recompile_ms;
        for (a, b) in self.full_reasons.iter_mut().zip(p.full_reasons.iter()) {
            *a += *b;
        }
        self.builds += u64::from(p.build_passes);
        self.build_ms += p.build_ms;
        self.elements += p.elements_built;
        self.draws += u64::from(p.draw_passes);
        self.layout_req += p.layout_req_ms;
        self.taffy += p.taffy_ms;
        self.prepaint += p.prepaint_ms;
        self.paint += p.paint_ms;
        self.lookups += f.lookups;
        self.shapes += f.shapes;
        self.shape_ms += f.shape_ms;
        self.resample_sync += u64::from(p.resample_sync);
        self.resample_sync_ms += p.resample_sync_ms;
        self.resample_async += u64::from(p.resample_async);
        self.fonts += u64::from(p.fonts_registered);
        self.img_srcs += u64::from(p.img_srcs);
    }
}

pub(crate) fn recording_start(cap: Option<usize>) -> Result<Json, String> {
    let limits = sk_runtime::redjs::profiling::Limits {
        frames: cap.unwrap_or(1200),
        ..Default::default()
    };
    let context = sk_runtime::JsRuntime::try_with(|runtime| runtime.ctx().raw() as usize);
    sk_tree::perf::set_tracing(true);
    let status = sk_runtime::redjs::profiling::start(limits, context);
    #[cfg(target_os = "windows")]
    gpui_windows::set_frame_profiler(Some(gpui_windows::FrameProfilerCallbacks {
        begin: native_profile_begin,
        end: sk_runtime::redjs::profiling::external_end,
    }));
    Ok(recording_status_json(&status))
}

pub(crate) fn recording_stop() -> Result<Json, String> {
    #[cfg(target_os = "windows")]
    gpui_windows::set_frame_profiler(None);
    let status = sk_runtime::redjs::profiling::stop()
        .ok_or_else(|| "no frame recording exists".to_string())?;
    if !active() {
        sk_tree::perf::set_tracing(false);
    }
    Ok(recording_status_json(&status))
}

pub(crate) fn recording_status() -> Result<Json, String> {
    let status =
        sk_runtime::redjs::profiling::status().unwrap_or(sk_runtime::redjs::profiling::Status {
            recording: false,
            session_id: 0,
            start_epoch_ms: 0.,
            limits: Default::default(),
            frame_count: 0,
            dropped_frames: 0,
            dropped_spans: 0,
        });
    Ok(recording_status_json(&status))
}

pub(crate) fn cancel_recording() {
    #[cfg(target_os = "windows")]
    gpui_windows::set_frame_profiler(None);
    NATIVE_PROFILE_WINDOW.with(|target| target.set(0));
    sk_runtime::redjs::profiling::clear();
    TRACE.with(|trace| {
        trace.borrow_mut().take();
    });
    sk_tree::perf::set_tracing(false);
}

pub(crate) fn frame_page(offset: usize, limit: usize) -> Result<Json, String> {
    let page = sk_runtime::redjs::profiling::frames(offset, limit)
        .ok_or_else(|| "no frame recording exists".to_string())?;
    Ok(Json::obj(vec![
        ("sessionId", Json::n(page.status.session_id as f64)),
        ("recording", Json::Bool(page.status.recording)),
        ("total", Json::n(page.total as f64)),
        ("offset", Json::n(page.offset as f64)),
        ("nextOffset", offset_json(page.next_offset)),
        (
            "frames",
            Json::arr(page.frames.iter().map(recording_frame_json).collect()),
        ),
    ]))
}

pub(crate) fn frame_detail(frame_id: u64, offset: usize, limit: usize) -> Result<Json, String> {
    let page =
        sk_runtime::redjs::profiling::frame_detail(frame_id, offset, limit).ok_or_else(|| {
            "frame is unavailable (it may have been evicted by the recording limits)".to_string()
        })?;
    Ok(Json::obj(vec![
        ("sessionId", Json::n(page.session_id as f64)),
        ("frame", recording_frame_json(&page.frame)),
        ("offset", Json::n(page.offset as f64)),
        ("total", Json::n(page.total as f64)),
        ("nextOffset", offset_json(page.next_offset)),
        ("truncated", Json::Bool(page.truncated)),
        (
            "spans",
            Json::arr(page.spans.iter().map(recording_span_json).collect()),
        ),
    ]))
}

fn offset_json(offset: Option<usize>) -> Json {
    offset.map_or(Json::Null, |offset| Json::n(offset as f64))
}
fn milliseconds(value: f64) -> Json {
    Json::n((value * 1000.).round() / 1000.)
}
fn recording_status_json(status: &sk_runtime::redjs::profiling::Status) -> Json {
    Json::obj(vec![
        ("recording", Json::Bool(status.recording)),
        ("sessionId", Json::n(status.session_id as f64)),
        ("startEpochMs", Json::n(status.start_epoch_ms)),
        ("frameCount", Json::n(status.frame_count as f64)),
        ("droppedFrames", Json::n(status.dropped_frames as f64)),
        ("droppedSpans", Json::n(status.dropped_spans as f64)),
        (
            "limits",
            Json::obj(vec![
                ("frames", Json::n(status.limits.frames as f64)),
                (
                    "spansPerFrame",
                    Json::n(status.limits.spans_per_frame as f64),
                ),
                ("totalSpans", Json::n(status.limits.total_spans as f64)),
            ]),
        ),
        (
            "timing",
            Json::s(
                "instrumented main-thread elapsed time; not GPU timestamps or sampled CPU cycles",
            ),
        ),
        (
            "coverage",
            Json::obj(vec![
                (
                    "javascript",
                    Json::s("native-invoked callbacks and their host calls"),
                ),
                (
                    "microtasks",
                    Json::s("checkpoint batches; individual Promise reactions are not sampled"),
                ),
            ]),
        ),
    ])
}
fn recording_frame_json(frame: &sk_runtime::redjs::profiling::FrameSummary) -> Json {
    Json::obj(vec![
        ("id", Json::n(frame.id as f64)),
        ("startMs", milliseconds(frame.start_ms)),
        ("durationMs", milliseconds(frame.duration_ms)),
        ("cpuMs", milliseconds(frame.cpu_ms)),
        ("spanCount", Json::n(frame.span_count as f64)),
        ("droppedSpans", Json::n(frame.dropped_spans as f64)),
        (
            "categories",
            Json::arr(
                frame
                    .categories
                    .iter()
                    .map(|category| {
                        Json::obj(vec![
                            ("category", Json::s(category.category)),
                            ("selfMs", milliseconds(category.self_ms)),
                            ("totalMs", milliseconds(category.total_ms)),
                            ("count", Json::n(category.count as f64)),
                        ])
                    })
                    .collect(),
            ),
        ),
        (
            "hints",
            Json::arr(
                frame
                    .hints
                    .iter()
                    .map(|hint| {
                        let mut values = vec![
                            ("code", Json::s(hint.code)),
                            ("severity", Json::s(hint.severity)),
                            ("message", Json::s(&hint.message)),
                            ("count", Json::n(hint.count as f64)),
                        ];
                        if let Some(id) = hint.span_id {
                            values.push(("spanId", Json::n(id as f64)));
                        }
                        Json::obj(values)
                    })
                    .collect(),
            ),
        ),
    ])
}
fn recording_span_json(span: &sk_runtime::redjs::profiling::EventSpan) -> Json {
    let mut values = vec![
        ("id", Json::n(span.id as f64)),
        (
            "parentId",
            span.parent_id.map_or(Json::Null, |id| Json::n(id as f64)),
        ),
        ("category", Json::s(span.category)),
        ("name", Json::s(&span.name)),
        ("startMs", milliseconds(span.start_ms)),
        ("durationMs", milliseconds(span.duration_ms)),
        ("selfMs", milliseconds(span.self_ms)),
        ("depth", Json::n(span.depth as f64)),
        ("count", Json::n(1.)),
    ];
    if let Some(site) = &span.callsite {
        let mut callsite = vec![("kind", Json::s(site.kind))];
        if !site.function.is_empty() {
            callsite.push(("function", Json::s(&site.function)));
        }
        if !site.url.is_empty() {
            callsite.push(("url", Json::s(&site.url)));
        }
        if site.line > 0 {
            callsite.push(("line", Json::n(f64::from(site.line))));
        }
        if site.column > 0 {
            callsite.push(("column", Json::n(f64::from(site.column))));
        }
        values.push(("callsite", Json::obj(callsite)));
    }
    if !span.counters.is_empty() {
        values.push((
            "counters",
            Json::Obj(
                span.counters
                    .iter()
                    .map(|(name, value)| (name.clone(), Json::n(*value as f64)))
                    .collect(),
            ),
        ));
    }
    Json::obj(values)
}

pub(crate) fn set_recording_native_window(window: usize) {
    NATIVE_PROFILE_WINDOW.with(|target| target.set(window));
}
#[cfg(target_os = "windows")]
fn native_profile_begin(
    window: usize,
    category: &'static str,
    name: &'static str,
    file: &'static str,
    line: u32,
    column: u32,
) -> u64 {
    if !NATIVE_PROFILE_WINDOW.with(|target| target.get() != 0 && target.get() == window) {
        return 0;
    }
    sk_runtime::redjs::profiling::external_begin(category, name, file, line, column)
}

pub(crate) fn set_recording_window_id(window: u64) {
    sk_runtime::redjs::profiling::set_window_target(window);
}