probius 0.0.9

metrics and tracing system that learns the structure of your system
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
use core::{
    cell::{Cell, OnceCell, RefCell, UnsafeCell},
    future::Future,
    ptr::NonNull,
    sync::{
        atomic::{AtomicU64, Ordering},
    },
};
use std::rc::Rc;
use std::sync::Mutex;

use probius_mproto::{GlobalSourceId, MetricAggregate, SourceId};

use crate::{
    component::{self, Component},
    encoding::ProbiusWriter,
    link_vec::{LinkVec, LinkVecPtr},
    void_sink,
};

static NEXT_SOURCE_ID: AtomicU64 = AtomicU64::new(0);
thread_local! {
    static NEXT_EVENT_SEQ: Cell<u16> = Cell::new(0);
    static PROBIUS: OnceCell<Probius> = OnceCell::new();
}

static APP_CONFIG: Mutex<Option<AppConfig>> = Mutex::new(None);

struct AppConfig {
    buffer_headroom: usize,
    buffer_pool: bab::HeapBufferPool,
}

pub fn init(buffer_headroom: usize, buffer_pool: bab::HeapBufferPool) {
    // bab requires that there are at least `num_threads * 3 / 2` batches to avoid any single
    // thread getting starved. In Probius's case, there will be at most 2 threads - the
    // publisher and the flusher (which may or may not run on the same thread).
    //
    // So we need at least 3 batches, but 4 divides better since people tend to supply large
    // even numbers for buffer counts.
    //let batch_count = 4;
    //let buffers_per_batch = (min_buffer_count + batch_count - 1) / batch_count;
    //let buffer_pool = bab::HeapBufferPool::new(buffer_size, batch_count, buffers_per_batch);

    let mut app_config = APP_CONFIG.lock().expect("probius app config lock");
    if app_config.is_some() {
        drop(app_config);
        panic!("probius::init called twice");
    } else {
        *app_config = Some(AppConfig { buffer_headroom, buffer_pool });
    }
}

fn with_probius<R>(f: impl FnOnce(&Probius) -> R) -> R {
    try_with_probius(move |probius| {
        f(probius)
    })
}

fn try_with_probius<R>(f: impl FnOnce(&Probius) -> R) -> R {
    PROBIUS.with(move |probius| {
        let probius = probius.get_or_init(move || {
            let mut maybe_app_config = APP_CONFIG.lock().expect("probius app config lock");
            let app_config = maybe_app_config.get_or_insert_with(|| {
                // Default to the void sink if none was setup by the application.
                let buffer_pool = bab::HeapBufferPool::new(8192, 16, 16);
                void_sink::spawn_void_sink_flusher(buffer_pool.clone());
                AppConfig { buffer_headroom: 0, buffer_pool }
            });

            Probius::new(app_config.buffer_headroom, app_config.buffer_pool.clone())
        });

        f(probius)
    })
}

pub fn flush() -> impl Iterator<Item = bab::BufferPtr> {
    with_probius(|probius| probius.inner.flush())
}

pub fn new_component(name: &str) -> Component {
    try_with_probius(|probius| {
        Component::new(probius.clone(), name, false)
    })
}

pub fn enter_component<R>(name: &str, f: impl FnOnce() -> R) -> R {
    try_with_probius(|probius| {
        Component::new(probius.clone(), name, false).enter(f)
    })
}

pub async fn enter_component_async<F: Future>(name: &str, f: F) -> F::Output {
    let probius = try_with_probius(|probius| probius.clone());

    Component::new(probius, name, false).enter_async(f).await
}

pub fn enter_component_ephemeral<R>(name: &str, f: impl FnOnce() -> R) -> R {
    try_with_probius(|probius| {
        Component::new(probius.clone(), name, false).enter(f)
    })
}

pub async fn enter_component_ephemeral_async<F: Future>(name: &str, f: F) -> F::Output {
    let probius = try_with_probius(|probius| probius.clone());

    Component::new(probius, name, false).enter_async(f).await
}

pub fn new_trace_source(name: &str) -> TraceSource {
    try_with_probius(|probius| {
        TraceSource::new(probius.clone(), name, true)
    })
}

pub fn new_trace_source_ephemeral(name: &str) -> TraceSource {
    try_with_probius(|probius| {
        TraceSource::new(probius.clone(), name, false)
    })
}

#[derive(Clone)]
pub struct Probius {
    inner: Rc<ProbiusWriter>,
}

impl Probius {
    fn new(
        buffer_headroom: usize,
        buffer_pool: bab::HeapBufferPool,
    ) -> Self {
        Self {
            inner: Rc::new(ProbiusWriter::new(buffer_headroom, buffer_pool.clone())),
        }
    }
}

#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct EventSeq(pub u16);

/// An event is identified mainly by its source and its timestamp. An event's seq is not unique and
/// is only used to differentiate multiple events from the same source occurring at the same
/// timestamp.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct EventId {
    pub source: SourceId,
    pub timestamp_nanos: u64,
    pub seq: EventSeq,
}

#[derive(Debug, PartialEq)]
pub enum TraceOp {
    CreateSource { source: SourceId },
    DeleteSource { source: SourceId },
    Call { source: SourceId },
    PushScope,
    PopScope,
    BranchStart,
    BranchEnd,
    Label { label: *const str },
    Tag,
    Metric { name: *const str, value: i64 },

    LocalChannelSend { channel: SourceId, version: u64 },
    LocalChannelReceive { channel: SourceId, version: u64 },
    LocalChannelTransferFrom {
        from: SourceId,
        from_version: u64,
        to: SourceId,
        to_version: u64,
    },

    GlobalChannelSend { channel: GlobalSourceId, version: u64 },
    GlobalChannelReceive { channel: GlobalSourceId, version: u64 },
    GlobalChannelTransferFrom {
        from: GlobalSourceId,
        from_version: u64,
        to: GlobalSourceId,
        to_version: u64,
    },
}

impl TraceOp {
    #[inline]
    fn as_op_aggregate(&self) -> TraceOpAggregate {
        match self {
            TraceOp::CreateSource { .. } => TraceOpAggregate::CreateSource,
            TraceOp::DeleteSource { .. } => TraceOpAggregate::DeleteSource,
            TraceOp::Call { source } => TraceOpAggregate::Call { source: *source },
            TraceOp::PushScope => TraceOpAggregate::PushScope,
            TraceOp::PopScope => TraceOpAggregate::PopScope,
            TraceOp::BranchStart => TraceOpAggregate::BranchStart,
            TraceOp::BranchEnd => TraceOpAggregate::BranchEnd,
            TraceOp::Label { label } => TraceOpAggregate::Label { label: *label }, 
            TraceOp::Tag { .. } => TraceOpAggregate::Tag,
            TraceOp::Metric { name, .. } => TraceOpAggregate::Metric { name: *name },

            TraceOp::LocalChannelSend { channel, .. } =>
                TraceOpAggregate::LocalChannelSend { channel: *channel },
            TraceOp::LocalChannelReceive { channel, .. } =>
                TraceOpAggregate::LocalChannelReceive { channel: *channel },
            TraceOp::LocalChannelTransferFrom { from, to, .. } =>
                TraceOpAggregate::LocalChannelTransferFrom { from: *from, to: *to },

            TraceOp::GlobalChannelSend { channel, .. } =>
                TraceOpAggregate::GlobalChannelSend { channel: *channel },
            TraceOp::GlobalChannelReceive { channel, .. } =>
                TraceOpAggregate::GlobalChannelReceive { channel: *channel },
            TraceOp::GlobalChannelTransferFrom { from, to, .. } =>
                TraceOpAggregate::GlobalChannelTransferFrom {
                    from: *from,
                    to: *to,
                },
        }
    }
}

pub struct Source {
    probius: Probius,

    id: SourceId,
    #[cfg(not(target_arch = "wasm32"))]
    create_time: std::time::Instant,
}

impl Source {
    pub(crate) fn new(probius: Probius, name: &str, is_recurring: bool) -> Self {
        let source = Self {
            probius: probius.clone(),

            id: SourceId { source: NEXT_SOURCE_ID.fetch_add(1, Ordering::Relaxed) },
            #[cfg(not(target_arch = "wasm32"))]
            create_time: std::time::Instant::now(),
        };

        component::with_current(|parent| {
            probius.inner.create_source(
                source.next_event_id(),
                name,
                parent.map(|p| p.id()),
                is_recurring,
            );
        });

        source
    }

    pub fn id(&self) -> SourceId { self.id }

    #[cfg(not(target_arch = "wasm32"))]
    fn now_nanos(&self) -> u64 {
        self.create_time.elapsed().as_nanos() as u64
    }

    #[cfg(target_arch = "wasm32")]
    fn now_nanos(&self) -> u64 {
        // TODO
        0
    }

    fn next_event_id(&self) -> probius_mproto::EventId {
        let seq = NEXT_EVENT_SEQ.get();
        NEXT_EVENT_SEQ.set(seq.wrapping_add(1));
        let timestamp_nanos = self.now_nanos();
        probius_mproto::EventId {
            source: self.id,
            timestamp_nanos,
            seq: probius_mproto::EventSeq { seq },
        }
    }
}

impl Drop for Source {
    fn drop(&mut self) {
        self.probius.inner.delete_source(self.next_event_id());
    }
}

thread_local! {
    static TRACE_STACK: Cell<Option<NonNull<()>>> = Cell::new(None);
}

pub struct TraceSource {
    source: Source,
    trace_aggregator: TraceAggregator,
}

impl TraceSource {
    fn new(probius: Probius, name: &str, is_recurring: bool) -> Self {
        Self {
            source: Source::new(probius, name, is_recurring),
            trace_aggregator: TraceAggregator::new(),
        }
    }

    #[inline]
    pub fn trace<R>(&self, f: impl FnOnce() -> R) -> R {
        let trace = Trace {
            is_detailed_trace: false,
            start_nanos: self.source.now_nanos(),
            trace_source: &self,
            aggregate_cursor: TraceAggregateCursor::start_cursor(),
            encode_cursor: Cell::new(0),
            encode_buf: UnsafeCell::new([0; 512]),
        };
        let parent = TRACE_STACK.replace(Some(NonNull::from(&trace).cast()));

        let result = f();

        TRACE_STACK.set(parent);
        drop(trace);

        result
    }

    #[inline]
    pub async fn trace_future<R>(&self, f: impl core::future::Future<Output = R>) -> R {
        let trace = Trace {
            is_detailed_trace: false,
            start_nanos: self.source.now_nanos(),
            trace_source: self,
            aggregate_cursor: TraceAggregateCursor::start_cursor(),
            encode_cursor: Cell::new(0),
            encode_buf: UnsafeCell::new([0; 512]),
        };

        let mut f = core::pin::pin!(f);
        let result = core::future::poll_fn(|cx| {
            let parent = TRACE_STACK.replace(Some(NonNull::from(&trace).cast()));
            let result = f.as_mut().poll(cx);
            TRACE_STACK.set(parent);
            result
        })
        .await;

        drop(trace);

        result
    }

    pub fn flush_aggregate_full(&self) {
        self.trace_aggregator.flush_full(&self.source);
    }
}

#[inline]
fn with_current_trace(f: impl FnOnce(&Trace)) {
    if let Some(trace_ptr) = TRACE_STACK.get() {
        let trace: &Trace = unsafe { trace_ptr.cast().as_ref() };
        f(trace);
    }
}

/*pub fn trace_create_source(name: &str) -> Source {
    let source = Source::new(name);
    with_current_trace(|trace| {
        trace.push_op(TraceOp::CreateSource { source: source.id });
    });
    source
}*/

#[inline]
pub fn trace_metric(name: &'static str, value: i64) {
    with_current_trace(|trace| trace.metric(name, value));
}

#[inline]
pub fn trace_label(label: &'static str) {
    with_current_trace(|trace| trace.label(label));
}

#[inline]
pub fn trace_branch<R>(f: impl FnOnce() -> R) -> R {
    if let Some(trace_ptr) = TRACE_STACK.get() {
        let trace: &Trace = unsafe { trace_ptr.cast().as_ref() };

        trace.branch_start();
        let result = f();
        trace.branch_end();
        result
    } else {
        f()
    }
}

#[inline]
pub fn trace_branch_start() {
    with_current_trace(|trace| trace.branch_start());
}

#[inline]
pub fn trace_branch_end() {
    with_current_trace(|trace| trace.branch_end());
}

pub struct Trace<'a> {
    is_detailed_trace: bool,
    start_nanos: u64,
    trace_source: &'a TraceSource,
    aggregate_cursor: TraceAggregateCursor,
    encode_cursor: Cell<usize>,
    encode_buf: UnsafeCell<[u8; 512]>,
}

impl Trace<'_> {
    /*fn create_source(&self, probius: Probius, name: &'static str, is_recurring: bool) -> Source {
        let source = Source::new(probius, name, is_recurring);
        self.push_op(TraceOp::CreateSource { source: source.id });
        source
    }*/

    #[inline]
    fn metric(&self, name: &'static str, value: i64) {
        self.push_op(TraceOp::Metric { name, value });
    }

    #[inline]
    fn label(&self, label: &'static str) {
        self.push_op(TraceOp::Label { label });
    }

    #[inline]
    fn branch_start(&self) {
        self.push_op(TraceOp::BranchStart);
    }

    #[inline]
    fn branch_end(&self) {
        self.push_op(TraceOp::BranchEnd);
    }

    #[inline]
    fn push_op(&self, op: TraceOp) {
        let op_node_index = self.trace_source.trace_aggregator.ingest(&self.aggregate_cursor, &op);

        if self.is_detailed_trace {
            if let Err(()) = self.try_write_op(op_node_index, op) {
                // TODO mark trace buffer as invalid
            }
        }
    }

    fn try_write_op(&self, op_node_index: u16, op: TraceOp) -> Result<(), ()> {
        self.try_write_mproto(op_node_index)?;
        match op {
            TraceOp::CreateSource { source } => {
                self.try_write_mproto(source)?;
            }
            TraceOp::DeleteSource { source } => {
                self.try_write_mproto(source)?;
            }
            TraceOp::Call { .. } => {
                // TODO can we identify the specific child trace here?
            }
            TraceOp::PushScope => { }
            TraceOp::PopScope => { }
            TraceOp::BranchStart => { }
            TraceOp::BranchEnd => { }
            TraceOp::Label { .. } => { }
            TraceOp::Tag => {
                // TODO
            }
            TraceOp::Metric { value, .. } => {
                self.try_write_mproto(value)?;
            }

            TraceOp::LocalChannelSend { .. } => {
            }
            TraceOp::LocalChannelReceive { .. } => {
            }
            TraceOp::LocalChannelTransferFrom { .. } => {
            }

            TraceOp::GlobalChannelSend { .. } => {
            }
            TraceOp::GlobalChannelReceive { .. } => {
            }
            TraceOp::GlobalChannelTransferFrom { .. } => {
            }
        }

        Ok(())
    }

    #[inline]
    fn try_write_mproto(&self, value: impl mproto::Encode) -> Result<(), ()> {
        let encoded_len = mproto::encoded_len(&value);
        let write_buf = self.try_write(encoded_len)?;
        mproto::encode_value(value, write_buf);
        Ok(())
    }

    #[inline]
    fn try_write(&self, len: usize) -> Result<&mut [u8], ()> {
        let start = self.encode_cursor.get();
        if start + len > 512 {
            return Err(());
        }
        self.encode_cursor.set(start + len);

        let slice_ptr = self.encode_buf.get() as *mut u8;
        Ok(unsafe { core::slice::from_raw_parts_mut(slice_ptr.add(start), len) })
    }
}

impl Drop for Trace<'_> {
    fn drop(&mut self) {
        if self.is_detailed_trace {
            let encode_buf = unsafe { &*self.encode_buf.get() };
            self.trace_source.source.probius.inner.trace(
                self.trace_source.source.next_event_id(),
                self.start_nanos,
                &encode_buf[..self.encode_cursor.get()],
            );
        }
    }
}

#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub enum TraceOpAggregate {
    CreateSource,
    DeleteSource,
    Call { source: SourceId },
    PushScope,
    PopScope,
    BranchStart,
    BranchEnd,
    Label { label: *const str },
    Tag,
    Metric { name: *const str },

    LocalChannelSend { channel: SourceId },
    LocalChannelReceive { channel: SourceId },
    LocalChannelTransferFrom {
        from: SourceId,
        to: SourceId,
    },

    GlobalChannelSend { channel: GlobalSourceId },
    GlobalChannelReceive { channel: GlobalSourceId },
    GlobalChannelTransferFrom {
        from: GlobalSourceId,
        to: GlobalSourceId,
    },
}

#[derive(Copy, Clone, Debug)]
pub enum TraceAggregateNodeData {
    CreateSource,
    DeleteSource,
    Call { source: SourceId },
    PushScope,
    PopScope,
    BranchStart {
        branch_end: TraceAggregateNodePtr,
    },
    BranchEnd {
        parent_branch_end: Option<TraceAggregateNodePtr>,
    },
    Label { label: &'static str },
    Tag,
    Metric {
        name: &'static str,
        index: u16,
    },

    LocalChannelSend { channel: SourceId },
    LocalChannelReceive { channel: SourceId },
    LocalChannelTransferFrom {
        from: SourceId,
        to: SourceId,
    },

    GlobalChannelSend { channel: GlobalSourceId },
    GlobalChannelReceive { channel: GlobalSourceId },
    GlobalChannelTransferFrom {
        from: GlobalSourceId,
        to: GlobalSourceId,
    },
}

impl TraceAggregateNodeData {
    #[inline]
    fn as_op_aggregate(&self) -> TraceOpAggregate {
        match self {
            TraceAggregateNodeData::CreateSource => TraceOpAggregate::CreateSource,
            TraceAggregateNodeData::DeleteSource => TraceOpAggregate::DeleteSource,
            TraceAggregateNodeData::Call { source } => TraceOpAggregate::Call { source: *source },
            TraceAggregateNodeData::PushScope => TraceOpAggregate::PushScope,
            TraceAggregateNodeData::PopScope => TraceOpAggregate::PopScope,
            TraceAggregateNodeData::BranchStart { .. } => TraceOpAggregate::BranchStart,
            TraceAggregateNodeData::BranchEnd { .. } => TraceOpAggregate::BranchEnd,
            TraceAggregateNodeData::Label { label } => TraceOpAggregate::Label { label: *label }, 
            TraceAggregateNodeData::Tag => TraceOpAggregate::Tag,
            TraceAggregateNodeData::Metric { name, .. } => {
                TraceOpAggregate::Metric { name: *name }
            }

            TraceAggregateNodeData::LocalChannelSend { channel } =>
                TraceOpAggregate::LocalChannelSend { channel: *channel },
            TraceAggregateNodeData::LocalChannelReceive { channel } =>
                TraceOpAggregate::LocalChannelReceive { channel: *channel },
            TraceAggregateNodeData::LocalChannelTransferFrom { from, to } =>
                TraceOpAggregate::LocalChannelTransferFrom { from: *from, to: *to },

            TraceAggregateNodeData::GlobalChannelSend { channel } =>
                TraceOpAggregate::GlobalChannelSend { channel: *channel },
            TraceAggregateNodeData::GlobalChannelReceive { channel } =>
                TraceOpAggregate::GlobalChannelReceive { channel: *channel },
            TraceAggregateNodeData::GlobalChannelTransferFrom { from, to } =>
                TraceOpAggregate::GlobalChannelTransferFrom {
                    from: *from,
                    to: *to,
                },
        }
    }

    fn as_mproto(&self)
        -> impl mproto::Encode + mproto::Compatible<probius_mproto::TraceOpAggregate>
    {
        match *self {
            TraceAggregateNodeData::CreateSource =>
                probius_mproto::TraceOpAggregate::CreateSource,
            TraceAggregateNodeData::DeleteSource =>
                probius_mproto::TraceOpAggregate::DeleteSource,
            TraceAggregateNodeData::Call { source } =>
                probius_mproto::TraceOpAggregate::Call { source },
            TraceAggregateNodeData::PushScope => probius_mproto::TraceOpAggregate::PushScope,
            TraceAggregateNodeData::PopScope => probius_mproto::TraceOpAggregate::PopScope,
            TraceAggregateNodeData::BranchStart { branch_end } =>
                probius_mproto::TraceOpAggregate::BranchStart {
                    branch_end: branch_end.index,
                },
            TraceAggregateNodeData::BranchEnd { parent_branch_end } =>
                probius_mproto::TraceOpAggregate::BranchEnd {
                    parent_branch_end: parent_branch_end.map(|n| n.index).unwrap_or(u16::MAX),
                },
            TraceAggregateNodeData::Label { label } =>
                probius_mproto::TraceOpAggregate::Label { label: label.into() },
            TraceAggregateNodeData::Tag => probius_mproto::TraceOpAggregate::Tag,
            TraceAggregateNodeData::Metric { name, index } =>
                probius_mproto::TraceOpAggregate::Metric {
                    name: name.into(),
                    index,
                },

            TraceAggregateNodeData::LocalChannelSend { channel } =>
                probius_mproto::TraceOpAggregate::ChannelSend { channel },
            TraceAggregateNodeData::LocalChannelReceive { channel } =>
                probius_mproto::TraceOpAggregate::ChannelReceive { channel },
            TraceAggregateNodeData::LocalChannelTransferFrom { from, to } =>
                probius_mproto::TraceOpAggregate::ChannelTransfer { from, to },

            TraceAggregateNodeData::GlobalChannelSend { channel } =>
                probius_mproto::TraceOpAggregate::GlobalChannelSend { channel },
            TraceAggregateNodeData::GlobalChannelReceive { channel } =>
                probius_mproto::TraceOpAggregate::GlobalChannelReceive { channel },
            TraceAggregateNodeData::GlobalChannelTransferFrom { from, to } =>
                probius_mproto::TraceOpAggregate::GlobalChannelTransfer { from, to },
        }
    }
}

#[derive(Debug)]
pub struct TraceAggregateNode {
    op: TraceAggregateNodeData,
    branch_sibling: OnceCell<TraceAggregateBranch>,
    next: OnceCell<TraceAggregateNodePtr>,
    index: u16,
}

#[derive(Debug)]
pub struct TraceAggregateBranch {
    next: TraceAggregateNodePtr,
}

pub struct TraceAggregateCursor {
    node: Cell<Option<TraceAggregateNodePtr>>,
    branch_end: Cell<Option<TraceAggregateNodePtr>>,
}

impl TraceAggregateCursor {
    #[inline]
    pub fn start_cursor() -> Self {
        Self {
            node: Cell::new(None),
            branch_end: Cell::new(None),
        }
    }
}

type TraceAggregateNodePtr = LinkVecPtr<TraceAggregateNode>;

/// Aggregator for traces from a single TraceSource
pub struct TraceAggregator {
    start_node: OnceCell<TraceAggregateNodePtr>,
    metrics: RefCell<Vec<MetricAggregate>>,
    nodes: LinkVec<TraceAggregateNode>,
}

impl TraceAggregator {
    fn new() -> Self {
        Self {
            start_node: OnceCell::new(),
            metrics: RefCell::new(Vec::new()),
            nodes: LinkVec::leak(),
        }
    }

    #[inline]
    fn ingest(&self, cursor: &TraceAggregateCursor, op: &TraceOp) -> u16 {
        let mut node = if let Some(n) = cursor.node.take() {
            match n.op {
                TraceAggregateNodeData::BranchStart { branch_end }
                    if matches!(op, TraceOp::BranchEnd)
                => {
                    // Handle empty branch
                    let TraceAggregateNodeData::BranchEnd { parent_branch_end } = branch_end.op
                    else {
                        panic!("expected branch end");
                    };
                    cursor.branch_end.set(parent_branch_end);
                    cursor.node.set(Some(branch_end));
                    return branch_end.index;
                }
                _ => {
                    *n.next.get_or_init(|| self.new_node(cursor, op))
                }
            }
        } else {
            *self.start_node
                // Initialize the start node if this is the first ever operation for this trace.
                .get_or_init(|| self.new_node(cursor, op))
        };

        let op_aggregate = op.as_op_aggregate();
        while node.op.as_op_aggregate() != op_aggregate {
            node = node.branch_sibling
                .get_or_init(|| {
                    // New branch
                    TraceAggregateBranch {
                        next: self.new_node(cursor, op),
                    }
                })
                .next;
        }

        match &node.op {
            TraceAggregateNodeData::BranchStart { branch_end } => {
                cursor.branch_end.set(Some(*branch_end));
            }
            TraceAggregateNodeData::BranchEnd { parent_branch_end } => {
                cursor.branch_end.set(*parent_branch_end);
            }
            TraceAggregateNodeData::Metric { index, .. } => {
                if let TraceOp::Metric { value, .. } = op {
                    if let Some(metric_aggregate) =
                        self.metrics.borrow_mut().get_mut(*index as usize)
                    {
                        metric_aggregate.count += 1;
                        metric_aggregate.sum += *value;
                        metric_aggregate.min = core::cmp::min(metric_aggregate.min, *value);
                        metric_aggregate.max = core::cmp::max(metric_aggregate.max, *value);
                    }
                }
            }
            _ => { }
        }

        cursor.node.set(Some(node));

        node.index
    }

    fn new_metric(&self) -> u16 {
        let mut metrics = self.metrics.borrow_mut();
        let index = metrics.len() as u16;
        metrics.push(MetricAggregate {
            count: 0,
            sum: 0,
            min: i64::MAX,
            max: i64::MIN,
        });
        index
    }

    #[inline]
    fn new_node(&self, cursor: &TraceAggregateCursor, op: &TraceOp) -> TraceAggregateNodePtr {
        let node_data = match op {
            TraceOp::CreateSource { .. } => TraceAggregateNodeData::CreateSource,
            TraceOp::DeleteSource { .. } => TraceAggregateNodeData::DeleteSource,
            TraceOp::Call { source } => TraceAggregateNodeData::Call { source: *source },
            TraceOp::PushScope => TraceAggregateNodeData::PushScope,
            TraceOp::PopScope => TraceAggregateNodeData::PopScope,
            TraceOp::BranchStart => {
                // Create both the branch start and a single branch end that all branches will
                // eventually flow into.
                let branch_end = self.nodes.push(TraceAggregateNode {
                    op: TraceAggregateNodeData::BranchEnd {
                        parent_branch_end: cursor.branch_end.take(),
                    },
                    branch_sibling: OnceCell::new(),
                    next: OnceCell::new(),
                    index: self.nodes.len() as u16,
                });
                TraceAggregateNodeData::BranchStart {
                    branch_end,
                }
            }
            TraceOp::BranchEnd => {
                // Use the existing branch end node that was created at this branch's start.
                let node = cursor.branch_end.take()
                    .expect("new probius trace aggregate branch end node");
                return node;
            }
            TraceOp::Label { label } => TraceAggregateNodeData::Label { label: unsafe { &**label } }, 
            TraceOp::Tag { .. } => TraceAggregateNodeData::Tag,
            TraceOp::Metric { name, .. } => {
                let index = self.new_metric();
                TraceAggregateNodeData::Metric { name: unsafe { &**name }, index }
            }

            TraceOp::LocalChannelSend { channel, .. } =>
                TraceAggregateNodeData::LocalChannelSend { channel: *channel },
            TraceOp::LocalChannelReceive { channel, .. } =>
                TraceAggregateNodeData::LocalChannelReceive { channel: *channel },
            TraceOp::LocalChannelTransferFrom { from, to, .. } =>
                TraceAggregateNodeData::LocalChannelTransferFrom { from: *from, to: *to },

            TraceOp::GlobalChannelSend { channel, .. } =>
                TraceAggregateNodeData::GlobalChannelSend { channel: *channel },
            TraceOp::GlobalChannelReceive { channel, .. } =>
                TraceAggregateNodeData::GlobalChannelReceive { channel: *channel },
            TraceOp::GlobalChannelTransferFrom { from, to, .. } =>
                TraceAggregateNodeData::GlobalChannelTransferFrom {
                    from: *from,
                    to: *to,
                },
        };

        self.nodes.push(TraceAggregateNode {
            op: node_data,
            branch_sibling: OnceCell::new(),
            next: OnceCell::new(),
            index: self.nodes.len() as u16,
        })
    }

    fn flush_full(&self, source: &Source) {
        let mut metrics = self.metrics.borrow_mut();

        source.probius.inner.trace_aggregate(
            source.next_event_id(),
            source.now_nanos(), // TODO this should be the previous flush time, not now
            &[],
            &metrics[..],
            self.nodes.iter().map(|n| {
                probius_mproto::TraceAggregateNodeGen {
                    op: n.op.as_mproto(),
                    branch_next: n.branch_sibling.get().map(|bn| bn.next.index),
                    next: n.next.get().map(|n| n.index)
                }
            }),
        );

        for node in self.nodes.iter() {
            match node.op {
                TraceAggregateNodeData::Metric { index, .. } => {
                    let index = index as usize;
                    metrics[index].count = 0;
                    metrics[index].sum = 0;
                    metrics[index].min = i64::MAX;
                    metrics[index].max = i64::MIN;
                }
                _ => {}
            }
        }
    }

    #[cfg(test)]
    fn print(&self) {
        enum Traverse {
            Node(TraceAggregateNodePtr),
            Depth(usize),
        }

        let mut branch_barriers = std::collections::HashMap::new();
        let mut next_branch_end = None;

        let indent = "  ".to_string();
        let mut depth = 0;
        let mut visit_stack = Vec::new();
        if let Some(node) = self.start_node.get().cloned() {
            visit_stack.push(Traverse::Node(node));
        }

        while let Some(traverse) = visit_stack.pop() {
            match traverse {
                Traverse::Depth(new_depth) => {
                    println!("{}-", indent.repeat(depth));
                    depth = new_depth;
                }
                Traverse::Node(node) => {
                    if let Some(sibling) = node.branch_sibling.get().map(|n| n.next.clone()) {
                        if let Some(branch_end) = next_branch_end {
                            let pending_branches = branch_barriers.get_mut(&branch_end)
                                .unwrap();
                            *pending_branches += 1;
                        }
                        visit_stack.push(Traverse::Node(sibling.clone()));
                        visit_stack.push(Traverse::Depth(depth));
                    }

                    match &node.op {
                        TraceAggregateNodeData::BranchStart { branch_end } => {
                            next_branch_end = Some(branch_end.clone());
                            branch_barriers.entry(*branch_end).or_insert(1);

                            println!(
                                "{}{:?} {:?}",
                                indent.repeat(depth),
                                node,
                                node.op.as_op_aggregate(),
                            );

                            depth += 1;
                        }
                        TraceAggregateNodeData::BranchEnd { parent_branch_end } => {
                            let branch_end = next_branch_end.clone()
                                .expect("unexpected branch end");

                            let pending_branches = branch_barriers.get_mut(&branch_end)
                                .unwrap();
                            *pending_branches -= 1;
                            if *pending_branches > 0 {
                                continue;
                            }

                            next_branch_end = parent_branch_end.clone();

                            depth -= 1;

                            println!(
                                "{}{:?} {:?}",
                                indent.repeat(depth),
                                node,
                                node.op.as_op_aggregate(),
                            );
                        }
                        _ => {
                            println!(
                                "{}{:?} {:?}",
                                indent.repeat(depth),
                                node,
                                node.op,
                            );
                        }
                    }

                    if let Some(next) = node.next.get().cloned() {
                        visit_stack.push(Traverse::Node(next));
                    }
                }
            }
        }
    }
}

impl Drop for TraceAggregator {
    fn drop(&mut self) {
        unsafe { self.nodes.unleak(); }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_trace_aggregation() {
        let buffer_pool = bab::HeapBufferPool::new(8192, 4, 16);
        init(10, buffer_pool);

        enter_component("test-component", || {
            let tracer = new_trace_source("test-tracer");
            for i in 0..10 {
                tracer.trace(|| {
                    trace_metric("start", 1);
                    trace_branch(|| {
                        if i % 3 == 0 {
                            trace_metric("even", 1);
                            trace_branch(|| {
                                if i % 4 == 0 {
                                    trace_metric("% 4 = 0", 1);
                                } else if i % 4 == 1 {
                                    trace_metric("% 4 = 1", 1);
                                    trace_metric("hehe", 1);
                                } else if i % 4 == 2 {
                                    trace_metric("% 4 = 2", 1);
                                    trace_metric("haha", 1);
                                } else {
                                    trace_metric("% 4 = 3", 1);
                                }
                            });
                            trace_metric("4", 1);
                        } else {
                            trace_metric("odd", 1);
                        }
                    });
                    trace_metric("oddeven", 1);
                });
            }

            tracer.trace_aggregator.print();

            tracer.flush_aggregate_full();
        });

        for flushed_buffer in flush() {
            let len = bab::WriterFlushSender::get_complete_buffer_len(flushed_buffer) as usize;
            println!("Flushed: {:?}", unsafe { flushed_buffer.slice(0..len) });
        }
    }
}