項目運行中磁盤讀 IO 飆升導致服務掛掉,`iostat`/`vmstat` 僅能看到設備級別吞吐,無法定位**具體進程讀了哪些文件**。
不同於用戶態 `strace -e trace=open,read -p PID` (極大性能開銷,生產環境慎用),`inotify` 由內核推送事件,開銷極低。
# 安裝
apt-get install fatrace # Debian/Ubuntu
yum install fatrace # CentOS/RHEL (需 EPEL)
# 運行:實時顯示所有進程文件讀寫事件
sudo fatrace -t -p $(pgrep -d, -f your_java_app)
# 輸出示例:
# 14:32:15.123456 java[1234]: R /var/log/app.log
# 14:32:15.123567 java[1234]: R /data/index/segment_45.dat
// C 示例:監聽目錄訪問事件
int fd = inotify_init1(IN_NONBLOCK);
int wd = inotify_add_watch(fd, "/data", IN_ACCESS | IN_OPEN | IN_CLOSE_NOWRITE);
char buf[4096];
while (read(fd, buf, sizeof(buf)) > 0) {
struct inotify_event *ev = (struct inotify_event*)buf;
if (ev->mask & IN_ACCESS) printf("PID %d read file: %s\n", getpid(), ev->name);
}
opensnoop (BCC) 實時顯示 `open()` 系統調用:`sudo opensnoop -p PID`。biolatency 分析塊設備延遲分布。