Skip to content

Commit

Permalink
Stack_Analyzer: Arrange code (#836)
Browse files Browse the repository at this point in the history
* update map api

Signed-off-by: LiuLingze <[email protected]>

* arrange code

Signed-off-by: LiuLingze <[email protected]>

* fix bug

Signed-off-by: legendlliu <[email protected]>

---------

Signed-off-by: LiuLingze <[email protected]>
Signed-off-by: legendlliu <[email protected]>
Co-authored-by: legendlliu <[email protected]>
  • Loading branch information
GorilaMond and legendlliu authored Jun 24, 2024
1 parent e70dc5e commit 031213f
Show file tree
Hide file tree
Showing 28 changed files with 473 additions and 1,771 deletions.
14 changes: 9 additions & 5 deletions eBPF_Supermarket/Stack_Analyser/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ ALL_LDFLAGS := $(LDFLAGS) $(EXTRA_LDFLAGS)

BIN = $(patsubst src/%.cpp, %, ${wildcard src/*.cpp})
BPF = $(patsubst bpf/%.bpf.c, %, ${wildcard bpf/*.bpf.c})
BPF_OBJ = $(patsubst %,$(OUTPUT)/%.bpf.o,$(BPF))
BPF_SKEL_H = $(patsubst %,$(BPF_SKEL)/%.skel.h,$(BPF))
BPF_WAPPER = $(patsubst %,$(OUTPUT)/%.o,$(BPF))
BIN_OBJ = $(patsubst %,$(OUTPUT)/%.o,$(BIN))

TARGETS = stack_analyzer

Expand Down Expand Up @@ -117,24 +121,24 @@ $(BPFTOOL): $(BPFTOOL_SRC) | $(BPFTOOL_OUTPUT)
$(Q)$(MAKE) ARCH= CROSS_COMPILE= OUTPUT=$(BPFTOOL_OUTPUT)/ -C $(BPFTOOL_SRC) bootstrap

# Build BPF code
$(OUTPUT)/%.bpf.o: bpf/%.bpf.c include/sa_ebpf.h $(LIBBPF_OBJ) $(VMLINUX) | $(OUTPUT) $(BPFTOOL)
$(BPF_OBJ): $(OUTPUT)/%.bpf.o: bpf/%.bpf.c include/ebpf.h $(LIBBPF_OBJ) $(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
$(BPF_SKEL)/%.skel.h: $(OUTPUT)/%.bpf.o | $(OUTPUT) $(BPFTOOL) $(BPF_SKEL)
$(BPF_SKEL_H): $(BPF_SKEL)/%.skel.h: $(OUTPUT)/%.bpf.o | $(OUTPUT) $(BPFTOOL) $(BPF_SKEL)
$(call msg,GEN-SKEL,$@)
$(Q)$(BPFTOOL) gen skeleton $< > $@

$(patsubst %,$(OUTPUT)/%.o,$(BPF)): $(OUTPUT)/%.o: src/bpf_wapper/%.cpp include/bpf_wapper/%.h $(BPF_SKEL)/%.skel.h $(OUTPUT)/eBPFStackCollector.o
$(BPF_WAPPER): $(OUTPUT)/%.o: src/bpf_wapper/%.cpp include/bpf_wapper/%.h $(BPF_SKEL)/%.skel.h $(OUTPUT)/eBPFStackCollector.o
$(call msg,CXX,$@)
$(Q)$(CXX) $(CFLAGS) $(INCLUDES) -c $< -o $@

# Build depending library
$(patsubst %,$(OUTPUT)/%.o,$(BIN)): $(OUTPUT)/%.o: src/%.cpp $(patsubst %,$(BPF_SKEL)/%.skel.h,$(BPF))
$(BIN_OBJ): $(OUTPUT)/%.o: src/%.cpp $(BPF_SKEL_H)
$(call msg,CXX,$@)
$(Q)$(CXX) $(CFLAGS) $(INCLUDES) -c $< -o $@

Expand All @@ -143,7 +147,7 @@ $(OUTPUT)/eBPFStackCollector.o: src/bpf_wapper/eBPFStackCollector.cpp include/bp
$(Q)$(CXX) $(CFLAGS) $(INCLUDES) -c $< -o $@

# Build application binary
$(TARGETS): $(OUTPUT)/eBPFStackCollector.o $(patsubst %,$(OUTPUT)/%.o,$(BIN)) $(patsubst %,$(OUTPUT)/%.o,$(BPF)) $(LIBBPF_OBJ)
$(TARGETS): $(OUTPUT)/eBPFStackCollector.o $(BIN_OBJ) $(BPF_WAPPER) $(LIBBPF_OBJ)
$(call msg,BINARY,$@)
$(Q)$(CXX) $^ $(ALL_LDFLAGS) -lstdc++ -lelf -lz -o $@

Expand Down
2 changes: 1 addition & 1 deletion eBPF_Supermarket/Stack_Analyser/bpf/io.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>

#include "sa_ebpf.h"
#include "ebpf.h"
#include "bpf_wapper/io.h"
#include "task.h"

Expand Down
2 changes: 1 addition & 1 deletion eBPF_Supermarket/Stack_Analyser/bpf/llc_stat.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>

#include "sa_ebpf.h"
#include "ebpf.h"
#include "bpf_wapper/llc_stat.h"
#include "task.h"

Expand Down
8 changes: 4 additions & 4 deletions eBPF_Supermarket/Stack_Analyser/bpf/memleak.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>

#include "sa_ebpf.h"
#include "ebpf.h"
#include "task.h"
#include "bpf_wapper/memleak.h"

Expand All @@ -31,9 +31,9 @@ const volatile bool wa_missing_free = false;
const volatile size_t page_size = 4096;
const volatile bool trace_all = false;

BPF_HASH(pid_size_map, u32, u64); // 记录了对应进程使用malloc,calloc等函数申请内存的大小
BPF_HASH(piddr_meminfo_map, piddr, mem_info); // 记录了每次申请的内存空间的起始地址等信息
BPF_HASH(memptrs_map, u32, u64);
BPF_HASH(pid_size_map, u32, u64, MAX_ENTRIES); // 记录了对应进程使用malloc,calloc等函数申请内存的大小
BPF_HASH(piddr_meminfo_map, piddr, mem_info, MAX_ENTRIES); // 记录了每次申请的内存空间的起始地址等信息
BPF_HASH(memptrs_map, u32, u64, MAX_ENTRIES);

const char LICENSE[] SEC("license") = "GPL";

Expand Down
4 changes: 2 additions & 2 deletions eBPF_Supermarket/Stack_Analyser/bpf/off_cpu.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>

#include "sa_ebpf.h"
#include "ebpf.h"
#include "task.h"

COMMON_MAPS(u32);
COMMON_VALS;
// 记录进程运行的起始时间
BPF_HASH(pid_offTs_map, u32, u64);
BPF_HASH(pid_offTs_map, u32, u64, MAX_ENTRIES/10);

const char LICENSE[] SEC("license") = "GPL";

Expand Down
2 changes: 1 addition & 1 deletion eBPF_Supermarket/Stack_Analyser/bpf/on_cpu.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>

#include "sa_ebpf.h"
#include "ebpf.h"
#include "task.h"

const char LICENSE[] SEC("license") = "GPL";
Expand Down
4 changes: 2 additions & 2 deletions eBPF_Supermarket/Stack_Analyser/bpf/probe.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
#include <bpf/bpf_core_read.h>
#include <bpf/usdt.bpf.h>

#include "sa_ebpf.h"
#include "ebpf.h"
#include "bpf_wapper/probe.h"
#include "task.h"

COMMON_MAPS(time_tuple);
COMMON_VALS;
BPF_HASH(starts, u32, u64);
BPF_HASH(starts, u32, u64, MAX_ENTRIES/10);

static int entry(void *ctx)
{
Expand Down
6 changes: 3 additions & 3 deletions eBPF_Supermarket/Stack_Analyser/bpf/readahead.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>

#include "sa_ebpf.h"
#include "ebpf.h"
#include "task.h"
#include "bpf_wapper/readahead.h"

Expand All @@ -31,8 +31,8 @@
COMMON_MAPS(ra_tuple);
COMMON_VALS;

BPF_HASH(in_ra_map, u32, psid);
BPF_HASH(page_psid_map, struct page *, psid);
BPF_HASH(in_ra_map, u32, psid, MAX_ENTRIES/10);
BPF_HASH(page_psid_map, struct page *, psid, MAX_ENTRIES);

SEC("fentry/page_cache_ra_unbounded") // fentry在内核函数page_cache_ra_unbounded进入时触发的挂载点
int BPF_PROG(page_cache_ra_unbounded)
Expand Down
2 changes: 1 addition & 1 deletion eBPF_Supermarket/Stack_Analyser/bpf/template.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>

#include "sa_ebpf.h"
#include "ebpf.h"
#include "bpf_wapper/template.h"
#include "task.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include <unistd.h>
#include <vector>
#include <string>
#include "sa_user.h"
#include "user.h"

struct Scale
{
Expand Down Expand Up @@ -62,7 +62,7 @@ class StackCollector
uint32_t freq = 49;
uint64_t cgroup = 0;
uint32_t tgid = 0;
int err = 0; // 用于保存错误代码
int err = 0; // 用于保存错误代码

bool ustack = false; // 是否跟踪用户栈
bool kstack = false; // 是否跟踪内核栈
Expand Down Expand Up @@ -94,25 +94,25 @@ class StackCollector
/// @brief 加载、初始化参数并打开指定类型的ebpf程序
/// @param ... 一些ebpf程序全局变量初始化语句
/// @note 失败会使上层函数返回-1
#define EBPF_LOAD_OPEN_INIT(...) \
{ \
skel = skel->open(NULL); \
#define EBPF_LOAD_OPEN_INIT(...) \
{ \
skel = skel->open(NULL); \
CHECK_ERR_RN1(!skel, "Fail to open BPF skeleton"); \
__VA_ARGS__; \
skel->rodata->trace_user = ustack; \
skel->rodata->trace_kernel = kstack; \
skel->rodata->self_tgid = self_tgid; \
skel->rodata->target_tgid = tgid; \
skel->rodata->target_cgroupid = cgroup; \
skel->rodata->freq = freq; \
err = skel->load(skel); \
__VA_ARGS__; \
skel->rodata->trace_user = ustack; \
skel->rodata->trace_kernel = kstack; \
skel->rodata->self_tgid = self_tgid; \
skel->rodata->target_tgid = tgid; \
skel->rodata->target_cgroupid = cgroup; \
skel->rodata->freq = freq; \
err = skel->load(skel); \
CHECK_ERR_RN1(err, "Fail to load BPF skeleton"); \
obj = skel->obj; \
obj = skel->obj; \
}

#define ATTACH_PROTO \
{ \
err = skel->attach(skel); \
#define ATTACH_PROTO \
{ \
err = skel->attach(skel); \
CHECK_ERR_RN1(err, "Failed to attach BPF skeleton"); \
}

Expand Down
9 changes: 9 additions & 0 deletions eBPF_Supermarket/Stack_Analyser/include/cgroup.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <stdint.h>
struct cgid_file_handle
{
// struct file_handle handle;
unsigned int handle_bytes;
int handle_type;
uint64_t cgid;
};
uint64_t get_cgroupid(const char *pathname);
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,17 @@
#define CONTAINER_ID_LEN (128)

/// @brief 栈计数的键,可以唯一标识一个用户内核栈
typedef struct {
typedef struct
{
__u32 pid;
__s32 ksid, usid;
} psid;

typedef struct {
typedef struct
{
__u32 pid;
__u32 tgid;
char comm[COMM_LEN];
} task_info;

#define _COL_PREFIX "\033["
#define _BLUE _COL_PREFIX "1;34m"
#define _GREEN _COL_PREFIX "1;32m"
#define _RED _COL_PREFIX "1;35m"
#define _ERED _COL_PREFIX "1;31m"
#define _RE _COL_PREFIX "0m"

#endif
25 changes: 0 additions & 25 deletions eBPF_Supermarket/Stack_Analyser/include/dt_elf.h

This file was deleted.

Loading

0 comments on commit 031213f

Please sign in to comment.