rt 0.19.0

A real-time operating system capable of full preemption
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
#include <rt/trace.h>

#include <rt/atomic.h>
#include <rt/event.h>
#include <rt/log.h>
#include <rt/mutex.h>
#include <rt/notify.h>
#include <rt/queue.h>
#include <rt/sem.h>
#include <rt/syscall.h>
#include <rt/task.h>
#include <rt/timer.h>

#if RT_LOG_ENABLE
#include <inttypes.h>
#endif

#if RT_TRACE_ENABLE
#ifndef RT_TRACE_NUM_EVENTS
#define RT_TRACE_NUM_EVENTS 128
#endif

#if RT_TRACE_NUM_EVENTS < 32
#error "Must have at least 32 trace event slots."
#endif

static struct rt_trace_event rt_trace_events[RT_TRACE_NUM_EVENTS];

static rt_atomic_uint32_t rt_trace_seq = 0;
static rt_atomic_size_t rt_trace_missed = 0;

#define RT_TRACE_SEND_THRESHOLD (RT_TRACE_NUM_EVENTS / 2)

static struct rt_trace_event *rt_trace_pop(void)
{
    const uint32_t cycle = rt_cycle();
    const uint32_t seq =
        rt_atomic_fetch_add(&rt_trace_seq, 1, RT_ATOMIC_RELAXED);
    struct rt_trace_event *const ev =
        &rt_trace_events[seq % RT_TRACE_NUM_EVENTS];
    enum rt_trace_event_state state = RT_TRACE_EVENT_STATE_EMPTY;
    if (!rt_atomic_compare_exchange(&ev->state, &state,
                                    RT_TRACE_EVENT_STATE_IN_PROGRESS,
                                    RT_ATOMIC_RELAXED, RT_ATOMIC_RELAXED))
    {
        rt_atomic_fetch_add(&rt_trace_missed, 1, RT_ATOMIC_RELAXED);
        return NULL;
    }
    ev->cycle = cycle;
    ev->seq = seq;
    return ev;
}

static RT_SEM_BINARY(rt_trace_send_sem);

static void rt_trace_push(struct rt_trace_event *ev)
{
    const bool should_post = ((ev->seq + 1) % RT_TRACE_SEND_THRESHOLD) == 0;
    rt_atomic_store(&ev->state, RT_TRACE_EVENT_STATE_FULL, RT_ATOMIC_RELEASE);
    if (should_post)
    {
        rt_sem_post(&rt_trace_send_sem);
    }
}

void rt_trace_timer_start(const struct rt_timer *timer, unsigned long period)
{
    struct rt_trace_event *const ev = rt_trace_pop();
    if (ev != NULL)
    {
        ev->type = RT_TRACE_EVENT_TIMER_START;
        ev->data.timer.timer = timer;
        ev->data.timer.period = period;
        rt_trace_push(ev);
    }
}

void rt_trace_timer_stop(const struct rt_timer *timer)
{
    struct rt_trace_event *const ev = rt_trace_pop();
    if (ev != NULL)
    {
        ev->type = RT_TRACE_EVENT_TIMER_STOP;
        ev->data.timer.timer = timer;
        rt_trace_push(ev);
    }
}

void rt_trace_timer_change_period(const struct rt_timer *timer,
                                  unsigned long period)
{
    struct rt_trace_event *const ev = rt_trace_pop();
    if (ev != NULL)
    {
        ev->type = RT_TRACE_EVENT_TIMER_CHANGE_PERIOD;
        ev->data.timer.timer = timer;
        ev->data.timer.period = period;
        rt_trace_push(ev);
    }
}

void rt_trace_timer_expire(const struct rt_timer *timer, unsigned long tick)
{
    struct rt_trace_event *const ev = rt_trace_pop();
    if (ev != NULL)
    {
        ev->type = RT_TRACE_EVENT_TIMER_EXPIRE;
        ev->data.timer.timer = timer;
        ev->data.timer.tick = tick;
        rt_trace_push(ev);
    }
}

void rt_trace_flush(void)
{
    rt_sem_post(&rt_trace_send_sem);
}

void rt_trace_syscall_run(enum rt_syscall syscall, uintptr_t arg0,
                          uintptr_t arg1, uintptr_t arg2)
{
    struct rt_trace_event *const ev = rt_trace_pop();
    if (ev != NULL)
    {
        ev->type = RT_TRACE_EVENT_SYSCALL_RUN;
        ev->data.syscall_run.syscall = syscall;
        ev->data.syscall_run.arg0 = arg0;
        ev->data.syscall_run.arg1 = arg1;
        ev->data.syscall_run.arg2 = arg2;
        rt_trace_push(ev);
    }
}

void rt_trace_syscall_run_pending(const struct rt_syscall_record *record)
{
    struct rt_trace_event *const ev = rt_trace_pop();
    if (ev != NULL)
    {
        ev->type = RT_TRACE_EVENT_SYSCALL_RUN_PENDING;
        ev->data.syscall_pendable.args = record->args;
        ev->data.syscall_pendable.syscall = record->syscall;
        rt_trace_push(ev);
    }
}

void rt_trace_syscall_pend(const struct rt_syscall_record *record)
{
    struct rt_trace_event *const ev = rt_trace_pop();
    if (ev != NULL)
    {
        ev->type = RT_TRACE_EVENT_SYSCALL_PEND;
        ev->data.syscall_pendable.args = record->args;
        ev->data.syscall_pendable.syscall = record->syscall;
        rt_trace_push(ev);
    }
}

void rt_trace_first_task(const struct rt_task *task)
{
    struct rt_trace_event *const ev = rt_trace_pop();
    if (ev != NULL)
    {
        ev->type = RT_TRACE_EVENT_FIRST_TASK;
        ev->data.first_task = task;
        rt_trace_push(ev);
    }
}

void rt_trace_active_task(const struct rt_task *old_task,
                          const struct rt_task *new_task)
{
    struct rt_trace_event *const ev = rt_trace_pop();
    if (ev != NULL)
    {
        ev->type = RT_TRACE_EVENT_ACTIVE_TASK;
        ev->data.active_task.old_task = old_task;
        ev->data.active_task.new_task = new_task;
        rt_trace_push(ev);
    }
}

void rt_trace_task_state(const struct rt_task *task,
                         enum rt_task_state old_state,
                         enum rt_task_state new_state)
{
    struct rt_trace_event *const ev = rt_trace_pop();
    if (ev != NULL)
    {
        ev->type = RT_TRACE_EVENT_TASK_STATE;
        ev->data.task_state.task = task;
        ev->data.task_state.old_state = old_state;
        ev->data.task_state.new_state = new_state;
        rt_trace_push(ev);
    }
}

void rt_trace_tick(unsigned long tick)
{
    struct rt_trace_event *const ev = rt_trace_pop();
    if (ev != NULL)
    {
        ev->type = RT_TRACE_EVENT_TICK;
        ev->data.tick = tick;
        rt_trace_push(ev);
    }
}

void rt_trace_interrupt_start(int32_t interrupt_number)
{
    struct rt_trace_event *const ev = rt_trace_pop();
    if (ev != NULL)
    {
        ev->type = RT_TRACE_EVENT_INTERRUPT_START;
        ev->data.interrupt_number = interrupt_number;
        rt_trace_push(ev);
    }
}

void rt_trace_interrupt_end(int32_t interrupt_number)
{
    struct rt_trace_event *const ev = rt_trace_pop();
    if (ev != NULL)
    {
        ev->type = RT_TRACE_EVENT_INTERRUPT_END;
        ev->data.interrupt_number = interrupt_number;
        rt_trace_push(ev);
    }
}

void rt_trace_sem_update(const struct rt_sem *sem, int old_value, int new_value)
{
    struct rt_trace_event *const ev = rt_trace_pop();
    if (ev != NULL)
    {
        ev->type = RT_TRACE_EVENT_SEM_UPDATE;
        ev->data.sem.sem = sem;
        ev->data.sem.old_value = old_value;
        ev->data.sem.new_value = new_value;
        rt_trace_push(ev);
    }
}

void rt_trace_event_wait(const struct rt_event *event, uint32_t bits,
                         uint32_t wait)
{
    struct rt_trace_event *const ev = rt_trace_pop();
    if (ev != NULL)
    {
        ev->type = RT_TRACE_EVENT_EVENT_WAIT;
        ev->data.event.event = event;
        ev->data.event.bits = bits;
        ev->data.event.wait_set = wait;
        rt_trace_push(ev);
    }
}

void rt_trace_event_set(const struct rt_event *event, uint32_t bits,
                        uint32_t set)
{
    struct rt_trace_event *const ev = rt_trace_pop();
    if (ev != NULL)
    {
        ev->type = RT_TRACE_EVENT_EVENT_SET;
        ev->data.event.event = event;
        ev->data.event.bits = bits;
        ev->data.event.wait_set = set;
        rt_trace_push(ev);
    }
}

void rt_trace_notify_or(const struct rt_notify *note, uint32_t old_value,
                        uint32_t or_value, uint32_t new_value)
{
    struct rt_trace_event *const ev = rt_trace_pop();
    if (ev != NULL)
    {
        ev->type = RT_TRACE_EVENT_NOTIFY_OR;
        ev->data.notify.note = note;
        ev->data.notify.old_value = old_value;
        ev->data.notify.arg = or_value;
        ev->data.notify.new_value = new_value;
        rt_trace_push(ev);
    }
}

void rt_trace_notify_set(const struct rt_notify *note, uint32_t old_value,
                         uint32_t new_value)
{
    struct rt_trace_event *const ev = rt_trace_pop();
    if (ev != NULL)
    {
        ev->type = RT_TRACE_EVENT_NOTIFY_SET;
        ev->data.notify.note = note;
        ev->data.notify.old_value = old_value;
        ev->data.notify.new_value = new_value;
        rt_trace_push(ev);
    }
}

void rt_trace_notify_clear(const struct rt_notify *note, uint32_t old_value,
                           uint32_t clear_value, uint32_t new_value)
{
    struct rt_trace_event *const ev = rt_trace_pop();
    if (ev != NULL)
    {
        ev->type = RT_TRACE_EVENT_NOTIFY_CLEAR;
        ev->data.notify.note = note;
        ev->data.notify.old_value = old_value;
        ev->data.notify.arg = clear_value;
        ev->data.notify.new_value = new_value;
        rt_trace_push(ev);
    }
}

void rt_trace_mutex_lock(const struct rt_mutex *mutex, uintptr_t holder)
{
    struct rt_trace_event *const ev = rt_trace_pop();
    if (ev != NULL)
    {
        ev->type = RT_TRACE_EVENT_MUTEX_LOCK;
        ev->data.mutex.mutex = mutex;
        ev->data.mutex.holder = holder;
        rt_trace_push(ev);
    }
}

void rt_trace_mutex_lock_fail(const struct rt_mutex *mutex, uintptr_t failer,
                              uintptr_t holder)
{
    struct rt_trace_event *const ev = rt_trace_pop();
    if (ev != NULL)
    {
        ev->type = RT_TRACE_EVENT_MUTEX_LOCK_FAIL;
        ev->data.mutex_lock_fail.mutex = mutex;
        ev->data.mutex_lock_fail.failer = failer;
        ev->data.mutex_lock_fail.holder = holder;
        rt_trace_push(ev);
    }
}

void rt_trace_mutex_unlock(const struct rt_mutex *mutex, uintptr_t holder)
{
    struct rt_trace_event *const ev = rt_trace_pop();
    if (ev != NULL)
    {
        ev->type = RT_TRACE_EVENT_MUTEX_UNLOCK;
        ev->data.mutex.mutex = mutex;
        ev->data.mutex.holder = holder;
        rt_trace_push(ev);
    }
}

static void rt_trace_queue(const struct rt_queue *queue, size_t seq,
                           enum rt_trace_event_type type)
{
    struct rt_trace_event *const ev = rt_trace_pop();
    if (ev != NULL)
    {
        ev->type = type;
        ev->data.queue.queue = queue;
        ev->data.queue.seq = seq;
        rt_trace_push(ev);
    }
}

void rt_trace_queue_push(const struct rt_queue *queue, size_t enq)
{
    rt_trace_queue(queue, enq, RT_TRACE_EVENT_QUEUE_PUSH);
}

void rt_trace_queue_push_skipped(const struct rt_queue *queue, size_t enq)
{
    rt_trace_queue(queue, enq, RT_TRACE_EVENT_QUEUE_PUSH_SKIPPED);
}

void rt_trace_queue_pop(const struct rt_queue *queue, size_t deq)
{
    rt_trace_queue(queue, deq, RT_TRACE_EVENT_QUEUE_POP);
}

void rt_trace_queue_pop_skip(const struct rt_queue *queue, size_t deq)
{
    rt_trace_queue(queue, deq, RT_TRACE_EVENT_QUEUE_POP_SKIP);
}

void rt_trace_queue_peek(const struct rt_queue *queue, size_t deq)
{
    rt_trace_queue(queue, deq, RT_TRACE_EVENT_QUEUE_PEEK);
}

void rt_trace_queue_peek_skip(const struct rt_queue *queue, size_t deq)
{
    rt_trace_queue(queue, deq, RT_TRACE_EVENT_QUEUE_PEEK_SKIP);
}

void rt_trace_end(void)
{
    struct rt_trace_event *const ev = rt_trace_pop();
    if (ev != NULL)
    {
        ev->type = RT_TRACE_EVENT_END;
        rt_trace_push(ev);
    }
    rt_trace_flush();
    rt_task_yield();
}

#if RT_LOG_ENABLE
static inline const char *mutex_holder_name(uintptr_t holder)
{
    holder &= RT_MUTEX_HOLDER_MASK;
    return holder == RT_MUTEX_HOLDER_INTERRUPT
               ? "interrupt"
               : ((const struct rt_task *)holder)->name;
}

__attribute__((weak)) bool
rt_trace_event_write(const struct rt_trace_event *event)
{
    rt_logf("%10" PRIu32 ",@%10" PRIu32 ": ", event->seq, event->cycle);
    switch (event->type)
    {
    case RT_TRACE_EVENT_SYSCALL_RUN:
    {
        const uintptr_t arg0 = event->data.syscall_run.arg0;
        const uintptr_t arg1 = event->data.syscall_run.arg1;
        const uintptr_t arg2 = event->data.syscall_run.arg2;
        rt_logf("syscall: ");
        switch (event->data.syscall_run.syscall)
        {
        case RT_SYSCALL_SLEEP:
            rt_logf("sleep(%" PRIuPTR ")\n", arg0);
            break;
        case RT_SYSCALL_SLEEP_PERIODIC:
            rt_logf("sleep_periodic(%" PRIuPTR ", %" PRIuPTR ")\n", arg0, arg1);
            break;
        case RT_SYSCALL_SEM_WAIT:
            rt_logf("sem_wait(%p)\n", (void *)arg0);
            break;
        case RT_SYSCALL_SEM_TIMEDWAIT:
            rt_logf("sem_timedwait(%p, %" PRIuPTR ")\n", (void *)arg0, arg1);
            break;
        case RT_SYSCALL_SEM_POST:
            if (arg1 == 1)
            {
                rt_logf("sem_post(%p)\n", (void *)arg0);
            }
            else
            {
                rt_logf("sem_post_n(%p, %" PRIuPTR ")\n", (void *)arg0, arg1);
            }
            break;
        case RT_SYSCALL_MUTEX_LOCK:
            rt_logf("mutex_lock(%p)\n", (void *)arg0);
            break;
        case RT_SYSCALL_MUTEX_TIMEDLOCK:
            rt_logf("mutex_timedlock(%p, %" PRIuPTR ")\n", (void *)arg0, arg1);
            break;
        case RT_SYSCALL_MUTEX_UNLOCK:
            rt_logf("mutex_unlock(%p)\n", (void *)arg0);
            break;
        case RT_SYSCALL_EVENT_WAIT:
            /* NOTE: Don't show the waited bits here because they are stored on
             * the stack of the waiting task, pointed to by arg1, and the trace
             * event may outlive the syscall. */
            rt_logf("event_wait(%p)\n", (void *)arg0);
            break;
        case RT_SYSCALL_EVENT_TIMEDWAIT:
            rt_logf("event_timedwait(%p, %" PRIuPTR ")\n", (void *)arg0, arg2);
            break;
        case RT_SYSCALL_EVENT_SET:
            rt_logf("event_set(%p)\n", (void *)arg0);
            break;
        case RT_SYSCALL_YIELD:
            rt_logf("yield()\n");
            break;
        case RT_SYSCALL_EXIT:
            rt_logf("exit()\n");
            break;
        }
        break;
    }
    case RT_TRACE_EVENT_SYSCALL_PEND:
    case RT_TRACE_EVENT_SYSCALL_RUN_PENDING:
    {
        if (event->type == RT_TRACE_EVENT_SYSCALL_PEND)
        {
            rt_logf("pend ");
        }
        else
        {
            rt_logf("run pending ");
        }
        switch (event->data.syscall_pendable.syscall)
        {
        case RT_SYSCALL_PENDABLE_SEM_POST:
        {
            struct rt_sem *const sem =
                event->data.syscall_pendable.args.sem_post.sem;
            const int n = event->data.syscall_pendable.args.sem_post.n;
            if (n == 1)
            {
                rt_logf("sem_post(%p)\n", (void *)sem);
            }
            else
            {
                rt_logf("sem_post_n(%p, %d)\n", (void *)sem, n);
            }
            break;
        }
        case RT_SYSCALL_PENDABLE_EVENT_SET:
        {
            struct rt_event *const ev =
                event->data.syscall_pendable.args.event_set.event;
            rt_logf("event_set(%p)\n", (void *)ev);
            break;
        }
        case RT_SYSCALL_PENDABLE_TICK:
            rt_logf("tick()\n");
            break;
        case RT_SYSCALL_PENDABLE_FUNCTION:
        {
            void (*const fn)(uintptr_t) =
                event->data.syscall_pendable.args.function.fn;
            const uintptr_t arg =
                event->data.syscall_pendable.args.function.arg;
            rt_logf("function<%p>(0x%" PRIxPTR ")\n", (void *)fn, arg);
            break;
        }
        }
        break;
    }
    case RT_TRACE_EVENT_FIRST_TASK:
    {
        const struct rt_task *const task =
            (const struct rt_task *)event->data.first_task;
        rt_logf("first active task: %s\n", task->name);
        break;
    }
    case RT_TRACE_EVENT_ACTIVE_TASK:
    {
        const struct rt_task *const old_task =
            (const struct rt_task *)event->data.active_task.old_task;
        const struct rt_task *const new_task =
            (const struct rt_task *)event->data.active_task.new_task;
        rt_logf("active task: %s -> %s\n", old_task->name, new_task->name);
        break;
    }
    case RT_TRACE_EVENT_TASK_STATE:
    {
        const struct rt_task *const task =
            (const struct rt_task *)event->data.task_state.task;
        static const char *const state_names[] = {
            "ready",
            "blocked on sem_wait",
            "blocked on sem_timedwait",
            "blocked on mutex_lock",
            "blocked on mutex_timedlock",
            "blocked on event_wait",
            "blocked on event_timedwait",
            "asleep",
            "exited",
            "terminated",
        };
        rt_static_assert(sizeof state_names / sizeof state_names[0] ==
                             RT_TASK_STATE_NUM_STATES,
                         "incorrect state_names length");
        rt_logf("task %s: %s -> %s\n", task->name,
                state_names[event->data.task_state.old_state],
                state_names[event->data.task_state.new_state]);
        break;
    }
    case RT_TRACE_EVENT_TICK:
        rt_logf("tick %lu\n", event->data.tick);
        break;
    case RT_TRACE_EVENT_INTERRUPT_START:
        rt_logf("interrupt %" PRId32 " start\n", event->data.interrupt_number);
        break;
    case RT_TRACE_EVENT_INTERRUPT_END:
        rt_logf("interrupt %" PRId32 " end\n", event->data.interrupt_number);
        break;
    case RT_TRACE_EVENT_SEM_UPDATE:
    {
        const int old_value = event->data.sem.old_value;
        const int new_value = event->data.sem.new_value;
        rt_logf("sem %p %s: %d -> %d\n", (const void *)event->data.sem.sem,
                (old_value > new_value) ? "sub" : "add", old_value, new_value);
        break;
    }
    case RT_TRACE_EVENT_EVENT_WAIT:
    {
        const uint32_t bits = event->data.event.bits;
        const uint32_t wait = event->data.event.wait_set;
        const bool all = (wait & RT_EVENT_WAIT_ALL) != 0;
        const bool noclear = (wait & RT_EVENT_WAIT_NOCLEAR) != 0;
        rt_logf("event %p: 0x%" PRIx32 ", wait 0x%" PRIx32 ", %s%s",
                (const void *)event->data.event.event,
                bits & ~RT_EVENT_WAIT_RESERVED, wait & ~RT_EVENT_WAIT_RESERVED,
                all ? "all" : "any", noclear ? ", no clear" : "");
        if (rt_event_bits_match(bits, wait))
        {
            rt_logf(", success");
            if (!noclear)
            {
                rt_logf(" -> 0x%" PRIx32,
                        (bits & ~wait) & ~RT_EVENT_WAIT_RESERVED);
            }
        }
        else
        {
            rt_logf(", fail");
        }
        rt_logf("\n");
        break;
    }
    case RT_TRACE_EVENT_EVENT_SET:
    {
        const uint32_t bits = event->data.event.bits;
        const uint32_t set = event->data.event.wait_set;
        rt_logf("event %p: 0x%" PRIx32 ", set 0x%" PRIx32 " -> 0x%" PRIx32 "\n",
                (const void *)event->data.event.event,
                bits & ~RT_EVENT_WAIT_RESERVED, set & ~RT_EVENT_WAIT_RESERVED,
                (bits | set) & ~RT_EVENT_WAIT_RESERVED);
        break;
    }
    case RT_TRACE_EVENT_TIMER_START:
        rt_logf("timer %p start: period %lu\n",
                (const void *)event->data.timer.timer,
                event->data.timer.period);
        break;
    case RT_TRACE_EVENT_TIMER_STOP:
        rt_logf("timer %p stop\n", (const void *)event->data.timer.timer);
        break;
    case RT_TRACE_EVENT_TIMER_CHANGE_PERIOD:
        rt_logf("timer %p change period: %lu\n",
                (const void *)event->data.timer.timer,
                event->data.timer.period);
        break;
    case RT_TRACE_EVENT_TIMER_EXPIRE:
        rt_logf("timer %p expired at tick %lu\n",
                (const void *)event->data.timer.timer, event->data.timer.tick);
        break;
    case RT_TRACE_EVENT_NOTIFY_OR:
        rt_logf("notify %p: 0x%" PRIx32 " |= 0x%" PRIx32 " -> 0x%" PRIx32 "\n",
                (const void *)event->data.notify.note,
                event->data.notify.old_value, event->data.notify.arg,
                event->data.notify.new_value);
        break;
    case RT_TRACE_EVENT_NOTIFY_SET:
        rt_logf("notify %p: 0x%" PRIx32 " -> 0x%" PRIx32 "\n",
                (const void *)event->data.notify.note,
                event->data.notify.old_value, event->data.notify.new_value);
        break;
    case RT_TRACE_EVENT_NOTIFY_CLEAR:
        rt_logf("notify %p: 0x%" PRIx32 " &= ~0x%" PRIx32 " -> 0x%" PRIx32 "\n",
                (const void *)event->data.notify.note,
                event->data.notify.old_value, event->data.notify.arg,
                event->data.notify.new_value);
        break;
    case RT_TRACE_EVENT_MUTEX_LOCK:
        rt_logf("mutex %p locked by %s\n",
                (const void *)event->data.mutex.mutex,
                mutex_holder_name(event->data.mutex.holder));
        break;
    case RT_TRACE_EVENT_MUTEX_LOCK_FAIL:
        rt_logf("mutex %p lock failed by %s, held by %s\n",
                (const void *)event->data.mutex_lock_fail.mutex,
                mutex_holder_name(event->data.mutex_lock_fail.failer),
                mutex_holder_name(event->data.mutex_lock_fail.holder));
        break;
    case RT_TRACE_EVENT_MUTEX_UNLOCK:
        rt_logf("mutex %p unlocked by %s\n",
                (const void *)event->data.mutex.mutex,
                mutex_holder_name(event->data.mutex.holder));
        break;
    case RT_TRACE_EVENT_QUEUE_PUSH:
    case RT_TRACE_EVENT_QUEUE_PUSH_SKIPPED:
    case RT_TRACE_EVENT_QUEUE_POP:
    case RT_TRACE_EVENT_QUEUE_POP_SKIP:
    case RT_TRACE_EVENT_QUEUE_PEEK:
    case RT_TRACE_EVENT_QUEUE_PEEK_SKIP:
    {
        static const char *const queue_op_names[] = {
            "push", "push skipped", "pop", "pop skip", "peek", "peek skip",
        };
        rt_static_assert(sizeof queue_op_names / sizeof queue_op_names[0] ==
                             RT_TRACE_EVENT_QUEUE_PEEK_SKIP -
                                 RT_TRACE_EVENT_QUEUE_PUSH + 1,
                         "incorrect queue_op_names length");
        const size_t seq = event->data.queue.seq;
        rt_logf("queue %p %s gen %u, index %zx\n",
                (const void *)event->data.queue.queue,
                queue_op_names[event->type - RT_TRACE_EVENT_QUEUE_PUSH],
                (unsigned)rt_queue_qsgen(seq) >> RT_QUEUE_STATE_BITS,
                rt_queue_qindex(seq));
        break;
    }
    case RT_TRACE_EVENT_MISSED:
        rt_logf("missed %zu trace events\n", event->data.missed);
        break;
    case RT_TRACE_EVENT_END:
        rt_logf("trace end\n");
        rt_log_flush();
        break;
    }
    return true;
}
#else  // !RT_LOG_ENABLE
__attribute__((weak)) bool
rt_trace_event_write(const struct rt_trace_event *event)
{
    (void)event;
    return true;
}
#endif // RT_LOG_ENABLE

void rt_trace_daemon(void)
{
    size_t deq = 0;
    bool end = false;
    while (!end)
    {
        rt_sem_wait(&rt_trace_send_sem);
        const size_t missed =
            rt_atomic_load(&rt_trace_missed, RT_ATOMIC_RELAXED);
        if (missed > 0)
        {
            struct rt_trace_event missed_event;
            /* The "missed" event doesn't use the circular event buffer, so
             * just load the current sequence counter and use it. */
            missed_event.seq = rt_atomic_load(&rt_trace_seq, RT_ATOMIC_RELAXED);
            missed_event.cycle = rt_cycle();
            missed_event.type = RT_TRACE_EVENT_MISSED;
            missed_event.data.missed = missed;
            rt_trace_event_write(&missed_event);
            rt_atomic_fetch_sub(&rt_trace_missed, missed, RT_ATOMIC_RELAXED);
        }

        for (size_t i = 0; i < RT_TRACE_NUM_EVENTS; ++i)
        {
            struct rt_trace_event *const ev =
                &rt_trace_events[deq % RT_TRACE_NUM_EVENTS];
            if (rt_atomic_load(&ev->state, RT_ATOMIC_ACQUIRE) !=
                RT_TRACE_EVENT_STATE_FULL)
            {
                break;
            }
            if (!rt_trace_event_write(ev))
            {
                break;
            }
            if (ev->type == RT_TRACE_EVENT_END)
            {
                end = true;
            }
            rt_atomic_store(&ev->state, RT_TRACE_EVENT_STATE_EMPTY,
                            RT_ATOMIC_RELAXED);
            ++deq;
        }
    }
}

#endif // RT_TRACE_ENABLE