Last active
March 26, 2021 02:27
-
-
Save CyrusF/cef49153e5f71dcf61d83a507131a54f to your computer and use it in GitHub Desktop.
disklatency.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# 2019 by Cyrus | |
from __future__ import print_function | |
from bcc import BPF | |
from time import sleep | |
# load BPF program | |
b = BPF(text=""" | |
#include <uapi/linux/ptrace.h> | |
#include <linux/blkdev.h> | |
BPF_HASH(start, struct request *); | |
BPF_HISTOGRAM(dist); | |
BPF_HISTOGRAM(dist_linear); | |
void trace_start(struct pt_regs *ctx, struct request *req) { | |
// stash start timestamp by request ptr | |
u64 ts = bpf_ktime_get_ns(); | |
start.update(&req, &ts); | |
} | |
void trace_completion(struct pt_regs *ctx, struct request *req) { | |
u64 *tsp, delta; | |
tsp = start.lookup(&req); | |
if (tsp != 0) { | |
delta = bpf_ktime_get_ns() - *tsp; | |
dist.increment(bpf_log2l(delta / 1000000)); | |
dist_linear.increment(delta / 1000000); | |
start.delete(&req); | |
} | |
} | |
""") | |
b.attach_kprobe(event="blk_start_request", fn_name="trace_start") | |
b.attach_kprobe(event="blk_mq_start_request", fn_name="trace_start") | |
b.attach_kprobe(event="blk_account_io_completion", fn_name="trace_completion") | |
# header | |
print("Tracing... Hit Ctrl-C to end.") | |
# trace until Ctrl-C | |
try: | |
sleep(99999999) | |
except KeyboardInterrupt: | |
print() | |
# output | |
print("log2 histogram") | |
print("~~~~~~~~~~~~~~") | |
b["dist"].print_log2_hist("time(ms)") | |
print("\nlinear histogram") | |
print("~~~~~~~~~~~~~~~~") | |
b["dist_linear"].print_linear_hist("time(ms)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You need to first determine whether there is a write request to open the hard disk IO block,like this(line 39):