rns-net 0.7.3

Network interfaces and node driver for the Reticulum Network Stack
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
//! Lightweight live profiling compatible with Reticulum's profiler results.
//!
//! Create a guard with [`profile`] or [`profile_with_parent`]. A capture is
//! registered immediately and completed when the guard is dropped, which makes
//! nested and reentrant calls independently visible without reporting partial
//! durations. Results are process-global and bounded per tag so the daemon can
//! expose them indefinitely over local and remote status APIs.

use std::collections::{BTreeMap, HashSet, VecDeque};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Mutex, OnceLock};
use std::thread::ThreadId;
use std::time::Instant;

use rns_core::msgpack::Value;

use crate::pickle::PickleValue;

/// Default maximum retained samples for each tag across all threads.
pub const MAX_CAPTURES: usize = 10_000;

#[derive(Debug, Clone, Copy)]
struct Capture {
    id: u64,
    started: f64,
    ended: Option<f64>,
    thread_id: ThreadId,
}

impl Capture {
    fn duration(self) -> Option<f64> {
        self.ended.map(|ended| ended - self.started)
    }
}

#[derive(Debug)]
struct TagCaptures {
    parent: Option<String>,
    max_captures: usize,
    captures: VecDeque<Capture>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct ProfilingStats {
    pub count: usize,
    pub mean: f64,
    pub median: f64,
    pub min: f64,
    pub max: f64,
    pub stdev: Option<f64>,
    pub sum: f64,
    pub threads: Option<usize>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct ProfilingResult {
    pub name: String,
    pub parent: Option<String>,
    pub stats_all: ProfilingStats,
    pub stats_1m: Option<ProfilingStats>,
    pub stats_5m: Option<ProfilingStats>,
    pub stats_30m: Option<ProfilingStats>,
    pub stats_60m: Option<ProfilingStats>,
}

static EPOCH: OnceLock<Instant> = OnceLock::new();
static NEXT_CAPTURE_ID: AtomicU64 = AtomicU64::new(1);
static CAPTURES: OnceLock<Mutex<BTreeMap<String, TagCaptures>>> = OnceLock::new();

fn elapsed_now() -> f64 {
    EPOCH.get_or_init(Instant::now).elapsed().as_secs_f64()
}

fn captures() -> &'static Mutex<BTreeMap<String, TagCaptures>> {
    CAPTURES.get_or_init(|| Mutex::new(BTreeMap::new()))
}

/// A timing sample that is completed when the guard is dropped.
#[must_use = "the guard must be retained for the duration being profiled"]
pub struct ProfileGuard {
    tag: String,
    capture_id: Option<u64>,
}

impl Drop for ProfileGuard {
    fn drop(&mut self) {
        if let Some(capture_id) = self.capture_id {
            complete_capture(&self.tag, capture_id, elapsed_now());
        }
    }
}

fn begin_capture(
    tag: &str,
    parent: Option<&str>,
    max_captures: usize,
    started: f64,
    thread_id: ThreadId,
) -> Option<u64> {
    if max_captures == 0 {
        return None;
    }
    let capture_id = NEXT_CAPTURE_ID.fetch_add(1, Ordering::Relaxed);
    let mut registry = captures()
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner());
    let entry = registry
        .entry(tag.to_owned())
        .or_insert_with(|| TagCaptures {
            parent: parent.map(str::to_owned),
            max_captures,
            captures: VecDeque::with_capacity(max_captures.min(1024)),
        });
    if entry.parent.is_none() {
        entry.parent = parent.map(str::to_owned);
    }
    while entry.captures.len() >= entry.max_captures {
        entry.captures.pop_front();
    }
    entry.captures.push_back(Capture {
        id: capture_id,
        started,
        ended: None,
        thread_id,
    });
    Some(capture_id)
}

fn complete_capture(tag: &str, capture_id: u64, ended: f64) {
    let mut registry = captures()
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner());
    let Some(entry) = registry.get_mut(tag) else {
        return;
    };
    if let Some(capture) = entry
        .captures
        .iter_mut()
        .find(|capture| capture.id == capture_id)
    {
        capture.ended = Some(ended);
    }
}

fn start_profile(tag: String, parent: Option<String>, max_captures: usize) -> ProfileGuard {
    let capture_id = begin_capture(
        &tag,
        parent.as_deref(),
        max_captures,
        elapsed_now(),
        std::thread::current().id(),
    );
    ProfileGuard { tag, capture_id }
}

/// Start profiling a top-level tag with the default retention limit.
pub fn profile(tag: impl Into<String>) -> ProfileGuard {
    start_profile(tag.into(), None, MAX_CAPTURES)
}

/// Start profiling a top-level tag with a custom per-tag retention limit.
pub fn profile_with_limit(tag: impl Into<String>, max_captures: usize) -> ProfileGuard {
    start_profile(tag.into(), None, max_captures)
}

/// Start profiling a tag nested under `parent` in formatted results.
pub fn profile_with_parent(tag: impl Into<String>, parent: impl Into<String>) -> ProfileGuard {
    start_profile(tag.into(), Some(parent.into()), MAX_CAPTURES)
}

/// Start nested profiling with a custom per-tag retention limit.
pub fn profile_with_parent_and_limit(
    tag: impl Into<String>,
    parent: impl Into<String>,
    max_captures: usize,
) -> ProfileGuard {
    start_profile(tag.into(), Some(parent.into()), max_captures)
}

/// Run a function or closure under a profiling tag and return its result.
///
/// This is the Rust counterpart to Reticulum's profiling decorator. Rust does
/// not expose a stable runtime qualified function name, so the tag is explicit.
/// The guard is also dropped during unwinding, preserving completed timing for
/// functions that panic when the panic is caught by their caller.
pub fn profile_function<T>(tag: impl Into<String>, function: impl FnOnce() -> T) -> T {
    let _profile = profile(tag);
    function()
}

/// Run a function or closure under a profiling tag with a custom retention
/// limit and return its result.
pub fn profile_function_with_limit<T>(
    tag: impl Into<String>,
    max_captures: usize,
    function: impl FnOnce() -> T,
) -> T {
    let _profile = profile_with_limit(tag, max_captures);
    function()
}

/// Return whether at least one timing sample has completed.
pub fn ran() -> bool {
    captures()
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner())
        .values()
        .any(|entry| entry.captures.iter().any(|capture| capture.ended.is_some()))
}

fn calculate_stats(captures: &[Capture], include_threads: bool) -> Option<ProfilingStats> {
    let completed: Vec<(f64, ThreadId)> = captures
        .iter()
        .filter_map(|capture| {
            capture
                .duration()
                .map(|duration| (duration, capture.thread_id))
        })
        .collect();
    if completed.is_empty() {
        return None;
    }
    let mut durations: Vec<f64> = completed.iter().map(|(duration, _)| *duration).collect();
    durations.sort_by(f64::total_cmp);
    let count = durations.len();
    let sum = durations.iter().sum::<f64>();
    let mean = sum / count as f64;
    let median = if count.is_multiple_of(2) {
        (durations[count / 2 - 1] + durations[count / 2]) / 2.0
    } else {
        durations[count / 2]
    };
    let stdev = (count > 1).then(|| {
        let variance = durations
            .iter()
            .map(|duration| (duration - mean).powi(2))
            .sum::<f64>()
            / (count - 1) as f64;
        variance.sqrt()
    });
    let threads = include_threads.then(|| {
        completed
            .iter()
            .map(|(_, thread_id)| *thread_id)
            .collect::<HashSet<_>>()
            .len()
    });
    Some(ProfilingStats {
        count,
        mean,
        median,
        min: durations[0],
        max: durations[count - 1],
        stdev,
        sum,
        threads,
    })
}

fn window_stats(
    captures: &[Capture],
    now: f64,
    youngest_age: f64,
    oldest_age: f64,
) -> Option<ProfilingStats> {
    let window: Vec<Capture> = captures
        .iter()
        .copied()
        .filter(|capture| {
            let age = now - capture.started;
            age >= youngest_age && age < oldest_age
        })
        .collect();
    calculate_stats(&window, false)
}

/// Return a stable, tag-sorted snapshot of all retained completed samples.
pub fn results() -> BTreeMap<String, ProfilingResult> {
    let now = elapsed_now();
    captures()
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner())
        .iter()
        .filter_map(|(tag, entry)| {
            let mut tag_captures: Vec<Capture> = entry.captures.iter().copied().collect();
            tag_captures.sort_by(|left, right| left.started.total_cmp(&right.started));
            let stats_all = calculate_stats(&tag_captures, true)?;
            Some((
                tag.clone(),
                ProfilingResult {
                    name: tag.clone(),
                    parent: entry.parent.clone(),
                    stats_all,
                    stats_1m: window_stats(&tag_captures, now, 0.0, 60.0),
                    stats_5m: window_stats(&tag_captures, now, 60.0, 5.0 * 60.0),
                    stats_30m: window_stats(&tag_captures, now, 5.0 * 60.0, 30.0 * 60.0),
                    stats_60m: window_stats(&tag_captures, now, 30.0 * 60.0, 60.0 * 60.0),
                },
            ))
        })
        .collect()
}

fn pickle_key(key: &str) -> PickleValue {
    PickleValue::String(key.into())
}

fn stats_to_pickle(stats: Option<&ProfilingStats>) -> PickleValue {
    let Some(stats) = stats else {
        return PickleValue::None;
    };
    PickleValue::Dict(vec![
        (pickle_key("count"), PickleValue::Int(stats.count as i64)),
        (pickle_key("mean"), PickleValue::Float(stats.mean)),
        (pickle_key("median"), PickleValue::Float(stats.median)),
        (pickle_key("min"), PickleValue::Float(stats.min)),
        (pickle_key("max"), PickleValue::Float(stats.max)),
        (
            pickle_key("stdev"),
            stats.stdev.map_or(PickleValue::None, PickleValue::Float),
        ),
        (pickle_key("sum"), PickleValue::Float(stats.sum)),
        (
            pickle_key("threads"),
            stats.threads.map_or(PickleValue::None, |threads| {
                PickleValue::Int(threads as i64)
            }),
        ),
    ])
}

fn result_to_pickle(result: &ProfilingResult) -> PickleValue {
    PickleValue::Dict(vec![
        (pickle_key("name"), PickleValue::String(result.name.clone())),
        (
            pickle_key("super"),
            result.parent.as_ref().map_or(PickleValue::None, |parent| {
                PickleValue::String(parent.clone())
            }),
        ),
        (
            pickle_key("stats_all"),
            stats_to_pickle(Some(&result.stats_all)),
        ),
        (
            pickle_key("stats_1m"),
            stats_to_pickle(result.stats_1m.as_ref()),
        ),
        (
            pickle_key("stats_5m"),
            stats_to_pickle(result.stats_5m.as_ref()),
        ),
        (
            pickle_key("stats_30m"),
            stats_to_pickle(result.stats_30m.as_ref()),
        ),
        (
            pickle_key("stats_60m"),
            stats_to_pickle(result.stats_60m.as_ref()),
        ),
    ])
}

/// Snapshot results in the dictionary shape used by Python's local RPC.
pub fn results_pickle() -> Option<PickleValue> {
    let results = results();
    (!results.is_empty()).then(|| {
        PickleValue::Dict(
            results
                .iter()
                .map(|(tag, result)| (PickleValue::String(tag.clone()), result_to_pickle(result)))
                .collect(),
        )
    })
}

fn stats_to_msgpack(stats: Option<&ProfilingStats>) -> Value {
    let Some(stats) = stats else {
        return Value::Nil;
    };
    Value::Map(vec![
        (Value::Str("count".into()), Value::UInt(stats.count as u64)),
        (Value::Str("mean".into()), Value::Float(stats.mean)),
        (Value::Str("median".into()), Value::Float(stats.median)),
        (Value::Str("min".into()), Value::Float(stats.min)),
        (Value::Str("max".into()), Value::Float(stats.max)),
        (
            Value::Str("stdev".into()),
            stats.stdev.map_or(Value::Nil, Value::Float),
        ),
        (Value::Str("sum".into()), Value::Float(stats.sum)),
        (
            Value::Str("threads".into()),
            stats
                .threads
                .map_or(Value::Nil, |threads| Value::UInt(threads as u64)),
        ),
    ])
}

fn result_to_msgpack(result: &ProfilingResult) -> Value {
    Value::Map(vec![
        (Value::Str("name".into()), Value::Str(result.name.clone())),
        (
            Value::Str("super".into()),
            result
                .parent
                .as_ref()
                .map_or(Value::Nil, |parent| Value::Str(parent.clone())),
        ),
        (
            Value::Str("stats_all".into()),
            stats_to_msgpack(Some(&result.stats_all)),
        ),
        (
            Value::Str("stats_1m".into()),
            stats_to_msgpack(result.stats_1m.as_ref()),
        ),
        (
            Value::Str("stats_5m".into()),
            stats_to_msgpack(result.stats_5m.as_ref()),
        ),
        (
            Value::Str("stats_30m".into()),
            stats_to_msgpack(result.stats_30m.as_ref()),
        ),
        (
            Value::Str("stats_60m".into()),
            stats_to_msgpack(result.stats_60m.as_ref()),
        ),
    ])
}

/// Snapshot results in the dictionary shape used by remote management.
pub fn results_msgpack() -> Value {
    let results = results();
    if results.is_empty() {
        Value::Nil
    } else {
        Value::Map(
            results
                .iter()
                .map(|(tag, result)| (Value::Str(tag.clone()), result_to_msgpack(result)))
                .collect(),
        )
    }
}

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

    fn record(tag: &str, started: f64, duration: f64, limit: usize) {
        let id = begin_capture(tag, None, limit, started, std::thread::current().id()).unwrap();
        complete_capture(tag, id, started + duration);
    }

    #[test]
    fn retention_limit_is_shared_across_threads() {
        let tag = "entry113.shared-limit";
        let barrier = std::sync::Arc::new(std::sync::Barrier::new(2));
        let handles: Vec<_> = (0..2)
            .map(|thread_index| {
                let barrier = barrier.clone();
                std::thread::spawn(move || {
                    for sample in 0..3 {
                        barrier.wait();
                        record(
                            tag,
                            elapsed_now(),
                            (thread_index * 10 + sample + 1) as f64,
                            3,
                        );
                        barrier.wait();
                    }
                })
            })
            .collect();
        for handle in handles {
            handle.join().unwrap();
        }

        let result = &results()[tag];
        assert_eq!(result.stats_all.count, 3);
        assert_eq!(result.stats_all.threads, Some(2));
    }

    #[test]
    fn reentrant_guards_complete_independent_captures() {
        let outer = profile("entry113.reentrant");
        let inner = profile("entry113.reentrant");
        drop(inner);
        assert_eq!(results()["entry113.reentrant"].stats_all.count, 1);
        drop(outer);
        assert_eq!(results()["entry113.reentrant"].stats_all.count, 2);
    }

    #[test]
    fn live_windows_are_non_overlapping() {
        let tag = "entry113.windows";
        let now = elapsed_now();
        record(tag, now - 30.0, 0.001, 10);
        record(tag, now - 120.0, 0.002, 10);
        record(tag, now - 600.0, 0.003, 10);
        record(tag, now - 2_400.0, 0.004, 10);

        let result = &results()[tag];
        assert_eq!(result.stats_all.count, 4);
        assert!((result.stats_all.sum - 0.010).abs() < 1e-9);
        assert_eq!(result.stats_1m.as_ref().unwrap().count, 1);
        assert!((result.stats_1m.as_ref().unwrap().sum - 0.001).abs() < 1e-9);
        assert!((result.stats_5m.as_ref().unwrap().sum - 0.002).abs() < 1e-9);
        assert!((result.stats_30m.as_ref().unwrap().sum - 0.003).abs() < 1e-9);
        assert!((result.stats_60m.as_ref().unwrap().sum - 0.004).abs() < 1e-9);
    }

    #[test]
    fn rpc_shape_moves_counts_into_statistics() {
        record("entry113.shape", elapsed_now(), 0.005, 10);
        let pickle = results_pickle().unwrap();
        let entry = pickle.get("entry113.shape").unwrap();
        assert!(entry.get("count").is_none());
        assert!(entry.get("threads").is_none());
        let all = entry.get("stats_all").unwrap();
        assert_eq!(all.get("count").and_then(PickleValue::as_int), Some(1));
        assert_eq!(all.get("threads").and_then(PickleValue::as_int), Some(1));
        let sum = all.get("sum").and_then(PickleValue::as_float).unwrap();
        assert!((sum - 0.005).abs() < 1e-9);
    }

    #[test]
    fn function_adapter_preserves_result_and_records_unwind() {
        assert_eq!(profile_function("entry113.function", || 42), 42);
        let unwind = std::panic::catch_unwind(|| {
            profile_function("entry113.unwind", || panic!("profiled failure"));
        });
        assert!(unwind.is_err());
        assert_eq!(results()["entry113.unwind"].stats_all.count, 1);
    }

    #[test]
    fn merged_live_profiling_pipeline_retains_final_schema() {
        profile_function("entry114.merge", || ());
        let Value::Map(tags) = results_msgpack() else {
            panic!("merged profiler results must be a MessagePack map");
        };
        let Value::Map(entry) = tags
            .iter()
            .find(|(key, _)| *key == Value::Str("entry114.merge".into()))
            .map(|(_, value)| value)
            .unwrap()
        else {
            panic!("merged profiler entry must be a map");
        };
        for key in [
            "name",
            "super",
            "stats_all",
            "stats_1m",
            "stats_5m",
            "stats_30m",
            "stats_60m",
        ] {
            assert!(entry
                .iter()
                .any(|(candidate, _)| *candidate == Value::Str(key.into())));
        }
    }
}