Skip to main content

nros_node/executor/
trace_marker.rs

1//! Application trace markers: one `(marker_id, arg)` event that the
2//! APPLICATION places, emitted through the same installed sink the executor's
3//! own callback events go to.
4//!
5//! The id space is shared with the executor's events, so the application gets
6//! a reserved block of its own:
7//!
8//! | ids        | owner                                                  |
9//! |------------|--------------------------------------------------------|
10//! | 0          | reserved: what a zeroed id reads as; never emitted     |
11//! | 1 ..= 15   | the APPLICATION ([`APP_MARKER_FIRST`]..=[`APP_MARKER_LAST`]) |
12//! | 16 and up  | the executor (16-27 today; the block grows upward)     |
13//!
14//! An id outside the application block is REFUSED, never clamped: a clamped
15//! id would land on another marker's meaning, and a capture with confidently
16//! wrong labels is worse than one with a gap.
17//!
18//! The range check and the error type are ungated, so a caller needs no `cfg`
19//! and an image built without callback tracing still validates its ids. The
20//! emission itself only happens with the `trace-callbacks` feature and an
21//! installed sink. With no sink installed, [`emit_app_marker`] costs the range
22//! compare, one atomic load and one branch; without the feature, the range
23//! compare alone.
24
25// Issue 2641 (GitHub #4009). Why 1..=15: `callback_trace` started the
26// executor's block at 16 "rather than 8 so those keep room to grow
27// contiguously" -- ids 1..7 were already the safety island's hand-placed
28// phase markers (`sys_trace_app_marker` called directly), and 8..15 is the
29// room that sentence left them. Choosing that block means every capture
30// already taken with ids 1..7 keeps its meaning, and no executor id (16-27,
31// growing upward) can ever be reached by an application call.
32
33/// First application marker id.
34pub const APP_MARKER_FIRST: u32 = 1;
35/// Last application marker id. Ids from 16 up belong to the executor.
36pub const APP_MARKER_LAST: u32 = 15;
37
38/// An application marker id outside
39/// [`APP_MARKER_FIRST`]..=[`APP_MARKER_LAST`]. Carries the refused id.
40#[derive(Debug, Clone, Copy, PartialEq, Eq)]
41pub struct AppMarkerIdOutOfRange(pub u32);
42
43impl core::fmt::Display for AppMarkerIdOutOfRange {
44    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
45        write!(
46            f,
47            "trace marker id {} is outside the application range {}..={}; ids from {} up \
48             belong to the executor",
49            self.0,
50            APP_MARKER_FIRST,
51            APP_MARKER_LAST,
52            APP_MARKER_LAST + 1
53        )
54    }
55}
56
57/// Whether `id` is an application marker id.
58#[inline]
59pub const fn is_app_marker(id: u32) -> bool {
60    id >= APP_MARKER_FIRST && id <= APP_MARKER_LAST
61}
62
63/// Emit one application event `(id, arg)` through the installed trace sink.
64///
65/// `id` must be in [`APP_MARKER_FIRST`]..=[`APP_MARKER_LAST`]; any other id is
66/// refused with [`AppMarkerIdOutOfRange`] and nothing is emitted. With no sink
67/// installed, or in a build without the `trace-callbacks` feature, an in-range
68/// call emits nothing and returns `Ok`.
69///
70/// Safe from any thread and from inside a callback: it reads one atomic and
71/// calls the sink, whose contract already covers being called from every
72/// thread that dispatches.
73#[inline]
74pub fn emit_app_marker(id: u32, arg: u32) -> Result<(), AppMarkerIdOutOfRange> {
75    if !is_app_marker(id) {
76        return Err(AppMarkerIdOutOfRange(id));
77    }
78    #[cfg(all(any(has_rmw, test), feature = "trace-callbacks"))]
79    super::callback_trace::app_marker(id, arg);
80    #[cfg(not(all(any(has_rmw, test), feature = "trace-callbacks")))]
81    let _ = arg;
82    Ok(())
83}
84
85#[cfg(test)]
86mod tests {
87    use super::*;
88
89    #[test]
90    fn the_application_block_is_below_every_executor_id() {
91        assert_eq!(APP_MARKER_FIRST, 1);
92        assert!(!is_app_marker(0));
93        assert!(is_app_marker(1) && is_app_marker(15));
94        // 16-27 are the executor's (register .. take stamp), and the block
95        // grows upward from there.
96        for id in 16..=27 {
97            assert!(!is_app_marker(id), "{id} is an executor id");
98            assert_eq!(emit_app_marker(id, 0), Err(AppMarkerIdOutOfRange(id)));
99        }
100        assert_eq!(
101            emit_app_marker(u32::MAX, 0),
102            Err(AppMarkerIdOutOfRange(u32::MAX))
103        );
104    }
105}