rtsched 0.2.0

A no_std runtime scheduler for Cortex-M thread management
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
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 kwangdo.yi

//! Diagnostic helpers for ktimer queue inspection and deadline misses.

use core::ptr;

use crate::ktimer::{
    KTimerEntity, KTimerQueue, is_cfs_ktimer, is_wait_ktimer, ktimer_name, with_ktimer_queue,
};
use crate::sync::SyncType;
use crate::thread::{
    CfsThread, RtThread, ThreadCtx, ThreadHandle, ThreadRef, ThreadState, cfs_thread_from_handle,
    rt_ktimer_entity, rt_thread_from_handle,
};

pub fn traverse_ktimer_queue() {
    with_ktimer_queue(|queue| unsafe {
        let mut entity = queue.first();

        crate::rtsched_println!("ktimer queue:");
        while !entity.is_null() {
            crate::rtsched_println!(
                "{} ktimer's remaining={}, active={}",
                ktimer_name(entity),
                (*entity).remaining_at(queue.now_ticks()),
                (*entity).is_active()
            );
            entity = queue.next(entity);
        }
    });
}

/// Traverse the ktimer queue and invoke `f` for each ktimer with its name
/// and remaining ticks. This is similar to `traverse_ktimer_queue` but allows the
/// caller to handle formatting/output (for example, writing to UART).
pub fn traverse_ktimer_queue_fn<F>(mut f: F)
where
    F: FnMut(&'static str, u32),
{
    with_ktimer_queue(|queue| unsafe {
        let mut entity = queue.first();

        while !entity.is_null() {
            f(
                ktimer_name(entity),
                (*entity).remaining_at(queue.now_ticks()),
            );
            entity = queue.next(entity);
        }
    });
}

pub(crate) unsafe fn record_rt_deadline_miss(
    queue: &KTimerQueue,
    entity: *mut KTimerEntity,
    rt_thread: *mut RtThread,
) {
    unsafe {
        let thread_name = (*rt_thread).thread.name;
        let runtime = (*rt_thread).runtime;
        let relative_deadline = (*entity).relative_deadline_ticks();

        crate::diagnostics::trace::record_deadline_miss(
            ptr::addr_of!((*rt_thread).thread),
            runtime,
            relative_deadline,
        );
        let rt_ktimer = KTimerEntity::container_of(entity);
        (*rt_ktimer).miss_cnt = (*rt_ktimer).miss_cnt.saturating_add(1);
        crate::rtsched_println!(
            "Deadline miss in thread '{}': timer expired at relative deadline {} ticks (runtime {} ticks)",
            thread_name,
            relative_deadline,
            runtime
        );
        print_rt_deadline_miss_diagnostics(queue, entity, rt_thread);
        panic!("RT deadline miss in thread '{}'", thread_name);
    }
}

unsafe fn print_rt_deadline_miss_diagnostics(
    queue: &KTimerQueue,
    missed_entity: *mut KTimerEntity,
    missed_thread: *mut RtThread,
) {
    unsafe {
        crate::rtsched_println!("rt deadline miss diagnostics:");
        print_ktimer_queue_statistics(queue, missed_entity);
        print_thread_statistics(queue, missed_entity, missed_thread);
    }
}

unsafe fn print_ktimer_queue_statistics(queue: &KTimerQueue, missed_entity: *mut KTimerEntity) {
    unsafe {
        crate::rtsched_println!(
            "ktimer queue statistics: now={} len={} first_active='{}'",
            queue.now_ticks(),
            queue.len(),
            ktimer_name_or_none(queue.first_active())
        );

        let mut entity = queue.first();
        let mut printed_missed = false;
        while !entity.is_null() {
            let is_missed = ptr::eq(entity.cast_const(), missed_entity.cast_const());
            if is_missed {
                printed_missed = true;
            }
            print_ktimer_statistics(queue, entity, if is_missed { "*" } else { " " }, true);
            entity = queue.next(entity);
        }

        if !missed_entity.is_null() && !printed_missed {
            crate::rtsched_println!("  * expired timer was already removed from the queue");
            print_ktimer_statistics(queue, missed_entity, "*", false);
        }
    }
}

unsafe fn print_ktimer_statistics(
    queue: &KTimerQueue,
    entity: *mut KTimerEntity,
    marker: &'static str,
    queued: bool,
) {
    unsafe {
        if !is_cfs_ktimer(entity) && !is_wait_ktimer(entity) {
            let rt_ktimer = KTimerEntity::container_of(entity);
            crate::rtsched_println!(
                "  {} name='{}' kind={} queued={} expire_at={} remaining={} active={} misses={}",
                marker,
                ktimer_name(entity),
                ktimer_kind_name(entity),
                yes_no(queued),
                (*entity).expire_at(),
                (*entity).remaining_at(queue.now_ticks()),
                yes_no((*entity).is_active()),
                (*rt_ktimer).miss_cnt
            );

            let thread_ctx = (*rt_ktimer).thread_ctx();
            if thread_ctx.is_null() {
                crate::rtsched_println!(
                    "    rt timing: period={} relative_deadline={} budget={} thread=<none>",
                    (*entity).period_ticks(),
                    (*entity).relative_deadline_ticks(),
                    (*entity).budget_ticks()
                );
            } else {
                let rt_thread = rt_thread_from_handle(ThreadHandle::from_thread_ctx(thread_ctx));
                crate::rtsched_println!(
                    "    rt timing: period={} relative_deadline={} budget={} thread_id={} thread_state={} runtime={}",
                    (*entity).period_ticks(),
                    (*entity).relative_deadline_ticks(),
                    (*entity).budget_ticks(),
                    (*thread_ctx).id,
                    thread_state_name((*thread_ctx).state),
                    (*rt_thread).runtime
                );
            }
        } else {
            crate::rtsched_println!(
                "  {} name='{}' kind={} queued={} expire_at={} remaining={} active={}",
                marker,
                ktimer_name(entity),
                ktimer_kind_name(entity),
                yes_no(queued),
                (*entity).expire_at(),
                (*entity).remaining_at(queue.now_ticks()),
                yes_no((*entity).is_active())
            );
        }
    }
}

unsafe fn print_thread_statistics(
    queue: &KTimerQueue,
    missed_entity: *mut KTimerEntity,
    missed_thread: *mut RtThread,
) {
    unsafe {
        crate::rtsched_println!("thread statistics:");
        print_rt_thread_statistics(
            "  ",
            "missed",
            &*missed_thread,
            missed_entity,
            queue.now_ticks(),
        );
        print_current_thread_statistics(queue.now_ticks());
        print_idle_thread_statistics();
        print_cfs_thread_statistics_list();
        print_wait_thread_statistics_list();
    }
}

unsafe fn print_current_thread_statistics(now_ticks: u64) {
    unsafe {
        let current = crate::sched::CURRENT_THREAD_CTX;
        if current.is_null() {
            crate::rtsched_println!("  current: <none>");
            return;
        }

        if crate::sched::is_idle_thread(current) {
            print_basic_thread_statistics("  ", "current", &*current, "idle");
            return;
        }

        let current = ThreadHandle::from_thread_ctx(current);
        if crate::sched::CURRENT_THREAD_IS_CFS {
            print_cfs_thread_statistics("  ", "current", &*cfs_thread_from_handle(current));
        } else {
            print_rt_thread_statistics(
                "  ",
                "current",
                &*rt_thread_from_handle(current),
                rt_ktimer_entity(current),
                now_ticks,
            );
        }
    }
}

unsafe fn print_idle_thread_statistics() {
    unsafe {
        let idle = crate::sched::IDLE_THREAD_CTX;
        if idle.is_null() {
            crate::rtsched_println!("  idle: <none>");
            return;
        }

        print_basic_thread_statistics("  ", "idle", &*idle, "idle");
    }
}

fn print_cfs_thread_statistics_list() {
    crate::rtsched_println!("  cfs threads:");
    let mut saw_thread = false;
    crate::diagnostics::runq::traverse_run_queue_fn(|thread| {
        saw_thread = true;
        print_cfs_thread_statistics("    ", "cfs", thread);
    });
    if !saw_thread {
        crate::rtsched_println!("    <empty>");
    }
}

fn print_wait_thread_statistics_list() {
    crate::rtsched_println!("  wait queue:");
    let mut saw_thread = false;
    crate::diagnostics::waitq::traverse_wait_queue_fn(|thread| {
        saw_thread = true;
        match thread {
            ThreadRef::Cfs(thread) => {
                let (wait_ticks, waitevt) = thread.wait_info();
                print_wait_thread_statistics(
                    "    ",
                    "wait",
                    thread.thread_ctx(),
                    "cfs",
                    wait_ticks,
                    waitevt,
                    None,
                );
            }
            ThreadRef::Rt(thread) => {
                let (wait_ticks, waitevt) = thread.wait_info();
                print_wait_thread_statistics(
                    "    ",
                    "wait",
                    thread.thread_ctx(),
                    "rt",
                    wait_ticks,
                    waitevt,
                    Some(thread.runtime()),
                );
            }
        }
    });
    if !saw_thread {
        crate::rtsched_println!("    <empty>");
    }
}

fn print_cfs_thread_statistics(indent: &str, label: &str, thread: &CfsThread) {
    let thread_ctx = thread.thread_ctx();
    let sched_info = thread.sched_info();
    crate::rtsched_println!(
        "{}{}: id={} name='{}' class=cfs state={} priority={} sched_ticks={} vruntime={}",
        indent,
        label,
        thread_ctx.id,
        thread_ctx.name,
        thread_state_name(thread_ctx.state),
        sched_info.priority,
        sched_info.sched_tick_cnt,
        sched_info.vruntime
    );
}

fn print_basic_thread_statistics(
    indent: &str,
    label: &str,
    thread_ctx: &ThreadCtx,
    class: &'static str,
) {
    crate::rtsched_println!(
        "{}{}: id={} name='{}' class={} state={}",
        indent,
        label,
        thread_ctx.id,
        thread_ctx.name,
        class,
        thread_state_name(thread_ctx.state)
    );
}

fn print_rt_thread_statistics(
    indent: &str,
    label: &str,
    thread: &RtThread,
    ktimer_entity: *mut KTimerEntity,
    now_ticks: u64,
) {
    let thread_ctx = thread.thread_ctx();
    if ktimer_entity.is_null() {
        crate::rtsched_println!(
            "{}{}: id={} name='{}' class=rt state={} runtime={} ktimer=<none>",
            indent,
            label,
            thread_ctx.id,
            thread_ctx.name,
            thread_state_name(thread_ctx.state),
            thread.runtime()
        );
        return;
    }

    unsafe {
        let rt_ktimer = KTimerEntity::container_of(ktimer_entity);
        crate::rtsched_println!(
            "{}{}: id={} name='{}' class=rt state={} runtime={} ktimer='{}' expire_at={} remaining={} active={} misses={}",
            indent,
            label,
            thread_ctx.id,
            thread_ctx.name,
            thread_state_name(thread_ctx.state),
            thread.runtime(),
            ktimer_name(ktimer_entity),
            (*ktimer_entity).expire_at(),
            (*ktimer_entity).remaining_at(now_ticks),
            yes_no((*ktimer_entity).is_active()),
            (*rt_ktimer).miss_cnt
        );
    }
}

fn print_wait_thread_statistics(
    indent: &str,
    label: &str,
    thread_ctx: &ThreadCtx,
    class: &'static str,
    wait_ticks: u32,
    waitevt: Option<SyncType>,
    runtime: Option<u32>,
) {
    if let Some(runtime) = runtime {
        crate::rtsched_println!(
            "{}{}: id={} name='{}' class={} state={} runtime={} wait_ticks={} waitevt={}",
            indent,
            label,
            thread_ctx.id,
            thread_ctx.name,
            class,
            thread_state_name(thread_ctx.state),
            runtime,
            wait_ticks,
            sync_type_name(waitevt)
        );
    } else {
        crate::rtsched_println!(
            "{}{}: id={} name='{}' class={} state={} wait_ticks={} waitevt={}",
            indent,
            label,
            thread_ctx.id,
            thread_ctx.name,
            class,
            thread_state_name(thread_ctx.state),
            wait_ticks,
            sync_type_name(waitevt)
        );
    }
}

unsafe fn ktimer_name_or_none(entity: *const KTimerEntity) -> &'static str {
    if entity.is_null() {
        "<none>"
    } else {
        unsafe { ktimer_name(entity) }
    }
}

fn ktimer_kind_name(entity: *const KTimerEntity) -> &'static str {
    if is_cfs_ktimer(entity) {
        "cfs"
    } else if is_wait_ktimer(entity) {
        "wait"
    } else {
        "rt"
    }
}

fn thread_state_name(state: ThreadState) -> &'static str {
    match state {
        ThreadState::ReadyActive => "ReadyActive",
        ThreadState::ReadyInactive => "ReadyInactive",
        ThreadState::Running => "Running",
        ThreadState::Waiting => "Waiting",
        ThreadState::Suspended => "Suspended",
    }
}

fn sync_type_name(waitevt: Option<SyncType>) -> &'static str {
    match waitevt {
        Some(SyncType::BinarySemaphore) => "binary-semaphore",
        Some(SyncType::CountingSemaphore) => "counting-semaphore",
        Some(SyncType::Mutex) => "mutex",
        Some(SyncType::MessageQueueSend) => "message-queue-send",
        Some(SyncType::MessageQueueReceive) => "message-queue-receive",
        None => "none",
    }
}

fn yes_no(value: bool) -> &'static str {
    if value { "yes" } else { "no" }
}