diff --git a/eBPF_Supermarket/Memory_Subsystem/mem_watcher/include/mem_watcher.h b/eBPF_Supermarket/Memory_Subsystem/mem_watcher/include/mem_watcher.h index 3b65256eb..de3729d1e 100644 --- a/eBPF_Supermarket/Memory_Subsystem/mem_watcher/include/mem_watcher.h +++ b/eBPF_Supermarket/Memory_Subsystem/mem_watcher/include/mem_watcher.h @@ -4,44 +4,45 @@ #define TASK_COMM_LEN 16 #define MAX_FILENAME_LEN 127 -#define ___GFP_DMA 0x01u -#define ___GFP_HIGHMEM 0x02u -#define ___GFP_DMA32 0x04u -#define ___GFP_MOVABLE 0x08u -#define ___GFP_RECLAIMABLE 0x10u -#define ___GFP_HIGH 0x20u -#define ___GFP_IO 0x40u -#define ___GFP_FS 0x80u -#define ___GFP_WRITE 0x100u -#define ___GFP_NOWARN 0x200u -#define ___GFP_RETRY_MAYFAIL 0x400u -#define ___GFP_NOFAIL 0x800u -#define ___GFP_NORETRY 0x1000u -#define ___GFP_MEMALLOC 0x2000u -#define ___GFP_COMP 0x4000u -#define ___GFP_ZERO 0x8000u -#define ___GFP_NOMEMALLOC 0x10000u -#define ___GFP_HARDWALL 0x20000u -#define ___GFP_THISNODE 0x40000u -#define ___GFP_ATOMIC 0x80000u -#define ___GFP_ACCOUNT 0x100000u -#define ___GFP_DIRECT_RECLAIM 0x200000u -#define ___GFP_KSWAPD_RECLAIM 0x400000u +#define ___GFP_DMA 0x01u +#define ___GFP_HIGHMEM 0x02u +#define ___GFP_DMA32 0x04u +#define ___GFP_MOVABLE 0x08u +#define ___GFP_RECLAIMABLE 0x10u +#define ___GFP_HIGH 0x20u +#define ___GFP_IO 0x40u +#define ___GFP_FS 0x80u +#define ___GFP_ZERO 0x100u +#define ___GFP_ATOMIC 0x200u +#define ___GFP_DIRECT_RECLAIM 0x400u +#define ___GFP_KSWAPD_RECLAIM 0x800u +#define ___GFP_WRITE 0x1000u +#define ___GFP_NOWARN 0x2000u +#define ___GFP_RETRY_MAYFAIL 0x4000u +#define ___GFP_NOFAIL 0x8000u +#define ___GFP_NORETRY 0x10000u +#define ___GFP_MEMALLOC 0x20000u +#define ___GFP_COMP 0x40000u +#define ___GFP_NOMEMALLOC 0x80000u +#define ___GFP_HARDWALL 0x100000u +#define ___GFP_THISNODE 0x200000u +#define ___GFP_ACCOUNT 0x400000u +#define ___GFP_ZEROTAGS 0x800000u +#define ___GFP_SKIP_KASAN_POISON 0x1000000u -#define GFP_ATOMIC (__GFP_HIGH|__GFP_ATOMIC|__GFP_KSWAPD_RECLAIM) -#define GFP_KERNEL (__GFP_RECLAIM | __GFP_IO | __GFP_FS) -#define GFP_KERNEL_ACCOUNT (GFP_KERNEL | __GFP_ACCOUNT) -#define GFP_NOWAIT (__GFP_KSWAPD_RECLAIM) -#define GFP_NOIO (__GFP_RECLAIM) -#define GFP_NOFS (__GFP_RECLAIM | __GFP_IO) -#define GFP_USER (__GFP_RECLAIM | __GFP_IO | __GFP_FS | __GFP_HARDWALL) -#define GFP_DMA __GFP_DMA -#define GFP_DMA32 __GFP_DMA32 -#define GFP_HIGHUSER (GFP_USER | __GFP_HIGHMEM) -#define GFP_HIGHUSER_MOVABLE (GFP_HIGHUSER | __GFP_MOVABLE) -#define GFP_TRANSHUGE_LIGHT ((GFP_HIGHUSER_MOVABLE | __GFP_COMP | \ - __GFP_NOMEMALLOC | __GFP_NOWARN) & ~__GFP_RECLAIM) -#define GFP_TRANSHUGE (GFP_TRANSHUGE_LIGHT | __GFP_DIRECT_RECLAIM) +#define GFP_ATOMIC (___GFP_HIGH|___GFP_ATOMIC|___GFP_KSWAPD_RECLAIM) +#define GFP_KERNEL (___GFP_RECLAIMABLE | ___GFP_IO | ___GFP_FS) +#define GFP_KERNEL_ACCOUNT (GFP_KERNEL | ___GFP_ACCOUNT) +#define GFP_NOWAIT (___GFP_KSWAPD_RECLAIM) +#define GFP_NOIO (___GFP_RECLAIMABLE) +#define GFP_NOFS (___GFP_RECLAIMABLE | ___GFP_IO) +#define GFP_USER (___GFP_RECLAIMABLE | ___GFP_IO | ___GFP_FS | ___GFP_HARDWALL) +#define GFP_DMA ___GFP_DMA +#define GFP_DMA32 ___GFP_DMA32 +#define GFP_HIGHUSER (GFP_USER | ___GFP_HIGHMEM) +#define GFP_HIGHUSER_MOVABLE (GFP_HIGHUSER | ___GFP_MOVABLE | ___GFP_SKIP_KASAN_POISON) +#define GFP_TRANSHUGE_LIGHT ((GFP_HIGHUSER_MOVABLE | ___GFP_COMP | ___GFP_NOMEMALLOC | ___GFP_NOWARN) & ~___GFP_RECLAIMABLE) +#define GFP_TRANSHUGE (GFP_TRANSHUGE_LIGHT | ___GFP_DIRECT_RECLAIM) struct paf_event { unsigned long min; diff --git a/eBPF_Supermarket/Memory_Subsystem/mem_watcher/mem_watcher.c b/eBPF_Supermarket/Memory_Subsystem/mem_watcher/mem_watcher.c index e317bd445..22bf3fee4 100644 --- a/eBPF_Supermarket/Memory_Subsystem/mem_watcher/mem_watcher.c +++ b/eBPF_Supermarket/Memory_Subsystem/mem_watcher/mem_watcher.c @@ -39,6 +39,57 @@ #include "blazesym.h" +// 定义标志结构体 +typedef struct { + int flag; + const char *name; +} Flag; + +// 定义所有组合修饰符和单独标志位 +Flag gfp_combined_list[] = { + {GFP_ATOMIC, "GFP_ATOMIC"}, + {GFP_KERNEL, "GFP_KERNEL"}, + {GFP_KERNEL_ACCOUNT, "GFP_KERNEL_ACCOUNT"}, + {GFP_NOWAIT, "GFP_NOWAIT"}, + {GFP_NOIO, "GFP_NOIO"}, + {GFP_NOFS, "GFP_NOFS"}, + {GFP_USER, "GFP_USER"}, + {GFP_DMA, "GFP_DMA"}, + {GFP_DMA32, "GFP_DMA32"}, + {GFP_HIGHUSER, "GFP_HIGHUSER"}, + {GFP_HIGHUSER_MOVABLE, "GFP_HIGHUSER_MOVABLE"}, + {GFP_TRANSHUGE_LIGHT, "GFP_TRANSHUGE_LIGHT"}, + {GFP_TRANSHUGE, "GFP_TRANSHUGE"}, +}; + +Flag gfp_separate_list[] = { + {___GFP_DMA, "___GFP_DMA"}, + {___GFP_HIGHMEM, "___GFP_HIGHMEM"}, + {___GFP_DMA32, "___GFP_DMA32"}, + {___GFP_MOVABLE, "___GFP_MOVABLE"}, + {___GFP_RECLAIMABLE, "___GFP_RECLAIMABLE"}, + {___GFP_HIGH, "___GFP_HIGH"}, + {___GFP_IO, "___GFP_IO"}, + {___GFP_FS, "___GFP_FS"}, + {___GFP_ZERO, "___GFP_ZERO"}, + {___GFP_ATOMIC, "___GFP_ATOMIC"}, + {___GFP_DIRECT_RECLAIM, "___GFP_DIRECT_RECLAIM"}, + {___GFP_KSWAPD_RECLAIM, "___GFP_KSWAPD_RECLAIM"}, + {___GFP_WRITE, "___GFP_WRITE"}, + {___GFP_NOWARN, "___GFP_NOWARN"}, + {___GFP_RETRY_MAYFAIL, "___GFP_RETRY_MAYFAIL"}, + {___GFP_NOFAIL, "___GFP_NOFAIL"}, + {___GFP_NORETRY, "___GFP_NORETRY"}, + {___GFP_MEMALLOC, "___GFP_MEMALLOC"}, + {___GFP_COMP, "___GFP_COMP"}, + {___GFP_NOMEMALLOC, "___GFP_NOMEMALLOC"}, + {___GFP_HARDWALL, "___GFP_HARDWALL"}, + {___GFP_THISNODE, "___GFP_THISNODE"}, + {___GFP_ACCOUNT, "___GFP_ACCOUNT"}, + {___GFP_ZEROTAGS, "___GFP_ZEROTAGS"}, + {___GFP_SKIP_KASAN_POISON, "___GFP_SKIP_KASAN_POISON"}, +}; + static const int perf_max_stack_depth = 127; // stack id 对应的堆栈的深度 static const int stack_map_max_entries = 10240; // 最大允许存储多少个stack_id static __u64 *g_stacks = NULL; @@ -313,6 +364,7 @@ static int handle_event_pr(void *ctx, void *data, size_t data_sz); static int handle_event_procstat(void *ctx, void *data, size_t data_sz); static int handle_event_sysstat(void *ctx, void *data, size_t data_sz); static int attach_uprobes(struct memleak_bpf *skel); +static void print_flag_modifiers(int flag); static int process_paf(struct paf_bpf *skel_paf); static int process_pr(struct pr_bpf *skel_pr); static int process_procstat(struct procstat_bpf *skel_procstat); @@ -786,36 +838,40 @@ static void setup_signals(void) signal(SIGALRM, sig_handler); } -/* -static char* flags(int flag) -{ - if(flag & GFP_ATOMIC) - return "GFP_ATOMIC"; - if(flag & GFP_KERNEL) - return "GFP_KERNEL"; - if(flag & GFP_KERNEL_ACCOUNT) - return "GFP_KERNEL_ACCOUNT"; - if(flag & GFP_NOWAIT) - return "GFP_NOWAIT"; - if(flag & GFP_NOIO ) - return "GFP_NOIO "; - if(flag & GFP_NOFS) - return "GFP_NOFS"; - if(flag & GFP_USER) - return "GFP_USER"; - if(flag & GFP_DMA) - return "GFP_DMA"; - if(flag & GFP_DMA32) - return "GFP_DMA32"; - if(flag & GFP_HIGHUSER) - return "GFP_HIGHUSER"; - if(flag & GFP_HIGHUSER_MOVABLE) - return "GFP_HIGHUSER_MOVABLE"; - if(flag & GFP_TRANSHUGE_LIGHT) - return "GFP_TRANSHUGE_LIGHT"; - return; +static void print_flag_modifiers(int flag) { + char combined[512] = {0}; // 用于保存组合修饰符 + char separate[512] = {0}; // 用于保存单独标志位 + + // 检查组合修饰符 + for (int i = 0; i < sizeof(gfp_combined_list) / sizeof(gfp_combined_list[0]); ++i) { + if ((flag & gfp_combined_list[i].flag) == gfp_combined_list[i].flag) { + strcat(combined, gfp_combined_list[i].name); + strcat(combined, " | "); + } + } + + // 移除最后一个 " | " 字符串的末尾 + if (strlen(combined) > 3) { + combined[strlen(combined) - 3] = '\0'; + } + + // 检查单独标志位 + for (int i = 0; i < sizeof(gfp_separate_list) / sizeof(gfp_separate_list[0]); ++i) { + if (flag & gfp_separate_list[i].flag) { + strcat(separate, gfp_separate_list[i].name); + strcat(separate, " | "); + } + } + + // 移除最后一个 " | " 字符串的末尾 + if (strlen(separate) > 3) { + separate[strlen(separate) - 3] = '\0'; + } + + // 打印组合修饰符和单独标志位 + printf("%-50s %-100s\n", combined, separate); } -*/ + static int handle_event_paf(void *ctx, void *data, size_t data_sz) { const struct paf_event *e = data; @@ -827,8 +883,10 @@ static int handle_event_paf(void *ctx, void *data, size_t data_sz) tm = localtime(&t); strftime(ts, sizeof(ts), "%H:%M:%S", tm); - printf("%-8lu %-8lu %-8lu %-8lu %-8x\n", - e->min, e->low, e->high, e->present, e->flag); + printf("%-8lu %-8lu %-8lu %-8lu %-8x ", + e->min, e->low, e->high, e->present, e->flag); + print_flag_modifiers(e->flag); + printf("\n"); return 0; }