Skip to content

Commit

Permalink
Merge branch 'linuxkerneltravel:develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
sf1999817 authored Dec 6, 2024
2 parents 65df502 + b5fc205 commit ef11ece
Show file tree
Hide file tree
Showing 11 changed files with 1,155 additions and 224 deletions.
25 changes: 21 additions & 4 deletions MagicEyes/src/backend/fs/fs_watcher/bpf/write.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,33 @@ struct {
__uint(max_entries,256 * 1024);
} rb SEC(".maps");

struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 256 * 1024);
__type(key, u32);
__type(value, struct dist_args);
} args_map SEC(".maps");

SEC("kprobe/vfs_write")
int kprobe_vfs_write(struct pt_regs *ctx)
{
pid_t pid;
int index = 0;
struct fs_t *e;
unsigned long inode_number;//定义用于存储inode号码的变量

//探测的是第一个参数,文件指针,读取inode_number
struct dist_args *d_args = bpf_map_lookup_elem(&args_map, &index);
if (d_args == NULL){
bpf_printk("Failed to look args\n");
return 0;
}

pid = bpf_get_current_pid_tgid() >> 32;
if (d_args->pid > 0 && d_args->pid != pid) {
return 0;
}

//探测的是第一个参数,文件指针,读取inode_number
struct file *filp = (struct file *)PT_REGS_PARM1(ctx);  
struct dentry *dentry = BPF_CORE_READ(filp,f_path.dentry);
if(!dentry){
Expand All @@ -42,11 +60,10 @@ int kprobe_vfs_write(struct pt_regs *ctx)

//探测的是第三个参数,要写入的字节数
size_t count = (size_t)PT_REGS_PARM3(ctx);

//这是vfs_write的返回值,它是一个实际写入的字节数
size_t real_count = PT_REGS_RC(ctx);

pid = bpf_get_current_pid_tgid() >> 32;

e = bpf_ringbuf_reserve(&rb,sizeof(*e),0);
if(!e)
return 0;
Expand Down
4 changes: 4 additions & 0 deletions MagicEyes/src/backend/fs/fs_watcher/include/fs_watcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,8 @@ struct event_CacheTrack{
unsigned long flags; // inode 标志
};

/*send pid to ebpf*/
struct dist_args {
pid_t pid;
};
#endif /* __MEM_WATCHER_H */
31 changes: 29 additions & 2 deletions MagicEyes/src/backend/fs/fs_watcher/src/fs_watcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,15 @@ static struct env{
bool disk_io_visit;
bool block_rq_issue;
bool CacheTrack;
pid_t pid;
}env = {
.open = false,
.read = false,
.write = false,
.disk_io_visit = false,
.block_rq_issue = false,
.CacheTrack = false,
.pid = -1,
};

static const struct argp_option opts[] = {
Expand All @@ -121,6 +123,7 @@ static const struct argp_option opts[] = {
{"disk_io_visit", 'd', 0, 0, "Print disk I/O visit report"},
{"block_rq_issue", 'b', 0, 0, "Print block I/O request submission events. Reports when block I/O requests are submitted to device drivers."},
{"CacheTrack", 't' , 0 ,0 , "WriteBack dirty lagency and other information"},
{"pid", 'p', "PID", 0, "Specify pid number when report weite. Only support for write report now"},
{0} // 结束标记,用于指示选项列表的结束
};

Expand All @@ -139,7 +142,19 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state) {
env.block_rq_issue = true;break;
case 't':
env.CacheTrack = true;break;
default:
case 'p':
if (arg) {
env.pid = atoi(arg);
if (env.pid <= 0) {
fprintf(stderr, "Invalid PID value: %s\n", arg);
argp_usage(state);
}
} else {
fprintf(stderr, "-p option requires an argument\n");
argp_usage(state);
}
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
Expand Down Expand Up @@ -351,9 +366,21 @@ static int process_read(struct read_bpf *skel_read){
static int process_write(struct write_bpf *skel_write){
int err;
struct ring_buffer *rb;

int arg_index = 0;

struct dist_args d_args = {-1};

LOAD_AND_ATTACH_SKELETON(skel_write,write);

d_args.pid = env.pid;
struct bpf_map *arg_map = bpf_object__find_map_by_name((const struct bpf_object *)*(skel_write->skeleton->obj), "args_map");
err = bpf_map__update_elem(arg_map, &arg_index, sizeof(arg_index), &d_args, sizeof(d_args), BPF_ANY);

if (err < 0) {
fprintf(stderr, "ERROR: failed to update args map\n");
goto write_cleanup;
}

printf("%-8s %-8s %-8s %-8s %-8s\n","ds","inode_number","pid","real_count","count");
POLL_RING_BUFFER(rb, 1000, err);

Expand Down
23 changes: 15 additions & 8 deletions MagicEyes/src/backend/hypervisor/kvm_watcher/src/kvm_watcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
#include "trace_helpers.h"
#include "uprobe_helpers.h"
#include "hypervisor/kvm_watcher/kvm_watcher.skel.h"

//可视化调整输出格式
int is_first = 1;
// 创建并打开临时文件
Expand Down Expand Up @@ -563,10 +562,15 @@ static int handle_event(void *ctx, void *data, size_t data_sz) {
break;
}
case CONTAINER_SYSCALL: {
printf("%-8u %-22s %-10lld %-10lld %-16s\n", e->syscall_data.pid,
if(env.show){
printf("%-10lld %-10lld\n",e->syscall_data.delay,e->syscall_data.syscall_id);
break;
}else{
printf("%-8u %-22s %-10lld %-10lld %-16s\n", e->syscall_data.pid,
e->syscall_data.container_id, e->syscall_data.delay,
e->syscall_data.syscall_id, e->syscall_data.comm);
break;
break;
}
}
case HALT_POLL: {
// 使用 e->halt_poll_data 访问 HALT_POLL 特有成员
Expand Down Expand Up @@ -779,8 +783,12 @@ static int print_event_head(struct env *env) {
"VAILD?");
break;
case CONTAINER_SYSCALL:
printf("%-8s %-22s %-9s %10s %-16s\n", "PID", "CONTAINER_ID",
"DELAY(us)", "SYSCALLID", "COMM");
if(env->show){
printf("%-9s %10s\n", "DELAY(us)","SYSCALLID");
}else{
printf("%-8s %-22s %-9s %10s %-16s\n", "PID", "CONTAINER_ID",
"DELAY(us)", "SYSCALLID", "COMM");
}
break;
case EXIT:
//可视化调整输出格式
Expand Down Expand Up @@ -1242,7 +1250,7 @@ void print_map_and_check_error(int (*print_func)(struct kvm_watcher_bpf *),
void print_logo() {
char *logo = LOGO_STRING;
char command[512];
sprintf(command, "echo \"%s\" | /usr/games/lolcat", logo);
sprintf(command, "bash -c 'echo \"%s\" | /usr/games/lolcat'", logo);
system(command);
}

Expand Down Expand Up @@ -1316,9 +1324,8 @@ int main(int argc, char **argv) {
fprintf(stderr, "Please specify an option using %s.\n", OPTIONS_LIST);
goto cleanup;
}
err = ring_buffer__poll(rb, RING_BUFFER_TIMEOUT_MS /* timeout, ms */);
while (!exiting) {
//err = ring_buffer__poll(rb, RING_BUFFER_TIMEOUT_MS /* timeout, ms */);
err = ring_buffer__poll(rb, RING_BUFFER_TIMEOUT_MS /* timeout, ms */);
if (env.execute_hypercall) {
print_map_and_check_error(print_hc_map, skel, "hypercall", err);
}
Expand Down
70 changes: 45 additions & 25 deletions MagicEyes/src/backend/memory/mem_watcher/bpf/fraginfo.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 102400);
__type(key, u64);
__type(value, struct zone_info);
} zones SEC(".maps");
__type(value, struct pgdat_info);
} nodes SEC(".maps");

struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 102400);
__type(key, u64);
__type(value, struct pgdat_info);
} nodes SEC(".maps");
__type(value, struct zone_info);
} zones SEC(".maps");


struct {
__uint(type, BPF_MAP_TYPE_HASH);
Expand All @@ -27,6 +28,10 @@ struct {
__type(value, struct ctg_info);
} orders SEC(".maps");

static inline bool populated_zone(struct zone *zone)
{
return zone->present_pages;
}
static void fill_contig_page_info(struct zone *zone, unsigned int suitable_order,
struct contig_page_info *info)
{
Expand All @@ -50,7 +55,6 @@ SEC("kprobe/get_page_from_freelist")
int BPF_KPROBE(get_page_from_freelist, gfp_t gfp_mask, unsigned int order, int alloc_flags,
const struct alloc_context *ac)
{
// bpf_printk("1111");
struct pgdat_info node_info = {};
struct zone_info zone_data = {};

Expand All @@ -59,38 +63,54 @@ int BPF_KPROBE(get_page_from_freelist, gfp_t gfp_mask, unsigned int order, int a
struct zone *z;
int i;
unsigned int a_order;
int valid_nr_zones = 0;

//节点信息
pgdat = BPF_CORE_READ(ac, preferred_zoneref, zone, zone_pgdat);
node_info.node_id = BPF_CORE_READ(pgdat, node_id);
node_info.nr_zones = BPF_CORE_READ(pgdat, nr_zones);
node_info.nr_zones = 0;
node_info.pgdat_ptr = (u64)pgdat;
u64 key = (u64)pgdat;

bpf_map_update_elem(&nodes, &key, &node_info, BPF_ANY);
// bpf_map_update_elem(&nodes, &key, &node_info, BPF_ANY);

//遍历
for (i = 0; i < __MAX_NR_ZONES; i++) {
zref = &pgdat->node_zonelists[0]._zonerefs[i];
z = BPF_CORE_READ(zref, zone);
if ((u64)z == 0) break;
zone_data.zone_ptr = (u64)z;
u64 zone_key = (u64)z;
zone_data.zone_start_pfn = BPF_CORE_READ(z, zone_start_pfn);
zone_data.spanned_pages = BPF_CORE_READ(z, spanned_pages);
zone_data.present_pages = BPF_CORE_READ(z, present_pages);
bpf_probe_read_kernel_str(zone_data.comm, sizeof(zone_data.comm), BPF_CORE_READ(z, name));
for (a_order = 0; a_order <= MAX_ORDER; ++a_order) {
zone_data.order = a_order;
struct order_zone order_key = {};
order_key.order = a_order;
if ((u64)z == 0) break;
order_key.zone_ptr = (u64)z;
struct contig_page_info ctg_info = {};
fill_contig_page_info(z, a_order, &ctg_info);
bpf_map_update_elem(&orders,&order_key,&ctg_info,BPF_ANY);
int zone_node_id = BPF_CORE_READ(z, node);
if (zone_node_id != node_info.node_id) {
continue; // 如果 zone 不属于当前 node,跳过
}
u64 present_pages = BPF_CORE_READ(z, present_pages);
if (present_pages> 0) {
valid_nr_zones++;
zone_data.zone_ptr = (u64)z;
zone_data.node_id=BPF_CORE_READ(z, node);
u64 zone_key = (u64)z;
zone_data.zone_start_pfn = BPF_CORE_READ(z, zone_start_pfn);
zone_data.spanned_pages = BPF_CORE_READ(z, spanned_pages);
zone_data.present_pages = present_pages;
bpf_probe_read_kernel_str(zone_data.comm, sizeof(zone_data.comm), BPF_CORE_READ(z, name));
for (a_order = 0; a_order <= MAX_ORDER; ++a_order) {
zone_data.order = a_order;
struct order_zone order_key = {};
order_key.order = a_order;
order_key.node_id= BPF_CORE_READ(z, node);
if ((u64)z == 0) break;
order_key.zone_ptr = (u64)z;

struct contig_page_info ctg_info = {};
fill_contig_page_info(z, a_order, &ctg_info);
bpf_map_update_elem(&orders,&order_key,&ctg_info,BPF_ANY);
}

bpf_map_update_elem(&zones, &zone_key, &zone_data, BPF_ANY);
}

bpf_map_update_elem(&zones, &zone_key, &zone_data, BPF_ANY);
}
node_info.nr_zones = valid_nr_zones;
bpf_map_update_elem(&nodes, &key, &node_info, BPF_ANY);

return 0;
}
}
Loading

0 comments on commit ef11ece

Please sign in to comment.