-
Notifications
You must be signed in to change notification settings - Fork 4k
add a new tool in libbpf-tools named ext4File #5440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
niebowen666
wants to merge
10
commits into
iovisor:master
Choose a base branch
from
niebowen666:ext4File
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+568
−3
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
315191c
upload ext4File.h
niebowen666 836d3f0
upload ext4File.c
niebowen666 1f8e5f7
upload ext4File.bpf.c
niebowen666 a5912d8
Add ext4File
niebowen666 64e5de6
delete
niebowen666 a6c5b43
LF
niebowen666 08662eb
ext4File fix
niebowen666 f926db5
Change function my_printf to targeted_printf and NAME_MAX to 255
niebowen666 56eb4a3
rename
niebowen666 420e24f
Makefile fix
niebowen666 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) | ||
| // Copyright (c) 2025 Samsung Electronics Co., Ltd. | ||
| #include <vmlinux.h> | ||
| #include <bpf/bpf_helpers.h> | ||
| #include <bpf/bpf_tracing.h> | ||
| #include <bpf/bpf_core_read.h> | ||
|
|
||
| #include "ext4file.h" | ||
|
|
||
| volatile __u64 dev_target = 0; | ||
| volatile __u64 blocks_per_group = 0; | ||
|
|
||
| #define N 16 | ||
| #define GROUP 64 | ||
|
|
||
| struct { | ||
| __uint(type, BPF_MAP_TYPE_HASH); | ||
| __uint(max_entries, 1000000); | ||
| __type(key, u32); | ||
| __type(value, struct file_info_key); | ||
| } ino_name_map SEC(".maps"); | ||
|
|
||
| struct { | ||
| __uint(type, BPF_MAP_TYPE_HASH); | ||
| __uint(max_entries, 1000000); | ||
| __type(key, struct file_info_key); | ||
| __type(value, struct file_info_val); | ||
| } file_info_map SEC(".maps"); | ||
|
|
||
| static __always_inline bool str_equal(const char *a, const char *b) { | ||
| for (size_t i = 0; i < MAX_FILE_NAME; i++) { | ||
| if (a[i] == '\0' && b[i] == '\0') | ||
| return true; | ||
| if (a[i] != b[i]) | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| SEC("fexit/ext4_add_entry") | ||
| int BPF_PROG(my_ext4_add_entry, handle_t* handle, | ||
| struct dentry* dentry, struct inode* inode) | ||
| { | ||
| bpf_printk("ext4_add_entry"); | ||
| struct inode* pa_inode = dentry->d_parent->d_inode; | ||
| dev_t dev_cur = pa_inode->i_sb->s_dev; | ||
| u64 ino_id = inode->i_ino; | ||
| struct file_info_key fik = {}; | ||
| struct file_info_val fiv = {}; | ||
|
|
||
| if (dev_target && dev_target != dev_cur) | ||
| return 0; | ||
|
|
||
| fik.fk_ino = ino_id; | ||
| fik.fk_pa_ino = pa_inode->i_ino; | ||
| bpf_probe_read_str(&fik.fk_name, | ||
| sizeof(fik.fk_name), dentry->d_name.name); | ||
|
|
||
| if (bpf_map_update_elem(&file_info_map, &fik, &fiv, BPF_ANY)) | ||
| bpf_printk("failed to update file_info_map\n"); | ||
| if (bpf_map_update_elem(&ino_name_map, &ino_id, &fik, BPF_ANY)) | ||
| bpf_printk("failed to update ino_name_map\n"); | ||
| bpf_printk("the file(%s %u %u) inserted", fik.fk_name, fik.fk_ino, fik.fk_pa_ino); | ||
| return 0; | ||
| } | ||
|
|
||
| SEC("tp_btf/ext4_unlink_enter") | ||
| int BPF_PROG(my_ext4_unlink, | ||
| struct inode * pa_inode, struct dentry *dentry) | ||
| { | ||
| bpf_printk("ext4_unlink_enter"); | ||
| struct inode* inode = dentry->d_inode; | ||
| struct file_info_key fik = {}, *fikp; | ||
| struct file_info_val* fivp = NULL; | ||
| u64 ino_id = inode->i_ino; | ||
| dev_t dev_cur = pa_inode->i_sb->s_dev; | ||
| if (dev_target && dev_target != dev_cur) | ||
| return 0; | ||
|
|
||
| fik.fk_ino = inode->i_ino; | ||
| fik.fk_pa_ino = pa_inode->i_ino; | ||
| bpf_probe_read_str(&fik.fk_name, | ||
| sizeof(fik.fk_name), dentry->d_name.name); | ||
| fivp = bpf_map_lookup_elem(&file_info_map, &fik); | ||
| if (!fivp) { | ||
| bpf_printk("the file(%s %u %u) is not in monitor", fik.fk_name, fik.fk_ino, fik.fk_pa_ino); | ||
| return 0; | ||
| } | ||
| fivp->fv_delete = true; | ||
| fikp = bpf_map_lookup_elem(&ino_name_map, &ino_id); | ||
| if (!fikp) { | ||
| return 0; | ||
| } | ||
| if (str_equal(fikp->fk_name, fik.fk_name)) | ||
| bpf_map_delete_elem(&ino_name_map, &ino_id); | ||
| return 0; | ||
| } | ||
|
|
||
| SEC("fentry/ext4_rmdir") | ||
| int BPF_PROG(my_ext4_rmdir, | ||
| struct inode* dir, struct dentry* dentry) | ||
| { | ||
| struct inode* inode = dentry->d_inode; | ||
| struct file_info_key fik = {}, *fikp; | ||
| struct file_info_val* fivp = NULL; | ||
| u64 ino_id = inode->i_ino; | ||
| dev_t dev_cur = inode->i_sb->s_dev; | ||
| if (dev_target && dev_target != dev_cur) | ||
| return 0; | ||
|
|
||
| fik.fk_ino = inode->i_ino; | ||
| fik.fk_pa_ino = dir->i_ino; | ||
| bpf_probe_read_str(&fik.fk_name, | ||
| sizeof(fik.fk_name), dentry->d_name.name); | ||
| fivp = bpf_map_lookup_elem(&file_info_map, &fik); | ||
| if (!fivp) { | ||
| bpf_printk("the file(%s) is not in monitor", fik.fk_name); | ||
| return 0; | ||
| } | ||
| fivp->fv_delete = true; | ||
|
|
||
| fikp = bpf_map_lookup_elem(&ino_name_map, &ino_id); | ||
| if (!fikp) { | ||
| return 0; | ||
| } | ||
| if (str_equal(fikp->fk_name, fik.fk_name)) | ||
| bpf_map_delete_elem(&ino_name_map, &ino_id); | ||
| return 0; | ||
| } | ||
|
|
||
| SEC("fentry/ext4_file_write_iter") | ||
| int BPF_PROG(my_ext4_file_write_iter, | ||
| struct kiocb *iocb, struct iov_iter *from) | ||
| { | ||
| struct inode* inode = iocb->ki_filp->f_inode; | ||
| dev_t dev_cur = inode->i_sb->s_dev; | ||
| struct file_info_key* fikp = NULL; | ||
| struct file_info_val* fivp = NULL; | ||
| u64 ino_id = inode->i_ino; | ||
|
|
||
| if (dev_target && dev_target != dev_cur) | ||
| return 0; | ||
|
|
||
| fikp = bpf_map_lookup_elem(&ino_name_map, &ino_id); | ||
| if (!fikp) { | ||
| bpf_printk("fail to find fikp: %d", ino_id); | ||
| return 0; | ||
| } | ||
| fivp = bpf_map_lookup_elem(&file_info_map, fikp); | ||
| if (!fivp) { | ||
| bpf_printk("failed to lookup file_info_map\n"); | ||
| return 0; | ||
| } | ||
|
|
||
| fivp->fv_hint = inode->i_write_hint; | ||
| if(iocb->ki_flags & IOCB_DIRECT) | ||
| __sync_fetch_and_add(&fivp->fv_rw_cnt[RW_TYPE_DIRECT_WRITE], 1); | ||
| else | ||
| __sync_fetch_and_add(&fivp->fv_rw_cnt[RW_TYPE_BUFFER_WRITE], 1); | ||
| return 0; | ||
| } | ||
|
|
||
| SEC("fentry/ext4_file_read_iter") | ||
| int BPF_PROG(my_ext4_file_read_iter, | ||
| struct kiocb *iocb, struct iov_iter *to) | ||
| { | ||
| //bpf_printk("ext4_file_read_iter\n"); | ||
| struct file* file = iocb->ki_filp; | ||
| struct inode* inode = file->f_inode; | ||
| dev_t dev_cur = inode->i_sb->s_dev; | ||
| struct file_info_key* fikp = NULL; | ||
| struct file_info_val* fivp = NULL; | ||
| u64 ino_id = inode->i_ino; | ||
| //bpf_printk("device_num:%d dev:%d\n", device_num, dev); | ||
| if (dev_target && dev_target != dev_cur) | ||
| return 0; | ||
|
|
||
| fikp = bpf_map_lookup_elem(&ino_name_map, &ino_id); | ||
| if (!fikp) { | ||
| bpf_printk("fail to find fikp: %d", ino_id); | ||
| return 0; | ||
| } | ||
| fivp = bpf_map_lookup_elem(&file_info_map, fikp); | ||
| if (!fivp) { | ||
| bpf_printk("failed to lookup file_info_map\n"); | ||
| return 0; | ||
| } | ||
|
|
||
| if (iocb->ki_flags & IOCB_DIRECT) | ||
| __sync_fetch_and_add(&fivp->fv_rw_cnt[RW_TYPE_DIRECT_READ], 1); | ||
| else | ||
| __sync_fetch_and_add(&fivp->fv_rw_cnt[RW_TYPE_BUFFER_READ], 1); | ||
| return 0; | ||
| } | ||
|
|
||
| char LICENSE[] SEC("license") = "Dual BSD/GPL"; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need this new tool ? We already have fsdist/fsslower/filelife/filetop ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ext4file is a tool used to track file-level buffer or direct I/O.
If a certain file is expected to be accessed by direct I/O, ext4file can detect abnormal I/O access.
I have read the source code of the tools you listed above.
ext4file can complement the tools mentioned above and can determine whether a file exhibits unexpected I/O under complex workloads.