mirror of
https://github.com/prometheus/prometheus
synced 2024-12-26 00:23:18 +00:00
7f5d3c2c29
Benchmark: $ go test -bench 'Fingerprint' -test.run 'Fingerprint' -test.cpu=1,2,4 OLD BenchmarkFingerprintLockerParallel 500000 3618 ns/op BenchmarkFingerprintLockerParallel-2 100000 12257 ns/op BenchmarkFingerprintLockerParallel-4 500000 10164 ns/op BenchmarkFingerprintLockerSerial 10000000 283 ns/op BenchmarkFingerprintLockerSerial-2 10000000 284 ns/op BenchmarkFingerprintLockerSerial-4 10000000 288 ns/op NEW BenchmarkFingerprintLockerParallel 1000000 1018 ns/op BenchmarkFingerprintLockerParallel-2 1000000 1164 ns/op BenchmarkFingerprintLockerParallel-4 2000000 910 ns/op BenchmarkFingerprintLockerSerial 50000000 56.0 ns/op BenchmarkFingerprintLockerSerial-2 50000000 47.9 ns/op BenchmarkFingerprintLockerSerial-4 50000000 54.5 ns/op Change-Id: I3c65a43822840e7e64c3c3cfe759e1de51272581
44 lines
1.6 KiB
Go
44 lines
1.6 KiB
Go
package local
|
|
|
|
import (
|
|
"sync"
|
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
|
)
|
|
|
|
// fingerprintLocker allows locking individual fingerprints. To limit the number
|
|
// of mutexes needed for that, only a fixed number of mutexes are
|
|
// allocated. Fingerprints to be locked are assigned to those pre-allocated
|
|
// mutexes by their value. (Note that fingerprints are calculated by a hash
|
|
// function, so that an approximately equal distribution over the mutexes is
|
|
// expected, even without additional hashing of the fingerprint value.)
|
|
// Collisions are not detected. If two fingerprints get assigned to the same
|
|
// mutex, only one of them can be locked at the same time. As long as the number
|
|
// of pre-allocated mutexes is much larger than the number of goroutines
|
|
// requiring a fingerprint lock concurrently, the loss in efficiency is
|
|
// small. However, a goroutine must never lock more than one fingerprint at the
|
|
// same time. (In that case a collision would try to acquire the same mutex
|
|
// twice).
|
|
type fingerprintLocker struct {
|
|
fpMtxs []sync.Mutex
|
|
numFpMtxs uint
|
|
}
|
|
|
|
// newFingerprintLocker returns a new fingerprintLocker ready for use.
|
|
func newFingerprintLocker(preallocatedMutexes int) *fingerprintLocker {
|
|
return &fingerprintLocker{
|
|
make([]sync.Mutex, preallocatedMutexes),
|
|
uint(preallocatedMutexes),
|
|
}
|
|
}
|
|
|
|
// Lock locks the given fingerprint.
|
|
func (l *fingerprintLocker) Lock(fp clientmodel.Fingerprint) {
|
|
l.fpMtxs[uint(fp)%l.numFpMtxs].Lock()
|
|
}
|
|
|
|
// Unlock unlocks the given fingerprint.
|
|
func (l *fingerprintLocker) Unlock(fp clientmodel.Fingerprint) {
|
|
l.fpMtxs[uint(fp)%l.numFpMtxs].Unlock()
|
|
}
|