Skip to content

Instantly share code, notes, and snippets.

@juan-lee
Created February 11, 2020 13:45
Show Gist options
  • Save juan-lee/60f52d3e7ebad168bd93a658d9b41f7e to your computer and use it in GitHub Desktop.
Save juan-lee/60f52d3e7ebad168bd93a658d9b41f7e to your computer and use it in GitHub Desktop.
#!/usr/bin/env bpftrace
/*
* ext4slower Summarize ext4 operation latency.
* For Linux, uses bpftrace and eBPF.
*
* This traces four common file system calls: read, write, open, and fsync.
* It can be customized to trace more if desired.
*
* USAGE: ext4dist.bt [min_us]
*
* By default, a minimum microsecond threshold of 1000 is used.
*
* This is a bpftrace version of the bcc tool of the same name.
*
* 11-Feb-2020 Juan-Lee Pang Created this.
*/
#include <linux/fs.h>
BEGIN
{
@slower = (uint64)1000;
if ($1 != 0)
{
@slower = $1;
}
printf("Tracing ext4 operations slower than %d microseconds...", @slower);
printf(" Hit Ctrl-C to end.\n");
printf("%-8s %-14s %-8s %1s %-8s %-8s ", "TIME", "COMM", "PID", "T", "BYTES", "OFFSET");
printf("%8s %s\n", "LAT(us)", "FILENAME");
}
kprobe:ext4_file_read_iter
{
$iocb = (struct kiocb *)arg0;
@filp[tid] = $iocb->ki_filp;
@offset[tid] = $iocb->ki_pos;
@func[tid] = "R";
@start[tid] = nsecs;
}
kprobe:ext4_file_write_iter
{
$iocb = (struct kiocb *)arg0;
@filp[tid] = $iocb->ki_filp;
@offset[tid] = $iocb->ki_pos;
@func[tid] = "W";
@start[tid] = nsecs;
}
kprobe:ext4_file_open
{
@filp[tid] = (struct file *)arg1;
@offset[tid] = 0;
@func[tid] = "O";
@start[tid] = nsecs;
}
kprobe:ext4_sync_file
{
@filp[tid] = (struct file *)arg0;
@offset[tid] = 0;
@func[tid] = "S";
@start[tid] = nsecs;
}
kretprobe:ext4_file_read_iter,
kretprobe:ext4_file_write_iter,
kretprobe:ext4_file_open,
kretprobe:ext4_sync_file
/@start[tid]/
{
$us = ((nsecs - @start[tid]) / 1000);
if ($us > @slower)
{
time("%H:%M:%S ");
printf("%-14.14s %-8d %1s %-8d %-8d %-8d", comm, pid, @func[tid], retval, @offset[tid], $us);
printf("%s\n", str(@filp[tid]->f_path.dentry->d_name.name));
}
@us = hist($us);
delete(@filp[tid]);
delete(@offset[tid]);
delete(@func[tid]);
delete(@start[tid]);
}
END
{
clear(@slower);
clear(@filp);
clear(@offset);
clear(@func);
clear(@start);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment