From dcb5d0c15bfb17d6edbea2bd845ec553c1b4bd41 Mon Sep 17 00:00:00 2001 From: gaoyixiang1 <45355878+gaoyixiang1@users.noreply.github.com> Date: Wed, 24 Jul 2024 10:37:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=93=E5=8D=B0=E6=AF=8F=E4=B8=AAzone?= =?UTF-8?q?=E4=B8=AD=E6=AF=8F=E4=B8=AAorder=E5=AF=B9=E5=BA=94=E7=9A=84cont?= =?UTF-8?q?ig=5Fpage=5Finfo=20(#866)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: gaoyixiang1 <1739037263@qq.com> --- .../mem_watcher/bpf/fraginfo.bpf.c | 9 +++- .../mem_watcher/include/fraginfo.h | 2 + .../mem_watcher/mem_watcher.c | 47 +++++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/eBPF_Supermarket/Memory_Subsystem/mem_watcher/bpf/fraginfo.bpf.c b/eBPF_Supermarket/Memory_Subsystem/mem_watcher/bpf/fraginfo.bpf.c index c05191dac..342411373 100644 --- a/eBPF_Supermarket/Memory_Subsystem/mem_watcher/bpf/fraginfo.bpf.c +++ b/eBPF_Supermarket/Memory_Subsystem/mem_watcher/bpf/fraginfo.bpf.c @@ -50,7 +50,7 @@ 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"); + // bpf_printk("1111"); struct pgdat_info node_info = {}; struct zone_info zone_data = {}; @@ -82,11 +82,16 @@ int BPF_KPROBE(get_page_from_freelist, gfp_t gfp_mask, unsigned int order, int a 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); - } + bpf_printk("Order: %d, Free pages: %lu, Free blocks total: %lu, Free blocks suitable: %lu", + a_order, ctg_info.free_pages, ctg_info.free_blocks_total, ctg_info.free_blocks_suitable); + bpf_printk("2"); + } + bpf_map_update_elem(&zones, &zone_key, &zone_data, BPF_ANY); } diff --git a/eBPF_Supermarket/Memory_Subsystem/mem_watcher/include/fraginfo.h b/eBPF_Supermarket/Memory_Subsystem/mem_watcher/include/fraginfo.h index c2c15f9a9..530e62065 100644 --- a/eBPF_Supermarket/Memory_Subsystem/mem_watcher/include/fraginfo.h +++ b/eBPF_Supermarket/Memory_Subsystem/mem_watcher/include/fraginfo.h @@ -17,6 +17,8 @@ struct zone_info { u64 zone_ptr; u64 zone_start_pfn; + //spanned_pages: 代表的是这个zone中所有的页,包含空洞,计算公式是: zone_end_pfn - zone_start_pfn + //present_pages: 代表的是这个zone中可用的所有物理页,计算公式是:spanned_pages-hole_pages u64 spanned_pages; u64 present_pages; char comm[32]; diff --git a/eBPF_Supermarket/Memory_Subsystem/mem_watcher/mem_watcher.c b/eBPF_Supermarket/Memory_Subsystem/mem_watcher/mem_watcher.c index 6f851f007..e317bd445 100644 --- a/eBPF_Supermarket/Memory_Subsystem/mem_watcher/mem_watcher.c +++ b/eBPF_Supermarket/Memory_Subsystem/mem_watcher/mem_watcher.c @@ -59,7 +59,24 @@ struct allocation __u64 size; size_t count; }; +// ============================= fraginfo==================================== +struct order_entry { + struct order_zone okey; + struct ctg_info oinfo; +}; + +int compare_entries(const void *a, const void *b) { + struct order_entry *entryA = (struct order_entry *)a; + struct order_entry *entryB = (struct order_entry *)b; + if (entryA->okey.zone_ptr != entryB->okey.zone_ptr) { + return (entryA->okey.zone_ptr < entryB->okey.zone_ptr) ? -1 : 1; + } else { + return (entryA->okey.order < entryB->okey.order) ? -1 : 1; + } +} + +// ============================= fraginfo==================================== static struct allocation *allocs; static volatile bool exiting = false; @@ -1125,6 +1142,33 @@ void print_zones(int fd) { } } +void print_orders(int fd) { + struct order_zone okey = {}; + struct ctg_info oinfo; + struct order_entry entries[256]; + int entry_count = 0; + + while (bpf_map_get_next_key(fd, &okey, &okey) == 0) { + if (bpf_map_lookup_elem(fd, &okey, &oinfo) == 0) { + entries[entry_count].okey = okey; + entries[entry_count].oinfo = oinfo; + entry_count++; + } + } + + //排序 + qsort(entries, entry_count, sizeof(struct order_entry), compare_entries); + + // 打印排序后的 + printf(" Order Zone_PTR Free Pages Free Blocks Total Free Blocks Suitable\n"); + for (int i = 0; i < entry_count; i++) { + printf(" %-8u 0x%-25llx %-20lu %-20lu %-20lu\n", + entries[i].okey.order, entries[i].okey.zone_ptr, entries[i].oinfo.free_pages, + entries[i].oinfo.free_blocks_total, entries[i].oinfo.free_blocks_suitable); + } +} + + static int process_fraginfo(struct fraginfo_bpf *skel_fraginfo) { @@ -1146,6 +1190,9 @@ static int process_fraginfo(struct fraginfo_bpf *skel_fraginfo) print_nodes(bpf_map__fd(skel_fraginfo->maps.nodes)); printf("\n"); print_zones(bpf_map__fd(skel_fraginfo->maps.zones)); + printf("\n"); + print_orders(bpf_map__fd(skel_fraginfo->maps.orders)); + printf("\n"); } fraginfo_cleanup: