瀏覽代碼

Fixed #TASK_QT-11100 构建适配

Carl 1 年之前
父節點
當前提交
5bdc68d468
共有 5 個文件被更改,包括 171 次插入68 次删除
  1. 24 24
      Makefile
  2. 79 30
      ebpftracer/Makefile
  3. 32 0
      ebpftracer/bindata_linux_amd64.go
  4. 32 0
      ebpftracer/bindata_linux_arm64.go
  5. 4 14
      ebpftracer/tracer.go

+ 24 - 24
Makefile

@@ -1,29 +1,29 @@
-.PHONY: all
-all: lint test
+FILTER=
+PARAMS=
+ifeq ($(debug),1)
+PARAMS+=debug=1
+endif
 
-.PHONY: test
-test: go-test
+ifdef pid
+	ifneq ($(pid),0)
+		PARAMS+=pid=$(pid)
+		FILTER=FILTER_PID=${pid}
+	endif
+endif
 
-.PHONY: lint
-lint: go-mod go-vet go-fmt go-imports
+all: c-build go-build
 
-.PHONY: go-mod
-go-mod:
-	go mod tidy
+build:
+	CGO_ENABLED=1 go build -gcflags="all=-N -l" -buildvcs=false -o euspace
+c:
+	docker exec -it 62d0676aa0b7 sh -c 'cd /opt/github/euspace/ebpftracer && make all ${PARAMS}'
+c-build: c
 
-.PHONY: go-vet
-go-vet:
-	go vet ./...
+go-build:
+	docker exec -it 62d0676aa0b7 bash -c 'cd /opt/github/euspace && source ~/.g/env && make build'
+go: go-build run
 
-.PHONY: go-fmt
-go-fmt:
-	gofmt -w .
-
-.PHONY: go-imports
-go-imports:
-	go install golang.org/x/tools/cmd/goimports@latest
-	goimports -w .
-
-.PHONY: go-test
-go-test:
-	go test ./...
+run:
+	ssh [email protected] 'cd /opt/github/euspace && TRACES_ENDPOINT=http://10.0.12.192:18080/docp/api/v2/data/receive ${FILTER} ./euspace --listen="0.0.0.0:8123"'
+send_run:
+	ssh [email protected] 'cd /opt/github/euspace && SEND=1 TRACES_ENDPOINT=http://10.0.12.192:18080/docp/api/v2/data/receive ${FILTER} ./euspace --listen="0.0.0.0:8123"'

+ 79 - 30
ebpftracer/Makefile

@@ -1,38 +1,87 @@
-build:
-	@echo ===BUILDING===
-	docker rmi -f ebpftracer
-	docker build -t ebpftracer --progress plain .
-	docker run --rm --name ebpftracer ebpftracer cat /tmp/ebpf/ebpf.go > ./ebpf.go
-	@echo
-
-test: test_vm1 test_vm2 test_vm3 test_vm4 test_vm5
-
-define test_in_vm
-	@echo ===TESTING IN $(1)===
-	vagrant ssh $(1) -c "uname -r && cd /tmp/src && sudo VM=$(1) go test -p 1 -count 1 -v ./ebpftracer/..."
-	@echo
-endef
+# Define compiler and options
+CC = /usr/bin/clang
+LLC = /usr/bin/llc
+LLVM_STRIP = /usr/bin/llvm-strip
+
+# Define source and output directories
+SRC = ./ebpf/ebpf.c
+
+ifeq ($(debug),1)
+DEBUG_MODE=-D_DEBUG_MODE
+endif
+
+ARCH ?= $(shell uname -m | sed -e s/x86_64/x86_64/ \
+				  -e s/aarch64.\*/arm64/)
+MACHINE_ARCH := $(ARCH)
+GO_ARCH := $(MACHINE_ARCH)
+
+ARCH_TAG=-D__TARGET_ARCH_x86 -D__x86_64__
+
+ifeq ($(MACHINE_ARCH),x86_64)
+        MACHINE_ARCH := amd64
+		GO_ARCH := amd64
+endif
+ifeq ($(MACHINE_ARCH),amd64)
+        MACHINE_ARCH := amd64
+		GO_ARCH := amd64
+endif
 
-test_vm1:
-	$(call test_in_vm,ubuntu1810)
+ifeq ($(MACHINE_ARCH),aarch64)
+        MACHINE_ARCH := arm64
+		GO_ARCH := arm64
+		ARCH_TAG=-D__TARGET_ARCH_arm64 -D__aarch64__ -D__AARCH64EB__
 
-test_vm2:
-	$(call test_in_vm,ubuntu2004)
+endif
 
-test_vm3:
-	$(call test_in_vm,ubuntu2010)
+ifeq ($(MACHINE_ARCH),arm64)
+        MACHINE_ARCH := arm64
+		GO_ARCH := arm64
+		ARCH_TAG=-D__TARGET_ARCH_arm64 -D__aarch64__ -D__AARCH64EB__
+endif
+
+OBJ_DIR = ebpf/bin/$(MACHINE_ARCH)
+
+# Define flags
+CFLAGS = -I. -Ivmlinux -Iinclude -Iebpf/include -Iebpf/utrace/go/include -Iebpf/utrace/java/include \
+	-D__BPF_TRACING__ -D GROUP_LEADER_OFFSET_OVERRIDE=0 -DSTART_BOOTTIME_OFFSET_OVERRIDE=0 \
+	-DSTART_BOOTTIME_VARNAME=real_start_time -std=gnu99 -Wimplicit-function-declaration \
+	-ffreestanding -fno-builtin -Wall -Wno-deprecated-declarations \
+	-Wno-gnu-variable-sized-type-not-at-end -Wno-pragma-once-outside-header \
+	-Wno-address-of-packed-member -Wno-unknown-warning-option \
+	-fno-color-diagnostics -fno-unwind-tables -fno-stack-protector \
+	-Wno-unused-variable \
+	$(ARCH_TAG) \
+	$(DEBUG_MODE) \
+	-fno-asynchronous-unwind-tables -g -O2 -emit-llvm
+
+# Define kernel versions and corresponding object files
+KERNEL_VERSIONS = 512
+#KERNEL_VERSIONS = 512 506 420 416
+
+define MAP_VERSION
+$(if $(filter $1,$(VERSION_512)),v5.12,$(if $(filter $1,$(VERSION_506)),v5.6,$(if $(filter $1,$(VERSION_420)),v4.20,$(if $(filter $1,$(VERSION_416)),v4.16,unknown))))
+endef
+# 定义版本值
+VERSION_512 = 512
+VERSION_506 = 506
+VERSION_420 = 420
+VERSION_416 = 416
 
-test_vm4:
-	$(call test_in_vm,ubuntu2110)
+# Create necessary directories
+$(OBJ_DIR):
+	mkdir -p $(OBJ_DIR)
 
-test_vm5:
-	$(call test_in_vm,ubuntu2204)
+all: $(KERNEL_VERSIONS:%=$(OBJ_DIR)/%_x86.o)
+# Rule to compile and process eBPF for each kernel version
+$(OBJ_DIR)/%_x86.o: $(OBJ_DIR)/%_$(MACHINE_ARCH).ll
+	$(LLC) -march=bpf -filetype=obj -mcpu=v2 -o $(OBJ_DIR)/$(call MAP_VERSION,$*)_$(MACHINE_ARCH).o $<
+	$(LLVM_STRIP) -g $(OBJ_DIR)/$(call MAP_VERSION,$*)_$(MACHINE_ARCH).o
 
-vms_start:
-	vagrant up
+# Rule to generate LLVM IR from eBPF source
+$(OBJ_DIR)/%_$(MACHINE_ARCH).ll: $(SRC) | $(OBJ_DIR)
+	$(CC) $(CFLAGS) -D__KERNEL_FROM=$* -o $@ -c $(SRC)
 
-vms_stop:
-	vagrant suspend
+clean:
+	rm -f $(OBJ_DIR)/*_$(MACHINE_ARCH).ll $(OBJ_DIR)/*_$(MACHINE_ARCH).o
 
-vms_delete:
-	vagrant destroy
+.PHONY: all clean

+ 32 - 0
ebpftracer/bindata_linux_amd64.go

@@ -0,0 +1,32 @@
+//go:build linux && amd64
+// +build linux,amd64
+
+package ebpftracer
+
+import (
+	"embed"
+	"fmt"
+	"golang.org/x/mod/semver"
+)
+
+//go:embed ebpf/bin/amd64
+var binData embed.FS
+
+func EbpfCode(v string) (string, []byte, error) {
+	var filePath string
+	var err error
+	versions := []string{"v5.12", "v5.6", "v4.20", "v4.16"}
+	for _, version := range versions {
+		if semver.Compare(v, version) >= 0 {
+			filePath = fmt.Sprintf("ebpf/bin/amd64/%s_amd64.o", version)
+			break
+		}
+	}
+
+	// Construct the full path to the desired file
+	data, err := binData.ReadFile(filePath)
+	if err != nil {
+		return "", nil, err
+	}
+	return filePath, data, nil
+}

+ 32 - 0
ebpftracer/bindata_linux_arm64.go

@@ -0,0 +1,32 @@
+//go:build linux && arm64
+// +build linux,arm64
+
+package ebpftracer
+
+import (
+	"embed"
+	"fmt"
+	"golang.org/x/mod/semver"
+)
+
+//go:embed ebpf/bin/arm64
+var binData embed.FS
+
+func EbpfCode(v string) (string, []byte, error) {
+	var filePath string
+	var err error
+	versions := []string{"v5.12", "v5.6", "v4.20", "v4.16"}
+	for _, version := range versions {
+		if semver.Compare(v, version) >= 0 {
+			filePath = fmt.Sprintf("ebpf/bin/arm64/%s_arm64.o", version)
+			break
+		}
+	}
+
+	// Construct the full path to the desired file
+	data, err := binData.ReadFile(filePath)
+	if err != nil {
+		return "", nil, err
+	}
+	return filePath, data, nil
+}

+ 4 - 14
ebpftracer/tracer.go

@@ -8,7 +8,6 @@ import (
 	"errors"
 	"fmt"
 	"os"
-	"runtime"
 	"strconv"
 	"strings"
 	"time"
@@ -21,7 +20,6 @@ import (
 	"github.com/coroot/coroot-node-agent/ebpftracer/tracer"
 	"github.com/coroot/coroot-node-agent/proc"
 	"github.com/coroot/coroot-node-agent/utils"
-	"golang.org/x/mod/semver"
 	"golang.org/x/sys/unix"
 	"inet.af/netaddr"
 	"k8s.io/klog/v2"
@@ -223,19 +221,11 @@ type perfMap struct {
 }
 
 func (t *Tracer) ebpf(ch chan<- Event) error {
-	if _, ok := ebpfProg[runtime.GOARCH]; !ok {
-		return fmt.Errorf("unsupported architecture: %s", runtime.GOARCH)
-	}
 	kv := "v" + common.KernelMajorMinor(t.kernelVersion)
-	var prg []byte
-	for _, p := range ebpfProg[runtime.GOARCH] {
-		if semver.Compare(kv, p.v) >= 0 {
-			prg = p.p
-			break
-		}
-	}
-	if len(prg) == 0 {
-		return fmt.Errorf("kv is %s, unsupported kernel version: %s", kv, t.kernelVersion)
+	path, prg, err := EbpfCode(kv)
+	klog.Infof("kv is %s, kernel version: %s path: %s\n", kv, t.kernelVersion, path)
+	if len(prg) == 0 || err != nil {
+		return fmt.Errorf("kv is %s, unsupported kernel version: %s path: %s err:<%v>", kv, t.kernelVersion, path, err)
 	}
 	_, debugFsErr := os.Stat("/sys/kernel/debug/tracing")
 	_, traceFsErr := os.Stat("/sys/kernel/tracing")