Skip to content

Instantly share code, notes, and snippets.

@florianl
Created January 12, 2024 07:52
Show Gist options
  • Save florianl/4892e38d6af0743fafe423b788a41879 to your computer and use it in GitHub Desktop.
Save florianl/4892e38d6af0743fafe423b788a41879 to your computer and use it in GitHub Desktop.
Listen to nvidia-smi ioctl commands
#!/usr/bin/env bpftrace
/*
* sudo bpftrace nvidia-smi.bt -o nvidia-smi.$(date +%s%N | cut -b1-13).out
*
* Dump is limited to 64 byte as enforced by BPFTRACE_STRLEN and stack size limitations.
*/
BEGIN
{
printf("Tracing nvidia-smi ioctl calls ... Hit Ctrl-C to end.\n\n");
printf("%-10s\t%-18s\tmemory ptr\tmemory dump\n","cmd","device (fd)");
}
tracepoint:syscalls:sys_enter_ioctl
/comm == "nvidia-smi"/
{
printf("0x%-8x\t%s (%d)\t0x%-8x\t%r\n", args->cmd, @fd[args->fd], args->fd, args->arg, buf(args->arg, 64));
@ioctl[tid] = nsecs;
@arg[tid] = args->arg;
}
tracepoint:syscalls:sys_exit_ioctl
/comm == "nvidia-smi" & @ioctl[tid] != 0/
{
printf("\t\t\t\t\t\t\t%r\n", buf(@arg[tid], 64));
delete(@ioctl[tid]);
delete(@arg[tid]);
}
/*
* Poor mans approach to track and map opened files to returned file descriptors.
* In sys_enter_openat two maps (open and path) are used to make sure
* tracking of opened files is concurrent safe.
* In sys_exit_openat the given path is stored along the returned file
* descriptor number in fd.
*/
tracepoint:syscalls:sys_enter_openat
/comm == "nvidia-smi"/
{
@openat[tid] = nsecs;
@path[tid] = str(args->filename);
}
tracepoint:syscalls:sys_exit_openat
/comm == "nvidia-smi" & @openat[tid] != 0/
{
@fd[args->ret] = @path[tid];
delete(@openat[tid]);
delete(@path[tid]);
}
END
{
clear(@fd);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment