Skip to content

Instantly share code, notes, and snippets.

@pranav-bhatt
Created September 20, 2021 14:24
Show Gist options
  • Save pranav-bhatt/a29d6d0e5be6ff94edf5dd5c351ebe9f to your computer and use it in GitHub Desktop.
Save pranav-bhatt/a29d6d0e5be6ff94edf5dd5c351ebe9f to your computer and use it in GitHub Desktop.
Custom bpftrace+pixie script
import pxtrace
import px
program = """
#include <uapi/linux/ptrace.h>
#include <linux/sched.h>
#include <linux/nsproxy.h>
#include <linux/pid_namespace.h>
tracepoint:sched:sched_wakeup,
tracepoint:sched:sched_wakeup_new
{
@qtime[args->pid] = nsecs;
}
tracepoint:sched:sched_switch {
if (args->prev_state == TASK_RUNNING) {
if (args->prev_pid != 0) {
@qtime[args->prev_pid] = nsecs;
}
}
$ns = @qtime[args->next_pid];
$latency = (nsecs - $ns)/1000;
if($latency != 0 && args->next_pid != 0 && args->prev_pid != 0){
printf(\"time_:%llu old_process:%s old_pid:%d latency:%lld new_process:%s new_pid:%d\",
nsecs,
args->prev_comm,
args->prev_pid,
$latency,
args->next_comm,
args->next_pid);
}
# delete(@qtime[args->next_pid]);
}
"""
def demo_func():
table_name = 'latencies_table'
pxtrace.UpsertTracepoint('latencies',
table_name,
program,
pxtrace.kprobe(),
"10m")
# Rename columns
df = px.DataFrame(table=table_name)
return df
df = demo_func()
px.display(df)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment