safer-ring 0.0.1

A safe Rust wrapper around io_uring with zero-cost abstractions and compile-time memory safety guarantees
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
//! Performance profiling and optimization utilities.

use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};

/// Performance counter for tracking operation metrics.
#[derive(Debug)]
pub struct PerfCounter {
    /// Total number of operations
    count: AtomicU64,
    /// Total time spent in operations (nanoseconds)
    total_time_ns: AtomicU64,
    /// Minimum operation time (nanoseconds)
    min_time_ns: AtomicU64,
    /// Maximum operation time (nanoseconds)
    max_time_ns: AtomicU64,
}

impl PerfCounter {
    /// Create a new performance counter.
    pub fn new() -> Self {
        Self {
            count: AtomicU64::new(0),
            total_time_ns: AtomicU64::new(0),
            min_time_ns: AtomicU64::new(u64::MAX),
            max_time_ns: AtomicU64::new(0),
        }
    }

    /// Record an operation duration.
    pub fn record(&self, duration: Duration) {
        let nanos = duration.as_nanos() as u64;

        self.count.fetch_add(1, Ordering::Relaxed);
        self.total_time_ns.fetch_add(nanos, Ordering::Relaxed);

        // Update min (with compare-and-swap loop)
        let mut current_min = self.min_time_ns.load(Ordering::Relaxed);
        while nanos < current_min {
            match self.min_time_ns.compare_exchange_weak(
                current_min,
                nanos,
                Ordering::Relaxed,
                Ordering::Relaxed,
            ) {
                Ok(_) => break,
                Err(x) => current_min = x,
            }
        }

        // Update max (with compare-and-swap loop)
        let mut current_max = self.max_time_ns.load(Ordering::Relaxed);
        while nanos > current_max {
            match self.max_time_ns.compare_exchange_weak(
                current_max,
                nanos,
                Ordering::Relaxed,
                Ordering::Relaxed,
            ) {
                Ok(_) => break,
                Err(x) => current_max = x,
            }
        }
    }

    /// Get performance statistics.
    pub fn stats(&self) -> PerfStats {
        let count = self.count.load(Ordering::Relaxed);
        let total_ns = self.total_time_ns.load(Ordering::Relaxed);
        let min_ns = self.min_time_ns.load(Ordering::Relaxed);
        let max_ns = self.max_time_ns.load(Ordering::Relaxed);

        let avg_ns = if count > 0 { total_ns / count } else { 0 };

        PerfStats {
            count,
            total_time: Duration::from_nanos(total_ns),
            avg_time: Duration::from_nanos(avg_ns),
            min_time: if min_ns == u64::MAX {
                Duration::ZERO
            } else {
                Duration::from_nanos(min_ns)
            },
            max_time: Duration::from_nanos(max_ns),
        }
    }

    /// Reset all counters.
    pub fn reset(&self) {
        self.count.store(0, Ordering::Relaxed);
        self.total_time_ns.store(0, Ordering::Relaxed);
        self.min_time_ns.store(u64::MAX, Ordering::Relaxed);
        self.max_time_ns.store(0, Ordering::Relaxed);
    }
}

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

/// Performance statistics snapshot.
#[derive(Debug, Clone)]
pub struct PerfStats {
    /// Number of operations recorded
    pub count: u64,
    /// Total time across all operations
    pub total_time: Duration,
    /// Average time per operation
    pub avg_time: Duration,
    /// Minimum operation time
    pub min_time: Duration,
    /// Maximum operation time
    pub max_time: Duration,
}

impl PerfStats {
    /// Calculate operations per second.
    pub fn ops_per_sec(&self) -> f64 {
        if self.total_time.is_zero() {
            0.0
        } else {
            self.count as f64 / self.total_time.as_secs_f64()
        }
    }

    /// Calculate throughput in bytes per second.
    pub fn throughput_bps(&self, bytes_per_op: u64) -> f64 {
        self.ops_per_sec() * bytes_per_op as f64
    }
}

/// RAII timer for automatic performance measurement.
pub struct PerfTimer<'a> {
    counter: &'a PerfCounter,
    start: Instant,
}

impl<'a> PerfTimer<'a> {
    /// Start timing an operation.
    pub fn new(counter: &'a PerfCounter) -> Self {
        Self {
            counter,
            start: Instant::now(),
        }
    }
}

impl<'a> Drop for PerfTimer<'a> {
    fn drop(&mut self) {
        let duration = self.start.elapsed();
        self.counter.record(duration);
    }
}

/// Memory usage tracker.
#[derive(Debug)]
pub struct MemoryTracker {
    /// Current allocated bytes
    allocated: AtomicUsize,
    /// Peak allocated bytes
    peak: AtomicUsize,
    /// Total allocations
    total_allocs: AtomicU64,
    /// Total deallocations
    total_deallocs: AtomicU64,
}

impl MemoryTracker {
    /// Create a new memory tracker.
    pub fn new() -> Self {
        Self {
            allocated: AtomicUsize::new(0),
            peak: AtomicUsize::new(0),
            total_allocs: AtomicU64::new(0),
            total_deallocs: AtomicU64::new(0),
        }
    }

    /// Record an allocation.
    pub fn record_alloc(&self, size: usize) {
        let new_allocated = self.allocated.fetch_add(size, Ordering::Relaxed) + size;
        self.total_allocs.fetch_add(1, Ordering::Relaxed);

        // Update peak
        let mut current_peak = self.peak.load(Ordering::Relaxed);
        while new_allocated > current_peak {
            match self.peak.compare_exchange_weak(
                current_peak,
                new_allocated,
                Ordering::Relaxed,
                Ordering::Relaxed,
            ) {
                Ok(_) => break,
                Err(x) => current_peak = x,
            }
        }
    }

    /// Record a deallocation.
    pub fn record_dealloc(&self, size: usize) {
        self.allocated.fetch_sub(size, Ordering::Relaxed);
        self.total_deallocs.fetch_add(1, Ordering::Relaxed);
    }

    /// Get current memory usage.
    pub fn current_usage(&self) -> usize {
        self.allocated.load(Ordering::Relaxed)
    }

    /// Get peak memory usage.
    pub fn peak_usage(&self) -> usize {
        self.peak.load(Ordering::Relaxed)
    }

    /// Get memory statistics.
    pub fn stats(&self) -> MemoryStats {
        MemoryStats {
            current: self.allocated.load(Ordering::Relaxed),
            peak: self.peak.load(Ordering::Relaxed),
            total_allocs: self.total_allocs.load(Ordering::Relaxed),
            total_deallocs: self.total_deallocs.load(Ordering::Relaxed),
        }
    }

    /// Reset all counters.
    pub fn reset(&self) {
        self.allocated.store(0, Ordering::Relaxed);
        self.peak.store(0, Ordering::Relaxed);
        self.total_allocs.store(0, Ordering::Relaxed);
        self.total_deallocs.store(0, Ordering::Relaxed);
    }
}

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

/// Memory usage statistics.
#[derive(Debug, Clone)]
pub struct MemoryStats {
    /// Current allocated bytes
    pub current: usize,
    /// Peak allocated bytes
    pub peak: usize,
    /// Total number of allocations
    pub total_allocs: u64,
    /// Total number of deallocations
    pub total_deallocs: u64,
}

impl MemoryStats {
    /// Calculate allocation rate (allocs per second).
    pub fn alloc_rate(&self, duration: Duration) -> f64 {
        if duration.is_zero() {
            0.0
        } else {
            self.total_allocs as f64 / duration.as_secs_f64()
        }
    }

    /// Calculate average allocation size.
    pub fn avg_alloc_size(&self) -> f64 {
        if self.total_allocs > 0 {
            self.peak as f64 / self.total_allocs as f64
        } else {
            0.0
        }
    }
}

/// Global performance registry for collecting metrics across the library.
pub struct PerfRegistry {
    counters: std::collections::HashMap<String, Arc<PerfCounter>>,
    memory_tracker: Arc<MemoryTracker>,
}

impl PerfRegistry {
    /// Create a new performance registry.
    pub fn new() -> Self {
        Self {
            counters: std::collections::HashMap::new(),
            memory_tracker: Arc::new(MemoryTracker::new()),
        }
    }

    /// Get or create a performance counter.
    pub fn counter(&mut self, name: &str) -> Arc<PerfCounter> {
        self.counters
            .entry(name.to_string())
            .or_insert_with(|| Arc::new(PerfCounter::new()))
            .clone()
    }

    /// Get the memory tracker.
    pub fn memory_tracker(&self) -> Arc<MemoryTracker> {
        self.memory_tracker.clone()
    }

    /// Get all counter statistics.
    pub fn all_stats(&self) -> std::collections::HashMap<String, PerfStats> {
        self.counters
            .iter()
            .map(|(name, counter)| (name.clone(), counter.stats()))
            .collect()
    }

    /// Reset all counters.
    pub fn reset_all(&self) {
        for counter in self.counters.values() {
            counter.reset();
        }
        self.memory_tracker.reset();
    }
}

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

/// Macro for easy performance timing.
#[macro_export]
macro_rules! time_operation {
    ($counter:expr, $operation:expr) => {{
        let _timer = $crate::perf::PerfTimer::new($counter);
        $operation
    }};
}

/// Macro for timing async operations.
#[macro_export]
macro_rules! time_async_operation {
    ($counter:expr, $operation:expr) => {{
        let start = std::time::Instant::now();
        let result = $operation.await;
        $counter.record(start.elapsed());
        result
    }};
}

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

    #[test]
    fn test_perf_counter() {
        let counter = PerfCounter::new();

        counter.record(Duration::from_millis(10));
        counter.record(Duration::from_millis(20));
        counter.record(Duration::from_millis(5));

        let stats = counter.stats();
        assert_eq!(stats.count, 3);
        assert_eq!(stats.min_time, Duration::from_millis(5));
        assert_eq!(stats.max_time, Duration::from_millis(20));
        assert!(stats.avg_time >= Duration::from_millis(11));
        assert!(stats.avg_time <= Duration::from_millis(12));
    }

    #[test]
    fn test_perf_timer() {
        let counter = PerfCounter::new();

        {
            let _timer = PerfTimer::new(&counter);
            thread::sleep(Duration::from_millis(10));
        }

        let stats = counter.stats();
        assert_eq!(stats.count, 1);
        assert!(stats.total_time >= Duration::from_millis(9));
    }

    #[test]
    fn test_memory_tracker() {
        let tracker = MemoryTracker::new();

        tracker.record_alloc(1024);
        assert_eq!(tracker.current_usage(), 1024);
        assert_eq!(tracker.peak_usage(), 1024);

        tracker.record_alloc(2048);
        assert_eq!(tracker.current_usage(), 3072);
        assert_eq!(tracker.peak_usage(), 3072);

        tracker.record_dealloc(1024);
        assert_eq!(tracker.current_usage(), 2048);
        assert_eq!(tracker.peak_usage(), 3072); // Peak doesn't decrease
    }

    #[test]
    fn test_perf_registry() {
        let mut registry = PerfRegistry::new();

        let counter1 = registry.counter("test1");
        let counter2 = registry.counter("test2");

        counter1.record(Duration::from_millis(10));
        counter2.record(Duration::from_millis(20));

        let all_stats = registry.all_stats();
        assert_eq!(all_stats.len(), 2);
        assert!(all_stats.contains_key("test1"));
        assert!(all_stats.contains_key("test2"));
    }

    #[test]
    fn test_concurrent_perf_counter() {
        let counter = Arc::new(PerfCounter::new());
        let mut handles = vec![];

        for _ in 0..10 {
            let counter_clone = counter.clone();
            handles.push(thread::spawn(move || {
                for _ in 0..100 {
                    counter_clone.record(Duration::from_nanos(1000));
                }
            }));
        }

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

        let stats = counter.stats();
        assert_eq!(stats.count, 1000);
    }
}