Skip to content

Commit d3c2906

Browse files
authored
Merge pull request #1438 from dave-tucker/cilium-ebpf
chore(pkg/bpf): Replace libbpfgo with cilium/ebpf
2 parents d116b08 + b582e2c commit d3c2906

File tree

186 files changed

+26249
-5662
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

186 files changed

+26249
-5662
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bpf/include/* linguist-vendored

.github/dependabot.yml

-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ updates:
1717
- dependency-name: github.com/HabanaAI/gohlml
1818
# Ignore go-nvml updates as this will require manual testing
1919
- dependency-name: github.com/NVIDIA/go-nvml
20-
# Ignore libbpfgo updates as this requires changing the base image
21-
- dependency-name: github.com/aquasecurity/libbpfgo
2220
- dependency-name: k8s.io/*
2321
update-types: [version-update:semver-major, version-update:semver-minor]
2422
- package-ecosystem: github-actions

.github/workflows/c.yml

+2-20
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,5 @@ jobs:
1414
- uses: jidicula/clang-format-action@v4.13.0
1515
with:
1616
clang-format-version: "17"
17-
check-path: bpfassets/libbpf/src
18-
19-
build:
20-
runs-on: ubuntu-latest
21-
steps:
22-
- uses: actions/checkout@v4.1.1
23-
- name: Install libbpf
24-
working-directory: bpfassets/libbpf
25-
run: |
26-
sudo apt remove libbpf-dev
27-
sudo apt install libelf-dev
28-
mkdir temp-libbpf
29-
cd temp-libbpf
30-
git clone https://github.com/libbpf/libbpf
31-
cd libbpf/src
32-
sudo make install_headers
33-
sudo make install_uapi_headers
34-
sudo prefix=/usr BUILD_STATIC_ONLY=y make install
35-
- name: Build
36-
run: make -C bpfassets/libbpf
17+
check-path: bpf
18+
exclude-regex: ^bpf/include/.*

.github/workflows/golang.yml

-24
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,6 @@ jobs:
3535
- uses: actions/setup-go@v5
3636
with:
3737
go-version-file: go.mod
38-
- name: Install libbpf
39-
working-directory: bpfassets/libbpf
40-
run: |
41-
sudo apt remove libbpf-dev
42-
sudo apt install libelf-dev
43-
mkdir temp-libbpf
44-
cd temp-libbpf
45-
git clone https://github.com/libbpf/libbpf
46-
cd libbpf/src
47-
sudo make install_headers
48-
sudo make install_uapi_headers
49-
sudo prefix=/usr BUILD_STATIC_ONLY=y make install
5038
- name: golangci-lint
5139
uses: golangci/golangci-lint-action@v6
5240
with:
@@ -59,18 +47,6 @@ jobs:
5947
- uses: actions/setup-go@v5
6048
with:
6149
go-version-file: go.mod
62-
- name: Install libbpf
63-
working-directory: bpfassets/libbpf
64-
run: |
65-
sudo apt remove libbpf-dev
66-
sudo apt install libelf-dev
67-
mkdir temp-libbpf
68-
cd temp-libbpf
69-
git clone https://github.com/libbpf/libbpf
70-
cd libbpf/src
71-
sudo make install_headers
72-
sudo make install_uapi_headers
73-
sudo prefix=/usr BUILD_STATIC_ONLY=y make install
7450
- name: Run go vet
7551
run: go vet ./...
7652

.github/workflows/unit_test.yml

+1-13
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ jobs:
2222
- uses: actions/setup-go@v5
2323
with:
2424
go-version-file: go.mod
25+
- run: sudo apt-get install clang llvm
2526
- name: Get ginkgo
2627
run: make ginkgo-set
2728
env:
@@ -32,21 +33,8 @@ jobs:
3233
sudo apt-get install -y cpuid clang
3334
cd doc/ && sudo ./dev/prepare_dev_env.sh && cd -
3435
git config --global --add safe.directory /kepler
35-
- name: install libbpf
36-
uses: sustainable-computing-io/kepler-action@v0.0.7
37-
with:
38-
ebpfprovider: libbpf
3936
- name: Run
4037
run: |
41-
sudo apt remove libbpf-dev
42-
mkdir temp-libbpf
43-
cd temp-libbpf
44-
git clone https://github.com/libbpf/libbpf
45-
cd libbpf/src
46-
sudo make install_headers
47-
sudo make install_uapi_headers
48-
sudo prefix=/usr BUILD_STATIC_ONLY=y make install
49-
cd ../../../
5038
make VERBOSE=1 test
5139
go tool cover -func=coverage.out -o=coverage.out
5240

.gitignore

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ local-dev-cluster
4141
/tmp
4242
.env
4343

44-
# object files
45-
*.o
46-
4744
# test coverage
4845
coverage.out
46+
47+
# old libbpf-based binaries
48+
bpfassets/

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ repos:
2020
rev: v1.59.1
2121
hooks:
2222
- id: golangci-lint
23-
exclude: ^vendor/.*|^_output/.*|^bpfassets/libbpf/include/.*
23+
exclude: ^vendor/.*|^_output/.*|^bpf/include/.*

Makefile

+9-14
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ CTR_CMD ?= $(or $(shell podman info > /dev/null 2>&1 && which podman)
4141
# E.g. --tls-verify=false for local develop when using podman
4242
CTR_CMD_PUSH_OPTIONS ?=
4343

44-
GENERAL_TAGS := 'include_gcs include_oss containers_image_openpgp gssapi providerless netgo osusergo '
44+
GENERAL_TAGS := 'include_gcs include_oss containers_image_openpgp gssapi providerless netgo osusergo'
4545
GPU_TAGS := ' gpu '
4646
ifeq ($(shell ldconfig -p | grep -q libhlml.so && echo exists),exists)
4747
GPU_TAGS := $(GPU_TAGS)'habana '
@@ -51,14 +51,9 @@ endif
5151
GOOS := $(shell go env GOOS)
5252
GOARCH := $(shell go env GOARCH)
5353

54-
LIBBPF_HEADERS := /usr/include/bpf
5554
GOENV = GO111MODULE="" \
5655
GOOS=$(GOOS) \
57-
GOARCH=$(GOARCH) \
58-
CGO_ENABLED=1 \
59-
CC=clang \
60-
CGO_CFLAGS="-I $(LIBBPF_HEADERS) -I/usr/include/" \
61-
CGO_LDFLAGS="-lelf -lz -lbpf"
56+
GOARCH=$(GOARCH)
6257

6358
LDFLAGS := $(LDFLAGS) \
6459
-X main.Version=$(VERSION) \
@@ -67,7 +62,6 @@ LDFLAGS := $(LDFLAGS) \
6762
-X main.OS=$(GOOS) \
6863
-X main.Arch=$(GOARCH)
6964

70-
7165
DOCKERFILE := $(SRC_ROOT)/build/Dockerfile
7266
IMAGE_BUILD_TAG := $(GIT_VERSION)-linux-$(GOARCH)
7367
GO_BUILD_TAGS := $(GENERAL_TAGS)$(GOOS)$(GPU_TAGS)
@@ -166,10 +160,11 @@ clean-cross-build:
166160
build: clean_build_local _build_local copy_build_local ## Build binary and copy to $(OUTPUT_DIR)/bin
167161
.PHONY: build
168162

169-
_build_ebpf_local:
170-
@make -C bpfassets/libbpf
163+
.PHONY: generate
164+
generate: ## Generate BPF code locally.
165+
+@$(GOENV) go generate ./pkg/bpf
171166

172-
_build_local: _build_ebpf_local ## Build Kepler binary locally.
167+
_build_local: generate ## Build Kepler binary locally.
173168
@echo TAGS=$(GO_BUILD_TAGS)
174169
@mkdir -p "$(CROSS_BUILD_BINDIR)/$(GOOS)_$(GOARCH)"
175170
+@$(GOENV) go build \
@@ -274,7 +269,7 @@ SUDO_TEST_PKGS := $(shell go list ./... | grep pkg/bpf)
274269
test: unit-test bpf-test bench ## Run all tests.
275270

276271
.PHONY: unit-test
277-
unit-test: ginkgo-set tidy-vendor ## Run unit tests.
272+
unit-test: generate ginkgo-set tidy-vendor ## Run unit tests.
278273
@echo TAGS=$(GO_TEST_TAGS)
279274
$(if $(VERBOSE),@echo GOENV=$(GOENV))
280275
@$(GOENV) go test -tags $(GO_TEST_TAGS) \
@@ -292,7 +287,7 @@ bench: ## Run benchmarks.
292287
-bench=. --count=1 $(TEST_PKGS)
293288

294289
.PHONY: bpf-test
295-
bpf-test: _build_ebpf_local ## Run BPF tests.
290+
bpf-test: generate ## Run BPF tests.
296291
for pkg in $(SUDO_TEST_PKGS); do \
297292
$(GOENV) go test -c $$pkg -tags $(GO_TEST_TAGS) -cover \
298293
-covermode=atomic -coverprofile=coverage.bpf.out \
@@ -311,7 +306,7 @@ format:
311306

312307
c-format:
313308
@echo "Checking c format"
314-
@git ls-files -- '*.c' '*.h' ':!:vendor' ':!:bpfassets/libbpf/include/' | xargs clang-format --dry-run --Werror
309+
@git ls-files -- '*.c' '*.h' ':!:vendor' ':!:/bpf/include/' | xargs clang-format --dry-run --Werror
315310

316311
golint:
317312
@mkdir -p $(base_dir)/.cache/golangci-lint
File renamed without changes.
File renamed without changes.

bpfassets/libbpf/Makefile

-27
This file was deleted.

build/Dockerfile

-2
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ COPY --from=builder /workspace/_output/bin/kepler /usr/bin/kepler
5353
COPY --from=builder /libbpf-source/linux-5.14.0-424.el9/tools/bpf/bpftool/bpftool /usr/bin/bpftool
5454

5555
RUN mkdir -p /var/lib/kepler/data
56-
RUN mkdir -p /var/lib/kepler/bpfassets
5756
COPY --from=builder /workspace/data/cpus.yaml /var/lib/kepler/data/cpus.yaml
58-
COPY --from=builder /workspace/bpfassets/libbpf/bpf.o /var/lib/kepler/bpfassets
5957

6058
# copy model weight
6159
COPY --from=builder /workspace/data/model_weight/acpi_AbsPowerModel.json /var/lib/kepler/data/acpi_AbsPowerModel.json

go.mod

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
module github.com/sustainable-computing-io/kepler
22

3-
go 1.21
3+
go 1.21.0
4+
5+
toolchain go1.21.9
46

57
require (
68
github.com/HabanaAI/gohlml v1.15.0
79
github.com/NVIDIA/go-dcgm v0.0.0-20240118201113-3385e277e49f
810
github.com/NVIDIA/go-nvml v0.12.0-1
9-
github.com/aquasecurity/libbpfgo v0.6.0-libbpf-1.3
11+
github.com/cilium/ebpf v0.15.0
1012
github.com/jaypipes/ghw v0.12.0
1113
github.com/joho/godotenv v1.5.1
1214
github.com/jszwec/csvutil v1.10.0
@@ -66,6 +68,7 @@ require (
6668
github.com/prometheus/procfs v0.12.0 // indirect
6769
github.com/spf13/pflag v1.0.5 // indirect
6870
github.com/stretchr/testify v1.9.0 // indirect
71+
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a // indirect
6972
golang.org/x/net v0.26.0 // indirect
7073
golang.org/x/oauth2 v0.21.0 // indirect
7174
golang.org/x/term v0.21.0 // indirect

go.sum

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ github.com/NVIDIA/go-nvml v0.12.0-1 h1:6mdjtlFo+17dWL7VFPfuRMtf0061TF4DKls9pkSw6
88
github.com/NVIDIA/go-nvml v0.12.0-1/go.mod h1:hy7HYeQy335x6nEss0Ne3PYqleRa6Ct+VKD9RQ4nyFs=
99
github.com/StackExchange/wmi v1.2.1 h1:VIkavFPXSjcnS+O8yTq7NI32k0R5Aj+v39y29VYDOSA=
1010
github.com/StackExchange/wmi v1.2.1/go.mod h1:rcmrprowKIVzvc+NUiLncP2uuArMWLCbu9SBzvHz7e8=
11-
github.com/aquasecurity/libbpfgo v0.6.0-libbpf-1.3 h1:mhDe1mAZR80LjnsCnteS+R2/EeBFi9qFjKslsIJwVSo=
12-
github.com/aquasecurity/libbpfgo v0.6.0-libbpf-1.3/go.mod h1:0rEApF1YBHGuZ4C8OYI9q5oDBVpgqtRqYATePl9mCDk=
1311
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
1412
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
1513
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
@@ -18,6 +16,8 @@ github.com/bits-and-blooms/bitset v1.13.0 h1:bAQ9OPNFYbGHV6Nez0tmNI0RiEu7/hxlYJR
1816
github.com/bits-and-blooms/bitset v1.13.0/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8=
1917
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
2018
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
19+
github.com/cilium/ebpf v0.15.0 h1:7NxJhNiBT3NG8pZJ3c+yfrVdHY8ScgKD27sScgjLMMk=
20+
github.com/cilium/ebpf v0.15.0/go.mod h1:DHp1WyrLeiBh19Cf/tfiSMhqheEiK8fXFZ4No0P1Hso=
2121
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2222
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2323
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
@@ -41,6 +41,8 @@ github.com/go-openapi/jsonreference v0.20.4 h1:bKlDxQxQJgwpUSgOENiMPzCTBVuc7vTdX
4141
github.com/go-openapi/jsonreference v0.20.4/go.mod h1:5pZJyJP2MnYCpoeoMAql78cCHauHj0V9Lhc506VOpw4=
4242
github.com/go-openapi/swag v0.22.9 h1:XX2DssF+mQKM2DHsbgZK74y/zj4mo9I99+89xUmuZCE=
4343
github.com/go-openapi/swag v0.22.9/go.mod h1:3/OXnFfnMAwBD099SwYRk7GD3xOrr1iL7d/XNLXVVwE=
44+
github.com/go-quicktest/qt v1.101.0 h1:O1K29Txy5P2OK0dGo59b7b0LR6wKfIhttaAhHUyn7eI=
45+
github.com/go-quicktest/qt v1.101.0/go.mod h1:14Bz/f7NwaXPtdYEgzsx46kqSxVwTbzVZsDC26tQJow=
4446
github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI=
4547
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
4648
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
@@ -147,6 +149,8 @@ go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
147149
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
148150
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
149151
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
152+
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a h1:Q8/wZp0KX97QFTc2ywcOE0YRjZPVIx+MXInMzdvQqcA=
153+
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a/go.mod h1:idGWGoKP1toJGkd5/ig9ZLuPcZBC3ewk7SzmH0uou08=
150154
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
151155
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
152156
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=

hack/libbpf-headers.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ headers=(
1616

1717
# Fetch libbpf release and extract the desired headers
1818
curl -sL "https://github.com/libbpf/libbpf/archive/refs/tags/v${LIBBPF_VERSION}.tar.gz" | \
19-
tar -C ./bpfassets/libbpf/include/bpf -xz --xform='s#.*/##' "${headers[@]}"
19+
tar -C ./bpf/include/bpf -xz --xform='s#.*/##' "${headers[@]}"

packaging/rpm/kepler.spec

-5
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,10 @@ install -d %{buildroot}%{_bindir}
5454
install -d %{buildroot}%{_sysconfdir}/kepler/
5555

5656
install -d %{buildroot}/var/lib/kepler/data
57-
install -d %{buildroot}/var/lib/kepler/bpfassets
5857
install -d %{buildroot}/etc/kepler/kepler.config
5958

6059
install -p -m755 ./_output/kepler %{buildroot}%{_bindir}/kepler
6160
install -p -m644 ./packaging/rpm/kepler.service %{buildroot}%{_unitdir}/kepler.service
62-
install -p -m644 ./bpfassets/libbpf/bpf.o/kepler.bpfel.o %{buildroot}/var/lib/kepler/bpfassets/kepler.bpfel.o
63-
install -p -m644 ./bpfassets/libbpf/bpf.o/kepler.bpfeb.o %{buildroot}/var/lib/kepler/bpfassets/kepler.bpfeb.o
6461
install -p -m644 ./_output/ENABLE_PROCESS_METRICS %{buildroot}/etc/kepler/kepler.config/ENABLE_PROCESS_METRICS
6562
install -p -m644 ./data/cpus.yaml %{buildroot}/var/lib/kepler/data/cpus.yaml
6663
install -p -m644 ./data/model_weight/acpi_AbsPowerModel.json %{buildroot}/var/lib/kepler/data/acpi_AbsPowerModel.json
@@ -76,8 +73,6 @@ install -p -m644 ./data/model_weight/intel_rapl_DynPowerModel.json %{buildroot}/
7673
%license LICENSE
7774
%{_bindir}/kepler
7875
%{_unitdir}/kepler.service
79-
/var/lib/kepler/bpfassets/kepler.bpfel.o
80-
/var/lib/kepler/bpfassets/kepler.bpfeb.o
8176
/var/lib/kepler/data/cpus.yaml
8277
/var/lib/kepler/data/acpi_AbsPowerModel.json
8378
/var/lib/kepler/data/acpi_DynPowerModel.json

pkg/bpf/bpf_suite_test.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,19 @@ import (
1010

1111
func TestBpf(t *testing.T) {
1212
RegisterFailHandler(Fail)
13-
RunSpecs(t, "Attacher Suite")
13+
RunSpecs(t, "Bpf Suite")
1414
}
1515

16-
func checkDataCollected(processesData []ProcessBPFMetrics) {
17-
// len > 0
16+
func checkDataCollected(processesData []ProcessMetrics) {
1817
Expect(len(processesData)).To(BeNumerically(">", 0))
19-
Expect(processesData[0].PID).To(BeNumerically(">=", uint64(0)))
20-
Expect(processesData[0].Command).NotTo(BeEmpty())
21-
Expect(processesData[0].CPUCycles).To(BeNumerically(">=", uint64(0)))
22-
Expect(processesData[0].CPUInstr).To(BeNumerically(">=", uint64(0)))
23-
Expect(processesData[0].CacheMisses).To(BeNumerically(">=", uint64(0)))
24-
Expect(processesData[0].CGroupID).To(BeNumerically(">", uint64(0)))
18+
for _, p := range processesData {
19+
Expect(p.Pid).To(BeNumerically(">=", 0))
20+
Expect(p.Comm).NotTo(BeEmpty())
21+
Expect(p.CpuCycles).To(BeNumerically(">=", uint64(0)))
22+
Expect(p.CpuInstr).To(BeNumerically(">=", uint64(0)))
23+
Expect(p.CacheMiss).To(BeNumerically(">=", uint64(0)))
24+
Expect(p.CgroupId).To(BeNumerically(">=", uint64(0)))
25+
}
2526
}
2627

2728
var _ = Describe("BPF Exporter test", func() {

0 commit comments

Comments
 (0)