-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix(ebpf): synchronize perfUsageCollector.Collect with Manager.Start #54952
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
pgimalac
wants to merge
1
commit into
main
Choose a base branch
from
pgimalac/fix-perf-usage-collector-race
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| // Unless explicitly stated otherwise all files in this repository are licensed | ||
| // under the Apache License Version 2.0. | ||
| // This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| // Copyright 2016-present Datadog, Inc. | ||
|
|
||
| //go:build linux | ||
|
|
||
| package telemetry | ||
|
|
||
| import ( | ||
| "sync" | ||
| "testing" | ||
|
|
||
| "github.com/prometheus/client_golang/prometheus" | ||
| ) | ||
|
|
||
| // TestLockForWriteBlocksCollect verifies that LockForWrite prevents | ||
| // concurrent Collect calls from reading perf telemetry fields. This is | ||
| // the synchronization mechanism that prevents the data race between | ||
| // perfUsageCollector.Collect (reader) and ebpf.Manager.Start (writer). | ||
| // | ||
| // The actual race requires eBPF support to trigger (Manager.Start writes | ||
| // to PerfMap/RingBuffer internal fields), so this test validates the lock | ||
| // mechanism itself rather than the full race. | ||
| func TestLockForWriteBlocksCollect(t *testing.T) { | ||
| c := NewPerfUsageCollector() | ||
| collector := c.(*perfUsageCollector) | ||
|
|
||
| // Take the write lock (simulating Manager.Start holding it) | ||
| LockForWrite() | ||
|
|
||
| collectStarted := make(chan struct{}) | ||
| collectDone := make(chan struct{}) | ||
| go func() { | ||
| close(collectStarted) | ||
| ch := make(chan prometheus.Metric, 1) | ||
| go func() { | ||
| for m := range ch { | ||
| _ = m | ||
| } | ||
| }() | ||
| collector.Collect(ch) | ||
| close(ch) | ||
| close(collectDone) | ||
| }() | ||
|
|
||
| // Wait for the goroutine to start, then confirm Collect is blocked | ||
| <-collectStarted | ||
| select { | ||
| case <-collectDone: | ||
| t.Fatal("Collect completed while write lock was held") | ||
| default: | ||
| // expected: Collect is blocked | ||
| } | ||
|
|
||
| // Release the write lock — Collect should now complete deterministically | ||
| UnlockForWrite() | ||
| <-collectDone | ||
| } | ||
|
|
||
| // TestCollectConcurrentSerializes verifies that multiple Collect calls | ||
| // serialize properly under the mutex without deadlocking. | ||
| func TestCollectConcurrentSerializes(t *testing.T) { | ||
| t.Parallel() | ||
| c := NewPerfUsageCollector() | ||
| collector := c.(*perfUsageCollector) | ||
|
|
||
| const goroutines = 4 | ||
| var wg sync.WaitGroup | ||
|
|
||
| for range goroutines { | ||
| wg.Go(func() { | ||
| ch := make(chan prometheus.Metric, 10) | ||
| go func() { | ||
| for m := range ch { | ||
| _ = m | ||
| } | ||
| }() | ||
| collector.Collect(ch) | ||
| close(ch) | ||
| }) | ||
| } | ||
|
|
||
| wg.Wait() | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.