Reduce lock contention in manager package#3756
Merged
dims merged 5 commits intogoogle:masterfrom Dec 4, 2025
Merged
Conversation
Replace mutex-protected infoLastUpdatedTime and statsLastUpdatedTime with atomic.Int64 to eliminate lock acquisition on timestamp updates. Signed-off-by: Damien Grisonnet <dgrisonn@redhat.com>
Move container filtering logic outside the lock to minimize containersLock hold time during iteration. Signed-off-by: Damien Grisonnet <dgrisonn@redhat.com>
Convert containers from map + RWMutex to sync.Map for lock-free reads. Eliminates contention between Prometheus scrapes and container lifecycle events. Benchmarks show 2.7x improvement in mixed read/write workloads. Signed-off-by: Damien Grisonnet <dgrisonn@redhat.com>
Add benchmarks comparing concurrent read, iteration, and mixed read/write performance between sync.Map and RWMutex patterns. Signed-off-by: Damien Grisonnet <dgrisonn@redhat.com>
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
78e73da to
357c216
Compare
Collaborator
|
@dgrisonnet please sign the CLA |
Contributor
Author
|
it should be good now |
Add atomicTime (wrapping atomic.Int64) and containerMap (wrapping sync.Map) to provide type-safe APIs. This removes boilerplate type assertions at call sites and improves code readability. Signed-off-by: Damien Grisonnet <dgrisonn@redhat.com>
Contributor
|
LGTM, this looks great thanks @dgrisonnet |
This was referenced Dec 4, 2025
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
This PR reduces lock contention in the cAdvisor manager package by replacing the global
containersLockRWMutex withsync.Map, resulting in 60% faster operations and 318x less lock contention in concurrent workloads.Changes
Use
atomic.Int64for container timestampsReplace mutex-protected
time.Timefields withatomic.Int64forinfoLastUpdatedTimeandstatsLastUpdatedTimeto eliminate lock acquisition on timestamp updates.Replace
containersLockwithsync.MapConvert
containersfrommap[namespacedContainerName]*containerData+sync.RWMutextosync.Mapfor lock-free reads.Benchmark Results
Raw Benchmark Output
Benchstat Comparison (8 CPUs, n=10)
Key results:
Scaling Behavior
sync.Mapscales better with CPU count.RWMutexgets slower from 4→8 CPUs due to increased contention.Mutex Contention Profiling
RWMutex Pattern
Total contention: 6.70 seconds (writers blocked by readers)
sync.Map Pattern
Total contention: 21 milliseconds (runtime overhead only, no lock contention)
Result: Lock contention reduced by 318x (6.70s → 21ms).
How to Reproduce