From 5c0fa9bf270beafcabfe46f9966f5b2c2dc84fff Mon Sep 17 00:00:00 2001
From: Tolya Korniltsev <korniltsev.anatoly@gmail.com>
Date: Fri, 1 Dec 2023 13:53:51 +0700
Subject: [PATCH] pyroscope.ebpf: update dependency  (#5902)

---
 CHANGELOG.md                                | 6 ++++++
 component/pyroscope/ebpf/ebpf_linux.go      | 8 ++++++--
 component/pyroscope/ebpf/ebpf_linux_test.go | 4 ++--
 go.mod                                      | 4 ++--
 go.sum                                      | 8 ++++----
 5 files changed, 20 insertions(+), 10 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5a9397a88bc1..7146605ebdc3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -32,6 +32,12 @@ Main (unreleased)
 
 - `pyroscope.ebpf` support python on arm64 platforms. (@korniltsev)
 
+### Bugfixes
+
+- Update `pyroscope.ebpf` to fix a logical bug causing to profile to many kthreads instead of regular processes https://github.com/grafana/pyroscope/pull/2778 (@korniltsev)
+ 
+- Update `pyroscope.ebpf` to produce more optimal pprof profiles for python processes https://github.com/grafana/pyroscope/pull/2788 (@korniltsev)
+
 v0.38.1 (2023-11-30)
 --------------------
 
diff --git a/component/pyroscope/ebpf/ebpf_linux.go b/component/pyroscope/ebpf/ebpf_linux.go
index 28910b92bce2..b8b1afbecf59 100644
--- a/component/pyroscope/ebpf/ebpf_linux.go
+++ b/component/pyroscope/ebpf/ebpf_linux.go
@@ -162,10 +162,14 @@ func (c *Component) collectProfiles() error {
 	level.Debug(c.options.Logger).Log("msg", "ebpf  collectProfiles")
 	args := c.args
 	builders := pprof.NewProfileBuilders(int64(args.SampleRate))
-	err := c.session.CollectProfiles(func(target *sd.Target, stack []string, value uint64, pid uint32) {
+	err := c.session.CollectProfiles(func(target *sd.Target, stack []string, value uint64, pid uint32, aggregation ebpfspy.SampleAggregation) {
 		labelsHash, labels := target.Labels()
 		builder := builders.BuilderForTarget(labelsHash, labels)
-		builder.AddSample(stack, value)
+		if aggregation == ebpfspy.SampleAggregated {
+			builder.CreateSample(stack, value)
+		} else {
+			builder.CreateSampleOrAddValue(stack, value)
+		}
 	})
 
 	if err != nil {
diff --git a/component/pyroscope/ebpf/ebpf_linux_test.go b/component/pyroscope/ebpf/ebpf_linux_test.go
index 7d91e894ed4c..d9216dda622d 100644
--- a/component/pyroscope/ebpf/ebpf_linux_test.go
+++ b/component/pyroscope/ebpf/ebpf_linux_test.go
@@ -45,13 +45,13 @@ func (m *mockSession) UpdateTargets(_ sd.TargetsOptions) {
 
 }
 
-func (m *mockSession) CollectProfiles(f func(target *sd.Target, stack []string, value uint64, pid uint32)) error {
+func (m *mockSession) CollectProfiles(f ebpfspy.CollectProfilesCallback) error {
 	m.collected++
 	if m.collectError != nil {
 		return m.collectError
 	}
 	for _, stack := range m.data {
-		f(m.dataTarget, stack, 1, 1)
+		f(m.dataTarget, stack, 1, 1, ebpfspy.SampleNotAggregated)
 	}
 	return nil
 }
diff --git a/go.mod b/go.mod
index 67f2e731f8b4..ab84d4303dcd 100644
--- a/go.mod
+++ b/go.mod
@@ -59,7 +59,7 @@ require (
 	github.com/grafana/loki v1.6.2-0.20231004111112-07cbef92268a
 	github.com/grafana/pyroscope-go/godeltaprof v0.1.3
 	github.com/grafana/pyroscope/api v0.2.0
-	github.com/grafana/pyroscope/ebpf v0.3.3
+	github.com/grafana/pyroscope/ebpf v0.4.0
 	github.com/grafana/regexp v0.0.0-20221123153739-15dc172cd2db
 	github.com/grafana/river v0.3.0
 	github.com/grafana/snowflake-prometheus-exporter v0.0.0-20221213150626-862cad8e9538
@@ -86,7 +86,7 @@ require (
 	github.com/jaegertracing/jaeger v1.50.0
 	github.com/jmespath/go-jmespath v0.4.0
 	github.com/json-iterator/go v1.1.12
-	github.com/klauspost/compress v1.17.1
+	github.com/klauspost/compress v1.17.3
 	github.com/lib/pq v1.10.7
 	github.com/mackerelio/go-osstat v0.2.3
 	github.com/miekg/dns v1.1.55
diff --git a/go.sum b/go.sum
index a7c169bd2807..65e785069389 100644
--- a/go.sum
+++ b/go.sum
@@ -1072,8 +1072,8 @@ github.com/grafana/pyroscope-go/godeltaprof v0.1.3 h1:eunWpv1B3Z7ZK9o4499EmQGlY+
 github.com/grafana/pyroscope-go/godeltaprof v0.1.3/go.mod h1:1HSPtjU8vLG0jE9JrTdzjgFqdJ/VgN7fvxBNq3luJko=
 github.com/grafana/pyroscope/api v0.2.0 h1:TzOxL0s6SiaLEy944ZAKgHcx/JDRJXu4O8ObwkqR6p4=
 github.com/grafana/pyroscope/api v0.2.0/go.mod h1:nhH+xai9cYFgs6lMy/+L0pKj0d5yCMwji/QAiQFCP+U=
-github.com/grafana/pyroscope/ebpf v0.3.3 h1:BAdrZfxGn5QuGrVOTK6oIsjRGHoIguBlWehAWaCAshM=
-github.com/grafana/pyroscope/ebpf v0.3.3/go.mod h1:WK4a+9QTRDOh6imvPgglbwblfxkvyS2cYGWF9jlKTI0=
+github.com/grafana/pyroscope/ebpf v0.4.0 h1:7fz+5S6MLSc+5cJfmIe7OCvBdooxEm8xy5OAV0s7GA0=
+github.com/grafana/pyroscope/ebpf v0.4.0/go.mod h1:eF5+k9lAUBYILVzGccr3hrrvuLy5ZvbDRWGQHDe021w=
 github.com/grafana/regexp v0.0.0-20221123153739-15dc172cd2db h1:7aN5cccjIqCLTzedH7MZzRZt5/lsAHch6Z3L2ZGn5FA=
 github.com/grafana/regexp v0.0.0-20221123153739-15dc172cd2db/go.mod h1:M5qHK+eWfAv8VR/265dIuEpL3fNfeC21tXXp9itM24A=
 github.com/grafana/river v0.3.0 h1:6TsaR/vkkcppUM9I0muGbPIUedCtpPu6OWreE5+CE6g=
@@ -1442,8 +1442,8 @@ github.com/klauspost/asmfmt v1.3.2/go.mod h1:AG8TuvYojzulgDAMCnYn50l/5QV3Bs/tp6j
 github.com/klauspost/compress v1.11.0/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
 github.com/klauspost/compress v1.11.12/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
 github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
-github.com/klauspost/compress v1.17.1 h1:NE3C767s2ak2bweCZo3+rdP4U/HoyVXLv/X9f2gPS5g=
-github.com/klauspost/compress v1.17.1/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
+github.com/klauspost/compress v1.17.3 h1:qkRjuerhUU1EmXLYGkSH6EZL+vPSxIrYjLNAK4slzwA=
+github.com/klauspost/compress v1.17.3/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM=
 github.com/klauspost/cpuid/v2 v2.2.5 h1:0E5MSMDEoAulmXNFquVs//DdoomxaoTY1kUhbc/qbZg=
 github.com/klauspost/cpuid/v2 v2.2.5/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
 github.com/knadh/koanf v1.5.0 h1:q2TSd/3Pyc/5yP9ldIrSdIz26MCcyNQzW0pEAugLPNs=