Skip to content

Commit

Permalink
disk_io_visit
Browse files Browse the repository at this point in the history
Signed-off-by: shangfan <[email protected]>
  • Loading branch information
sf1999817 committed Jul 26, 2024
1 parent 25d6be4 commit da6ac1e
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>
#include "disk_io_visit.h"

char LICENSE[] SEC("license") = "Dual BSD/GPL";

struct {
__uint(type, BPF_MAP_TYPE_RINGBUF);
__uint(max_entries, 256 * 1024);
} rb SEC(".maps");

// 进程名与 I/O 计数映射
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 1024);
__type(key, char[TASK_COMM_LEN]);
__type(value, u32);
} io_count_map SEC(".maps");

SEC("tracepoint/block/block_rq_complete")
int tracepoint_block_visit(struct trace_event_raw_block_rq_complete *ctx) {
struct event *e;
u32 *count, new_count;
char comm[TASK_COMM_LEN];
unsigned long long ts;

// 获取当前进程名
bpf_get_current_comm(comm, sizeof(comm));

// 查找或初始化该进程的I/O计数
count = bpf_map_lookup_elem(&io_count_map, comm);
if (count) {
new_count = *count + 1;
} else {
new_count = 1;
}
bpf_map_update_elem(&io_count_map, comm, &new_count, BPF_ANY);

// 分配 ringbuf 空间
e = bpf_ringbuf_reserve(&rb, sizeof(*e), 0);
if (!e) {
return 0;
}

// 填充事件数据
ts= bpf_ktime_get_ns();
e->timestamp = ts;
e->blk_dev = ctx->dev;
e->sectors = ctx->nr_sector;
e->rwbs = (ctx->rwbs[0] == 'R') ? 1 : 0;
e->count = new_count;
__builtin_memcpy(e->comm, comm, sizeof(comm));

// 提交事件
bpf_ringbuf_submit(e, 0);
return 0;
}
93 changes: 93 additions & 0 deletions eBPF_Supermarket/Filesystem_Subsystem/old_project/disk_io_visit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include <stdio.h>
#include <signal.h>
#include <time.h>
#include <bpf/libbpf.h>
#include "disk_io_visit.h"
#include "disk_io_visit.skel.h"
#include <inttypes.h> // For PRIu64

static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args)
{
return vfprintf(stderr, format, args);
}

static volatile bool exiting = false;

static void sig_handler(int sig)
{
exiting = true;
}

static int handle_event(void *ctx, void *data,unsigned long data_sz) {
const struct event *e = data;

printf("%-10llu %-9d %-7d %-4d %-7d %-16s\n",
e->timestamp, e->blk_dev, e->sectors, e->rwbs, e->count, e->comm);

return 0;
}

int main(int argc, char **argv)
{
struct ring_buffer *rb = NULL;
struct disk_io_visit_bpf *skel;
int err;

/* Set up libbpf errors and debug info callback */
libbpf_set_print(libbpf_print_fn);

/* Cleaner handling of Ctrl-C */
signal(SIGINT, sig_handler);
signal(SIGTERM, sig_handler);

/* Open BPF application */
skel = disk_io_visit_bpf__open();
if (!skel) {
fprintf(stderr, "Failed to open BPF skeleton\n");
return 1;
}

/* Load & verify BPF programs */
err = disk_io_visit_bpf__load(skel);
if (err) {
fprintf(stderr, "Failed to load and verify BPF skeleton\n");
goto cleanup;
}

/* Attach tracepoints */
err = disk_io_visit_bpf__attach(skel);
if (err) {
fprintf(stderr, "Failed to attach BPF skeleton\n");
goto cleanup;
}

/* Set up ring buffer polling */
rb = ring_buffer__new(bpf_map__fd(skel->maps.rb), handle_event, NULL, NULL);
if (!rb) {
err = -1;
fprintf(stderr, "Failed to create ring buffer\n");
goto cleanup;
}

printf("%-18s %-7s %-7s %-4s %-7s %-16s\n","TIME", "DEV", "SECTOR", "RWBS", "COUNT", "COMM");
while (!exiting) {
err = ring_buffer__poll(rb, 100 /* timeout, ms */);
/* Ctrl-C will cause -EINTR */
if (err == -EINTR) {
err = 0;
break;
}

if (err < 0) {
printf("Error polling ring buffer: %d\n", err);
break;
}
}

cleanup:
/* Clean up */
ring_buffer__free(rb);
disk_io_visit_bpf__destroy(skel);

return err < 0 ? -err : 0;
}
15 changes: 15 additions & 0 deletions eBPF_Supermarket/Filesystem_Subsystem/old_project/disk_io_visit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef DISK_IO_VISIT_H
#define DISK_IO_VISIT_H

#define TASK_COMM_LEN 256

struct event {
long timestamp; // 时间戳
int blk_dev; // 块设备号
int sectors; // 访问的扇区数
int rwbs; // 读写标识符,1表示读操作,0表示写操作
int count; // I/O 操作计数
char comm[TASK_COMM_LEN]; // 进程名
};

#endif // DISK_IO_VISIT_H

0 comments on commit da6ac1e

Please sign in to comment.