micrometer 0.2.7

Profiling for fast, high frequency events in multithreaded applications with low overhead
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
/*!
# Micrometer
Profiling for fast, high frequency events in multithreaded applications with low overhead

### Important

By default every measure is a no-op, to measure and consume measures, enable the `enable`
feature. This is done to allow libs to instrument their code without runitme costs if
no one is using the measures.

## Definitions

+ *Measurement*: a measurement is the timing data gathered for a single event.
+ *Record*: a record is the retrieval format for measurements. Records may be 1-to-1 with
respects to measurements, however, if *perforation* is enabled (default), as measurements
increase in numbers, a single record may be the aggregate of multiple measurements.
+ *Location*: a location is a region of interest of the program. All measurements start
from a location. Each measure is associated with the name of the location it originated
from.
+ *Thread location*: as `micrometer` is intended for multithreaded environments, each
*location* may be relevant for multiple threads, the location in the context of a specific
thread is referred to as *thread location*
+ `Span`: a *span* is a guard object that measures the time between when it was created
and when it was dropped (unless it was disarmed). Each span will produce one measurement
+ `Track`: a *track* is the struct responsible of handling all *measurements* originating
from a *thread location*. It is used to create spans or manually record the duration of an
event.
+ `TrackPoint`: while a *track* maps to the concept of *thread location* (as a single track
can only be owned by one thread), a *track point* maps to the concept of *location*. The
track point struct is a lazily allocated, thread local handle to multiple tracks that are
associated with the same name. Usually, track points are used as static variables, using
the `get_or_init` method to get the *track* for the active thread.

## Examples

#### Measuring the duration of a loop

```
for _ in 0..100 {
    // Define a `TrackPoint` named "loop_duration", get (or init) the `Track` for the
    // current thread, then create a span and assign it to a new local variable called
    // `loop_duration`
    micrometer::span!(loop_duration);

    std::hint::black_box("do something");
    // `loop_duration` is automatically dropped recording the measure
}
// Print a summary of the measurements
micrometer::summary();
```

#### Measuring the duration of a loop, threaded

```
std::thread::scope(|s| {
    for t in 1..=4 {
        s.spawn(move || {
            for _ in 0..(10 * t) {
                micrometer::span!(); // Name automatically assigned to source file and line
                std::hint::black_box("do something");
            }
        });
    }
});
// Print a summary of the measurements
micrometer::summary();
// Print a summary of the measurements, aggregating measures for the same location
micrometer::summary_grouped();
```
### Measuring the duration of an expression

```
// This works like the `dbg!` macro, allowing you to transparently wrap an expression:
// a span is created, the expression is executed, then the span is closed and the result
// of the expression is passed along
let a = micrometer::span!(5 * 5, "fives_sq");
let b = micrometer::span!(a * a); // Name automatically assigned to source file and line
assert_eq!(a, 25);
assert_eq!(b, 25 * 25);
```

### Measuring a code segment

```
let a = 5;
micrometer::span!(guard, "a_sq");
let b = a * a;
drop(guard); // Measurement stops here
let c = b * a;
```

*/

use once_cell::sync::Lazy;
use parking_lot::Mutex;
use std::{
    collections::HashMap,
    ops::AddAssign,
    path::Path,
    sync::Arc,
    time::{Duration, Instant},
};
use thread_local::ThreadLocal;

/// Minimum size fo the buffer in which measurements are stored
pub const BUFFER_INIT_BYTES: usize = 8 << 10; // 8kiB

/// When using perforation (default) consequent measures will be combined
/// if the number of measures for a track point exceeds `PERFORATION_STEP`
///
/// The first `PERFORATION_STEP` measures will be saved as is, after that,
/// the following `PERFORATION_STEP` measures will be composed of 2 measures
/// each, the next will be 4 each and so on
#[cfg(feature = "perforation")]
pub const PERFORATION_STEP: usize = {
    #[cfg(feature = "perforation-128")]
    {
        128
    }
    #[cfg(feature = "perforation-1k")]
    {
        1 << 10
    }
    #[cfg(feature = "perforation-16k")]
    {
        16 << 10
    }
};

static GLOBAL_REGISTRY: Lazy<Registry> = Lazy::new(Registry::default);

/// Get a reference to the global micrometer register
#[inline]
pub fn global() -> &'static Registry {
    &GLOBAL_REGISTRY
}

#[cfg(not(feature = "enable"))]
pub fn summary_grouped() {
    eprintln!("micrometer disabled, add 'enable' feature to gather statistics.");
}

#[cfg(feature = "enable")]
/// Print a summary for all track points. Group all measurements with the
/// same name.
pub fn summary_grouped() {
    let mut v: Vec<_> = global().stats_grouped().into_iter().collect();
    v.sort_unstable_by_key(|(name, _)| name.clone());
    v.into_iter().for_each(|(s, q)| {
        eprintln!(
            "{s:36}: {mean:12?}({std:12?}) {tot:12?} [{n:6}]({copies:2})",
            mean = q.mean,
            std = Duration::from_secs_f64(q.var.sqrt()),
            tot = q.sum,
            n = q.count,
            copies = q.copies,
        )
    })
}

#[cfg(not(feature = "enable"))]
pub fn summary() {
    eprintln!("micrometer disabled, add 'enable' feature to gather statistics.");
}

#[cfg(feature = "enable")]
/// Print a summary for all track points. Report measures with the same name,
/// but different thread separately
pub fn summary() {
    let mut v: Vec<_> = global().stats().into_iter().collect();
    v.sort_by_key(|(name, _)| name.clone());
    v.into_iter().for_each(|(s, q)| {
        eprintln!(
            "{s:36}: {mean:12?}({std:12?}) {tot:12?} [{n:6}]({copies:2})",
            mean = q.mean,
            std = Duration::from_secs_f64(q.var.sqrt()),
            tot = q.sum,
            n = q.count,
            copies = q.copies,
        )
    })
}

#[cfg(not(feature = "enable"))]
pub fn save_csv(path: impl AsRef<Path>) -> std::io::Result<()> {
    let _ = path;
    eprintln!("micrometer disabled, add 'enable' feature to gather statistics.");
    Ok(())
}

#[cfg(feature = "enable")]
/// Save all measurements to a csv file
pub fn save_csv(path: impl AsRef<Path>) -> std::io::Result<()> {
    use std::fs::File;
    use std::io::{BufWriter, Write};

    let mut f = File::create(path)?;
    let mut w = BufWriter::new(&mut f);
    let mut threads: HashMap<_, usize> = HashMap::new();

    let mut data = global().drain_raw();
    data.sort_by(|a, b| a.0.cmp(&b.0));

    writeln!(
        &mut w,
        "\"index\",\"name\",\"thread\",\"count\",\"time\",\"end\""
    )?;

    for (name, records) in data {
        let t = threads.entry(name.clone()).or_default();
        let thread = *t;
        *t += 1;
        let mut idx = 0;
        for Record { ns, count, end } in records {
            if !count.is_power_of_two() {
                continue;
            }
            let duration = Duration::from_nanos(ns) / count;
            let end = Duration::from_nanos(end).as_secs_f64();
            let seconds = duration.as_secs_f64();
            writeln!(
                &mut w,
                "\"{idx}\",\"{name}\",\"{thread}\",\"{count}\",\"{seconds:e}\",\"{end:e}\""
            )?;
            idx += count as usize;
        }
    }

    Ok(())
}

#[cfg(not(feature = "enable"))]
#[allow(unused_variables)]
pub fn append_csv(path: impl AsRef<Path>, experiment: &str) -> std::io::Result<()> {
    eprintln!("micrometer disabled, add 'enable' feature to gather statistics.");
    Ok(())
}

#[cfg(feature = "enable")]
/// Append all measurements to a csv file
pub fn append_csv(path: impl AsRef<Path>, experiment: &str) -> std::io::Result<()> {
    use std::fs::File;
    use std::io::{BufWriter, Write};

    let mut f = File::options().append(true).create(true).open(path)?;
    if f.metadata()?.len() == 0 {
        writeln!(
            &mut f,
            "\"index\",\"experiment\",\"name\",\"thread\",\"count\",\"time\",\"end\""
        )?;
    }
    let mut w = BufWriter::new(&mut f);
    let mut threads: HashMap<_, usize> = HashMap::new();

    let mut data = global().drain_raw();
    data.sort_by(|a, b| a.0.cmp(&b.0));

    for (name, records) in data {
        let t = threads.entry(name.clone()).or_default();
        let thread = *t;
        *t += 1;
        let mut idx = 0;
        for Record { ns, count, end } in records {
            if !count.is_power_of_two() {
                continue;
            }
            let duration = Duration::from_nanos(ns) / count;
            let end = Duration::from_nanos(end).as_secs_f64();
            let seconds = duration.as_secs_f64();
            writeln!(
                &mut w,
                "\"{idx}\",\"{experiment}\",\"{name}\",\"{thread}\",\"{count}\",\"{seconds:e}\",\"{end:e}\""
            )?;
            idx += count as usize;
        }
    }

    Ok(())
}

#[cfg(not(feature = "enable"))]
pub fn save_csv_uniform(path: impl AsRef<Path>) -> std::io::Result<()> {
    let _ = path;
    eprintln!("micrometer disabled, add 'enable' feature to gather statistics.");
    Ok(())
}

#[cfg(feature = "enable")]
/// Save all measurements to a csv file. Measures will be uniformed in count
/// to the largest granularity that has been measured for each location.
pub fn save_csv_uniform(path: impl AsRef<Path>) -> std::io::Result<()> {
    use std::fs::File;
    use std::io::{BufWriter, Write};

    let mut f = File::create(path)?;
    let mut w = BufWriter::new(&mut f);
    let mut threads: HashMap<_, usize> = HashMap::new();

    let mut data = global().drain_raw();
    data.sort_by(|a, b| a.0.cmp(&b.0));

    writeln!(&mut w, "\"index\",\"name\",\"thread\",\"count\",\"time\"")?;

    for (name, records) in data {
        if records.is_empty() {
            continue;
        }
        let t = threads.entry(name.clone()).or_default();
        let thread = *t;
        *t += 1;

        let width = records
            .get(records.len().saturating_sub(2))
            .expect("records.is_empty() checked early, will always have at least 1 element")
            .count;

        let mut r = Record::default();
        let mut rev_vec = Vec::new();
        for i in (0..records.len()).rev() {
            if i == records.len() - 1 && records[i].count != width {
                continue;
            }

            r += records[i];

            if r.count == width {
                rev_vec.push(r);
                r = Default::default();
            } else if r.count > width {
                eprintln!(
                    "WARN: micrometer illegal state {} ({}) {:+}. Resetting current",
                    r.count,
                    width,
                    r.count - width
                );
                r = Default::default();
            }
        }
        for (i, r) in rev_vec.into_iter().rev().enumerate() {
            let count = r.count;
            let duration = Duration::from_nanos(r.ns) / count;
            let seconds = duration.as_secs_f64();
            let index = i * width as usize;
            writeln!(
                &mut w,
                "\"{index}\",\"{name}\",\"{thread}\",\"{count}\",\"{seconds:e}\""
            )?;
        }
    }

    Ok(())
}

#[cfg(not(feature = "enable"))]
pub fn append_csv_uniform(path: impl AsRef<Path>, experiment: &str) -> std::io::Result<()> {
    let _ = path;
    let _ = experiment;
    eprintln!("micrometer disabled, add 'enable' feature to gather statistics.");
    Ok(())
}

#[cfg(feature = "enable")]
/// Save all measurements to a csv file. Measures will be uniformed in count
/// to the largest granularity that has been measured for each location.
pub fn append_csv_uniform(path: impl AsRef<Path>, experiment: &str) -> std::io::Result<()> {
    use std::fs::File;
    use std::io::{BufWriter, Write};

    let mut f = File::options().append(true).create(true).open(path)?;
    if f.metadata()?.len() == 0 {
        writeln!(
            &mut f,
            "\"index\",\"experiment\",\"name\",\"thread\",\"count\",\"time\",\"end\""
        )?;
    }
    let mut w = BufWriter::new(&mut f);
    let mut threads: HashMap<_, usize> = HashMap::new();

    let mut data = global().drain_raw();
    data.sort_by(|a, b| a.0.cmp(&b.0));

    for (name, records) in data {
        if records.is_empty() {
            continue;
        }
        let t = threads.entry(name.clone()).or_default();
        let thread = *t;
        *t += 1;

        let width = records
            .get(records.len().saturating_sub(2))
            .expect("records.is_empty() checked early, will always have at least 1 element")
            .count;

        let mut r = Record::default();
        let mut rev_vec = Vec::new();
        for i in (0..records.len()).rev() {
            if i == records.len() - 1 && records[i].count != width {
                continue;
            }

            r += records[i];

            if r.count == width {
                rev_vec.push(r);
                r = Default::default();
            } else if r.count > width {
                eprintln!(
                    "WARN: micrometer illegal state {} ({}) {:+}. Resetting current",
                    r.count,
                    width,
                    r.count - width
                );
                r = Default::default();
            }
        }
        for (i, r) in rev_vec.into_iter().rev().enumerate() {
            let count = r.count;
            let duration = Duration::from_nanos(r.ns) / count;
            let seconds = duration.as_secs_f64();
            let end = Duration::from_nanos(r.end).as_secs_f64();
            let index = i * width as usize;
            writeln!(
                &mut w,
                "\"{index}\",\"{experiment}\",\"{name}\",\"{thread}\",\"{count}\",\"{seconds:e}\",\"{end:e}\""
            )?;
        }
    }

    Ok(())
}

/// Measurement registry, used to register new track points and collect results.
#[derive(Default)]
pub struct Registry {
    trackers: Mutex<Vec<Track>>,
}

impl Registry {
    /// Register a new track
    #[inline]
    pub fn register(&self, t: &Track) {
        self.trackers.lock().push(t.clone());
    }

    /// Delete all measurements for all track points
    pub fn clear(&self) {
        self.trackers.lock().iter().for_each(|t| {
            let mut g = t.buf.0.lock();
            g.consolidate();
            g.vec.drain(..);
        });
    }

    /// Get all the measurements for all tracks leaving them empty.
    pub fn drain_raw(&self) -> Vec<(String, Vec<Record>)> {
        self.trackers
            .lock()
            .iter()
            .map(|t| {
                let mut g = t.buf.0.lock();
                g.consolidate();
                (t.name.into(), std::mem::take(&mut g.vec))
            })
            .collect()
    }

    /// Get all the measurements for all tracks leaving them empty.
    /// Merges tracks with the same name
    pub fn drain_raw_merged(&self) -> HashMap<String, Vec<Record>> {
        self.trackers
            .lock()
            .iter()
            .map(|t| (t.name.into(), t.buf.0.lock()))
            .fold(HashMap::new(), |mut h, (name, mut g)| {
                g.consolidate();
                h.entry(name)
                    .or_default()
                    .append(&mut std::mem::take(&mut g.vec));
                h
            })
    }

    /// Get basic statistics about all tracks. Groups tracks with
    /// the same name
    pub fn stats_grouped(&self) -> HashMap<String, Stats> {
        #[derive(Default, Debug)]
        struct Part {
            copies: usize,
            count: usize,
            sum: Duration,
            sqsum: f64,
        }

        impl AddAssign<Part> for Part {
            fn add_assign(&mut self, rhs: Part) {
                self.copies += rhs.copies;
                self.count += rhs.count;
                self.sum += rhs.sum;
                self.sqsum += rhs.sqsum;
            }
        }

        let lock = self.trackers.lock();
        let mut locked_trackers = lock
            .iter()
            .map(|t| (t.name.to_string(), t.buf.0.lock()))
            .collect::<Vec<_>>();

        let map: HashMap<String, Part> =
            locked_trackers
                .iter_mut()
                .fold(HashMap::new(), |mut h, (name, buf)| {
                    buf.consolidate();
                    if !buf.vec.is_empty() {
                        let mut part = buf.vec.iter().fold(Part::default(), |mut part, r| {
                            part.count += r.count as usize;
                            part.sum += Duration::from_nanos(r.ns);
                            part
                        });
                        part.copies = 1;

                        *h.entry(name.clone()).or_default() += part;
                    }
                    h
                });

        let map = locked_trackers.iter().fold(map, |mut h, (name, buf)| {
            if !buf.vec.is_empty() {
                let mean = h
                    .get(name)
                    .map(|p| p.sum / p.count as u32)
                    .unwrap()
                    .as_secs_f64();
                let sqsum = buf.vec.iter().fold(0., |acc, r| {
                    let s = r.ns as f64 / 1_000_000_000.;
                    let c = r.count as f64;
                    let d = (s / c) - mean;

                    acc + (d * d * c)
                });

                h.get_mut(name).unwrap().sqsum += sqsum;
            }
            h
        });

        map.into_iter()
            .map(|(name, part)| {
                (
                    name,
                    Stats {
                        copies: part.copies,
                        count: part.count,
                        sum: part.sum,
                        mean: part.sum / part.count as u32,
                        var: part.sqsum / (part.count - 1).max(1) as f64,
                    },
                )
            })
            .collect()
    }

    /// Get basic statistics about all tracks. Groups tracks with
    pub fn stats(&self) -> Vec<(String, Stats)> {
        self.trackers
            .lock()
            .iter()
            .filter_map(|t| {
                let mut buf = t.buf.0.lock();
                if buf.vec.is_empty() {
                    return None;
                }
                buf.consolidate();

                let (sum, count): (Duration, usize) =
                    buf.vec.iter().fold(Default::default(), |(sum, count), r| {
                        (sum + Duration::from_nanos(r.ns), count + r.count as usize)
                    });
                let mean = (sum / count as u32).as_secs_f64();
                let sqsum = buf.vec.iter().fold(0., |acc, r| {
                    let s = r.ns as f64 / 1_000_000_000.;
                    let c = r.count as f64;
                    let d = (s / c) - mean;

                    acc + (d * d * c)
                });
                Some((
                    t.name.to_owned(),
                    Stats {
                        copies: 1,
                        count,
                        sum,
                        mean: sum / count as u32,
                        var: sqsum / (count - 1).max(1) as f64,
                    },
                ))
            })
            .collect()
    }
}

#[cfg(feature = "instant")]
static START: Lazy<Instant> = Lazy::new(Instant::now);

/// Force initialization of start time. If not called it will be
/// automatically initialized when the first measurement is recorded
#[cfg(feature = "instant")]
pub fn start() {
    Lazy::force(&START);
}

/// Single unit of measurement
#[derive(Default, Clone, Copy)]
pub struct Record {
    /// Total duration of recorded events
    pub ns: u64,
    /// Number of recorded events
    pub count: u32,
    /// End timestamp for the last event
    #[cfg(feature = "instant")]
    pub end: u64,
}

impl Record {
    /// Mean duration of recorded events
    #[inline]
    pub fn mean(&self) -> Duration {
        Duration::from_nanos(self.ns / self.count as u64)
    }
}

impl AddAssign<Record> for Record {
    fn add_assign(&mut self, rhs: Record) {
        self.ns += rhs.ns;
        self.count += rhs.count;
        self.end = self.end.max(rhs.end);
    }
}

#[derive(Clone)]
struct RecordBuf(Arc<Mutex<RecordBufInner>>);
struct RecordBufInner {
    cur: Record,
    vec: Vec<Record>,
}

impl RecordBufInner {
    #[cfg(feature = "perforation")]
    #[inline]
    fn update(&mut self, d: Duration) {
        let q = self.vec.len() / PERFORATION_STEP;
        match q {
            0 => self.vec.push(Record {
                ns: d.as_nanos() as u64,
                count: 1,
                end: START.elapsed().as_nanos() as u64,
            }),
            q => {
                self.cur.count += 1;
                self.cur.ns += d.as_nanos() as u64;
                if self.cur.count == 1 << q {
                    self.cur.end = START.elapsed().as_nanos() as u64;
                    self.consolidate();
                }
            }
        }
    }

    #[cfg(not(feature = "perforation"))]
    #[inline]
    fn update(&mut self, d: Duration) {
        self.vec.push(Record {
            ns: d.as_nanos() as u64,
            end: START.elapsed().as_nanos() as u64,
            count: 1,
        });
    }

    #[inline]
    fn consolidate(&mut self) {
        if self.cur.count > 0 {
            self.vec.push(std::mem::take(&mut self.cur))
        }
    }
}

impl RecordBuf {
    #[inline]
    fn record(&self, d: Duration) {
        // Lock is never contended except on consultation of results
        // so despite the lock, this is cheap and non blocking most of the times
        self.0.lock().update(d);
    }
}

impl Default for RecordBuf {
    #[inline]
    fn default() -> Self {
        Self(Arc::new(Mutex::new(RecordBufInner {
            cur: Default::default(),
            vec: Vec::with_capacity(BUFFER_INIT_BYTES / std::mem::size_of::<Record>()),
        })))
    }
}

/// Lazy, thread local track. Should be used as a static variable
/// for each point of the program that needs to be measured.
///
/// ```
/// # use micrometer::*;
/// // Initialize a thread local track point
/// static TRACK_POINT: TrackPoint = TrackPoint::new_thread_local();
/// // Use the track point to create a new span
/// let span = TRACK_POINT.get_or_init("name").span();
/// ```
pub struct TrackPoint(Lazy<ThreadLocal<Track>>);

impl TrackPoint {
    /// Create a new thread local track point.
    #[inline]
    pub const fn new_thread_local() -> Self {
        Self(Lazy::new(ThreadLocal::new))
    }

    /// Get the thread local track that is used to measure events.
    /// The name of the track will be initialized to `name` on the first call (for each thread)
    /// and cannot be modified.
    #[inline]
    pub fn get_or_init(&self, name: &'static str) -> &Track {
        self.0.get_or(|| Track::new(name))
    }
}

/// Component used to create and save measures
#[derive(Clone)]
pub struct Track {
    name: &'static str,
    buf: RecordBuf,
}

impl Track {
    #[inline]
    pub fn new(name: &'static str) -> Self {
        let buf = RecordBuf::default();
        let t = Self { buf, name };

        global().register(&t);

        t
    }

    // Get a span guard, measures time from its creation to when it's dropped
    #[inline]
    pub fn span(&self) -> Span<'_> {
        Span::new(self)
    }

    #[inline]
    pub fn record(&self, r: Duration) {
        self.buf.record(r);
    }
}

#[derive(Clone)]
#[cfg(feature = "enable")]
pub struct Span<'a> {
    start: Instant,
    owner: &'a Track,
    armed: bool,
}

#[cfg(feature = "enable")]
impl<'a> Span<'a> {
    #[inline]
    fn new(owner: &'a Track) -> Self {
        let start = Instant::now();
        Self {
            start,
            owner,
            armed: true,
        }
    }

    #[inline]
    pub fn arm(&mut self) {
        self.armed = true;
    }

    #[inline]
    pub fn disarm(&mut self) {
        self.armed = false;
    }
}

#[cfg(feature = "enable")]
impl Drop for Span<'_> {
    #[inline]
    fn drop(&mut self) {
        if self.armed {
            self.owner.record(self.start.elapsed());
        }
    }
}

#[derive(Clone, Default)]
#[cfg(not(feature = "enable"))]
pub struct Span<'a> {
    owner: std::marker::PhantomData<&'a ()>,
}

#[cfg(not(feature = "enable"))]
impl<'a> Span<'a> {
    #[inline]
    fn new(_owner: &'a Track) -> Self {
        Self {
            owner: std::marker::PhantomData,
        }
    }

    #[inline]
    pub fn arm(&mut self) {}

    #[inline]
    pub fn disarm(&mut self) {}
}

#[cfg(not(feature = "enable"))]
impl Drop for Span<'_> {
    #[inline]
    fn drop(&mut self) {}
}

#[derive(Debug)]
pub struct Stats {
    pub copies: usize,
    pub count: usize,
    pub sum: Duration,
    pub mean: Duration,
    pub var: f64,
}

#[cfg(feature = "enable")]
#[macro_export]
macro_rules! span {
    () => {
        $crate::span!(
            __span,
            concat!(module_path!(), ":", line!(), ":", column!())
        )
    };
    ($span:ident) => {
        $crate::span!($span, stringify!($span))
    };
    ($span:ident, $name:expr) => {
        #[allow(unused)]
        let $span = {
            use $crate::TrackPoint;
            static TRACK_POINT: TrackPoint = TrackPoint::new_thread_local();
            TRACK_POINT.get_or_init($name).span()
        };
    };
    ($e:expr) => {{
        #[allow(unused)]
        let __span = {
            use $crate::TrackPoint;
            static TRACK_POINT: TrackPoint = TrackPoint::new_thread_local();
            TRACK_POINT
                .get_or_init(concat!(module_path!(), ":", line!(), ":", column!()))
                .span()
        };
        $e
    }};
    ($e:expr, $name:expr) => {{
        #[allow(unused)]
        let __span = {
            use $crate::TrackPoint;
            static TRACK_POINT: TrackPoint = TrackPoint::new_thread_local();
            TRACK_POINT.get_or_init($name).span()
        };
        $e
    }};
}

#[cfg(not(feature = "enable"))]
#[macro_export]
macro_rules! span {
    () => {
        $crate::Span::default()
    };
    ($span:ident) => {
        #[allow(unused)]
        let $span = $crate::Span::default();
    };
    ($span:ident, $name:expr) => {
        #[allow(unused)]
        let $span = $crate::Span::default();
    };
    ($e:expr) => {
        $e
    };
    ($e:expr, $name:expr) => {{
        $e
    }};
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::time::Duration;

    fn example_events() {
        super::span!(once_event);
        for _ in 0..50 {
            super::span!(loop_event);
            std::hint::black_box("do something");
            _ = super::span!(5 * 5, "expr_event");
        }
    }

    #[test]
    fn stats() {
        example_events();
        eprintln!("{:?}", super::global().stats());
        super::global().clear();
    }

    #[test]
    fn stats_grouped() {
        example_events();
        eprintln!("{:?}", super::global().stats_grouped());
        super::global().clear();
    }

    #[test]
    fn stats_threaded() {
        std::thread::scope(|s| {
            for _ in 0..4 {
                s.spawn(example_events);
            }
        });
        eprintln!("{:?}", super::global().stats());
        super::global().clear();
    }

    #[test]
    fn stats_grouped_threaded() {
        std::thread::scope(|s| {
            for _ in 0..4 {
                s.spawn(example_events);
            }
        });
        eprintln!("{:?}", super::global().stats_grouped());
        super::global().clear();
    }

    #[test]
    fn save_csv() {
        example_events();

        let dir = tempfile::tempdir().expect("failed to create temp file");
        let path = dir.path().join("micrometer-test.csv");
        super::save_csv(path).expect("failed to save csv");
        let path = dir.path().join("micrometer-test-uniform.csv");
        super::save_csv_uniform(path).expect("failed to save csv");
    }

    #[test]
    fn append_csv() {
        example_events();

        let dir = tempfile::tempdir().expect("failed to create temp file");
        let path = dir.path().join("micrometer-test.csv");
        super::append_csv(&path, "a").expect("failed to save csv");
        super::append_csv(&path, "b").expect("failed to save csv");
    }

    #[test]
    fn append_csv_uniform() {
        example_events();

        let dir = tempfile::tempdir().expect("failed to create temp file");
        let path = dir.path().join("micrometer-test.csv");
        super::append_csv_uniform(&path, "a").expect("failed to save csv");
        super::append_csv_uniform(&path, "b").expect("failed to save csv");
    }

    #[test]
    fn example() {
        span!(full);

        (0..4)
            .map(|_| {
                std::thread::spawn(|| {
                    span!(thread);
                    std::thread::sleep(Duration::from_micros(200));
                })
            })
            .collect::<Vec<_>>()
            .into_iter()
            .for_each(|j| j.join().unwrap());

        for _ in 0..16 {
            span!(inside);
            std::thread::sleep(Duration::from_micros(100));
        }

        drop(full);

        summary();
        println!();
        summary_grouped();
    }
}