Skip to content

Commit

Permalink
Merge pull request #885 from Super-Lzzx/develop
Browse files Browse the repository at this point in the history
Mem_watcher:添加oomkiller代码
  • Loading branch information
LinkinPF authored Sep 13, 2024
2 parents 4c019df + 7827a89 commit a4e8e61
Show file tree
Hide file tree
Showing 4 changed files with 276 additions and 128 deletions.
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
APPS = paf pr procstat sysstat memleak fraginfo vmasnap oomkiller

TARGETS= mem_watcher
CARGO ?= $(shell which cargo)
Expand Down
51 changes: 51 additions & 0 deletions eBPF_Supermarket/Memory_Subsystem/mem_watcher/bpf/oomkiller.bpf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>
#include "mem_watcher.h"

char __license[] SEC("license") = "Dual MIT/GPL";

struct {
__uint(type, BPF_MAP_TYPE_RINGBUF);
__uint(max_entries, 1 << 24);
} events SEC(".maps");

SEC("kprobe/oom_kill_process")
int BPF_KPROBE(oom_kill_process, struct oom_control *oc, const char *message) {
// 打印 OOM 触发信息
bpf_printk("oom_kill_process triggered\n");

// 准备存储事件数据
struct event *task_info;
task_info = bpf_ringbuf_reserve(&events, sizeof(struct event), 0);
if (!task_info) {
return 0;
}

// 获取被 OOM 杀死的进程
struct task_struct *p;
bpf_probe_read(&p, sizeof(p), &oc->chosen);
bpf_probe_read(&task_info->oomkill_pid, sizeof(task_info->oomkill_pid), &p->pid);

// 获取被杀进程的命令名 (comm)
bpf_probe_read(&task_info->comm, sizeof(task_info->comm), &p->comm);

// 获取触发 OOM 的进程信息
struct task_struct *trigger_task = (struct task_struct *)bpf_get_current_task();
task_info->triggered_pid = BPF_CORE_READ(trigger_task, pid);

// 获取未被杀掉的进程的内存页信息
struct mm_struct *mm;
mm = BPF_CORE_READ(trigger_task, mm);
if (mm) {
task_info->mem_pages = BPF_CORE_READ(mm, total_vm); // 获取进程的虚拟内存页面总数
} else {
task_info->mem_pages = 0; // 如果进程没有分配内存
}

// 提交事件
bpf_ringbuf_submit(task_info, 0);

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,12 @@ struct find_event_t {
unsigned long long vm_end;
};

#endif /* __MEM_WATCHER_H */
/* OOM Killer Event */
struct event {
uint32_t triggered_pid; // 触发 OOM 的进程 PID
uint32_t oomkill_pid; // 被 OOM 杀死的进程 PID
uint32_t mem_pages; // 没有被杀掉的进程所使用的内存页数
char comm[TASK_COMM_LEN]; // 被杀死进程的命令名
};

#endif /* __MEM_WATCHER_H */
Loading

0 comments on commit a4e8e61

Please sign in to comment.