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

add libbpf_sar #532

Merged
merged 28 commits into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
eb4ad95
modify test_proc.c
zhangzihengya Sep 11, 2023
dab09f8
Merge branch 'linuxkerneltravel:develop' into develop
zhangzihengya Sep 14, 2023
eeef808
Update README.md
zhangzihengya Sep 14, 2023
f6536cc
Update lifecycle_image.bpf.c
zhangzihengya Sep 14, 2023
105adf6
Update test_proc.c
zhangzihengya Sep 16, 2023
ee659ee
Merge branch 'linuxkerneltravel:develop' into develop
zhangzihengya Sep 21, 2023
8126e68
add keytime_image
zhangzihengya Sep 21, 2023
dca348f
Merge branch 'develop' of github.com:zhangzihengya/lmp into develop
zhangzihengya Sep 21, 2023
709ffac
rm text.txt
zhangzihengya Sep 21, 2023
0726cca
Merge branch 'linuxkerneltravel:develop' into develop
zhangzihengya Sep 24, 2023
0101fe0
Merge branch 'linuxkerneltravel:develop' into develop
zhangzihengya Sep 26, 2023
40a1888
add libbpf_sar
zhangzihengya Sep 26, 2023
9388e55
Update system_cpu.yml
zhangzihengya Sep 26, 2023
ca34bf2
Update libbpf_sar.c
zhangzihengya Sep 26, 2023
f14f044
Update system_cpu.yml
zhangzihengya Sep 26, 2023
49d4e43
Update system_cpu.yml
zhangzihengya Sep 26, 2023
675330e
Update libbpf_sar.c
zhangzihengya Sep 26, 2023
6dad0a1
Update system_cpu.yml
zhangzihengya Sep 26, 2023
33363cf
Update libbpf_sar.c
zhangzihengya Sep 26, 2023
8260551
Update libbpf_sar.c
zhangzihengya Sep 26, 2023
6b5c6bb
Update keytime_image.c
zhangzihengya Sep 26, 2023
a09c40b
Update keytime_image.h
zhangzihengya Sep 26, 2023
d5e0488
md libbpf_sar.c
zhangzihengya Sep 26, 2023
d848541
Merge branch 'linuxkerneltravel:develop' into develop
zhangzihengya Sep 27, 2023
345cb16
modify detail
zhangzihengya Sep 27, 2023
1adc62a
Update README.md
zhangzihengya Sep 28, 2023
8ff713e
Update README.md
zhangzihengya Sep 28, 2023
b730219
Update README.md
zhangzihengya Oct 6, 2023
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
16 changes: 16 additions & 0 deletions .github/workflows/system_cpu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,19 @@ jobs:
uname -r
./run.sh

libbpf_sar-project-build-and-test:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3

- name: Install dependencies and Init Env
run: |
sudo apt update
sudo apt install libbpf-dev clang llvm libelf-dev libpcap-dev gcc-multilib build-essential
git submodule update --init --recursive

- name: Run libbpf_sar
run: |
cd eBPF_Supermarket/CPU_Subsystem/libbpf_sar
make libbpf_sar
sudo ./libbpf_sar -t 1
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@
[submodule "eBPF_Hub/libbpf-bootstrap-android"]
path = eBPF_Hub/libbpf-bootstrap-android
url = https://github.com/JiaHuann/libbpf-bootstrap-android
helight marked this conversation as resolved.
Show resolved Hide resolved
[submodule "eBPF_Supermarket/CPU_Subsystem/libbpf_sar/libbpf-bootstrap"]
path = eBPF_Supermarket/CPU_Subsystem/libbpf_sar/libbpf-bootstrap
url = https://github.com/libbpf/libbpf-bootstrap.git
155 changes: 155 additions & 0 deletions eBPF_Supermarket/CPU_Subsystem/libbpf_sar/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
# Copyright 2023 The LMP Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://github.com/linuxkerneltravel/lmp/blob/develop/LICENSE
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# author: [email protected]
#
# compile the current folder code

# 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_OBJ := $(abspath $(OUTPUT)/libbpf.a)
BPFTOOL_OUTPUT ?= $(abspath $(OUTPUT)/bpftool)
BPFTOOL ?= $(BPFTOOL_OUTPUT)/bootstrap/bpftool
LIBBLAZESYM_SRC := $(abspath libbpf-bootstrap/blazesym/)
LIBBLAZESYM_INC := $(abspath $(LIBBLAZESYM_SRC)/include)
LIBBLAZESYM_OBJ := $(abspath $(OUTPUT)/libblazesym.a)
ARCH ?= $(shell uname -m | sed 's/x86_64/x86/' \
| sed 's/arm.*/arm/' \
| sed 's/aarch64/arm64/' \
| sed 's/ppc64le/powerpc/' \
| sed 's/mips.*/mips/' \
| sed 's/riscv64/riscv/' \
| sed 's/loongarch64/loongarch/')
VMLINUX := libbpf-bootstrap/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)
CFLAGS := -g -Wall
ALL_LDFLAGS := $(LDFLAGS) $(EXTRA_LDFLAGS)

APPS = libbpf_sar

CARGO ?= $(shell which cargo)
ifeq ($(strip $(CARGO)),)
BZS_APPS :=
else
BZS_APPS := profile
APPS += $(BZS_APPS)
# Required by libblazesym
ALL_LDFLAGS += -lrt -ldl -lpthread -lm
endif

# Get Clang's default includes on this system. We'll explicitly add these dirs
# to the includes list when compiling with `-target bpf` because otherwise some
# architecture-specific dirs will be "missing" on some architectures/distros -
# headers such as asm/types.h, asm/byteorder.h, asm/socket.h, asm/sockios.h,
# sys/cdefs.h etc. might be missing.
#
# Use '-idirafter': Don't interfere with include mechanics except where the
# build would have failed anyways.
CLANG_BPF_SYS_INCLUDES ?= $(shell $(CLANG) -v -E - </dev/null 2>&1 \
| sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }')

ifeq ($(V),1)
Q =
msg =
else
Q = @
msg = @printf ' %-8s %s%s\n' \
"$(1)" \
"$(patsubst $(abspath $(OUTPUT))/%,%,$(2))" \
"$(if $(3), $(3))";
MAKEFLAGS += --no-print-directory
endif

define allow-override
$(if $(or $(findstring environment,$(origin $(1))),\
$(findstring command line,$(origin $(1)))),,\
$(eval $(1) = $(2)))
endef

$(call allow-override,CC,$(CROSS_COMPILE)cc)
$(call allow-override,LD,$(CROSS_COMPILE)ld)

.PHONY: all
all: $(APPS)

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

$(OUTPUT) $(OUTPUT)/libbpf $(BPFTOOL_OUTPUT):
$(call msg,MKDIR,$@)
$(Q)mkdir -p $@

# Build libbpf
$(LIBBPF_OBJ): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(OUTPUT)/libbpf
$(call msg,LIB,$@)
$(Q)$(MAKE) -C $(LIBBPF_SRC) BUILD_STATIC_ONLY=1 \
OBJDIR=$(dir $@)/libbpf DESTDIR=$(dir $@) \
INCLUDEDIR= LIBDIR= UAPIDIR= \
install

# Build bpftool
$(BPFTOOL): | $(BPFTOOL_OUTPUT)
$(call msg,BPFTOOL,$@)
$(Q)$(MAKE) ARCH= CROSS_COMPILE= OUTPUT=$(BPFTOOL_OUTPUT)/ -C $(BPFTOOL_SRC) bootstrap


$(LIBBLAZESYM_SRC)/target/release/libblazesym.a::
$(Q)cd $(LIBBLAZESYM_SRC) && $(CARGO) build --release

$(LIBBLAZESYM_OBJ): $(LIBBLAZESYM_SRC)/target/release/libblazesym.a | $(OUTPUT)
$(call msg,LIB, $@)
$(Q)cp $(LIBBLAZESYM_SRC)/target/release/libblazesym.a $@

# Build BPF code
$(OUTPUT)/%.bpf.o: %.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)
$(call msg,GEN-SKEL,$@)
$(Q)$(BPFTOOL) gen skeleton $< > $@

# 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 $(filter %.c,$^) -o $@

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

$(BZS_APPS): $(LIBBLAZESYM_OBJ)

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

# delete failed targets
.DELETE_ON_ERROR:

# keep intermediate (.skel.h, .bpf.o, etc) targets
.SECONDARY:
Submodule libbpf-bootstrap added at b9b368
54 changes: 54 additions & 0 deletions eBPF_Supermarket/CPU_Subsystem/libbpf_sar/libbpf_sar.bpf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2023 The LMP Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://github.com/linuxkerneltravel/lmp/blob/develop/LICENSE
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// author: [email protected]
//
// kernel-mode code for libbpf sar

#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_core_read.h>
#include <bpf/bpf_tracing.h>
#include "libbpf_sar.h"

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

const volatile long long unsigned int forks_addr = 0;

// 计数表格,第0项为上下文切换次数,第1项为总共新建进程个数
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__uint(max_entries, 3);
__type(key, int);
__type(value, u64);
} countMap SEC(".maps");

// 统计fork数
SEC("kprobe/finish_task_switch.isra.0")
int kprobe__finish_task_switch(struct pt_regs *ctx)
{
u32 key = 0;
u64 val, *valp = NULL;
unsigned long total_forks;

if(forks_addr !=0){
valp = (u64 *)forks_addr;
bpf_probe_read_kernel(&total_forks, sizeof(unsigned long), valp);
key = 1;
val = total_forks;
bpf_map_update_elem(&countMap,&key,&val,BPF_ANY);
}

return 0;
}
Loading