-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add: exponential backoff for CAS operations on floats #1661
Merged
bwplotka
merged 5 commits into
prometheus:main
from
imorph:add_exp_backoff_for_tight_loops
Nov 11, 2024
+237
−39
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9829910
add: exponential backoff for CAS operations of floats
imorph 27067c1
add: some more benchmark use cases (higher contention)
imorph d06c03f
fmt: fumpted some files
imorph 62d6f57
add: license header
imorph ba58fd6
add: comment explaining origin of backoff constants
imorph 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 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,50 @@ | ||
// Copyright 2014 The Prometheus Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package prometheus | ||
|
||
import ( | ||
"math" | ||
"sync/atomic" | ||
"time" | ||
) | ||
|
||
// atomicUpdateFloat atomically updates the float64 value pointed to by bits | ||
// using the provided updateFunc, with an exponential backoff on contention. | ||
func atomicUpdateFloat(bits *uint64, updateFunc func(float64) float64) { | ||
const ( | ||
// both numbers are derived from empirical observations | ||
// documented in this PR: https://github.com/prometheus/client_golang/pull/1661 | ||
maxBackoff = 320 * time.Millisecond | ||
initialBackoff = 10 * time.Millisecond | ||
) | ||
backoff := initialBackoff | ||
|
||
for { | ||
loadedBits := atomic.LoadUint64(bits) | ||
oldFloat := math.Float64frombits(loadedBits) | ||
newFloat := updateFunc(oldFloat) | ||
newBits := math.Float64bits(newFloat) | ||
|
||
if atomic.CompareAndSwapUint64(bits, loadedBits, newBits) { | ||
break | ||
} else { | ||
// Exponential backoff with sleep and cap to avoid infinite wait | ||
time.Sleep(backoff) | ||
backoff *= 2 | ||
if backoff > maxBackoff { | ||
backoff = maxBackoff | ||
} | ||
} | ||
} | ||
} |
This file contains 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,167 @@ | ||
// Copyright 2014 The Prometheus Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package prometheus | ||
|
||
import ( | ||
"math" | ||
"sync" | ||
"sync/atomic" | ||
"testing" | ||
"unsafe" | ||
) | ||
|
||
var output float64 | ||
|
||
func TestAtomicUpdateFloat(t *testing.T) { | ||
var val float64 = 0.0 | ||
bits := (*uint64)(unsafe.Pointer(&val)) | ||
var wg sync.WaitGroup | ||
numGoroutines := 100000 | ||
increment := 1.0 | ||
|
||
for i := 0; i < numGoroutines; i++ { | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
atomicUpdateFloat(bits, func(f float64) float64 { | ||
return f + increment | ||
}) | ||
}() | ||
} | ||
|
||
wg.Wait() | ||
expected := float64(numGoroutines) * increment | ||
if val != expected { | ||
t.Errorf("Expected %f, got %f", expected, val) | ||
} | ||
} | ||
|
||
// Benchmark for atomicUpdateFloat with single goroutine (no contention). | ||
func BenchmarkAtomicUpdateFloat_SingleGoroutine(b *testing.B) { | ||
var val float64 = 0.0 | ||
bits := (*uint64)(unsafe.Pointer(&val)) | ||
|
||
for i := 0; i < b.N; i++ { | ||
atomicUpdateFloat(bits, func(f float64) float64 { | ||
return f + 1.0 | ||
}) | ||
} | ||
|
||
output = val | ||
} | ||
|
||
// Benchmark for old implementation with single goroutine (no contention) -> to check overhead of backoff | ||
func BenchmarkAtomicNoBackoff_SingleGoroutine(b *testing.B) { | ||
var val float64 = 0.0 | ||
bits := (*uint64)(unsafe.Pointer(&val)) | ||
|
||
for i := 0; i < b.N; i++ { | ||
for { | ||
loadedBits := atomic.LoadUint64(bits) | ||
newBits := math.Float64bits(math.Float64frombits(loadedBits) + 1.0) | ||
if atomic.CompareAndSwapUint64(bits, loadedBits, newBits) { | ||
break | ||
} | ||
} | ||
} | ||
|
||
output = val | ||
} | ||
|
||
// Benchmark varying the number of goroutines. | ||
func benchmarkAtomicUpdateFloatConcurrency(b *testing.B, numGoroutines int) { | ||
var val float64 = 0.0 | ||
bits := (*uint64)(unsafe.Pointer(&val)) | ||
b.SetParallelism(numGoroutines) | ||
|
||
b.ResetTimer() | ||
b.RunParallel(func(pb *testing.PB) { | ||
for pb.Next() { | ||
atomicUpdateFloat(bits, func(f float64) float64 { | ||
return f + 1.0 | ||
}) | ||
} | ||
}) | ||
|
||
output = val | ||
} | ||
|
||
func benchmarkAtomicNoBackoffFloatConcurrency(b *testing.B, numGoroutines int) { | ||
var val float64 = 0.0 | ||
bits := (*uint64)(unsafe.Pointer(&val)) | ||
b.SetParallelism(numGoroutines) | ||
|
||
b.ResetTimer() | ||
b.RunParallel(func(pb *testing.PB) { | ||
for pb.Next() { | ||
for { | ||
loadedBits := atomic.LoadUint64(bits) | ||
newBits := math.Float64bits(math.Float64frombits(loadedBits) + 1.0) | ||
if atomic.CompareAndSwapUint64(bits, loadedBits, newBits) { | ||
break | ||
} | ||
} | ||
} | ||
}) | ||
|
||
output = val | ||
} | ||
|
||
func BenchmarkAtomicUpdateFloat_1Goroutine(b *testing.B) { | ||
benchmarkAtomicUpdateFloatConcurrency(b, 1) | ||
} | ||
|
||
func BenchmarkAtomicNoBackoff_1Goroutine(b *testing.B) { | ||
benchmarkAtomicNoBackoffFloatConcurrency(b, 1) | ||
} | ||
|
||
func BenchmarkAtomicUpdateFloat_2Goroutines(b *testing.B) { | ||
benchmarkAtomicUpdateFloatConcurrency(b, 2) | ||
} | ||
|
||
func BenchmarkAtomicNoBackoff_2Goroutines(b *testing.B) { | ||
benchmarkAtomicNoBackoffFloatConcurrency(b, 2) | ||
} | ||
|
||
func BenchmarkAtomicUpdateFloat_4Goroutines(b *testing.B) { | ||
benchmarkAtomicUpdateFloatConcurrency(b, 4) | ||
} | ||
|
||
func BenchmarkAtomicNoBackoff_4Goroutines(b *testing.B) { | ||
benchmarkAtomicNoBackoffFloatConcurrency(b, 4) | ||
} | ||
|
||
func BenchmarkAtomicUpdateFloat_8Goroutines(b *testing.B) { | ||
benchmarkAtomicUpdateFloatConcurrency(b, 8) | ||
} | ||
|
||
func BenchmarkAtomicNoBackoff_8Goroutines(b *testing.B) { | ||
benchmarkAtomicNoBackoffFloatConcurrency(b, 8) | ||
} | ||
|
||
func BenchmarkAtomicUpdateFloat_16Goroutines(b *testing.B) { | ||
benchmarkAtomicUpdateFloatConcurrency(b, 16) | ||
} | ||
|
||
func BenchmarkAtomicNoBackoff_16Goroutines(b *testing.B) { | ||
benchmarkAtomicNoBackoffFloatConcurrency(b, 16) | ||
} | ||
|
||
func BenchmarkAtomicUpdateFloat_32Goroutines(b *testing.B) { | ||
benchmarkAtomicUpdateFloatConcurrency(b, 32) | ||
} | ||
|
||
func BenchmarkAtomicNoBackoff_32Goroutines(b *testing.B) { | ||
benchmarkAtomicNoBackoffFloatConcurrency(b, 32) | ||
} |
This file contains 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 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 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 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It feels we could use
b.Run
here as I was proposing in https://www.bwplotka.dev/2024/go-microbenchmarks-benchstat/Any good reasons to NOT do it? (e.g.
"goroutines=%v"
)Nevertheless we can do later, not a blocker for this PR, thanks!