feldera-samply 0.301.0

Enhance profiles produced using samply
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
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
//! Annotations for Firefox Profiler profiles.
//!
//! [Firefox Profiler] can display user-defined annotations for timespans and
//! events, as well as information about network activity and memory use, but
//! only if the profiler added those annotations.  The popular [samply]
//! profiler, however, has only very limited support for adding these
//! annotations.  This crate provides a way for a process that is being profiled
//! to record its own annotations and then merge them into profiler output in a
//! postprocessing step.
//!
//! # Use
//!
//! This crate can be integrated into an existing profiler workflow.  For
//! example, the [Feldera incremental compute engine] runs `samply` as a
//! subprocess, targeting itself.  While `samply` runs, Feldera uses [Capture],
//! [Span], [LongSpan], and [Event] to record annotations.  After `samply`
//! completes, Feldera finishes the capture to obtain [Annotations], applies
//! them, and then passes the postprocessed output to the user.  The annotation
//! step is invisible to the user.
//!
//! Short of this kind of integration, where a process effectively profiles
//! itself, there must be some way to enable capturing and saving profile data.
//! For example, a command-line option or an environment variable could do the
//! trick.  Once the capture is complete, the process needs to somehow save the
//! annotations.
//!
//! # Viewing in Firefox Profiler
//!
//! Spans and events logged by this module show up in the Marker Chart and
//! Marker Table tabs for a given thread. They are linked to particular threads
//! and the profiler will only show them when those threads are selected.
//!
//! Spans and events are enabled only when a profile is running.  They have
//! minimal overhead otherwise.
//!
//! [samply]: https://github.com/mstange/samply?tab=readme-ov-file#samply
//! [Firefox Profiler]: https://profiler.firefox.com/
//! [Feldera incremental compute engine]: https://feldera.com
#![warn(missing_docs)]
use std::{
    borrow::Cow,
    collections::{HashMap, HashSet},
    fmt::{Debug, Display},
    io::{Cursor, Read},
    iter::{once, repeat_n},
    mem::swap,
    sync::{
        Arc,
        atomic::{AtomicBool, AtomicI64, Ordering},
    },
    thread::JoinHandle,
    time::{Duration, Instant},
};

use crossbeam::sync::{Parker, Unparker};
use flate2::{
    Compression,
    bufread::{GzDecoder, GzEncoder},
};
use itertools::Itertools;
use memory_stats::memory_stats;
use nix::time::{ClockId, clock_gettime};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use size_of::HumanBytes;
use tracing::warn;

/// Atomic `Option<Timestamp>`.
///
/// Treats `i64::MIN` as a niche.
#[derive(Debug)]
struct AtomicOptionTimestamp(AtomicI64);

impl Default for AtomicOptionTimestamp {
    fn default() -> Self {
        Self::new(None)
    }
}

impl AtomicOptionTimestamp {
    fn new(value: Option<Timestamp>) -> Self {
        Self(AtomicI64::new(
            value.map_or(i64::MIN, |timestamp| timestamp.0),
        ))
    }

    fn load(&self) -> Option<Timestamp> {
        let value = self.0.load(Ordering::Relaxed);
        (value != i64::MIN).then_some(Timestamp(value))
    }

    fn store(&self, value: Option<Timestamp>) {
        self.0.store(
            value.map_or(i64::MIN, |timestamp| timestamp.0),
            Ordering::Relaxed,
        )
    }
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[repr(transparent)]
struct Timestamp(
    /// In nanoseconds in terms of `CLOCK_MONOTONIC`.
    i64,
);

impl Timestamp {
    fn now() -> Self {
        let now = clock_gettime(ClockId::CLOCK_MONOTONIC).unwrap();
        Self(now.tv_sec() as i64 * 1_000_000_000 + now.tv_nsec() as i64)
    }
}

impl From<Instant> for Timestamp {
    fn from(value: Instant) -> Self {
        // SAFETY: On Unix, `Instant` is implemented using CLOCK_MONOTONIC,
        // which is the clock that we need to use for the profiler, but the Rust
        // standard library provides no way to get the value out.  We don't want
        // to make assumptions about the layout of [Instant], and in fact it is
        // not defined as libc's struct timespec but different and
        // Rust-specific.  If we just transmute then we get the wrong value.  It
        // seems rather safer to assume that the all-bytes-zeros `Instant` is
        // the origin, and it works OK for now at least.
        //
        // The completely safe alternative would be to make Timestamp public and
        // force clients to always get both a Timestamp and an Instant if they
        // need both, which is wasteful.
        let zero = unsafe { std::mem::zeroed::<Instant>() };
        Self((value - zero).as_nanos() as i64)
    }
}

impl Display for Timestamp {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl Serialize for Timestamp {
    /// Serializes the format used in the [Gecko profiler] format.
    ///
    /// [Gecko profiler]: https://github.com/firefox-devtools/profiler/blob/main/src/types/profile.ts
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let milliseconds = self.0 as f64 / 1_000_000.0;
        milliseconds.serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for Timestamp {
    /// Deserializes the format used in the [Gecko profiler] format.
    ///
    /// [Gecko profiler]: https://github.com/firefox-devtools/profiler/blob/main/src/types/profile.ts
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let milliseconds = f64::deserialize(deserializer)?;
        Ok(Self((milliseconds * 1_000_000.0) as i64))
    }
}

impl Debug for Span {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Span")?;
        if let Some(inner) = &self.0 {
            write!(f, "({})", &inner.name)?;
        }
        Ok(())
    }
}

struct SpanInner {
    start: Timestamp,
    category: &'static str,
    name: &'static str,
    tooltip: String,
}

impl SpanInner {
    #[cold]
    fn new(name: &'static str) -> Self {
        Self {
            start: Timestamp::now(),
            category: "Other",
            name,
            tooltip: String::new(),
        }
    }

    fn into_marker(self, end: MarkerEnd) -> Marker {
        Marker {
            start: self.start,
            end,
            category: self.category,
            name: self.name,
            tooltip: self.tooltip,
        }
    }

    #[cold]
    fn record(self, end: MarkerEnd) {
        QUEUE.with(|queue| queue.push(self.into_marker(end)));
    }
}

/// Annotates a timespan during a [Capture].
///
/// Constructing and dropping a [Span], when marker spans are being captured
/// with [Capture], records the start and end times of the [Span] along with a
/// name, category, and tooltip.
///
/// `Span` is for timespans.  Use [Event] for point-in-time events.
///
/// When [Capture] is not active, `Span` has minimal overhead.
///
/// [samply]: https://github.com/mstange/samply?tab=readme-ov-file#samply
pub struct Span(Option<SpanInner>);

impl Span {
    /// The number of bytes of memory used during capture to record a [Span],
    /// [LongSpan], or [Event].
    pub const BYTES: usize = std::mem::size_of::<Marker>();

    /// Constructs a new [Span] with the given name.  When the constructed
    /// span is dropped, it is automatically recorded.
    ///
    /// [Span] does nothing when markers are not being captured.  A span
    /// will be recorded in a profile only if markers were being captured both
    /// when it was created and when it was dropped.
    ///
    /// The name should ordinarily be a short static string indicating what
    /// happens during the span.  The Firefox Profiler's marker chart view shows
    /// all the spans in a thread with the same name and category on a single
    /// horizontal timeline (unless that would cause overlaps).
    #[must_use]
    pub fn new(name: &'static str) -> Self {
        Self(Capture::is_active().then(|| SpanInner::new(name)))
    }

    /// Adds `category` to this span.
    ///
    /// The Firefox Profiler's marker chart view groups the markers in each
    /// category and labels them with the category name.
    ///
    /// The default category is "Other".
    #[must_use]
    pub fn with_category(mut self, category: &'static str) -> Self {
        if let Some(inner) = &mut self.0 {
            inner.category = category;
        }
        self
    }

    /// Evaluates `tooltip` and adds it to this span.
    ///
    /// The Firefox Profiler shows the given tooltip in the marker chart
    /// timeline (often truncated) and on hover, and as "details" in the marker
    /// table view.
    ///
    /// `tooltip` is only evaluated if capturing is active.
    #[must_use]
    pub fn with_tooltip<F>(mut self, tooltip: F) -> Self
    where
        F: FnOnce() -> String,
    {
        if let Some(inner) = &mut self.0 {
            inner.tooltip = tooltip();
        }
        self
    }

    /// Sets the starting time for this span to `start`.  The default starting
    /// time is when the [Span] was constructed, so this is only useful if
    /// it's easier to create the span just before recording it.
    #[must_use]
    pub fn with_start(mut self, start: Instant) -> Self {
        if let Some(inner) = &mut self.0 {
            inner.start = start.into();
        }
        self
    }

    /// Calls `f` and records the span.  Returns whatever `f` returned.
    pub fn in_scope<F, T>(self, f: F) -> T
    where
        F: FnOnce() -> T,
    {
        f()
    }

    /// Records the span.
    pub fn record(self) {
        // [Drop] records the span.
    }

    /// Consumes the span without recording it.
    pub fn cancel(mut self) {
        let _ = self.0.take();
    }
}

impl Drop for Span {
    fn drop(&mut self) {
        if let Some(inner) = self.0.take() {
            inner.record(MarkerEnd::At(Timestamp::now()))
        }
    }
}

/// Builds a [LongSpan] for annotating a long timespan.
pub struct LongSpanBuilder(SpanInner);

impl LongSpanBuilder {
    /// Constructs a new [LongSpanBuilder] with the given name.
    ///
    /// The name should ordinarily be a short static string indicating what
    /// happens during the span.  The Firefox Profiler's marker chart view shows
    /// all the spans in a thread with the same name and category on a single
    /// horizontal timeline (unless that would cause overlaps).
    #[must_use]
    pub fn new(name: &'static str) -> Self {
        Self(SpanInner::new(name))
    }

    /// Adds `category` to this span.
    ///
    /// The Firefox Profiler's marker chart view groups the markers in each
    /// category and labels them with the category name.
    ///
    /// The default category is "Other".
    #[must_use]
    pub fn with_category(mut self, category: &'static str) -> Self {
        self.0.category = category;
        self
    }

    /// Adds `tooltip` to this span.
    ///
    /// The Firefox Profiler shows the given tooltip in the marker chart
    /// timeline (often truncated) and on hover, and as "details" in the marker
    /// table view.
    #[must_use]
    pub fn with_tooltip(mut self, tooltip: impl Into<String>) -> Self {
        self.0.tooltip = tooltip.into();
        self
    }

    /// Sets the starting time for this span to `start`.  The default starting
    /// time is when the [LongSpanBuilder] was constructed, so this is only
    /// useful if it's easier to create the span just before recording it.
    #[must_use]
    pub fn with_start(mut self, start: Instant) -> Self {
        self.0.start = start.into();
        self
    }

    /// Builds the [LongSpan].
    #[must_use]
    pub fn build(self) -> LongSpan {
        let timestamp = Arc::new(AtomicOptionTimestamp::default());
        QUEUE.with(|queue| {
            queue.push_long_span(self.0.into_marker(MarkerEnd::Long(timestamp.clone())))
        });
        LongSpan(timestamp)
    }
}

/// A relatively expensive way to annotate a longer timespan during [Capture]s.
///
/// `LongSpan` is much like [Span].  However, whereas [Span] is optimized to
/// minimize time and space overhead when a capture is not active, `LongSpan`
/// always tracks the span.  This allows it to show up in captures that start
/// after the `LongSpan` is allocated or that end before the `LongSpan` is
/// recorded.
///
/// [samply]: https://github.com/mstange/samply?tab=readme-ov-file#samply
pub struct LongSpan(Arc<AtomicOptionTimestamp>);

impl LongSpan {
    /// Marks this `LongSpan` as complete.
    ///
    /// This is equivalent to dropping it.
    pub fn complete(self) {
        // [Drop] completes the span.
    }
}

impl Drop for LongSpan {
    fn drop(&mut self) {
        self.0.store(Some(Timestamp::now()));
    }
}

/// Annotates an event during a [Capture].
///
/// When a [Capture] is running, use this type to record an event along
/// with a name, category, and tooltip.
///
/// An event happens at a point in time; use [Span] to record a timespan.
///
/// When [Capture] is not active, `Event` has minimal overhead.
///
/// [samply]: https://github.com/mstange/samply?tab=readme-ov-file#samply
/// [module documentation]: crate
pub struct Event(Option<SpanInner>);

impl Event {
    /// Constructs a new [Event] with the given name.
    ///
    /// [Event] does nothing when markers are not being captured.
    ///
    /// The name should ordinarily be a short static string indicating what the
    /// event did.  The Firefox Profiler's marker chart view shows all the
    /// events in a thread with the same name and category on a single
    /// horizontal timeline (unless that would cause overlaps).
    #[must_use]
    pub fn new(name: &'static str) -> Self {
        Self(Capture::is_active().then(|| SpanInner::new(name)))
    }

    /// Adds `category` to this event.
    ///
    /// The Firefox Profiler's marker chart view groups the markers in each
    /// category and labels them with the category name.
    ///
    /// The default category is "Other".
    #[must_use]
    pub fn with_category(mut self, category: &'static str) -> Self {
        if let Some(inner) = &mut self.0 {
            inner.category = category;
        }
        self
    }

    /// Evaluates `tooltip` and adds it to this event.
    ///
    /// The Firefox Profiler shows the given tooltip in the marker chart
    /// timeline (often truncated) and on hover, and as "details" in the marker
    /// table view.
    ///
    /// `tooltip` is only evaluated if capturing is active.
    #[must_use]
    pub fn with_tooltip<F>(mut self, tooltip: F) -> Self
    where
        F: FnOnce() -> String,
    {
        if let Some(inner) = &mut self.0 {
            inner.tooltip = tooltip();
        }
        self
    }

    /// Records the event.
    pub fn record(self) {
        if let Some(inner) = self.0 {
            let end = MarkerEnd::At(inner.start);
            inner.record(end);
        }
    }
}

/// Options for capturing profile annotations.
#[derive(Clone, Debug)]
pub struct CaptureOptions {
    memory_limit: Option<usize>,
    record_rss: bool,
}

impl Default for CaptureOptions {
    fn default() -> Self {
        Self {
            memory_limit: None,
            record_rss: true,
        }
    }
}

impl CaptureOptions {
    /// Creates new capture parameters with default settings.
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets a limit on the amount of memory that can be used for recording
    /// captured spans to `memory_limit`, in bytes.  If `memory_limit` is
    /// `None`, then there will be no limit (which is also the default).
    ///
    /// The limit is honored approximately.  Recording a span takes
    /// [Span::BYTES] bytes.
    pub fn with_memory_limit(self, memory_limit: Option<usize>) -> Self {
        Self {
            memory_limit,
            ..self
        }
    }

    /// Enables or disables recording the amount of memory in use (the resident
    /// set size) during the capture.  By default, RSS is recorded every 100
    /// milliseconds.  When this feature is enabled, capture runs a thread for
    /// recording the data.
    pub fn with_record_rss(self, record_rss: bool) -> Self {
        Self { record_rss, ..self }
    }

    /// Starts capturing profile annotations, returning a [Capture] that can be
    /// used to finish or abort captures.  Dropping the [Capture] will also
    /// abort capturing.
    ///
    /// For use in asynchronous contexts.  If a capture is already in progress,
    /// this function will wait for it to complete before starting a new one.
    pub async fn start(self) -> Capture {
        Capture::new(self, CAPTURE_MUTEX.lock().await)
    }

    /// Starts capturing profile annotations, returning a [Capture] that can be
    /// used to finish or abort captures.  Dropping the [Capture] will also
    /// abort capturing.
    ///
    /// For use in blocking contexts.  If a capture is already in progress, this
    /// function will wait for it to complete before starting a new one.
    ///
    /// # Panic
    ///
    /// Panics if called from an asynchronous execution context.
    pub fn blocking_start(self) -> Capture {
        Capture::new(self, CAPTURE_MUTEX.blocking_lock())
    }

    /// Starts capturing profile annotations, returning a [Capture] that can be
    /// used to finish or abort captures.  Dropping the [Capture] will also
    /// abort capturing.
    ///
    /// If a capture is already in progress, this function returns an error
    /// instead of waiting.
    pub fn try_start(self) -> Result<Capture, Self> {
        let guard = match CAPTURE_MUTEX.try_lock() {
            Ok(guard) => guard,
            Err(_) => return Err(self),
        };
        Ok(Capture::new(self, guard))
    }
}

static CAPTURE_MUTEX: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());

/// An in-progress capture of profile annotations.
///
/// To start a capture, use [CaptureOptions::start],
/// [CaptureOptions::blocking_start], or [CaptureOptions::try_start].
///
/// Only one `Capture` may exist at one time.
pub struct Capture {
    _guard: tokio::sync::MutexGuard<'static, ()>,
    memory: Option<JoinHandle<Vec<(Timestamp, usize)>>>,
    unparker: Unparker,
    request_exit: Arc<AtomicBool>,
    block_limit: i64,
}

impl Capture {
    fn new(params: CaptureOptions, guard: tokio::sync::MutexGuard<'static, ()>) -> Self {
        if let Some(memory_limit) = params.memory_limit {
            tracing::info!(
                "marker capture limited to {}",
                HumanBytes::from(memory_limit)
            );
        }
        let block_limit = params.memory_limit.map_or(i64::MAX, |memory_limit| {
            (memory_limit / BYTES_PER_BLOCK) as i64
        });
        let parker = Parker::new();
        let unparker = parker.unparker().clone();
        let request_exit = Arc::new(AtomicBool::new(false));
        let memory = params.record_rss.then(|| {
            std::thread::Builder::new()
                .name(String::from("capture-rss"))
                .spawn({
                    let request_exit = request_exit.clone();
                    move || {
                        let mut memory = Vec::new();
                        while !request_exit.load(Ordering::Acquire) {
                            if let Some(memory_stats) = memory_stats() {
                                memory.push((Timestamp::now(), memory_stats.physical_mem));
                            }
                            parker.park_timeout(Duration::from_millis(100));
                        }
                        memory
                    }
                })
                .expect("should be able to start a capture thread")
        });
        FREE_BLOCKS.store(block_limit, Ordering::Relaxed);
        CAPTURING.store(true, Ordering::Release);
        Self {
            block_limit,
            memory,
            unparker,
            request_exit,
            _guard: guard,
        }
    }

    /// Finishes recording profile annotations and returns what was recorded.
    pub fn finish(mut self) -> Annotations {
        let end_time = Timestamp::now();
        CAPTURING.store(false, Ordering::Release);
        let remaining_blocks = FREE_BLOCKS.load(Ordering::Relaxed);
        if remaining_blocks <= 0 {
            tracing::info!("marker capture exceeded the limit");
        } else {
            let used_bytes = (self.block_limit - remaining_blocks) as usize * BYTES_PER_BLOCK;
            tracing::info!("marker capture used {}", HumanBytes::from(used_bytes));
        }

        let markers = self
            .all_threads()
            .into_iter()
            .map(|thread| {
                let mut blocks = thread.queue.take_blocks();

                let long_spans = thread.queue.take_long_spans();
                if !long_spans.is_empty() {
                    blocks.0.push(Block(long_spans));
                }

                (thread.tid, (thread.name, blocks))
            })
            .collect();

        Annotations {
            end_time,
            markers,
            memory: self.take_memory(),
        }
    }

    /// Aborts recording profile annotations.
    ///
    /// This is equivalent to dropping the `Capture` object.
    pub fn abort(self) {
        tracing::info!("aborting profile annotation capture");
    }

    /// Returns true if a profile annotation capture is ongoing.
    ///
    /// This only reports the status of annotation captures.  It does not
    /// indicate whether `samply` or `perf` or another profiler is currently
    /// capturing profile data for this process (this crate does not provide a
    /// way to do that).
    pub fn is_active() -> bool {
        CAPTURING.load(Ordering::Acquire)
    }

    fn all_threads(&mut self) -> Vec<ThreadMarkers> {
        ALL_THREAD_MARKERS.lock().unwrap().clone()
    }

    fn take_memory(&mut self) -> Vec<(Timestamp, usize)> {
        if let Some(memory) = self.memory.take() {
            self.request_exit.store(true, Ordering::Release);
            self.unparker.unpark();
            memory.join().unwrap_or_default()
        } else {
            Default::default()
        }
    }
}

impl Drop for Capture {
    fn drop(&mut self) {
        self.take_memory();

        // Might already have been done in [Capture::finish] but it doesn't hurt
        // to do it again (since the lock is still held).
        CAPTURING.store(false, Ordering::Release);
    }
}

/// Error returned by [Annotations::apply].
#[derive(thiserror::Error, Debug)]
pub enum Error {
    /// Error decompressing the profile.
    #[error("Error decompressing profile ")]
    GzDecoderError(#[from] std::io::Error),

    /// Error parsing the profile.
    #[error("Error parsing profile")]
    SerdeError(#[from] serde_json_path_to_error::Error),
}

/// Options for applying annotations.
#[derive(Default, Clone, Debug)]
pub struct AnnotationOptions {
    product: Option<String>,
    os_cpu: Option<String>,
}

impl AnnotationOptions {
    /// Constructs a default set of options.
    pub fn new() -> Self {
        Self::default()
    }

    /// Overrides the product string in the profile.  `None` uses the default,
    /// which is `PID <pid>`.
    ///
    /// The Firefox Profiler prominently displays the product and OS-CPU string
    /// together in the form `<product> - <OS-CPU>`.
    pub fn with_product(self, product: Option<impl Into<String>>) -> Self {
        Self {
            product: product.map(|s| s.into()),
            ..self
        }
    }

    /// Overrides the OS and CPU string in the profile.  `None` uses the
    /// default, which looks like `Ubuntu 24.0.04.4 LTS`.
    ///
    /// The Firefox Profiler prominently displays the product and OS-CPU string
    /// together in the form `<product> - <OS-CPU>`.
    pub fn with_os_cpu(self, os_cpu: Option<impl Into<String>>) -> Self {
        Self {
            os_cpu: os_cpu.map(|s| s.into()),
            ..self
        }
    }
}

/// Profile annotation data.
///
/// Obtained from [Capture::finish].
pub struct Annotations {
    end_time: Timestamp,
    markers: HashMap<usize, (Option<String>, Blocks)>,
    memory: Vec<(Timestamp, usize)>,
}

impl Annotations {
    /// Applies these annotations with the given `options` to `profile`, which
    /// must be the `profile.json` output by samply, and returns the annotated
    /// profile.
    ///
    /// The input may be gzipped or already decompressed.  The output will be in
    /// the same form.
    pub fn apply(&self, profile: &[u8], options: AnnotationOptions) -> Result<Vec<u8>, Error> {
        // Decompress `profile` if it starts with the GZIP magic number,
        // otherwise assume it has already been decompressed.
        let mut buffer = Vec::new();
        let json = if profile.starts_with(&[0x1f, 0x8b]) {
            GzDecoder::new(profile).read_to_end(&mut buffer)?;
            &buffer
        } else {
            profile
        };
        let gzip = !buffer.is_empty();

        // Deserialize.
        let mut profile = serde_json_path_to_error::from_slice::<Profile>(json)?;
        if let Some(product) = options.product {
            profile.meta.product = product;
        }
        if let Some(os_cpu) = options.os_cpu {
            profile.meta.os_cpu = os_cpu;
        }
        profile.meta.marker_schema.push(json!({
            "name": "FelderaMarker",
            "display": [
                "marker-chart",
                "marker-table"
            ],
            "chartLabel": "{marker.data.name}",
            "tooltipLabel": "{marker.data.name}",
            "tableLabel": "{marker.data.name}",
            "description": "Marker generated by Feldera.",
            "fields": [
                {
                    "key": "name",
                    "label": "Name",
                    "format": "unique-string"
                }
            ]
        }));
        /// The colors that the profiler accepts for categories (see
        /// https://github.com/firefox-devtools/profiler/blob/main/src/types/profile.ts).
        static CATEGORY_COLORS: [&str; 12] = [
            "purple",
            "green",
            "orange",
            "yellow",
            "lightblue",
            "blue",
            "brown",
            "magenta",
            "red",
            "lightred",
            "darkgrey",
            "grey",
        ];
        let mut categories = profile
            .meta
            .categories
            .iter()
            .enumerate()
            .map(|(index, category)| (category.name.clone(), index))
            .collect::<HashMap<_, _>>();
        for (category, color) in self
            .markers
            .values()
            .flat_map(|(_, markers)| markers.iter())
            .map(|marker| marker.category)
            .collect::<HashSet<_>>()
            .into_iter()
            .zip(CATEGORY_COLORS.iter().cycle())
        {
            categories.insert(category.into(), profile.meta.categories.len());
            profile.meta.categories.push(Category {
                color: (*color).into(),
                name: category.into(),
                other: [(String::from("subcategories"), json!(["Other"]))]
                    .into_iter()
                    .collect(),
            });
        }
        for thread in &mut profile.threads {
            if let Some(tid) = &thread.tid
                && let Ok(tid) = tid.parse::<usize>()
                && let Some((name, markers)) = self.markers.get(&tid)
            {
                if let Some(name) = name {
                    thread.name = Some(name.clone());
                }
                for marker in markers.iter() {
                    thread.markers.length += 1;
                    thread.markers.category.push(categories[marker.category]);
                    thread.markers.data.push(ProfileMarkerData {
                        type_: Cow::from("FelderaMarker"),
                        name: profile.shared.add_name(&marker.tooltip),
                    });
                    thread.markers.start_time.push(marker.start);
                    thread
                        .markers
                        .end_time
                        .push(marker.end.timestamp().unwrap_or(self.end_time));
                    thread
                        .markers
                        .name
                        .push(profile.shared.add_name(marker.name));
                    thread.markers.phase.push(1);
                }
            }
        }

        if !self.memory.is_empty() {
            profile.counters.push(RawCounter {
                name: String::from("RSS"),
                category: String::from("Memory"),
                description: String::from("RSS in bytes"),
                pid: profile.threads.first().unwrap().pid.clone(),
                main_thread_index: 0,
                samples: {
                    RawCounterSamplesTable {
                        time: self
                            .memory
                            .iter()
                            .copied()
                            .map(|(time, _rss)| time)
                            .collect(),
                        time_deltas: Vec::new(),
                        number: repeat_n(0, self.memory.len()).collect(),
                        count: once(self.memory[0].1 as i64)
                            .chain(
                                self.memory
                                    .iter()
                                    .map(|(_time, rss)| *rss as i64)
                                    .tuple_windows()
                                    .map(|(prev, next)| next - prev),
                            )
                            .collect(),
                        length: self.memory.len(),
                        other: Default::default(),
                    }
                },
                other: Default::default(),
            });
        }

        // Produce the output, gzipping it if the input was gzipped.
        let output = serde_json::to_vec(&profile).unwrap();
        let output = if gzip {
            let mut gzipped_output = Vec::new();
            GzEncoder::new(Cursor::new(output), Compression::fast())
                .read_to_end(&mut gzipped_output)
                .unwrap();
            gzipped_output
        } else {
            output
        };

        return Ok(output);

        /// This is the [Gecko profiler] format.
        ///
        /// [Gecko profiler]: https://github.com/firefox-devtools/profiler/blob/main/src/types/profile.ts
        #[derive(Debug, Serialize, Deserialize)]
        #[serde(rename_all = "camelCase")]
        struct Profile {
            meta: Meta,
            threads: Vec<Thread>,
            #[serde(default)]
            counters: Vec<RawCounter>,
            shared: Shared,
            #[serde(flatten)]
            other: HashMap<String, Value>,
        }

        #[derive(Debug, Serialize, Deserialize)]
        #[serde(rename_all = "camelCase")]
        struct Meta {
            product: String,
            #[serde(rename = "oscpu")]
            os_cpu: String,
            categories: Vec<Category>,
            marker_schema: Vec<Value>,
            #[serde(flatten)]
            other: HashMap<String, Value>,
        }

        #[derive(Debug, Serialize, Deserialize)]
        #[serde(rename_all = "camelCase")]
        struct Category {
            color: String,
            name: String,
            #[serde(flatten)]
            other: HashMap<String, Value>,
        }

        #[derive(Debug, Serialize, Deserialize)]
        #[serde(rename_all = "camelCase")]
        struct Thread {
            name: Option<String>,
            #[serde(default)]
            markers: ProfileMarkers,
            tid: Option<String>,
            pid: String,
            #[serde(flatten)]
            other: HashMap<String, Value>,
        }

        #[derive(Default, Debug, Serialize, Deserialize)]
        #[serde(rename_all = "camelCase")]
        struct ProfileMarkers {
            length: usize,
            category: Vec<usize>,
            data: Vec<ProfileMarkerData>,
            start_time: Vec<Timestamp>,
            end_time: Vec<Timestamp>,
            name: Vec<usize>,
            phase: Vec<usize>,
        }

        #[derive(Default, Debug, Serialize, Deserialize)]
        #[serde(rename_all = "camelCase")]
        struct ProfileMarkerData {
            #[serde(rename = "type")]
            type_: Cow<'static, str>,
            name: usize,
        }

        #[derive(Debug, Serialize, Deserialize)]
        #[serde(rename_all = "camelCase")]
        struct Shared {
            string_array: Vec<String>,
            #[serde(flatten)]
            other: HashMap<String, Value>,
        }

        impl Shared {
            fn add_name(&mut self, name: &str) -> usize {
                let index = self.string_array.len();
                self.string_array.push(name.into());
                index
            }
        }

        #[derive(Default, Debug, Serialize, Deserialize)]
        #[serde(rename_all = "camelCase")]
        struct RawCounter {
            name: String,
            category: String,
            description: String,
            pid: String,
            main_thread_index: usize,
            samples: RawCounterSamplesTable,
            #[serde(flatten)]
            other: HashMap<String, Value>,
        }

        #[derive(Default, Debug, Serialize, Deserialize)]
        #[serde(rename_all = "camelCase")]
        struct RawCounterSamplesTable {
            #[serde(default, skip_serializing_if = "Vec::is_empty")]
            time: Vec<Timestamp>,
            #[serde(default, skip_serializing_if = "Vec::is_empty")]
            time_deltas: Vec<Timestamp>,
            #[serde(default, skip_serializing_if = "Vec::is_empty")]
            number: Vec<usize>,
            count: Vec<i64>,
            length: usize,
            #[serde(flatten)]
            other: HashMap<String, Value>,
        }
    }
}

/// Whether capturing is active.
static CAPTURING: AtomicBool = AtomicBool::new(false);

/// End time of a marker.
#[derive(Clone, Debug)]
enum MarkerEnd {
    /// A particular end time for a [Span].
    At(Timestamp),

    /// A possible end time for a [LongSpan].  If the span has not yet ended,
    /// this is `None`.
    Long(Arc<AtomicOptionTimestamp>),
}

impl MarkerEnd {
    /// Returns false if this marker should be dropped because it is no longer
    /// significant.
    ///
    /// `capturing` specifies whether a capture is currently running.  More
    /// markers are relevant when a capture is running because they will be
    /// recorded when the capture ends.
    fn should_keep(&self, capturing: bool) -> bool {
        if let MarkerEnd::Long(timestamp) = self {
            if timestamp.load().is_some() {
                // Drop it if there is no capture running, because it ended
                // before any new capture can start.
                capturing
            } else if Arc::strong_count(timestamp) > 1 {
                // There's another owner who might eventually complete this span.
                true
            } else {
                // This span is orphaned and will never complete.  One could
                // argue for keeping it.  We drop it to avoid having a kind of
                // memory leak.
                //
                // We keep it if a capture is running, so that it is included in
                // the current capture.
                capturing
            }
        } else {
            false
        }
    }

    /// Returns the inner timestamp, or `None` if there isn't one yet because
    /// this is an ongoing [LongSpan].
    fn timestamp(&self) -> Option<Timestamp> {
        match self {
            MarkerEnd::At(timestamp) => Some(*timestamp),
            MarkerEnd::Long(timestamp) => timestamp.load(),
        }
    }
}

/// A single marker as captured.
#[derive(Clone, Debug)]
struct Marker {
    /// Start time.
    start: Timestamp,
    /// End time.
    end: MarkerEnd,
    /// Category (used for outer grouping).
    category: &'static str,
    /// Name (used for inner grouping).
    name: &'static str,
    /// Shown on hover.
    tooltip: String,
}

/// Markers for a given thread.
#[derive(Clone)]
struct ThreadMarkers {
    /// The thread's tid.
    ///
    /// We need this to identify this thread in the profiler file.
    tid: usize,

    /// The thread's name.
    ///
    /// The profiler gets thread names from the kernel, but they are truncated
    /// at 15 bytes.  We supply the full thread name.
    name: Option<String>,

    /// The thread's markers.
    ///
    /// The thread itself records [Event]s and [Span]s by pushing them onto the
    /// queue.  [Capture::finish] pops them all off.
    queue: Arc<Queue>,
}

impl ThreadMarkers {
    fn new(queue: Arc<Queue>) -> Self {
        #[cfg(target_os = "linux")]
        let tid = nix::unistd::gettid().as_raw() as usize;
        #[cfg(not(target_os = "linux"))]
        let tid = thread_id::get();

        Self {
            tid,
            name: std::thread::current().name().map(|s| s.into()),
            queue,
        }
    }
}

/// [ThreadMarkers] for every thread that has recorded a marker.
static ALL_THREAD_MARKERS: std::sync::Mutex<Vec<ThreadMarkers>> = std::sync::Mutex::new(Vec::new());

static FREE_BLOCKS: AtomicI64 = AtomicI64::new(0);
const MARKERS_PER_BLOCK: usize = 32;
const BYTES_PER_BLOCK: usize = MARKERS_PER_BLOCK * Span::BYTES;

struct Block(Vec<Marker>);
impl Block {
    fn new(marker: Marker) -> Self {
        let mut markers = Vec::with_capacity(MARKERS_PER_BLOCK);
        markers.push(marker);
        Self(markers)
    }
    fn is_full(&self) -> bool {
        self.0.len() >= self.0.capacity()
    }
    fn push(&mut self, marker: Marker) {
        self.0.push(marker);
    }
}

struct Blocks(Vec<Block>);

impl Default for Blocks {
    fn default() -> Self {
        Self(Vec::with_capacity(32))
    }
}

impl Blocks {
    fn push(&mut self, marker: Marker) {
        if let Some(block) = self.0.last_mut()
            && !block.is_full()
        {
            block.push(marker);
        } else {
            match FREE_BLOCKS.fetch_sub(1, Ordering::Relaxed) {
                1.. => self.0.push(Block::new(marker)),
                0 => warn!("marker capture space exhausted"),
                _ => (),
            }
        }
    }

    fn iter(&self) -> impl Iterator<Item = &Marker> {
        self.0.iter().flat_map(|block| block.0.iter())
    }
}

#[derive(Debug, Default)]
struct LongSpans {
    markers: Vec<Marker>,
}

impl LongSpans {
    fn push(&mut self, marker: Marker) {
        if self.markers.len() == self.markers.capacity() {
            // Do garbage collection.
            let capturing = Capture::is_active();
            self.markers
                .retain(|marker| marker.end.should_keep(capturing));
        }
        self.markers.push(marker);
    }

    fn append(&mut self, other: &mut Vec<Marker>) {
        if self.markers.is_empty() {
            swap(&mut self.markers, other);
        } else {
            self.markers.append(other);
        }
    }
}

struct Queue {
    /// Records [Span] and [Event] markers.
    blocks: std::sync::Mutex<Blocks>,

    /// Records [LongSpan] markers.
    ///
    /// These are recorded separately because they are not accounted the same
    /// way and because they need to be garbage collected.
    long_spans: std::sync::Mutex<LongSpans>,
}

impl Queue {
    fn new() -> Arc<Self> {
        let queue = Arc::new(Self {
            blocks: Default::default(),
            long_spans: Default::default(),
        });
        ALL_THREAD_MARKERS
            .lock()
            .unwrap()
            .push(ThreadMarkers::new(queue.clone()));
        queue
    }

    /// Adds `marker` if there's room with the limit.
    fn push(&self, marker: Marker) {
        self.blocks.lock().unwrap().push(marker);
    }

    /// Adds `marker` to the collection of long spans.
    fn push_long_span(&self, marker: Marker) {
        self.long_spans.lock().unwrap().push(marker);
    }

    fn take_blocks(&self) -> Blocks {
        std::mem::take(&mut *self.blocks.lock().unwrap())
    }

    fn take_long_spans(&self) -> Vec<Marker> {
        let old_long_spans = std::mem::take(&mut *self.long_spans.lock().unwrap()).markers;

        // Requeue the long spans that we removed that should stay in place.
        let mut new_long_spans = Vec::with_capacity(old_long_spans.capacity());
        for marker in &old_long_spans {
            if marker.end.should_keep(false) {
                new_long_spans.push(marker.clone());
            }
        }
        if !new_long_spans.is_empty() {
            self.long_spans.lock().unwrap().append(&mut new_long_spans);
        }

        old_long_spans
    }
}

thread_local! {
    static QUEUE: Arc<Queue> = Queue::new();
}