From 4dc6834dd82c139bbe02e49f9f2f1444771df711 Mon Sep 17 00:00:00 2001 From: shangfan <45649554+sf1999817@users.noreply.github.com> Date: Fri, 20 Sep 2024 11:53:52 +0000 Subject: [PATCH] add_block Signed-off-by: shangfan <45649554+sf1999817@users.noreply.github.com> --- .../fs_watcher/block_rq_issue.bpf.c | 46 +++++++++++++++---- .../fs_watcher/block_rq_issue.c | 15 +++--- .../fs_watcher/block_rq_issue.h | 2 + .../fs_watcher/fs_watcher.c | 8 ++-- .../fs_watcher/include/fs_watcher.h | 1 + 5 files changed, 53 insertions(+), 19 deletions(-) diff --git a/eBPF_Supermarket/Filesystem_Subsystem/fs_watcher/block_rq_issue.bpf.c b/eBPF_Supermarket/Filesystem_Subsystem/fs_watcher/block_rq_issue.bpf.c index 192e9ed79..161df83ab 100644 --- a/eBPF_Supermarket/Filesystem_Subsystem/fs_watcher/block_rq_issue.bpf.c +++ b/eBPF_Supermarket/Filesystem_Subsystem/fs_watcher/block_rq_issue.bpf.c @@ -6,17 +6,23 @@ char LICENSE[] SEC("license") = "Dual BSD/GPL"; -// 定义 ringbuf,用于传输事件信息 struct { __uint(type, BPF_MAP_TYPE_RINGBUF); __uint(max_entries, 256 * 1024); } rb SEC(".maps"); -// 这里挂载点必须是struct trace_event_raw_block_rq_completion *ctx +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(max_entries, 1024); + __type(key, u32); // 使用进程 PID 作为键 + __type(value, u64); // I/O 总大小作为值 +} io_size_map SEC(".maps"); + SEC("tracepoint/block/block_rq_issue") int tracepoint_block_rq_issue(struct trace_event_raw_block_rq_completion *ctx) { struct event *e; - char comm[TASK_COMM_LEN]; + u32 pid = bpf_get_current_pid_tgid() >> 32; // 获取进程 ID + u64 *size, total_size; // 分配 ringbuf 空间 e = bpf_ringbuf_reserve(&rb, sizeof(*e), 0); @@ -24,15 +30,37 @@ int tracepoint_block_rq_issue(struct trace_event_raw_block_rq_completion *ctx) { return 0; // 如果分配失败,提前返回 } + // 获取当前进程名 + bpf_get_current_comm(e->comm, sizeof(e->comm)); + // 填充事件数据 e->timestamp = bpf_ktime_get_ns(); - e->dev = ctx->dev; // 读取设备号 - e->sector = ctx->sector; // 读取扇区号 - e->nr_sectors = ctx->nr_sector; // 读取扇区数 + e->dev = ctx->dev; + e->sector = ctx->sector; + e->nr_sectors = ctx->nr_sector; + + // 日志输出调试信息 + bpf_printk("PID: %u, Sector: %d, nr_sectors: %d\n", pid, ctx->sector, ctx->nr_sector); + + // 查找或初始化该进程的 I/O 总大小 + size = bpf_map_lookup_elem(&io_size_map, &pid); + if (size) { + total_size = *size; + } else { + total_size = 0; + } + + // 计算本次 I/O 请求的大小 + const u64 sector_size = 512; // 标准扇区大小 + total_size += ctx->nr_sector * sector_size; + + // 更新 I/O 总大小 + bpf_map_update_elem(&io_size_map, &pid, &total_size, BPF_ANY); + + e->total_io = total_size; - // 获取进程名 - bpf_get_current_comm(comm, sizeof(comm)); - __builtin_memcpy(e->comm, comm, sizeof(comm)); + // 日志输出当前总 I/O 大小 + bpf_printk("Updated Total I/O for PID %u: %llu\n", pid, total_size); // 提交事件 bpf_ringbuf_submit(e, 0); diff --git a/eBPF_Supermarket/Filesystem_Subsystem/fs_watcher/block_rq_issue.c b/eBPF_Supermarket/Filesystem_Subsystem/fs_watcher/block_rq_issue.c index bede3f552..a99e17e7b 100644 --- a/eBPF_Supermarket/Filesystem_Subsystem/fs_watcher/block_rq_issue.c +++ b/eBPF_Supermarket/Filesystem_Subsystem/fs_watcher/block_rq_issue.c @@ -4,6 +4,9 @@ #include #include "block_rq_issue.h" #include "block_rq_issue.skel.h" +#include // For PRIu64 +#include // For uint32_t, uint64_t +#include // For getpid() static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args) { @@ -17,15 +20,15 @@ static void sig_handler(int sig) exiting = true; } -static int handle_event_block_rq_issue(void *ctx, void *data,unsigned long data_sz) { +static int handle_event_block_rq_issue(void *ctx, void *data, unsigned long data_sz) { const struct event *e = data; - printf("%-10llu %-9d %-7d %-4d %-16s\n", - e->timestamp, e->dev, e->sector, e->nr_sectors,e->comm); - + printf("%-10llu %-9d %-7d %-4d %-16s Total I/O: %" PRIu64 "\n", + e->timestamp, e->dev, e->sector, e->nr_sectors, e->comm, e->total_io); return 0; } + int main(int argc, char **argv) { struct ring_buffer *rb = NULL; @@ -68,7 +71,7 @@ int main(int argc, char **argv) goto cleanup; } - printf("%-18s %-7s %-7s %-4s %-7s %-16s\n","TIME", "DEV", "SECTOR", "RWBS", "COUNT", "COMM"); + printf("%-10s %-9s %-7s %-4s %-16s %-12s\n", "TIME", "DEV", "SECTOR", "RWBS", "COMM", "Total I/O"); while (!exiting) { err = ring_buffer__poll(rb, 100 /* timeout, ms */); /* Ctrl-C will cause -EINTR */ @@ -89,4 +92,4 @@ int main(int argc, char **argv) block_rq_issue_bpf__destroy(skel); return err < 0 ? -err : 0; -} \ No newline at end of file +} diff --git a/eBPF_Supermarket/Filesystem_Subsystem/fs_watcher/block_rq_issue.h b/eBPF_Supermarket/Filesystem_Subsystem/fs_watcher/block_rq_issue.h index 229e9ba88..8e33daf60 100644 --- a/eBPF_Supermarket/Filesystem_Subsystem/fs_watcher/block_rq_issue.h +++ b/eBPF_Supermarket/Filesystem_Subsystem/fs_watcher/block_rq_issue.h @@ -3,12 +3,14 @@ #define TASK_COMM_LEN 256 +// 定义事件结构体 struct event { long timestamp; // 时间戳 int dev; // 设备号 int sector; // 扇区号 int nr_sectors; // 扇区数 char comm[TASK_COMM_LEN]; // 进程名 + int total_io; //I/O总大小 }; #endif // BLOCK_RQ_ISSUE \ No newline at end of file diff --git a/eBPF_Supermarket/Filesystem_Subsystem/fs_watcher/fs_watcher.c b/eBPF_Supermarket/Filesystem_Subsystem/fs_watcher/fs_watcher.c index fe36747f4..d88bff59d 100644 --- a/eBPF_Supermarket/Filesystem_Subsystem/fs_watcher/fs_watcher.c +++ b/eBPF_Supermarket/Filesystem_Subsystem/fs_watcher/fs_watcher.c @@ -229,6 +229,7 @@ static int handle_event_open(void *ctx, void *data, size_t data_sz) int i = 0; int map_fd = *(int *)ctx;//传递map得文件描述符 + for (; i < e->n_; ++i) { snprintf(fd_path, sizeof(fd_path), "/proc/%d/fd/%d", e->pid_, i); @@ -291,9 +292,8 @@ static int handle_event_disk_io_visit(void *ctx, void *data,unsigned long data_s static int handle_event_block_rq_issue(void *ctx, void *data,unsigned long data_sz) { const struct event_block_rq_issue *e = data; - - printf("%-10llu %-9d %-7d %-4d %-16s\n", - e->timestamp, e->dev, e->sector, e->nr_sectors,e->comm); + printf("%-10llu %-9d %-7d %-4d %-16s Total I/O: %" PRIu64 "\n", + e->timestamp, e->dev, e->sector, e->nr_sectors, e->comm, e->total_io); return 0; } @@ -367,7 +367,7 @@ static int process_block_rq_issue(struct block_rq_issue_bpf *skel_block_rq_issue struct ring_buffer *rb; LOAD_AND_ATTACH_SKELETON(skel_block_rq_issue,block_rq_issue); - printf("%-18s %-7s %-7s %-4s %-16s\n","TIME", "DEV", "SECTOR", "SECTORS","COMM"); + printf("%-18s %-7s %-7s %-4s %-16s %-5sn","TIME", "DEV", "SECTOR", "SECTORS","COMM","Total_Size"); POLL_RING_BUFFER(rb, 1000, err); block_rq_issue_cleanup: diff --git a/eBPF_Supermarket/Filesystem_Subsystem/fs_watcher/include/fs_watcher.h b/eBPF_Supermarket/Filesystem_Subsystem/fs_watcher/include/fs_watcher.h index da78cc0dd..b52ab6bbb 100644 --- a/eBPF_Supermarket/Filesystem_Subsystem/fs_watcher/include/fs_watcher.h +++ b/eBPF_Supermarket/Filesystem_Subsystem/fs_watcher/include/fs_watcher.h @@ -45,6 +45,7 @@ struct event_block_rq_issue { int sector; // 扇区号 int nr_sectors; // 扇区数 char comm[TASK_COMM_LEN]; // 进程名 + int total_io; //I/O总大小 }; #endif /* __MEM_WATCHER_H */ \ No newline at end of file