Skip to main content

hazard3_executor/raw/
trace.rs

1//! # Tracing
2//!
3//! The `trace` feature enables a number of callbacks that can be used to track the
4//! lifecycle of tasks and/or executors.
5//!
6//! The passed IDs are only guaranteed to be unique for the duration of time the item is valid. If a task
7//! ends, and is re-spawned, it MAY or MAY NOT have the same ID. While a task is active, the id will not change.
8//! For executors, the same applies, but the IDs will be stable for practical embedded programs.
9//!
10//! Callbacks can be used by enabling the `trace` feature, implementing the [`Trace`]
11//! trait, and registering the implementation with the [`trace_impl!`](crate::trace_impl) macro.
12//! All callbacks must be implemented.
13//!
14//! ## Task Tracing lifecycle
15//!
16//! ```text
17//! ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
18//!        │(1)                                            │
19//! │      │
20//!   ╔════▼════╗ (2) ┌─────────┐ (3) ┌─────────┐          │
21//! │ ║ SPAWNED ║────▶│ WAITING │────▶│ RUNNING │
22//!   ╚═════════╝     └─────────┘     └─────────┘          │
23//! │                 ▲         ▲     │    │    │
24//!                   │           (4)      │    │(6)       │
25//! │                 │(7)      └ ─ ─ ┘    │    │
26//!                   │                    │    │          │
27//! │             ┌──────┐             (5) │    │  ┌─────┐
28//!               │ IDLE │◀────────────────┘    └─▶│ END │ │
29//! │             └──────┘                         └─────┘
30//!   ┌──────────────────────┐                             │
31//! └ ┤ Task Trace Lifecycle │─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
32//!   └──────────────────────┘
33//! ```
34//!
35//! 1. A task is spawned, [`task_new`](Trace::task_new) is called
36//! 2. A task is enqueued for the first time, [`task_ready_begin`](Trace::task_ready_begin) is called
37//! 3. A task is polled, [`task_exec_begin`](Trace::task_exec_begin) is called
38//! 4. WHILE a task is polled, the task is re-awoken, and [`task_ready_begin`](Trace::task_ready_begin) is
39//!      called. The task does not IMMEDIATELY move state, until polling is complete and the
40//!      RUNNING state is exited. [`task_exec_end`](Trace::task_exec_end) is called when polling is
41//!      complete, marking the transition to WAITING
42//! 5. Polling is complete, [`task_exec_end`](Trace::task_exec_end) is called
43//! 6. The task has completed, and [`task_end`](Trace::task_end) is called
44//! 7. A task is awoken, [`task_ready_begin`](Trace::task_ready_begin) is called
45//!
46//! ## Executor Tracing lifecycle
47//!
48//! ```text
49//! ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
50//!       │(1)                                             │
51//! │     │
52//!   ╔═══▼══╗   (2)     ┌────────────┐  (3)  ┌─────────┐  │
53//! │ ║ IDLE ║──────────▶│ SCHEDULING │──────▶│ POLLING │
54//!   ╚══════╝           └────────────┘       └─────────┘  │
55//! │     ▲              │            ▲            │
56//!       │      (5)     │            │  (4)       │       │
57//! │     └──────────────┘            └────────────┘
58//!   ┌──────────────────────────┐                         │
59//! └ ┤ Executor Trace Lifecycle │─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
60//!   └──────────────────────────┘
61//! ```
62//!
63//! 1. The executor is started (no associated trace)
64//! 2. A task on this executor is awoken. [`task_ready_begin`](Trace::task_ready_begin) is called
65//!      when this occurs, and [`poll_start`](Trace::poll_start) is called when the executor
66//!      actually begins running
67//! 3. The executor has decided a task to poll. [`task_exec_begin`](Trace::task_exec_begin) is called
68//! 4. The executor finishes polling the task. [`task_exec_end`](Trace::task_exec_end) is called
69//! 5. The executor has finished polling tasks. [`executor_idle`](Trace::executor_idle) is called
70//!
71//! ## Idle
72//!
73//! [`executor_idle`](Trace::executor_idle) only means that a single executor has run out of work. With
74//! multiple executors (e.g. a thread-mode executor plus one or more interrupt
75//! executors), an interrupt executor going idle returns to the preempted
76//! lower-priority context, which keeps running. The current thread/core is only
77//! idle when its thread-mode executor reaches its sleep site, at which point
78//! [`idle`](Trace::idle) is called. In multi-core chips, or when using threads under std or an
79//! RTOS, this does not mean the entire system is idle.
80
81use crate::ExecutorId;
82use crate::raw::TaskRef;
83
84hazard3_unitrait::unitrait! {
85    /// Executor trace hooks.
86    ///
87    /// Implement this trait and register the implementation with the
88    /// [`trace_impl!`](crate::trace_impl) macro to receive callbacks on task and executor
89    /// lifecycle events. All callbacks must be implemented.
90    ///
91    /// See the [module documentation](crate::raw::trace) for the task and executor tracing lifecycles.
92    #[symbol_prefix = "_embassy_trace_v2"]
93    pub trait Trace {
94        /// This callback is called when the executor begins polling. This will always
95        /// be paired with a later call to [`executor_idle`](Self::executor_idle).
96        ///
97        /// This marks the EXECUTOR state transition from IDLE -> SCHEDULING.
98        fn poll_start(executor: ExecutorId);
99
100        /// This callback is called AFTER a task is initialized/allocated, and BEFORE
101        /// it is enqueued to run for the first time. If the task ends (and does not
102        /// loop "forever"), there will be a matching call to [`task_end`](Self::task_end).
103        ///
104        /// Tasks start life in the SPAWNED state.
105        fn task_new(executor: ExecutorId, task: TaskRef);
106
107        /// This callback is called AFTER a task is destructed/freed. This will always
108        /// have a prior matching call to [`task_new`](Self::task_new).
109        fn task_end(executor: ExecutorId, task: TaskRef);
110
111        /// This callback is called AFTER a task has been dequeued from the runqueue,
112        /// and BEFORE the task is polled. There will always be a matching call to
113        /// [`task_exec_end`](Self::task_exec_end).
114        ///
115        /// This marks the TASK state transition from WAITING -> RUNNING
116        /// This marks the EXECUTOR state transition from SCHEDULING -> POLLING
117        fn task_exec_begin(executor: ExecutorId, task: TaskRef);
118
119        /// This callback is called AFTER a task has completed polling. There will
120        /// always be a matching call to [`task_exec_begin`](Self::task_exec_begin).
121        ///
122        /// This marks the TASK state transition from either:
123        /// * RUNNING -> IDLE - if there were no [`task_ready_begin`](Self::task_ready_begin) events
124        ///     for this task since the last [`task_exec_begin`](Self::task_exec_begin) for THIS task
125        /// * RUNNING -> WAITING - if there WAS a [`task_ready_begin`](Self::task_ready_begin) event
126        ///     for this task since the last [`task_exec_begin`](Self::task_exec_begin) for THIS task
127        ///
128        /// This marks the EXECUTOR state transition from POLLING -> SCHEDULING
129        fn task_exec_end(executor: ExecutorId, task: TaskRef);
130
131        /// This callback is called AFTER the waker for a task is awoken, and BEFORE it
132        /// is added to the run queue.
133        ///
134        /// If the given task is currently RUNNING, this marks no state change, BUT the
135        /// RUNNING task will then move to the WAITING stage when polling is complete.
136        ///
137        /// If the given task is currently IDLE, this marks the TASK state transition
138        /// from IDLE -> WAITING.
139        ///
140        /// NOTE: This may be called from an interrupt, outside the context of the current
141        /// task or executor.
142        fn task_ready_begin(executor: ExecutorId, task: TaskRef);
143
144        /// This callback is called AFTER all dequeued tasks in a single call to poll
145        /// have been processed. This will always be paired with a call to
146        /// [`poll_start`](Self::poll_start).
147        ///
148        /// This marks the EXECUTOR state transition from SCHEDULING -> IDLE
149        fn executor_idle(executor: ExecutorId);
150
151        /// This callback is called right before the thread-mode executor puts the
152        /// current thread/core to sleep (e.g. `wfe`/`wfi`).
153        ///
154        /// Unlike [`executor_idle`](Self::executor_idle), this is never called by interrupt executors,
155        /// since they return to a preempted context after polling and the system
156        /// keeps running.
157        ///
158        /// Note in multi-core chips, or when using threads under std or an RTOS, this
159        /// doesn't mean the entire system is idle, it only means the current core/thread is.
160        fn idle();
161
162        /// This callback is called AFTER the name of a task is set.
163        ///
164        /// This function can be called when the task is not running and it does not signal a state change.
165        ///
166        /// The default name from `#[task]` and `#[main]` is set when the task is initialized,
167        /// without this callback: read it with `task.metadata().name()` in
168        /// [`task_new`](Self::task_new).
169        fn task_name_set(task: TaskRef, name: &'static str);
170
171        /// This callback is called AFTER the priority of a task is set.
172        ///
173        /// This function can be called when the task is not running and it does not signal a state change.
174        fn task_priority_set(task: TaskRef, priority: u8);
175
176        /// This callback is called AFTER the deadline of a task is set.
177        ///
178        /// This function can be called when the task is not running and it does not signal a state change.
179        fn task_deadline_set(task: TaskRef, deadline: u64);
180    }
181
182    /// The global [`Trace`] implementation.
183    pub(crate) struct TraceImpl;
184
185    /// Register a type as the global executor trace hook implementation.
186    ///
187    /// See [`raw::trace::Trace`](crate::raw::trace::Trace).
188    macro trace_impl(path = $crate::raw::trace);
189}