Merge pull request #2004 from redbaron/no-false-sharing

Avoid having contended mutexes on same cacheline
pull/2000/head
Julius Volz 8 years ago committed by GitHub
commit c9c2663a54

@ -15,10 +15,21 @@ package local
import ( import (
"sync" "sync"
"unsafe"
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
) )
const (
cacheLineSize = 64
)
// Avoid false sharing when using array of mutexes.
type paddedMutex struct {
sync.Mutex
pad [cacheLineSize - unsafe.Sizeof(sync.Mutex{})]byte
}
// fingerprintLocker allows locking individual fingerprints. To limit the number // fingerprintLocker allows locking individual fingerprints. To limit the number
// of mutexes needed for that, only a fixed number of mutexes are // of mutexes needed for that, only a fixed number of mutexes are
// allocated. Fingerprints to be locked are assigned to those pre-allocated // allocated. Fingerprints to be locked are assigned to those pre-allocated
@ -30,7 +41,7 @@ import (
// fingerprint at the same time. (In that case a collision would try to acquire // fingerprint at the same time. (In that case a collision would try to acquire
// the same mutex twice). // the same mutex twice).
type fingerprintLocker struct { type fingerprintLocker struct {
fpMtxs []sync.Mutex fpMtxs []paddedMutex
numFpMtxs uint numFpMtxs uint
} }
@ -41,7 +52,7 @@ func newFingerprintLocker(preallocatedMutexes int) *fingerprintLocker {
preallocatedMutexes = 1024 preallocatedMutexes = 1024
} }
return &fingerprintLocker{ return &fingerprintLocker{
make([]sync.Mutex, preallocatedMutexes), make([]paddedMutex, preallocatedMutexes),
uint(preallocatedMutexes), uint(preallocatedMutexes),
} }
} }

Loading…
Cancel
Save