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

Avoid having contended mutexes on same cacheline
This commit is contained in:
Julius Volz 2016-09-19 00:40:03 +02:00 committed by GitHub
commit c9c2663a54
1 changed files with 13 additions and 2 deletions

View File

@ -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),
} }
} }