Skip to content

Commit

Permalink
Merge pull request #909 from gaoyixiang1/develop
Browse files Browse the repository at this point in the history
获取numa架构下所有的内存节点信息
  • Loading branch information
chenamy2017 authored Sep 23, 2024
2 parents 8607cc5 + dd3b9ff commit 94fde80
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/ebpf_mem_watcher.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ jobs:
bpftool btf dump file /sys/kernel/btf/vmlinux format c > vmlinux.h
cd ../../mem_watcher
make
sudo ./mem_watcher -N
sudo timeout 20 ./mem_watcher
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 fraginfo vmasnap drsnoop oomkiller
APPS = paf pr procstat sysstat memleak fraginfo vmasnap drsnoop oomkiller numafraginfo

TARGETS= mem_watcher
CARGO ?= $(shell which cargo)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#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 pgdat_info);
} nodes SEC(".maps");

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 pglist_data *pgdat;

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

return 0;
}
43 changes: 43 additions & 0 deletions eBPF_Supermarket/Memory_Subsystem/mem_watcher/mem_watcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "procstat.skel.h"
#include "sysstat.skel.h"
#include "fraginfo.skel.h"
#include "numafraginfo.skel.h"
#include "memleak.skel.h"
#include "vmasnap.skel.h"
#include "drsnoop.skel.h"
Expand Down Expand Up @@ -264,6 +265,7 @@ static struct env
bool sysstat; // 是否启用系统内存状态报告
bool memleak; // 是否启用内核态/用户态内存泄漏检测
bool fraginfo; // 是否启用内存碎片信息
bool numafraginfo;
bool vmasnap; // 是否启用虚拟内存区域信息
bool drsnoop;
bool kernel_trace; // 是否启用内核态跟踪
Expand All @@ -283,6 +285,7 @@ static struct env
.sysstat = false, // 默认关闭系统内存状态报告
.memleak = false, // 默认关闭内存泄漏检测
.fraginfo = false, // 默认关闭内存碎片信息
.numafraginfo=false,
.vmasnap = false, // 默认关闭虚拟内存区域信息
.drsnoop = false,
.kernel_trace = true, // 默认启用内核态跟踪
Expand Down Expand Up @@ -338,6 +341,9 @@ static const struct argp_option opts[] = {

{0, 0, 0, 0, "oomkiller:", 15}, // 新增的 oomkiller 选项
{"oomkiller", 'o', 0, 0, "print oomkiller (内存不足时被杀死的进程信息)"},
{0, 0, 0, 0, "numafraginfo:", 16},
{"numafraginfo", 'N', 0, 0, "print numafraginfo"},


{NULL, 'h', NULL, OPTION_HIDDEN, "show the full help"},
{0},
Expand Down Expand Up @@ -400,6 +406,9 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
case 'd':
env.duration = atoi(arg);
break;
case 'N':
env.numafraginfo = true;
break;
default:
return ARGP_ERR_UNKNOWN;
}
Expand Down Expand Up @@ -436,6 +445,7 @@ static int process_procstat(struct procstat_bpf *skel_procstat);
static int process_sysstat(struct sysstat_bpf *skel_sysstat);
static int process_memleak(struct memleak_bpf *skel_memleak, struct env);
static int process_fraginfo(struct fraginfo_bpf *skel_fraginfo);
static int process_numafraginfo(struct numafraginfo_bpf *skel_numafraginfo);
static int process_vmasnap(struct vmasnap_bpf *skel_vmasnap);
static int process_drsnoop(struct drsnoop_bpf *skel_drsnoop);
static int process_oomkiller(struct oomkiller_bpf *skel_oomkiller); // 新增的oomkiller处理函数原型
Expand All @@ -456,6 +466,7 @@ int main(int argc, char **argv)
struct sysstat_bpf *skel_sysstat;
struct memleak_bpf *skel_memleak;
struct fraginfo_bpf *skel_fraginfo;
struct numafraginfo_bpf *skel_numafraginfo;
struct vmasnap_bpf *skel_vmasnap;
struct oomkiller_bpf *skel_oomkiller;
struct drsnoop_bpf *skel_drsnoop;
Expand Down Expand Up @@ -485,6 +496,9 @@ int main(int argc, char **argv)
{
PROCESS_SKEL(skel_fraginfo, fraginfo);
}
else if(env.numafraginfo){
PROCESS_SKEL(skel_numafraginfo, numafraginfo);
}
else if (env.vmasnap)
{
PROCESS_SKEL(skel_vmasnap, vmasnap);
Expand Down Expand Up @@ -1491,6 +1505,35 @@ static int process_fraginfo(struct fraginfo_bpf *skel_fraginfo)
fraginfo_bpf__destroy(skel_fraginfo);
return -err;
}
// =========================================numafraginfo=================================================
static int process_numafraginfo(struct numafraginfo_bpf *skel_numafraginfo)
{

int err = numafraginfo_bpf__load(skel_numafraginfo);
if (err)
{
fprintf(stderr, "Failed to load and verify BPF skeleton\n");
goto numafraginfo_cleanup;
}

err = numafraginfo_bpf__attach(skel_numafraginfo);
if (err)
{
fprintf(stderr, "Failed to attach BPF skeleton\n");
goto numafraginfo_cleanup;
}
while (1)
{
sleep(env.interval);
print_nodes(bpf_map__fd(skel_numafraginfo->maps.nodes));
printf("\n");
break;
}

numafraginfo_cleanup:
numafraginfo_bpf__destroy(skel_numafraginfo);
return -err;
}

// ================================================== vmasnap ====================================================================
static void print_find_event_data(int map_fd)
Expand Down

0 comments on commit 94fde80

Please sign in to comment.