Skip to content

Commit f8d437a

Browse files
committed
deduplicate uprobes
1 parent 9e229ea commit f8d437a

7 files changed

Lines changed: 295 additions & 177 deletions

File tree

‎containers/container.go‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,13 +1238,17 @@ func (c *Container) attachTlsUprobes(tracer *ebpftracer.Tracer, pid uint32) {
12381238
return
12391239
}
12401240
if !p.openSslUprobesChecked {
1241-
p.uprobes = append(p.uprobes, tracer.AttachOpenSslUprobes(pid)...)
1241+
if key := tracer.AttachOpenSslUprobes(pid); key != nil {
1242+
p.uprobeKeys = append(p.uprobeKeys, *key)
1243+
}
12421244
p.openSslUprobesChecked = true
12431245
}
12441246
if !p.goTlsUprobesChecked {
1245-
uprobes, isGolangApp := tracer.AttachGoTlsUprobes(pid)
1247+
key, isGolangApp := tracer.AttachGoTlsUprobes(pid)
12461248
p.isGolangApp = isGolangApp
1247-
p.uprobes = append(p.uprobes, uprobes...)
1249+
if key != nil {
1250+
p.uprobeKeys = append(p.uprobeKeys, *key)
1251+
}
12481252
p.goTlsUprobesChecked = true
12491253
}
12501254
}

‎containers/process.go‎

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"os"
77
"time"
88

9-
"github.com/cilium/ebpf/link"
109
"github.com/coroot/coroot-node-agent/ebpftracer"
1110
"github.com/coroot/coroot-node-agent/gpu"
1211
"github.com/coroot/coroot-node-agent/proc"
@@ -35,10 +34,11 @@ type Process struct {
3534
ctx context.Context
3635
cancelFunc context.CancelFunc
3736

37+
tracer *ebpftracer.Tracer
3838
dotNetMonitor *DotNetMonitor
3939
isGolangApp bool
4040

41-
uprobes []link.Link
41+
uprobeKeys []ebpftracer.UprobeKey
4242
goTlsUprobesChecked bool
4343
openSslUprobesChecked bool
4444
pythonGilChecked bool
@@ -50,7 +50,7 @@ type Process struct {
5050
}
5151

5252
func NewProcess(pid uint32, stats *taskstats.Stats, tracer *ebpftracer.Tracer) *Process {
53-
p := &Process{Pid: pid, StartedAt: stats.BeginTime}
53+
p := &Process{Pid: pid, StartedAt: stats.BeginTime, tracer: tracer}
5454
p.Flags, _ = proc.GetFlags(pid)
5555
p.ctx, p.cancelFunc = context.WithCancel(context.Background())
5656
go p.instrument(tracer)
@@ -118,8 +118,10 @@ func (p *Process) instrumentPython(cmdline []byte, tracer *ebpftracer.Tracer) {
118118
if !pythonCmd.Match(cmd) {
119119
return
120120
}
121-
p.pythonPrevStats = &ebpftracer.PythonStats{}
122-
p.uprobes = append(p.uprobes, tracer.AttachPythonThreadLockProbes(p.Pid)...)
121+
if key := tracer.AttachPythonThreadLockProbes(p.Pid); key != nil {
122+
p.pythonPrevStats = &ebpftracer.PythonStats{}
123+
p.uprobeKeys = append(p.uprobeKeys, *key)
124+
}
123125
}
124126

125127
func (p *Process) instrumentNodejs(exe string, tracer *ebpftracer.Tracer) {
@@ -130,8 +132,10 @@ func (p *Process) instrumentNodejs(exe string, tracer *ebpftracer.Tracer) {
130132
if !nodejsCmd.MatchString(exe) {
131133
return
132134
}
133-
p.nodejsPrevStats = &ebpftracer.NodejsStats{}
134-
p.uprobes = append(p.uprobes, tracer.AttachNodejsProbes(p.Pid, exe)...)
135+
if key := tracer.AttachNodejsProbes(p.Pid, exe); key != nil {
136+
p.nodejsPrevStats = &ebpftracer.NodejsStats{}
137+
p.uprobeKeys = append(p.uprobeKeys, *key)
138+
}
135139
}
136140

137141
func (p *Process) addGpuUsageSample(sample gpu.ProcessUsageSample) {
@@ -173,13 +177,8 @@ func (p *Process) removeOldGpuUsageSamples(cutoff time.Time) {
173177

174178
func (p *Process) Close() {
175179
p.cancelFunc()
176-
if len(p.uprobes) > 0 {
177-
uprobes := p.uprobes
178-
p.uprobes = nil
179-
go func() {
180-
for _, u := range uprobes {
181-
_ = u.Close()
182-
}
183-
}()
180+
if len(p.uprobeKeys) > 0 {
181+
p.tracer.ReleaseGlobalUprobes(p.uprobeKeys)
182+
p.uprobeKeys = nil
184183
}
185184
}

‎ebpftracer/nodejs.go‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"k8s.io/klog/v2"
1212
)
1313

14-
func (t *Tracer) AttachNodejsProbes(pid uint32, exe string) []link.Link {
14+
func (t *Tracer) AttachNodejsProbes(pid uint32, exe string) *UprobeKey {
1515
log := func(libPath, msg string, err error) {
1616
if err != nil {
1717
for _, s := range []string{"no such file or directory", "no such process", "permission denied"} {
@@ -26,17 +26,23 @@ func (t *Tracer) AttachNodejsProbes(pid uint32, exe string) []link.Link {
2626
}
2727

2828
for _, libPath := range append(getLibuv(pid), proc.Path(pid, "root", exe)) {
29-
if links, err := t.attachNodejsUprobes(libPath, pid); err == nil {
29+
key, ok := t.AcquireGlobalUprobe(libPath, func() []link.Link {
30+
links, err := t.attachNodejsUprobes(libPath)
31+
if err != nil {
32+
log(libPath, "failed to attach nodejs uprobes", err)
33+
return nil
34+
}
3035
log(libPath, "nodejs uprobes attached", nil)
3136
return links
32-
} else {
33-
log(libPath, "failed to attach nodejs uprobes", err)
37+
})
38+
if ok {
39+
return &key
3440
}
3541
}
3642
return nil
3743
}
3844

39-
func (t *Tracer) attachNodejsUprobes(libPath string, pid uint32) ([]link.Link, error) {
45+
func (t *Tracer) attachNodejsUprobes(libPath string) ([]link.Link, error) {
4046
exe, err := link.OpenExecutable(libPath)
4147
if err != nil {
4248
return nil, err
@@ -51,14 +57,14 @@ func (t *Tracer) attachNodejsUprobes(libPath string, pid uint32) ([]link.Link, e
5157
if err != nil {
5258
return nil, err
5359
}
54-
l, err := s.AttachUprobe(exe, t.uprobes["uv_io_poll_enter"], pid)
60+
l, err := s.AttachUprobe(exe, t.uprobes["uv_io_poll_enter"], 0)
5561
if err != nil {
5662
return nil, err
5763
}
5864
var links []link.Link
5965
links = append(links, l)
6066

61-
ls, err := s.AttachUretprobes(exe, t.uprobes["uv_io_poll_exit"], pid)
67+
ls, err := s.AttachUretprobes(exe, t.uprobes["uv_io_poll_exit"], 0)
6268
links = append(links, ls...)
6369
if err != nil {
6470
for _, l := range links {
@@ -72,12 +78,12 @@ func (t *Tracer) attachNodejsUprobes(libPath string, pid uint32) ([]link.Link, e
7278
if err != nil {
7379
break
7480
}
75-
l, err = s.AttachUprobe(exe, t.uprobes["uv_io_cb_enter"], pid)
81+
l, err = s.AttachUprobe(exe, t.uprobes["uv_io_cb_enter"], 0)
7682
if err != nil {
7783
break
7884
}
7985
links = append(links, l)
80-
ls, err = s.AttachUretprobes(exe, t.uprobes["uv_io_cb_exit"], pid)
86+
ls, err = s.AttachUretprobes(exe, t.uprobes["uv_io_cb_exit"], 0)
8187
links = append(links, ls...)
8288
if err != nil {
8389
break

‎ebpftracer/python.go‎

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var (
1717
muslRegexp = regexp.MustCompile(`ld-musl[\.-]`)
1818
)
1919

20-
func (t *Tracer) AttachPythonThreadLockProbes(pid uint32) []link.Link {
20+
func (t *Tracer) AttachPythonThreadLockProbes(pid uint32) *UprobeKey {
2121
log := func(libPath, msg string, err error) {
2222
if err != nil {
2323
for _, s := range []string{"no such file or directory", "no such process", "permission denied"} {
@@ -31,26 +31,24 @@ func (t *Tracer) AttachPythonThreadLockProbes(pid uint32) []link.Link {
3131
klog.InfofDepth(1, "pid=%d lib=%s: %s", pid, libPath, msg)
3232
}
3333

34-
var (
35-
links []link.Link
36-
err error
37-
)
38-
3934
for _, libPath := range getPthreadLibs(pid) {
40-
if links, err = t.attachPythonUprobes(libPath, pid); err == nil {
35+
key, ok := t.AcquireGlobalUprobe(libPath, func() []link.Link {
36+
links, err := t.attachPythonUprobes(libPath)
37+
if err != nil {
38+
log(libPath, "failed to attach python uprobes", err)
39+
return nil
40+
}
4141
log(libPath, "python uprobes attached", nil)
4242
return links
43-
} else {
44-
log(libPath, "failed to attach python uprobes", err)
43+
})
44+
if ok {
45+
return &key
4546
}
4647
}
47-
if len(links) > 0 {
48-
49-
}
50-
return links
48+
return nil
5149
}
5250

53-
func (t *Tracer) attachPythonUprobes(libPath string, pid uint32) ([]link.Link, error) {
51+
func (t *Tracer) attachPythonUprobes(libPath string) ([]link.Link, error) {
5452
exe, err := link.OpenExecutable(libPath)
5553
if err != nil {
5654
return nil, err
@@ -65,12 +63,12 @@ func (t *Tracer) attachPythonUprobes(libPath string, pid uint32) ([]link.Link, e
6563
if err != nil {
6664
return nil, err
6765
}
68-
l, err := s.AttachUprobe(exe, t.uprobes["pthread_cond_timedwait_enter"], pid)
66+
l, err := s.AttachUprobe(exe, t.uprobes["pthread_cond_timedwait_enter"], 0)
6967
if err != nil {
7068
return nil, err
7169
}
7270
links := []link.Link{l}
73-
ls, err := s.AttachUretprobes(exe, t.uprobes["pthread_cond_timedwait_exit"], pid)
71+
ls, err := s.AttachUretprobes(exe, t.uprobes["pthread_cond_timedwait_exit"], 0)
7472
links = append(links, ls...)
7573
if err != nil {
7674
for _, l := range links {

0 commit comments

Comments
 (0)