Skip to content
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

合并 fraginfo 到 mem_watcher #862

Merged
merged 4 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ebpf_mem_watcher.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ jobs:
cd eBPF_Supermarket/Memory_Subsystem/mem_watcher/
bpftool btf dump file /sys/kernel/btf/vmlinux format c > vmlinux.h
make
sudo timeout 20 ./mem_watcher
sudo ./mem_watcher -f -i 10
2 changes: 1 addition & 1 deletion eBPF_Supermarket/Memory_Subsystem/mem_watcher/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ INCLUDES := -I$(OUTPUT) -I../../libbpf/include/uapi -I$(dir $(VMLINUX)) -I$(LIBB
CFLAGS := -g -Wall
ALL_LDFLAGS := $(LDFLAGS) $(EXTRA_LDFLAGS)

APPS = paf pr procstat sysstat memleak
APPS = paf pr procstat sysstat memleak fraginfo

CARGO ?= $(shell which cargo)
ifeq ($(strip $(CARGO)),)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_core_read.h>
#include <bpf/bpf_tracing.h>
#include "fraginfo.h"

char LICENSE[] SEC("license") = "Dual BSD/GPL";

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, u64);
__type(value, struct pgdat_info);
} nodes 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 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)
{
bpf_printk("1111");
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;

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.pgdat_ptr = (u64)pgdat;
u64 key = (u64)pgdat;

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;
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);
}

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef FRAGINFO_H
#define FRAGINFO_H

#define MAX_ORDER 10
typedef __u64 u64;
struct order_zone{
unsigned int order;
u64 zone_ptr;
};
struct ctg_info {
long unsigned int free_pages;
long unsigned int free_blocks_total;
long unsigned int free_blocks_suitable;
};

struct zone_info
{
u64 zone_ptr;
u64 zone_start_pfn;
u64 spanned_pages;
u64 present_pages;
char comm[32];
unsigned int order;
};

struct pgdat_info
{
u64 pgdat_ptr;
int nr_zones;
int node_id;
};

#endif
Loading
Loading