Skip to content

Commit

Permalink
Merge branch 'develop' into FS_WATCHER_V
Browse files Browse the repository at this point in the history
  • Loading branch information
sf1999817 authored Dec 17, 2024
2 parents 0485e9b + ae8fcc2 commit 4e22a77
Show file tree
Hide file tree
Showing 11 changed files with 405 additions and 190 deletions.
129 changes: 44 additions & 85 deletions MagicEyes/src/backend/fs/fs_watcher/bpf/CacheTrack.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,115 +6,74 @@

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

// struct {
// __uint(type, BPF_MAP_TYPE_HASH);
// __uint(max_entries, 1024);
// __type(key, char[30] );
// __type(value,struct event_CacheTrack);
// } data SEC(".maps");

// struct {
// __uint(type, BPF_MAP_TYPE_HASH);
// __uint(max_entries, 1024);
// __type(key, u64 );
// __type(value,struct event_CacheTrack);
// } unique_map SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 1024);
__type(key, u64); // 使用inode作为key
__type(value, struct event_CacheTrack); //存储事件结构体
} data_map SEC(".maps");

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


//事件会在脏 inode 开始进行写回时触发
SEC("tracepoint/writeback/writeback_dirty_inode_start")
int trace_writeback_start(struct trace_event_raw_writeback_dirty_inode_template *ctx){
pid_t pid = bpf_get_current_pid_tgid() >> 32;
u64 timestamp = bpf_ktime_get_ns();
ino_t inode;
char comm[16];
struct event_CacheTrack event_info ={};
char name[32];
// 获取当前进程的命令名称
bpf_get_current_comm(&comm, sizeof(comm));

event_info.ino = ctx->ino;
// 将 comm 字符串复制到 event_info.comm
__builtin_memcpy(event_info.comm, comm, sizeof(comm));

event_info.ino = inode = ctx->ino;

event_info.state = ctx->state;

event_info.flags = ctx->flags;

event_info.time = timestamp;

// bpf_probe_read(name, sizeof(name), ctx->name); // 从 ctx 复制设备名称

// 检查 name 是否为空字符串
// if (name[0] == '\0') {
// return 0;
// }
// if(name == NULL)
// return 0;

// __builtin_memcpy(event_info.name, name, sizeof(name));
// bpf_printk("comm_123:%s\n",event_info.name);
bpf_map_update_elem(&data_map,&inode,&event_info,BPF_ANY);
return 0;
}

// 事件会在每个 inode 进行单独写回时触发
SEC("tracepoint/writeback/writeback_single_inode")
int trace_writeback_single_inode(struct trace_event_raw_writeback_single_inode_template *ctx){
ino_t inode = ctx->ino;
u64 timestamp_conmplete = bpf_ktime_get_ns();
struct event_CacheTrack *event_info;

//从map中获取该inode对应的事件信息
event_info = bpf_map_lookup_elem(&data_map,&inode);
if(!event_info){
bpf_printk("failed to found event_info\n");
return 0;
}
//更新inode写回完成的信息
event_info->wrote = ctx->wrote; //已写回的字节数
event_info->nr_to_write = ctx->nr_to_write; //待写回的字节数
event_info->writeback_index = ctx->writeback_index; //表示写回操作的索引或序号
event_info->time_complete = timestamp_conmplete;

// 将事件信息提交到 ring buffer
struct event_CacheTrack *ring_event = bpf_ringbuf_reserve(&rb, sizeof(struct event_CacheTrack), 0);
if (!ring_event)
if (!ring_event) {
bpf_printk("Failed to reserve space in ring buffer for inode %llu\n", inode);
return 0;
}

__builtin_memcpy(ring_event, &event_info, sizeof(event_info));

bpf_printk("event_info_ino:%d\n",event_info.ino);

// 将事件信息从 map 拷贝到 ring buffer
__builtin_memcpy(ring_event, event_info, sizeof(struct event_CacheTrack));
bpf_ringbuf_submit(ring_event, 0);


// bpf_map_update_elem(&data,name,&event_info,BPF_ANY);
// bpf_map_update_elem(&unique_map,&event_info.queue_id,&event_info,BPF_ANY);
return 0;
}

// SEC("tracepoint/writeback/writeback_written")
// int trace_writeback_end(struct trace_event_raw_writeback_work_class *ctx) {
// bpf_printk("2222222\n");
// u64 timestamp = bpf_ktime_get_ns();
// char name[30];
// bpf_probe_read_str(name, sizeof(name), ctx->name); // 从 ctx 复制设备名称

// if(name == NULL)
// return 0;

// bpf_printk("comm:%s\n",name);

// struct event_CacheTrack *e = bpf_map_lookup_elem(&data,name);
// if(!e){
// bpf_printk("e failed\n");
// return 0;
// }


// struct event_CacheTrack *q = bpf_map_lookup_elem(&unique_map,&e->queue_id);
// if(!q){
// bpf_printk("q failed\n");
// return 0;
// }

// struct event_CacheTrack *q_event = bpf_ringbuf_reserve(&rb, sizeof(struct event_CacheTrack), 0);
// if (!q_event){
// bpf_printk("Ring buffer is full!\n");
// return 0;
// }

// q_event->nr_pages = ctx->nr_pages;
// q_event->sb_dev = ctx->sb_dev;
// q_event->sync_mode = ctx->sync_mode;
// q_event->for_kupdate = ctx->for_kupdate;
// q_event->range_cyclic = ctx->range_cyclic;
// q_event->for_background = ctx->for_background;
// q_event->reason = ctx->reason;
// q_event->cgroup_ino = ctx->cgroup_ino;
// q_event->time = timestamp - q->start_timestamp;

// bpf_printk("time:%llu\n",q_event->time);
// bpf_printk("123\n");

// bpf_ringbuf_submit(q_event, 0);

// return 0;

// }
}
10 changes: 7 additions & 3 deletions MagicEyes/src/backend/fs/fs_watcher/include/fs_watcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,20 @@ struct event_block_rq_issue {

/*CacheTrack*/
struct event_CacheTrack{
pid_t pid;
char comm[16];
long long time; //耗时
// char name[32]; // 设备名称
ino_t ino; // inode 号
unsigned long state; // inode 状态
unsigned long flags; // inode 标志
long int nr_to_write; // 待写回字节数
long unsigned int writeback_index; //写回操作的索引或序号
long unsigned int wrote; //已写回的字节数
long long time_complete; // 写回开始时间
};

/*send pid to ebpf*/
struct dist_args {
pid_t pid;
};
#endif /* __MEM_WATCHER_H */
#endif /* __MEM_WATCHER_H */

74 changes: 50 additions & 24 deletions MagicEyes/src/backend/fs/fs_watcher/src/fs_watcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ static const struct argp argp = {
static int libbpf_print_fn(enum libbpf_print_level level, const char *format,
va_list args)
{
return vfprintf(stderr, format, args);
// return vfprintf(stderr, format, args);
return 0;
}

static volatile bool exiting = false;
Expand All @@ -198,7 +199,7 @@ static int process_CacheTrack(struct CacheTrack_bpf *skel_CacheTrack);


int main(int argc,char **argv){

int err;
struct open_bpf *skel_open;
struct read_bpf *skel_read;
Expand All @@ -209,19 +210,19 @@ int main(int argc,char **argv){


libbpf_set_strict_mode(LIBBPF_STRICT_ALL);


/* 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);
signal(SIGALRM, sig_handler);


err = argp_parse(&argp, argc, argv, 0, NULL, NULL);
printf("success!\n");
// printf("success!\n");
if (err)
return err;

Expand Down Expand Up @@ -254,7 +255,7 @@ static int handle_event_open(void *ctx, void *data, size_t data_sz)
char comm[TASK_COMM_LEN];
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_,
Expand All @@ -266,12 +267,12 @@ static int handle_event_open(void *ctx, void *data, size_t data_sz)
int result = strcmp(e->path_name_, actual_path);
if (result == 0) {
if(bpf_map_lookup_elem(map_fd,&e->pid_,&comm)==0){
printf("get , filename:%s , fd:%d , pid:%d ,comm:%s\n",
printf("%-60s %-8d %-8d %-8s\n",
e->path_name_, i,e->pid_,comm);
}else{
fprintf(stderr, "Failed to lookup value for key %d\n", e->pid_);
}

}
}
}
Expand All @@ -290,7 +291,7 @@ static int handle_event_read(void *ctx, void *data, size_t data_sz)
tm = localtime(&t);
strftime(ts, sizeof(ts), "%H:%M:%S", tm);

printf("%-8s %-7d %-7llu\n", ts, e->pid,e->duration_ns);
printf("%-10s %-8d %-8llu\n",ts,e->pid,e->duration_ns);
return 0;
}

Expand All @@ -303,41 +304,55 @@ static int handle_event_write(void *ctx, void *data, size_t data_sz)
time(&t);
tm = localtime(&t);
strftime(ts, sizeof(ts), "%H:%M:%S", tm);
printf("ts:%-8s pid:%-7ld inode_number:%-3ld cout:%-3ld real_count:%-3ld\n", ts, e->pid,e->inode_number,e->count,e->real_count);
printf("%-8s %-8ld %-8ld %-8ld %-8ld\n", ts, e->pid,e->inode_number,e->count,e->real_count);
return 0;
}

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

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

return 0;
}

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 Total I/O: %" PRIu64 "\n",
printf("%-18llu %-15d %-15d %-10d %-16s Total I/O: %" PRIu64 "\n",
e->timestamp, e->dev, e->sector, e->nr_sectors, e->comm, e->total_io);

return 0;
}

static int handle_event_CacheTrack(void *ctx, void *data,unsigned long data_sz) {
static int handle_event_CacheTrack(void *ctx, void *data, unsigned long data_sz) {
const struct event_CacheTrack *event = data;
printf("%-20lld %-19lu %-20lu %-5lu\n", event->time, event->ino, event->state, event->flags);

// 计算写回操作的耗时
long long writeback_duration = event->time_complete - event->time;

// 打印所有相关的信息
printf("%-19llu %-15s %-20lu %-5lu %-20ld %-20lu %-20llu %-20lld\n",
event->ino, // inode 号
event->comm, //进程comm
event->state, // inode 状态
event->flags, // inode 标志
event->nr_to_write, // 待写回字节数
event->writeback_index, // 写回操作的索引或序号
event->wrote, // 已写回的字节数
writeback_duration); // 写回耗时

return 0;
}


static int process_open(struct open_bpf *skel_open){
int err;
struct ring_buffer *rb;

LOAD_AND_ATTACH_SKELETON_MAP(skel_open,open);

printf("%-8s %-8s %-8s %-8s\n","filenamename","fd","pid","comm");
printf("%-60s %-8s %-8s %-8s\n","filenamename","fd","pid","comm");
POLL_RING_BUFFER(rb, 1000, err);

open_cleanup:
Expand All @@ -350,10 +365,10 @@ static int process_open(struct open_bpf *skel_open){
static int process_read(struct read_bpf *skel_read){
int err;
struct ring_buffer *rb;

LOAD_AND_ATTACH_SKELETON(skel_read,read);

printf("%-8s %-8s %-8s %-8s\n","filename","fd","pid","ds");
printf("%-10s %-8s %-8s\n","TIME","PID","DS");
POLL_RING_BUFFER(rb, 1000, err);

read_cleanup:
Expand All @@ -370,6 +385,7 @@ static int process_write(struct write_bpf *skel_write){

struct dist_args d_args = {-1};


LOAD_AND_ATTACH_SKELETON(skel_write,write);

d_args.pid = env.pid;
Expand All @@ -394,7 +410,7 @@ static int process_write(struct write_bpf *skel_write){
static int process_disk_io_visit(struct disk_io_visit_bpf *skel_disk_io_visit){
int err;
struct ring_buffer *rb;

LOAD_AND_ATTACH_SKELETON(skel_disk_io_visit,disk_io_visit);
printf("%-18s %-7s %-7s %-4s %-7s %-16s\n","TIME", "DEV", "SECTOR", "RWBS", "COUNT", "COMM");
POLL_RING_BUFFER(rb, 1000, err);
Expand All @@ -410,9 +426,9 @@ static int process_disk_io_visit(struct disk_io_visit_bpf *skel_disk_io_visit){
static int process_block_rq_issue(struct block_rq_issue_bpf *skel_block_rq_issue){
int err;
struct ring_buffer *rb;

LOAD_AND_ATTACH_SKELETON(skel_block_rq_issue,block_rq_issue);
printf("%-18s %-7s %-7s %-4s %-16s %-5sn","TIME", "DEV", "SECTOR", "SECTORS","COMM","Total_Size");
printf("%-18s %-15s %-15s %-10s %-16s %-5s\n","TIME", "DEV", "SECTOR", "SECTORS","COMM","Total_Size");
POLL_RING_BUFFER(rb, 1000, err);

block_rq_issue_cleanup:
Expand All @@ -426,9 +442,19 @@ static int process_block_rq_issue(struct block_rq_issue_bpf *skel_block_rq_issue
static int process_CacheTrack(struct CacheTrack_bpf *skel_CacheTrack){
int err;
struct ring_buffer *rb;

LOAD_AND_ATTACH_SKELETON(skel_CacheTrack,CacheTrack);
printf("%-20s %-19s %-20s %-5s\n","TIME", "INO","STATE","FLAGS");
// 打印列标题说明(解释各列的含义)
printf("%-19s %-15s %-20s %-5s %-20s %-20s %-20s %-20s\n",
"INODE", // inode号
"COMM", //comm进程名
"STATE", // inode 状态
"FLAGS", // inode 标志
"NR_TO_WRITE", // 待写回字节数
"WRITEBACK_INDEX", // 写回操作的索引或序号
"WROTE", // 已写回字节数
"WRITEBACK_DURATION"); // 写回操作的耗时

POLL_RING_BUFFER(rb, 1000, err);

CacheTrack_cleanup:
Expand Down
Loading

0 comments on commit 4e22a77

Please sign in to comment.