Skip to content

Commit

Permalink
Merge pull request #927 from gaoyixiang1/develop
Browse files Browse the repository at this point in the history
mem_watcher:解决了nr_zones获取的问题,并添加了nuam架构下的内存碎片化指数计算
  • Loading branch information
chenamy2017 authored Oct 23, 2024
2 parents e0f63be + 9403e5f commit d8a170b
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 11 deletions.
89 changes: 86 additions & 3 deletions MagicEyes/src/backend/memory/mem_watcher/bpf/numafraginfo.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,104 @@ struct {
__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 zone_info);
} zones SEC(".maps");


struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 102400);
__type(key,struct order_zone);
__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)
{
unsigned int order;
info->free_pages = 0;
info->free_blocks_total = 0;
info->free_blocks_suitable = 0;
for (order = 0; order <= MAX_ORDER; order++) {
unsigned long blocks;
unsigned long nr_free;
nr_free = BPF_CORE_READ(&zone->free_area[order], nr_free);
blocks = nr_free;
info->free_blocks_total += blocks;
info->free_pages += blocks << order;
if (order >= suitable_order)
info->free_blocks_suitable += blocks << (order - suitable_order);
}
}

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)
{
struct pgdat_info node_info = {};
struct zone_info zone_data = {};

struct pglist_data *pgdat;
struct zoneref *zref;
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_NOEXIST);
// 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;
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);
}
}
node_info.nr_zones = valid_nr_zones;
bpf_map_update_elem(&nodes, &key, &node_info, BPF_ANY);

return 0;
}
}
2 changes: 2 additions & 0 deletions MagicEyes/src/backend/memory/mem_watcher/include/fraginfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ typedef __u64 u64;
struct order_zone{
unsigned int order;
u64 zone_ptr;
int node_id;
};
struct ctg_info {
long unsigned int free_pages;
Expand All @@ -23,6 +24,7 @@ struct zone_info
u64 present_pages;
char comm[32];
unsigned int order;
int node_id;
};

struct pgdat_info
Expand Down
21 changes: 13 additions & 8 deletions MagicEyes/src/backend/memory/mem_watcher/src/mem_watcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -1417,24 +1417,25 @@ void print_zones(int fd)
{
struct zone_info zinfo;
__u64 key = 0, next_key;
printf("%-20s %-20s %-25s %-20s %-20s", " COMM", "ZONE_PTR", "ZONE_PFN ", " SUM_PAGES", "FACT_PAGES ");
printf("%-15s %-30s %-23s %-23s %-20s %-20s", "NODE_ID","ZONE_COMM", "ZONE_PTR", "ZONE_PFN ", " SUM_PAGES", "FACT_PAGES ");
printf("\n");
while (bpf_map_get_next_key(fd, &key, &next_key) == 0)
{
bpf_map_lookup_elem(fd, &next_key, &zinfo);
printf(" %-15s 0x%-25llx %-25llu %-20llu %-15llu\n", zinfo.comm, zinfo.zone_ptr, zinfo.zone_start_pfn, zinfo.spanned_pages, zinfo.present_pages);
printf(" %-15d %-25s 0x%-25llx %-25llu %-20llu %-15llu\n",zinfo.node_id, zinfo.comm, zinfo.zone_ptr, zinfo.zone_start_pfn, zinfo.spanned_pages, zinfo.present_pages);
key = next_key;
}
}
void print_nodes(int fd)
{
struct pgdat_info pinfo;
__u64 key = 0, next_key;
printf(" Node ID PGDAT_PTR NR_ZONES \n");
printf("%-25s %-30s %-23s", "NODE_ID","PGDAT_PTR", "NR_ZONES");
printf("\n");
while (bpf_map_get_next_key(fd, &key, &next_key) == 0)
{
bpf_map_lookup_elem(fd, &next_key, &pinfo);
printf(" %5d 0x%llx %5d\n",
printf(" %-15d 0x%-33llx %-25d\n",
pinfo.node_id, pinfo.pgdat_ptr, pinfo.nr_zones);
key = next_key;
}
Expand All @@ -1460,16 +1461,16 @@ void print_orders(int fd)
qsort(entries, entry_count, sizeof(struct order_entry), compare_entries);

// 打印排序后的
printf(" Order Zone_PTR Free Pages Free Blocks Total Free Blocks Suitable SCOREA SCOREB\n");
printf("%-13s %-32s %-20s %-21s %-20s %-24s %-15s %-20s", "NODE_ID"," Order", "ZONE_PTR", "Free Pages ", "Free Blocks Total", "Free Blocks Suitable","SCOREA","SCOREB");
printf("\n");
for (int i = 0; i < entry_count; i++)
{
int res = __fragmentation_index(entries[i].okey.order, entries[i].oinfo.free_blocks_total, entries[i].oinfo.free_blocks_suitable, entries[i].oinfo.free_pages);
int tmp = unusable_free_index(entries[i].okey.order, entries[i].oinfo.free_blocks_total, entries[i].oinfo.free_blocks_suitable, entries[i].oinfo.free_pages);
// int part1 = res / 1000;
// int dec1 = res % 1000;

int part2 = tmp / 1000;
int dec2 = tmp % 1000;
printf(" %-8u 0x%-25llx %-20lu %-20lu %-20lu %d %d.%03d\n",
printf(" %-15d %-25u 0x%-25llx %-25lu %-20lu %-15lu %-15d %d.%03d\n",entries[i].okey.node_id,
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, res, part2, dec2);
}
Expand Down Expand Up @@ -1528,6 +1529,10 @@ static int process_numafraginfo(struct numafraginfo_bpf *skel_numafraginfo)
sleep(env.interval);
print_nodes(bpf_map__fd(skel_numafraginfo->maps.nodes));
printf("\n");
print_zones(bpf_map__fd(skel_numafraginfo->maps.zones));
printf("\n");
print_orders(bpf_map__fd(skel_numafraginfo->maps.orders));
printf("\n");
break;
}

Expand Down

0 comments on commit d8a170b

Please sign in to comment.