Skip to content

Commit

Permalink
Merge pull request prometheus#2004 from redbaron/no-false-sharing
Browse files Browse the repository at this point in the history
Avoid having contended mutexes on same cacheline
  • Loading branch information
juliusv authored Sep 18, 2016
2 parents 5f5a78e + bdc5309 commit c9c2663
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions storage/local/locker.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,21 @@ package local

import (
"sync"
"unsafe"

"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
// of mutexes needed for that, only a fixed number of mutexes are
// allocated. Fingerprints to be locked are assigned to those pre-allocated
Expand All @@ -30,7 +41,7 @@ import (
// fingerprint at the same time. (In that case a collision would try to acquire
// the same mutex twice).
type fingerprintLocker struct {
fpMtxs []sync.Mutex
fpMtxs []paddedMutex
numFpMtxs uint
}

Expand All @@ -41,7 +52,7 @@ func newFingerprintLocker(preallocatedMutexes int) *fingerprintLocker {
preallocatedMutexes = 1024
}
return &fingerprintLocker{
make([]sync.Mutex, preallocatedMutexes),
make([]paddedMutex, preallocatedMutexes),
uint(preallocatedMutexes),
}
}
Expand Down

0 comments on commit c9c2663

Please sign in to comment.