Skip to content

Commit f124c46

Browse files
feat(ebpf): pythonless, portable ebpf (#1314)
* pythonless ebpfspy * libbpf * tmp * ebpf makefile & dockerfile * bring back pid filter * commit profile.bpf.o, vmlinux.h * listen for dead processes and remove caches, also add lru cache * cleanup pid_exits read * fix mem leak * rm pid_exit listener, cleanup * batch delete from counts map, tmp disable sym resolve * more debug logging * moar debug logs * fix mem leak * moar debug logs * tmp disable symbols * moar debug logs * moar debug logs * moar debug logs * moar debug logs * fix: call fstat less * k8s labels * fixbug * bufix * bufix * bufix * moar tags * tmp * rm vmlinux & ebpf.o * do not commit vmlinux.h & profile.bpf.o do not use .bss because it is not available on old kernels * fix * containerd * parsing containerd cgroup * rework buildscripts * linter * linter * try to use upstream revive temporarily * try to use golang 1.19 revive * try to fail linter * revert $(EXTRA_GOBUILD_ARGS) * Revert " try to fail linter" This reverts commit 8c0942d. * separate command for ebpfspy * linter * rm unused file * Update cmd/pyroscope/command/ebpfspy.go Co-authored-by: Dmitry Filimonov <[email protected]> * Update pkg/config/config.go Co-authored-by: Dmitry Filimonov <[email protected]> * Update pkg/config/config.go Co-authored-by: Dmitry Filimonov <[email protected]> * fixes * fixes * fixes * fixes * debug log * moar debug logs * rename src * fix high numbers bug * ebpf OnlyServices flag * linter Co-authored-by: Dmitry Filimonov <[email protected]>
1 parent 9a457d9 commit f124c46

32 files changed

+1230
-148
lines changed

.dockerignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,10 @@ benchmark/docker-compose.yml
1111
benchmark/grafana-*
1212
benchmark/node_modules
1313
benchmark/*.env
14+
15+
third_party/bcc/lib
16+
third_party/bcc/src
17+
third_party/libbpf/lib
18+
third_party/libbpf/src
19+
pkg/agent/ebpfspy/bpf/profile.bpf.o
20+
pkg/agent/ebpfspy/bpf/vmlinux.h

.github/workflows/lint-go.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
- name: Checkout
1414
uses: actions/checkout@v2
1515
- name: Run revive action
16-
uses: petethepig/revive-action@v5
16+
uses: korniltsev/revive-action@v6
1717
with:
1818
config: revive.toml
1919
# same as in the `lint` rule of Makefile

Dockerfile

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,25 @@ ARG EXTRA_METADATA=""
6868

6969
RUN EXTRA_METADATA=$EXTRA_METADATA make assets-release
7070

71+
72+
73+
# _ __
74+
# | | / _|
75+
# ___| |__ _ __ | |_
76+
# / _ \ '_ \| '_ \| _|
77+
# | __/ |_) | |_) | |
78+
# \___|_.__/| .__/|_|
79+
# | |
80+
# |_|
81+
FROM alpine:3.12 as ebpf-builder
82+
RUN apk add cmake make binutils gcc g++ clang musl-dev linux-headers zlib-dev elfutils-dev libelf-static zlib-static git openssh
83+
ADD third_party/libbpf/Makefile /build/libbpf/
84+
RUN make -C /build/libbpf/
85+
ADD third_party/bcc/Makefile /build/bcc/
86+
RUN make -C /build/bcc/
87+
ADD pkg/agent/ebpfspy/bpf/Makefile pkg/agent/ebpfspy/bpf/profile.bpf.c pkg/agent/ebpfspy/bpf/profile.bpf.h /build/profile.bpf/
88+
RUN CFLAGS=-I/build/libbpf/lib/include make -C /build/profile.bpf
89+
7190
# _
7291
# | |
7392
# __ _ ___ | | __ _ _ __ __ _
@@ -84,8 +103,8 @@ RUN EXTRA_METADATA=$EXTRA_METADATA make assets-release
84103
# see https://github.com/pyroscope-io/pyroscope/pull/372 for more context
85104
FROM pyroscope/golang:1.18.0-alpine3.12 AS go-builder
86105

87-
88-
RUN apk add --no-cache make git zstd gcc g++ libc-dev musl-dev bash
106+
RUN apk add --no-cache make git zstd gcc g++ libc-dev musl-dev bash zlib-dev elfutils-dev libelf-static zlib-static \
107+
linux-headers
89108
RUN apk upgrade binutils
90109
RUN apk upgrade elfutils
91110

@@ -98,6 +117,9 @@ COPY third_party/rustdeps/pyspy.h /opt/pyroscope/third_party/rustdeps/pyspy.h
98117
COPY third_party/phpspy/phpspy.h /opt/pyroscope/third_party/phpspy/phpspy.h
99118
COPY --from=phpspy-builder /var/www/html/third_party/phpspy/libphpspy.a /opt/pyroscope/third_party/phpspy/libphpspy.a
100119
COPY --from=js-builder /opt/pyroscope/webapp/public ./webapp/public
120+
COPY --from=ebpf-builder /build/bcc/lib third_party/bcc/lib
121+
COPY --from=ebpf-builder /build/libbpf/lib third_party/libbpf/lib
122+
COPY --from=ebpf-builder /build/profile.bpf/profile.bpf.o pkg/agent/ebpfspy/bpf/profile.bpf.o
101123
COPY Makefile ./
102124
COPY tools ./tools
103125
COPY go.mod go.sum ./
@@ -159,7 +181,7 @@ LABEL maintainer="Pyroscope team <[email protected]>"
159181
WORKDIR /var/lib/pyroscope
160182

161183
RUN apk add --no-cache ca-certificates bash tzdata openssl musl-utils
162-
RUN apk add --no-cache bcc-tools python3
184+
163185
RUN ln -s $(which python3) /usr/bin/python
164186

165187
RUN addgroup -S pyroscope && adduser -S pyroscope -G pyroscope

Makefile

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,21 @@ else
3131
endif
3232

3333
EXTRA_GO_TAGS ?=
34+
CGO_CFLAGS ?=
35+
CGO_LDFLAGS ?=
36+
EXTRA_CGO_CFLAGS ?=
37+
EXTRA_CGO_LDFLAGS ?=
3438
GO_TAGS = $(ENABLED_SPIES)$(EXTRA_GO_TAGS)
3539
ALPINE_TAG =
3640

41+
ifneq (,$(findstring ebpfspy,$(GO_TAGS)))
42+
EXTRA_CGO_CFLAGS := $(EXTRA_CGO_CFLAGS) -I$(abspath ./third_party/libbpf/lib/include) \
43+
-I$(abspath ./third_party/bcc/lib/include)
44+
EXTRA_CGO_LDFLAGS := $(EXTRA_CGO_LDFLAGS) -L$(abspath ./third_party/libbpf/lib/lib64) -lbpf \
45+
-L$(abspath ./third_party/bcc/lib/lib) -lbcc-syms -lstdc++ -lelf -lz
46+
THIRD_PARTY_DEPENDENCIES := $(THIRD_PARTY_DEPENDENCIES) build-profile-bpf build-bcc build-libbpf
47+
endif
48+
3749
ifeq ("$(OS)", "Linux")
3850
ifeq ("$(shell cat /etc/os-release | grep ^ID=)", "ID=alpine")
3951
RUST_TARGET ?= "$(ARCH)-unknown-linux-musl"
@@ -56,7 +68,7 @@ else
5668
endif
5769

5870
EMBEDDED_ASSETS_DEPS ?= "assets-release"
59-
EXTRA_LDFLAGS ?= ""
71+
EXTRA_LDFLAGS ?=
6072

6173
ifndef $(GOPATH)
6274
GOPATH=$(shell go env GOPATH || true)
@@ -106,7 +118,9 @@ install-go-dependencies: ## installs golang dependencies
106118

107119
.PHONY: build
108120
build: ## Builds the binary
109-
$(GOBUILD) -tags "$(GO_TAGS)" -ldflags "$(EXTRA_LDFLAGS) $(shell scripts/generate-build-flags.sh)" -o ./bin/pyroscope ./cmd/pyroscope
121+
CGO_CFLAGS="$(CGO_CFLAGS) $(EXTRA_CGO_CFLAGS)" \
122+
CGO_LDFLAGS="$(CGO_LDFLAGS) $(EXTRA_CGO_LDFLAGS)" \
123+
$(GOBUILD) -tags "$(GO_TAGS)" -ldflags "$(EXTRA_LDFLAGS) $(shell scripts/generate-build-flags.sh)" -o ./bin/pyroscope ./cmd/pyroscope
110124

111125
.PHONY: build-release
112126
build-release: embedded-assets ## Builds the release build
@@ -136,6 +150,19 @@ build-phpspy-dependencies: ## Builds the PHP dependency
136150
cd third_party/phpspy_src && USE_ZEND=1 make CFLAGS="-DUSE_DIRECT" || $(MAKE) print-deps-error-message
137151
cp third_party/phpspy_src/libphpspy.a third_party/phpspy/libphpspy.a
138152

153+
.PHONY: build-libbpf
154+
build-libbpf:
155+
$(MAKE) -C third_party/libbpf
156+
157+
.PHONY: build-bcc
158+
build-bcc:
159+
$(MAKE) -C third_party/bcc
160+
161+
.PHONY: build-profile-bpf
162+
build-profile-bpf: build-libbpf
163+
CFLAGS="-I$(abspath ./third_party/libbpf/lib/include)" $(MAKE) -C pkg/agent/ebpfspy/bpf
164+
165+
139166
.PHONY: build-third-party-dependencies
140167
build-third-party-dependencies: $(shell echo $(THIRD_PARTY_DEPENDENCIES)) ## Builds third party dep
141168

@@ -236,6 +263,9 @@ go-deps-graph: ## Generate the deps graph
236263
.PHONY: clean
237264
clean: ## Clean up storage
238265
rm -rf tmp/pyroscope-storage
266+
$(MAKE) -C third_party/bcc clean
267+
$(MAKE) -C third_party/libbpf clean
268+
$(MAKE) -C pkg/agent/ebpfspy/bpf clean
239269

240270
.PHONY: update-contributors
241271
update-contributors: ## Update the contributors

cmd/pyroscope/command/ebpf.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//go:build ebpfspy
2+
3+
package command
4+
5+
import (
6+
"github.com/spf13/cobra"
7+
8+
"github.com/pyroscope-io/pyroscope/pkg/cli"
9+
"github.com/pyroscope-io/pyroscope/pkg/config"
10+
"github.com/pyroscope-io/pyroscope/pkg/exec"
11+
)
12+
13+
func newEBPFSpyCmd(cfg *config.EBPF) *cobra.Command {
14+
vpr := newViper()
15+
connectCmd := &cobra.Command{
16+
Use: "ebpf [flags]",
17+
Short: "Profile whole system using eBPF sampling profiler",
18+
Args: cobra.NoArgs,
19+
20+
RunE: cli.CreateCmdRunFn(cfg, vpr, func(_ *cobra.Command, _ []string) error {
21+
return exec.RunEBPF(cfg)
22+
}),
23+
}
24+
25+
cli.PopulateFlagSet(cfg, connectCmd.Flags(), vpr)
26+
return connectCmd
27+
}

cmd/pyroscope/command/ebpf_stub.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//go:build !ebpfspy
2+
3+
package command
4+
5+
import (
6+
"github.com/spf13/cobra"
7+
8+
"github.com/pyroscope-io/pyroscope/pkg/config"
9+
)
10+
11+
func newEBPFSpyCmd(_ *config.EBPF) *cobra.Command {
12+
return nil
13+
}

cmd/pyroscope/command/root.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func Execute() error {
2121
newAdminCmd(&cfg.Admin),
2222
newAgentCmd(&cfg.Agent),
2323
newConnectCmd(&cfg.Connect),
24+
newEBPFSpyCmd(&cfg.EBPF),
2425
newConvertCmd(&cfg.Convert),
2526
newDbManagerCmd(&config.CombinedDbManager{DbManager: &cfg.DbManager, Server: &cfg.Server}),
2627
newExecCmd(&cfg.Exec),
@@ -29,6 +30,9 @@ func Execute() error {
2930
}
3031

3132
for _, c := range subcommands {
33+
if c == nil {
34+
continue
35+
}
3236
addHelpSubcommand(c)
3337
c.HasHelpSubCommands()
3438
rootCmd.AddCommand(c)

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/pyroscope-io/pyroscope
33
go 1.18
44

55
require (
6+
github.com/aquasecurity/libbpfgo v0.3.0-libbpf-0.8.0
67
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d
78
github.com/aws/aws-sdk-go v1.44.37
89
github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY
8383
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
8484
github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
8585
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
86+
github.com/aquasecurity/libbpfgo v0.3.0-libbpf-0.8.0 h1:NQEf484vQOshZwZOLTE7kzo62TvYrM906gUjlVg4D2k=
87+
github.com/aquasecurity/libbpfgo v0.3.0-libbpf-0.8.0/go.mod h1:qu0TVGRvtNMFkuKLscJkY1FwmageNBLqeImAFslqPPc=
8688
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
8789
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
8890
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
@@ -1015,6 +1017,7 @@ golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7w
10151017
golang.org/x/sys v0.0.0-20210420072515-93ed5bcd2bfe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
10161018
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
10171019
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1020+
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
10181021
golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
10191022
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
10201023
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

pkg/agent/ebpfspy/bpf/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
libbpf*
2+
bcc-src
3+
libs/
4+
profile.bpf.o
5+
vmlinux.h

0 commit comments

Comments
 (0)