Skip to content

Commit

Permalink
Merge pull request #844 from Super-Lzzx/develop
Browse files Browse the repository at this point in the history
Mem_watcher:修改mem_watcher结构,增加测试代码
  • Loading branch information
chenamy2017 authored Jun 26, 2024
2 parents ab41377 + a44b112 commit 4289a97
Show file tree
Hide file tree
Showing 29 changed files with 97 additions and 132 deletions.
1 change: 0 additions & 1 deletion eBPF_Supermarket/Memory_Subsystem/bpftool
Submodule bpftool deleted from 06c61e
1 change: 0 additions & 1 deletion eBPF_Supermarket/Memory_Subsystem/libbpf
Submodule libbpf deleted from 46eafb
38 changes: 21 additions & 17 deletions eBPF_Supermarket/Memory_Subsystem/mem_watcher/Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
OUTPUT := .output
CLANG ?= clang
LIBBPF_SRC := $(abspath ../libbpf/src)
BPFTOOL_SRC := $(abspath ../bpftool/src)
LIBBPF_SRC := $(abspath ../../lib/libbpf/src)
BPFTOOL_SRC := $(abspath ../../lib/bpftool/src)
LIBBPF_OBJ := $(abspath $(OUTPUT)/libbpf.a)
BPFTOOL_OUTPUT ?= $(abspath $(OUTPUT)/bpftool)
BPFTOOL ?= $(BPFTOOL_OUTPUT)/bootstrap/bpftool
LIBBLAZESYM_SRC := $(abspath ../blazesym/)
LIBBLAZESYM_INC := $(abspath $(LIBBLAZESYM_SRC)/capi/include)
LIBBLAZESYM_INC := $(abspath ../blazesym/capi/include)
LIBBLAZESYM_OBJ := $(abspath $(OUTPUT)/libblazesym_c.a)
ARCH ?= $(shell uname -m | sed 's/x86_64/x86/' \
| sed 's/arm.*/arm/' \
Expand All @@ -20,7 +20,7 @@ VMLINUX := ../vmlinux/$(ARCH)/vmlinux.h
# Use our own libbpf API headers and Linux UAPI headers distributed with
# libbpf to avoid dependency on system-wide headers, which could be missing or
# outdated
INCLUDES := -I$(OUTPUT) -I../libbpf/include/uapi -I$(dir $(VMLINUX)) -I$(LIBBLAZESYM_INC)
INCLUDES := -I$(OUTPUT) -I../../libbpf/include/uapi -I$(dir $(VMLINUX)) -I$(LIBBLAZESYM_INC) -I./include
CFLAGS := -g -Wall
ALL_LDFLAGS := $(LDFLAGS) $(EXTRA_LDFLAGS)

Expand All @@ -30,8 +30,9 @@ CARGO ?= $(shell which cargo)
ifeq ($(strip $(CARGO)),)
BZS_APPS :=
else
BZS_APPS := mem_watcher
APPS += $(BZS_APPS)
BZS_APPS :=
TARGETS= mem_watcher

# Required by libblazesym
ALL_LDFLAGS += -lrt -ldl -lpthread -lm
endif
Expand Down Expand Up @@ -69,12 +70,12 @@ $(call allow-override,CC,$(CROSS_COMPILE)cc)
$(call allow-override,LD,$(CROSS_COMPILE)ld)

.PHONY: all
all: $(APPS)
all: $(TARGETS)

.PHONY: clean
clean:
$(call msg,CLEAN)
$(Q)rm -rf $(OUTPUT) $(APPS)
$(Q)rm -rf $(OUTPUT) $(TARGETS)

$(OUTPUT) $(OUTPUT)/libbpf $(BPFTOOL_OUTPUT):
$(call msg,MKDIR,$@)
Expand Down Expand Up @@ -102,33 +103,36 @@ $(LIBBLAZESYM_OBJ): $(LIBBLAZESYM_SRC)/target/release/libblazesym_c.a | $(OUTPUT
$(Q)cp $(LIBBLAZESYM_SRC)/target/release/libblazesym_c.a $@

# Build BPF code
$(OUTPUT)/%.bpf.o: %.bpf.c $(LIBBPF_OBJ) $(wildcard %.h) $(VMLINUX) | $(OUTPUT) $(BPFTOOL)
$(OUTPUT)/%.bpf.o: bpf/%.bpf.c $(LIBBPF_OBJ) $(wildcard %.h) $(VMLINUX) | $(OUTPUT) $(BPFTOOL)
$(call msg,BPF,$@)
$(Q)$(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \
$(INCLUDES) $(CLANG_BPF_SYS_INCLUDES) \
-c $(filter %.c,$^) -o $(patsubst %.bpf.o,%.tmp.bpf.o,$@)
$(Q)$(BPFTOOL) gen object $@ $(patsubst %.bpf.o,%.tmp.bpf.o,$@)

# Generate BPF skeletons
$(OUTPUT)/%.skel.h: $(OUTPUT)/%.bpf.o | $(OUTPUT) $(BPFTOOL)
.PHONY: $(APPS)
$(APPS): %: $(OUTPUT)/%.bpf.o | $(OUTPUT) $(BPFTOOL)
$(call msg,GEN-SKEL,$@)
$(Q)$(BPFTOOL) gen skeleton $< > $@
$(Q)$(BPFTOOL) gen skeleton $< > $(OUTPUT)/$@.skel.h

# Build user-space code
$(patsubst %,$(OUTPUT)/%.o,$(APPS)): %.o: %.skel.h

$(OUTPUT)/%.o: %.c $(wildcard %.h) | $(OUTPUT)
$(call msg,CC,$@)
$(Q)$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

$(OUTPUT)/$(TARGETS).o: $(TARGETS).c $(APPS) | $(OUTPUT)
$(call msg,CC,$@)
$(Q)$(CC) $(CFLAGS) $(INCLUDES) -c $(filter %.c,$^) -o $@

$(patsubst %,$(OUTPUT)/%.o,$(BZS_APPS)): $(LIBBLAZESYM_OBJ)
$(patsubst %,$(OUTPUT)/%.o,$(TARGETS)): $(LIBBLAZESYM_OBJ)

$(BZS_APPS): $(LIBBLAZESYM_OBJ)
$(TARGETS): $(LIBBLAZESYM_OBJ)

# Build application binary
$(APPS): %: $(OUTPUT)/%.o $(LIBBPF_OBJ) | $(OUTPUT)
$(TARGETS): %: $(OUTPUT)/%.o $(COMMON_OBJ) $(LIBBPF_OBJ) | $(OUTPUT)
$(call msg,BINARY,$@)
$(Q)$(CC) $(CFLAGS) $^ $(ALL_LDFLAGS) -lelf -lz -o $@
$(Q)$(CC) $(CFLAGS) $^ $(ALL_LDFLAGS) -lstdc++ -lelf -lz -o $@

SUCCESS_MESSAGE:
@echo "\e[38;2;255;102;204m _ _ \e[0m"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pid_t user_pid = 0;
SEC("kprobe/finish_task_switch")
int BPF_KPROBE(finish_task_switch, struct task_struct *prev) {
struct procstat_event *e;
struct percpu_counter rss = {};
struct mm_rss_stat rss = {};
struct mm_struct *mms;
long long *t;
pid_t pid = bpf_get_current_pid_tgid() >> 32;
Expand Down Expand Up @@ -65,7 +65,7 @@ int BPF_KPROBE(finish_task_switch, struct task_struct *prev) {
e->nvcsw = BPF_CORE_READ(prev, nvcsw);
e->nivcsw = BPF_CORE_READ(prev, nivcsw);

rss = *BPF_CORE_READ(prev, mm, rss_stat);
rss = BPF_CORE_READ(prev, mm, rss_stat);
t = (long long *)(rss.count);
e->rssfile = *t;
e->rssanon = *(t + 1);
Expand Down
33 changes: 0 additions & 33 deletions eBPF_Supermarket/Memory_Subsystem/mem_watcher/memleak.c

This file was deleted.

16 changes: 0 additions & 16 deletions eBPF_Supermarket/Memory_Subsystem/mem_watcher/paf.c

This file was deleted.

16 changes: 0 additions & 16 deletions eBPF_Supermarket/Memory_Subsystem/mem_watcher/pr.c

This file was deleted.

16 changes: 0 additions & 16 deletions eBPF_Supermarket/Memory_Subsystem/mem_watcher/procstat.c

This file was deleted.

14 changes: 0 additions & 14 deletions eBPF_Supermarket/Memory_Subsystem/mem_watcher/sysstat.c

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Compiler and flags
CC = gcc
CFLAGS = -Wall

# Source files
C_SRC = pr_test.c

# Output binary
C_OUT = pr_test

.PHONY: all clean run

all: $(C_OUT)

$(C_OUT): $(C_SRC)
$(CC) $(CFLAGS) $< -o $@

clean:
rm -f $(C_OUT)

run: $(C_OUT)
sudo ./$(C_OUT)
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#define ALLOC_SIZE 1024*1024*1024 // 分配 512 MB 内存

int main() {
void *memory;
printf("Allocating memory...\n");
memory = malloc(ALLOC_SIZE);
if (!memory) {
perror("Failed to allocate memory");
return -1;
}

// 填充内存以确保页面被分配
printf("Filling memory...\n");
for (size_t i = 0; i < ALLOC_SIZE; ++i) {
((char*)memory)[i] = (char)i;
}

printf("Freeing memory...\n");
free(memory);

// 给内核更多时间处理回收
printf("Sleeping for 10 seconds...\n");
sleep(10); // 增加等待时间到 10 秒

printf("Memory management demo finished.\n");
return 0;
}

// 分配大量内存。
//填充内存以确保页面分配。
//释放内存。
//等待一段时间以允许内核处理任何可能的内存回收。
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
OUTPUT := .output
CLANG ?= clang
LIBBPF_SRC := $(abspath ../libbpf-bootstrap/libbpf/src)
BPFTOOL_SRC := $(abspath ../libbpf-bootstrap/bpftool/src)
LIBBPF_SRC := $(abspath ../../../../lib/libbpf/src)
BPFTOOL_SRC := $(abspath ../../../../lib/bpftool/src)
LIBBPF_OBJ := $(abspath $(OUTPUT)/libbpf.a)
BPFTOOL_OUTPUT ?= $(abspath $(OUTPUT)/bpftool)
BPFTOOL ?= $(BPFTOOL_OUTPUT)/bootstrap/bpftool
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
OUTPUT := .output
CLANG ?= clang
LIBBPF_SRC := $(abspath ../libbpf-bootstrap/libbpf/src)
BPFTOOL_SRC := $(abspath ../libbpf-bootstrap/bpftool/src)
LIBBPF_SRC := $(abspath ../../../../lib/libbpf/src)
BPFTOOL_SRC := $(abspath ../../../../lib/bpftool/src)
LIBBPF_OBJ := $(abspath $(OUTPUT)/libbpf.a)
BPFTOOL_OUTPUT ?= $(abspath $(OUTPUT)/bpftool)
BPFTOOL ?= $(BPFTOOL_OUTPUT)/bootstrap/bpftool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
OUTPUT := .output
CLANG ?= clang
LLVM_STRIP ?= llvm-strip
LIBBPF_SRC := $(abspath ../../libbpf/src)
BPFTOOL_SRC := $(abspath ../../bpftool/src)
LIBBPF_SRC := $(abspath ../../../../lib/libbpf/src)
BPFTOOL_SRC := $(abspath ../../../../lib/bpftool/src)
LIBBPF_OBJ := $(abspath $(OUTPUT)/libbpf.a)
BPFTOOL_OUTPUT ?= $(abspath $(OUTPUT)/bpftool)
BPFTOOL ?= $(BPFTOOL_OUTPUT)/bootstrap/bpftool
Expand Down
4 changes: 2 additions & 2 deletions eBPF_Supermarket/Memory_Subsystem/old_project/paf/Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
OUTPUT := .output
CLANG ?= clang
LIBBPF_SRC := $(abspath ../../libbpf/src)
BPFTOOL_SRC := $(abspath ../../bpftool/src)
LIBBPF_SRC := $(abspath ../../../lib/libbpf/src)
BPFTOOL_SRC := $(abspath ../../../lib/bpftool/src)
LIBBPF_OBJ := $(abspath $(OUTPUT)/libbpf.a)
BPFTOOL_OUTPUT ?= $(abspath $(OUTPUT)/bpftool)
BPFTOOL ?= $(BPFTOOL_OUTPUT)/bootstrap/bpftool
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
OUTPUT := .output
CLANG ?= clang
LIBBPF_SRC := $(abspath ../../libbpf/src)
BPFTOOL_SRC := $(abspath ../../bpftool/src)
LIBBPF_SRC := $(abspath ../../../lib/libbpf/src)
BPFTOOL_SRC := $(abspath ../../../lib/bpftool/src)
LIBBPF_OBJ := $(abspath $(OUTPUT)/libbpf.a)
BPFTOOL_OUTPUT ?= $(abspath $(OUTPUT)/../bpftool)
BPFTOOL ?= $(BPFTOOL_OUTPUT)/bootstrap/bpftool
Expand Down
4 changes: 2 additions & 2 deletions eBPF_Supermarket/Memory_Subsystem/old_project/pr/Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
OUTPUT := .output
CLANG ?= clang
LIBBPF_SRC := $(abspath ../../libbpf/src)
BPFTOOL_SRC := $(abspath ../../bpftool/src)
LIBBPF_SRC := $(abspath ../../../lib/libbpf/src)
BPFTOOL_SRC := $(abspath ../../../lib/bpftool/src)
LIBBPF_OBJ := $(abspath $(OUTPUT)/libbpf.a)
BPFTOOL_OUTPUT ?= $(abspath $(OUTPUT)/bpftool)
BPFTOOL ?= $(BPFTOOL_OUTPUT)/bootstrap/bpftool
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
OUTPUT := .output
CLANG ?= clang
LIBBPF_SRC := $(abspath ../../libbpf/src)
BPFTOOL_SRC := $(abspath ../../bpftool/src)
LIBBPF_SRC := $(abspath ../../../lib/libbpf/src)
BPFTOOL_SRC := $(abspath ../../../lib/bpftool/src)
LIBBPF_OBJ := $(abspath $(OUTPUT)/libbpf.a)
BPFTOOL_OUTPUT ?= $(abspath $(OUTPUT)/bpftool)
BPFTOOL ?= $(BPFTOOL_OUTPUT)/bootstrap/bpftool
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
OUTPUT := .output
CLANG ?= clang
LIBBPF_SRC := $(abspath ../../libbpf/src)
BPFTOOL_SRC := $(abspath ../../bpftool/src)
LIBBPF_SRC := $(abspath ../../../lib/libbpf/src)
BPFTOOL_SRC := $(abspath ../../../lib/bpftool/src)
LIBBPF_OBJ := $(abspath $(OUTPUT)/libbpf.a)
BPFTOOL_OUTPUT ?= $(abspath $(OUTPUT)/bpftool)
BPFTOOL ?= $(BPFTOOL_OUTPUT)/bootstrap/bpftool
Expand Down

0 comments on commit 4289a97

Please sign in to comment.