stackpulse 0.1.0

Linux perf_event stack sampling with native unwinding, symbolization, and compact spooling
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
//! Sample error statistics for tracking profiling failures.
//!
//! This module provides types for tracking and reporting the various
//! error conditions that can cause sample failures during profiling.

use std::fmt;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Mutex;
use std::time::{Duration, Instant};

/// Categories of sample failures for statistics tracking.
///
/// Each variant represents a distinct failure reason that can occur
/// during stack sampling. These are designed to be distinguishable
/// for debugging and optimization purposes.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum SampleErrorKind {
    // Stack Chunks (0-9)
    /// Stack chunk size is 0 or exceeds 64MB limit
    StackChunkInvalidSize = 0,
    /// Failed to read stack chunk from remote memory
    StackChunkReadFailure = 1,
    /// Stack chunk list cycle detected (corrupted remote memory)
    StackChunkCycle = 2,
    /// Too many stack chunks (possible corrupted chunk list)
    StackChunkTooMany = 3,

    // Frame Parsing (10-19)
    /// Unknown frame owner type
    FrameUnknownOwner = 10,
    /// Failed to parse the initial frame in a chain (generic fallback)
    FrameParseFailure = 11,
    /// Initial frame had null code object address
    FrameInitialNullCode = 12,
    /// Initial frame was owned by a frame object (e.g. sys._getframe() keeping it alive)
    FrameInitialFrameObjectOwned = 13,
    /// Initial frame had an invalid owner value (likely memory corruption)
    FrameInitialGarbageOwner = 14,

    // Frame Chain (20-29)
    /// Too many frames in chain (>1024, possible infinite loop)
    FrameChainTooMany = 20,
    /// Broken frame chain (expected vs actual address mismatch)
    FrameChainBroken = 21,
    /// Incomplete walk (didn't reach base frame)
    FrameChainIncomplete = 22,

    // Code Objects (30-39)
    /// Failed to read code object from remote memory
    CodeObjectReadFailure = 30,
    /// NULL TLBC pointer (free-threaded Python)
    CodeObjectTlbcNull = 31,
    /// Invalid TLBC array size
    CodeObjectTlbcInvalidSize = 32,

    // Thread State (40-49)
    /// Failed to read thread state from remote memory
    ThreadStateReadFailure = 40,

    // Native Unwinding (50-59)
    /// Failed to capture thread registers
    NativeRegisterCapture = 50,
    /// Failed to read native stack memory
    NativeStackRead = 51,
    /// Native stack copy was too small and unwind was truncated
    NativeStackTruncated = 52,
    /// Framehop error: unwinding did not advance frame/stack pointer
    NativeFramehopDidNotAdvance = 53,
    /// Framehop error: return address became NULL
    NativeFramehopReturnAddressNull = 54,
    /// Framehop error: frame pointer unwinding moved backwards
    NativeFramehopMovedBackwards = 55,
    /// Framehop error: integer overflow during unwind calculations
    NativeFramehopIntegerOverflow = 56,

    // Stack Merging (60-69)
    /// More Python frame groups than native eval frames during merge
    MergeTooManyPythonFrames = 60,
    /// Fewer Python frame groups than native eval frames during merge
    MergeTooFewPythonFrames = 61,

    // Thread List (70-79)
    /// Thread list cycle detected (corrupted remote memory)
    ThreadListCycle = 70,
    /// Too many threads (>4096, possible corrupted thread list)
    ThreadListTooMany = 71,
}

/// Number of error kinds (must match the last discriminant + 1)
const ERROR_KIND_COUNT: usize = 72;

impl SampleErrorKind {
    /// All variants for iteration, ordered by category for grouped display.
    pub const ALL: &'static [SampleErrorKind] = &[
        SampleErrorKind::StackChunkInvalidSize,
        SampleErrorKind::StackChunkReadFailure,
        SampleErrorKind::StackChunkCycle,
        SampleErrorKind::StackChunkTooMany,
        SampleErrorKind::FrameUnknownOwner,
        SampleErrorKind::FrameParseFailure,
        SampleErrorKind::FrameInitialNullCode,
        SampleErrorKind::FrameInitialFrameObjectOwned,
        SampleErrorKind::FrameInitialGarbageOwner,
        SampleErrorKind::FrameChainTooMany,
        SampleErrorKind::FrameChainBroken,
        SampleErrorKind::FrameChainIncomplete,
        SampleErrorKind::CodeObjectReadFailure,
        SampleErrorKind::CodeObjectTlbcNull,
        SampleErrorKind::CodeObjectTlbcInvalidSize,
        SampleErrorKind::ThreadStateReadFailure,
        SampleErrorKind::NativeRegisterCapture,
        SampleErrorKind::NativeStackRead,
        SampleErrorKind::NativeStackTruncated,
        SampleErrorKind::NativeFramehopDidNotAdvance,
        SampleErrorKind::NativeFramehopReturnAddressNull,
        SampleErrorKind::NativeFramehopMovedBackwards,
        SampleErrorKind::NativeFramehopIntegerOverflow,
        SampleErrorKind::MergeTooManyPythonFrames,
        SampleErrorKind::MergeTooFewPythonFrames,
        SampleErrorKind::ThreadListCycle,
        SampleErrorKind::ThreadListTooMany,
    ];

    /// Category name for grouping in display.
    #[must_use]
    pub fn category(&self) -> &'static str {
        match self {
            Self::StackChunkInvalidSize
            | Self::StackChunkReadFailure
            | Self::StackChunkCycle
            | Self::StackChunkTooMany => "Stack Chunks",
            Self::FrameUnknownOwner
            | Self::FrameParseFailure
            | Self::FrameInitialNullCode
            | Self::FrameInitialFrameObjectOwned
            | Self::FrameInitialGarbageOwner => "Frame Parsing",
            Self::FrameChainTooMany | Self::FrameChainBroken | Self::FrameChainIncomplete => {
                "Frame Chain"
            }
            Self::CodeObjectReadFailure
            | Self::CodeObjectTlbcNull
            | Self::CodeObjectTlbcInvalidSize => "Code Objects",
            Self::ThreadStateReadFailure => "Thread State",
            Self::NativeRegisterCapture
            | Self::NativeStackRead
            | Self::NativeStackTruncated
            | Self::NativeFramehopDidNotAdvance
            | Self::NativeFramehopReturnAddressNull
            | Self::NativeFramehopMovedBackwards
            | Self::NativeFramehopIntegerOverflow => "Native Unwinding",
            Self::MergeTooManyPythonFrames | Self::MergeTooFewPythonFrames => "Stack Merging",
            Self::ThreadListCycle | Self::ThreadListTooMany => "Thread List",
        }
    }

    /// Short human-readable description.
    #[must_use]
    pub fn description(&self) -> &'static str {
        match self {
            Self::StackChunkInvalidSize => "Invalid chunk size",
            Self::StackChunkReadFailure => "Chunk read failure",
            Self::FrameUnknownOwner => "Unknown frame owner",
            Self::FrameChainTooMany => "Too many frames",
            Self::FrameChainBroken => "Broken chain",
            Self::FrameChainIncomplete => "Incomplete walk",
            Self::CodeObjectReadFailure | Self::ThreadStateReadFailure => "Read failure",
            Self::CodeObjectTlbcNull => "NULL TLBC pointer",
            Self::CodeObjectTlbcInvalidSize => "Invalid TLBC size",
            Self::NativeRegisterCapture => "Register capture failed",
            Self::NativeStackRead => "Stack read failed",
            Self::NativeStackTruncated => "Stack copy too small (truncated unwind)",
            Self::NativeFramehopDidNotAdvance => "Framehop: did not advance",
            Self::NativeFramehopReturnAddressNull => "Framehop: return address is NULL",
            Self::NativeFramehopMovedBackwards => "Framehop: frame pointer moved backwards",
            Self::NativeFramehopIntegerOverflow => "Framehop: integer overflow",
            Self::MergeTooManyPythonFrames => "Too many Python frames for native eval slots",
            Self::MergeTooFewPythonFrames => "Too few Python frames for native eval slots",
            Self::ThreadListCycle => "Cycle detected",
            Self::ThreadListTooMany => "Too many threads",
            Self::StackChunkCycle => "Chunk list cycle",
            Self::StackChunkTooMany => "Too many chunks",
            Self::FrameParseFailure => "Initial frame parse failed",
            Self::FrameInitialNullCode => "Initial frame: null code object",
            Self::FrameInitialFrameObjectOwned => {
                "Initial frame: held by frame object (sys._getframe)"
            }
            Self::FrameInitialGarbageOwner => "Initial frame: invalid owner (memory corruption)",
        }
    }
}

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

/// Minimum interval between debug log emissions for the same error kind.
///
/// Sampling errors fire on a hot loop (~100 Hz × N threads × M frames),
/// so we throttle per-kind to roughly one debug log per second per kind.
/// A new failure of a *different* kind logs immediately.
const SAMPLE_ERROR_LOG_INTERVAL: Duration = Duration::from_secs(1);

/// Atomic counters for sample error statistics.
///
/// Uses a fixed-size array indexed by [`SampleErrorKind`] discriminant
/// for O(1) access with no allocations. Thread-safe via atomic operations.
#[derive(Debug)]
pub struct SampleErrorStats {
    counts: [AtomicU64; ERROR_KIND_COUNT],
    /// Per-kind throttle for debug log emission.
    last_logged: Mutex<[Option<Instant>; ERROR_KIND_COUNT]>,
}

impl SampleErrorStats {
    /// Create new stats with all counters at zero.
    #[must_use]
    pub fn new() -> Self {
        Self {
            counts: std::array::from_fn(|_| AtomicU64::new(0)),
            last_logged: Mutex::new([None; ERROR_KIND_COUNT]),
        }
    }

    /// Record an error occurrence. O(1), zero-allocation.
    #[inline]
    pub fn record(&self, kind: SampleErrorKind) {
        self.counts[kind as usize].fetch_add(1, Ordering::Relaxed);
    }

    /// Record an error occurrence and emit a rate-limited debug log with context.
    ///
    /// `context` is a closure invoked only when the throttle allows a log to fire,
    /// so callers pay the format cost only when the event is emitted. Per-kind
    /// throttling means a new failure of a different kind logs immediately while
    /// repeated failures of the same kind collapse to ~1 log per second.
    pub fn record_with_log(&self, kind: SampleErrorKind, context: impl FnOnce() -> String) {
        self.record(kind);
        if self.should_log(kind) {
            tracing::debug!(
                target: "stackpulse::sampler::error",
                kind = %kind,
                category = kind.category(),
                context = %context(),
                "sample error recorded"
            );
        }
    }

    fn should_log(&self, kind: SampleErrorKind) -> bool {
        let mut guard = match self.last_logged.lock() {
            Ok(g) => g,
            Err(poisoned) => poisoned.into_inner(),
        };
        let now = Instant::now();
        let slot = &mut guard[kind as usize];
        match *slot {
            Some(prev) if now.duration_since(prev) < SAMPLE_ERROR_LOG_INTERVAL => false,
            _ => {
                *slot = Some(now);
                true
            }
        }
    }

    /// Get count for a specific error kind.
    #[inline]
    pub fn get(&self, kind: SampleErrorKind) -> u64 {
        self.counts[kind as usize].load(Ordering::Relaxed)
    }

    /// Total errors across all categories.
    pub fn total(&self) -> u64 {
        self.counts.iter().map(|c| c.load(Ordering::Relaxed)).sum()
    }

    /// Check if any errors were recorded.
    pub fn has_errors(&self) -> bool {
        self.counts.iter().any(|c| c.load(Ordering::Relaxed) > 0)
    }

    /// Iterate over all non-zero error counts.
    pub fn iter_nonzero(&self) -> impl Iterator<Item = (SampleErrorKind, u64)> + '_ {
        SampleErrorKind::ALL.iter().filter_map(|&kind| {
            let count = self.get(kind);
            if count > 0 {
                Some((kind, count))
            } else {
                None
            }
        })
    }

    /// Reset all counters to zero.
    pub fn reset(&self) {
        for counter in &self.counts {
            counter.store(0, Ordering::Relaxed);
        }
    }
}

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

impl Clone for SampleErrorStats {
    fn clone(&self) -> Self {
        let new = Self::new();
        for (i, counter) in self.counts.iter().enumerate() {
            new.counts[i].store(counter.load(Ordering::Relaxed), Ordering::Relaxed);
        }
        new
    }
}

/// Snapshot of per-kind counters for emitting periodic delta summaries.
#[derive(Debug, Clone)]
pub struct SampleErrorSnapshot {
    counts: [u64; ERROR_KIND_COUNT],
}

impl Default for SampleErrorSnapshot {
    fn default() -> Self {
        Self {
            counts: [0u64; ERROR_KIND_COUNT],
        }
    }
}

impl SampleErrorSnapshot {
    /// Take a snapshot of the current counter state.
    #[must_use]
    pub fn capture(stats: &SampleErrorStats) -> Self {
        let mut counts = [0u64; ERROR_KIND_COUNT];
        for (i, counter) in stats.counts.iter().enumerate() {
            counts[i] = counter.load(Ordering::Relaxed);
        }
        Self { counts }
    }

    /// Iterate over kinds whose counter increased since `previous`.
    pub fn delta_iter<'a>(
        &'a self,
        previous: &'a Self,
    ) -> impl Iterator<Item = (SampleErrorKind, u64)> + 'a {
        SampleErrorKind::ALL.iter().filter_map(move |&kind| {
            let cur = self.counts[kind as usize];
            let prev = previous.counts[kind as usize];
            if cur > prev {
                Some((kind, cur - prev))
            } else {
                None
            }
        })
    }

    /// Total errors across all categories in this snapshot.
    #[must_use]
    pub fn total(&self) -> u64 {
        self.counts.iter().sum()
    }
}

/// Format error statistics for display.
///
/// Groups errors by category with counts and percentages.
/// Includes progress bars for visual representation.
pub struct ErrorStatsFormatter<'a> {
    stats: &'a SampleErrorStats,
    total_samples: u64,
    successful_samples: u64,
}

impl<'a> ErrorStatsFormatter<'a> {
    /// Create a new formatter.
    pub fn new(stats: &'a SampleErrorStats, total_samples: u64, successful_samples: u64) -> Self {
        Self {
            stats,
            total_samples,
            successful_samples,
        }
    }

    /// Generate a progress bar string.
    fn progress_bar(percentage: f64, width: usize) -> String {
        let filled = ((percentage / 100.0) * width as f64).round() as usize;
        let empty = width.saturating_sub(filled);
        format!("{}{}", "".repeat(filled), "".repeat(empty))
    }

    /// Write formatted stats to the provided writer.
    ///
    /// # Errors
    /// Returns a `fmt::Error` if writing to the output fails.
    pub fn write_to(&self, w: &mut impl fmt::Write) -> fmt::Result {
        let total_errors = self.stats.total();
        let entries: Vec<_> = self.stats.iter_nonzero().collect();
        let desc_width = entries
            .iter()
            .map(|(kind, _)| kind.description().len() + 1) // include trailing ':'
            .max()
            .unwrap_or(24)
            .max(24);

        // Overview section
        writeln!(w, "\nOverview:")?;
        writeln!(
            w,
            "  Total samples:       {}",
            format_number(self.total_samples)
        )?;
        writeln!(
            w,
            "  Successful:          {} ({:.2}%)",
            format_number(self.successful_samples),
            if self.total_samples > 0 {
                // Stats counters converted to f64 for percentage display
                self.successful_samples as f64 / self.total_samples as f64 * 100.0
            } else {
                0.0
            }
        )?;
        writeln!(
            w,
            "  Sample errors:       {} ({:.2}%)",
            format_number(total_errors),
            if self.total_samples > 0 {
                // Stats counters converted to f64 for percentage display
                total_errors as f64 / self.total_samples as f64 * 100.0
            } else {
                0.0
            }
        )?;

        if total_errors == 0 {
            writeln!(w, "\n  No sample errors recorded")?;
            return Ok(());
        }

        // Group by category
        let mut current_category = "";
        for (kind, count) in &entries {
            let category = kind.category();
            if category != current_category {
                writeln!(w, "\n{category}:")?;
                current_category = category;
            }

            // Stats counters converted to f64 for percentage display
            let pct_of_errors = (*count as f64 / total_errors as f64) * 100.0;
            let pct_of_samples = if self.total_samples > 0 {
                (*count as f64 / self.total_samples as f64) * 100.0
            } else {
                0.0
            };
            let bar = Self::progress_bar(pct_of_errors, 20);

            writeln!(
                w,
                "  {:3} {:<desc_width$} {:>8} ({:>5.1}% of errors, {:>5.2}% of samples)  {}",
                "",
                format!("{}:", kind.description()),
                format_number(*count),
                pct_of_errors,
                pct_of_samples,
                bar
            )?;
        }

        Ok(())
    }
}

/// Format a number with thousand separators.
fn format_number(n: u64) -> String {
    let s = n.to_string();
    let mut result = String::with_capacity(s.len() + s.len() / 3);
    for (i, c) in s.chars().rev().enumerate() {
        if i > 0 && i % 3 == 0 {
            result.push(',');
        }
        result.push(c);
    }
    result.chars().rev().collect()
}

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

    #[test]
    fn test_category_grouping() {
        // Verify that related errors share categories
        assert_eq!(
            SampleErrorKind::StackChunkInvalidSize.category(),
            SampleErrorKind::StackChunkReadFailure.category()
        );
        assert_eq!(
            SampleErrorKind::StackChunkCycle.category(),
            SampleErrorKind::StackChunkInvalidSize.category()
        );
        assert_eq!(
            SampleErrorKind::StackChunkTooMany.category(),
            SampleErrorKind::StackChunkInvalidSize.category()
        );
        assert_eq!(
            SampleErrorKind::FrameChainTooMany.category(),
            SampleErrorKind::FrameChainIncomplete.category()
        );
        assert_eq!(
            SampleErrorKind::FrameUnknownOwner.category(),
            SampleErrorKind::FrameParseFailure.category()
        );
        assert_eq!(
            SampleErrorKind::NativeRegisterCapture.category(),
            SampleErrorKind::NativeStackRead.category()
        );
    }

    // SampleErrorStats basic operations

    #[test]
    fn test_new_stats_are_zero() {
        let stats = SampleErrorStats::new();
        for kind in SampleErrorKind::ALL {
            assert_eq!(stats.get(*kind), 0, "{kind:?} should start at 0");
        }
        assert_eq!(stats.total(), 0);
        assert!(!stats.has_errors());
    }

    #[test]
    fn test_record_and_get() {
        let stats = SampleErrorStats::new();

        stats.record(SampleErrorKind::FrameChainIncomplete);
        assert_eq!(stats.get(SampleErrorKind::FrameChainIncomplete), 1);
        assert!(stats.has_errors());

        stats.record(SampleErrorKind::FrameChainIncomplete);
        assert_eq!(stats.get(SampleErrorKind::FrameChainIncomplete), 2);

        stats.record(SampleErrorKind::StackChunkReadFailure);
        assert_eq!(stats.get(SampleErrorKind::StackChunkReadFailure), 1);
        assert_eq!(stats.get(SampleErrorKind::FrameUnknownOwner), 0);
        assert_eq!(stats.total(), 3);
    }

    #[test]
    fn test_iter_nonzero_empty() {
        let stats = SampleErrorStats::new();
        let nonzero: Vec<_> = stats.iter_nonzero().collect();
        assert!(nonzero.is_empty());
    }

    #[test]
    fn test_iter_nonzero() {
        let stats = SampleErrorStats::new();
        stats.record(SampleErrorKind::FrameChainBroken);
        stats.record(SampleErrorKind::FrameChainBroken);
        stats.record(SampleErrorKind::MergeTooManyPythonFrames);

        let nonzero: Vec<_> = stats.iter_nonzero().collect();
        assert_eq!(nonzero.len(), 2);

        assert!(nonzero.contains(&(SampleErrorKind::FrameChainBroken, 2)));
        assert!(nonzero.contains(&(SampleErrorKind::MergeTooManyPythonFrames, 1)));
    }

    #[test]
    fn test_iter_nonzero_preserves_order() {
        let stats = SampleErrorStats::new();
        // Record in reverse order
        stats.record(SampleErrorKind::MergeTooManyPythonFrames);
        stats.record(SampleErrorKind::StackChunkInvalidSize);

        let nonzero: Vec<_> = stats.iter_nonzero().collect();

        // Should be in ALL order, not recording order
        assert_eq!(nonzero[0].0, SampleErrorKind::StackChunkInvalidSize);
        assert_eq!(nonzero[1].0, SampleErrorKind::MergeTooManyPythonFrames);
    }

    // SampleErrorStats reset

    #[test]
    fn test_reset() {
        let stats = SampleErrorStats::new();

        for kind in SampleErrorKind::ALL {
            stats.record(*kind);
            stats.record(*kind);
        }

        assert!(stats.has_errors());
        assert_eq!(stats.total(), (SampleErrorKind::ALL.len() * 2) as u64);

        stats.reset();

        assert!(!stats.has_errors());
        assert_eq!(stats.total(), 0);
        for kind in SampleErrorKind::ALL {
            assert_eq!(stats.get(*kind), 0);
        }
    }

    // SampleErrorStats clone

    #[test]
    fn test_clone() {
        let stats = SampleErrorStats::new();
        stats.record(SampleErrorKind::FrameChainIncomplete);
        stats.record(SampleErrorKind::StackChunkReadFailure);

        let cloned = stats.clone();

        // Clone has same values
        assert_eq!(cloned.get(SampleErrorKind::FrameChainIncomplete), 1);
        assert_eq!(cloned.get(SampleErrorKind::StackChunkReadFailure), 1);
        assert_eq!(cloned.total(), 2);
    }

    #[test]
    fn test_clone_independence() {
        let stats = SampleErrorStats::new();
        stats.record(SampleErrorKind::FrameChainIncomplete);

        let cloned = stats.clone();

        // Modify original
        stats.record(SampleErrorKind::FrameChainIncomplete);
        stats.record(SampleErrorKind::StackChunkReadFailure);

        // Clone is unaffected
        assert_eq!(cloned.get(SampleErrorKind::FrameChainIncomplete), 1);
        assert_eq!(cloned.get(SampleErrorKind::StackChunkReadFailure), 0);
        assert_eq!(cloned.total(), 1);

        // Original has new values
        assert_eq!(stats.get(SampleErrorKind::FrameChainIncomplete), 2);
        assert_eq!(stats.get(SampleErrorKind::StackChunkReadFailure), 1);
        assert_eq!(stats.total(), 3);
    }

    // ErrorStatsFormatter tests

    #[test]
    fn test_formatter_no_errors() {
        let stats = SampleErrorStats::new();
        let formatter = ErrorStatsFormatter::new(&stats, 1000, 1000);

        let mut output = String::new();
        formatter.write_to(&mut output).unwrap();

        assert!(output.contains("Total samples:"));
        assert!(output.contains("1,000"));
        assert!(output.contains("No sample errors recorded"));
    }

    #[test]
    fn test_formatter_with_errors() {
        let stats = SampleErrorStats::new();
        stats.record(SampleErrorKind::FrameChainIncomplete);
        stats.record(SampleErrorKind::FrameChainIncomplete);
        stats.record(SampleErrorKind::StackChunkReadFailure);

        let formatter = ErrorStatsFormatter::new(&stats, 100, 97);

        let mut output = String::new();
        formatter.write_to(&mut output).unwrap();

        assert!(output.contains("Total samples:"));
        assert!(output.contains("100"));
        assert!(output.contains("Successful:"));
        assert!(output.contains("97"));
        assert!(output.contains("Sample errors:"));
        assert!(output.contains("Frame Chain:"));
        assert!(output.contains("Incomplete walk:"));
        assert!(output.contains("Stack Chunks:"));
    }

    #[test]
    fn test_formatter_errors_add_up() {
        let stats = SampleErrorStats::new();
        for _ in 0..50 {
            stats.record(SampleErrorKind::CodeObjectReadFailure);
        }
        for _ in 0..30 {
            stats.record(SampleErrorKind::MergeTooManyPythonFrames);
        }
        for _ in 0..20 {
            stats.record(SampleErrorKind::FrameChainIncomplete);
        }

        let formatter = ErrorStatsFormatter::new(&stats, 1000, 900);

        let mut output = String::new();
        formatter.write_to(&mut output).unwrap();

        assert!(output.contains("Sample errors:"));
        assert!(output.contains("100"));
    }

    #[test]
    fn test_formatter_categories_grouped() {
        let stats = SampleErrorStats::new();
        // Add errors from different categories
        stats.record(SampleErrorKind::StackChunkInvalidSize);
        stats.record(SampleErrorKind::StackChunkReadFailure);
        stats.record(SampleErrorKind::FrameChainBroken);
        stats.record(SampleErrorKind::MergeTooManyPythonFrames);

        let formatter = ErrorStatsFormatter::new(&stats, 100, 96);

        let mut output = String::new();
        formatter.write_to(&mut output).unwrap();

        // Verify category headers appear
        assert!(output.contains("Stack Chunks:"));
        assert!(output.contains("Frame Chain:"));
        assert!(output.contains("Stack Merging:"));
    }

    #[test]
    fn test_formatter_percentages() {
        let stats = SampleErrorStats::new();
        // Add 80 of one type, 20 of another = 80% and 20% of errors
        // With 1000 total samples: 8% and 2% of samples
        for _ in 0..80 {
            stats.record(SampleErrorKind::FrameChainIncomplete);
        }
        for _ in 0..20 {
            stats.record(SampleErrorKind::StackChunkReadFailure);
        }

        let formatter = ErrorStatsFormatter::new(&stats, 1000, 900);

        let mut output = String::new();
        formatter.write_to(&mut output).unwrap();

        // Check percentage of errors
        assert!(output.contains("80.0% of errors"));
        assert!(output.contains("20.0% of errors"));
        // Check percentage of samples
        assert!(output.contains("8.00% of samples"));
        assert!(output.contains("2.00% of samples"));
    }

    #[test]
    fn test_formatter_progress_bars_are_aligned() {
        let stats = SampleErrorStats::new();
        stats.record(SampleErrorKind::NativeFramehopMovedBackwards);
        stats.record(SampleErrorKind::NativeFramehopDidNotAdvance);
        stats.record(SampleErrorKind::MergeTooManyPythonFrames);

        let formatter = ErrorStatsFormatter::new(&stats, 1000, 997);
        let mut output = String::new();
        formatter.write_to(&mut output).unwrap();

        let lines: Vec<&str> = output
            .lines()
            .filter(|line| line.contains("Framehop:") || line.contains("Too many Python"))
            .collect();
        assert_eq!(lines.len(), 3);

        let bar_start = |line: &str| {
            line.find('')
                .or_else(|| line.find(''))
                .expect("formatted line should contain a progress bar")
        };

        let first = bar_start(lines[0]);
        assert_eq!(bar_start(lines[1]), first);
        assert_eq!(bar_start(lines[2]), first);
    }

    // Thread safety (basic verification)

    #[test]
    fn test_concurrent_recording() {
        use std::sync::Arc;
        use std::thread;

        let stats = Arc::new(SampleErrorStats::new());
        let num_threads: u64 = 4;
        let records_per_thread: u64 = 1000;

        let handles: Vec<_> = (0..num_threads)
            .map(|_| {
                let stats = Arc::clone(&stats);
                thread::spawn(move || {
                    for _ in 0..records_per_thread {
                        stats.record(SampleErrorKind::FrameChainIncomplete);
                    }
                })
            })
            .collect();

        for handle in handles {
            handle.join().unwrap();
        }

        assert_eq!(
            stats.get(SampleErrorKind::FrameChainIncomplete),
            num_threads * records_per_thread
        );
    }
}