use ax_task::{SchedTracepoint, TaskId, task_by_id};
use crate::task::AsThread;
ax_tracepoint::define_event_trace!(
sched_switch,
TP_kops(crate::tracepoint::KernelTraceAux),
TP_system(sched),
TP_PROTO(prev_tid: u64, next_tid: u64, prev_state: u32),
TP_STRUCT__entry {
prev_tid: u64,
next_tid: u64,
prev_state: u32,
},
TP_fast_assign {
prev_tid: prev_tid,
next_tid: next_tid,
prev_state: prev_state,
},
TP_ident(__entry),
TP_printk({
alloc::format!(
"prev_tid={} next_tid={} prev_state={}",
__entry.prev_tid,
__entry.next_tid,
__entry.prev_state,
)
})
);
struct SchedTracepointImpl;
#[ax_crate_interface::impl_interface]
impl SchedTracepoint for SchedTracepointImpl {
fn on_sched_switch(prev_task: TaskId, next_task: TaskId, prev_state: u32) {
trace_sched_switch(trace_tid(prev_task), trace_tid(next_task), prev_state);
}
}
fn trace_tid(task_id: TaskId) -> u64 {
task_by_id(task_id)
.and_then(|task| task.try_as_thread().map(|thread| thread.tid().get() as u64))
.unwrap_or_else(|| task_id.as_u64())
}